forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbor.go
103 lines (85 loc) · 1.94 KB
/
cbor.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"fmt"
"reflect"
cbor "github.com/fxamacker/cbor/v2"
)
var (
em, emError = initCBOREncMode()
dm, dmError = initCBORDecMode()
comidTagsMap = map[uint64]interface{}{
32: TaggedURI(""),
37: TaggedUUID{},
111: TaggedOID{},
// CoMID tags
550: TaggedUEID{},
551: TaggedInt(0),
552: TaggedSVN(0),
553: TaggedMinSVN(0),
554: TaggedPKIXBase64Key(""),
555: TaggedPKIXBase64Cert(""),
556: TaggedPKIXBase64CertPath(""),
557: TaggedThumbprint{},
558: TaggedCOSEKey{},
559: TaggedCertThumbprint{},
560: TaggedBytes{},
561: TaggedCertPathThumbprint{},
// PSA profile tags
600: TaggedImplID{},
601: TaggedPSARefValID{},
602: TaggedCCAPlatformConfigID(""),
}
)
func comidTags() cbor.TagSet {
opts := cbor.TagOptions{
EncTag: cbor.EncTagRequired,
DecTag: cbor.DecTagRequired,
}
tags := cbor.NewTagSet()
for tag, typ := range comidTagsMap {
if err := tags.Add(opts, reflect.TypeOf(typ), tag); err != nil {
panic(err)
}
}
return tags
}
func initCBOREncMode() (en cbor.EncMode, err error) {
encOpt := cbor.EncOptions{
Sort: cbor.SortCoreDeterministic,
IndefLength: cbor.IndefLengthForbidden,
TimeTag: cbor.EncTagRequired,
}
return encOpt.EncModeWithTags(comidTags())
}
func initCBORDecMode() (dm cbor.DecMode, err error) {
decOpt := cbor.DecOptions{
IndefLength: cbor.IndefLengthForbidden,
}
return decOpt.DecModeWithTags(comidTags())
}
func registerCOMIDTag(tag uint64, t interface{}) error {
if _, exists := comidTagsMap[tag]; exists {
return fmt.Errorf("tag %d is already registered", tag)
}
comidTagsMap[tag] = t
var err error
em, err = initCBOREncMode()
if err != nil {
return err
}
dm, err = initCBORDecMode()
if err != nil {
return err
}
return nil
}
func init() {
if emError != nil {
panic(emError)
}
if dmError != nil {
panic(dmError)
}
}