Debian Codegraph @

tests/_test_save_load_state

raw symbols SCIP
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-test-save-load-state}"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktemp -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14 MY_VOLATILE_STATE_FILE=$TPM_PATH/my.volatilestate
15 MY_PERMANENT_STATE_FILE=$TPM_PATH/my.permanent
16 MY_SAVESTATE_STATE_FILE=$TPM_PATH/my.savestate
17 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
18 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
19 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
20 LINEAR_STATE_FILE=$TPM_PATH/linear-state
21 BACKEND_PARAM=""
22
23 if [ "${SWTPM_TEST_LINEAR_FILE:-0}" -ne 0 ]; then
24 BACKEND_PARAM="--tpmstate backend-uri=file://$LINEAR_STATE_FILE"
25 fi
26
27 logfile="$(mktemp)" || exit 1
28
29 function cleanup()
30 {
31 pid=${SWTPM_PID}
32 if [ -n "$pid" ]; then
33 kill_quiet -9 "$pid"
34 fi
35 rm -f "$logfile"
36 rm -rf "$TPM_PATH"
37 }
38
39 trap "cleanup" EXIT
40
41 source "${TESTDIR}/common"
42 [ "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse"
43
44 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
45
46 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
47 ${BACKEND_PARAM:+${BACKEND_PARAM}} \
48 --log "file=$logfile"
49
50 display_processes_by_name "swtpm"
51
52 if ! kill_quiet -0 "${SWTPM_PID}"; then
53 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
54 echo "TPM Logfile:"
55 cat "$logfile"
56 exit 1
57 fi
58
59 # Init the TPM
60 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
61 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
62 echo "TPM Logfile:"
63 cat "$logfile"
64 exit 1
65 fi
66
67 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
68 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
69 echo "TPM Logfile:"
70 cat "$logfile"
71 exit 1
72 fi
73
74 # Startup the TPM
75 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
76 exp=' 00 c4 00 00 00 0a 00 00 00 00'
77 if [ "$RES" != "$exp" ]; then
78 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
79 echo "expected: $exp"
80 echo "received: $RES"
81 exit 1
82 fi
83
84 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then
85 echo "Error: Hash command did not work."
86 echo "TPM Logfile:"
87 cat "$logfile"
88 exit 1
89 fi
90
91 # Read PCR 17
92 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
93 exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
94 if [ "$RES" != "$exp" ]; then
95 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
96 echo "expected: $exp"
97 echo "received: $RES"
98 exit 1
99 fi
100
101 # Assert physical presence
102 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x40\x00\x00\x0A\x00\x20')
103 exp=' 00 c4 00 00 00 0a 00 00 00 00'
104 if [ "$RES" != "$exp" ]; then
105 echo "Error: (1) Did not get expected result from TSC_PhysicalPresence(ENABLE)"
106 echo "expected: $exp"
107 echo "received: $RES"
108 exit 1
109 fi
110
111 # Create a big NVRAM Area with 4000 bytes (0xfa0)
112 tmp='\x00\xC1\x00\x00\x00\x65\x00\x00\x00\xcc\x00\x18\x00\x00\x00\x01'
113 tmp+='\x00\x03\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
114 tmp+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01'
115 tmp+='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
116 tmp+='\x00\x00\x00\x00\x00\x17\x00\x01\x00\x01\x00\x00\x00\x00\x00\x0f'
117 tmp+='\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
118 tmp+='\x00\x00\x00\x00\x00'
119 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" $tmp)
120 exp=' 00 c4 00 00 00 0a 00 00 00 00'
121 if [ "$RES" != "$exp" ]; then
122 echo "Error: (1) Did not get expected result from TPM_NVDefineSpace()"
123 echo "expected: $exp"
124 echo "received: $RES"
125 exit 1
126 fi
127
128 # Send SaveState command
129 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0a\x00\x00\x00\x98')
130 exp=' 00 c4 00 00 00 0a 00 00 00 00'
131 if [ "$RES" != "$exp" ]; then
132 echo "Error: (1) Did not get expected result from TPM_SaveState()"
133 echo "expected: $exp"
134 echo "received: $RES"
135 exit 1
136 fi
137
138 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then
139 echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
140 echo "TPM Logfile:"
141 cat "$logfile"
142 exit 1
143 fi
144 if [ ! -r "$MY_PERMANENT_STATE_FILE" ]; then
145 echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
146 echo "TPM Logfile:"
147 cat "$logfile"
148 exit 1
149 fi
150 echo "Saved permanent state."
151
152 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then
153 echo "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE."
154 echo "TPM Logfile:"
155 cat "$logfile"
156 exit 1
157 fi
158 if [ ! -r "$MY_VOLATILE_STATE_FILE" ]; then
159 echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
160 echo "TPM Logfile:"
161 cat "$logfile"
162 exit 1
163 fi
164 echo "Saved volatile state."
165
166 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save savestate "$MY_SAVESTATE_STATE_FILE"; then
167 echo "Error: Could not write savestate state file $MY_SAVESTATE_STATE_FILE."
168 echo "TPM Logfile:"
169 cat "$logfile"
170 exit 1
171 fi
172 if [ ! -r "$MY_SAVESTATE_STATE_FILE" ]; then
173 echo "Error: Savestate state file $MY_SAVESTATE_STATE_FILE does not exist."
174 echo "TPM Logfile:"
175 cat "$logfile"
176 exit 1
177 fi
178 echo "Saved savestate state."
179
180 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
181 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
182
183 # we will use our own volatile state
184 rm -f "$VOLATILE_STATE_FILE" "$STATE_FILE"
185
186 # Stop the TPM; this will not shut it down
187 exec 100>&-
188 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop; then
189 echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM."
190 echo "TPM Logfile:"
191 cat "$logfile"
192 exit 1
193 fi
194
195 if ! kill_quiet -0 "${SWTPM_PID}"; then
196 echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
197 echo "TPM Logfile:"
198 cat "$logfile"
199 exit 1
200 fi
201
202 # load state into the TPM
203 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "$MY_PERMANENT_STATE_FILE"; then
204 echo "Could not load permanent state into vTPM"
205 echo "TPM Logfile:"
206 cat "$logfile"
207 exit 1
208 fi
209 echo "Loaded permanent state."
210
211 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then
212 echo "Could not load volatile state into vTPM"
213 echo "TPM Logfile:"
214 cat "$logfile"
215 exit 1
216 fi
217 echo "Loaded volatile state."
218
219 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load savestate "$MY_SAVESTATE_STATE_FILE"; then
220 echo "Could not load savestate state into vTPM"
221 echo "TPM Logfile:"
222 cat "$logfile"
223 exit 1
224 fi
225 echo "Loaded savestate state."
226
227 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
228 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
229
230 # Init the TPM
231 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
232 echo "TPM Init failed."
233 echo "TPM Logfile:"
234 cat "$logfile"
235 exit 1
236 fi
237
238 # Volatile state must have been removed by TPM now
239 if [ -r "$VOLATILE_STATE_FILE" ]; then
240 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
241 echo "TPM Logfile:"
242 cat "$logfile"
243 exit 1
244 fi
245
246 # Read the PCR again ...
247 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
248 exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
249 if [ "$RES" != "$exp" ]; then
250 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
251 echo "expected: $exp"
252 echo "received: $RES"
253 exit 1
254 fi
255
256 # Save the volatile state again
257 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
258 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
259 echo "TPM Logfile:"
260 cat "$logfile"
261 exit 1
262 fi
263
264 if [ "${SWTPM_TEST_LINEAR_FILE:-0}" -eq 0 ] && [ ! -r "$VOLATILE_STATE_FILE" ]; then
265 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
266 echo "TPM Logfile:"
267 cat "$logfile"
268 exit 1
269 fi
270
271 # Send a new TPM_Init
272 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
273 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
274 echo "TPM Logfile:"
275 cat "$logfile"
276 exit 1
277 fi
278
279 # Volatile state must have been removed by TPM now
280 if [ -r "$VOLATILE_STATE_FILE" ]; then
281 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
282 echo "TPM Logfile:"
283 cat "$logfile"
284 exit 1
285 fi
286
287 # Read the PCR again ...
288 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
289 exp=' 00 c4 00 00 00 1e 00 00 00 00 97 e9 76 e4 f2 2c d6 d2 4a fd 21 20 85 ad 7a 86 64 7f 2a e5'
290 if [ "$RES" != "$exp" ]; then
291 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
292 echo "expected: $exp"
293 echo "received: $RES"
294 exit 1
295 fi
296
297 # Final shut down
298 exec 100>&-
299 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
300 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
301 echo "TPM Logfile:"
302 cat "$logfile"
303 exit 1
304 fi
305
306 if wait_process_gone "${SWTPM_PID}" 4; then
307 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
308 echo "TPM Logfile:"
309 cat "$logfile"
310 exit 1
311 fi
312
313 if [ "${SWTPM_TEST_LINEAR_FILE:-0}" -ne 0 ]; then
314 if [ ! -e "$LINEAR_STATE_FILE" ]; then
315 echo "Error: TPM state file $LINEAR_STATE_FILE does not exist."
316 echo "TPM Logfile:"
317 cat "$logfile"
318 exit 1
319 fi
320 else
321 if [ ! -e "$STATE_FILE" ]; then
322 echo "Error: TPM state file $STATE_FILE does not exist."
323 echo "TPM Logfile:"
324 cat "$logfile"
325 exit 1
326 fi
327 fi
328
329 echo "OK"
330
331 exit 0