#!/usr/bin/env python3 """Independent clean-room verifier for the Evolution 81 evidence bundle. Scope ----- This file was written by a different agent session from the one that produced the bundle. It uses only the Python standard library and ``cryptography``. It does not import any ``cain`` module and does not read the project's own verifier (``scripts/evolution81/verify_e81_offline.py``); the scheme below was inferred from the bytes on disk and confirmed against them, not copied. What it checks -------------- 1. ``manifest.json`` parses and its Ed25519 signature verifies over the canonical JSON encoding of the manifest body (every key except ``signature_b64``) using the signer public key embedded in the manifest. 2. Every file listed in the manifest exists, has the declared byte length and the declared SHA-256, and no unlisted file is present in the bundle directory (``manifest.json`` itself is structurally unlisted). 3. The Merkle root recomputes under the RFC 6962-style construction inferred from the data: leaf = SHA256(0x00 || raw_digest_bytes), node = SHA256(0x01 || left || right), an odd final node is duplicated. If the computed root differs, the mismatch is reported; it is not forced. 4. ``events.json`` is a hash chain: first ``prev`` is 64 zeros, each ``prev`` equals the previous row's ``hash``, and SHA-256 over the canonical JSON of the row without ``hash`` equals ``hash``. Each row's Ed25519 signature verifies over exactly the fields event_id, mission_id, agent_id, action, evidence_digest, prev. Fatal failures are listed in ``failures`` and drive ``ok``. Governance observations that are not integrity failures (for example a single signing key shared by every role) are reported in ``warnings`` and do not flip ``ok``. Usage: python3 verify.py [BUNDLE_DIR] Prints one JSON object to stdout. Exit code 0 when ok is true, 1 otherwise. """ from __future__ import annotations import base64 import hashlib import json import os import sys from cryptography.exceptions import InvalidSignature from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey ZERO_HASH = "0" * 64 EVENT_SIGNED_FIELDS = ( "event_id", "mission_id", "agent_id", "action", "evidence_digest", "prev", ) EXPECTED_SCHEMA = "cain2027.e81.evidence.manifest.v1" def canonical_json(obj) -> bytes: """Compact, key-sorted JSON used by the bundle for hashing and signing.""" return json.dumps( obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False ).encode("utf-8") def sha256_hex(data: bytes) -> str: return hashlib.sha256(data).hexdigest() def sha256_file(path: str) -> tuple[str, int]: h = hashlib.sha256() size = 0 with open(path, "rb") as fh: for chunk in iter(lambda: fh.read(1 << 20), b""): h.update(chunk) size += len(chunk) return h.hexdigest(), size def merkle_root(digests: list[bytes]) -> str: """RFC 6962-style tree: 0x00 leaf prefix, 0x01 node prefix, odd duplicated.""" if not digests: return hashlib.sha256(b"").hexdigest() level = [hashlib.sha256(b"\x00" + d).digest() for d in digests] while len(level) > 1: nxt = [] for i in range(0, len(level) - 1, 2): nxt.append(hashlib.sha256(b"\x01" + level[i] + level[i + 1]).digest()) if len(level) % 2: nxt.append(level[-1]) level = nxt return level[0].hex() def load_json(path: str): with open(path, "rb") as fh: return json.loads(fh.read().decode("utf-8")) def verify_manifest_signature(manifest: dict) -> tuple[bool, str]: signer = manifest.get("signer") or {} pub_b64 = signer.get("public_key_b64") sig_b64 = manifest.get("signature_b64") if not pub_b64 or not sig_b64: return False, "missing signer.public_key_b64 or signature_b64" try: pub_raw = base64.b64decode(pub_b64, validate=True) sig = base64.b64decode(sig_b64, validate=True) except Exception as exc: # noqa: BLE001 - report, never crash return False, f"base64 decode error: {exc}" body = {k: v for k, v in manifest.items() if k != "signature_b64"} try: Ed25519PublicKey.from_public_bytes(pub_raw).verify(sig, canonical_json(body)) return True, "" except InvalidSignature: return False, "Ed25519 signature does not verify over canonical manifest body" except Exception as exc: # noqa: BLE001 return False, f"signature verification error: {exc}" def verify_events( events: list, manifest_key_b64: str | None, ) -> tuple[dict, list[dict], list[str]]: checks: dict[str, bool] = {} failures: list[dict] = [] warnings: list[str] = [] if not isinstance(events, list): return ( {"events_is_list": False}, [{"check": "events_is_list", "detail": "events.json is not a JSON array"}], [], ) checks["events_is_list"] = True prev = ZERO_HASH chain_start_ok = True links_ok = True hashes_ok = True sigs_ok = True fields_ok = True signer_keys: set[str] = set() actions: list[str] = [] for idx, row in enumerate(events): if not isinstance(row, dict): failures.append( {"check": "event_row_shape", "detail": f"event #{idx} is not an object"} ) fields_ok = False continue missing = [f for f in EVENT_SIGNED_FIELDS if f not in row] if missing or "hash" not in row: failures.append( { "check": "event_required_fields", "detail": f"event #{idx} missing {missing or ['hash']}", } ) fields_ok = False # Cannot meaningfully check the remainder of this row. prev = row.get("hash", prev) continue actions.append(str(row.get("action"))) if idx == 0 and row["prev"] != ZERO_HASH: chain_start_ok = False failures.append( { "check": "event_chain_start", "detail": f"first event prev={row['prev']!r}, expected 64 zeros", } ) if idx > 0 and row["prev"] != prev: links_ok = False failures.append( { "check": "event_chain_link", "detail": f"event #{idx} prev={row['prev']} != previous hash={prev}", } ) body = {k: v for k, v in row.items() if k != "hash"} recomputed = sha256_hex(canonical_json(body)) if recomputed != row["hash"]: hashes_ok = False failures.append( { "check": "event_hash", "detail": f"event #{idx} hash={row['hash']} != recomputed={recomputed}", } ) pub_b64 = row.get("agent_public_key_b64") sig_b64 = row.get("signature_b64") if not pub_b64 or not sig_b64: sigs_ok = False failures.append( { "check": "event_signature", "detail": f"event #{idx} missing agent_public_key_b64 or signature_b64", } ) else: signer_keys.add(pub_b64) signed = {f: row[f] for f in EVENT_SIGNED_FIELDS} try: Ed25519PublicKey.from_public_bytes( base64.b64decode(pub_b64, validate=True) ).verify( base64.b64decode(sig_b64, validate=True), canonical_json(signed) ) except InvalidSignature: sigs_ok = False failures.append( { "check": "event_signature", "detail": f"event #{idx} signature does not verify", } ) except Exception as exc: # noqa: BLE001 sigs_ok = False failures.append( { "check": "event_signature", "detail": f"event #{idx} signature error: {exc}", } ) prev = row["hash"] checks["event_required_fields"] = fields_ok checks["event_chain_starts_at_zero"] = chain_start_ok checks["event_chain_links"] = links_ok checks["event_hashes"] = hashes_ok checks["event_signatures"] = sigs_ok if manifest_key_b64 is not None: same_key = signer_keys == {manifest_key_b64} checks["event_signer_key_matches_manifest"] = same_key if not same_key: failures.append( { "check": "event_signer_key_matches_manifest", "detail": f"event signer key(s) {sorted(signer_keys)} != manifest signer", } ) # Observation only: one key signing every role is not an integrity # failure, but it removes any cryptographic separation of duties. if len(signer_keys) <= 1: warnings.append( "all events were signed by a single key that is also the manifest signing " "key; proposer and verifier are not cryptographically separated" ) if same_key: warnings.append( "the signer public key is self-declared inside the manifest and is not " "pinned out of band, so the signature proves internal consistency only" ) if len(actions) != len(set(actions)): duplicates = sorted({a for a in actions if actions.count(a) > 1}) warnings.append( f"duplicate event actions in the evidence chain: {duplicates} " "(the chain contains repeated state transitions rather than one timeline)" ) return checks, failures, warnings def verify_bundle(bundle_dir: str) -> dict: bundle_dir = os.path.abspath(bundle_dir) checks: dict[str, bool] = {} failures: list[dict] = [] warnings: list[str] = [] counts: dict = {} def fail(check: str, detail: str) -> None: checks[check] = False failures.append({"check": check, "detail": detail}) manifest_path = os.path.join(bundle_dir, "manifest.json") if not os.path.isfile(manifest_path): return { "ok": False, "schema": "cain2027.e81.independent_verification.v1", "bundle": bundle_dir, "checks": {"manifest_present": False}, "failures": [{"check": "manifest_present", "detail": "manifest.json missing"}], "warnings": [], "counts": {}, } checks["manifest_present"] = True try: manifest = load_json(manifest_path) except Exception as exc: # noqa: BLE001 fail("manifest_json_valid", f"manifest.json is not valid JSON: {exc}") return { "ok": False, "schema": "cain2027.e81.independent_verification.v1", "bundle": bundle_dir, "checks": checks, "failures": failures, "warnings": warnings, "counts": counts, } checks["manifest_json_valid"] = True if not isinstance(manifest, dict): fail("manifest_is_object", "manifest.json is not a JSON object") return { "ok": False, "schema": "cain2027.e81.independent_verification.v1", "bundle": bundle_dir, "checks": checks, "failures": failures, "warnings": warnings, "counts": counts, } checks["manifest_is_object"] = True if manifest.get("schema") != EXPECTED_SCHEMA: fail("manifest_schema", f"schema={manifest.get('schema')!r}, expected {EXPECTED_SCHEMA!r}") else: checks["manifest_schema"] = True sig_ok, sig_detail = verify_manifest_signature(manifest) if sig_ok: checks["manifest_signature"] = True else: fail("manifest_signature", sig_detail) # The manifest's key_fingerprint is the first 32 hex chars of # sha256(public_key_b64 as ASCII). This is an identifier, not a trust # anchor: it is reproducible from the key, but nothing pins it externally. signer = manifest.get("signer") or {} declared_fp = signer.get("key_fingerprint") pub_b64 = signer.get("public_key_b64") if declared_fp and pub_b64: computed_fp = sha256_hex(pub_b64.encode("ascii"))[:32] checks["signer_fingerprint_matches_key"] = computed_fp == declared_fp if computed_fp != declared_fp: failures.append( { "check": "signer_fingerprint_matches_key", "detail": f"key_fingerprint={declared_fp} != sha256(public_key_b64)[:32]={computed_fp}", } ) # ---- file layer ----------------------------------------------------- files = manifest.get("files") if not isinstance(files, list) or not files: fail("files_listed", "manifest.files is missing or empty") files = [] else: checks["files_listed"] = True listed_paths: set[str] = set() digests: list[bytes] = [] files_present = True file_hashes = True file_sizes = True path_safe = True files_regular = True for entry in files: if not isinstance(entry, dict): fail("files_shape", "manifest.files contains a non-object entry") file_hashes = False continue rel = entry.get("path") if not isinstance(rel, str) or not rel: fail("files_shape", f"entry without a usable path: {entry!r}") file_hashes = False continue norm = os.path.normpath(rel) if os.path.isabs(rel) or norm.startswith("..") or norm != rel.replace("\\", "/"): path_safe = False failures.append( {"check": "no_path_traversal", "detail": f"listed path escapes bundle: {rel!r}"} ) listed_paths.add(norm) full = os.path.join(bundle_dir, norm) if os.path.islink(full): files_regular = False failures.append( {"check": "files_are_regular", "detail": f"{rel}: listed file is a symlink"} ) if not os.path.isfile(full): files_present = False failures.append({"check": "files_present", "detail": f"missing listed file: {rel}"}) continue actual_hash, actual_size = sha256_file(full) declared_hash = str(entry.get("sha256", "")).lower() if actual_hash != declared_hash: file_hashes = False failures.append( { "check": "file_hashes", "detail": f"{rel}: sha256={actual_hash} != declared={declared_hash}", } ) if entry.get("bytes") is not None and int(entry["bytes"]) != actual_size: file_sizes = False failures.append( { "check": "file_sizes", "detail": f"{rel}: bytes={actual_size} != declared={entry['bytes']}", } ) try: digests.append(bytes.fromhex(declared_hash)) except ValueError: file_hashes = False failures.append( {"check": "file_hashes", "detail": f"{rel}: declared sha256 is not hex"} ) checks["files_present"] = files_present checks["file_hashes"] = file_hashes checks["file_sizes"] = file_sizes checks["no_path_traversal"] = path_safe checks["files_are_regular"] = files_regular # No unlisted files. manifest.json cannot list itself; allow it explicitly. actual_files: set[str] = set() for root, _dirs, names in os.walk(bundle_dir): for name in names: full = os.path.join(root, name) actual_files.add(os.path.relpath(full, bundle_dir).replace(os.sep, "/")) allowed = set(listed_paths) | {"manifest.json"} unlisted = sorted(actual_files - allowed) if unlisted: fail("no_unlisted_files", f"unlisted files present: {unlisted}") warnings.append( "extra files not covered by the signature: " + ", ".join(unlisted) ) else: checks["no_unlisted_files"] = True if "manifest.json" not in listed_paths: warnings.append( "manifest.json is structurally outside its own file list and is therefore " "only self-authenticated by its embedded signature" ) # ---- Merkle --------------------------------------------------------- computed_root = merkle_root(digests) declared_root = manifest.get("merkle_root") if computed_root == declared_root: checks["merkle_root"] = True else: fail( "merkle_root", f"computed={computed_root} != manifest.merkle_root={declared_root} " "(RFC6962-style over raw file digests, listed order, odd duplicated)", ) checks["merkle_leaf_count_matches"] = manifest.get("merkle_leaf_count") == len(files) if not checks["merkle_leaf_count_matches"]: failures.append( { "check": "merkle_leaf_count_matches", "detail": f"merkle_leaf_count={manifest.get('merkle_leaf_count')} " f"!= files listed={len(files)}", } ) checks["file_count_matches"] = manifest.get("file_count") == len(files) if not checks["file_count_matches"]: failures.append( { "check": "file_count_matches", "detail": f"file_count={manifest.get('file_count')} != files listed={len(files)}", } ) # ---- events --------------------------------------------------------- events_path = os.path.join(bundle_dir, "events.json") events: list = [] if not os.path.isfile(events_path): fail("events_present", "events.json missing") events_checks: dict[str, bool] = {} else: checks["events_present"] = True try: events = load_json(events_path) except Exception as exc: # noqa: BLE001 fail("events_json_valid", f"events.json is not valid JSON: {exc}") events = [] signer = manifest.get("signer") or {} events_checks, event_failures, event_warnings = verify_events( events, signer.get("public_key_b64") ) for name, value in events_checks.items(): checks[name] = value failures.extend(event_failures) warnings.extend(event_warnings) counts["events"] = len(events) if isinstance(events, list) else 0 checks["event_count_matches_manifest"] = manifest.get("event_count") == counts["events"] if not checks["event_count_matches_manifest"]: failures.append( { "check": "event_count_matches_manifest", "detail": f"event_count={manifest.get('event_count')} != events.json rows={counts['events']}", } ) # ---- counts / cross-file consistency -------------------------------- def load_side(name: str): path = os.path.join(bundle_dir, name) if not os.path.isfile(path): return None try: return load_json(path) except Exception: # noqa: BLE001 return None tasks = load_side("tasks.json") effects = load_side("effects.json") counts["tasks"] = len(tasks) if isinstance(tasks, list) else 0 counts["effects"] = len(effects) if isinstance(effects, list) else 0 counts["files_listed"] = len(files) counts["files_checked"] = sum( 1 for e in files if isinstance(e, dict) and os.path.isfile(os.path.join(bundle_dir, os.path.normpath(str(e.get("path"))))) ) counts["merkle_leaves"] = len(digests) counts["event_signatures_checked"] = ( len([r for r in events if isinstance(r, dict) and "signature_b64" in r]) if isinstance(events, list) else 0 ) checks["task_count_matches_manifest"] = manifest.get("task_count") == counts["tasks"] if not checks["task_count_matches_manifest"]: failures.append( { "check": "task_count_matches_manifest", "detail": f"task_count={manifest.get('task_count')} != tasks.json rows={counts['tasks']}", } ) checks["effect_count_matches_manifest"] = manifest.get("effect_count") == counts["effects"] if not checks["effect_count_matches_manifest"]: failures.append( { "check": "effect_count_matches_manifest", "detail": f"effect_count={manifest.get('effect_count')} != effects.json rows={counts['effects']}", } ) if isinstance(tasks, list) and isinstance(effects, list): task_idem = sorted( str(t.get("idem")) for t in tasks if isinstance(t, dict) and t.get("idem") ) effect_idem = sorted( str(e.get("idem")) for e in effects if isinstance(e, dict) and e.get("idem") ) idem_ok = task_idem == effect_idem and len(set(effect_idem)) == len(effect_idem) checks["task_effect_idempotency"] = idem_ok if not idem_ok: failures.append( { "check": "task_effect_idempotency", "detail": f"task idempotency keys ({len(task_idem)}) do not match " f"unique effect keys ({len(set(effect_idem))})", } ) mission = load_side("mission.json") if isinstance(mission, dict): counts["mission_state"] = mission.get("state") counts["mission_terminal"] = mission.get("terminal") checks["mission_state_present"] = bool(mission.get("state")) if not checks["mission_state_present"]: failures.append( {"check": "mission_state_present", "detail": "mission.json has no state field"} ) # Informational only: verdict is reported, never trusted as a check. verdict = manifest.get("verdict") if isinstance(mission, dict) and verdict and mission.get("state") != verdict: warnings.append( f"manifest verdict {verdict!r} != mission.json state {mission.get('state')!r}" ) if verdict in ("COMPLETED_WITH_UNVERIFIED_ITEMS", "COMPLETED_PARTIAL"): warnings.append( f"bundle verdict is {verdict}: the producer itself did not claim full completion" ) ok = bool(checks) and all(checks.values()) and not failures return { "ok": ok, "schema": "cain2027.e81.independent_verification.v1", "bundle": bundle_dir, "checks": checks, "failures": failures, "warnings": warnings, "counts": counts, "manifest": { "schema": manifest.get("schema"), "mission_id": manifest.get("mission_id"), "verdict": manifest.get("verdict"), "evidence_class": manifest.get("evidence_class"), "signer_fingerprint": (manifest.get("signer") or {}).get("key_fingerprint"), "merkle_root": manifest.get("merkle_root"), "computed_merkle_root": computed_root, }, } def main(argv: list[str]) -> int: bundle = argv[1] if len(argv) > 1 else "artifacts/e81/bundle" result = verify_bundle(bundle) print(json.dumps(result, indent=1, sort_keys=True)) return 0 if result["ok"] else 1 if __name__ == "__main__": sys.exit(main(sys.argv))