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 / trust1_attended_evidence_check.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 | # trust1_attended_evidence_check.sh: HOST VALIDATION ONLY. |
| 18 | # |
| 19 | # Reads evidence that an attended TRUST-1 sitting left behind and says whether |
| 20 | # the evidence is well formed and bound to what the operator expects. It |
| 21 | # REJECTS missing, mismatched (wrong digest or candidate), partial and failing |
| 22 | # records. It does not and cannot prove the physical event happened: a file |
| 23 | # can be forged. ACCEPT means "this record is internally consistent and |
| 24 | # matches the digests you named", never "the boot or ceremony was done". |
| 25 | # Gate rows in scripts/trust1_m5_qualify.sh stay NOT_RUN (BLOCKED_OPERATOR / |
| 26 | # BLOCKED_HARDWARE) until the physical event is recorded by the operator; this |
| 27 | # tool is not wired into those rows. |
| 28 | # |
| 29 | # Read-only: no sudo, no TPM, no efivar or disk writes. Works on files only. |
| 30 | # |
| 31 | # Subcommands (each prints REJECT lines with the reason, then one verdict): |
| 32 | # gate1 COLLECTOR_LOG --expect-test-sha SHA [--expect-loader-sha SHA] |
| 33 | # Gate 1 attended recovery-stick boot: output of |
| 34 | # scripts/collect_recovery_boot_evidence.sh. Needs Secure Boot ON. |
| 35 | # gate0-stability DIR1 DIR2 DIR3 [DIR...] [--expect-secure-boot 0|1 (default 1)] |
| 36 | # [--expect-pcr7 HEX] [--expect-firmware VERSION] |
| 37 | # Gate 0 cold-boot PCR stability: three or more capture folders made by |
| 38 | # scripts/tpm_measurement_campaign.sh capture. The operator must give |
| 39 | # cold boots only (a warm restart legitimately moves PCR1); this tool |
| 40 | # cannot tell the difference. |
| 41 | # gate2-receipt RECEIPT.json --before DIR --after DIR --post-rollback DIR |
| 42 | # [--expect-baseline-sha SHA] [--expect-variable NAME] |
| 43 | # Gate 2 per-experiment receipt written by tpm_measurement_campaign.sh |
| 44 | # receipt. The three capture folders are required; their digests must match |
| 45 | # the receipt (a missing folder is a partial record and is rejected). |
| 46 | # gate3 PUBLIC_DIR --expect-manifest-sha SHA |
| 47 | # Gate 3 key ceremony public folder (ceremony_record.txt, manifest). |
| 48 | # Runs scripts/trust1_key_ceremony.sh verify when openssl is usable. |
| 49 | # |
| 50 | # Output: "HOST VALIDATION ONLY" banner, REJECT/NOTE lines, and |
| 51 | # TRUST1_ATTENDED_EVIDENCE <gate>: ACCEPT | REJECT (n reasons) |
| 52 | # Exit: 0 ACCEPT, 1 REJECT, 2 usage error. |
| 53 | set -uo pipefail |
| 54 | |
| 55 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 56 | REASONS=0 |
| 57 | GATE="?" |
| 58 | |
| 59 | banner() { echo "HOST VALIDATION ONLY: checks recorded files, does not prove the physical event happened"; } |
| 60 | usage() { sed -n '/^# Subcommands/,/^# Exit/p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' >&2; exit 2; } |
| 61 | rej() { echo "REJECT $*"; REASONS=$((REASONS + 1)); } |
| 62 | note() { echo "NOTE $*"; } |
| 63 | is_sha() { [[ "$1" =~ ^[0-9a-f]{64}$ ]]; } |
| 64 | verdict() { |
| 65 | if [[ "${REASONS}" -eq 0 ]]; then |
| 66 | echo "TRUST1_ATTENDED_EVIDENCE ${GATE}: ACCEPT (host validation only)" |
| 67 | exit 0 |
| 68 | fi |
| 69 | echo "TRUST1_ATTENDED_EVIDENCE ${GATE}: REJECT (${REASONS} reason(s))" |
| 70 | exit 1 |
| 71 | } |
| 72 | need_sha_arg() { # name value |
| 73 | [[ -n "$2" ]] || { echo "usage: $1 is required" >&2; exit 2; } |
| 74 | is_sha "$2" || { echo "usage: $1 must be 64 lowercase hex characters" >&2; exit 2; } |
| 75 | } |
| 76 | |
| 77 | # ------------------------------------------------------------------ gate 1 |
| 78 | cmd_gate1() { |
| 79 | GATE="gate1" |
| 80 | local log="${1:-}" test_sha="" loader_sha="" |
| 81 | [[ -n "${log}" ]] || usage |
| 82 | shift |
| 83 | while [[ $# -gt 0 ]]; do |
| 84 | case "$1" in |
| 85 | --expect-test-sha) test_sha="${2:-}"; shift 2 ;; |
| 86 | --expect-loader-sha) loader_sha="${2:-}"; shift 2 ;; |
| 87 | *) echo "usage: unknown option $1" >&2; exit 2 ;; |
| 88 | esac |
| 89 | done |
| 90 | need_sha_arg --expect-test-sha "${test_sha}" |
| 91 | [[ -z "${loader_sha}" ]] || need_sha_arg --expect-loader-sha "${loader_sha}" |
| 92 | banner |
| 93 | if [[ ! -s "${log}" ]]; then rej "collector log missing or empty: ${log}"; verdict; fi |
| 94 | |
| 95 | local pass_n fail_n |
| 96 | pass_n="$(grep -c '^RECOVERY_BOOT_GATE: PASS$' "${log}")" |
| 97 | fail_n="$(grep -c '^RECOVERY_BOOT_GATE: FAIL$' "${log}")" |
| 98 | [[ "${fail_n}" -eq 0 ]] || rej "log contains RECOVERY_BOOT_GATE: FAIL" |
| 99 | [[ "${pass_n}" -eq 1 ]] || rej "log has ${pass_n} 'RECOVERY_BOOT_GATE: PASS' lines (need exactly 1; a cut-off log has none)" |
| 100 | if grep -q '^FAIL ' "${log}"; then rej "log contains failing checks: $(grep '^FAIL ' "${log}" | head -n 3 | tr '\n' ';')"; fi |
| 101 | local c |
| 102 | for c in booted_from_removable_media secure_boot_state_recorded nvme_root_mounted pcrs_read \ |
| 103 | event_log_recorded esp_loaders_hashed recovery_unlock_readonly test_artifact_round_trip; do |
| 104 | grep -q "^PASS ${c}:" "${log}" || rej "required check '${c}' has no PASS line (missing or partial record)" |
| 105 | done |
| 106 | if grep -q '^recovery_unlock: not authorized' "${log}"; then rej "recovery unlock was not run (partial record)"; fi |
| 107 | grep -qx 'secure_boot_byte: 1' "${log}" || rej "Secure Boot was not recorded ON (need 'secure_boot_byte: 1'; Gate 1 requires Secure Boot ON)" |
| 108 | local got |
| 109 | got="$(sed -n 's/^test_artifact: .* sha256=\([0-9a-f]\{64\}\)$/\1/p' "${log}" | head -n 1)" |
| 110 | if [[ -z "${got}" ]]; then |
| 111 | rej "no 'test_artifact: ... sha256=' line (test file not read back)" |
| 112 | elif [[ "${got}" != "${test_sha}" ]]; then |
| 113 | rej "test file digest mismatch: log ${got}, expected ${test_sha}" |
| 114 | fi |
| 115 | if [[ -n "${loader_sha}" ]] && ! grep -q "^efi_image: .* sha256=${loader_sha}\$" "${log}"; then |
| 116 | rej "no efi_image line with expected loader digest ${loader_sha} (wrong candidate loader)" |
| 117 | fi |
| 118 | verdict |
| 119 | } |
| 120 | |
| 121 | # -------------------------------------------------------- gate 0 stability |
| 122 | env_get() { sed -n "s/^$2=//p" "$1/capture.env" | head -n 1; } |
| 123 | pcr_get() { awk -v n="$2" '$1 == n { print $2 }' "$1/pcr_sha256.norm"; } |
| 124 | |
| 125 | # Prints the reason to stdout; returns 0 if the capture folder is intact. |
| 126 | capture_intact() { |
| 127 | local d="$1" f listed present |
| 128 | for f in capture.env pcr_sha256.norm SHA256SUMS; do |
| 129 | [[ -f "${d}/${f}" ]] || { echo "${d}: missing ${f} (partial capture)"; return 1; } |
| 130 | done |
| 131 | if ! (cd "${d}" && sha256sum --quiet -c SHA256SUMS >/dev/null 2>&1); then |
| 132 | echo "${d}: files do not match its SHA256SUMS (tampered or damaged)"; return 1 |
| 133 | fi |
| 134 | listed="$(sed 's/^[0-9a-f]* //' "${d}/SHA256SUMS" | sort)" |
| 135 | present="$(cd "${d}" && find . -type f ! -name SHA256SUMS | sort)" |
| 136 | [[ "${listed}" == "${present}" ]] || { echo "${d}: files added or removed since capture"; return 1; } |
| 137 | [[ "$(wc -l <"${d}/pcr_sha256.norm")" -ge 8 ]] || { echo "${d}: fewer than 8 SHA-256 PCRs (partial capture)"; return 1; } |
| 138 | return 0 |
| 139 | } |
| 140 | |
| 141 | cmd_gate0_stability() { |
| 142 | GATE="gate0-stability" |
| 143 | local dirs=() sb=1 want7="" want_fw="" |
| 144 | while [[ $# -gt 0 ]]; do |
| 145 | case "$1" in |
| 146 | --expect-secure-boot) sb="${2:-}"; shift 2 ;; |
| 147 | --expect-pcr7) want7="${2:-}"; shift 2 ;; |
| 148 | --expect-firmware) want_fw="${2:-}"; shift 2 ;; |
| 149 | --*) echo "usage: unknown option $1" >&2; exit 2 ;; |
| 150 | *) dirs+=("$1"); shift ;; |
| 151 | esac |
| 152 | done |
| 153 | [[ "${sb}" == 0 || "${sb}" == 1 ]] || { echo "usage: --expect-secure-boot is 0 or 1" >&2; exit 2; } |
| 154 | [[ ${#dirs[@]} -ge 1 ]] || usage |
| 155 | banner |
| 156 | [[ ${#dirs[@]} -ge 3 ]] || rej "${#dirs[@]} capture folder(s) given; stability needs at least 3 boots" |
| 157 | local d msg bid ok=() ids="" |
| 158 | for d in "${dirs[@]}"; do |
| 159 | if msg="$(capture_intact "${d}")"; then |
| 160 | ok+=("${d}") |
| 161 | else |
| 162 | rej "${msg}"; continue |
| 163 | fi |
| 164 | [[ "$(env_get "${d}" eventlog_replay)" == "match" ]] || rej "${d}: event log replay is '$(env_get "${d}" eventlog_replay)', not 'match'" |
| 165 | [[ "$(env_get "${d}" secure_boot)" == "${sb}" ]] || rej "${d}: secure_boot=$(env_get "${d}" secure_boot), expected ${sb}" |
| 166 | [[ "$(env_get "${d}" setup_mode)" == "0" ]] || rej "${d}: setup_mode=$(env_get "${d}" setup_mode), expected 0" |
| 167 | bid="$(env_get "${d}" boot_id)" |
| 168 | if [[ -z "${bid}" || "${bid}" == unknown ]]; then rej "${d}: no usable boot_id" |
| 169 | elif grep -qx "${bid}" <<<"${ids}"; then rej "${d}: boot_id ${bid} repeats an earlier folder (same boot counted twice)" |
| 170 | else ids+="${bid}"$'\n'; fi |
| 171 | [[ -z "${want_fw}" || "$(env_get "${d}" firmware_version)" == "${want_fw}" ]] \ |
| 172 | || rej "${d}: firmware_version=$(env_get "${d}" firmware_version), expected ${want_fw}" |
| 173 | [[ -z "${want7}" || "$(pcr_get "${d}" 7)" == "0x${want7#0x}" ]] \ |
| 174 | || rej "${d}: PCR7 $(pcr_get "${d}" 7) differs from expected ${want7} (wrong candidate state)" |
| 175 | done |
| 176 | # Compare every intact folder with the first intact one. |
| 177 | if [[ ${#ok[@]} -ge 2 ]]; then |
| 178 | local ref="${ok[0]}" k va vb |
| 179 | for d in "${ok[@]:1}"; do |
| 180 | for k in 0 1 2 3 4 5 6 7; do |
| 181 | va="$(pcr_get "${ref}" "${k}")"; vb="$(pcr_get "${d}" "${k}")" |
| 182 | [[ "${va}" == "${vb}" ]] || rej "PCR${k} changed between ${ref} and ${d} (not stable; variation must be explained, not accepted)" |
| 183 | done |
| 184 | for k in PK_efivar_sha256 KEK_efivar_sha256 db_efivar_sha256 dbx_efivar_sha256 firmware_version; do |
| 185 | [[ "$(env_get "${ref}" "${k}")" == "$(env_get "${d}" "${k}")" ]] || rej "${k} changed between ${ref} and ${d}" |
| 186 | [[ "$(env_get "${d}" "${k}")" != "unreadable" ]] || rej "${d}: ${k} unreadable (partial capture)" |
| 187 | done |
| 188 | done |
| 189 | fi |
| 190 | note "cannot tell cold from warm restarts; the operator must supply cold boots only" |
| 191 | verdict |
| 192 | } |
| 193 | |
| 194 | # ------------------------------------------------------------- gate 2 receipt |
| 195 | cmd_gate2_receipt() { |
| 196 | GATE="gate2-receipt" |
| 197 | local rc="${1:-}" base="" var="" before="" after="" post="" |
| 198 | [[ -n "${rc}" ]] || usage |
| 199 | shift |
| 200 | while [[ $# -gt 0 ]]; do |
| 201 | case "$1" in |
| 202 | --expect-baseline-sha) base="${2:-}"; shift 2 ;; |
| 203 | --expect-variable) var="${2:-}"; shift 2 ;; |
| 204 | --before) before="${2:-}"; shift 2 ;; |
| 205 | --after) after="${2:-}"; shift 2 ;; |
| 206 | --post-rollback) post="${2:-}"; shift 2 ;; |
| 207 | *) echo "usage: unknown option $1" >&2; exit 2 ;; |
| 208 | esac |
| 209 | done |
| 210 | [[ -z "${base}" ]] || need_sha_arg --expect-baseline-sha "${base}" |
| 211 | command -v jq >/dev/null || { echo "usage: jq is required" >&2; exit 2; } |
| 212 | banner |
| 213 | if [[ ! -s "${rc}" ]]; then rej "receipt missing or empty: ${rc}"; verdict; fi |
| 214 | if ! jq -e . "${rc}" >/dev/null 2>&1; then rej "receipt is not valid JSON"; verdict; fi |
| 215 | local f |
| 216 | for f in receipt_type variable before after post_rollback changed_by_mutation not_restored_after_rollback \ |
| 217 | rollback_restored previous_receipt_sha256 baseline_receipt_sha256 result; do |
| 218 | jq -e "has(\"${f}\")" "${rc}" >/dev/null 2>&1 || rej "receipt lacks field '${f}' (partial record)" |
| 219 | done |
| 220 | [[ "$(jq -r '.receipt_type // ""' "${rc}")" == "trust1_gate2_measurement" ]] || rej "receipt_type is not trust1_gate2_measurement" |
| 221 | [[ "$(jq -r '.result // ""' "${rc}")" == "RECORDED" ]] || rej "result is '$(jq -r '.result // "absent"' "${rc}")', not RECORDED (failing record)" |
| 222 | [[ "$(jq -r '.rollback_restored // ""' "${rc}")" == "true" ]] || rej "rollback_restored is not true" |
| 223 | [[ "$(jq -r '(.not_restored_after_rollback // ["x"]) | length' "${rc}")" == "0" ]] || rej "items not restored after rollback" |
| 224 | [[ -z "${var}" || "$(jq -r '.variable // ""' "${rc}")" == "${var}" ]] || rej "variable is '$(jq -r '.variable // ""' "${rc}")', expected '${var}'" |
| 225 | local rb |
| 226 | rb="$(jq -r '.baseline_receipt_sha256 // ""' "${rc}")" |
| 227 | if [[ -n "${base}" && "${rb}" != "${base}" ]]; then rej "baseline_receipt_sha256 '${rb}' does not match expected ${base} (wrong chain)"; fi |
| 228 | local pair key dir want got msg |
| 229 | for pair in "before:${before}" "after:${after}" "post_rollback:${post}"; do |
| 230 | key="${pair%%:*}"; dir="${pair#*:}" |
| 231 | if [[ -z "${dir}" ]]; then rej "${key}: capture folder not given (--${key/_/-}); its digest cannot be checked (partial evidence)"; continue; fi |
| 232 | if [[ ! -f "${dir}/SHA256SUMS" ]]; then rej "${key}: folder ${dir} has no SHA256SUMS"; continue; fi |
| 233 | want="$(jq -r ".${key}.capture_digest // \"\"" "${rc}")" |
| 234 | got="$(sha256sum "${dir}/SHA256SUMS" | cut -d' ' -f1)" |
| 235 | [[ "${want}" == "${got}" ]] || rej "${key}: receipt digest '${want:-absent}' does not match folder digest ${got}" |
| 236 | if ! msg="$(capture_intact "${dir}")"; then rej "${msg}"; fi |
| 237 | done |
| 238 | verdict |
| 239 | } |
| 240 | |
| 241 | # ------------------------------------------------------------------ gate 3 |
| 242 | cmd_gate3() { |
| 243 | GATE="gate3" |
| 244 | local pub="${1:-}" want="" |
| 245 | [[ -n "${pub}" ]] || usage |
| 246 | shift |
| 247 | while [[ $# -gt 0 ]]; do |
| 248 | case "$1" in |
| 249 | --expect-manifest-sha) want="${2:-}"; shift 2 ;; |
| 250 | *) echo "usage: unknown option $1" >&2; exit 2 ;; |
| 251 | esac |
| 252 | done |
| 253 | need_sha_arg --expect-manifest-sha "${want}" |
| 254 | banner |
| 255 | local rec="${pub}/ceremony_record.txt" man="${pub}/authority_manifest.txt" |
| 256 | if [[ ! -f "${rec}" ]]; then rej "ceremony_record.txt missing in ${pub}"; verdict; fi |
| 257 | if [[ ! -f "${man}" ]]; then rej "authority_manifest.txt missing in ${pub}"; verdict; fi |
| 258 | grep -qx 'aienos_key_ceremony_record=1' "${rec}" || rej "record header 'aienos_key_ceremony_record=1' missing" |
| 259 | local rv actual r ra ma n sig out |
| 260 | rv="$(sed -n 's/^manifest_sha256=//p' "${rec}" | head -n 1)" |
| 261 | actual="$(sha256sum "${man}" | cut -d' ' -f1)" |
| 262 | [[ "${rv}" == "${actual}" ]] || rej "record manifest_sha256 '${rv:-absent}' does not match the manifest file ${actual} (altered after the record)" |
| 263 | [[ "${actual}" == "${want}" ]] || rej "manifest digest ${actual} does not match expected ${want} (wrong candidate)" |
| 264 | if grep -q 'PRIVATE KEY' "${rec}" "${man}"; then rej "private key material found in the public record"; fi |
| 265 | for r in owner_root boot_signer release_signer operator_approval; do |
| 266 | ra="$(sed -n "s/^role\\.${r}\\.pub_sha256=//p" "${rec}" | head -n 1)" |
| 267 | ma="$(sed -n "s/^role\\.${r}\\.pub_sha256=//p" "${man}" | head -n 1)" |
| 268 | if [[ -z "${ra}" ]]; then rej "record has no fingerprint line for role ${r} (partial record)" |
| 269 | elif [[ "${ra}" != "${ma}" ]]; then rej "role ${r} fingerprint in the record differs from the manifest"; fi |
| 270 | done |
| 271 | n="$(sed -n 's/^backup_count=//p' "${rec}" | head -n 1)" |
| 272 | if ! [[ "${n:-}" =~ ^[0-9]+$ ]] || [[ "${n}" -lt 2 ]]; then rej "backup_count '${n:-absent}' is below 2 (Gate 3 needs two verified backups)"; fi |
| 273 | [[ "$(grep -c '^backup ' "${rec}")" -ge 2 ]] || rej "fewer than two 'backup ' lines in the record" |
| 274 | grep -qx 'firmware_changed=no' "${rec}" || rej "'firmware_changed=no' missing (Gate 3 must not touch firmware)" |
| 275 | sig="$(sed -n 's/^operator_signature=//p' "${rec}" | head -n 1)" |
| 276 | if [[ -z "${sig}" || "${sig}" == "<operator signs here"* ]]; then rej "operator signature not filled in (still the placeholder)"; fi |
| 277 | if command -v openssl >/dev/null 2>&1 && [[ -f "${repo_root}/scripts/trust1_key_ceremony.sh" ]]; then |
| 278 | if ! out="$(bash "${repo_root}/scripts/trust1_key_ceremony.sh" verify "${pub}" 2>&1)"; then |
| 279 | rej "trust1_key_ceremony.sh verify failed: $(tail -n 2 <<<"${out}" | tr '\n' ' ')" |
| 280 | fi |
| 281 | else |
| 282 | note "openssl not available: manifest signature NOT re-verified" |
| 283 | fi |
| 284 | verdict |
| 285 | } |
| 286 | |
| 287 | sub="${1:-}" |
| 288 | [[ -n "${sub}" ]] || usage |
| 289 | shift |
| 290 | case "${sub}" in |
| 291 | gate1) cmd_gate1 "$@" ;; |
| 292 | gate0-stability) cmd_gate0_stability "$@" ;; |
| 293 | gate2-receipt) cmd_gate2_receipt "$@" ;; |
| 294 | gate3) cmd_gate3 "$@" ;; |
| 295 | -h|--help|help) usage ;; |
| 296 | *) echo "usage: unknown subcommand ${sub}" >&2; usage ;; |
| 297 | esac |