2018-07-25 09:42:00 -04:00
|
|
|
use chrono::Utc;
|
2019-11-03 07:22:51 -05:00
|
|
|
use jsonwebtoken::{decode, decode_pem, encode, sign, verify, Algorithm, Header, Key, Validation};
|
2019-11-03 10:36:19 -05:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2017-02-22 02:45:28 -05:00
|
|
|
|
2019-06-07 14:08:54 -04:00
|
|
|
const RSA_ALGORITHMS: &[Algorithm] = &[
|
|
|
|
Algorithm::RS256,
|
|
|
|
Algorithm::RS384,
|
|
|
|
Algorithm::RS512,
|
|
|
|
Algorithm::PS256,
|
|
|
|
Algorithm::PS384,
|
|
|
|
Algorithm::PS512,
|
|
|
|
];
|
2017-02-22 02:45:28 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
2019-07-06 14:36:32 -04:00
|
|
|
pub struct Claims {
|
2017-02-22 02:45:28 -05:00
|
|
|
sub: String,
|
2018-07-25 09:42:00 -04:00
|
|
|
company: String,
|
|
|
|
exp: i64,
|
2017-02-22 02:45:28 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 23:40:01 -04:00
|
|
|
#[test]
|
2019-11-03 07:22:51 -05:00
|
|
|
fn round_trip_sign_verification_der() {
|
2019-05-15 10:20:25 -04:00
|
|
|
let privkey = include_bytes!("private_rsa_key.der");
|
2019-06-07 14:08:54 -04:00
|
|
|
for &alg in RSA_ALGORITHMS {
|
2019-06-16 12:00:00 -04:00
|
|
|
let encrypted = sign("hello world", Key::Der(&privkey[..]), alg).unwrap();
|
2019-06-07 14:08:54 -04:00
|
|
|
let is_valid =
|
2019-06-16 12:00:00 -04:00
|
|
|
verify(&encrypted, "hello world", Key::Der(include_bytes!("public_rsa_key.der")), alg)
|
|
|
|
.unwrap();
|
2019-06-07 14:08:54 -04:00
|
|
|
assert!(is_valid);
|
2019-11-03 07:22:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_sign_verification_pem_pkcs1() {
|
|
|
|
let privkey_pem = decode_pem(include_str!("private_rsa_key_pkcs1.pem")).unwrap();
|
|
|
|
let pubkey_pem = decode_pem(include_str!("public_rsa_key_pkcs1.pem")).unwrap();
|
|
|
|
|
|
|
|
for &alg in RSA_ALGORITHMS {
|
|
|
|
let privkey_key = privkey_pem.as_key().unwrap();
|
|
|
|
let pubkey_key = pubkey_pem.as_key().unwrap();
|
|
|
|
let encrypted = sign("hello world", privkey_key, alg).unwrap();
|
|
|
|
let is_valid = verify(&encrypted, "hello world", pubkey_key, alg).unwrap();
|
|
|
|
assert!(is_valid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_sign_verification_pem_pkcs8() {
|
|
|
|
let privkey_pem = decode_pem(include_str!("private_rsa_key_pkcs8.pem")).unwrap();
|
|
|
|
let pubkey_pem = decode_pem(include_str!("public_rsa_key_pkcs8.pem")).unwrap();
|
|
|
|
|
|
|
|
for &alg in RSA_ALGORITHMS {
|
|
|
|
let privkey_key = privkey_pem.as_key().unwrap();
|
|
|
|
let pubkey_key = pubkey_pem.as_key().unwrap();
|
|
|
|
let encrypted = sign("hello world", privkey_key, alg).unwrap();
|
|
|
|
let is_valid = verify(&encrypted, "hello world", pubkey_key, alg).unwrap();
|
|
|
|
assert!(is_valid);
|
2019-06-07 14:08:54 -04:00
|
|
|
}
|
2017-04-10 23:40:01 -04:00
|
|
|
}
|
2017-02-22 02:45:28 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_claim() {
|
|
|
|
let my_claims = Claims {
|
|
|
|
sub: "b@b.com".to_string(),
|
2018-07-25 09:42:00 -04:00
|
|
|
company: "ACME".to_string(),
|
|
|
|
exp: Utc::now().timestamp() + 10000,
|
2017-02-22 02:45:28 -05:00
|
|
|
};
|
2019-05-15 10:20:25 -04:00
|
|
|
let privkey = include_bytes!("private_rsa_key.der");
|
2019-06-16 12:00:00 -04:00
|
|
|
|
2019-06-07 14:08:54 -04:00
|
|
|
for &alg in RSA_ALGORITHMS {
|
2019-06-16 12:00:00 -04:00
|
|
|
let token = encode(&Header::new(alg), &my_claims, Key::Der(&privkey[..])).unwrap();
|
|
|
|
let token_data = decode::<Claims>(
|
|
|
|
&token,
|
|
|
|
Key::Der(include_bytes!("public_rsa_key.der")),
|
|
|
|
&Validation::new(alg),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-06-07 14:08:54 -04:00
|
|
|
assert_eq!(my_claims, token_data.claims);
|
|
|
|
assert!(token_data.header.kid.is_none());
|
|
|
|
}
|
2017-02-22 02:45:28 -05:00
|
|
|
}
|
2019-05-15 10:20:25 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "InvalidRsaKey")]
|
|
|
|
fn fails_with_different_key_format() {
|
|
|
|
let privkey = include_bytes!("private_rsa_key.der");
|
2019-06-16 11:51:43 -04:00
|
|
|
sign("hello world", Key::Pkcs8(&privkey[..]), Algorithm::RS256).unwrap();
|
2019-05-15 10:20:25 -04:00
|
|
|
}
|
2019-07-06 14:36:32 -04:00
|
|
|
|
|
|
|
#[test]
|
2019-07-13 11:43:44 -04:00
|
|
|
fn rsa_modulus_exponent() {
|
|
|
|
let modulus: Vec<u8> = vec![
|
|
|
|
0xc9, 0x11, 0x3a, 0xac, 0x7b, 0x8d, 0x47, 0x44, 0x1b, 0x1c, 0xed, 0xc7, 0xdc, 0xab, 0x76,
|
|
|
|
0xa4, 0xe2, 0x86, 0x56, 0x14, 0x2a, 0x19, 0x95, 0xc8, 0x9c, 0xe7, 0x6e, 0x40, 0xdc, 0x57,
|
|
|
|
0xce, 0xe2, 0xa5, 0xbd, 0x04, 0xcb, 0x51, 0x3b, 0xf8, 0x97, 0x8b, 0x20, 0x82, 0x1e, 0x7f,
|
|
|
|
0x09, 0x86, 0x22, 0xfd, 0xcb, 0xc8, 0xf9, 0x25, 0xd5, 0x4f, 0xd9, 0x0f, 0x59, 0x22, 0x97,
|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 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);
|
|
|
|
}
|
2019-11-03 10:36:19 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "InvalidKeyFormat")]
|
|
|
|
fn fails_with_non_pkcs8_key_format() {
|
|
|
|
let privkey = include_bytes!("private_rsa_key.der");
|
|
|
|
let _encrypted = sign("hello world", Key::Der(&privkey[..]), Algorithm::ES256).unwrap();
|
|
|
|
}
|