create_signer.rs (2982B)
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 //! The `create_signer` module provides a way to obtain a [`Signer`] 17 //! instance for each signing format supported by this crate. 18 #[cfg(feature = "file_io")] 19 use std::path::Path; 20 21 use crate::{ 22 error::Result, 23 openssl::{EcSigner, EdSigner, RsaSigner}, 24 signer::ConfigurableSigner, 25 Signer, SigningAlg, 26 }; 27 28 /// Creates a [`Signer`] instance using signing certificate and private key 29 /// as byte slices. 30 /// 31 /// The signing certificate and private key are passed to the underlying 32 /// C++ code, which copies them into its own storage. 33 /// 34 /// # Arguments 35 /// 36 /// * `signcert` - Signing certificate 37 /// * `pkey` - Private key 38 /// * `alg` - Format for signing 39 /// * `tsa_url` - Optional URL for a timestamp authority 40 pub fn from_keys( 41 signcert: &[u8], 42 pkey: &[u8], 43 alg: SigningAlg, 44 tsa_url: Option<String>, 45 ) -> Result<Box<dyn Signer>> { 46 Ok(match alg { 47 SigningAlg::Ps256 | SigningAlg::Ps384 | SigningAlg::Ps512 => Box::new( 48 RsaSigner::from_signcert_and_pkey(signcert, pkey, alg, tsa_url)?, 49 ), 50 SigningAlg::Es256 | SigningAlg::Es384 | SigningAlg::Es512 => Box::new( 51 EcSigner::from_signcert_and_pkey(signcert, pkey, alg, tsa_url)?, 52 ), 53 SigningAlg::Ed25519 => Box::new(EdSigner::from_signcert_and_pkey( 54 signcert, pkey, alg, tsa_url, 55 )?), 56 }) 57 } 58 59 /// Creates a [`Signer`] instance using signing certificate and 60 /// private key files. 61 /// 62 /// # Arguments 63 /// 64 /// * `signcert_path` - Path to the signing certificate file 65 /// * `pkey_path` - Path to the private key file 66 /// * `alg` - Format for signing 67 /// * `tsa_url` - Optional URL for a timestamp authority 68 #[cfg(feature = "file_io")] 69 pub fn from_files<P: AsRef<Path>>( 70 signcert_path: P, 71 pkey_path: P, 72 alg: SigningAlg, 73 tsa_url: Option<String>, 74 ) -> Result<Box<dyn Signer>> { 75 Ok(match alg { 76 SigningAlg::Ps256 | SigningAlg::Ps384 | SigningAlg::Ps512 => Box::new( 77 RsaSigner::from_files(&signcert_path, &pkey_path, alg, tsa_url)?, 78 ), 79 SigningAlg::Es256 | SigningAlg::Es384 | SigningAlg::Es512 => Box::new( 80 EcSigner::from_files(&signcert_path, &pkey_path, alg, tsa_url)?, 81 ), 82 SigningAlg::Ed25519 => Box::new(EdSigner::from_files( 83 &signcert_path, 84 &pkey_path, 85 alg, 86 tsa_url, 87 )?), 88 }) 89 }