forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoid.go
223 lines (174 loc) · 4.04 KB
/
oid.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/asn1"
"encoding/json"
"fmt"
"strconv"
"strings"
)
const OIDType = "oid"
// BER-encoded absolute OID
type OID []byte
func asn1OIDFromDottedDecimalString(s string) (asn1.ObjectIdentifier, error) {
if s == "" {
return nil, fmt.Errorf("empty OID")
}
if s[0] == '.' {
return nil, fmt.Errorf("OID must be absolute")
}
var asn1OID asn1.ObjectIdentifier
for _, s := range strings.Split(s, ".") {
n, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("invalid OID: %w", err)
}
if n < 0 {
return nil, fmt.Errorf("invalid OID: negative arc %d not allowed", n)
}
asn1OID = append(asn1OID, n)
}
if len(asn1OID) < 3 {
return nil,
fmt.Errorf(
"invalid OID: got %d arcs, expecting at least %d",
len(asn1OID), 3,
)
}
return asn1OID, nil
}
func (o *OID) FromString(s string) error {
// decode absolute OID in dotted-decimal format to internal representation
asn1OID, err := asn1OIDFromDottedDecimalString(s)
if err != nil {
return err
}
// encode internal representation to BER
berOID, err := asn1.Marshal(asn1OID)
if err != nil {
return err
}
// drop T&L and keep the value
oidVal, err := extractBERValue(berOID)
if err != nil {
return err
}
*o = OID(oidVal)
return nil
}
func (o OID) String() string {
var asn1OID asn1.ObjectIdentifier
tlv, err := constructBERFromVal(o)
if err != nil {
return ""
}
if _, err := asn1.Unmarshal(tlv, &asn1OID); err != nil {
return ""
}
return asn1OID.String()
}
const (
asn1AbsoluteOIDType = 0x06
asn1LongLenMask = 0x80
asn1LenBytesMask = 0x7F
)
func extractBERValue(asn1OID []byte) ([]byte, error) {
if asn1OID[0] != asn1AbsoluteOIDType {
return nil, fmt.Errorf("the supplied value is not an ASN.1 OID")
}
byteOffset := 2
if asn1OID[1]&asn1LongLenMask != 0 {
byteOffset += int(asn1OID[1] & asn1LenBytesMask)
}
return asn1OID[byteOffset:], nil
}
const (
// MaxASN1OIDLen is the maximum OID length accepted by the implementation
MaxASN1OIDLen = 255
// MinNumOIDArcs represents the minimum required arcs for a valid OID
MinNumOIDArcs = 3
)
func constructBERFromVal(val []byte) ([]byte, error) {
const maxTLOffset = 3
var OID [MaxASN1OIDLen + maxTLOffset]byte
berOID := OID[:2]
berOID[0] = asn1AbsoluteOIDType
if len(val) < 127 {
berOID[1] = byte(len(val))
} else if len(val) <= MaxASN1OIDLen {
berOID[1] = 1
berOID[1] |= asn1LongLenMask
berOID = append(berOID, byte(len(val)))
} else {
return nil, fmt.Errorf("OIDs greater than %d bytes are not accepted", MaxASN1OIDLen)
}
berOID = append(berOID, val...)
return berOID, nil
}
func (o *OID) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
return o.FromString(s)
}
func (o OID) MarshalJSON() ([]byte, error) {
return json.Marshal(o.String())
}
type TaggedOID OID
func NewTaggedOID(val any) (*TaggedOID, error) {
ret := TaggedOID{}
if val == nil {
return &ret, nil
}
switch t := val.(type) {
case string:
var berOID OID
if err := berOID.FromString(t); err != nil {
return nil, err
}
ret = TaggedOID(berOID)
case []byte:
ret = make([]byte, len(t))
copy(ret, t)
case TaggedOID:
ret = make([]byte, len(t))
copy(ret, t)
case OID:
ret = make([]byte, len(t))
copy(ret, t)
case *TaggedOID:
ret = make([]byte, len(*t))
copy(ret, (*t))
case *OID:
ret = make([]byte, len(*t))
copy(ret, (*t))
}
return &ret, nil
}
func (o TaggedOID) Type() string {
return OIDType
}
func (o TaggedOID) String() string {
return OID(o).String()
}
func (o TaggedOID) Valid() error {
return nil
}
func (o TaggedOID) Bytes() []byte {
return o
}
func (o *TaggedOID) FromString(s string) error {
return (*OID)(o).FromString(s)
}
func (o *TaggedOID) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
return o.FromString(s)
}
func (o TaggedOID) MarshalJSON() ([]byte, error) {
return json.Marshal(o.String())
}