Skip to content

Commit 1d3bfcb

Browse files
committed
initrd/gpg: fix additional review issues from PR #2158
- Remove incorrect || [ $? -eq 2 ] pattern: exit code 2 from gpg --import is a fatal error, not "unchanged" as previously documented - Fix pubkey.asc import: check success/failure properly, fall back to keyring export on failure instead of silently continuing with stale PUBKEY - Fix gpg_reset_nk3_secret_app: add local error_code to prevent global scope leak, implicit return 0 for non-NK3 devices (non-zero fall-through broke OEM reset on NK3-incompatible hardware) - Fix partition derivation for NVMe/MMC: sed pattern (p?)[0-9]+$ was removing the p separator from nvme0n1p1 -> nvme0n1, producing nvme0n12 instead of nvme0n1p2. Use [0-9]+$ without p? capture. Signed-off-by: Thierry Laurion <insurgo@riseup.net>
1 parent 3932448 commit 1d3bfcb

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

initrd/etc/gpg_functions.sh

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ gpg_flash_rom() {
77
[ -e /.gnupg/trustdb.gpg ] && rm /.gnupg/trustdb.gpg
88
fi
99

10-
cat "$PUBKEY" | gpg --import || [ $? -eq 2 ]
10+
cat "$PUBKEY" | gpg --import
1111
gpg --list-keys --fingerprint --with-colons | sed -E -n -e 's/^fpr:::::::::([0-9A-F]+):$/\1:6:/p' | gpg --import-ownertrust
1212
gpg --update-trust
1313

@@ -154,6 +154,7 @@ gpg_replace_key_reflash() {
154154
gpg_reset_nk3_secret_app() {
155155
TRACE_FUNC
156156
local admin_pin="$1"
157+
local error_code
157158
if [ "$DONGLE_BRAND" = "Nitrokey 3" ] && [ -x /bin/hotp_verification ]; then
158159
STATUS "Resetting Nitrokey 3 Secrets app (physical touch will be required)"
159160
for attempt in 1 2 3; do
@@ -781,7 +782,7 @@ reprovision_smartcard_from_backup() {
781782
# and derive parent disk (e.g. /dev/sdb) + public partition (/dev/sdb2).
782783
local part_name
783784
part_name="$(basename "$mapper_dev" | sed 's/^usb_mount_//')"
784-
parent_disk="$(echo "/dev/$part_name" | sed -E 's/(p?)[0-9]+$//')"
785+
parent_disk="$(echo "/dev/$part_name" | sed -E 's/[0-9]+$//')"
785786
pub_partition="${parent_disk}2"
786787
DEBUG "mapper_dev=$mapper_dev part_name=$part_name parent_disk=$parent_disk pub_partition=${pub_partition:-none}"
787788
cryptsetup close "$(basename "$mapper_dev")" 2>/dev/null || true
@@ -791,8 +792,6 @@ reprovision_smartcard_from_backup() {
791792
fi
792793

793794
# Phase 9: mount the public partition and import pubkey.asc.
794-
# Validate against the key already in ~/.gnupg; re-import is idempotent
795-
# so exit code 2 (unchanged) is a normal result.
796795
enable_usb
797796
enable_usb_storage
798797
STATUS "Mounting GPG key backup (public partition)"
@@ -814,10 +813,18 @@ reprovision_smartcard_from_backup() {
814813

815814
STATUS "Importing public key from backup"
816815
if [ -f /media/pubkey.asc ]; then
817-
gpg --import </media/pubkey.asc || [ $? -eq 2 ]
818-
PUBKEY=/media/pubkey.asc
819-
DEBUG "pubkey.asc found on public partition at /media/pubkey.asc"
820-
STATUS_OK "Public key imported"
816+
if gpg --import </media/pubkey.asc 2>/dev/null; then
817+
PUBKEY=/media/pubkey.asc
818+
DEBUG "pubkey.asc found on public partition at /media/pubkey.asc"
819+
STATUS_OK "Public key imported"
820+
else
821+
DEBUG "pubkey.asc import failed; exporting from imported keyring"
822+
gpg --export --armor "$key_id" >/tmp/reprovision_pubkey.asc 2>/dev/null || {
823+
umount /media 2>/dev/null || true
824+
DIE "Failed to export public key for ROM flash"
825+
}
826+
PUBKEY=/tmp/reprovision_pubkey.asc
827+
fi
821828
else
822829
# Fallback: export from the keyring (public key is already there
823830
# from the privkey.sec import)

0 commit comments

Comments
 (0)