system-design-primer-skill 1.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/README.md +114 -60
  2. package/SKILL.md +437 -93
  3. package/VERSION +1 -1
  4. package/evals/README.md +1022 -0
  5. package/evals/system-design-cases.json +414 -0
  6. package/package.json +46 -4
  7. package/references/03-data-and-storage.md +8 -2
  8. package/references/04-caching.md +3 -3
  9. package/references/05-async-and-messaging.md +4 -0
  10. package/references/06-distributed-systems.md +2 -2
  11. package/references/07-reliability-and-resilience.md +7 -3
  12. package/references/08-api-security-observability.md +4 -0
  13. package/references/10-workflows-and-checklists.md +1 -1
  14. package/references/13-architecture-intelligence.md +91 -0
  15. package/references/14-api-and-data-contracts.md +80 -0
  16. package/references/15-failure-analysis.md +88 -0
  17. package/references/16-multi-agent-architecture.md +76 -0
  18. package/references/17-agent-harness-and-runtime.md +87 -0
  19. package/references/18-mcp-architecture.md +67 -0
  20. package/references/19-agent-security-and-permissions.md +90 -0
  21. package/references/20-agent-state-and-memory.md +76 -0
  22. package/references/21-agent-evaluation-and-observability.md +90 -0
  23. package/references/22-kubernetes-and-container-platforms.md +29 -0
  24. package/references/23-service-mesh-and-traffic-management.md +21 -0
  25. package/references/24-multi-region-and-disaster-recovery.md +28 -0
  26. package/references/25-platform-engineering-and-developer-experience.md +26 -0
  27. package/references/26-finops-and-cloud-cost.md +29 -0
  28. package/references/27-production-observability-and-slo.md +33 -0
  29. package/references/28-event-streaming-and-kafka-pulsar.md +33 -0
  30. package/references/29-event-sourcing-and-cqrs.md +23 -0
  31. package/references/30-change-data-capture-and-debezium.md +27 -0
  32. package/references/31-data-lakehouse-and-stream-batch.md +24 -0
  33. package/references/32-workflow-orchestration-and-temporal.md +28 -0
  34. package/references/33-schema-registry-and-event-governance.md +22 -0
  35. package/references/34-postgresql-internals-and-performance.md +45 -0
  36. package/references/35-mysql-innodb-and-performance.md +29 -0
  37. package/references/36-sharding-and-distributed-sql.md +22 -0
  38. package/references/37-consensus-and-distributed-transactions.md +29 -0
  39. package/references/38-search-architecture-elasticsearch-opensearch.md +24 -0
  40. package/references/39-vector-databases-and-hybrid-retrieval.md +31 -0
  41. package/references/40-zero-downtime-database-migrations.md +27 -0
  42. package/references/41-api-gateway-and-edge-architecture.md +23 -0
  43. package/references/42-rest-api-design-and-lifecycle.md +23 -0
  44. package/references/43-graphql-architecture-and-governance.md +16 -0
  45. package/references/44-grpc-and-service-contracts.md +16 -0
  46. package/references/45-websocket-sse-and-realtime-systems.md +16 -0
  47. package/references/46-realtime-collaboration-ot-crdt.md +16 -0
  48. package/references/47-api-security-rate-limiting-and-abuse.md +16 -0
  49. package/references/48-oauth-oidc-and-passkeys.md +20 -0
  50. package/references/49-iam-rbac-abac-rebac.md +20 -0
  51. package/references/50-zero-trust-and-workload-identity.md +16 -0
  52. package/references/51-secrets-kms-pki-and-key-management.md +20 -0
  53. package/references/52-supply-chain-security-sbom-slsa.md +78 -0
  54. package/references/53-threat-modeling-stride-and-attack-trees.md +16 -0
  55. package/references/54-privacy-engineering-and-data-governance.md +20 -0
  56. package/references/55-opentelemetry-and-instrumentation.md +54 -0
  57. package/references/56-distributed-tracing-and-context-propagation.md +47 -0
  58. package/references/57-continuous-profiling-and-performance-diagnostics.md +45 -0
  59. package/references/58-load-stress-soak-testing-and-workload-modeling.md +44 -0
  60. package/references/59-capacity-planning-and-performance-modeling.md +38 -0
  61. package/references/60-chaos-engineering-and-resilience-experiments.md +45 -0
  62. package/references/61-incident-management-and-postmortems.md +38 -0
  63. package/references/62-dns-and-service-discovery.md +35 -0
  64. package/references/63-load-balancing-and-global-traffic-management.md +31 -0
  65. package/references/64-cdn-and-edge-caching.md +34 -0
  66. package/references/65-cloud-networking-and-connectivity.md +34 -0
  67. package/references/66-edge-computing-architecture.md +33 -0
  68. package/references/67-storage-infrastructure-and-storage-systems.md +34 -0
  69. package/references/68-tcp-quic-and-network-performance.md +27 -0
  70. package/references/69-task-routing-and-evidence.md +115 -0
  71. package/scripts/audit_chain.py +36 -0
  72. package/scripts/compare_reports.py +43 -0
  73. package/scripts/evaluate.py +475 -0
  74. package/scripts/evaluation_catalog.py +129 -0
  75. package/scripts/evaluation_fingerprints.py +20 -0
  76. package/scripts/evidence_archive.py +79 -0
  77. package/scripts/evidence_signing.py +98 -0
  78. package/scripts/evidence_timestamp.py +441 -0
  79. package/scripts/install.ps1 +12 -3
  80. package/scripts/install.sh +15 -8
  81. package/scripts/report_integrity.py +184 -0
  82. package/scripts/review_consensus.py +77 -0
  83. package/scripts/review_evidence.py +105 -0
  84. package/scripts/review_provenance.py +44 -0
  85. package/scripts/reviewer_calibration.py +215 -0
  86. package/scripts/rubric_compatibility.py +134 -0
  87. package/scripts/run_evals.py +130 -0
  88. package/scripts/run_manifest.py +179 -0
  89. package/scripts/statistical_analysis.py +308 -0
  90. package/scripts/tsa_attestation.py +709 -0
  91. package/scripts/tsa_validation_report.py +627 -0
  92. package/scripts/validate.py +356 -11
  93. package/scripts/verify_build_provenance.py +625 -0
  94. package/scripts/verify_tsa_ocsp.py +962 -0
  95. package/scripts/verify_tsa_revocation.py +782 -0
  96. package/scripts/windows_broker_protocol.py +274 -0
  97. package/scripts/windows_broker_transport.py +141 -0
  98. package/scripts/windows_broker_win32.py +713 -0
  99. package/templates/agent-evaluation-plan-template.md +57 -0
  100. package/templates/agent-security-review-template.md +46 -0
  101. package/templates/agent-system-design-template.md +74 -0
  102. package/templates/api-architecture-design-template.md +33 -0
  103. package/templates/api-security-and-rate-limit-review-template.md +29 -0
  104. package/templates/architecture-decision-matrix-template.md +48 -0
  105. package/templates/architecture-review-scorecard-template.md +36 -0
  106. package/templates/cdc-data-pipeline-template.md +41 -0
  107. package/templates/cdn-and-edge-strategy-template.md +36 -0
  108. package/templates/chaos-experiment-template.md +39 -0
  109. package/templates/cloud-cost-model-template.md +37 -0
  110. package/templates/database-architecture-review-template.md +33 -0
  111. package/templates/edge-computing-readiness-template.md +36 -0
  112. package/templates/event-sourcing-cqrs-template.md +41 -0
  113. package/templates/event-streaming-design-template.md +47 -0
  114. package/templates/failure-analysis-template.md +36 -0
  115. package/templates/identity-and-access-architecture-template.md +29 -0
  116. package/templates/incident-postmortem-template.md +39 -0
  117. package/templates/multi-region-disaster-recovery-template.md +43 -0
  118. package/templates/network-architecture-design-template.md +38 -0
  119. package/templates/observability-architecture-template.md +52 -0
  120. package/templates/performance-test-plan-template.md +52 -0
  121. package/templates/privacy-data-governance-template.md +24 -0
  122. package/templates/production-readiness-checklist-template.md +52 -0
  123. package/templates/realtime-system-design-template.md +28 -0
  124. package/templates/security-threat-model-template.md +26 -0
  125. package/templates/sharding-distributed-sql-design-template.md +34 -0
  126. package/templates/storage-architecture-design-template.md +35 -0
  127. package/templates/supply-chain-security-review-template.md +20 -0
  128. package/templates/zero-downtime-database-migration-template.md +32 -0
