2018-06-19 19:27:27 +00:00
|
|
|
#!/bin/sh
|
2020-06-11 15:04:03 +00:00
|
|
|
# Retrieve the sealed TOTP secret and initialize a USB Security dongle with it
|
2018-06-19 19:27:27 +00:00
|
|
|
|
|
|
|
. /etc/functions
|
|
|
|
|
|
|
|
HOTP_SEALED="/tmp/secret/hotp.sealed"
|
|
|
|
HOTP_SECRET="/tmp/secret/hotp.key"
|
|
|
|
HOTP_COUNTER="/boot/kexec_hotp_counter"
|
|
|
|
|
|
|
|
mount_boot()
|
|
|
|
{
|
|
|
|
# Mount local disk if it is not already mounted
|
|
|
|
if ! grep -q /boot /proc/mounts ; then
|
|
|
|
mount -o ro /boot \
|
|
|
|
|| recovery "Unable to mount /boot"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
tpm nv_readvalue \
|
|
|
|
-in 4d47 \
|
|
|
|
-sz 312 \
|
|
|
|
-of "$HOTP_SEALED" \
|
|
|
|
|| die "Unable to retrieve sealed file from TPM NV"
|
|
|
|
|
|
|
|
tpm unsealfile \
|
|
|
|
-hk 40000000 \
|
|
|
|
-if "$HOTP_SEALED" \
|
|
|
|
-of "$HOTP_SECRET" \
|
|
|
|
|| die "Unable to unseal HOTP secret"
|
|
|
|
|
2019-02-22 01:16:02 +00:00
|
|
|
shred -n 10 -z -u "$HOTP_SEALED" 2> /dev/null
|
2018-06-19 19:27:27 +00:00
|
|
|
|
2018-06-20 16:20:39 +00:00
|
|
|
# Store counter in file instead of TPM for now, as it conflicts with Heads
|
|
|
|
# config TPM counter as TPM 1.2 can only increment one counter between reboots
|
2018-06-19 19:27:27 +00:00
|
|
|
# get current value of HOTP counter in TPM, create if absent
|
|
|
|
mount_boot
|
|
|
|
|
2018-06-20 16:20:39 +00:00
|
|
|
#check_tpm_counter $HOTP_COUNTER hotp \
|
|
|
|
#|| die "Unable to find/create TPM counter"
|
|
|
|
#counter="$TPM_COUNTER"
|
|
|
|
#
|
|
|
|
#counter_value=$(read_tpm_counter $counter | cut -f2 -d ' ' | awk 'gsub("^000e","")')
|
|
|
|
#if [ "$counter_value" == "" ]; then
|
|
|
|
# die "Unable to read HOTP counter"
|
|
|
|
#fi
|
2018-06-19 19:27:27 +00:00
|
|
|
|
2018-06-20 16:20:39 +00:00
|
|
|
#counter_value=$(printf "%d" 0x${counter_value})
|
2018-06-19 19:27:27 +00:00
|
|
|
|
2018-06-20 16:20:39 +00:00
|
|
|
counter_value=1
|
2018-06-19 19:27:27 +00:00
|
|
|
|
|
|
|
enable_usb
|
|
|
|
if ! libremkey_hotp_verification info ; then
|
2020-06-11 15:04:03 +00:00
|
|
|
echo "Insert your $(CONFIG_HOTPKEY_BRANDING) and press Enter to configure it"
|
2018-06-19 19:27:27 +00:00
|
|
|
read
|
2019-05-24 16:50:27 +00:00
|
|
|
if ! libremkey_hotp_verification info ; then
|
|
|
|
# don't leak key on failure
|
|
|
|
shred -n 10 -z -u "$HOTP_SECRET" 2> /dev/null
|
2020-06-11 15:04:03 +00:00
|
|
|
die "Unable to find $(CONFIG_HOTPKEY_BRANDING)"
|
2019-05-24 16:50:27 +00:00
|
|
|
fi
|
2018-06-19 19:27:27 +00:00
|
|
|
fi
|
|
|
|
|
2019-06-29 04:26:20 +00:00
|
|
|
echo -e ""
|
2020-06-11 15:04:03 +00:00
|
|
|
read -s -p "Enter your $(CONFIG_HOTPKEY_BRANDING) Admin PIN: " admin_pin
|
2019-06-29 04:26:20 +00:00
|
|
|
echo -e "\n"
|
2018-06-19 19:27:27 +00:00
|
|
|
|
2019-05-24 23:29:58 +00:00
|
|
|
libremkey_hotp_initialize "$admin_pin" $HOTP_SECRET $counter_value
|
2018-06-19 19:27:27 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2019-06-29 04:26:20 +00:00
|
|
|
echo -e "\n"
|
|
|
|
read -s -p "Error setting HOTP secret, re-enter Admin PIN and try again: " admin_pin
|
|
|
|
echo -e "\n"
|
2019-05-24 23:29:58 +00:00
|
|
|
if ! libremkey_hotp_initialize "$admin_pin" $HOTP_SECRET $counter_value ; then
|
2019-05-24 16:50:27 +00:00
|
|
|
# don't leak key on failure
|
|
|
|
shred -n 10 -z -u "$HOTP_SECRET" 2> /dev/null
|
|
|
|
die "Setting HOTP secret failed"
|
|
|
|
fi
|
2018-06-19 19:27:27 +00:00
|
|
|
fi
|
|
|
|
|
2019-05-24 16:50:27 +00:00
|
|
|
# HOTP key no longer needed
|
|
|
|
shred -n 10 -z -u "$HOTP_SECRET" 2> /dev/null
|
2018-06-19 19:27:27 +00:00
|
|
|
|
|
|
|
# Make sure our counter is incremented ahead of the next check
|
2018-06-20 16:20:39 +00:00
|
|
|
#increment_tpm_counter $counter > /dev/null \
|
|
|
|
#|| die "Unable to increment tpm counter"
|
|
|
|
#increment_tpm_counter $counter > /dev/null \
|
|
|
|
#|| die "Unable to increment tpm counter"
|
2018-06-19 19:27:27 +00:00
|
|
|
|
|
|
|
mount -o remount,rw /boot
|
2018-06-20 16:20:39 +00:00
|
|
|
|
|
|
|
counter_value=`expr $counter_value + 1`
|
|
|
|
echo $counter_value > $HOTP_COUNTER \
|
2018-06-19 19:27:27 +00:00
|
|
|
|| die "Unable to create hotp counter file"
|
2018-06-20 16:20:39 +00:00
|
|
|
|
|
|
|
#sha256sum /tmp/counter-$counter > $HOTP_COUNTER \
|
|
|
|
#|| die "Unable to create hotp counter file"
|
2018-06-19 19:27:27 +00:00
|
|
|
mount -o remount,ro /boot
|
|
|
|
|
2020-06-11 15:04:03 +00:00
|
|
|
echo -e "\n$(CONFIG_HOTPKEY_BRANDING) initialized successfully. Press Enter to continue."
|
2018-06-19 19:27:27 +00:00
|
|
|
read
|
|
|
|
|
|
|
|
exit 0
|