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 / test_trust1_key_ceremony.sh

130 lines · 7479 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# test_trust1_key_ceremony.sh: self-test for trust1_key_ceremony.sh with
18# THROWAWAY keys in a private temp folder (deleted on exit). Never an owner
19# key. Checks: generate + verify; private keys are stored encrypted; two
20# backups each decrypt from their own copy; the record holds no private
21# material; a changed manifest, a swapped public key, and a wrong
22# passphrase are each refused; the Boot Signer cert can sign and verify an
23# EFI image (when sbsign/sbverify and a built loader are present; AIENOS_KEY_CEREMONY_EFI
24# names another PE image). If that last check cannot run the verdict is NOT_RUN (exit 3),
25# never ALL PASS.
26
27set -euo pipefail
28
29repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
30tool="${repo_root}/scripts/trust1_key_ceremony.sh"
31OPENSSL="${AIENOS_OPENSSL:-openssl}"
32
33work="$(mktemp -d)"
34trap 'rm -rf --one-file-system "${work}"' EXIT
35chmod 700 "${work}"
36
37fail() { echo "FAIL $*"; exit 1; }
38pass() { echo "PASS $*"; }
39
40printf 'throwaway-test-passphrase\n' >"${work}/pass"
41export AIENOS_CEREMONY_PASS_FILE="${work}/pass"
42out="${work}/ceremony"
43
44bash "${tool}" generate "${out}" --allow-online >/dev/null || fail "generate"
45bash "${tool}" verify "${out}/public" >/dev/null || fail "verify after generate"
46pass "generate: four keys and a root-signed manifest that verifies"
47
48for k in "${out}"/private/*.key.pem; do
49 grep -q "BEGIN ENCRYPTED PRIVATE KEY" "${k}" || fail "$(basename "${k}") not encrypted"
50done
51[[ "$(find "${out}/private" -type f | wc -l)" -eq 4 ]] || fail "unexpected files in private/"
52pass "every private key is stored encrypted"
53
54bash "${tool}" generate "${out}" --allow-online >/dev/null 2>&1 && fail "generate overwrote a ceremony"
55pass "generate refuses to overwrite"
56
57mkdir -p "${work}/mediaA" "${work}/mediaB"
58bash "${tool}" backup "${out}" "${work}/mediaA" backup-A >/dev/null || fail "backup A"
59bash "${tool}" backup "${out}" "${work}/mediaB" backup-B >/dev/null || fail "backup B"
60bash "${tool}" record "${out}" >/dev/null || fail "record"
61grep -q '^backup_count=2$' "${out}/public/ceremony_record.txt" || fail "record backup count"
62grep -rq "PRIVATE KEY" "${out}/public" && fail "private material in public/"
63pass "two backups decrypt from their own copies; record has no private material"
64
65printf 'wrong-passphrase-123\n' >"${work}/wrongpass"
66mkdir -p "${work}/mediaC"
67AIENOS_CEREMONY_PASS_FILE="${work}/wrongpass" bash "${tool}" backup "${out}" "${work}/mediaC" backup-C \
68 >/dev/null 2>&1 && fail "backup accepted a wrong passphrase"
69pass "a wrong passphrase cannot open the backup copy"
70
71cp -r "${out}/public" "${work}/tampered"
72sed -i.bak 's/^generation=1$/generation=2/' "${work}/tampered/authority_manifest.txt"
73rm -f "${work}/tampered/authority_manifest.txt.bak"
74bash "${tool}" verify "${work}/tampered" >/dev/null 2>&1 && fail "changed manifest verified"
75pass "a changed manifest is refused"
76
77cp -r "${out}/public" "${work}/swapped"
78"${OPENSSL}" genpkey -algorithm ed25519 -out "${work}/other.pem" 2>/dev/null
79"${OPENSSL}" pkey -in "${work}/other.pem" -pubout -out "${work}/swapped/release_signer.pub.pem"
80bash "${tool}" verify "${work}/swapped" >/dev/null 2>&1 && fail "swapped release key verified"
81pass "a swapped public key is refused"
82
83# Rotation: add -> verify (old and new both trusted) -> revoke (only new).
84cp -r "${out}/public" "${work}/gen1snap"
85printf 'release payload\n' >"${work}/payload"
86"${OPENSSL}" pkeyutl -sign -rawin -inkey "${out}/private/release_signer.key.pem" -passin file:"${work}/pass" \
87 -in "${work}/payload" -out "${work}/payload.oldsig"
88bash "${tool}" rotate "${out}" release_signer add >/dev/null || fail "rotate add"
89grep -q '^generation=2$' "${out}/public/authority_manifest.txt" || fail "add did not make generation 2"
90grep -q '^role\.release_signer\.next\.pub_sha256=' "${out}/public/authority_manifest.txt" || fail "next key not in manifest"
91"${OPENSSL}" pkeyutl -verify -rawin -pubin -inkey "${out}/public/release_signer.pub.pem" \
92 -in "${work}/payload" -sigfile "${work}/payload.oldsig" >/dev/null 2>&1 || fail "old key stopped verifying during transition"
93pass "rotation add: generation 2 trusts old and new release keys; old signatures still verify"
94bash "${tool}" rotate "${out}" release_signer revoke >/dev/null || fail "rotate revoke"
95grep -q '^generation=3$' "${out}/public/authority_manifest.txt" || fail "revoke did not make generation 3"
96grep -q '^role\.release_signer\.revoked_pub_sha256=' "${out}/public/authority_manifest.txt" || fail "old key not listed revoked"
97"${OPENSSL}" pkeyutl -verify -rawin -pubin -inkey "${out}/public/release_signer.pub.pem" \
98 -in "${work}/payload" -sigfile "${work}/payload.oldsig" >/dev/null 2>&1 && fail "revoked key still current"
99ls "${out}"/private/release_signer.gen2.revoked.key.pem >/dev/null || fail "old key not quarantined"
100pass "rotation revoke: generation 3 trusts only the new key; old key quarantined, not deleted"
101bash "${tool}" rotate "${out}" owner_root add >/dev/null 2>&1 && fail "rotate allowed owner_root"
102pass "owner_root cannot be rotated by the routine tool (special recovery ceremony only)"
103bash "${tool}" verify "${work}/gen1snap" 1 >/dev/null || fail "intact generation 1 snapshot refused at minimum 1"
104bash "${tool}" verify "${work}/gen1snap" 3 >/dev/null 2>&1 && fail "rollback to generation 1 accepted"
105bash "${tool}" verify "${out}/public" 3 >/dev/null || fail "current generation 3 refused"
106pass "a validly signed older generation is refused when generation 3 is the minimum"
107
108efi="${AIENOS_KEY_CEREMONY_EFI:-${repo_root}/target/aarch64-unknown-uefi/release/aienos-handoff.efi}"
109skipped_checks=()
110if command -v sbsign >/dev/null && command -v sbverify >/dev/null && [[ -f "${efi}" ]]; then
111 "${OPENSSL}" pkey -in "${out}/private/boot_signer.key.pem" -passin file:"${work}/pass" \
112 -out "${work}/bs.key" 2>/dev/null
113 sbsign --key "${work}/bs.key" --cert "${out}/public/boot_signer.crt.pem" \
114 --output "${work}/signed.efi" "${efi}" 2>/dev/null
115 sbverify --cert "${out}/public/boot_signer.crt.pem" "${work}/signed.efi" >/dev/null 2>&1 \
116 || fail "Boot Signer signature does not verify"
117 pass "Boot Signer cert signs and verifies the EFI loader (sbsign/sbverify)"
118else
119 echo "SKIP Boot Signer EFI signing (sbsign, sbverify or built loader missing)"; skipped_checks+=("Boot Signer EFI signing")
120fi
121
122# A skipped check is not a passed check. The ALL PASS line is printed only when
123# every check ran; otherwise NOT_RUN and exit 3 (the qualify table maps 3 to
124# NOT_RUN).
125if [[ ${#skipped_checks[@]} -gt 0 ]]; then
126 printf 'NOT_RUN skipped check: %s\n' "${skipped_checks[@]}"
127 echo "TRUST-1 key ceremony self-test (throwaway keys): NOT_RUN (${#skipped_checks[@]} check(s) skipped; every other check passed)"
128 exit 3
129fi
130echo "TRUST-1 key ceremony self-test (throwaway keys): ALL PASS"