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 / tpm_measurement_campaign.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 | # tpm_measurement_campaign.sh: TRUST-1 Gate 0 / Gate 2 measurement tool. |
| 18 | # |
| 19 | # Read-only on the machine. It never writes efivars, never changes boot |
| 20 | # entries, never touches TPM objects, never reads secrets. It only reads PCR |
| 21 | # banks, the firmware event log, Secure Boot variables (public certificate |
| 22 | # lists), boot entries and version strings, and writes them into OUT_DIR. |
| 23 | # |
| 24 | # Subcommands: |
| 25 | # capture OUT_DIR [LABEL] one snapshot of the measured boot state |
| 26 | # compare DIR_A DIR_B item-by-item diff of two snapshots |
| 27 | # exit 0 = identical, 3 = differences found |
| 28 | # receipt --variable NAME --before DIR --after DIR --post-rollback DIR |
| 29 | # [--prev RECEIPT] [--baseline RECEIPT] [--explain TEXT] --out FILE |
| 30 | # Gate 2 per-experiment receipt (JSON) |
| 31 | # OUT_DIR legacy form, same as `capture OUT_DIR` |
| 32 | # |
| 33 | # FLAG(sovereignty): requires tpm2-tools (outside dep), Linux host only; replace with in-house C tool. |
| 34 | # Requirements: tpm2-tools (tpm2_pcrread, tpm2_eventlog), read access to |
| 35 | # /dev/tpmrm0 and the event log (member of group tss), sha256sum. |
| 36 | # TPM2TOOLS_TCTI may point at a software TPM for host testing. |
| 37 | |
| 38 | set -euo pipefail |
| 39 | |
| 40 | die() { echo "Error: $*" >&2; exit 2; } |
| 41 | |
| 42 | sb_vars=(PK KEK db dbx) |
| 43 | efi_global=8be4df61-93ca-11d2-aa0d-00e098032b8c |
| 44 | efi_imgsec=d719b2cb-3d3a-4596-a3bc-dad00e67656f |
| 45 | efivars_dir="${AIENOS_EFIVARS_DIR:-/sys/firmware/efi/efivars}" |
| 46 | eventlog_path="${AIENOS_EVENTLOG:-/sys/kernel/security/tpm0/binary_bios_measurements}" |
| 47 | |
| 48 | var_guid() { |
| 49 | case "$1" in |
| 50 | PK|KEK|SecureBoot|SetupMode|BootOrder|BootCurrent) echo "${efi_global}" ;; |
| 51 | db|dbx) echo "${efi_imgsec}" ;; |
| 52 | *) die "unknown variable $1" ;; |
| 53 | esac |
| 54 | } |
| 55 | |
| 56 | # Normalize `tpm2_pcrread <bank>` or the `pcrs:` block of tpm2_eventlog into |
| 57 | # "INDEX HEX" lines, lowercase, sorted as text (the order `join` needs). |
| 58 | # Both tools print " 7 : 0x.." for one-digit and " 10: 0x.." (or "10 :") |
| 59 | # for two-digit indices. |
| 60 | normalize_pcrs() { |
| 61 | local bank="$1" |
| 62 | awk -v bank="${bank}" ' |
| 63 | $1 == bank":" { inb = 1; next } |
| 64 | inb && /^ *[a-z0-9]+:$/ { inb = 0; next } |
| 65 | inb && match($0, /^ *[0-9]+ *: *0x[0-9A-Fa-f]+/) { |
| 66 | line = $0; gsub(/[ :]+/, " ", line); sub(/^ /, "", line) |
| 67 | split(line, f, " "); printf "%d %s\n", f[1], tolower(f[2]) |
| 68 | } |
| 69 | ' | sort -k1,1 |
| 70 | } |
| 71 | |
| 72 | efivar_byte() { |
| 73 | # Last byte of a one-byte efivar (4 attribute bytes + value). |
| 74 | local f="${efivars_dir}/$1-$(var_guid "$1")" |
| 75 | [[ -r "${f}" ]] || { echo "unreadable"; return; } |
| 76 | od -An -tu1 -j4 -N1 "${f}" | tr -d ' ' |
| 77 | } |
| 78 | |
| 79 | cmd_capture() { |
| 80 | local out="${1:?capture needs OUT_DIR}" label="${2:-unlabelled}" |
| 81 | [[ "${label}" =~ ^[A-Za-z0-9._-]+$ ]] || die "label must match [A-Za-z0-9._-]+" |
| 82 | [[ ! -e "${out}" ]] || die "${out} already exists; captures are never overwritten" |
| 83 | # FLAG(sovereignty): tpm2_pcrread/tpm2_eventlog (outside dep), Linux host only; replace with in-house C PCR reader and event-log parser. |
| 84 | command -v tpm2_pcrread >/dev/null || die "tpm2_pcrread not installed" |
| 85 | command -v tpm2_eventlog >/dev/null || die "tpm2_eventlog not installed" |
| 86 | mkdir -p "${out}/efivars" |
| 87 | |
| 88 | # PCR banks. SHA-256 is mandatory; SHA-1/SHA-384 are recorded if present. |
| 89 | tpm2_pcrread sha256 >"${out}/pcr_sha256.txt" || die "tpm2_pcrread sha256 failed" |
| 90 | normalize_pcrs sha256 <"${out}/pcr_sha256.txt" >"${out}/pcr_sha256.norm" |
| 91 | [[ "$(wc -l <"${out}/pcr_sha256.norm")" -ge 8 ]] || die "fewer than 8 SHA-256 PCRs read" |
| 92 | local bank |
| 93 | for bank in sha1 sha384; do |
| 94 | if tpm2_pcrread "${bank}" >"${out}/pcr_${bank}.txt" 2>"${out}/pcr_${bank}.err"; then |
| 95 | normalize_pcrs "${bank}" <"${out}/pcr_${bank}.txt" >"${out}/pcr_${bank}.norm" |
| 96 | rm -f "${out}/pcr_${bank}.err" |
| 97 | [[ -s "${out}/pcr_${bank}.norm" ]] || rm -f "${out}/pcr_${bank}.norm" |
| 98 | fi |
| 99 | done |
| 100 | |
| 101 | # Event log: copy, digest, parse, and replay. Parser correctness check: |
| 102 | # every PCR the log replays must equal the live PCR value. |
| 103 | local ev_sha="absent" ev_size=0 replay_result="no_eventlog" |
| 104 | if [[ -r "${eventlog_path}" ]]; then |
| 105 | cat "${eventlog_path}" >"${out}/eventlog.bin" |
| 106 | ev_sha="$(sha256sum "${out}/eventlog.bin" | cut -d' ' -f1)" |
| 107 | ev_size="$(wc -c <"${out}/eventlog.bin")" |
| 108 | tpm2_eventlog "${out}/eventlog.bin" >"${out}/eventlog_parsed.yaml" \ |
| 109 | || die "tpm2_eventlog could not parse the event log" |
| 110 | sed -n '/^pcrs:/,$p' "${out}/eventlog_parsed.yaml" | normalize_pcrs sha256 \ |
| 111 | >"${out}/eventlog_replay_sha256.norm" |
| 112 | [[ -s "${out}/eventlog_replay_sha256.norm" ]] || die "event log replay produced no SHA-256 PCRs" |
| 113 | if join "${out}/eventlog_replay_sha256.norm" "${out}/pcr_sha256.norm" \ |
| 114 | | awk '{ print $1, ($2 == $3 ? "match" : "MISMATCH") }' >"${out}/eventlog_replay_check.txt" \ |
| 115 | && ! grep -q MISMATCH "${out}/eventlog_replay_check.txt"; then |
| 116 | replay_result="match" |
| 117 | else |
| 118 | replay_result="mismatch" |
| 119 | fi |
| 120 | fi |
| 121 | |
| 122 | # Secure Boot variables: public certificate/hash lists. The digest covers |
| 123 | # the efivarfs file bytes (4 attribute bytes + EFI_SIGNATURE_LIST data). |
| 124 | local v f sums="" |
| 125 | for v in "${sb_vars[@]}"; do |
| 126 | f="${efivars_dir}/${v}-$(var_guid "${v}")" |
| 127 | if [[ -r "${f}" ]]; then |
| 128 | cat "${f}" >"${out}/efivars/${v}.efivar" |
| 129 | sums+="${v}_efivar_sha256=$(sha256sum "${out}/efivars/${v}.efivar" | cut -d' ' -f1)"$'\n' |
| 130 | else |
| 131 | sums+="${v}_efivar_sha256=unreadable"$'\n' |
| 132 | fi |
| 133 | done |
| 134 | |
| 135 | efibootmgr -v >"${out}/boot_entries.txt" 2>&1 || echo "efibootmgr failed" >>"${out}/boot_entries.txt" |
| 136 | if command -v mokutil >/dev/null; then |
| 137 | mokutil --list-enrolled 2>/dev/null | grep -E 'SHA1 Fingerprint|Subject:' >"${out}/mok_enrolled.txt" || true |
| 138 | fi |
| 139 | |
| 140 | { |
| 141 | echo "capture_label=${label}" |
| 142 | echo "capture_utc=$(date -u +%Y-%m-%dT%H:%M:%SZ)" |
| 143 | echo "host=$(uname -n)" |
| 144 | echo "boot_id=$(cat /proc/sys/kernel/random/boot_id 2>/dev/null || echo unknown)" |
| 145 | echo "uptime_s=$(cut -d' ' -f1 /proc/uptime 2>/dev/null || echo unknown)" |
| 146 | echo "kernel_release=$(uname -r)" |
| 147 | echo "firmware_version=$(cat /sys/class/dmi/id/bios_version 2>/dev/null || echo unknown)" |
| 148 | echo "firmware_date=$(cat /sys/class/dmi/id/bios_date 2>/dev/null || echo unknown)" |
| 149 | echo "secure_boot=$(efivar_byte SecureBoot)" |
| 150 | echo "setup_mode=$(efivar_byte SetupMode)" |
| 151 | printf '%s' "${sums}" |
| 152 | echo "eventlog_sha256=${ev_sha}" |
| 153 | echo "eventlog_size=${ev_size}" |
| 154 | echo "eventlog_replay=${replay_result}" |
| 155 | echo "boot_current=$(awk '/^BootCurrent:/ {print $2}' "${out}/boot_entries.txt")" |
| 156 | echo "boot_order=$(awk '/^BootOrder:/ {print $2}' "${out}/boot_entries.txt")" |
| 157 | echo "boot_next=$(awk '/^BootNext:/ {print $2}' "${out}/boot_entries.txt")" |
| 158 | # FLAG(sovereignty): systemctl (systemd) queried on Linux host; remove, read fwupd state another way or drop the line. |
| 159 | echo "fwupd_refresh_timer=$(systemctl is-enabled fwupd-refresh.timer 2>/dev/null || echo unknown)" |
| 160 | echo "pcr_banks_with_values=$(cd "${out}" && ls pcr_*.norm | sed "s/pcr_//; s/.norm//" | paste -sd, -)" |
| 161 | # FLAG(sovereignty): tpm2_pcrread --version used only to record tool version; drop with tpm2-tools. |
| 162 | echo "tpm2_tools=$(tpm2_pcrread --version 2>/dev/null | sed -n 's/.*version="\([^"]*\)".*/\1/p')" |
| 163 | } >"${out}/capture.env" |
| 164 | |
| 165 | (cd "${out}" && find . -type f ! -name SHA256SUMS -print0 | sort -z | xargs -0 sha256sum >SHA256SUMS) |
| 166 | echo "capture_digest=$(sha256sum "${out}/SHA256SUMS" | cut -d' ' -f1)" |
| 167 | echo "eventlog_replay=${replay_result}" |
| 168 | echo "TPM measurement capture saved to ${out}" |
| 169 | [[ "${replay_result}" != "mismatch" ]] || { echo "STOP: event log replay does not match live PCRs" >&2; exit 4; } |
| 170 | } |
| 171 | |
| 172 | env_get() { sed -n "s/^$2=//p" "$1/capture.env"; } |
| 173 | |
| 174 | # Prints one line per compared item: "SAME item" or "CHANGED item old -> new". |
| 175 | diff_items() { |
| 176 | local a="$1" b="$2" k |
| 177 | join -a1 -a2 -e missing -o 0,1.2,2.2 "${a}/pcr_sha256.norm" "${b}/pcr_sha256.norm" \ |
| 178 | | awk '{ if ($2 == $3) print "SAME pcr_sha256_" $1; else print "CHANGED pcr_sha256_" $1, $2, "->", $3 }' |
| 179 | for k in PK_efivar_sha256 KEK_efivar_sha256 db_efivar_sha256 dbx_efivar_sha256 \ |
| 180 | secure_boot setup_mode eventlog_sha256 boot_order firmware_version kernel_release fwupd_refresh_timer; do |
| 181 | local va vb |
| 182 | va="$(env_get "${a}" "${k}")"; vb="$(env_get "${b}" "${k}")" |
| 183 | if [[ "${va}" == "${vb}" ]]; then echo "SAME ${k}"; else echo "CHANGED ${k} ${va:-empty} -> ${vb:-empty}"; fi |
| 184 | done |
| 185 | } |
| 186 | |
| 187 | check_capture() { |
| 188 | [[ -f "$1/capture.env" && -f "$1/pcr_sha256.norm" && -f "$1/SHA256SUMS" ]] || die "$1 is not a capture directory" |
| 189 | (cd "$1" && sha256sum --quiet -c SHA256SUMS) || die "$1 failed its own SHA256SUMS check" |
| 190 | local listed present |
| 191 | listed="$(sed "s/^[0-9a-f]* //" "$1/SHA256SUMS" | sort)" |
| 192 | present="$(cd "$1" && find . -type f ! -name SHA256SUMS | sort)" |
| 193 | [[ "${listed}" == "${present}" ]] || die "$1 has files added or removed since capture" |
| 194 | } |
| 195 | |
| 196 | cmd_compare() { |
| 197 | local a="${1:?compare needs DIR_A}" b="${2:?compare needs DIR_B}" |
| 198 | check_capture "${a}"; check_capture "${b}" |
| 199 | echo "A: $(env_get "${a}" capture_label) boot_id=$(env_get "${a}" boot_id)" |
| 200 | echo "B: $(env_get "${b}" capture_label) boot_id=$(env_get "${b}" boot_id)" |
| 201 | local lines |
| 202 | lines="$(diff_items "${a}" "${b}")" |
| 203 | grep '^CHANGED' <<<"${lines}" || true |
| 204 | local changed |
| 205 | changed="$(grep -c '^CHANGED' <<<"${lines}" || true)" |
| 206 | echo "compared=$(wc -l <<<"${lines}") changed=${changed}" |
| 207 | [[ "${changed}" -eq 0 ]] || exit 3 |
| 208 | } |
| 209 | |
| 210 | json_str() { printf '"%s"' "$(printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr -d '\n\r\t')"; } |
| 211 | |
| 212 | capture_json() { |
| 213 | printf '{"label":%s,"boot_id":%s,"capture_utc":%s,"capture_digest":%s}' \ |
| 214 | "$(json_str "$(env_get "$1" capture_label)")" "$(json_str "$(env_get "$1" boot_id)")" \ |
| 215 | "$(json_str "$(env_get "$1" capture_utc)")" \ |
| 216 | "$(json_str "$(sha256sum "$1/SHA256SUMS" | cut -d' ' -f1)")" |
| 217 | } |
| 218 | |
| 219 | changed_json() { |
| 220 | local first=1 line |
| 221 | printf '[' |
| 222 | while IFS= read -r line; do |
| 223 | [[ "${line}" == CHANGED* ]] || continue |
| 224 | [[ ${first} -eq 1 ]] || printf ',' |
| 225 | first=0 |
| 226 | json_str "${line#CHANGED }" |
| 227 | done <<<"$(diff_items "$1" "$2")" |
| 228 | printf ']' |
| 229 | } |
| 230 | |
| 231 | cmd_receipt() { |
| 232 | local variable="" before="" after="" post="" prev="" baseline="" explain="" out="" |
| 233 | while [[ $# -gt 0 ]]; do |
| 234 | case "$1" in |
| 235 | --variable) variable="$2"; shift 2 ;; |
| 236 | --before) before="$2"; shift 2 ;; |
| 237 | --after) after="$2"; shift 2 ;; |
| 238 | --post-rollback) post="$2"; shift 2 ;; |
| 239 | --prev) prev="$2"; shift 2 ;; |
| 240 | --baseline) baseline="$2"; shift 2 ;; |
| 241 | --explain) explain="$2"; shift 2 ;; |
| 242 | --out) out="$2"; shift 2 ;; |
| 243 | *) die "unknown receipt option $1" ;; |
| 244 | esac |
| 245 | done |
| 246 | [[ -n "${variable}" && -n "${before}" && -n "${after}" && -n "${post}" && -n "${out}" ]] \ |
| 247 | || die "receipt needs --variable --before --after --post-rollback --out" |
| 248 | [[ ! -e "${out}" ]] || die "${out} already exists; receipts are never overwritten" |
| 249 | check_capture "${before}"; check_capture "${after}"; check_capture "${post}" |
| 250 | local prev_sha="none" base_sha="none" |
| 251 | [[ -z "${prev}" ]] || prev_sha="$(sha256sum "${prev}" | cut -d' ' -f1)" |
| 252 | [[ -z "${baseline}" ]] || base_sha="$(sha256sum "${baseline}" | cut -d' ' -f1)" |
| 253 | |
| 254 | local restored=true result=RECORDED |
| 255 | local rollback_diff |
| 256 | rollback_diff="$(diff_items "${before}" "${post}")" |
| 257 | if grep -q '^CHANGED' <<<"${rollback_diff}"; then |
| 258 | restored=false |
| 259 | result=STOP_UNEXPECTED_STATE |
| 260 | fi |
| 261 | { |
| 262 | printf '{\n "schema_version": "1.0.0",\n "receipt_type": "trust1_gate2_measurement",\n' |
| 263 | printf ' "variable": %s,\n' "$(json_str "${variable}")" |
| 264 | printf ' "explanation": %s,\n' "$(json_str "${explain}")" |
| 265 | printf ' "before": %s,\n' "$(capture_json "${before}")" |
| 266 | printf ' "after": %s,\n' "$(capture_json "${after}")" |
| 267 | printf ' "post_rollback": %s,\n' "$(capture_json "${post}")" |
| 268 | printf ' "changed_by_mutation": %s,\n' "$(changed_json "${before}" "${after}")" |
| 269 | printf ' "not_restored_after_rollback": %s,\n' "$(changed_json "${before}" "${post}")" |
| 270 | printf ' "rollback_restored": %s,\n' "${restored}" |
| 271 | printf ' "previous_receipt_sha256": %s,\n' "$(json_str "${prev_sha}")" |
| 272 | printf ' "baseline_receipt_sha256": %s,\n' "$(json_str "${base_sha}")" |
| 273 | printf ' "result": %s\n}\n' "$(json_str "${result}")" |
| 274 | } >"${out}" |
| 275 | echo "receipt=${out} sha256=$(sha256sum "${out}" | cut -d' ' -f1) result=${result}" |
| 276 | [[ "${result}" == RECORDED ]] || exit 4 |
| 277 | } |
| 278 | |
| 279 | case "${1:-}" in |
| 280 | capture) shift; cmd_capture "$@" ;; |
| 281 | compare) shift; cmd_compare "$@" ;; |
| 282 | receipt) shift; cmd_receipt "$@" ;; |
| 283 | ""|-h|--help) sed -n '2,22p' "$0"; exit 0 ;; |
| 284 | *) cmd_capture "$1" "${2:-unlabelled}" ;; |
| 285 | esac |