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