-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaims_p2.go
331 lines (251 loc) · 7.49 KB
/
claims_p2.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package psatoken
import (
_ "crypto/sha256" // used hash algorithms need to be imported explicitly
"encoding/json"
"fmt"
"github.com/veraison/eat"
)
const Profile2Name = "http://arm.com/psa/2.0.0"
// Profile2 provides the IClaims implementation associated with http://arm.com/psa/2.0.0
type Profile2 struct{}
func (o Profile2) GetName() string {
return Profile2Name
}
func (o Profile2) GetClaims() IClaims {
return newP2Claims()
}
// P2Claims are associated with profile "http://arm.com/psa/2.0.0"
// See https://datatracker.ietf.org/doc/html/draft-tschofenig-rats-psa-token-13
type P2Claims struct {
Profile *eat.Profile `cbor:"265,keyasint" json:"eat-profile"`
ClientID *int32 `cbor:"2394,keyasint" json:"psa-client-id"`
SecurityLifeCycle *uint16 `cbor:"2395,keyasint" json:"psa-security-lifecycle"`
ImplID *[]byte `cbor:"2396,keyasint" json:"psa-implementation-id"`
BootSeed *[]byte `cbor:"2397,keyasint,omitempty" json:"psa-boot-seed,omitempty"`
CertificationReference *string `cbor:"2398,keyasint,omitempty" json:"psa-certification-reference,omitempty"`
SwComponents ISwComponents `cbor:"2399,keyasint" json:"psa-software-components"`
Nonce *eat.Nonce `cbor:"10,keyasint" json:"psa-nonce"`
InstID *eat.UEID `cbor:"256,keyasint" json:"psa-instance-id"`
VSI *string `cbor:"2400,keyasint,omitempty" json:"psa-verification-service-indicator,omitempty"`
// CanonicalProfile contains the "correct" profile name associated with
// this IClaims implementation (e.g. "http://arm.com/psa/2.0.0" for
// P2Claims). The reason this is a field rather than a global constant
// is so that derived profiles can embed this struct and rely on its
// existing validation methods.
CanonicalProfile string `cbor:"-" json:"-"`
}
func newP2Claims() IClaims {
p := eat.Profile{}
if err := p.Set(Profile2Name); err != nil {
// should never get here as using known good constant as input
panic(err)
}
return &P2Claims{
Profile: &p,
SwComponents: &SwComponents[*SwComponent]{},
CanonicalProfile: Profile2Name,
}
}
// Semantic validation
func (c P2Claims) Validate() error { //nolint:gocritic
return ValidateClaims(&c)
}
func (c *P2Claims) SetClientID(v int32) error {
// any int32 value is acceptable
c.ClientID = &v
return nil
}
func (c *P2Claims) SetSecurityLifeCycle(v uint16) error {
if err := ValidateSecurityLifeCycle(v); err != nil {
return err
}
c.SecurityLifeCycle = &v
return nil
}
func (c *P2Claims) SetImplID(v []byte) error {
if err := ValidateImplID(v); err != nil {
return err
}
c.ImplID = &v
return nil
}
func (c *P2Claims) SetBootSeed(v []byte) error {
l := len(v)
if l < 8 || l > 32 {
return fmt.Errorf(
"%w: invalid length %d (MUST be between 8 and 32 bytes)",
ErrWrongSyntax, l,
)
}
c.BootSeed = &v
return nil
}
func (c *P2Claims) SetCertificationReference(v string) error {
if !CertificationReferenceP1RE.MatchString(v) &&
!CertificationReferenceP2RE.MatchString(v) {
return fmt.Errorf(
"%w: MUST be in EAN-13 or EAN-13+5 format",
ErrWrongSyntax,
)
}
c.CertificationReference = &v
return nil
}
func (c *P2Claims) SetSoftwareComponents(scs []ISwComponent) error {
if c.SwComponents == nil {
c.SwComponents = &SwComponents[*SwComponent]{}
}
return c.SwComponents.Replace(scs)
}
func (c *P2Claims) SetNonce(v []byte) error {
if err := ValidatePSAHashType(v); err != nil {
return err
}
n := eat.Nonce{}
if err := n.Add(v); err != nil {
return err
}
c.Nonce = &n
return nil
}
func (c *P2Claims) SetInstID(v []byte) error {
if err := ValidateInstID(v); err != nil {
return err
}
ueid := eat.UEID(v)
c.InstID = &ueid
return nil
}
func (c *P2Claims) SetVSI(v string) error {
if err := ValidateVSI(v); err != nil {
return err
}
c.VSI = &v
return nil
}
// Codecs
// this type alias is used to prevent infinite recursion during marshaling.
type p2Claims P2Claims
func (c *P2Claims) UnmarshalCBOR(buf []byte) error {
c.Profile = nil // clear profile to make sure we take it from buf
return dm.Unmarshal(buf, (*p2Claims)(c))
}
func (c *P2Claims) UnmarshalJSON(buf []byte) error {
c.Profile = nil // clear profile to make sure we take it from buf
return json.Unmarshal(buf, (*p2Claims)(c))
}
// Getters return a validated value or an error
// After successful call to Validate(), getters of mandatory claims are assured
// to never fail. Getters of optional claim may still fail with
// ErrOptionalClaimMissing in case the claim is not present.
func (c P2Claims) GetProfile() (string, error) { //nolint:gocritic
if c.Profile == nil {
return "", ErrMandatoryClaimMissing
}
profileString, err := c.Profile.Get()
if err != nil {
return "", err
}
if profileString != c.CanonicalProfile {
return "", fmt.Errorf("%w: expecting %q, got %q",
ErrWrongProfile, c.CanonicalProfile, profileString)
}
return profileString, nil
}
func (c P2Claims) GetClientID() (int32, error) { //nolint:gocritic
if c.ClientID == nil {
return 0, ErrMandatoryClaimMissing
}
return *c.ClientID, nil
}
func (c P2Claims) GetSecurityLifeCycle() (uint16, error) { //nolint:gocritic
if c.SecurityLifeCycle == nil {
return 0, ErrMandatoryClaimMissing
}
if err := ValidateSecurityLifeCycle(*c.SecurityLifeCycle); err != nil {
return 0, err
}
return *c.SecurityLifeCycle, nil
}
func (c P2Claims) GetImplID() ([]byte, error) { //nolint:gocritic
if c.ImplID == nil {
return nil, ErrMandatoryClaimMissing
}
if err := ValidateImplID(*c.ImplID); err != nil {
return nil, err
}
return *c.ImplID, nil
}
func (c P2Claims) GetBootSeed() ([]byte, error) { //nolint:gocritic
if c.BootSeed == nil {
return nil, ErrOptionalClaimMissing
}
l := len(*c.BootSeed)
if l < 8 || l > 32 {
return nil, fmt.Errorf(
"%w: invalid length %d (MUST be between 8 and 32 bytes)",
ErrWrongSyntax, l,
)
}
return *c.BootSeed, nil
}
func (c P2Claims) GetCertificationReference() (string, error) { //nolint:gocritic
if c.CertificationReference == nil {
return "", ErrOptionalClaimMissing
}
if !CertificationReferenceP2RE.MatchString(*c.CertificationReference) {
return "", fmt.Errorf(
"%w: MUST be in EAN-13+5 format",
ErrWrongSyntax,
)
}
return *c.CertificationReference, nil
}
func (c P2Claims) GetSoftwareComponents() ([]ISwComponent, error) { //nolint:gocritic
if c.SwComponents == nil || c.SwComponents.IsEmpty() {
return nil, fmt.Errorf("%w (MUST have at least one sw component)",
ErrMandatoryClaimMissing)
}
return c.SwComponents.Values()
}
func (c P2Claims) GetNonce() ([]byte, error) { //nolint:gocritic
v := c.Nonce
if v == nil {
return nil, ErrMandatoryClaimMissing
}
l := v.Len()
if l != 1 {
return nil, fmt.Errorf("%w: got %d nonces, want 1", ErrWrongSyntax, l)
}
n := v.GetI(0)
if err := ValidateNonce(n); err != nil {
return nil, err
}
return n, nil
}
func (c P2Claims) GetInstID() ([]byte, error) { //nolint:gocritic
v := c.InstID
if v == nil {
return nil, ErrMandatoryClaimMissing
}
if err := ValidateInstID(*v); err != nil {
return nil, err
}
return *v, nil
}
func (c P2Claims) GetVSI() (string, error) { //nolint:gocritic
if c.VSI == nil {
return "", ErrOptionalClaimMissing
}
if err := ValidateVSI(*c.VSI); err != nil {
return "", err
}
return *c.VSI, nil
}
func init() {
if err := RegisterProfile(Profile2{}); err != nil {
panic(err)
}
}