Skip to content

Commit 249f5f7

Browse files
authored
Don't panic when computing thumbprint (#524)
1 parent b5d9c38 commit 249f5f7

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

src/jwk.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ impl Jwk {
612612
)
613613
}
614614
EllipticCurve::Ed25519 => {
615-
panic!("EllipticCurve can't contain this curve type")
615+
return Err(ErrorKind::InvalidKeyFormat.into());
616616
}
617617
},
618618
AlgorithmParameters::RSA(a) => {
@@ -632,7 +632,7 @@ impl Jwk {
632632
}
633633
AlgorithmParameters::OctetKeyPair(a) => match a.curve {
634634
EllipticCurve::P256 | EllipticCurve::P384 | EllipticCurve::P521 => {
635-
panic!("OctetKeyPair can't contain this curve type")
635+
return Err(ErrorKind::InvalidKeyFormat.into());
636636
}
637637
EllipticCurve::Ed25519 => {
638638
format!(
@@ -676,8 +676,8 @@ mod tests {
676676
use crate::Algorithm;
677677
use crate::errors::ErrorKind;
678678
use crate::jwk::{
679-
AlgorithmParameters, Jwk, JwkSet, KeyAlgorithm, OctetKeyType, RSAKeyParameters,
680-
ThumbprintHash,
679+
AlgorithmParameters, CommonParameters, EllipticCurve, Jwk, JwkSet, KeyAlgorithm,
680+
OctetKeyPairParameters, OctetKeyPairType, OctetKeyType, RSAKeyParameters, ThumbprintHash,
681681
};
682682
use crate::serialization::b64_encode;
683683
use crate::{DecodingKey, EncodingKey};
@@ -738,6 +738,26 @@ mod tests {
738738
assert_eq!(tp.as_str(), "NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs");
739739
}
740740

741+
#[test]
742+
fn check_thumbprint_bad_key() {
743+
let jwk = Jwk {
744+
common: CommonParameters {
745+
key_algorithm: Some(KeyAlgorithm::ES256),
746+
..Default::default()
747+
},
748+
algorithm: AlgorithmParameters::OctetKeyPair(OctetKeyPairParameters {
749+
key_type: OctetKeyPairType::OctetKeyPair,
750+
curve: EllipticCurve::P256,
751+
x: "".to_string(),
752+
}),
753+
};
754+
755+
assert_eq!(
756+
jwk.thumbprint(ThumbprintHash::SHA256).unwrap_err().into_kind(),
757+
ErrorKind::InvalidKeyFormat
758+
);
759+
}
760+
741761
#[test]
742762
#[wasm_bindgen_test]
743763
fn check_alg_key_alg_conversion() {

0 commit comments

Comments
 (0)