Skip to content

Commit dcc3b0f

Browse files
committed
oem-factory-reset: unify booleen y/n variable usage and double check logic. Also move USB Security dongle capability detection under code already checking for USB Security Dongle's smartcard presence.
Signed-off-by: Thierry Laurion <[email protected]>
1 parent 160367d commit dcc3b0f

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

initrd/bin/oem-factory-reset

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,14 @@ generate_checksums() {
641641
[ -z "$param_files" ] &&
642642
whiptail_error_die "No kexec parameter files to sign"
643643

644-
if [ "$GPG_GEN_KEY_IN_MEMORY" = "1" -a "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "0" ]; then
645-
#The local keyring is still valid since no key has been moved to smartcard
646-
#Local keyring passwd is ADMIN_PIN. We need to set USER_PIN to ADMIN_PIN
644+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "y" -a "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "n" ]; then
645+
#The local keyring used to generate in memory subkeys is still valid since no key has been moved to smartcard
646+
#Local keyring passwd is ADMIN_PIN. We need to set USER_PIN to ADMIN_PIN to be able to sign next in this boot session
647647
DEBUG "Setting GPG User PIN to GPG Admin PIN so local keyring can be used to detach-sign kexec files next"
648648
USER_PIN=$ADMIN_PIN
649649
fi
650650

651-
DEBUG "Detach-signing boot files under kexec.sig: $param_files"
651+
DEBUG "Detach-signing boot files under kexec.sig: ${param_files}"
652652
if sha256sum $param_files 2>/dev/null | gpg \
653653
--pinentry-mode loopback \
654654
--passphrase "${USER_PIN}" \
@@ -841,9 +841,6 @@ mount_boot
841841
# We show current integrity measurements status and time
842842
report_integrity_measurements
843843

844-
# Determine gpg algorithm to be used, based on available usb-token
845-
usb_security_token_capabilities_check
846-
847844
# Clear the screen
848845
clear
849846

@@ -888,7 +885,7 @@ if [ "$use_defaults" == "n" -o "$use_defaults" == "N" ]; then
888885
if [ "$prompt_output" == "y" \
889886
-o "$prompt_output" == "Y" ] \
890887
; then
891-
GPG_GEN_KEY_IN_MEMORY=1
888+
GPG_GEN_KEY_IN_MEMORY="y"
892889
echo " ++++ Master key and subkeys will be generated in memory, backed up to dedicated LUKS container +++"
893890
echo -e -n "Would you like in-memory generated subkeys to be copied to USB Security Dongle's smartcard?\n (Highly recommended so the smartcard is used on daily basis and backup is kept safe, but not required) [Y/n]: "
894891
read -n 1 prompt_output
@@ -897,15 +894,16 @@ if [ "$use_defaults" == "n" -o "$use_defaults" == "N" ]; then
897894
-o "$prompt_output" == "N" ]; then
898895
warn "Subkeys will NOT be copied to USB Security Dongle's smartcard"
899896
warn "Your GPG key material backup thumb drive should be cloned to a second thumb drive for redundancy for production environements"
900-
GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD=0
897+
GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD="n"
901898
else
902899
echo "++++ Subkeys will be copied to USB Security Dongle's smartcard ++++"
903900
warn "Please keep your GPG key material backup thumb drive safe"
904-
GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD=1
901+
GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD="y"
905902
fi
906903
else
907904
echo "GPG key material will be generated on USB Security Dongle's smartcard without backup"
908-
GPG_GEN_KEY_IN_MEMORY=0
905+
GPG_GEN_KEY_IN_MEMORY="n"
906+
GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD="n"
909907
fi
910908

911909
# Dynamic messages to be given to user in terms of security components that will be applied
@@ -918,12 +916,12 @@ if [ "$use_defaults" == "n" -o "$use_defaults" == "N" ]; then
918916
if [ "$CONFIG_TPM" = "y" ]; then
919917
CUSTOM_PASS_AFFECTED_COMPONENTS+="TPM Owner Password\n"
920918
fi
921-
if [ "$GPG_GEN_KEY_IN_MEMORY" = "1" ]; then
919+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "y" ]; then
922920
CUSTOM_PASS_AFFECTED_COMPONENTS+="GPG Key material backup passphrase (Same as GPG Admin PIN)\n"
923921
fi
924922
CUSTOM_PASS_AFFECTED_COMPONENTS+="GPG Admin PIN\n"
925923
# Only show GPG User PIN as affected component if GPG_GEN_KEY_IN_MEMORY variable is empty/not existing or GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD is set to 1
926-
if [ -z "$GPG_GEN_KEY_IN_MEMORY" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "1" ]; then
924+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "n" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "y" ]; then
927925
CUSTOM_PASS_AFFECTED_COMPONENTS+="GPG User PIN\n"
928926
fi
929927

