Blame SOURCES/setup-named-softhsm.sh

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