#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Blake. See verifier/LICENSE.
"""Verify osfi-gate evidence without osfi-gate.

One file, Python 3.8 or later, the standard library only. Signatures are checked with the
`openssl` command, because the standard library has no ECDSA. Written from
`docs/evidence-format.md`, not from osfi-gate's code: the point is that the format can be
checked by something its author did not write, and this is the nearest a single author gets.

    python3 osfi_verify.py verify --evidence-dir DIR [--repo R] [--pubkey KEY.pem]
                                  [--anchors DIR] [--json]
    python3 osfi_verify.py sample-check SAMPLE.zip --evidence-dir DIR [--evidence-dir DIR ...]

Exit status: 0 when everything checked holds, 1 when anything fails or a signature that should
have been checked was not, 2 when there was nothing to check or the arguments were wrong.

What it does not do -- and says on every run -- is re-run the policy over the recorded input.
That needs the policy engine and is `osfi-gate verify --policy-dir`.
"""

from __future__ import annotations

import argparse
import base64
import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import tarfile
import tempfile
import urllib.request
import zipfile
from datetime import datetime, timedelta, timezone

GENESIS = "0" * 64
CONTROL_FILES = ("manifest.json", "statement.json", "statement.sigstore.json")
PEM_BODY = re.compile(r"-----BEGIN PUBLIC KEY-----(.+?)-----END PUBLIC KEY-----", re.S)


def sha256_hex(data):
    return hashlib.sha256(data).hexdigest()


def slug(repo):
    return repo.replace("/", "__")


# --------------------------------------------------------------------------- canonical JSON (§2)

class Malformed(Exception):
    pass


def _check_values(value):
    """§2: integers, strings, booleans, null, arrays, objects. Nothing else."""
    if isinstance(value, bool) or value is None or isinstance(value, str):
        return
    if isinstance(value, int):
        if abs(value) > 2 ** 53 - 1:
            raise Malformed("an integer beyond 2^53 - 1, which canonical JSON does not define")
        return
    if isinstance(value, float):
        raise Malformed("a non-integer number, which canonical JSON does not define")
    if isinstance(value, list):
        for v in value:
            _check_values(v)
        return
    if isinstance(value, dict):
        for k, v in value.items():
            _check_values(v)
        return
    raise Malformed("an unexpected value")


def canonical(value):
    _check_values(value)
    return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")


def entry_hash(entry):
    return sha256_hex(canonical({k: v for k, v in entry.items() if k != "entry_hash"}))


def key_id(pem_text):
    m = PEM_BODY.search(pem_text)
    if not m:
        return None
    return sha256_hex("".join(m.group(1).split()).encode("ascii"))


# --------------------------------------------------------------------------- reading


def read_ledger(path):
    entries, problems = [], []
    with open(path, "rb") as f:
        # LF only (§4): str.splitlines() would also split at U+2028 inside a string.
        for n, line in enumerate(f.read().decode("utf-8").split("\n"), 1):
            if not line.strip():
                continue
            try:
                e = json.loads(line)
                if not isinstance(e, dict):
                    raise Malformed("not a JSON object")
                _check_values(e)
                for field, kind in (("seq", int), ("bundle_id", str), ("entry_hash", str),
                                    ("prev_entry_hash", str), ("manifest_sha256", str),
                                    ("statement_sha256", str), ("signing", dict)):
                    if not isinstance(e.get(field), kind) or isinstance(e.get(field), bool):
                        raise Malformed(f"'{field}' is missing or has the wrong type")
            except (ValueError, Malformed) as ex:
                problems.append(("ledger.malformed", n, f"line {n}: {ex}"))
                continue
            entries.append(e)
    return entries, problems


