199a5d
#!/bin/sh
199a5d
#
199a5d
# This script will initialise token storage of softhsm PKCS11 provider
199a5d
# in custom location. Is useful to store tokens in non-standard location.
199a5d
#
199a5d
# Output can be evaluated from bash, it will prepare it for usage of temporary tokens.
199a5d
# Quotes around eval are mandatory!
199a5d
# Recommended use:
199a5d
# eval "$(bash setup-named-softhsm.sh -A)"
199a5d
#
199a5d
199a5d
SOFTHSM2_CONF="$1"
199a5d
TOKENPATH="$2"
199a5d
GROUPNAME="$3"
199a5d
# Do not use this script for real keys worth protection
199a5d
# This is intended for crypto accelerators using PKCS11 interface.
199a5d
# Uninitialized token would fail any crypto operation.
199a5d
PIN=1234
199a5d
SO_PIN=1234
199a5d
LABEL=rpm
199a5d
199a5d
set -e
199a5d
199a5d
echo_i()
199a5d
{
199a5d
	echo "#" $@
199a5d
}
199a5d
199a5d
random()
199a5d
{
199a5d
	if [ -x "$(which openssl 2>/dev/null)" ]; then
199a5d
		openssl rand -base64 $1
199a5d
	else
199a5d
		dd if=/dev/urandom bs=1c count=$1 | base64
199a5d
	fi
199a5d
}
199a5d
199a5d
usage()
199a5d
{
199a5d
	echo "Usage: $0 -A [token directory] [group]"
199a5d
	echo "   or: $0 <config file> <token directory> [group]"
199a5d
}
199a5d
199a5d
if [ "$SOFTHSM2_CONF" = "-A" -a -z "$TOKENPATH" ]; then
199a5d
	TOKENPATH=$(mktemp -d /var/tmp/softhsm-XXXXXX)
199a5d
fi
199a5d
199a5d
if [ -z "$SOFTHSM2_CONF" -o -z "$TOKENPATH" ]; then
199a5d
	usage >&2
199a5d
	exit 1
199a5d
fi
199a5d
199a5d
if [ "$SOFTHSM2_CONF" = "-A" ]; then
199a5d
	# Automagic mode instead
199a5d
	MODE=secure
199a5d
	SOFTHSM2_CONF="$TOKENPATH/softhsm2.conf"
199a5d
	PIN_SOURCE="$TOKENPATH/pin"
199a5d
	SOPIN_SOURCE="$TOKENPATH/so-pin"
199a5d
	TOKENPATH="$TOKENPATH/tokens"
199a5d
else
199a5d
	MODE=legacy
199a5d
fi
199a5d
199a5d
[ -d "$TOKENPATH" ] || mkdir -p "$TOKENPATH"
199a5d
199a5d
umask 0022
199a5d
199a5d
if ! [ -f "$SOFTHSM2_CONF" ]; then
199a5d
cat  << SED > "$SOFTHSM2_CONF"
199a5d
# SoftHSM v2 configuration file
199a5d
199a5d
directories.tokendir = ${TOKENPATH}
199a5d
objectstore.backend = file
199a5d
199a5d
# ERROR, WARNING, INFO, DEBUG
199a5d
log.level = ERROR
199a5d
199a5d
# If CKF_REMOVABLE_DEVICE flag should be set
199a5d
slots.removable = false
199a5d
SED
199a5d
else
199a5d
	echo_i "Config file $SOFTHSM2_CONF already exists" >&2
199a5d
fi
199a5d
199a5d
if [ -n "$PIN_SOURCE" ]; then
199a5d
	touch "$PIN_SOURCE" "$SOPIN_SOURCE"
199a5d
	chmod 0600 "$PIN_SOURCE" "$SOPIN_SOURCE"
199a5d
	if [ -n "$GROUPNAME" ]; then
199a5d
		chgrp "$GROUPNAME" "$PIN_SOURCE" "$SOPIN_SOURCE"
199a5d
		chmod g+r "$PIN_SOURCE" "$SOPIN_SOURCE"
199a5d
	fi
199a5d
fi
199a5d
199a5d
export SOFTHSM2_CONF
199a5d
199a5d
if softhsm2-util --show-slots | grep 'Initialized:[[:space:]]*yes' > /dev/null
199a5d
then
199a5d
	echo_i "Token in ${TOKENPATH} is already initialized" >&2
199a5d
199a5d
	[ -f "$PIN_SOURCE" ] && PIN=$(cat "$PIN_SOURCE")
199a5d
	[ -f "$SOPIN_SOURCE" ] && SO_PIN=$(cat "$SOPIN_SOURCE")
199a5d
else
199a5d
	PIN=$(random 6)
199a5d
	SO_PIN=$(random 18)
199a5d
	if [ -n "$PIN_SOURCE" ]; then
199a5d
		echo -n "$PIN" > "$PIN_SOURCE"
199a5d
		echo -n "$SO_PIN" > "$SOPIN_SOURCE"
199a5d
	fi
199a5d
199a5d
	echo_i "Initializing tokens to ${TOKENPATH}..."
199a5d
	softhsm2-util --init-token --free --label "$LABEL" --pin "$PIN" --so-pin "$SO_PIN" | sed -e 's/^/# /'
199a5d
199a5d
	if [ -n "$GROUPNAME" ]; then
199a5d
		chgrp -R -- "$GROUPNAME" "$TOKENPATH"
199a5d
		chmod -R -- g=rX,o= "$TOKENPATH"
199a5d
	fi
199a5d
fi
199a5d
199a5d
echo "export SOFTHSM2_CONF=\"$SOFTHSM2_CONF\""
199a5d
echo "export PIN_SOURCE=\"$PIN_SOURCE\""
199a5d
echo "export SOPIN_SOURCE=\"$SOPIN_SOURCE\""
199a5d
# These are intentionaly not exported
199a5d
echo "PIN=\"$PIN\""
199a5d
echo "SO_PIN=\"$SO_PIN\""