@@ -975,7 +973,7 @@ if [ "$use_defaults" == "n" -o "$use_defaults" == "N" ]; then
975973
#USER PIN not required in case of GPG_GEN_KEY_IN_MEMORY=1 while GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD=0
976974
# That is, if keys were NOT generated in memory (on smartcard only) or
977975
# if keys were generated in memory but are to be moved from local keyring to smartcard
978-
if [ "$GPG_GEN_KEY_IN_MEMORY" = "0" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "1" ]; then
976+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "n" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "y" ]; then
979977
while [[ ${#USER_PIN} -lt 8 ]] || [[ ${#USER_PIN} -gt $MAX_HOTP_GPG_PIN_LENGTH ]]; do
980978
echo -e -n "\nThis PIN should be between 8 to $MAX_HOTP_GPG_PIN_LENGTH characters in length.\n"
981979
echo -e -n "Enter desired GPG User PIN: "
@@ -1040,7 +1038,7 @@ if [ "$ADMIN_PIN" == "" ]; then ADMIN_PIN=${ADMIN_PIN_DEF}; fi
10401038

10411039
## sanity check the USB, GPG key, and boot device before proceeding further
10421040

1043-
if [ "$GPG_GEN_KEY_IN_MEMORY" == "0" ]; then
1041+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "n" ]; then
10441042
# Prompt to insert USB drive if desired
10451043
echo -e -n "\nWould you like to export your public key to an USB drive? [y/N]: "
10461044
read -n 1 prompt_output
@@ -1070,8 +1068,8 @@ if [ "$GPG_GEN_KEY_IN_MEMORY" == "0" ]; then
10701068
fi
10711069
fi
10721070

1073-
# ensure USB Security Dongle connected if GPG_GEN_KEY_IN_MEMORY=0
1074-
if [ "$GPG_GEN_KEY_IN_MEMORY" == "0" ]; then
1071+
# ensure USB Security Dongle connected if GPG_GEN_KEY_IN_MEMORY=n or if GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD=y
1072+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "n" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "y" ]; then
10751073
echo -e "\nChecking for USB Security Dongle...\n"
10761074
enable_usb
10771075
if ! gpg --card-status >/dev/null 2>&1; then
@@ -1081,6 +1079,9 @@ if [ "$GPG_GEN_KEY_IN_MEMORY" == "0" ]; then
10811079
whiptail_error_die "Unable to detect USB Security Dongle:\n\n${ERROR}"
10821080
fi
10831081
fi
1082+
1083+
#Now that USB Security Dongle is detected, we can check its capabilities and limitations
1084+
usb_security_token_capabilities_check
10841085
fi
10851086

10861087
assert_signable
@@ -1135,7 +1136,7 @@ rm /.gnupg/*.kbx 2>/dev/null
11351136
gpg --list-keys >/dev/null 2>&1
11361137

11371138
#Generate keys in memory and copy to smartcard
1138-
if [ "$GPG_GEN_KEY_IN_MEMORY" == "1" ]; then
1139+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "y" ]; then
11391140
if [ "$GPG_ALGO" == "RSA" ]; then
11401141
# Generate GPG master key
11411142
generate_inmemory_RSA_master_and_subkeys
@@ -1146,7 +1147,7 @@ if [ "$GPG_GEN_KEY_IN_MEMORY" == "1" ]; then
11461147
fi
11471148
wipe_thumb_drive_and_copy_gpg_key_material
11481149
set_user_config "CONFIG_HAVE_GPG_KEY_BACKUP" "y"
1149-
if [ "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" == "1" ]; then
1150+
if [ "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "y" ]; then
11501151
keytocard_subkeys_to_smartcard
11511152
fi
11521153
else
@@ -1168,7 +1169,7 @@ if ! gpg --export --armor "$GPG_GEN_KEY" >"${PUBKEY}" 2>/tmp/error; then
11681169
fi
11691170

11701171
#Applying custom GPG PINs to the smartcard if they were provided
1171-
if [ -z "GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "1" ]; then
1172+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "n" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "y" ]; then
11721173
#Only apply smartcard PIN change if smartcard only or if keytocard op is expected next
11731174
if [ "${USER_PIN}" != "" -o "${ADMIN_PIN}" != "" ]; then
11741175
echo -e "\nChanging default GPG Admin PIN\n"
@@ -1274,13 +1275,13 @@ fi
12741275

12751276
#GPG PINs output
12761277
passphrases+="GPG Admin PIN: ${ADMIN_PIN}\n"
1277-
#USER PIN was configured if GPG_GEN_KEY_IN_MEMORY is not defined or GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD=1
1278-
if [ -z "$GPG_GEN_KEY_IN_MEMORY" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "1" ]; then
1278+
#USER PIN was configured if GPG_GEN_KEY_IN_MEMORY is not active or if GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD is active
1279+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "n" -o "$GPG_GEN_KEY_IN_MEMORY_COPY_TO_SMARTCARD" = "y" ]; then
12791280
passphrases+="GPG User PIN: ${USER_PIN}\n"
12801281
fi
12811282

12821283
#If user decided to generate keys in memory, we add the thumb drive passphrase
1283-
if [ "$GPG_GEN_KEY_IN_MEMORY" == "1" ]; then
1284+
if [ "$GPG_GEN_KEY_IN_MEMORY" = "y" ]; then
12841285
passphrases+="GPG key material backup passphrase: ${ADMIN_PIN}\n"
12851286
fi
12861287

initrd/etc/ash_functions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ confirm_gpg_card() {
115115
die "Unable to detach-sign $CR_NONCE with GPG private signing subkey using GPG Admin PIN"
116116
#verify detached signature against public key in rom
117117
gpg --verify "$CR_SIG" "$CR_NONCE" > /dev/null 2>&1 && \
118-
echo "++++ Local GPG keyring can be used to sign/encrypt/authenticate in this boot session ++++" || \
118+
echo "++++ Local GPG keyring can be used to sign/encrypt/authenticate in this boot session ++++" || \
119119
die "Unable to verify $CR_SIG detached signature against public key in ROM"
120120
#Wipe any previous CR_NONCE and CR_SIG
121121
shred -n 10 -z -u "$CR_NONCE" "$CR_SIG" >/dev/null 2>&1 || true

0 commit comments

Comments
 (0)