2023-02-08 21:01:48 +00:00
#!/bin/bash
2019-02-11 22:29:13 +00:00
#
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
2023-02-20 16:01:17 +00:00
TRACE "Under /bin/gpg-gui.sh"
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
2022-10-09 21:24:16 +00:00
if ( cbfs.sh -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/pubring.kbx" ) ; then
cbfs.sh -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/pubring.kbx"
if ( cbfs.sh -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/pubring.gpg" ) ; then
cbfs.sh -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/pubring.gpg"
2019-04-10 16:34:31 +00:00
if [ -e /.gnupg/pubring.gpg ] ; then
rm /.gnupg/pubring.gpg
fi
fi
fi
#to be compatible with gpgv1
if [ -e /.gnupg/pubring.kbx ] ; then
2022-10-09 21:24:16 +00:00
cbfs.sh -o /tmp/gpg-gui.rom -a "heads/initrd/.gnupg/pubring.kbx" -f /.gnupg/pubring.kbx
2019-04-10 16:34:31 +00:00
if [ -e /.gnupg/pubring.gpg ] ; then
rm /.gnupg/pubring.gpg
fi
fi
if [ -e /.gnupg/pubring.gpg ] ; then
2022-10-09 21:24:16 +00:00
cbfs.sh -o /tmp/gpg-gui.rom -a "heads/initrd/.gnupg/pubring.gpg" -f /.gnupg/pubring.gpg
2019-04-10 16:34:31 +00:00
fi
2022-10-09 21:24:16 +00:00
if ( cbfs.sh -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/trustdb.gpg" ) then
cbfs.sh -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/trustdb.gpg"
2019-04-10 16:34:31 +00:00
fi
2019-07-09 16:41:31 +00:00
if [ -e /.gnupg/trustdb.gpg ] ; then
2022-10-09 21:24:16 +00:00
cbfs.sh -o /tmp/gpg-gui.rom -a "heads/initrd/.gnupg/trustdb.gpg" -f /.gnupg/trustdb.gpg
2019-07-09 16:41:31 +00:00
fi
2019-04-10 16:34:31 +00:00
#Remove old method owner trust exported file
2022-10-09 21:24:16 +00:00
if ( cbfs.sh -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/.gnupg/otrust.txt" ) then
cbfs.sh -o /tmp/gpg-gui.rom -d "heads/initrd/.gnupg/otrust.txt"
2019-04-10 16:34:31 +00:00
fi
2019-07-08 01:45:36 +00:00
# persist user config changes
2022-10-09 21:24:16 +00:00
if ( cbfs.sh -o /tmp/gpg-gui.rom -l | grep -q "heads/initrd/etc/config.user" ) then
cbfs.sh -o /tmp/gpg-gui.rom -d "heads/initrd/etc/config.user"
2019-07-08 01:45:36 +00:00
fi
2019-07-09 16:41:31 +00:00
if [ -e /etc/config.user ] ; then
2022-10-09 21:24:16 +00:00
cbfs.sh -o /tmp/gpg-gui.rom -a "heads/initrd/etc/config.user" -f /etc/config.user
2019-07-09 16:41:31 +00:00
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' \
2022-11-09 16:51:27 +00:00
--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." 0 80) then
2021-09-24 20:05:14 +00:00
if ! update_checksums ; then
whiptail $BG_COLOR_ERROR --title 'ERROR' \
2022-11-09 16:51:27 +00:00
--msgbox "Failed to update checksums / sign default config" 0 80
2021-09-24 20:05:14 +00:00
fi
2019-07-05 22:07:06 +00:00
else
/bin/reboot
fi
whiptail --title 'Files in /boot Updated Successfully' \
2023-06-30 18:21:11 +00:00
--msgbox "Checksums have been updated and /boot files signed.\n\nPress Enter to reboot" 0 80
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?' \
2022-11-09 16:51:27 +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 " 0 80) 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" \
2022-11-09 16:51:27 +00:00
--msgbox " ${ GPG_GEN_KEY } .asc copied successfully. " 0 80
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' \
2022-11-09 16:51:27 +00:00
--msgbox " Unable to copy ${ GPG_GEN_KEY } .asc to /media " 0 80
2019-04-19 15:35:01 +00:00
fi
umount /media
fi
if ( whiptail --title 'Add Public Key to Running BIOS?' \
2022-11-09 16:51:27 +00:00
--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" 0 80) then
2019-04-19 15:35:01 +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!' \
2023-06-30 18:21:11 +00:00
--msgbox "Unable to read BIOS" 0 80
2019-04-19 15:35:01 +00:00
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' \
2022-11-09 16:51:27 +00:00
--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?" 0 80) then
2019-07-09 03:16:57 +00:00
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!' \
2023-06-30 18:21:11 +00:00
--msgbox "Unable to read BIOS" 0 80
2019-07-09 03:16:57 +00:00
exit 1
fi
if ( whiptail --title 'Update ROM?' \
2022-11-09 16:51:27 +00:00
--yesno "This will reflash your BIOS with the updated version\n\nDo you want to proceed?" 0 80) then
2019-07-09 03:16:57 +00:00
gpg_flash_rom
else
exit 0
fi
fi
fi
}
2019-02-11 22:29:13 +00:00
while true; do
unset menu_choice
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_MAIN_MENU --title "GPG Management Menu" \
2022-11-09 16:51:27 +00:00
--menu 'Select the GPG function to perform' 0 80 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' \
2022-11-09 16:51:27 +00:00
--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?" 0 80) then
2019-02-11 22:29:13 +00:00
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?' \
2022-11-09 16:51:27 +00:00
--yesno " This will replace your old ROM with $ROM \n\nDo you want to proceed? " 0 80) 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' \
2022-11-09 16:51:27 +00:00
--msgbox " ${ GPG_KEYRING } " 0 80
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?' \
2022-11-09 16:51:27 +00:00
--yesno "Would you like to copy GPG public key(s) to a USB drive?\n\nThe file will show up as public-key.asc" 0 80) then
2020-06-23 13:32:51 +00:00
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" \
2023-06-30 18:21:11 +00:00
--msgbox "public-key.asc copied successfully." 0 80
2020-06-23 13:32:51 +00:00
else
2020-10-23 23:07:34 +00:00
whiptail $BG_COLOR_ERROR --title 'ERROR: Copy Failed' \
2023-06-30 18:21:11 +00:00
--msgbox "Unable to copy public-key.asc to /media" 0 80
2020-06-23 13:32:51 +00:00
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