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_NAME:-vtpm-test-tpm2-encrypted-state}
10 SWTPM_DEV_NAME
="/dev/${VTPM_NAME}"
11 export TPM_PATH
=$
(mktemp
-d)
12 STATE_FILE
=$TPM_PATH/tpm2-00.permall
13 VOLATILE_STATE_FILE
=$TPM_PATH/tpm2-00.volatilestate
14 KEY
=1234567890abcdef1234567890abcdef
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
21 echo "$KEY" > $keyfile
26 if [ -n "$pid" ]; then
29 rm -f $keyfile $logfile
35 [ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
36 source ${TESTDIR}/common
38 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev
/null
40 run_swtpm
${SWTPM_INTERFACE} \
41 --key file=$keyfile,mode
=aes-cbc
,format
=hex \
45 display_processes_by_name
"$SWTPM"
47 kill_quiet
-0 ${SWTPM_PID}
49 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
56 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
58 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
64 kill_quiet
-0 ${SWTPM_PID} 2>/dev
/null
66 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
73 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
74 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
75 exp
=' 80 01 00 00 00 0a 00 00 00 00'
76 if [ "$RES" != "$exp" ]; then
77 echo "Error: Did not get expected result from TPM2_Startup(SU_Clear)"
83 run_swtpm_ioctl
${SWTPM_INTERFACE} -h 1234
85 echo "Error: Hash command did not work."
92 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
93 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')
94 exp
=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 19 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'
95 if [ "$RES" != "$exp" ]; then
96 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
102 # Save the volatile state
103 run_swtpm_ioctl
${SWTPM_INTERFACE} -v
104 if [ $?
-ne 0 ]; then
105 echo "Error: Saving the volatile state failed."
111 if [ ! -r $VOLATILE_STATE_FILE ]; then
112 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
118 tmp
=$
(run_swtpm_ioctl
${SWTPM_INTERFACE} -g | cut
-d":" -f2)
119 if [ $?
-ne 0 ]; then
120 echo "Error: Could not get the configration flags of the ${SWTPM_INTERFACE} TPM."
126 if [ "$tmp" != " 0x1" ]; then
127 echo "Error: Unexpected configuration flags: $tmp; expected 0x1."
135 run_swtpm_ioctl
${SWTPM_INTERFACE} -s
136 if [ $?
-ne 0 ]; then
137 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
143 # Start the TPM again; have the keyfile removed
144 run_swtpm
${SWTPM_INTERFACE} \
145 --key file=$keyfile,mode
=aes-cbc
,format
=hex
,remove \
146 --log file=$logfile \
149 display_processes_by_name
"$SWTPM"
151 kill_quiet
-0 ${SWTPM_PID}
152 if [ $?
-ne 0 ]; then
153 echo "Error (2): ${SWTPM_INTERFACE} TPM did not start."
159 if [ -r $keyfile ]; then
160 echo "Error: Keyfile $keyfile was not removed by swtpm_cuse."
167 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
168 if [ $?
-ne 0 ]; then
169 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
175 # Volatile state must have been removed by TPM now
176 if [ -r $VOLATILE_STATE_FILE ]; then
177 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
183 # Read the PCR again ...
184 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
185 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')
186 exp
=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 19 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'
187 if [ "$RES" != "$exp" ]; then
188 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
189 echo "expected: $exp"
190 echo "received: $RES"
194 # Save the volatile state again
195 run_swtpm_ioctl
${SWTPM_INTERFACE} -v
196 if [ $?
-ne 0 ]; then
197 echo "Error: Saving the volatile state failed."
203 if [ ! -r $VOLATILE_STATE_FILE ]; then
204 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
210 # Send a new TPM_Init
211 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
212 if [ $?
-ne 0 ]; then
213 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
219 # Volatile state must have been removed by TPM now
220 if [ -r $VOLATILE_STATE_FILE ]; then
221 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
227 # Read the PCR again ...
228 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
229 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')
230 exp
=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 19 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'
231 if [ "$RES" != "$exp" ]; then
232 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
233 echo "expected: $exp"
234 echo "received: $RES"
241 run_swtpm_ioctl
${SWTPM_INTERFACE} -s
242 if [ $?
-ne 0 ]; then
243 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
249 if wait_process_gone
${SWTPM_PID} 1; then
250 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
256 if [ ! -e $STATE_FILE ]; then
257 echo "Error: TPM state file $STATE_FILE does not exist."