Add support for modulus/exponent

This commit is contained in:
Vincent Prouillet 2019-07-13 17:43:44 +02:00
parent b8627260b2
commit 10105af2fd
4 changed files with 59 additions and 8 deletions

View File

@ -3,6 +3,7 @@
# 7.0.0 (unreleased)
- Add support for PS256, PS384 and PS512
- Add support for verifying with modulus/exponent components for RSA
- Change API for both sign/verify to take a `Key` enum rather than bytes
- Update to 2018 edition

View File

@ -123,18 +123,33 @@ fn verify_ring_es(
}
fn verify_ring_rsa(
alg: &dyn signature::VerificationAlgorithm,
alg: &signature::RsaParameters,
signature: &str,
signing_input: &str,
key: Key,
) -> Result<bool> {
let bytes = match key {
Key::Der(bytes) | Key::Pkcs8(bytes) => bytes,
_ => {
return Err(ErrorKind::InvalidKeyFormat)?;
match key {
Key::Der(bytes) | Key::Pkcs8(bytes) => verify_ring(alg, signature, signing_input, bytes),
Key::ModulusExponent(n, e) => {
let signature_bytes = base64::decode_config(signature, base64::URL_SAFE_NO_PAD)?;
let message = untrusted::Input::from(signing_input.as_bytes());
let modulus = untrusted::Input::from(n);
let exponent = untrusted::Input::from(e);
let expected_signature = untrusted::Input::from(signature_bytes.as_slice());
let res = signature::primitive::verify_rsa(
alg,
(modulus, exponent),
message,
expected_signature,
);
Ok(res.is_ok())
}
};
verify_ring(alg, signature, signing_input, bytes)
_ => {
Err(ErrorKind::InvalidKeyFormat)?
}
}
}
/// Compares the signature given with a re-computed signature for HMAC or using the public key

View File

@ -10,4 +10,6 @@ pub enum Key<'a> {
/// This is not a key format, but provided for convenience since HMAC is
/// a supported signing algorithm.
Hmac(&'a [u8]),
/// A Modulus/exponent for a RSA public key
ModulusExponent(&'a [u8], &'a [u8]),
}

View File

@ -64,4 +64,37 @@ fn fails_with_different_key_format() {
}
#[test]
fn can_decode_jwt_io_example() {}
fn rsa_modulus_exponent() {
let modulus: Vec<u8> = vec![
0xc9, 0x11, 0x3a, 0xac, 0x7b, 0x8d, 0x47, 0x44, 0x1b, 0x1c, 0xed, 0xc7, 0xdc, 0xab, 0x76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, 0x18, 0xdf, 0x13, 0xcb, 0x37, 0x2f, 0x9c, 0x6a, 0x8b, 0xec, 0x94,
0xa1, 0xdf, 0xa3, 0xf0, 0xcb, 0x6f, 0x22, 0x3f, 0x35, 0xd9, 0xd9, 0x12, 0xe1, 0x03, 0x22,
0x45, 0x53, 0x7f, 0x6f, 0x2d, 0xa1, 0xdd, 0x96, 0x3c, 0x2d, 0x85, 0x46, 0xae, 0xa6, 0x57,
0x65, 0x37, 0x20, 0x9f, 0x6b, 0xa3, 0x9f, 0xcb, 0x8a, 0x8d, 0x72, 0xd9, 0x54, 0x3e, 0x53,
0x75,
];
let exponent: Vec<u8> = vec![0x01, 0x00, 0x01];
let privkey = include_bytes!("private_rsa_key.der");
let encrypted = sign("hello world", Key::Der(&privkey[..]), Algorithm::RS256).unwrap();
let is_valid = verify(
&encrypted,
"hello world",
Key::ModulusExponent(&modulus, &exponent),
Algorithm::RS256,
)
.unwrap();
assert!(is_valid);
}