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