jsonwebtoken/src/crypto/mod.rs

129 lines
4.7 KiB
Rust
Raw 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-11-08 14:00:19 -05:00
use crate::errors::{Result};
use crate::pem_decoder::PemEncodedKey;
2019-11-08 14:00:19 -05:00
use crate::serialization::{encode, decode};
pub(crate) mod rsa;
pub(crate) mod ecdsa;
/// The actual HS signing + encoding
2019-11-08 14:00:19 -05:00
pub(crate) fn sign_hmac(alg: hmac::Algorithm, key: &[u8], signing_input: &str) -> Result<String> {
let digest = hmac::sign(&hmac::Key::new(alg, key), signing_input.as_bytes());
2019-11-08 14:00:19 -05:00
Ok(encode(digest.as_ref()))
}
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.
///
/// Only use this function if you want to do something other than JWT.
2019-11-08 14:00:19 -05:00
/// `key` is the secret for HMAC and a pem encoded string otherwise
pub fn sign(signing_input: &str, key: &[u8], algorithm: Algorithm) -> Result<String> {
2017-02-22 02:45:28 -05:00
match algorithm {
2019-08-10 17:38:14 -04:00
Algorithm::HS256 => sign_hmac(hmac::HMAC_SHA256, key, signing_input),
Algorithm::HS384 => sign_hmac(hmac::HMAC_SHA384, key, signing_input),
Algorithm::HS512 => sign_hmac(hmac::HMAC_SHA512, key, signing_input),
2017-02-22 02:45:28 -05:00
Algorithm::ES256 => {
2019-11-08 14:00:19 -05:00
ecdsa::sign(&signature::ECDSA_P256_SHA256_FIXED_SIGNING, key, signing_input)
}
Algorithm::ES384 => {
2019-11-08 14:00:19 -05:00
ecdsa::sign(&signature::ECDSA_P384_SHA384_FIXED_SIGNING, key, signing_input)
}
2019-11-08 14:00:19 -05:00
Algorithm::RS256 => rsa::sign(&signature::RSA_PKCS1_SHA256, key, signing_input),
Algorithm::RS384 => rsa::sign(&signature::RSA_PKCS1_SHA384, key, signing_input),
Algorithm::RS512 => rsa::sign(&signature::RSA_PKCS1_SHA512, key, signing_input),
2019-11-08 14:00:19 -05:00
Algorithm::PS256 => rsa::sign(&signature::RSA_PSS_SHA256, key, signing_input),
Algorithm::PS384 => rsa::sign(&signature::RSA_PSS_SHA384, key, signing_input),
Algorithm::PS512 => rsa::sign(&signature::RSA_PSS_SHA512, key, signing_input),
2017-02-22 02:45:28 -05:00
}
}
2017-04-12 21:08:07 -04:00
/// Compares the signature given with a re-computed signature for HMAC or using the public key
2017-04-13 03:36:32 -04:00
/// for RSA.
///
/// Only use this function if you want to do something other than JWT.
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
///
2017-02-22 02:45:28 -05:00
/// `signing_input` is base64(header) + "." + base64(claims)
2018-10-28 14:58:35 -04:00
pub fn verify(
signature: &str,
signing_input: &str,
key: &[u8],
2018-10-28 14:58:35 -04:00
algorithm: Algorithm,
) -> Result<bool> {
2017-02-22 02:45:28 -05:00
match algorithm {
Algorithm::HS256 | Algorithm::HS384 | Algorithm::HS512 => {
// we just re-sign the data with the key and compare if they are equal
let signed = sign(signing_input, key, algorithm)?;
Ok(verify_slices_are_equal(signature.as_ref(), signed.as_ref()).is_ok())
2018-10-28 14:58:35 -04:00
}
Algorithm::ES256 => {
2019-06-16 12:00:00 -04:00
verify_ring_es(&signature::ECDSA_P256_SHA256_FIXED, signature, signing_input, key)
}
Algorithm::ES384 => {
2019-06-16 12:00:00 -04:00
verify_ring_es(&signature::ECDSA_P384_SHA384_FIXED, signature, signing_input, key)
}
2018-10-28 14:58:35 -04:00
Algorithm::RS256 => {
2019-06-16 12:00:00 -04:00
verify_ring_rsa(&signature::RSA_PKCS1_2048_8192_SHA256, signature, signing_input, key)
2018-10-28 14:58:35 -04:00
}
Algorithm::RS384 => {
2019-06-16 12:00:00 -04:00
verify_ring_rsa(&signature::RSA_PKCS1_2048_8192_SHA384, signature, signing_input, key)
2018-10-28 14:58:35 -04:00
}
Algorithm::RS512 => {
2019-06-16 12:00:00 -04:00
verify_ring_rsa(&signature::RSA_PKCS1_2048_8192_SHA512, signature, signing_input, key)
2018-10-28 14:58:35 -04:00
}
Algorithm::PS256 => {
2019-06-16 12:00:00 -04:00
verify_ring_rsa(&signature::RSA_PSS_2048_8192_SHA256, signature, signing_input, key)
}
Algorithm::PS384 => {
2019-06-16 12:00:00 -04:00
verify_ring_rsa(&signature::RSA_PSS_2048_8192_SHA384, signature, signing_input, key)
}
Algorithm::PS512 => {
2019-06-16 12:00:00 -04:00
verify_ring_rsa(&signature::RSA_PSS_2048_8192_SHA512, signature, signing_input, key)
}
}
}
2019-11-08 14:00:19 -05:00
// TODO: see if we can remove stuff?
/// See Ring docs for more details
fn verify_ring(
alg: &'static dyn signature::VerificationAlgorithm,
signature: &str,
signing_input: &str,
key: &[u8],
) -> Result<bool> {
let signature_bytes = decode(signature)?;
let public_key = signature::UnparsedPublicKey::new(alg, key);
let res = public_key.verify(signing_input.as_bytes(), &signature_bytes);
Ok(res.is_ok())
}
fn verify_ring_es(
alg: &'static dyn signature::VerificationAlgorithm,
signature: &str,
signing_input: &str,
key: &[u8],
) -> Result<bool> {
let pem_key = PemEncodedKey::new(key)?;
verify_ring(alg, signature, signing_input, pem_key.as_ec_public_key()?)
}
fn verify_ring_rsa(
alg: &'static signature::RsaParameters,
signature: &str,
signing_input: &str,
key: &[u8],
) -> Result<bool> {
let pem_key = PemEncodedKey::new(key)?;
verify_ring(alg, signature, signing_input, pem_key.as_rsa_key()?)
}