Merge pull request #930 from c-po/T861-secure-boot

T861: minor improvements to secure-boot certificate handling
This commit is contained in:
Christian Breunig 2025-03-21 08:37:34 +01:00 committed by GitHub
commit d552f7f8c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 38 additions and 21 deletions

3
.gitignore vendored
View File

@ -1,11 +1,12 @@
.build/config
build/*
config/*
*.pyc
packer_build/*
packer_cache/*
key/*
packages/*
!packages/*/
data/live-build-config/includes.chroot/var/lib/shim-signed/mok/*
/testinstall*.img
/testinstall*.efivars
/*.qcow2

View File

@ -79,7 +79,7 @@ clean:
rm -f config/binary config/bootstrap config/chroot config/common config/source
rm -f build.log
rm -f vyos-*.iso
rm -f *.img
rm -f *.img *.efivars
rm -f *.xz
rm -f *.vhd
rm -f *.raw

1
data/certificates/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.key

View File

@ -1,7 +1,7 @@
#!/bin/sh
SIGN_FILE=$(find /usr/lib -name sign-file)
MOK_KEY="/var/lib/shim-signed/mok/MOK.key"
MOK_CERT="/var/lib/shim-signed/mok/MOK.pem"
KERNEL_KEY="/var/lib/shim-signed/mok/vyos-dev-2025-linux.key"
KERNEL_CERT="/var/lib/shim-signed/mok/vyos-dev-2025-linux.pem"
VMLINUZ=$(readlink /boot/vmlinuz)
# All Linux Kernel modules need to be cryptographically signed
@ -13,10 +13,19 @@ find /lib/modules -type f -name \*.ko | while read MODULE; do
fi
done
if [ ! -f ${MOK_KEY} ]; then
if [ ! -f ${KERNEL_KEY} ] && [ ! -f ${KERNEL_CERT} ]; then
echo "I: Signing key for Linux Kernel not found - Secure Boot not possible"
else
echo "I: Signing Linux Kernel for Secure Boot"
sbsign --key ${MOK_KEY} --cert ${MOK_CERT} /boot/${VMLINUZ} --output /boot/${VMLINUZ}
sbsign --key ${KERNEL_KEY} --cert ${KERNEL_CERT} /boot/${VMLINUZ} --output /boot/${VMLINUZ}
sbverify --list /boot/${VMLINUZ}
rm -f ${KERNEL_KEY}
fi
for cert in $(ls /var/lib/shim-signed/mok/); do
if grep -rq "BEGIN PRIVATE KEY" /var/lib/shim-signed/mok/${cert}; then
echo "Found private key - bailing out"
exit 1
fi
done

View File

@ -1,11 +0,0 @@
# Secure Boot
## CA
Create Certificate Authority used for Kernel signing. CA is loaded into the
Machine Owner Key store on the target system.
```bash
openssl req -new -x509 -newkey rsa:4096 -keyout MOK.key -outform DER -out MOK.der -days 36500 -subj "/CN=VyOS Secure Boot CA/" -nodes
openssl x509 -inform der -in MOK.der -out MOK.pem
```

View File

@ -18,7 +18,7 @@
# This Dockerfile is installable on both x86, x86-64, armhf and arm64 systems
ARG ARCH=
FROM ${ARCH}debian:bookworm
FROM ${ARCH}debian:bookworm-slim
RUN grep "VERSION_ID" /etc/os-release || (echo 'VERSION_ID="12"' >> /etc/os-release)

View File

@ -398,6 +398,16 @@ try:
loginVM(c, log)
#################################################
# Check for no private key contents within the image
#################################################
msg = 'Found private key - bailing out'
c.sendline(f'if sudo grep -rq "BEGIN PRIVATE KEY" /var/lib/shim-signed/mok; then echo {msg}; exit 1; fi')
tmp = c.expect([f'\n{msg}', op_mode_prompt])
if tmp == 0:
log.error(msg)
exit(1)
#################################################
# Installing into VyOS system
#################################################
@ -879,7 +889,7 @@ except pexpect.exceptions.ExceptionPexpect:
EXCEPTION = 1
except Exception:
log.error('Unknown error occured while VyOS!')
log.error('Unknown error occured!')
traceback.print_exc()
EXCEPTION = 1

View File

@ -367,6 +367,11 @@ if __name__ == "__main__":
shutil.copytree("data/live-build-config/", lb_config_dir)
os.makedirs(lb_config_dir, exist_ok=True)
## Secure Boot - Copy public Keys to image
sb_certs = 'data/certificates'
if os.path.isdir(sb_certs):
shutil.copytree(sb_certs, f'{lb_config_dir}/includes.chroot/var/lib/shim-signed/mok')
# Switch to the build directory, this is crucial for the live-build work
# because the efective build config files etc. are there.
#
@ -611,6 +616,7 @@ DOCUMENTATION_URL="{build_config['documentation_url']}"
## Configure live-build
lb_config_tmpl = jinja2.Template("""
lb config noauto \
--no-color \
--apt-indices false \
--apt-options "--yes -oAPT::Get::allow-downgrades=true" \
--apt-recommends false \

View File

@ -36,12 +36,13 @@ do
done
# Change name of Signing Cert
sed -i -e "s/CN =.*/CN=VyOS build time autogenerated kernel key/" certs/default_x509.genkey
sed -i -e "s/CN =.*/CN=VyOS Networks build time autogenerated Kernel key/" certs/default_x509.genkey
TRUSTED_KEYS_FILE=trusted_keys.pem
# start with empty key file
echo -n "" > $TRUSTED_KEYS_FILE
CERTS=$(find ../../../../data/live-build-config/includes.chroot/var/lib/shim-signed/mok -name "*.pem" -type f || true)
GIT_ROOT=$(git rev-parse --show-toplevel)
CERTS=$(find ${GIT_ROOT}/data/certificates -name "*.pem" -type f || true)
if [ ! -z "${CERTS}" ]; then
# add known public keys to Kernel certificate chain
for file in $CERTS; do