Debian Codegraph @

tests/_test_hashing2

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(pwdscip-shell system . . pwd.)/..}
7 TESTDIR=${abs_top_testdir:-$(dirnamescip-shell system . . dirname. "$0")}
8
9 VTPM_NAME="vtpm-test-hashing2"
10 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
11 TPM_PATH="$(mktempscip-shell system . . mktemp. -d)" || exit 1
12 STATE_FILE=$TPM_PATH/tpm-00.permall
13 VOLATILE_STATE_FILE=$TPM_PATH/tpm-00.volatilestate
14 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
15 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
16 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
17
18 function cleanupscip-shell shell swtpm 0.10.1+ds-1 cleanup().()
19 {
20 pid=${SWTPM_PID}
21 if [scip-shell system . . `[`. -n "$pid" ]; then
22 kill_quiet -9 "$pid"
23 fi
24 rmscip-shell system . . rm. -rf "$TPM_PATH"
25 }
26
27 trap "cleanup" EXIT
28
29 source "${TESTDIR}/common"
30 [scip-shell system . . `[`. "${SWTPM_INTERFACE}" == cuse ] && source "${TESTDIR}/test_cuse"
31
32 rmscip-shell system . . rm. -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/nullscip-shell filesystem . . `/dev/null`.
33
34 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}"
35
36 display_processes_by_name "swtpm"
37
38 if ! kill_quiet -0 "${SWTPM_PID}"; then
39 echoscip-shell system . . 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 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM initialization failed."
46 exit 1
47 fi
48
49 sleepscip-shell system . . sleep. 0.5
50
51 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
52 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
53 exit 1
54 fi
55
56 # Startup the TPM
57 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01')
58 exp=' 00 c4 00 00 00 0a 00 00 00 00'
59 if [scip-shell system . . `[`. "$RES" != "$exp" ]; then
60 echoscip-shell system . . echo. "Error: Did not get expected result from TPM_Startup(ST_Clear)"
61 echoscip-shell system . . echo. "expected: $exp"
62 echoscip-shell system . . echo. "received: $RES"
63 exit 1
64 fi
65
66 # Check the TPM Established bit before the hashing
67 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then
68 echoscip-shell system . . echo. "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
69 exit 1
70 fi
71 exp='tpmEstablished is 0'
72 if [scip-shell system . . `[`. "$RES" != "$exp" ]; then
73 echoscip-shell system . . echo. "Error (1): TPM Established flag has wrong value."
74 echoscip-shell system . . echo. "expected: $exp"
75 echoscip-shell system . . echo. "received: $RES"
76 exit 1
77 fi
78
79 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -h 1234; then
80 echoscip-shell system . . echo. "Error: Hash command did not work."
81 exit 1
82 fi
83
84 # Read PCR 17
85 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
86 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'
87 if [scip-shell system . . `[`. "$RES" != "$exp" ]; then
88 echoscip-shell system . . echo. "Error: (1) Did not get expected result from TPM_PCRRead(17)"
89 echoscip-shell system . . echo. "expected: $exp"
90 echoscip-shell system . . echo. "received: $RES"
91 exit 1
92 fi
93
94 # Check the TPM Established bit after the hashing
95 if ! RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e); then
96 echoscip-shell system . . echo. "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM."
97 exit 1
98 fi
99 exp='tpmEstablished is 1'
100 if [scip-shell system . . `[`. "$RES" != "$exp" ]; then
101 echoscip-shell system . . echo. "Error (2): TPM Established flag has wrong value."
102 echoscip-shell system . . echo. "expected: $exp"
103 echoscip-shell system . . echo. "received: $RES"
104 exit 1
105 fi
106
107 # Reset the establishment bit; we switch to locality 0 and reset via locality 3
108 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -l 0; then
109 echoscip-shell system . . echo. "Error: Could not set locality 0"
110 exit 1
111 fi
112
113 for ((l = 0; l <= 2; l++)); do
114 # Resetting via locality 2 must fail
115 if ERR="$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -r "$l" 2>&1)"; then
116 echo "Error: Could reset the establishment bit via locality $l"
117 exit 1
118 fi
119 exp="TPM result from PTM_RESET_TPMESTABLISHED: 0x3d"
120 if [ "$ERR" != "$exp" ]; then
121 echo "Error: Unexpected error message"
122 echo "Received: $ERR"
123 echo "Expected: $exp"
124 exit 1
125 fi
126 done
127
128 # We expect the same results for the TPM_ResetEstablishment command
129 for ((l = 0; l <= 2; l++)); do
130 # Set locality
131 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -l "$l"; then
132 echo "Error: Could not choose locality $l"
133 exit 1
134 fi
135 # Have to use external echo command
136 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0a\x40\x00\x00\x0b')
137 exp=' 00 c4 00 00 00 0a 00 00 00 3d'
138 if [ "$RES" != "$exp" ]; then
139 echo "Error: Could reset TPM establishment bit in locality $l using command"
140 echo "expected: $exp"
141 echo "received: $RES"
142 exit 1
143 fi
144 done
145
146 # Resetting via locality 3 must work
147 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -l 3; then
148 echoscip-shell system . . echo. "Error: Could not reset the establishment bit via locality 3"
149 exit 1
150 fi
151
152 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0a\x40\x00\x00\x0b')
153 exp=' 00 c4 00 00 00 0a 00 00 00 00'
154 if [scip-shell system . . `[`. "$RES" != "$exp" ]; then
155 echoscip-shell system . . echo. "Error: Could reset TPM establishment bit in locality 3 using command"
156 echoscip-shell system . . echo. "expected: $exp"
157 echoscip-shell system . . echo. "received: $RES"
158 exit 1
159 fi
160
161 # Check the TPM Established bit after the reset
162 RES=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" -e)
163 exp='tpmEstablished is 0'
164 if [scip-shell system . . `[`. "$RES" != "$exp" ]; then
165 echoscip-shell system . . echo. "Error (3): TPM Established flag has wrong value."
166 echoscip-shell system . . echo. "expected: $exp"
167 echoscip-shell system . . echo. "received: $RES"
168 exit 1
169 fi
170
171 # Read from a file
172 ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. bs=1024 count=1024 2>/dev/nullscip-shell filesystem . . `/dev/null`. |\
173 run_swtpm_ioctl "${SWTPM_INTERFACE}" -h -
174
175 # Read PCR 17
176 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11')
177 exp=' 00 c4 00 00 00 1e 00 00 00 00 d8 0e 7a 7b 3c 37 88 7d b4 c2 88 08 1d a7 53 f6 4b 11 3a 9c'
178 if [scip-shell system . . `[`. "$RES" != "$exp" ]; then
179 echoscip-shell system . . echo. "Error: (2) Did not get expected result from TPM_PCRRead(17)"
180 echoscip-shell system . . echo. "expected: $exp"
181 echoscip-shell system . . echo. "received: $RES"
182 exit 1
183 fi
184
185 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
186 echoscip-shell system . . echo. "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
187 exit 1
188 fi
189
190 if wait_process_gone "${SWTPM_PID}" 4; then
191 echoscip-shell system . . echo. "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
192 exit 1
193 fi
194
195 if [scip-shell system . . `[`. ! -e "$STATE_FILE" ]; then
196 echoscip-shell system . . echo. "Error: TPM state file $STATE_FILE does not exist."
197 exit 1
198 fi
199
200 echoscip-shell system . . echo. "OK"
201
202 exit 0