def read_bundle(bundles_dir, repo, bundle_id):
    """The bundle's files as {relative path: bytes}, or (None, code, detail).

    Archives are read in memory and never extracted, and anything in one that is not a plain
    file under the bundle's own directory is refused: the archive is the input under test.
    """
    base = os.path.join(bundles_dir, slug(repo))
    directory = os.path.join(base, bundle_id)
    if os.path.isfile(os.path.join(directory, "manifest.json")):
        files = {}
        for root, _, names in os.walk(directory):
            for name in names:
                full = os.path.join(root, name)
                rel = os.path.relpath(full, directory).replace(os.sep, "/")
                with open(full, "rb") as f:
                    files[rel] = f.read()
        return files, None, None
    archive = os.path.join(base, bundle_id + ".tar.gz")
    if not os.path.isfile(archive):
        return None, "bundle.missing", "no directory or archive for " + bundle_id
    files = {}
    with tarfile.open(archive, "r:gz") as tar:
        for m in tar.getmembers():
            name = m.name
            parts = name.split("/")
            if m.issym() or m.islnk() or name.startswith("/") or ".." in parts:
                return None, "bundle.unsafe_archive", f"{bundle_id}: refused member {name!r}"
            if m.isdir():
                continue
            if not m.isfile() or parts[0] != bundle_id or len(parts) < 2:
                return None, "bundle.unsafe_archive", f"{bundle_id}: unexpected member {name!r}"
            files["/".join(parts[1:])] = tar.extractfile(m).read()
    return files, None, None


# --------------------------------------------------------------------------- signatures (§6)


def check_signature(statement, sig_file, signing, pubkey_pem):
    """(code or None, note or None). A code of None means verified."""
    if signing.get("mode") != "key":
        return "signature.unsupported", f"{signing.get('mode')} signature: not checked by this verifier"
    try:
        sig = base64.b64decode(json.loads(sig_file)["base64Signature"], validate=True)
    except (ValueError, KeyError, TypeError):
        return "signature.unsupported", "a signature file this version does not define: not checked"
    if pubkey_pem is None:
        return "signature.no_key", "key-signed; pass --pubkey to check it"
    openssl = shutil.which("openssl")
    if openssl is None:
        return "signature.no_tool", "no openssl command to check an ECDSA signature with: not checked"
    with tempfile.TemporaryDirectory() as t:
        paths = {}
        for name, data in (("key.pem", pubkey_pem.encode()), ("sig.der", sig), ("stmt", statement)):
            paths[name] = os.path.join(t, name)
            with open(paths[name], "wb") as f:
                f.write(data)
        res = subprocess.run([openssl, "dgst", "-sha256", "-verify", paths["key.pem"],
                              "-signature", paths["sig.der"], paths["stmt"]],
                             capture_output=True)
    if res.returncode != 0:
        return "signature.invalid", "the signature does not verify against the key given"
    return None, None


# --------------------------------------------------------------------------- verification (§5)


def verify_bundle(entry, files, pubkey_pem):
    codes, notes = [], []
    manifest_bytes = files.get("manifest.json")
    statement = files.get("statement.json")
    if manifest_bytes is None or statement is None:
        missing = "manifest.json" if manifest_bytes is None else "statement.json"
        return [("file.missing", f"{missing} is not in the bundle")], notes
    if sha256_hex(manifest_bytes) != entry["manifest_sha256"]:
        codes.append(("manifest.digest", "the manifest is not the one the ledger records"))
    try:
        manifest = json.loads(manifest_bytes)
        listed = manifest["files"]
        if manifest.get("seq") != entry["seq"] or manifest.get("prev_entry_hash") != entry["prev_entry_hash"]:
            codes.append(("manifest.position", "the manifest's place in the chain is not the ledger's"))
    except (ValueError, KeyError, TypeError):
        return codes + [("manifest.digest", "the manifest cannot be read")], notes
    evidence = {p: b for p, b in files.items() if p not in CONTROL_FILES}
    for path, digest in sorted(listed.items()):
        if path not in evidence:
            codes.append(("file.missing", path))
        elif sha256_hex(evidence[path]) != digest:
            codes.append(("file.changed", path))
    for path in sorted(set(evidence) - set(listed)):
        codes.append(("file.extra", path))
    if sha256_hex(statement) != entry["statement_sha256"]:
        codes.append(("statement.digest", "the statement is not the one the ledger records"))
    try:
        subjects = json.loads(statement).get("subject") or []
    except ValueError:
        subjects = []
    want = {"name": "manifest.json", "digest": {"sha256": sha256_hex(manifest_bytes)}}
    if want not in subjects:
        codes.append(("statement.coverage", "the statement does not cover this manifest"))

    signing = entry["signing"]
    if signing.get("signed"):
        held = key_id(pubkey_pem) if pubkey_pem else None
        if held and signing.get("key_id") and held != signing["key_id"]:
            notes.append(("signature.key_mismatch",
                          f"the bundle names key {signing['key_id'][:16]}; the key given is {held[:16]}"))
        code, why = check_signature(statement, files.get("statement.sigstore.json", b"{}"),
                                    signing, pubkey_pem)
        if code:
            codes.append((code, why))
        else:
            notes.append(("signature.ok", "signature verified (key)"))
    else:
        notes.append(("signature.unsigned", "unsigned: integrity rests on the ledger chain only"))
    return codes, notes


