Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bin/util/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tokens

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I don't think we need this if there is a proper clean up as suggested below.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I don't think we need this if there is a proper clean up as suggested below.

Please let keep this.
For instance user my cancel unfinished test and git should ignore tests artifacts.

9 changes: 8 additions & 1 deletion src/bin/util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ add_test(
bash ${CMAKE_CURRENT_SOURCE_DIR}/mlkem512-import-key-test.sh
)

add_test(
NAME ${PROJECT_NAME}-p11prov
COMMAND ${CMAKE_COMMAND} -E env
bash ${CMAKE_CURRENT_SOURCE_DIR}/p11prov-test.sh
)

# Make tests returning code 77 considered as skipped, to avoid marking the test suite as failed when running on an unsupported OpenSSL version or with a Botan-based SoftHSM2.
set_tests_properties(${PROJECT_NAME}-ml-dsa PROPERTIES SKIP_RETURN_CODE 77)
set_tests_properties(${PROJECT_NAME}-ml-kem PROPERTIES SKIP_RETURN_CODE 77)
set_tests_properties(${PROJECT_NAME}-ml-kem PROPERTIES SKIP_RETURN_CODE 77)
set_tests_properties(${PROJECT_NAME}-p11prov PROPERTIES SKIP_RETURN_CODE 77)
4 changes: 4 additions & 0 deletions src/bin/util/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
TESTS_ENVIRONMENT = top_builddir='$(top_builddir)' top_srcdir='$(top_srcdir)' srcdir='$(srcdir)' builddir='$(builddir)'

TESTS = mldsa44-import-key-test.sh mlkem512-import-key-test.sh
if WITH_OPENSSL
TESTS += p11prov-test.sh
endif

check_SCRIPTS = $(TESTS)

EXTRA_DIST = $(srcdir)/CMakeLists.txt \
$(srcdir)/p11prov-test.sh \
$(srcdir)/import-key-test-common.sh \
$(srcdir)/mldsa44-import-key-test.sh \
$(srcdir)/mlkem512-import-key-test.sh
174 changes: 174 additions & 0 deletions src/bin/util/test/p11prov-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#! /bin/sh

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be probably better to add it to to test subdit (it means src/bin/util/test) so it's clear it's a test.

# This file is in the public domain

CWD=`pwd`

# binaries

OPENSSL=${OPENSSL-openssl}
OPENSSL=`command -v "$OPENSSL"`
if test -z "$OPENSSL" ; then
echo "error: openssl utility not found" >&2
exit 77
fi

openssl() {
"$OPENSSL" ${1+"$@"}
}

openssl_version=`openssl version` || exit $?
if test -z "$openssl_version" ; then
echo "cannot determine OpenSSL version" >&2
exit 99
fi

case $openssl_version in
*"OpenSSL 0.9."*|\
*"OpenSSL 1."*)
Comment on lines +26 to +27

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be also skip for LibreSSL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm it might just get covered by the provider test but wouldn't hurt to add it...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be also skip for LibreSSL.

fixed by new commit "restrict tests to OpenSSL 3+: address"

echo "$openssl_version is not impacted" >&2
exit 77
;;
*"OpenSSL "*)
# OpenSSL 3+ - with provider loadable module
;;
*)
echo "unsupported: $openssl_version" >&2
exit 77
;;
esac
# NOTE OpenSSL > 1.*


# find a PKCS#11 provider
p11_find_provider() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this doesn't work in the CI as there is no pkcs11-provider installed - it might be good to extend CI for that so it's not purely local test.

@petrovr petrovr Jul 21, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this doesn't work in the CI as there is no pkcs11-provider installed - it might be good to extend CI for that so it's not purely local test.

This is managed by exist status - 77 means skip tests.
Also if is installed the test should pass..

if test -z "$PROV_PKCS11" ; then

# try to extract path ...
moduledir=`openssl version -m 2>/dev/null \
| sed -e 's/^MODULESDIR: "//' -e 's/"$//'`
if test -z "$moduledir" ; then
echo "cannot determine OpenSSL MODULESDIR" >&2
exit 99
fi
if test -d "$moduledir" ; then :
else
echo "does not exist MODULESDIR: $moduledir" >&2
exit 99
fi

