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