interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_secureboot_signing_test.sh

141 lines · 6378 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# qemu_secureboot_signing_test.sh: TRUST-1 Gate 4, Secure Boot enforcement in QEMU.
18#
19# Boots the AIENOS handoff loader under AArch64 UEFI firmware with Secure
20# Boot ON, using the Debian "snakeoil" test key set (AAVMF_CODE.snakeoil.fd +
21# AAVMF_VARS.snakeoil.fd). The snakeoil key is a published TEST key shipped
22# with qemu-efi-aarch64 (password "snakeoil"); it is never an owner key and
23# nothing here touches real firmware, real keys or the TPM.
24#
25# Cases (each a fresh copy of the firmware variables):
26# 1. signed with the snakeoil key (the enrolled db key) -> must boot to "kernel: alive"
27# 2. unsigned -> must be refused
28# 3. signed, then one byte flipped in the signed region -> must be refused
29# 4. signed with a throwaway key that is not in db -> must be refused
30# Case 1 is the control: it proves the firmware boots our loader at all, so
31# the refusals in 2-4 come from signature checking, not from a broken image.
32#
33# Exit 0 = all four as expected; 1 = a case failed; 2 = missing tools.
34
35set -euo pipefail
36
37repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
38cd "${repo_root}"
39
40code_fd="${AAVMF_SB_CODE:-/usr/share/AAVMF/AAVMF_CODE.snakeoil.fd}"
41vars_fd="${AAVMF_SB_VARS:-/usr/share/AAVMF/AAVMF_VARS.snakeoil.fd}"
42sk_key="${SNAKEOIL_KEY:-/usr/share/qemu-efi-aarch64/PkKek-1-snakeoil.key}"
43sk_crt="${SNAKEOIL_CERT:-/usr/share/qemu-efi-aarch64/PkKek-1-snakeoil.pem}"
44boot_timeout="${AIENOS_QEMU_TIMEOUT:-120}"
45reject_timeout="${AIENOS_SB_REJECT_TIMEOUT:-45}"
46
47for t in qemu-system-aarch64 sbsign sbverify openssl cargo; do
48 command -v "${t}" >/dev/null || { echo "Error: ${t} not installed" >&2; exit 2; }
49done
50for f in "${code_fd}" "${vars_fd}" "${sk_key}" "${sk_crt}"; do
51 [[ -r "${f}" ]] || { echo "Error: ${f} not found (package qemu-efi-aarch64)" >&2; exit 2; }
52done
53
54commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
55echo "Building aienos-handoff binary for the Secure Boot test..."
56AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \
57 -p aienos-boot --target aarch64-unknown-uefi --features handoff --bin aienos-handoff
58efi="target/aarch64-unknown-uefi/release/aienos-handoff.efi"
59
60work="$(mktemp -d)"
61trap 'rm -rf "${work}"' EXIT
62chmod 700 "${work}"
63
64# sbsign cannot read an encrypted key; decrypt the public test key into the
65# private temp folder only.
66openssl pkey -in "${sk_key}" -passin pass:snakeoil -out "${work}/snakeoil.key" 2>/dev/null
67openssl req -new -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=AIENOS throwaway not-in-db/" \
68 -keyout "${work}/wrong.key" -out "${work}/wrong.crt" 2>/dev/null
69
70mkimg() { # name source
71 mkdir -p "${work}/$1/esp/EFI/BOOT" "${work}/$1/esp/EFI/AIENOS"
72 touch "${work}/$1/esp/EFI/AIENOS/BOOTREPORT.TXT"
73 cp "$2" "${work}/$1/esp/EFI/BOOT/BOOTAA64.EFI"
74}
75
76sbsign --key "${work}/snakeoil.key" --cert "${sk_crt}" --output "${work}/signed.efi" "${efi}" 2>/dev/null
77sbverify --cert "${sk_crt}" "${work}/signed.efi" >/dev/null 2>&1 \
78 || { echo "FAIL sbverify does not accept our own snakeoil signature"; exit 1; }
79sbsign --key "${work}/wrong.key" --cert "${work}/wrong.crt" --output "${work}/wrongkey.efi" "${efi}" 2>/dev/null
80
81# Flip one byte in the middle of the image (inside the code, which the
82# Authenticode hash covers; the appended signature is at the end).
83cp "${work}/signed.efi" "${work}/tampered.efi"
84size="$(stat -c %s "${efi}")"
85off=$((size / 2))
86orig="$(od -An -tu1 -j "${off}" -N1 "${work}/tampered.efi" | tr -d ' ')"
87printf "$(printf '\\%03o' $(((orig ^ 0xff) & 0xff)))" \
88 | dd of="${work}/tampered.efi" bs=1 seek="${off}" conv=notrunc status=none
89sbverify --cert "${sk_crt}" "${work}/tampered.efi" >/dev/null 2>&1 \
90 && { echo "FAIL tampered image still verifies (byte flip missed the hashed region)"; exit 1; }
91
92mkimg signed "${work}/signed.efi"
93mkimg unsigned "${efi}"
94mkimg tampered "${work}/tampered.efi"
95mkimg wrongkey "${work}/wrongkey.efi"
96
97boot() { # name timeout
98 local d="${work}/$1"
99 cp "${vars_fd}" "${d}/vars.fd"
100 set +e
101 timeout "$2" qemu-system-aarch64 \
102 -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
103 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
104 -drive if=pflash,format=raw,file="${d}/vars.fd" \
105 -drive if=none,id=esp,format=raw,file=fat:rw:"${d}/esp" \
106 -device virtio-blk-pci,drive=esp \
107 -device ramfb -display none -nic none \
108 -serial file:"${d}/serial.log" -no-reboot >/dev/null 2>&1
109 set -e
110 tr -d '\r' <"${d}/serial.log" >"${d}/serial.txt"
111 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${d}/serial.txt" "${AIENOS_LOG_DIR}/gate4_secureboot_$1.log"
112}
113
114status=0
115boot signed "${boot_timeout}"
116if grep -q "kernel: alive" "${work}/signed/serial.txt"; then
117 echo "PASS signed with enrolled test key: booted to kernel alive (Secure Boot on)"
118else
119 echo "FAIL signed image did not boot; refusals below would prove nothing"
120 tail -n 20 "${work}/signed/serial.txt"
121 exit 1
122fi
123
124for c in unsigned tampered wrongkey; do
125 boot "${c}" "${reject_timeout}"
126 if grep -q "kernel: alive" "${work}/${c}/serial.txt"; then
127 echo "FAIL ${c} image booted with Secure Boot on"
128 status=1
129 elif grep -q "Access Denied" "${work}/${c}/serial.txt"; then
130 # EFI_ACCESS_DENIED from LoadImage is the firmware signature verdict.
131 echo "PASS ${c} image refused by firmware signature check (Access Denied)"
132 else
133 echo "FAIL ${c} image did not boot, but no Access Denied verdict was logged"
134 status=1
135 fi
136done
137
138if [[ ${status} -eq 0 ]]; then
139 echo "TRUST-1 Gate 4 Secure Boot signing test: ALL PASS"
140fi
141exit "${status}"