summaryrefslogtreecommitdiffstats
path: root/third_party/rust/authenticator/src/ctap2/commands/get_info.rs
blob: f676605a0b8fa00e31e5dffc383fb2dc3bf3e760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
use super::{Command, CommandError, CtapResponse, RequestCtap2, StatusCode};
use crate::ctap2::attestation::AAGuid;
use crate::ctap2::server::PublicKeyCredentialParameters;
use crate::transport::errors::HIDError;
use crate::transport::{FidoDevice, VirtualFidoDevice};
use serde::{
    de::{Error as SError, IgnoredAny, MapAccess, Visitor},
    Deserialize, Deserializer, Serialize,
};
use serde_cbor::{de::from_slice, Value};
use std::collections::BTreeMap;
use std::fmt;

#[derive(Debug, Default)]
pub struct GetInfo {}

impl RequestCtap2 for GetInfo {
    type Output = AuthenticatorInfo;

    fn command(&self) -> Command {
        Command::GetInfo
    }

    fn wire_format(&self) -> Result<Vec<u8>, HIDError> {
        Ok(Vec::new())
    }

    fn handle_response_ctap2<Dev: FidoDevice>(
        &self,
        _dev: &mut Dev,
        input: &[u8],
    ) -> Result<Self::Output, HIDError> {
        if input.is_empty() {
            return Err(CommandError::InputTooSmall.into());
        }

        let status: StatusCode = input[0].into();

        if input.len() > 1 {
            if status.is_ok() {
                trace!("parsing authenticator info data: {:#04X?}", &input);
                let authenticator_info =
                    from_slice(&input[1..]).map_err(CommandError::Deserializing)?;
                Ok(authenticator_info)
            } else {
                let data: Value = from_slice(&input[1..]).map_err(CommandError::Deserializing)?;
                Err(CommandError::StatusCode(status, Some(data)).into())
            }
        } else {
            Err(CommandError::InputTooSmall.into())
        }
    }

    fn send_to_virtual_device<Dev: VirtualFidoDevice>(
        &self,
        dev: &mut Dev,
    ) -> Result<Self::Output, HIDError> {
        dev.get_info()
    }
}

fn true_val() -> bool {
    true
}

#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Serialize)]
pub struct AuthenticatorOptions {
    /// Indicates that the device is attached to the client and therefore can’t
    /// be removed and used on another client.
    #[serde(rename = "plat", default)]
    pub platform_device: bool,
    /// Indicates that the device is capable of storing keys on the device
    /// itself and therefore can satisfy the authenticatorGetAssertion request
    /// with allowList parameter not specified or empty.
    #[serde(rename = "rk", default)]
    pub resident_key: bool,

    /// Client PIN:
    ///  If present and set to true, it indicates that the device is capable of
    ///   accepting a PIN from the client and PIN has been set.
    ///  If present and set to false, it indicates that the device is capable of
    ///   accepting a PIN from the client and PIN has not been set yet.
    ///  If absent, it indicates that the device is not capable of accepting a
    ///   PIN from the client.
    /// Client PIN is one of the ways to do user verification.
    #[serde(rename = "clientPin")]
    pub client_pin: Option<bool>,

    /// Indicates that the device is capable of testing user presence.
    #[serde(rename = "up", default = "true_val")]
    pub user_presence: bool,

    /// Indicates that the device is capable of verifying the user within
    /// itself. For example, devices with UI, biometrics fall into this
    /// category.
    ///  If present and set to true, it indicates that the device is capable of
    ///   user verification within itself and has been configured.
    ///  If present and set to false, it indicates that the device is capable of
    ///   user verification within itself and has not been yet configured. For
    ///   example, a biometric device that has not yet been configured will
    ///   return this parameter set to false.
    ///  If absent, it indicates that the device is not capable of user
    ///   verification within itself.
    /// A device that can only do Client PIN will not return the "uv" parameter.
    /// If a device is capable of verifying the user within itself as well as
    /// able to do Client PIN, it will return both "uv" and the Client PIN
    /// option.
    // TODO(MS): My Token (key-ID FIDO2) does return Some(false) here, even though
    //           it has no built-in verification method. Not to be trusted...
    #[serde(rename = "uv")]
    pub user_verification: Option<bool>,

    // ----------------------------------------------------
    // CTAP 2.1 options
    // ----------------------------------------------------
    /// If pinUvAuthToken is:
    /// present and set to true
    ///  if the clientPin option id is present and set to true, then the
    ///  authenticator supports authenticatorClientPIN's getPinUvAuthTokenUsingPinWithPermissions
    ///  subcommand. If the uv option id is present and set to true, then
    ///  the authenticator supports authenticatorClientPIN's getPinUvAuthTokenUsingUvWithPermissions
    ///  subcommand.
    /// present and set to false, or absent.
    ///  the authenticator does not support authenticatorClientPIN's
    ///  getPinUvAuthTokenUsingPinWithPermissions and getPinUvAuthTokenUsingUvWithPermissions
    ///  subcommands.
    #[serde(rename = "pinUvAuthToken")]
    pub pin_uv_auth_token: Option<bool>,

