forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassid.go
469 lines (386 loc) · 10.6 KB
/
classid.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
)
// ClassID identifies the environment via a well-known identifier. This can be
// an OID, a UUID, variable-length opaque bytes or a profile-defined extension type.
type ClassID struct {
Value IClassIDValue
}
// NewClassID creates a new ClassID of the specified type using the specified value.
func NewClassID(val any, typ string) (*ClassID, error) {
factory, ok := classIDValueRegister[typ]
if !ok {
return nil, fmt.Errorf("unknown class id type: %s", typ)
}
return factory(val)
}
// Valid returns nil if the ClassID is valid, or an error describing the
// problem, if it is not.
func (o ClassID) Valid() error {
if o.Value == nil {
return errors.New("nil value")
}
return o.Value.Valid()
}
// Type returns the type of the ClassID
func (o ClassID) Type() string {
if o.Value == nil {
return ""
}
return o.Value.Type()
}
// Bytes returns a []byte containing the raw bytes of the class id value
func (o ClassID) Bytes() []byte {
if o.Value == nil {
return []byte{}
}
return o.Value.Bytes()
}
// IsSet returns true iff the underlying class id value has been set (is not nil)
func (o ClassID) IsSet() bool {
return o.Value != nil
}
// MarshalCBOR serializes the target ClassID to CBOR
func (o ClassID) MarshalCBOR() ([]byte, error) {
return em.Marshal(o.Value)
}
// UnmarshalCBOR deserializes the supplied CBOR buffer into the target ClassID.
// It is undefined behavior to try and inspect the target ClassID in case this
// method returns an error.
func (o *ClassID) UnmarshalCBOR(data []byte) error {
return dm.Unmarshal(data, &o.Value)
}
// UnmarshalJSON deserializes the supplied JSON object into the target ClassID
// The class id object must have the following shape:
//
// {
// "type": "<CLASS_ID_TYPE>",
// "value": <CLASS_ID_VALUE>
// }
//
// where <CLASS_ID_TYPE> must be one of the known IClassIDValue implementation
// type names (available in this implementation: "uuid", "oid",
// "psa.impl-id", "int", "bytes"), and <CLASS_ID_VALUE> is the JSON encoding of the underlying
// class id value. The exact encoding is <CLASS_ID_TYPE> dependent. For the base
// implementation types it is
//
// oid: dot-separated integers, e.g. "1.2.3.4"
// psa.impl-id: base64-encoded bytes, e.g. "YWNtZS1pbXBsZW1lbnRhdGlvbi1pZC0wMDAwMDAwMDE="
// uuid: standard UUID string representation, e.g. "550e8400-e29b-41d4-a716-446655440000"
// int: an integer value, e.g. 7
// bytes: a variable length opaque bytes, example {0x07, 0x12, 0x34}
func (o *ClassID) UnmarshalJSON(data []byte) error {
var tnv encoding.TypeAndValue
if err := json.Unmarshal(data, &tnv); err != nil {
return fmt.Errorf("class id decoding failure: %w", err)
}
decoded, err := NewClassID(nil, tnv.Type)
if err != nil {
return err
}
if err := json.Unmarshal(tnv.Value, &decoded.Value); err != nil {
return fmt.Errorf(
"cannot unmarshal class id: %w",
err,
)
}
if err := decoded.Value.Valid(); err != nil {
return fmt.Errorf("invalid %s: %w", tnv.Type, err)
}
o.Value = decoded.Value
return nil
}
// MarshalJSON serializes the target ClassID to JSON
func (o ClassID) MarshalJSON() ([]byte, error) {
return extensions.TypeChoiceValueMarshalJSON(o.Value)
}
// String returns a printable string of the ClassID value. UUIDs use the
// canonical 8-4-4-4-12 format, PSA Implementation IDs are base64 encoded.
// OIDs are output in dotted-decimal notation.
func (o ClassID) String() string {
if o.Value == nil {
return ""
}
return o.Value.String()
}
// SetImplID sets the value of the target ClassID to the supplied PSA
// Implementation ID (see Section 3.2.2 of draft-tschofenig-rats-psa-token)
func (o *ClassID) SetImplID(implID ImplID) *ClassID {
if o != nil {
o.Value = TaggedImplID(implID)
}
return o
}
// GetImplID retrieves the value of the PSA Implementation ID
// (see Section 3.2.2 of draft-tschofenig-rats-psa-token) from ClassID
func (o ClassID) GetImplID() (ImplID, error) {
switch t := o.Value.(type) {
case *TaggedImplID:
return ImplID(*t), nil
case TaggedImplID:
return ImplID(t), nil
default:
return ImplID{}, fmt.Errorf("class-id type is: %T", t)
}
}
type IClassIDValue interface {
extensions.ITypeChoiceValue
Bytes() []byte
}
// SetUUID sets the value of the target ClassID to the supplied UUID
func (o *ClassID) SetUUID(uuid UUID) *ClassID {
if o != nil {
o.Value = TaggedUUID(uuid)
}
return o
}
func (o ClassID) GetUUID() (UUID, error) {
switch t := o.Value.(type) {
case *TaggedUUID:
return UUID(*t), nil
case TaggedUUID:
return UUID(t), nil
default:
return UUID{}, fmt.Errorf("class-id type is: %T", t)
}
}
// SetOID sets the value of the target ClassID to the supplied OID.
// The OID is a string in dotted-decimal notation
func (o *ClassID) SetOID(s string) error {
if o != nil {
var berOID OID
if err := berOID.FromString(s); err != nil {
return err
}
o.Value = TaggedOID(berOID)
}
return nil
}
// GetOID gets the value of the OID in a string dotted-decimal notation
func (o ClassID) GetOID() (string, error) {
switch t := o.Value.(type) {
case *TaggedOID:
oid := OID(*t)
stringOID := oid.String()
return stringOID, nil
case TaggedOID:
oid := OID(t)
stringOID := oid.String()
return stringOID, nil
default:
return "", fmt.Errorf("class-id type is: %T", t)
}
}
const ImplIDType = "psa.impl-id"
type ImplID [32]byte
func (o ImplID) String() string {
return base64.StdEncoding.EncodeToString(o[:])
}
func (o ImplID) Valid() error {
return nil
}
type TaggedImplID ImplID
func NewImplIDClassID(val any) (*ClassID, error) {
var ret TaggedImplID
if val == nil {
return &ClassID{&TaggedImplID{}}, nil
}
switch t := val.(type) {
case []byte:
if nb := len(t); nb != 32 {
return nil, fmt.Errorf("bad psa.impl-id: got %d bytes, want 32", nb)
}
copy(ret[:], t)
case string:
v, err := base64.StdEncoding.DecodeString(t)
if err != nil {
return nil, fmt.Errorf("bad psa.impl-id: %w", err)
}
if nb := len(v); nb != 32 {
return nil, fmt.Errorf("bad psa.impl-id: decoded %d bytes, want 32", nb)
}
copy(ret[:], v)
case TaggedImplID:
copy(ret[:], t[:])
case *TaggedImplID:
copy(ret[:], (*t)[:])
case ImplID:
copy(ret[:], t[:])
case *ImplID:
copy(ret[:], (*t)[:])
default:
return nil, fmt.Errorf("unexpected type for psa.impl-id: %T", t)
}
return &ClassID{&ret}, nil
}
func MustNewImplIDClassID(val any) *ClassID {
ret, err := NewImplIDClassID(val)
if err != nil {
panic(err)
}
return ret
}
func (o TaggedImplID) Valid() error {
return ImplID(o).Valid()
}
func (o TaggedImplID) String() string {
return ImplID(o).String()
}
func (o TaggedImplID) Type() string {
return ImplIDType
}
func (o TaggedImplID) Bytes() []byte {
return o[:]
}
func (o TaggedImplID) MarshalJSON() ([]byte, error) {
return json.Marshal((o)[:])
}
func (o *TaggedImplID) UnmarshalJSON(data []byte) error {
var out []byte
if err := json.Unmarshal(data, &out); err != nil {
return err
}
if len(out) != 32 {
return fmt.Errorf("bad psa.impl-id: decoded %d bytes, want 32", len(out))
}
copy((*o)[:], out)
return nil
}
func NewOIDClassID(val any) (*ClassID, error) {
ret, err := NewTaggedOID(val)
if err != nil {
return nil, err
}
return &ClassID{ret}, nil
}
func MustNewOIDClassID(val any) *ClassID {
ret, err := NewOIDClassID(val)
if err != nil {
panic(err)
}
return ret
}
func NewUUIDClassID(val any) (*ClassID, error) {
if val == nil {
return &ClassID{&TaggedUUID{}}, nil
}
ret, err := NewTaggedUUID(val)
if err != nil {
return nil, err
}
return &ClassID{ret}, nil
}
func MustNewUUIDClassID(val any) *ClassID {
ret, err := NewUUIDClassID(val)
if err != nil {
panic(err)
}
return ret
}
const IntType = "int"
type TaggedInt int
func NewIntClassID(val any) (*ClassID, error) {
if val == nil {
zeroVal := TaggedInt(0)
return &ClassID{&zeroVal}, nil
}
var ret TaggedInt
switch t := val.(type) {
case string:
i, err := strconv.Atoi(t)
if err != nil {
return nil, fmt.Errorf("bad int: %w", err)
}
ret = TaggedInt(i)
case []byte:
if len(t) != 8 {
return nil, fmt.Errorf("bad int: want 8 bytes, got %d bytes", len(t))
}
ret = TaggedInt(binary.BigEndian.Uint64(t))
case int:
ret = TaggedInt(t)
case *int:
ret = TaggedInt(*t)
case int64:
ret = TaggedInt(t)
case *int64:
ret = TaggedInt(*t)
case uint64:
ret = TaggedInt(t)
case *uint64:
ret = TaggedInt(*t)
default:
return nil, fmt.Errorf("unexpected type for int: %T", t)
}
if err := ret.Valid(); err != nil {
return nil, err
}
return &ClassID{&ret}, nil
}
func (o TaggedInt) String() string {
return fmt.Sprint(int(o))
}
func (o TaggedInt) Valid() error {
return nil
}
func (o TaggedInt) Type() string {
return "int"
}
func (o TaggedInt) Bytes() []byte {
var ret [8]byte
binary.BigEndian.PutUint64(ret[:], uint64(o))
return ret[:]
}
// NewBytesClassID creates a New ClassID of type bytes
// The supplied interface parameter could be
// a byte slice, a pointer to a byte slice or a string
func NewBytesClassID(val any) (*ClassID, error) {
ret, err := NewBytes(val)
if err != nil {
return nil, err
}
return &ClassID{ret}, nil
}
// IClassIDFactory defines the signature for the factory functions that may be
// registred using RegisterClassIDType to provide a new implementation of the
// corresponding type choice. The factory function should create a new *ClassID
// 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 IClassIDFactory func(any) (*ClassID, error)
var classIDValueRegister = map[string]IClassIDFactory{
OIDType: NewOIDClassID,
UUIDType: NewUUIDClassID,
IntType: NewIntClassID,
BytesType: NewBytesClassID,
ImplIDType: NewImplIDClassID,
}
// RegisterClassIDType registers a new IClassIDValue implementation (created
// by the provided IClassIDFactory) under the specified CBOR tag.
func RegisterClassIDType(tag uint64, factory IClassIDFactory) error {
nilVal, err := factory(nil)
if err != nil {
return err
}
typ := nilVal.Type()
if _, exists := classIDValueRegister[typ]; exists {
return fmt.Errorf("class ID type with name %q already exists", typ)
}
if err := registerCOMIDTag(tag, nilVal.Value); err != nil {
return err
}
classIDValueRegister[typ] = factory
return nil
}