| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright © 2024 Mobi - Com Polska Sp. z o.o. |
| 4 |
# Author: Małgorzata Olszówka <[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 test checks if it is possible to use the keys without specifying the |
| 20 |
# token if there is only one initialized token available. |
| 21 |
|
| 22 |
outdir="output.$$" |
| 23 |
|
| 24 |
# Load common test functions |
| 25 |
. ${srcdir}/common.sh |
| 26 |
|
| 27 |
if [[ "${OPENSSL_VERSION}" =~ ^[012].* ]]; then |
| 28 |
echo "Skipping provider tests with OpenSSL ${OPENSSL_VERSION}" |
| 29 |
rm -rf "$outdir" |
| 30 |
exit 77 |
| 31 |
fi |
| 32 |
|
| 33 |
# These URIs don't contain the token specification |
| 34 |
PRIVATE_KEY="pkcs11:object=server-key-0;type=private;pin-value=${PIN}" |
| 35 |
PUBLIC_KEY="pkcs11:object=server-key-0;type=public" |
| 36 |
CERTIFICATE="pkcs11:object=server-key-0;type=cert" |
| 37 |
|
| 38 |
# Do the token initialization |
| 39 |
init_token "rsa" "1" "libp11" ${ID} "server-key" "privkey" "pubkey" "cert" |
| 40 |
|
| 41 |
# Ensure the use of the locally built provider; applies after running 'pkcs11-tool' |
| 42 |
unset OPENSSL_ENGINES |
| 43 |
export OPENSSL_MODULES="../src/.libs/" |
| 44 |
export PKCS11_MODULE_PATH=${MODULE} |
| 45 |
echo "OPENSSL_MODULES=${OPENSSL_MODULES}" |
| 46 |
echo "PKCS11_MODULE_PATH=${PKCS11_MODULE_PATH}" |
| 47 |
|
| 48 |
# Create input file |
| 49 |
echo "secret" >"${outdir}/in.txt" |
| 50 |
|
| 51 |
# Load openssl settings |
| 52 |
. ${srcdir}/openssl-settings.sh |
| 53 |
|
| 54 |
# Restore openssl settings |
| 55 |
trap cleanup EXIT |
| 56 |
|
| 57 |
# Run the test |
| 58 |
# Generate signature without specifying the token in the PKCS#11 URI |
| 59 |
${WRAPPER} ${OPENSSL} pkeyutl -provider pkcs11prov -provider default \ |
| 60 |
-inkey ${PRIVATE_KEY} -sign -out "${outdir}/signature.bin" \ |
| 61 |
-in "${outdir}/in.txt" |
| 62 |
if [[ $? -ne 0 ]]; then |
| 63 |
echo "Failed to generate signature using PKCS#11 URI ${PRIVATE_KEY}" |
| 64 |
exit 1 |
| 65 |
fi |
| 66 |
|
| 67 |
# Verify the signature using the public without specifying the token |
| 68 |
${OPENSSL} pkeyutl -provider pkcs11prov -provider default -pubin \ |
| 69 |
-inkey ${PUBLIC_KEY} -verify -sigfile "${outdir}/signature.bin" \ |
| 70 |
-in "${outdir}/in.txt" |
| 71 |
if [[ $? -ne 0 ]]; then |
| 72 |
echo "Failed to verify signature using PKCS#11 URI ${PUBLIC_KEY}" |
| 73 |
exit 1 |
| 74 |
fi |
| 75 |
|
| 76 |
# Verify the signature using a certificate without specifying the token |
| 77 |
${OPENSSL} pkeyutl -provider pkcs11prov -provider default -certin \ |
| 78 |
-inkey ${PUBLIC_KEY} -verify -sigfile "${outdir}/signature.bin" \ |
| 79 |
-in "${outdir}/in.txt" |
| 80 |
if [[ $? -ne 0 ]]; then |
| 81 |
echo "Failed to verify signature using PKCS#11 URI ${PUBLIC_KEY}" |
| 82 |
exit 1 |
| 83 |
fi |
| 84 |
|
| 85 |
# Compute the SHA-256 binary digest of the input file |
| 86 |
${OPENSSL} dgst -sha256 -binary -out "${outdir}/hash.bin" "${outdir}/in.txt" |
| 87 |
|
| 88 |
# Sign the SHA-256 digest with an RSA key using RSA-PSS (raw input) |
| 89 |
${OPENSSL} pkeyutl -provider pkcs11prov -provider default \ |
| 90 |
-inkey ${PRIVATE_KEY} -sign -rawin -pkeyopt rsa_padding_mode:pss \ |
| 91 |
-out "${outdir}/signature.bin" -in "${outdir}/hash.bin" |
| 92 |
|
| 93 |
if [[ $? -ne 0 ]]; then |
| 94 |
echo "Failed to sign a digest using RSA-PSS" |
| 95 |
exit 1 |
| 96 |
fi |
| 97 |
|
| 98 |
rm -rf "$outdir" |
| 99 |
|
| 100 |
exit 0 |