    /// If this noMcGaPermissionsWithClientPin is:
    /// present and set to true
    ///  A pinUvAuthToken obtained via getPinUvAuthTokenUsingPinWithPermissions
    ///  (or getPinToken) cannot be used for authenticatorMakeCredential or
    ///  authenticatorGetAssertion commands, because it will lack the necessary
    ///  mc and ga permissions. In this situation, platforms SHOULD NOT attempt
    ///  to use getPinUvAuthTokenUsingPinWithPermissions if using
    ///  getPinUvAuthTokenUsingUvWithPermissions fails.
    /// present and set to false, or absent.
    ///  A pinUvAuthToken obtained via getPinUvAuthTokenUsingPinWithPermissions
    ///  (or getPinToken) can be used for authenticatorMakeCredential or
    ///  authenticatorGetAssertion commands.
    /// Note: noMcGaPermissionsWithClientPin MUST only be present if the
    ///       clientPin option ID is present.
    #[serde(rename = "noMcGaPermissionsWithClientPin")]
    pub no_mc_ga_permissions_with_client_pin: Option<bool>,

    /// If largeBlobs is:
    /// present and set to true
    ///  the authenticator supports the authenticatorLargeBlobs command.
    /// present and set to false, or absent.
    ///  The authenticatorLargeBlobs command is NOT supported.
    #[serde(rename = "largeBlobs")]
    pub large_blobs: Option<bool>,

    /// Enterprise Attestation feature support:
    /// If ep is:
    /// Present and set to true
    ///  The authenticator is enterprise attestation capable, and enterprise
    ///  attestation is enabled.
    /// Present and set to false
    ///  The authenticator is enterprise attestation capable, and enterprise
    ///  attestation is disabled.
    /// Absent
    ///  The Enterprise Attestation feature is NOT supported.
    #[serde(rename = "ep")]
    pub ep: Option<bool>,

    /// If bioEnroll is:
    /// present and set to true
    ///  the authenticator supports the authenticatorBioEnrollment commands,
    ///  and has at least one bio enrollment presently provisioned.
    /// present and set to false
    ///  the authenticator supports the authenticatorBioEnrollment commands,
    ///  and does not yet have any bio enrollments provisioned.
    /// absent
    ///  the authenticatorBioEnrollment commands are NOT supported.
    #[serde(rename = "bioEnroll")]
    pub bio_enroll: Option<bool>,

    /// "FIDO_2_1_PRE" Prototype Credential management support:
    /// If userVerificationMgmtPreview is:
    /// present and set to true
    ///  the authenticator supports the Prototype authenticatorBioEnrollment (0x41)
    ///  commands, and has at least one bio enrollment presently provisioned.
    /// present and set to false
    ///  the authenticator supports the Prototype authenticatorBioEnrollment (0x41)
    ///  commands, and does not yet have any bio enrollments provisioned.
    /// absent
    ///  the Prototype authenticatorBioEnrollment (0x41) commands are not supported.
    #[serde(rename = "userVerificationMgmtPreview")]
    pub user_verification_mgmt_preview: Option<bool>,

    /// getPinUvAuthTokenUsingUvWithPermissions support for requesting the be permission:
    /// This option ID MUST only be present if bioEnroll is also present.
    /// If uvBioEnroll is:
    /// present and set to true
    ///  requesting the be permission when invoking getPinUvAuthTokenUsingUvWithPermissions
    ///  is supported.
    /// present and set to false, or absent.
    ///  requesting the be permission when invoking getPinUvAuthTokenUsingUvWithPermissions
    ///  is NOT supported.
    #[serde(rename = "uvBioEnroll")]
    pub uv_bio_enroll: Option<bool>,

    /// authenticatorConfig command support:
    /// If authnrCfg is:
    /// present and set to true
    ///  the authenticatorConfig command is supported.
    /// present and set to false, or absent.
    ///  the authenticatorConfig command is NOT supported.
    #[serde(rename = "authnrCfg")]
    pub authnr_cfg: Option<bool>,

    /// getPinUvAuthTokenUsingUvWithPermissions support for requesting the acfg permission:
    /// This option ID MUST only be present if authnrCfg is also present.
    /// If uvAcfg is:
    /// present and set to true
    ///  requesting the acfg permission when invoking getPinUvAuthTokenUsingUvWithPermissions
    ///  is supported.
    /// present and set to false, or absent.
    ///  requesting the acfg permission when invoking getPinUvAuthTokenUsingUvWithPermissions
    ///  is NOT supported.
    #[serde(rename = "uvAcfg")]
    pub uv_acfg: Option<bool>,

    /// Credential management support:
    /// If credMgmt is:
    /// present and set to true
    ///  the authenticatorCredentialManagement command is supported.
    /// present and set to false, or absent.
    ///  the authenticatorCredentialManagement command is NOT supported.
    #[serde(rename = "credMgmt")]
    pub cred_mgmt: Option<bool>,

    /// "FIDO_2_1_PRE" Prototype Credential management support:
    /// If credentialMgmtPreview is:
    /// present and set to true
    ///  the Prototype authenticatorCredentialManagement (0x41) command is supported.
    /// present and set to false, or absent.
    ///  the Prototype authenticatorCredentialManagement (0x41) command is NOT supported.
    #[serde(rename = "credentialMgmtPreview")]
    pub credential_mgmt_preview: Option<bool>,

