]> git.proxmox.com Git - swtpm.git/blob - tests/_test_encrypted_state
tests: Enable running tests in out-of-source builds
[swtpm.git] / tests / _test_encrypted_state
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-test-encrypted-state"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 export TPM_PATH=$(mktemp -d)
12 STATE_FILE=$TPM_PATH/tpm-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14 KEY=1234567890abcdef1234567890abcdef
15 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
18
19 keyfile=$(mktemp)
20 logfile=$(mktemp)
21 echo "$KEY" > $keyfile
22
23 function cleanup()
24 {
25 pid=${SWTPM_PID}
26 if [ -n "$pid" ]; then
27 kill -9 $pid
28 fi
29 rm -f $keyfile $logfile
30 rm -rf $TPM_PATH
31 }
32
33 trap "cleanup" EXIT
34
35 [ "${SWTPM_INTERFACE}" == cuse ] && source ${TESTDIR}/test_cuse
36 source ${TESTDIR}/common
37
38 rm -f $STATE_FILE $VOLATILE_STATE_FILE 2>/dev/null
39
40 run_swtpm ${SWTPM_INTERFACE} \
41 --key file=$keyfile,mode=aes-cbc,format=hex \
42 --log file=$logfile
43
44 ps aux | grep $SWTPM | grep -v grep
45
46 kill -0 ${SWTPM_PID}
47 if [ $? -ne 0 ]; then
48 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
49 echo "TPM Logfile:"
50 cat $logfile
51 exit 1
52 fi
53
54 # Init the TPM
55 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
56 if [ $? -ne 0 ]; then
57 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
58 echo "TPM Logfile:"
59 cat $logfile
60 exit 1
61 fi
62
63 kill -0 ${SWTPM_PID} 2>/dev/null
64 if [ $? -ne 0 ]; 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
72 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
73 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
74 exp=' 00 c4 00 00 00 0a 00 00 00 00'
75 if [ "$RES" != "$exp" ]; then
76 echo "Error: Did not get expected result from TPM_Startup(ST_Clear)"
77 echo "expected: $exp"
78 echo "received: $RES"
79 exit 1
80 fi
81
82 run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234
83 if [ $? -ne 0 ]; then
84 echo "Error: Hash command did not work."
85 echo "TPM Logfile:"
86 cat $logfile
87 exit 1
88 fi
89
90 # Read PCR 17
91 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
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 # Save the volatile state
102 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
103 if [ $? -ne 0 ]; then
104 echo "Error: Saving the volatile state failed."
105 echo "TPM Logfile:"
106 cat $logfile
107 exit 1
108 fi
109
110 if [ ! -r $VOLATILE_STATE_FILE ]; then
111 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
112 echo "TPM Logfile:"
113 cat $logfile
114 exit 1
115 fi
116
117 tmp=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -g | cut -d":" -f2)
118 if [ $? -ne 0 ]; then
119 echo "Error: Could not get the configration flags of the ${SWTPM_INTERFACE} TPM."
120 echo "TPM Logfile:"
121 cat $logfile
122 exit 1
123 fi
124
125 if [ "$tmp" != " 0x1" ]; then
126 echo "Error: Unexpected configuration flags: $tmp; expected 0x1."
127 echo "TPM Logfile:"
128 cat $logfile
129 exit 1
130 fi
131
132 # Shut the TPM down
133 exec 100>&-
134 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
135 if [ $? -ne 0 ]; then
136 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
137 echo "TPM Logfile:"
138 cat $logfile
139 exit 1
140 fi
141
142 # Start the TPM again; have the keyfile removed
143 run_swtpm ${SWTPM_INTERFACE} \
144 --key file=$keyfile,mode=aes-cbc,format=hex,remove \
145 --log file=$logfile
146
147 ps aux | grep $SWTPM | grep -v grep
148
149 kill -0 ${SWTPM_PID}
150 if [ $? -ne 0 ]; then
151 echo "Error (2): ${SWTPM_INTERFACE} TPM did not start."
152 echo "TPM Logfile:"
153 cat $logfile
154 exit 1
155 fi
156
157 if [ -r $keyfile ]; then
158 echo "Error: Keyfile $keyfile was not removed by swtpm_cuse."
159 echo "TPM Logfile:"
160 cat $logfile
161 exit 1
162 fi
163
164 # Init the TPM
165 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
166 if [ $? -ne 0 ]; then
167 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
168 echo "TPM Logfile:"
169 cat $logfile
170 exit 1
171 fi
172
173 # Volatile state must have been removed by TPM now
174 if [ -r $VOLATILE_STATE_FILE ]; then
175 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
176 echo "TPM Logfile:"
177 cat $logfile
178 exit 1
179 fi
180
181 # Read the PCR again ...
182 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
183 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
184 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'
185 if [ "$RES" != "$exp" ]; then
186 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
187 echo "expected: $exp"
188 echo "received: $RES"
189 exit 1
190 fi
191
192 # Save the volatile state again
193 run_swtpm_ioctl ${SWTPM_INTERFACE} -v
194 if [ $? -ne 0 ]; then
195 echo "Error: Saving the volatile state failed."
196 echo "TPM Logfile:"
197 cat $logfile
198 exit 1
199 fi
200
201 if [ ! -r $VOLATILE_STATE_FILE ]; then
202 echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist."
203 echo "TPM Logfile:"
204 cat $logfile
205 exit 1
206 fi
207
208 # Send a new TPM_Init
209 run_swtpm_ioctl ${SWTPM_INTERFACE} -i
210 if [ $? -ne 0 ]; then
211 echo "Error: ${SWTPM_INTERFACE} TPM initialization failed."
212 echo "TPM Logfile:"
213 cat $logfile
214 exit 1
215 fi
216
217 # Volatile state must have been removed by TPM now
218 if [ -r $VOLATILE_STATE_FILE ]; then
219 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
220 echo "TPM Logfile:"
221 cat $logfile
222 exit 1
223 fi
224
225 # Read the PCR again ...
226 swtpm_open_cmddev ${SWTPM_INTERFACE} 100
227 RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
228 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'
229 if [ "$RES" != "$exp" ]; then
230 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
231 echo "expected: $exp"
232 echo "received: $RES"
233 exit 1
234 fi
235
236
237 # Final shut down
238 exec 100>&-
239 run_swtpm_ioctl ${SWTPM_INTERFACE} -s
240 if [ $? -ne 0 ]; then
241 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
242 echo "TPM Logfile:"
243 cat $logfile
244 exit 1
245 fi
246
247 if wait_process_gone ${SWTPM_PID} 1; then
248 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
249 echo "TPM Logfile:"
250 cat $logfile
251 exit 1
252 fi
253
254 if [ ! -e $STATE_FILE ]; then
255 echo "Error: TPM state file $STATE_FILE does not exist."
256 echo "TPM Logfile:"
257 cat $logfile
258 exit 1
259 fi
260
261 echo "OK"
262
263 exit 0