]>
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-volatilestate" |
10 | SWTPM_DEV_NAME="/dev/${VTPM_NAME}" | |
11 | export TPM_PATH=$(mktemp -d) | |
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 | |
22 | kill -9 $pid | |
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 | ||
36 | ps aux | grep $SWTPM | grep -v grep | |
37 | ||
38 | kill -0 ${SWTPM_PID} | |
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: Could not initialize the ${SWTPM_INTERFACE} TPM." | |
48 | exit 1 | |
49 | fi | |
50 | ||
51 | kill -0 ${SWTPM_PID} 2>/dev/null | |
52 | if [ $? -ne 0 ]; then | |
53 | echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT." | |
54 | exit 1 | |
55 | fi | |
56 | ||
57 | # Startup the TPM | |
58 | swtpm_open_cmddev ${SWTPM_INTERFACE} 100 | |
59 | RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0C\x00\x00\x00\x99\x00\x01') | |
60 | exp=' 00 c4 00 00 00 0a 00 00 00 00' | |
61 | if [ "$RES" != "$exp" ]; then | |
62 | echo "Error: Did not get expected result from TPM_Startup(ST_Clear)" | |
63 | echo "expected: $exp" | |
64 | echo "received: $RES" | |
65 | exit 1 | |
66 | fi | |
67 | ||
68 | run_swtpm_ioctl ${SWTPM_INTERFACE} -h 1234 | |
69 | if [ $? -ne 0 ]; then | |
70 | echo "Error: Hash command did not work." | |
71 | exit 1 | |
72 | fi | |
73 | ||
74 | # Read PCR 17 | |
75 | swtpm_open_cmddev ${SWTPM_INTERFACE} 100 | |
76 | RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') | |
77 | 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' | |
78 | if [ "$RES" != "$exp" ]; then | |
79 | echo "Error: (1) Did not get expected result from TPM_PCRRead(17)" | |
80 | echo "expected: $exp" | |
81 | echo "received: $RES" | |
82 | exit 1 | |
83 | fi | |
84 | ||
85 | # Check the TPM Established bit after the hashing | |
86 | RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e) | |
87 | exp='tpmEstablished is 1' | |
88 | if [ "$RES" != "$exp" ]; then | |
89 | echo "Error (2): TPM Established flag has wrong value." | |
90 | echo "expected: $exp" | |
91 | echo "received: $RES" | |
92 | exit 1 | |
93 | fi | |
94 | ||
95 | # Save the volatile state | |
96 | run_swtpm_ioctl ${SWTPM_INTERFACE} -v | |
97 | if [ $? -ne 0 ]; then | |
98 | echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file." | |
99 | exit 1 | |
100 | fi | |
101 | ||
102 | if [ ! -r $VOLATILE_STATE_FILE ]; then | |
103 | echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist." | |
104 | exit 1 | |
105 | fi | |
106 | ||
107 | # Shut the TPM down | |
108 | exec 100>&- | |
109 | run_swtpm_ioctl ${SWTPM_INTERFACE} -s | |
110 | if [ $? -ne 0 ]; then | |
111 | echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." | |
112 | exit 1 | |
113 | fi | |
114 | ||
115 | # Start the TPM again | |
116 | run_swtpm ${SWTPM_INTERFACE} | |
117 | ||
118 | ps aux | grep $SWTPM | grep -v grep | |
119 | ||
120 | kill -0 ${SWTPM_PID} | |
121 | if [ $? -ne 0 ]; then | |
122 | echo "Error: ${SWTPM_INTERFACE} TPM did not start." | |
123 | exit 1 | |
124 | fi | |
125 | ||
126 | # Init the TPM | |
127 | run_swtpm_ioctl ${SWTPM_INTERFACE} -i | |
128 | if [ $? -ne 0 ]; then | |
129 | echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." | |
130 | exit 1 | |
131 | fi | |
132 | ||
133 | # Volatile state must have been removed by TPM now | |
134 | if [ -r $VOLATILE_STATE_FILE ]; then | |
135 | echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." | |
136 | exit 1 | |
137 | fi | |
138 | ||
139 | # Read the PCR again ... | |
140 | swtpm_open_cmddev ${SWTPM_INTERFACE} 100 | |
141 | RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') | |
142 | 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' | |
143 | if [ "$RES" != "$exp" ]; then | |
144 | echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" | |
145 | echo "expected: $exp" | |
146 | echo "received: $RES" | |
147 | exit 1 | |
148 | fi | |
149 | ||
150 | # Check that the TPM Established bit is still set | |
151 | RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e) | |
152 | exp='tpmEstablished is 1' | |
153 | if [ "$RES" != "$exp" ]; then | |
154 | echo "Error (2): TPM Established flag has wrong value." | |
155 | echo "expected: $exp" | |
156 | echo "received: $RES" | |
157 | exit 1 | |
158 | fi | |
159 | ||
160 | # Save the volatile state again | |
161 | run_swtpm_ioctl ${SWTPM_INTERFACE} -v | |
162 | if [ $? -ne 0 ]; then | |
163 | echo "Error: Could not have the ${SWTPM_INTERFACE} TPM store the volatile state to a file." | |
164 | exit 1 | |
165 | fi | |
166 | ||
167 | if [ ! -r $VOLATILE_STATE_FILE ]; then | |
168 | echo "Error: Volatile state file $VOLATILE_STATE_FILE does not exist." | |
169 | exit 1 | |
170 | fi | |
171 | ||
172 | # Send a new TPM_Init | |
173 | run_swtpm_ioctl ${SWTPM_INTERFACE} -i | |
174 | if [ $? -ne 0 ]; then | |
175 | echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM." | |
176 | exit 1 | |
177 | fi | |
178 | ||
179 | # Volatile state must have been removed by TPM now | |
180 | if [ -r $VOLATILE_STATE_FILE ]; then | |
181 | echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists." | |
182 | exit 1 | |
183 | fi | |
184 | ||
185 | # Read the PCR again ... | |
186 | swtpm_open_cmddev ${SWTPM_INTERFACE} 100 | |
187 | RES=$(swtpm_cmd_tx ${SWTPM_INTERFACE} '\x00\xC1\x00\x00\x00\x0E\x00\x00\x00\x15\x00\x00\x00\x11') | |
188 | 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' | |
189 | if [ "$RES" != "$exp" ]; then | |
190 | echo "Error: (2) Did not get expected result from TPM_PCRRead(17)" | |
191 | echo "expected: $exp" | |
192 | echo "received: $RES" | |
193 | exit 1 | |
194 | fi | |
195 | ||
196 | # Check that the TPM Established bit is still set | |
197 | RES=$(run_swtpm_ioctl ${SWTPM_INTERFACE} -e) | |
198 | if [ $? -ne 0 ]; then | |
199 | echo "Error: Could not get the TPM Established flag from the ${SWTPM_INTERFACE} TPM." | |
200 | exit 1 | |
201 | fi | |
202 | exp='tpmEstablished is 1' | |
203 | if [ "$RES" != "$exp" ]; then | |
204 | echo "Error (2): TPM Established flag has wrong value." | |
205 | echo "expected: $exp" | |
206 | echo "received: $RES" | |
207 | exit 1 | |
208 | fi | |
209 | ||
210 | # Final shut down | |
211 | exec 100>&- | |
212 | run_swtpm_ioctl ${SWTPM_INTERFACE} -s | |
213 | if [ $? -ne 0 ]; then | |
214 | echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM." | |
215 | exit 1 | |
216 | fi | |
217 | ||
36407c93 | 218 | if wait_process_gone ${SWTPM_PID} 1; then |
01aa2ed3 SB |
219 | echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore." |
220 | exit 1 | |
221 | fi | |
222 | ||
223 | if [ ! -e $STATE_FILE ]; then | |
224 | echo "Error: TPM state file $STATE_FILE does not exist." | |
225 | exit 1 | |
226 | fi | |
227 | ||
228 | echo "OK" | |
229 | ||
230 | exit 0 |