Skip to content

Commit 44c92c8

Browse files
committed
ssh-key: add a crate feature to allow insecure RSA keys - fixes #336
1 parent 0b0d51a commit 44c92c8

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

ssh-key/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ encryption = [
7676
"rand_core"
7777
]
7878
getrandom = ["rand_core/getrandom"]
79+
hazmat-allow-insecure-rsa-keys = []
7980
p256 = ["dep:p256", "ecdsa"]
8081
p384 = ["dep:p384", "ecdsa"]
8182
p521 = ["dep:p521", "ecdsa"]

ssh-key/src/private/rsa.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,17 @@ pub struct RsaKeypair {
138138

139139
impl 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

ssh-key/src/public/rsa.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct RsaPublicKey {
2828

2929
impl 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

0 commit comments

Comments
 (0)