Debian Codegraph @

tests/_test_tpm2_save_load_state_da_timeout

raw
1 #!/bin/bash
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6
7 ROOT=${abs_top_builddir:-$(pwd)/..}
8 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
9
10 VTPM_NAME="${VTPM_NAME:-vtpm-tpm2-test-save-load-state-da-timeout}"
11 SWTPM_DEV_NAME="/dev/${VTPM_NAME}"
12 TPM_PATH="$(mktemp -d)" || exit 1
13 STATE_FILE=$TPM_PATH/tpm2-00.permall
14 VOLATILE_STATE_FILE=$TPM_PATH/tpm2-00.volatilestate
15 MY_VOLATILE_STATE_FILE=$TPM_PATH/my.volatilestate
16 MY_PERMANENT_STATE_FILE=$TPM_PATH/my.permanent
17 SWTPM_INTERFACE=${SWTPM_INTERFACE:-cuse}
18 SWTPM_CMD_UNIX_PATH=${TPM_PATH}/unix-cmd.sock
19 SWTPM_CTRL_UNIX_PATH=${TPM_PATH}/unix-ctrl.sock
20
21 logfile="$(mktemp)" || exit 1
22
23 function cleanup()
24 {
25 pid=${SWTPM_PID}
26 if [ -n "$pid" ]; then
27 kill_quiet -9 "$pid"
28 fi
29 rm -f "$logfile"
30 rm -rf "$TPM_PATH"
31 }
32
33 trap "cleanup" EXIT
34
35 source "${TESTDIR}/common"
36 [ "${SWTPM_INTERFACE}" == "cuse" ] && source "${TESTDIR}/test_cuse"
37
38 rm -f "$STATE_FILE" "$VOLATILE_STATE_FILE" 2>/dev/null
39
40 TPM_PATH=$TPM_PATH run_swtpm "${SWTPM_INTERFACE}" \
41 --log "file=$logfile,level=20" \
42 --tpm2
43
44 display_processes_by_name "swtpm"
45
46 if ! kill_quiet -0 "${SWTPM_PID}"; then
47 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
48 echo "TPM Logfile:"
49 cat "$logfile"
50 exit 1
51 fi
52
53 # Init the TPM
54 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
55 echo "Error: Could not initialize the ${SWTPM_INTERFACE} TPM."
56 echo "TPM Logfile:"
57 cat "$logfile"
58 exit 1
59 fi
60
61 if ! kill_quiet -0 "${SWTPM_PID}" 2>/dev/null; then
62 echo "Error: ${SWTPM_INTERFACE} TPM not running anymore after INIT."
63 echo "TPM Logfile:"
64 cat "$logfile"
65 exit 1
66 fi
67
68 # Startup the TPM
69 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00')
70 exp=' 80 01 00 00 00 0a 00 00 00 00'
71 if [ "$RES" != "$exp" ]; then
72 echo "Error: Did not get expected result from TPM2_Startup(ST_Clear)"
73 echo "expected: $exp"
74 echo "received: $RES"
75 exit 1
76 fi
77
78 # Create an NVRAM location: nvdefinespace -hi o -ha 01000000 -pwdn nnn -sz 16 -at da
79 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x02\x00\x00\x00\x30\x00\x00\x01\x2a\x40\x00\x00\x01\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x03\x6e\x6e\x6e\x00\x0e\x01\x00\x00\x00\x00\x0b\x00\x04\x00\x04\x00\x00\x00\x10')
80 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
81 if [ "$RES" != "$exp" ]; then
82 echo "Error: Did not get expected result from TPM2_NV_DefineSpace()"
83 echo "expected: $exp"
84 echo "received: $RES"
85 exit 1
86 fi
87
88 sleep 1
89
90 # TPM2_NV_Write with good password 'nnn' and bad password 'nn'
91 NVWRITE_GOOD='\x80\x02\x00\x00\x00\x2a\x00\x00\x01\x37\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0c\x40\x00\x00\x09\x00\x00\x00\x00\x03\x6e\x6e\x6e\x00\x04\x74\x65\x73\x74\x00\x00'
92 NVWRITE_BAD='\x80\x02\x00\x00\x00\x29\x00\x00\x01\x37\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x0b\x40\x00\x00\x09\x00\x00\x00\x00\x02\x6e\x6e\x00\x04\x74\x65\x73\x74\x00\x00'
93
94 # Write some data into the NVRAM area: nvwrite -ha 01000000 -ic test -pwdn nnn
95 # Due to -at da being used on TPM2_NV_DefineSpace, the first time the command will request a retry
96 for ((i = 0; i < 2; i++)); do
97 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
98 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
99 if [ "$i" -eq 1 ] && [ "$RES" != "$exp" ]; then
100 echo "Error: Did not get expected result from TPM2_NV_Write()"
101 echo "expected: $exp"
102 echo "received: $RES"
103 exit 1
104 fi
105 done
106
107 # Set the dictionary attack parameters: dictionaryattackparameters -lr 6 -nrt 6
108 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" '\x80\x02\x00\x00\x00\x27\x00\x00\x01\x3a\x40\x00\x00\x0a\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x06')
109 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
110 if [ "$RES" != "$exp" ]; then
111 echo "Error: Did not get expected result from TPM2_DictionaryAttackParameters()"
112 echo "expected: $exp"
113 echo "received: $RES"
114 exit 1
115 fi
116
117 timenow=$(date +%s)
118 timeout=$((timenow + 6))
119 # Send TPM2_NV_Write with wrong password
120 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_BAD})
121 exp=' 80 01 00 00 00 0a 00 00 09 8e'
122 if [ "$RES" != "$exp" ]; then
123 echo "Error: Did not get expected result from TPM2_NV_Write() with bad password"
124 echo "expected: $exp"
125 echo "received: $RES"
126 exit 1
127 fi
128 timenow=$(date +%s)
129 timerecovery=$((timenow + 6))
130
131 # TPM2_NV_Write with good password must now fail until $timeout
132 while :; do
133 timenow=$(date +%s)
134 echo "Writing with good password failed due to lockout until $timeout - now is $timenow."
135 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
136 exp=' 80 01 00 00 00 0a 00 00 09 21'
137 # busy systems may run the above at >= $timeout and get an unexpected result; check time again
138 timenow=$(date +%s)
139 if [ "$RES" != "$exp" ] && [ "$timenow" -lt "$timeout" ]; then
140 echo "Error: Did not get expected failure from TPM2_NV_Write() with good password. Lockout should be enabled."
141 echo "expected: $exp"
142 echo "received: $RES"
143 exit 1
144 fi
145 [ "$timenow" -ge "$timeout" ] && break
146 sleep 1
147 done
148
149 while :; do
150 sleep 1
151 timenow=$(date +%s)
152 [ "$timenow" -gt "$timerecovery" ] && break
153 done
154 timenow_after=$(date +%s)
155 echo "Time is now ${timenow_after} -- trying with good password should work now."
156 # Now writing with the good password must work again
157 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
158 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
159 if [ "$RES" != "$exp" ]; then
160 echo "Error: Did not get expected result from TPM2_NV_Write() with good password"
161 echo "expected: $exp"
162 echo "received: $RES"
163 # OS X special check
164 if [ "$timenow_after" -lt "$timenow" ]; then
165 echo "Time went backwards..."
166 exit 77
167 fi
168 exit 1
169 fi
170
171 timenow=$(date +%s)
172 timeout=$((timenow + 6))
173 # Again cause lockout: Send TPM2_NV_Write with wrong password
174 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_BAD})
175 exp=' 80 01 00 00 00 0a 00 00 09 8e'
176 if [ "$RES" != "$exp" ]; then
177 echo "Error: Did not get expected result from TPM2_NV_Write() with bad password"
178 echo "expected: $exp"
179 echo "received: $RES"
180 exit 1
181 fi
182 timenow=$(date +%s)
183 timerecovery=$((timenow + 6))
184
185 # Save the state and restore it and then try to poll again
186 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save permanent "$MY_PERMANENT_STATE_FILE"; then
187 echo "Error: Could not write permanent state file $MY_PERMANENT_STATE_FILE."
188 echo "TPM Logfile:"
189 cat "$logfile"
190 exit 1
191 fi
192 if [ ! -r "$MY_PERMANENT_STATE_FILE" ]; then
193 echo "Error: Permanent state file $MY_PERMANENT_STATE_FILE does not exist."
194 echo "TPM Logfile:"
195 cat "$logfile"
196 exit 1
197 fi
198 echo "Saved permanent state."
199
200 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --save volatile "$MY_VOLATILE_STATE_FILE"; then
201 echo "Error: Could not write volatile state file $MY_VOLATILE_STATE_FILE."
202 echo "TPM Logfile:"
203 cat "$logfile"
204 exit 1
205 fi
206 if [ ! -r "$MY_VOLATILE_STATE_FILE" ]; then
207 echo "Error: Volatile state file $MY_VOLATILE_STATE_FILE does not exist."
208 echo "TPM Logfile:"
209 cat "$logfile"
210 exit 1
211 fi
212 echo "Saved volatile state."
213
214 # we will use our own volatile state
215 rm -f "$VOLATILE_STATE_FILE" "$STATE_FILE"
216
217 # Stop the TPM; this will not shut it down
218 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --stop; then
219 echo "Error: Could not stop the ${SWTPM_INTERFACE} TPM."
220 echo "TPM Logfile:"
221 cat "$logfile"
222 exit 1
223 fi
224
225 if ! kill_quiet -0 "${SWTPM_PID}"; then
226 echo "Error (2): ${SWTPM_INTERFACE} TPM is not running anymore."
227 echo "TPM Logfile:"
228 cat "$logfile"
229 exit 1
230 fi
231
232 # load state into the TPM
233 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load permanent "$MY_PERMANENT_STATE_FILE"; then
234 echo "Could not load permanent state into vTPM"
235 echo "TPM Logfile:"
236 cat "$logfile"
237 exit 1
238 fi
239 echo "Loaded permanent state."
240
241 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" --load volatile "$MY_VOLATILE_STATE_FILE"; then
242 echo "Could not load volatile state into vTPM"
243 echo "TPM Logfile:"
244 cat "$logfile"
245 exit 1
246 fi
247 echo "Loaded volatile state."
248
249 #ls -l $(dirname $MY_VOLATILE_STATE_FILE)/*
250 #sha1sum $(dirname $MY_VOLATILE_STATE_FILE)/*
251
252 # Init the TPM
253 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -i; then
254 echo "TPM Init failed."
255 echo "TPM Logfile:"
256 cat "$logfile"
257 exit 1
258 fi
259
260 # Volatile state must have been removed by TPM now
261 if [ -r "$VOLATILE_STATE_FILE" ]; then
262 echo "Error: Volatile state file $VOLATILE_STATE_FILE still exists."
263 echo "TPM Logfile:"
264 cat "$logfile"
265 exit 1
266 fi
267
268
269 # TPM2_NV_Write with good password must now fail until $timeout
270 while :; do
271 timenow=$(date +%s)
272 echo "Writing with good password failed due to lockout until $timeout - now is $timenow."
273 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
274 exp=' 80 01 00 00 00 0a 00 00 09 21'
275 # busy systems may run the above at >= $timeout and get an unexpected result; check time again
276 timenow=$(date +%s)
277 if [ "$RES" != "$exp" ] && [ "$timenow" -lt "$timeout" ]; then
278 echo "Error: Did not get expected failure from TPM2_NV_Write() with good password. Lockout should be enabled."
279 echo "expected: $exp"
280 echo "received: $RES"
281 exit 1
282 fi
283 [ "$timenow" -ge "$timeout" ] && break
284 sleep 1
285 done
286
287 while :; do
288 sleep 1
289 timenow=$(date +%s)
290 [ "$timenow" -gt "$timerecovery" ] && break
291 done
292 timenow_after=$(date +%s)
293 echo "Time is now $timenow_after -- trying with good password should work now."
294 # Now writing with the good password must work again
295 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" ${NVWRITE_GOOD})
296 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
297 if [ "$RES" != "$exp" ]; then
298 echo "Error: Did not get expected result from TPM2_NV_Write() with good password"
299 echo "expected: $exp"
300 echo "received: $RES"
301 # OS X special check
302 if [ "$timenow_after" -lt "$timenow" ]; then
303 echo "Time went backwards..."
304 exit 77
305 fi
306 exit 1
307 fi
308
309 # Final shut down
310 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
311 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
312 echo "TPM Logfile:"
313 cat "$logfile"
314 exit 1
315 fi
316
317 if wait_process_gone "${SWTPM_PID}" 4; then
318 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
319 echo "TPM Logfile:"
320 cat "$logfile"
321 exit 1
322 fi
323
324 if [ ! -e "$STATE_FILE" ]; then
325 echo "Error: TPM state file $STATE_FILE does not exist."
326 echo "TPM Logfile:"
327 cat "$logfile"
328 exit 1
329 fi
330
331 echo "OK"
332
333 exit 0