Debian Codegraph @

tests/test_tpm2_swtpm_setup_create_cert

raw
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4
5 TOPBUILD=${abs_top_builddir:-$(dirname "$0")/..}
6 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
7 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
8
9 source "${TESTDIR}/common"
10 skip_test_no_tpm20 "${SWTPM_EXE}"
11
12 workdir="$(mktemp -d "/tmp/path with spaces.XXXXXX")" || exit 1
13
14 SIGNINGKEY=${workdir}/signingkey.pem
15 ISSUERCERT=${workdir}/issuercert.pem
16 CERTSERIAL=${workdir}/certserial
17 USER_CERTSDIR=${workdir}/mycerts
18 mkdir -p "${USER_CERTSDIR}"
19
20 PATH=${TOPBUILD}/src/swtpm_bios:$PATH
21
22 trap "cleanup" SIGTERM EXIT
23
24 function cleanup()
25 {
26 rm -rf "${workdir}"
27 }
28
29 # We want swtpm_cert to use the local CA and see that the
30 # local CA script automatically creates a signingkey and
31 # self-signed certificate
32
33 cat <<_EOF_ > "${workdir}/swtpm-localca.conf"
34 statedir=${workdir}
35 signingkey = ${SIGNINGKEY}
36 issuercert = ${ISSUERCERT}
37 certserial = ${CERTSERIAL}
38 _EOF_
39
40 #max. serial number must immediately be replace with 20 digits one
41 echo -n 1461501637330902918203684832716283019655932542975 > "${CERTSERIAL}"
42
43 cat <<_EOF_ > "${workdir}/swtpm-localca.options"
44 --tpm-manufacturer IBM
45 --tpm-model swtpm-libtpms
46 --tpm-version 2
47 --platform-manufacturer "Fedora XYZ"
48 --platform-version 2.1
49 --platform-model "QEMU A.B"
50 _EOF_
51
52 export MY_SWTPM_LOCALCA="${SWTPM_LOCALCA}"
53
54 cat <<_EOF_ > "${workdir}/swtpm_setup.conf"
55 create_certs_tool=\${MY_SWTPM_LOCALCA}
56 create_certs_tool_config=${workdir}/swtpm-localca.conf
57 create_certs_tool_options=${workdir}/swtpm-localca.options
58 profile = {"Name": "default-v1"}
59 _EOF_
60
61 # We need to adapt the PATH so the correct swtpm_cert is picked
62 export PATH=${TOPBUILD}/src/swtpm_cert:${PATH}
63
64 keysizes="2048"
65 if ${SWTPM_SETUP} \
66 --tpm2 \
67 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
68 --print-capabilities |
69 grep -q tpm2-rsa-keysize-3072; then
70 keysizes+=" 3072"
71 fi
72
73 for keysize in ${keysizes}; do
74 echo "Testing with RSA keysize $keysize"
75 # we need to create at least one cert: --create-ek-cert
76 if ! ${SWTPM_SETUP} \
77 --tpm2 \
78 --allow-signing \
79 --tpm-state "${workdir}" \
80 --create-ek-cert \
81 --create-platform-cert \
82 --config "${workdir}/swtpm_setup.conf" \
83 --logfile "${workdir}/logfile" \
84 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
85 --rsa-keysize "${keysize}" \
86 --overwrite \
87 --write-ek-cert-files "${USER_CERTSDIR}";
88 then
89 echo "Error: Could not run $SWTPM_SETUP."
90 echo "Logfile output:"
91 cat "${workdir}/logfile"
92 exit 1
93 fi
94
95 if [ ! -r "${SIGNINGKEY}" ]; then
96 echo "Error: Signingkey file ${SIGNINGKEY} was not created."
97 exit 1
98 fi
99
100 if [ ! -r "${ISSUERCERT}" ]; then
101 echo "Error: Issuer cert file ${ISSUERCERT} was not created."
102 exit 1
103 fi
104
105 if [ ! -r "${CERTSERIAL}" ]; then
106 echo "Error: Cert serial number file ${CERTSERIAL} was not created."
107 exit 1
108 fi
109
110 tmp=$(cat "${CERTSERIAL}")
111 if [ "${#tmp}" -ne 20 ]; then
112 echo "Error: Cert serial number in file should now be 20 digits long."
113 exit 1
114 fi
115
116 certfile="${USER_CERTSDIR}/ek-rsa${keysize}.crt"
117 if [ ! -f "${certfile}" ]; then
118 echo "Error: EK file '${certfile}' was not written."
119 ls -l "${USER_CERTSDIR}"
120 exit 1
121 fi
122
123 if ! $CERTTOOL --inder --infile "${certfile}" -i | grep -q "${keysize} bits"; then
124 echo "Error: EK file '${certfile}' is not an RSA ${keysize} bit key."
125 $CERTTOOL --inder --infile "${certfile}" -i
126 exit 1
127 fi
128
129 rm -rf "${SIGNINGKEY}" "${ISSUERCERT}" "${CERTSERIAL}" "${USER_CERTSDIR}"/ek-*.crt
130 done
131
132 echo "Test 1: OK"
133
134 function swtpm_setup_reconfigure() {
135 local workdir="$1"
136 local pwdfile="$2"
137
138 # Reconfigure the active PCR banks a few times; the size of the state
139 # file must not change but its content (hash) must change every time
140 # since activating the PCR banks changes a few bits in the permanent
141 # state, also when the state is not encrypted.
142 local PERMALL_FILE="${workdir}/tpm2-00.permall"
143 local permall_hash permall_size newhash newsize
144
145 permall_size=$(get_filesize "${PERMALL_FILE}")
146
147 for pcrbanks in "sha256" "sha256,sha384" "sha256,sha384,sha512"; do
148 # hash must change between before and after
149 permall_hash=$(get_sha1_file "${PERMALL_FILE}")
150
151 if ! ${SWTPM_SETUP} \
152 --tpm2 \
153 --tpm-state "${workdir}" \
154 --config "${workdir}/swtpm_setup.conf" \
155 --logfile "${workdir}/logfile" \
156 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
157 --pcr-banks "${pcrbanks}" \
158 --reconfigure \
159 ${pwdfile:+--pwdfile "${pwdfile}"};
160 then
161 echo "Error: Could not run $SWTPM_SETUP --reconfigure."
162 echo "Logfile output:"
163 cat "${workdir}/logfile"
164 exit 1
165 fi
166
167 newhash=$(get_sha1_file "${PERMALL_FILE}")
168 if [ "${newhash}" = "${permall_hash}" ]; then
169 echo "Error: The hash of the permanent state did not change."
170 exit 1
171 fi
172
173 newsize=$(get_filesize "${PERMALL_FILE}")
174 if [ "${newsize}" != "${permall_size}" ]; then
175 echo "Error: The size of the permanent state file changed."
176 echo "Actual : ${newsize}"
177 echo "Expected: ${permall_size}"
178 exit 1
179 fi
180 echo "Filesize: ${newsize}; hash: ${newhash}; pwdfile: ${pwdfile}"
181 done
182 }
183
184 # Create with certificates with and without encryption enabled and reconfigure
185 # the PCR banks
186 PWDFILE="${workdir}/pwd"
187 echo -n "password" > "${PWDFILE}"
188 rm -f "${workdir}/logfile"
189
190 for pwdfile in "" "${PWDFILE}"; do
191 if ! ${SWTPM_SETUP} \
192 --tpm2 \
193 --ecc \
194 --tpm-state "${workdir}" \
195 --create-ek-cert \
196 --create-platform-cert \
197 --config "${workdir}/swtpm_setup.conf" \
198 --logfile "${workdir}/logfile" \
199 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
200 --overwrite \
201 --write-ek-cert-files "${workdir}" \
202 ${pwdfile:+--pwdfile "${pwdfile}"}; then
203 echo "Error: Could not run $SWTPM_SETUP."
204 echo "Logfile output:"
205 cat "${workdir}/logfile"
206 exit 1
207 fi
208
209 if [ ! -r "${SIGNINGKEY}" ]; then
210 echo "Error: Signingkey file ${SIGNINGKEY} was not created."
211 exit 1
212 fi
213
214 if [ ! -r "${ISSUERCERT}" ]; then
215 echo "Error: Issuer cert file ${ISSUERCERT} was not created."
216 exit 1
217 fi
218
219 if [ ! -r "${CERTSERIAL}" ]; then
220 echo "Error: Cert serial number file ${CERTSERIAL} was not created."
221 exit 1
222 fi
223
224 certfile="${workdir}/ek-secp384r1.crt"
225 if [ ! -f "${certfile}" ]; then
226 echo "Error: EK file '${certfile}' was not written."
227 ls -l "${workdir}"
228 exit 1
229 fi
230
231 if ! $CERTTOOL --inder --infile "${certfile}" -i | grep -q "384 bits"; then
232 echo "Error: EK file '${certfile}' is not an ECC 384 bit key."
233 $CERTTOOL --inder --infile "${certfile}" -i
234 exit 1
235 fi
236
237 swtpm_setup_reconfigure "${workdir}" "${pwdfile}"
238 done
239
240 echo "Test 2: OK"
241
242 exit 0