]>
git.proxmox.com Git - swtpm.git/blob - tests/_test_tpm2_save_load_state
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-tpm2-test-save-load-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 MY_VOLATILE_STATE_FILE
=$TPM_PATH/my.volatilestate
15 MY_PERMANENT_STATE_FILE
=$TPM_PATH/my.permanent
16 SWTPM_INTERFACE
=${SWTPM_INTERFACE:-cuse}
17 SWTPM_CMD_UNIX_PATH
=${TPM_PATH}/unix-cmd.sock
18 SWTPM_CTRL_UNIX_PATH
=${TPM_PATH}/unix-ctrl.sock
25 if [ -n "$pid" ]; then
34 [ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
35 source ${TESTDIR}/common
37 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev
/null
39 run_swtpm
${SWTPM_INTERFACE} \
40 --log file=$logfile,level
=20 \
43 ps aux |
grep $SWTPM |
grep -v grep
47 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
54 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
56 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
62 kill -0 ${SWTPM_PID} 2>/dev
/null
64 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
71 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
72 RES
=$
(swtpm_cmd_tx
${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
73 exp
=' 80 01 00 00 00 0a 00 00 00 00'
74 if [ "$RES" != "$exp" ]; then
75 echo "Error: Did not get expected result from TPM2_Startup(ST_Clear)"
81 run_swtpm_ioctl
${SWTPM_INTERFACE} -h 1234
83 echo "Error: Hash command did not work."
89 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
91 # length CC count hashalg sz
92 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')
93 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'
94 if [ "$RES" != "$exp" ]; then
95 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
101 run_swtpm_ioctl
${SWTPM_INTERFACE} --save permanent
$MY_PERMANENT_STATE_FILE
102 if [ $?
-ne 0 ]; then
103 echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
108 if [ ! -r $MY_PERMANENT_STATE_FILE ]; then
109 echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
114 echo "Saved permanent state."
116 run_swtpm_ioctl
${SWTPM_INTERFACE} --save volatile
$MY_VOLATILE_STATE_FILE
117 if [ $?
-ne 0 ]; then
118 echo "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE."
123 if [ ! -r $MY_VOLATILE_STATE_FILE ]; then
124 echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
129 echo "Saved volatile state."
131 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
132 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
134 # we will use our own volatile state
135 rm -f $VOLATILE_STATE_FILE $STATE_FILE
137 # Stop the TPM; this will not shut it down
139 run_swtpm_ioctl
${SWTPM_INTERFACE} --stop
140 if [ $?
-ne 0 ]; then
141 echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM."
148 if [ $?
-ne 0 ]; then
149 echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
155 # load state into the TPM
156 run_swtpm_ioctl
${SWTPM_INTERFACE} --load permanent
$MY_PERMANENT_STATE_FILE
157 if [ $?
-ne 0 ]; then
158 echo "Could not load permanent state into vTPM"
163 echo "Loaded permanent state."
165 run_swtpm_ioctl
${SWTPM_INTERFACE} --load volatile
$MY_VOLATILE_STATE_FILE
166 if [ $?
-ne 0 ]; then
167 echo "Could not load volatile state into vTPM"
172 echo "Loaded volatile state."
174 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
175 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
178 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
179 if [ $?
-ne 0 ]; then
180 echo "TPM Init failed."
186 # Volatile state must have been removed by TPM now
187 if [ -r $VOLATILE_STATE_FILE ]; then
188 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
194 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
195 # Read the PCR again ...
196 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')
197 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'
198 if [ "$RES" != "$exp" ]; then
199 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
200 echo "expected: $exp"
201 echo "received: $RES"
205 # Save the volatile state again
206 run_swtpm_ioctl
${SWTPM_INTERFACE} -v
207 if [ $?
-ne 0 ]; then
208 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
214 if [ ! -r $VOLATILE_STATE_FILE ]; then
215 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
221 # Send a new TPM_Init
222 run_swtpm_ioctl
${SWTPM_INTERFACE} -i
223 if [ $?
-ne 0 ]; then
224 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
230 # Volatile state must have been removed by TPM now
231 if [ -r $VOLATILE_STATE_FILE ]; then
232 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
238 # Read the PCR again ...
239 swtpm_open_cmddev
${SWTPM_INTERFACE} 100
240 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')
241 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'
242 if [ "$RES" != "$exp" ]; then
243 echo "Error: (3) Did not get expected result from TPM_PCRRead(17)"
244 echo "expected: $exp"
245 echo "received: $RES"
251 run_swtpm_ioctl
${SWTPM_INTERFACE} -s
252 if [ $?
-ne 0 ]; then
253 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
261 kill -0 ${SWTPM_PID} 2>/dev
/null
262 if [ $?
-eq 0 ]; then
263 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
269 if [ ! -e $STATE_FILE ]; then
270 echo "Error: TPM state file $STATE_FILE does not exist."