for N in pkcs11 libpkcs11 ; do
for S in so dll ; do
test -f "$moduledir"/$N.$S || continue
PROV_PKCS11="$moduledir"/$N.$S
break
done
test -n "$PROV_PKCS11" && break
done
test -n "$PROV_PKCS11"
else
test -f "$PROV_PKCS11"
fi
}

if p11_find_provider ; then :
else
echo "error: PKCS#11 provider not found" >&2
exit 77
fi


# get absolute path to SoftHSM pkcs#11 module
# NOTE: Depending on the build model, the module
# may be located in a subdirectory.
D=`cd ../../../lib/ && pwd`
if test -z "$D" ; then
echo "unexpectedly missing library directory" >&2
exit 99
fi
P11MODULE=
for F in `find "$D" -name '*softhsm2.*'` ; do
case "$F" in
*.so|*.dll);;
*) continue;;
esac
test -f "$F" || continue
P11MODULE="$F"
break
done
if test -z "$P11MODULE" ; then
echo "error: unexpected module suffix" >&2
exit 99
fi
Comment thread
coderabbitai[bot] marked this conversation as resolved.
if command -v realpath > /dev/null ; then
P11MODULE=`realpath "$P11MODULE"`
fi

softhsm2_tool() {
"$CWD"/../softhsm2-util --module "$P11MODULE" ${1+"$@"}
}


# configurations
TOKEN_DIR="$CWD"/tokens
rm -rf "$TOKEN_DIR"
mkdir "$TOKEN_DIR"
Comment on lines +112 to +114

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should clean the tokens after the test in the same way as src/bin/util/test/import-key-test-common.sh does

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should clean the tokens after the test in the same way as src/bin/util/test/import-key-test-common.sh does

Fixed by new commit "clean-up "token" directory on success"



OPENSSL_CONF="$TOKEN_DIR"/openssl.conf
cat > "$OPENSSL_CONF" <<EOF
openssl_conf = config

[ config ]
providers = provider_section

[ provider_section ]
default = default_section
provider1 = provider1_section

[default_section]
activate = 1

[provider1_section]
pkcs11-module-path = $P11MODULE
module = $PROV_PKCS11
activate = 1
EOF
export OPENSSL_CONF


SOFTHSM2_CONF="$TOKEN_DIR"/softhsm2.conf
cat > "$SOFTHSM2_CONF" <<EOF
directories.tokendir = $TOKEN_DIR
objectstore.backend = file
slots.removable = false
slots.mechanisms = ALL
log.level = ERROR
EOF
export SOFTHSM2_CONF


# execution
set -e

TOKEN_PIN=4321
TOKEN_ID=01
PASS_URI=pass:$TOKEN_PIN
PASS_FILE=pass:dcba
KEY_URI=pkcs11:id=%$TOKEN_ID
KEY_FILE="$TOKEN_DIR"/test_key
SIGN_FILE="$TOKEN_DIR"/test_sign

softhsm2_tool --init-token --label test0 --slot free --so-pin 12345678 --pin $TOKEN_PIN
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$KEY_FILE" -pass $PASS_FILE
softhsm2_tool --import "$KEY_FILE" --import-type keypair --id $TOKEN_ID --label test_key --token test0 --pin $TOKEN_PIN

#openssl storeutl -passin $PASS_URI $KEY_URI

# sample command that crash before workaround due to OpenSSL "at_exit" flaw
openssl pkeyutl -sign -inkey $KEY_URI -passin $PASS_URI -rawin -in ./Makefile -out "$SIGN_FILE"

# just in case
openssl pkeyutl -verify -inkey "$KEY_FILE" -passin $PASS_FILE -rawin -in ./Makefile -sigfile "$SIGN_FILE"

# clean-up
rm -rf "$TOKEN_DIR"
2 changes: 2 additions & 0 deletions src/lib/SoftHSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
#include <unistd.h>
#endif

bool SoftHSM::isInitialised;

// Initialise the one-and-only instance

#ifdef HAVE_CXX11
Expand Down
2 changes: 1 addition & 1 deletion src/lib/SoftHSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class SoftHSM
#endif

// Is the SoftHSM PKCS #11 library initialised?
bool isInitialised;
static bool isInitialised;
bool isRemovable;

SessionObjectStore* sessionObjectStore;
Expand Down