Debian Codegraph @

tests/softhsm_setup

raw
1 #!/usr/bin/env bash
2
3 # SPDX-License-Identifier: GPL-2.0 and BSD-3-clause
4 # This program originates from 'swtpm' project (https://github.com/stefanberger/swtpm/)
5
6 if [ -z "$(type -P p11tool)" ]; then
7 echo "Need p11tool from gnutls"
8 exit 77
9 fi
10
11 if [ -z "$(type -P softhsm2-util)" ]; then
12 echo "Need softhsm2-util from softhsm2 package"
13 exit 77
14 fi
15
16 MAJOR=$(softhsm2-util -v | cut -d '.' -f1)
17 MINOR=$(softhsm2-util -v | cut -d '.' -f2)
18 if [ ${MAJOR} -lt 2 ] || [ ${MAJOR} -eq 2 -a ${MINOR} -lt 2 ]; then
19 echo "Need softhsm v2.2.0 or later"
20 exit 77
21 fi
22
23 NAME=swtpm-test
24 PIN=${PIN:-1234}
25 SO_PIN=${SO_PIN:-1234}
26 SOFTHSM_SETUP_CONFIGDIR=${SOFTHSM_SETUP_CONFIGDIR:-~/.config/softhsm2}
27 export SOFTHSM2_CONF=${SOFTHSM_SETUP_CONFIGDIR}/softhsm2.conf
28
29 UNAME_S="$(uname -s)"
30
31 case "${UNAME_S}" in
32 Darwin)
33 msg=$(sudo -v -n)
34 if [ $? -ne 0 ]; then
35 echo "Need password-less sudo rights on OS X to change /etc/gnutls/pkcs11.conf"
36 exit 1
37 fi
38 ;;
39 esac
40
41 teardown_softhsm() {
42 local configdir=${SOFTHSM_SETUP_CONFIGDIR}
43 local configfile=${SOFTHSM2_CONF}
44 local bakconfigfile=${configfile}.bak
45 local tokendir=${configdir}/tokens
46
47 softhsm2-util --token "${NAME}" --delete-token &>/dev/null
48
49 case "${UNAME_S}" in
50 Darwin*)
51 if [ -f /etc/gnutls/pkcs11.conf.bak ]; then
52 sudo rm -f /etc/gnutls/pkcs11.conf
53 sudo mv /etc/gnutls/pkcs11.conf.bak \
54 /etc/gnutls/pkcs11.conf &>/dev/null
55 fi
56 ;;
57 esac
58
59 if [ -f "$bakconfigfile" ]; then
60 mv "$bakconfigfile" "$configfile"
61 else
62 rm -f "$configfile"
63 fi
64 if [ -d "$tokendir" ]; then
65 rm -rf "${tokendir}"
66 fi
67 return 0
68 }
69
70 setup_softhsm() {
71 local msg tokenuri keyuri
72 local configdir=${SOFTHSM_SETUP_CONFIGDIR}
73 local configfile=${SOFTHSM2_CONF}
74 local bakconfigfile=${configfile}.bak
75 local tokendir=${configdir}/tokens
76 local rc
77
78 case "${UNAME_S}" in
79 Darwin*)
80 if [ -f /etc/gnutls/pkcs11.conf.bak ]; then
81 echo "/etc/gnutls/pkcs11.conf.bak already exists; need to 'teardown' first"
82 return 1
83 fi
84 sudo mv /etc/gnutls/pkcs11.conf \
85 /etc/gnutls/pkcs11.conf.bak &>/dev/null
86 if [ $(id -u) -eq 0 ]; then
87 SONAME="$(sudo -u nobody brew ls --verbose softhsm | \
88 grep -E "\.so$")"
89 else
90 SONAME="$(brew ls --verbose softhsm | \
91 grep -E "\.so$")"
92 fi
93 sudo mkdir -p /etc/gnutls &>/dev/null
94 sudo bash -c "echo "load=${SONAME}" > /etc/gnutls/pkcs11.conf"
95 ;;
96 esac
97
98 if ! [ -d $configdir ]; then
99 mkdir -p $configdir
100 fi
101 mkdir -p ${tokendir}
102
103 if [ -f $configfile ]; then
104 mv "$configfile" "$bakconfigfile"
105 fi
106
107 if ! [ -f $configfile ]; then
108 cat <<_EOF_ > $configfile
109 directories.tokendir = ${tokendir}
110 objectstore.backend = file
111 log.level = DEBUG
112 slots.removable = false
113 _EOF_
114 fi
115
116 msg=$(p11tool --list-tokens 2>&1 | grep "token=${NAME}" | tail -n1)
117 if [ $? -ne 0 ]; then
118 echo "Could not list existing tokens"
119 echo "$msg"
120 fi
121 tokenuri=$(echo "$msg" | sed -n 's/.*URL: \([[:print:]*]\)/\1/p')
122
123 if [ -z "$tokenuri" ]; then
124 msg=$(softhsm2-util \
125 --init-token --pin ${PIN} --so-pin ${SO_PIN} \
126 --free --label ${NAME} 2>&1)
127 if [ $? -ne 0 ]; then
128 echo "Could not initialize token"
129 echo "$msg"
130 return 2
131 fi
132
133 slot=$(echo "$msg" | \
134 sed -n 's/.* reassigned to slot \([0-9]*\)$/\1/p')
135 if [ -z "$slot" ]; then
136 slot=$(softhsm2-util --show-slots | \
137 grep -E "^Slot " | head -n1 |
138 sed -n 's/Slot \([0-9]*\)/\1/p')
139 if [ -z "$slot" ]; then
140 echo "Could not parse slot number from output."
141 echo "$msg"
142 return 3
143 fi
144 fi
145
146 msg=$(p11tool --list-tokens 2>&1 | \
147 grep "token=${NAME}" | tail -n1)
148 if [ $? -ne 0 ]; then
149 echo "Could not list existing tokens"
150 echo "$msg"
151 fi
152 tokenuri=$(echo "$msg" | sed -n 's/.*URL: \([[:print:]*]\)/\1/p')
153 if [ -z "${tokenuri}" ]; then
154 echo "Could not get tokenuri!"
155 return 4
156 fi
157
158 # more recent versions of p11tool have --generate-privkey ...
159 msg=$(GNUTLS_PIN=$PIN p11tool \
160 --generate-privkey=rsa --bits 2048 --label mykey --login \
161 "${tokenuri}" 2>&1)
162 if [ $? -ne 0 ]; then
163 # ... older versions have --generate-rsa
164 msg=$(GNUTLS_PIN=$PIN p11tool \
165 --generate-rsa --bits 2048 --label mykey --login \
166 "${tokenuri}" 2>&1)
167 if [ $? -ne 0 ]; then
168 echo "Could not create RSA key!"
169 echo "$msg"
170 return 5
171 fi
172 fi
173 fi
174
175 getkeyuri_softhsm $slot
176 rc=$?
177 if [ $rc -ne 0 ]; then
178 teardown_softhsm
179 fi
180
181 return $rc
182 }
183
184 _getkeyuri_softhsm() {
185 local msg tokenuri keyuri
186
187 msg=$(p11tool --list-tokens 2>&1 | grep "token=${NAME}")
188 if [ $? -ne 0 ]; then
189 echo "Could not list existing tokens"
190 echo "$msg"
191 return 5
192 fi
193 tokenuri=$(echo "$msg" | sed -n 's/.*URL: \([[:print:]*]\)/\1/p')
194 if [ -z "$tokenuri" ]; then
195 echo "Could not get token URL"
196 echo "$msg"
197 return 6
198 fi
199 msg=$(p11tool --list-all ${tokenuri} 2>&1)
200 if [ $? -ne 0 ]; then
201 echo "Could not list object under token $tokenuri"
202 echo "$msg"
203 softhsm2-util --show-slots
204 return 7
205 fi
206
207 keyuri=$(echo "$msg" | sed -n 's/.*URL: \([[:print:]*]\)/\1/p')
208 if [ -z "$keyuri" ]; then
209 echo "Could not get key URL"
210 echo "$msg"
211 return 8
212 fi
213 echo "$keyuri"
214 return 0
215 }
216
217 getkeyuri_softhsm() {
218 local keyuri rc
219
220 keyuri=$(_getkeyuri_softhsm)
221 rc=$?
222 if [ $rc -ne 0 ]; then
223 return $rc
224 fi
225 echo "keyuri: $keyuri?pin-value=${PIN}" #&module-name=softhsm2"
226 return 0
227 }
228
229 getpubkey_softhsm() {
230 local keyuri rc
231
232 keyuri=$(_getkeyuri_softhsm)
233 rc=$?
234 if [ $rc -ne 0 ]; then
235 return $rc
236 fi
237 GNUTLS_PIN=${PIN} p11tool --export-pubkey "${keyuri}" --login 2>/dev/null
238 return $?
239 }
240
241 usage() {
242 cat <<_EOF_
243 Usage: $0 [command]
244
245 Supported commands are:
246
247 setup : Setup the user's account for softhsm and create a
248 token and key with a test configuration
249
250 getkeyuri : Get the key's URI; may only be called after setup
251
252 getpubkey : Get the public key in PEM format; may only be called after setup
253
254 teardown : Remove the temporary softhsm test configuration
255
256 _EOF_
257 }
258
259 main() {
260 local ret
261
262 if [ $# -lt 1 ]; then
263 usage $0
264 echo -e "Missing command.\n\n"
265 return 1
266 fi
267 case "$1" in
268 setup)
269 setup_softhsm
270 ret=$?
271 ;;
272 getkeyuri)
273 getkeyuri_softhsm
274 ret=$?
275 ;;
276 getpubkey)
277 getpubkey_softhsm
278 ret=$?
279 ;;
280 teardown)
281 teardown_softhsm
282 ret=$?
283 ;;
284 *)
285 echo -e "Unsupported command: $1\n\n"
286 usage $0
287 ret=1
288 esac
289 return $ret
290 }
291
292 main "$@"
293 exit $?