Skip to content

Commit

Permalink
Changed PwriEncryptor return types to Result<>
Browse files Browse the repository at this point in the history
  • Loading branch information
bkstein committed Dec 5, 2023
1 parent 7b23a0f commit d0a4f8e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
8 changes: 4 additions & 4 deletions cms/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlgorithmIdentifierOwned>;
fn key_derivation_algorithm(&self) -> Result<Option<AlgorithmIdentifierOwned>>;
/// 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<AlgorithmIdentifierOwned>;
/// Encrypt the wrapped content-encryption key twice following RFC 3211, § 2.3.1
fn encrypt_rfc3211(&self, wrapped_content_encryption_key: &[u8]) -> Result<Vec<u8>>;
}
Expand Down Expand Up @@ -757,8 +757,8 @@ where
/// `rng`: Random number generator, required for padding values.
pub fn new(key_encryptor: P, rng: &'r mut R) -> Result<PasswordRecipientInfoBuilder<'r, P, R>> {
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,
})
Expand Down
32 changes: 15 additions & 17 deletions cms/tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -634,26 +634,24 @@ fn test_create_password_recipient_info() {
Ok(encryptor.encrypt_padded_vec_mut::<Pkcs7>(tmp.as_slice()))
}

fn key_derivation_algorithm(&self) -> Option<AlgorithmIdentifierOwned> {
Some(AlgorithmIdentifierOwned {
fn key_derivation_algorithm(&self) -> Result<Option<AlgorithmIdentifierOwned>> {
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<AlgorithmIdentifierOwned> {
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(),
)?),
})
}
}

Expand Down

0 comments on commit d0a4f8e

Please sign in to comment.