#!/usr/bin/env python3
"""KEYSTONE-family verifier: re-check a signed record from ANY product in the family, offline, with no CAIN code.

The family shares one evidence convention -- canonical JSON, SHA-256 record hash, Ed25519 signature against a
pinned key, a chain link to the previous record, and a digest of the artifact the record is about. This verifier
re-checks all of it and understands each product's artifact field, so ONE tool verifies the whole family,
including records produced by another session's product (MCP-Attestor).

You must pin the signing key yourself. The bundle's own public_key.json is never trusted on its own: anyone who
can hand you a zip can also put their own key in it and re-sign every record. Get the key from a source you
trust (the issuer's published key URL, fetched yourself) and pass it in:

    python3 family_verify.py --pinned-pub <b64>                  # verify the whole bundle in this folder
    python3 family_verify.py --pinned-fingerprint SHA256:<hex>   # same, pinning the key by its fingerprint
    python3 family_verify.py record.json --pinned-pub <b64>      # one record
    python3 family_verify.py record.json --prev prev.json --pinned-pub <b64>   # also check the chain link

Without a pin it refuses (exit 3) and prints the fingerprint of the key the bundle claims, so you can compare it.

A bundle also carries head.json: a statement signed by the same key that lists exactly which records the bundle
holds, in order. Removing, adding, or reordering a record, or rewriting the chain, makes it fail.

Needs Python 3 and `cryptography` (pip install cryptography). Exit code 0 only when every check passes.
Imports nothing from CAIN, on purpose: the verdict must not depend on the claimant's own code.
"""
from __future__ import annotations

import base64
import glob
import hashlib
import json
import os
import sys

from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey

BUNDLE_HEAD_KIND = "cain42.keystone.bundle-head.v1"
NOT_PINNED = 3

# schema -> (artifact field, digest field). The family convention: the record carries the exact artifact text and
# the SHA-256 of that text, so a changed byte anywhere shows up as a digest mismatch.
ARTIFACT = {
    "cain42.keystone.fix-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    # Second, independent proof of the same fix: the project's own tests, run in a throwaway checkout.
    "cain42.keystone.project-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.mcp-hardening.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.vericoding-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.openclaw-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.swarm-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.vibeguard-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.memguard-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.surfguard-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.actuary-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.codeguard-proof.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.ci-attestation.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.broker-issuance.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.approval-event.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.governor-event.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.recorder-seal.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.agentci-verdict.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.keystone.redteam-patch.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    # Operational records: they carry no code artifact, only a signed statement (anchor outcome / ledger audit).
    "cain42.keystone.anchor.v1": (),
    "cain42.keystone.anchor.error.v1": (),
    "cain42.keystone.ledger-audit.v1": (),
    "cain42.keystone.sample-audit.v1": ("original_code", "original_sha256", "fixed_code", "fixed_sha256"),
    "cain42.mcp_attestor.proof.v1": ("descriptor", "descriptor_sha256", None, None),
    # CAIN-42 audit deliverables (cain_audit): the manifest binds what was scanned, the report is the artifact.
    "cain42.keystone.fabrication-audit.v1": ("scanned_manifest", "scanned_manifest_sha256", "report", "report_sha256",
                                             "findings_json", "findings_sha256"),
    "cain42.keystone.mcp-review.v1": ("tools_json", "tools_sha256", "report", "report_sha256",
                                      "findings_json", "findings_sha256"),
    # Six-product delivery spine (cain_audit/deliver.py): the report and the raw engine output are the artifacts.
    "cain42.delivery.record.v1": ("report", "report_sha256", "findings_json", "findings_sha256"),
    # PROOFLOOP Repo Sweep (keystone/sweep.py via cain_audit): same binding as the fabrication audit.
    "cain42.proofloop.sweep.v1": ("scanned_manifest", "scanned_manifest_sha256", "report", "report_sha256",
                                  "findings_json", "findings_sha256"),
    # AUTHORITY NOTARY v3 (cain/notary): one record binds a six-layer stack as a canonical JSON artifact.
    # The artifact is `layers_json`; the verifier recomputes its digest and, in the bundle, its own
    # re-derivation of every layer (see verify_notary.py).
    "cain42.notary.v3.stack.v1": ("layers_json", "layers_sha256", None, None),
    # Proof Overlay: the event payload is covered by the record hash; no separate artifact field.
    "cain42.overlay.event.v1": (),
}

