File tree Expand file tree Collapse file tree 3 files changed +17
-14
lines changed Expand file tree Collapse file tree 3 files changed +17
-14
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ encryption = [
7676 " rand_core"
7777]
7878getrandom = [" rand_core/getrandom" ]
79+ hazmat-allow-insecure-rsa-keys = []
7980p256 = [" dep:p256" , " ecdsa" ]
8081p384 = [" dep:p384" , " ecdsa" ]
8182p521 = [" dep:p521" , " ecdsa" ]
Original file line number Diff line number Diff line change @@ -138,17 +138,17 @@ pub struct RsaKeypair {
138138
139139impl RsaKeypair {
140140 /// Minimum allowed RSA key size.
141- #[ cfg( feature = "rsa" ) ]
141+ #[ cfg( all ( feature = "rsa" , not ( feature = "hazmat-allow-insecure-rsa-keys" ) ) ) ]
142142 pub ( crate ) const MIN_KEY_SIZE : usize = 2048 ;
143143
144144 /// Generate a random RSA keypair of the given size.
145145 #[ cfg( feature = "rsa" ) ]
146146 pub fn random ( rng : & mut impl CryptoRngCore , bit_size : usize ) -> Result < Self > {
147- if bit_size >= Self :: MIN_KEY_SIZE {
148- rsa:: RsaPrivateKey :: new ( rng, bit_size) ?. try_into ( )
149- } else {
150- Err ( Error :: Crypto )
147+ #[ cfg( not( feature = "hazmat-allow-insecure-rsa-keys" ) ) ]
148+ if bit_size < Self :: MIN_KEY_SIZE {
149+ return Err ( Error :: Crypto ) ;
151150 }
151+ rsa:: RsaPrivateKey :: new ( rng, bit_size) ?. try_into ( )
152152 }
153153
154154 /// Create a new keypair from the given `public` and `private` key components.
@@ -260,11 +260,12 @@ impl TryFrom<&RsaKeypair> for rsa::RsaPrivateKey {
260260 ] ,
261261 ) ?;
262262
263- if ret. size ( ) . saturating_mul ( 8 ) >= RsaKeypair :: MIN_KEY_SIZE {
264- Ok ( ret)
265- } else {
266- Err ( Error :: Crypto )
263+ #[ cfg( not( feature = "hazmat-allow-insecure-rsa-keys" ) ) ]
264+ if ret. size ( ) . saturating_mul ( 8 ) < RsaKeypair :: MIN_KEY_SIZE {
265+ return Err ( Error :: Crypto ) ;
267266 }
267+
268+ Ok ( ret)
268269 }
269270}
270271
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ pub struct RsaPublicKey {
2828
2929impl RsaPublicKey {
3030 /// Minimum allowed RSA key size.
31- #[ cfg( feature = "rsa" ) ]
31+ #[ cfg( all ( feature = "rsa" , not ( feature = "hazmat-allow-insecure-rsa-keys" ) ) ) ]
3232 pub ( crate ) const MIN_KEY_SIZE : usize = RsaKeypair :: MIN_KEY_SIZE ;
3333
3434 /// Create a new [`RsaPublicKey`] with the given components:
@@ -117,11 +117,12 @@ impl TryFrom<&RsaPublicKey> for rsa::RsaPublicKey {
117117 )
118118 . map_err ( |_| Error :: Crypto ) ?;
119119
120- if ret. size ( ) . saturating_mul ( 8 ) >= RsaPublicKey :: MIN_KEY_SIZE {
121- Ok ( ret)
122- } else {
123- Err ( Error :: Crypto )
120+ #[ cfg( not( feature = "hazmat-allow-insecure-rsa-keys" ) ) ]
121+ if ret. size ( ) . saturating_mul ( 8 ) < RsaPublicKey :: MIN_KEY_SIZE {
122+ return Err ( Error :: Crypto ) ;
124123 }
124+
125+ Ok ( ret)
125126 }
126127}
127128
You can’t perform that action at this time.
0 commit comments