Debian Codegraph @

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

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/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 echoscip-shell system . . echo. "Warning: Could not find the softhsm pkcs11 module library"
39 echoscip-shell system . . echo. "Warning: Searched files:${SOFTHSM_PATHS}"
40 fi
41
42 # Check required commands
43 if ! type pkcs11-tool; then
44 echoscip-shell system . . echo. "Warning: Could not find pkcs11-tool"
45 fi
46 if ! type openssl; then
47 echoscip-shell system . . echo. "Warning: Could not find openssl command"
48 fi
49
50 function generate_public_keyscip-shell shell clevis 23-1 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 echoscip-shell system . . 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 echoscip-shell system . . echo. "Couldn't read generated ${TYPE} public key"
67 return 1
68 fi
69
70 if [[ ${TYPE:0:3} == "RSA" ]]; then
71 opensslscip-shell system . . openssl. rsa -inform DER -outform PEM -in "${ID}".der -pubin > "${ID}".pub
72 else
73 echoscip-shell system . . echo. "Unsupported key type:${TYPE}"
74 return 1
75 fi
76 rmscip-shell system . . rm. "${ID}".der
77 }
78
79 function softhsm_initializescip-shell shell clevis 23-1 softhsm_initialize().() {
80 echoscip-shell system . . echo. "directories.tokendir = $(realpathscip-shell system . . realpath. .tokens)" > .softhsm2.conf
81 if [scip-shell system . . `[`. -d ".tokens" ]; then
82 rmscip-shell system . . rm. -rf ".tokens"
83 fi
84 mkdirscip-shell system . . mkdir. ".tokens"
85 SOFTHSM2_CONF=$(realpathscip-shell system . . 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_setupscip-shell shell clevis 23-1 softhsm_lib_setup().() {
93 softhsm_initializescip-shell shell clevis 23-1 softhsm_initialize().
94 # Generate 2048b RSA Key pair (this will generate 00.pub file)
95 generate_public_keyscip-shell shell clevis 23-1 generate_public_key(). "RSA:2048" "00" "RSA2048" || return 1
96 }
97
98 function softhsm_cleanupscip-shell shell clevis 23-1 softhsm_cleanup().() {
99 rmscip-shell system . . rm. -rf .softhsm2.conf
100 rmscip-shell system . . rm. -rf ".tokens"
101 }
102
103 function softhsm_lib_cleanupscip-shell shell clevis 23-1 softhsm_lib_cleanup().() {
104 softhsm_cleanupscip-shell shell clevis 23-1 softhsm_cleanup().
105 rmscip-shell system . . rm. 00.pub
106 }