#!/usr/bin/env python3 """Verify this Certvas data pack against its own evidence bundle. Standalone; no network. python VERIFY.py # run it from anywhere; the pack is this file's directory Exit code 0 means every shipped file hashes to the value recorded in MANIFEST.json, and those recorded values hash to the bundle digest printed below. WHAT THIS PROVES, AND WHAT IT DOES NOT. It proves the pack is INTERNALLY consistent - nothing was truncated, re-encoded or swapped after the manifest was written. It is NOT a signature: whoever could alter these files could also recompute the digests. It becomes evidence of ORIGIN only when you compare the bundle digest below against the value Certvas gave you by another route - the release page, your delivery agreement, or an email from mandase@certvas.com. If those two strings match, this is the pack Certvas built. Questions: mandase@certvas.com """ import hashlib import json import pathlib import sys SPEC = "certvas-evidence-bundle/1" PACK = pathlib.Path(__file__).resolve().parent def digest(path): h = hashlib.sha256() n = 0 with open(path, "rb") as fh: while True: chunk = fh.read(1 << 20) if not chunk: break h.update(chunk) n += len(chunk) return h.hexdigest(), n def canonical(files): """Spec line, blank line, then path / sha256 / bytes for each path in ascending order.""" parts = [SPEC, ""] for path in sorted(files): rec = files[path] parts += [path, str(rec["sha256"]), str(rec["bytes"]), ""] return ("\n".join(parts) + "\n").encode("utf-8") def main(): manifest_path = PACK / "MANIFEST.json" if not manifest_path.exists(): print("FAIL: no MANIFEST.json beside this script.") return 2 manifest = json.loads(manifest_path.read_text(encoding="utf-8")) block = manifest.get("evidence") if not block: print("FAIL: MANIFEST.json carries no evidence bundle.") return 2 if block.get("spec") != SPEC: print("FAIL: bundle spec is %r, this script verifies %r." % (block.get("spec"), SPEC)) return 2 ignored = set(block.get("ignored") or ["MANIFEST.json"]) claimed = block.get("files") or {} on_disk = {} for p in sorted(PACK.rglob("*")): if p.is_file() and p.name not in ignored: h, n = digest(p) on_disk[p.relative_to(PACK).as_posix()] = {"sha256": h, "bytes": n} problems = [] for path in sorted(set(claimed) - set(on_disk)): problems.append("MISSING %s (recorded in the manifest, not present)" % path) for path in sorted(set(on_disk) - set(claimed)): problems.append("UNLISTED %s (present, not recorded in the manifest)" % path) for path in sorted(set(claimed) & set(on_disk)): if claimed[path].get("sha256") != on_disk[path]["sha256"]: problems.append( "ALTERED %s (recorded %s..., found %s...)" % (path, str(claimed[path].get("sha256"))[:16], on_disk[path]["sha256"][:16]) ) recomputed = hashlib.sha256(canonical(claimed)).hexdigest() if recomputed != block.get("bundle_sha256"): problems.append( "BUNDLE manifest says %s..., its own file digests hash to %s..." % (str(block.get("bundle_sha256"))[:16], recomputed[:16]) ) print("Certvas evidence bundle - %s" % PACK.name) print(" files checked : %d" % len(on_disk)) print(" bundle sha256 : %s" % block.get("bundle_sha256")) print("") if problems: print("FAIL - %d problem(s):" % len(problems)) for line in problems: print(" " + line) print("") print("Do not use this copy. Re-download it, or contact mandase@certvas.com.") return 1 print("PASS - every shipped file matches the manifest, and the manifest matches its digest.") print("") print("This proves the pack is internally consistent. To prove it is the pack CERTVAS built,") print("compare the bundle sha256 above with the value published for this release.") return 0 if __name__ == "__main__": sys.exit(main())