Commit 3396cfd
committed
initrd/gpg: refactor OEM card management into shared functions for reuse, add reprovision flow
Extract shared OpenPGP smartcard management functions from
initrd/bin/oem-factory-reset.sh into initrd/etc/gpg_functions.sh
so they can be reused by multiple callers. OEM functions become
thin wrappers passing global variables as positional args; the
reprovision flow calls the same shared functions to restore GPG
subkeys from a LUKS-encrypted backup USB onto a replacement USB
Security dongle.
Shared functions (replacing OEM inline code):
- gpg_card_factory_reset(): factory-reset + forcesig + key-attr
(RSA and ECC/P-256), parameterized on admin PIN
- gpg_keytocard_subkeys(): enable USB, verify card, move subkeys
to slots 1-3 (sign, encrypt, auth)
- gpg_set_card_identity(): cardholder name and login fields
- gpg_reset_nk3_secret_app(): Nitrokey 3 Secrets app reset
- gpg_card_change_pin(): GPG User PIN change
New reprovision flow (reprovision_smartcard_from_backup()):
10-phase flow: mount LUKS backup -> detect key type -> factory-
reset card -> import subkeys -> set identity -> sign /boot -> flash
GUI integration:
- k option in GPG Management Menu (gpg-gui.sh), always visible
- K option in prompt_missing_gpg_key_action (gui-init.sh)
- Clean boot wizard: OEM reset, reprovision, or ignore
Code quality fixes from review:
- Fix 6 sites where $? was clobbered by TRACE_FUNC/DEBUG calls
- Replace --passphrase-file with --passphrase-fd 3 3< to avoid
leaking PIN through /proc/<pid>/cmdline
- Fix algo_code/bit_len extraction: head -1 for multi-key backups
- Fix STATUS_OK mount emission: only on success
- Fix gpg_reset_nk3_secret_app: return actual error code from
hotp_verification, explicit return 0 for non-NK3
- Remove incorrect || [ $? -eq 2 ] patch: gpg --import exit code 2
is a fatal error, not unchanged
- Fix pubkey.asc import: check success properly, fall back to
keyring export on failure
- Fix partition derivation for NVMe/MMC: stable [0-9]+$ regex
- Add _luks_cleanup helper for every error return path (replaces
broken EXIT trap pattern)
- Re-run NK3 Secrets app reset with custom PIN on factory-reset
retry
- Add chmod 600 on /tmp/secret/gpg_pin
- Write backup passphrase to /tmp/secret/backup_pass, use
mount-usb.sh --pass-file to avoid argv leak
- Shred /tmp/secret/backup_pass on mount success and failure
- Shred /tmp/secret/gpg_pin after all GPG operations complete
- Limit key_id derivation to first match (head -1) to prevent
multi-line values breaking gpg --edit-key calls
- Strip trailing <email> from cardholder name when no comment
is present in UID line
- Reset card_admin_pin to default after factory-reset step so
subsequent forcesig/key-attr use the correct PIN
- Ensure /tmp/secret exists before writing backup_pass (mkdir -p)
- Export public key from keyring instead of referencing mount
file: after successful pubkey.asc import, re-export from the
keyring to /tmp/reprovision_pubkey.asc rather than pointing
PUBKEY at the potentially-stale /media/pubkey.asc
- Fix gpg_reset_nk3_secret_app header comment: removes
inaccurate "to factory default" phrasing
- Remove unused uid_decoded local variable from reprovision_smartcard_from_backup
- Fix key_email extraction: add head -1 to prevent multi-line
values when UID contains multiple <...> blocks
- Fix parent_disk derivation for NVMe/MMC: use p?[0-9]+$ regex
the original partition used a p<num> separator and reconstruct the
public partition as parent_disk+p2 vs parent_disk+2 accordingly
hex fingerprints from gpg --with-colons
- Add --pass-file option to mount-usb.sh: use PASS_FILE variable
and pass path directly to cryptsetup --key-file instead of
reading into PASS and echoing via process substitution,
eliminating the argv leak that --pass-file was meant to fix
Documentation:
- doc/gpg.md, doc/configuring-keys.md, doc/architecture.md:
updated for the new recovery path
- doc/qemu.md: USB flash drive workflow, hardlink preservation,
reprovision test
- doc/recovery-shell.md: Authentication and Resetting Config
sections
Extends PR #1515 (2023): authentication via gpg_auth can now be
enforced with key material restored from a backup.
Tested on Nitrokey Pro 2 (reprovision end-to-end: factory reset,
keytocard, PIN change, public key import, flash, reboot, gpg_auth
works). Tested on Nitrokey 3 (OEM factory reset creates LUKS
backup + provisions dongle).
Signed-off-by: Thierry Laurion <insurgo@riseup.net>1 parent 8d0064f commit 3396cfd
10 files changed
Lines changed: 1141 additions & 364 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | | - | |
91 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | | - | |
| 165 | + | |
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
174 | 174 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
89 | | - | |
90 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | | - | |
| 99 | + | |
| 100 | + | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
| 126 | + | |
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
163 | 180 | | |
164 | 181 | | |
165 | 182 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
81 | | - | |
82 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | | - | |
91 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | | - | |
| 95 | + | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
133 | 145 | | |
134 | 146 | | |
135 | | - | |
| 147 | + | |
136 | 148 | | |
137 | 149 | | |
138 | 150 | | |
| |||
0 commit comments