Debian Codegraph @

tests/_test_volatilestate

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