Debian Codegraph @

tests/softhsm_setup

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