-
Notifications
You must be signed in to change notification settings - Fork 2
/
ear_test.go
395 lines (342 loc) · 13.1 KB
/
ear_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
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
// Copyright 2022 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package ear
import (
"fmt"
"testing"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testECDSAPublicKey = `{
"kty": "EC",
"crv": "P-256",
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4"
}`
testECDSAPrivateKey = `{
"kty": "EC",
"crv": "P-256",
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
"d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM"
}`
testVidBuild = "rrtrap-v1.0.0"
testVidDeveloper = "Acme Inc."
testStatus = TrustTierAffirming
testIAT = int64(1666091373)
testPolicyID = "policy://test/01234"
testVerifierID = VerifierIdentity{
Build: &testVidBuild,
Developer: &testVidDeveloper,
}
testProfile = EatProfile
testUnsupportedProfile = "1.2.3.4.5"
testNonce = "0123456789abcdef"
testBadNonce = "1337"
testEvidenceID = "405e0c3127e455ebc22361210b43ca9499ca80d3f6b1dc79b89fa35290cee3d9"
testEvidence = []byte("evidence")
testTeeName = "aws-nitro"
testAttestationResultsWithVeraisonExtns = AttestationResult{
IssuedAt: &testIAT,
VerifierID: &testVerifierID,
Profile: &testProfile,
Submods: map[string]*Appraisal{
"test": {
Status: &testStatus,
AppraisalPolicyID: &testPolicyID,
AppraisalExtensions: AppraisalExtensions{
VeraisonPolicyClaims: &map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
VeraisonAnnotatedEvidence: &map[string]interface{}{
"k1": "v1",
"k2": "v2",
},
VeraisonKeyAttestation: &map[string]interface{}{
"akpub": "YWtwdWIK",
},
},
},
},
}
)
func TestToJSON_fail(t *testing.T) {
testTrustTier := TrustTierAffirming
tvs := []struct {
ar AttestationResult
expected string
}{
{
ar: AttestationResult{},
expected: `missing mandatory 'eat_profile', 'iat', 'verifier-id', 'submods' (at least one appraisal must be present)`,
},
{
ar: AttestationResult{
Submods: map[string]*Appraisal{},
},
expected: `missing mandatory 'eat_profile', 'iat', 'verifier-id', 'submods' (at least one appraisal must be present)`,
},
{
ar: AttestationResult{
IssuedAt: &testIAT,
Submods: map[string]*Appraisal{
"test": {},
},
},
expected: `missing mandatory 'eat_profile', 'verifier-id'; invalid value(s) for submods[test]: missing mandatory 'ear.status'`,
},
{
ar: AttestationResult{
Profile: &testProfile,
Submods: map[string]*Appraisal{
"test": {Status: &testTrustTier},
},
},
expected: `missing mandatory 'iat', 'verifier-id'`,
},
{
ar: AttestationResult{
Profile: &testUnsupportedProfile,
Submods: map[string]*Appraisal{
"test": {Status: &testTrustTier},
},
},
expected: `missing mandatory 'iat', 'verifier-id'; invalid value(s) for eat_profile (1.2.3.4.5)`,
},
{
ar: AttestationResult{
IssuedAt: &testIAT,
Profile: &testProfile,
VerifierID: &testVerifierID,
Nonce: &testBadNonce,
Submods: map[string]*Appraisal{
"test": {Status: &testTrustTier},
},
},
expected: `invalid value(s) for eat_nonce (4 bytes)`,
},
}
for i, tv := range tvs {
_, err := tv.ar.MarshalJSON()
assert.EqualError(t, err, tv.expected, "failed test vector at index %d", i)
}
}
func TestUnmarshalJSON_fail(t *testing.T) {
tvs := []struct {
ar string
expected string
}{
{
ar: `{`,
expected: `unexpected end of JSON input`,
},
{
ar: `[]`,
expected: `json: cannot unmarshal array into Go value of type map[string]interface {}`,
},
{
ar: `{}`,
expected: `missing mandatory 'eat_profile', 'ear.verifier-id', 'iat', 'submods'`,
},
}
for i, tv := range tvs {
var ar AttestationResult
err := ar.UnmarshalJSON([]byte(tv.ar))
assert.EqualError(t, err, tv.expected, "failed test vector at index %d", i)
}
}
func TestVerify_pass(t *testing.T) {
tvs := []struct {
token string
expected AttestationResult
}{
{
token: `eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJlYXIudmVyaWZpZXItaWQiOnsiYnVpbGQiOiJycnRyYXAtdjEuMC4wIiwiZGV2ZWxvcGVyIjoiQWNtZSBJbmMuIn0sImVhdF9wcm9maWxlIjoidGFnOmdpdGh1Yi5jb20sMjAyMzp2ZXJhaXNvbi9lYXIiLCJpYXQiOjE2NjYwOTEzNzMsInN1Ym1vZHMiOnsidGVzdCI6eyJlYXIuYXBwcmFpc2FsLXBvbGljeS1pZCI6InBvbGljeTovL3Rlc3QvMDEyMzQiLCJlYXIuc3RhdHVzIjoiYWZmaXJtaW5nIiwiZWFyLnZlcmFpc29uLmFubm90YXRlZC1ldmlkZW5jZSI6eyJrMSI6InYxIiwiazIiOiJ2MiJ9LCJlYXIudmVyYWlzb24ua2V5LWF0dGVzdGF0aW9uIjp7ImFrcHViIjoiWVd0d2RXSUsifSwiZWFyLnZlcmFpc29uLnBvbGljeS1jbGFpbXMiOnsiYmFyIjoiYmF6IiwiZm9vIjoiYmFyIn19fX0.gTuJrH5Ctf6sAXlaFu1NvHAtI4H0iSqsp2ZtxPPhSfZJBkyeWmZi62lTBw644JDRI0DY9X7Wk7CBWWE6dmBVAA`,
expected: testAttestationResultsWithVeraisonExtns,
},
}
k, err := jwk.ParseKey([]byte(testECDSAPublicKey))
require.NoError(t, err)
for i, tv := range tvs {
var ar AttestationResult
err := ar.Verify([]byte(tv.token), jwa.ES256, k)
assert.NoError(t, err, "failed test vector at index %d", i)
assert.Equal(t, tv.expected, ar)
}
}
func TestVerify_fail(t *testing.T) {
tvs := []struct {
token string
expected string
}{
{
// non-matching alg (HS256)
token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdGF0dXMiOiJhZmZpcm1pbmciLCJ0aW1lc3RhbXAiOiIyMDIyLTA5LTI2VDE3OjI5OjAwWiIsImFwcHJhaXNhbC1wb2xpY3ktaWQiOiJodHRwczovL3ZlcmFpc29uLmV4YW1wbGUvcG9saWN5LzEvNjBhMDA2OGQiLCJ2ZXJhaXNvbi5wcm9jZXNzZWQtZXZpZGVuY2UiOnsiazEiOiJ2MSIsImsyIjoidjIifSwidmVyYWlzb24udmVyaWZpZXItYWRkZWQtY2xhaW1zIjp7ImJhciI6ImJheiIsImZvbyI6ImJhciJ9fQ.Dv3PqGA2W8anXne0YZs8cvIhQhNF1Su1RS83RPzDVg4OhJFNN1oSF-loDpjfIwPdzCWt0eA6JYxSMqpGiemq-Q`,
expected: `failed verifying JWT message: could not verify message using any of the signatures or keys`,
},
{
// alg "none"
token: `eyJhbGciOiJub25lIn0.eyJzdGF0dXMiOiJhZmZpcm1pbmciLCJ0aW1lc3RhbXAiOiIyMDIyLTA5LTI2VDE3OjI5OjAwWiIsImFwcHJhaXNhbC1wb2xpY3ktaWQiOiJodHRwczovL3ZlcmFpc29uLmV4YW1wbGUvcG9saWN5LzEvNjBhMDA2OGQiLCJ2ZXJhaXNvbi5wcm9jZXNzZWQtZXZpZGVuY2UiOnsiazEiOiJ2MSIsImsyIjoidjIifSwidmVyYWlzb24udmVyaWZpZXItYWRkZWQtY2xhaW1zIjp7ImJhciI6ImJheiIsImZvbyI6ImJhciJ9fQ.`,
expected: `failed verifying JWT message: could not verify message using any of the signatures or keys`,
},
{
// bad JWT formatting
token: `.eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdGF0dXMiOiJhZmZpcm1pbmciLCJ0aW1lc3RhbXAiOiIyMDIyLTA5LTI2VDE3OjI5OjAwWiIsImFwcHJhaXNhbC1wb2xpY3ktaWQiOiJodHRwczovL3ZlcmFpc29uLmV4YW1wbGUvcG9saWN5LzEvNjBhMDA2OGQiLCJ2ZXJhaXNvbi5wcm9jZXNzZWQtZXZpZGVuY2UiOnsiazEiOiJ2MSIsImsyIjoidjIifSwidmVyYWlzb24udmVyaWZpZXItYWRkZWQtY2xhaW1zIjp7ImJhciI6ImJheiIsImZvbyI6ImJhciJ9fQ.Dv3PqGA2W8anXne0YZs8cvIhQhNF1Su1RS83RPzDVg4OhJFNN1oSF-loDpjfIwPdzCWt0eA6JYxSMqpGiemq-Q`,
expected: `failed verifying JWT message: failed to parse jws: failed to parse JOSE headers: EOF`,
},
{
// empty attestation results
token: `eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.e30.9Tvx3hVBNfkmVXTndrVfv9ZeNJgX59w0JpR2vyjUn8lGxL8VT7OggUeYSYFnxrouSi2TusNh61z8rLdOqxGA-A`,
expected: `missing mandatory 'eat_profile', 'ear.verifier-id', 'submods'`,
},
{
// empty attestation results
token: `eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJlYXIudmVyaWZpZXItaWQiOnsiYnVpbGQiOiJycnRyYXAtdjEuMC4wIiwiZGV2ZWxvcGVyIjoiQWNtZSBJbmMuIn0sImVhdF9wcm9maWxlIjoidGFnOmdpdGh1Yi5jb20sMjAyMzp2ZXJhaXNvbi9lYXIiLCJpYXQiOjEuNjY2MDkxMzczZSswOSwianRpIjoiMTY1Zjg0YzY3YzE0ZTEwZDFlNGI0MzM0MDgzY2IyYTIxZDI2YmYxN2FhNjBkNWU5ZGE5ZDhmNzE3NmFjNWI2MyIsIm5iZiI6MTY3NjQ3MjcwOSwic3VibW9kcyI6eyJ0ZXN0Ijp7ImVhci5hcHByYWlzYWwtcG9saWN5LWlkIjoicG9saWN5Oi8vdGVzdC8wMTIzNCIsImVhci5zdGF0dXMiOiJhZmZpcm1pbmciLCJlYXIudmVyYWlzb24uYW5ub3RhdGVkLWV2aWRlbmNlIjp7ImsxIjoidjEiLCJrMiI6InYyIn0sImVhci52ZXJhaXNvbi5wb2xpY3ktY2xhaW1zIjp7ImJhciI6ImJheiIsImZvbyI6ImJhciJ9fX19.LunlKAnUiVHZxIUr7jNnrwFlRtd7t6f6W1rzIFgcWFLdtJELKIVGkPVV5PriHh8T0uLLIEJafwvi6hmIr27aDw.trailing-rubbish`,
expected: `failed to parse token: invalid character 'e' looking for beginning of value`,
},
}
k, err := jwk.ParseKey([]byte(testECDSAPublicKey))
require.NoError(t, err)
for i, tv := range tvs {
var ar AttestationResult
err := ar.Verify([]byte(tv.token), jwa.ES256, k)
assert.ErrorContains(t, err, tv.expected, "failed test vector at index %d", i)
}
}
func TestSign_fail(t *testing.T) {
sigK, err := jwk.ParseKey([]byte(testECDSAPrivateKey))
require.NoError(t, err)
// an empty AR is not a valid AR4SI payload
var ar AttestationResult
_, err = ar.Sign(jwa.ES256, sigK)
assert.EqualError(t, err, `missing mandatory 'eat_profile', 'iat', 'verifier-id', 'submods' (at least one appraisal must be present)`)
}
func TestRoundTrip_pass(t *testing.T) {
sigK, err := jwk.ParseKey([]byte(testECDSAPrivateKey))
require.NoError(t, err)
token, err := testAttestationResultsWithVeraisonExtns.Sign(jwa.ES256, sigK)
assert.NoError(t, err)
fmt.Println(string(token))
vfyK, err := jwk.ParseKey([]byte(testECDSAPublicKey))
require.NoError(t, err)
var actual AttestationResult
err = actual.Verify(token, jwa.ES256, vfyK)
assert.NoError(t, err)
assert.Equal(t, testAttestationResultsWithVeraisonExtns, actual)
}
func TestRoundTrip_tampering(t *testing.T) {
sigK, err := jwk.ParseKey([]byte(testECDSAPrivateKey))
require.NoError(t, err)
token, err := testAttestationResultsWithVeraisonExtns.Sign(jwa.ES256, sigK)
assert.NoError(t, err)
vfyK, err := jwk.ParseKey([]byte(testECDSAPublicKey))
require.NoError(t, err)
var actual AttestationResult
// Tamper with the signature.
// Note that since ES256 is randomized, this could result in different kinds
// of verification errors. Therefore we have to use ErrorContains rather
// than EqualError.
token[len(token)-1] ^= 1
err = actual.Verify(token, jwa.ES256, vfyK)
assert.ErrorContains(t, err, "failed verifying JWT message")
}
func TestUpdateStatusFromTrustVector(t *testing.T) {
ar := NewAttestationResult("test", "test", "test")
ar.UpdateStatusFromTrustVector()
assert.Equal(t, TrustTierNone, *ar.Submods["test"].Status)
ar.Submods["test"].TrustVector.Configuration = ApprovedConfigClaim
ar.UpdateStatusFromTrustVector()
assert.Equal(t, TrustTierAffirming, *ar.Submods["test"].Status)
*ar.Submods["test"].Status = TrustTierWarning
ar.UpdateStatusFromTrustVector()
assert.Equal(t, TrustTierWarning, *ar.Submods["test"].Status)
ar.Submods["test"].TrustVector.Configuration = UnsupportableConfigClaim
ar.UpdateStatusFromTrustVector()
assert.Equal(t, TrustTierContraindicated, *ar.Submods["test"].Status)
}
func TestAsMap(t *testing.T) {
policyID := "foo"
ar := NewAttestationResult("someScheme", "test", "test")
status := NewTrustTier(TrustTierAffirming)
ar.Submods["someScheme"].Status = status
ar.Submods["someScheme"].TrustVector.Executables = ApprovedRuntimeClaim
ar.Submods["someScheme"].AppraisalPolicyID = &policyID
ar.Nonce = &testNonce
expected := map[string]interface{}{
"submods": map[string]interface{}{
"someScheme": map[string]interface{}{
"ear.status": *status,
"ear.trustworthiness-vector": map[string]interface{}{
"instance-identity": NoClaim,
"configuration": NoClaim,
"executables": ApprovedRuntimeClaim,
"file-system": NoClaim,
"hardware": NoClaim,
"runtime-opaque": NoClaim,
"storage-opaque": NoClaim,
"sourced-data": NoClaim,
},
"ear.appraisal-policy-id": "foo",
},
},
"eat_profile": EatProfile,
"eat_nonce": testNonce,
}
m := ar.AsMap()
for _, field := range []string{
"submods",
"eat_profile",
"ear.appraisal-policy-id",
} {
assert.Equal(t, expected[field], m[field])
}
}
func Test_populateFromMap(t *testing.T) {
var ar AttestationResult
m := map[string]interface{}{
"submods": map[string]interface{}{
"test": map[string]interface{}{
"ear.status": 2,
"ear.trustworthiness-vector": map[string]interface{}{
"instance-identity": 0,
"configuration": 0,
"executables": 2,
"file-system": 0,
"hardware": 0,
"runtime-opaque": 0,
"storage-opaque": 0,
"sourced-data": 0,
},
"ear.appraisal-policy-id": "foo",
},
},
"ear.raw-evidence": "SSBkaWRuJ3QgZG8gaXQ",
"iat": 1234,
"eat_profile": EatProfile,
"ear.verifier-id": map[string]interface{}{
"build": "rrtrap-v1.0.0",
"developer": "Acme Inc.",
},
}
err := ar.populateFromMap(m)
assert.NoError(t, err)
assert.Equal(t, TrustTierAffirming, *ar.Submods["test"].Status)
assert.Equal(t, EatProfile, *ar.Profile)
}
func TestTrustTier_ColorString(t *testing.T) {
assert.Equal(t, "\\033[47mnone\\033[0m", TrustTierNone.ColorString())
assert.Equal(t, "\\033[42maffirming\\033[0m", TrustTierAffirming.ColorString())
assert.Equal(t, "\\033[43mwarning\\033[0m", TrustTierWarning.ColorString())
assert.Equal(t, "\\033[41mcontraindicated\\033[0m", TrustTierContraindicated.ColorString())
}
func TestNewAttestationResult(t *testing.T) {
ar := NewAttestationResult("test", "testBuild", "testDev")
_, err := ar.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, "testBuild", *ar.VerifierID.Build)
assert.Equal(t, "testDev", *ar.VerifierID.Developer)
}