Skip to content
systemcoding.dev
Security

Replace the Platform Keys and Own Your Secure Boot Chain

Generate your own PK, KEK and db keys, enroll them from UEFI setup mode with efitools, and sign a Linux kernel image with sbsign so only your binaries boot.

7 min read
Summarize in AI
Flat illustration of a boot chain of stages, each handing over to the next, with the Linux, GNU and Coreboot logos.

A machine that ships with Secure Boot enabled trusts whatever the firmware vendor and Microsoft decided to trust. That is a sensible default for a laptop and the wrong one for a device you build, audit or deploy in a fleet. Owning the chain means three things: you generate the Platform Key (PK), the Key Exchange Key (KEK) and the signature database (db), you enroll them yourself, and you sign every EFI binary the firmware will run. This post walks that end to end with efitools 1.9.2 and sbsign from sbsigntools 0.9.5, on a firmware that implements UEFI 2.9 or later. It assumes you already know why firmware verifies a bootloader before running it, which the complete guide to sandboxing Linux processes places in a wider picture of privilege reduction.

The four variables and who may write them

UEFI Secure Boot is driven by authenticated variables, defined in section 32 of the UEFI Specification. Four of them matter.

  • PK holds one certificate, the platform owner. Whoever holds the matching private key can replace the KEK.

  • KEK holds the certificates allowed to update db and dbx.

  • db holds the certificates and hashes of binaries the firmware will run.

  • dbx is the forbidden list. A match in dbx beats a match in db.

Each variable can only be changed by a payload signed with the key one level above it: db and dbx by a KEK, KEK by the PK, and the PK by itself. The exception is setup mode. When no PK is enrolled the firmware is in setup mode, writes need no signature, and enrolling a PK flips the machine into user mode, where Secure Boot enforcement begins. Every flow below depends on that one transition.

Generate the keys

Three independent RSA 2048 key pairs keep the blast radius small. The PK private key can live offline, the KEK is used rarely, and only the db key signs day to day.

Shell
for name in PK KEK db; do
  openssl req -new -x509 -newkey rsa:2048 -sha256 -nodes -days 3650 \
    -subj "/CN=Example Fleet ${name}/" \
    -keyout "${name}.key" -out "${name}.crt"
done
chmod 600 PK.key KEK.key db.key
Text
(no output; PK.key PK.crt KEK.key KEK.crt db.key db.crt are created)

RSA 2048 is what the firmware vendors test against, and some firmware rejects larger keys, which is why this example does not pick 4096. Store PK.key on removable media that stays in a safe.

Convert certificates to signed enrollment payloads

The firmware accepts an EFI Signature List (ESL) wrapped in an authenticated-variable header. cert-to-efi-sig-list builds the list and sign-efi-sig-list signs it.

Shell
GUID="$(uuidgen)"
echo "$GUID" > GUID.txt

for name in PK KEK db; do
  cert-to-efi-sig-list -g "$GUID" "${name}.crt" "${name}.esl"
done

sign-efi-sig-list -g "$GUID" -k PK.key  -c PK.crt  PK  PK.esl  PK.auth
sign-efi-sig-list -g "$GUID" -k PK.key  -c PK.crt  KEK KEK.esl KEK.auth
sign-efi-sig-list -g "$GUID" -k KEK.key -c KEK.crt db  db.esl  db.auth

The PK payload is signed by the PK itself, the KEK payload by the PK, and the db payload by the KEK. That mirrors the hierarchy above. The GUID names the owner in the variable and appears in efi-readvar output later.

Decide whether to keep the Microsoft keys

Replacing the PK does not force you to drop Microsoft's certificates from db. Many GPUs, network cards and RAID controllers carry option ROMs signed by the Microsoft UEFI CA. If db no longer trusts that CA, a machine with such a card can fail to display anything at boot, and recovering means clearing the variables in firmware setup. The rule we follow: keep Microsoft's third-party UEFI CA in db on desktops and servers with add-in cards, and drop it only on a fixed board whose option ROMs you have inventoried.