NOT_CHECKED = {"signature.no_key", "signature.unsupported", "signature.no_tool"}


def verify_ledger(evidence_dir, ledger_path, pubkey_pem, anchors_dir):
    entries, findings = read_ledger(ledger_path)
    out = {"ledger": os.path.basename(ledger_path), "entries": len(entries), "findings": [],
           "bundles": []}
    for code, seq, detail in findings:
        out["findings"].append({"code": code, "seq": seq, "detail": detail})
    prev = GENESIS
    for i, e in enumerate(entries, 1):
        if e["seq"] != i:
            out["findings"].append({"code": "chain.seq", "seq": e["seq"], "detail": f"expected {i}"})
        if e["prev_entry_hash"] != prev:
            out["findings"].append({"code": "chain.prev_link", "seq": e["seq"], "detail": ""})
        if entry_hash(e) != e["entry_hash"]:
            out["findings"].append({"code": "chain.entry_hash", "seq": e["seq"], "detail": "entry edited"})
        prev = e["entry_hash"]
    out["head"] = prev
    out["repo"] = entries[0]["repo"] if entries else None

    for e in entries:
        files, code, detail = read_bundle(os.path.join(evidence_dir, "bundles"), e["repo"], e["bundle_id"])
        if files is None:
            out["bundles"].append({"bundle_id": e["bundle_id"], "seq": e["seq"], "ok": False,
                                   "codes": [code], "detail": [detail], "notes": [],
                                   "note_codes": []})
            continue
        codes, notes = verify_bundle(e, files, pubkey_pem)
        failed = [c for c, _ in codes if c not in NOT_CHECKED]
        ok = False if failed else (None if codes else True)
        out["bundles"].append({"bundle_id": e["bundle_id"], "seq": e["seq"], "ok": ok,
                               "codes": [c for c, _ in codes], "detail": [d for _, d in codes],
                               "notes": [n for _, n in notes],
                               "note_codes": [c for c, _ in notes]})

    out["anchors"] = check_anchors(anchors_dir, out["repo"], entries) if anchors_dir else None
    return out


def check_anchors(anchors_dir, repo, entries):
    by_seq = {e["seq"]: e["entry_hash"] for e in entries}
    result = {"checked": 0, "findings": []}
    if not repo or not os.path.isdir(anchors_dir):
        return result
    for name in sorted(os.listdir(anchors_dir)):
        if not (name.startswith(slug(repo) + "-") and name.endswith(".json")):
            continue
        with open(os.path.join(anchors_dir, name), "rb") as f:
            try:
                a = json.loads(f.read())
            except ValueError:
                continue
        result["checked"] += 1
        if a.get("seq") not in by_seq:
            result["findings"].append({"code": "anchor.truncated", "seq": a.get("seq"),
                                       "detail": f"anchored at seq {a.get('seq')}, which the ledger no longer has"})
        elif by_seq[a["seq"]] != a.get("entry_hash"):
            result["findings"].append({"code": "anchor.rewritten", "seq": a["seq"],
                                       "detail": f"seq {a['seq']} has a different hash from the one observed"})
    return result


