3 # For the license, see the LICENSE file in the root directory.
6 ROOT
=${abs_top_builddir:-$(pwd)/..}
7 TESTDIR
=${abs_top_testdir:-$(dirname "$0")}
9 VTPM_NAME
="vtpm-test-tpm2-volatilestate"
10 SWTPM_DEV_NAME
="/dev/${VTPM_NAME}"
11 export TPM_PATH
="$(mktemp -d)" ||
exit 1
12 STATE_FILE
=$TPM_PATH/tpm2-00.permall
13 VOLATILE_STATE_FILE
=$TPM_PATH/tpm2-00.volatilestate
14 SWTPM_CMD_UNIX_PATH
=$TPM_PATH/unix-cmd.sock
15 SWTPM_CTRL_UNIX_PATH
=$TPM_PATH/unix-ctrl.sock
16 SWTPM_INTERFACE
=${SWTPM_INTERFACE:-cuse}
21 if [ -n "$pid" ]; then
29 [ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
30 source ${TESTDIR}/common
32 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev
/null
34 run_swtpm
${SWTPM_INTERFACE} --tpm2
36 display_processes_by_name
"$SWTPM"
38 kill_quiet
-0 ${SWTPM_PID}
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
45 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
47 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
51 kill_quiet
-0 ${SWTPM_PID} 2>/dev
/null
53 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
58 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
59 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
60 exp
=' 80 01 00 00 00 0a 00 00 00 00'
61 if [ "$RES" != "$exp" ]; then
62 echo "Error: Did not get expected result from TPM_Startup(SU_Clear)"
68 run_swtpm_ioctl
${SWTPM_INTERFACE} -h 1234
70 echo "Error: Hash command did not work."
75 # length CC count hashalg sz
76 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
77 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
78 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 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54'
79 if [ "$RES" != "$exp" ]; then
80 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
86 # Check the TPM Established bit after the hashing
87 RES
=$
(run_swtpm_ioctl
${SWTPM_INTERFACE} -e)
88 exp
='tpmEstablished is 1'
89 if [ "$RES" != "$exp" ]; then
90 echo "Error (2): TPM Established flag has wrong value."
96 # Save the volatile state
97 run_swtpm_ioctl
${SWTPM_INTERFACE} -v
99 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
103 if [ ! -r $VOLATILE_STATE_FILE ]; then
104 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
110 run_swtpm_ioctl
${SWTPM_INTERFACE} -s
111 if [ $?
-ne 0 ]; then
112 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
116 # Start the TPM again
117 run_swtpm
${SWTPM_INTERFACE} --tpm2
119 display_processes_by_name
"$SWTPM"
121 kill_quiet
-0 ${SWTPM_PID}
122 if [ $?
-ne 0 ]; then
123 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
128 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
129 if [ $?
-ne 0 ]; then
130 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
134 # Volatile state must have been removed by TPM now
135 if [ -r $VOLATILE_STATE_FILE ]; then
136 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
140 # Read the PCR again ...
141 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
143 # length CC count hashalg sz
144 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
145 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 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54'
146 if [ "$RES" != "$exp" ]; then
147 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
148 echo "expected: $exp"
149 echo "received: $RES"
153 # Check that the TPM Established bit is still set
154 RES
=$
(run_swtpm_ioctl
${SWTPM_INTERFACE} -e)
155 exp
='tpmEstablished is 1'
156 if [ "$RES" != "$exp" ]; then
157 echo "Error (2): TPM Established flag has wrong value."
158 echo "expected: $exp"
159 echo "received: $RES"
163 # Save the volatile state again
164 run_swtpm_ioctl
${SWTPM_INTERFACE} -v
165 if [ $?
-ne 0 ]; then
166 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
170 if [ ! -r $VOLATILE_STATE_FILE ]; then
171 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
175 # Send a new TPM_Init
176 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
177 if [ $?
-ne 0 ]; then
178 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
182 # Volatile state must have been removed by TPM now
183 if [ -r $VOLATILE_STATE_FILE ]; then
184 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
188 # Read the PCR again ...
189 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
191 # length CC count hashalg sz
192 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x00\x02')
193 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 fc a5 d6 49 bf b0 c9 22 fd 33 0f 79 b2 00 43 28 9d af d6 0d 01 a4 c4 37 3c f2 8a db 56 c9 b4 54'
194 if [ "$RES" != "$exp" ]; then
195 echo "Error: (3) Did not get expected result from TPM_PCRRead(17)"
196 echo "expected: $exp"
197 echo "received: $RES"
201 # Check that the TPM Established bit is still set
202 RES
=$
(run_swtpm_ioctl
${SWTPM_INTERFACE} -e)
203 if [ $?
-ne 0 ]; then
204 echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
207 exp
='tpmEstablished is 1'
208 if [ "$RES" != "$exp" ]; then
209 echo "Error (2): TPM Established flag has wrong value."
210 echo "expected: $exp"
211 echo "received: $RES"
217 run_swtpm_ioctl
${SWTPM_INTERFACE} -s
218 if [ $?
-ne 0 ]; then
219 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
223 if wait_process_gone
${SWTPM_PID} 4; then
224 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
228 if [ ! -e $STATE_FILE ]; then
229 echo "Error: TPM state file $STATE_FILE does not exist."