Debian Codegraph @

src/pins/pkcs11/tests/pkcs11-common-tests

raw symbols SCIP
1 #!/bin/bash
2 #
3 # Copyright (c) 2024 Red Hat, Inc.
4 # Author: Sergio Arroutbi <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>
18 #
19 # This file is based on OpenSC common test infrastructure:
20 # https://github.com/OpenSC/OpenSC/blob/master/tests/common.sh
21 #
22 SOPIN="12345678"
23 PIN="123456"
24 P11LIB=""
25 SOFTHSM_PATHS="/usr/local/lib/softhsm/libsofthsm2.so \
26 /usr/lib/softhsm/libsofthsm2.so \
27 /usr/lib64/pkcs11/libsofthsm2.so"
28
29 for hsmlib in ${SOFTHSM_PATHS}; do
30 if [[ -f "${hsmlib}" ]]; then
31 P11LIB="${hsmlib}"
32 break
33 fi
34 done
35
36 # Check required libraries
37 if [[ -z "${P11LIB}" ]]; then
38 echo "Warning: Could not find the softhsm pkcs11 module library"
39 echo "Warning: Searched files:${SOFTHSM_PATHS}"
40 fi
41
42 # Check required commands
43 if ! type pkcs11-tool; then
44 echo "Warning: Could not find pkcs11-tool"
45 fi
46 if ! type openssl; then
47 echo "Warning: Could not find openssl command"
48 fi
49
50 function generate_public_key() {
51 TYPE="$1"
52 ID="$2"
53 LABEL="$3"
54
55 # Generate key pair
56 if ! pkcs11-tool --keypairgen --key-type="${TYPE}" --login --pin="${PIN}" \
57 --module="${P11LIB}" --label="${LABEL}" --id="${ID}"; then
58 echo "Couldn't generate ${TYPE} key pair"
59 return 1
60 fi
61
62 # Extract public key
63 if ! pkcs11-tool --read-object --id "${ID}" --type pubkey --output-file \
64 "${ID}".der --module="${P11LIB}";
65 then
66 echo "Couldn't read generated ${TYPE} public key"
67 return 1
68 fi
69
70 if [[ ${TYPE:0:3} == "RSA" ]]; then
71 openssl rsa -inform DER -outform PEM -in "${ID}".der -pubin > "${ID}".pub
72 else
73 echo "Unsupported key type:${TYPE}"
74 return 1
75 fi
76 rm "${ID}".der
77 }
78
79 function softhsm_initialize() {
80 echo "directories.tokendir = $(realpath .tokens)" > .softhsm2.conf
81 if [ -d ".tokens" ]; then
82 rm -rf ".tokens"
83 fi
84 mkdir ".tokens"
85 SOFTHSM2_CONF=$(realpath ".softhsm2.conf")
86 export SOFTHSM2_CONF
87 # Init token
88 softhsm2-util --init-token --slot 0 --label "Clevis PKCS11 test" \
89 --so-pin="${SOPIN}" --pin="${PIN}"
90 }
91
92 function softhsm_lib_setup() {
93 softhsm_initialize
94 # Generate 2048b RSA Key pair (this will generate 00.pub file)
95 generate_public_key "RSA:2048" "00" "RSA2048" || return 1
96 }
97
98 function softhsm_cleanup() {
99 rm -rf .softhsm2.conf
100 rm -rf ".tokens"
101 }
102
103 function softhsm_lib_cleanup() {
104 softhsm_cleanup
105 rm 00.pub
106 }