Debian Codegraph @

tests/search-all-matching-tokens.softhsm

raw
1 #!/bin/bash
2
3 # Copyright © 2024 Mobi - Com Polska Sp. z o.o.
4 # Author: Małgorzata Olszówka <[email protected]>
5 # Copyright (C) 2015 Nikos Mavrogiannopoulos
6 #
7 # GnuTLS is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 3 of the License, or (at
10 # your option) any later version.
11 #
12 # GnuTLS is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GnuTLS; if not, write to the Free Software Foundation,
19 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 # This test checks if the search for objects in tokens will continue past the
22 # first token found.
23 #
24 # Generic PKCS#11 URIs are used to make the search to match more than one
25 # token. The search should be able to find the objects in each device, which are
26 # labeled differently per token.
27 #
28 # This test also contains a negative test to verify that the engine will not try
29 # to login to a token if more than one token matched the search. This is why it
30 # is required to have only one match to be able to use a private key.
31
32 outdir="output.$$"
33
34 PRIVATE_KEY="pkcs11:token=libp11-3;object=label-3;type=private;pin-value=1234"
35 PRIVATE_KEY_WITHOUT_TOKEN="pkcs11:object=label-3;type=private;pin-value=1234"
36 PUBLIC_KEY_ANY="pkcs11:type=public"
37 CERTIFICATE="pkcs11:object=label-3;type=cert;pin-value=1234"
38 NUM_DEVICES=5
39
40 # Load common test functions
41 . ${srcdir}/common.sh
42
43 export OPENSSL_CONF="${outdir}/engines.cnf"
44 echo "OPENSSL_CONF=${OPENSSL_CONF}"
45
46 # Do the token initialization, create some devices
47 init_token "rsa" ${NUM_DEVICES} "libp11" ${ID} "label" "privkey" "pubkey" "cert"
48
49 # Create the input file
50 echo "secret" > "${outdir}/in.txt"
51
52 # Load openssl settings
53 . ${srcdir}/openssl-settings.sh
54
55 if ! "${OPENSSL}" engine >/dev/null 2>&1; then
56 echo "Skipping engine tests: ENGINE support not available"
57 rm -rf "$outdir"
58 exit 77
59 fi
60
61 # Restore openssl settings
62 trap cleanup EXIT
63
64 # Run the test
65 # Verify that it doesn't try to login if more than one token matched the search
66 ${WRAPPER} ${OPENSSL} pkeyutl -engine pkcs11 -keyform engine \
67 -inkey ${PRIVATE_KEY_WITHOUT_TOKEN} -sign \
68 -out "${outdir}/signature.bin" -in "${outdir}/in.txt"
69 if [[ $? -eq 0 ]]; then
70 echo "Did not fail when the PKCS#11 URI matched multiple tokens"
71 exit 1
72 fi
73
74 # Generate signature specifying the token in the PKCS#11 URI
75 ${OPENSSL} pkeyutl -engine pkcs11 -keyform engine \
76 -inkey ${PRIVATE_KEY} -sign -out "${outdir}/signature.bin" \
77 -in "${outdir}/in.txt"
78 if [[ $? -ne 0 ]]; then
79 echo "Failed to sign file using PKCS#11 URI ${PRIVATE_KEY}"
80 exit 1
81 fi
82
83 # Verify the signature using the public key from each token
84 i=0
85 while [[ $i -lt ${NUM_DEVICES} ]]; do
86 pubkey="pkcs11:object=label-$i;type=public;pin-value=1234"
87 ${OPENSSL} pkeyutl -engine pkcs11 -keyform engine -pubin \
88 -inkey ${pubkey} -verify -sigfile "${outdir}/signature.bin" \
89 -in "${outdir}/in.txt"
90 if [[ $? -ne 0 ]]; then
91 echo "Failed to verify the signature using the PKCS#11 URI ${pubkey}"
92 exit 1
93 fi
94 i=$(($i + 1))
95 done
96
97 # Verify the signature using a certificate without specifying the token
98 ${OPENSSL} pkeyutl -engine pkcs11 -keyform engine -pubin \
99 -inkey ${CERTIFICATE} -verify -sigfile "${outdir}/signature.bin" \
100 -in "${outdir}/in.txt"
101 if [[ $? -ne 0 ]]; then
102 echo "Failed to verify the signature using the PKCS#11 URI ${CERTIFICATE}"
103 exit 1
104 fi
105
106 # Verify the signature using the first public key found
107 ${OPENSSL} pkeyutl -engine pkcs11 -keyform engine -pubin \
108 -inkey ${PUBLIC_KEY_ANY} -verify -sigfile "${outdir}/signature.bin" \
109 -in "${outdir}/in.txt"
110 if [[ $? -ne 0 ]]; then
111 echo "Failed to verify the signature using the PKCS#11 URI ${PUBLIC_KEY_ANY}."
112 exit 1
113 fi
114
115 rm -rf "$outdir"
116
117 exit 0