-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaims_p2_test.go
311 lines (232 loc) · 8.98 KB
/
claims_p2_test.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
// Copyright 2021-2022 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package psatoken
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func mustBuildValidP2Claims(t *testing.T, includeOptional bool) IClaims {
c := newP2Claims()
err := c.SetClientID(testClientIDSPE)
require.NoError(t, err)
err = c.SetSecurityLifeCycle(testSecurityLifecycleSecured)
require.NoError(t, err)
err = c.SetImplID(testImplementationID)
require.NoError(t, err)
err = c.SetNonce(testNonce)
require.NoError(t, err)
err = c.SetInstID(testInstID)
require.NoError(t, err)
err = c.SetSoftwareComponents(testSoftwareComponents)
require.NoError(t, err)
if includeOptional {
err = c.SetBootSeed(testBootSeedMin)
require.NoError(t, err)
err = c.SetCertificationReference(testCertificationReferenceP2)
require.NoError(t, err)
err = c.SetVSI(testVSI)
require.NoError(t, err)
}
return c
}
func Test_NewP2Claims_ok(t *testing.T) {
c := newP2Claims()
expected := Profile2Name
actual, err := c.GetProfile()
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
func Test_P2Claims_Validate_all_claims(t *testing.T) {
c := mustBuildValidP2Claims(t, true)
err := c.Validate()
assert.NoError(t, err)
}
func Test_P2Claims_Validate_mandatory_only_claims(t *testing.T) {
c := mustBuildValidP2Claims(t, false)
err := c.Validate()
assert.NoError(t, err)
}
func Test_P2Claims_ToCBOR_invalid(t *testing.T) {
c := newP2Claims()
expectedErr := `validating security lifecycle: missing mandatory claim`
_, err := ValidateAndEncodeClaimsToCBOR(c)
assert.EqualError(t, err, expectedErr)
}
func Test_P2Claims_ToCBOR_all_claims(t *testing.T) {
c := mustBuildValidP2Claims(t, true)
expected := mustHexDecode(t, testEncodedP2ClaimsAll)
actual, err := ValidateAndEncodeClaimsToCBOR(c)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
func Test_P2Claims_ToCBOR_mandatory_only_claims(t *testing.T) {
c := mustBuildValidP2Claims(t, false)
expected := mustHexDecode(t, testEncodedP2ClaimsMandatoryOnly)
actual, err := ValidateAndEncodeClaimsToCBOR(c)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
func Test_P2Claims_FromCBOR_bad_input(t *testing.T) {
buf := mustHexDecode(t, testNotCBOR)
expectedErr := "unexpected EOF"
_, err := DecodeAndValidateClaimsFromCBOR(buf)
assert.EqualError(t, err, expectedErr)
}
func Test_P2Claims_FromCBOR_missing_mandatory_claim(t *testing.T) {
buf := mustHexDecode(t, testEncodedP2ClaimsMissingMandatoryNonce)
expectedErr := "validating nonce: missing mandatory claim"
_, err := DecodeAndValidateClaimsFromCBOR(buf)
assert.EqualError(t, err, expectedErr)
}
func Test_P2Claims_FromCBOR_invalid_multi_nonce(t *testing.T) {
buf := mustHexDecode(t, testEncodedP2ClaimsInvalidMultiNonce)
expectedErr := "validating nonce: wrong syntax: got 2 nonces, want 1"
_, err := DecodeAndValidateClaimsFromCBOR(buf)
assert.EqualError(t, err, expectedErr)
}
func Test_P2Claims_FromCBOR_ok_mandatory_only(t *testing.T) {
buf := mustHexDecode(t, testEncodedP2ClaimsMandatoryOnly)
c, err := DecodeAndValidateClaimsFromCBOR(buf)
assert.NoError(t, err)
// mandatory
expectedProfile := Profile2Name
actualProfile, err := c.GetProfile()
assert.NoError(t, err)
assert.Equal(t, expectedProfile, actualProfile)
expectedClientID := testClientIDSPE
actualClientID, err := c.GetClientID()
assert.NoError(t, err)
assert.Equal(t, expectedClientID, actualClientID)
expectedSecurityLifeCycle := testSecurityLifecycleSecured
actualSecurityLifeCycle, err := c.GetSecurityLifeCycle()
assert.NoError(t, err)
assert.Equal(t, expectedSecurityLifeCycle, actualSecurityLifeCycle)
expectedImplID := testImplementationID
actualImplID, err := c.GetImplID()
assert.NoError(t, err)
assert.Equal(t, expectedImplID, actualImplID)
expectedNonce := testNonce
actualNonce, err := c.GetNonce()
assert.NoError(t, err)
assert.Equal(t, expectedNonce, actualNonce)
expectedInstID := testInstID
actualInstID, err := c.GetInstID()
assert.NoError(t, err)
assert.Equal(t, expectedInstID, actualInstID)
expectedSwComp := testSoftwareComponents
actualSwComp, err := c.GetSoftwareComponents()
assert.NoError(t, err)
assert.Equal(t, expectedSwComp, actualSwComp)
// optional (missing)
// note that boot-seed is optional in P2
expectedError := ErrOptionalClaimMissing
_, err = c.GetVSI()
assert.Equal(t, err, expectedError)
expectedError = ErrOptionalClaimMissing
_, err = c.GetBootSeed()
assert.Equal(t, err, expectedError)
expectedError = ErrOptionalClaimMissing
_, err = c.GetCertificationReference()
assert.Equal(t, err, expectedError)
}
func Test_P2Claims_Validate_positives(t *testing.T) {
validatePositives(t, Profile2Name)
}
func Test_P2Claims_Validate_negatives(t *testing.T) {
validateNegatives(t, Profile2Name)
}
func Test_P2Claims_SetSecurityLifecycle_invalid(t *testing.T) {
c := newP2Claims()
expectedErr := `wrong syntax: value 65535 is invalid`
err := c.SetSecurityLifeCycle(0xffff)
assert.EqualError(t, err, expectedErr)
}
func Test_P2Claims_FromJSON_positives(t *testing.T) {
tvs := []string{
/* 0 */ "testvectors/json/test-token-valid-full.json",
/* 1 */ "testvectors/json/test-token-valid-minimalist-p2.json",
/* 2 */ "testvectors/json/test-boot-seed-invalid-missing.json", // missing boot seed ok for P2
}
for i, fn := range tvs {
buf, err := os.ReadFile(fn)
require.NoError(t, err)
claims := newP2Claims()
err = json.Unmarshal(buf, claims)
require.NoError(t, err)
err = claims.Validate()
assert.NoError(t, err, "test vector %d failed", i)
}
}
func Test_P2Claims_FromJSON_negatives(t *testing.T) {
tvs := []string{
/* 0 */ "testvectors/json/test-boot-seed-invalid-long.json",
/* 1 */ "testvectors/json/test-token-valid-minimalist-no-sw-measurements.json",
/* 2 */ "testvectors/json/test-boot-seed-invalid-short.json",
/* 3 */ "testvectors/json/test-client-id-invalid-missing.json",
/* 4 */ "testvectors/json/test-hardware-version-invalid.json",
/* 5 */ "testvectors/json/test-implementation-id-invalid-long.json",
/* 6 */ "testvectors/json/test-implementation-id-invalid-missing.json",
/* 7 */ "testvectors/json/test-implementation-id-invalid-short.json",
/* 8 */ "testvectors/json/test-instance-id-invalid-euid-type.json",
/* 9 */ "testvectors/json/test-instance-id-invalid-long.json",
/* 10 */ "testvectors/json/test-instance-id-invalid-missing.json",
/* 11 */ "testvectors/json/test-instance-id-invalid-short.json",
/* 12 */ "testvectors/json/test-nonce-invalid-long.json",
/* 13 */ "testvectors/json/test-nonce-invalid-missing.json",
/* 14 */ "testvectors/json/test-nonce-invalid-short.json",
/* 15 */ "testvectors/json/test-profile-invalid-unknown.json",
/* 16 */ "testvectors/json/test-security-lifecycle-invalid-missing.json",
/* 17 */ "testvectors/json/test-security-lifecycle-invalid-state.json",
/* 18 */ "testvectors/json/test-sw-component-and-no-sw-measurements-missing.json",
/* 19 */ "testvectors/json/test-sw-component-measurement-value-invalid-missing.json",
/* 20 */ "testvectors/json/test-sw-component-measurement-value-invalid-short.json",
/* 21 */ "testvectors/json/test-sw-component-signer-id-invalid-missing.json",
/* 22 */ "testvectors/json/test-sw-component-signer-id-invalid-short.json",
/* 23 */ "testvectors/json/test-sw-components-empty.json",
/* 24 */ "testvectors/json/test-sw-components-invalid-combo.json",
/* 25 */ "testvectors/json/test-sw-components-invalid-missing.json",
/* 26 */ "testvectors/json/test-vsi-invalid-empty.json",
/* 27 */ "testvectors/json/test-profile-invalid-missing.json", // missing profile defaults to P1
/* 28 */ "testvectors/json/test-profile-valid-missing.json",
}
for i, fn := range tvs {
buf, err := os.ReadFile(fn)
require.NoError(t, err)
claims := newP2Claims()
err = json.Unmarshal(buf, claims)
require.NoError(t, err)
err = claims.Validate()
assert.Error(t, err, "test vector %d failed", i)
}
}
func TestP2Claims_FromJSON_invalid_json(t *testing.T) {
expectedErr := `unexpected end of JSON input`
_, err := DecodeAndValidateClaimsFromJSON(testNotJSON)
assert.EqualError(t, err, expectedErr)
}
func Test_P2Claims_ToJSON_ok(t *testing.T) {
c := mustBuildValidP2Claims(t, true)
expected := `{
"eat-profile": "http://arm.com/psa/2.0.0",
"psa-client-id": 2147483647,
"psa-security-lifecycle": 12288,
"psa-implementation-id": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
"psa-boot-seed": "AAAAAAAAAAA=",
"psa-certification-reference": "1234567890123-12345",
"psa-software-components": [
{
"measurement-value": "AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM=",
"signer-id": "BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ="
}
],
"psa-nonce": "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=",
"psa-instance-id": "AQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC",
"psa-verification-service-indicator": "https://veraison.example/v1/challenge-response"
}`
actual, err := ValidateAndEncodeClaimsToJSON(c)
assert.NoError(t, err)
assert.JSONEq(t, expected, string(actual))
}