Blame tests/p11-kit-load.sh

Packit Service 4684c1
#!/bin/sh
Packit Service 4684c1
Packit Service 4684c1
# Copyright (C) 2017 Red Hat, Inc.
Packit Service 4684c1
#
Packit Service 4684c1
# This file is part of p11-kit.
Packit Service 4684c1
#
Packit Service 4684c1
# p11-kit is free software; you can redistribute it and/or modify it
Packit Service 4684c1
# under the terms of the GNU General Public License as published by the
Packit Service 4684c1
# Free Software Foundation; either version 3 of the License, or (at
Packit Service 4684c1
# your option) any later version.
Packit Service 4684c1
#
Packit Service 4684c1
# p11-kit is distributed in the hope that it will be useful, but
Packit Service 4684c1
# WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
# General Public License for more details.
Packit Service 4684c1
#
Packit Service 4684c1
# You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
# along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 4684c1
Packit Service 4684c1
#set -e
Packit Service 4684c1
Packit Service 4684c1
srcdir="${srcdir:-.}"
Packit Service 4684c1
builddir="${builddir:-.}"
Packit Service 4684c1
CERTTOOL="${CERTTOOL:-../src/certtool${EXEEXT}}"
Packit Service 4684c1
P11TOOL="${P11TOOL:-../src/p11tool${EXEEXT}}"
Packit Service 4684c1
DIFF="${DIFF:-diff}"
Packit Service 4684c1
PKGCONFIG="${PKG_CONFIG:-$(which pkg-config)}"
Packit Service 4684c1
TMP_SOFTHSM_DIR="./softhsm-load.$$.tmp"
Packit Service 4684c1
P11DIR="p11-kit-conf.$$.tmp"
Packit Service 4684c1
PIN=1234
Packit Service 4684c1
PUK=1234
Packit Service 4684c1
Packit Service 4684c1
for lib in ${libdir} ${libdir}/pkcs11 /usr/lib64/pkcs11/ /usr/lib/pkcs11/ /usr/lib/x86_64-linux-gnu/pkcs11/;do
Packit Service 4684c1
	if test -f "${lib}/p11-kit-trust.so"; then
Packit Service 4684c1
		TRUST_MODULE="${lib}/p11-kit-trust.so"
Packit Service 4684c1
		echo "located ${MODULE}"
Packit Service 4684c1
		break
Packit Service 4684c1
	fi
Packit Service 4684c1
done
Packit Service 4684c1
Packit Service 4684c1
for lib in ${libdir} ${libdir}/pkcs11 /usr/lib64/pkcs11/ /usr/lib/pkcs11/ /usr/lib/x86_64-linux-gnu/pkcs11/ /usr/lib/softhsm/;do
Packit Service 4684c1
	if test -f "${lib}/libsofthsm2.so"; then
Packit Service 4684c1
		SOFTHSM_MODULE="${lib}/libsofthsm2.so"
Packit Service 4684c1
		echo "located ${MODULE}"
Packit Service 4684c1
		break
Packit Service 4684c1
	fi
Packit Service 4684c1
done
Packit Service 4684c1
Packit Service 4684c1
${PKGCONFIG} --version >/dev/null || exit 77
Packit Service 4684c1
Packit Service 4684c1
${PKGCONFIG} --atleast-version=0.23.10 p11-kit-1
Packit Service 4684c1
if test $? != 0;then
Packit Service 4684c1
	echo p11-kit 0.23.10 is required
Packit Service 4684c1
	exit 77
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
if ! test -f "${TRUST_MODULE}"; then
Packit Service 4684c1
	echo "p11-kit trust module was not found"
Packit Service 4684c1
	exit 77
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
if ! test -f "${SOFTHSM_MODULE}"; then
Packit Service 4684c1
	echo "softhsm module was not found"
