Skip to content

Commit 2edd479

Browse files
fix: always validate keys in from_components
Otherwise the inner precompute could fail
1 parent 201ad81 commit 2edd479

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/key.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ impl RsaPrivateKey {
251251
d: BigUint,
252252
mut primes: Vec<BigUint>,
253253
) -> Result<RsaPrivateKey> {
254-
let mut should_validate = false;
255254
if primes.len() < 2 {
256255
if !primes.is_empty() {
257256
return Err(Error::NprimesTooSmall);
@@ -261,7 +260,6 @@ impl RsaPrivateKey {
261260
let (p, q) = recover_primes(&n, &e, &d)?;
262261
primes.push(p);
263262
primes.push(q);
264-
should_validate = true;
265263
}
266264

267265
let mut k = RsaPrivateKey {
@@ -271,10 +269,8 @@ impl RsaPrivateKey {
271269
precomputed: None,
272270
};
273271

274-
// Validate the key if we had to recover the primes.
275-
if should_validate {
276-
k.validate()?;
277-
}
272+
// Alaways validate the key, to ensure precompute can't fail
273+
k.validate()?;
278274

279275
// precompute when possible, ignore error otherwise.
280276
let _ = k.precompute();
@@ -717,13 +713,13 @@ mod tests {
717713
Base64::decode_vec("CUWC+hRWOT421kwRllgVjy6FYv6jQUcgDNHeAiYZnf5HjS9iK2ki7v8G5dL/0f+Yf+NhE/4q8w4m8go51hACrVpP1p8GJDjiT09+RsOzITsHwl+ceEKoe56ZW6iDHBLlrNw5/MtcYhKpjNU9KJ2udm5J/c9iislcjgckrZG2IB8ADgXHMEByZ5DgaMl4AKZ1Gx8/q6KftTvmOT5rNTMLi76VN5KWQcDWK/DqXiOiZHM7Nr4dX4me3XeRgABJyNR8Fqxj3N1+HrYLe/zs7LOaK0++F9Ul3tLelhrhsvLxei3oCZkF9A/foD3on3luYA+1cRcxWpSY3h2J4/22+yo4+Q==").unwrap(),
718714
];
719715

720-
RsaPrivateKey::from_components(
716+
let res = RsaPrivateKey::from_components(
721717
BigUint::from_bytes_be(&n),
722718
BigUint::from_bytes_be(&e),
723719
BigUint::from_bytes_be(&d),
724720
primes.iter().map(|p| BigUint::from_bytes_be(p)).collect(),
725-
)
726-
.unwrap();
721+
);
722+
assert_eq!(res, Err(Error::InvalidModulus));
727723
}
728724

729725
#[test]

0 commit comments

Comments
 (0)