Skip to content

Commit 528af8d

Browse files
Do not fail on unknown algorithms (fix #66) (#111)
Allows parsing unknown algorithm types without failing. Only ES256 is every used for requests, and it's guaranteed to be supported by the spec, so checking the value is not necessary for now.
1 parent 02436bb commit 528af8d

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

libwebauthn/src/proto/ctap2/model.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ impl Ctap2PublicKeyCredentialUserEntity {
116116
pub enum Ctap2PublicKeyCredentialType {
117117
#[serde(rename = "public-key")]
118118
PublicKey,
119+
120+
#[serde(other)]
121+
Unknown,
119122
}
120123

121124
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
@@ -154,9 +157,11 @@ pub enum Ctap2COSEAlgorithmIdentifier {
154157
ES256 = -7,
155158
EDDSA = -8,
156159
TOPT = -9,
160+
#[serde(other)]
161+
Unknown = -999,
157162
}
158163

159-
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
164+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
160165
pub struct Ctap2CredentialType {
161166
#[serde(rename = "alg")]
162167
pub algorithm: Ctap2COSEAlgorithmIdentifier,
@@ -184,6 +189,11 @@ impl Ctap2CredentialType {
184189
algorithm,
185190
}
186191
}
192+
193+
pub fn is_known(&self) -> bool {
194+
self.algorithm != Ctap2COSEAlgorithmIdentifier::Unknown
195+
&& self.public_key_type != Ctap2PublicKeyCredentialType::Unknown
196+
}
187197
}
188198

189199
pub trait Ctap2UserVerifiableRequest {
@@ -243,4 +253,44 @@ mod tests {
243253
let expected = hex::decode("a2626964414264747970656a7075626c69632d6b6579").unwrap();
244254
assert_eq!(serialized, expected);
245255
}
256+
257+
#[test]
258+
pub fn deserialize_known_credential_type() {
259+
// python $ cbor2.dumps({"alg":-7,"type":"public-key"}).hex()
260+
let serialized: Vec<u8> =
261+
hex::decode("a263616c672664747970656a7075626c69632d6b6579").unwrap();
262+
let credential_type: Ctap2CredentialType = serde_cbor::from_slice(&serialized).unwrap();
263+
assert_eq!(
264+
credential_type,
265+
Ctap2CredentialType {
266+
algorithm: Ctap2COSEAlgorithmIdentifier::ES256,
267+
public_key_type: Ctap2PublicKeyCredentialType::PublicKey,
268+
}
269+
);
270+
assert!(credential_type.is_known());
271+
}
272+
273+
#[test]
274+
pub fn deserialize_unknown_credential_type_algorithm() {
275+
// python $ cbor2.dumps({"alg":-42,"type":"public-key"}).hex()
276+
let serialized: Vec<u8> =
277+
hex::decode("a263616c67382964747970656a7075626c69632d6b6579").unwrap();
278+
let credential_type: Ctap2CredentialType = serde_cbor::from_slice(&serialized).unwrap();
279+
assert_eq!(
280+
credential_type,
281+
Ctap2CredentialType {
282+
algorithm: Ctap2COSEAlgorithmIdentifier::Unknown,
283+
public_key_type: Ctap2PublicKeyCredentialType::PublicKey,
284+
}
285+
);
286+
assert!(!credential_type.is_known());
287+
}
288+
289+
#[test]
290+
pub fn deserialize_unknown_credential_type() {
291+
// python $ cbor2.dumps({"alg":-7,"type":"unknown"}).hex()
292+
let serialized: Vec<u8> = hex::decode("a263616c6726647479706567756e6b6e6f776e").unwrap();
293+
let credential_type: Ctap2CredentialType = serde_cbor::from_slice(&serialized).unwrap();
294+
assert!(!credential_type.is_known());
295+
}
246296
}

0 commit comments

Comments
 (0)