    /// Support for the Set Minimum PIN Length feature.
    /// If setMinPINLength is:
    /// present and set to true
    ///  the setMinPINLength subcommand is supported.
    /// present and set to false, or absent.
    ///  the setMinPINLength subcommand is NOT supported.
    /// Note: setMinPINLength MUST only be present if the clientPin option ID is present.
    #[serde(rename = "setMinPINLength")]
    pub set_min_pin_length: Option<bool>,

    /// Support for making non-discoverable credentials without requiring User Verification.
    /// If makeCredUvNotRqd is:
    /// present and set to true
    ///  the authenticator allows creation of non-discoverable credentials without
    ///  requiring any form of user verification, if the platform requests this behaviour.
    /// present and set to false, or absent.
    ///  the authenticator requires some form of user verification for creating
    ///  non-discoverable credentials, regardless of the parameters the platform supplies
    ///  for the authenticatorMakeCredential command.
    /// Authenticators SHOULD include this option with the value true.
    #[serde(rename = "makeCredUvNotRqd")]
    pub make_cred_uv_not_rqd: Option<bool>,

    /// Support for the Always Require User Verification feature:
    /// If alwaysUv is
    /// present and set to true
    ///  the authenticator supports the Always Require User Verification feature and it is enabled.
    /// present and set to false
    ///  the authenticator supports the Always Require User Verification feature but it is disabled.
    /// absent
    ///  the authenticator does not support the Always Require User Verification feature.
    /// Note: If the alwaysUv option ID is present and true the authenticator MUST set the value
    ///       of makeCredUvNotRqd to false.
    #[serde(rename = "alwaysUv")]
    pub always_uv: Option<bool>,
}

impl Default for AuthenticatorOptions {
    fn default() -> Self {
        AuthenticatorOptions {
            platform_device: false,
            resident_key: false,
            client_pin: None,
            user_presence: true,
            user_verification: None,
            pin_uv_auth_token: None,
            no_mc_ga_permissions_with_client_pin: None,
            large_blobs: None,
            ep: None,
            bio_enroll: None,
            user_verification_mgmt_preview: None,
            uv_bio_enroll: None,
            authnr_cfg: None,
            uv_acfg: None,
            cred_mgmt: None,
            credential_mgmt_preview: None,
            set_min_pin_length: None,
            make_cred_uv_not_rqd: None,
            always_uv: None,
        }
    }
}

#[allow(non_camel_case_types)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum AuthenticatorVersion {
    U2F_V2,
    FIDO_2_0,
    FIDO_2_1_PRE,
    FIDO_2_1,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
pub struct AuthenticatorInfo {
    pub versions: Vec<AuthenticatorVersion>,
    pub extensions: Vec<String>,
    pub aaguid: AAGuid,
    pub options: AuthenticatorOptions,
    pub max_msg_size: Option<usize>,
    pub pin_protocols: Option<Vec<u64>>,
    // CTAP 2.1
    pub max_credential_count_in_list: Option<usize>,
    pub max_credential_id_length: Option<usize>,
    pub transports: Option<Vec<String>>,
    pub algorithms: Option<Vec<PublicKeyCredentialParameters>>,
    pub max_ser_large_blob_array: Option<u64>,
    pub force_pin_change: Option<bool>,
    pub min_pin_length: Option<u64>,
    pub firmware_version: Option<u64>,
    pub max_cred_blob_length: Option<u64>,
    pub max_rpids_for_set_min_pin_length: Option<u64>,
    pub preferred_platform_uv_attempts: Option<u64>,
    pub uv_modality: Option<u64>,
    pub certifications: Option<BTreeMap<String, u64>>,
    pub remaining_discoverable_credentials: Option<u64>,
    pub vendor_prototype_config_commands: Option<Vec<u64>>,
}

impl AuthenticatorInfo {
    pub fn supports_cred_protect(&self) -> bool {
        self.extensions.contains(&"credProtect".to_string())
    }

    pub fn supports_hmac_secret(&self) -> bool {
        self.extensions.contains(&"hmac-secret".to_string())
    }

    pub fn max_supported_version(&self) -> AuthenticatorVersion {
        let versions = vec![
            AuthenticatorVersion::FIDO_2_1,
            AuthenticatorVersion::FIDO_2_1_PRE,
            AuthenticatorVersion::FIDO_2_0,
            AuthenticatorVersion::U2F_V2,
        ];
        for ver in versions {
            if self.versions.contains(&ver) {
                return ver;
            }
        }
        AuthenticatorVersion::U2F_V2
    }

    pub fn device_is_protected(&self) -> bool {
        self.options.client_pin == Some(true) || self.options.user_verification == Some(true)
    }
}

impl CtapResponse for AuthenticatorInfo {}

macro_rules! parse_next_optional_value {
    ($name:expr, $map:expr) => {
        if $name.is_some() {
            return Err(serde::de::Error::duplicate_field("$name"));
        }
        $name = Some($map.next_value()?);
    };
}

impl<'de> Deserialize<'de> for AuthenticatorInfo {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct AuthenticatorInfoVisitor;

