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