Debian Codegraph @

tests/test_ctrlchannel

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4
5 # shellcheck disable=SC2035,SC2061
6
7 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
8 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
9
10 TPMDIR="$(mktemp -d)" || exit 1
11 SWTPM_CTRL_UNIX_PATH=$TPMDIR/sock
12 PID_FILE=$TPMDIR/swtpm.pid
13 LOG_FILE=$TPMDIR/swtpm.log
14 CMD_PATH=$TPMDIR/cmd
15 RESP_PATH=$TPMDIR/resp
16
17 source "${TESTDIR}/test_common"
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 SWTPM_INTERFACE=socket+unix
30 SWTPM_SERVER_PORT=65430
31 SWTPM_SERVER_NAME=localhost
32 source "${TESTDIR}/common"
33 skip_test_no_tpm12 "${SWTPM_EXE}"
34
35 # Test 1: test the control channel on the socket tpm
36
37 # OS X would not allow nobody to access the $TPMDIR easily; skip it
38 if [ "$(id -u)" -eq 0 ] && [ "$(uname -s)" != "Darwin" ]; then
39 FOWNER=",uid=nobody,gid=$(id -Gn nobody | cut -d" " -f1)"
40 FILEOWNER="$(id -u nobody) $(id -G nobody | cut -d" " -f1)"
41 RUNAS="--runas nobody"
42 if ! chown nobody "$TPMDIR"; then
43 echo "Error: Could not change ownership of $TPMDIR"
44 exit 1
45 fi
46 fi
47
48 if [[ "$(uname -s)" =~ CYGWIN_NT- ]]; then
49 FILEMODE=661
50 else
51 FILEMODE=621
52 fi
53
54 case "$(uname -s)" in
55 FreeBSD)
56 kldload pty
57 ;;
58 esac
59
60 # use a pseudo terminal
61 if [ -c /dev/ptmx ]; then
62 exec 100<>/dev/ptmx
63 elif [ -c /dev/ptm ]; then
64 exec 100<>/dev/ptm
65 else
66 echo "Could not find chardev for opening file descriptor."
67 exit 1
68 fi
69
70 case $(uname -s) in
71 Linux|CYGWIN_NT-|Darwin)
72 PIDPARAM="fd=101"
73 exec 101<>"$PID_FILE"
74 ;;
75 *)
76 PIDPARAM="file=$PID_FILE"
77 ;;
78 esac
79
80 $SWTPM_EXE socket \
81 --fd 100 \
82 --tpmstate "dir=$TPMDIR" \
83 --pid "$PIDPARAM" \
84 --ctrl "type=unixio,path=$SWTPM_CTRL_UNIX_PATH,mode=${FILEMODE}${FOWNER}" \
85 --log "file=$LOG_FILE,level=20" \
86 ${RUNAS:+${RUNAS}} \
87 --daemon \
88 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}}
89 exec 100>&-
90 exec 101>&-
91
92
93 if [ ! -f "$PID_FILE" ]; then
94 echo "Error: Socket TPM did not write pidfile."
95 exit 1
96 fi
97
98 PID=$(cat "$PID_FILE")
99
100 # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
101 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x01')"
102 if [[ "$(uname -s)" =~ (Linux|OpenBSD|FreeBSD|NetBSD|Darwin|DragonFly) ]]; then
103 exp=" 00 00 00 00 00 01 7f ff"
104 else
105 exp=" 00 00 00 00 00 01 6f ff"
106 fi
107 if [ "$res" != "$exp" ]; then
108 echo "Error: Unexpected response from CMD_GET_CAPABILITY:"
109 echo " actual : $res"
110 echo " expected: $exp"
111 exit 1
112 fi
113
114 filemode=$(get_filemode "$SWTPM_CTRL_UNIX_PATH")
115 if [ "$filemode" != "$FILEMODE" ]; then
116 echo "Filemode bits are wrong"
117 echo "Expected: $FILEMODE"
118 echo "Actual : $filemode"
119 exit 1
120 fi
121
122 fileowner=$(get_fileowner "$SWTPM_CTRL_UNIX_PATH")
123 if [ -n "$FILEOWNER" ] && [ "$fileowner" != "$FILEOWNER" ]; then
124 echo "File ownership is wrong"
125 echo "Expected: $FILEOWNER"
126 echo "Actual : $fileowner"
127 exit 1
128 fi
129
130 # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags
131 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x02\x00\x00\x00\x00')"
132 exp=" 00 00 00 00"
133 if [ "$res" != "$exp" ]; then
134 echo "Error: Unexpected response from CMD_INIT:"
135 echo " actual : $res"
136 echo " expected: $exp"
137 exit 1
138 fi
139
140 # Send unknown command to the TPM
141 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\xff\xff')"
142 exp=" 00 00 00 0a"
143 if [ "$res" != "$exp" ]; then
144 echo "Error: Unexpected response from sending unsupported command:"
145 echo " actual : $res"
146 echo " expected: $exp"
147 exit 1
148 fi
149
150 # Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a
151 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0a')"
152 exp=" 00 00 00 00"
153 if [ "$res" != "$exp" ]; then
154 echo "Error: Unexpected response from CMD_STORE_VOLATILE:"
155 echo " actual : $res"
156 echo " expected: $exp"
157 exit 1
158 fi
159
160 if [ ! -r "$TPMDIR/tpm-00.volatilestate" ]; then
161 echo "Error: Socket TPM: Did not write volatile state file"
162 exit 1
163 fi
164
165 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
166 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0e')"
167 exp=" 00 00 00 00"
168 if [ "$res" != "$exp" ]; then
169 echo "Error: Socket TPM: Unexpected response from CMD_STOP:"
170 echo " actual : $res"
171 echo " expected: $exp"
172 exit 1
173 fi
174
175 # Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f
176 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0f')"
177 exp=" 00 00 00 00 00 00 00 00"
178 if [ "$res" != "$exp" ]; then
179 echo "Error: Socket TPM: Unexpected response from CMD_GET_CONFIG:"
180 echo " actual : $res"
181 echo " expected: $exp"
182 exit 1
183 fi
184
185 # To enable coverage of the above running as non-root we change the .gcda
186 # files' ownership with this small hack
187 if [ "$(id -u)" -eq 0 ] && [ "$(uname -s)" != "Darwin" ]; then
188 find "$ROOT" -name *.gcda -exec chown nobody {} \;
189 fi
190
191 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
192 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x03')"
193 exp=" 00 00 00 00"
194 if [ "$res" != "$exp" ]; then
195 echo "Error: Unexpected response from CMD_SHUTDOWN:"
196 echo " actual : $res"
197 echo " expected: $exp"
198 exit 1
199 fi
200
201 if wait_file_gone "$PID_FILE" 2; then
202 echo "Error: TPM should have removed PID file by now."
203 exit 1
204 fi
205
206 if wait_process_gone "${PID}" 4; then
207 echo "Error: TPM should not be running anymore."
208 exit 1
209 fi
210
211 check_logfile_patterns_level_20 "$LOG_FILE"
212 rm -f "$LOG_FILE"
213
214 echo "OK"
215
216 # Test 2: test the control channel on the socket tpm
217
218 # There are a few more tests here that require sending commands to the TPM
219
220 # use a pseudo terminal
221 run_swtpm "${SWTPM_INTERFACE}" \
222 --tpmstate "dir=$TPMDIR" \
223 --pid "file=$PID_FILE" \
224 --log "file=$LOG_FILE" \
225 --flags startup-clear \
226 ${RUNAS:+${RUNAS}}
227 PID=$SWTPM_PID
228
229 if wait_for_file "${PID_FILE}" 4; then
230 echo "Error: Socket TPM did not write pidfile."
231 cat "$LOG_FILE"
232 exit 1
233 fi
234
235 validate_pidfile "$PID" "$PID_FILE"
236
237 # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
238 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x01')"
239 if [[ "$(uname -s)" =~ (Linux|OpenBSD|FreeBSD|NetBSD|Darwin|DragonFly) ]]; then
240 exp=" 00 00 00 00 00 01 7f ff"
241 else
242 exp=" 00 00 00 00 00 01 6f ff"
243 fi
244 if [ "$res" != "$exp" ]; then
245 echo "Error: Socket TPM: Unexpected response from CMD_GET_CAPABILITY:"
246 echo " actual : $res"
247 echo " expected: $exp"
248 exit 1
249 fi
250
251 # Send unknown command to the TPM
252 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\xff\xff')"
253 exp=" 00 00 00 0a"
254 if [ "$res" != "$exp" ]; then
255 echo "Error: Socket TPM: Unexpected response from sending unsupported command:"
256 echo " actual : $res"
257 echo " expected: $exp"
258 exit 1
259 fi
260
261 # Startup the TPM; we use --flags startup-clear, so expect this to fail with error 0x26 (INVALID POST INIT)
262 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')"
263 exp=' 00 c4 00 00 00 0a 00 00 00 26'
264 if [ "$res" != "$exp" ]; then
265 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
266 echo "expected: $exp"
267 echo "received: $res"
268 exit 1
269 fi
270
271 # Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a
272 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0a')"
273 exp=" 00 00 00 00"
274 if [ "$res" != "$exp" ]; then
275 echo "Error: Socket TPM: Unexpected response from CMD_STORE_VOLATILE:"
276 echo " actual : $res"
277 echo " expected: $exp"
278 exit 1
279 fi
280
281 if [ ! -r "$TPMDIR/tpm-00.volatilestate" ]; then
282 echo "Error: Socket TPM: Did not write volatile state file"
283 exit 1
284 fi
285
286 # 1. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
287 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x04')"
288 exp=" 00 00 00 00 00 00 00 00"
289 if [ "$res" != "$exp" ]; then
290 echo "Error: Socket TPM: Unexpected response from sending CMD_GET_TPMESTABLISHED command:"
291 echo " actual : $res"
292 echo " expected: $exp"
293 exit 1
294 fi
295
296 # 2. Send command to start HASH : CMD_HASH_START = 00 00 00 06
297 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x06')"
298 exp=" 00 00 00 00"
299 if [ "$res" != "$exp" ]; then
300 echo "Error: Socket TPM: Unexpected response from sending CMD_HASH_START command:"
301 echo " actual : $res"
302 echo " expected: $exp"
303 exit 1
304 fi
305
306 # 2.1. Send command to hash data : CMD_HASH_DATA = 00 00 00 07 uint32(length) data
307 # We send 0x100 null bytes
308 echo -en '\x00\x00\x00\x07\x00\x00\x20\x00' > "$CMD_PATH"
309 dd if=/dev/zero count=$((0x2000)) bs=1 >> "$CMD_PATH" 2>/dev/null
310 socat -x -t10 "FILE:$CMD_PATH,rdonly" "UNIX-CONNECT:$SWTPM_CTRL_UNIX_PATH" 2>&1 | \
311 sed -n '/^ /p' | \
312 tail -n1 > "$RESP_PATH"
313 res="$(cat "$RESP_PATH")"
314 exp=" 00 00 00 00"
315 if [ "$res" != "$exp" ]; then
316 echo "Error: Socket TPM: Unexpected response from sending CMD_HASH_DATA command:"
317 echo " actual : $res"
318 echo " expected: $exp"
319 exit 1
320 fi
321
322 # 3. Send command to end HASH : CMD_HASH_END = 00 00 00 08
323 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x08')"
324 exp=" 00 00 00 00"
325 if [ "$res" != "$exp" ]; then
326 echo "Error: Socket TPM: Unexpected response from sending CMD_HASH_END command:"
327 echo " actual : $res"
328 echo " expected: $exp"
329 exit 1
330 fi
331
332 # 4. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
333 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x04')"
334 exp=" 00 00 00 00 01 00 00 00"
335 if [ "$res" != "$exp" ]; then
336 echo "Error: Socket TPM: Unexpected response from sending CMD_GET_TPMESTABLISHED command:"
337 echo " actual : $res"
338 echo " expected: $exp"
339 exit 1
340 fi
341
342 # 5. Send command to reset TPM established flag: CMD_RESET_TPMESTABLISHED = 00 00 00 0b 03
343 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0b\x03')"
344 exp=" 00 00 00 00"
345 if [ "$res" != "$exp" ]; then
346 echo "Error: Socket TPM: Unexpected response from sending CMD_GET_TPMESTABLISHED command:"
347 echo " actual : $res"
348 echo " expected: $exp"
349 exit 1
350 fi
351
352 # 6. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
353 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x04')"
354 exp=" 00 00 00 00 00 00 00 00"
355 if [ "$res" != "$exp" ]; then
356 echo "Error: Socket TPM: Unexpected response from sending CMD_GET_TPMESTABLISHED command:"
357 echo " actual : $res"
358 echo " expected: $exp"
359 exit 1
360 fi
361
362 # Read PCR 17
363 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')"
364 exp=' 00 c4 00 00 00 1e 00 00 00 00 c4 e1 e1 c9 81 c0 cd b1 e0 43 df 97 20 72 f9 5d a9 ff 06 ff'
365 if [ "$res" != "$exp" ]; then
366 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
367 echo "expected: $exp"
368 echo "received: $res"
369 exit 1
370 fi
371
372 # Get the volatile state of the TPM: CMD_GET_STATEBLOB = 00 00 00 0c
373 # cmd | flags | type | offset |
374 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00')"
375 # result | flags | totlength | length |
376 exp=" 00 00 00 00 00 00 00 00 00 00 04 e5 00 00 04 e5"
377 if [ "${res:0:48}" != "$exp" ]; then
378 echo "Error: Socket TPM: Unexpected response from CMD_GET_STATEBLOB:"
379 echo " actual : $res"
380 echo " expected: $exp"
381 exit 1
382 fi
383
384 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
385 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0e')"
386 exp=" 00 00 00 00"
387 if [ "$res" != "$exp" ]; then
388 echo "Error: Socket TPM: Unexpected response from CMD_STOP:"
389 echo " actual : $res"
390 echo " expected: $exp"
391 exit 1
392 fi
393
394 # Read PCR 17 -- should fail now
395 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')"
396 exp=' 00 c4 00 00 00 0a 00 00 00 09'
397 if [ "$res" != "$exp" ]; then
398 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
399 echo "expected: $exp"
400 echo "received: $res"
401 exit 1
402 fi
403
404 # Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f
405 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0f')"
406 exp=" 00 00 00 00 00 00 00 00"
407 if [ "$res" != "$exp" ]; then
408 echo "Error: Socket TPM: Unexpected response from CMD_GET_CONFIG:"
409 echo " actual : $res"
410 echo " expected: $exp"
411 exit 1
412 fi
413
414 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
415 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x03')"
416 exp=" 00 00 00 00"
417 if [ "$res" != "$exp" ]; then
418 echo "Error: Socket TPM: Unexpected response from CMD_SHUTDOWN:"
419 echo " actual : $res"
420 echo " expected: $exp"
421 exit 1
422 fi
423
424 if wait_file_gone "$PID_FILE" 2; then
425 echo "Error: TPM should have removed PID file by now."
426 exit 1
427 fi
428
429 if wait_process_gone "${PID}" 4; then
430 echo "Error: Socket TPM should not be running anymore."
431 exit 1
432 fi
433
434 # Expecting to see an error message for the unknown command
435 check_logfile_patterns_level_1 "$LOG_FILE" 1
436 rm -f "$LOG_FILE"
437
438 echo "OK"
439
440 # Test 3: test the control channel on the socket tpm: resume encrypted state
441
442 # copy all the state files
443 cp "${TESTDIR}"/data/tpmstate2/* "${TPMDIR}"
444
445 run_swtpm "${SWTPM_INTERFACE}" \
446 --tpmstate "dir=$TPMDIR" \
447 --pid "file=$PID_FILE" \
448 --key "pwdfile=${TESTDIR}/data/tpmstate2/pwdfile.txt,kdf=sha512" \
449 --log "file=$LOG_FILE,level=20" \
450 --flags not-need-init
451 PID=$SWTPM_PID
452
453 if wait_for_file "$PID_FILE" 3; then
454 echo "Error: Socket TPM did not write pidfile."
455 exit 1
456 fi
457
458 validate_pidfile "$PID" "$PID_FILE"
459
460 # Read PCR 10
461 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')"
462 exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
463 if [ "$res" != "$exp" ]; then
464 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
465 echo "expected: $exp"
466 echo "received: $res"
467 exit 1
468 fi
469
470 # Get the volatile state of the TPM: CMD_GET_STATEBLOB = 00 00 00 0c
471 # cmd | flags | type | offset |
472 vstate="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00')"
473 # result | flags | totlength | length |
474 exp=" 00 00 00 00 00 00 00 02 00 00 05 22 00 00 05 22"
475 if [ "${vstate:0:48}" != "$exp" ]; then
476 echo "Error: Socket TPM: Unexpected response from CMD_GET_STATEBLOB:"
477 echo " actual : ${vstate:0:48}"
478 echo " expected: $exp"
479 exit 1
480 fi
481
482 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
483 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x03')"
484 exp=" 00 00 00 00"
485 if [ "$res" != "$exp" ]; then
486 echo "Error: Socket TPM: Unexpected response from CMD_SHUTDOWN:"
487 echo " actual : $res"
488 echo " expected: $exp"
489 exit 1
490 fi
491
492 if wait_file_gone "$PID_FILE" 2; then
493 echo "Error: TPM should have removed PID file by now."
494 exit 1
495 fi
496
497 if wait_process_gone "${PID}" 4; then
498 echo "Error: Socket TPM should not be running anymore."
499 exit 1
500 fi
501
502 check_logfile_patterns_level_20 "$LOG_FILE"
503 rm -f "$LOG_FILE"
504
505 echo "OK"
506
507 # remove volatile state
508 rm -f "$TPMDIR"/*.volatilestate
509
510 run_swtpm "${SWTPM_INTERFACE}" \
511 --tpmstate "dir=$TPMDIR" \
512 --pid "file=$PID_FILE" \
513 --key "pwdfile=${TESTDIR}/data/tpmstate2/pwdfile.txt,kdf=sha512" \
514 --log "file=$LOG_FILE" \
515 --flags not-need-init
516 PID=$SWTPM_PID
517
518 if wait_for_file "$PID_FILE" 3; then
519 echo "Error: Socket TPM did not write pidfile."
520 exit 1
521 fi
522
523 validate_pidfile "$PID" "$PID_FILE"
524
525 # Read PCR 10 -- this should fail now
526 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')"
527 exp=' 00 c4 00 00 00 0a 00 00 00 26'
528 if [ "$res" != "$exp" ]; then
529 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
530 echo "expected: $exp"
531 echo "received: $res"
532 exit 1
533 fi
534
535 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
536 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x0e')"
537 exp=" 00 00 00 00"
538 if [ "$res" != "$exp" ]; then
539 echo "Error: Socket TPM: Unexpected response from CMD_STOP:"
540 echo " actual : $res"
541 echo " expected: $exp"
542 exit 1
543 fi
544
545 # Send the volatile state to the TPM (while it is stopped)
546 # | cmd | flags | type |
547 vstate=${vstate:48}
548 size=$((${#vstate} / 3))
549 size=$(printf "%08x" $size | sed 's/\([0-9a-f]\{2\}\)/\\x\1/g')
550 vstate=${vstate// /\\x}
551 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" "\x00\x00\x00\x0d\x00\x00\x00\x02\x00\x00\x00\x02${size}${vstate}")"
552 exp=" 00 00 00 00"
553 if [ "$res" != "$exp" ]; then
554 echo "Error: Socket TPM: Unexpected response from CMD_SET_STATEBLOB:"
555 echo " actual : $res"
556 echo " expected: $exp"
557 exit 1
558 fi
559
560 # Send init command to the TPM: CMD_INIT = 00 00 00 02
561 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x02\x00\x00\x00\x00')"
562 exp=" 00 00 00 00"
563 if [ "$res" != "$exp" ]; then
564 echo "Error: Socket TPM: Unexpected response from CMD_INIT:"
565 echo " actual : $res"
566 echo " expected: $exp"
567 exit 1
568 fi
569
570 # Read PCR 10 -- has to return same result as before
571 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a')"
572 exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
573 if [ "$res" != "$exp" ]; then
574 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
575 echo "expected: $exp"
576 echo "received: $res"
577 exit 1
578 fi
579
580 # Reset PCR 20 while in locality 0 -- should not work
581 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0F\x00\x00\x00\xC8\x00\x03\x00\x00\x10')"
582 exp=' 00 c4 00 00 00 0a 00 00 00 33'
583 if [ "$res" != "$exp" ]; then
584 echo "Error: Trying to reset PCR 20 in locality 0 returned unexpected result"
585 echo "expected: $exp"
586 echo "received: $res"
587 exit 1
588 fi
589
590 # In locality 2 we can reset PCR 20
591 # Set the localoty on the TPM: CMD_SET_LOCALITY = 00 00 00 05 <locality>
592 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x05\x02')"
593 exp=" 00 00 00 00"
594 if [ "$res" != "$exp" ]; then
595 echo "Error: Socket TPM: Unexpected response from CMD_SET_LOCALITY:"
596 echo " actual : $res"
597 echo " expected: $exp"
598 exit 1
599 fi
600
601 # Reset PCR 20 while in locality 2 -- has to work
602 res="$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0F\x00\x00\x00\xC8\x00\x03\x00\x00\x10')"
603 exp=' 00 c4 00 00 00 0a 00 00 00 00'
604 if [ "$res" != "$exp" ]; then
605 echo "Error: Could not reset PCR 20 in locality 2"
606 echo "expected: $exp"
607 echo "received: $res"
608 exit 1
609 fi
610
611 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
612 res="$(swtpm_ctrl_tx "${SWTPM_INTERFACE}" '\x00\x00\x00\x03')"
613 exp=" 00 00 00 00"
614 if [ "$res" != "$exp" ]; then
615 echo "Error: Socket TPM: Unexpected response from CMD_SHUTDOWN:"
616 echo " actual : $res"
617 echo " expected: $exp"
618 exit 1
619 fi
620
621 if wait_file_gone "$PID_FILE" 2; then
622 echo "Error: TPM should have removed PID file by now."
623 exit 1
624 fi
625
626 if wait_process_gone "${PID}" 4; then
627 echo "Error: Socket TPM should not be running anymore."
628 exit 1
629 fi
630
631 # (Currently) expecting to see nothing in the log file
632 check_logfile_patterns_level_1 "$LOG_FILE" 0
633 rm -f "$LOG_FILE"
634
635 echo "OK"
636
637 exit 0