c2pa-rs

A fork of https://github.com/contentauth/c2pa-rs/
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/c2pa-rs.git
Log | Files | Refs | README

validator.rs (3157B)


      1 // Copyright 2022 Adobe. All rights reserved.
      2 // This file is licensed to you under the Apache License,
      3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
      4 // or the MIT license (http://opensource.org/licenses/MIT),
      5 // at your option.
      6 
      7 // Unless required by applicable law or agreed to in writing,
      8 // this software is distributed on an "AS IS" BASIS, WITHOUT
      9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
     10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
     11 // specific language governing permissions and limitations under
     12 // each license.
     13 
     14 use chrono::{DateTime, Utc};
     15 use x509_parser::num_bigint::BigUint;
     16 
     17 #[cfg(feature = "openssl")]
     18 use crate::openssl::{EcValidator, EdValidator, RsaValidator};
     19 use crate::{Result, SigningAlg};
     20 
     21 #[derive(Debug, Default)]
     22 pub struct ValidationInfo {
     23     pub alg: Option<SigningAlg>, // validation algorithm
     24     pub date: Option<DateTime<Utc>>,
     25     pub cert_serial_number: Option<BigUint>,
     26     pub issuer_org: Option<String>,
     27     pub validated: bool,     // claim signature is valid
     28     pub cert_chain: Vec<u8>, // certificate chain used to validate signature
     29     pub revocation_status: Option<bool>,
     30 }
     31 
     32 /// Trait to support validating a signature against the provided data
     33 pub(crate) trait CoseValidator {
     34     /// validate signature "sig" for given "data using provided public key"
     35     #[allow(dead_code)] // this here for wasm builds to pass clippy  (todo: remove)
     36     fn validate(&self, sig: &[u8], data: &[u8], pkey: &[u8]) -> Result<bool>;
     37 }
     38 
     39 pub struct DummyValidator;
     40 impl CoseValidator for DummyValidator {
     41     fn validate(&self, _sig: &[u8], _data: &[u8], _pkey: &[u8]) -> Result<bool> {
     42         println!("This signature verified by DummyValidator.  Results not valid!");
     43         Ok(true)
     44     }
     45 }
     46 
     47 // C2PA Supported Signature type
     48 // • ES256 (ECDSA using P-256 and SHA-256)
     49 // • ES384 (ECDSA using P-384 and SHA-384)
     50 // • ES512 (ECDSA using P-521 and SHA-512)
     51 // • PS256 (RSASSA-PSS using SHA-256 and MGF1 with SHA-256)
     52 // • PS384 (RSASSA-PSS using SHA-384 and MGF1 with SHA-384)
     53 // • PS512 (RSASSA-PSS using SHA-512 and MGF1 with SHA-512)
     54 // • RS256	RSASSA-PKCS1-v1_5 using SHA-256
     55 // • RS384	RSASSA-PKCS1-v1_5 using SHA-384
     56 // • RS512	RSASSA-PKCS1-v1_5 using SHA-512
     57 // • ED25519 Edwards Curve ED25519
     58 
     59 /// return validator for supported C2PA  algorithms
     60 #[cfg(feature = "openssl")]
     61 pub(crate) fn get_validator(alg: SigningAlg) -> Box<dyn CoseValidator> {
     62     match alg {
     63         SigningAlg::Es256 | SigningAlg::Es384 | SigningAlg::Es512 => {
     64             Box::new(EcValidator::new(alg))
     65         }
     66         SigningAlg::Ps256 | SigningAlg::Ps384 | SigningAlg::Ps512 => {
     67             Box::new(RsaValidator::new(alg))
     68         }
     69         // "rs256" => Some(Box::new(RsaValidator::new("rs256"))),
     70         // "rs384" => Some(Box::new(RsaValidator::new("rs384"))),
     71         // "rs512" => Some(Box::new(RsaValidator::new("rs512"))),
     72         SigningAlg::Ed25519 => Box::new(EdValidator::new(alg)),
     73     }
     74 }
     75 
     76 #[cfg(not(feature = "openssl"))]
     77 #[allow(dead_code)]
     78 pub(crate) fn get_validator(_alg: SigningAlg) -> Box<dyn CoseValidator> {
     79     Box::new(DummyValidator)
     80 }