Debian Codegraph @

tests/_test_tpm2_swtpm_bios

raw
1 #!/bin/bash
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(pwd)/..}
7 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
9 VTPM_NAME="vtpm-test-tpm2-swtpm-bios"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktemp -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm2-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14 PID_FILE=$TPM_PATH/swtpm.pid
15 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
16 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
17 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
18
19 function cleanup()
20 {
21 pid=${SWTPM_PID}
22 if [ -n "$pid" ]; then
23 kill_quiet -9 "$pid"
24 fi
25 rm -rf "$TPM_PATH"
26 }
27
28 trap "cleanup" EXIT
29
30 source "${TESTDIR}/common"
31 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
32
33 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
34
35 run_swtpm "${SWTPM_INTERFACE}" --tpm2 --tpmstate "dir=$TPM_PATH" --pid "file=$PID_FILE"
36
37 display_processes_by_name "swtpm"
38
39 if ! kill_quiet -0 "${SWTPM_PID}"; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42 fi
43
44 if wait_for_file "${PID_FILE}" 4; then
45 echo "Error: ${SWTPM_INTERFACE} TPM did not write pidfile."
46 exit 1
47 fi
48
49 PIDF="$(cat "$PID_FILE")"
50 if [ "$PIDF" != "${SWTPM_PID}" ]; then
51 echo "Error: ${SWTPM_INTERFACE} TPM wrote pid $PIDF, but found ${SWTPM_PID}."
52 exit 1
53 fi
54
55 # Init the TPM
56 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
57 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
58 exit 1
59 fi
60
61 if ! wait_process_gone "${SWTPM_PID}" 4; then
62 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
63 exit 1
64 fi
65
66 # must work
67 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" --tpm2 -o 2>&1) || [ -n "$res" ]; then
68 echo "Error: Could not startup the ${SWTPM_INTERFACE} TPM."
69 exit 1
70 fi
71
72 # must work
73 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" --tpm2 -n --cs 2>&1) || [ -n "$res" ]; then
74 echo "Error: Could not self-test the ${SWTPM_INTERFACE} TPM."
75 exit 1
76 fi
77
78 # must work
79 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" --tpm2 -n -u 2>&1) || [ -n "$res" ]; then
80 echo "Error: Could not give up physical presence on the ${SWTPM_INTERFACE} TPM."
81 exit 1
82 fi
83
84 # will NOT work -- we get error output in $res
85 res=$(run_swtpm_bios "${SWTPM_INTERFACE}" --tpm2 -n -u 2>&1)
86 ret=$?
87 if [ $ret -eq 0 ] || [ -z "$res" ]; then
88 echo "Error: Could give up physical presence on the ${SWTPM_INTERFACE} TPM."
89 exit 1
90 fi
91
92 if [ $ret -ne 128 ]; then
93 echo "Error: Wrong return code from swtpm_bios. Should be 128, had $ret."
94 exit 1
95 fi
96
97 # will NOT work (wrong device) -- we get error output in $res
98 if [ "${SWTPM_INTERFACE}" == "cuse" ]; then
99 res=$($SWTPM_BIOS --tpm-device /dev/${VTPM_NAME}123--tpm2 -n -u 2>&1)
100 ret=$?
101 if [ $ret -eq 0 ] || [ -z "$res" ]; then
102 echo "Error: Could give up physical presence on wrong device."
103 exit 1
104 fi
105
106 if [ $ret -ne 255 ]; then
107 echo "Error: Wrong return code from swtpm_bios. Should be 255, had $ret."
108 exit 1
109 fi
110 fi
111
112 # RESET TPM
113
114 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
115 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
116 exit 1
117 fi
118
119 # must work
120 if ! res=$(run_swtpm_bios "${SWTPM_INTERFACE}" --tpm2 --cs -u 2>&1) || [ -n "$res" ]; then
121 echo "Error: Could not startup the ${SWTPM_INTERFACE} TPM."
122 exit 1
123 fi
124
125 # will NOT work -- we get error output in $res
126 res=$(run_swtpm_bios "${SWTPM_INTERFACE}" --tpm2 -n -u 2>&1)
127 ret=$?
128 if [ $ret -eq 0 ] || [ -z "$res" ]; then
129 echo "Error: Could give up physical presence on the ${SWTPM_INTERFACE} TPM."
130 exit 1
131 fi
132
133 if [ $ret -ne 128 ]; then
134 echo "Error: Wrong return code from swtpm_bios. Should be 128, had $ret."
135 exit 1
136 fi
137
138 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
139 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
140 exit 1
141 fi
142
143 if wait_process_gone "${SWTPM_PID}" 4; then
144 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
145 exit 1
146 fi
147
148 if [ ! -e "$STATE_FILE" ]; then
149 echo "Error: TPM state file $STATE_FILE does not exist."
150 exit 1
151 fi
152
153 echo "OK"
154
155 exit 0