2023-02-08 16:01:48 -05:00
|
|
|
#!/bin/bash
|
2018-06-19 12:27:27 -07:00
|
|
|
# Retrieve the sealed file and counter from the NVRAM, unseal it and compute the hotp
|
|
|
|
|
|
|
|
. /etc/functions
|
|
|
|
|
|
|
|
HOTP_SECRET="/tmp/secret/hotp.key"
|
|
|
|
HOTP_COUNTER="/boot/kexec_hotp_counter"
|
|
|
|
|
2019-05-17 18:07:07 -05:00
|
|
|
mount_boot_or_die()
|
2018-06-19 12:27:27 -07:00
|
|
|
{
|
2023-02-20 11:01:17 -05:00
|
|
|
TRACE "Under /bin/unseal-hotp:mount_boot_or_die"
|
2018-06-19 12:27:27 -07:00
|
|
|
# Mount local disk if it is not already mounted
|
|
|
|
if ! grep -q /boot /proc/mounts ; then
|
|
|
|
mount -o ro /boot \
|
2019-05-17 18:07:07 -05:00
|
|
|
|| die "Unable to mount /boot"
|
2018-06-19 12:27:27 -07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-02-20 11:01:17 -05:00
|
|
|
TRACE "Under /bin/unseal-hotp"
|
2023-02-18 12:58:43 -05:00
|
|
|
|
2019-05-18 20:22:11 -05: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
|
|
|
|
# get current value of HOTP counter in TPM, create if absent
|
|
|
|
mount_boot_or_die
|
|
|
|
|
2018-06-20 09:20:39 -07: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","")')
|
|
|
|
#
|
2018-06-19 12:27:27 -07:00
|
|
|
|
2018-06-20 09:20:39 -07:00
|
|
|
counter_value=$(cat $HOTP_COUNTER)
|
2018-06-19 12:27:27 -07:00
|
|
|
|
|
|
|
if [ "$counter_value" == "" ]; then
|
2018-06-20 09:20:39 -07:00
|
|
|
die "Unable to read HOTP counter"
|
2018-06-19 12:27:27 -07:00
|
|
|
fi
|
|
|
|
|
2018-06-20 09:20:39 -07:00
|
|
|
#counter_value=$(printf "%d" 0x${counter_value})
|
2023-02-28 13:36:11 -05:00
|
|
|
if [ "$CONFIG_TPM" = "y" ]; then
|
2023-03-09 13:28:04 -05:00
|
|
|
DEBUG "Unsealing HOTP secret reuses TOTP sealed secret..."
|
2023-08-31 17:15:24 -04:00
|
|
|
tpmr unseal 4d47 0,1,2,3,4,7 312 "$HOTP_SECRET" || die "Unable to unseal HOTP secret"
|
2019-11-25 12:25:11 -06:00
|
|
|
else
|
2023-07-05 10:18:06 -04:00
|
|
|
# without a TPM, generate a secret based on the SHA-256 of the ROM
|
2023-07-03 16:59:23 -04:00
|
|
|
secret_from_rom_hash > "$HOTP_SECRET" || die "Reading ROM failed"
|
2022-08-25 14:43:31 -04:00
|
|
|
fi
|
2019-05-18 20:22:11 -05:00
|
|
|
|
2023-07-05 10:18:06 -04:00
|
|
|
# Truncate the secret if it is longer than the maximum HOTP secret
|
|
|
|
truncate_max_bytes 20 "$HOTP_SECRET"
|
|
|
|
|
2018-06-19 12:27:27 -07:00
|
|
|
if ! hotp $counter_value < "$HOTP_SECRET"; then
|
2019-02-21 20:16:02 -05:00
|
|
|
shred -n 10 -z -u "$HOTP_SECRET" 2> /dev/null
|
2018-06-19 12:27:27 -07:00
|
|
|
die 'Unable to compute HOTP hash?'
|
|
|
|
fi
|
|
|
|
|
2019-02-21 20:16:02 -05:00
|
|
|
shred -n 10 -z -u "$HOTP_SECRET" 2> /dev/null
|
2018-06-19 12:27:27 -07:00
|
|
|
|
2018-06-20 09:20:39 -07:00
|
|
|
#increment_tpm_counter $counter > /dev/null \
|
|
|
|
#|| die "Unable to increment tpm counter"
|
2018-06-19 12:27:27 -07:00
|
|
|
|
|
|
|
mount -o remount,rw /boot
|
2018-06-20 09:20:39 -07:00
|
|
|
|
|
|
|
counter_value=`expr $counter_value + 1`
|
|
|
|
echo $counter_value > $HOTP_COUNTER \
|
2018-06-19 12:27:27 -07:00
|
|
|
|| die "Unable to create hotp counter file"
|
2018-06-20 09:20:39 -07:00
|
|
|
|
|
|
|
#sha256sum /tmp/counter-$counter > $HOTP_COUNTER \
|
|
|
|
#|| die "Unable to create hotp counter file"
|
2018-06-19 12:27:27 -07:00
|
|
|
mount -o remount,ro /boot
|
|
|
|
|
|
|
|
exit 0
|