# Deliverables that also leave the record as standalone files in the bundle. The record is the root of
# truth, so a file that no longer matches its record field is tampering even if the hash chain is intact.
# (schema -> [(artifact field, digest field, filename in the bundle), ...])
CURRENT_STANDARD = "cain42.keystone.proof-standard.v2"

BUNDLE_FILES = {
    "cain42.keystone.fabrication-audit.v1": [
        ("report", "report_sha256", "report.md"),
        ("findings_json", "findings_sha256", "findings.json"),
    ],
    "cain42.keystone.mcp-review.v1": [
        ("report", "report_sha256", "report.md"),
        ("findings_json", "findings_sha256", "findings.json"),
    ],
    "cain42.delivery.record.v1": [
        ("report", "report_sha256", "report.md"),
        ("findings_json", "findings_sha256", "findings.json"),
    ],
    "cain42.proofloop.sweep.v1": [
        ("report", "report_sha256", "report.md"),
        ("findings_json", "findings_sha256", "findings.json"),
    ],
    "cain42.notary.v3.stack.v1": [
        ("layers_json", "layers_sha256", "layers.json"),
    ],
    # Proof Overlay: a normalized decision/telemetry event from another vendor, signed and chained.
    # It binds no code/text artifact of its own; the event payload is covered by the record hash.
    "cain42.overlay.event.v1": [],
}


def sha(text) -> str:
    return hashlib.sha256(text.encode() if isinstance(text, str) else text).hexdigest()


def canon(obj) -> bytes:
    return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode()


def fingerprint(public_key_b64: str) -> str:
    """SHA-256 of the raw 32-byte public key, the same form the issuer's pages show."""
    return "SHA256:" + hashlib.sha256(base64.b64decode(public_key_b64)).hexdigest()


def _signature_ok(pinned_b64: str, signature_b64, signed_hash) -> bool:
    try:
        Ed25519PublicKey.from_public_bytes(base64.b64decode(pinned_b64)).verify(
            base64.b64decode(signature_b64 or ""), str(signed_hash or "").encode())
        return True
    except (InvalidSignature, ValueError):
        return False


def recheck(rec: dict, pinned_b64: str, prev: dict | None = None) -> list:
    """Return the list of problems (empty means valid). Never trusts the record's own claims."""
    problems = []
    body = {k: v for k, v in rec.items() if k not in ("record_hash", "signature_b64", "public_key_b64")}
    if sha(canon(body)) != rec.get("record_hash"):
        problems.append("content does not match record_hash")
    if not _signature_ok(pinned_b64, rec.get("signature_b64"), rec.get("record_hash")):
        problems.append("signature does not verify against the pinned key")
    if prev is not None and rec.get("prev_record_hash") != prev.get("record_hash"):
        problems.append("prev_record_hash does not link to the previous record")
    schema = rec.get("schema")
    spec = ARTIFACT.get(schema)
    if spec is None:
        problems.append(f"unknown record schema {schema!r}: this verifier cannot check its artifact digest")
    else:
        for i in range(0, len(spec), 2):
            text_field, digest_field = spec[i], spec[i + 1]
            if text_field is None:
                continue
            if text_field in rec or digest_field in rec:
                if sha(rec.get(text_field, "")) != rec.get(digest_field):
                    problems.append(f"{text_field} does not match {digest_field}")
    return problems


