Registry Mirror
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ differ from the x86 ROM/ZIP workflow; see

- **No network at boot** — all verification is local; no certificate authorities
- **Hardware root of trust** — on the typical x86 path with TPM support and `CONFIG_TPM_MEASURED_BOOT=y`, the coreboot bootblock (IBB) acts as the Static Core Root of Trust for Measurement (S-CRTM): the first code executed by the CPU, directly from SPI flash. Coreboot implements a transitive chain: the CRTM measures FMAP and the bootblock image into the preram log, then bootblock measures romstage, romstage measures ramstage, and ramstage measures the Heads payload. Measurements are cached before TPM initialization and flushed to PCR 2 during `tpm_setup()`. This is not the Talos or LinuxBoot path, and measured boot/TPM availability varies by board. See [tpm.md](tpm.md#root-of-trust-and-srtm-chain) for the per-board PCR map and timing, and [wp-notes.md](wp-notes.md#pr0-chipset-locking) for SPI write protection.
- **Fail-closed** — failed integrity verification drops to a recovery shell. Recovery shell authentication via GPG smartcard is enforced when GPG key backup has been configured (`CONFIG_HAVE_GPG_KEY_BACKUP=y`), which is set by answering "y" to `"Would you like to format an encrypted USB Thumb drive to store GPG key material? (Required to enable GPG authentication)"` during OEM Factory Reset / Re-Ownership. Otherwise the recovery shell is unauthenticated. An "Ignore tampering and force a boot (Unsafe!)" option is available to override this.
- **Fail-closed** — failed integrity verification drops to a recovery shell. Recovery shell authentication via GPG smartcard is enforced when GPG key backup has been configured (`CONFIG_HAVE_GPG_KEY_BACKUP=y`), which is set by answering "y" to `"Would you like to format an encrypted USB Thumb drive to store GPG key material? (Required to enable GPG authentication)"` during OEM Factory Reset / Re-Ownership, or by running "Reprovision USB Security dongle from GPG key backup" from the GPG Management Menu. Otherwise the recovery shell is unauthenticated. An "Ignore tampering and force a boot (Unsafe!)" option is available to override this. See [recovery-shell.md](recovery-shell.md#authentication) for details.
- **Separation of duties** — the public key that verifies `/boot` signatures is stored in CBFS (ROM). The private key that signs `/boot` stays on a USB security dongle and never leaves it.
- **Auditability** — source inputs, build recipes, and recorded hashes can be inspected; byte-identical ROM output must be demonstrated for the compared build

Expand Down
51 changes: 51 additions & 0 deletions doc/configuring-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,57 @@ If you already have a provisioned USB Security dongle:
3. Follow the steps. After reflashing, reboot.
4. Generate a new TOTP/HOTP secret when prompted.

## Restoring Keys from Backup

If you chose the in-memory backup path during OEM factory reset and your
dongle is lost, broken, or wiped:

1. Insert the backup USB thumb drive and your (new) dongle.
2. Go to `Options -> GPG Management -> 'k' Reprovision USB Security dongle from GPG key backup`.
3. Enter the backup passphrase when prompted.
4. Heads detects the key type from the backup, factory-resets the dongle,
restores the subkeys, sets the card identity, resets the TPM, creates a
fresh rollback counter, and re-signs /boot (see below).
5. After success, flash the public key to the running BIOS for persistence
(skipped automatically on QEMU boards).
6. Reboot to finalize.

This requires the LUKS-encrypted backup USB drive created during OEM factory
reset (answer Y to "format an encrypted USB Thumb drive"). Without it, run
a new OEM Factory Reset / Re-Ownership to rekey the device.

The reprovision flow is reachable from several places, not just the GPG
Management Menu:

- `Options -> GPG Management -> 'k'` — always visible (gpg-gui.sh).
- `'K'` in the empty-GPG-keyring error dialog (`check_gpg_key` in gui-init.sh).
- `'K'` in the measured integrity report (`report_integrity_measurements` in
gui_functions.sh), both the normal and the DONGLE KEY NOT ROM-TRUSTED
variants.
- `'K'` in the TPM State Inconsistent (rollback preflight) dialog — since
reprovisioning resets the TPM and creates a fresh counter, the preflight
check re-runs afterwards and the gate proceeds to boot when it passes.
- `'K'` in the clean boot wizard (`clean_boot_check`), next to `F` (OEM
Factory Reset), `i` (ignore), and `x` (recovery shell).

**TPM handling:** reprovisioning is an ownership-level operation, so like
OEM Factory Reset it resets the TPM itself rather than punting a manual
"Reset the TPM" step to you. After you set the new TPM owner passphrase,
Heads clears the old ownership, drops stale rollback-counter references,
creates a fresh rollback counter, and re-signs /boot — all through the same
`kexec-sign-config.sh` path used by OEM factory reset. Signing is atomic:
the new manifests are staged under /tmp and moved into /boot only after
signing and verification succeed. If any step fails, /boot keeps its
previous valid signatures, no ROM flash is offered, and Heads explains how
to recover (re-sign via `Options -> Update checksums and sign all files in
/boot`, or reset the TPM first if that failed).

Because the TPM was reset, unsealing the old TOTP/HOTP secrets on the next
boot will fail; Heads offers to regenerate them (choose `g`).

After reprovisioning, the recovery shell and USB boot will require GPG
smartcard authentication; see [recovery-shell.md](recovery-shell.md#authentication).

## Forgotten GPG User PIN

From Recovery Shell with the dongle inserted:
Expand Down
12 changes: 12 additions & 0 deletions doc/gpg.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ public key into the Heads firmware:
3. From Heads: `Options -> GPG Management -> Add a GPG key to the running BIOS + reflash`.
4. Reboot. Generate a new TOTP/HOTP secret when prompted.

## Restoring Keys from Backup

See [configuring-keys.md](configuring-keys.md#restoring-keys-from-backup) for
the full recovery flow. In short:

1. Insert the backup USB thumb drive and the replacement dongle.
2. From Heads: `Options -> GPG Management -> 'k' Reprovision USB Security dongle from GPG key backup`.
3. Enter the backup passphrase.

The backup drive must have been created by the OEM factory reset flow with
the "format an encrypted USB Thumb drive" option set to Y.

## Nitrokey 3 Specifics

- Supports NIST P-256 ECC keys in addition to RSA — significantly faster key
Expand Down
73 changes: 73 additions & 0 deletions doc/qemu.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,54 @@ After OS install + USB provisioned, reference both from `./qemu_img/`:
inject_gpg run


### Testing GPG key reprovision from a backup drive

The GPG key reprovision flow ('k' in the GPG Management Menu, or the 'K'
option when signing fails) restores subkeys from a LUKS-encrypted backup
drive created during OEM factory reset onto a (new) OpenPGP smartcard.
This can be tested in QEMU.

First run OEM factory reset to populate the virtual USB drive with backup
material (this creates `build/x86/<board>/usb_fd.raw` with the LUKS private
+ exFAT public partition layout). Afterwards, save the virtual USB drive
and canokey state, then hardlink back for the second run:

```bash
# qemu_img/ is the in-clone store for QEMU images (Docker-visible).
# It must be on the same filesystem as build/ so cp -alf hardlinks work.
mkdir -p qemu_img

# Save canokey state and populated USB backup image (hardlink, zero-cost).
cp -al build/x86/qemu-coreboot-fbwhiptail-tpm1-hotp/.canokey-file \
qemu_img/.canokey-file
cp -al build/x86/qemu-coreboot-fbwhiptail-tpm1-hotp/usb_fd.raw \
qemu_img/backup_drive.raw

# Hardlink back into the build dir so make run picks them up; make
# requires the raw image to live inside the local heads dir.
cp -alf qemu_img/.canokey-file \
build/x86/qemu-coreboot-fbwhiptail-tpm1-hotp/.canokey-file
cp -alf qemu_img/backup_drive.raw \
build/x86/qemu-coreboot-fbwhiptail-tpm1-hotp/usb_fd.raw

# Second run uses the preserved backup with no USB_FD_IMG override.
./docker_repro.sh make BOARD=qemu-coreboot-fbwhiptail-tpm1-hotp run
```

Inside the VM: Options -> GPG Options -> 'k' Reprovision USB Security dongle
from GPG key backup. Enter the backup passphrase (the Admin PIN you set
during OEM factory reset). The flow will:
- Detect the key type (RSA or ECC) from the backup
- Factory-reset the virtual canokey and set matching key attributes
- Import the master key and subkeys from the LUKS partition
- Move subkeys to the smartcard via keytocard
- Set the card identity (name, email) from the backup key's UID
- Reset the TPM and create a fresh rollback counter (TPM boards)
- Re-sign /boot so the next boot trusts the restored state
- Skip flashing the public key to ROM automatically (QEMU cannot reflash;
inject the exported pubkey into the firmware image instead)


Running via Docker wrappers
===
We provide convenient wrapper scripts at the repository root that encapsulate Docker invocation and automatically handle common host integrations needed for QEMU runs.
Expand Down Expand Up @@ -299,6 +347,31 @@ Inspecting wrapper options
be used, including KVM and X11-related mounts when their host prerequisites are
present. This is an inspection path, not a QEMU or wrapper behavior test.

Resetting state
---

QEMU boards using the default virtual token persist canokey and TPM state
between runs. To simulate a fresh dongle and TPM for testing:

```bash
# Wipe the virtual Canokey (new dongle, no keys on card).
sudo rm -f build/x86/<BOARD>/.canokey-file

# Wipe the virtual TPM (new TPM, no sealed secrets or counters).
sudo rm -rf build/x86/<BOARD>/vtpm/
```

The next `make run` will create fresh `.canokey-file` and `vtpm/`
directories automatically. The Heads setup wizard will then offer OEM
factory reset (F) or reprovision from backup (K).

To preserve canokey state for reuse:

```bash
cp build/x86/<BOARD>/.canokey-file qemu_img/.canokey-file.bak
cp qemu_img/.canokey-file.bak build/x86/<BOARD>/.canokey-file
```

Troubleshooting
---

Expand Down
108 changes: 108 additions & 0 deletions doc/recovery-shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,114 @@ normal `generic` value (or the unextended state). This means:
create or reseal secrets are not available in the recovery shell.
- To perform seal/unseal operations return to the normal GUI boot.

## Authentication

`gpg_auth()` in `initrd/etc/functions.sh` guards the recovery shell and
external media boot entry. On boards with `CONFIG_HAVE_GPG_KEY_BACKUP=y`,
recovery calls `gpg_auth()` before opening the bash prompt, and
`media-scan.sh` (called from `usb-init.sh`) also invokes `gpg_auth` before
scanning USB devices. TPM operations, flash/update, GPG management, and
all other GUI menu functions are NOT gated by this check — they remain
accessible from the main menu.

After OEM Factory Reset / Re-Ownership or the reprovision flow,
`CONFIG_HAVE_GPG_KEY_BACKUP=y` is set in the user config. It is
persisted to CBFS only after a successful ROM flash: the reprovision
flow updates `/etc/config.user` in RAM and lets the user decline
flashing, and QEMU skips internal flashing entirely. Only after a
successful flash is recovery shell and USB boot authentication enforced
across reboots, even on boards where the compile-time default differs.

To enable `gpg_auth` during OEM Factory Reset / Re-Ownership, answer Y to
"format an encrypted USB Thumb drive to store GPG key material? (Required
to enable GPG authentication)". Choosing N skips the backup drive creation
and leaves `gpg_auth` disabled (recovery shell and USB boot remain
unauthenticated).

**What `gpg_auth` guards — and what it does not:**

`gpg_auth()` is called from exactly two places: the recovery shell entry
point (`recovery()` in `functions.sh`) and the USB/external media boot
scanner (`media-scan.sh`, called from `usb-init.sh`). The normal GUI boot
flow (`gui-init.sh`) does NOT invoke `gpg_auth()` — it handles GPG key
presence through its own `check_gpg_key` mechanism, which is separate from
`gpg_auth`.

Many destructive operations are already available from the GUI without
`gpg_auth`: the user can reflash firmware (`Options -> Flash/Update`),
wipe GPG keys and config (`Options -> Clear GPG key(s) and reset all user
settings`), reset the TPM, re-sign boot hash manifests
(`Options -> Update checksums and sign all files in /boot`),
and generate new GPG keys. All of these are tamper-evident — the TPM
PCR measurements and HOTP/TOTP codes will detect changes.

What `gpg_auth` prevents:

- **Recovery shell entry without authentication.** The recovery shell
gives unrestricted root access to raw block devices, SPI flash, TPM
commands, and GPG key operations. The GUI does not expose
`flash.sh -r` (dump running firmware). An attacker with recovery
shell access could dump the full SPI flash for offline analysis, forge
a malicious firmware image, and flash it back undetected. `gpg_auth`
blocks this path. (Note: an attacker with physical access can still
extract the SPI flash via an external programmer after disassembly —
`gpg_auth` only protects the in-software path through the running
system.)

- **USB/external media boot from an untrusted drive.** `media-scan.sh`
(called from `usb-init.sh`) invokes `gpg_auth` before scanning USB
devices. When `CONFIG_HAVE_GPG_KEY_BACKUP` is not set, `gpg_auth`
is a no-op and USB boot proceeds without restriction. When the flag
is set (after OEM factory reset or reprovision), the user must
authenticate with their GPG smartcard before external media boot is
allowed. (Note: the boot process locks the SPI flash controller,
making Heads the only internal flasher. An attacker booting from
USB cannot reflash the firmware even with root access. External
SPI flashing via hardware programmer after disassembly is always
possible regardless — see [wp-notes.md](wp-notes.md).)

**What `gpg_auth` does NOT prevent:**

- Reflashing firmware through the GUI
- Wiping GPG keys, config, or TPM state through the GUI
- Re-signing `/boot` through the GUI
- Any operation the user can perform from the main menu

**How it works:** `gpg_auth()` generates a random nonce, the user signs it
with their GPG key (smartcard or backup USB drive) within 3 attempts, and the
signature is verified against the ROM-fused public keyring. On failure,
`DIE` exits the session. With `CONFIG_RESTRICTED_BOOT=y`, the shell is
blocked entirely and the system reboots after 5 seconds.

**Without authentication:** If the board does not have
`CONFIG_HAVE_GPG_KEY_BACKUP` set, `gpg_auth()` is a no-op and the recovery
shell opens without prompting.

## Resetting Configuration

`Options -> Change configuration settings -> 'r'` (`Clear GPG key(s) and reset
all user settings`) wipes the running system configuration:

- Clears `~/.gnupg` (GPG keyring, trustdb)
- Deletes `/boot/kexec*.txt` and `/boot/kexec.sig` (boot hash manifests,
checksums, rollback counter, signatures)
- Removes all `heads/` files from CBFS (keyring, trustdb, config.user)
- Reflashes the cleaned firmware
- Resets the TPM if present

**Attestation impact:** Removing `heads/` files from CBFS changes the SPI flash
contents measured into PCR 7 by `cbfs-init` at next boot. Combined with the
TPM reset, TOTP/HOTP unseal will fail on the next boot — Heads shows the
standard red-menu TOTP error prompt.

**Recovery after wipe:** Run OEM Factory Reset / Re-Ownership (Options -> 'F')
to fully reprovision ([configuring-keys.md](configuring-keys.md)). If you have a
GPG key backup USB drive from a previous in-memory OEM reset, use
`GPG Options -> 'k' Reprovision USB Security dongle from GPG key backup`
to restore subkeys from the backup ([configuring-keys.md#restoring-keys-from-backup](configuring-keys.md#restoring-keys-from-backup)),
then flash the public key to ROM, re-sign /boot, and generate new TOTP/HOTP
secrets.

## Common Operations

### Manual boot
Expand Down
9 changes: 8 additions & 1 deletion initrd/bin/gpg-gui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ TRACE_FUNC
while true; do
unset menu_choice
whiptail_type $BG_COLOR_MAIN_MENU --title "GPG Management Menu" \
--menu 'Select the GPG function to perform' 0 80 10 \
--menu 'Select the GPG function to perform' 0 80 11 \
'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' \
'l' ' List GPG keys in your keyring' \
'p' ' Export public GPG key to USB drive' \
'g' ' Generate GPG keys manually on a USB security dongle' \
'k' ' Reprovision USB Security dongle from GPG key backup' \
'x' ' Exit' \
2>/tmp/whiptail || recovery "GUI menu failed"

Expand Down Expand Up @@ -64,6 +65,12 @@ while true; do
gpg_post_gen_mgmt
fi
;;
"k")
# The function returns non-zero on benign paths (user declining,
# wrong passphrase, signing failure); this script runs under
# set -e, so guard the call to stay inside the management menu.
reprovision_smartcard_from_backup || true
;;
esac

done
Expand Down
Loading