Skip to content

Commit 10105af

Browse files
committed
Add support for modulus/exponent
1 parent b862726 commit 10105af

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# 7.0.0 (unreleased)
44

55
- Add support for PS256, PS384 and PS512
6+
- Add support for verifying with modulus/exponent components for RSA
67
- Change API for both sign/verify to take a `Key` enum rather than bytes
78
- Update to 2018 edition
89

src/crypto.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,33 @@ fn verify_ring_es(
123123
}
124124

125125
fn verify_ring_rsa(
126-
alg: &dyn signature::VerificationAlgorithm,
126+
alg: &signature::RsaParameters,
127127
signature: &str,
128128
signing_input: &str,
129129
key: Key,
130130
) -> Result<bool> {
131-
let bytes = match key {
132-
Key::Der(bytes) | Key::Pkcs8(bytes) => bytes,
131+
match key {
132+
Key::Der(bytes) | Key::Pkcs8(bytes) => verify_ring(alg, signature, signing_input, bytes),
133+
Key::ModulusExponent(n, e) => {
134+
let signature_bytes = base64::decode_config(signature, base64::URL_SAFE_NO_PAD)?;
135+
let message = untrusted::Input::from(signing_input.as_bytes());
136+
let modulus = untrusted::Input::from(n);
137+
let exponent = untrusted::Input::from(e);
138+
let expected_signature = untrusted::Input::from(signature_bytes.as_slice());
139+
140+
let res = signature::primitive::verify_rsa(
141+
alg,
142+
(modulus, exponent),
143+
message,
144+
expected_signature,
145+
);
146+
147+
Ok(res.is_ok())
148+
}
133149
_ => {
134-
return Err(ErrorKind::InvalidKeyFormat)?;
150+
Err(ErrorKind::InvalidKeyFormat)?
135151
}
136-
};
137-
verify_ring(alg, signature, signing_input, bytes)
152+
}
138153
}
139154

140155
/// Compares the signature given with a re-computed signature for HMAC or using the public key

src/keys.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ pub enum Key<'a> {
1010
/// This is not a key format, but provided for convenience since HMAC is
1111
/// a supported signing algorithm.
1212
Hmac(&'a [u8]),
13+
/// A Modulus/exponent for a RSA public key
14+
ModulusExponent(&'a [u8], &'a [u8]),
1315
}

tests/rsa.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,37 @@ fn fails_with_different_key_format() {
6464
}
6565

6666
#[test]
67-
fn can_decode_jwt_io_example() {}
67+
fn rsa_modulus_exponent() {
68+
let modulus: Vec<u8> = vec![
69+
0xc9, 0x11, 0x3a, 0xac, 0x7b, 0x8d, 0x47, 0x44, 0x1b, 0x1c, 0xed, 0xc7, 0xdc, 0xab, 0x76,
70+
0xa4, 0xe2, 0x86, 0x56, 0x14, 0x2a, 0x19, 0x95, 0xc8, 0x9c, 0xe7, 0x6e, 0x40, 0xdc, 0x57,
71+
0xce, 0xe2, 0xa5, 0xbd, 0x04, 0xcb, 0x51, 0x3b, 0xf8, 0x97, 0x8b, 0x20, 0x82, 0x1e, 0x7f,
72+
0x09, 0x86, 0x22, 0xfd, 0xcb, 0xc8, 0xf9, 0x25, 0xd5, 0x4f, 0xd9, 0x0f, 0x59, 0x22, 0x97,
73+
0xc4, 0x95, 0xc1, 0x5d, 0xdf, 0xf8, 0x2e, 0x4b, 0xdc, 0x3e, 0xe5, 0x1a, 0x90, 0x1a, 0x00,
74+
0x91, 0xf8, 0x7e, 0x7a, 0x21, 0x55, 0x32, 0x1d, 0x95, 0xad, 0x4c, 0x96, 0xca, 0x3d, 0xcc,
75+
0x16, 0x5d, 0x07, 0x4d, 0x51, 0x7d, 0x2b, 0x04, 0x57, 0x2c, 0x07, 0x30, 0x91, 0x11, 0x22,
76+
0x4b, 0x79, 0xe9, 0x4e, 0x11, 0xd1, 0xc8, 0x8c, 0x6e, 0xcb, 0x46, 0x4c, 0x79, 0x97, 0xf1,
77+
0x54, 0xbe, 0x5a, 0xac, 0xc8, 0x70, 0xd5, 0x24, 0x44, 0x2c, 0x1f, 0x07, 0xa0, 0x67, 0xc6,
78+
0xfc, 0x0b, 0x47, 0xf3, 0xd0, 0x48, 0x13, 0xd8, 0xc3, 0x04, 0x76, 0x7d, 0x74, 0xb7, 0xa5,
79+
0x2b, 0xd6, 0xb5, 0xf3, 0x8c, 0xc0, 0x7f, 0xc2, 0xf0, 0xa0, 0xf2, 0xf1, 0xbc, 0x96, 0xf7,
80+
0x22, 0x5e, 0x67, 0x9d, 0xca, 0x8f, 0x71, 0x27, 0xca, 0x0c, 0x3a, 0x1d, 0x30, 0x50, 0x48,
81+
0x31, 0xce, 0x25, 0x43, 0x30, 0xca, 0x2f, 0x98, 0x2f, 0x9a, 0x25, 0xcb, 0x5c, 0x1d, 0x40,
82+
0x18, 0xb9, 0xbc, 0x28, 0x18, 0xdf, 0x13, 0xcb, 0x37, 0x2f, 0x9c, 0x6a, 0x8b, 0xec, 0x94,
83+
0xa1, 0xdf, 0xa3, 0xf0, 0xcb, 0x6f, 0x22, 0x3f, 0x35, 0xd9, 0xd9, 0x12, 0xe1, 0x03, 0x22,
84+
0x45, 0x53, 0x7f, 0x6f, 0x2d, 0xa1, 0xdd, 0x96, 0x3c, 0x2d, 0x85, 0x46, 0xae, 0xa6, 0x57,
85+
0x65, 0x37, 0x20, 0x9f, 0x6b, 0xa3, 0x9f, 0xcb, 0x8a, 0x8d, 0x72, 0xd9, 0x54, 0x3e, 0x53,
86+
0x75,
87+
];
88+
let exponent: Vec<u8> = vec![0x01, 0x00, 0x01];
89+
let privkey = include_bytes!("private_rsa_key.der");
90+
91+
let encrypted = sign("hello world", Key::Der(&privkey[..]), Algorithm::RS256).unwrap();
92+
let is_valid = verify(
93+
&encrypted,
94+
"hello world",
95+
Key::ModulusExponent(&modulus, &exponent),
96+
Algorithm::RS256,
97+
)
98+
.unwrap();
99+
assert!(is_valid);
100+
}

0 commit comments

Comments
 (0)