2019-02-11 22:29:13 +00:00
#!/bin/sh
#
2019-02-19 14:48:35 +00:00
set -e -o pipefail
2019-02-11 22:29:13 +00:00
. /etc/functions
2020-07-10 17:58:32 +00:00
. /etc/gui_functions
2019-02-11 22:29:13 +00:00
. /tmp/config
2019-04-10 16:34:31 +00:00
gpg_flash_rom( ) {
2019-07-09 03:16:57 +00:00
if [ " $1 " = "replace" ] ; then
# clear local keyring
[ -e /.gnupg/pubring.gpg ] && rm /.gnupg/pubring.gpg
[ -e /.gnupg/pubring.kbx ] && rm /.gnupg/pubring.kbx
[ -e /.gnupg/trustdb.gpg ] && rm /.gnupg/trustdb.gpg
fi
2019-04-10 16:34:31 +00:00
cat " $PUBKEY " | gpg --import
#update /.gnupg/trustdb.gpg to ultimately trust all user provided public keys
gpg --list-keys --fingerprint --with-colons | sed -E -n -e 's/^fpr:::::::::([0-9A-F]+):$/\1:6:/p' | gpg --import-ownertrust
gpg --update-trust
if ( cbfs -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/pubring.kbx" ) ; then
cbfs -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/pubring.kbx"
if ( cbfs -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/pubring.gpg" ) ; then
cbfs -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/pubring.gpg"
if [ -e /.gnupg/pubring.gpg ] ; then
rm /.gnupg/pubring.gpg
fi
fi
fi
#to be compatible with gpgv1
if [ -e /.gnupg/pubring.kbx ] ; then
cbfs -o /tmp/gpg-gui.rom -a "heads/initrd/.gnupg/pubring.kbx" -f /.gnupg/pubring.kbx
if [ -e /.gnupg/pubring.gpg ] ; then
rm /.gnupg/pubring.gpg
fi
fi
if [ -e /.gnupg/pubring.gpg ] ; then
cbfs -o /tmp/gpg-gui.rom -a "heads/initrd/.gnupg/pubring.gpg" -f /.gnupg/pubring.gpg
fi
if ( cbfs -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/trustdb.gpg" ) then
cbfs -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/trustdb.gpg"
fi
2019-07-09 16:41:31 +00:00
if [ -e /.gnupg/trustdb.gpg ] ; then
cbfs -o /tmp/gpg-gui.rom -a "heads/initrd/.gnupg/trustdb.gpg" -f /.gnupg/trustdb.gpg
fi
2019-04-10 16:34:31 +00:00
#Remove old method owner trust exported file
if ( cbfs -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/otrust.txt" ) then
cbfs -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/otrust.txt"
fi
2019-07-08 01:45:36 +00:00
# persist user config changes
if ( cbfs -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/etc/config.user" ) then
cbfs -o /tmp/gpg-gui.rom -d "heads/initrd/etc/config.user"
fi
2019-07-09 16:41:31 +00:00
if [ -e /etc/config.user ] ; then
cbfs -o /tmp/gpg-gui.rom -a "heads/initrd/etc/config.user" -f /etc/config.user
fi
2019-04-10 16:34:31 +00:00
/bin/flash.sh /tmp/gpg-gui.rom
2019-07-05 22:07:06 +00:00
if ( whiptail --title 'BIOS Flashed Successfully' \
--yesno "Would you like to update the checksums and sign all of the files in /boot?\n\nYou will need your GPG key to continue and this will modify your disk.\n\nOtherwise the system will reboot immediately." 16 90) then
2021-09-24 20:05:14 +00:00
if ! update_checksums ; then
whiptail $BG_COLOR_ERROR --title 'ERROR' \
--msgbox "Failed to update checksums / sign default config" 16 90
fi
2019-07-05 22:07:06 +00:00
else
/bin/reboot
fi
whiptail --title 'Files in /boot Updated Successfully' \
--msgbox "Checksums have been updated and /boot files signed.\n\nPress Enter to reboot" 16 60
2019-04-10 16:34:31 +00:00
/bin/reboot
2019-07-05 22:07:06 +00:00
2019-04-10 16:34:31 +00:00
}
2019-04-19 15:35:01 +00:00
gpg_post_gen_mgmt( ) {
GPG_GEN_KEY = ` grep -A1 pub /tmp/gpg_card_edit_output | tail -n1 | sed -nr 's/^([ ])*//p' `
gpg --export --armor $GPG_GEN_KEY > " /tmp/ ${ GPG_GEN_KEY } .asc "
if ( whiptail --title 'Add Public Key to USB disk?' \
2020-06-23 13:32:51 +00:00
--yesno " Would you like to copy the GPG public key you generated to a USB disk?\n\nYou may need it, if you want to use it outside of Heads later.\n\nThe file will show up as ${ GPG_GEN_KEY } .asc " 16 90) then
2019-04-19 15:35:01 +00:00
mount_usb
mount -o remount,rw /media
cp " /tmp/ ${ GPG_GEN_KEY } .asc " " /media/ ${ GPG_GEN_KEY } .asc "
if [ $? -eq 0 ] ; then
whiptail --title "The GPG Key Copied Successfully" \
2021-10-29 18:18:31 +00:00
--msgbox " ${ GPG_GEN_KEY } .asc copied successfully. " 16 90
2019-04-19 15:35:01 +00:00
else
2020-10-23 23:07:34 +00:00
whiptail $BG_COLOR_ERROR --title 'ERROR: Copy Failed' \
2021-10-29 18:18:31 +00:00
--msgbox " Unable to copy ${ GPG_GEN_KEY } .asc to /media " 16 90
2019-04-19 15:35:01 +00:00
fi
umount /media
fi
if ( whiptail --title 'Add Public Key to Running BIOS?' \
--yesno "Would you like to add the GPG public key you generated to the BIOS?\n\nThis makes it a trusted key used to sign files in /boot\n\n" 16 90) then
/bin/flash.sh -r /tmp/gpg-gui.rom
if [ ! -s /tmp/gpg-gui.rom ] ; then
2020-10-23 23:07:34 +00:00
whiptail $BG_COLOR_ERROR --title 'ERROR: BIOS Read Failed!' \
2019-04-19 15:35:01 +00:00
--msgbox "Unable to read BIOS" 16 60
exit 1
fi
PUBKEY = " /tmp/ ${ GPG_GEN_KEY } .asc "
gpg_flash_rom
fi
}
2019-02-11 22:29:13 +00:00
2019-07-09 03:16:57 +00:00
gpg_add_key_reflash( ) {
if ( whiptail --title 'GPG public key required' \
--yesno "This requires you insert a USB drive containing:\n* Your GPG public key (*.key or *.asc)\n\nAfter you select this file, this program will copy and reflash your BIOS\n\nDo you want to proceed?" 16 90) then
mount_usb
if grep -q /media /proc/mounts ; then
find /media -name '*.key' > /tmp/filelist.txt
find /media -name '*.asc' >> /tmp/filelist.txt
file_selector "/tmp/filelist.txt" "Choose your GPG public key"
2019-07-09 21:53:40 +00:00
# bail if user didn't select a file
if [ " $FILE " = "" ] ; then
return
else
PUBKEY = $FILE
fi
2019-07-09 03:16:57 +00:00
/bin/flash.sh -r /tmp/gpg-gui.rom
if [ ! -s /tmp/gpg-gui.rom ] ; then
2020-10-23 23:07:34 +00:00
whiptail $BG_COLOR_ERROR --title 'ERROR: BIOS Read Failed!' \
2019-07-09 03:16:57 +00:00
--msgbox "Unable to read BIOS" 16 60
exit 1
fi
if ( whiptail --title 'Update ROM?' \
--yesno "This will reflash your BIOS with the updated version\n\nDo you want to proceed?" 16 90) then
gpg_flash_rom
else
exit 0
fi
fi
fi
}
2019-02-11 22:29:13 +00:00
while true; do
unset menu_choice
2021-09-23 17:56:17 +00:00
whiptail $BG_COLOR_MAIN_MENU --clear --title "GPG Management Menu" \
2019-02-11 22:29:13 +00:00
--menu 'Select the GPG function to perform' 20 90 10 \
2020-08-04 20:55:01 +00:00
'r' ' Add GPG key to running BIOS and reflash' \
'a' ' Add GPG key to standalone BIOS image and flash' \
'e' ' Replace GPG key(s) in the current ROM and reflash' \
2019-02-11 22:29:13 +00:00
'l' ' List GPG keys in your keyring' \
2020-06-23 13:32:51 +00:00
'p' ' Export public GPG key to USB drive' \
2019-04-19 15:35:01 +00:00
'g' ' Generate GPG keys manually on a USB security token' \
2019-02-11 22:29:13 +00:00
'x' ' Exit' \
2>/tmp/whiptail || recovery "GUI menu failed"
menu_choice = $( cat /tmp/whiptail)
case " $menu_choice " in
"x" )
exit 0
; ;
"a" )
if ( whiptail --title 'ROM and GPG public key required' \
--yesno "This requires you insert a USB drive containing:\n* Your GPG public key (*.key or *.asc)\n* Your BIOS image (*.rom)\n\nAfter you select these files, this program will reflash your BIOS\n\nDo you want to proceed?" 16 90) then
mount_usb
if grep -q /media /proc/mounts ; then
find /media -name '*.key' > /tmp/filelist.txt
find /media -name '*.asc' >> /tmp/filelist.txt
file_selector "/tmp/filelist.txt" "Choose your GPG public key"
if [ " $FILE " = = "" ] ; then
return
else
PUBKEY = $FILE
fi
find /media -name '*.rom' > /tmp/filelist.txt
file_selector "/tmp/filelist.txt" "Choose the ROM to load your key onto"
if [ " $FILE " = = "" ] ; then
return
else
ROM = $FILE
fi
cp " $ROM " /tmp/gpg-gui.rom
2021-09-23 17:56:17 +00:00
if ( whiptail $BG_COLOR_WARNING --title 'Flash ROM?' \
2019-02-11 22:29:13 +00:00
--yesno " This will replace your old ROM with $ROM \n\nDo you want to proceed? " 16 90) then
2019-04-10 16:34:31 +00:00
gpg_flash_rom
2019-02-11 22:29:13 +00:00
else
exit 0
fi
fi
fi
; ;
"r" )
2019-07-09 03:16:57 +00:00
gpg_add_key_reflash
exit 0;
; ;
"e" )
# clear local keyring
[ -e /.gnupg/pubring.gpg ] && rm /.gnupg/pubring.gpg
[ -e /.gnupg/pubring.kbx ] && rm /.gnupg/pubring.kbx
[ -e /.gnupg/trustdb.gpg ] && rm /.gnupg/trustdb.gpg
# add key and reflash
gpg_add_key_reflash
2019-02-11 22:29:13 +00:00
; ;
"l" )
GPG_KEYRING = ` gpg -k`
whiptail --title 'GPG Keyring' \
2021-10-29 18:18:31 +00:00
--msgbox " ${ GPG_KEYRING } " 16 90
2019-02-11 22:29:13 +00:00
; ;
2020-06-23 13:32:51 +00:00
"p" )
if ( whiptail --title 'Export Public Key(s) to USB drive?' \
--yesno "Would you like to copy GPG public key(s) to a USB drive?\n\nThe file will show up as public-key.asc" 16 90) then
mount_usb
mount -o remount,rw /media
gpg --export --armor > "/tmp/public-key.asc"
cp "/tmp/public-key.asc" "/media/public-key.asc"
if [ $? -eq 0 ] ; then
whiptail --title "The GPG Key Copied Successfully" \
--msgbox "public-key.asc copied successfully." 16 60
else
2020-10-23 23:07:34 +00:00
whiptail $BG_COLOR_ERROR --title 'ERROR: Copy Failed' \
2020-06-23 13:32:51 +00:00
--msgbox "Unable to copy public-key.asc to /media" 16 60
fi
umount /media
fi
; ;
2019-02-11 22:29:13 +00:00
"g" )
confirm_gpg_card
2019-04-10 16:34:31 +00:00
echo -e "\n\n\n\n"
2019-02-11 22:29:13 +00:00
echo "********************************************************************************"
echo "*"
echo "* INSTRUCTIONS:"
echo "* Type 'admin' and then 'generate' and follow the prompts to generate a GPG key."
2019-04-10 16:34:31 +00:00
echo "* Type 'quit' once you have generated the key to exit GPG."
2019-02-11 22:29:13 +00:00
echo "*"
echo "********************************************************************************"
2019-04-10 16:34:31 +00:00
gpg --card-edit > /tmp/gpg_card_edit_output
if [ $? -eq 0 ] ; then
2019-04-19 15:35:01 +00:00
gpg_post_gen_mgmt
fi
; ;
2019-02-11 22:29:13 +00:00
esac
done
exit 0