Debian Codegraph @

tests/_test_save_load_state

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(pwdscip-shell system . . pwd.)/..}
7 TESTDIR=${abs_top_testdir:-$(dirnamescip-shell system . . dirname. "$0")}
8
9 VTPM_NAME="${VTPM_NAME:-vtpm-test-save-load-state}"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktempscip-shell system . . 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 [scip-shell system . . `[`. "${SWTPM_TEST_LINEAR_FILE:-0}" -ne 0 ]; then
24 BACKEND_PARAM="--tpmstate backend-uri=file://$LINEAR_STATE_FILE"
25 fi
26
27 logfile="$(mktempscip-shell system . . mktemp.)" || exit 1
28
29 function cleanupscip-shell shell swtpm 0.10.1+ds-1 cleanup().()
30 {
31 pid=${SWTPM_PID}
32 if [scip-shell system . . `[`. -n "$pid" ]; then
33 kill_quiet -9 "$pid"
34 fi
35 rmscip-shell system . . rm. -f "$logfile"
36 rmscip-shell system . . rm. -rf "$TPM_PATH"
37 }
38
39 trap "cleanup" EXIT
40
41 source "${TESTDIR}/common"
42 [scip-shell system . . `[`. "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse"
43
44 rmscip-shell system . . rm. -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/nullscip-shell filesystem . . `/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 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM did not start."
54 echoscip-shell system . . echo. "TPM Logfile:"
55 catscip-shell system . . cat. "$logfile"
56 exit 1
57 fi
58
59 # Init the TPM
60 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
61 echoscip-shell system . . echo. "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
62 echoscip-shell system . . echo. "TPM Logfile:"
63 catscip-shell system . . cat. "$logfile"
64 exit 1
65 fi
66
67 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
68 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
69 echoscip-shell system . . echo. "TPM Logfile:"
70 catscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then
78 echoscip-shell system . . echo. "Error: Did not get expected result from TPM_Startup(ST_Clear)"
79 echoscip-shell system . . echo. "expected: $exp"
80 echoscip-shell system . . echo. "received: $RES"
81 exit 1
82 fi
83
84 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then
85 echoscip-shell system . . echo. "Error: Hash command did not work."
86 echoscip-shell system . . echo. "TPM Logfile:"
87 catscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then
95 echoscip-shell system . . echo. "Error: (1) Did not get expected result from TPM_PCRRead(17)"
96 echoscip-shell system . . echo. "expected: $exp"
97 echoscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then
105 echoscip-shell system . . echo. "Error: (1) Did not get expected result from TSC_PhysicalPresence(ENABLE)"
106 echoscip-shell system . . echo. "expected: $exp"
107 echoscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then
122 echoscip-shell system . . echo. "Error: (1) Did not get expected result from TPM_NVDefineSpace()"
123 echoscip-shell system . . echo. "expected: $exp"
124 echoscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then
132 echoscip-shell system . . echo. "Error: (1) Did not get expected result from TPM_SaveState()"
133 echoscip-shell system . . echo. "expected: $exp"
134 echoscip-shell system . . echo. "received: $RES"
135 exit 1
136 fi
137
138 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then
139 echoscip-shell system . . echo. "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
140 echoscip-shell system . . echo. "TPM Logfile:"
141 catscip-shell system . . cat. "$logfile"
142 exit 1
143 fi
144 if [scip-shell system . . `[`. ! -r "$MY_PERMANENT_STATE_FILE" ]; then
145 echoscip-shell system . . echo. "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
146 echoscip-shell system . . echo. "TPM Logfile:"
147 catscip-shell system . . cat. "$logfile"
148 exit 1
149 fi
150 echoscip-shell system . . echo. "Saved permanent state."
151
152 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then
153 echoscip-shell system . . echo. "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE."
154 echoscip-shell system . . echo. "TPM Logfile:"
155 catscip-shell system . . cat. "$logfile"
156 exit 1
157 fi
158 if [scip-shell system . . `[`. ! -r "$MY_VOLATILE_STATE_FILE" ]; then
159 echoscip-shell system . . echo. "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
160 echoscip-shell system . . echo. "TPM Logfile:"
161 catscip-shell system . . cat. "$logfile"
162 exit 1
163 fi
164 echoscip-shell system . . echo. "Saved volatile state."
165
166 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save savestate "$MY_SAVESTATE_STATE_FILE"; then
167 echoscip-shell system . . echo. "Error: Could not write savestate state file $MY_SAVESTATE_STATE_FILE."
168 echoscip-shell system . . echo. "TPM Logfile:"
169 catscip-shell system . . cat. "$logfile"
170 exit 1
171 fi
172 if [scip-shell system . . `[`. ! -r "$MY_SAVESTATE_STATE_FILE" ]; then
173 echoscip-shell system . . echo. "Error: Savestate state file $MY_SAVESTATE_STATE_FILE does not exist."
174 echoscip-shell system . . echo. "TPM Logfile:"
175 catscip-shell system . . cat. "$logfile"
176 exit 1
177 fi
178 echoscip-shell system . . 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 rmscip-shell system . . 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 echoscip-shell system . . echo. "Error: Could not stop the ${SWTPM_INTERFACE} TPM."
190 echoscip-shell system . . echo. "TPM Logfile:"
191 catscip-shell system . . cat. "$logfile"
192 exit 1
193 fi
194
195 if ! kill_quiet -0 "${SWTPM_PID}"; then
196 echoscip-shell system . . echo. "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
197 echoscip-shell system . . echo. "TPM Logfile:"
198 catscip-shell system . . 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 echoscip-shell system . . echo. "Could not load permanent state into vTPM"
205 echoscip-shell system . . echo. "TPM Logfile:"
206 catscip-shell system . . cat. "$logfile"
207 exit 1
208 fi
209 echoscip-shell system . . echo. "Loaded permanent state."
210
211 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then
212 echoscip-shell system . . echo. "Could not load volatile state into vTPM"
213 echoscip-shell system . . echo. "TPM Logfile:"
214 catscip-shell system . . cat. "$logfile"
215 exit 1
216 fi
217 echoscip-shell system . . echo. "Loaded volatile state."
218
219 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load savestate "$MY_SAVESTATE_STATE_FILE"; then
220 echoscip-shell system . . echo. "Could not load savestate state into vTPM"
221 echoscip-shell system . . echo. "TPM Logfile:"
222 catscip-shell system . . cat. "$logfile"
223 exit 1
224 fi
225 echoscip-shell system . . 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 echoscip-shell system . . echo. "TPM Init failed."
233 echoscip-shell system . . echo. "TPM Logfile:"
234 catscip-shell system . . cat. "$logfile"
235 exit 1
236 fi
237
238 # Volatile state must have been removed by TPM now
239 if [scip-shell system . . `[`. -r "$VOLATILE_STATE_FILE" ]; then
240 echoscip-shell system . . echo. "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
241 echoscip-shell system . . echo. "TPM Logfile:"
242 catscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then
250 echoscip-shell system . . echo. "Error: (2) Did not get expected result from TPM_PCRRead(17)"
251 echoscip-shell system . . echo. "expected: $exp"
252 echoscip-shell system . . 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 echoscip-shell system . . echo. "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file."
259 echoscip-shell system . . echo. "TPM Logfile:"
260 catscip-shell system . . cat. "$logfile"
261 exit 1
262 fi
263
264 if [scip-shell system . . `[`. "${SWTPM_TEST_LINEAR_FILE:-0}" -eq 0 ] && [scip-shell system . . `[`. ! -r "$VOLATILE_STATE_FILE" ]; then
265 echoscip-shell system . . echo. "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
266 echoscip-shell system . . echo. "TPM Logfile:"
267 catscip-shell system . . cat. "$logfile"
268 exit 1
269 fi
270
271 # Send a new TPM_Init
272 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
273 echoscip-shell system . . echo. "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
274 echoscip-shell system . . echo. "TPM Logfile:"
275 catscip-shell system . . cat. "$logfile"
276 exit 1
277 fi
278
279 # Volatile state must have been removed by TPM now
280 if [scip-shell system . . `[`. -r "$VOLATILE_STATE_FILE" ]; then
281 echoscip-shell system . . echo. "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
282 echoscip-shell system . . echo. "TPM Logfile:"
283 catscip-shell system . . 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 [scip-shell system . . `[`. "$RES" != "$exp" ]; then
291 echoscip-shell system . . echo. "Error: (2) Did not get expected result from TPM_PCRRead(17)"
292 echoscip-shell system . . echo. "expected: $exp"
293 echoscip-shell system . . 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 echoscip-shell system . . echo. "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
301 echoscip-shell system . . echo. "TPM Logfile:"
302 catscip-shell system . . cat. "$logfile"
303 exit 1
304 fi
305
306 if wait_process_gone "${SWTPM_PID}" 4; then
307 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
308 echoscip-shell system . . echo. "TPM Logfile:"
309 catscip-shell system . . cat. "$logfile"
310 exit 1
311 fi
312
313 if [scip-shell system . . `[`. "${SWTPM_TEST_LINEAR_FILE:-0}" -ne 0 ]; then
314 if [scip-shell system . . `[`. ! -e "$LINEAR_STATE_FILE" ]; then
315 echoscip-shell system . . echo. "Error: TPM state file $LINEAR_STATE_FILE does not exist."
316 echoscip-shell system . . echo. "TPM Logfile:"
317 catscip-shell system . . cat. "$logfile"
318 exit 1
319 fi
320 else
321 if [scip-shell system . . `[`. ! -e "$STATE_FILE" ]; then
322 echoscip-shell system . . echo. "Error: TPM state file $STATE_FILE does not exist."
323 echoscip-shell system . . echo. "TPM Logfile:"
324 catscip-shell system . . cat. "$logfile"
325 exit 1
326 fi
327 fi
328
329 echoscip-shell system . . echo. "OK"
330
331 exit 0