How to Enable UEFI Secure Boot on Linux
Overview
Secure Boot is a UEFI option that can be turned on or off. (On a few machines Secure Boot is enabled with no option to disable it, and the motherboard ships only with Windows' public-key certificates, so it can load Windows and nothing else. The user has no choice: you can't turn it off or switch operating systems, and this post doesn't apply.) The public-key certificates that Secure Boot needs are stored in the motherboard's flash (not on the disk!). A small flash area on the motherboard holds the PK, KEK, db, and dbx certificate chain. So before you start, make sure Secure Boot can be disabled in your computer's UEFI-BIOS. Otherwise, if you make a mistake, the certificates won't match any program file, the motherboard will refuse to load anything, and you'll brick it. (It isn't really a BIOS, but many people call it UEFI-BIOS, so I'll use that name here too.)
A word on how Fedora manages to boot with Secure Boot enabled: the Fedora project actually buys a signature from Microsoft, so the motherboard recognizes Fedora just like Windows. The open-source community discourages this kind of purchase, but the benefit is that Fedora can be recognized and booted on almost any machine that can boot Windows, even if the hardware can't disable Secure Boot.
I have only tested this successfully on a ThinkPad and haven't tried other machines, but in theory it should work there as well.
Tools
The following tools are essential:
- openssl
- efitools
- sbsigntools
If you installed prebuilt packages from a third party, you can skip this:
When building efitools from source, you need a Perl module called File Slurp, available on cpan.
Preparation
Boot into the UEFI-BIOS setup, find the Secure Boot option on the security page, disable Secure Boot (if it isn't already), then delete or clear all certificates. The machine should switch from User Mode to Setup Mode. If you can't do these two things, the later steps may be affected.
Steps
Concepts
A brief introduction to PK, KEK, db, dbx, and MOK.
-
Platform Key (PK) – The PK establishes trust between the hardware platform and the platform owner. A hardware platform can be owned by only one owner, i.e. there can be only one PK. The owner's public key is stored in the PK variable in flash, and the owner's private key can sign and manage PK, KEK, db, and dbx.
-
Key Exchange Key (KEK) – The KEK establishes trust between the hardware platform and the operating system. Multiple KEK public keys can be stored in the motherboard flash, i.e. there can be multiple KEKs, one for each bootable operating system. Likewise, the KEK private key can sign and manage db and dbx.
-
Database (db) – db is the signature database of EFI files permitted to load. Like KEK, db can hold many public keys. (On a UEFI platform, the OS loader is an EFI file.)
-
Database Excluded (dbx) – dbx is a blacklist signature database: any EFI file whose signature is in it gets blocked. dbx can also hold many public keys.
-
Machine Owner Key (MOK) – MOK serves the same role as db, but it isn't part of standard Secure Boot;
PreLoaderandShimuse MOK.
Create the PK, KEK, and db key pairs
This requires uuidgen on your Linux system:
echo $(uuidgen) >guid
If you don't plan to boot Windows, ignore everything in this post related to Windows.
If your system doesn't have uuidgen, write any GUID into that guid file. Then write Microsoft's Owner GUID into another file:
echo 77fa9abd-0359-4d32-bd60-28f4e78f784b >ms-guid
Create the key pair:
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=Platform Key/" -keyout PK.key -out PK.crt -days 3650 -nodes -sha256
You can set the -subj argument yourself. -days is the key's validity period—10 years here, but you can choose another value. This produces the private key PK.key and the public key PK.crt.
KEK and db are created the same way:
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=Key Exchange Key/" -keyout KEK.key -out KEK.crt -days 3650 -nodes -sha256
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=Database/" -keyout db.key -out db.crt -days 3650 -nodes -sha256
No dbx blacklist is created here, since we don't need it for now and have no blacklist signature files. MOK isn't universal, so it isn't covered either.
Create EFI signature list files
An EFI signature list file is simply a public key paired with the platform owner's GUID:
cert-to-efi-sig-list -g $(cat guid) PK.crt PK.esl
cert-to-efi-sig-list -g $(cat guid) KEK.crt KEK.esl
cert-to-efi-sig-list -g $(cat guid) db.crt db.esl
Now all three public keys are list files.
To also boot the Windows installation on disk, download three Microsoft public keys—one KEK and two db:
curl -L http://go.microsoft.com/fwlink/?LinkID=321185 -o MSKEK.der
curl -L http://go.microsoft.com/fwlink/?LinkID=321192 -o MSdbWPCA.der
curl -L http://go.microsoft.com/fwlink/?LinkID=321194 -o MSdbUDSCA.der
These three Microsoft keys are in a different format from the ones used here, so convert them:
openssl x509 -in MSKEK.der -inform DER -out MSKEK.crt
openssl x509 -in MSdbWPCA.der -inform DER -out MSdbWPCA.crt
openssl x509 -in MSdbUDSCA.der -inform DER -out MSdbUDSCA.crt
They also need to be converted into list files, but here it's best to use Microsoft's Owner GUID:
cert-to-efi-sig-list -g $(cat ms-guid) MSKEK.crt MSKEK.esl
cert-to-efi-sig-list -g $(cat ms-guid) MSdbWPCA.crt MSdbWPCA.esl
cert-to-efi-sig-list -g $(cat ms-guid) MSdbUDSCA.crt MSdbUDSCA.esl
Note: on a ThinkPad, adding a KEK a second time (with the -a flag) fails. My workaround was to merge the lists into a single list file:
cat KEK.esl MSKEK.esl >realKEK.esl
cat db.esl MSdbWPCA.esl MSdbUDSCA.esl >realDB.esl
According to the efitools documentation, concatenating with cat is a valid way to merge them.
Sign the list files
sign-efi-sig-list -k PK.key -c PK.crt PK PK.esl PK.esl.signed
sign-efi-sig-list -k PK.key -c PK.crt KEK realKEK.esl realKEK.esl.signed
sign-efi-sig-list -k KEK.key -c KEK.crt db realDB.esl realDB.esl.signed
Here the platform owner's key has the highest authority and can sign both the PK itself and the KEK. The KEK establishes trust between the OS and the hardware platform and is used only to sign db.
Sign the EFI binary
This is the final step of key management and signing. I use grub; others such as elilo work the same way. Sign grub's EFI file:
sbsign --key db.key --cert db.crt --output grubx64.efi.signed /boot/efi/EFI/slackware64/grub64.efi
You don't need to sign the kernel vmlinuz, since it isn't the platform's boot EFI file, though signing it does no harm. If you want to sign the kernel, do it the same way as grub, using the private key db.key and its public key db.crt. The db here is the one you created yourself—not the one merged with Microsoft's db, which is realDB.
Write to efivars
This is the last step of the whole process. First make sure efivarfs is mounted correctly: ls /sys/firmware/efi/efivars/ to see whether files exist. Some systems mount efivarfs by default and some don't (the following steps require root):
mount -t efivarfs efivarfs /sys/firmware/efi/efivars/
Then write the three signed list files to the motherboard:
efi-updatevar -f PK.esl.signed PK
efi-updatevar -f realKEK.esl.signed KEK
efi-updatevar -f realDB.esl.signed db
A solution on LinuxQuestions adds the
-aflag toefi-updatevarto append entries, but on the ThinkPad every second attempt was aborted withOperation not permitted. By merging the EFI list files, you only need to add once and everything is included, with no need for-a. It's hard to tell whether the issue is a key-chain logic problem or a hardware protection mechanism, but with the merge approach both Windows and Linux boot normally under Secure Boot, so it seems fine.
When everything is done, run efi-readvar to check the result. As long as efivarfs is still mounted, you can view it even as a non-root user.
Related links
[pcworld.com](http://www.pcworld.