Debian Codegraph @

tests/_test_tpm2_save_load_encrypted_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-test-tpm2-save-load-encrypted-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 KEY=1234567890abcdef1234567890abcdef
15 MY_VOLATILE_STATE_FILE=$TPM_PATH/my.volatilestate
16 MY_PERMANENT_STATE_FILE=$TPM_PATH/my.permanent
17 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
18 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
19 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
20
21 keyfile="$(mktemp)" || exit 1
22 logfile="$(mktemp)" || exit 1
23 echo "$KEY" > "$keyfile"
24
25 function cleanup()
26 {
27 pid=${SWTPM_PID}
28 if [ -n "$pid" ]; then
29 kill_quiet -9 "$pid"
30 fi
31 rm -f "$keyfile" "$logfile"
32 rm -rf "$TPM_PATH"
33 }
34
35 trap "cleanup" EXIT
36
37 source "${TESTDIR}/common"
38 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
39
40 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
41
42 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
43 --key "file=$keyfile,mode=aes-cbc,format=hex" \
44 --log "file=$logfile" \
45 --tpm2
46
47 display_processes_by_name "swtpm"
48
49 if ! kill_quiet -0 "${SWTPM_PID}"; then
50 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
51 echo "TPM Logfile:"
52 cat "$logfile"
53 exit 1
54 fi
55
56 # Init the TPM
57 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
58 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
59 echo "TPM Logfile:"
60 cat "$logfile"
61 exit 1
62 fi
63
64 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
65 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
66 echo "TPM Logfile:"
67 cat "$logfile"
68 exit 1
69 fi
70
71 # Startup the TPM (SU_CLEAR)
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(SU_Clear)"
76 echo "expected: $exp"
77 echo "received: $RES"
78 exit 1
79 fi
80
81 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then
82 echo "Error: Could not hash the data."
83 echo "TPM Logfile:"
84 cat "$logfile"
85 exit 1
86 fi
87
88 # Read PCR 17
89 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')
90 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'
91 if [ "$RES" != "$exp" ]; then
92 echo "Error: (1) Did not get expected result from TPM2_PCRRead(17)"
93 echo "expected: $exp"
94 echo "received: $RES"
95 exit 1
96 fi
97
98 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then
99 echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
100 echo "TPM Logfile:"
101 cat "$logfile"
102 exit 1
103 fi
104 if [ ! -r "$MY_PERMANENT_STATE_FILE" ]; then
105 echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
106 echo "TPM Logfile:"
107 cat "$logfile"
108 exit 1
109 fi
110 echo "Saved permanent state."
111
112 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then
113 echo "Error: Could not write volatile state file $MY_PERMANENT_STATE_FILE."
114 echo "TPM Logfile:"
115 cat "$logfile"
116 exit 1
117 fi
118 if [ ! -r "$MY_VOLATILE_STATE_FILE" ]; then
119 echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
120 echo "TPM Logfile:"
121 cat "$logfile"
122 exit 1
123 fi
124 echo "Saved volatile state."
125
126 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
127 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
128
129 # we will use our own volatile state
130 rm -f "$VOLATILE_STATE_FILE" "$STATE_FILE"
131
132 # Stop the TPM; this will not shut it down
133 run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop
134
135 if ! kill_quiet -0 "${SWTPM_PID}"; then
136 echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
137 echo "TPM Logfile:"
138 cat "$logfile"
139 exit 1
140 fi
141
142 # load state into the TPM
143 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "$MY_PERMANENT_STATE_FILE"; then
144 echo "Could not load permanent state into vTPM"
145 echo "TPM Logfile:"
146 cat "$logfile"
147 exit 1
148 fi
149 echo "Loaded permanent state."
150
151 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then
152 echo "Could not load volatile state into vTPM"
153 echo "TPM Logfile:"
154 cat "$logfile"
155 exit 1
156 fi
157 echo "Loaded volatile state."
158
159 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
160 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
161
162 # Init the TPM
163 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
164 echo "TPM Init failed."
165 echo "TPM Logfile:"
166 cat "$logfile"
167 exit 1
168 fi
169
170 # Volatile state must have been removed by TPM now
171 if [ -r "$VOLATILE_STATE_FILE" ]; then
172 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
173 echo "TPM Logfile:"
174 cat "$logfile"
175 exit 1
176 fi
177
178 # Read the PCR again ...
179 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')
180 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'
181 if [ "$RES" != "$exp" ]; then
182 echo "Error: (2) Did not get expected result from TPM2_PCRRead(17)"
183 echo "expected: $exp"
184 echo "received: $RES"
185 exit 1
186 fi
187
188 # Save the volatile state again
189 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -v; then
190 echo "Error: Could not have the ${SWTPM_INTERFACE} TPM write the volatile state to a file."
191 echo "TPM Logfile:"
192 cat "$logfile"
193 exit 1
194 fi
195 if [ ! -r "$VOLATILE_STATE_FILE" ]; then
196 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
197 echo "TPM Logfile:"
198 cat "$logfile"
199 exit 1
200 fi
201
202 # Send a new TPM_Init
203 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
204 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
205 echo "TPM Logfile:"
206 cat "$logfile"
207 exit 1
208 fi
209
210 # Volatile state must have been removed by TPM now
211 if [ -r "$VOLATILE_STATE_FILE" ]; then
212 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
213 echo "TPM Logfile:"
214 cat "$logfile"
215 exit 1
216 fi
217
218 # Read the PCR again ...
219 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')
220 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'
221 if [ "$RES" != "$exp" ]; then
222 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
223 echo "expected: $exp"
224 echo "received: $RES"
225 exit 1
226 fi
227
228 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
229 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
230 echo "TPM Logfile:"
231 cat "$logfile"
232 exit 1
233 fi
234
235 if wait_process_gone "${SWTPM_PID}" 4; then
236 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
237 echo "TPM Logfile:"
238 cat "$logfile"
239 exit 1
240 fi
241
242 echo "Test 1: Ok"
243
244 # This time start we start the TPM with a wrong state encryption key
245 # (key used as password) and try to start it. It has to fail and
246 # the state must not have been modified.
247
248 # volatile state file does not exist
249 sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}")
250 sha1_permanent=$(get_sha1_file "${STATE_FILE}")
251 echo "sha1(volatile) : $sha1_volatile"
252 echo "sha1(permanent): $sha1_permanent"
253
254 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
255 --key "pwdfile=$keyfile" \
256 --log "file=$logfile" \
257 --tpm2
258
259 display_processes_by_name "swtpm"
260
261 if ! kill_quiet -0 "${SWTPM_PID}"; then
262 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
263 echo "TPM Logfile:"
264 cat "$logfile"
265 exit 1
266 fi
267
268 # Init the TPM
269 if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -i 2>&1)"; then
270 echo "Error: ${SWTPM_INTERFACE} TPM initialization should have failed."
271 echo "TPM Logfile:"
272 cat "$logfile"
273 exit 1
274 fi
275 exp="TPM result from PTM_INIT: 0x101"
276 if [ "$ERR" != "$exp" ]; then
277 echo "Error: Unexpected error message"
278 echo "Received: $ERR"
279 echo "Expected: $exp"
280 exit 1
281 fi
282
283 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
284 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
285 echo "TPM Logfile:"
286 cat "$logfile"
287 exit 1
288 fi
289
290 if [ "${sha1_volatile}" != "$(get_sha1_file "${VOLATILE_STATE_FILE}")" ]; then
291 echo "Error: Volatile state file was modified during failed init."
292 exit 1
293 fi
294
295 if [ "${sha1_permanent}" != "$(get_sha1_file "${STATE_FILE}")" ]; then
296 echo "Error: Permanent state file was modified during failed init."
297 exit 1
298 fi
299
300 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
301 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
302 echo "TPM Logfile:"
303 cat "$logfile"
304 exit 1
305 fi
306
307 if wait_process_gone "${SWTPM_PID}" 4; then
308 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
309 echo "TPM Logfile:"
310 cat "$logfile"
311 exit 1
312 fi
313
314 # shut it down
315
316 echo "Test 2: Ok"
317
318 # This time start we start the TPM with a wrong state encryption key
319 # (key used as password) and try to start it. It has to fail and
320 # the state must not have been modified.
321
322 # volatile state file does not exist
323 sha1_volatile=$(get_sha1_file "${VOLATILE_STATE_FILE}")
324 sha1_permanent=$(get_sha1_file "${STATE_FILE}")
325 echo "sha1(volatile) : $sha1_volatile"
326 echo "sha1(permanent): $sha1_permanent"
327
328 # we need a 256bit key
329 echo "${KEY}${KEY}" > "$keyfile"
330
331 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
332 --key "pwdfile=$keyfile,mode=aes-256-cbc" \
333 --log "file=$logfile" \
334 --tpm2
335
336 display_processes_by_name "swtpm"
337
338 if ! kill_quiet -0 "${SWTPM_PID}"; then
339 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
340 echo "TPM Logfile:"
341 cat "$logfile"
342 exit 1
343 fi
344
345 # Init the TPM
346 if run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
347 echo "Error: ${SWTPM_INTERFACE} TPM initialization should have failed."
348 echo "TPM Logfile:"
349 cat "$logfile"
350 exit 1
351 fi
352
353 if ! wait_process_gone "${SWTPM_PID}" 4; then
354 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after failed INIT."
355 echo "TPM Logfile:"
356 cat "$logfile"
357 exit 1
358 fi
359
360 if [ "${sha1_volatile}" != "$(get_sha1_file "${VOLATILE_STATE_FILE}")" ]; then
361 echo "Error: Volatile state file was modified during failed init."
362 exit 1
363 fi
364
365 if [ "${sha1_permanent}" != "$(get_sha1_file "${STATE_FILE}")" ]; then
366 echo "Error: Permanent state file was modified during failed init."
367 exit 1
368 fi
369
370 echo "Test 3: Ok"
371
372 # Final shut down
373 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
374 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
375 echo "TPM Logfile:"
376 cat "$logfile"
377 exit 1
378 fi
379
380 if wait_process_gone "${SWTPM_PID}" 4; then
381 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
382 echo "TPM Logfile:"
383 cat "$logfile"
384 exit 1
385 fi
386
387 if [ ! -e "$STATE_FILE" ]; then
388 echo "Error: TPM state file $STATE_FILE does not exist."
389 echo "TPM Logfile:"
390 cat "$logfile"
391 exit 1
392 fi
393
394 echo "OK"
395
396 exit 0