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

temp_signer_async.rs (3002B)


      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 #![deny(missing_docs)]
     15 
     16 //! Temporary async signing instances for testing purposes.
     17 //!
     18 //! This is only a demonstration async Signer that is used to test
     19 //! the asynchronous signing of claims.
     20 //! This module should be used only for testing purposes.
     21 
     22 #[cfg(feature = "openssl_sign")]
     23 use crate::SigningAlg;
     24 
     25 #[cfg(feature = "openssl_sign")]
     26 fn get_local_signer(alg: SigningAlg) -> Box<dyn crate::Signer> {
     27     let cert_dir = crate::utils::test::fixture_path("certs");
     28 
     29     match alg {
     30         SigningAlg::Ps256 | SigningAlg::Ps384 | SigningAlg::Ps512 => {
     31             let (s, _k) = super::temp_signer::get_rsa_signer(&cert_dir, alg, None);
     32             Box::new(s)
     33         }
     34         SigningAlg::Es256 | SigningAlg::Es384 | SigningAlg::Es512 => {
     35             let (s, _k) = super::temp_signer::get_ec_signer(&cert_dir, alg, None);
     36             Box::new(s)
     37         }
     38         SigningAlg::Ed25519 => {
     39             let (s, _k) = super::temp_signer::get_ed_signer(&cert_dir, alg, None);
     40             Box::new(s)
     41         }
     42     }
     43 }
     44 
     45 #[cfg(feature = "openssl_sign")]
     46 pub struct AsyncSignerAdapter {
     47     alg: SigningAlg,
     48     certs: Vec<Vec<u8>>,
     49     reserve_size: usize,
     50     tsa_url: Option<String>,
     51     ocsp_val: Option<Vec<u8>>,
     52 }
     53 
     54 #[cfg(feature = "openssl_sign")]
     55 impl AsyncSignerAdapter {
     56     pub fn new(alg: SigningAlg) -> Self {
     57         let signer = get_local_signer(alg);
     58 
     59         AsyncSignerAdapter {
     60             alg,
     61             certs: signer.certs().unwrap_or_default(),
     62             reserve_size: signer.reserve_size(),
     63             tsa_url: signer.time_authority_url(),
     64             ocsp_val: signer.ocsp_val(),
     65         }
     66     }
     67 }
     68 
     69 #[cfg(test)]
     70 #[cfg(feature = "openssl_sign")]
     71 #[async_trait::async_trait]
     72 impl crate::AsyncSigner for AsyncSignerAdapter {
     73     async fn sign(&self, data: Vec<u8>) -> crate::error::Result<Vec<u8>> {
     74         let signer = get_local_signer(self.alg);
     75         signer.sign(&data)
     76     }
     77 
     78     fn alg(&self) -> SigningAlg {
     79         self.alg
     80     }
     81 
     82     fn certs(&self) -> crate::Result<Vec<Vec<u8>>> {
     83         let mut output: Vec<Vec<u8>> = Vec::new();
     84         for v in &self.certs {
     85             output.push(v.clone());
     86         }
     87         Ok(output)
     88     }
     89 
     90     fn reserve_size(&self) -> usize {
     91         self.reserve_size
     92     }
     93 
     94     fn time_authority_url(&self) -> Option<String> {
     95         self.tsa_url.clone()
     96     }
     97 
     98     async fn ocsp_val(&self) -> Option<Vec<u8>> {
     99         self.ocsp_val.clone()
    100     }
    101 }