|
| 1 | +//! Implementations of the [`JwtSigner`] and [`JwtVerifier`] traits for the |
| 2 | +//! RSA family of algorithms using [`aws_lc_rs`] |
| 3 | +
|
| 4 | +use aws_lc_rs::{rand, signature as crypto_sig}; |
| 5 | +use signature::{Signer, Verifier}; |
| 6 | + |
| 7 | +use crate::algorithms::AlgorithmFamily; |
| 8 | +use crate::crypto::{JwtSigner, JwtVerifier}; |
| 9 | +use crate::decoding::DecodingKeyKind; |
| 10 | +use crate::errors::{new_error, ErrorKind, Result}; |
| 11 | +use crate::{Algorithm, DecodingKey, EncodingKey}; |
| 12 | + |
| 13 | +/// Try to sign the `message` using an `RSA` `algorithm`. |
| 14 | +fn try_sign_rsa( |
| 15 | + algorithm: &'static dyn crypto_sig::RsaEncoding, |
| 16 | + encoding_key: &EncodingKey, |
| 17 | + msg: &[u8], |
| 18 | +) -> std::result::Result<Vec<u8>, signature::Error> { |
| 19 | + let key_pair = crypto_sig::RsaKeyPair::from_der(encoding_key.inner()) |
| 20 | + .map_err(|err| signature::Error::from_source(err))?; |
| 21 | + |
| 22 | + let mut signature = vec![0; key_pair.public_modulus_len()]; |
| 23 | + let rng = rand::SystemRandom::new(); |
| 24 | + key_pair |
| 25 | + .sign(algorithm, &rng, msg, &mut signature) |
| 26 | + .map_err(|err| signature::Error::from_source(err))?; |
| 27 | + |
| 28 | + Ok(signature) |
| 29 | +} |
| 30 | + |
| 31 | +/// Return a `aws_lc_rs` RSA public key from a [`DecodingKey`] |
| 32 | +/// |
| 33 | +/// # Errors |
| 34 | +/// |
| 35 | +/// - If `decoding_key` is not from the RSA family. |
| 36 | +fn verify_rsa( |
| 37 | + algorithm: &'static crypto_sig::RsaParameters, |
| 38 | + decoding_key: &DecodingKey, |
| 39 | + msg: &[u8], |
| 40 | + signature: &[u8], |
| 41 | +) -> std::result::Result<(), signature::Error> { |
| 42 | + match &decoding_key.kind { |
| 43 | + DecodingKeyKind::SecretOrDer(bytes) => { |
| 44 | + let public_key = crypto_sig::UnparsedPublicKey::new(algorithm, bytes); |
| 45 | + public_key.verify(msg, signature).map_err(|err| signature::Error::from_source(err))?; |
| 46 | + } |
| 47 | + DecodingKeyKind::RsaModulusExponent { n, e } => { |
| 48 | + let public_key = crypto_sig::RsaPublicKeyComponents { n, e }; |
| 49 | + public_key |
| 50 | + .verify(algorithm, msg, &signature) |
| 51 | + .map_err(|err| signature::Error::from_source(err))?; |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + Ok(()) |
| 56 | +} |
| 57 | + |
| 58 | +pub struct Rsa256Signer(EncodingKey); |
| 59 | + |
| 60 | +impl Rsa256Signer { |
| 61 | + pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { |
| 62 | + if encoding_key.family != AlgorithmFamily::Rsa { |
| 63 | + return Err(new_error(ErrorKind::InvalidKeyFormat)); |
| 64 | + } |
| 65 | + |
| 66 | + Ok(Self(encoding_key.clone())) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Signer<Vec<u8>> for Rsa256Signer { |
| 71 | + fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { |
| 72 | + try_sign_rsa(&crypto_sig::RSA_PKCS1_SHA256, &self.0, msg) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl JwtSigner for Rsa256Signer { |
| 77 | + fn algorithm(&self) -> Algorithm { |
| 78 | + Algorithm::RS256 |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +pub struct Rsa256Verifier(DecodingKey); |
| 83 | + |
| 84 | +impl Rsa256Verifier { |
| 85 | + pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { |
| 86 | + if decoding_key.family != AlgorithmFamily::Rsa { |
| 87 | + return Err(new_error(ErrorKind::InvalidKeyFormat)); |
| 88 | + } |
| 89 | + |
| 90 | + Ok(Self(decoding_key.clone())) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Verifier<Vec<u8>> for Rsa256Verifier { |
| 95 | + fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { |
| 96 | + verify_rsa(&crypto_sig::RSA_PKCS1_2048_8192_SHA256, &self.0, msg, signature) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl JwtVerifier for Rsa256Verifier { |
| 101 | + fn algorithm(&self) -> Algorithm { |
| 102 | + Algorithm::RS256 |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +pub struct Rsa384Signer(EncodingKey); |
| 107 | + |
| 108 | +impl Rsa384Signer { |
| 109 | + pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { |
| 110 | + if encoding_key.family != AlgorithmFamily::Rsa { |
| 111 | + return Err(new_error(ErrorKind::InvalidKeyFormat)); |
| 112 | + } |
| 113 | + |
| 114 | + Ok(Self(encoding_key.clone())) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl Signer<Vec<u8>> for Rsa384Signer { |
| 119 | + fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { |
| 120 | + try_sign_rsa(&crypto_sig::RSA_PKCS1_SHA384, &self.0, msg) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl JwtSigner for Rsa384Signer { |
| 125 | + fn algorithm(&self) -> Algorithm { |
| 126 | + Algorithm::RS384 |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +pub struct Rsa384Verifier(DecodingKey); |
| 131 | + |
| 132 | +impl Rsa384Verifier { |
| 133 | + pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { |
| 134 | + if decoding_key.family != AlgorithmFamily::Rsa { |
| 135 | + return Err(new_error(ErrorKind::InvalidKeyFormat)); |
| 136 | + } |
| 137 | + |
| 138 | + Ok(Self(decoding_key.clone())) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl Verifier<Vec<u8>> for Rsa384Verifier { |
| 143 | + fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { |
| 144 | + verify_rsa(&crypto_sig::RSA_PKCS1_2048_8192_SHA384, &self.0, msg, signature) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl JwtVerifier for Rsa384Verifier { |
| 149 | + fn algorithm(&self) -> Algorithm { |
| 150 | + Algorithm::RS384 |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +pub struct Rsa512Signer(EncodingKey); |
| 155 | + |
| 156 | +impl Rsa512Signer { |
| 157 | + pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { |
| 158 | + if encoding_key.family != AlgorithmFamily::Rsa { |
| 159 | + return Err(new_error(ErrorKind::InvalidKeyFormat)); |
| 160 | + } |
| 161 | + |
| 162 | + Ok(Self(encoding_key.clone())) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl Signer<Vec<u8>> for Rsa512Signer { |
| 167 | + fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { |
| 168 | + try_sign_rsa(&crypto_sig::RSA_PKCS1_SHA512, &self.0, msg) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +impl JwtSigner for Rsa512Signer { |
| 173 | + fn algorithm(&self) -> Algorithm { |
| 174 | + Algorithm::RS512 |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +pub struct Rsa512Verifier(DecodingKey); |
| 179 | + |
| 180 | +impl Rsa512Verifier { |
| 181 | + pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { |
| 182 | + if decoding_key.family != AlgorithmFamily::Rsa { |
| 183 | + return Err(new_error(ErrorKind::InvalidKeyFormat)); |
| 184 | + } |
| 185 | + |
| 186 | + Ok(Self(decoding_key.clone())) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +impl Verifier<Vec<u8>> for Rsa512Verifier { |
| 191 | + fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { |
| 192 | + verify_rsa(&crypto_sig::RSA_PKCS1_2048_8192_SHA512, &self.0, msg, signature) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +impl JwtVerifier for Rsa512Verifier { |
| 197 | + fn algorithm(&self) -> Algorithm { |
| 198 | + Algorithm::RS512 |
| 199 | + } |
| 200 | +} |
0 commit comments