1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
use ct_codecs::{Base64UrlSafeNoPadding, Encoder}; use p256::ecdsa::{self, signature::DigestVerifier as _, signature::RandomizedDigestSigner as _}; use serde::{de::DeserializeOwned, Serialize}; use std::convert::TryFrom; use crate::claims::*; use crate::common::*; use crate::error::*; use crate::jwt_header::*; use crate::token::*; #[doc(hidden)] #[derive(Debug)] pub struct P256PublicKey(ecdsa::VerifyingKey); impl AsRef<ecdsa::VerifyingKey> for P256PublicKey { fn as_ref(&self) -> &ecdsa::VerifyingKey { &self.0 } } impl P256PublicKey { pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> { let p256_pk = ecdsa::VerifyingKey::from_sec1_bytes(raw).map_err(|_| JWTError::InvalidPublicKey)?; Ok(P256PublicKey(p256_pk)) } pub fn to_bytes(&self) -> Vec<u8> { self.0.to_encoded_point(true).as_bytes().to_vec() } } #[doc(hidden)] pub struct P256KeyPair(ecdsa::SigningKey); impl AsRef<ecdsa::SigningKey> for P256KeyPair { fn as_ref(&self) -> &ecdsa::SigningKey { &self.0 } } impl P256KeyPair { pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> { let p256_key_pair = ecdsa::SigningKey::from_bytes(raw).map_err(|_| JWTError::InvalidKeyPair)?; Ok(P256KeyPair(p256_key_pair)) } pub fn to_bytes(&self) -> Vec<u8> { self.0.to_bytes().to_vec() } pub fn public_key(&self) -> P256PublicKey { let p256_pk = self.0.verify_key(); P256PublicKey(p256_pk) } pub fn generate() -> Self { let rng = rand::thread_rng(); let p256_sk = ecdsa::SigningKey::random(rng); P256KeyPair(p256_sk) } } #[doc(hidden)] pub trait ECDSAP256KeyPairLike { fn jwt_alg_name() -> &'static str; fn key_pair(&self) -> &P256KeyPair; fn key_id(&self) -> &Option<String>; fn sign<CustomClaims: Serialize + DeserializeOwned>( &self, claims: JWTClaims<CustomClaims>, ) -> Result<String, Error> { let mut jwt_header = JWTHeader::default(); jwt_header.algorithm = Self::jwt_alg_name().to_string(); jwt_header.key_id = self.key_id().clone(); Token::build(&jwt_header, claims, |authenticated| { let mut digest = hmac_sha256::Hash::new(); digest.update(authenticated.as_bytes()); let rng = rand::thread_rng(); let signature: ecdsa::Signature = self.key_pair().as_ref().sign_digest_with_rng(rng, digest); Ok(signature.as_ref().to_vec()) }) } } #[doc(hidden)] pub trait ECDSAP256PublicKeyLike { fn jwt_alg_name() -> &'static str; fn public_key(&self) -> &P256PublicKey; fn key_id(&self) -> &Option<String>; fn set_key_id(&mut self, key_id: String); fn verify_token<CustomClaims: Serialize + DeserializeOwned>( &self, token: &str, options: Option<VerificationOptions>, ) -> Result<JWTClaims<CustomClaims>, Error> { Token::verify( Self::jwt_alg_name(), token, options, |authenticated, signature| { let ecdsa_signature = ecdsa::Signature::try_from(signature) .map_err(|_| JWTError::InvalidSignature)?; let mut digest = hmac_sha256::Hash::new(); digest.update(authenticated.as_bytes()); self.public_key() .as_ref() .verify_digest(digest, &ecdsa_signature) .map_err(|_| JWTError::InvalidSignature)?; Ok(()) }, ) } fn create_key_id(&mut self) -> &str { self.set_key_id( Base64UrlSafeNoPadding::encode_to_string(hmac_sha256::Hash::hash( &self.public_key().to_bytes(), )) .unwrap(), ); &self.key_id().as_ref().map(|x| x.as_str()).unwrap() } } pub struct ES256KeyPair { key_pair: P256KeyPair, key_id: Option<String>, } #[derive(Debug)] pub struct ES256PublicKey { pk: P256PublicKey, key_id: Option<String>, } impl ECDSAP256KeyPairLike for ES256KeyPair { fn jwt_alg_name() -> &'static str { "ES256" } fn key_pair(&self) -> &P256KeyPair { &self.key_pair } fn key_id(&self) -> &Option<String> { &self.key_id } } impl ES256KeyPair { pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> { Ok(ES256KeyPair { key_pair: P256KeyPair::from_bytes(raw)?, key_id: None, }) } pub fn to_bytes(&self) -> Vec<u8> { self.key_pair.to_bytes() } pub fn public_key(&self) -> ES256PublicKey { ES256PublicKey { pk: self.key_pair.public_key(), key_id: self.key_id.clone(), } } pub fn generate() -> Self { ES256KeyPair { key_pair: P256KeyPair::generate(), key_id: None, } } pub fn with_key_id(mut self, key_id: &str) -> Self { self.key_id = Some(key_id.to_string()); self } } impl ECDSAP256PublicKeyLike for ES256PublicKey { fn jwt_alg_name() -> &'static str { "ES256" } fn public_key(&self) -> &P256PublicKey { &self.pk } fn key_id(&self) -> &Option<String> { &self.key_id } fn set_key_id(&mut self, key_id: String) { self.key_id = Some(key_id); } } impl ES256PublicKey { pub fn from_bytes(raw: &[u8]) -> Result<Self, Error> { Ok(ES256PublicKey { pk: P256PublicKey::from_bytes(raw)?, key_id: None, }) } pub fn to_bytes(&self) -> Vec<u8> { self.pk.to_bytes() } pub fn with_key_id(mut self, key_id: &str) -> Self { self.key_id = Some(key_id.to_string()); self } }