Debian Codegraph @

tests/test_tpm2_ctrlchannel2

raw
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4
5 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
6 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
7
8 TPMDIR="$(mktemp -d)" || exit 1
9 PID_FILE=$TPMDIR/swtpm.pid
10 SOCK_PATH=$TPMDIR/sock
11 LOGFILE=$TPMDIR/logfile
12 VOLATILESTATE=$TPMDIR/volatile
13
14 source "${TESTDIR}/common"
15 source "${TESTDIR}/test_common"
16 skip_test_no_chardev "${SWTPM_EXE}"
17 skip_test_no_tpm20 "${SWTPM_EXE}"
18
19 trap "cleanup" SIGTERM EXIT
20
21 function cleanup()
22 {
23 rm -rf "$TPMDIR"
24 if [ -n "$PID" ]; then
25 kill_quiet -SIGTERM "$PID" 2>/dev/null
26 fi
27 }
28
29 # Test 1: test the control channel on the chardev tpm
30 if [ "$(id -u)" -eq 0 ]; then
31 FOWNER=",uid=$(id -u nobody),gid=$(id -G nobody | cut -d" " -f1)"
32 FILEOWNER="$(id -u nobody) $(id -G nobody | cut -d" " -f1)"
33 fi
34
35 # make sure --print-capabiities exits with '0'
36 if ! msg=$($SWTPM_EXE chardev --print-capabilities 2>&1); then
37 echo "Error: swtpm chardev --print-capabilities failed"
38 echo "$msg"
39 exit 1
40 fi
41
42 FILEMODE=621
43 # use a pseudo terminal
44 exec 100<>/dev/ptmx
45 $SWTPM_EXE chardev \
46 --fd 100 \
47 --tpmstate "dir=$TPMDIR" \
48 --pid "file=$PID_FILE" \
49 --ctrl "type=unixio,path=$SOCK_PATH,mode=${FILEMODE}${FOWNER}" \
50 --tpm2 \
51 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
52 PID=$!
53
54 if wait_for_file "$PID_FILE" 3; then
55 echo "Error: Chardev TPM did not write pidfile."
56 exit 1
57 fi
58
59 validate_pidfile "$PID" "$PID_FILE"
60
61 # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
62 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -c 2>&1); then
63 echo "Error: $SWTPM_IOCTL CMD_GET_CAPABILITY failed: $act"
64 exit 1
65 fi
66
67 filemode=$(get_filemode "$SOCK_PATH")
68 if [ "$filemode" != "$FILEMODE" ]; then
69 echo "Filemode bits are wrong"
70 echo "Expected: $FILEMODE"
71 echo "Actual : $filemode"
72 exit 1
73 fi
74
75 fileowner=$(get_fileowner "$SOCK_PATH")
76 if [ -n "$FILEOWNER" ] && [ "$fileowner" != "$FILEOWNER" ]; then
77 echo "File ownership is wrong"
78 echo "Expected: $FILEOWNER"
79 echo "Actual : $fileowner"
80 exit 1
81 fi
82
83 exp="ptm capability is 0x([[:xdigit:]]+)"
84 if ! [[ "$act" =~ ^${exp}$ ]]; then
85 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
86 exit 1
87 fi
88
89 # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags
90 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then
91 echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act"
92 exit 1
93 fi
94
95 # Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a
96 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -v 2>&1); then
97 echo "Error: $SWTPM_IOCTL CMD_STORE_VOLATILE failed: $act"
98 exit 1
99 fi
100
101 if [ ! -r "$TPMDIR/tpm2-00.volatilestate" ]; then
102 echo "Error: Socket TPM: Did not write volatile state file"
103 exit 1
104 fi
105
106 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
107 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --stop 2>&1); then
108 echo "Error: $SWTPM_IOCTL CMD_STOP failed: $act"
109 exit 1
110 fi
111
112 # Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f
113 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -g 2>&1); then
114 echo "Error: $SWTPM_IOCTL CMD_GET_CONFIG failed: $act"
115 exit 1
116 fi
117
118 exp="ptm configuration flags: 0x([[:xdigit:]]+)"
119 if ! [[ "$act" =~ ^${exp}$ ]]; then
120 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
121 exit 1
122 fi
123
124 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
125 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
126 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
127 exit 1
128 fi
129
130 if wait_file_gone "$PID_FILE" 2; then
131 echo "Error: TPM should have removed PID file by now."
132 exit 1
133 fi
134
135 if wait_process_gone "${PID}" 4; then
136 echo "Error: TPM should not be running anymore."
137 exit 1
138 fi
139
140 echo "OK"
141
142 # Test 2: test the control channel on the socket tpm
143
144 # There are a few more tests here that require sending commands to the TPM
145
146 # use a pseudo terminal
147 $SWTPM_EXE socket \
148 --server port=65532,disconnect=true \
149 --tpmstate "dir=$TPMDIR" \
150 --pid "file=$PID_FILE" \
151 --ctrl "type=unixio,path=$SOCK_PATH" \
152 --log "file=$LOGFILE,level=20" \
153 --tpm2 \
154 --flags startup-clear \
155 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
156 PID=$!
157
158 if wait_for_file "$PID_FILE" 3; then
159 echo "Error: Socket TPM did not write pidfile."
160 exit 1
161 fi
162
163 validate_pidfile "$PID" "$PID_FILE"
164
165 exec 100<>/dev/tcp/localhost/65532
166
167 # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
168 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -c 2>&1); then
169 echo "Error: $SWTPM_IOCTL CMD_GET_CAPABILITY failed: $act"
170 exit 1
171 fi
172
173 exp="ptm capability is 0x([[:xdigit:]]+)"
174 if ! [[ "$act" =~ ^${exp}$ ]]; then
175 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
176 exit 1
177 fi
178
179 # Startup the TPM; we used --flags startup-clear so expect this to fail now with 0x100
180 echo -en '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00' >&100
181 RES=$(cat <&100 | od -t x1 -A n)
182 exp=' 80 01 00 00 00 0a 00 00 01 00'
183 if [ "$RES" != "$exp" ]; then
184 echo "Error: Did not get expected result from TPM_Startup(SU_Clear)"
185 echo "expected: $exp"
186 echo "received: $RES"
187 exit 1
188 fi
189
190 # Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a
191 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -v 2>&1); then
192 echo "Error: $SWTPM_IOCTL CMD_STORE_VOLATILE failed: $act"
193 exit 1
194 fi
195
196 if [ ! -r "$TPMDIR/tpm2-00.volatilestate" ]; then
197 echo "Error: Socket TPM: Did not write volatile state file"
198 exit 1
199 fi
200
201 # 1. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
202 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -e 2>&1); then
203 echo "Error: $SWTPM_IOCTL CMD_GET_TPMESTABLISHED failed: $act"
204 exit 1
205 fi
206
207 exp="tpmEstablished is 0"
208 if [ "$act" != "$exp" ]; then
209 echo "Error: Expected '$exp' but got '$act'."
210 exit 1
211 fi
212
213 # 2. Hash the given data
214 data="a"
215 while [ ${#data} -lt $((0x2000)) ]; do
216 data="${data}${data}"
217 done
218 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -h $data 2>&1); then
219 echo "Error: $SWTPM_IOCTL data hashing failed: $act"
220 exit 1
221 fi
222
223 # 3. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
224 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -e 2>&1); then
225 echo "Error: $SWTPM_IOCTL CMD_GET_TPMESTABLISHED failed: $act"
226 exit 1
227 fi
228
229 exp="tpmEstablished is 1"
230 if [ "$act" != "$exp" ]; then
231 echo "Error: Expected '$exp' but got '$act'."
232 exit 1
233 fi
234
235 # 4. Send command to reset TPM established flag: CMD_RESET_TPMESTABLISHED = 00 00 00 0b 03
236 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -r 3 2>&1); then
237 echo "Error: $SWTPM_IOCTL CMD_RESET_TPMESTABLISHED failed: $act"
238 exit 1
239 fi
240
241 # 5. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
242 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -e 2>&1); then
243 echo "Error: $SWTPM_IOCTL CMD_GET_TPMESTABLISHED failed: $act"
244 exit 1
245 fi
246
247 exp="tpmEstablished is 0"
248 if [ "$act" != "$exp" ]; then
249 echo "Error: Expected '$exp' but got '$act'."
250 exit 1
251 fi
252
253 # Read PCR 17
254 exec 100<>/dev/tcp/localhost/65532
255 # length CC count hashalg sz
256 echo -en '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02' >&100
257 RES=$(cat <&100 | od -t x1 -A n | tr -d "\n")
258 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 18 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 e5 17 e3 9b 10 a3 5b 3b b7 29 95 79 4b c6 4a 07 f8 bc b0 bd e6 bb 31 ad 35 27 fb 6f 64 f8 4c b9'
259 if [ "$RES" != "$exp" ]; then
260 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
261 echo "expected: $exp"
262 echo "received: $RES"
263 exit 1
264 fi
265
266 # Get the volatile state of the TPM: CMD_GET_STATEBLOB = 00 00 00 0c
267 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --save volatile "$VOLATILESTATE" 2>&1); then
268 echo "Error: $SWTPM_IOCTL CMD_GET_STATEBLOB failed: $act"
269 exit 1
270 fi
271
272 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
273 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --stop 2>&1); then
274 echo "Error: $SWTPM_IOCTL CMD_STOP failed: $act"
275 exit 1
276 fi
277
278 # Read PCR 17 -- should fail now
279 exec 100<>/dev/tcp/localhost/65532
280 # length CC count hashalg sz
281 echo -en '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02' >&100
282 RES=$(cat <&100 | od -t x1 -A n | tr -d "\n")
283 exp=' 80 01 00 00 00 0a 00 00 01 01'
284 if [ "$RES" != "$exp" ]; then
285 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
286 echo "expected: $exp"
287 echo "received: $RES"
288 exit 1
289 fi
290
291 # Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f
292 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -g 2>&1); then
293 echo "Error: $SWTPM_IOCTL CMD_GET_CONFIG failed: $act"
294 exit 1
295 fi
296
297 exp="ptm configuration flags: 0x([[:xdigit:]]+)"
298 if ! [[ "$act" =~ ^${exp}$ ]]; then
299 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
300 exit 1
301 fi
302
303 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
304 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
305 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
306 exit 1
307 fi
308
309 if wait_file_gone "$PID_FILE" 2; then
310 echo "Error: TPM should have removed PID file by now."
311 exit 1
312 fi
313
314 if wait_process_gone "${PID}" 4; then
315 echo "Error: TPM should not be running anymore."
316 exit 1
317 fi
318
319 echo "OK"
320
321 # Test 3: test the control channel on the socket tpm: resume encrypted state
322
323 # copy all the state files
324 cp "${TESTDIR}/data/tpm2state2/"* "${TPMDIR}"
325
326 $SWTPM_EXE socket \
327 --server port=65532,disconnect=true \
328 --tpmstate "dir=$TPMDIR" \
329 --pid "file=$PID_FILE" \
330 --ctrl "type=unixio,path=$SOCK_PATH" \
331 --key "pwdfile=${TESTDIR}/data/tpm2state2/pwdfile.txt,kdf=sha512" \
332 --tpm2 \
333 --flags not-need-init \
334 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
335 PID=$!
336
337 if wait_for_file "$PID_FILE" 3; then
338 echo "Error: Socket TPM did not write pidfile."
339 exit 1
340 fi
341
342 validate_pidfile "$PID" "$PID_FILE"
343
344 # Read PCR 10
345 exec 100<>/dev/tcp/localhost/65532
346 # length CC count hashalg sz
347 echo -en '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00' >&100
348 RES=$(cat <&100 | od -t x1 -A n -w128)
349 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
350 if [ "$RES" != "$exp" ]; then
351 echo "Error: (1) Did not get expected result from TPM2_PCRRead(10)"
352 echo "expected: $exp"
353 echo "received: $RES"
354 exit 1
355 fi
356
357 # Get the volatile state of the TPM: CMD_GET_STATEBLOB = 00 00 00 0c
358 rm -f "$VOLATILESTATE"
359 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --save volatile "$VOLATILESTATE" 2>&1); then
360 echo "Error: $SWTPM_IOCTL CMD_GET_STATEBLOB failed: $act"
361 exit 1
362 fi
363
364 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
365 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
366 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
367 exit 1
368 fi
369
370 if wait_process_gone "${PID}" 4; then
371 echo "Error: TPM should not be running anymore."
372 exit 1
373 fi
374
375 if [ -f "$PID_FILE" ]; then
376 echo "Error: Socket TPM should have removed the PID file."
377 exit 1
378 fi
379
380 # remove volatile state
381 rm -f "$TPMDIR"/*.volatilestate
382
383 $SWTPM_EXE socket \
384 --server port=65532,disconnect=true \
385 --tpmstate "dir=$TPMDIR" \
386 --pid "file=$PID_FILE" \
387 --ctrl "type=unixio,path=$SOCK_PATH" \
388 --key "pwdfile=${TESTDIR}/data/tpm2state2/pwdfile.txt,kdf=sha512" \
389 --tpm2 \
390 --flags not-need-init \
391 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
392 PID=$!
393
394 if wait_for_file "$PID_FILE" 3; then
395 echo "Error: Socket TPM did not write pidfile."
396 exit 1
397 fi
398
399 validate_pidfile "$PID" "$PID_FILE"
400
401 # Read PCR 10 -- this should fail now
402 exec 100<>/dev/tcp/localhost/65532
403 # length CC count hashalg sz
404 echo -en '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00' >&100
405 RES=$(cat <&100 | od -t x1 -A n -w128)
406 exp=' 80 01 00 00 00 0a 00 00 01 00'
407 if [ "$RES" != "$exp" ]; then
408 echo "Error: (1) Did not get expected result from TPM2_PCRRead(10)"
409 echo "expected: $exp"
410 echo "received: $RES"
411 exit 1
412 fi
413
414 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
415 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --stop 2>&1); then
416 echo "Error: $SWTPM_IOCTL CMD_STOP failed: $act"
417 exit 1
418 fi
419
420 # Send the volatile state to the TPM (while it is stopped)
421 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --load volatile "$VOLATILESTATE" 2>&1); then
422 echo "Error: $SWTPM_IOCTL CMD_SET_STATEBLOB failed: $act"
423 exit 1
424 fi
425
426 # Send init command to the TPM: CMD_INIT = 00 00 00 02
427 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then
428 echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act"
429 exit 1
430 fi
431
432 # Read PCR 10 -- has to return same result as before
433 exec 100<>/dev/tcp/localhost/65532
434 # length CC count hashalg sz
435 echo -en '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00' >&100
436 RES=$(cat <&100 | od -t x1 -A n -w128)
437 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
438 if [ "$RES" != "$exp" ]; then
439 echo "Error: (1) Did not get expected result from TPM2_PCRRead(10)"
440 echo "expected: $exp"
441 echo "received: $RES"
442 exit 1
443 fi
444
445 # Reset PCR 20 while in locality 0 -- should not work
446 exec 100<>/dev/tcp/localhost/65532
447 echo -en '\x80\x02\x00\x00\x00\x1b\x00\x00\x01\x3d\x00\x00\x00\x14\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00' >&100
448 RES=$(cat <&100 | od -t x1 -A n)
449 exp=' 80 01 00 00 00 0a 00 00 09 07'
450 if [ "$RES" != "$exp" ]; then
451 echo "Error: Trying to reset PCR 20 in locality 0 returned unexpected result"
452 echo "expected: $exp"
453 echo "received: $RES"
454 exit 1
455 fi
456
457 # In locality 2 we can reset PCR 20
458 # Set the locality on the TPM: CMD_SET_LOCALITY = 00 00 00 05 <locality>
459 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -l 2 2>&1); then
460 echo "Error: $SWTPM_IOCTL CMD_SET_LOCALITY failed: $act"
461 exit 1
462 fi
463
464 # Reset PCR 20 while in locality 2 -- has to work
465 exec 100<>/dev/tcp/localhost/65532
466 echo -en '\x80\x02\x00\x00\x00\x1b\x00\x00\x01\x3d\x00\x00\x00\x14\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00' >&100
467 RES=$(cat <&100 | od -t x1 -A n -w512)
468 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
469 if [ "$RES" != "$exp" ]; then
470 echo "Error: Could not reset PCR 20 in locality 2"
471 echo "expected: $exp"
472 echo "received: $RES"
473 exit 1
474 fi
475
476 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
477 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
478 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
479 exit 1
480 fi
481
482 if wait_file_gone "$PID_FILE" 2; then
483 echo "Error: TPM should have removed PID file by now."
484 exit 1
485 fi
486
487 if wait_process_gone "${PID}" 4; then
488 echo "Error: TPM should not be running anymore."
489 exit 1
490 fi
491
492 echo "OK"
493
494 exit 0