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_attended_evidence_check.sh

227 lines · 15697 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_attended_evidence_check.sh: HOST VALIDATION ONLY fixture tests
18# for scripts/trust1_attended_evidence_check.sh. Every fixture is synthetic or
19# made with throwaway keys in a temp folder; nothing touches Machine 1, a TPM,
20# firmware, boot media or a real key.
21#
22# For every gate the checker must ACCEPT one good record and REJECT each of:
23# missing, mismatched (wrong digest or candidate), partial and failing records.
24# Negative control: the whole suite is first run against a stub checker that
25# accepts everything; that run must be RED (catch nothing). Then the real
26# checker must be GREEN. Marker on success:
27# TRUST1_ATTENDED_EVIDENCE_CHECK self-test (HOST VALIDATION ONLY): ALL PASS
28# Env: AIENOS_QUIET_FLAG is not used; no chip or QEMU is started.
29set -uo pipefail
30
31repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
32real="${repo_root}/scripts/trust1_attended_evidence_check.sh"
33campaign="${repo_root}/scripts/tpm_measurement_campaign.sh"
34ceremony="${repo_root}/scripts/trust1_key_ceremony.sh"
35for t in jq sha256sum openssl; do command -v "${t}" >/dev/null || { echo "Error: ${t} not installed" >&2; exit 2; }; done
36
37work="$(mktemp -d)"
38trap 'rm -rf --one-file-system "${work}"' EXIT
39chmod 700 "${work}"
40echo "HOST VALIDATION ONLY: fixture tests, no hardware, throwaway keys"
41
42# ------------------------------------------------------------------ fixtures
43hex() { printf '%064x' "$1"; }
44make_capture() { # dir label boot_id secure_boot pcr7_seed [pcr1_seed]
45 local d="$1" i
46 mkdir -p "${d}/efivars"
47 for i in 0 1 2 3 4 5 6 7 10; do
48 local seed="${i}"
49 [[ "${i}" == 7 ]] && seed="${5}"
50 [[ "${i}" == 1 && -n "${6:-}" ]] && seed="${6}"
51 echo "${i} 0x$(hex "$((seed + 100))")"
52 done >"${d}/pcr_sha256.norm"
53 echo "fake pcrs" >"${d}/pcr_sha256.txt"
54 echo "fake efivar" >"${d}/efivars/PK.efivar"
55 {
56 echo "capture_label=$2"; echo "capture_utc=2026-10-05T00:00:00Z"; echo "host=fixture"
57 echo "boot_id=$3"; echo "firmware_version=5.36_0ACUM018"
58 echo "secure_boot=$4"; echo "setup_mode=0"
59 echo "PK_efivar_sha256=$(hex 1)"; echo "KEK_efivar_sha256=$(hex 2)"
60 echo "db_efivar_sha256=$(hex 3)"; echo "dbx_efivar_sha256=$(hex 4)"
61 echo "eventlog_replay=match"
62 } >"${d}/capture.env"
63 (cd "${d}" && find . -type f ! -name SHA256SUMS -print0 | sort -z | xargs -0 sha256sum >SHA256SUMS)
64}
65resum() { (cd "$1" && find . -type f ! -name SHA256SUMS -print0 | sort -z | xargs -0 sha256sum >SHA256SUMS); }
66
67TEST_SHA="$(hex 4242)"
68LOADER_SHA="$(hex 7777)"
69make_log() { # out
70 cat >"$1" <<LOG
71== recovery environment identity
72captured_utc: 2026-10-05T00:00:00Z
73PASS booted_from_removable_media: root is /dev/ram0 (not the internal NVMe)
74secure_boot_byte: 1
75PASS secure_boot_state_recorded: byte=1 (1=enabled)
76PASS nvme_root_mounted: findmnt /mnt/root
77PASS pcrs_read: tpm2_pcrread sha256
78PASS event_log_recorded: /sys/kernel/security/tpm0/binary_bios_measurements
79efi_image: /EFI/ubuntu/shimaa64.efi sha256=$(hex 5555)
80efi_image: /EFI/AIENOS/loader.efi sha256=${LOADER_SHA}
81PASS esp_loaders_hashed: 2 EFI image(s) hashed
82PASS recovery_unlock_readonly: store opened read-only with the offline spare
83test_artifact: trust1-roundtrip-test.txt sha256=${TEST_SHA}
84PASS test_artifact_round_trip: expected ${TEST_SHA}
85== verdict
86RECOVERY_BOOT_GATE: PASS
87LOG
88}
89
90# --------------------------------------------------------------------- suite
91FAILS=0
92CHECKER="${real}"
93expect() { # want(accept|reject) name needle -- args...
94 local want="$1" name="$2" needle="$3"; shift 4
95 local out rc
96 out="$(bash "${CHECKER}" "$@" 2>&1)"; rc=$?
97 if [[ "${want}" == accept ]]; then
98 if [[ "${rc}" -eq 0 && "${out}" == *"ACCEPT"* ]]; then echo "ok accept: ${name}"; return; fi
99 else
100 if [[ "${rc}" -eq 1 && "${out}" == *"REJECT"* && "${out}" == *"${needle}"* ]]; then echo "ok reject: ${name}"; return; fi
101 fi
102 echo "MISS ${want} expected: ${name} (exit ${rc}; needle '${needle}')"
103 FAILS=$((FAILS + 1))
104}
105
106run_suite() {
107 FAILS=0
108 local w="${work}/s$RANDOM"; mkdir -p "${w}"
109 # ---- gate 1
110 make_log "${w}/g1.log"
111 expect accept "gate1 good log" "" -- gate1 "${w}/g1.log" --expect-test-sha "${TEST_SHA}" --expect-loader-sha "${LOADER_SHA}"
112 expect reject "gate1 missing log" "missing or empty" -- gate1 "${w}/nope.log" --expect-test-sha "${TEST_SHA}"
113 : >"${w}/empty.log"
114 expect reject "gate1 empty log" "missing or empty" -- gate1 "${w}/empty.log" --expect-test-sha "${TEST_SHA}"
115 expect reject "gate1 wrong test digest" "digest mismatch" -- gate1 "${w}/g1.log" --expect-test-sha "$(hex 9)"
116 expect reject "gate1 wrong candidate loader" "wrong candidate loader" -- gate1 "${w}/g1.log" --expect-test-sha "${TEST_SHA}" --expect-loader-sha "$(hex 8)"
117 sed '/^RECOVERY_BOOT_GATE/d;/^== verdict/d' "${w}/g1.log" >"${w}/g1-cut.log"
118 expect reject "gate1 cut-off log (no verdict)" "cut-off log" -- gate1 "${w}/g1-cut.log" --expect-test-sha "${TEST_SHA}"
119 sed 's/^PASS test_artifact_round_trip:.*/FAIL test_artifact_round_trip: expected x/;s/^RECOVERY_BOOT_GATE: PASS/RECOVERY_BOOT_GATE: FAIL/' "${w}/g1.log" >"${w}/g1-fail.log"
120 expect reject "gate1 failing record" "failing checks" -- gate1 "${w}/g1-fail.log" --expect-test-sha "${TEST_SHA}"
121 { cat "${w}/g1.log"; echo "RECOVERY_BOOT_GATE: FAIL"; } >"${w}/g1-both.log"
122 expect reject "gate1 log with both PASS and FAIL verdicts" "contains RECOVERY_BOOT_GATE: FAIL" -- gate1 "${w}/g1-both.log" --expect-test-sha "${TEST_SHA}"
123 sed '/recovery_unlock_readonly/d;/test_artifact/d;/^PASS test_artifact/d' "${w}/g1.log" >"${w}/g1-part.log"
124 echo 'recovery_unlock: not authorized for this run (set AIENOS_UNLOCK_CIPHER, AIENOS_UNLOCK_SPARE, AIENOS_UNLOCK_IDENTITY)' >>"${w}/g1-part.log"
125 expect reject "gate1 partial (unlock not run)" "partial record" -- gate1 "${w}/g1-part.log" --expect-test-sha "${TEST_SHA}"
126 sed 's/^secure_boot_byte: 1/secure_boot_byte: 0/' "${w}/g1.log" >"${w}/g1-sb0.log"
127 expect reject "gate1 Secure Boot OFF" "Secure Boot was not recorded ON" -- gate1 "${w}/g1-sb0.log" --expect-test-sha "${TEST_SHA}"
128
129 # ---- gate 0 stability
130 make_capture "${w}/c1" b1 id-1 1 7; make_capture "${w}/c2" b2 id-2 1 7; make_capture "${w}/c3" b3 id-3 1 7
131 expect accept "gate0 three identical cold captures" "" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/c3" --expect-pcr7 "$(hex 107)"
132 expect reject "gate0 only two captures" "needs at least 3" -- gate0-stability "${w}/c1" "${w}/c2"
133 expect reject "gate0 missing folder" "partial capture" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/none"
134 make_capture "${w}/d3" b3 id-3 1 8
135 expect reject "gate0 PCR7 moved" "PCR7 changed" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/d3"
136 expect reject "gate0 wrong expected PCR7 (wrong candidate)" "wrong candidate state" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/c3" --expect-pcr7 "$(hex 999)"
137 make_capture "${w}/e3" b3 id-3 1 7 50
138 expect reject "gate0 PCR1 moved (warm-style)" "PCR1 changed" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/e3"
139 cp -r "${w}/c3" "${w}/t3"; echo "0 0x$(hex 1)" >>"${w}/t3/pcr_sha256.norm"
140 expect reject "gate0 tampered capture" "SHA256SUMS" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/t3"
141 cp -r "${w}/c3" "${w}/p3"; rm "${w}/p3/capture.env"
142 expect reject "gate0 partial capture (no capture.env)" "partial capture" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/p3"
143 make_capture "${w}/s3" b3 id-3 0 7
144 expect reject "gate0 Secure Boot OFF capture" "secure_boot=0" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/s3"
145 make_capture "${w}/s1" b1 id-1 0 7; make_capture "${w}/s2" b2 id-2 0 7
146 expect accept "gate0 OFF captures with --expect-secure-boot 0" "" -- gate0-stability "${w}/s1" "${w}/s2" "${w}/s3" --expect-secure-boot 0
147 make_capture "${w}/r3" b3 id-1 1 7
148 expect reject "gate0 same boot counted twice" "repeats an earlier folder" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/r3"
149 cp -r "${w}/c3" "${w}/m3"; sed -i 's/^eventlog_replay=match/eventlog_replay=mismatch/' "${w}/m3/capture.env"; resum "${w}/m3"
150 expect reject "gate0 event log replay mismatch" "not 'match'" -- gate0-stability "${w}/c1" "${w}/c2" "${w}/m3"
151
152 # ---- gate 2 receipt (written by the real campaign tool from fixtures)
153 make_capture "${w}/before" before id-b 1 7; make_capture "${w}/after" after id-a 1 8; make_capture "${w}/post" post id-p 1 7
154 : >"${w}/baseline.json"; echo '{"baseline":1}' >"${w}/baseline.json"
155 bash "${campaign}" receipt --variable timer --before "${w}/before" --after "${w}/after" --post-rollback "${w}/post" \
156 --baseline "${w}/baseline.json" --out "${w}/r.json" >/dev/null 2>&1 || { echo "MISS could not build gate2 fixture receipt"; FAILS=$((FAILS + 1)); }
157 local bs; bs="$(sha256sum "${w}/baseline.json" | cut -d' ' -f1)"
158 expect accept "gate2 good receipt with folders" "" -- gate2-receipt "${w}/r.json" --expect-baseline-sha "${bs}" --expect-variable timer \
159 --before "${w}/before" --after "${w}/after" --post-rollback "${w}/post"
160 expect reject "gate2 good receipt without capture folders" "capture folder not given" -- gate2-receipt "${w}/r.json" --expect-baseline-sha "${bs}" --expect-variable timer
161 expect reject "gate2 good receipt missing post-rollback folder" "post_rollback: capture folder not given" -- gate2-receipt "${w}/r.json" --expect-baseline-sha "${bs}" --expect-variable timer --before "${w}/before" --after "${w}/after"
162 expect reject "gate2 missing receipt" "missing or empty" -- gate2-receipt "${w}/none.json"
163 echo '{ not json' >"${w}/bad.json"
164 expect reject "gate2 garbage receipt" "not valid JSON" -- gate2-receipt "${w}/bad.json"
165 jq 'del(.rollback_restored)' "${w}/r.json" >"${w}/r-part.json"
166 expect reject "gate2 partial receipt (field removed)" "lacks field 'rollback_restored'" -- gate2-receipt "${w}/r-part.json"
167 jq '.result="STOP_UNEXPECTED_STATE" | .rollback_restored=false' "${w}/r.json" >"${w}/r-fail.json"
168 expect reject "gate2 failing receipt" "not RECORDED" -- gate2-receipt "${w}/r-fail.json"
169 expect reject "gate2 wrong baseline digest" "wrong chain" -- gate2-receipt "${w}/r.json" --expect-baseline-sha "$(hex 5)"
170 expect reject "gate2 wrong variable" "expected 'boot_order'" -- gate2-receipt "${w}/r.json" --expect-variable boot_order
171 expect reject "gate2 folder does not match receipt digest" "does not match folder digest" -- gate2-receipt "${w}/r.json" --before "${w}/after"
172 jq '.receipt_type="other"' "${w}/r.json" >"${w}/r-type.json"
173 expect reject "gate2 wrong receipt type" "receipt_type" -- gate2-receipt "${w}/r-type.json"
174
175 # ---- gate 3 (real ceremony tool, throwaway keys)
176 local c="${w}/cer"
177 printf 'throwaway-test-passphrase\n' >"${w}/pass"
178 export AIENOS_CEREMONY_PASS_FILE="${w}/pass"
179 mkdir -p "${w}/mA" "${w}/mB"
180 if bash "${ceremony}" generate "${c}" --allow-online >/dev/null 2>&1 \
181 && bash "${ceremony}" backup "${c}" "${w}/mA" backup-A >/dev/null 2>&1 \
182 && bash "${ceremony}" backup "${c}" "${w}/mB" backup-B >/dev/null 2>&1 \
183 && bash "${ceremony}" record "${c}" >/dev/null 2>&1; then
184 local pub="${c}/public" ms
185 ms="$(sha256sum "${pub}/authority_manifest.txt" | cut -d' ' -f1)"
186 # a record the operator has not signed yet is a partial record
187 expect reject "gate3 unsigned record (placeholder)" "placeholder" -- gate3 "${pub}" --expect-manifest-sha "${ms}"
188 cp -r "${pub}" "${w}/signed"
189 sed -i 's/^operator_signature=.*/operator_signature=fixture-operator-signature/' "${w}/signed/ceremony_record.txt"
190 expect accept "gate3 good signed record" "" -- gate3 "${w}/signed" --expect-manifest-sha "${ms}"
191 expect reject "gate3 wrong manifest digest (wrong candidate)" "wrong candidate" -- gate3 "${w}/signed" --expect-manifest-sha "$(hex 3)"
192 expect reject "gate3 missing folder" "missing" -- gate3 "${w}/nope" --expect-manifest-sha "${ms}"
193 cp -r "${w}/signed" "${w}/g3a"; sed -i 's/^backup_count=2/backup_count=1/;0,/^backup label=/{//d}' "${w}/g3a/ceremony_record.txt"
194 expect reject "gate3 only one backup" "below 2" -- gate3 "${w}/g3a" --expect-manifest-sha "${ms}"
195 cp -r "${w}/signed" "${w}/g3b"; sed -i '/^role\.release_signer\.pub_sha256=/d' "${w}/g3b/ceremony_record.txt"
196 expect reject "gate3 partial record (role fingerprint missing)" "role release_signer" -- gate3 "${w}/g3b" --expect-manifest-sha "${ms}"
197 cp -r "${w}/signed" "${w}/g3c"; sed -i 's/^generation=1$/generation=2/' "${w}/g3c/authority_manifest.txt"
198 expect reject "gate3 manifest altered after record" "does not match the manifest file" -- gate3 "${w}/g3c" --expect-manifest-sha "${ms}"
199 cp -r "${w}/signed" "${w}/g3d"; echo "-----BEGIN PRIVATE KEY-----" >>"${w}/g3d/ceremony_record.txt"
200 expect reject "gate3 private material in record" "private key material" -- gate3 "${w}/g3d" --expect-manifest-sha "${ms}"
201 cp -r "${w}/signed" "${w}/g3e"; sed -i 's/^firmware_changed=no/firmware_changed=yes/' "${w}/g3e/ceremony_record.txt"
202 expect reject "gate3 firmware changed" "firmware_changed=no" -- gate3 "${w}/g3e" --expect-manifest-sha "${ms}"
203 cp -r "${w}/signed" "${w}/g3f"; cp "${w}/g3c/authority_manifest.txt" "${w}/g3f/authority_manifest.txt"
204 sed -i "s/^manifest_sha256=.*/manifest_sha256=$(sha256sum "${w}/g3f/authority_manifest.txt" | cut -d' ' -f1)/" "${w}/g3f/ceremony_record.txt"
205 expect reject "gate3 re-pointed record, manifest signature fails" "verify failed" -- gate3 "${w}/g3f" --expect-manifest-sha "$(sha256sum "${w}/g3f/authority_manifest.txt" | cut -d' ' -f1)"
206 else
207 echo "MISS could not build gate3 throwaway ceremony"; FAILS=$((FAILS + 1))
208 fi
209 unset AIENOS_CEREMONY_PASS_FILE
210 # ---- usage
211 local rc; bash "${CHECKER}" bogus >/dev/null 2>&1; rc=$?
212 if [[ "${rc}" -eq 2 ]]; then echo "ok usage: unknown subcommand exits 2"; else echo "MISS usage exit ${rc}"; FAILS=$((FAILS + 1)); fi
213 return 0
214}
215
216# Negative control: a stub that accepts everything must be caught.
217stub="${work}/accept-all.sh"
218printf '#!/usr/bin/env bash\necho "TRUST1_ATTENDED_EVIDENCE stub: ACCEPT"\nexit 0\n' >"${stub}"
219echo "== RED run: stub checker that accepts everything"
220CHECKER="${stub}"; run_suite >"${work}/red.log"; red_fails="${FAILS}"
221grep -c '^MISS' "${work}/red.log" | sed 's/^/stub misses caught by the suite: /'
222if [[ "${red_fails}" -lt 10 ]]; then echo "FAIL the suite did not go red against an accept-all stub (${red_fails} misses)"; exit 1; fi
223echo "== GREEN run: real checker"
224CHECKER="${real}"; run_suite >"${work}/green.log"; grep -v "^ok" "${work}/green.log"
225if [[ "${FAILS}" -ne 0 ]]; then echo "TRUST1_ATTENDED_EVIDENCE_CHECK self-test (HOST VALIDATION ONLY): FAIL (${FAILS})"; exit 1; fi
226echo "checks passed: $(grep -c '^ok' "${work}/green.log")"
227echo "TRUST1_ATTENDED_EVIDENCE_CHECK self-test (HOST VALIDATION ONLY): ALL PASS"