        impl<'de> Visitor<'de> for AuthenticatorInfoVisitor {
            type Value = AuthenticatorInfo;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a byte array")
            }

            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
            where
                M: MapAccess<'de>,
            {
                let mut versions = Vec::new();
                let mut extensions = Vec::new();
                let mut aaguid = None;
                let mut options = AuthenticatorOptions::default();
                let mut max_msg_size = None;
                let mut pin_protocols: Option<Vec<_>> = None;
                let mut max_credential_count_in_list = None;
                let mut max_credential_id_length = None;
                let mut transports = None;
                let mut algorithms = None;
                let mut max_ser_large_blob_array = None;
                let mut force_pin_change = None;
                let mut min_pin_length = None;
                let mut firmware_version = None;
                let mut max_cred_blob_length = None;
                let mut max_rpids_for_set_min_pin_length = None;
                let mut preferred_platform_uv_attempts = None;
                let mut uv_modality = None;
                let mut certifications = None;
                let mut remaining_discoverable_credentials = None;
                let mut vendor_prototype_config_commands = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        0x01 => {
                            if !versions.is_empty() {
                                return Err(serde::de::Error::duplicate_field("versions"));
                            }
                            versions = map.next_value()?;
                        }
                        0x02 => {
                            if !extensions.is_empty() {
                                return Err(serde::de::Error::duplicate_field("extensions"));
                            }
                            extensions = map.next_value()?;
                        }
                        0x03 => {
                            parse_next_optional_value!(aaguid, map);
                        }
                        0x04 => {
                            options = map.next_value()?;
                        }
                        0x05 => {
                            parse_next_optional_value!(max_msg_size, map);
                        }
                        0x06 => {
                            parse_next_optional_value!(pin_protocols, map);
                        }
                        0x07 => {
                            parse_next_optional_value!(max_credential_count_in_list, map);
                        }
                        0x08 => {
                            parse_next_optional_value!(max_credential_id_length, map);
                        }
                        0x09 => {
                            parse_next_optional_value!(transports, map);
                        }
                        0x0a => {
                            parse_next_optional_value!(algorithms, map);
                        }
                        0x0b => {
                            parse_next_optional_value!(max_ser_large_blob_array, map);
                        }
                        0x0c => {
                            parse_next_optional_value!(force_pin_change, map);
                        }
                        0x0d => {
                            parse_next_optional_value!(min_pin_length, map);
                        }
                        0x0e => {
                            parse_next_optional_value!(firmware_version, map);
                        }
                        0x0f => {
                            parse_next_optional_value!(max_cred_blob_length, map);
                        }
                        0x10 => {
                            parse_next_optional_value!(max_rpids_for_set_min_pin_length, map);
                        }
                        0x11 => {
                            parse_next_optional_value!(preferred_platform_uv_attempts, map);
                        }
                        0x12 => {
                            parse_next_optional_value!(uv_modality, map);
                        }
                        0x13 => {
                            parse_next_optional_value!(certifications, map);
                        }
                        0x14 => {
                            parse_next_optional_value!(remaining_discoverable_credentials, map);
                        }
                        0x15 => {
                            parse_next_optional_value!(vendor_prototype_config_commands, map);
                        }
                        k => {
                            warn!("GetInfo: unexpected key: {:?}", k);
                            let _ = map.next_value::<IgnoredAny>()?;
                            continue;
                        }
                    }
                }

                if versions.is_empty() {
                    return Err(M::Error::custom(
                        "expected at least one version, got none".to_string(),
                    ));
                }

                if let Some(protocols) = &pin_protocols {
                    if protocols.is_empty() {
                        return Err(M::Error::custom(
                            "Token returned empty PIN protocol list, which is not allowed"
                                .to_string(),
                        ));
                    }
                }

                if let Some(aaguid) = aaguid {
                    Ok(AuthenticatorInfo {
                        versions,
                        extensions,
                        aaguid,
                        options,
                        max_msg_size,
                        pin_protocols,
                        max_credential_count_in_list,
                        max_credential_id_length,
                        transports,
                        algorithms,
                        max_ser_large_blob_array,
                        force_pin_change,
                        min_pin_length,
                        firmware_version,
                        max_cred_blob_length,
                        max_rpids_for_set_min_pin_length,
                        preferred_platform_uv_attempts,
                        uv_modality,
                        certifications,
                        remaining_discoverable_credentials,
                        vendor_prototype_config_commands,
                    })
                } else {
                    Err(M::Error::custom("No AAGuid specified".to_string()))
                }
            }
        }

        deserializer.deserialize_bytes(AuthenticatorInfoVisitor)
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::consts::{Capability, HIDCmd, CID_BROADCAST};
    use crate::crypto::COSEAlgorithm;
    use crate::transport::device_selector::Device;
    use crate::transport::platform::device::IN_HID_RPT_SIZE;
    use crate::transport::{hid::HIDDevice, FidoDevice, FidoProtocol};
    use rand::{thread_rng, RngCore};
    use serde_cbor::de::from_slice;

    // Raw data take from https://github.com/Yubico/python-fido2/blob/master/test/test_ctap2.py
    pub const AAGUID_RAW: [u8; 16] = [
        0xF8, 0xA0, 0x11, 0xF3, 0x8C, 0x0A, 0x4D, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1F, 0x9E, 0xDC,
        0x7D,
    ];

    pub const AUTHENTICATOR_INFO_PAYLOAD: [u8; 89] = [
        0xa6, // map(6)
        0x01, //   unsigned(1)
        0x82, //   array(2)
        0x66, //     text(6)
        0x55, 0x32, 0x46, 0x5f, 0x56, 0x32, // "U2F_V2"
        0x68, //     text(8)
        0x46, 0x49, 0x44, 0x4f, 0x5f, 0x32, 0x5f, 0x30, //  "FIDO_2_0"
        0x02, //   unsigned(2)
        0x82, //   array(2)
        0x63, //     text(3)
        0x75, 0x76, 0x6d, // "uvm"
        0x6b, //     text(11)
        0x68, 0x6d, 0x61, 0x63, 0x2d, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, // "hmac-secret"
        0x03, //  unsigned(3)
        0x50, //  bytes(16)
        0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc,
        0x7d, // "\xF8\xA0\u0011\xF3\x8C\nM\u0015\x80\u0006\u0017\u0011\u001F\x9E\xDC}"
        0x04, //  unsigned(4)
        0xa4, //  map(4)
        0x62, //    text(2)
        0x72, 0x6b, //  "rk"
        0xf5, //    primitive(21)
        0x62, //    text(2)
        0x75, 0x70, // "up"
        0xf5, //    primitive(21)
        0x64, //    text(4)
        0x70, 0x6c, 0x61, 0x74, // "plat"
        0xf4, //    primitive(20)
        0x69, //    text(9)
        0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x69, 0x6e, // "clientPin"
        0xf4, //    primitive(20)
        0x05, //  unsigned(5)
        0x19, 0x04, 0xb0, // unsigned(1200)
        0x06, //  unsigned(6)
        0x81, //  array(1)
        0x01, //    unsigned(1)
    ];

    // Real world example from Yubikey Bio
    pub const AUTHENTICATOR_INFO_PAYLOAD_YK_BIO_5C: [u8; 409] = [
        0xB3, // map(19)
        0x01, //   unsigned(1)
        0x84, //     array(4)
        0x66, //       text(6)
        0x55, 0x32, 0x46, 0x5F, 0x56, 0x32, // "U2F_V2"
        0x68, //       text(8)
        0x46, 0x49, 0x44, 0x4F, 0x5F, 0x32, 0x5F, 0x30, // "FIDO_2_0"
        0x6C, //       text(12)
        0x46, 0x49, 0x44, 0x4F, 0x5F, 0x32, 0x5F, 0x31, 0x5F, 0x50, 0x52,
        0x45, //         "FIDO_2_1_PRE"
        0x68, //       text(8)
        0x46, 0x49, 0x44, 0x4F, 0x5F, 0x32, 0x5F, 0x31, // "FIDO_2_1"
        0x02, //   unsigned(2)
        0x85, //   array(5)
        0x6B, //     text(11)
        0x63, 0x72, 0x65, 0x64, 0x50, 0x72, 0x6F, 0x74, 0x65, 0x63, 0x74, // "credProtect"
        0x6B, //     text(11)
        0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, // "hmac-secret"
        0x6C, //     text(12)
        0x6C, 0x61, 0x72, 0x67, 0x65, 0x42, 0x6C, 0x6F, 0x62, 0x4B, 0x65,
        0x79, //       "largeBlobKey"
        0x68, //     text(8)
        0x63, 0x72, 0x65, 0x64, 0x42, 0x6C, 0x6F, 0x62, // "credBlob"
        0x6C, //     text(12)
        0x6D, 0x69, 0x6E, 0x50, 0x69, 0x6E, 0x4C, 0x65, 0x6E, 0x67, 0x74,
        0x68, //       "minPinLength"
        0x03, //   unsigned(3)
        0x50, //   bytes(16)
        0xD8, 0x52, 0x2D, 0x9F, 0x57, 0x5B, 0x48, 0x66, 0x88, 0xA9, 0xBA, 0x99, 0xFA, 0x02, 0xF3,
        0x5B, //     "\xD8R-\x9FW[Hf\x88\xA9\xBA\x99\xFA\u0002\xF3["
        0x04, //   unsigned(4)
        0xB0, //   map(16)
        0x62, //     text(2)
        0x72, 0x6B, // "rk"
        0xF5, //     primitive(21)
        0x62, //     text(2)
        0x75, 0x70, // "up"
        0xF5, //     primitive(21)
        0x62, //     text(2)
        0x75, 0x76, // "uv"
        0xF5, //     primitive(21)
        0x64, //     text(4)
        0x70, 0x6C, 0x61, 0x74, // "plat"
        0xF4, //     primitive(20)
        0x67, //     text(7)
        0x75, 0x76, 0x54, 0x6F, 0x6B, 0x65, 0x6E, // "uvToken"
        0xF5, //     primitive(21)
        0x68, //     text(8)
        0x61, 0x6C, 0x77, 0x61, 0x79, 0x73, 0x55, 0x76, // "alwaysUv"
        0xF5, //     primitive(21)
        0x68, //     text(8)
        0x63, 0x72, 0x65, 0x64, 0x4D, 0x67, 0x6D, 0x74, // "credMgmt"
        0xF5, //     primitive(21)
        0x69, //     text(9)
        0x61, 0x75, 0x74, 0x68, 0x6E, 0x72, 0x43, 0x66, 0x67, // "authnrCfg"
        0xF5, //     primitive(21)
        0x69, //     text(9)
        0x62, 0x69, 0x6F, 0x45, 0x6E, 0x72, 0x6F, 0x6C, 0x6C, // "bioEnroll"
        0xF5, //     primitive(21)
        0x69, //     text(9)
        0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x50, 0x69, 0x6E, // "clientPin"
        0xF5, //     primitive(21)
        0x6A, //     text(10)
        0x6C, 0x61, 0x72, 0x67, 0x65, 0x42, 0x6C, 0x6F, 0x62, 0x73, // "largeBlobs"
        0xF5, //     primitive(21)
        0x6E, //     text(14)
        0x70, 0x69, 0x6E, 0x55, 0x76, 0x41, 0x75, 0x74, 0x68, 0x54, 0x6F, 0x6B, 0x65,
        0x6E, //       "pinUvAuthToken"
        0xF5, //     primitive(21)
        0x6F, //     text(15)
        0x73, 0x65, 0x74, 0x4D, 0x69, 0x6E, 0x50, 0x49, 0x4E, 0x4C, 0x65, 0x6E, 0x67, 0x74,
        0x68, //       "setMinPINLength"
        0xF5, //     primitive(21)
        0x70, //     text(16)
        0x6D, 0x61, 0x6B, 0x65, 0x43, 0x72, 0x65, 0x64, 0x55, 0x76, 0x4E, 0x6F, 0x74, 0x52, 0x71,
        0x64, //       "makeCredUvNotRqd"
        0xF4, //     primitive(20)
        0x75, //     text(21)
        0x63, 0x72, 0x65, 0x64, 0x65, 0x6E, 0x74, 0x69, 0x61, 0x6C, 0x4D, 0x67, 0x6D, 0x74, 0x50,
        0x72, 0x65, 0x76, 0x69, 0x65, 0x77, // "credentialMgmtPreview"
        0xF5, //     primitive(21)
        0x78, 0x1B, // text(27)
        0x75, 0x73, 0x65, 0x72, 0x56, 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F,
        0x6E, 0x4D, 0x67, 0x6D, 0x74, 0x50, 0x72, 0x65, 0x76, 0x69, 0x65,
        0x77, //       "userVerificationMgmtPreview"
        0xF5, //     primitive(21)
        0x05, //   unsigned(5)
        0x19, 0x04, 0xB0, // unsigned(1200)
        0x06, //   unsigned(6)
        0x82, //   array(2)
        0x02, //     unsigned(2)
        0x01, //     unsigned(1)
        0x07, //   unsigned(7)
        0x08, //   unsigned(8)
        0x08, //   unsigned(8)
        0x18, 0x80, // unsigned(128)
        0x09, //   unsigned(9)
        0x81, //   array(1)
        0x63, //     text(3)
        0x75, 0x73, 0x62, // "usb"
        0x0A, //   unsigned(10)
        0x82, //   array(2)
        0xA2, //     map(2)
        0x63, //       text(3)
        0x61, 0x6C, 0x67, // "alg"
        0x26, //     negative(6)
        0x64, //     text(4)
        0x74, 0x79, 0x70, 0x65, // "type"
        0x6A, //     text(10)
        0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x2D, 0x6B, 0x65, 0x79, // "public-key"
        0xA2, //     map(2)
        0x63, //       text(3)
        0x61, 0x6C, 0x67, // "alg"
        0x27, //     negative(7)
        0x64, //     text(4)
        0x74, 0x79, 0x70, 0x65, // "type"
        0x6A, //     text(10)
        0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x2D, 0x6B, 0x65, 0x79, // "public-key"
        0x0B, //  unsigned(11)
        0x19, 0x04, 0x00, // unsigned(1024)
        0x0C, //   unsigned(12)
        0xF4, //   primitive(20)
        0x0D, //   unsigned(13)
        0x04, //   unsigned(4)
        0x0E, //   unsigned(14)
        0x1A, 0x00, 0x05, 0x05, 0x06, // unsigned(328966)
        0x0F, //   unsigned(15)
        0x18, 0x20, // unsigned(32)
        0x10, //   unsigned(16)
        0x01, //   unsigned(1)
        0x11, //   unsigned(17)
        0x03, //   unsigned(3)
        0x12, //   unsigned(18)
        0x02, //   unsigned(2)
        0x14, //   unsigned(20)
        0x18, 0x18, // unsigned(24)
    ];

    #[test]
    fn parse_authenticator_info() {
        let authenticator_info: AuthenticatorInfo =
            from_slice(&AUTHENTICATOR_INFO_PAYLOAD).unwrap();

        let expected = AuthenticatorInfo {
            versions: vec![AuthenticatorVersion::U2F_V2, AuthenticatorVersion::FIDO_2_0],
            extensions: vec!["uvm".to_string(), "hmac-secret".to_string()],
            aaguid: AAGuid(AAGUID_RAW),
            options: AuthenticatorOptions {
                platform_device: false,
                resident_key: true,
                client_pin: Some(false),
                user_presence: true,
                user_verification: None,
                ..Default::default()
            },
            max_msg_size: Some(1200),
            pin_protocols: Some(vec![1]),
            max_credential_count_in_list: None,
            max_credential_id_length: None,
            transports: None,
            algorithms: None,
            max_ser_large_blob_array: None,
            force_pin_change: None,
            min_pin_length: None,
            firmware_version: None,
            max_cred_blob_length: None,
            max_rpids_for_set_min_pin_length: None,
            preferred_platform_uv_attempts: None,
            uv_modality: None,
            certifications: None,
            remaining_discoverable_credentials: None,
            vendor_prototype_config_commands: None,
        };

        assert_eq!(authenticator_info, expected);

        // Test broken auth info
        let mut broken_payload = AUTHENTICATOR_INFO_PAYLOAD.to_vec();
        // Have one more entry in the map
        broken_payload[0] += 1;
        // Add the additional entry at the back with an invalid key
        broken_payload.extend_from_slice(&[
            0x17, // unsigned(23) -> invalid key-number. CTAP2.1 goes only to 0x15
            0x6B, //   text(11)
            0x69, 0x6E, 0x76, 0x61, 0x6C, 0x69, 0x64, 0x5F, 0x6B, 0x65, 0x79, // "invalid_key"
        ]);

        let authenticator_info: AuthenticatorInfo = from_slice(&broken_payload).unwrap();
        assert_eq!(authenticator_info, expected);
    }

    #[test]
    fn parse_authenticator_info_yk_bio_5c() {
        let authenticator_info: AuthenticatorInfo =
            from_slice(&AUTHENTICATOR_INFO_PAYLOAD_YK_BIO_5C).unwrap();

        let expected = AuthenticatorInfo {
            versions: vec![
                AuthenticatorVersion::U2F_V2,
                AuthenticatorVersion::FIDO_2_0,
                AuthenticatorVersion::FIDO_2_1_PRE,
                AuthenticatorVersion::FIDO_2_1,
            ],
            extensions: vec![
                "credProtect".to_string(),
                "hmac-secret".to_string(),
                "largeBlobKey".to_string(),
                "credBlob".to_string(),
                "minPinLength".to_string(),
            ],
            aaguid: AAGuid([
                0xd8, 0x52, 0x2d, 0x9f, 0x57, 0x5b, 0x48, 0x66, 0x88, 0xa9, 0xba, 0x99, 0xfa, 0x02,
                0xf3, 0x5b,
            ]),
            options: AuthenticatorOptions {
                platform_device: false,
                resident_key: true,
                client_pin: Some(true),
                user_presence: true,
                user_verification: Some(true),
                pin_uv_auth_token: Some(true),
                no_mc_ga_permissions_with_client_pin: None,
                large_blobs: Some(true),
                ep: None,
                bio_enroll: Some(true),
                user_verification_mgmt_preview: Some(true),
                uv_bio_enroll: None,
                authnr_cfg: Some(true),
                uv_acfg: None,
                cred_mgmt: Some(true),
                credential_mgmt_preview: Some(true),
                set_min_pin_length: Some(true),
                make_cred_uv_not_rqd: Some(false),
                always_uv: Some(true),
            },
            max_msg_size: Some(1200),
            pin_protocols: Some(vec![2, 1]),
            max_credential_count_in_list: Some(8),
            max_credential_id_length: Some(128),
            transports: Some(vec!["usb".to_string()]),
            algorithms: Some(vec![
                PublicKeyCredentialParameters {
                    alg: COSEAlgorithm::ES256,
                },
                PublicKeyCredentialParameters {
                    alg: COSEAlgorithm::EDDSA,
                },
            ]),
            max_ser_large_blob_array: Some(1024),
            force_pin_change: Some(false),
            min_pin_length: Some(4),
            firmware_version: Some(328966),
            max_cred_blob_length: Some(32),
            max_rpids_for_set_min_pin_length: Some(1),
            preferred_platform_uv_attempts: Some(3),
            uv_modality: Some(2),
            certifications: None,
            remaining_discoverable_credentials: Some(24),
            vendor_prototype_config_commands: None,
        };

        assert_eq!(authenticator_info, expected);
    }

    #[test]
    fn test_get_info_ctap2_only() {
        let mut device = Device::new("commands/get_info").unwrap();
        assert_eq!(device.get_protocol(), FidoProtocol::CTAP2);
        let nonce = [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01];

        // channel id
        let mut cid = [0u8; 4];
        thread_rng().fill_bytes(&mut cid);

        // init packet
        let mut msg = CID_BROADCAST.to_vec();
        msg.extend(vec![HIDCmd::Init.into(), 0x00, 0x08]); // cmd + bcnt
        msg.extend_from_slice(&nonce);
        device.add_write(&msg, 0);

        // init_resp packet
        let mut msg = CID_BROADCAST.to_vec();
        msg.extend(vec![
            0x06, /* HIDCmd::Init without TYPE_INIT */
            0x00, 0x11,
        ]); // cmd + bcnt
        msg.extend_from_slice(&nonce);
        msg.extend_from_slice(&cid); // new channel id

        // We are setting NMSG, to signal that the device does not support CTAP1
        msg.extend(vec![0x02, 0x04, 0x01, 0x08, 0x01 | 0x04 | 0x08]); // versions + flags (wink+cbor+nmsg)
        device.add_read(&msg, 0);

        // ctap2 request
        let mut msg = cid.to_vec();
        msg.extend(vec![HIDCmd::Cbor.into(), 0x00, 0x1]); // cmd + bcnt
        msg.extend(vec![0x04]); // authenticatorGetInfo
        device.add_write(&msg, 0);

        // ctap2 response
        let mut msg = cid.to_vec();
        msg.extend(vec![HIDCmd::Cbor.into(), 0x00, 0x5A]); // cmd + bcnt
        msg.extend(vec![0]); // Status code: Success
        msg.extend(&AUTHENTICATOR_INFO_PAYLOAD[0..(IN_HID_RPT_SIZE - 8)]);
        device.add_read(&msg, 0);
        // Continuation package
        let mut msg = cid.to_vec();
        msg.extend(vec![0x00]); // SEQ
        msg.extend(&AUTHENTICATOR_INFO_PAYLOAD[(IN_HID_RPT_SIZE - 8)..]);
        device.add_read(&msg, 0);
        device.init().expect("Failed to init device");

        assert_eq!(device.get_cid(), &cid);

        let dev_info = device.get_device_info();
        assert_eq!(
            dev_info.cap_flags,
            Capability::WINK | Capability::CBOR | Capability::NMSG
        );

        let result = device
            .get_authenticator_info()
            .expect("Didn't get any authenticator_info");
        let expected = AuthenticatorInfo {
            versions: vec![AuthenticatorVersion::U2F_V2, AuthenticatorVersion::FIDO_2_0],
            extensions: vec!["uvm".to_string(), "hmac-secret".to_string()],
            aaguid: AAGuid(AAGUID_RAW),
            options: AuthenticatorOptions {
                platform_device: false,
                resident_key: true,
                client_pin: Some(false),
                user_presence: true,
                user_verification: None,
                ..Default::default()
            },
            max_msg_size: Some(1200),
            pin_protocols: Some(vec![1]),
            max_credential_count_in_list: None,
            max_credential_id_length: None,
            transports: None,
            algorithms: None,
            max_ser_large_blob_array: None,
            force_pin_change: None,
            min_pin_length: None,
            firmware_version: None,
            max_cred_blob_length: None,
            max_rpids_for_set_min_pin_length: None,
            preferred_platform_uv_attempts: None,
            uv_modality: None,
            certifications: None,
            remaining_discoverable_credentials: None,
            vendor_prototype_config_commands: None,
        };

        assert_eq!(result, &expected);
    }

    #[test]
    fn test_authenticator_info_max_version() {
        let fido2_0 = AuthenticatorInfo {
            versions: vec![AuthenticatorVersion::U2F_V2, AuthenticatorVersion::FIDO_2_0],
            ..Default::default()
        };
        assert_eq!(
            fido2_0.max_supported_version(),
            AuthenticatorVersion::FIDO_2_0
        );

        let fido2_1_pre = AuthenticatorInfo {
            versions: vec![
                AuthenticatorVersion::FIDO_2_1_PRE,
                AuthenticatorVersion::U2F_V2,
            ],
            ..Default::default()
        };
        assert_eq!(
            fido2_1_pre.max_supported_version(),
            AuthenticatorVersion::FIDO_2_1_PRE
        );

        let fido2_1 = AuthenticatorInfo {
            versions: vec![
                AuthenticatorVersion::FIDO_2_1_PRE,
                AuthenticatorVersion::FIDO_2_1,
                AuthenticatorVersion::U2F_V2,
                AuthenticatorVersion::FIDO_2_0,
            ],
            ..Default::default()
        };
        assert_eq!(
            fido2_1.max_supported_version(),
            AuthenticatorVersion::FIDO_2_1
        );
    }

    #[test]
    fn parse_authenticator_info_protocol_versions() {
        let mut expected = AuthenticatorInfo {
            versions: vec![AuthenticatorVersion::U2F_V2, AuthenticatorVersion::FIDO_2_0],
            extensions: vec!["uvm".to_string(), "hmac-secret".to_string()],
            aaguid: AAGuid(AAGUID_RAW),
            options: AuthenticatorOptions {
                platform_device: false,
                resident_key: true,
                client_pin: Some(false),
                user_presence: true,
                ..Default::default()
            },
            max_msg_size: Some(1200),
            pin_protocols: None,
            ..Default::default()
        };

        let raw_data = AUTHENTICATOR_INFO_PAYLOAD.to_vec();
        // pin protocol entry (last 3 bytes in payload):
        // 0x06, //  unsigned(6)
        // 0x81, //  array(1)
        // 0x01, //    unsigned(1)

        let mut raw_empty_list = raw_data.clone();
        raw_empty_list.pop();
        let raw_list_len = raw_empty_list.len();
        raw_empty_list[raw_list_len - 1] = 0x80; // array(0) instead of array(1)

        // Empty protocols-array, that should produce an error
        from_slice::<AuthenticatorInfo>(&raw_empty_list).unwrap_err();

        // No protocols specified
        let mut raw_no_list = raw_data.clone();
        raw_no_list.pop();
        raw_no_list.pop();
        raw_no_list.pop();
        raw_no_list[0] = 0xa5; // map(5) instead of map(6)

        let authenticator_info: AuthenticatorInfo = from_slice(&raw_no_list).unwrap();
        assert_eq!(authenticator_info, expected);

        // Both 1 and 2
        let mut raw_list = raw_data;
        let raw_list_len = raw_list.len();
        raw_list[raw_list_len - 2] = 0x82; // array(2) instead of array(1)
        raw_list.push(0x02);

        expected.pin_protocols = Some(vec![1, 2]);
        let authenticator_info: AuthenticatorInfo = from_slice(&raw_list).unwrap();
        assert_eq!(authenticator_info, expected);
    }
}