Skip to content

Commit 95865ee

Browse files
Fix #95, correct key ordering for CBOR encoding
1 parent db56a46 commit 95865ee

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

libwebauthn/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ num_enum = "0.7.1"
4141
x509-parser = "0.16.0"
4242
time = "0.3.35"
4343
curve25519-dalek = "4.1.3"
44-
hex = "0.4.2"
44+
hex = "0.4.3"
4545
mockall = "0.11.4"
4646
hidapi = { version = "2.4.1", default-features = false, features = [
4747
"linux-static-hidraw",

libwebauthn/src/proto/ctap2/model.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ impl From<&Ctap1Transport> for Ctap2Transport {
139139

140140
#[derive(Debug, Clone, Serialize, Deserialize)]
141141
pub struct Ctap2PublicKeyCredentialDescriptor {
142-
pub r#type: Ctap2PublicKeyCredentialType,
143142
pub id: ByteBuf,
143+
pub r#type: Ctap2PublicKeyCredentialType,
144144

145145
#[serde(skip_serializing_if = "Option::is_none")]
146146
pub transports: Option<Vec<Ctap2Transport>>,
@@ -156,11 +156,11 @@ pub enum Ctap2COSEAlgorithmIdentifier {
156156

157157
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
158158
pub struct Ctap2CredentialType {
159-
#[serde(rename = "type")]
160-
pub public_key_type: Ctap2PublicKeyCredentialType,
161-
162159
#[serde(rename = "alg")]
163160
pub algorithm: Ctap2COSEAlgorithmIdentifier,
161+
162+
#[serde(rename = "type")]
163+
pub public_key_type: Ctap2PublicKeyCredentialType,
164164
}
165165

166166
impl Default for Ctap2CredentialType {
@@ -205,3 +205,40 @@ pub enum Ctap2UserVerificationOperation {
205205
GetPinToken,
206206
None,
207207
}
208+
209+
#[cfg(test)]
210+
mod tests {
211+
use crate::proto::ctap2::Ctap2PublicKeyCredentialDescriptor;
212+
213+
use super::{Ctap2CredentialType, Ctap2COSEAlgorithmIdentifier, Ctap2PublicKeyCredentialType};
214+
use serde_bytes::ByteBuf;
215+
use serde_cbor;
216+
use hex;
217+
218+
#[test]
219+
/// Verify CBOR serialization conforms to CTAP canonical standard, including ordering (see #95)
220+
pub fn credential_type_field_serialization() {
221+
let credential_type = Ctap2CredentialType {
222+
algorithm: Ctap2COSEAlgorithmIdentifier::ES256,
223+
public_key_type: Ctap2PublicKeyCredentialType::PublicKey,
224+
};
225+
let serialized = serde_cbor::to_vec(&credential_type).unwrap();
226+
// Known good, verified by hand with cbor.me playground
227+
let expected = hex::decode("a263616c672664747970656a7075626c69632d6b6579").unwrap();
228+
assert_eq!(serialized, expected);
229+
}
230+
231+
#[test]
232+
/// Verify CBOR serialization conforms to CTAP canonical standard, including ordering (see #95)
233+
pub fn credential_descriptor_serialization() {
234+
let credential_descriptor = Ctap2PublicKeyCredentialDescriptor {
235+
id: ByteBuf::from(vec![0x42]),
236+
r#type: Ctap2PublicKeyCredentialType::PublicKey,
237+
transports: None,
238+
};
239+
let serialized = serde_cbor::to_vec(&credential_descriptor).unwrap();
240+
// Known good, verified by hand with cbor.me playground
241+
let expected = hex::decode("a2626964414264747970656a7075626c69632d6b6579").unwrap();
242+
assert_eq!(serialized, expected);
243+
}
244+
}

0 commit comments

Comments
 (0)