Debian Codegraph @

tests/_test_tpm2_probe

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-test-tpm2-probe"
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 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
15 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
16 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
17
18 function cleanup()
19 {
20 pid=${SWTPM_PID}
21 if [ -n "$pid" ]; then
22 kill_quiet -9 "$pid"
23 fi
24 rm -rf "$TPM_PATH"
25 }
26
27 trap "cleanup" EXIT
28
29 source "${TESTDIR}/common"
30 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
31
32 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
33
34 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2
35
36 display_processes_by_name "swtpm"
37
38 if ! kill_quiet -0 "${SWTPM_PID}"; then
39 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
40 exit 1
41 fi
42
43 if [ "${SWTPM_INTERFACE}" != "cuse" ]; then
44 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop; then
45 echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM"
46 exit 1
47 fi
48 fi
49
50 # Before TPM_INIT: Read PCR 17 -- this gives a fatal error
51 # length CC count hashalg sz
52 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')
53 exp=' 80 01 00 00 00 0a 00 00 01 01'
54 if [ "$RES" != "$exp" ]; then
55 echo "Error: Before TPM_INIT: Did not get expected result from TPM_PCRRead(17)"
56 echo "expected: $exp"
57 echo "received: $RES"
58 exit 1
59 fi
60
61 # Init the TPM
62 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
63 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
64 exit 1
65 fi
66
67 # Read PCR 17 -- this should give TPM_INVALID_POSTINIT
68 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')
69 exp=' 80 01 00 00 00 0a 00 00 01 00'
70 if [ "$RES" != "$exp" ]; then
71 echo "Error: Did not get expected result from TPM_PCRRead(17)"
72 echo "expected: $exp"
73 echo "received: $RES"
74 exit 1
75 fi
76
77 if ! check_seccomp_profile "${SWTPM_EXE}" "${SWTPM_PID}" 2; then
78 exit 1
79 fi
80
81 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
82 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
83 exit 1
84 fi
85
86 if wait_process_gone "${SWTPM_PID}" 4; then
87 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
88 exit 1
89 fi
90
91 echo "OK"
92
93 exit 0