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