:

DIR="$1"

KEY_FILE="$DIR/yubipass.key"

if [ ! -e "$KEY_FILE" ]
then	# The 'postgres' operating system user must have permission to
	# access the Yubikey device.

	openssl rand -hex 32 |
	xxd -plain -cols 999 |
	openssl rsautl -engine pkcs11 -keyform engine -encrypt \
		-inkey 0:3 -passin file:/u/postgres/tmp/piv.pin -out "$KEY_FILE"

	# Warn the user to save the passphrase in a safe place
	cat 1>&2 <<END

WARNING:  The Yubikey can be locked and require a reset if too many pin
attempts fail.  It is recommended to run this command manually and save
the passphrase in a secure location for possible recovery.
END

fi

openssl rsautl -engine pkcs11 -keyform engine -decrypt \
		-inkey 0:3 -passin file:/u/postgres/tmp/piv.pin -in "$KEY_FILE"

exit 0
