Debian Codegraph @

tests/test_tpm2_swtpm_localca

raw
1 #!/usr/bin/env bash
2
3 # For the license, see the LICENSE file in the root directory.
4 #set -x
5
6 ROOT=${abs_top_builddir:-$(dirname "$0")/..}
7 TOPBUILD=${abs_top_builddir:-$(dirname "$0")/..}
8 TESTDIR=${abs_top_testdir:-$(dirname "$0")}
9
10 workdir="$(mktemp -d "/tmp/path with spaces.XXXXXX")" || exit 1
11
12 ek="80" # 2048 bit key must have highest bit set
13 for ((i = 1; i < 256; i++)); do
14 ek="${ek}$(printf "%02x" "$i")"
15 done
16
17 SIGNINGKEY=${workdir}/signingkey.pem
18 ISSUERCERT=${workdir}/issuercert.pem
19 CERTSERIAL=${workdir}/certserial
20
21 PATH=${TOPBUILD}/src/swtpm_cert:$PATH
22
23 source "${TESTDIR}/common"
24
25 if ${CERTTOOL} --help | grep -q -E "\--verify-profile"; then
26 verify_profile="--verify-profile=medium"
27 fi
28
29 trap "cleanup" SIGTERM EXIT
30
31 function cleanup()
32 {
33 rm -rf "${workdir}"
34 }
35
36 cat <<_EOF_ > "${workdir}/swtpm-localca.conf"
37 statedir=${workdir}
38 signingkey = ${SIGNINGKEY}
39 issuercert = ${ISSUERCERT}
40 certserial = ${CERTSERIAL}
41 signingkey_password = password
42 _EOF_
43
44 cat <<_EOF_ > "${workdir}/swtpm-localca.options"
45 --tpm-manufacturer IBM
46 --tpm-model swtpm-libtpms
47 --tpm-version 2
48 --platform-manufacturer Fedora
49 --platform-version 2.1
50 --platform-model QEMU
51 _EOF_
52
53 # the following contains the test parameters and
54 # expected key usage
55 for testparams in \
56 "--allow-signing|Digital signature" \
57 "--allow-signing --decryption|Digital signature,Key encipherment" \
58 "--decryption|Key encipherment" \
59 "|Key encipherment";
60 do
61 params=$(echo "${testparams}" | cut -d"|" -f1)
62 usage=$(echo "${testparams}" | cut -d"|" -f2)
63
64 if ! ${SWTPM_LOCALCA} \
65 --type ek \
66 --ek "${ek}" \
67 --dir "${workdir}" \
68 --vmid test \
69 --tpm2 \
70 --configfile "${workdir}/swtpm-localca.conf" \
71 --optsfile "${workdir}/swtpm-localca.options" \
72 --tpm-spec-family 2.0 --tpm-spec-revision 146 --tpm-spec-level 0 \
73 ${params:+${params}}; then
74 echo "Error: Test with parameters '$params' failed."
75 exit 1
76 fi
77
78 # Signing key should always be password protected
79 if ! grep -q "ENCRYPTED PRIVATE KEY" "${SIGNINGKEY}"; then
80 echo "Error: Signing key is not password protected."
81 exit 1
82 fi
83
84 # For the root CA's key we flip the password protection
85 if [ -n "${SWTPM_ROOTCA_PASSWORD}" ] ;then
86 if ! grep -q "ENCRYPTED PRIVATE KEY" "${workdir}/swtpm-localca-rootca-privkey.pem"; then
87 echo "Error: Root CA's private key is not password protected."
88 exit 1
89 fi
90 unset SWTPM_ROOTCA_PASSWORD
91 else
92 if grep -q "ENCRYPTED PRIVATE KEY" "${workdir}/swtpm-localca-rootca-privkey.pem"; then
93 echo "Error: Root CA's private key is password protected but should not be."
94 exit 1
95 fi
96 export SWTPM_ROOTCA_PASSWORD=xyz
97 fi
98
99 if [ ! -r "${workdir}/ek.cert" ]; then
100 echo "Error: ${workdir}/ek.cert was not created."
101 exit 1
102 fi
103
104 OIFS="$IFS"
105 IFS=","
106
107 for u in $usage; do
108 echo "$u"
109 if ! ${CERTTOOL} -i \
110 --inder --infile "${workdir}/ek.cert" | \
111 grep "Key Usage" -A2 | \
112 grep -q "$u"; then
113 echo "Error: Could not find key usage $u in key created " \
114 "with $params."
115 else
116 echo "Found '$u'"
117 fi
118 done
119
120 IFS="$OIFS"
121
122 ${CERTTOOL} \
123 -i \
124 --inder --infile "${workdir}/ek.cert" \
125 --outfile "${workdir}/ek.pem"
126
127 if ! ${CERTTOOL} \
128 --verify \
129 ${verify_profile:+${verify_profile}} \
130 --load-ca-certificate "${ISSUERCERT}" \
131 --infile "${workdir}/ek.pem"; then
132 echo "Error: Could not verify certificate chain."
133 exit 1
134 fi
135
136 # Delete all keys to have CA re-created
137 rm -rf "${workdir}"/*.pem
138 done
139
140 echo "Test 1: OK"
141 echo
142
143 #A few tests with odd vm Ids
144 for vmid in \
145 "s p a c e|s p a c e" \
146 "\$(ls)>foo|\$(ls)\>foo" \
147 "\`ls\`&; #12|\`ls\`&\; #12" \
148 "foo>&1<&2;\$(ls)|foo\>&1\<&2\;\$(ls)" \
149 "'*|'*" \
150 '"*|\"*' \
151 ":\$\$|:\$\$" \
152 "\${t}[]|\${t}[]";
153 do
154 in=$(echo "$vmid" | cut -d"|" -f1)
155 exp=$(echo "$vmid" | cut -d"|" -f2)
156
157 if ! ${SWTPM_LOCALCA} \
158 --type ek \
159 --ek "${ek}" \
160 --dir "${workdir}" \
161 --vmid "$in" \
162 --tpm2 \
163 --configfile "${workdir}/swtpm-localca.conf" \
164 --optsfile "${workdir}/swtpm-localca.options" \
165 --tpm-spec-family 2.0 --tpm-spec-revision 146 --tpm-spec-level 0 \
166 ${params:+${params}} &>/dev/null; then
167 echo "Error: Test with parameters '$params' failed."
168 exit 1
169 fi
170
171 if [ ! -r "${workdir}/ek.cert" ]; then
172 echo "Error: ${workdir}/ek.cert was not created."
173 exit 1
174 fi
175
176 ac=$(${CERTTOOL} -i --inder --infile "${workdir}/ek.cert" | \
177 sed -n "s/.*Subject: CN=\(.*\)$/\1/p")
178 if [ "$ac" != "$exp" ]; then
179 echo "Error: unexpected subject string"
180 echo "actual : $ac"
181 echo "expected : $exp"
182 else
183 echo "Pass: $ac"
184 fi
185 done
186
187 echo "Test 2: OK"
188
189 exit 0