@@ -2,20 +2,22 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
22use cosey:: PublicKey ;
33use serde:: {
44 de:: { DeserializeOwned , Error as DesError , Visitor } ,
5- ser:: Error as SerError ,
6- Deserialize , Deserializer , Serialize , Serializer ,
5+ Deserialize , Deserializer , Serialize ,
76} ;
87use serde_bytes:: ByteBuf ;
98use std:: {
109 fmt,
1110 io:: { Cursor , Read } ,
1211 marker:: PhantomData ,
1312} ;
14- use tracing:: warn;
15-
16- use crate :: proto:: {
17- ctap2:: { Ctap2PublicKeyCredentialDescriptor , Ctap2PublicKeyCredentialType } ,
18- CtapError ,
13+ use tracing:: { error, warn} ;
14+
15+ use crate :: {
16+ proto:: {
17+ ctap2:: { Ctap2PublicKeyCredentialDescriptor , Ctap2PublicKeyCredentialType } ,
18+ CtapError ,
19+ } ,
20+ webauthn:: { Error , PlatformError } ,
1921} ;
2022
2123#[ derive( Debug , PartialEq , Eq ) ]
@@ -62,28 +64,6 @@ pub struct AttestedCredentialData {
6264 pub credential_public_key : PublicKey ,
6365}
6466
65- impl Serialize for AttestedCredentialData {
66- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
67- where
68- S : Serializer ,
69- {
70- // Name | Length
71- // --------------------------------
72- // aaguid | 16
73- // credentialIdLenght | 2
74- // credentialId | L
75- // credentialPublicKey | variable
76- let mut res = self . aaguid . to_vec ( ) ;
77- res. write_u16 :: < BigEndian > ( self . credential_id . len ( ) as u16 )
78- . map_err ( SerError :: custom) ?;
79- res. extend ( & self . credential_id ) ;
80- let cose_encoded_public_key =
81- serde_cbor:: to_vec ( & self . credential_public_key ) . map_err ( SerError :: custom) ?;
82- res. extend ( cose_encoded_public_key) ;
83- serializer. serialize_bytes ( & res)
84- }
85- }
86-
8767impl From < & AttestedCredentialData > for Ctap2PublicKeyCredentialDescriptor {
8868 fn from ( data : & AttestedCredentialData ) -> Self {
8969 Self {
@@ -103,14 +83,11 @@ pub struct AuthenticatorData<T> {
10383 pub extensions : Option < T > ,
10484}
10585
106- impl < T > Serialize for AuthenticatorData < T >
86+ impl < T > AuthenticatorData < T >
10787where
10888 T : Clone + Serialize ,
10989{
110- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
111- where
112- S : Serializer ,
113- {
90+ pub fn to_response_bytes ( & self ) -> Result < Vec < u8 > , Error > {
11491 // Name | Length
11592 // -----------------------------------
11693 // rpIdHash | 32
@@ -121,14 +98,46 @@ where
12198 let mut res = self . rp_id_hash . to_vec ( ) ;
12299 res. push ( self . flags . bits ( ) ) ;
123100 res. write_u32 :: < BigEndian > ( self . signature_count )
124- . map_err ( SerError :: custom) ?;
101+ . map_err ( |e| {
102+ error ! ( "Failed to create AuthenticatorData output vec at signature_count: {e:?}" ) ;
103+ Error :: Platform ( PlatformError :: InvalidDeviceResponse )
104+ } ) ?;
105+
125106 if let Some ( att_data) = & self . attested_credential {
126- res. extend ( serde_cbor:: to_vec ( att_data) . map_err ( SerError :: custom) ?) ;
107+ // Name | Length
108+ // --------------------------------
109+ // aaguid | 16
110+ // credentialIdLenght | 2
111+ // credentialId | L
112+ // credentialPublicKey | variable
113+ res. extend ( att_data. aaguid ) ;
114+ res. write_u16 :: < BigEndian > ( att_data. credential_id . len ( ) as u16 )
115+ . map_err ( |e| {
116+ error ! (
117+ "Failed to create AuthenticatorData output vec at attested_credential.credential_id: {e:?}"
118+ ) ;
119+ Error :: Platform ( PlatformError :: InvalidDeviceResponse )
120+ } ) ?;
121+ res. extend ( & att_data. credential_id ) ;
122+ let cose_encoded_public_key =
123+ serde_cbor:: to_vec ( & att_data. credential_public_key )
124+ . map_err ( |e| {
125+ error ! (
126+ "Failed to create AuthenticatorData output vec at attested_credential.credential_public_key: {e:?}"
127+ ) ;
128+ Error :: Platform ( PlatformError :: InvalidDeviceResponse )
129+ } ) ?;
130+ res. extend ( cose_encoded_public_key) ;
127131 }
128- if let Some ( extensions) = & self . extensions {
129- res. extend ( serde_cbor:: to_vec ( extensions) . map_err ( SerError :: custom) ?) ;
132+
133+ if self . extensions . is_some ( ) || self . flags . contains ( AuthenticatorDataFlags :: EXTENSION_DATA )
134+ {
135+ res. extend ( serde_cbor:: to_vec ( & self . extensions ) . map_err ( |e| {
136+ error ! ( "Failed to create AuthenticatorData output vec at extensions: {e:?}" ) ;
137+ Error :: Platform ( PlatformError :: InvalidDeviceResponse )
138+ } ) ?) ;
130139 }
131- serializer . serialize_bytes ( & res)
140+ Ok ( res)
132141 }
133142}
134143
0 commit comments