Blame SOURCES/setup-named-softhsm.sh

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