Packit Service 4684c1
	exit 77
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Create pkcs11.conf with two modules, a trusted (p11-kit-trust)
Packit Service 4684c1
# and softhsm (not trusted)
Packit Service 4684c1
mkdir -p ${P11DIR}
Packit Service 4684c1
Packit Service 4684c1
cat <<_EOF_ >${P11DIR}/p11-kit-trust.module
Packit Service 4684c1
module: p11-kit-trust.so
Packit Service 4684c1
trust-policy: yes
Packit Service 4684c1
_EOF_
Packit Service 4684c1
Packit Service 4684c1
cat <<_EOF_ >${P11DIR}/softhsm.module
Packit Service 4684c1
module: libsofthsm2.so
Packit Service 4684c1
_EOF_
Packit Service 4684c1
Packit Service 4684c1
# Setup softhsm
Packit Service 4684c1
rm -rf ${TMP_SOFTHSM_DIR}
Packit Service 4684c1
mkdir -p ${TMP_SOFTHSM_DIR}
Packit Service 4684c1
SOFTHSM2_CONF=${TMP_SOFTHSM_DIR}/conf
Packit Service 4684c1
export SOFTHSM2_CONF
Packit Service 4684c1
echo "objectstore.backend = file" > "${SOFTHSM2_CONF}"
Packit Service 4684c1
echo "directories.tokendir = ${TMP_SOFTHSM_DIR}" >> "${SOFTHSM2_CONF}"
Packit Service 4684c1
Packit Service 4684c1
softhsm2-util --init-token --slot 0 --label "GnuTLS-Test" --so-pin "${PUK}" --pin "${PIN}" >/dev/null #2>&1
Packit Service 4684c1
if test $? != 0; then
Packit Service 4684c1
	echo "failed to initialize softhsm"
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
GNUTLS_PIN="${PIN}" ${P11TOOL} --login --label GnuTLS-Test-RSA --generate-privkey rsa --provider "${SOFTHSM_MODULE}" pkcs11: --outfile /dev/null
Packit Service 4684c1
if test $? != 0; then
Packit Service 4684c1
	echo "failed to generate privkey"
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
FILTERTOKEN="sed s/token=.*//g"
Packit Service 4684c1
Packit Service 4684c1
# Check whether both are listed
Packit Service 4684c1
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -a|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
#nr=$(${P11TOOL} --list-tokens|grep 'Module:'|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 2;then
Packit Service 4684c1
	echo "Error: did not find 2 modules ($nr)"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR}
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether whether list-tokens will list the trust module
Packit Service 4684c1
# if we only load softhsm. It shouldn't as we only load the
Packit Service 4684c1
# trust module when needed (e.g., verification).
Packit Service 4684c1
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -s "${SOFTHSM_MODULE}"|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 1;then
Packit Service 4684c1
	echo "Error: did not find softhsm module"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -s "${SOFTHSM_MODULE}"
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether both modules are found when gnutls_pkcs11_init
Packit Service 4684c1
# is not called but a pkcs11 operation is called.
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -d|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 2;then
Packit Service 4684c1
	echo "Error in test 1: did not find 2 modules"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -d
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether both modules are found when gnutls_pkcs11_init 
Packit Service 4684c1
# is called with the auto flag
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -a|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 2;then
Packit Service 4684c1
	echo "Error in test 2: did not find 2 modules"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -a
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether only trusted modules are listed when the
Packit Service 4684c1
# trusted flag is given to gnutls_pkcs11_init().
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -t|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 1;then
Packit Service 4684c1
	echo "Error in test 3: did not find the trusted module"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -t
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether only trusted is listed after certificate verification
Packit Service 4684c1
# is performed.
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -v|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 1;then
Packit Service 4684c1
	echo "Error in test 4: did not find 1 module"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -v
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether only trusted is listed when gnutls_pkcs11_init
Packit Service 4684c1
# is called with manual flag and a certificate verification is performed.
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -v|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 1;then
Packit Service 4684c1
	echo "Error in test 5: did not find 1 module"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -v
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether all modules are listed after certificate verification
Packit Service 4684c1
# is performed then a PKCS#11 function is called.
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -v -d|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 2;then
Packit Service 4684c1
	echo "Error in test 6: did not find all modules"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -v -d
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether all modules are listed after a private key operation.
Packit Service 4684c1
nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -p|${FILTERTOKEN}|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 2;then
Packit Service 4684c1
	echo "Error in test 7: did not find all modules"
Packit Service 4684c1
	${builddir}/pkcs11/list-tokens -o ${P11DIR} -p
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether public key and privkey are listed.
Packit Service 4684c1
nr=$(GNUTLS_PIN="${PIN}" ${builddir}/pkcs11/list-objects -o ${P11DIR} -t all pkcs11:token=GnuTLS-Test|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 2;then
Packit Service 4684c1
	echo "Error in test 8: did not find all objects"
Packit Service 4684c1
	${builddir}/pkcs11/list-objects -o ${P11DIR} -t all pkcs11:token=GnuTLS-Test
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
# Check whether all privkeys are listed even if trust module is registered.
Packit Service 4684c1
nr=$(GNUTLS_PIN="${PIN}" ${builddir}/pkcs11/list-objects -o ${P11DIR} -t privkey pkcs11:|sort -u|wc -l)
Packit Service 4684c1
if test "$nr" != 1;then
Packit Service 4684c1
	echo "Error in test 9: did not find privkey objects"
Packit Service 4684c1
	${builddir}/pkcs11/list-objects -o ${P11DIR} -t privkey pkcs11:
Packit Service 4684c1
	exit 1
Packit Service 4684c1
fi
Packit Service 4684c1
Packit Service 4684c1
rm -f ${P11DIR}/*
Packit Service 4684c1
rm -rf ${TMP_SOFTHSM_DIR}
Packit Service 4684c1
Packit Service 4684c1
exit 0