Repository navigation
Expand file tree
/
Copy pathrawvalue.go
More file actions
347 lines (283 loc) · 9.1 KB
/
Copy pathrawvalue.go
File metadata and controls
347 lines (283 loc) · 9.1 KB
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
// Copyright 2021-2026 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
)
const MaskedType = "masked"
// IRawValueValue is the interface implemented by concrete RawValue value
// types.
type IRawValueValue interface {
extensions.ITypeChoiceValue
Bytes() []byte
}
// NewRawValue returns the pointer to a new RawValue of the specified type,
// constructed using the provided value b. The type of b depends on the
// specified raw value type. For a bytes value, it must be a []byte, for
// masked bytes, it must be a [2][]byte, [][]byte of length 2, or a []byte (in
// which case the mast will be a value of the same length with all bits set).
func NewRawValue(b any, typ string) (*RawValue, error) {
factory, ok := rawValueValueRegister[typ]
if !ok {
return nil, fmt.Errorf("unexpected RawValue type: %s", typ)
}
return factory(b)
}
// MustNewRawValue is the same as NewRawValues, but it panics on error.
func MustNewRawValue(b any, typ string) *RawValue {
ret, err := NewRawValue(b, typ)
if err != nil {
panic(err)
}
return ret
}
// NewRawValuesFromBytes returns a pointer to a new RawValue with the specified
// bytes as the underlying value.
func NewRawValueFromBytes(b []byte) *RawValue {
ret, err := NewBytesRawValue(b)
if err != nil {
// cannot happen
panic(err)
}
return ret
}
// NewRawValueWithMask returns a pointer to a new RawValue with a masked
// underlying values created from the specified value and mask. An error is
// returned if the length of the mask does not match that of the value.
func NewRawValueWithMask(val []byte, mask []byte) (*RawValue, error) {
return NewMaskedRawValue([2][]byte{val, mask})
}
// MustNewRawValueWithMask is like NewRawValueWithMask but panics if the value
// and mask are of different lengths.
func MustNewRawValueWithMask(val []byte, mask []byte) *RawValue {
ret, err := NewRawValueWithMask(val, mask)
if err != nil {
panic(err)
}
return ret
}
// RawValue models a $raw-value-type-choice. These are vendor-defined values that
// are checked for consistency. Optionally, a mask specifies which bits within the
// value will be checked.
type RawValue struct {
Value IRawValueValue
}
// Type returns the type of the underlying value
func (o RawValue) Type() string {
return o.Value.Type()
}
// Bytes returns the bytes of the underlying value
func (o RawValue) Bytes() []byte {
return o.Value.Bytes()
}
// Mask returns the mask of the underlying value. If the underlying value is
// not masked, nil is returned.
func (o RawValue) Mask() []byte {
masked, ok := o.Value.(interface{ Mask() []byte })
if ok {
return masked.Mask()
}
return nil
}
// Equal returns true if both raw values are equal, or false if they are not or
// of one or more of RawValues is invalid. If both values specify masks and
// masks differ, false is returned (even if bytes masked with respective masks
// equal).
func (o RawValue) Equal(other *RawValue) bool {
ret, _ := maskedEqual(o.Bytes(), o.Mask(), other.Bytes(), other.Mask())
return ret
}
// CompareAgainstReference checks if a RawValue object matches with a reference
//
// (draft-ietf-rats-corim §9.4.6.1.4).
func (o RawValue) CompareAgainstReference(ref []byte, mask []byte) bool {
ret, _ := maskedEqual(o.Bytes(), o.Mask(), ref, mask)
return ret
}
func (o RawValue) MarshalCBOR() ([]byte, error) {
return em.Marshal(o.Value)
}
func (o *RawValue) UnmarshalCBOR(data []byte) error {
return dm.Unmarshal(data, &o.Value)
}
func (o RawValue) MarshalJSON() ([]byte, error) {
valueBytes, err := json.Marshal(o.Value)
if err != nil {
return nil, err
}
value := encoding.TypeAndValue{
Type: o.Value.Type(),
Value: valueBytes,
}
return json.Marshal(value)
}
func (o *RawValue) UnmarshalJSON(data []byte) error {
var value encoding.TypeAndValue
if err := json.Unmarshal(data, &value); err != nil {
return err
}
decoded, err := NewRawValue(nil, value.Type)
if err != nil {
return err
}
if err := json.Unmarshal(value.Value, &decoded.Value); err != nil {
return err
}
o.Value = decoded.Value
return nil
}
// NewBytesRawValue creates a new RawValue with the underlying value being
// TaggedBytes constructed from the provided input.
func NewBytesRawValue(val any) (*RawValue, error) {
if val == nil {
return &RawValue{&TaggedBytes{}}, nil
}
switch t := val.(type) {
case []byte:
return &RawValue{(*TaggedBytes)(&t)}, nil
case TaggedBytes:
return &RawValue{&t}, nil
case *TaggedBytes:
return &RawValue{t}, nil
default:
return nil, fmt.Errorf("value must be a byte slice; found %T", t)
}
}
// TaggedMaskedRawValue is a RawValue type that combines a value with a mask.
// The mask must be of the same length as the value. When this is being
// compared with an unmasked RawValue, only the masked bits of both underlying
// values are compared.
type TaggedMaskedRawValue struct {
_ struct{} `cbor:",toarray"`
Value []byte `json:"value"`
MaskBytes []byte `json:"mask"`
}
func (o TaggedMaskedRawValue) Type() string {
return MaskedType
}
func (o TaggedMaskedRawValue) Bytes() []byte {
return o.Value
}
func (o TaggedMaskedRawValue) Mask() []byte {
return o.MaskBytes
}
func (o TaggedMaskedRawValue) String() string {
return string(o.Value)
}
func (o TaggedMaskedRawValue) Valid() error {
if len(o.Value) == 0 {
return errors.New("value not set")
}
if len(o.MaskBytes) != len(o.Value) {
return errors.New("mask and value lengths differ")
}
return nil
}
// NewMaskedRawValue returns a new RawValue with the underlying value and mask
// constructed from the provided input. If the input is a [2][]byte or a
// [][]byte of length 2, then the first element is used as the value, and the
// second element as the mask. If the input is a []byte, then that is used as
// the value and the mask is a []byte of the same length with all bits set.
func NewMaskedRawValue(val any) (*RawValue, error) {
if val == nil {
return &RawValue{&TaggedMaskedRawValue{}}, nil
}
switch t := val.(type) {
case []byte:
return &RawValue{&TaggedMaskedRawValue{Value: t, MaskBytes: allSet(len(t))}}, nil
case TaggedBytes:
return &RawValue{&TaggedMaskedRawValue{Value: t, MaskBytes: allSet(len(t))}}, nil
case *TaggedBytes:
return &RawValue{&TaggedMaskedRawValue{Value: *t, MaskBytes: allSet(len(*t))}}, nil
case [2][]byte:
return &RawValue{&TaggedMaskedRawValue{Value: t[0], MaskBytes: t[1]}}, nil
case [][]byte:
if len(t) != 2 {
return nil, errors.New("[][]byte must contain exactly two elements")
}
return &RawValue{&TaggedMaskedRawValue{Value: t[0], MaskBytes: t[1]}}, nil
default:
return nil, fmt.Errorf("value must be a byte slice; found %T", t)
}
}
// MustNewMaskedRawValue is like NewMaskedRawValue but panics on error.
func MustNewMaskedRawValue(val any) *RawValue {
ret, err := NewMaskedRawValue(val)
if err != nil {
panic(err)
}
return ret
}
// IRawValueFactory defines the signature for the factory functions that may be
// registered using RegisterRawValueType to provide a new implementation of the
// corresponding type choice. The factory function should create a new
// *RawValue 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 the implemented type.
// See also https://go.dev/ref/spec#The_zero_value
type IRawValueFactory func(any) (*RawValue, error)
var rawValueValueRegister = map[string]IRawValueFactory{
BytesType: NewBytesRawValue,
MaskedType: NewMaskedRawValue,
}
// RegisterRawValueType registers a new IRawValueValue implementation
// (created by the provided IRawValueFactory) under the specified type name
// and CBOR tag.
func RegisterRawValueType(tag uint64, factory IRawValueFactory) error {
nilVal, err := factory(nil)
if err != nil {
return err
}
typ := nilVal.Type()
if _, exists := rawValueValueRegister[typ]; exists {
return fmt.Errorf("raw value type with name %q already exists", typ)
}
if err := registerCOMIDTag(tag, nilVal.Value); err != nil {
return err
}
rawValueValueRegister[typ] = factory
return nil
}
func allSet(n int) []byte {
ret := make([]byte, n)
for i := range ret {
ret[i] = 0xff
}
return ret
}
func maskedEqual(lhs []byte, lhsMask []byte, rhs []byte, rhsMask []byte) (bool, error) {
if lhsMask != nil && rhsMask != nil && !bytes.Equal(lhsMask, rhsMask) {
return false, errors.New("LHS and RHS masks are both set and are unequal")
}
if lhsMask != nil && len(lhsMask) != len(lhs) {
return false, errors.New("LHS mask and value lengths differ")
}
if rhsMask != nil && len(rhsMask) != len(rhs) {
return false, errors.New("RHS mask and value lengths differ")
}
if len(lhs) != len(rhs) {
return false, nil
}
mask := lhsMask
if rhsMask != nil {
mask = rhsMask
}
if mask != nil {
lhs = applyMask(lhs, mask)
rhs = applyMask(rhs, mask)
}
return bytes.Equal(lhs, rhs), nil
}
func applyMask(val, mask []byte) []byte {
ret := make([]byte, len(val))
for i, b := range val {
ret[i] = b & mask[i]
}
return ret
}