Debian Codegraph @

tests/_test_tpm2_save_load_state

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_NAME:-vtpm-tpm2-test-save-load-state}"
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/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
19
20 logfile="$(mktemp)" || exit 1
21
22 function cleanup()
23 {
24 pid=${SWTPM_PID}
25 if [ -n "$pid" ]; then
26 kill_quiet -9 "$pid"
27 fi
28 rm -f "$logfile"
29 rm -rf "$TPM_PATH"
30 }
31
32 trap "cleanup" EXIT
33
34 source "${TESTDIR}/common"
35 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
36
37 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
38
39 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
40 --log "file=$logfile,level=20" \
41 --tpm2
42
43 display_processes_by_name "swtpm"
44
45 if ! kill_quiet -0 "${SWTPM_PID}"; then
46 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
47 echo "TPM Logfile:"
48 cat "$logfile"
49 exit 1
50 fi
51
52 # Init the TPM
53 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
54 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
55 echo "TPM Logfile:"
56 cat "$logfile"
57 exit 1
58 fi
59
60 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
61 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
62 echo "TPM Logfile:"
63 cat "$logfile"
64 exit 1
65 fi
66
67 # Startup the TPM
68 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
69 exp=' 80 01 00 00 00 0a 00 00 00 00'
70 if [ "$RES" != "$exp" ]; then
71 echo "Error: Did not get expected result from TPM2_Startup(ST_Clear)"
72 echo "expected: $exp"
73 echo "received: $RES"
74 exit 1
75 fi
76
77 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then
78 echo "Error: Hash command did not work."
79 echo "TPM Logfile:"
80 cat "$logfile"
81 exit 1
82 fi
83
84 # Read PCR 17
85 # length CC count hashalg sz
86 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')
87 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'
88 if [ "$RES" != "$exp" ]; then
89 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
90 echo "expected: $exp"
91 echo "received: $RES"
92 exit 1
93 fi
94
95 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then
96 echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
97 echo "TPM Logfile:"
98 cat "$logfile"
99 exit 1
100 fi
101 if [ ! -r "$MY_PERMANENT_STATE_FILE" ]; then
102 echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
103 echo "TPM Logfile:"
104 cat "$logfile"
105 exit 1
106 fi
107 echo "Saved permanent state."
108
109 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then
110 echo "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE."
111 echo "TPM Logfile:"
112 cat "$logfile"
113 exit 1
114 fi
115 if [ ! -r "$MY_VOLATILE_STATE_FILE" ]; then
116 echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
117 echo "TPM Logfile:"
118 cat "$logfile"
119 exit 1
120 fi
121 echo "Saved volatile state."
122
123 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
124 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
125
126 # we will use our own volatile state
127 rm -f "$VOLATILE_STATE_FILE" "$STATE_FILE"
128
129 # Stop the TPM; this will not shut it down
130 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop; then
131 echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM."
132 echo "TPM Logfile:"
133 cat "$logfile"
134 exit 1
135 fi
136
137 if ! kill_quiet -0 "${SWTPM_PID}"; then
138 echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
139 echo "TPM Logfile:"
140 cat "$logfile"
141 exit 1
142 fi
143
144 # load state into the TPM
145 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "$MY_PERMANENT_STATE_FILE"; then
146 echo "Could not load permanent state into vTPM"
147 echo "TPM Logfile:"
148 cat "$logfile"
149 exit 1
150 fi
151 echo "Loaded permanent state."
152
153 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then
154 echo "Could not load volatile state into vTPM"
155 echo "TPM Logfile:"
156 cat "$logfile"
157 exit 1
158 fi
159 echo "Loaded volatile state."
160
161 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
162 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
163
164 # Init the TPM
165 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
166 echo "TPM Init failed."
167 echo "TPM Logfile:"
168 cat "$logfile"
169 exit 1
170 fi
171
172 # Volatile state must have been removed by TPM now
173 if [ -r "$VOLATILE_STATE_FILE" ]; then
174 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
175 echo "TPM Logfile:"
176 cat "$logfile"
177 exit 1
178 fi
179
180 # Read the PCR again ...
181 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')
182 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'
183 if [ "$RES" != "$exp" ]; then
184 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
185 echo "expected: $exp"
186 echo "received: $RES"
187 exit 1
188 fi
189
190 # Save the volatile state again
191 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
192 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
193 echo "TPM Logfile:"
194 cat "$logfile"
195 exit 1
196 fi
197
198 if [ ! -r "$VOLATILE_STATE_FILE" ]; then
199 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
200 echo "TPM Logfile:"
201 cat "$logfile"
202 exit 1
203 fi
204
205 # Send a new TPM_Init
206 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
207 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
208 echo "TPM Logfile:"
209 cat "$logfile"
210 exit 1
211 fi
212
213 # Volatile state must have been removed by TPM now
214 if [ -r "$VOLATILE_STATE_FILE" ]; then
215 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
216 echo "TPM Logfile:"
217 cat "$logfile"
218 exit 1
219 fi
220
221 # Read the PCR again ...
222 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')
223 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'
224 if [ "$RES" != "$exp" ]; then
225 echo "Error: (3) Did not get expected result from TPM_PCRRead(17)"
226 echo "expected: $exp"
227 echo "received: $RES"
228 exit 1
229 fi
230
231 # Final shut down
232 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
233 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
234 echo "TPM Logfile:"
235 cat "$logfile"
236 exit 1
237 fi
238
239 if wait_process_gone "${SWTPM_PID}" 4; then
240 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
241 echo "TPM Logfile:"
242 cat "$logfile"
243 exit 1
244 fi
245
246 if [ ! -e "$STATE_FILE" ]; then
247 echo "Error: TPM state file $STATE_FILE does not exist."
248 echo "TPM Logfile:"
249 cat "$logfile"
250 exit 1
251 fi
252
253 echo "OK"
254
255 exit 0