From d0a4f8e9e0c44044e56336a58531b76920957010 Mon Sep 17 00:00:00 2001 From: Bernd Krietenstein Date: Tue, 5 Dec 2023 08:48:47 +0100 Subject: [PATCH] Changed `PwriEncryptor` return types to Result<> --- cms/src/builder.rs | 8 ++++---- cms/tests/builder.rs | 32 +++++++++++++++----------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/cms/src/builder.rs b/cms/src/builder.rs index 81b33ec8b..2c0ad2376 100644 --- a/cms/src/builder.rs +++ b/cms/src/builder.rs @@ -714,10 +714,10 @@ pub trait PwriEncryptor { /// Returns the algorithm identifier of the used key derivation algorithm, /// which is used to derive an encryption key from the secret/password /// shared with the recipient. Includes eventual parameters (e.g. the used iv). - fn key_derivation_algorithm(&self) -> Option; + fn key_derivation_algorithm(&self) -> Result>; /// Returns the algorithm identifier of the used encryption algorithm /// including eventual parameters (e.g. the used iv). - fn key_encryption_algorithm(&self) -> AlgorithmIdentifierOwned; + fn key_encryption_algorithm(&self) -> Result; /// Encrypt the wrapped content-encryption key twice following RFC 3211, ยง 2.3.1 fn encrypt_rfc3211(&self, wrapped_content_encryption_key: &[u8]) -> Result>; } @@ -757,8 +757,8 @@ where /// `rng`: Random number generator, required for padding values. pub fn new(key_encryptor: P, rng: &'r mut R) -> Result> { Ok(PasswordRecipientInfoBuilder { - key_derivation_alg: key_encryptor.key_derivation_algorithm(), - key_enc_alg: key_encryptor.key_encryption_algorithm(), + key_derivation_alg: key_encryptor.key_derivation_algorithm()?, + key_enc_alg: key_encryptor.key_encryption_algorithm()?, key_encryptor, rng, }) diff --git a/cms/tests/builder.rs b/cms/tests/builder.rs index bd74ba899..ba9df545c 100644 --- a/cms/tests/builder.rs +++ b/cms/tests/builder.rs @@ -6,7 +6,7 @@ use cipher::{BlockDecryptMut, BlockEncryptMut, Iv, KeyIvInit}; use cms::builder::{ create_signing_time_attribute, ContentEncryptionAlgorithm, EnvelopedDataBuilder, KeyEncryptionInfo, KeyTransRecipientInfoBuilder, PasswordRecipientInfoBuilder, PwriEncryptor, - SignedDataBuilder, SignerInfoBuilder, + Result, SignedDataBuilder, SignerInfoBuilder, }; use cms::cert::{CertificateChoices, IssuerAndSerialNumber}; use cms::content_info::ContentInfo; @@ -634,26 +634,24 @@ fn test_create_password_recipient_info() { Ok(encryptor.encrypt_padded_vec_mut::(tmp.as_slice())) } - fn key_derivation_algorithm(&self) -> Option { - Some(AlgorithmIdentifierOwned { + fn key_derivation_algorithm(&self) -> Result> { + Ok(Some(AlgorithmIdentifierOwned { oid: const_oid::db::rfc5911::ID_PBKDF_2, - parameters: Some( - Any::new( - der::Tag::Sequence, - self.key_derivation_params.to_der().unwrap(), - ) - .unwrap(), - ), - }) + parameters: Some(Any::new( + der::Tag::Sequence, + self.key_derivation_params.to_der()?, + )?), + })) } - fn key_encryption_algorithm(&self) -> AlgorithmIdentifierOwned { - AlgorithmIdentifierOwned { + fn key_encryption_algorithm(&self) -> Result { + Ok(AlgorithmIdentifierOwned { oid: const_oid::db::rfc5911::ID_AES_128_CBC, - parameters: Some( - Any::new(der::Tag::OctetString, self.key_encryption_iv.to_vec()).unwrap(), - ), - } + parameters: Some(Any::new( + der::Tag::OctetString, + self.key_encryption_iv.to_vec(), + )?), + }) } }