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

523 lines · 22857 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# trust1_gate5_policy_sim.sh: TRUST-1 Gate 5, TPM authorization policy
18# simulation on a software TPM (swtpm).
19#
20# bash scripts/trust1_gate5_policy_sim.sh selftest
21#
22# SIMULATION ONLY. swTPM is never Machine 1 qualification. Every key here is
23# a throwaway test key made in a temp dir and deleted on exit; no owner key
24# material is read or created, and no real TPM (/dev/tpm*) is touched.
25#
26# Policy spec (docs/TRUST-1-IMPLEMENTATION-PLAN.md, Gate 5)
27# --------------------------------------------------------
28# The sealed secret (stand-in for a storage unlock key) is bound to ONE
29# fixed policy that never names a PCR value:
30#
31# sealed = PolicyOR( ROOT_BRANCH , RECOVERY_BRANCH )
32# ROOT_BRANCH = PolicyAuthorize(Owner Root key, ref "deleg")
33# RECOVERY_BRANCH = PolicySigned(Operator Approval key, fresh TPM nonce,
34# ref "recovery")
35#
36# Owner Root signs one delegation per Release Signer:
37# deleg(S) = PolicyAuthorize(S, ref "set") ; PolicyNV(SIGNER_FLOOR <= epoch(S))
38# A Release Signer signs a release set {G1..Gk} (k <= 8):
39# set = PolicyOR( branch(G1) .. branch(Gk) ) (k = 1: branch(G1))
40# branch(G) = PolicyPCR(PCR 7, PCR 11 = expected values of release G)
41# ; PolicyNV(RELEASE_FLOOR <= G)
42#
43# PCR 7 carries "Secure Boot active + owner Boot Signer chain executed" and
44# PCR 11 the release manifest digest. These two PCRs are placeholders: Gate 2
45# decides the real selection. The statement enforced is "Secure Boot active,
46# owner-approved chain executed, manifest in an approved release set"; the
47# expected PCR values live only in signed, replaceable release sets, never as
48# a frozen value inside the sealed object.
49#
50# RELEASE_FLOOR and SIGNER_FLOOR are TPM NV counters (monotonic: they can only
51# go up). Raising them needs owner (operator) authorization.
52#
53# Anti-rollback generation semantics (Q29):
54# approved generation G >= RELEASE_FLOOR and G in a set signed by a
55# signer whose epoch >= SIGNER_FLOOR: unseals normally.
56# emergency-only generation G < RELEASE_FLOOR (retired): the normal path is
57# refused by the TPM; only the recovery branch, which needs a
58# fresh Operator Approval signature over the TPM nonce, can
59# unseal.
60# revoked signer epoch < SIGNER_FLOOR, or G in no signed set: normal
61# path refused; recovery branch still operator-only.
62# Release transition: approve {N} -> approve {N, N+1} (both boot) -> N+1
63# healthy -> operator raises RELEASE_FLOOR to N+1 (N retired).
64# Signer rotation: add (root signs deleg for the new signer) -> verify ->
65# exercise recovery -> revoke (operator raises SIGNER_FLOOR). Never
66# delete-first.
67#
68# Every refusal below comes from the TPM (a policy command or the unseal
69# fails); the shell only compares outcomes.
70#
71# Exit: 0 PASS, 1 FAIL, 2 missing tools.
72
73set -euo pipefail
74
75usage() {
76 echo "usage: $0 selftest" >&2
77 exit 2
78}
79[[ $# -eq 1 && "$1" == "selftest" ]] || usage
80
81# FLAG(sovereignty): whole script drives tpm2-tools (tpm2_* list below and every tpm2_ call to end of file, outside dep), host-only software-TPM sim; replace with in-house C TPM policy simulator.
82for t in swtpm swtpm_ioctl tpm2_startup tpm2_pcrextend tpm2_createprimary \
83 tpm2_create tpm2_load tpm2_unseal tpm2_startauthsession tpm2_policypcr \
84 tpm2_policynv tpm2_policyor tpm2_policyauthorize tpm2_policysigned \
85 tpm2_verifysignature tpm2_loadexternal tpm2_nvdefine tpm2_nvincrement \
86 tpm2_nvread tpm2_nvwrite tpm2_flushcontext tpm2_changeauth tpm2_dictionarylockout openssl xxd cmp; do
87 command -v "${t}" >/dev/null || {
88 echo "Error: ${t} not installed (need swtpm, tpm2-tools, openssl, xxd)" >&2
89 echo "TRUST1_GATE5_POLICY_SIM: FAIL"
90 exit 2
91 }
92done
93
94echo "NOTE: swTPM SIMULATION. This is never Machine 1 qualification."
95echo "NOTE: throwaway test keys only; no real TPM is touched."
96
97work="$(mktemp -d)"
98SWTPM_PID=""
99cleanup() {
100 if [[ -n "${SWTPM_PID}" ]]; then
101 kill "${SWTPM_PID}" 2>/dev/null || true
102 wait "${SWTPM_PID}" 2>/dev/null || true
103 fi
104 rm -rf "${work}"
105}
106trap cleanup EXIT
107trap 'exit 1' INT TERM HUP
108
109fails=0
110passes=0
111pass() { echo "PASS $*"; passes=$((passes + 1)); }
112fail() { echo "FAIL $*"; fails=$((fails + 1)); }
113die() {
114 echo "FAIL setup: $*"
115 [[ -f "${log:-}" ]] && tail -n 15 "${log}" >&2
116 echo "TRUST1_GATE5_POLICY_SIM: FAIL"
117 exit 1
118}
119
120# ---- swtpm (same pattern as scripts/test_trust1_measurement_tools.sh) ----
121port=""
122for _ in 1 2 3 4 5; do
123 port=$((20000 + RANDOM % 20000))
124 mkdir -p "${work}/tpm"
125 swtpm socket --tpm2 --tpmstate dir="${work}/tpm" \
126 --server type=tcp,port="${port}",bindaddr=127.0.0.1 \
127 --ctrl type=tcp,port=$((port + 1)),bindaddr=127.0.0.1 \
128 --flags not-need-init,startup-clear >"${work}/swtpm.log" 2>&1 &
129 SWTPM_PID=$!
130 export TPM2TOOLS_TCTI="swtpm:host=127.0.0.1,port=${port}"
131 for _ in $(seq 1 50); do tpm2_pcrread sha256:0 >/dev/null 2>&1 && break; sleep 0.1; done
132 tpm2_pcrread sha256:0 >/dev/null 2>&1 && break
133 kill "${SWTPM_PID}" 2>/dev/null || true
134 wait "${SWTPM_PID}" 2>/dev/null || true
135 SWTPM_PID=""
136done
137[[ -n "${SWTPM_PID}" ]] || die "swtpm did not start"
138ctrl="127.0.0.1:$((port + 1))"
139log="${work}/tpm2.log"
140q() { "$@" >>"${log}" 2>&1; }
141
142OWNER_AUTH="throwaway-owner-$RANDOM$RANDOM"
143RELEASE_FLOOR=0x01500010
144SIGNER_FLOOR=0x01500011
145PCRSEL="sha256:7,11"
146
147k="${work}/keys"; d="${work}/pol"; mkdir -p "${k}" "${d}"
148printf 'set' >"${d}/ref_set"
149printf 'deleg' >"${d}/ref_deleg"
150printf 'recovery' >"${d}/ref_recovery"
151
152flush_all() {
153 tpm2_flushcontext -t >/dev/null 2>&1 || true
154 tpm2_flushcontext -s >/dev/null 2>&1 || true
155 tpm2_flushcontext -l >/dev/null 2>&1 || true
156}
157
158# ---- throwaway keys ----
159newkey() {
160 openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 \
161 -out "${k}/$1.key" 2>/dev/null || die "genpkey $1"
162 openssl pkey -in "${k}/$1.key" -pubout -out "${k}/$1.pub.pem" 2>/dev/null
163 tpm2_loadexternal -C o -G rsa -u "${k}/$1.pub.pem" -c "${k}/$1.ctx" \
164 -n "${k}/$1.name" >>"${log}" 2>&1 || die "loadexternal $1"
165 flush_all
166}
167for key in root relA relB operator attacker opattacker; do newkey "${key}"; done
168# Throwaway Boot Signer stand-in: only its public fingerprint is measured.
169boot_fp="$(openssl pkey -in "${k}/root.pub.pem" -pubin -outform DER 2>/dev/null |
170 openssl dgst -sha256 -r | cut -c1-64)"
171foreign_fp="$(printf 'foreign-boot-signer' | openssl dgst -sha256 -r | cut -c1-64)"
172
173sig() { openssl dgst -sha256 -sign "${k}/$1.key" -out "$3" "$2"; }
174
175# ---- NV counters (monotonic floors), owner-write, open read ----
176# Dictionary-attack lockout would trip on the many deliberate refusals in
177# this suite; raise the threshold for the simulation only.
178q tpm2_dictionarylockout -s -n 1000 -t 1 -l 1 || die "DA parameters"
179q tpm2_changeauth -c o "${OWNER_AUTH}" || die "set owner auth"
180for idx in "${RELEASE_FLOOR}" "${SIGNER_FLOOR}"; do
181 q tpm2_nvdefine "${idx}" -C o -P "${OWNER_AUTH}" -s 8 \
182 -a "nt=counter|ownerwrite|authread|no_da" || die "nvdefine ${idx}"
183 q tpm2_nvincrement -C o -P "${OWNER_AUTH}" "${idx}" || die "nvincrement ${idx}"
184done
185nvval() { tpm2_nvread -C "$1" "$1" 2>>"${log}" | xxd -p | tr -d '\n'; }
186N=$((16#$(nvval "${RELEASE_FLOOR}")))
187EA=$((16#$(nvval "${SIGNER_FLOOR}")))
188EB=$((EA + 1))
189N1=$((N + 1))
190be64() { printf '%016x' "$1" | xxd -r -p; }
191echo "INFO release floor N=${N}, signer A epoch=${EA}, signer B epoch=${EB}"
192
193# ---- expected PCR values from measurement lists ----
194sha_hex() { openssl dgst -sha256 -r | cut -c1-64; }
195pcr_chain() { # measurements (hex) -> final PCR hex
196 local v
197 v="$(printf '%064d' 0)"
198 for m in "$@"; do v="$(printf '%s%s' "${v}" "${m}" | xxd -r -p | sha_hex)"; done
199 echo "${v}"
200}
201sb_meas() { printf 'SecureBoot=%s' "$1" | sha_hex; }
202manifest() { printf 'aienos-release-manifest-v1 generation=%s kernel=test-%s' "$1" "$1"; }
203mf_meas() { manifest "$1" | sha_hex; }
204# boot profile: sb(on|off) chain(owner|foreign) generation
205expected_pcrs() { # -> file with PCR7||PCR11
206 local fp="${boot_fp}"
207 [[ "$2" == owner ]] || fp="${foreign_fp}"
208 { pcr_chain "$(sb_meas "$1")" "${fp}"; pcr_chain "$(mf_meas "$3")"; } |
209 tr -d '\n' | xxd -r -p >"$4"
210}
211
212# Power-cycle the swTPM (PCRs reset, NV kept) and replay one boot's
213# measurements, like firmware + owner loader would.
214boot() { # sb chain generation
215 flush_all
216 q swtpm_ioctl --tcp "${ctrl}" -i || die "swtpm power cycle"
217 tpm2_startup -c >>"${log}" 2>&1 || true
218 local fp="${boot_fp}"
219 [[ "$2" == owner ]] || fp="${foreign_fp}"
220 q tpm2_pcrextend "7:sha256=$(sb_meas "$1")" || die "extend"
221 q tpm2_pcrextend "7:sha256=${fp}" || die "extend"
222 q tpm2_pcrextend "11:sha256=$(mf_meas "$3")" || die "extend"
223}
224
225# ---- trial-session policy building ----
226trial() { q tpm2_startauthsession -S "${d}/trial.ctx"; }
227trial_end() {
228 tpm2_flushcontext "${d}/trial.ctx" >/dev/null 2>&1 || true
229}
230branch_digest() { # gen out
231 expected_pcrs on owner "$1" "${d}/pcrs_$1.bin"
232 trial
233 q tpm2_policypcr -S "${d}/trial.ctx" -l "${PCRSEL}" -f "${d}/pcrs_$1.bin" || die "trial pcr"
234 be64 "$1" | q tpm2_policynv -S "${d}/trial.ctx" -i- "${RELEASE_FLOOR}" ule \
235 -L "$2" || die "trial nv"
236 trial_end
237}
238or_list() { # files... -> sha256:f1,f2
239 local IFS=,
240 echo "sha256:$*"
241}
242# make_set NAME SIGNER gen...: signed release set
243make_set() {
244 local name="$1" signer="$2"; shift 2
245 local files=() g
246 for g in "$@"; do
247 branch_digest "${g}" "${d}/br_${name}_${g}.dig"
248 files+=("${d}/br_${name}_${g}.dig")
249 done
250 if [[ $# -eq 1 ]]; then
251 cp "${files[0]}" "${d}/set_${name}.dig"
252 else
253 trial
254 q tpm2_policyor -S "${d}/trial.ctx" -l "$(or_list "${files[@]}")" \
255 -L "${d}/set_${name}.dig" || die "trial or"
256 trial_end
257 fi
258 printf '%s\n' "${files[@]}" >"${d}/set_${name}.branches"
259 echo "${signer}" >"${d}/set_${name}.signer"
260 cat "${d}/set_${name}.dig" "${d}/ref_set" >"${d}/set_${name}.msg"
261 sig "${signer}" "${d}/set_${name}.msg" "${d}/set_${name}.sig"
262}
263# make_deleg NAME SIGNER EPOCH: Owner Root delegation to a Release Signer
264make_deleg() {
265 trial
266 q tpm2_policyauthorize -S "${d}/trial.ctx" -n "${k}/$2.name" -q "${d}/ref_set" \
267 -i /dev/null || die "trial authorize"
268 be64 "$3" | q tpm2_policynv -S "${d}/trial.ctx" -i- "${SIGNER_FLOOR}" ule \
269 -L "${d}/deleg_$1.dig" || die "trial deleg nv"
270 trial_end
271 echo "$2" >"${d}/deleg_$1.signer"
272 echo "$3" >"${d}/deleg_$1.epoch"
273 cat "${d}/deleg_$1.dig" "${d}/ref_deleg" >"${d}/deleg_$1.msg"
274 sig root "${d}/deleg_$1.msg" "${d}/deleg_$1.sig"
275}
276
277flush_all
278trial
279q tpm2_policyauthorize -S "${d}/trial.ctx" -n "${k}/root.name" -q "${d}/ref_deleg" \
280 -i /dev/null -L "${d}/root_branch.dig" || die "trial root branch"
281trial_end
282trial
283q tpm2_loadexternal -C o -G rsa -u "${k}/operator.pub.pem" -c "${k}/operator.ctx" || die "load op"
284q tpm2_policysigned -S "${d}/trial.ctx" -g sha256 -c "${k}/operator.ctx" -q "${d}/ref_recovery" \
285 -L "${d}/recovery_branch.dig" || die "trial recovery branch"
286trial_end
287flush_all
288trial
289q tpm2_policyor -S "${d}/trial.ctx" \
290 -l "$(or_list "${d}/root_branch.dig" "${d}/recovery_branch.dig")" \
291 -L "${d}/sealed.dig" || die "trial sealed policy"
292trial_end
293flush_all
294
295# ---- seal the throwaway secret ----
296head -c 32 /dev/urandom >"${work}/secret.bin"
297q tpm2_createprimary -C o -P "${OWNER_AUTH}" -c "${d}/prim.ctx" || die "createprimary"
298seal_obj() { # policy-file out-prefix
299 tpm2_flushcontext -t >/dev/null 2>&1 || true
300 q tpm2_create -C "${d}/prim.ctx" -L "$1" -i "${work}/secret.bin" \
301 -u "$2.pub" -r "$2.priv" -a "fixedtpm|fixedparent" || die "seal"
302 tpm2_flushcontext -t >/dev/null 2>&1 || true
303}
304seal_obj "${d}/sealed.dig" "${d}/sealed"
305# Negative-control object: same secret, sealed to a one-byte-mutated policy.
306xxd -p "${d}/sealed.dig" | tr -d '\n' | sed 's/^\(..\)/ff/' | xxd -r -p >"${d}/mutated.dig"
307cmp -s "${d}/sealed.dig" "${d}/mutated.dig" && die "mutation did not change policy"
308seal_obj "${d}/mutated.dig" "${d}/mutated"
309flush_all
310
311# ---- runtime: one unseal attempt; returns 0 only if TPM releases secret ----
312out="${work}/out.bin"
313S="${work}/sess.ctx"
314load_and_unseal() { # object-prefix
315 q tpm2_createprimary -C o -P "${OWNER_AUTH}" -c "${work}/p.ctx" || return 1
316 q tpm2_load -C "${work}/p.ctx" -u "$1.pub" -r "$1.priv" -c "${work}/o.ctx" || return 1
317 tpm2_flushcontext -t >/dev/null 2>&1 || true
318 rm -f "${out}"
319 tpm2_unseal -c "${work}/o.ctx" -p "session:${S}" -o "${out}" >>"${log}" 2>&1 || return 1
320 cmp -s "${out}" "${work}/secret.bin"
321}
322no_policy_unseal() { # plain password auth, no policy session
323 flush_all
324 q tpm2_createprimary -C o -P "${OWNER_AUTH}" -c "${work}/p.ctx" || return 1
325 q tpm2_load -C "${work}/p.ctx" -u "${d}/sealed.pub" -r "${d}/sealed.priv" -c "${work}/o.ctx" || return 1
326 tpm2_flushcontext -t >/dev/null 2>&1 || true
327 rm -f "${out}"
328 tpm2_unseal -c "${work}/o.ctx" -o "${out}" >>"${log}" 2>&1 || return 1
329 cmp -s "${out}" "${work}/secret.bin"
330}
331# normal_path SET GEN DELEG [object]
332normal_path() {
333 local set="$1" gen="$2" deleg="$3" obj="${4:-${d}/sealed}"
334 local signer dsigner depoch
335 signer="$(cat "${d}/set_${set}.signer")"
336 dsigner="$(cat "${d}/deleg_${deleg}.signer")"
337 depoch="$(cat "${d}/deleg_${deleg}.epoch")"
338 flush_all
339 q tpm2_startauthsession --policy-session -S "${S}" || return 1
340 q tpm2_policypcr -S "${S}" -l "${PCRSEL}" || return 1
341 be64 "${gen}" | q tpm2_policynv -S "${S}" -i- "${RELEASE_FLOOR}" ule || return 1
342 if [[ $(wc -l <"${d}/set_${set}.branches") -gt 1 ]]; then
343 q tpm2_policyor -S "${S}" -l "$(or_list $(cat "${d}/set_${set}.branches"))" || return 1
344 fi
345 q tpm2_loadexternal -C o -G rsa -u "${k}/${signer}.pub.pem" -c "${work}/v.ctx" || return 1
346 q tpm2_verifysignature -c "${work}/v.ctx" -g sha256 -m "${d}/set_${set}.msg" \
347 -s "${d}/set_${set}.sig" -f rsassa -t "${work}/t1.tk" || return 1
348 tpm2_flushcontext -t >/dev/null 2>&1 || true
349 q tpm2_policyauthorize -S "${S}" -i "${d}/set_${set}.dig" -q "${d}/ref_set" \
350 -n "${k}/${dsigner}.name" -t "${work}/t1.tk" || return 1
351 be64 "${depoch}" | q tpm2_policynv -S "${S}" -i- "${SIGNER_FLOOR}" ule || return 1
352 q tpm2_loadexternal -C o -G rsa -u "${k}/root.pub.pem" -c "${work}/v.ctx" || return 1
353 q tpm2_verifysignature -c "${work}/v.ctx" -g sha256 -m "${d}/deleg_${deleg}.msg" \
354 -s "${d}/deleg_${deleg}.sig" -f rsassa -t "${work}/t2.tk" || return 1
355 tpm2_flushcontext -t >/dev/null 2>&1 || true
356 q tpm2_policyauthorize -S "${S}" -i "${d}/deleg_${deleg}.dig" -q "${d}/ref_deleg" \
357 -n "${k}/root.name" -t "${work}/t2.tk" || return 1
358 q tpm2_policyor -S "${S}" -l "$(or_list "${d}/root_branch.dig" "${d}/recovery_branch.dig")" || return 1
359 load_and_unseal "${obj}"
360}
361# recovery_path KEY [replay-from-file] [object]: fresh operator signature over nonceTPM
362recovery_path() {
363 local key="$1" replay="${2:-}" obj="${3:-${d}/sealed}"
364 flush_all
365 q tpm2_startauthsession --policy-session -S "${S}" || return 1
366 q tpm2_loadexternal -C o -G rsa -u "${k}/${key}.pub.pem" -c "${work}/op.ctx" || return 1
367 q tpm2_policysigned -S "${S}" -g sha256 -c "${work}/op.ctx" -q "${d}/ref_recovery" -x \
368 --raw-data "${work}/tosign.bin" || return 1
369 if [[ -n "${replay}" ]]; then
370 cp "${replay}" "${work}/op.sig"
371 else
372 sig "${key}" "${work}/tosign.bin" "${work}/op.sig" || return 1
373 cp "${work}/op.sig" "${work}/last_op.sig"
374 fi
375 q tpm2_policysigned -S "${S}" -g sha256 -c "${work}/op.ctx" -q "${d}/ref_recovery" -x \
376 -s "${work}/op.sig" -f rsassa || return 1
377 tpm2_flushcontext -t >/dev/null 2>&1 || true
378 q tpm2_policyor -S "${S}" -l "$(or_list "${d}/root_branch.dig" "${d}/recovery_branch.dig")" || return 1
379 load_and_unseal "${obj}"
380}
381
382allow() { # label cmd...
383 local label="$1"; shift
384 if "$@"; then pass "${label}: unsealed, secret byte-exact"; else
385 fail "${label}: expected unseal, TPM refused"
386 grep "ERROR" "${log}" | tail -n 2 >&2 || true
387 fi
388}
389refuse() {
390 local label="$1"; shift
391 local mark where
392 mark=$(wc -l <"${log}")
393 if "$@"; then
394 fail "${label}: expected TPM refusal, secret was released"
395 else
396 where="$(tail -n +"$((mark + 1))" "${log}" | grep -o 'Unable to run tpm2_[a-z]*' | tail -n 1 | sed 's/Unable to run //')"
397 [[ -n "${where}" ]] && pass "${label}: refused by TPM at ${where}" ||
398 pass "${label}: refused (secret mismatch)"
399 fi
400}
401
402# ---- signed artefacts (all throwaway) ----
403make_deleg A relA "${EA}"
404make_set N relA "${N}"
405make_set NN1 relA "${N}" "${N1}"
406flush_all
407
408echo "== current"
409boot on owner "${N}"
410allow "current: approved {N}, boot N" normal_path N "${N}" A
411
412echo "== next (transition N -> N+1)"
413boot on owner "${N1}"
414refuse "next: boot N+1 under old set {N}" normal_path N "${N1}" A
415allow "next: approved {N,N+1}, boot N+1" normal_path NN1 "${N1}" A
416boot on owner "${N}"
417allow "next: approved {N,N+1}, boot N still accepted" normal_path NN1 "${N}" A
418
419echo "== invalid"
420boot on owner "$((N1 + 5))"
421refuse "invalid: unseal without any policy session" no_policy_unseal
422refuse "invalid: manifest not in approved set" normal_path NN1 "$((N1 + 5))" A
423boot off owner "${N}"
424refuse "invalid: Secure Boot off" normal_path NN1 "${N}" A
425boot on foreign "${N}"
426refuse "invalid: non-owner boot chain" normal_path NN1 "${N}" A
427boot on owner "${N}"
428# Set signed by a key Owner Root never delegated to.
429make_set ATK attacker "${N}" "${N1}"
430# The attacker presents Owner Root's real delegation for signer A, but signs
431# the release set with its own key (Owner Root never delegated to it).
432echo attacker >"${d}/deleg_ATK.signer"; echo "${EA}" >"${d}/deleg_ATK.epoch"
433cp "${d}/deleg_A.msg" "${d}/deleg_ATK.msg"; cp "${d}/deleg_A.sig" "${d}/deleg_ATK.sig"
434cp "${d}/deleg_A.dig" "${d}/deleg_ATK.dig"
435flush_all
436refuse "invalid: release set signed by undelegated key" normal_path ATK "${N}" ATK
437cp "${d}/set_NN1.sig" "${d}/sig.keep"
438printf '\x01' | dd of="${d}/set_NN1.sig" bs=1 seek=17 conv=notrunc status=none
439refuse "invalid: tampered release-set signature" normal_path NN1 "${N}" A
440cp "${d}/sig.keep" "${d}/set_NN1.sig"
441allow "invalid: control, restored signature accepted again" normal_path NN1 "${N}" A
442
443echo "== retire N (N+1 healthy)"
444if tpm2_nvincrement "${RELEASE_FLOOR}" -C "${RELEASE_FLOOR}" >>"${log}" 2>&1 ||
445 tpm2_nvincrement -C o -P wrong-owner-auth "${RELEASE_FLOOR}" >>"${log}" 2>&1; then
446 fail "retire: floor raised without operator authorization"
447else
448 pass "retire: raising the floor without operator authorization refused by TPM"
449fi
450q tpm2_nvincrement -C o -P "${OWNER_AUTH}" "${RELEASE_FLOOR}" || die "retire increment"
451[[ $((16#$(nvval "${RELEASE_FLOOR}"))) -eq ${N1} ]] && pass "retire: release floor now N+1" ||
452 fail "retire: release floor not N+1"
453boot on owner "${N1}"
454allow "retire: boot N+1 after retirement" normal_path NN1 "${N1}" A
455
456echo "== rollback"
457boot on owner "${N}"
458refuse "rollback: boot retired N under {N,N+1}" normal_path NN1 "${N}" A
459refuse "rollback: boot retired N under old signed {N}" normal_path N "${N}" A
460if be64 "${N}" | tpm2_nvwrite -C o -P "${OWNER_AUTH}" -i- "${RELEASE_FLOOR}" >>"${log}" 2>&1; then
461 fail "rollback: floor counter was overwritten"
462else
463 pass "rollback: floor counter cannot be written back (monotonic)"
464fi
465
466echo "== rotation (signer A -> B: add, verify, exercise recovery, revoke)"
467make_deleg B relB "${EB}"
468make_set NN1B relB "${N}" "${N1}"
469flush_all
470boot on owner "${N1}"
471allow "rotation: add, new signer B accepted" normal_path NN1B "${N1}" B
472allow "rotation: overlap, old signer A still accepted" normal_path NN1 "${N1}" A
473refuse "rotation: B set with A delegation refused" normal_path NN1B "${N1}" A
474allow "rotation: recovery exercised before revoke" recovery_path operator
475if tpm2_nvincrement -C o -P wrong-owner-auth "${SIGNER_FLOOR}" >>"${log}" 2>&1 ||
476 tpm2_nvincrement "${SIGNER_FLOOR}" -C "${SIGNER_FLOOR}" >>"${log}" 2>&1; then
477 fail "rotation: signer floor raised without operator authorization"
478else
479 pass "rotation: raising the signer floor without operator authorization refused by TPM"
480fi
481q tpm2_nvincrement -C o -P "${OWNER_AUTH}" "${SIGNER_FLOOR}" || die "revoke increment"
482refuse "rotation: revoked signer A refused" normal_path NN1 "${N1}" A
483allow "rotation: signer B accepted after revoke" normal_path NN1B "${N1}" B
484
485echo "== recovery (operator control only)"
486boot off foreign "${N}"
487allow "recovery: operator-approved unseal of emergency-only state" recovery_path operator
488cp "${work}/last_op.sig" "${work}/replay.sig"
489refuse "recovery: replayed operator signature (stale nonce)" recovery_path operator "${work}/replay.sig"
490refuse "recovery: signature by non-operator key" recovery_path opattacker
491refuse "recovery: normal path in emergency-only state" normal_path NN1B "${N}" B
492
493echo "== negative controls (the harness can fail)"
494boot on owner "${N1}"
495if normal_path NN1B "${N1}" B "${d}/mutated"; then
496 fail "control: object sealed to mutated policy released its secret"
497else
498 pass "control: object sealed to mutated policy refused by TPM"
499fi
500if recovery_path operator "" "${d}/mutated"; then
501 fail "control: mutated object released secret via recovery"
502else
503 pass "control: mutated object refused on recovery path too"
504fi
505# A checker that should report failure does: allow() on a refused case.
506saved_fails=${fails}
507allow "control: (expected to report FAIL)" normal_path NN1 "${N1}" A >/dev/null 2>&1
508if [[ ${fails} -eq $((saved_fails + 1)) ]]; then
509 fails=${saved_fails}
510 pass "control: allow() reports FAIL when the TPM refuses"
511else
512 fails=$((saved_fails + 1))
513 echo "FAIL control: allow() did not detect a refusal"
514fi
515
516echo "SUMMARY pass=${passes} fail=${fails} (swTPM simulation, not Machine 1)"
517flush_all
518if [[ ${fails} -eq 0 && ${passes} -gt 0 ]]; then
519 echo "TRUST1_GATE5_POLICY_SIM: PASS"
520 exit 0
521fi
522echo "TRUST1_GATE5_POLICY_SIM: FAIL"
523exit 1