Blame tests/search-all-matching-tokens.softhsm

Packit Service a0c135
#!/bin/sh
Packit Service a0c135
Packit Service a0c135
# Copyright (C) 2015 Nikos Mavrogiannopoulos
Packit Service a0c135
#
Packit Service a0c135
# GnuTLS is free software; you can redistribute it and/or modify it
Packit Service a0c135
# under the terms of the GNU General Public License as published by the
Packit Service a0c135
# Free Software Foundation; either version 3 of the License, or (at
Packit Service a0c135
# your option) any later version.
Packit Service a0c135
#
Packit Service a0c135
# GnuTLS is distributed in the hope that it will be useful, but
Packit Service a0c135
# WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a0c135
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service a0c135
# General Public License for more details.
Packit Service a0c135
#
Packit Service a0c135
# You should have received a copy of the GNU General Public License
Packit Service a0c135
# along with GnuTLS; if not, write to the Free Software Foundation,
Packit Service a0c135
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit Service a0c135
Packit Service a0c135
# This test checks if the search for objects in tokens will continue past the
Packit Service a0c135
# first token found.
Packit Service a0c135
#
Packit Service a0c135
# Generic PKCS#11 URIs are used to make the search to match more than one
Packit Service a0c135
# token. The search should be able to find the objects in each device, which are
Packit Service a0c135
# labeled differently per token.
Packit Service a0c135
#
Packit Service a0c135
# This test also contains a negative test to verify that the engine will not try
Packit Service a0c135
# to login to a token if more than one token matched the search. This is why it
Packit Service a0c135
# is required to have only one match to be able to use a private key.
Packit Service a0c135
Packit Service a0c135
outdir="output.$$"
Packit Service a0c135
Packit Service a0c135
# Load common test functions
Packit Service a0c135
. ${srcdir}/rsa-common.sh
Packit Service a0c135
Packit Service a0c135
PIN=1234
Packit Service a0c135
PUK=1234
Packit Service a0c135
Packit Service a0c135
NUM_DEVICES=5
Packit Service a0c135
Packit Service a0c135
# Initialize the SoftHSM DB
Packit Service a0c135
init_db
Packit Service a0c135
Packit Service a0c135
# Create some devices
Packit Service a0c135
create_devices $NUM_DEVICES $PIN $PUK "libp11-test" "label"
Packit Service a0c135
Packit Service a0c135
sed -e "s|@MODULE_PATH@|${MODULE}|g" -e "s|@ENGINE_PATH@|../src/.libs/pkcs11.so|g" <"${srcdir}/engines.cnf.in" >"${outdir}/engines.cnf"
Packit Service a0c135
Packit Service a0c135
export OPENSSL_ENGINES="../src/.libs/"
Packit Service a0c135
export OPENSSL_CONF="${outdir}/engines.cnf"
Packit Service a0c135
Packit Service a0c135
PRIVATE_KEY="pkcs11:token=libp11-test-3;object=label-3;type=private;pin-value=1234"
Packit Service a0c135
PRIVATE_KEY_WITHOUT_TOKEN="pkcs11:object=label-3;type=private;pin-value=1234"
Packit Service a0c135
PUBLIC_KEY_ANY="pkcs11:type=public"
Packit Service a0c135
CERTIFICATE="pkcs11:object=label-3;type=cert;pin-value=1234"
Packit Service a0c135
Packit Service a0c135
# Create input file
Packit Service a0c135
echo "secret" > "${outdir}/in.txt"
Packit Service a0c135
Packit Service a0c135
# Verify that it doesn't try to login if more than one token matched the search
Packit Service a0c135
openssl pkeyutl -engine pkcs11 -keyform engine \
Packit Service a0c135
	-inkey "${PRIVATE_KEY_WITHOUT_TOKEN}" \
Packit Service a0c135
	-sign -out "${outdir}/signature.bin" -in "${outdir}/in.txt"
Packit Service a0c135
if test $? = 0;then
Packit Service a0c135
	echo "Did not fail when the PKCS#11 URI matched multiple tokens"
Packit Service a0c135
	exit 1;
Packit Service a0c135
fi
Packit Service a0c135
Packit Service a0c135
# Generate signature specifying the token in the PKCS#11 URI
Packit Service a0c135
openssl pkeyutl -engine pkcs11 -keyform engine -inkey "${PRIVATE_KEY}" \
Packit Service a0c135
	-sign -out "${outdir}/signature.bin" -in "${outdir}/in.txt"
Packit Service a0c135
if test $? != 0;then
Packit Service a0c135
	echo "Failed to sign file using PKCS#11 URI ${PRIVATE_KEY}"
Packit Service a0c135
	exit 1;
Packit Service a0c135
fi
Packit Service a0c135
Packit Service a0c135
# Verify the signature using the public key from each token
Packit Service a0c135
i=0
Packit Service a0c135
while [ $i -le ${NUM_DEVICES} ]; do
Packit Service a0c135
	pubkey="pkcs11:object=label-$i;type=public;pin-value=1234"
Packit Service a0c135
	openssl pkeyutl -engine pkcs11 -keyform engine -pubin -inkey "${pubkey}" \
Packit Service a0c135
		-verify -sigfile "${outdir}/signature.bin" -in "${outdir}/in.txt"
Packit Service a0c135
	if test $? != 0;then
Packit Service a0c135
		echo "Failed to verify the signature using the PKCS#11 URI ${pubkey}"
Packit Service a0c135
		exit 1;
Packit Service a0c135
	fi
Packit Service a0c135
	i=$(($i + 1))
Packit Service a0c135
done
Packit Service a0c135
Packit Service a0c135
# Verify the signature using a certificate without specifying the token
Packit Service a0c135
openssl pkeyutl -engine pkcs11 -keyform engine -pubin -inkey "${CERTIFICATE}" \
Packit Service a0c135
	-verify -sigfile "${outdir}/signature.bin" -in "${outdir}/in.txt"
Packit Service a0c135
if test $? != 0;then
Packit Service a0c135
	echo "Failed to verify the signature using the PKCS#11 URI ${CERTIFICATE}"
Packit Service a0c135
	exit 1;
Packit Service a0c135
fi
Packit Service a0c135
Packit Service a0c135
# Verify the signature using the first public key found
Packit Service a0c135
openssl pkeyutl -engine pkcs11 -keyform engine -pubin -inkey "${PUBLIC_KEY_ANY}" \
Packit Service a0c135
	-verify -sigfile "${outdir}/signature.bin" -in "${outdir}/in.txt"
Packit Service a0c135
if test $? != 0;then
Packit Service a0c135
	echo "Failed to verify the signature using the PKCS#11 URI ${PUBLIC_KEY_ANY}."
Packit Service a0c135
	exit 1;
Packit Service a0c135
fi
Packit Service a0c135
Packit Service a0c135
rm -rf "$outdir"
Packit Service a0c135
Packit Service a0c135
exit 0