Skip to content

Commit b906dc2

Browse files
committed
initrd/gpg: address review comments for reprovision flow
Fix 6 instances of $? clobbered by intervening TRACE_FUNC/DEBUG calls: capture rc=$? immediately after each DO_WITH_DEBUG gpg pipeline, check $rc instead of $? after TRACE_FUNC/DEBUG. Fix algo_code/bit_len extraction: add head -1 to prevent multi-line values when multiple keys are present in the backup. Fix STATUS_OK on public partition mount: only emit STATUS_OK when mount-usb.sh succeeds, not unconditionally. Fix gpg_reset_nk3_secret_app return code: propagate the actual hotp_verification error code instead of returning generic 1. Signed-off-by: Thierry Laurion <insurgo@riseup.net>
1 parent 526aa9f commit b906dc2

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

initrd/etc/gpg_functions.sh

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ gpg_reset_nk3_secret_app() {
166166
whiptail_warning --msgbox "$DONGLE_BRAND requires physical presence: touch the dongle when requested" 0 80 --title "$DONGLE_BRAND secrets app reset attempt: $attempt/3"
167167
else
168168
DEBUG "NK3 Secrets app reset failed with error $error_code"
169-
return 1
169+
return $error_code
170170
fi
171171
fi
172172
done
@@ -194,9 +194,10 @@ gpg_card_factory_reset() {
194194
} | DO_WITH_DEBUG gpg --command-fd=0 --status-fd=1 --pinentry-mode=loopback \
195195
--passphrase-file <(echo -n "$card_admin_pin") --card-edit \
196196
>/tmp/gpg_card_edit_output 2>&1
197+
rc=$?
197198
TRACE_FUNC
198199
DEBUG "GPG factory-reset output: $(cat /tmp/gpg_card_edit_output)"
199-
if [ $? -ne 0 ]; then
200+
if [ $rc -ne 0 ]; then
200201
return 1
201202
fi
202203

@@ -218,9 +219,10 @@ gpg_card_factory_reset() {
218219
echo "${card_admin_pin}"
219220
} | DO_WITH_DEBUG gpg --command-fd=0 --status-fd=1 --pinentry-mode=loopback --card-edit \
220221
>/tmp/gpg_card_edit_output 2>&1
222+
rc=$?
221223
TRACE_FUNC
222224
DEBUG "GPG forcesig toggle output: $(cat /tmp/gpg_card_edit_output)"
223-
if [ $? -ne 0 ]; then
225+
if [ $rc -ne 0 ]; then
224226
WARN "Could not enable forced signature PIN; continuing anyway"
225227
else
226228
STATUS_OK "Forced signature PIN enabled"
@@ -244,9 +246,10 @@ gpg_card_factory_reset() {
244246
echo "${card_admin_pin}"
245247
} | DO_WITH_DEBUG gpg --expert --command-fd=0 --status-fd=1 --pinentry-mode=loopback --card-edit \
246248
>/tmp/gpg_card_edit_output 2>&1
249+
rc=$?
247250
TRACE_FUNC
248251
DEBUG "GPG p256 key-attr output: $(cat /tmp/gpg_card_edit_output)"
249-
if [ $? -ne 0 ]; then
252+
if [ $rc -ne 0 ]; then
250253
return 1
251254
fi
252255
STATUS_OK "NIST P-256 key attributes set"
@@ -266,9 +269,10 @@ gpg_card_factory_reset() {
266269
echo "${card_admin_pin}"
267270
} | DO_WITH_DEBUG gpg --command-fd=0 --status-fd=1 --pinentry-mode=loopback --card-edit \
268271
>/tmp/gpg_card_edit_output 2>&1
272+
rc=$?
269273
TRACE_FUNC
270274
DEBUG "GPG RSA key-attr output: $(cat /tmp/gpg_card_edit_output)"
271-
if [ $? -ne 0 ]; then
275+
if [ $rc -ne 0 ]; then
272276
return 1
273277
fi
274278
STATUS_OK "RSA ${rsa_key_length}-bit key attributes set"
@@ -365,9 +369,10 @@ gpg_card_change_pin() {
365369
echo q
366370
} | DO_WITH_DEBUG gpg --command-fd=0 --status-fd=2 --pinentry-mode=loopback --card-edit \
367371
>/tmp/gpg_card_edit_output 2>&1
372+
rc=$?
368373
TRACE_FUNC
369374
DEBUG "GPG PIN change output: $(cat /tmp/gpg_card_edit_output)"
370-
if [ $? -ne 0 ]; then
375+
if [ $rc -ne 0 ]; then
371376
return 1
372377
fi
373378
TRACE_FUNC
@@ -416,9 +421,10 @@ gpg_keytocard_subkeys() {
416421
} | DO_WITH_DEBUG gpg --expert --command-fd=0 --status-fd=1 --pinentry-mode=loopback \
417422
--edit-key "$key_id" \
418423
>/tmp/gpg_card_edit_output 2>&1
424+
rc=$?
419425
TRACE_FUNC
420426
DEBUG "GPG keytocard output: $(cat /tmp/gpg_card_edit_output)"
421-
if [ $? -ne 0 ]; then
427+
if [ $rc -ne 0 ]; then
422428
DEBUG "keytocard failed"
423429
return 1
424430
fi
@@ -521,8 +527,8 @@ reprovision_smartcard_from_backup() {
521527
# gpg --with-colons field layout:
522528
# pub: ... :<bit_len>:<algo>:<key_id>: ...
523529
# uid: ... :<escaped_uid>: ...
524-
algo_code="$(gpg --with-colons --list-keys 2>/dev/null | grep '^pub:' | cut -d: -f4)"
525-
bit_len="$(gpg --with-colons --list-keys 2>/dev/null | grep '^pub:' | cut -d: -f3)"
530+
algo_code="$(gpg --with-colons --list-keys 2>/dev/null | grep '^pub:' | cut -d: -f4 | head -1)"
531+
bit_len="$(gpg --with-colons --list-keys 2>/dev/null | grep '^pub:' | cut -d: -f3 | head -1)"
526532
uid_line="$(gpg --with-colons --list-keys 2>/dev/null | grep '^uid:' | head -1 | cut -d: -f10)"
527533

528534
case "$algo_code" in
@@ -743,12 +749,16 @@ reprovision_smartcard_from_backup() {
743749
DEBUG "Mounting public partition via explicit device: $pub_partition"
744750
if ! mount-usb.sh --device "$pub_partition" --mode ro --mountpoint /media; then
745751
DEBUG "Could not mount public partition at $pub_partition"
752+
else
753+
STATUS_OK "Public partition mounted"
746754
fi
747-
STATUS_OK "Public partition mounted"
748755
else
749756
DEBUG "No explicit public device; falling back to auto-detection"
750-
mount-usb.sh --mode ro --mountpoint /media 2>/dev/null ||
757+
if mount-usb.sh --mode ro --mountpoint /media 2>/dev/null; then
758+
STATUS_OK "Public partition mounted"
759+
else
751760
DEBUG "Could not auto-detect public partition"
761+
fi
752762
fi
753763

754764
STATUS "Importing public key from backup"

0 commit comments

Comments
 (0)