Skip to content

Commit 71e2199

Browse files
committed
Unify encoding and decoding key APIs
1 parent 09ccfe6 commit 71e2199

9 files changed

Lines changed: 56 additions & 34 deletions

File tree

src/crypto/aws_lc/ecdsa.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ macro_rules! define_ecdsa_verifier {
6262
impl Verifier<Vec<u8>> for $name {
6363
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), Error> {
6464
$verification_alg
65-
.verify_sig(self.0.as_bytes(), msg, signature)
65+
.verify_sig(
66+
self.0.try_get_as_bytes().map_err(Error::from_source)?,
67+
msg,
68+
signature,
69+
)
6670
.map_err(Error::from_source)?;
6771
Ok(())
6872
}

src/crypto/aws_lc/eddsa.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ impl EdDSAVerifier {
4848

4949
impl Verifier<Vec<u8>> for EdDSAVerifier {
5050
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), Error> {
51-
ED25519.verify_sig(self.0.as_bytes(), msg, signature).map_err(Error::from_source)?;
51+
ED25519
52+
.verify_sig(self.0.try_get_as_bytes().map_err(Error::from_source)?, msg, signature)
53+
.map_err(Error::from_source)?;
5254
Ok(())
5355
}
5456
}

src/crypto/aws_lc/hmac.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ use aws_lc_rs::hmac;
55
use signature::{Signer, Verifier};
66

77
use crate::crypto::{JwtSigner, JwtVerifier};
8-
use crate::errors::Result;
9-
use crate::{Algorithm, DecodingKey, EncodingKey};
8+
use crate::errors::{ErrorKind, Result, new_error};
9+
use crate::{Algorithm, AlgorithmFamily, DecodingKey, EncodingKey};
1010

1111
macro_rules! define_hmac_signer {
1212
($name:ident, $alg:expr, $hmac_alg:expr) => {
1313
pub struct $name(hmac::Key);
1414

1515
impl $name {
1616
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
17-
Ok(Self(hmac::Key::new($hmac_alg, encoding_key.try_get_hmac_secret()?)))
17+
if encoding_key.family() != AlgorithmFamily::Hmac {
18+
return Err(new_error(ErrorKind::InvalidKeyFormat));
19+
}
20+
21+
Ok(Self(hmac::Key::new($hmac_alg, encoding_key.inner())))
1822
}
1923
}
2024

@@ -38,7 +42,11 @@ macro_rules! define_hmac_verifier {
3842

3943
impl $name {
4044
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
41-
Ok(Self(hmac::Key::new($hmac_alg, decoding_key.try_get_hmac_secret()?)))
45+
if decoding_key.family() != AlgorithmFamily::Hmac {
46+
return Err(new_error(ErrorKind::InvalidKeyFormat));
47+
}
48+
49+
Ok(Self(hmac::Key::new($hmac_alg, decoding_key.try_get_as_bytes()?)))
4250
}
4351
}
4452

src/crypto/rust_crypto/ecdsa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ macro_rules! define_ecdsa_verifier {
5757
}
5858

5959
Ok(Self(
60-
<$verifying_key>::from_sec1_bytes(decoding_key.as_bytes())
60+
<$verifying_key>::from_sec1_bytes(decoding_key.try_get_as_bytes()?)
6161
.map_err(|_| ErrorKind::InvalidEcdsaKey)?,
6262
))
6363
}

src/crypto/rust_crypto/eddsa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl EdDSAVerifier {
4545

4646
Ok(Self(
4747
VerifyingKey::from_bytes(
48-
<&[u8; 32]>::try_from(&decoding_key.as_bytes()[..32])
48+
<&[u8; 32]>::try_from(&decoding_key.try_get_as_bytes()?[..32])
4949
.map_err(|_| ErrorKind::InvalidEddsaKey)?,
5050
)
5151
.map_err(|_| ErrorKind::InvalidEddsaKey)?,

src/crypto/rust_crypto/hmac.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use sha2::{Sha256, Sha384, Sha512};
66
use signature::{Signer, Verifier};
77

88
use crate::crypto::{JwtSigner, JwtVerifier};
9-
use crate::errors::{ErrorKind, Result};
10-
use crate::{Algorithm, DecodingKey, EncodingKey};
9+
use crate::errors::{ErrorKind, Result, new_error};
10+
use crate::{Algorithm, AlgorithmFamily, DecodingKey, EncodingKey};
1111

1212
type HmacSha256 = Hmac<Sha256>;
1313
type HmacSha384 = Hmac<Sha384>;
@@ -20,7 +20,11 @@ macro_rules! define_hmac_signer {
2020

2121
impl $name {
2222
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
23-
let inner = <$hmac_type>::new_from_slice(encoding_key.try_get_hmac_secret()?)
23+
if encoding_key.family() != AlgorithmFamily::Hmac {
24+
return Err(new_error(ErrorKind::InvalidKeyFormat));
25+
}
26+
27+
let inner = <$hmac_type>::new_from_slice(encoding_key.inner())
2428
.map_err(|_| ErrorKind::InvalidKeyFormat)?;
2529

2630
Ok(Self(inner))
@@ -52,7 +56,11 @@ macro_rules! define_hmac_verifier {
5256

5357
impl $name {
5458
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
55-
let inner = <$hmac_type>::new_from_slice(decoding_key.try_get_hmac_secret()?)
59+
if decoding_key.family() != AlgorithmFamily::Hmac {
60+
return Err(new_error(ErrorKind::InvalidKeyFormat));
61+
}
62+
63+
let inner = <$hmac_type>::new_from_slice(decoding_key.try_get_as_bytes()?)
5664
.map_err(|_| ErrorKind::InvalidKeyFormat)?;
5765

5866
Ok(Self(inner))

src/decoding.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,15 @@ impl DecodingKey {
228228
}
229229
}
230230

231-
/// Get the value of the key.
232-
pub fn as_bytes(&self) -> &[u8] {
231+
/// Try to get the key in raw byte format.
232+
///
233+
/// To be used for defining your own `CryptoProvider`.
234+
pub fn try_get_as_bytes(&self) -> Result<&[u8]> {
233235
match &self.kind {
234-
DecodingKeyKind::SecretOrDer(b) => b,
235-
DecodingKeyKind::RsaModulusExponent { .. } => unreachable!(),
236-
}
237-
}
238-
239-
/// Try to get the HMAC secret from a key.
240-
pub fn try_get_hmac_secret(&self) -> Result<&[u8]> {
241-
if self.family == AlgorithmFamily::Hmac {
242-
Ok(self.as_bytes())
243-
} else {
244-
Err(new_error(ErrorKind::InvalidKeyFormat))
236+
DecodingKeyKind::SecretOrDer(b) => Ok(b),
237+
DecodingKeyKind::RsaModulusExponent { .. } => {
238+
Err(new_error(ErrorKind::InvalidKeyFormat))
239+
}
245240
}
246241
}
247242
}

src/encoding.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,11 @@ impl EncodingKey {
110110
}
111111

112112
/// Get the value of the key.
113+
///
114+
/// To be used for defining your own `CryptoProvider`.
113115
pub fn inner(&self) -> &[u8] {
114116
&self.content
115117
}
116-
117-
/// Try to get the HMAC secret from a key.
118-
pub fn try_get_hmac_secret(&self) -> Result<&[u8]> {
119-
if self.family == AlgorithmFamily::Hmac {
120-
Ok(self.inner())
121-
} else {
122-
Err(new_error(ErrorKind::InvalidKeyFormat))
123-
}
124-
}
125118
}
126119

127120
impl Debug for EncodingKey {

tests/hmac.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ fn decode_token_wrong_algorithm() {
231231
assert_eq!(claims.unwrap_err().into_kind(), ErrorKind::InvalidAlgorithm);
232232
}
233233

234+
#[test]
235+
#[wasm_bindgen_test]
236+
fn decode_token_wrong_key_family() {
237+
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUiLCJleHAiOjI1MzI1MjQ4OTF9.9r56oF7ZliOBlOAyiOFperTGxBtPykRQiWNFxhDCW98";
238+
let claims = decode::<Claims>(
239+
token,
240+
&DecodingKey::from_rsa_der(b"secret"),
241+
&Validation::new(Algorithm::HS256),
242+
);
243+
assert_eq!(claims.unwrap_err().into_kind(), ErrorKind::InvalidKeyFormat);
244+
}
245+
234246
#[test]
235247
#[wasm_bindgen_test]
236248
fn encode_wrong_alg_family() {

0 commit comments

Comments
 (0)