def check_head(head: dict, records: list, pinned_b64: str) -> list:
    """The signed head must name exactly these records, in this order, and itself verify against the pinned key."""
    problems = []
    if head.get("schema") != BUNDLE_HEAD_KIND:
        problems.append(f"head.json has schema {head.get('schema')!r}, expected {BUNDLE_HEAD_KIND!r}")
    body = {k: v for k, v in head.items() if k not in ("head_hash", "signature_b64")}
    if sha(canon(body)) != head.get("head_hash"):
        problems.append("head.json content does not match its head_hash")
    if not _signature_ok(pinned_b64, head.get("signature_b64"), head.get("head_hash")):
        problems.append("head.json signature does not verify against the pinned key")
    listed = head.get("record_hashes") or []
    present = [r.get("record_hash") for r in records]
    if head.get("count") != len(listed):
        problems.append("head.json count does not match its own record list")
    if listed != present:
        missing = [h for h in listed if h not in present]
        extra = [h for h in present if h not in listed]
        if missing:
            problems.append(f"{len(missing)} record(s) named by the signed head are missing from the bundle")
        if extra:
            problems.append(f"{len(extra)} record(s) in the bundle are not named by the signed head")
        if not missing and not extra:
            problems.append("records are not in the order the signed head lists")
    if records and head.get("first_prev_record_hash") != records[0].get("prev_record_hash"):
        problems.append("the first record does not start where the signed head says the chain starts")
    seqs = [r.get("sequence") for r in records]
    if seqs and all(isinstance(s, int) for s in seqs) and seqs != list(range(seqs[0], seqs[0] + len(seqs))):
        problems.append("record sequence numbers are not contiguous")
    return problems


def load(path: str):
    with open(path, encoding="utf-8") as fh:
        return json.load(fh)


def _take(argv: list, flag: str):
    if flag not in argv:
        return None
    i = argv.index(flag)
    if i + 1 >= len(argv):
        raise SystemExit(f"{flag} needs a value")
    value = argv[i + 1]
    del argv[i:i + 2]
    return value


def resolve_pin(pinned_pub, pinned_fp, bundled_b64):
    """Return (key_b64, None) when the caller pinned a key, else (None, message). Never falls back silently."""
    if pinned_pub:
        if bundled_b64 and bundled_b64 != pinned_pub:
            print("note: this bundle's public_key.json is a DIFFERENT key from the one you pinned "
                  f"({fingerprint(bundled_b64)}); records are checked against your pin only")
        return pinned_pub, None
    if pinned_fp:
        if not bundled_b64:
            return None, "--pinned-fingerprint needs the bundle's public_key.json to compare against"
        want = pinned_fp.strip().lower()
        want = want if want.startswith("sha256:") else "sha256:" + want
        got = fingerprint(bundled_b64).lower()
        if len(want) < len("sha256:") + 32 or not got.startswith(want):
            return None, (f"the bundle's key {fingerprint(bundled_b64)} does NOT match the fingerprint you pinned "
                          f"({pinned_fp}): this bundle was not signed by the key you trust")
        return bundled_b64, None
    claimed = f"The bundle claims the key {fingerprint(bundled_b64)}." if bundled_b64 else "The bundle names no key."
    return None, ("no pinned key. A key that ships inside the bundle proves nothing: whoever hands you the bundle "
                  "can replace it and re-sign every record. " + claimed + " Fetch the issuer's published key "
                  "yourself and re-run with --pinned-pub <b64>, or compare fingerprints and re-run with "
                  "--pinned-fingerprint SHA256:<hex>.")


def _open_zip(path: str) -> str:
    """Extract a KEYSTONE evidence bundle ZIP into a temporary directory and return it.

    Auditors often download the bundle and run the verifier on the ZIP directly. Supporting that here removes the
    unzip step (and the confusing 'unreadable' error). Member names are checked so a crafted ZIP cannot escape the
    temporary directory. Nothing is executed; the files are only read as JSON."""
    import tempfile
    import zipfile
    td = tempfile.mkdtemp(prefix="keystone-verify-")
    root = os.path.realpath(td)
    with zipfile.ZipFile(path) as z:
        for m in z.infolist():
            name = m.filename
            parts = os.path.normpath(name).replace("\\", "/").split("/")
            if name.startswith("/") or ".." in parts or (parts and ":" in parts[0]):
                raise ValueError(f"refusing an unsafe member name in the bundle: {name!r}")
            dest = os.path.realpath(os.path.join(td, name))
            if dest != root and not dest.startswith(root + os.sep):
                raise ValueError(f"refusing a member outside the bundle: {name!r}")
            if name.endswith("/"):
                os.makedirs(dest, exist_ok=True)
            else:
                os.makedirs(os.path.dirname(dest), exist_ok=True)
                with z.open(m) as src, open(dest, "wb") as out:
                    out.write(src.read())
    return td


