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
|
#!/bin/bash
#
#################################################################################
# #
# TPM2 regression test #
# Written by Ken Goldman #
# IBM Thomas J. Watson Research Center #
# #
# (c) Copyright IBM Corporation 2016 - 2020 #
# #
# All rights reserved. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are #
# met: #
# #
# Redistributions of source code must retain the above copyright notice, #
# this list of conditions and the following disclaimer. #
# #
# Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in the #
# documentation and/or other materials provided with the distribution. #
# #
# Neither the names of the IBM Corporation nor the names of its #
# contributors may be used to endorse or promote products derived from #
# this software without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS #
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT #
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR #
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT #
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, #
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT #
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, #
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY #
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT #
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE #
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
# #
#################################################################################
# used for the name in policy ticket
if [ -z $TPM_DATA_DIR ]; then
TPM_DATA_DIR=.
fi
# PolicyCommandCode - sign
# cc69 18b2 2627 3b08 f5bd 406d 7f10 cf16
# 0f0a 7d13 dfd8 3b77 70cc bcd1 aa80 d811
# NV index name after written
# 000b
# 5e8e bdf0 4581 9419 070c 7d57 77bf eb61
# ffac 4996 ea4b 6fba de6d a42b 632d 4918
# PolicyAuthorizeNV with above Name
# 66 1f a1 02 db cd c2 f6 a0 61 7b 33 a0 ee 6d 95
# ab f6 2c 76 b4 98 b2 91 10 0d 30 91 19 f4 11 fa
# Policy in NV index 01000000
# signing key 80000001
echo ""
echo "Policy Authorize NV"
echo ""
echo "Start a policy session 03000000"
${PREFIX}startauthsession -se p > run.out
checkSuccess $?
echo "Create a signing key, policyauthnv"
${PREFIX}create -hp 80000000 -si -opr tmppriv.bin -opu tmppub.bin -pwdp sto -pwdk sig -pol policies/policyauthorizenv.bin > run.out
checkSuccess $?
echo "Load the signing key under the primary key"
${PREFIX}load -hp 80000000 -ipr tmppriv.bin -ipu tmppub.bin -pwdp sto > run.out
checkSuccess $?
echo "NV Define Space"
${PREFIX}nvdefinespace -hi o -ha 01000000 -sz 50 > run.out
checkSuccess $?
echo "NV not written, policyauthorizenv - should fail"
${PREFIX}policyauthorizenv -ha 01000000 -hs 03000000 > run.out
checkFailure $?
echo "Write algorithm ID into NV index 01000000"
${PREFIX}nvwrite -ha 01000000 -off 0 -if policies/sha256.bin > run.out
checkSuccess $?
echo "Write policy command code sign into NV index 01000000"
${PREFIX}nvwrite -ha 01000000 -off 2 -if policies/policyccsign.bin > run.out
checkSuccess $?
echo "Policy command code - sign"
${PREFIX}policycommandcode -ha 03000000 -cc 15d > run.out
checkSuccess $?
echo "Policy get digest - should be cc 69 ..."
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Policy Authorize NV against 01000000"
${PREFIX}policyauthorizenv -ha 01000000 -hs 03000000 > run.out
checkSuccess $?
echo "Policy get digest - should be 66 1f ..."
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Sign a digest - policy and wrong password"
${PREFIX}sign -hk 80000001 -if msg.bin -os sig.bin -se0 03000000 1 -pwdk xxx > run.out
checkSuccess $?
echo "Policy restart, set back to zero"
${PREFIX}policyrestart -ha 03000000 > run.out
checkSuccess $?
echo "Policy command code - sign"
${PREFIX}policycommandcode -ha 03000000 -cc 15d > run.out
checkSuccess $?
echo "Policy Authorize NV against 01000000"
${PREFIX}policyauthorizenv -ha 01000000 -hs 03000000 > run.out
checkSuccess $?
echo "Quote - policy, should fail"
${PREFIX}quote -hp 0 -hk 80000001 -os sig.bin -se0 03000000 1 > run.out
checkFailure $?
echo "Policy restart, set back to zero"
${PREFIX}policyrestart -ha 03000000 > run.out
checkSuccess $?
echo "Policy command code - quote"
${PREFIX}policycommandcode -ha 03000000 -cc 158 > run.out
checkSuccess $?
echo "Policy Authorize NV against 01000000 - should fail"
${PREFIX}policyauthorizenv -ha 01000000 -hs 03000000 > run.out
checkFailure $?
echo "NV Undefine Space"
${PREFIX}nvundefinespace -hi o -ha 01000000 > run.out
checkSuccess $?
echo "Flush the policy session 03000000"
${PREFIX}flushcontext -ha 03000000 > run.out
checkSuccess $?
echo "Flush the signing key 80000001 "
${PREFIX}flushcontext -ha 80000001 > run.out
checkSuccess $?
echo ""
echo "Policy Template"
echo ""
# create template hash
# run createprimary -si -v, extract template
# policies/policytemplate.txt
# 00 01 00 0b 00 04 04 72 00 00 00 10 00 10 08 00
# 00 00 00 00 00 00
# policymaker -if policies/policytemplate.txt -pr -of policies/policytemplate.bin -nz
# -nz says do not extend, just hash the hexascii line
# yields a template hash for policytemplate
# ef 64 da 91 18 fc ac 82 f4 36 1b 28 84 28 53 d8
# aa f8 7d fc e1 45 e9 25 cf fe 58 68 aa 2d 22 b6
# prepend the command code 00000190 to ef 64 ... and construct the actual object policy
# policymaker -if policies/policytemplatehash.txt -pr -of policies/policytemplatehash.bin
# fb 94 b1 43 e5 2b 07 95 b7 ec 44 37 79 99 d6 47
# 70 1c ae 4b 14 24 af 5a b8 7e 46 f2 58 af eb de
echo ""
echo "Policy Template with TPM2_Create"
echo ""
echo "Create a primary storage key policy template, 80000001"
${PREFIX}createprimary -hi p -pol policies/policytemplatehash.bin > run.out
checkSuccess $?
echo "Start a policy session 03000000"
${PREFIX}startauthsession -se p > run.out
checkSuccess $?
echo "Policy Template"
${PREFIX}policytemplate -ha 03000000 -te policies/policytemplate.bin > run.out
checkSuccess $?
echo "Policy get digest - should be fb 94 ... "
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Create signing key under primary key"
${PREFIX}create -si -hp 80000001 -kt f -kt p -se0 03000000 1 > run.out
checkSuccess $?
echo ""
echo "Policy Template with TPM2_CreateLoaded"
echo ""
echo "Policy restart, set back to zero"
${PREFIX}policyrestart -ha 03000000 > run.out
checkSuccess $?
echo "Policy Template"
${PREFIX}policytemplate -ha 03000000 -te policies/policytemplate.bin > run.out
checkSuccess $?
echo "Policy get digest - should be fb 94 ... "
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Create loaded signing key under primary key"
${PREFIX}createloaded -si -hp 80000001 -kt f -kt p -se0 03000000 1 > run.out
checkSuccess $?
echo "Flush the primary key 80000001"
${PREFIX}flushcontext -ha 80000001 > run.out
checkSuccess $?
echo "Flush the created key 80000002"
${PREFIX}flushcontext -ha 80000002 > run.out
checkSuccess $?
echo ""
echo "Policy Template with TPM2_CreatePrimary"
echo ""
echo "Set primary policy for platform hierarchy"
${PREFIX}setprimarypolicy -hi p -halg sha256 -pol policies/policytemplatehash.bin > run.out
checkSuccess $?
echo "Policy restart, set back to zero"
${PREFIX}policyrestart -ha 03000000 > run.out
checkSuccess $?
echo "Policy Template"
${PREFIX}policytemplate -ha 03000000 -te policies/policytemplate.bin > run.out
checkSuccess $?
echo "Policy get digest - should be fb 94 ... "
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Create loaded primary signing key policy template, 80000001"
${PREFIX}createprimary -si -hi p -se0 03000000 0 > run.out
checkSuccess $?
echo "Flush the primary key 80000001"
${PREFIX}flushcontext -ha 80000001 > run.out
checkSuccess $?
#
# Use case of the PCR brittleness solution using PolicyAuthorize, but
# where the authorizing public key is not hard coded in the sealed
# blob policy. Rather, it's in an NV Index, so that the authorizing
# key can be changed. Here, the authorization to change is platform
# auth. The NV index is locked until reboot as a second level of
# protection.
#
# Policy design
# PolicyAuthorizeNV and Name of NV index AND Unseal
# where the NV index holds PolicyAuthorize with the Name of the authorizing signing key
# where PolicyAuthorize will authorize command Unseal AND PCR values
# construct Policies
# Provision the NV Index data first. The NV Index Name is needed for the policy
# PolicyAuthorize with the Name of the authorizing signing key.
# The authorizing signing key Name can be obtained using the TPM from
# loadexternal below. It can also be calculated off line using this
# utility
# > publicname -ipem policies/rsapubkey.pem -halg sha256 -nalg sha256 -v -ns
# policyauthorize and CA public key
# policies/policyauthorizesha256.txt
# 0000016a000b64ac921a035c72b3aa55ba7db8b599f1726f52ec2f682042fc0e0d29fae81799
# (need blank line for policyRef)
# > policymaker -halg sha256 -if policies/policyauthorizesha256.txt -pr -v -ns -of policies/policyauthorizesha256.bin
# intermediate policy digest length 32
# fc 17 cd 86 c0 4f be ca d7 17 5f ef c7 75 5b 63
# a8 90 49 12 c3 2e e6 9a 4c 99 1a 7b 5a 59 bd 82
# intermediate policy digest length 32
# eb a3 f9 8c 5e af 1e a8 f9 4f 51 9b 4d 2a 31 83
# ee 79 87 66 72 39 8e 23 15 d9 33 c2 88 a8 e5 03
# policy digest length 32
# eb a3 f9 8c 5e af 1e a8 f9 4f 51 9b 4d 2a 31 83
# ee 79 87 66 72 39 8e 23 15 d9 33 c2 88 a8 e5 03
# policy digest:
# eba3f98c5eaf1ea8f94f519b4d2a3183ee79876672398e2315d933c288a8e503
# Once the NV Index Name is known, calculated the sealed blob policy.
# PolicyAuthorizeNV and Name of NV Index AND Unseal
#
# get NV Index Name from nvreadpublic after provisioning
# 000b56e16f0b810a6418daab06822be142858beaf9a79d66f66ad7e8e541f142498e
#
# policies/policyauthorizenv-unseal.txt
#
# policyauthorizenv and Name of NV Index
# 00000192000b56e16f0b810a6418daab06822be142858beaf9a79d66f66ad7e8e541f142498e
# policy command code unseal
# 0000016c0000015e
#
# > policymaker -halg sha256 -if policies/policyauthorizenv-unseal.txt -of policies/policyauthorizenv-unseal.bin -pr -v -ns
# intermediate policy digest length 32
# 2f 7a d9 b7 53 26 35 e5 03 8c e7 7b 8f 63 5e 4c
# f9 96 c8 62 18 13 98 94 c2 71 45 e7 7d d5 e8 e8
# intermediate policy digest length 32
# cd 1b 24 26 fe 10 08 6c 52 35 85 94 22 a0 59 69
# 33 4b 88 47 82 0d 0b d9 8c 43 1f 7f f7 36 34 5d
# policy digest length 32
# cd 1b 24 26 fe 10 08 6c 52 35 85 94 22 a0 59 69
# 33 4b 88 47 82 0d 0b d9 8c 43 1f 7f f7 36 34 5d
# policy digest:
# cd1b2426fe10086c5235859422a05969334b8847820d0bd98c431f7ff736345d
# The authorizing signer signs the PCR white list, here just PCR 16 extended with aaa
# PCR 16 is the resettable debug PCR, convenient for development
echo ""
echo "PolicyAuthorizeNV -> PolicyAuthorize -> PolicyPCR"
echo ""
# Initial provisioning (NV Index)
echo "NV Define Space"
${PREFIX}nvdefinespace -ha 01000000 -hi p -hia p -sz 34 +at wst +at ar > run.out
checkSuccess $?
echo "Write algorithm ID into NV index 01000000"
${PREFIX}nvwrite -ha 01000000 -hia p -off 0 -if policies/sha256.bin > run.out
checkSuccess $?
echo "Write the NV index at offset 2 with policy authorize and the Name of the CA signing key"
${PREFIX}nvwrite -ha 01000000 -hia p -off 2 -if policies/policyauthorizesha256.bin > run.out
checkSuccess $?
echo "Lock the NV Index"
${PREFIX}nvwritelock -ha 01000000 -hia p
checkSuccess $?
echo "Read the NV Index Name to be used above in Policy"
${PREFIX}nvreadpublic -ha 01000000 -ns > run.out
checkSuccess $?
# Initial provisioning (Sealed Data)
echo "Create a sealed data object"
${PREFIX}create -hp 80000000 -nalg sha256 -bl -kt f -kt p -opr tmppriv.bin -opu tmppub.bin -pwdp sto -uwa -if msg.bin -pol policies/policyauthorizenv-unseal.bin > run.out
checkSuccess $?
# Once per new PCR approved values, signer authorizing PCRs in policysha256.bin
echo "Openssl generate and sign aHash (empty policyRef) ${HALG}"
openssl dgst -sha256 -sign policies/rsaprivkey.pem -passin pass:rrrr -out pssig.bin policies/policypcr16aaasha256.bin > run.out 2>&1
echo " INFO:"
# Once per boot, simulating setting PCRs to authorized values, lock
# the NV index, which is unloaded at reboot to permit platform auth to
# roll the authorized signing key
echo "Lock the NV Index"
${PREFIX}nvwritelock -ha 01000000 -hia p
checkSuccess $?
echo "PCR 16 Reset"
${PREFIX}pcrreset -ha 16 > run.out
checkSuccess $?
echo "Extend PCR 16 to correct value"
${PREFIX}pcrextend -halg sha256 -ha 16 -if policies/aaa > run.out
checkSuccess $?
# At each unseal, or reuse the ticket tkt.bin for its lifetime
echo "Load external just the public part of PEM authorizing key sha256 80000001"
${PREFIX}loadexternal -hi p -halg sha256 -nalg sha256 -ipem policies/rsapubkey.pem -ns > run.out
checkSuccess $?
echo "Verify the signature to generate ticket 80000001 sha256"
${PREFIX}verifysignature -hk 80000001 -halg sha256 -if policies/policypcr16aaasha256.bin -is pssig.bin -raw -tk tkt.bin > run.out
checkSuccess $?
# Run time unseal
echo "Start a policy session"
${PREFIX}startauthsession -se p -halg sha256 > run.out
checkSuccess $?
echo "Policy PCR, update with the correct PCR 16 value"
${PREFIX}policypcr -halg sha256 -ha 03000000 -bm 10000 > run.out
checkSuccess $?
echo "Policy get digest - should be policies/policypcr16aaasha256.bin"
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
# policyauthorize process
echo "Policy authorize using the ticket"
${PREFIX}policyauthorize -ha 03000000 -appr policies/policypcr16aaasha256.bin -skn ${TPM_DATA_DIR}/h80000001.bin -tk tkt.bin > run.out
checkSuccess $?
echo "Get policy digest, should be policies/policyauthorizesha256.bin"
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Flush the authorizing public key"
${PREFIX}flushcontext -ha 80000001 > run.out
checkSuccess $?
echo "Policy Authorize NV against NV Index 01000000"
${PREFIX}policyauthorizenv -ha 01000000 -hs 03000000 > run.out
checkSuccess $?
echo "Get policy digest, should be policies/policyauthorizenv-unseal.bin intermediate"
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Policy command code - unseal"
${PREFIX}policycommandcode -ha 03000000 -cc 0000015e > run.out
checkSuccess $?
echo "Get policy digest, should be policies/policyauthorizenv-unseal.bin final"
${PREFIX}policygetdigest -ha 03000000 > run.out
checkSuccess $?
echo "Load the sealed data object"
${PREFIX}load -hp 80000000 -ipr tmppriv.bin -ipu tmppub.bin -pwdp sto > run.out
checkSuccess $?
echo "Unseal the data blob"
${PREFIX}unseal -ha 80000001 -of tmp.bin -se0 03000000 1 > run.out
checkSuccess $?
echo "Verify the unsealed result"
diff msg.bin tmp.bin > run.out
checkSuccess $?
echo "Flush the sealed object"
${PREFIX}flushcontext -ha 80000001 > run.out
checkSuccess $?
echo "Flush the policy session"
${PREFIX}flushcontext -ha 03000000 > run.out
checkSuccess $?
echo "NV Undefine Space"
${PREFIX}nvundefinespace -hi p -ha 01000000 > run.out
checkSuccess $?
# cleanup
rm -f tmppriv.bin
rm -f tmppub.bin
|