Repository navigation
Expand file tree
/
Copy pathtpm2-commands.robot
More file actions
346 lines (308 loc) · 18.9 KB
/
Copy pathtpm2-commands.robot
File metadata and controls
346 lines (308 loc) · 18.9 KB
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
*** Settings ***
Library Collections
Library OperatingSystem
Library Process
Library String
Library Telnet timeout=20 seconds connection_timeout=120 seconds
Library SSHLibrary timeout=390 seconds
Library RequestsLibrary
# TODO: maybe have a single file to include if we need to include the same
# stuff in all test cases
Resource ../variables.robot
Resource ../keywords.robot
Resource ../lib/tpm.robot
Resource ../lib/tpm2.robot
Resource ../keys.robot
# TODO:
# - document which setup/teardown keywords to use and what are they doing
# - go threough them and make sure they are doing what the name suggest (not
# exactly the case right now)
Suite Setup Run Keywords
... Prepare Test Suite
... AND Skip If not ${TESTS_IN_UBUNTU_SUPPORT}
... AND TPM2 Suite Setup
Suite Teardown Log Out And Close Connection
Test Setup Flush TPM Contexts
Default Tags automated
*** Variables ***
${TPM_FAILURE_CONTEXT}=
... The suite might fail due to TPM Lockout after power cut-offs. The device might need to be left ON for some time to recover. \n
... - https://github.com/Dasharo/dasharo-issues/issues/782#issuecomment-4098062653 \n
... - https://www.dell.com/support/kbdoc/en-us/000142311/tpm-failure-tries-recovery-time-and-lockout-recovery
*** Test Cases ***
TPMCMD001.201 Check if both SHA1 and SHA256 PCRs are enabled (Ubuntu)
[Documentation] This test aims to verify that `PCRALLOCATE` function
... works properly. It allows the user to specify a PCR
... allocation for the TPM.
Skip If ${SHA1_ENABLED} != ${SHA256_ENABLED} Only one bank is enabled
Should Be True ${SHA1_ENABLED}
Should Be True ${SHA256_ENABLED}
TPMCMD002.201 PCRREAD Function Verification (Ubuntu)
[Documentation] This test aims to verify that PCRREAD function works
... properly. Function reads contains of PCR banks and
... returns it to the terminal.
Skip If not ${SHA1_ENABLED} and not ${SHA256_ENABLED} No PCR banks enabled
${out}= Execute Linux Tpm2 Tools Command tpm2_pcrread
Should Contain ${out} sha1:
Should Contain ${out} sha256:
Should Contain ${out} 0x0000000000000000000000000000000000000000
Should Contain ${out} 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
TPMCMD003.201 PCREXTEND And PCRRESET Functions (Ubuntu)
[Documentation] This test aims to verify that PCREXTEND and PCRRESET
... functions are working properly.
Skip If not ${SHA1_ENABLED} and not ${SHA256_ENABLED} No PCR banks enabled
VAR ${sha1}= f1d2d2f924e986ac86fdf7b36c94bcdf32beec15
VAR ${sha256}= b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
${sha1_0s}= Evaluate "0" * 40
${sha256_0s}= Evaluate "0" * 64
Execute Linux Command tpm2_pcrreset 23
${out1}= Execute Linux Tpm2 Tools Command tpm2_pcrread
Execute Linux Tpm2 Tools Command tpm2_pcrextend 23:sha1=${sha1},sha256=${sha256}
${out2}= Execute Linux Tpm2 Tools Command tpm2_pcrread
Execute Linux Tpm2 Tools Command tpm2_pcrreset 23
${out3}= Execute Linux Tpm2 Tools Command tpm2_pcrread
# Append shasum to default PCR value of all 0s
${sha1}= Evaluate "0" * 40 + "${sha1}"
${sha256}= Evaluate "0" * 64 + "${sha256}"
# Calculate shasum of result
${sha1}= Execute Linux Command echo -n ${sha1} | xxd -r -p | sha1sum
VAR ${sha1}= ${sha1.split()}[0]
${sha256}= Execute Linux Command echo -n ${sha256} | xxd -r -p | sha256sum
VAR ${sha256}= ${sha256.split()}[0]
# Compare with PCR values reported by TPM
IF ${SHA1_ENABLED}
Should Contain ${out1} 23: 0x${sha1_0s}
Should Contain ${out2} 23: 0x${sha1.upper()}
Should Contain ${out3} 23: 0x${sha1_0s}
END
IF ${SHA256_ENABLED}
Should Contain ${out1} 23: 0x${sha256_0s}
Should Contain ${out2} 23: 0x${sha256.upper()}
Should Contain ${out3} 23: 0x${sha256_0s}
END
TPMCMD004.201 PCREXTEND And PCRRESET Functions - locality protections (Ubuntu)
[Documentation] This test aims to verify that PCREXTEND and PCRRESET
... functions are working properly when trying to modify protected PCRs.
Skip If not ${SHA1_ENABLED} and not ${SHA256_ENABLED} No PCR banks enabled
${sha1}= Generate Random String 40 [NUMBERS]abcdef
${sha256}= Generate Random String 64 [NUMBERS]abcdef
${out1}= Execute Linux Command tpm2_pcrreset 18
${out2}= Execute Linux Command tpm2_pcrextend 18:sha1=${sha1},sha256=${sha256}
${out3}= Execute Linux Tpm2 Tools Command tpm2_pcrread
Should Contain ${out1} tpm:warn(2.0): bad locality
Should Contain ${out2} tpm:warn(2.0): bad locality
Should Contain ${out3} 18: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
TPMCMD005.201 PCREVENT Function (Ubuntu)
[Documentation] This test aims to verify that PCREVENT function is
... working properly.
Skip If not ${SHA1_ENABLED} and not ${SHA256_ENABLED} No PCR banks enabled
Execute Linux Tpm2 Tools Command tpm2_pcrreset 23
${out}= Execute Linux Tpm2 Tools Command tpm2_pcrread
${sha1}= Evaluate "0" * 40
${sha256}= Evaluate "0" * 64
IF ${SHA1_ENABLED} Should Contain ${out} 23: 0x${sha1}
IF ${SHA256_ENABLED} Should Contain ${out} 23: 0x${sha256}
Execute Linux Command echo "foo" > data
${out}= Execute Linux Tpm2 Tools Command tpm2_pcrevent 23 data
# Calculate file shasums and compare with result of tpm2_pcrevent
${sha1}= Execute Linux Command sha1sum data
VAR ${sha1}= ${sha1.split()}[0]
${sha256}= Execute Linux Command sha256sum data
VAR ${sha256}= ${sha256.split()}[0]
Execute Linux Command rm -f data
IF ${SHA1_ENABLED} Should Contain ${out} sha1: ${sha1}
IF ${SHA256_ENABLED} Should Contain ${out} sha256: ${sha256}
# Append shasum to default PCR value of all 0s
${sha1}= Evaluate "0" * 40 + "${sha1}"
${sha256}= Evaluate "0" * 64 + "${sha256}"
# Calculate shasum of result
${sha1}= Execute Linux Command echo -n ${sha1} | xxd -r -p | sha1sum
VAR ${sha1}= ${sha1.split()}[0]
${sha256}= Execute Linux Command echo -n ${sha256} | xxd -r -p | sha256sum
VAR ${sha256}= ${sha256.split()}[0]
# Compare with PCR values reported by TPM
${out}= Execute Linux Tpm2 Tools Command tpm2_pcrread
IF ${SHA1_ENABLED} Should Contain ${out} 23: 0x${sha1.upper()}
IF ${SHA256_ENABLED}
Should Contain ${out} 23: 0x${sha256.upper()}
END
TPMCMD006.201 CREATEPRIMARY Function Verification (Ubuntu)
[Documentation] This test aims to verify that CREATEPRIMARY function
... works as expected. This command is used to create a
... primary object under one of the hierarchies: Owner,
... Platform, Endorsement, NULL.
${out}= Execute Linux Tpm2 Tools Command tpm2_createprimary -c primary.ctx 60
Execute Linux Command rm -f primary.ctx
Should Contain ${out} value: sha256
Should Contain ${out} value: fixedtpm|fixedparent|sensitivedataorigin|userwithauth|restricted|decrypt
Should Contain ${out} bits: 2048
TPMCMD007.201 NVDEFINE and NVUNDEFINE Functions Verification (Ubuntu)
[Documentation] This test aims to verify that NVDEFINE and NVUNDEFINE
... functions are working as expected. Those functions are
... used to define and undefine a TPM Non-Volatile index.
Execute Linux Tpm2 Tools Command tpm2_nvdefine -C o -s 32 -a "ownerread|policywrite|ownerwrite" 1
Execute Linux Command echo "nvtest" > nv.dat
Execute Linux Tpm2 Tools Command tpm2_nvwrite -C o -i nv.dat 1
# NV data is usually padded with 0xFF which Python doesn't like, change it to 0x00
${out1}= Execute Linux Tpm2 Tools Command tpm2_nvread -C o -s 32 1 | tr '\\377' '\\000'
Execute Linux Tpm2 Tools Command tpm2_nvundefine -C o 1
${out2}= Execute Linux Command tpm2_nvread -C o -s 32 1 2>&1
Execute Linux Command rm -f nv.dat
Should Contain ${out1} nvtest
Should Contain ${out2} ERROR: Unable to run tpm2_nvread
TPMCMD008.201 CREATE Function (Ubuntu)
[Documentation] This test aims to verify that CREATE function works as
... expected. It will create an object using all the default
... values and store the TPM sealed private and public
... portions to the paths specified via `-u` and `-r`
... respectively.
Execute Linux Tpm2 Tools Command tpm2_createprimary -c primary.ctx 60
${out}= Execute Linux Tpm2 Tools Command tpm2_create -C primary.ctx -u obj.pub -r obj.priv
Execute Linux Command rm -f primary.ctx obj.pub obj.priv
Should Contain ${out} value: sha256
Should Contain ${out} value: fixedtpm|fixedparent|sensitivedataorigin|userwithauth|decrypt|sign
Should Contain ${out} bits: 2048
TPMCMD009.201 CREATELOADED Function (Ubuntu)
[Documentation] This test aims to verify that CREATELOADED function works
... as expected. It will create an object using all the
... default values and store key context to the path
... specified via `-c`.
Execute Linux Tpm2 Tools Command tpm2_createprimary -c primary.ctx 60
${out}= Execute Linux Tpm2 Tools Command tpm2_create -C primary.ctx -c obj.key
Execute Linux Command rm -f primary.ctx obj.key
Should Contain ${out} value: sha256
Should Contain ${out} value: fixedtpm|fixedparent|sensitivedataorigin|userwithauth|decrypt|sign
Should Contain ${out} bits: 2048
TPMCMD010.201 Signing the file (Ubuntu)
[Documentation] Check whether the TPM supports file signing.
Execute Linux Tpm2 Tools Command tpm2_createprimary -c primary_key.ctx 60
Execute Linux Tpm2 Tools Command tpm2_create -u key.pub -r key.priv -C primary_key.ctx
Flush TPM Contexts
Execute Linux Tpm2 Tools Command tpm2_load -C primary_key.ctx -u key.pub -r key.priv -c key.ctx
Execute Linux Command echo "my secret" > secret.data
Execute Linux Tpm2 Tools Command tpm2_sign -c key.ctx -o sig.rssa secret.data
Flush TPM Contexts
Execute Linux Tpm2 Tools Command tpm2_verifysignature -c key.ctx -s sig.rssa -m secret.data
Execute Linux Command rm -f primary_key.ctx key.pub key.priv key.ctx sig.rssa secret.data
TPMCMD011.201 Encryption and Decryption of the file (Ubuntu)
[Documentation] Check whether the TPM supports the encryption and
... decryption of the file.
${out}= Execute Linux Tpm2 Tools Command tpm2_getcap commands
${passed}= Run Keyword And Return Status
... Should Contain ${out} TPM2_EncryptDecrypt
# At least one must be implemented, but both are caught by one test
Skip If not ${passed} TPM doesn't supports TPM2_EncryptDecrypt nor TPM2_EncryptDecrypt2
Execute Linux Tpm2 Tools Command tpm2_createprimary -c primary_key.ctx 60
Execute Linux Tpm2 Tools Command tpm2_create -u key.pub -r key.priv -C primary_key.ctx -Gaes128
Flush TPM Contexts
Execute Linux Tpm2 Tools Command tpm2_load -C primary_key.ctx -u key.pub -r key.priv -c key.ctx
Execute Linux Command echo "my secret" > secret.data
# Avoid getting 'WARN: Using a weak IV, try specifying an IV'
Execute Linux Command dd if=/dev/zero bs=1 count=16 of=iv.bin
Execute Linux Tpm2 Tools Command tpm2_encryptdecrypt -c key.ctx -o secret.enc secret.data -t iv.bin
Flush TPM Contexts
Execute Linux Tpm2 Tools Command tpm2_encryptdecrypt -d -c key.ctx -o secret.dec secret.enc -t iv.bin
${out}= Execute Linux Command cat secret.dec
Execute Linux Command rm -f primary_key.ctx key.pub key.priv key.ctx secret.enc secret.dec secret.data iv.bin
Should Contain ${out} my secret
TPMCMD012.201 Hashing the file (Ubuntu)
[Documentation] Check whether the TPM supports file hashing.
Execute Linux Command echo "my secret" > secret.data
Execute Linux Tpm2 Tools Command tpm2_hash -o hash.out -t ticket.out secret.data
${out1}= Execute Linux Command ls
${out2}= Execute Linux Command find . -empty -name hash.out
${out3}= Execute Linux Command find . -empty -name ticket.out
Execute Linux Command rm -f hash.out ticket.out secret.data
Should Contain ${out1} hash.out
Should Contain ${out1} ticket.out
Should Not Contain ${out2} hash.out
Should Not Contain ${out3} ticket.out
TPMCMD013.201 Performing HMAC operation on the file (Ubuntu)
[Documentation] Check whether the TPM supports HMAC operation.
Execute Linux Tpm2 Tools Command tpm2_createprimary -c primary_key.ctx 60
Execute Linux Tpm2 Tools Command tpm2_create -u key.pub -r key.priv -C primary_key.ctx -G hmac
Flush TPM Contexts
Execute Linux Tpm2 Tools Command tpm2_load -C primary_key.ctx -u key.pub -r key.priv -c hmac.key
Execute Linux Command echo "my secret" > secret.data
Execute Linux Tpm2 Tools Command tpm2_hmac -c hmac.key -o hmac.out secret.data
${out1}= Execute Linux Command ls
${out2}= Execute Linux Command find . -empty -name hmac.out
Execute Linux Command rm -f hmac.out hmac.key secret.data primary_key.ctx key.pub key.priv
Should Contain ${out1} hmac.out
Should Not Contain ${out2} hmac.out
TPMCMD014.201 Sealing and Unsealing the file without Policy (Ubuntu)
[Documentation] This test verifies TPM sealing functionality.
VAR ${secret}= my sealed data
Execute Linux Tpm2 Tools Command tpm2_createprimary -c primary.ctx
Execute Linux Command echo "${secret}" > seal.dat
Execute Linux Tpm2 Tools Command tpm2_create -C primary.ctx -i seal.dat -u key.pub -r key.priv
Execute Linux Tpm2 Tools Command tpm2_evictcontrol --hierarchy owner --object-context seal.ctx -o seal.handle
Execute Linux Tpm2 Tools Command tpm2_unseal -c seal.handle > unsealed.dat
${out}= Execute Linux Command cat unsealed.dat
Should Contain ${out} ${secret}
TPMCMD015.201 Sealing and Unsealing with Policy - Password Only (Ubuntu)
[Documentation] Check whether the TPM supports sealing and unsealing using password policy.
VAR ${secret}= password policy sealed data
Execute Linux Tpm2 Tools Command tpm2_createprimary -C e -g sha256 -G ecc -c primary.ctx
Execute Linux Command echo "${secret}" > seal.dat
Execute Linux Tpm2 Tools Command tpm2_startauthsession -S session.dat
Execute Linux Tpm2 Tools Command tpm2_policypassword -S session.dat -L policy.dat
Execute Linux Tpm2 Tools Command
... tpm2_create -Q -u key.pub -r key.priv -C primary.ctx -L policy.dat -i seal.dat -p policypswd
Execute Linux Tpm2 Tools Command tpm2_load -C primary.ctx -u key.pub -r key.priv -n seal.name -c seal.ctx
Execute Linux Tpm2 Tools Command tpm2_startauthsession --policy-session -S session.dat
Execute Linux Tpm2 Tools Command tpm2_policypassword -S session.dat -L policy.dat
${out}= Execute Linux Tpm2 Tools Command tpm2_unseal -p session:session.dat+policypswd -c seal.ctx
Should Contain ${out} ${secret}
TPMCMD016.201 Sealing and Unsealing with Policy - PCR Only (Ubuntu)
[Documentation] Check whether the TPM supports sealing and unsealing using PCR policy.
VAR ${secret}= PCR policy sealed data
Execute Linux Tpm2 Tools Command tpm2_createprimary -C e -g sha256 -G ecc -c primary.ctx
Execute Linux Command echo "${secret}" > seal.dat
Execute Linux Tpm2 Tools Command tpm2_startauthsession -S session.dat
Execute Linux Tpm2 Tools Command tpm2_policypcr -S session.dat -l "sha1:0,1,2,3,7" -L policy.dat
Execute Linux Tpm2 Tools Command tpm2_create -Q -u key.pub -r key.priv -C primary.ctx -L policy.dat -i seal.dat
Execute Linux Tpm2 Tools Command tpm2_load -C primary.ctx -u key.pub -r key.priv -n seal.name -c seal.ctx
Execute Linux Tpm2 Tools Command tpm2_startauthsession --policy-session -S session.dat
Execute Linux Tpm2 Tools Command tpm2_policypcr -S session.dat -l "sha1:0,1,2,3,7" -L policy.dat
${out}= Execute Linux Tpm2 Tools Command tpm2_unseal -p session:session.dat -c seal.ctx
Should Contain ${out} ${secret}
TPMCMD017.201 Sealing and unsealing with Policy - Password and PCR (Ubuntu)
[Documentation] Check whether the TPM supports sealing and unsealing using PCR and password
... policy at the same time.
VAR ${secret}= policy sealed data
Execute Linux Tpm2 Tools Command tpm2_createprimary -C e -g sha256 -G ecc -c primary.ctx
Execute Linux Command echo "${secret}" > seal.dat
Execute Linux Tpm2 Tools Command tpm2_startauthsession -S session.dat
Execute Linux Tpm2 Tools Command tpm2_policypassword -S session.dat -L policy.dat
Execute Linux Tpm2 Tools Command tpm2_policypcr -S session.dat -l "sha1:0,1,2,3,7" -L policy.dat
Execute Linux Tpm2 Tools Command
... tpm2_create -Q -u key.pub -r key.priv -C primary.ctx -L policy.dat -i seal.dat -p policypswd
Execute Linux Tpm2 Tools Command tpm2_load -C primary.ctx -u key.pub -r key.priv -n seal.name -c seal.ctx
Execute Linux Tpm2 Tools Command tpm2_startauthsession --policy-session -S session.dat
Execute Linux Tpm2 Tools Command tpm2_policypassword -S session.dat -L policy.dat
Execute Linux Tpm2 Tools Command tpm2_policypcr -S session.dat -l "sha1:0,1,2,3,7" -L policy.dat
${out}= Execute Linux Tpm2 Tools Command tpm2_unseal -p session:session.dat+policypswd -c seal.ctx
Should Contain ${out} ${secret}
*** Keywords ***
TPM2 Suite Setup
Skip If '${TPM_SUPPORTED_VERSION}' != '2' TPM commands tests supported only TPM2
Power On
Boot System Or From Connected Disk ${ENV_ID_UBUNTU}
Login To Linux
Switch To Root User
Verify Presence Of TPM Via Sysfs
Detect Or Install Package tpm2-tools
${passed}= Run Keyword And Return Status
... Check If SHA1 And SHA256 Banks Are Enabled
IF not ${passed}
# Restore default allocations in case any bank was disabled and reboot
Execute Linux Tpm2 Tools Command tpm2_pcrallocate
Execute Reboot Command
Boot System Or From Connected Disk ${ENV_ID_UBUNTU}
Login To Linux
Switch To Root User
END
${sha1_state} ${sha256_state}= Check Which TPM2 Banks Are Enabled
VAR ${SHA1_ENABLED}= ${sha1_state} scope=SUITE
VAR ${SHA256_ENABLED}= ${sha256_state} scope=SUITE