Skip to content

Commit 9c16fcf

Browse files
committed
Make Algorithm enum non_exhaustive, allow thumbprint computation to fail
1 parent 0c5931a commit 9c16fcf

5 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/algorithms.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl AlgorithmFamily {
3939
/// The algorithms supported for signing/verifying JWTs
4040
#[allow(clippy::upper_case_acronyms)]
4141
#[derive(Debug, Default, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
42+
#[non_exhaustive]
4243
pub enum Algorithm {
4344
/// HMAC using SHA-256
4445
#[default]

src/crypto/aws_lc/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ fn extract_ec_public_key_coordinates(
4848
Ok((curve, x.to_vec(), y.to_vec()))
4949
}
5050

51-
fn compute_digest(data: &[u8], hash_function: ThumbprintHash) -> Vec<u8> {
51+
fn compute_digest(data: &[u8], hash_function: ThumbprintHash) -> errors::Result<Vec<u8>> {
5252
let algorithm = match hash_function {
5353
ThumbprintHash::SHA256 => &digest::SHA256,
5454
ThumbprintHash::SHA384 => &digest::SHA384,
5555
ThumbprintHash::SHA512 => &digest::SHA512,
5656
};
57-
digest::digest(algorithm, data).as_ref().to_vec()
57+
Ok(digest::digest(algorithm, data).as_ref().to_vec())
5858
}
5959

6060
fn new_signer(algorithm: &Algorithm, key: &EncodingKey) -> Result<Box<dyn JwtSigner>, Error> {

src/crypto/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub struct JwkUtils {
144144
pub extract_ec_public_key_coordinates:
145145
fn(&[u8], Algorithm) -> Result<(EllipticCurve, Vec<u8>, Vec<u8>)>,
146146
/// Given some data and a name of a hash function, compute hash_function(data)
147-
pub compute_digest: fn(&[u8], ThumbprintHash) -> Vec<u8>,
147+
pub compute_digest: fn(&[u8], ThumbprintHash) -> Result<Vec<u8>>,
148148
}
149149

150150
impl JwkUtils {

src/crypto/rust_crypto/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ fn extract_ec_public_key_coordinates(
5555
}
5656
}
5757

58-
fn compute_digest(data: &[u8], hash_function: ThumbprintHash) -> Vec<u8> {
59-
match hash_function {
58+
fn compute_digest(data: &[u8], hash_function: ThumbprintHash) -> errors::Result<Vec<u8>> {
59+
Ok(match hash_function {
6060
ThumbprintHash::SHA256 => Sha256::digest(data).to_vec(),
6161
ThumbprintHash::SHA384 => Sha384::digest(data).to_vec(),
6262
ThumbprintHash::SHA512 => Sha512::digest(data).to_vec(),
63-
}
63+
})
6464
}
6565

6666
fn new_signer(algorithm: &Algorithm, key: &EncodingKey) -> Result<Box<dyn JwtSigner>, Error> {

src/jwk.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl Jwk {
496496
/// Compute the thumbprint of the JWK.
497497
///
498498
/// Per [RFC-7638](https://datatracker.ietf.org/doc/html/rfc7638)
499-
pub fn thumbprint(&self, hash_function: ThumbprintHash) -> String {
499+
pub fn thumbprint(&self, hash_function: ThumbprintHash) -> crate::errors::Result<String> {
500500
let pre = match &self.algorithm {
501501
AlgorithmParameters::EllipticCurve(a) => match a.curve {
502502
EllipticCurve::P256 | EllipticCurve::P384 | EllipticCurve::P521 => {
@@ -540,10 +540,10 @@ impl Jwk {
540540
},
541541
};
542542

543-
b64_encode((CryptoProvider::get_default().jwk_utils.compute_digest)(
543+
Ok(b64_encode((CryptoProvider::get_default().jwk_utils.compute_digest)(
544544
pre.as_bytes(),
545545
hash_function,
546-
))
546+
)?))
547547
}
548548
}
549549

@@ -624,7 +624,9 @@ mod tests {
624624
e: "AQAB".to_string(),
625625
}),
626626
}
627-
.thumbprint(ThumbprintHash::SHA256);
627+
.thumbprint(ThumbprintHash::SHA256)
628+
.unwrap();
629+
628630
assert_eq!(tp.as_str(), "NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs");
629631
}
630632
}

0 commit comments

Comments
 (0)