forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriples.go
199 lines (162 loc) · 5.16 KB
/
triples.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"fmt"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
)
type Triples struct {
ReferenceValues *ValueTriples `cbor:"0,keyasint,omitempty" json:"reference-values,omitempty"`
EndorsedValues *ValueTriples `cbor:"1,keyasint,omitempty" json:"endorsed-values,omitempty"`
DevIdentityKeys *KeyTriples `cbor:"2,keyasint,omitempty" json:"dev-identity-keys,omitempty"`
AttestVerifKeys *KeyTriples `cbor:"3,keyasint,omitempty" json:"attester-verification-keys,omitempty"`
Extensions
}
// RegisterExtensions registers a struct as a collections of extensions
func (o *Triples) RegisterExtensions(exts extensions.Map) error {
refValExts := extensions.NewMap()
endValExts := extensions.NewMap()
for p, v := range exts {
switch p {
case ExtTriples:
o.Extensions.Register(v)
case ExtReferenceValue:
refValExts[ExtMval] = v
case ExtReferenceValueFlags:
refValExts[ExtFlags] = v
case ExtEndorsedValue:
endValExts[ExtMval] = v
case ExtEndorsedValueFlags:
endValExts[ExtFlags] = v
default:
return fmt.Errorf("%w: %q", extensions.ErrUnexpectedPoint, p)
}
}
if len(refValExts) != 0 {
if o.ReferenceValues == nil {
o.ReferenceValues = NewValueTriples()
}
if err := o.ReferenceValues.RegisterExtensions(refValExts); err != nil {
return err
}
}
if len(endValExts) != 0 {
if o.EndorsedValues == nil {
o.EndorsedValues = NewValueTriples()
}
if err := o.EndorsedValues.RegisterExtensions(refValExts); err != nil {
return err
}
}
return nil
}
// GetExtensions returns pervisouosly registered extension
func (o *Triples) GetExtensions() extensions.IMapValue {
return o.Extensions.IMapValue
}
// UnmarshalCBOR deserializes from CBOR
func (o *Triples) UnmarshalCBOR(data []byte) error {
return encoding.PopulateStructFromCBOR(dm, data, o)
}
// MarshalCBOR serializes to CBOR
func (o Triples) MarshalCBOR() ([]byte, error) {
// If extensions have been registered, the collection will exist, but
// might be empty. If that is the case, set it to nil to avoid
// marshaling an empty list (and let the marshaller omit the claim
// instead). Note that since the receiver was passed by value, we do not
// need to worry about saving the field's value before setting it to
// nil.
if o.ReferenceValues != nil && o.ReferenceValues.IsEmpty() {
o.ReferenceValues = nil
}
if o.EndorsedValues != nil && o.EndorsedValues.IsEmpty() {
o.EndorsedValues = nil
}
return encoding.SerializeStructToCBOR(em, o)
}
// UnmarshalJSON deserializes from JSON
func (o *Triples) UnmarshalJSON(data []byte) error {
return encoding.PopulateStructFromJSON(data, o)
}
// MarshalJSON serializes to JSON
func (o Triples) MarshalJSON() ([]byte, error) {
// If extensions have been registered, the collection will exist, but
// might be empty. If that is the case, set it to nil to avoid
// marshaling an empty list (and let the marshaller omit the claim
// instead). Note that since the receiver was passed by value, we do not
// need to worry about saving the field's value before setting it to
// nil.
if o.ReferenceValues != nil && o.ReferenceValues.IsEmpty() {
o.ReferenceValues = nil
}
if o.EndorsedValues != nil && o.EndorsedValues.IsEmpty() {
o.EndorsedValues = nil
}
return encoding.SerializeStructToJSON(o)
}
// Valid checks that the Triples is valid as per the specification
func (o Triples) Valid() error {
// non-empty<>
if (o.ReferenceValues == nil || o.ReferenceValues.IsEmpty()) &&
(o.EndorsedValues == nil || o.EndorsedValues.IsEmpty()) &&
(o.AttestVerifKeys == nil || len(*o.AttestVerifKeys) == 0) &&
(o.DevIdentityKeys == nil || len(*o.DevIdentityKeys) == 0) {
return fmt.Errorf("triples struct must not be empty")
}
if o.ReferenceValues != nil {
if err := o.ReferenceValues.Valid(); err != nil {
return fmt.Errorf("reference values: %w", err)
}
}
if o.EndorsedValues != nil {
if err := o.EndorsedValues.Valid(); err != nil {
return fmt.Errorf("endorsed values: %w", err)
}
}
if o.AttestVerifKeys != nil {
for i, ak := range *o.AttestVerifKeys {
if err := ak.Valid(); err != nil {
return fmt.Errorf("attestation verification key at index %d: %w", i, err)
}
}
}
if o.DevIdentityKeys != nil {
for i, dk := range *o.DevIdentityKeys {
if err := dk.Valid(); err != nil {
return fmt.Errorf("device identity key at index %d: %w", i, err)
}
}
}
return o.Extensions.validTriples(&o)
}
func (o *Triples) AddReferenceValue(val ValueTriple) *Triples {
if o != nil {
if o.ReferenceValues == nil {
o.ReferenceValues = new(ValueTriples)
}
o.ReferenceValues.Add(&val)
}
return o
}
func (o *Triples) AddEndorsedValue(val ValueTriple) *Triples {
if o != nil {
if o.EndorsedValues == nil {
o.EndorsedValues = new(ValueTriples)
}
o.EndorsedValues.Add(&val)
}
return o
}
func (o *Triples) AddAttestVerifKey(val KeyTriple) *Triples {
if o != nil {
*o.AttestVerifKeys = append(*o.AttestVerifKeys, val)
}
return o
}
func (o *Triples) AddDevIdentityKey(val KeyTriple) *Triples {
if o != nil {
*o.DevIdentityKeys = append(*o.DevIdentityKeys, val)
}
return o
}