Debian Codegraph @

tests/_test_tpm2_hashing2

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-hashing2"
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 # Init the TPM
44 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
45 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
46 exit 1
47 fi
48
49 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
50 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
51 exit 1
52 fi
53
54 # Startup the TPM
55 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
56 exp=' 80 01 00 00 00 0a 00 00 00 00'
57 if [ "$RES" != "$exp" ]; then
58 echo "Error: Did not get expected result from TPM2_Startup(SU_Clear)"
59 echo "expected: $exp"
60 echo "received: $RES"
61 exit 1
62 fi
63
64 # Check the TPM Established bit before the hashing
65 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then
66 echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM."
67 exit 1
68 fi
69
70 exp='tpmEstablished is 0'
71 if [ "$RES" != "$exp" ]; then
72 echo "Error (1): TPM Established flag has wrong value."
73 echo "expected: $exp"
74 echo "received: $RES"
75 exit 1
76 fi
77
78 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then
79 echo "Error: The hash command failed."
80 exit 1
81 fi
82
83 # Read PCR 17
84 # length CC count hashalg sz
85 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')
86 # disregard the update counter using a regex comparison
87 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'
88 if ! [[ $RES =~ ^${exp}$ ]]; then
89 echo "Error: (1) Did not get expected result from TPM_PCRRead(17)"
90 echo "expected: $exp"
91 echo "received: $RES"
92 exit 1
93 fi
94
95 # Check the TPM Established bit after the hashing
96 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then
97 echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM."
98 exit 1
99 fi
100
101 exp='tpmEstablished is 1'
102 if [ "$RES" != "$exp" ]; then
103 echo "Error (2): TPM Established flag has wrong value."
104 echo "expected: $exp"
105 echo "received: $RES"
106 exit 1
107 fi
108
109 # Reset the establishment bit; we switch to locality 0 and reset via locality 3
110 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -l 0; then
111 echo "Error: Could not set locality 0"
112 exit 1
113 fi
114
115 for ((l = 0; l <= 2; l++)); do
116 # Resetting via locality 2 must fail
117 if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -r "$l" 2>&1)"; then
118 echo "Error: Could reset the establishment bit via locality $l"
119 exit 1
120 fi
121 exp="TPM result from PTM_RESET_TPMESTABLISHED: 0x3d"
122 if [ "$ERR" != "$exp" ]; then
123 echo "Error: Unexpected error message"
124 echo "Received: $ERR"
125 echo "Expected: $exp"
126 exit 1
127 fi
128 done
129
130 # Resetting via locality 3 must work
131 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -r 3; then
132 echo "Error: Could not reset the establishment bit via locality 3"
133 exit 1
134 fi
135
136 # Check the TPM Established bit after the reset
137 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then
138 echo "Error: Could not get the TPM Established bit from the ${SWTPM_INTERFACE} TPM."
139 exit 1
140 fi
141
142 exp='tpmEstablished is 0'
143 if [ "$RES" != "$exp" ]; then
144 echo "Error (3): TPM Established flag has wrong value."
145 echo "expected: $exp"
146 echo "received: $RES"
147 exit 1
148 fi
149
150 # Read from a file
151 dd if=/dev/zero bs=1024 count=1024 2>/dev/null| \
152 run_swtpm_ioctl "${SWTPM_INTERFACE}" -h -
153
154 # Read PCR 17
155 # length CC count hashalg sz
156 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')
157 # disregard the update counter using a regex comparison
158 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'
159 if ! [[ $RES =~ ^${exp}$ ]]; then
160 echo "Error: (2) Did not get expected result from TPM_PCRRead(17)"
161 echo "expected: $exp"
162 echo "received: $RES"
163 exit 1
164 fi
165
166 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
167 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
168 exit 1
169 fi
170
171 sleep 0.5
172
173 if kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
174 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
175 exit 1
176 fi
177
178 if [ ! -e "$STATE_FILE" ]; then
179 echo "Error: TPM state file $STATE_FILE does not exist."
180 exit 1
181 fi
182
183 echo "OK"
184
185 exit 0