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