]> git.proxmox.com Git - swtpm.git/blame - tests/_test_tpm2_resume_volatile
swtpm: Disable fsync on file & dir due to TPM timeouts (issue #597)
[swtpm.git] / tests / _test_tpm2_resume_volatile
CommitLineData
c88b36f0
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
c88b36f0
SB
9VTPM_NAME=${vtpm-test-tpm2-resume-volatile:-VTPM_NAME}
10SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
12
13tpmstatedir="$(mktemp -d)"
14if [ -z "$tpmstatedir" ]; then
15 echo "Could not create temporary directory"
16 exit 1
17fi
18
19SWTPM_CMD_UNIX_PATH=${tpmstatedir}/unix-cmd.sock
20SWTPM_CTRL_UNIX_PATH=${tpmstatedir}/unix-ctrl.sock
21
22function cleanup()
23{
24 pid=${SWTPM_PID}
25 if [ -n "$pid" ]; then
47c7ea77 26 kill_quiet -9 $pid
c88b36f0
SB
27 fi
28 rm -rf $tpmstatedir
29}
30
31trap "cleanup" EXIT
32
313cf75c
SB
33[ "${SWTPM_INTERFACE}" == "cuse" ] && source ${TESTDIR}/test_cuse
34source ${TESTDIR}/common
c88b36f0
SB
35
36export TPM_PATH=$tpmstatedir
37
38# copy all the state files
313cf75c 39cp ${TESTDIR}/data/tpm2state1/* ${TPM_PATH}
c88b36f0
SB
40
41run_swtpm ${SWTPM_INTERFACE} --tpm2
42
100317d5 43display_processes_by_name "$SWTPM"
c88b36f0 44
47c7ea77 45kill_quiet -0 ${SWTPM_PID}
c88b36f0
SB
46if [ $? -ne 0 ]; then
47 echo "Error: CUSE TPM did not start."
48 exit 1
49fi
50
51# Init the TPM
52run_swtpm_ioctl ${SWTPM_INTERFACE} -i
53if [ $? -ne 0 ]; then
54 echo "Error: Could not initialize the CUSE TPM."
55 exit 1
56fi
57
47c7ea77 58kill_quiet -0 ${SWTPM_PID} 2>/dev/null
c88b36f0
SB
59if [ $? -ne 0 ]; then
60 echo "Error: CUSE TPM not running anymore after INIT."
61 exit 1
62fi
63
64swtpm_open_cmddev ${SWTPM_INTERFACE} 100
65
66# Read PCR 10 (from pcrextend -ha 10 -ic test)
67RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
68exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
69if [ "$RES" != "$exp" ]; then
70 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
71 echo "expected: $exp"
72 echo "received: $RES"
73 exit 1
74fi
75
76# Save the volatile state again
77run_swtpm_ioctl ${SWTPM_INTERFACE} -v
78if [ $? -ne 0 ]; then
79 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
80 exit 1
81fi
82if [ ! -r $VOLATILE_STATE_FILE ]; then
83 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
84 exit 1
85fi
86
87# Shut the TPM down
88exec 100>&-
89run_swtpm_ioctl ${SWTPM_INTERFACE} -s
90
91echo "Test 1: Ok"
92
93# 2nd test: with encrypted state
94# copy all the state files
313cf75c 95cp ${TESTDIR}/data/tpm2state2/* ${TPM_PATH}
c88b36f0
SB
96
97run_swtpm ${SWTPM_INTERFACE} \
98 --tpm2 \
a39f098f 99 --key pwdfile=${TESTDIR}/data/tpm2state2/pwdfile.txt,kdf=sha512
c88b36f0 100
100317d5 101display_processes_by_name "$SWTPM"
c88b36f0 102
47c7ea77 103kill_quiet -0 ${SWTPM_PID}
c88b36f0
SB
104if [ $? -ne 0 ]; then
105 echo "Error: CUSE TPM did not start."
106 exit 1
107fi
108
109# Init the TPM
110run_swtpm_ioctl ${SWTPM_INTERFACE} -i
111if [ $? -ne 0 ]; then
112 echo "Error: CUSE TPM initialization failed."
113 exit 1
114fi
115
47c7ea77 116kill_quiet -0 ${SWTPM_PID} 2>/dev/null
c88b36f0
SB
117if [ $? -ne 0 ]; then
118 echo "Error: CUSE TPM not running anymore after INIT."
119 exit 1
120fi
121
122swtpm_open_cmddev ${SWTPM_INTERFACE} 100
123if [ $? -ne 0 ]; then
124 echo "Error: Could not open command interface."
125 ext 1
126fi
127
128# Read PCR 10 (from pcrextend -ha 10 -ic test)
129RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
130exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
131if [ "$RES" != "$exp" ]; then
132 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
133 echo "expected: $exp"
134 echo "received: $RES"
135 exit 1
136fi
137
138# Save the volatile state again
139run_swtpm_ioctl ${SWTPM_INTERFACE} -v
140if [ $? -ne 0 ]; then
141 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
142 exit 1
143fi
144if [ ! -r $VOLATILE_STATE_FILE ]; then
145 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
146 exit 1
147fi
148
149# Shut the TPM down
150exec 100>&-
151run_swtpm_ioctl ${SWTPM_INTERFACE} -s
152if [ $? -ne 0 ]; then
153 echo "Error: Could not shut down the CUSE TPM."
154 exit 1
155fi
156
157echo "Test 2: Ok"
71d9581a
SB
158
159# 3rd test: with encrypted state using aes-256-cbc
160# copy all the state files
161cp ${TESTDIR}/data/tpm2state2b/* ${TPM_PATH}
162
163run_swtpm ${SWTPM_INTERFACE} \
164 --tpm2 \
165 --key pwdfile=${TESTDIR}/data/tpm2state2b/pwdfile.txt,mode=aes-256-cbc
166
100317d5 167display_processes_by_name "$SWTPM"
71d9581a
SB
168
169kill_quiet -0 ${SWTPM_PID}
170if [ $? -ne 0 ]; then
171 echo "Error: CUSE TPM did not start."
172 exit 1
173fi
174
175# Init the TPM
176run_swtpm_ioctl ${SWTPM_INTERFACE} -i
177if [ $? -ne 0 ]; then
178 echo "Error: CUSE TPM initialization failed."
179 exit 1
180fi
181
182kill_quiet -0 ${SWTPM_PID} 2>/dev/null
183if [ $? -ne 0 ]; then
184 echo "Error: CUSE TPM not running anymore after INIT."
185 exit 1
186fi
187
188swtpm_open_cmddev ${SWTPM_INTERFACE} 100
189if [ $? -ne 0 ]; then
190 echo "Error: Could not open command interface."
191 ext 1
192fi
193
194# Read PCR 10 (from pcrextend -ha 10 -ic test)
195RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b\x03\x00\x04\x00')
196exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 16 00 00 00 01 00 0b 03 00 04 00 00 00 00 01 00 20 f6 85 98 e5 86 8d e6 8b 97 29 99 60 f2 71 7d 17 67 89 a4 2f 9a ae a8 c7 b7 aa 79 a8 62 56 c1 de'
197if [ "$RES" != "$exp" ]; then
198 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
199 echo "expected: $exp"
200 echo "received: $RES"
201 exit 1
202fi
203
204# Save the volatile state again
205run_swtpm_ioctl ${SWTPM_INTERFACE} -v
206if [ $? -ne 0 ]; then
207 echo "Error: Could not have the CUSE TPM write the volatile state to a file."
208 exit 1
209fi
210if [ ! -r $VOLATILE_STATE_FILE ]; then
211 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
212 exit 1
213fi
214
215# Shut the TPM down
216exec 100>&-
217run_swtpm_ioctl ${SWTPM_INTERFACE} -s
218if [ $? -ne 0 ]; then
219 echo "Error: Could not shut down the CUSE TPM."
220 exit 1
221fi
222
223echo "Test 3: Ok"