def cmd_verify(a):
    pubkey = open(a.pubkey).read() if a.pubkey else None
    ledger_dir = os.path.join(a.evidence_dir, "ledger")
    ledgers = ([os.path.join(ledger_dir, slug(a.repo) + ".jsonl")] if a.repo else
               sorted(os.path.join(ledger_dir, n) for n in os.listdir(ledger_dir) if n.endswith(".jsonl"))
               if os.path.isdir(ledger_dir) else [])
    ledgers = [p for p in ledgers if os.path.isfile(p)]
    if not ledgers:
        print("no ledgers found", file=sys.stderr)
        return 2
    reports = [verify_ledger(a.evidence_dir, p, pubkey, a.anchors) for p in ledgers]
    ok = all(not r["findings"] and all(b["ok"] is True for b in r["bundles"])
             and not (r["anchors"] or {}).get("findings") for r in reports)
    if a.json:
        print(json.dumps({"ok": ok, "replayed": False, "ledgers": reports}, indent=2))
        return 0 if ok else 1
    for r in reports:
        print(f"{r['ledger']}: {r['entries']} entries, chain "
              f"{'OK' if not r['findings'] else 'BROKEN'}, head {r['head'][:16]}")
        for f in r["findings"]:
            print(f"  ! {f['code']} at seq {f['seq']} {f['detail']}")
        anchors = r["anchors"]
        if anchors is None:
            print("  ? anchors NOT CHECKED (no --anchors): the chain and the signatures are checks "
                  "this evidence makes about itself")
        else:
            for f in anchors["findings"]:
                print(f"  FAIL {f['code']}: {f['detail']}")
            if not anchors["findings"]:
                print(f"  OK   {anchors['checked']} anchor(s) agree")
        for b in r["bundles"]:
            mark = {True: "OK  ", False: "FAIL", None: "?   "}[b["ok"]]
            print(f"  {mark} {b['bundle_id']}")
            for code, detail in zip(b["codes"], b["detail"]):
                print(f"       ! {code}: {detail}")
            for note in b["notes"]:
                print(f"       - {note}")
    print("\ndecisions NOT replayed: re-running the policy needs the policy engine "
          "(osfi-gate verify --policy-dir)")
    return 0 if ok else 1


# --------------------------------------------------------------------------- samples (§8)

SAMPLE_TAG = "osfi-gate/sample/v1"
DRAND_CHAIN = "8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce"
DRAND_RELAYS = ("https://api.drand.sh", "https://drand.cloudflare.com")


def rank_key(seed, entry_hash_hex):
    return sha256_hex((SAMPLE_TAG + "\n" + seed + "\n" + entry_hash_hex).encode("utf-8"))


def seed_of(src):
    if src["source"] == "auditor":
        return "auditor:" + src["seed"]
    return f"drand:{src['chain_hash']}:{src['round']}:{src['randomness']}"


