#!/usr/bin/env python3 """Offline Proofmark PDF check. Requires Python 3 and Poppler's pdfdetach. Usage: python3 verify-proofmark.py agreement.pdf [--expected-sha256 HEX] """ import argparse import hashlib import json from pathlib import Path import re import subprocess import tempfile from urllib.parse import unquote def sha256(path): digest = hashlib.sha256() with path.open("rb") as stream: for chunk in iter(lambda: stream.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def attachment(pdf, name, destination): result = subprocess.run( ["pdfdetach", "-savefile", name, "-o", str(destination), str(pdf)], capture_output=True, timeout=30, ) if result.returncode == 0 and destination.is_file(): return destination return None def studio_source_hash(content): prefix = "\n\n"): return None studio = json.loads(unquote(content[start + len(prefix):-4])) if studio.get("version") == 1 and isinstance(studio.get("fields"), list): return studio.get("sourcePdfSha256") return None def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("pdf", type=Path) parser.add_argument("--expected-sha256", type=str.lower, help="A trusted fingerprint for this exact PDF") args = parser.parse_args() if args.expected_sha256 and not re.fullmatch(r"[0-9a-f]{64}", args.expected_sha256): parser.error("expected SHA-256 must be 64 hexadecimal characters") pdf = args.pdf.resolve() if not pdf.is_file(): parser.error("choose an existing PDF file") file_hash = sha256(pdf) file_matches = file_hash == args.expected_sha256 if args.expected_sha256 else None report = {"pdfSha256": file_hash, "trustedFileHashMatches": file_matches, "originalAgreementSha256": None, "originalPdfSha256": None} failed = file_matches is False try: with tempfile.TemporaryDirectory() as directory: path = Path(directory) record = attachment(pdf, "proofmark-signing-record.json", path / "record.json") if record: data = json.loads(record.read_text(encoding="utf-8")) if data.get("version") not in (1, 2): raise ValueError("unknown Proofmark signing record version") document = data["document"] content = document["canonicalContent"] content_hash = document["contentHash"] source_hash = document.get("sourcePdfSha256") report["originalAgreementSha256"] = content_hash report["embeddedAgreementMatches"] = hashlib.sha256(content.encode("utf-8")).hexdigest() == content_hash failed |= not report["embeddedAgreementMatches"] if source_hash is not None: original = attachment(pdf, "original-agreement.pdf", path / "original.pdf") report["originalPdfSha256"] = source_hash report["originalPdfMatches"] = bool( original and studio_source_hash(content) == source_hash and sha256(original) == source_hash ) failed |= not report["originalPdfMatches"] else: report["embeddedSigningRecord"] = "unavailable" except FileNotFoundError: parser.error("install Poppler to check embedded records with pdfdetach") except (ValueError, TypeError, KeyError, AttributeError, subprocess.TimeoutExpired) as error: report["error"] = f"Could not read the embedded record: {error}" failed = True print(json.dumps(report, indent=2)) return 1 if failed else 0 if __name__ == "__main__": raise SystemExit(main())