-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathFido2SampleRun.Operations.cs
More file actions
1595 lines (1365 loc) · 63.8 KB
/
Fido2SampleRun.Operations.cs
File metadata and controls
1595 lines (1365 loc) · 63.8 KB
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
// Copyright 2023 Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Yubico.YubiKey.Cryptography;
using Yubico.YubiKey.Fido2;
using Yubico.YubiKey.Fido2.Commands;
using Yubico.YubiKey.Fido2.Cose;
using Yubico.YubiKey.Sample.SharedCode;
namespace Yubico.YubiKey.Sample.Fido2SampleCode
{
// This file contains the methods to run each of the main menu items.
// The main menu is displayed, the user selects an option, and the code that
// receives the choice will call the appropriate method in this file to
// make the appropriate calls to perform the operation selected.
public partial class Fido2SampleRun
{
public bool RunMenuItem(Fido2MainMenuItem menuItem)
{
if (menuItem >= Fido2MainMenuItem.MakeCredential
&& menuItem < Fido2MainMenuItem.AuthenticatorSelection)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\n---This sample uses the SDK's automatic authentication (see the User's Manual)---\n");
}
return menuItem switch
{
Fido2MainMenuItem.Exit => false,
// Find all currently connected YubiKeys that can communicate
// over the HID FIDO protocol. This is the protocol used to
// communicate with the Fido2 application.
// Using Transport.HidFido finds all YubiKeys connected via USB.
Fido2MainMenuItem.ListYubiKeys => ListYubiKeys.RunListYubiKeys(Transport.HidFido),
Fido2MainMenuItem.ChooseYubiKey => RunChooseYubiKey(),
Fido2MainMenuItem.SetPin => RunSetPin(),
Fido2MainMenuItem.ChangePin => RunChangePin(),
Fido2MainMenuItem.VerifyPin => RunVerifyPin(),
Fido2MainMenuItem.VerifyUv => RunVerifyUv(),
Fido2MainMenuItem.MakeCredential => RunMakeCredential(),
Fido2MainMenuItem.GetAssertion => RunGetAssertions(),
Fido2MainMenuItem.ListCredentials => RunListCredentials(),
Fido2MainMenuItem.UpdateCredentialUserInfo => RunUpdateCredentialUserInfo(),
Fido2MainMenuItem.DeleteCredential => RunDeleteCredential(),
Fido2MainMenuItem.RetrieveLargeBlobData => RunRetrieveLargeBlobData(),
Fido2MainMenuItem.StoreLargeBlobData => RunStoreLargeBlobData(),
Fido2MainMenuItem.DeleteLargeBlobData => RunDeleteLargeBlobData(),
Fido2MainMenuItem.GetBioInfo => RunGetBioInfo(),
Fido2MainMenuItem.EnrollFingerprint => RunEnrollFingerprint(),
Fido2MainMenuItem.SetBioTemplateFriendlyName => RunSetBioTemplateFriendlyName(),
Fido2MainMenuItem.RemoveBioEnrollment => RunRemoveBioEnrollment(),
Fido2MainMenuItem.EnableEnterpriseAttestation => RunEnableEnterpriseAttestation(),
Fido2MainMenuItem.ToggleAlwaysUv => RunToggleAlwaysUv(),
Fido2MainMenuItem.SetPinConfig => RunSetPinConfig(),
Fido2MainMenuItem.Reset => RunReset(),
Fido2MainMenuItem.AuthenticatorSelection => RunAuthenticatorSelection(),
_ => RunUnimplementedOperation(),
};
}
public static bool RunInvalidEntry()
{
SampleMenu.WriteMessage(MessageType.Special, 0, "Invalid entry");
return true;
}
public static bool RunUnimplementedOperation()
{
SampleMenu.WriteMessage(MessageType.Special, 0, "Unimplemented operation");
return true;
}
public bool RunAuthenticatorSelection()
{
_keyCollector.Operation = Fido2KeyCollectorOperation.AuthenticatorSelection;
_ = Fido2AuthenticatorSelection.Run(
_keyCollector.Fido2SampleKeyCollectorDelegate,
ref _yubiKeyChosen);
return true;
}
public bool RunReset()
{
string versionNumber = _yubiKeyChosen.FirmwareVersion.ToString();
SampleMenu.WriteMessage(MessageType.Title, 0, "DANGER!!!");
SampleMenu.WriteMessage(MessageType.Title, 0, "Resetting the FIDO2 application will mean losing all FIDO2");
SampleMenu.WriteMessage(MessageType.Title, 0, "credentials on this YubiKey.\n");
string[] menuItems = new string[] {
"Yes",
"No",
};
int response = _menuObject.RunMenu("Do you want to continue?", menuItems);
if (response != 0)
{
return true;
}
SampleMenu.WriteMessage(MessageType.Title, 0, "This is the YubiKey for which the FIDO2 application will be reset.\n");
int? serial = _yubiKeyChosen.SerialNumber;
if (serial is null)
{
SampleMenu.WriteMessage(MessageType.Title, 0, "Unknown serial number : version = " + versionNumber);
}
else
{
SampleMenu.WriteMessage(MessageType.Title, 0, serial.ToString() + " : version = " + versionNumber);
}
response = _menuObject.RunMenu("\nIs this correct?", menuItems);
if (response != 0)
{
return true;
}
SampleMenu.WriteMessage(MessageType.Title, 0, "To reset, when prompted, you will need to remove, then re-insert");
SampleMenu.WriteMessage(MessageType.Title, 0, "the YubiKey. Then, when prompted, touch the YubiKey's contact.\n");
response = _menuObject.RunMenu("Do you want to continue?", menuItems);
if (response != 0)
{
return true;
}
_keyCollector.Operation = Fido2KeyCollectorOperation.Reset;
// To reset the FIDO2 application, one must call the reset command
// within a short time limit after the YubiKey has been "rebooted".
// In order to obtain an IYubiKeyDevice quickly after reinserting
// we're going to listen for the event.
// This means we need to worry about asynchronous operations, and the
// EventHandler delegates must have access to the serial number.
// So we're going to use a separate class to handle this.
var fido2Reset = new Fido2Reset(_yubiKeyChosen.SerialNumber);
var status = fido2Reset.RunFido2Reset(_keyCollector.Fido2SampleKeyCollectorDelegate);
if (status == ResponseStatus.Success)
{
SampleMenu.WriteMessage(MessageType.Title, 0, "\nFIDO2 application successfully reset.\n");
}
else
{
SampleMenu.WriteMessage(MessageType.Title, 0, "\nFIDO2 application NOT reset.\n");
}
return true;
}
// It is not possible to set the PIN if it is already set.
// If it is already set, it is possible to change it.
public bool RunSetPin()
{
bool isSet = Fido2Pin.SetPin(_yubiKeyChosen, _keyCollector.Fido2SampleKeyCollectorDelegate);
WritePinMessage("Set PIN", isSet);
return true;
}
public bool RunChangePin()
{
bool isChanged = Fido2Pin.ChangePin(_yubiKeyChosen, _keyCollector.Fido2SampleKeyCollectorDelegate);
WritePinMessage("Change PIN", isChanged);
return true;
}
public bool RunVerifyPin()
{
bool isValid = Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo);
if (isValid)
{
isValid = GetVerifyArguments(
true, authenticatorInfo, out var permissions, out string relyingPartyId);
if (isValid)
{
_keyCollector.Operation = Fido2KeyCollectorOperation.Verify;
if (Fido2Pin.VerifyPin(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
permissions,
relyingPartyId) == false)
{
SampleMenu.WriteMessage(MessageType.Special, 0, "PIN collection canceled.");
}
return true;
}
}
return false;
}
public bool RunVerifyUv()
{
bool isValid = Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo);
if (isValid)
{
isValid = GetVerifyArguments(
false, authenticatorInfo, out var permissions, out string relyingPartyId);
if (isValid && !(permissions is null))
{
_keyCollector.Operation = Fido2KeyCollectorOperation.Verify;
return Fido2Pin.VerifyUv(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
permissions.Value,
relyingPartyId);
}
}
return false;
}
public bool RunMakeCredential()
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"In order to make a credential, this sample code will collect relying party and user\n" +
"info, and will set the \"rk\" option to true. If the hmac-secret extension is supported,\n" +
"it will make sure one is created. If the credBlob extension is supported, it will ask\n" +
"if the caller wants to store any data in the credBlob. If the credProtect extension is\n" +
"supported, it will ask if the caller wants to set the cred protect policy.\n" +
"No other optional elements will be set.\n" +
"For expedience, it will generate a value based on the relying party info and use it as\n" +
"the ClientDataHash, even though what is performed in this sample code is not how the\n" +
"standard specifies that it be computed.\n" +
"In addition, the User ID is binary data (a byte array), and this sample simply uses\n" +
"random numbers (as recommended in W3C).\n" +
"Furthermore, the standard specifies that the relyingPartyName, user Name, and user\n" +
"DisplayName are optional. However, this sample requires these elements.\n\n" +
"Note that the relying party name is available as a convenience, it allows the\n" +
"application to store a more easily understood description of the relying party. The\n" +
"relying party ID is the value used in FIDO2 computations. For example, it is often\n" +
"based on the URL of the entity to which the user is connecting, such as\n" +
"\"example.login.com\".\n");
SampleMenu.WriteMessage(MessageType.Title, 0, "Enter the relyingPartyName");
_ = SampleMenu.ReadResponse(out string relyingPartyName);
SampleMenu.WriteMessage(MessageType.Title, 0, "Enter the relyingPartyId");
_ = SampleMenu.ReadResponse(out string relyingPartyId);
SampleMenu.WriteMessage(MessageType.Title, 0, "Enter the user Name");
_ = SampleMenu.ReadResponse(out string userName);
SampleMenu.WriteMessage(MessageType.Title, 0, "Enter the user DisplayName");
_ = SampleMenu.ReadResponse(out string userDisplayName);
var randomObject = CryptographyProviders.RngCreator();
byte[] randomBytes = new byte[16];
randomObject.GetBytes(randomBytes);
var userId = new ReadOnlyMemory<byte>(randomBytes);
var clientDataHash = BuildFakeClientDataHash(relyingPartyId);
if (!Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo))
{
return false;
}
var credProtectPolicy = CredProtectPolicy.None;
if (authenticatorInfo.Extensions.Contains("credProtect"))
{
string[] menuItems = new string[] {
"No (use default)",
"Yes - " + Enum.GetName<CredProtectPolicy>(CredProtectPolicy.UserVerificationOptional),
"Yes - " + Enum.GetName<CredProtectPolicy>(CredProtectPolicy.UserVerificationOptionalWithCredentialIDList),
"Yes - " + Enum.GetName<CredProtectPolicy>(CredProtectPolicy.UserVerificationRequired),
};
SampleMenu.WriteMessage(
MessageType.Title, 0,
"This YubiKey allows you to set the credProtectPolicy. If you do not set it,\n" +
"the policy will be the YubiKey's default. For most YubiKeys, the default is\n" +
"\"UserVerificationOptional\".\n");
int response = _menuObject.RunMenu("Do you want to set the credProtectPolicy?", menuItems);
credProtectPolicy = (CredProtectPolicy)response;
}
byte[] credBlobData = Array.Empty<byte>();
int maxCredBlobLength = authenticatorInfo.MaximumCredentialBlobLength ?? 0;
if (maxCredBlobLength > 0)
{
string[] menuItems = new string[] {
"Yes",
"No",
};
int response = _menuObject.RunMenu("Do you want to store credBlob data? (Maximum " + maxCredBlobLength + " bytes)", menuItems);
if (response == 0)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"The credBlob data can be any binary bytes but this sample code will store a string.\n" +
"This sample code will expect each character in the string to be UTF-16, so the limit\n" +
"is " + (maxCredBlobLength / 2) + " characters. If you use any characters other than UTF-16,\n" +
"there is no guarantee this sample will execute properly.\n" +
"Note that the SDK will check the length of the credBlob, but not this sample code.");
SampleMenu.WriteMessage(MessageType.Title, 0, "Enter the credBlob data");
_ = SampleMenu.ReadResponse(out string credBlobDataString);
credBlobData = Encoding.Unicode.GetBytes(credBlobDataString);
}
}
_keyCollector.Operation = Fido2KeyCollectorOperation.MakeCredential;
bool isValid = Fido2Protocol.RunMakeCredential(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
clientDataHash,
relyingPartyName, relyingPartyId,
userName, userDisplayName, userId,
credProtectPolicy,
credBlobData,
out var makeCredentialData);
if (!isValid)
{
return false;
}
// Store whatever information you need.
_credentialList.Add(makeCredentialData);
var publicKey = (CoseEcPublicKey)makeCredentialData.AuthenticatorData.CredentialPublicKey;
string xCoordinate = BitConverter.ToString(publicKey.XCoordinate.ToArray()).Replace("-", string.Empty, StringComparison.Ordinal);
string yCoordinate = BitConverter.ToString(publicKey.YCoordinate.ToArray()).Replace("-", string.Empty, StringComparison.Ordinal);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"public key credential:\n x-coordinate = " + xCoordinate + "\n " + "y-coordinate = " + yCoordinate + "\n");
return true;
}
public bool RunGetAssertions()
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"This will return zero, one, or more assertions.\n" +
"Because this sample code only has access to public keys of credentials made during\n" +
"this sample run, it will only be able to verify the signatures of assertions for\n" +
"those credentials.\n" +
"For expedience, it will generate a value based on the relying party info and use it\n" +
"as the ClientDataHash, even though what is performed in this sample code is not how\n" +
"it is actually computed.\n");
SampleMenu.WriteMessage(MessageType.Title, 0, "Enter the relyingPartyId");
_ = SampleMenu.ReadResponse(out string relyingPartyId);
var clientDataHash = BuildFakeClientDataHash(relyingPartyId);
var salt = ReadOnlyMemory<byte>.Empty;
bool isValid = Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo);
if (isValid)
{
if (authenticatorInfo.Extensions.Contains("hmac-secret"))
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nWould you like the hmac-secret returned with the assertions?\n" +
"If not, type Enter.\n" +
"Otherwise, enter a string that will be used to derive a salt.\n" +
"Normally, a salt is 32 random bytes or the digest of some identifying data.\n" +
"This sample code will perform SHA-256 on the input you provide and send that\n" +
"digest to the YubiKey as the salt.\n");
_ = SampleMenu.ReadResponse(out string dataToDigest);
byte[] dataBytes = System.Text.Encoding.Unicode.GetBytes(dataToDigest);
var digester = CryptographyProviders.Sha256Creator();
_ = digester.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
salt = new ReadOnlyMemory<byte>(digester.Hash);
}
}
_keyCollector.Operation = Fido2KeyCollectorOperation.GetAssertion;
if (!Fido2Protocol.RunGetAssertions(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
clientDataHash,
relyingPartyId,
salt,
out var assertions,
out var hmacSecrets))
{
return false;
}
if (assertions.Count == 0)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThe YubiKey was not able to get any assertions for the specified relying party ID.\n");
return true;
}
for (int index = 0; index < assertions.Count; index++)
{
DisplayAssertion(index, assertions[index], hmacSecrets[index], clientDataHash);
}
return true;
}
public bool RunListCredentials()
{
if (!Fido2Protocol.RunGetCredentialData(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var credentialData))
{
return false;
}
ReportCredentials(credentialData, true, true, out int _);
return true;
}
public bool RunUpdateCredentialUserInfo()
{
if (!Fido2Protocol.RunGetCredentialData(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var credentialData))
{
return false;
}
ReportCredentials(credentialData, false, false, out int credentialCount);
var userInfo = SelectCredential(credentialData, credentialCount);
if (userInfo is null)
{
return false;
}
var updatedInfo = GetUpdatedInfo(userInfo.User);
return Fido2Protocol.RunUpdateUserInfo(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
userInfo.CredentialId,
updatedInfo);
}
public bool RunDeleteCredential()
{
if (!Fido2Protocol.RunGetCredentialData(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var credentialData))
{
return false;
}
ReportCredentials(credentialData, false, false, out int credentialCount);
var userInfo = SelectCredential(credentialData, credentialCount);
if (userInfo is null)
{
return false;
}
if (!Fido2Protocol.RunDeleteCredential(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
userInfo.CredentialId))
{
return false;
}
// The FIDO2 standard recommends deleting any largeBlob
// data associated with a credential along with the credential itself.
// This sample code will still return true even if something goes
// wrong trying to delete an entry in the largeBlob data.
if (userInfo.LargeBlobKey is null)
{
return true;
}
if (!Fido2Protocol.RunGetLargeBlobArray(
_yubiKeyChosen,
out var blobArray))
{
return true;
}
_ = Fido2Protocol.GetLargeBlobEntry(
blobArray, userInfo.LargeBlobKey.Value, out int entryIndex);
if (entryIndex < 0)
{
return true;
}
blobArray.RemoveEntry(entryIndex);
_ = Fido2Protocol.RunStoreLargeBlobArray(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
blobArray);
return true;
}
public bool RunRetrieveLargeBlobData()
{
if (!Fido2Protocol.RunGetLargeBlobArray(
_yubiKeyChosen,
out var blobArray))
{
return false;
}
if (blobArray.Entries.Count == 0)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThere is no largeBlob data stored on the YubiKey.\n");
return true;
}
if (blobArray.Entries.Count == 1)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThere is one largeBlob entry stored on the YubiKey.\n");
}
else
{
string entryCount = blobArray.Entries.Count.ToString(CultureInfo.InvariantCulture);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThere are " + entryCount + " largeBlob entries stored on the YubiKey.\n");
}
string[] menuItems = new string[] {
"Yes",
"No",
};
int response = _menuObject.RunMenu("Continue?", menuItems);
if (response != 0)
{
return true;
}
if (!Fido2Protocol.RunGetCredentialData(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var credentialData))
{
return false;
}
ReportCredentials(credentialData, false, true, out int credentialCount);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"LargeBlob data is stored against a credential. Select a credential for which\n" +
"you want to see the largeBlob data. It is possible to retrieve data only for\n" +
"credentials that have an available Large Blob Key.\n");
var userInfo = SelectCredential(credentialData, credentialCount);
if (userInfo is null)
{
return false;
}
if (userInfo.LargeBlobKey is null)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"There is no large blob key for this credential, therefore it will not be\n" +
"possible to retrieve largeBlob data. It is likely there is no largeBlob\n" +
"data for this credential.\n");
return true;
}
string currentContents = Fido2Protocol.GetLargeBlobEntry(
blobArray, userInfo.LargeBlobKey.Value, out int entryIndex);
if (entryIndex < 0)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThere is no largeBlob data associated with the selected credential.\n");
}
else
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThe largeBlob data for the selected credential is the following:\n\n" + currentContents + "\n");
}
return true;
}
public bool RunStoreLargeBlobData()
{
// The way to store large blob data is to get the current large blob
// data and "edit" it.
if (!Fido2Protocol.RunGetLargeBlobArray(
_yubiKeyChosen,
out var blobArray))
{
return false;
}
if (!Fido2Protocol.RunGetCredentialData(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var credentialData))
{
return false;
}
ReportCredentials(credentialData, false, true, out int credentialCount);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"LargeBlob data is stored against a credential. That is, for each credential, it\n" +
"is possible to store some largeBlob data. However, the credential must be made\n" +
"with the largeBlob option. Hence, you must choose a credential against which the\n" +
"data will be stored, and that credential must have an available Large Blob Key.\n" +
"Note that this sample code will store only one entry per credential.\n");
var userInfo = SelectCredential(credentialData, credentialCount);
if (userInfo is null)
{
return false;
}
if (userInfo.LargeBlobKey is null)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"There is no large blob key for this credential, therefore it will not be\n" +
"possible to store largeBlob data.\n");
return true;
}
string currentContents = Fido2Protocol.GetLargeBlobEntry(
blobArray, userInfo.LargeBlobKey.Value, out int entryIndex);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"The largeBlob data can be any binary bytes but this sample code will accept only\n" +
"strings. This sample code will expect each character in the string to be UTF-16.\n" +
"If you use any characters other than UTF-16, there is no guarantee this sample will\n" +
"execute properly.\n" +
"Note also that what you supply as the largeBlob data will replace the current contents.");
if (entryIndex < 0)
{
SampleMenu.WriteMessage(MessageType.Title, 0, "\nThere are no current contents.\n");
}
else
{
SampleMenu.WriteMessage(MessageType.Title, 0, "\nThe current contents:\n\n" + currentContents + "\n");
blobArray.RemoveEntry(entryIndex);
}
SampleMenu.WriteMessage(MessageType.Title, 0, "Enter the largeBlob data to store.");
_ = SampleMenu.ReadResponse(out string largeBlobDataString);
byte[] largeBlobData = Encoding.Unicode.GetBytes(largeBlobDataString);
blobArray.AddEntry(largeBlobData, userInfo.LargeBlobKey.Value);
return Fido2Protocol.RunStoreLargeBlobArray(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
blobArray);
}
public bool RunDeleteLargeBlobData()
{
if (!Fido2Protocol.RunGetLargeBlobArray(
_yubiKeyChosen,
out var blobArray))
{
return false;
}
if (!Fido2Protocol.RunGetCredentialData(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var credentialData))
{
return false;
}
ReportCredentials(credentialData, false, true, out int credentialCount);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"LargeBlob data is stored against a credential. Select a credential for which\n" +
"you want to delete the largeBlob data. If there are no credentials on the\n" +
"YubiKey, or if there is no largeBlob data stored, or nothing stored against\n" +
"the selected credential, this sample code will do nothing.\n");
var userInfo = SelectCredential(credentialData, credentialCount);
if (userInfo is null || userInfo.LargeBlobKey is null)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"There is no large blob key for this credential, therefore it will not be\n" +
"possible to delete largeBlob data. It is likely there is no largeBlob\n" +
"data for this credential.\n");
return true;
}
string currentContents = Fido2Protocol.GetLargeBlobEntry(
blobArray, userInfo.LargeBlobKey.Value, out int entryIndex);
if (entryIndex < 0)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThere is no largeBlob data associated with the selected credential.\n");
return true;
}
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nThe largeBlob data (to be deleted) for the selected credential is the following:\n\n" + currentContents + "\n");
blobArray.RemoveEntry(entryIndex);
return Fido2Protocol.RunStoreLargeBlobArray(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
blobArray);
}
public bool RunGetBioInfo()
{
if (!Fido2Protocol.RunGetBioInfo(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var modality,
out var sensorInfo,
out var templates))
{
return false;
}
ReportBioInfo(modality, sensorInfo, templates, false);
return true;
}
public bool RunEnrollFingerprint()
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"Enter the friendlyName (or simply Enter if you do not want one for now).\n" +
"Note that if there is already a template with the requested name or if the\n" +
"YubiKey rejects the name (likely because it is too long), then the template\n" +
"will be created with no friendly name.");
_ = SampleMenu.ReadResponse(out string friendlyName);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nEnter a timeout (for each sample, in milliseconds) if you want to override the\n" +
"default. Note that some YubiKeys do not allow a timeout override and will ignore\n" +
"your requested timeout.\n" +
"If you do not want to override the default, just Enter");
int timeoutMilliseconds = SampleMenu.ReadResponse(out string _);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nTo enroll a fingerprint, you will be asked to provide several samples.\n" +
"You will be notified when to provide a sample. Each notification after the\n" +
"first will indicate whether the previous sample was good or not, along\n" +
"with the number of good samples still needed to complete the enrollment.\n");
string[] menuItems = new string[] {
"Yes",
"No",
};
int response = _menuObject.RunMenu("Do you want to continue?", menuItems);
if (response != 0)
{
return true;
}
try
{
var templateInfo = Fido2Protocol.RunEnrollFingerprint(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
friendlyName,
timeoutMilliseconds);
SampleMenu.WriteMessage(
MessageType.Title, 0,
"friendly name applied: " + (string.IsNullOrEmpty(templateInfo.FriendlyName) ? "-" : friendlyName));
string idString = BitConverter.ToString(templateInfo.TemplateId.ToArray()).Replace("-", string.Empty, StringComparison.Ordinal);
SampleMenu.WriteMessage(
MessageType.Title, 0,
" templateID : " + idString + "\n");
}
catch (Exception ex) when (ex is OperationCanceledException || ex is Fido2Exception)
{
SampleMenu.WriteMessage(MessageType.Title, 0, ex.Message + "\n\n");
}
return true;
}
public bool RunSetBioTemplateFriendlyName()
{
if (!Fido2Protocol.RunGetBioInfo(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var modality,
out var sensorInfo,
out var templates))
{
return false;
}
ReportBioInfo(modality, sensorInfo, templates, true);
if (templates.Count == 0)
{
return true;
}
SampleMenu.WriteMessage(
MessageType.Title, 0, "For which template do you want to set the friendly name?");
int response = SampleMenu.ReadResponse(out string _);
SampleMenu.WriteMessage(
MessageType.Title, 0, "Enter the friendly name.");
_ = SampleMenu.ReadResponse(out string friendlyName);
return Fido2Protocol.RunSetBioTemplateFriendlyName(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
templates[response - 1].TemplateId,
friendlyName);
}
public bool RunRemoveBioEnrollment()
{
if (!Fido2Protocol.RunGetBioInfo(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var modality,
out var sensorInfo,
out var templates))
{
return false;
}
ReportBioInfo(modality, sensorInfo, templates, true);
if (templates.Count == 0)
{
return true;
}
SampleMenu.WriteMessage(
MessageType.Title, 0, "Which template do you want to delete?");
int response = SampleMenu.ReadResponse(out string _);
return Fido2Protocol.RunRemoveBioEnrollment(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
templates[response - 1].TemplateId);
}
public bool RunEnableEnterpriseAttestation()
{
bool isEnabled = Fido2Protocol.RunEnableEnterpriseAttestation(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate);
if (isEnabled)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"Enterprise Attestation is enabled.\n\n");
}
else
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"The selected YubiKey does not support AuthenticatorConfig operations.\n\n");
}
return true;
}
public bool RunToggleAlwaysUv()
{
bool isValid = Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo);
if (!isValid)
{
return false;
}
var optionValue = authenticatorInfo.GetOptionValue("alwaysUv");
string[] menuItems = new string[] {
"Yes",
"No",
};
int response;
switch (optionValue)
{
case OptionValue.True:
SampleMenu.WriteMessage(
MessageType.Title, 0,
"The current status of always UV is True, toggling will set it to False.\n");
response = _menuObject.RunMenu("Do you want to toggle alwaysUv to False?", menuItems);
break;
case OptionValue.False:
SampleMenu.WriteMessage(
MessageType.Title, 0,
"The current status of always UV is False, toggling will set it to True.\n");
response = _menuObject.RunMenu("Do you want to toggle alwaysUv to True?", menuItems);
break;
default:
SampleMenu.WriteMessage(
MessageType.Title, 0,
"The selected YubiKey does not support AuthenticatorConfig operations.\n\n");
return true;
}
if (response != 0)
{
return true;
}
isValid = Fido2Protocol.RunToggleAlwaysUv(
_yubiKeyChosen,
_keyCollector.Fido2SampleKeyCollectorDelegate,
out var newValue);
if (isValid)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"The Option alwaysUv is now " + Enum.GetName<OptionValue>(newValue) + ".\n\n");
}
return isValid;
}
public bool RunSetPinConfig()
{
bool isValid = Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo);
if (!isValid)
{
return false;
}
var setMinPinValue = authenticatorInfo.GetOptionValue(AuthenticatorOptions.setMinPINLength);
if (setMinPinValue != OptionValue.True)
{
SampleMenu.WriteMessage(
MessageType.Title, 0,