aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / test_trust1_key_ceremony.sh
| 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 | |
| 27 | set -euo pipefail |
| 28 | |
| 29 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 30 | tool="${repo_root}/scripts/trust1_key_ceremony.sh" |
| 31 | OPENSSL="${AIENOS_OPENSSL:-openssl}" |
| 32 | |
| 33 | work="$(mktemp -d)" |
| 34 | trap 'rm -rf --one-file-system "${work}"' EXIT |
| 35 | chmod 700 "${work}" |
| 36 | |
| 37 | fail() { echo "FAIL $*"; exit 1; } |
| 38 | pass() { echo "PASS $*"; } |
| 39 | |
| 40 | printf 'throwaway-test-passphrase\n' >"${work}/pass" |
| 41 | export AIENOS_CEREMONY_PASS_FILE="${work}/pass" |
| 42 | out="${work}/ceremony" |
| 43 | |
| 44 | bash "${tool}" generate "${out}" --allow-online >/dev/null || fail "generate" |
| 45 | bash "${tool}" verify "${out}/public" >/dev/null || fail "verify after generate" |
| 46 | pass "generate: four keys and a root-signed manifest that verifies" |
| 47 | |
| 48 | for k in "${out}"/private/*.key.pem; do |
| 49 | grep -q "BEGIN ENCRYPTED PRIVATE KEY" "${k}" || fail "$(basename "${k}") not encrypted" |
| 50 | done |
| 51 | [[ "$(find "${out}/private" -type f | wc -l)" -eq 4 ]] || fail "unexpected files in private/" |
| 52 | pass "every private key is stored encrypted" |
| 53 | |
| 54 | bash "${tool}" generate "${out}" --allow-online >/dev/null 2>&1 && fail "generate overwrote a ceremony" |
| 55 | pass "generate refuses to overwrite" |
| 56 | |
| 57 | mkdir -p "${work}/mediaA" "${work}/mediaB" |
| 58 | bash "${tool}" backup "${out}" "${work}/mediaA" backup-A >/dev/null || fail "backup A" |
| 59 | bash "${tool}" backup "${out}" "${work}/mediaB" backup-B >/dev/null || fail "backup B" |
| 60 | bash "${tool}" record "${out}" >/dev/null || fail "record" |
| 61 | grep -q '^backup_count=2$' "${out}/public/ceremony_record.txt" || fail "record backup count" |
| 62 | grep -rq "PRIVATE KEY" "${out}/public" && fail "private material in public/" |
| 63 | pass "two backups decrypt from their own copies; record has no private material" |
| 64 | |
| 65 | printf 'wrong-passphrase-123\n' >"${work}/wrongpass" |
| 66 | mkdir -p "${work}/mediaC" |
| 67 | AIENOS_CEREMONY_PASS_FILE="${work}/wrongpass" bash "${tool}" backup "${out}" "${work}/mediaC" backup-C \ |
| 68 | >/dev/null 2>&1 && fail "backup accepted a wrong passphrase" |
| 69 | pass "a wrong passphrase cannot open the backup copy" |
| 70 | |
| 71 | cp -r "${out}/public" "${work}/tampered" |
| 72 | sed -i.bak 's/^generation=1$/generation=2/' "${work}/tampered/authority_manifest.txt" |
| 73 | rm -f "${work}/tampered/authority_manifest.txt.bak" |
| 74 | bash "${tool}" verify "${work}/tampered" >/dev/null 2>&1 && fail "changed manifest verified" |
| 75 | pass "a changed manifest is refused" |
| 76 | |
| 77 | cp -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" |
| 80 | bash "${tool}" verify "${work}/swapped" >/dev/null 2>&1 && fail "swapped release key verified" |
| 81 | pass "a swapped public key is refused" |
| 82 | |
| 83 | # Rotation: add -> verify (old and new both trusted) -> revoke (only new). |
| 84 | cp -r "${out}/public" "${work}/gen1snap" |
| 85 | printf '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" |
| 88 | bash "${tool}" rotate "${out}" release_signer add >/dev/null || fail "rotate add" |
| 89 | grep -q '^generation=2$' "${out}/public/authority_manifest.txt" || fail "add did not make generation 2" |
| 90 | grep -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" |
| 93 | pass "rotation add: generation 2 trusts old and new release keys; old signatures still verify" |
| 94 | bash "${tool}" rotate "${out}" release_signer revoke >/dev/null || fail "rotate revoke" |
| 95 | grep -q '^generation=3$' "${out}/public/authority_manifest.txt" || fail "revoke did not make generation 3" |
| 96 | grep -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" |
| 99 | ls "${out}"/private/release_signer.gen2.revoked.key.pem >/dev/null || fail "old key not quarantined" |
| 100 | pass "rotation revoke: generation 3 trusts only the new key; old key quarantined, not deleted" |
| 101 | bash "${tool}" rotate "${out}" owner_root add >/dev/null 2>&1 && fail "rotate allowed owner_root" |
| 102 | pass "owner_root cannot be rotated by the routine tool (special recovery ceremony only)" |
| 103 | bash "${tool}" verify "${work}/gen1snap" 1 >/dev/null || fail "intact generation 1 snapshot refused at minimum 1" |
| 104 | bash "${tool}" verify "${work}/gen1snap" 3 >/dev/null 2>&1 && fail "rollback to generation 1 accepted" |
| 105 | bash "${tool}" verify "${out}/public" 3 >/dev/null || fail "current generation 3 refused" |
| 106 | pass "a validly signed older generation is refused when generation 3 is the minimum" |
| 107 | |
| 108 | efi="${AIENOS_KEY_CEREMONY_EFI:-${repo_root}/target/aarch64-unknown-uefi/release/aienos-handoff.efi}" |
| 109 | skipped_checks=() |
| 110 | if 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)" |
| 118 | else |
| 119 | echo "SKIP Boot Signer EFI signing (sbsign, sbverify or built loader missing)"; skipped_checks+=("Boot Signer EFI signing") |
| 120 | fi |
| 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). |
| 125 | if [[ ${#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 |
| 129 | fi |
| 130 | echo "TRUST-1 key ceremony self-test (throwaway keys): ALL PASS" |