-
Notifications
You must be signed in to change notification settings - Fork 66
/
key.go
1512 lines (1368 loc) · 45.8 KB
/
key.go
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 2020 Google LLC
//
// 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
//
// https://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.
package piv
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"errors"
"fmt"
"io"
"math/big"
"strconv"
"strings"
rsafork "github.com/go-piv/piv-go/third_party/rsa"
)
// errMismatchingAlgorithms is returned when a cryptographic operation
// is given keys using different algorithms.
var errMismatchingAlgorithms = errors.New("mismatching key algorithms")
// errUnsupportedKeySize is returned when a key has an unsupported size
var errUnsupportedKeySize = errors.New("unsupported key size")
// unsupportedCurveError is used when a key has an unsupported curve
type unsupportedCurveError struct {
curve int
}
func (e unsupportedCurveError) Error() string {
return fmt.Sprintf("unsupported curve: %d", e.curve)
}
// Slot is a private key and certificate combination managed by the security key.
type Slot struct {
// Key is a reference for a key type.
//
// See: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=32
Key uint32
// Object is a reference for data object.
//
// See: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=30
Object uint32
}
var (
extIDFirmwareVersion = asn1.ObjectIdentifier([]int{1, 3, 6, 1, 4, 1, 41482, 3, 3})
extIDSerialNumber = asn1.ObjectIdentifier([]int{1, 3, 6, 1, 4, 1, 41482, 3, 7})
extIDKeyPolicy = asn1.ObjectIdentifier([]int{1, 3, 6, 1, 4, 1, 41482, 3, 8})
extIDFormFactor = asn1.ObjectIdentifier([]int{1, 3, 6, 1, 4, 1, 41482, 3, 9})
)
// Version encodes a major, minor, and patch version.
type Version struct {
Major int
Minor int
Patch int
}
// Formfactor enumerates the physical set of forms a key can take. USB-A vs.
// USB-C and Keychain vs. Nano (and FIPS variants for these).
type Formfactor int
// The mapping between known Formfactor values and their descriptions.
var formFactorStrings = map[Formfactor]string{
FormfactorUSBAKeychain: "USB-A Keychain",
FormfactorUSBANano: "USB-A Nano",
FormfactorUSBCKeychain: "USB-C Keychain",
FormfactorUSBCNano: "USB-C Nano",
FormfactorUSBCLightningKeychain: "USB-C/Lightning Keychain",
FormfactorUSBAKeychainFIPS: "USB-A Keychain FIPS",
FormfactorUSBANanoFIPS: "USB-A Nano FIPS",
FormfactorUSBCKeychainFIPS: "USB-C Keychain FIPS",
FormfactorUSBCNanoFIPS: "USB-C Nano FIPS",
FormfactorUSBCLightningKeychainFIPS: "USB-C/Lightning Keychain FIPS",
}
// String returns the human-readable description for the given form-factor
// value, or a fallback value for any other, unknown form-factor.
func (f Formfactor) String() string {
if s, ok := formFactorStrings[f]; ok {
return s
}
return fmt.Sprintf("unknown(0x%02x)", int(f))
}
// Formfactors recognized by this package. See the reference for more information:
// https://developers.yubico.com/yubikey-manager/Config_Reference.html#_form_factor
const (
FormfactorUSBAKeychain = 0x1
FormfactorUSBANano = 0x2
FormfactorUSBCKeychain = 0x3
FormfactorUSBCNano = 0x4
FormfactorUSBCLightningKeychain = 0x5
FormfactorUSBAKeychainFIPS = 0x81
FormfactorUSBANanoFIPS = 0x82
FormfactorUSBCKeychainFIPS = 0x83
FormfactorUSBCNanoFIPS = 0x84
FormfactorUSBCLightningKeychainFIPS = 0x85
)
// Prefix in the x509 Subject Common Name for YubiKey attestations
// https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
const yubikeySubjectCNPrefix = "YubiKey PIV Attestation "
// Attestation returns additional information about a key attested to be generated
// on a card. See https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
// for more information.
type Attestation struct {
// Version of the YubiKey's firmware.
Version Version
// Serial is the YubiKey's serial number.
Serial uint32
// Formfactor indicates the physical type of the YubiKey.
//
// Formfactor may be empty Formfactor(0) for some YubiKeys.
Formfactor Formfactor
// PINPolicy set on the slot.
PINPolicy PINPolicy
// TouchPolicy set on the slot.
TouchPolicy TouchPolicy
// Slot is the inferred slot the attested key resides in based on the
// common name in the attestation. If the slot cannot be determined,
// this field will be an empty struct.
Slot Slot
}
func (a *Attestation) addExt(e pkix.Extension) error {
if e.Id.Equal(extIDFirmwareVersion) {
if len(e.Value) != 3 {
return fmt.Errorf("expected 3 bytes for firmware version, got: %d", len(e.Value))
}
a.Version = Version{
Major: int(e.Value[0]),
Minor: int(e.Value[1]),
Patch: int(e.Value[2]),
}
} else if e.Id.Equal(extIDSerialNumber) {
var serial int64
if _, err := asn1.Unmarshal(e.Value, &serial); err != nil {
return fmt.Errorf("parsing serial number: %v", err)
}
if serial < 0 {
return fmt.Errorf("serial number was negative: %d", serial)
}
a.Serial = uint32(serial)
} else if e.Id.Equal(extIDKeyPolicy) {
if len(e.Value) != 2 {
return fmt.Errorf("expected 2 bytes from key policy, got: %d", len(e.Value))
}
switch e.Value[0] {
case 0x01:
a.PINPolicy = PINPolicyNever
case 0x02:
a.PINPolicy = PINPolicyOnce
case 0x03:
a.PINPolicy = PINPolicyAlways
default:
return fmt.Errorf("unrecognized pin policy: 0x%x", e.Value[0])
}
switch e.Value[1] {
case 0x01:
a.TouchPolicy = TouchPolicyNever
case 0x02:
a.TouchPolicy = TouchPolicyAlways
case 0x03:
a.TouchPolicy = TouchPolicyCached
default:
return fmt.Errorf("unrecognized touch policy: 0x%x", e.Value[1])
}
} else if e.Id.Equal(extIDFormFactor) {
if len(e.Value) != 1 {
return fmt.Errorf("expected 1 byte from formfactor, got: %d", len(e.Value))
}
a.Formfactor = Formfactor(e.Value[0])
}
return nil
}
// Verify proves that a key was generated on a YubiKey. It ensures the slot and
// YubiKey certificate chains up to the Yubico CA, parsing additional information
// out of the slot certificate, such as the touch and PIN policies of a key.
func Verify(attestationCert, slotCert *x509.Certificate) (*Attestation, error) {
var v Verifier
return v.Verify(attestationCert, slotCert)
}
// Verifier allows specifying options when verifying attestations produced by
// YubiKeys.
type Verifier struct {
// Root certificates to use to validate challenges. If nil, this defaults to Yubico's
// CA bundle.
//
// https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
// https://developers.yubico.com/PIV/Introduction/piv-attestation-ca.pem
// https://developers.yubico.com/U2F/yubico-u2f-ca-certs.txt
Roots *x509.CertPool
}
// Verify proves that a key was generated on a YubiKey.
//
// As opposed to the package level [Verify], it uses any options enabled on the [Verifier].
func (v *Verifier) Verify(attestationCert, slotCert *x509.Certificate) (*Attestation, error) {
o := x509.VerifyOptions{KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny}}
o.Roots = v.Roots
if o.Roots == nil {
cas, err := yubicoCAs()
if err != nil {
return nil, fmt.Errorf("failed to load yubico CAs: %v", err)
}
o.Roots = cas
}
o.Intermediates = x509.NewCertPool()
// The attestation cert in some yubikey 4 does not encode X509v3 Basic Constraints.
// This isn't valid as per https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.9
// (fourth paragraph) and thus makes x509.go validation fail.
// Work around this by setting this constraint here.
if !attestationCert.BasicConstraintsValid {
attestationCert.BasicConstraintsValid = true
attestationCert.IsCA = true
}
o.Intermediates.AddCert(attestationCert)
_, err := slotCert.Verify(o)
if err != nil {
return nil, fmt.Errorf("error verifying attestation certificate: %v", err)
}
return parseAttestation(slotCert)
}
func parseAttestation(slotCert *x509.Certificate) (*Attestation, error) {
var a Attestation
for _, ext := range slotCert.Extensions {
if err := a.addExt(ext); err != nil {
return nil, fmt.Errorf("parsing extension: %v", err)
}
}
slot, ok := parseSlot(slotCert.Subject.CommonName)
if ok {
a.Slot = slot
}
return &a, nil
}
func parseSlot(commonName string) (Slot, bool) {
if !strings.HasPrefix(commonName, yubikeySubjectCNPrefix) {
return Slot{}, false
}
slotName := strings.TrimPrefix(commonName, yubikeySubjectCNPrefix)
key, err := strconv.ParseUint(slotName, 16, 32)
if err != nil {
return Slot{}, false
}
switch uint32(key) {
case SlotAuthentication.Key:
return SlotAuthentication, true
case SlotSignature.Key:
return SlotSignature, true
case SlotCardAuthentication.Key:
return SlotCardAuthentication, true
case SlotKeyManagement.Key:
return SlotKeyManagement, true
}
return RetiredKeyManagementSlot(uint32(key))
}
// yubicoPIVCAPEMAfter2018 is the PEM encoded attestation certificate used by Yubico.
//
// https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
const yubicoPIVCAPEMAfter2018 = `-----BEGIN CERTIFICATE-----
MIIDFzCCAf+gAwIBAgIDBAZHMA0GCSqGSIb3DQEBCwUAMCsxKTAnBgNVBAMMIFl1
YmljbyBQSVYgUm9vdCBDQSBTZXJpYWwgMjYzNzUxMCAXDTE2MDMxNDAwMDAwMFoY
DzIwNTIwNDE3MDAwMDAwWjArMSkwJwYDVQQDDCBZdWJpY28gUElWIFJvb3QgQ0Eg
U2VyaWFsIDI2Mzc1MTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMN2
cMTNR6YCdcTFRxuPy31PabRn5m6pJ+nSE0HRWpoaM8fc8wHC+Tmb98jmNvhWNE2E
ilU85uYKfEFP9d6Q2GmytqBnxZsAa3KqZiCCx2LwQ4iYEOb1llgotVr/whEpdVOq
joU0P5e1j1y7OfwOvky/+AXIN/9Xp0VFlYRk2tQ9GcdYKDmqU+db9iKwpAzid4oH
BVLIhmD3pvkWaRA2H3DA9t7H/HNq5v3OiO1jyLZeKqZoMbPObrxqDg+9fOdShzgf
wCqgT3XVmTeiwvBSTctyi9mHQfYd2DwkaqxRnLbNVyK9zl+DzjSGp9IhVPiVtGet
X02dxhQnGS7K6BO0Qe8CAwEAAaNCMEAwHQYDVR0OBBYEFMpfyvLEojGc6SJf8ez0
1d8Cv4O/MA8GA1UdEwQIMAYBAf8CAQEwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3
DQEBCwUAA4IBAQBc7Ih8Bc1fkC+FyN1fhjWioBCMr3vjneh7MLbA6kSoyWF70N3s
XhbXvT4eRh0hvxqvMZNjPU/VlRn6gLVtoEikDLrYFXN6Hh6Wmyy1GTnspnOvMvz2
lLKuym9KYdYLDgnj3BeAvzIhVzzYSeU77/Cupofj093OuAswW0jYvXsGTyix6B3d
bW5yWvyS9zNXaqGaUmP3U9/b6DlHdDogMLu3VLpBB9bm5bjaKWWJYgWltCVgUbFq
Fqyi4+JE014cSgR57Jcu3dZiehB6UtAPgad9L5cNvua/IWRmm+ANy3O2LH++Pyl8
SREzU8onbBsjMg9QDiSf5oJLKvd/Ren+zGY7
-----END CERTIFICATE-----`
// Yubikeys manufactured sometime in 2018 and prior to mid-2017
// were certified using the U2F root CA with serial number 457200631
// See https://github.com/Yubico/developers.yubico.com/pull/392/commits/a58f1003f003e04fc9baf09cad9f64f0c284fd47
// Cert available at https://developers.yubico.com/U2F/yubico-u2f-ca-certs.txt
const yubicoPIVCAPEMU2F = `-----BEGIN CERTIFICATE-----
MIIDHjCCAgagAwIBAgIEG0BT9zANBgkqhkiG9w0BAQsFADAuMSwwKgYDVQQDEyNZ
dWJpY28gVTJGIFJvb3QgQ0EgU2VyaWFsIDQ1NzIwMDYzMTAgFw0xNDA4MDEwMDAw
MDBaGA8yMDUwMDkwNDAwMDAwMFowLjEsMCoGA1UEAxMjWXViaWNvIFUyRiBSb290
IENBIFNlcmlhbCA0NTcyMDA2MzEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQC/jwYuhBVlqaiYWEMsrWFisgJ+PtM91eSrpI4TK7U53mwCIawSDHy8vUmk
5N2KAj9abvT9NP5SMS1hQi3usxoYGonXQgfO6ZXyUA9a+KAkqdFnBnlyugSeCOep
8EdZFfsaRFtMjkwz5Gcz2Py4vIYvCdMHPtwaz0bVuzneueIEz6TnQjE63Rdt2zbw
nebwTG5ZybeWSwbzy+BJ34ZHcUhPAY89yJQXuE0IzMZFcEBbPNRbWECRKgjq//qT
9nmDOFVlSRCt2wiqPSzluwn+v+suQEBsUjTGMEd25tKXXTkNW21wIWbxeSyUoTXw
LvGS6xlwQSgNpk2qXYwf8iXg7VWZAgMBAAGjQjBAMB0GA1UdDgQWBBQgIvz0bNGJ
hjgpToksyKpP9xv9oDAPBgNVHRMECDAGAQH/AgEAMA4GA1UdDwEB/wQEAwIBBjAN
BgkqhkiG9w0BAQsFAAOCAQEAjvjuOMDSa+JXFCLyBKsycXtBVZsJ4Ue3LbaEsPY4
MYN/hIQ5ZM5p7EjfcnMG4CtYkNsfNHc0AhBLdq45rnT87q/6O3vUEtNMafbhU6kt
hX7Y+9XFN9NpmYxr+ekVY5xOxi8h9JDIgoMP4VB1uS0aunL1IGqrNooL9mmFnL2k
LVVee6/VR6C5+KSTCMCWppMuJIZII2v9o4dkoZ8Y7QRjQlLfYzd3qGtKbw7xaF1U
sG/5xUb/Btwb2X2g4InpiB/yt/3CpQXpiWX/K4mBvUKiGn05ZsqeY1gx4g0xLBqc
U9psmyPzK+Vsgw2jeRQ5JlKDyqE0hebfC1tvFu0CCrJFcw==
-----END CERTIFICATE-----`
func yubicoCAs() (*x509.CertPool, error) {
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM([]byte(yubicoPIVCAPEMAfter2018)) {
return nil, fmt.Errorf("failed to parse yubico cert")
}
bU2F, _ := pem.Decode([]byte(yubicoPIVCAPEMU2F))
if bU2F == nil {
return nil, fmt.Errorf("failed to decode yubico pem data")
}
certU2F, err := x509.ParseCertificate(bU2F.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse yubico cert: %v", err)
}
// The U2F root cert has pathlen x509 basic constraint set to 0.
// As per RFC 5280 this means that no intermediate cert is allowed
// in the validation path. This isn't really helpful since we do
// want to use the device attestation cert as intermediate cert in
// the chain. To make this work, set pathlen of the U2F root to 1.
//
// See https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.9
certU2F.MaxPathLen = 1
certPool.AddCert(certU2F)
return certPool, nil
}
// Slot combinations pre-defined by this package.
//
// Object IDs are specified in NIST 800-73-4 section 4.3:
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=30
//
// Key IDs are specified in NIST 800-73-4 section 5.1:
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=32
var (
SlotAuthentication = Slot{0x9a, 0x5fc105}
SlotSignature = Slot{0x9c, 0x5fc10a}
SlotCardAuthentication = Slot{0x9e, 0x5fc101}
SlotKeyManagement = Slot{0x9d, 0x5fc10b}
slotAttestation = Slot{0xf9, 0x5fff01}
)
var retiredKeyManagementSlots = map[uint32]Slot{
0x82: {0x82, 0x5fc10d},
0x83: {0x83, 0x5fc10e},
0x84: {0x84, 0x5fc10f},
0x85: {0x85, 0x5fc110},
0x86: {0x86, 0x5fc111},
0x87: {0x87, 0x5fc112},
0x88: {0x88, 0x5fc113},
0x89: {0x89, 0x5fc114},
0x8a: {0x8a, 0x5fc115},
0x8b: {0x8b, 0x5fc116},
0x8c: {0x8c, 0x5fc117},
0x8d: {0x8d, 0x5fc118},
0x8e: {0x8e, 0x5fc119},
0x8f: {0x8f, 0x5fc11a},
0x90: {0x90, 0x5fc11b},
0x91: {0x91, 0x5fc11c},
0x92: {0x92, 0x5fc11d},
0x93: {0x93, 0x5fc11e},
0x94: {0x94, 0x5fc11f},
0x95: {0x95, 0x5fc120},
}
// RetiredKeyManagementSlot provides access to "retired" slots. Slots meant for old Key Management
// keys that have been rotated. YubiKeys 4 and later support values between 0x82 and 0x95 (inclusive).
//
// slot, ok := RetiredKeyManagementSlot(0x82)
// if !ok {
// // unrecognized slot
// }
// pub, err := yk.GenerateKey(managementKey, slot, key)
//
// https://developers.yubico.com/PIV/Introduction/Certificate_slots.html#_slot_82_95_retired_key_management
func RetiredKeyManagementSlot(key uint32) (Slot, bool) {
slot, ok := retiredKeyManagementSlots[key]
return slot, ok
}
// String returns the two-character hex representation of the slot
func (s Slot) String() string {
return strconv.FormatUint(uint64(s.Key), 16)
}
// Algorithm represents a specific algorithm and bit size supported by the PIV
// specification.
type Algorithm int
// Algorithms supported by this package. Note that not all cards will support
// every algorithm.
//
// AlgorithmEd25519 is currently only implemented by SoloKeys.
//
// For algorithm discovery, see: https://github.com/ericchiang/piv-go/issues/1
const (
AlgorithmEC256 Algorithm = iota + 1
AlgorithmEC384
AlgorithmEd25519
AlgorithmRSA1024
AlgorithmRSA2048
)
// PINPolicy represents PIN requirements when signing or decrypting with an
// asymmetric key in a given slot.
type PINPolicy int
// PIN policies supported by this package.
//
// BUG(ericchiang): Caching for PINPolicyOnce isn't supported on YubiKey
// versions older than 4.3.0 due to issues with verifying if a PIN is needed.
// If specified, a PIN will be required for every operation.
const (
PINPolicyNever PINPolicy = iota + 1
PINPolicyOnce
PINPolicyAlways
)
// TouchPolicy represents proof-of-presence requirements when signing or
// decrypting with asymmetric key in a given slot.
type TouchPolicy int
// Touch policies supported by this package.
const (
TouchPolicyNever TouchPolicy = iota + 1
TouchPolicyAlways
TouchPolicyCached
)
// Origin represents whether a key was generated on the hardware, or has been
// imported into it.
type Origin int
// Origins supported by this package.
const (
OriginGenerated Origin = iota + 1
OriginImported
)
const (
tagPINPolicy = 0xaa
tagTouchPolicy = 0xab
)
var pinPolicyMap = map[PINPolicy]byte{
PINPolicyNever: 0x01,
PINPolicyOnce: 0x02,
PINPolicyAlways: 0x03,
}
var pinPolicyMapInv = map[byte]PINPolicy{
0x01: PINPolicyNever,
0x02: PINPolicyOnce,
0x03: PINPolicyAlways,
}
var touchPolicyMap = map[TouchPolicy]byte{
TouchPolicyNever: 0x01,
TouchPolicyAlways: 0x02,
TouchPolicyCached: 0x03,
}
var touchPolicyMapInv = map[byte]TouchPolicy{
0x01: TouchPolicyNever,
0x02: TouchPolicyAlways,
0x03: TouchPolicyCached,
}
var originMap = map[Origin]byte{
OriginGenerated: 0x01,
OriginImported: 0x02,
}
var originMapInv = map[byte]Origin{
0x01: OriginGenerated,
0x02: OriginImported,
}
var algorithmsMap = map[Algorithm]byte{
AlgorithmEC256: algECCP256,
AlgorithmEC384: algECCP384,
AlgorithmEd25519: algEd25519,
AlgorithmRSA1024: algRSA1024,
AlgorithmRSA2048: algRSA2048,
}
var algorithmsMapInv = map[byte]Algorithm{
algECCP256: AlgorithmEC256,
algECCP384: AlgorithmEC384,
algEd25519: AlgorithmEd25519,
algRSA1024: AlgorithmRSA1024,
algRSA2048: AlgorithmRSA2048,
}
// AttestationCertificate returns the YubiKey's attestation certificate, which
// is unique to the key and signed by Yubico.
func (yk *YubiKey) AttestationCertificate() (*x509.Certificate, error) {
return yk.Certificate(slotAttestation)
}
// Attest generates a certificate for a key, signed by the YubiKey's attestation
// certificate. This can be used to prove a key was generate on a specific
// YubiKey.
//
// This method is only supported for YubiKey versions >= 4.3.0.
// https://developers.yubico.com/PIV/Introduction/PIV_attestation.html
//
// Certificates returned by this method MUST NOT be used for anything other than
// attestion or determining the slots public key. For example, the certificate
// is NOT suitable for TLS.
//
// If the slot doesn't have a key, the returned error wraps ErrNotFound.
func (yk *YubiKey) Attest(slot Slot) (*x509.Certificate, error) {
cert, err := ykAttest(yk.tx, slot)
if err == nil {
return cert, nil
}
var e *apduErr
if errors.As(err, &e) && e.sw1 == 0x6A && e.sw2 == 0x80 {
return nil, ErrNotFound
}
return nil, err
}
func ykAttest(tx *scTx, slot Slot) (*x509.Certificate, error) {
cmd := apdu{
instruction: insAttest,
param1: byte(slot.Key),
}
resp, err := tx.Transmit(cmd)
if err != nil {
return nil, fmt.Errorf("command failed: %w", err)
}
if bytes.HasPrefix(resp, []byte{0x70}) {
b, _, err := unmarshalASN1(resp, 0, 0x10) // tag 0x70
if err != nil {
return nil, fmt.Errorf("unmarshaling certificate: %v", err)
}
resp = b
}
cert, err := x509.ParseCertificate(resp)
if err != nil {
return nil, fmt.Errorf("parsing certificate: %v", err)
}
return cert, nil
}
// KeyInfo holds unprotected metadata about a key slot.
type KeyInfo struct {
Algorithm Algorithm
PINPolicy PINPolicy
TouchPolicy TouchPolicy
Origin Origin
PublicKey crypto.PublicKey
}
func (ki *KeyInfo) unmarshal(b []byte) error {
for len(b) > 0 {
var v asn1.RawValue
rest, err := asn1.Unmarshal(b, &v)
if err != nil {
return err
}
b = rest
if v.Class != 0 || v.IsCompound {
continue
}
var ok bool
switch v.Tag {
case 1:
if len(v.Bytes) != 1 {
return errors.New("invalid algorithm in response")
}
if ki.Algorithm, ok = algorithmsMapInv[v.Bytes[0]]; !ok {
return errors.New("unknown algorithm in response")
}
case 2:
if len(v.Bytes) != 2 {
return errors.New("invalid policy in response")
}
if ki.PINPolicy, ok = pinPolicyMapInv[v.Bytes[0]]; !ok {
return errors.New("unknown PIN policy in response")
}
if ki.TouchPolicy, ok = touchPolicyMapInv[v.Bytes[1]]; !ok {
return errors.New("unknown touch policy in response")
}
case 3:
if len(v.Bytes) != 1 {
return errors.New("invalid origin in response")
}
if ki.Origin, ok = originMapInv[v.Bytes[0]]; !ok {
return errors.New("unknown origin in response")
}
case 4:
ki.PublicKey, err = decodePublic(v.Bytes, ki.Algorithm)
if err != nil {
return fmt.Errorf("parse public key: %w", err)
}
default:
// TODO: According to the Yubico website, we get two more fields,
// if we pass 0x80 or 0x81 as slots:
// 1. Default value (for PIN/PUK and management key): Whether the
// default value is used.
// 2. Retries (for PIN/PUK): The number of retries remaining
// However, it seems the reference implementation does not expect
// these and can not parse them out:
// https://github.com/Yubico/yubico-piv-tool/blob/yubico-piv-tool-2.3.1/lib/util.c#L1529
// For now, we just ignore them.
}
}
return nil
}
// KeyInfo returns public information about the given key slot. It is only
// supported by YubiKeys with a version >= 5.3.0.
func (yk *YubiKey) KeyInfo(slot Slot) (KeyInfo, error) {
// https://developers.yubico.com/PIV/Introduction/Yubico_extensions.html#_get_metadata
cmd := apdu{
instruction: insGetMetadata,
param1: 0x00,
param2: byte(slot.Key),
}
resp, err := yk.tx.Transmit(cmd)
if err != nil {
return KeyInfo{}, fmt.Errorf("command failed: %w", err)
}
var ki KeyInfo
if err := ki.unmarshal(resp); err != nil {
return KeyInfo{}, err
}
return ki, nil
}
// Certificate returns the certifiate object stored in a given slot.
//
// If a certificate hasn't been set in the provided slot, the returned error
// wraps ErrNotFound.
func (yk *YubiKey) Certificate(slot Slot) (*x509.Certificate, error) {
cmd := apdu{
instruction: insGetData,
param1: 0x3f,
param2: 0xff,
data: []byte{
0x5c, // Tag list
0x03, // Length of tag
byte(slot.Object >> 16),
byte(slot.Object >> 8),
byte(slot.Object),
},
}
resp, err := yk.tx.Transmit(cmd)
if err != nil {
return nil, fmt.Errorf("command failed: %w", err)
}
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=85
obj, _, err := unmarshalASN1(resp, 1, 0x13) // tag 0x53
if err != nil {
return nil, fmt.Errorf("unmarshaling response: %v", err)
}
certDER, _, err := unmarshalASN1(obj, 1, 0x10) // tag 0x70
if err != nil {
return nil, fmt.Errorf("unmarshaling certificate: %v", err)
}
cert, err := x509.ParseCertificate(certDER)
if err != nil {
return nil, fmt.Errorf("parsing certificate: %v", err)
}
return cert, nil
}
// marshalASN1Length encodes the length.
func marshalASN1Length(n uint64) []byte {
var l []byte
if n < 0x80 {
l = []byte{byte(n)}
} else if n < 0x100 {
l = []byte{0x81, byte(n)}
} else {
l = []byte{0x82, byte(n >> 8), byte(n)}
}
return l
}
// marshalASN1 encodes a tag, length and data.
//
// TODO: clean this up and maybe switch to cryptobyte?
func marshalASN1(tag byte, data []byte) []byte {
l := marshalASN1Length(uint64(len(data)))
d := append([]byte{tag}, l...)
return append(d, data...)
}
// SetCertificate stores a certificate object in the provided slot. Setting a
// certificate isn't required to use the associated key for signing or
// decryption.
func (yk *YubiKey) SetCertificate(key [24]byte, slot Slot, cert *x509.Certificate) error {
if err := ykAuthenticate(yk.tx, key, yk.rand); err != nil {
return fmt.Errorf("authenticating with management key: %w", err)
}
return ykStoreCertificate(yk.tx, slot, cert)
}
func ykStoreCertificate(tx *scTx, slot Slot, cert *x509.Certificate) error {
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=40
data := marshalASN1(0x70, cert.Raw)
// "for a certificate encoded in uncompressed form CertInfo shall be 0x00"
data = append(data, marshalASN1(0x71, []byte{0x00})...)
// Error Detection Code
data = append(data, marshalASN1(0xfe, nil)...)
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=94
data = append([]byte{
0x5c, // Tag list
0x03, // Length of tag
byte(slot.Object >> 16),
byte(slot.Object >> 8),
byte(slot.Object),
}, marshalASN1(0x53, data)...)
cmd := apdu{
instruction: insPutData,
param1: 0x3f,
param2: 0xff,
data: data,
}
if _, err := tx.Transmit(cmd); err != nil {
return fmt.Errorf("command failed: %v", err)
}
return nil
}
// Key is used for key generation and holds different options for the key.
//
// While keys can have default PIN and touch policies, this package currently
// doesn't support this option, and all fields must be provided.
type Key struct {
// Algorithm to use when generating the key.
Algorithm Algorithm
// PINPolicy for the key.
//
// BUG(ericchiang): some older YubiKeys (third generation) will silently
// drop this value. If PINPolicyNever or PINPolicyOnce is supplied but the
// key still requires a PIN every time, you may be using a buggy key and
// should supply PINPolicyAlways. See https://github.com/go-piv/piv-go/issues/60
PINPolicy PINPolicy
// TouchPolicy for the key.
TouchPolicy TouchPolicy
}
// GenerateKey generates an asymmetric key on the card, returning the key's
// public key.
func (yk *YubiKey) GenerateKey(key [24]byte, slot Slot, opts Key) (crypto.PublicKey, error) {
if err := ykAuthenticate(yk.tx, key, yk.rand); err != nil {
return nil, fmt.Errorf("authenticating with management key: %w", err)
}
return ykGenerateKey(yk.tx, slot, opts)
}
func ykGenerateKey(tx *scTx, slot Slot, o Key) (crypto.PublicKey, error) {
alg, ok := algorithmsMap[o.Algorithm]
if !ok {
return nil, fmt.Errorf("unsupported algorithm")
}
tp, ok := touchPolicyMap[o.TouchPolicy]
if !ok {
return nil, fmt.Errorf("unsupported touch policy")
}
pp, ok := pinPolicyMap[o.PINPolicy]
if !ok {
return nil, fmt.Errorf("unsupported pin policy")
}
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=95
cmd := apdu{
instruction: insGenerateAsymmetric,
param2: byte(slot.Key),
data: []byte{
0xac,
0x09, // length of remaining data
algTag, 0x01, alg,
tagPINPolicy, 0x01, pp,
tagTouchPolicy, 0x01, tp,
},
}
resp, err := tx.Transmit(cmd)
if err != nil {
return nil, fmt.Errorf("command failed: %w", err)
}
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=95
obj, _, err := unmarshalASN1(resp, 1, 0x49)
if err != nil {
return nil, fmt.Errorf("unmarshal response: %v", err)
}
return decodePublic(obj, o.Algorithm)
}
func decodePublic(b []byte, alg Algorithm) (crypto.PublicKey, error) {
var curve elliptic.Curve
switch alg {
case AlgorithmRSA1024, AlgorithmRSA2048:
pub, err := decodeRSAPublic(b)
if err != nil {
return nil, fmt.Errorf("decoding rsa public key: %v", err)
}
return pub, nil
case AlgorithmEC256:
curve = elliptic.P256()
case AlgorithmEC384:
curve = elliptic.P384()
case AlgorithmEd25519:
pub, err := decodeEd25519Public(b)
if err != nil {
return nil, fmt.Errorf("decoding ed25519 public key: %v", err)
}
return pub, nil
default:
return nil, fmt.Errorf("unsupported algorithm")
}
pub, err := decodeECPublic(b, curve)
if err != nil {
return nil, fmt.Errorf("decoding ec public key: %v", err)
}
return pub, nil
}
// KeyAuth is used to authenticate against the YubiKey on each signing and
// decryption request.
type KeyAuth struct {
// PIN, if provided, is a static PIN used to authenticate against the key.
// If provided, PINPrompt is ignored.
PIN string
// PINPrompt can be used to interactively request the PIN from the user. The
// method is only called when needed. For example, if a key specifies
// PINPolicyOnce, PINPrompt will only be called once per YubiKey struct.
PINPrompt func() (pin string, err error)
// PINPolicy can be used to specify the PIN caching strategy for the slot. If
// not provided, this will be inferred from the attestation certificate.
//
// This field is required on older (<4.3.0) YubiKeys when using PINPrompt,
// as well as for keys imported to the card.
PINPolicy PINPolicy
}
func (k KeyAuth) authTx(yk *YubiKey, pp PINPolicy) error {
// PINPolicyNever shouldn't require a PIN.
if pp == PINPolicyNever {
return nil
}
// PINPolicyAlways should always prompt a PIN even if the key says that
// login isn't needed.
// https://github.com/go-piv/piv-go/issues/49
if pp != PINPolicyAlways && !ykLoginNeeded(yk.tx) {
return nil
}
pin := k.PIN
if pin == "" && k.PINPrompt != nil {
p, err := k.PINPrompt()
if err != nil {
return fmt.Errorf("pin prompt: %v", err)
}
pin = p
}
if pin == "" {
return fmt.Errorf("pin required but wasn't provided")
}
return ykLogin(yk.tx, pin)
}
func (k KeyAuth) do(yk *YubiKey, pp PINPolicy, f func(tx *scTx) ([]byte, error)) ([]byte, error) {
if err := k.authTx(yk, pp); err != nil {
return nil, err
}
return f(yk.tx)
}
func pinPolicy(yk *YubiKey, slot Slot) (PINPolicy, error) {
if supportsVersion(yk.Version(), 5, 3, 0) {
info, err := yk.KeyInfo(slot)
if err != nil {
return 0, fmt.Errorf("get key info: %v", err)
}
return info.PINPolicy, nil
}
cert, err := yk.Attest(slot)
if err != nil {
var e *apduErr
if errors.As(err, &e) && e.sw1 == 0x6d && e.sw2 == 0x00 {
// Attestation cert command not supported, probably an older YubiKey.
// Guess PINPolicyAlways.
//
// See https://github.com/go-piv/piv-go/issues/55
return PINPolicyAlways, nil
}
return 0, fmt.Errorf("get attestation cert: %v", err)
}
a, err := parseAttestation(cert)
if err != nil {
return 0, fmt.Errorf("parse attestation cert: %v", err)
}
if _, ok := pinPolicyMap[a.PINPolicy]; ok {
return a.PINPolicy, nil
}
return PINPolicyOnce, nil
}
// PrivateKey is used to access signing and decryption options for the key
// stored in the slot. The returned key implements crypto.Signer and/or
// crypto.Decrypter depending on the key type.
//
// If the public key hasn't been stored externally, it can be provided by
// fetching the slot's attestation certificate:
//
// cert, err := yk.Attest(slot)
// if err != nil {
// // ...
// }
// priv, err := yk.PrivateKey(slot, cert.PublicKey, auth)
func (yk *YubiKey) PrivateKey(slot Slot, public crypto.PublicKey, auth KeyAuth) (crypto.PrivateKey, error) {
pp := PINPolicyNever
if _, ok := pinPolicyMap[auth.PINPolicy]; ok {
// If the PIN policy is manually specified, trust that value instead of
// trying to use the attestation certificate.
pp = auth.PINPolicy
} else if auth.PIN != "" || auth.PINPrompt != nil {
// Attempt to determine the key's PIN policy. This helps inform the
// strategy for when to prompt for a PIN.
policy, err := pinPolicy(yk, slot)
if err != nil {
return nil, err
}
pp = policy
}
switch pub := public.(type) {
case *ecdsa.PublicKey:
return &ECDSAPrivateKey{yk, slot, pub, auth, pp}, nil
case ed25519.PublicKey:
return &keyEd25519{yk, slot, pub, auth, pp}, nil
case *rsa.PublicKey:
return &keyRSA{yk, slot, pub, auth, pp}, nil
default:
return nil, fmt.Errorf("unsupported public key type: %T", public)
}
}
// SetPrivateKeyInsecure is an insecure method which imports a private key into the slot.
// Users should almost always use GeneratePrivateKey() instead.
//