1- #! [ cfg ( feature = "builder" ) ]
1+ //! Cryptographic Message Syntax Builder
22
3- //! CMS Builder
3+ #! [ cfg ( feature = "builder" ) ]
44
5- use crate :: cert:: CertificateChoices ;
6- use crate :: content_info:: { CmsVersion , ContentInfo } ;
7- use crate :: enveloped_data:: {
8- EncryptedContentInfo , EncryptedKey , EnvelopedData , KekIdentifier , KeyTransRecipientInfo ,
9- OriginatorInfo , PasswordRecipientInfo , RecipientIdentifier , RecipientInfo , RecipientInfos ,
10- UserKeyingMaterial ,
11- } ;
12- use crate :: revocation:: { RevocationInfoChoice , RevocationInfoChoices } ;
13- use crate :: signed_data:: {
14- CertificateSet , DigestAlgorithmIdentifiers , EncapsulatedContentInfo , SignatureValue ,
15- SignedAttributes , SignedData , SignerIdentifier , SignerInfo , SignerInfos , UnsignedAttributes ,
5+ use crate :: {
6+ cert:: CertificateChoices ,
7+ content_info:: { CmsVersion , ContentInfo } ,
8+ enveloped_data:: {
9+ EncryptedContentInfo , EncryptedKey , EnvelopedData , KekIdentifier , KeyTransRecipientInfo ,
10+ OriginatorInfo , PasswordRecipientInfo , RecipientIdentifier , RecipientInfo , RecipientInfos ,
11+ UserKeyingMaterial ,
12+ } ,
13+ revocation:: { RevocationInfoChoice , RevocationInfoChoices } ,
14+ signed_data:: {
15+ CertificateSet , DigestAlgorithmIdentifiers , EncapsulatedContentInfo , SignatureValue ,
16+ SignedAttributes , SignedData , SignerIdentifier , SignerInfo , SignerInfos ,
17+ UnsignedAttributes ,
18+ } ,
1619} ;
1720use aes:: { Aes128 , Aes192 , Aes256 } ;
18- use alloc:: borrow:: ToOwned ;
19- use alloc:: boxed:: Box ;
20- use alloc:: string:: { String , ToString } ;
21- use alloc:: vec:: Vec ;
21+ use alloc:: {
22+ borrow:: ToOwned ,
23+ boxed:: Box ,
24+ string:: { String , ToString } ,
25+ vec,
26+ vec:: Vec ,
27+ } ;
2228use cipher:: {
23- BlockModeEncrypt , Key , KeyIvInit , KeySizeUser , block_padding:: Pkcs7 , rand_core:: CryptoRng ,
29+ BlockModeEncrypt , Iv , Key , KeyIvInit , block_padding:: Pkcs7 , crypto_common:: Generate ,
30+ rand_core:: CryptoRng ,
2431} ;
2532use const_oid:: ObjectIdentifier ;
26- use core:: cmp:: Ordering ;
27- use core :: fmt ;
28- use core :: marker :: PhantomData ;
29- use der :: asn1:: { BitString , Null , OctetString , OctetStringRef , SetOfVec } ;
30- use der :: oid:: db:: DB ;
31- use der :: { Any , AnyRef , Decode , Encode , ErrorKind , Tag } ;
33+ use core:: { cmp:: Ordering , fmt , marker :: PhantomData } ;
34+ use der :: {
35+ Any , AnyRef , Decode , Encode , ErrorKind , Tag ,
36+ asn1:: { BitString , Null , OctetString , OctetStringRef , SetOfVec } ,
37+ oid:: db:: DB ,
38+ } ;
3239use digest:: Digest ;
3340use rsa:: Pkcs1v15Encrypt ;
3441use sha2:: digest;
@@ -39,7 +46,6 @@ use spki::{
3946 AlgorithmIdentifierOwned , DynSignatureAlgorithmIdentifier , EncodePublicKey ,
4047 SignatureBitStringEncoding ,
4148} ;
42- use std:: vec;
4349use x509_cert:: {
4450 attr:: { Attribute , AttributeValue , Attributes } ,
4551 builder:: { self , AsyncBuilder , Builder } ,
@@ -1172,18 +1178,16 @@ fn get_hasher(
11721178macro_rules! encrypt_block_mode {
11731179 ( $data: expr, $block_mode: ident:: $typ: ident<$alg: ident>, $key: expr, $rng: expr, $oid: expr) => { {
11741180 let ( key, iv) = match $key {
1175- None => $block_mode:: $typ:: <$alg>:: generate_key_iv_with_rng( $rng) ,
1181+ None => {
1182+ let key = Key :: <$block_mode:: $typ<$alg>>:: generate_from_rng( $rng) ;
1183+ let iv = Iv :: <$block_mode:: $typ<$alg>>:: generate_from_rng( $rng) ;
1184+ ( key, iv)
1185+ }
11761186 Some ( key) => {
1177- if key. len( ) != $alg:: key_size( ) {
1178- return Err ( Error :: Builder ( String :: from(
1179- "Invalid key size for chosen algorithm" ,
1180- ) ) ) ;
1181- }
1182- (
1183- Key :: <$block_mode:: $typ<$alg>>:: try_from( key)
1184- . expect( "size invariants violation" ) ,
1185- $block_mode:: $typ:: <$alg>:: generate_iv_with_rng( $rng) ,
1186- )
1187+ let key = Key :: <$block_mode:: $typ<$alg>>:: try_from( key)
1188+ . map_err( |_| Error :: Builder ( "invalid key size for chosen algorithm" . into( ) ) ) ?;
1189+ let iv = Iv :: <$block_mode:: $typ<$alg>>:: generate_from_rng( $rng) ;
1190+ ( key, iv)
11871191 }
11881192 } ;
11891193 let encryptor = $block_mode:: $typ:: <$alg>:: new( & key. into( ) , & iv. into( ) ) ;
0 commit comments