Debian Codegraph @

tests/test_ctrlchannel2

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4
5 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
6 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
7
8 TPMDIR="$(mktemp -d)" || exit 1
9 PID_FILE=$TPMDIR/swtpm.pid
10 SOCK_PATH=$TPMDIR/sock
11 VOLATILESTATE=$TPMDIR/volatile
12
13 source "${TESTDIR}/common"
14 skip_test_no_chardev "${SWTPM_EXE}"
15 skip_test_no_tpm12 "${SWTPM_EXE}"
16
17 trap "cleanup" SIGTERM EXIT
18
19 function cleanup()
20 {
21 rm -rf "$TPMDIR"
22 if [ -n "$PID" ]; then
23 kill_quiet -SIGTERM "$PID" 2>/dev/null
24 fi
25 }
26
27 # Test 1: test the control channel on the chardev tpm
28
29 # use a pseudo terminal
30 exec 100<>/dev/ptmx
31 $SWTPM_EXE chardev \
32 --fd 100 \
33 --tpmstate "dir=$TPMDIR" \
34 --pid "file=$PID_FILE" \
35 --ctrl "type=unixio,path=$SOCK_PATH" \
36 --daemon \
37 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}}
38
39 if [ ! -f "$PID_FILE" ]; then
40 echo "Error: Chardev TPM did not write pidfile."
41 exit 1
42 fi
43
44 PID=$(cat "$PID_FILE")
45
46 # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
47 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -c 2>&1); then
48 echo "Error: $SWTPM_IOCTL CMD_GET_CAPABILITY failed: $act"
49 exit 1
50 fi
51
52 exp="ptm capability is 0x([[:xdigit:]]+)"
53 if ! [[ "$act" =~ ^${exp}$ ]]; then
54 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
55 exit 1
56 fi
57
58 # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags
59 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then
60 echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act"
61 exit 1
62 fi
63
64 # Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a
65 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -v 2>&1); then
66 echo "Error: $SWTPM_IOCTL CMD_STORE_VOLATILE failed: $act"
67 exit 1
68 fi
69
70 if [ ! -r "$TPMDIR/tpm-00.volatilestate" ]; then
71 echo "Error: Socket TPM: Did not write volatile state file"
72 exit 1
73 fi
74
75 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
76 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --stop 2>&1); then
77 echo "Error: $SWTPM_IOCTL CMD_STOP failed: $act"
78 exit 1
79 fi
80
81 # Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f
82 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -g 2>&1); then
83 echo "Error: $SWTPM_IOCTL CMD_GET_CONFIG failed: $act"
84 exit 1
85 fi
86
87 exp="ptm configuration flags: 0x([[:xdigit:]]+)"
88 if ! [[ "$act" =~ ^${exp}$ ]]; then
89 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
90 exit 1
91 fi
92
93 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
94 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
95 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
96 exit 1
97 fi
98
99 if wait_file_gone "$PID_FILE" 2; then
100 echo "Error: TPM should have removed PID file by now."
101 exit 1
102 fi
103
104 if wait_process_gone "${PID}" 4; then
105 echo "Error: TPM should not be running anymore."
106 exit 1
107 fi
108
109 echo "OK"
110
111 # Test 2: test the control channel on the socket tpm
112
113 # There are a few more tests here that require sending commands to the TPM
114
115 BINDADDR="127.0.0.1"
116 case $(uname -s) in
117 Linux*)
118 # make sure IPv6 is available
119 ip -6 addr show lo | grep -q " ::1/128" && BINDADDR="::1"
120 ;;
121 esac
122
123 # use a pseudo terminal
124 $SWTPM_EXE socket \
125 --server port=65431,disconnect=true,bindaddr=$BINDADDR \
126 --tpmstate "dir=$TPMDIR" \
127 --pid "file=$PID_FILE" \
128 --ctrl "type=unixio,path=$SOCK_PATH" \
129 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
130 PID=$!
131
132 if wait_for_file "$PID_FILE" 3; then
133 echo "Error: Socket TPM did not write pidfile."
134 exit 1
135 fi
136
137 validate_pidfile "$PID" "$PID_FILE"
138
139 # Get the capability bits: CMD_GET_CAPABILITY = 0x00 00 00 01
140 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -c 2>&1); then
141 echo "Error: $SWTPM_IOCTL CMD_GET_CAPABILITY failed: $act"
142 exit 1
143 fi
144
145 exp="ptm capability is 0x([[:xdigit:]]+)"
146 if ! [[ "$act" =~ ^${exp}$ ]]; then
147 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
148 exit 1
149 fi
150
151 # Send TPM_Init to the TPM: CMD_INIT = 0x00 00 00 02 + flags
152 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then
153 echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act"
154 exit 1
155 fi
156
157 # Startup the TPM
158 if ! msg=$($SWTPM_BIOS --tcp ${BINDADDR}:65431 -o 2>&1); then
159 echo "Error: Failed to startup TPM: $msg"
160 exit 1
161 fi
162
163 # Save the volatile state: CMD_STORE_VOLATILE = 0x00 00 00 0a
164 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -v 2>&1); then
165 echo "Error: $SWTPM_IOCTL CMD_STORE_VOLATILE failed: $act"
166 exit 1
167 fi
168
169 if [ ! -r "$TPMDIR/tpm-00.volatilestate" ]; then
170 echo "Error: Socket TPM: Did not write volatile state file"
171 exit 1
172 fi
173
174 # 1. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
175 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -e 2>&1); then
176 echo "Error: $SWTPM_IOCTL CMD_GET_TPMESTABLISHED failed: $act"
177 exit 1
178 fi
179
180 exp="tpmEstablished is 0"
181 if [ "$act" != "$exp" ]; then
182 echo "Error: Expected '$exp' but got '$act'."
183 exit 1
184 fi
185
186 # 2. Hash the given data
187 data="a"
188 while [ ${#data} -lt $((0x2000)) ]; do
189 data="${data}${data}"
190 done
191 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -h $data 2>&1); then
192 echo "Error: $SWTPM_IOCTL data hashing failed: $act"
193 exit 1
194 fi
195
196 # 3. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
197 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -e 2>&1); then
198 echo "Error: $SWTPM_IOCTL CMD_GET_TPMESTABLISHED failed: $act"
199 exit 1
200 fi
201
202 exp="tpmEstablished is 1"
203 if [ "$act" != "$exp" ]; then
204 echo "Error: Expected '$exp' but got '$act'."
205 exit 1
206 fi
207
208 # 4. Send command to reset TPM established flag: CMD_RESET_TPMESTABLISHED = 00 00 00 0b 03
209 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -r 3 2>&1); then
210 echo "Error: $SWTPM_IOCTL CMD_RESET_TPMESTABLISHED failed: $act"
211 exit 1
212 fi
213
214 # 5. Send command to get TPM established flag: CMD_GET_TPMESTABLISHED = 00 00 00 04
215 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -e 2>&1); then
216 echo "Error: $SWTPM_IOCTL CMD_GET_TPMESTABLISHED failed: $act"
217 exit 1
218 fi
219
220 exp="tpmEstablished is 0"
221 if [ "$act" != "$exp" ]; then
222 echo "Error: Expected '$exp' but got '$act'."
223 exit 1
224 fi
225
226 # Read PCR 17
227 exec 100<>/dev/tcp/${BINDADDR}/65431
228 echo -en '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11' >&100
229 RES=$(cat <&100 | od -t x1 -A n | tr -d "\n")
230 exp=' 00 c4 00 00 00 1e 00 00 00 00 f9 87 3e 96 6b 9e 46 c8 45 46 2d 1f f2 52 eb cc c1 9b df fd'
231 if [ "$RES" != "$exp" ]; then
232 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
233 echo "expected: $exp"
234 echo "received: $RES"
235 exit 1
236 fi
237
238 # Get the volatile state of the TPM: CMD_GET_STATEBLOB = 00 00 00 0c
239 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --save volatile "$VOLATILESTATE" 2>&1); then
240 echo "Error: $SWTPM_IOCTL CMD_GET_STATEBLOB failed: $act"
241 exit 1
242 fi
243
244 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
245 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --stop 2>&1); then
246 echo "Error: $SWTPM_IOCTL CMD_STOP failed: $act"
247 exit 1
248 fi
249
250 # Read PCR 17 -- should fail now
251 exec 100<>/dev/tcp/${BINDADDR}/65431
252 echo -en '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11' >&100
253 RES=$(cat <&100 | od -t x1 -A n | tr -d "\n")
254 exp=' 00 c4 00 00 00 0a 00 00 00 09'
255 if [ "$RES" != "$exp" ]; then
256 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
257 echo "expected: $exp"
258 echo "received: $RES"
259 exit 1
260 fi
261
262 # Send get config command to the TPM: CMD_GET_CONFIG = 00 00 00 0f
263 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -g 2>&1); then
264 echo "Error: $SWTPM_IOCTL CMD_GET_CONFIG failed: $act"
265 exit 1
266 fi
267
268 exp="ptm configuration flags: 0x([[:xdigit:]]+)"
269 if ! [[ "$act" =~ ^${exp}$ ]]; then
270 echo "Error: Expected string following regular expression '$exp' from ioctl tool but got '$act'."
271 exit 1
272 fi
273
274 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
275 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
276 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
277 exit 1
278 fi
279
280 if wait_file_gone "$PID_FILE" 2; then
281 echo "Error: TPM should have removed PID file by now."
282 exit 1
283 fi
284
285 if wait_process_gone "${PID}" 4; then
286 echo "Error: Socket TPM should not be running anymore."
287 exit 1
288 fi
289
290 echo "OK"
291
292 # Test 3: test the control channel on the socket tpm: resume encrypted state
293
294 # copy all the state files
295 cp "${TESTDIR}/data/tpmstate2/"* "${TPMDIR}"
296
297 $SWTPM_EXE socket \
298 --server port=65431,disconnect=true \
299 --tpmstate "dir=$TPMDIR" \
300 --pid "file=$PID_FILE" \
301 --ctrl "type=unixio,path=$SOCK_PATH" \
302 --key "pwdfile=${TESTDIR}/data/tpmstate2/pwdfile.txt,kdf=sha512" \
303 --flags not-need-init \
304 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
305 PID=$!
306
307 if wait_for_file "$PID_FILE" 3; then
308 echo "Error: Socket TPM did not write pidfile."
309 exit 1
310 fi
311
312 validate_pidfile "$PID" "$PID_FILE"
313
314 # Read PCR 10
315 exec 100<>/dev/tcp/localhost/65431
316 echo -en '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a' >&100
317 RES=$(cat <&100 | od -t x1 -A n -w128)
318 exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
319 if [ "$RES" != "$exp" ]; then
320 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
321 echo "expected: $exp"
322 echo "received: $RES"
323 exit 1
324 fi
325
326 # Get the volatile state of the TPM: CMD_GET_STATEBLOB = 00 00 00 0c
327 rm -f "$VOLATILESTATE"
328 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --save volatile "$VOLATILESTATE" 2>&1); then
329 echo "Error: $SWTPM_IOCTL CMD_GET_STATEBLOB failed: $act"
330 exit 1
331 fi
332
333 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
334 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
335 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
336 exit 1
337 fi
338
339 if wait_file_gone "$PID_FILE" 2; then
340 echo "Error: TPM should have removed PID file by now."
341 exit 1
342 fi
343
344 if wait_process_gone "${PID}" 4; then
345 echo "Error: Socket TPM should not be running anymore."
346 exit 1
347 fi
348
349 # remove volatile state
350 rm -f "$TPMDIR"/*.volatilestate
351
352 $SWTPM_EXE socket \
353 --server port=65431,disconnect=true \
354 --tpmstate "dir=$TPMDIR" \
355 --pid "file=$PID_FILE" \
356 --ctrl "type=unixio,path=$SOCK_PATH" \
357 --key "pwdfile=${TESTDIR}/data/tpmstate2/pwdfile.txt,kdf=sha512" \
358 --flags not-need-init \
359 ${SWTPM_TEST_SECCOMP_OPT:+${SWTPM_TEST_SECCOMP_OPT}} &
360 PID=$!
361
362 if wait_for_file "$PID_FILE" 3; then
363 echo "Error: Socket TPM did not write pidfile."
364 exit 1
365 fi
366
367 validate_pidfile "$PID" "$PID_FILE"
368
369 # Read PCR 10 -- this should fail now
370 exec 100<>/dev/tcp/localhost/65431
371 echo -en '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a' >&100
372 RES=$(cat <&100 | od -t x1 -A n -w128)
373 exp=' 00 c4 00 00 00 0a 00 00 00 26'
374 if [ "$RES" != "$exp" ]; then
375 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
376 echo "expected: $exp"
377 echo "received: $RES"
378 exit 1
379 fi
380
381 # Send stop command to the TPM: CMD_STOP = 00 00 00 0e
382 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --stop 2>&1); then
383 echo "Error: $SWTPM_IOCTL CMD_STOP failed: $act"
384 exit 1
385 fi
386
387 # Send the volatile state to the TPM (while it is stopped)
388 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" --load volatile "$VOLATILESTATE" 2>&1); then
389 echo "Error: $SWTPM_IOCTL CMD_SET_STATEBLOB failed: $act"
390 exit 1
391 fi
392
393 # Send init command to the TPM: CMD_INIT = 00 00 00 02
394 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -i 2>&1); then
395 echo "Error: $SWTPM_IOCTL CMD_INIT failed: $act"
396 exit 1
397 fi
398
399 # Read PCR 10 -- has to return same result as before
400 exec 100<>/dev/tcp/localhost/65431
401 echo -en '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x0a' >&100
402 RES=$(cat <&100 | od -t x1 -A n -w128)
403 exp=' 00 c4 00 00 00 1e 00 00 00 00 c7 8a 6e 94 c7 3c 4d 7f c3 05 c8 a6 6b bf 15 45 f4 ed b7 a5'
404 if [ "$RES" != "$exp" ]; then
405 echo "Error: (1) Did not get expected result from TPM_PCRRead(10)"
406 echo "expected: $exp"
407 echo "received: $RES"
408 exit 1
409 fi
410
411
412 # Reset PCR 20 while in locality 0 -- should not work
413 exec 100<>/dev/tcp/localhost/65431
414 echo -en '\x00\xC1\x00\x00\x00\x0F\x00\x00\x00\xC8\x00\x03\x00\x00\x10' >&100
415 RES=$(cat <&100 | od -t x1 -A n)
416 exp=' 00 c4 00 00 00 0a 00 00 00 33'
417 if [ "$RES" != "$exp" ]; then
418 echo "Error: Trying to reset PCR 20 in locality 0 returned unexpected result"
419 echo "expected: $exp"
420 echo "received: $RES"
421 exit 1
422 fi
423
424 # In locality 2 we can reset PCR 20
425 # Set the locality on the TPM: CMD_SET_LOCALITY = 00 00 00 05 <locality>
426 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -l 2 2>&1); then
427 echo "Error: $SWTPM_IOCTL CMD_SET_LOCALITY failed: $act"
428 exit 1
429 fi
430
431 # Reset PCR 20 while in locality 2 -- has to work
432 exec 100<>/dev/tcp/localhost/65431
433 echo -en '\x00\xC1\x00\x00\x00\x0F\x00\x00\x00\xC8\x00\x03\x00\x00\x10' >&100
434 RES=$(cat <&100 | od -t x1 -A n)
435 exp=' 00 c4 00 00 00 0a 00 00 00 00'
436 if [ "$RES" != "$exp" ]; then
437 echo "Error: Could not reset PCR 20 in locality 2"
438 echo "expected: $exp"
439 echo "received: $RES"
440 exit 1
441 fi
442
443 # Send shutdown command to the TPM: CMD_SHUTDOWN = 00 00 00 03
444 if ! act=$($SWTPM_IOCTL --unix "$SOCK_PATH" -s 2>&1); then
445 echo "Error: $SWTPM_IOCTL CMD_SHUTDOWN failed: $act"
446 exit 1
447 fi
448
449 if wait_file_gone "$PID_FILE" 2; then
450 echo "Error: TPM should have removed PID file by now."
451 exit 1
452 fi
453
454 if wait_process_gone "${PID}" 4; then
455 echo "Error: Socket TPM should not be running anymore."
456 exit 1
457 fi
458
459 echo "OK"
460
461 exit 0