Debian Codegraph @

tests/_test_tpm2_hashing

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-hashing"
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
19 function cleanup()
20 {
21 pid=${SWTPM_PID}
22 if [ -n "$pid" ]; then
23 kill_quiet -9 "$pid"
24 fi
25 rm -rf "$TPM_PATH"
26 }
27
28 trap "cleanup" EXIT
29
30 source "${TESTDIR}/common"
31 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
32
33 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
34
35 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" --tpm2
36
37 display_processes_by_name "swtpm"
38
39 if ! kill_quiet -0 "${SWTPM_PID}"; then
40 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
41 exit 1
42 fi
43
44 # Init the TPM
45 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
46 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
47 exit 1
48 fi
49
50 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
51 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
52 exit 1
53 fi
54
55 # Startup the TPM2
56 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
57 exp=' 80 01 00 00 00 0a 00 00 00 00'
58 if [ "$RES" != "$exp" ]; then
59 echo "Error: Did not get expected result from TPM2_Startup(SU_Clear)"
60 echo "expected: $exp"
61 echo "received: $RES"
62 exit 1
63 fi
64
65 # Check the TPM Established bit before the hashing
66 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e ); then
67 echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM."
68 exit 1
69 fi
70
71 exp='tpmEstablished is 0'
72 if [ "$RES" != "$exp" ]; then
73 echo "Error (1): TPM Established flag has wrong value."
74 echo "expected: $exp"
75 echo "received: $RES"
76 exit 1
77 fi
78
79 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then
80 echo "Error: The hash command failed."
81 exit 1
82 fi
83
84 # Read PCR 17
85 # length CC count hashalg sz
86 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')
87 # disregard the update counter using a regex comparison
88 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 .. 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'
89 if ! [[ $RES =~ ^${exp}$ ]]; then
90 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
91 echo "expected: $exp"
92 echo "received: $RES"
93 exit 1
94 fi
95
96 # Check the TPM Established bit after the hashing
97 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then
98 echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM."
99 exit 1
100 fi
101
102 exp='tpmEstablished is 1'
103 if [ "$RES" != "$exp" ]; then
104 echo "Error (2): TPM Established flag has wrong value."
105 echo "expected: $exp"
106 echo "received: $RES"
107 exit 1
108 fi
109
110 # Reset the establishment bit is not possible via a command
111 # So we do it directly here in locality 3
112 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -r 3; then
113 echo "Error: Could not reset establishment bit in locality 3"
114 exit 1
115 fi
116
117 # Check the TPM Established bit after the reset
118 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then
119 echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM."
120 exit 1
121 fi
122
123 exp='tpmEstablished is 0'
124 if [ "$RES" != "$exp" ]; then
125 echo "Error (3): TPM Established flag has wrong value."
126 echo "expected: $exp"
127 echo "received: $RES"
128 exit 1
129 fi
130
131 # back to locality 0
132 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -l 0; then
133 echo "Error: Could not set the locality to 0."
134 exit 1
135 fi
136 # Read from a file
137 dd if=/dev/zero bs=1024 count=1024 2>/dev/null| \
138 run_swtpm_ioctl "${SWTPM_INTERFACE}" -h -
139
140 # Read PCR 17
141 # length CC count hashalg sz
142 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')
143 # disregard the update counter using a regex comparison
144 exp=' 80 01 00 00 00 3e 00 00 00 00 00 00 00 .. 00 00 00 01 00 0b 03 00 00 02 00 00 00 01 00 20 27 7e de f1 02 56 46 5d 8e 71 65 38 3f d3 63 c9 8a be 89 e2 90 2e 4d 3a 2b 3a 30 80 2f 28 af 19'
145 if ! [[ $RES =~ ^${exp}$ ]]; then
146 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
147 echo "expected: $exp"
148 echo "received: $RES"
149 exit 1
150 fi
151
152 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
153 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
154 exit 1
155 fi
156
157 if wait_process_gone "${SWTPM_PID}" 4; then
158 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
159 exit 1
160 fi
161
162 if [ ! -e "$STATE_FILE" ]; then
163 echo "Error: TPM state file $STATE_FILE does not exist."
164 exit 1
165 fi
166
167 echo "OK"
168
169 exit 0