Blame tests/Run-internal-BIND-test-suite/setup-named-softhsm.sh

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