Debian Codegraph @

tests/test_tpm2_swtpm_setup_profile_name

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=65482
14 SWTPM_CTRL_PORT=65483
15 SWTPM_SERVER_NAME=127.0.0.1
16 SWTPM_INTERFACE="socket+socket"
17
18 SP="[[:blank:]+]"
19
20 profilesdir="${workdir}/profiles"
21 mkdir -p "${profilesdir}"
22
23 trap "cleanup" SIGTERM EXIT
24
25
26 cat <<_EOF_ > "${workdir}/swtpm-localca.options"
27 --tpm-manufacturer IBM
28 --tpm-model swtpm-libtpms
29 --tpm-version 2
30 --platform-manufacturer "Fedora XYZ"
31 --platform-version 2.1
32 --platform-model "QEMU A.B"
33 _EOF_
34
35 cat <<_EOF_ > "${workdir}/swtpm_setup.conf"
36 create_certs_tool=\${SWTPM_LOCALCA}
37 create_certs_tool_config=${workdir}/swtpm-localca.conf
38 create_certs_tool_options=${workdir}/swtpm-localca.options
39 local_profiles_dir = ${workdir}/profiles
40 _EOF_
41
42 function cleanup()
43 {
44 rm -rf "${workdir}"
45
46 if kill_quiet -0 "${SWTPM_PID}"; then
47 kill_quiet -9 "${SWTPM_PID}"
48 fi
49 }
50
51 test_swtpm_setup_profile()
52 {
53 local workdir="${1}"
54 local profilename="${2}"
55 local exp_response="${3}" # expected 'ActiveProfile' response; optional
56 local exp_fail="${4}" # swtpm_setup is expexted to fail when '1'
57
58 local response
59
60 rm -f "${workdir}/logfile"
61
62 if ! $SWTPM_SETUP \
63 --tpm2 \
64 --tpmstate "${workdir}" \
65 --config "${workdir}/swtpm_setup.conf" \
66 --log "${workdir}/logfile" \
67 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
68 --overwrite \
69 ${profilename:+--profile-name ${profilename}}
70 then
71 if [ "${exp_fail}" -eq 1 ]; then
72 # swtpm_setup failed as expected
73 return 0
74 fi
75 echo "Test failed: Error: Could not run $SWTPM_SETUP."
76 echo "Setup Logfile:"
77 cat "${workdir}/logfile"
78 exit 1
79 fi
80 if [ -n "${exp_fail}" ] && [ "${exp_fail}" -eq 1 ]; then
81 echo "Error: swtpm_setup did not fail as expected"
82 exit 1
83 fi
84
85 run_swtpm "${SWTPM_INTERFACE}" \
86 --tpm2 \
87 --flags not-need-init,startup-clear
88
89 if ! kill_quiet -0 "${SWTPM_PID}"; then
90 echo "Error: ${SWTPM_INTERFACE} TPM did not start."
91 exit 1
92 fi
93
94 if [ -n "${exp_response}" ]; then
95 response=$(run_swtpm_ioctl "${SWTPM_INTERFACE}" --info 0x20)
96 if ! [[ "${response}" =~ ${exp_response} ]]; then
97 echo "Error: Response does not match expected response regular expression"
98 echo "Actual : ${response}"
99 echo "Expected : ${exp_response}"
100 exit 1
101 fi
102 fi
103
104 if ! run_swtpm_ioctl "${SWTPM_INTERFACE}" -s; then
105 echo "Error: Could not shut down the ${SWTPM_INTERFACE} TPM."
106 exit 1
107 fi
108
109 if wait_process_gone "${SWTPM_PID}" 4; then
110 echo "Error: ${SWTPM_INTERFACE} TPM should not be running anymore."
111 exit 1
112 fi
113 }
114
115 # Force-load built-in's and local's
116
117 exp_response_null=$(echo "^\{\"ActiveProfile\":\{" \
118 "\"Name\":\"null\"," \
119 "\"StateFormatLevel\":1,"\
120 "\"Commands\":\"[,x[:xdigit:]-]+\","\
121 "\"Algorithms\":\"[,=[:alnum:]-]+\","\
122 "\"Description\":\"The${SP}profile${SP}enables${SP}[[:print:]]+\""\
123 "\}\}\$"| tr -d " ")
124 test_swtpm_setup_profile "${workdir}" "builtin:null" "${exp_response_null}" "0"
125
126 exp_response_default=$(echo "^\{\"ActiveProfile\":\{" \
127 "\"Name\":\"default-v1\"," \
128 "\"StateFormatLevel\":[0-9]+,"\
129 "\"Commands\":\"[,x[:xdigit:]-]+\","\
130 "\"Algorithms\":\"[,=[:alnum:]-]+\","\
131 "(\"Attributes\":\"[,=[:alnum:]-]+\",)?"\
132 "\"Description\":\"This${SP}profile${SP}enables${SP}all${SP}[[:print:]]+\""\
133 "\}\}\$"| tr -d " ")
134 test_swtpm_setup_profile "${workdir}" "builtin:default-v1" "${exp_response_default}" "0"
135
136 # No local profile must be found
137 output=$($SWTPM_SETUP \
138 --tpm2 \
139 --print-profiles \
140 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
141 --config "${workdir}/swtpm_setup.conf")
142 if ! echo "${output}" | grep -q '"local":\[\]'; then
143 echo "Error: Unexpected output. There should be no local profiles."
144 echo "${output}"
145 exit 1
146 fi
147
148 echo '{"Name":"custom","Description":"test1"}' > "${profilesdir}/test1.json"
149 exp_response_test1=$(echo "^\{\"ActiveProfile\":\{" \
150 "\"Name\":\"custom\"," \
151 "\"StateFormatLevel\":[0-9]+,"\
152 "\"Commands\":\"[,x[:xdigit:]-]+\","\
153 "\"Algorithms\":\"[,=[:alnum:]-]+\","\
154 "\"Description\":\"test1\""\
155 "\}\}\$"| tr -d " ")
156 test_swtpm_setup_profile "${workdir}" "local:test1" "${exp_response_test1}" "0"
157
158 # One local profile must be found
159 output=$($SWTPM_SETUP \
160 --tpm2 \
161 --print-profiles \
162 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
163 --config "${workdir}/swtpm_setup.conf")
164 if ! echo "${output}" | grep -q '"local":\[{"Name":"test1","Description":"test1"}\]'; then
165 echo "Error: Unexpected output. There should be 1 local profile."
166 echo "${output}"
167 exit 1
168 fi
169
170 echo '{"Name":"custom","Description":"test2"}' > "${profilesdir}/test2.json"
171 exp_response_test2=$(echo "^\{\"ActiveProfile\":\{" \
172 "\"Name\":\"custom\"," \
173 "\"StateFormatLevel\":[0-9]+,"\
174 "\"Commands\":\"[,x[:xdigit:]-]+\","\
175 "\"Algorithms\":\"[,=[:alnum:]-]+\","\
176 "\"Description\":\"test2\""\
177 "\}\}\$"| tr -d " ")
178 test_swtpm_setup_profile "${workdir}" "local:test2" "${exp_response_test2}" "0"
179
180 # Two local profiles must be found
181 output=$($SWTPM_SETUP \
182 --tpm2 \
183 --print-profiles \
184 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
185 --config "${workdir}/swtpm_setup.conf")
186 # No assumption can be made on order in which test1 and test2 appear
187 if ! echo "${output}" | grep -qE '"local":\[.*\{"Name":"test1","Description":"test1"\}.*"builtin":' || \
188 ! echo "${output}" | grep -qE '"local":\[.*\{"Name":"test2","Description":"test2"\}.*"builtin":'; then
189 echo "Error: Unexpected output. There should be 2 local profiles."
190 echo "${output}"
191 exit 1
192 fi
193
194 # Load by searching through directories and last use built-in's
195
196 test_swtpm_setup_profile "${workdir}" "null" "${exp_response_null}" "0"
197 test_swtpm_setup_profile "${workdir}" "default-v1" "${exp_response_default}" "0"
198 test_swtpm_setup_profile "${workdir}" "test1" "${exp_response_test1}" "0"
199 test_swtpm_setup_profile "${workdir}" "test2" "${exp_response_test2}" "0"
200 test_swtpm_setup_profile "${workdir}" "test3" "${exp_response_test2}" "1"