forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptokey.go
763 lines (610 loc) · 17.6 KB
/
cryptokey.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
// Copyright 2023 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"bytes"
"crypto"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
"github.com/veraison/go-cose"
"github.com/veraison/swid"
)
const (
// PKIXBase64KeyType indicates a PEM-encoded SubjectPublicKeyInfo. See
// https://www.rfc-editor.org/rfc/rfc7468#section-13
PKIXBase64KeyType = "pkix-base64-key"
// PKIXBase64CertType indicates a PEM-encoded X.509 public key
// certificate. See https://www.rfc-editor.org/rfc/rfc7468#section-5
PKIXBase64CertType = "pkix-base64-cert"
// PKIXBase64CertPathType indicates a X.509 certificate chain created
// by the concatenation of as many PEM encoded X.509 certificates as
// needed. The certificates MUST be concatenated in order so that each
// directly certifies the one preceding.
PKIXBase64CertPathType = "pkix-base64-cert-path"
// COSEKeyType represents a CBOR encoded COSE_Key or COSE_KeySet. See
// https://www.rfc-editor.org/rfc/rfc9052#section-7
COSEKeyType = "cose-key"
// ThumbprintType represents a digest of a raw public key. The digest
// value may be used to find the public key if contained in a lookup
// table.
ThumbprintType = "thumbprint"
// CertThumbprintType represents a digest of a certificate. The digest
// value may be used to find the certificate if contained in a lookup
// table.
CertThumbprintType = "cert-thumbprint"
// CertPathThumbprintType represents a digest of a certification path.
// The digest value may be used to find the certificate path if
// contained in a lookup table.
CertPathThumbprintType = "cert-path-thumbprint"
)
// CryptoKey is the struct implementing CoRIM crypto-key-type-choice. See
// https://www.ietf.org/archive/id/draft-ietf-rats-corim-02.html#name-crypto-keys
type CryptoKey struct {
Value ICryptoKeyValue
}
// NewCryptoKey returns the pointer to a new CryptoKey of the specified type,
// constructed using the provided value k. The type of k depends on the
// specified crypto key type. For PKIX types, k must be a string. For COSE_Key,
// k must be a []byte. For thumbprint types, k must be a swid.HashEntry.
func NewCryptoKey(k any, typ string) (*CryptoKey, error) {
factory, ok := cryptoKeyValueRegister[typ]
if !ok {
return nil, fmt.Errorf("unexpected CryptoKey type: %s", typ)
}
return factory(k)
}
// MustNewCryptoKey is the same as NewCryptoKey, but does not return an error,
// and panics if there is a problem.
func MustNewCryptoKey(k any, typ string) *CryptoKey {
key, err := NewCryptoKey(k, typ)
if err != nil {
panic(err)
}
return key
}
// String returns the string representation of the CryptoKey.
func (o CryptoKey) String() string {
return o.Value.String()
}
// Valid returns an error if validation of the CryptoKey fails, or nil if it
// succeeds.
func (o CryptoKey) Valid() error {
return o.Value.Valid()
}
// Type returns the type of the CryptoKey value
func (o CryptoKey) Type() string {
return o.Value.Type()
}
// PublicKey returns a crypto.PublicKey constructed from the CryptoKey's
// underlying value. This returns an error if the CryptoKey is one of the
// thumbprint types.
func (o CryptoKey) PublicKey() (crypto.PublicKey, error) {
return o.Value.PublicKey()
}
// MarshalJSON returns a []byte containing the JSON representation of the
// CryptoKey.
func (o CryptoKey) MarshalJSON() ([]byte, error) {
valueBytes, err := json.Marshal(o.Value.String())
if err != nil {
return nil, err
}
value := encoding.TypeAndValue{
Type: o.Value.Type(),
Value: valueBytes,
}
return json.Marshal(value)
}
// UnmarshalJSON populates the CryptoKey from the JSON representation inside
// the provided []byte.
func (o *CryptoKey) UnmarshalJSON(b []byte) error {
var value encoding.TypeAndValue
if err := json.Unmarshal(b, &value); err != nil {
return err
}
if value.Type == "" {
return errors.New("key type not set")
}
factory, ok := cryptoKeyValueRegister[value.Type]
if !ok {
return fmt.Errorf("unexpected ICryptoKeyValue type: %q", value.Type)
}
var valueString string
if err := json.Unmarshal(value.Value, &valueString); err != nil {
return err
}
k, err := factory(valueString)
if err != nil {
return err
}
o.Value = k.Value
return o.Valid()
}
// MarshalCBOR returns a []byte containing the CBOR representation of the
// CryptoKey.
func (o CryptoKey) MarshalCBOR() ([]byte, error) {
return em.Marshal(o.Value)
}
// UnmarshalCBOR populates the CryptoKey from the CBOR representation inside
// the provided []byte.
func (o *CryptoKey) UnmarshalCBOR(b []byte) error {
return dm.Unmarshal(b, &o.Value)
}
// ICryptoKeyValue is the interface implemented by the concrete CryptoKey value
// types.
type ICryptoKeyValue interface {
extensions.ITypeChoiceValue
// PublicKey returns a crypto.PublicKey constructed from the
// ICryptoKeyValue's underlying value. This returns an error if the
// ICryptoKeyValue is one of the thumbprint types.
PublicKey() (crypto.PublicKey, error)
}
// TaggedPKIXBase64Key is a PEM-encoded SubjectPublicKeyInfo. See
// https://www.rfc-editor.org/rfc/rfc7468#section-13
type TaggedPKIXBase64Key string
func NewPKIXBase64Key(k any) (*CryptoKey, error) {
s, ok := k.(string)
if !ok {
return nil, fmt.Errorf("value must be a string; found %T", k)
}
key := TaggedPKIXBase64Key(s)
if err := key.Valid(); err != nil {
return nil, err
}
return &CryptoKey{key}, nil
}
func MustNewPKIXBase64Key(k any) *CryptoKey {
key, err := NewPKIXBase64Key(k)
if err != nil {
panic(err)
}
return key
}
func (o TaggedPKIXBase64Key) String() string {
return string(o)
}
func (o TaggedPKIXBase64Key) Valid() error {
_, err := o.PublicKey()
return err
}
func (o TaggedPKIXBase64Key) Type() string {
return PKIXBase64KeyType
}
func (o TaggedPKIXBase64Key) PublicKey() (crypto.PublicKey, error) {
if string(o) == "" {
return nil, errors.New("key value not set")
}
block, rest := pem.Decode([]byte(o))
if block == nil {
return nil, errors.New("could not decode PEM block")
}
if len(rest) != 0 {
return nil, errors.New("trailing data found after PEM block")
}
if block.Type != "PUBLIC KEY" {
return nil, fmt.Errorf(
"unexpected PEM block type: %q, expected \"PUBLIC KEY\"",
block.Type,
)
}
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("unable to parse public key: %w", err)
}
return key, nil
}
// TaggedPKIXBase64Cert is a PEM-encoded X.509 public key
// certificate. See https://www.rfc-editor.org/rfc/rfc7468#section-5
type TaggedPKIXBase64Cert string
func NewPKIXBase64Cert(k any) (*CryptoKey, error) {
s, ok := k.(string)
if !ok {
return nil, fmt.Errorf("value must be a string; found %T", k)
}
cert := TaggedPKIXBase64Cert(s)
if err := cert.Valid(); err != nil {
return nil, err
}
return &CryptoKey{cert}, nil
}
func MustNewPKIXBase64Cert(k any) *CryptoKey {
cert, err := NewPKIXBase64Cert(k)
if err != nil {
panic(err)
}
return cert
}
func (o TaggedPKIXBase64Cert) String() string {
return string(o)
}
func (o TaggedPKIXBase64Cert) Valid() error {
_, err := o.cert()
return err
}
func (o TaggedPKIXBase64Cert) Type() string {
return PKIXBase64CertType
}
func (o TaggedPKIXBase64Cert) PublicKey() (crypto.PublicKey, error) {
cert, err := o.cert()
if err != nil {
return nil, err
}
if cert.PublicKey == nil {
return nil, errors.New("cert does not contain a crypto.PublicKey")
}
return cert.PublicKey, nil
}
func (o TaggedPKIXBase64Cert) cert() (*x509.Certificate, error) {
if string(o) == "" {
return nil, errors.New("cert value not set")
}
block, rest := pem.Decode([]byte(o))
if block == nil {
return nil, errors.New("could not decode PEM block")
}
if len(rest) != 0 {
return nil, errors.New("trailing data found after PEM block")
}
if block.Type != "CERTIFICATE" {
return nil, fmt.Errorf(
"unexpected PEM block type: %q, expected \"CERTIFICATE\"",
block.Type,
)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("could not parse x509 cert: %w", err)
}
return cert, nil
}
// TaggedPKIXBase64CertPath is a X.509 certificate chain created
// by the concatenation of as many PEM encoded X.509 certificates as
// needed. The certificates MUST be concatenated in order so that each
// directly certifies the one preceding.
type TaggedPKIXBase64CertPath string
func NewPKIXBase64CertPath(k any) (*CryptoKey, error) {
s, ok := k.(string)
if !ok {
return nil, fmt.Errorf("value must be a string; found %T", k)
}
cert := TaggedPKIXBase64CertPath(s)
if err := cert.Valid(); err != nil {
return nil, err
}
return &CryptoKey{cert}, nil
}
func MustNewPKIXBase64CertPath(k any) *CryptoKey {
cert, err := NewPKIXBase64CertPath(k)
if err != nil {
panic(err)
}
return cert
}
func (o TaggedPKIXBase64CertPath) String() string {
return string(o)
}
func (o TaggedPKIXBase64CertPath) Valid() error {
_, err := o.certPath()
return err
}
func (o TaggedPKIXBase64CertPath) Type() string {
return PKIXBase64CertPathType
}
func (o TaggedPKIXBase64CertPath) PublicKey() (crypto.PublicKey, error) {
certs, err := o.certPath()
if err != nil {
return nil, err
}
if len(certs) == 0 {
return nil, errors.New("empty cert path")
}
if certs[0].PublicKey == nil {
return nil, errors.New("leaf cert does not contain a crypto.PublicKey")
}
return certs[0].PublicKey, nil
}
func (o TaggedPKIXBase64CertPath) certPath() ([]*x509.Certificate, error) {
if string(o) == "" {
return nil, errors.New("cert value not set")
}
var certs []*x509.Certificate
var block *pem.Block
var rest []byte
rest = []byte(o)
i := 0
for {
if len(rest) == 0 {
break
}
block, rest = pem.Decode(rest)
if block == nil {
return nil, fmt.Errorf("could not decode PEM block %d", i)
}
if block.Type != "CERTIFICATE" {
return nil, fmt.Errorf(
"unexpected type for PEM block %d: %q, expected \"CERTIFICATE\"",
i, block.Type,
)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf(
"could not parse x509 cert in PEM block %d: %w",
i, err,
)
}
certs = append(certs, cert)
i++
}
return certs, nil
}
// TaggedCOSEKey is a CBOR encoded COSE_Key or COSE_KeySet. See
// https://www.rfc-editor.org/rfc/rfc9052#section-7
type TaggedCOSEKey []byte
func NewCOSEKey(k any) (*CryptoKey, error) {
var b []byte
var err error
switch t := k.(type) {
case []byte:
b = t
case string:
b, err = base64.StdEncoding.DecodeString(t)
if err != nil {
return nil, fmt.Errorf("base64 decode error: %w", err)
}
default:
return nil, fmt.Errorf("value must be a []byte or a string; found %T", k)
}
key := TaggedCOSEKey(b)
if err := key.Valid(); err != nil {
return nil, err
}
return &CryptoKey{key}, nil
}
func MustNewCOSEKey(k any) *CryptoKey {
key, err := NewCOSEKey(k)
if err != nil {
panic(err)
}
return key
}
func (o TaggedCOSEKey) String() string {
return base64.StdEncoding.EncodeToString(o)
}
func (o TaggedCOSEKey) Valid() error {
if len(o) == 0 {
return errors.New("empty COSE_Key bytes")
}
var err error
// CBOR Major type 4 == array == COSE_KeySet. Key sets are currently
// not supported by go-cose library.
if ((o[0] & 0xe0) >> 5) == 4 {
_, err = o.coseKeySet()
} else {
_, err = o.coseKey()
}
return err
}
func (o TaggedCOSEKey) Type() string {
return COSEKeyType
}
func (o TaggedCOSEKey) PublicKey() (crypto.PublicKey, error) {
if len(o) == 0 {
return nil, errors.New("empty COSE_Key value")
}
// CBOR Major type 4 == array == COSE_KeySet. Key sets are currently
// not supported by go-cose library.
if ((o[0] & 0xe0) >> 5) == 4 {
keySet, err := o.coseKeySet()
if err != nil {
return nil, err
}
if len(keySet) == 0 {
return nil, errors.New("empty COSE_KeySet")
} else if len(keySet) > 1 {
return nil, errors.New("COSE_KeySet contains more than one key")
}
return keySet[0].PublicKey()
}
coseKey, err := o.coseKey()
if err != nil {
return nil, err
}
return coseKey.PublicKey()
}
func (o TaggedCOSEKey) MarshalCBOR() ([]byte, error) {
var buf bytes.Buffer
// encodeMarshalerType in github.com/fxamacker/cbor/v2 does not look up
// assocated Tags, so we have to write them ourselves.
if _, err := buf.Write([]byte{0xd9, 0x02, 0x2e}); err != nil { // tag 558
return nil, err
}
if _, err := buf.Write(o); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (o *TaggedCOSEKey) UnmarshalCBOR(b []byte) error {
// the first 3 bytes are the tag
if !bytes.Equal([]byte{0xd9, 0x02, 0x2e}, b[:3]) {
return errors.New("did not see CBOR tag 588 at the beginning of COSE_Key structure")
}
*o = b[3:]
return nil
}
func (o TaggedCOSEKey) coseKey() (*cose.Key, error) {
coseKey := new(cose.Key)
if err := coseKey.UnmarshalCBOR(o); err != nil {
return nil, err
}
return coseKey, nil
}
func (o TaggedCOSEKey) coseKeySet() ([]*cose.Key, error) {
var keySet []*cose.Key
if err := cbor.Unmarshal(o, &keySet); err != nil {
return nil, err
}
return keySet, nil
}
type digest struct {
swid.HashEntry
}
func (o digest) String() string {
return o.HashEntry.String()
}
func (o digest) Valid() error {
return swid.ValidHashEntry(o.HashAlgID, o.HashValue)
}
func (o digest) PublicKey() (crypto.PublicKey, error) {
return nil, errors.New("cannot get PublicKey from a digest")
}
// ThumbprintTypeTaggedThumbprint represents a digest of a raw public key. The
// digest value may be used to find the public key if contained in a lookup
// table.
type TaggedThumbprint struct {
digest
}
func NewThumbprint(k any) (*CryptoKey, error) {
var he swid.HashEntry
var err error
switch t := k.(type) {
case string:
he, err = swid.ParseHashEntry(t)
if err != nil {
return nil, fmt.Errorf("swid.HashEntry decode error: %w", err)
}
case swid.HashEntry:
he = t
default:
return nil, fmt.Errorf("value must be a swid.HashEntry or a string; found %T", k)
}
key := &CryptoKey{TaggedThumbprint{digest{he}}}
if err := key.Valid(); err != nil {
return nil, err
}
return key, nil
}
func MustNewThumbprint(k any) *CryptoKey {
key, err := NewThumbprint(k)
if err != nil {
panic(err)
}
return key
}
func (o TaggedThumbprint) Type() string {
return ThumbprintType
}
// TaggedCertThumbprint represents a digest of a certificate. The digest value
// may be used to find the certificate if contained in a lookup table.
type TaggedCertThumbprint struct {
digest
}
func NewCertThumbprint(k any) (*CryptoKey, error) {
var he swid.HashEntry
var err error
switch t := k.(type) {
case string:
he, err = swid.ParseHashEntry(t)
if err != nil {
return nil, fmt.Errorf("swid.HashEntry decode error: %w", err)
}
case swid.HashEntry:
he = t
default:
return nil, fmt.Errorf("value must be a swid.HashEntry or a string; found %T", k)
}
key := &CryptoKey{TaggedCertThumbprint{digest{he}}}
if err := key.Valid(); err != nil {
return nil, err
}
return key, nil
}
func MustNewCertThumbprint(k any) *CryptoKey {
key, err := NewCertThumbprint(k)
if err != nil {
panic(err)
}
return key
}
func (o TaggedCertThumbprint) Type() string {
return CertThumbprintType
}
// TaggedCertPathThumbprint represents a digest of a certification path. The
// digest value may be used to find the certificate path if contained in a
// lookup table.
type TaggedCertPathThumbprint struct {
digest
}
func NewCertPathThumbprint(k any) (*CryptoKey, error) {
var he swid.HashEntry
var err error
switch t := k.(type) {
case string:
he, err = swid.ParseHashEntry(t)
if err != nil {
return nil, fmt.Errorf("swid.HashEntry decode error: %w", err)
}
case swid.HashEntry:
he = t
default:
return nil, fmt.Errorf("value must be a swid.HashEntry or a string; found %T", k)
}
key := &CryptoKey{TaggedCertPathThumbprint{digest{he}}}
if err := key.Valid(); err != nil {
return nil, err
}
return key, nil
}
func MustNewCertPathThumbprint(k any) *CryptoKey {
key, err := NewCertPathThumbprint(k)
if err != nil {
panic(err)
}
return key
}
func (o TaggedCertPathThumbprint) Type() string {
return CertPathThumbprintType
}
// ICryptoKeyFactory defines the signature for the factory functions that may be
// registred using RegisterCryptoKeyType to provide a new implementation of the
// corresponding type choice. The factory function should create a new *CryptoKey
// with the underlying value created based on the provided input. The range of
// valid inputs is up to the specific type choice implementation, however it
// _must_ accept nil as one of the inputs, and return the Zero value for
// implemented type.
// See also https://go.dev/ref/spec#The_zero_value
type ICryptoKeyFactory func(any) (*CryptoKey, error)
var cryptoKeyValueRegister = map[string]ICryptoKeyFactory{
// types defined by the core spec
PKIXBase64KeyType: NewPKIXBase64Key,
PKIXBase64CertType: NewPKIXBase64Cert,
PKIXBase64CertPathType: NewPKIXBase64CertPath,
COSEKeyType: NewCOSEKey,
ThumbprintType: NewThumbprint,
CertThumbprintType: NewCertThumbprint,
CertPathThumbprintType: NewCertPathThumbprint,
}
// RegisterCryptoKeyType registers a new ICryptoKeyValue implementation
// (created by the provided ICryptoKeyFactory) under the specified type name
// and CBOR tag.
func RegisterCryptoKeyType(tag uint64, factory ICryptoKeyFactory) error {
nilVal, err := factory(nil)
if err != nil {
return err
}
typ := nilVal.Type()
if _, exists := cryptoKeyValueRegister[typ]; exists {
return fmt.Errorf("crypto key type with name %q already exists", typ)
}
if err := registerCOMIDTag(tag, nilVal.Value); err != nil {
return err
}
cryptoKeyValueRegister[typ] = factory
return nil
}