def _utc(stamp):
    return datetime.strptime(stamp.replace("Z", ""), "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc)


def _get_json(url):
    req = urllib.request.Request(url, headers={"User-Agent": "osfi_verify"})
    with urllib.request.urlopen(req, timeout=20) as r:
        return json.loads(r.read().decode())


def _locate(dirs, repos):
    found = {}
    for d in dirs:
        ledger_dir = os.path.join(d, "ledger")
        for repo in repos:
            if os.path.isfile(os.path.join(ledger_dir, slug(repo) + ".jsonl")):
                if repo in found and found[repo] != d:
                    raise ValueError(f"{repo} has a ledger in both {found[repo]} and {d}")
                found[repo] = d
    return found


def sample_problems(doc, dirs, get=_get_json):
    if doc.get("version") != SAMPLE_TAG:
        return [f"this is a {doc.get('version')!r} sample; this verifier draws {SAMPLE_TAG}"]
    repos = doc["filters"]["repos"]
    try:
        where = _locate(dirs, repos)
    except ValueError as ex:
        return [str(ex)]
    problems = [f"no ledger for {r}" for r in repos if r not in where]
    if problems:
        return problems
    ledgers = {}
    for repo in repos:
        entries, bad = read_ledger(os.path.join(where[repo], "ledger", slug(repo) + ".jsonl"))
        if bad:
            return [f"{repo}: {d}" for _, _, d in bad]
        prev = GENESIS
        for i, e in enumerate(entries, 1):
            if e["seq"] != i or e["prev_entry_hash"] != prev or entry_hash(e) != e["entry_hash"]:
                return [f"{repo}: the chain is broken at seq {e['seq']}"]
            prev = e["entry_hash"]
        head = doc["heads"][repo]
        at = next((e for e in entries if e["seq"] == head["seq"]), None)
        if at is None:
            return [f"{repo}: the ledger no longer reaches seq {head['seq']} -- it has been truncated"]
        if at["entry_hash"] != head["entry_hash"]:
            return [f"{repo}: seq {head['seq']} has a different hash -- the history has been rewritten"]
        ledgers[repo] = entries

    src = doc["randomness"]
    if src["source"] == "drand":
        answers = []
        for relay in DRAND_RELAYS:
            try:
                answers.append(get(f"{relay}/{DRAND_CHAIN}/public/{src['round']}"))
            except Exception:
                continue
        if not answers:
            return ["no drand relay answered, so the beacon could not be checked"]
        for r in answers:
            if r.get("randomness") != src["randomness"]:
                return [f"drand round {src['round']} has randomness {r.get('randomness')}, "
                        f"not the one the sample records"]
            if sha256_hex(bytes.fromhex(r["signature"])) != r["randomness"]:
                return [f"a relay's round {src['round']} randomness is not sha256 of its signature"]

    start = datetime.strptime(doc["period"]["from"], "%Y-%m-%d").replace(tzinfo=timezone.utc)
    end = datetime.strptime(doc["period"]["to"], "%Y-%m-%d").replace(tzinfo=timezone.utc) + timedelta(days=1)
    result, event = doc["filters"].get("result"), doc["filters"].get("event")
    population = [e for repo in sorted(ledgers) for e in ledgers[repo]
                  if e["seq"] <= doc["heads"][repo]["seq"] and start <= _utc(e["recorded_at"]) < end
                  and (not result or e.get("result") == result) and (not event or e.get("event") == event)]
    if len(population) != doc["population"]["total"]:
        problems.append(f"the population is {len(population)} entries, not the "
                        f"{doc['population']['total']} the sample records")
    seed = seed_of(src)
    ranked = sorted(population, key=lambda e: (rank_key(seed, e["entry_hash"]), e["entry_hash"]))
    got = [(e["repo"], e["seq"], e["entry_hash"]) for e in ranked[:doc["size"]]]
    want = [(s["repo"], s["seq"], s["entry_hash"]) for s in doc["selection"]]
    if got != want:
        problems.append("drawn again, the selection differs")
    return problems


def cmd_sample_check(a):
    if zipfile.is_zipfile(a.sample):
        with zipfile.ZipFile(a.sample) as z:
            doc = json.loads(z.read("sample.json"))
    else:
        with open(a.sample, "rb") as f:
            doc = json.loads(f.read())
    problems = sample_problems(doc, a.evidence_dir)
    if problems:
        print(f"{a.sample}: does NOT reproduce")
        for p in problems:
            print("  ! " + p)
        return 1
    print(f"{a.sample}: reproduces -- the same {len(doc['selection'])} of "
          f"{doc['population']['total']} runs")
    return 0


def main(argv=None):
    p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    sub = p.add_subparsers(dest="command", required=True)
    v = sub.add_parser("verify", help="verify the ledgers and bundles in an evidence directory")
    v.add_argument("--evidence-dir", required=True)
    v.add_argument("--repo", help="only this repository's ledger")
    v.add_argument("--pubkey", help="PEM public key for key-signed bundles")
    v.add_argument("--anchors", help="a directory of anchor files (docs/evidence-format.md §7)")
    v.add_argument("--json", action="store_true")
    v.set_defaults(func=cmd_verify)
    s = sub.add_parser("sample-check", help="draw an osfi-gate audit sample again")
    s.add_argument("sample", help="the sample zip, or its sample.json")
    s.add_argument("--evidence-dir", action="append", required=True)
    s.set_defaults(func=cmd_sample_check)
    a = p.parse_args(argv)
    return a.func(a)


if __name__ == "__main__":
    sys.exit(main())