@@ -0,0 +1,79 @@
1
+ """Create and verify an integrity manifest for offline TSA validation evidence."""
2
+ from __future__ import annotations
3
+ import argparse, hashlib, json, re, sys
4
+ from datetime import datetime
5
+ from pathlib import Path, PurePosixPath
6
+ from typing import Any
7
+ SCHEMA_VERSION=1
8
+ HEX256=re.compile(r"^[0-9a-f]{64}$")
9
+ UTC=re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$")
10
+ def parse_utc(value: str) -> datetime:
11
+ if not isinstance(value,str) or not UTC.fullmatch(value): raise ValueError("captured_at must be an RFC 3339 UTC timestamp ending in Z")
12
+ try: return datetime.fromisoformat(value[:-1]+"+00:00")
13
+ except ValueError as exc: raise ValueError("captured_at is not a valid UTC timestamp") from exc
14
+ def safe_relative_path(value: str) -> PurePosixPath:
15
+ if not isinstance(value,str) or not value or "\\" in value: raise ValueError("evidence path must be a non-empty POSIX relative path")
16
+ path=PurePosixPath(value)
17
+ if path.is_absolute() or any(part in ("",".","..") for part in path.parts): raise ValueError("evidence path must not be absolute or traverse directories")
18
+ return path
19
+ def sha256_file(path: Path) -> str:
20
+ digest=hashlib.sha256()
21
+ with path.open("rb") as stream:
22
+ for chunk in iter(lambda:stream.read(1024*1024),b""): digest.update(chunk)
23
+ return digest.hexdigest()
24
+ def create_archive(evidence_dir: Path, output: Path, captured_at: str) -> dict[str,Any]:
25
+ parse_utc(captured_at); root=evidence_dir.resolve(strict=True)
26
+ if not root.is_dir(): raise ValueError("evidence directory must be a directory")
27
+ out=output.resolve()
28
+ if out==root or root in out.parents: raise ValueError("archive manifest must be outside the evidence directory")
29
+ entries=[]
30
+ for path in sorted(root.rglob("*")):
31
+ if path.is_symlink(): raise ValueError("symlinks are not allowed in evidence archives")
32
+ if path.is_dir(): continue
33
+ if not path.is_file(): raise ValueError("evidence archive contains a non-regular file")
34
+ rel=path.relative_to(root).as_posix(); safe_relative_path(rel)
35
+ entries.append({"path":rel,"size":path.stat().st_size,"sha256":sha256_file(path)})
36
+ if not entries: raise ValueError("evidence directory is empty")
37
+ value={"schema_version":SCHEMA_VERSION,"captured_at":captured_at,"entries":entries}
38
+ encoded=json.dumps(value,sort_keys=True,separators=(",",":"),ensure_ascii=False)+"\n"
39
+ with output.open("x",encoding="utf-8",newline="\n") as stream: stream.write(encoded)
40
+ return {"created":True,"entry_count":len(entries),"manifest_sha256":hashlib.sha256(encoded.encode()).hexdigest()}
41
+ def verify_archive(evidence_dir: Path, manifest_path: Path, expected_manifest_sha256: str) -> dict[str,Any]:
42
+ if not isinstance(expected_manifest_sha256,str) or not HEX256.fullmatch(expected_manifest_sha256): raise ValueError("expected manifest SHA-256 must be 64 lowercase hex characters")
43
+ raw=manifest_path.read_bytes(); actual=hashlib.sha256(raw).hexdigest()
44
+ if actual!=expected_manifest_sha256: raise ValueError("evidence archive manifest SHA-256 pin mismatch")
45
+ value=json.loads(raw.decode("utf-8"))
46
+ if not isinstance(value,dict) or set(value)!={"schema_version","captured_at","entries"}: raise ValueError("invalid evidence archive manifest fields")
47
+ if value["schema_version"]!=SCHEMA_VERSION: raise ValueError("unsupported evidence archive schema")
48
+ parse_utc(value["captured_at"]); entries=value["entries"]
49
+ if not isinstance(entries,list) or not entries: raise ValueError("evidence archive entries must be a non-empty array")
50
+ root=evidence_dir.resolve(strict=True)
51
+ if not root.is_dir(): raise ValueError("evidence directory must be a directory")
52
+ seen=set(); failures=[]
53
+ for entry in entries:
54
+ if not isinstance(entry,dict) or set(entry)!={"path","size","sha256"}: raise ValueError("invalid evidence archive entry")
55
+ rel=safe_relative_path(entry["path"]); key=rel.as_posix()
56
+ if key in seen: raise ValueError("duplicate evidence archive path")
57
+ seen.add(key)
58
+ if isinstance(entry["size"],bool) or not isinstance(entry["size"],int) or entry["size"]<0: raise ValueError("evidence file size must be a non-negative integer")
59
+ if not isinstance(entry["sha256"],str) or not HEX256.fullmatch(entry["sha256"]): raise ValueError("invalid evidence file SHA-256")
60
+ target=root.joinpath(*rel.parts)
61
+ if target.is_symlink() or not target.is_file(): failures.append({"path":key,"reason":"missing_or_non_regular"}); continue
62
+ if root not in target.resolve().parents: raise ValueError("evidence path escapes archive directory")
63
+ if target.stat().st_size!=entry["size"] or sha256_file(target)!=entry["sha256"]: failures.append({"path":key,"reason":"size_or_digest_mismatch"})
64
+ actual_files=set()
65
+ for path in root.rglob("*"):
66
+ if path.is_symlink(): raise ValueError("symlinks are not allowed in evidence archives")
67
+ if path.is_file(): actual_files.add(path.relative_to(root).as_posix())
68
+ failures.extend({"path":p,"reason":"unlisted_file"} for p in sorted(actual_files-seen))
69
+ if failures: raise ValueError("evidence archive verification failed: "+json.dumps(failures,sort_keys=True))
70
+ return {"verified":True,"entry_count":len(entries),"captured_at":value["captured_at"],"manifest_sha256":actual,"integrity_only":True,"note":"Hashes prove byte integrity against the pinned manifest; they do not validate CRL/OCSP signatures or status semantics."}
71
+ def main() -> int:
72
+ parser=argparse.ArgumentParser(description=__doc__); sub=parser.add_subparsers(dest="command",required=True)
73
+ create=sub.add_parser("create"); create.add_argument("--evidence-dir",type=Path,required=True); create.add_argument("--output",type=Path,required=True); create.add_argument("--captured-at",required=True)
74
+ verify=sub.add_parser("verify"); verify.add_argument("--evidence-dir",type=Path,required=True); verify.add_argument("--manifest",type=Path,required=True); verify.add_argument("--manifest-sha256",required=True)
75
+ args=parser.parse_args()
76
+ try: result=create_archive(args.evidence_dir,args.output,args.captured_at) if args.command=="create" else verify_archive(args.evidence_dir,args.manifest,args.manifest_sha256)
77
+ except (OSError,UnicodeError,json.JSONDecodeError,ValueError,TypeError) as exc: print("ERROR: "+str(exc),file=sys.stderr); return 1
78
+ print(json.dumps(result,sort_keys=True,indent=2)); return 0
79
+ if __name__=="__main__": raise SystemExit(main())
@@ -0,0 +1,98 @@
1
+ """Ed25519 signatures and current trust-state evaluation for evidence roots."""
2
+ from __future__ import annotations
3
+ import argparse, base64, hashlib, json, re, sys
4
+ from datetime import datetime, timezone
5
+ from pathlib import Path
6
+ from typing import Any
7
+ from cryptography.hazmat.primitives import serialization
8
+ from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey
9
+ SCHEMA_VERSION=1
10
+ DOMAIN=b"system-design-primer-review-evidence-signature-v1\0"
11
+ KEY_ID_RE=re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$")
12
+ SIGNATURE_FIELDS={"schema_version","algorithm","key_id","signed_at","evidence_root","signature"}
13
+ STORE_FIELDS={"schema_version","keys"}
14
+ KEY_FIELDS={"public_key_pem","status","not_before","not_after","revoked_at"}
15
+ def canonical_json(value: Any) -> bytes:
16
+ return json.dumps(value,ensure_ascii=False,sort_keys=True,separators=(",",":")).encode("utf-8")
17
+ def trust_store_sha256(path: Path) -> str:
18
+ return hashlib.sha256(path.read_bytes()).hexdigest()
19
+ def parse_utc(value: Any,field: str) -> datetime:
20
+ if not isinstance(value,str) or not value.endswith("Z"): raise ValueError(field+" must be a UTC RFC3339 timestamp ending in Z")
21
+ try: parsed=datetime.fromisoformat(value[:-1]+"+00:00")
22
+ except ValueError as exc: raise ValueError("invalid "+field+" timestamp") from exc
23
+ if parsed.utcoffset().total_seconds()!=0: raise ValueError(field+" must be UTC")
24
+ return parsed
25
+ def load_report(path: Path) -> dict[str,Any]:
26
+ report=json.loads(path.read_text(encoding="utf-8"))
27
+ if not isinstance(report,dict): raise ValueError("report must be a JSON object")
28
+ try: from .review_evidence import verify_report_evidence
29
+ except ImportError: from review_evidence import verify_report_evidence
30
+ verify_report_evidence(report)
31
+ if not isinstance(report.get("review_evidence_root"),str): raise ValueError("missing evidence root")
32
+ return report
33
+ def payload(key_id: str,signed_at: str,root: str) -> bytes:
34
+ return DOMAIN+canonical_json({"schema_version":SCHEMA_VERSION,"algorithm":"Ed25519","key_id":key_id,"signed_at":signed_at,"evidence_root":root})
35
+ def sign_report(report_path: Path,private_path: Path,key_id: str) -> dict[str,Any]:
36
+ if not KEY_ID_RE.fullmatch(key_id): raise ValueError("invalid key_id")
37
+ report=load_report(report_path); key=serialization.load_pem_private_key(private_path.read_bytes(),password=None)
38
+ if not isinstance(key,Ed25519PrivateKey): raise ValueError("private key must be an unencrypted Ed25519 PEM key")
39
+ signed_at=datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00","Z"); root=report["review_evidence_root"]
40
+ signature=key.sign(payload(key_id,signed_at,root))
41
+ return {"schema_version":SCHEMA_VERSION,"algorithm":"Ed25519","key_id":key_id,"signed_at":signed_at,"evidence_root":root,"signature":base64.b64encode(signature).decode("ascii")}
42
+ def verify_signature(report_path: Path,signature_path: Path,trust_path: Path,expected_trust_sha256: str) -> dict[str,Any]:
43
+ report=load_report(report_path); envelope=json.loads(signature_path.read_text(encoding="utf-8"))
44
+ if not isinstance(envelope,dict) or set(envelope)!=SIGNATURE_FIELDS or envelope.get("schema_version")!=SCHEMA_VERSION or envelope.get("algorithm")!="Ed25519": raise ValueError("invalid signature envelope schema")
45
+ key_id=envelope.get("key_id"); signed_at=envelope.get("signed_at"); root=envelope.get("evidence_root")
46
+ if not isinstance(key_id,str) or not KEY_ID_RE.fullmatch(key_id): raise ValueError("invalid key_id")
47
+ signed_time=parse_utc(signed_at,"signed_at")
48
+ if root!=report.get("review_evidence_root"): raise ValueError("signature evidence root does not match report")
49
+ if not re.fullmatch(r"[0-9a-f]{64}",expected_trust_sha256): raise ValueError("expected trust-store SHA-256 must be 64 lowercase hex characters")
50
+ actual_trust_sha256=trust_store_sha256(trust_path)
51
+ if actual_trust_sha256!=expected_trust_sha256: raise ValueError("trust-store SHA-256 pin mismatch")
52
+ store=json.loads(trust_path.read_text(encoding="utf-8"))
53
+ if not isinstance(store,dict) or set(store)!=STORE_FIELDS or store.get("schema_version")!=2 or not isinstance(store.get("keys"),dict): raise ValueError("invalid trust store schema v2")
54
+ entry=store["keys"].get(key_id)
55
+ if not isinstance(entry,dict) or set(entry)!=KEY_FIELDS: raise ValueError("unknown or malformed trust-store key")
56
+ if entry["status"] not in {"active","revoked","disabled"}: raise ValueError("invalid trust-store key status")
57
+ not_before=parse_utc(entry["not_before"],"not_before"); not_after=parse_utc(entry["not_after"],"not_after")
58
+ if not_before>=not_after: raise ValueError("key validity interval must have not_before < not_after")
59
+ if entry["status"]=="revoked" and entry["revoked_at"] is None:
60
+ raise ValueError("revoked trust-store keys must declare revoked_at")
61
+ revoked_at=None if entry["revoked_at"] is None else parse_utc(entry["revoked_at"],"revoked_at")
62
+ if not_before>signed_time or signed_time>not_after: raise ValueError("signature timestamp claim is outside key validity interval")
63
+ if revoked_at is not None and signed_time>=revoked_at: raise ValueError("signature timestamp claim is at or after key revocation effective time")
64
+ public=serialization.load_pem_public_key(entry["public_key_pem"].encode("ascii"))
65
+ if not isinstance(public,Ed25519PublicKey): raise ValueError("trust-store key must be Ed25519")
66
+ try: public.verify(base64.b64decode(envelope["signature"],validate=True),payload(key_id,signed_at,root))
67
+ except Exception as exc: raise ValueError("Ed25519 signature verification failed") from exc
68
+ now=datetime.now(timezone.utc)
69
+ currently_authorized=entry["status"]=="active" and not_before<=now<=not_after and (revoked_at is None or now<revoked_at)
70
+ return {
71
+ "verified":True,
72
+ "verification_scope":{"ed25519_signature":"verified","signing_time_claim":"not_independently_verified"},
73
+ "historically_valid":None,
74
+ "historical_validity":"unproven",
75
+ "signing_time_provenance":"signer_asserted",
76
+ "currently_authorized":currently_authorized,
77
+ "key_status":entry["status"],
78
+ "key_id":key_id,
79
+ "signed_at":signed_at,
80
+ "not_before":entry["not_before"],
81
+ "not_after":entry["not_after"],
82
+ "revoked_at":entry["revoked_at"],
83
+ "trust_store_sha256":actual_trust_sha256,
84
+ "evidence_root":root,
85
+ }
86
+ def main() -> int:
87
+ p=argparse.ArgumentParser(description=__doc__); sub=p.add_subparsers(dest="command",required=True)
88
+ s=sub.add_parser("sign"); s.add_argument("--report",type=Path,required=True); s.add_argument("--private-key",type=Path,required=True); s.add_argument("--key-id",required=True); s.add_argument("--output",type=Path,required=True)
89
+ v=sub.add_parser("verify"); v.add_argument("--report",type=Path,required=True); v.add_argument("--signature",type=Path,required=True); v.add_argument("--trust-store",type=Path,required=True); v.add_argument("--trust-store-sha256",required=True)
90
+ a=p.parse_args()
91
+ try:
92
+ if a.command=="sign":
93
+ result=sign_report(a.report,a.private_key,a.key_id); a.output.write_text(json.dumps(result,sort_keys=True,indent=2)+"\n",encoding="utf-8")
94
+ result={"signed":True,"key_id":result["key_id"],"evidence_root":result["evidence_root"],"output":str(a.output)}
95
+ else: result=verify_signature(a.report,a.signature,a.trust_store,a.trust_store_sha256)
96
+ except (OSError,json.JSONDecodeError,ValueError,TypeError) as exc: print("ERROR: "+str(exc),file=sys.stderr); return 1
97
+ print(json.dumps(result,sort_keys=True,indent=2)); return 0
98
+ if __name__=="__main__": raise SystemExit(main())
@@ -0,0 +1,441 @@
1
+ """RFC 3161 timestamp requests and offline verification via OpenSSL."""
2
+ from __future__ import annotations
3
+ import argparse
4
+ import base64
5
+ import hashlib
6
+ import json
7
+ import os
8
+ import re
9
+ import shutil
10
+ import stat
11
+ import subprocess
12
+ import sys
13
+ import tempfile
14
+ from datetime import datetime, timezone
15
+ from pathlib import Path
16
+ from typing import Any
17
+
18
+ from cryptography import x509
19
+ from cryptography.hazmat.primitives import hashes
20
+ from cryptography.x509.oid import ExtendedKeyUsageOID, ExtensionOID
21
+
22
+ ENVELOPE_FIELDS = {"schema_version", "algorithm", "key_id", "signed_at", "evidence_root", "signature"}
23
+ ATTESTATION_FIELDS = {"schema_version", "algorithm", "key_id", "attested_at", "report_sha256", "verifier_bundle_sha256", "verifier_identity", "claims_sha256", "signature"}
24
+ HEX256 = re.compile(r"^[0-9a-f]{64}$")
25
+ MAX_TIMESTAMP_INPUT_BYTES = 16 * 1024 * 1024
26
+
27
+
28
+ def _read_stable_timestamp_input(source: Path) -> bytes:
29
+ """Read one regular evidence input and reject oversized or changing files."""
30
+ try:
31
+ with source.open("rb") as handle:
32
+ before = os.fstat(handle.fileno())
33
+ if not stat.S_ISREG(before.st_mode):
34
+ raise ValueError("timestamp evidence inputs must be regular files")
35
+ if before.st_size > MAX_TIMESTAMP_INPUT_BYTES:
36
+ raise ValueError("timestamp evidence input exceeds the 16 MiB limit")
37
+ data = handle.read(MAX_TIMESTAMP_INPUT_BYTES + 1)
38
+ after = os.fstat(handle.fileno())
39
+ except OSError:
40
+ raise
41
+ if len(data) > MAX_TIMESTAMP_INPUT_BYTES:
42
+ raise ValueError("timestamp evidence input exceeds the 16 MiB limit")
43
+ identity_before = (
44
+ before.st_dev, before.st_ino, before.st_size,
45
+ before.st_mtime_ns, before.st_ctime_ns,
46
+ )
47
+ identity_after = (
48
+ after.st_dev, after.st_ino, after.st_size,
49
+ after.st_mtime_ns, after.st_ctime_ns,
50
+ )
51
+ if identity_before != identity_after or len(data) != after.st_size:
52
+ raise ValueError("timestamp evidence input changed while being snapshotted: " + str(source))
53
+ return data
54
+
55
+
56
+ def load_signature(path: Path) -> tuple[dict[str, Any], bytes]:
57
+ value = json.loads(path.read_text(encoding="utf-8"))
58
+ if not isinstance(value, dict) or set(value) not in (ENVELOPE_FIELDS, ATTESTATION_FIELDS):
59
+ raise ValueError("invalid Ed25519 signature or TSA attestation envelope")
60
+ if value.get("schema_version") != 1 or value.get("algorithm") != "Ed25519":
61
+ raise ValueError("unsupported signature envelope")
62
+ try:
63
+ signature = base64.b64decode(value["signature"], validate=True)
64
+ except (TypeError, ValueError) as exc:
65
+ raise ValueError("invalid signature encoding") from exc
66
+ if len(signature) != 64:
67
+ raise ValueError("Ed25519 signature must contain exactly 64 bytes")
68
+ return value, signature
69
+
70
+
71
+ def openssl_path() -> str:
72
+ executable = shutil.which("openssl")
73
+ if not executable:
74
+ raise ValueError("OpenSSL CLI is required for RFC 3161 timestamp operations")
75
+ return executable
76
+
77
+
78
+ def run_openssl(args: list[str]) -> str:
79
+ result = subprocess.run(
80
+ [openssl_path(), *args], capture_output=True, text=True, check=False
81
+ )
82
+ if result.returncode:
83
+ detail = (result.stderr or result.stdout).strip()
84
+ raise ValueError("OpenSSL timestamp operation failed: " + detail[-2000:])
85
+ return result.stdout
86
+
87
+
88
+ def create_request(signature_path: Path, output_path: Path) -> dict[str, str]:
89
+ if output_path.exists():
90
+ raise ValueError("refusing to overwrite existing timestamp request")
91
+ _, signature = load_signature(signature_path)
92
+ digest = hashlib.sha256(signature).hexdigest()
93
+ run_openssl(["ts", "-query", "-digest", digest, "-sha256", "-cert", "-out", str(output_path)])
94
+ return {"request_created": True, "signature_sha256": digest, "output": str(output_path)}
95
+
96
+
97
+
98
+ def verification_time_bounds(value: datetime) -> tuple[int, ...]:
99
+ """Return exact floor/ceiling Unix seconds without float precision loss."""
100
+ if value.tzinfo is None or value.utcoffset() is None:
101
+ raise ValueError("timestamp verification time must be timezone-aware")
102
+ delta = value.astimezone(timezone.utc) - datetime(1970, 1, 1, tzinfo=timezone.utc)
103
+ floor_second = delta.days * 86400 + delta.seconds
104
+ return (floor_second, floor_second + 1) if delta.microseconds else (floor_second,)
105
+
106
+
107
+ def _parse_canonical_gen_time(value: str) -> tuple[datetime, bool]:
108
+ """Parse an ISO UTC genTime while retaining whether any fractional value is nonzero."""
109
+ match = re.fullmatch(
110
+ r"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d+))?Z",
111
+ value,
112
+ )
113
+ if not match:
114
+ raise ValueError("timestamp token genTime is not a supported UTC GeneralizedTime")
115
+ try:
116
+ whole_seconds = datetime.fromisoformat(match.group(1)).replace(tzinfo=timezone.utc)
117
+ except ValueError as exc:
118
+ raise ValueError("timestamp token genTime is not a supported UTC GeneralizedTime") from exc
119
+ fraction = match.group(2)
120
+ if fraction is not None and fraction.endswith("0"):
121
+ raise ValueError(
122
+ "timestamp token genTime fractional seconds must omit trailing zeros"
123
+ )
124
+ return whole_seconds, bool(fraction and any(digit != "0" for digit in fraction))
125
+
126
+
127
+ def verification_time_bounds_for_gen_time(value: str) -> tuple[int, ...]:
128
+ """Return exact floor/ceiling seconds for arbitrary-precision RFC 3161 genTime."""
129
+ whole_seconds, has_fraction = _parse_canonical_gen_time(value)
130
+ floor_second = verification_time_bounds(whole_seconds)[0]
131
+ return (floor_second, floor_second + 1) if has_fraction else (floor_second,)
132
+
133
+
134
+ def verification_time_within_validity(
135
+ value: datetime | str, not_before: datetime, not_after: datetime
136
+ ) -> bool:
137
+ """Require both integer-second verification bounds to lie within certificate validity."""
138
+ bounds = (
139
+ verification_time_bounds_for_gen_time(value)
140
+ if isinstance(value, str) else verification_time_bounds(value)
141
+ )
142
+ start = verification_time_bounds(not_before)[0]
143
+ end = verification_time_bounds(not_after)[0]
144
+ return all(start <= instant <= end for instant in bounds)
145
+
146
+
147
+ def parse_openssl_gen_time(value: str) -> str:
148
+ """Convert OpenSSL's display form without truncating arbitrary fractional precision."""
149
+ match = re.fullmatch(
150
+ r"([A-Za-z]{3} +\d{1,2} \d{2}:\d{2}:\d{2})(?:\.(\d+))? (\d{4}) GMT",
151
+ value.strip(),
152
+ )
153
+ if not match:
154
+ raise ValueError("timestamp token genTime is not a supported UTC GeneralizedTime")
155
+ clock, fraction, year = match.groups()
156
+ if fraction is not None and fraction.endswith("0"):
157
+ raise ValueError(
158
+ "timestamp token genTime fractional seconds must omit trailing zeros"
159
+ )
160
+ try:
161
+ parsed = datetime.strptime(f"{clock} {year} GMT", "%b %d %H:%M:%S %Y GMT")
162
+ except ValueError as exc:
163
+ raise ValueError("timestamp token genTime is not a supported UTC GeneralizedTime") from exc
164
+ canonical = parsed.strftime("%Y-%m-%dT%H:%M:%S")
165
+ return canonical + (f".{fraction}" if fraction is not None else "") + "Z"
166
+
167
+
168
+ def parse_accuracy(details: str) -> dict[str, int] | None:
169
+ """Parse Accuracy; None means absent, while omitted components in a present field are zero."""
170
+ matches = re.findall(r"^Accuracy:[ \t]*(.*?)[ \t]*$", details, re.I | re.M)
171
+ if not matches:
172
+ return None
173
+ if len(matches) != 1 or not matches[0]:
174
+ raise ValueError("timestamp token accuracy field is malformed")
175
+
176
+ result: dict[str, int] = {}
177
+ order = {"seconds": 0, "millis": 1, "micros": 2}
178
+ previous = -1
179
+ for component in matches[0].split(","):
180
+ parsed = re.fullmatch(
181
+ r"[ \t]*(\d+)[ \t]+(seconds?|millis|micros)[ \t]*",
182
+ component, re.I,
183
+ )
184
+ if not parsed:
185
+ raise ValueError("timestamp token accuracy field is malformed")
186
+ amount, unit = parsed.groups()
187
+ unit = unit.lower()
188
+ key = "seconds" if unit.startswith("second") else unit
189
+ if key in result:
190
+ raise ValueError("timestamp token contains duplicate accuracy components")
191
+ if order[key] <= previous:
192
+ raise ValueError("timestamp token accuracy components are out of order")
193
+ value = int(amount)
194
+ if key in ("millis", "micros") and not 1 <= value <= 999:
195
+ raise ValueError("timestamp token millis and micros accuracy must be between 1 and 999")
196
+ result[key] = value
197
+ previous = order[key]
198
+ # RFC 3161 assigns zero to omitted components within a present Accuracy field.
199
+ return {component: result.get(component, 0) for component in ("seconds", "millis", "micros")}
200
+
201
+
202
+ def inspect_tsa_signer_certificate(response_path: Path, gen_time: str) -> dict[str, str]:
203
+ """Extract the CMS SignerInfo certificate and bind its TSA profile to genTime."""
204
+ # Keep the original fractional digits: datetime only represents microseconds.
205
+ with tempfile.TemporaryDirectory(prefix="tsa-token-") as directory:
206
+ token_path = Path(directory) / "token.der"
207
+ signer_path = Path(directory) / "signer.pem"
208
+ content_path = Path(directory) / "content.der"
209
+ extract = subprocess.run(
210
+ [openssl_path(), "ts", "-reply", "-in", str(response_path),
211
+ "-out", str(token_path), "-token_out"],
212
+ capture_output=True, text=True, check=False,
213
+ )
214
+ if extract.returncode:
215
+ detail = (extract.stderr or extract.stdout).strip()
216
+ raise ValueError("could not extract verified timestamp token: " + detail[-2000:])
217
+ # CMS resolves the certificate using the actual SignerInfo issuer/serial
218
+ # or subjectKeyIdentifier. -noverify skips path validation only; ts -verify
219
+ # has already performed the timestamp chain check.
220
+ verify = subprocess.run(
221
+ [openssl_path(), "cms", "-verify", "-inform", "DER", "-in", str(token_path),
222
+ "-noverify", "-signer", str(signer_path), "-out", str(content_path)],
223
+ capture_output=True, text=True, check=False,
224
+ )
225
+ if verify.returncode or not signer_path.is_file():
226
+ detail = (verify.stderr or verify.stdout).strip()
227
+ raise ValueError("could not identify the actual CMS timestamp signer certificate: " + detail[-2000:])
228
+ try:
229
+ certificate = x509.load_pem_x509_certificate(signer_path.read_bytes())
230
+ except (OSError, ValueError) as exc:
231
+ raise ValueError("CMS timestamp signer certificate is not valid PEM X.509") from exc
232
+ try:
233
+ eku_ext = certificate.extensions.get_extension_for_oid(ExtensionOID.EXTENDED_KEY_USAGE)
234
+ except x509.ExtensionNotFound as exc:
235
+ raise ValueError("TSA signer certificate is missing timeStamping EKU") from exc
236
+ if not eku_ext.critical or list(eku_ext.value) != [ExtendedKeyUsageOID.TIME_STAMPING]:
237
+ raise ValueError("TSA signer certificate must have a critical, exclusive timeStamping EKU")
238
+ try:
239
+ key_usage_ext = certificate.extensions.get_extension_for_oid(ExtensionOID.KEY_USAGE)
240
+ except x509.ExtensionNotFound:
241
+ key_usage_ext = None
242
+ if key_usage_ext is not None:
243
+ # RFC 5280 §4.2.1.3 says KeyUsage SHOULD be critical when present;
244
+ # non-critical is still valid and must be processed by relying parties.
245
+ key_usage = key_usage_ext.value
246
+ permitted = key_usage.digital_signature or key_usage.content_commitment
247
+ prohibited = any((key_usage.key_encipherment, key_usage.data_encipherment,
248
+ key_usage.key_agreement, key_usage.key_cert_sign, key_usage.crl_sign))
249
+ if not permitted or prohibited:
250
+ raise ValueError("TSA signer KeyUsage must be limited to digitalSignature or contentCommitment")
251
+ # cryptography>=42 exposes timezone-aware UTC validity properties; prefer
252
+ # them over the deprecated naive-UTC accessors to avoid implicit timezone relabeling.
253
+ not_before = certificate.not_valid_before_utc
254
+ not_after = certificate.not_valid_after_utc
255
+ if not_before.tzinfo is None or not_after.tzinfo is None:
256
+ raise ValueError("TSA signer certificate validity must be timezone-aware")
257
+ if not verification_time_within_validity(gen_time, not_before, not_after):
258
+ raise ValueError("TSA signer certificate is not valid at RFC 3161 genTime")
259
+ return {
260
+ "tsa_signer_certificate_sha256": certificate.fingerprint(hashes.SHA256()).hex(),
261
+ "tsa_signer_serial_number": format(certificate.serial_number, "x"),
262
+ "tsa_signer_valid_at_gen_time": True,
263
+ "tsa_signer_eku": "critical:id-kp-timeStamping",
264
+ "tsa_signer_identity_source": "CMS SignerInfo certificate selected by OpenSSL cms -verify -signer",
265
+ }
266
+
267
+
268
+
269
+ def parse_ordering(details: str) -> bool:
270
+ """Parse RFC 3161 ordering; an absent DEFAULT FALSE field is false."""
271
+ matches = re.findall(r"^Ordering:[ \t]*(.*?)[ \t]*$", details, re.I | re.M)
272
+ if not matches:
273
+ return False
274
+ if len(matches) != 1 or matches[0].lower() not in {"yes", "no"}:
275
+ raise ValueError("timestamp token ordering field is malformed")
276
+ return matches[0].lower() == "yes"
277
+
278
+
279
+ def verify_timestamp(
280
+ signature_path: Path, request_path: Path, response_path: Path,
281
+ tsa_ca_path: Path, expected_ca_sha256: str, chain_path: Path | None = None,
282
+ ) -> dict[str, Any]:
283
+ """Verify one immutable snapshot of all RFC 3161 input files."""
284
+ if not HEX256.fullmatch(expected_ca_sha256):
285
+ raise ValueError("expected TSA CA SHA-256 must be 64 lowercase hex characters")
286
+ with tempfile.TemporaryDirectory(prefix="tsa-timestamp-inputs-") as directory:
287
+ snapshot_dir = Path(directory)
288
+
289
+ def snapshot(name: str, source: Path) -> Path:
290
+ destination = snapshot_dir / f"{name}{source.suffix}"
291
+ destination.write_bytes(_read_stable_timestamp_input(source))
292
+ return destination
293
+
294
+ ca_snapshot = snapshot("tsa-ca", tsa_ca_path)
295
+ if hashlib.sha256(ca_snapshot.read_bytes()).hexdigest() != expected_ca_sha256:
296
+ raise ValueError("TSA CA bundle SHA-256 pin mismatch")
297
+ signature_snapshot = snapshot("signature-envelope", signature_path)
298
+ request_snapshot = snapshot("timestamp-request", request_path)
299
+ response_snapshot = snapshot("timestamp-response", response_path)
300
+ chain_snapshot = snapshot("tsa-chain", chain_path) if chain_path is not None else None
301
+ return _verify_timestamp_snapshot(
302
+ signature_snapshot, request_snapshot, response_snapshot,
303
+ ca_snapshot, expected_ca_sha256, chain_snapshot,
304
+ )
305
+
306
+
307
+ def _verify_timestamp_snapshot(
308
+ signature_path: Path, request_path: Path, response_path: Path,
309
+ tsa_ca_path: Path, expected_ca_sha256: str, chain_path: Path | None = None,
310
+ ) -> dict[str, Any]:
311
+ """Verify RFC 3161 token binding and TSA path, not the wider evidence trust model.
312
+
313
+ The caller selects and pins the trust bundle. Revocation evidence, policy-OID
314
+ acceptability, the Ed25519 signature itself, and evidence-archive completeness
315
+ require separate policy and verification steps.
316
+ """
317
+ if not HEX256.fullmatch(expected_ca_sha256):
318
+ raise ValueError("expected TSA CA SHA-256 must be 64 lowercase hex characters")
319
+ actual_ca_sha256 = hashlib.sha256(tsa_ca_path.read_bytes()).hexdigest()
320
+ if actual_ca_sha256 != expected_ca_sha256:
321
+ raise ValueError("TSA CA bundle SHA-256 pin mismatch")
322
+ _, signature = load_signature(signature_path)
323
+ expected_imprint = hashlib.sha256(signature).hexdigest()
324
+ request_text = run_openssl(["ts", "-query", "-in", str(request_path), "-text"])
325
+ algorithm = re.search(r"Hash Algorithm:\s*([A-Za-z0-9-]+)", request_text, re.I)
326
+ imprint = re.search(r"Message data:\s*(?:0x)?([0-9a-fA-F]+)", request_text, re.I)
327
+ if not algorithm or algorithm.group(1).lower().replace("-", "") != "sha256":
328
+ raise ValueError("timestamp request must use SHA-256")
329
+ if not imprint or imprint.group(1).lower() != expected_imprint:
330
+ raise ValueError("timestamp request does not bind the supplied Ed25519 signature")
331
+ request_nonce = re.search(r"^Nonce:\s*(\S+)", request_text, re.I | re.M)
332
+ details = run_openssl(["ts", "-reply", "-in", str(response_path), "-text"])
333
+ gen_time = re.search(r"^Time(?: stamp)?\s*:\s*(.+)$", details, re.I | re.M)
334
+ if not gen_time:
335
+ raise ValueError("timestamp token did not expose a generation time for historical chain verification")
336
+ canonical_gen_time = parse_openssl_gen_time(gen_time.group(1))
337
+ verification_times = verification_time_bounds_for_gen_time(canonical_gen_time)
338
+ # OpenSSL ts does not expose -no-CApath/-no-CAstore. Supply explicit empty
339
+ # locations so only the caller-pinned CAfile can contribute trust anchors.
340
+ with tempfile.TemporaryDirectory(prefix="tsa-empty-trust-") as empty_trust:
341
+ empty_path = Path(empty_trust) / "ca-path"
342
+ empty_path.mkdir()
343
+ empty_store = Path(empty_trust) / "ca-store.pem"
344
+ empty_store.write_bytes(b"")
345
+ command = ["ts", "-verify", "-queryfile", str(request_path), "-in", str(response_path),
346
+ "-CAfile", str(tsa_ca_path), "-CApath", str(empty_path),
347
+ "-CAstore", str(empty_store), "-auth_level", "2", "-purpose", "timestampsign", "-x509_strict"]
348
+ if chain_path is not None:
349
+ command.extend(["-untrusted", str(chain_path)])
350
+ for verification_time in verification_times:
351
+ run_openssl([*command, "-attime", str(verification_time)])
352
+ gen_time = re.search(r"^Time(?: stamp)?\s*:\s*(.+)$", details, re.I | re.M)
353
+ policy = re.search(r"^Policy OID\s*:\s*(\S+)", details, re.I | re.M)
354
+ serial = re.search(r"^Serial number\s*:\s*(\S+)", details, re.I | re.M)
355
+ response_algorithm = re.search(r"^Hash Algorithm:\s*([A-Za-z0-9-]+)", details, re.I | re.M)
356
+ response_imprint = re.search(r"^Message data:\s*(?:0x)?([0-9a-fA-F]+)", details, re.I | re.M)
357
+ if not gen_time:
358
+ raise ValueError("verified timestamp token did not expose a generation time")
359
+ if not policy or not serial:
360
+ raise ValueError("verified timestamp token is missing its mandatory policy OID or serial number")
361
+ if not response_algorithm or response_algorithm.group(1).lower().replace("-", "") != "sha256":
362
+ raise ValueError("timestamp token message-imprint algorithm is not SHA-256")
363
+ if not response_imprint or response_imprint.group(1).lower() != expected_imprint:
364
+ raise ValueError("timestamp token message imprint does not bind the supplied Ed25519 signature")
365
+ canonical_gen_time = parse_openssl_gen_time(gen_time.group(1))
366
+ signer_evidence = inspect_tsa_signer_certificate(response_path, canonical_gen_time)
367
+ nonce = re.search(r"^Nonce:\s*(\S+)", details, re.I | re.M)
368
+ return {
369
+ "verified": True,
370
+ "verification_scope": {
371
+ "cms_signature": "verified",
372
+ "request_imprint": "verified",
373
+ "response_imprint": "verified",
374
+ "request_response_binding": "verified",
375
+ "nonce_binding": "verified" if request_nonce else "not_requested",
376
+ "tsa_pkix_chain_at_gen_time": "verified",
377
+ "tsa_signer_certificate_profile": "verified",
378
+ "tsa_signer_validity_at_gen_time": "verified",
379
+ },
380
+ "not_established": [
381
+ "The caller is responsible for deciding whether the pinned CA bundle and its trust anchors satisfy the relying party trust policy.",
382
+ "The TSA policy OID is reported but its acceptability for a particular use is not assessed.",
383
+ "This command does not retrieve or independently validate CRL or OCSP revocation evidence; use the separate revocation verification workflow.",
384
+ "The Ed25519 signature itself and the completeness or integrity of the wider evidence archive are not verified by timestamp-token verification.",
385
+ ],
386
+ "signature_sha256": expected_imprint,
387
+ "gen_time": parse_openssl_gen_time(gen_time.group(1)),
388
+ "policy_oid": policy.group(1),
389
+ "serial_number": serial.group(1),
390
+ "accuracy": parse_accuracy(details),
391
+ "accuracy_interpretation": "maximum time deviation around gen_time; reported separately from PKIX path validation",
392
+ "ordering": parse_ordering(details),
393
+ "ordering_interpretation": "when true, genTime values from this TSA are ordered; when false, compare time differences against both tokens' accuracies",
394
+ "nonce_present": nonce is not None,
395
+ "request_sha256": hashlib.sha256(request_path.read_bytes()).hexdigest(),
396
+ "response_sha256": hashlib.sha256(response_path.read_bytes()).hexdigest(),
397
+ "tsa_ca_sha256": actual_ca_sha256,
398
+ "tsa_chain_sha256": (
399
+ hashlib.sha256(chain_path.read_bytes()).hexdigest()
400
+ if chain_path is not None else None
401
+ ),
402
+ "tsa_chain_valid_at_gen_time": True,
403
+ "tsa_chain_validation_time": canonical_gen_time,
404
+ "tsa_chain_validation_scope": "PKIX path validation at the floor/ceiling Unix-second bounds of genTime; RFC 3161 Accuracy is reported separately and does not widen the -attime interval",
405
+ "tsa_chain_validation_policy": "OpenSSL ts -verify uses the caller-pinned -CAfile and explicit empty -CApath/-CAstore, -auth_level 2, -purpose timestampsign, and -x509_strict at the exact floor/ceiling Unix-second bound(s) of genTime; pathLenConstraint and Name Constraints enforced by OpenSSL",
406
+ **signer_evidence,
407
+ "verification": "OpenSSL RFC 3161 CMS signature, request/response imprint, nonce (when requested), and TSA chain verification at genTime",
408
+ }
409
+
410
+
411
+ def main() -> int:
412
+ parser = argparse.ArgumentParser(description=__doc__)
413
+ sub = parser.add_subparsers(dest="command", required=True)
414
+ request = sub.add_parser("request")
415
+ request.add_argument("--signature", type=Path, required=True)
416
+ request.add_argument("--output", type=Path, required=True)
417
+ verify = sub.add_parser("verify")
418
+ verify.add_argument("--signature", type=Path, required=True)
419
+ verify.add_argument("--request", type=Path, required=True)
420
+ verify.add_argument("--response", type=Path, required=True)
421
+ verify.add_argument("--tsa-ca", type=Path, required=True)
422
+ verify.add_argument("--tsa-ca-sha256", required=True)
423
+ verify.add_argument("--tsa-chain", type=Path)
424
+ args = parser.parse_args()
425
+ try:
426
+ if args.command == "request":
427
+ result = create_request(args.signature, args.output)
428
+ else:
429
+ result = verify_timestamp(
430
+ args.signature, args.request, args.response, args.tsa_ca,
431
+ args.tsa_ca_sha256, args.tsa_chain,
432
+ )
433
+ except (OSError, json.JSONDecodeError, ValueError, TypeError) as exc:
434
+ print("ERROR: " + str(exc), file=sys.stderr)
435
+ return 1
436
+ print(json.dumps(result, sort_keys=True, indent=2))
437
+ return 0
438
+
439
+
440
+ if __name__ == "__main__":
441
+ raise SystemExit(main())