e55890
#!/bin/sh
e55890
#
e55890
# This script will initialise token storage of softhsm PKCS11 provider
e55890
# in custom location. Is useful to store tokens in non-standard location.
e55890
e55890
SOFTHSM2_CONF="$1"
e55890
TOKENPATH="$2"
e55890
GROUPNAME="$3"
e55890
# Do not use this script for real keys worth protection
e55890
# This is intended for crypto accelerators using PKCS11 interface.
e55890
# Uninitialized token would fail any crypto operation.
e55890
PIN=1234
e55890
e55890
set -e
e55890
e55890
if [ -z "$SOFTHSM2_CONF" -o -z "$TOKENPATH" ]; then
e55890
	echo "Usage: $0 <config file> <token directory> [group]" >&2
e55890
	exit 1
e55890
fi
e55890
e55890
if ! [ -f "$SOFTHSM2_CONF" ]; then
e55890
cat  << SED > "$SOFTHSM2_CONF"
e55890
# SoftHSM v2 configuration file
e55890
e55890
directories.tokendir = ${TOKENPATH}
e55890
objectstore.backend = file
e55890
e55890
# ERROR, WARNING, INFO, DEBUG
e55890
log.level = ERROR
e55890
e55890
# If CKF_REMOVABLE_DEVICE flag should be set
e55890
slots.removable = false
e55890
SED
e55890
else
e55890
	echo "Config file $SOFTHSM2_CONF already exists" >&2
e55890
fi
e55890
e55890
[ -d "$TOKENPATH" ] || mkdir -p "$TOKENPATH"
e55890
e55890
export SOFTHSM2_CONF
e55890
e55890
if softhsm2-util --show-slots | grep 'Initialized:[[:space:]]*yes' > /dev/null
e55890
then
e55890
	echo "Token in ${TOKENPATH} is already initialized" >&2
e55890
else
e55890
	echo "Initializing tokens to ${TOKENPATH}..."
e55890
	softhsm2-util --init-token --free --label rpm --pin $PIN --so-pin $PIN
e55890
e55890
	if [ -n "$GROUPNAME" ]; then
e55890
		chgrp -R -- "$GROUPNAME" "$TOKENPATH"
e55890
		chmod -R -- g=rX,o= "$TOKENPATH"
e55890
	fi
e55890
fi
e55890
e55890
echo "export SOFTHSM2_CONF=\"$SOFTHSM2_CONF\""