3 # For the license, see the LICENSE file in the root directory.
6 if [ "$(id -u)" -ne 0 ]; then
7 echo "Need to be root to run this test."
11 ROOT
=${abs_top_builddir:-$(dirname "$0")/..}
12 TESTDIR
=${abs_top_testdir:-$(dirname "$0")}
15 SWTPM_EXE
=$ROOT/src
/swtpm
/$SWTPM
16 TPM_PATH
="$(mktemp -d)" ||
exit 1
17 STATE_FILE
=$TPM_PATH/tpm2-00.permall
18 VOLATILE_STATE_FILE
=$TPM_PATH/tpm2-00.volatilestate
19 PID_FILE
=$TPM_PATH/${SWTPM}.pid
20 SOCK_PATH
=$TPM_PATH/sock
21 CMD_PATH
=$TPM_PATH/cmd
22 RESP_PATH
=$TPM_PATH/resp
23 LOGFILE
=$TPM_PATH/logfile
27 pid
=$
(ps aux |
grep $SWTPM |
grep -E " file=${PID_FILE}\$" | gawk
'{print $2}')
28 if [ -n "$pid" ]; then
36 source ${TESTDIR}/common
37 skip_test_no_tpm20
"${SWTPM_EXE}"
39 source ${TESTDIR}/load_vtpm_proxy
41 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev
/null
46 --tpmstate dir
=$TPM_PATH \
47 --ctrl type=unixio
,path
=$SOCK_PATH \
48 --flags startup-clear \
49 ${SWTPM_TEST_SECCOMP_OPT} \
50 --pid file=$PID_FILE &>$LOGFILE &
52 PID
=$
(ps aux |
grep $SWTPM |
grep -E " file=${PID_FILE}\$" | gawk
'{print $2}')
54 display_processes_by_name
"$SWTPM"
58 echo "Error: Chardev TPM did not start."
61 TPM_DEVICE
=$
(sed -n 's,.*\(/dev/tpm[0-9]\+\).*,\1,p' $LOGFILE)
62 echo "Using ${TPM_DEVICE}."
64 # Wait for chardev to appear
65 for ((i
= 0; i
< 10; i
++)); do
66 [ -c "${TPM_DEVICE}" ] && break
69 if ! [ -c "${TPM_DEVICE}" ]; then
70 echo "Error: Chardev ${TPM_DEVICE} did not appear"
74 # Open access to the TPM
77 echo "Error: Could not open $TPM_DEVICE"
82 # length CC count hashalg sz
83 echo -en '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02' >&100
84 RES
=$
(od -t x1
-A n
-w128 <&100)
85 exp
=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 14 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff'
86 if [ "$RES" != "$exp" ]; then
87 echo "Error: Did not get expected result from TPM_PCRRead(17)"
97 echo "Error: Chardev TPM must have crashed."
101 if [ ! -e $STATE_FILE ]; then
102 echo "Error: TPM state file $STATE_FILE does not exist."
106 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
107 echo -en '\x00\x00\x00\x03' > $CMD_PATH
108 socat
-x -t10 FILE
:$CMD_PATH,rdonly UNIX-CONNECT
:$SOCK_PATH 2>&1 | \
110 tail -n1 > $RESP_PATH
111 res
="$(cat $RESP_PATH)"
113 if [ "$res" != "$exp" ]; then
114 echo "Error: Unexpected response from CMD_SHUTDOWN:"
115 echo " actual : $res"
116 echo " expected: $exp"
120 if wait_process_gone
${PID} 1; then
121 echo "Error: TPM should not be running anymore."
125 if [ -f $PID_FILE ]; then
126 echo "Error: TPM should have removed the PID file."