To keep it, export the existing variables before you change anything, and append your signed key rather than replacing the list.

Shell
efi-readvar -v db  -o old-db.esl
efi-readvar -v KEK -o old-KEK.esl
efi-readvar -v dbx -o old-dbx.esl
Text
Variable db, length 3636
Variable KEK, length 1556
Variable dbx, length 14500

The lengths vary per vendor and firmware release. The files are the raw ESLs. You will combine them with your own list in the next step.

Enroll the keys from setup mode

Put the firmware in setup mode through its setup screen (the option is usually named "Clear Secure Boot keys" or "Delete all keys"). Then, from Linux booted in that state, write the variables. efi-updatevar is part of efitools and writes through the efivarfs interface.

Shell
cat old-db.esl db.esl > db-combined.esl
sign-efi-sig-list -g "$GUID" -k KEK.key -c KEK.crt db db-combined.esl db-combined.auth

efi-updatevar -f db-combined.auth db
efi-updatevar -f KEK.auth KEK
efi-updatevar -f PK.auth PK
Text
(no output on success)

The PK goes last on purpose. The moment PK is written the firmware leaves setup mode, and later writes would then require valid signatures. Reverse the order and the db write fails with a permission error. Check the result:

Shell
efi-readvar -v PK
mokutil --sb-state
Text
Variable PK, length 833
PK: List 0, type X509
    Signature 0, size 805, owner 5d1b2c9e-3f4a-4d6e-9a31-0c7e2b8f1a44
        Subject:
            CN=Example Fleet PK
        Issuer:
            CN=Example Fleet PK
SecureBoot enabled

The GUID, size and length will differ on your machine. SecureBoot enabled from mokutil confirms the firmware is enforcing.

Sign the kernel and the bootloader

Firmware verifies the EFI executable it loads. With GRUB or systemd-boot as the first stage, sign that binary, and sign the kernel too if the loader verifies it (shim and GRUB with the shim_lock verifier do, and systemd-boot does for a unified kernel image). sbsign embeds an Authenticode signature.

Shell
sbsign --key db.key --cert db.crt \
  --output /boot/efi/EFI/Linux/vmlinuz-6.12.signed.efi \
  /boot/vmlinuz-6.12

sbverify --cert db.crt /boot/efi/EFI/Linux/vmlinuz-6.12.signed.efi
Text
Signing Unsigned original image
Signature verification OK

sbverify here checks the signature against your db certificate only. The firmware does its own check against the real db and dbx, so a kernel that passes sbverify can still be rejected if its hash is in dbx.

Keep it working after updates

The first kernel update after you switch is when people find out the signing step is not automatic. Hook it: on Debian derived systems a script in /etc/kernel/postinst.d/ runs after each kernel install, and with systemd-boot unified kernel images you sign the output of ukify in the build step. Whatever you pick, fail loudly: an unsigned kernel at the next boot is a locked-out machine.

Two operational points worth stating plainly. First, never delete the old signed kernel until the new one has booted. Second, rotating the db key means signing a new db payload with the KEK, enrolling it with efi-updatevar -a -f, signing the binaries with the new key, and only then removing the old certificate.

What this does not protect

Secure Boot checks the chain up to the kernel. It does not check the initramfs unless the kernel is a unified image that embeds it, and it does not check the root filesystem. Pair it with dm-verity or an equivalent for the rest.

Back up and rehearse recovery

Before you touch a production machine, copy the three private keys and the exported ESL files to offline storage, and write down the recovery path in plain steps: enter firmware setup, clear the keys, boot a rescue image, re-enroll. A fleet that has never rehearsed this will discover the firmware quirks during an outage. Vendors differ in how they expose setup mode, some require a firmware password, and a few ship boards where user-mode keys can be cleared only with a physical jumper. Learn which kind you own on a spare unit. Also keep a signed rescue kernel and bootloader on removable media, signed with the same db key, so a machine whose main kernel fails verification still has something it will boot. We treat the signing key as the most sensitive file in the build pipeline, with signing done on a dedicated host or in an HSM and never on a developer laptop.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.