4
4
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5
5
// http://opensource.org/licenses/MIT>, at your option. This file may not be
6
6
// copied, modified, or distributed except according to those terms.
7
- use cbor_codec:: { Config , Encoder , Decoder , GenericDecoder , GenericEncoder } ;
8
- use cbor_codec:: value:: Value ;
9
7
use cbor_codec:: value;
8
+ use cbor_codec:: value:: Value ;
9
+ use cbor_codec:: { Config , Decoder , Encoder , GenericDecoder , GenericEncoder } ;
10
10
11
- use byteorder:: { WriteBytesExt , ReadBytesExt , BigEndian , ByteOrder } ;
11
+ use byteorder:: { BigEndian , ByteOrder , ReadBytesExt , WriteBytesExt } ;
12
12
use failure:: ResultExt ;
13
13
14
14
use std:: collections:: HashMap ;
@@ -29,25 +29,23 @@ impl<'a> Request<'a> {
29
29
match self {
30
30
Request :: MakeCredential ( req) => req. encode ( & mut encoder) ,
31
31
Request :: GetAssertion ( req) => req. encode ( & mut encoder) ,
32
- Request :: GetInfo => {
33
- encoder
34
- . writer ( )
35
- . write_u8 ( 0x04 )
36
- . context ( FidoErrorKind :: CborEncode )
37
- . map_err ( From :: from)
38
- }
32
+ Request :: GetInfo => encoder
33
+ . writer ( )
34
+ . write_u8 ( 0x04 )
35
+ . context ( FidoErrorKind :: CborEncode )
36
+ . map_err ( From :: from) ,
39
37
Request :: ClientPin ( req) => req. encode ( & mut encoder) ,
40
38
}
41
39
}
42
40
43
41
pub fn decode < R : ReadBytesExt > ( & self , reader : R ) -> FidoResult < Response > {
44
42
Ok ( match self {
45
- Request :: MakeCredential ( _) => Response :: MakeCredential (
46
- MakeCredentialResponse :: decode ( reader) ?,
47
- ) ,
48
- Request :: GetAssertion ( _) => Response :: GetAssertion (
49
- GetAssertionResponse :: decode ( reader) ?,
50
- ) ,
43
+ Request :: MakeCredential ( _) => {
44
+ Response :: MakeCredential ( MakeCredentialResponse :: decode ( reader) ?)
45
+ }
46
+ Request :: GetAssertion ( _) => {
47
+ Response :: GetAssertion ( GetAssertionResponse :: decode ( reader) ?)
48
+ }
51
49
Request :: GetInfo => Response :: GetInfo ( GetInfoResponse :: decode ( reader) ?) ,
52
50
Request :: ClientPin ( _) => Response :: ClientPin ( ClientPinResponse :: decode ( reader) ?) ,
53
51
} )
@@ -77,9 +75,10 @@ pub struct MakeCredentialRequest<'a> {
77
75
78
76
impl < ' a > MakeCredentialRequest < ' a > {
79
77
pub fn encode < W : WriteBytesExt > ( & self , mut encoder : & mut Encoder < W > ) -> FidoResult < ( ) > {
80
- encoder. writer ( ) . write_u8 ( 0x01 ) . context (
81
- FidoErrorKind :: CborEncode ,
82
- ) ?; // authenticatorMakeCredential
78
+ encoder
79
+ . writer ( )
80
+ . write_u8 ( 0x01 )
81
+ . context ( FidoErrorKind :: CborEncode ) ?; // authenticatorMakeCredential
83
82
let mut length = 4 ;
84
83
length += !self . exclude_list . is_empty ( ) as usize ;
85
84
length += !self . extensions . is_empty ( ) as usize ;
@@ -176,9 +175,10 @@ pub struct GetAssertionRequest<'a> {
176
175
177
176
impl < ' a > GetAssertionRequest < ' a > {
178
177
pub fn encode < W : WriteBytesExt > ( & self , mut encoder : & mut Encoder < W > ) -> FidoResult < ( ) > {
179
- encoder. writer ( ) . write_u8 ( 0x02 ) . context (
180
- FidoErrorKind :: CborEncode ,
181
- ) ?; // authenticatorGetAssertion
178
+ encoder
179
+ . writer ( )
180
+ . write_u8 ( 0x02 )
181
+ . context ( FidoErrorKind :: CborEncode ) ?; // authenticatorGetAssertion
182
182
let mut length = 2 ;
183
183
length += !self . allow_list . is_empty ( ) as usize ;
184
184
length += !self . extensions . is_empty ( ) as usize ;
@@ -315,9 +315,10 @@ pub struct ClientPinRequest<'a> {
315
315
316
316
impl < ' a > ClientPinRequest < ' a > {
317
317
pub fn encode < W : WriteBytesExt > ( & self , encoder : & mut Encoder < W > ) -> FidoResult < ( ) > {
318
- encoder. writer ( ) . write_u8 ( 0x06 ) . context (
319
- FidoErrorKind :: CborEncode ,
320
- ) ?; // authenticatorClientPIN
318
+ encoder
319
+ . writer ( )
320
+ . write_u8 ( 0x06 )
321
+ . context ( FidoErrorKind :: CborEncode ) ?; // authenticatorClientPIN
321
322
let mut length = 2 ;
322
323
length += self . key_agreement . is_some ( ) as usize ;
323
324
length += self . pin_auth . is_some ( ) as usize ;
@@ -383,7 +384,6 @@ impl ClientPinResponse {
383
384
}
384
385
}
385
386
386
-
387
387
#[ derive( Debug ) ]
388
388
pub struct OptionsInfo {
389
389
pub plat : bool ,
@@ -439,21 +439,28 @@ impl AuthenticatorData {
439
439
let flags = bytes[ 32 ] ;
440
440
data. up = ( flags & 0x01 ) == 0x01 ;
441
441
data. uv = ( flags & 0x02 ) == 0x02 ;
442
+ let is_attested = ( flags & 0x40 ) == 0x40 ;
443
+ let has_extension_data = ( flags & 0x80 ) == 0x80 ;
442
444
data. sign_count = BigEndian :: read_u32 ( & bytes[ 33 ..37 ] ) ;
443
445
if bytes. len ( ) < 38 {
444
446
return Ok ( data) ;
445
447
}
448
+
446
449
let mut cur = Cursor :: new ( & bytes[ 37 ..] ) ;
447
- let attested_credential_data = AttestedCredentialData :: from_bytes ( & mut cur) ?;
448
- data. attested_credential_data = attested_credential_data;
449
- if cur. position ( ) >= ( bytes. len ( ) - 37 ) as u64 {
450
- return Ok ( data) ;
450
+ if is_attested {
451
+ let attested_credential_data = AttestedCredentialData :: from_bytes ( & mut cur) ?;
452
+ data. attested_credential_data = attested_credential_data;
453
+ if cur. position ( ) >= ( bytes. len ( ) - 37 ) as u64 {
454
+ return Ok ( data) ;
455
+ }
451
456
}
452
- let mut decoder = GenericDecoder :: new ( Config :: default ( ) , cur) ;
453
- for _ in 0 ..decoder. borrow_mut ( ) . object ( ) ? {
454
- let key = decoder. borrow_mut ( ) . text ( ) ?;
455
- let value = decoder. value ( ) ?;
456
- data. extensions . insert ( key. to_string ( ) , value) ;
457
+ if has_extension_data {
458
+ let mut decoder = GenericDecoder :: new ( Config :: default ( ) , cur) ;
459
+ for _ in 0 ..decoder. borrow_mut ( ) . object ( ) ? {
460
+ let key = decoder. borrow_mut ( ) . text ( ) ?;
461
+ let value = decoder. value ( ) ?;
462
+ data. extensions . insert ( key. to_string ( ) , value) ;
463
+ }
457
464
}
458
465
Ok ( data)
459
466
}
@@ -494,15 +501,15 @@ impl P256Key {
494
501
if cose. key_type != 2 || cose. algorithm != -7 {
495
502
Err ( FidoErrorKind :: KeyType ) ?
496
503
}
497
- if let ( Some ( Value :: U8 ( curve ) ) ,
498
- Some ( Value :: Bytes ( value :: Bytes :: Bytes ( x ) ) ) ,
499
- Some ( Value :: Bytes ( value:: Bytes :: Bytes ( y ) ) ) ) =
500
- (
501
- cose . parameters . get ( & - 1 ) ,
502
- cose. parameters . get ( & -2 ) ,
503
- cose. parameters . get ( & -3 ) ,
504
- )
505
- {
504
+ if let (
505
+ Some ( Value :: U8 ( curve ) ) ,
506
+ Some ( Value :: Bytes ( value:: Bytes :: Bytes ( x ) ) ) ,
507
+ Some ( Value :: Bytes ( value :: Bytes :: Bytes ( y ) ) ) ,
508
+ ) = (
509
+ cose. parameters . get ( & -1 ) ,
510
+ cose. parameters . get ( & -2 ) ,
511
+ cose . parameters . get ( & - 3 ) ,
512
+ ) {
506
513
if * curve != 1 {
507
514
Err ( FidoErrorKind :: KeyType ) ?
508
515
}
@@ -532,9 +539,10 @@ impl P256Key {
532
539
( -1 , Value :: U8 ( 1 ) ) ,
533
540
( -2 , Value :: Bytes ( value:: Bytes :: Bytes ( self . x . to_vec ( ) ) ) ) ,
534
541
( -3 , Value :: Bytes ( value:: Bytes :: Bytes ( self . y . to_vec ( ) ) ) ) ,
535
- ] . iter ( )
536
- . cloned ( )
537
- . collect ( ) ,
542
+ ]
543
+ . iter ( )
544
+ . cloned ( )
545
+ . collect ( ) ,
538
546
}
539
547
}
540
548
0 commit comments