jsonwebtoken/src/crypto/mod.rs

108 lines
3.7 KiB
Rust
Raw Permalink Normal View History

use ring::constant_time::verify_slices_are_equal;
2019-11-08 14:00:19 -05:00
use ring::{hmac, signature};
2019-07-06 14:36:32 -04:00
use crate::algorithms::Algorithm;
2019-12-29 15:50:06 -05:00
use crate::decoding::{DecodingKey, DecodingKeyKind};
2019-12-29 12:42:35 -05:00
use crate::encoding::EncodingKey;
2019-11-09 06:42:40 -05:00
use crate::errors::Result;
2019-11-11 14:16:34 -05:00
use crate::serialization::{b64_decode, b64_encode};
2019-11-08 14:00:19 -05:00
pub(crate) mod ecdsa;
2020-11-17 08:15:17 -05:00
pub(crate) mod eddsa;
2019-11-09 06:42:40 -05:00
pub(crate) mod rsa;
/// The actual HS signing + encoding
2019-11-11 14:16:34 -05:00
/// Could be in its own file to match RSA/EC but it's 2 lines...
2021-02-19 15:29:19 -05:00
pub(crate) fn sign_hmac(alg: hmac::Algorithm, key: &[u8], message: &[u8]) -> String {
2020-11-17 08:17:40 -05:00
let digest = hmac::sign(&hmac::Key::new(alg, key), message);
b64_encode(digest)
}
2017-04-13 03:36:32 -04:00
/// Take the payload of a JWT, sign it using the algorithm given and return
/// the base64 url safe encoded of the result.
///
2019-11-14 13:43:43 -05:00
/// If you just want to encode a JWT, use `encode` instead.
2020-11-17 08:28:07 -05:00
pub fn sign(message: &[u8], key: &EncodingKey, algorithm: Algorithm) -> Result<String> {
2017-02-22 02:45:28 -05:00
match algorithm {
2021-02-19 15:29:19 -05:00
Algorithm::HS256 => Ok(sign_hmac(hmac::HMAC_SHA256, key.inner(), message)),
Algorithm::HS384 => Ok(sign_hmac(hmac::HMAC_SHA384, key.inner(), message)),
Algorithm::HS512 => Ok(sign_hmac(hmac::HMAC_SHA512, key.inner(), message)),
2017-02-22 02:45:28 -05:00
2019-11-11 14:29:57 -05:00
Algorithm::ES256 | Algorithm::ES384 => {
2019-12-29 12:42:35 -05:00
ecdsa::sign(ecdsa::alg_to_ec_signing(algorithm), key.inner(), message)
2019-11-11 14:29:57 -05:00
}
2020-11-17 08:15:17 -05:00
Algorithm::EdDSA => eddsa::sign(key.inner(), message),
2019-11-11 14:29:57 -05:00
Algorithm::RS256
| Algorithm::RS384
| Algorithm::RS512
| Algorithm::PS256
| Algorithm::PS384
2019-12-29 12:42:35 -05:00
| Algorithm::PS512 => rsa::sign(rsa::alg_to_rsa_signing(algorithm), key.inner(), message),
2019-11-09 06:42:40 -05:00
}
}
2019-11-11 14:29:57 -05:00
/// See Ring docs for more details
fn verify_ring(
alg: &'static dyn signature::VerificationAlgorithm,
signature: &str,
2020-11-17 08:17:40 -05:00
message: &[u8],
2019-11-11 14:29:57 -05:00
key: &[u8],
) -> Result<bool> {
let signature_bytes = b64_decode(signature)?;
let public_key = signature::UnparsedPublicKey::new(alg, key);
2020-11-17 08:17:40 -05:00
let res = public_key.verify(message, &signature_bytes);
2019-11-11 14:29:57 -05:00
Ok(res.is_ok())
}
2017-04-12 21:08:07 -04:00
/// Compares the signature given with a re-computed signature for HMAC or using the public key
2019-11-11 14:16:34 -05:00
/// for RSA/EC.
2017-04-13 03:36:32 -04:00
///
2019-11-14 13:43:43 -05:00
/// If you just want to decode a JWT, use `decode` instead.
2017-02-22 02:45:28 -05:00
///
/// `signature` is the signature part of a jwt (text after the second '.')
2017-04-12 21:08:07 -04:00
///
2019-11-11 14:16:34 -05:00
/// `message` is base64(header) + "." + base64(claims)
2019-12-29 15:50:06 -05:00
pub fn verify(
2020-11-17 08:17:40 -05:00
signature: &str,
message: &[u8],
key: &DecodingKey,
algorithm: Algorithm,
2019-12-29 15:50:06 -05:00
) -> Result<bool> {
2017-02-22 02:45:28 -05:00
match algorithm {
Algorithm::HS256 | Algorithm::HS384 | Algorithm::HS512 => {
2019-11-09 06:42:40 -05:00
// we just re-sign the message with the key and compare if they are equal
2020-11-17 08:28:07 -05:00
let signed = sign(message, &EncodingKey::from_secret(key.as_bytes()), algorithm)?;
2017-02-22 02:45:28 -05:00
Ok(verify_slices_are_equal(signature.as_ref(), signed.as_ref()).is_ok())
2018-10-28 14:58:35 -04:00
}
2019-12-29 15:50:06 -05:00
Algorithm::ES256 | Algorithm::ES384 => verify_ring(
ecdsa::alg_to_ec_verification(algorithm),
signature,
message,
key.as_bytes(),
),
2020-11-17 08:15:17 -05:00
Algorithm::EdDSA => verify_ring(
eddsa::alg_to_ec_verification(algorithm),
signature,
message,
key.as_bytes(),
),
2019-11-11 14:29:57 -05:00
Algorithm::RS256
| Algorithm::RS384
| Algorithm::RS512
| Algorithm::PS256
| Algorithm::PS384
| Algorithm::PS512 => {
2019-12-29 15:50:06 -05:00
let alg = rsa::alg_to_rsa_parameters(algorithm);
match &key.kind {
DecodingKeyKind::SecretOrDer(bytes) => verify_ring(alg, signature, message, bytes),
DecodingKeyKind::RsaModulusExponent { n, e } => {
rsa::verify_from_components(alg, signature, message, (n, e))
}
}
2019-11-11 14:29:57 -05:00
}
}
}