def main(argv=None) -> int:
    """CLI entry point. A bundle ZIP is extracted into a temporary directory; restore the caller's cwd afterwards
    so importing this module and calling main() cannot change the caller's working directory (fixed 2026-11-06)."""
    cwd = os.getcwd()
    try:
        return _main(argv)
    finally:
        try:
            os.chdir(cwd)
        except OSError:
            pass


def _main(argv=None) -> int:
    argv = list(sys.argv[1:] if argv is None else argv)
    zips = [a for a in argv if a.lower().endswith(".zip")]
    if zips:
        if len(zips) > 1:
            print("pass one bundle ZIP at a time")
            return 2
        zp = zips[0]
        if not os.path.isfile(zp):
            print(f"no such bundle: {zp}")
            return 2
        import zipfile
        try:
            os.chdir(_open_zip(zp))
        except (OSError, ValueError, zipfile.BadZipFile) as e:
            print(f"INVALID bundle: {e}")
            print("RESULT: TAMPERED OR INVALID")
            return 1
        argv.remove(zp)
    pinned_pub = _take(argv, "--pinned-pub")
    pinned_fp = _take(argv, "--pinned-fingerprint")
    prev_path = _take(argv, "--prev")
    prev = load(prev_path) if prev_path else None
    bundle_mode = not argv
    files = argv or sorted(glob.glob("record*.json"))
    if not files:
        print("no record files given (pass record.json, or run where record*.json exist)")
        return 2
    bundled = None
    if os.path.exists("public_key.json"):
        try:
            bundled = load("public_key.json")["public_key_b64"]
        except (OSError, ValueError, KeyError):
            bundled = None
    pinned, why = resolve_pin(pinned_pub, pinned_fp, bundled)
    if pinned is None:
        print("NOT VERIFIED: " + why)
        print("RESULT: NOT VERIFIED (no trusted key)")
        return NOT_PINNED
    print(f"pinned key {fingerprint(pinned)}")
    ok = True
    records = []
    whole_bundle = bundle_mode or sorted(files) == sorted(glob.glob("record*.json"))
    for f in files:
        try:
            rec = load(f)
        except (OSError, ValueError) as e:
            print(f"INVALID {f}  (unreadable: {e})")
            ok = False
            continue
        problems = recheck(rec, pinned, prev)
        if whole_bundle:
            for text_field, digest_field, filename in BUNDLE_FILES.get(rec.get("schema"), []):
                if filename and os.path.exists(filename):
                    with open(filename, "rb") as fh:
                        if sha(fh.read()) != rec.get(digest_field):
                            problems.append(
                                f"{filename} in the bundle does not match the signed record's {digest_field}")
        std = str(rec.get("standard") or "legacy/pre-v2")
        label = (("VALID   " if not problems else "INVALID ") + f +
                 "  product=" + str(rec.get("product") or rec.get("schema")) +
                 "  verdict=" + str(rec.get("verdict")))
        if std != CURRENT_STANDARD:
            # The signature is authentic, but the record predates the independent-test rule. Say so on the line an
            # auditor reads, so a legacy VERIFIED_FIX is never mistaken for a proof under the current standard.
            label += f"  standard={std} (legacy; not proof under the current standard)"
        print(label)
        for p in problems:
            print("   - " + p)
        ok = ok and not problems
        records.append(rec)
        prev = rec
    if os.path.exists("head.json") and whole_bundle:
        try:
            head = load("head.json")
        except (OSError, ValueError) as e:
            head, ok = None, False
            print(f"INVALID head.json  (unreadable: {e})")
        if head is not None:
            problems = check_head(head, records, pinned)
            print(("VALID   " if not problems else "INVALID ") + f"head.json  records={len(records)}"
                  f"  issued_at={head.get('issued_at')}")
            for p in problems:
                print("   - " + p)
            ok = ok and not problems
    elif bundle_mode:
        print("INVALID bundle: no head.json, so removed or reordered records cannot be ruled out")
        ok = False
    else:
        print("note: only the record(s) you named were checked, not the bundle's completeness "
              "(run with no file names to check the whole bundle against head.json)")
    print("RESULT:", "ALL RECORDS VALID" if ok else "TAMPERED OR INVALID")
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())
