Debian Codegraph @

tests/test_tpm2_swtpm_setup_profile

raw
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4
5 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
6 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
7
8 source "${TESTDIR}/common"
9 skip_test_no_tpm20 "${SWTPM_EXE}"
10
11 workdir="$(mktemp -d)" || exit 1
12 export TPM_PATH=${workdir}
13 SWTPM_SERVER_PORT=65480
14 SWTPM_CTRL_PORT=65481
15 SWTPM_SERVER_NAME=127.0.0.1
16 SWTPM_INTERFACE="socket+socket"
17
18 trap "cleanup" SIGTERM EXIT
19
20
21 cat <<_EOF_ > "${workdir}/swtpm-localca.options"
22 --tpm-manufacturer IBM
23 --tpm-model swtpm-libtpms
24 --tpm-version 2
25 --platform-manufacturer "Fedora XYZ"
26 --platform-version 2.1
27 --platform-model "QEMU A.B"
28 _EOF_
29
30 cat <<_EOF_ > "${workdir}/swtpm_setup.conf"
31 create_certs_tool=\${SWTPM_LOCALCA}
32 create_certs_tool_config=${workdir}/swtpm-localca.conf
33 create_certs_tool_options=${workdir}/swtpm-localca.options
34 _EOF_
35
36 function cleanup()
37 {
38 rm -rf "${workdir}"
39
40 if kill_quiet -0 "${SWTPM_PID}"; then
41 kill_quiet -9 "${SWTPM_PID}"
42 fi
43 }
44
45 test_swtpm_setup_profile()
46 {
47 local workdir="${1}"
48 local profile="${2}"
49 local exp_response="${3}" # expected 'ActiveProfile' response; optional
50 local disabled_algos="${4}" # disabled algorithms; optional
51 local tpm2request="${5}" # TPM 2 request to send; optional
52 local exp_tpm2response="${6}" # expected response; optional
53 local exp_fail="${7}" # swtpm_setup is expexted to fail when '1'
54
55 local response
56
57 rm -f "${workdir}/logfile"
58
59 if ! $SWTPM_SETUP \
60 --tpm2 \
61 --tpmstate "${workdir}" \
62 --config "${workdir}/swtpm_setup.conf" \
63 --log "${workdir}/logfile" \
64 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
65 --overwrite \
66 ${profile:+--profile ${profile}}
67 then
68 if [ "${exp_fail}" -eq 1 ]; then
69 # swtpm_setup failed as expected
70 return 0
71 fi
72 echo "Test failed: Error: Could not run $SWTPM_SETUP."
73 echo "Setup Logfile:"
74 cat "${workdir}/logfile"
75 exit 1
76 fi
77 if [ -n "${exp_fail}" ] && [ "${exp_fail}" -eq 1 ]; then
78 echo "Error: swtpm_setup did not fail as expected"
79 exit 1
80 fi
81
82 run_swtpm "${SWTPM_INTERFACE}" \
83 --tpm2 \
84 --flags not-need-init,startup-clear
85
86 if ! kill_quiet -0 "${SWTPM_PID}"; then
87 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
88 exit 1
89 fi
90
91 if [ -n "${exp_response}" ]; then
92 response=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --info 0x20)
93 if ! [[ "${response}" =~ ${exp_response} ]]; then
94 echo "Error: Response does not match expected response regular expression"
95 echo "Actual : ${response}"
96 echo "Expected : ${exp_response}"
97 exit 1
98 fi
99 fi
100
101 if [ -n "${disabled_algos}" ]; then
102 response=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --info 0x8 |
103 sed -n 's/.*"Disabled":"\([^"]*\).*/\1/p')
104 if [ "${response}" != "${disabled_algos}" ]; then
105 echo "Error: Response for disabled algorithms does not match expected response"
106 echo "Actual : ${response}"
107 echo "Expected : ${disabled_algos}"
108 exit 1
109 fi
110 fi
111
112 if [ -n "${tpm2request}" ]; then
113 response=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" "${tpm2request}")
114 if [ "${response}" != "${exp_tpm2response}" ]; then
115 echo "Error: TPM 2 response does not match expected response"
116 echo "Actual : ${response}"
117 echo "Expected : ${exp_tpm2response}"
118 exit 1
119 fi
120 fi
121
122 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
123 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
124 exit 1
125 fi
126
127 if wait_process_gone "${SWTPM_PID}" 4; then
128 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
129 exit 1
130 fi
131
132 # Expecting same results from swtpm directly
133 if [ -n "${exp_response}" ]; then
134 response=$(${SWTPM_EXE} socket \
135 --tpmstate "dir=${workdir}" \
136 --tpm2 \
137 --print-info 0x20)
138 if ! [[ "${response}" =~ ${exp_response} ]]; then
139 echo "Error: Response does not match expected response regular expression (swtpm)"
140 echo "Actual : ${response}"
141 echo "Expected : ${exp_response}"
142 exit 1
143 fi
144 fi
145 }
146
147 # Get available profiles and algorithms that are implemented and those that can be disabled
148 run_swtpm "${SWTPM_INTERFACE}" \
149 --tpm2 \
150 --flags not-need-init
151
152 if ! kill_quiet -0 "${SWTPM_PID}"; then
153 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
154 exit 1
155 fi
156
157 profiles=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --info 0x40 |
158 sed $'s/{"Name/\\\n{"Name/g' |
159 sed -n 's/^{"Name":"\([^"]*\)".*/\1/p')
160
161 canbedisabled_algos_str=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --info 0x8 |
162 sed -n 's/.*"CanBeDisabled":"\([^"]*\).*/\1/p')
163
164 implemented_algos_str=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --info 0x8 |
165 sed -n 's/.*"Implemented":"\([^"]*\).*/\1/p')
166
167 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
168 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
169 exit 1
170 fi
171
172 if wait_process_gone "${SWTPM_PID}" 4; then
173 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
174 exit 1
175 fi
176
177 # Test with no profile
178 exp_response=$(echo "^\{\"ActiveProfile\":\{" \
179 "\"Name\":\"null\"," \
180 "\"StateFormatLevel\":[0-9]+,"\
181 "\"Commands\":\"[,x[:xdigit:]-]+\","\
182 "\"Algorithms\":\"[,=[:alnum:]-]+\","\
183 "\"Description\":\"[[:print:]]+\""\
184 "\}\}\$"| tr -d " ")
185 test_swtpm_setup_profile \
186 "${workdir}" "" "${exp_response}" \
187 "" "" "" ""
188
189 echo "Test without profile passed"
190
191 # Test-setup each profile that is implemented
192 for profile in ${profiles}; do
193 exp_response=$(echo "^\{\"ActiveProfile\":\{" \
194 "\"Name\":\"${profile}\"," \
195 "\"StateFormatLevel\":[0-9]+,"\
196 "\"Commands\":\"[,x[:xdigit:]-]+\","\
197 "\"Algorithms\":\"[,=[:alnum:]-]+\","\
198 "(\"Attributes\":\"[,=[:alnum:]-]+\",)?"\
199 "\"Description\":\"[[:print:]]+\""\
200 "\}\}\$"| tr -d " ")
201 test_swtpm_setup_profile \
202 "${workdir}" "{\"Name\":\"${profile}\"}" "${exp_response}" "" "" "" ""
203
204 echo "Test with profile '${profile}' passed"
205 done
206
207 # Setup the custom profile with an increasing set of disabled algorithms
208 # that swtpm_ioctl has to show then.
209 IFS="," read -r -a canbedisabled_algos <<< "${canbedisabled_algos_str}"
210 IFS="," read -r -a implemented_algos <<< "${implemented_algos_str}"
211
212 algos="${implemented_algos[*]}"
213 disabled=()
214 for todisable in "${canbedisabled_algos[@]}"; do
215 # Remove algorithm ${todisable} from list of implemented algorithms
216 algos=$(echo "${algos}" | sed "s/${todisable}//" | tr -s ' ')
217 disabled+=("${todisable}")
218
219 # Format profile
220 a=$(echo "${algos}" | sed -e 's/ /,/g' -e 's/,$//')
221 profile="{\"Name\":\"custom\",\"Algorithms\":\"${a}\"}"
222
223 d=${disabled[*]}
224 d=${d// /,}
225
226 test_swtpm_setup_profile \
227 "${workdir}" "${profile}" "" "${d}" "" "" ""
228 echo "Test with custom profile and disabled algorithms '${d}' passed"
229 done
230
231 # Test the custom profiles with disabled commands and various StateFormatLevels
232 test_custom_profile_sfls()
233 {
234 local cmds="$1"
235 local sfls_list="$2"
236 local msg="$3"
237
238 local sfl exp_sfl exp_fail profile exp_response
239
240 for sfls in ${sfls_list}; do
241 sfl=$(cut -d":" -f1 <<< "${sfls}")
242 exp_sfl=$(cut -d":" -f2 <<< "${sfls}")
243 exp_fail=$(cut -d":" -f3 <<< "${sfls}")
244 profile="{\"Name\":\"custom:test-sfl-${sfl}\",\"Commands\":\"${cmds}\",\"StateFormatLevel\":${sfl}}"
245 exp_response=".*,\"StateFormatLevel\":${exp_sfl},.*"
246 test_swtpm_setup_profile \
247 "${workdir}" "${profile}" "${exp_response}" "" "" "" "${exp_fail}"
248 echo "Test with custom profile and commands${msg} passed with StateFormatLevel ${sfl}."
249 done
250 }
251
252 # because of 0x199-0x19a we must have StateFormatLevel >=3 for it to be acceptable
253 cmds="0x11f-0x122,0x124-0x12e,0x130-0x140,0x142-0x159,0x15b-0x15e,0x160-0x165,0x167-0x174,0x176-0x178,0x17a-0x193,0x199-0x19a"
254 test_custom_profile_sfls "${cmds}" "1:f:1 2:f:1 3:3:0 4:4:0 5:5:0 6:6:0 7:7:0" " (with 0x199-0x19a)"
255
256 # because of 0x19b we must have StateFormatLevel >=5 for it to be acceptable
257 cmds="0x11f-0x122,0x124-0x12e,0x130-0x140,0x142-0x159,0x15b-0x15e,0x160-0x165,0x167-0x174,0x176-0x178,0x17a-0x193,0x199-0x19a,0x19b"
258 test_custom_profile_sfls "${cmds}" "1:f:1 2:f:1 3:f:1 4:f:1 5:5:0 6:6:0 7:7:0" " (with 0x199-0x19b)"
259
260 # Without given StateFormatLevel it should go up to StateFormatLevel '4' (due to AES-192 being selected)
261 profile="{\"Name\":\"custom\",\"Commands\":\"${cmds}\"}"
262 exp_response=".*,\"StateFormatLevel\":[[:digit:]]+,.*"
263 test_swtpm_setup_profile \
264 "${workdir}" "${profile}" "${exp_response}" "" "" "" "0"
265
266 # with cmds 0x199-0x19a missing it can have many StateFormatLevels >= 2
267 cmds="0x11f-0x122,0x124-0x12e,0x130-0x140,0x142-0x159,0x15b-0x15e,0x160-0x165,0x167-0x174,0x176-0x178,0x17a-0x193"
268 # StateFormatLevel 3 enables 'nothing' additional but will be kept
269 # StateFormatLevel 4 enables AES-192 and will remain at '4'
270 test_custom_profile_sfls "${cmds}" "1:f:1 2:2:0 3:3:0 4:4:0 5:5:0 6:6:0 7:7:0" ""
271
272 # Without given StateFormatLevel it should go up to StateFormatLevel '4' (due to AES-192 being selected)
273 profile="{\"Name\":\"custom\",\"Commands\":\"${cmds}\"}"
274 exp_response=".*,\"StateFormatLevel\":[[:digit:]]+,.*"
275 test_swtpm_setup_profile \
276 "${workdir}" "${profile}" "${exp_response}" "" "" "" "${exp_fail}"
277
278 # Setup the TPM 2 with custom profile and sha1 removed
279 # Check that the GetCapability command returns the proper set of PCR banks
280
281 algos="${implemented_algos[*]}"
282 algos=$(echo "${algos}" | sed "s/sha1//" | tr -s ' ')
283 # Format profile
284 a=$(echo "${algos}" | sed -e 's/ /,/g' -e 's/,$//')
285 profile="{\"Name\":\"custom:no-sha1\",\"Algorithms\":\"${a}\"}"
286
287 # Check PCR bank sha1 is not available: tssgetcapability -cap 5
288 test_swtpm_setup_profile \
289 "${workdir}" \
290 "${profile}" \
291 "" \
292 "" \
293 "\x80\x01\x00\x00\x00\x16\x00\x00\x01\x7a\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x40" \
294 " 80 01 00 00 00 25 00 00 00 00 00 00 00 00 05 00 00 00 03 00 0b 03 ff ff ff 00 0c 03 00 00 00 00 0d 03 00 00 00" \
295 ""
296
297 echo "Test with custom profile and 'sha1' disabled algorithms passed"
298
299 # Check that swtpm adjusts key sizes and sets 'Attributes' when
300 # '--profile-remove-disabled fips-host' is passed
301 profile="{\"Name\":\"custom\",\"Description\":\"Test\"} --profile-remove-disabled fips-host"
302 exp_response="\{\"ActiveProfile\":\{\"Name\":\"custom\",.*,\"Algorithms\":\".*,rsa-min-size=2048,.*,ecc-min-size=224,.*\",\"Attributes\":\"no-sha1-signing,no-sha1-verification,no-unpadded-encryption\",\"Description\":\"Test\"\}\}"
303 test_swtpm_setup_profile "${workdir}" "${profile}" "${exp_response}" "" "" "" "0"
304
305 profile="{\"Name\":\"custom\",\"Attributes\":\"no-sha1-signing\",\"Description\":\"Test\"} --profile-remove-disabled fips-host"
306 test_swtpm_setup_profile "${workdir}" "${profile}" "${exp_response}" "" "" "" "0"
307
308 profile="{\"Name\":\"custom:test\",\"Attributes\":\"fips-host\",\"Description\":\"Test\"} --profile-remove-disabled fips-host"
309 exp_response="\{\"ActiveProfile\":\{\"Name\":\"custom:test\",.*,\"Algorithms\":\".*,rsa-min-size=2048,.*,ecc-min-size=224,.*\",\"Attributes\":\"fips-host\",\"Description\":\"Test\"\}\}"
310 test_swtpm_setup_profile "${workdir}" "${profile}" "${exp_response}" "" "" "" "0"
311
312 # --profile-remove-disabled must not have any effect on other profiles
313 for p in null default-v1; do
314 profile="{\"Name\":\"${p}\"} --profile-remove-disabled fips-host"
315 exp_response="\{\"ActiveProfile\":\{\"Name\":\"${p}\",.*,\"Algorithms\":\".*,rsa-min-size=1024,.*,ecc-min-size=192,.*\",.*\}\}"
316 test_swtpm_setup_profile "${workdir}" "${profile}" "${exp_response}" "" "" "" "0"
317 done
318
319 echo "Tests with custom profile and --profile-remove-disabled option passed"
320
321 # Copy TPM 2 state written by libtpms v0.9 and have the TPM read and re-write the state.
322 # The size of the state file must not change since we don't want the profile to be
323 # written into it.
324
325 cp "${TESTDIR}/data/tpm2state6/tpm2-00.permall" "${workdir}"
326 before=$(get_filesize "${workdir}/tpm2-00.permall")
327
328 # Avoid swptm sending TPM2_Shutdown(SU_STATE) and adding savestate to the state file
329 rm -f "${workdir}/logfile"
330 run_swtpm "${SWTPM_INTERFACE}" \
331 --tpm2 \
332 --flags not-need-init,startup-clear,disable-auto-shutdown \
333 --log "file=${workdir}/logfile"
334
335 if ! kill_quiet -0 "${SWTPM_PID}"; then
336 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
337 exit 1
338 fi
339
340 # Run ChangeEPS so the state is re-written: tsschangeeps
341 cmd='\x80\x02\x00\x00\x00\x1b\x00\x00\x01\x24\x40\x00\x00\x0c\x00\x00\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00'
342 RES=$(swtpm_cmd_tx "${SWTPM_INTERFACE}" "${cmd}")
343 exp=' 80 02 00 00 00 13 00 00 00 00 00 00 00 00 00 00 01 00 00'
344 if [ "$RES" != "$exp" ]; then
345 echo "Error: Did not get expected result from TPM2_ChangeEPS"
346 echo "expected: $exp"
347 echo "received: $RES"
348 exit 1
349 fi
350
351 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
352 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
353 exit 1
354 fi
355
356 if wait_process_gone "${SWTPM_PID}" 4; then
357 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
358 exit 1
359 fi
360
361 after=$(get_filesize "${workdir}/tpm2-00.permall")
362 if [ "${after}" -ne "${before}" ]; then
363 echo "Error: The size of the TPM 2 state file has changed"
364 echo "Actual : ${after}"
365 echo "Expected : ${before}"
366 exit 1
367 fi
368
369 echo "Test with state written by libtpms v0.9 passed"
370
371 # Check that swtpm emitted log message with OPENSSL_ENABLE_SHA1_SIGNATURES=1 on
372 # RHEL and CentOS; libtpms 0.9 supported SHA1 signatures
373 case $(get_distro_name) in
374 RHEL) [ "$(get_rhel_version)" -ge 904 ] && exp=1;;
375 CentOS) [ "$(get_centos_version)" -ge 900 ] && exp=1;;
376 *) exp=0;;
377 esac
378 if [ "${exp}" -eq 1 ]; then
379 if ! grep -q OPENSSL_ENABLE_SHA1_SIGNATURES "${workdir}/logfile"; then
380 echo "Missing reference to OPENSSL_ENABLE_SHA1_SIGNATURES in logfile"
381 exit 1
382 fi
383 echo "Test checking for reference to OPENSSL_ENABLE_SHA1_SIGNATURES in logfile passed"
384 fi
385
386 # If the user passes the null profile in then libtpms has to write state
387 # at the level of libtpms v0.9 and the size of the state file has to be
388 # the same as the one created with libtpms v0.9
389 rm -f "${workdir}"/tpm2-00.*
390
391 if ! $SWTPM_SETUP \
392 --tpm2 \
393 --tpmstate "${workdir}" \
394 --config "${workdir}/swtpm_setup.conf" \
395 --log "${workdir}/logfile" \
396 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
397 --profile-file <(echo '{"Name":"null"}')
398 then
399 echo "Test failed: Error: Could not run $SWTPM_SETUP and set null-profile."
400 echo "Setup Logfile:"
401 cat "${workdir}/logfile"
402 exit 1
403 fi
404
405 after=$(get_filesize "${workdir}/tpm2-00.permall")
406 if [ "${after}" -ne "${before}" ]; then
407 echo "Error: The size of the TPM 2 state file is different than if created by libtpms v0.9"
408 echo "Actual : ${after}"
409 echo "Expected : ${before}"
410 exit 1
411 fi
412
413 echo "Test of writing state at the level of libtpms v0.9 passed"
414
415 # Test enablement of PCR banks
416 rm -f "${workdir}/logfile"
417 algorithms="rsa,rsa-min-size=1024,hmac,aes,aes-min-size=128,mgf1,keyedhash,xor,sha256,sha384,null,rsassa,rsapss,oaep,ecdsa,kdf1-sp800-56a,kdf2,kdf1-sp800-108,ecc,ecc-nist,symcipher,cmac,ctr,ofb,cbc,cfb,ecb,ecdh"
418 profile="{\"Name\":\"custom\",\"Algorithms\":\"${algorithms}\"}"
419 if ! $SWTPM_SETUP \
420 --tpm2 \
421 --tpmstate "${workdir}" \
422 --config "${workdir}/swtpm_setup.conf" \
423 --log "${workdir}/logfile" \
424 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
425 --profile "${profile}" \
426 --pcr-banks sha256 \
427 --overwrite; then
428 echo "Error: swtpm_setup failed to run:"
429 cat "${workdir}/logfile"
430 exit 1
431 fi
432 if ! grep -q "PCR banks sha256 among sha256,sha384." "${workdir}/logfile"; then
433 echo "Error: Did not get expected output from activation of SHA256 PCR bank."
434 cat "${workdir}/logfile"
435 exit 1
436 fi
437
438 # sha512 enablement must fail
439 rm -f "${workdir}/logfile"
440 if $SWTPM_SETUP \
441 --tpm2 \
442 --tpmstate "${workdir}" \
443 --config "${workdir}/swtpm_setup.conf" \
444 --log "${workdir}/logfile" \
445 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
446 --profile "${profile}" \
447 --pcr-banks sha512 \
448 --overwrite; then
449 echo "Error: Enablement of SHA512 bank should have failed."
450 cat "${workdir}/logfile"
451 exit 1
452 fi
453 echo "Test of activation of sha256 bank when only sha256 & sha384 banks are available passed"
454
455 exit 0