Debian Codegraph @

tests/test_swtpm_setup_create_cert

raw symbols SCIP
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_tpm12 "${SWTPM_EXE}"
10
11 workdir="$(mktemp -d)" || exit 1
12
13 SIGNINGKEY=${workdir}/signingkey.pem
14 ISSUERCERT=${workdir}/issuercert.pem
15 CERTSERIAL=${workdir}/certserial
16
17 PATH=${ROOT}/src/swtpm_bios:$PATH
18
19 trap "cleanup" SIGTERM EXIT
20
21 function cleanup()
22 {
23 rm -rf "${workdir}"
24 }
25
26 # We want swtpm_cert to use the local CA and see that the
27 # local CA script automatically creates a signingkey and
28 # self-signed certificate; use ${WORKDIR} in the config files
29 # to test env variable resolution
30
31 cat <<_EOF_ > "${workdir}/swtpm-localca.conf"
32 statedir=\${WORKDIR}
33 signingkey = \${WORKDIR}/signingkey.pem
34 issuercert = \${WORKDIR}/issuercert.pem
35 certserial = \${WORKDIR}/certserial
36 _EOF_
37
38 cat <<_EOF_ > "${workdir}/swtpm-localca.options"
39 --tpm-manufacturer IBM
40 --tpm-model swtpm-libtpms
41 --tpm-version 1.2
42 --platform-manufacturer Fedora
43 --platform-version 2.1
44 --platform-model QEMU
45 _EOF_
46
47 cat <<_EOF_ > "${workdir}/swtpm_setup.conf"
48 create_certs_tool=${SWTPM_LOCALCA}
49 create_certs_tool_config=\${WORKDIR}/swtpm-localca.conf
50 create_certs_tool_options=\${WORKDIR}/swtpm-localca.options
51 _EOF_
52
53 # We need to adapt the PATH so the correct swtpm_cert is picked
54 export PATH=${ROOT}/src/swtpm_cert:${PATH}
55
56 # Create a ROOT CA with a password-protected private key
57 export SWTPM_ROOTCA_PASSWORD=password
58
59 # we need to create at least one cert: --create-ek-cert
60 if ! WORKDIR=${workdir} $SWTPM_SETUP \
61 --tpm-state "${workdir}" \
62 --create-ek-cert \
63 --config "${workdir}/swtpm_setup.conf" \
64 --logfile "${workdir}/logfile" \
65 --tpm "${SWTPM_EXE} socket ${SWTPM_TEST_SECCOMP_OPT}" \
66 --write-ek-cert-files "${workdir}";
67 then
68 echo "Error: Could not run $SWTPM_SETUP."
69 echo "Setup Logfile:"
70 cat "${workdir}/logfile"
71 exit 1
72 fi
73
74 if [ ! -r "${SIGNINGKEY}" ]; then
75 echo "Error: Signingkey file ${SIGNINGKEY} was not created."
76 echo "Setup Logfile:"
77 cat "${workdir}/logfile"
78 exit 1
79 fi
80
81 if [ ! -r "${ISSUERCERT}" ]; then
82 echo "Error: Issuer cert file ${ISSUERCERT} was not created."
83 echo "Setup Logfile:"
84 cat "${workdir}/logfile"
85 exit 1
86 fi
87
88 if [ ! -r "${CERTSERIAL}" ]; then
89 echo "Error: Cert serial number file ${CERTSERIAL} was not created."
90 echo "Setup Logfile:"
91 cat "${workdir}/logfile"
92 exit 1
93 fi
94
95 if ! grep -q "ENCRYPTED PRIVATE KEY" "${workdir}/swtpm-localca-rootca-privkey.pem"; then
96 echo "Error: Root CA's private key should be encrypted"
97 cat "${workdir}/swtpm-localca-rootca-privkey.pem"
98 exit 1
99 fi
100
101 certfile="${workdir}/ek-rsa2048.crt"
102 if [ ! -f "${certfile}" ]; then
103 echo "Error: EK file '${certfile}' was not written."
104 ls -l "${workdir}"
105 exit 1
106 fi
107
108 if ! $CERTTOOL --inder --infile "${certfile}" -i | grep -q "2048 bits"; then
109 echo "Error: EK file '${certfile}' is not an RSA 2048 bit key."
110 $CERTTOOL --inder --infile "${certfile}" -i
111 exit 1
112 fi
113
114 expiration="$($CERTTOOL --inder --infile "${certfile}" -i | grep "Not After:")"
115 expected1="Not After: Fri Dec 31 23:59:59 UTC 9999"
116 # 32bit machines
117 expected2="Not After: Thu Dec 31 23:23:23 UTC 2037"
118 if ! echo "${expiration}" | grep -q "${expected1}" && \
119 ! echo "${expiration}" | grep -q "${expected2}"; then
120 echo "Error: EK file '${certfile}' does not expire in 9999 or 2037"
121 echo "actual : ${expiration}"
122 echo "expected1: ${expected1}"
123 echo "expected2: ${expected2} (32 bit machines)"
124 exit 1
125 fi
126
127 echo "OK"
128
129 exit 0