Debian Codegraph @

script/tests/softhsm_setup

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