Blame SOURCES/setup-named-softhsm.sh

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