-
Notifications
You must be signed in to change notification settings - Fork 10
/
cmds_hierarchy_test.go
546 lines (471 loc) · 20.5 KB
/
cmds_hierarchy_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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package tpm2_test
import (
"bytes"
"testing"
. "github.com/canonical/go-tpm2"
"github.com/canonical/go-tpm2/testutil"
)
func TestCreatePrimary(t *testing.T) {
tpm, _, closeTPM := testutil.NewTPMContextT(t, testutil.TPMFeatureOwnerHierarchy|testutil.TPMFeatureEndorsementHierarchy|testutil.TPMFeatureNV)
defer closeTPM()
run := func(t *testing.T, hierarchy ResourceContext, sensitive *SensitiveCreate, template *Public, outsideInfo Data, creationPCR PCRSelectionList, session SessionContext) (ResourceContext, *Public) {
objectContext, outPublic, creationData, creationHash, creationTicket, err := tpm.CreatePrimary(hierarchy, sensitive, template, outsideInfo, creationPCR, session)
if err != nil {
t.Fatalf("CreatePrimary failed: %v", err)
}
if _, ok := objectContext.(ObjectContext); !ok {
t.Errorf("CreatePrimary return an invalid resource type")
}
if objectContext.Handle().Type() != HandleTypeTransient {
t.Errorf("CreatePrimary returned an invalid handle 0x%08x", objectContext.Handle())
}
verifyPublicAgainstTemplate(t, outPublic, template)
verifyCreationData(t, tpm, creationData, creationHash, template, outsideInfo, creationPCR, hierarchy)
verifyCreationTicket(t, creationTicket, hierarchy)
nameAlgSize := template.NameAlg.Size()
if len(objectContext.Name()) != nameAlgSize+2 {
t.Errorf("CreatePrimary returned a name of the wrong length %d", len(objectContext.Name()))
}
return objectContext, outPublic
}
t.Run("RSASrk", func(t *testing.T) {
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
creationPCR := PCRSelectionList{
{Hash: HashAlgorithmSHA1, Select: []int{0, 1}},
{Hash: HashAlgorithmSHA256, Select: []int{7, 8}}}
objectContext, pub := run(t, tpm.OwnerHandleContext(), nil, &template, Data{}, creationPCR, nil)
defer flushContext(t, tpm, objectContext)
verifyRSAAgainstTemplate(t, pub, &template)
})
t.Run("ECCSrk", func(t *testing.T) {
template := Public{
Type: ObjectTypeECC,
NameAlg: HashAlgorithmSHA1,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt,
Params: &PublicParamsU{
ECCDetail: &ECCParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: ECCScheme{Scheme: ECCSchemeNull},
CurveID: ECCCurveNIST_P256,
KDF: KDFScheme{Scheme: KDFAlgorithmNull}}}}
creationPCR := PCRSelectionList{
{Hash: HashAlgorithmSHA1, Select: []int{0, 1}},
{Hash: HashAlgorithmSHA256, Select: []int{7, 8}}}
objectContext, pub := run(t, tpm.OwnerHandleContext(), nil, &template, Data{}, creationPCR, nil)
defer flushContext(t, tpm, objectContext)
if len(pub.Unique.ECC.X) != 32 || len(pub.Unique.ECC.Y) != 32 {
t.Errorf("CreatePrimary returned object with invalid ECC coords")
}
})
t.Run("Ek", func(t *testing.T) {
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrAdminWithPolicy | AttrRestricted | AttrDecrypt,
AuthPolicy: []byte{0x83, 0x71, 0x97, 0x67, 0x44, 0x84, 0xb3, 0xf8, 0x1a, 0x90, 0xcc, 0x8d, 0x46, 0xa5, 0xd7, 0x24, 0xfd, 0x52,
0xd7, 0x6e, 0x06, 0x52, 0x0b, 0x64, 0xf2, 0xa1, 0xda, 0x1b, 0x33, 0x14, 0x69, 0xaa},
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
objectContext, pub := run(t, tpm.EndorsementHandleContext(), nil, &template, Data{}, PCRSelectionList{}, nil)
defer flushContext(t, tpm, objectContext)
verifyRSAAgainstTemplate(t, pub, &template)
})
t.Run("CreateWithAuthValue", func(t *testing.T) {
sensitive := SensitiveCreate{UserAuth: testAuth}
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt | AttrNoDA,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
creationPCR := PCRSelectionList{
{Hash: HashAlgorithmSHA1, Select: []int{0, 1}},
{Hash: HashAlgorithmSHA256, Select: []int{7, 8}}}
objectContext, pub := run(t, tpm.OwnerHandleContext(), &sensitive, &template, Data{}, creationPCR, nil)
defer flushContext(t, tpm, objectContext)
verifyRSAAgainstTemplate(t, pub, &template)
childTemplate := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrDecrypt | AttrSign,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{Algorithm: SymObjectAlgorithmNull},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
// We shouldn't need to call ResourceContext.SetAuthValue to use the primary object.
_, _, _, _, _, err := tpm.Create(objectContext, nil, &childTemplate, nil, nil, nil)
if err != nil {
t.Errorf("Use of authorization on primary key failed: %v", err)
}
// Verify that the primary object was created with the right auth value
objectContext.SetAuthValue(testAuth)
_, _, _, _, _, err = tpm.Create(objectContext, nil, &childTemplate, nil, nil, nil)
if err != nil {
t.Errorf("Use of authorization on primary key failed: %v", err)
}
})
t.Run("UsePasswordAuth", func(t *testing.T) {
setHierarchyAuthForTest(t, tpm, tpm.OwnerHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.OwnerHandleContext())
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
objectContext, pub := run(t, tpm.OwnerHandleContext(), nil, &template, Data{}, PCRSelectionList{}, nil)
defer flushContext(t, tpm, objectContext)
verifyRSAAgainstTemplate(t, pub, &template)
})
t.Run("UseSessionAuth", func(t *testing.T) {
setHierarchyAuthForTest(t, tpm, tpm.OwnerHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.OwnerHandleContext())
sessionContext, err := tpm.StartAuthSession(nil, tpm.OwnerHandleContext(), SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer verifyContextFlushed(t, tpm, sessionContext)
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
objectContext, pub := run(t, tpm.OwnerHandleContext(), nil, &template, Data{}, PCRSelectionList{}, sessionContext)
defer flushContext(t, tpm, objectContext)
verifyRSAAgainstTemplate(t, pub, &template)
})
t.Run("WithOutsideInfo", func(t *testing.T) {
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
creationPCR := PCRSelectionList{
{Hash: HashAlgorithmSHA1, Select: []int{0, 1}},
{Hash: HashAlgorithmSHA256, Select: []int{7, 8}}}
data := Data("foo")
objectContext, pub := run(t, tpm.OwnerHandleContext(), nil, &template, data, creationPCR, nil)
defer flushContext(t, tpm, objectContext)
verifyRSAAgainstTemplate(t, pub, &template)
})
t.Run("InvalidTemplate", func(t *testing.T) {
template := Public{
Type: ObjectTypeECC,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
_, _, _, _, _, err := tpm.CreatePrimary(tpm.OwnerHandleContext(), nil, &template, nil, nil, nil)
if !IsTPMParameterError(err, ErrorSymmetric, CommandCreatePrimary, 2) {
t.Errorf("CreatePrimary returned an unexpected error: %v", err)
}
})
}
func TestHierarchyControl(t *testing.T) {
tpm, _, closeTPM := testutil.NewTPMContextT(t, testutil.TPMFeatureOwnerHierarchy|testutil.TPMFeatureEndorsementHierarchy|testutil.TPMFeaturePlatformHierarchy|testutil.TPMFeatureNV)
defer closeTPM()
run := func(t *testing.T, authContext ResourceContext, enable Handle, state bool, session SessionContext) {
if err := tpm.HierarchyControl(authContext, enable, state, session); err != nil {
t.Errorf("HierarchyControl failed: %v", err)
}
props, err := tpm.GetCapabilityTPMProperties(PropertyStartupClear, 1)
if err != nil || len(props) == 0 {
t.Fatalf("GetCapability failed: %v", err)
}
var mask StartupClearAttributes
switch enable {
case HandleOwner:
mask = AttrShEnable
case HandleEndorsement:
mask = AttrEhEnable
}
var expected StartupClearAttributes
if state {
expected = mask
}
if StartupClearAttributes(props[0].Value)&mask != expected {
t.Errorf("Unexpected value")
}
}
t.Run("Owner", func(t *testing.T) {
run(t, tpm.OwnerHandleContext(), HandleOwner, false, nil)
run(t, tpm.PlatformHandleContext(), HandleOwner, true, nil)
})
t.Run("Endorsement", func(t *testing.T) {
run(t, tpm.EndorsementHandleContext(), HandleEndorsement, false, nil)
run(t, tpm.PlatformHandleContext(), HandleEndorsement, true, nil)
})
t.Run("UsePasswordAuth", func(t *testing.T) {
setHierarchyAuthForTest(t, tpm, tpm.OwnerHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.OwnerHandleContext())
run(t, tpm.OwnerHandleContext(), HandleOwner, false, nil)
run(t, tpm.PlatformHandleContext(), HandleOwner, true, nil)
})
t.Run("UseSessionAuth", func(t *testing.T) {
setHierarchyAuthForTest(t, tpm, tpm.OwnerHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.OwnerHandleContext())
sessionContext, err := tpm.StartAuthSession(nil, tpm.OwnerHandleContext(), SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer verifyContextFlushed(t, tpm, sessionContext)
run(t, tpm.OwnerHandleContext(), HandleOwner, false, sessionContext)
run(t, tpm.PlatformHandleContext(), HandleOwner, true, nil)
})
}
func TestClear(t *testing.T) {
tpm, _, closeTPM := testutil.NewTPMContextT(t, testutil.TPMFeatureOwnerHierarchy|testutil.TPMFeatureEndorsementHierarchy|testutil.TPMFeatureLockoutHierarchy|testutil.TPMFeaturePlatformHierarchy|testutil.TPMFeatureClear|testutil.TPMFeatureNV)
defer closeTPM()
run := func(t *testing.T, authSession SessionContext) {
cleared := false
owner := tpm.OwnerHandleContext()
// Create storage primary key to test it gets evicted
primary := createRSASrkForTesting(t, tpm, nil)
// Persist storage primary key to test it gets evicted
primaryPersistHandle := Handle(0x8100ffff)
primaryPersist := persistObjectForTesting(t, tpm, owner, primary, primaryPersistHandle)
defer func() {
if cleared {
return
}
flushContext(t, tpm, primary)
evictPersistentObject(t, tpm, owner, primaryPersist)
}()
// Set endorsement hierarchy auth value (should be reset by Clear)
setHierarchyAuthForTest(t, tpm, tpm.EndorsementHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.EndorsementHandleContext())
// Set platform hierarchy auth value (shouldn't be reset by Clear)
setHierarchyAuthForTest(t, tpm, tpm.PlatformHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.PlatformHandleContext())
primaryHandle := primary.Handle()
// Perform the clear
if err := tpm.Clear(tpm.LockoutHandleContext(), authSession); err != nil {
t.Fatalf("Clear failed: %v", err)
}
cleared = true
// Verify that the objects we created have gone so we know that the command executed
if _, err := tpm.NewResourceContext(primaryHandle); err == nil {
t.Errorf("Clear didn't evict owner object")
}
if _, err := tpm.NewResourceContext(primaryPersistHandle); err == nil {
t.Errorf("Clear didn't evict owner object")
}
if tpm.EndorsementHandleContext().AuthValue() != nil {
t.Errorf("Clear didn't reset the authorization value for the EH ResourceContext")
}
if !bytes.Equal(tpm.PlatformHandleContext().AuthValue(), testAuth) {
t.Errorf("Clear reset the authorization value for the PH ResourceContext")
}
}
t.Run("NoAuth", func(t *testing.T) {
run(t, nil)
})
t.Run("UsePasswordAuth", func(t *testing.T) {
setHierarchyAuthForTest(t, tpm, tpm.LockoutHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.LockoutHandleContext())
run(t, nil)
})
t.Run("UseSessionAuth", func(t *testing.T) {
setHierarchyAuthForTest(t, tpm, tpm.LockoutHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.LockoutHandleContext())
sessionContext, err := tpm.StartAuthSession(nil, tpm.LockoutHandleContext(), SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer verifyContextFlushed(t, tpm, sessionContext)
run(t, sessionContext)
})
t.Run("UseUnboundSessionAuth", func(t *testing.T) {
setHierarchyAuthForTest(t, tpm, tpm.LockoutHandleContext())
defer resetHierarchyAuth(t, tpm, tpm.LockoutHandleContext())
sessionContext, err := tpm.StartAuthSession(nil, nil, SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer verifyContextFlushed(t, tpm, sessionContext)
run(t, sessionContext)
})
}
func TestHierarchyChangeAuth(t *testing.T) {
tpm, _, closeTPM := testutil.NewTPMContextT(t, testutil.TPMFeatureOwnerHierarchy|testutil.TPMFeatureEndorsementHierarchy|testutil.TPMFeatureNV)
defer closeTPM()
setAuth := func(t *testing.T, hierarchy ResourceContext, session SessionContext, testHierarchy func(t *testing.T)) {
if err := tpm.HierarchyChangeAuth(hierarchy, testAuth, session); err != nil {
t.Fatalf("HierarchyChangeAuth failed: %v", err)
}
testHierarchy(t)
hierarchy.SetAuthValue(testAuth)
testHierarchy(t)
}
resetAuth := func(t *testing.T, hierarchy ResourceContext, session SessionContext, testHierarchy func(*testing.T)) {
if err := tpm.HierarchyChangeAuth(hierarchy, nil, session); err != nil {
t.Errorf("HierarchyChangeAuth failed: %v", err)
}
testHierarchy(t)
hierarchy.SetAuthValue(nil)
testHierarchy(t)
}
createSrk := func(t *testing.T) {
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrUserWithAuth | AttrRestricted | AttrDecrypt,
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
objectContext, _, _, _, _, err := tpm.CreatePrimary(tpm.OwnerHandleContext(), nil, &template, nil, nil, nil)
if err != nil {
t.Errorf("CreatePrimary failed: %v", err)
}
flushContext(t, tpm, objectContext)
}
createEk := func(t *testing.T) {
template := Public{
Type: ObjectTypeRSA,
NameAlg: HashAlgorithmSHA256,
Attrs: AttrFixedTPM | AttrFixedParent | AttrSensitiveDataOrigin | AttrAdminWithPolicy | AttrRestricted | AttrDecrypt,
AuthPolicy: []byte{0x83, 0x71, 0x97, 0x67, 0x44, 0x84, 0xb3, 0xf8, 0x1a, 0x90, 0xcc, 0x8d, 0x46, 0xa5, 0xd7, 0x24, 0xfd, 0x52,
0xd7, 0x6e, 0x06, 0x52, 0x0b, 0x64, 0xf2, 0xa1, 0xda, 0x1b, 0x33, 0x14, 0x69, 0xaa},
Params: &PublicParamsU{
RSADetail: &RSAParams{
Symmetric: SymDefObject{
Algorithm: SymObjectAlgorithmAES,
KeyBits: &SymKeyBitsU{Sym: 128},
Mode: &SymModeU{Sym: SymModeCFB}},
Scheme: RSAScheme{Scheme: RSASchemeNull},
KeyBits: 2048,
Exponent: 0}}}
objectContext, _, _, _, _, err := tpm.CreatePrimary(tpm.EndorsementHandleContext(), nil, &template, nil, nil, nil)
if err != nil {
t.Errorf("CreatePrimary failed: %v", err)
}
flushContext(t, tpm, objectContext)
}
t.Run("OwnerWithPW", func(t *testing.T) {
setAuth(t, tpm.OwnerHandleContext(), nil, createSrk)
resetAuth(t, tpm.OwnerHandleContext(), nil, createSrk)
})
t.Run("EndorsementWithPW", func(t *testing.T) {
setAuth(t, tpm.EndorsementHandleContext(), nil, createEk)
resetAuth(t, tpm.EndorsementHandleContext(), nil, createEk)
})
t.Run("OwnerWithBoundHMACSession/1", func(t *testing.T) {
sessionContext, err := tpm.StartAuthSession(nil, tpm.OwnerHandleContext(), SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer flushContext(t, tpm, sessionContext)
sessionContext.SetAttrs(AttrContinueSession)
setAuth(t, tpm.OwnerHandleContext(), sessionContext, createSrk)
resetAuth(t, tpm.OwnerHandleContext(), sessionContext, createSrk)
})
t.Run("OwnerWithBoundHMACSession/2", func(t *testing.T) {
sessionContext1, err := tpm.StartAuthSession(nil, tpm.OwnerHandleContext(), SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer verifyContextFlushed(t, tpm, sessionContext1)
setAuth(t, tpm.OwnerHandleContext(), sessionContext1, createSrk)
sessionContext2, err := tpm.StartAuthSession(nil, tpm.OwnerHandleContext(), SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer verifyContextFlushed(t, tpm, sessionContext2)
resetAuth(t, tpm.OwnerHandleContext(), sessionContext2, createSrk)
})
t.Run("OwnerWithUnboundHMACSession/1", func(t *testing.T) {
sessionContext, err := tpm.StartAuthSession(nil, nil, SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer flushContext(t, tpm, sessionContext)
sessionContext.SetAttrs(AttrContinueSession)
setAuth(t, tpm.OwnerHandleContext(), sessionContext, createSrk)
resetAuth(t, tpm.OwnerHandleContext(), sessionContext, createSrk)
})
t.Run("OwnerWithUnboundHMACSession/2", func(t *testing.T) {
// This test highlights a bug where we didn't preserve the value of Session.includeAuthValue (which should be true) before computing
// the response HMAC. It's not caught by OwnerWithUnboundHMACSession because the lack of session key combined with
// Session.includeAuthValue incorrectly being false was causing processResponseSessionAuth to bail out early
primary := createRSASrkForTesting(t, tpm, nil)
defer flushContext(t, tpm, primary)
sessionContext, err := tpm.StartAuthSession(primary, nil, SessionTypeHMAC, nil, HashAlgorithmSHA256)
if err != nil {
t.Fatalf("StartAuthSession failed: %v", err)
}
defer flushContext(t, tpm, sessionContext)
sessionContext.SetAttrs(AttrContinueSession)
setAuth(t, tpm.OwnerHandleContext(), sessionContext, createSrk)
resetAuth(t, tpm.OwnerHandleContext(), sessionContext, createSrk)
})
}