evidence_seal/signing.py

118 lines · 4228 bytes

  1"""Optional ed25519 signing of manifests.
  2
  3Signing adds attribution — *who* sealed the evidence — on top of the integrity
  4the hashes already provide. It requires the ``cryptography`` package (install
  5``evidence-seal[sign]``); the core seal/verify path never imports this module.
  6
  7The signature covers the manifest's canonical bytes (the same bytes its id is
  8derived from), so a valid signature vouches for every file hash and all
  9metadata at once.
 10"""
 11
 12from __future__ import annotations
 13
 14from datetime import datetime, timezone
 15from pathlib import Path
 16
 17from .manifest import canonical_bytes
 18
 19
 20def _require_crypto():
 21    try:
 22        from cryptography.hazmat.primitives.asymmetric import ed25519
 23    except ImportError as exc:  # pragma: no cover - exercised via a clear message
 24        raise RuntimeError(
 25            "signing requires the 'cryptography' package — install evidence-seal[sign]"
 26        ) from exc
 27    return ed25519
 28
 29
 30def generate_keypair(private_path: str | Path, public_path: str | Path) -> None:
 31    """Write a new ed25519 keypair as PEM files (private key is unencrypted)."""
 32    ed25519 = _require_crypto()
 33    from cryptography.hazmat.primitives import serialization
 34
 35    private = ed25519.Ed25519PrivateKey.generate()
 36    Path(private_path).write_bytes(
 37        private.private_bytes(
 38            encoding=serialization.Encoding.PEM,
 39            format=serialization.PrivateFormat.PKCS8,
 40            encryption_algorithm=serialization.NoEncryption(),
 41        )
 42    )
 43    Path(public_path).write_bytes(
 44        private.public_key().public_bytes(
 45            encoding=serialization.Encoding.PEM,
 46            format=serialization.PublicFormat.SubjectPublicKeyInfo,
 47        )
 48    )
 49
 50
 51def _public_hex(public_key) -> str:
 52    from cryptography.hazmat.primitives import serialization
 53
 54    raw = public_key.public_bytes(
 55        encoding=serialization.Encoding.Raw,
 56        format=serialization.PublicFormat.Raw,
 57    )
 58    return raw.hex()
 59
 60
 61def sign_manifest(
 62    manifest: dict, private_key_path: str | Path, password: bytes | None = None
 63) -> dict:
 64    """Return a copy of *manifest* with a ``signature`` block attached.
 65
 66    *password* unlocks an encrypted PEM private key.
 67    """
 68    _require_crypto()
 69    from cryptography.hazmat.primitives import serialization
 70
 71    private = serialization.load_pem_private_key(
 72        Path(private_key_path).read_bytes(), password=password
 73    )
 74    signature = private.sign(canonical_bytes(manifest))
 75    signed = dict(manifest)
 76    signed["signature"] = {
 77        "algorithm": "ed25519",
 78        "public_key": _public_hex(private.public_key()),
 79        "value": signature.hex(),
 80        "signed_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
 81    }
 82    return signed
 83
 84
 85def verify_signature(manifest: dict, expected_public_key: str | None = None) -> tuple[bool, str]:
 86    """Verify a manifest's signature.
 87
 88    Returns ``(ok, message)``. If *expected_public_key* (hex) is given, the
 89    signer's key must also match it — otherwise a valid signature by *any* key
 90    would pass, which proves integrity but not identity.
 91    """
 92    ed25519 = _require_crypto()
 93
 94    block = manifest.get("signature")
 95    if not block:
 96        return False, "manifest is not signed"
 97    if block.get("algorithm") != "ed25519":
 98        return False, f"unsupported signature algorithm: {block.get('algorithm')}"
 99
100    signer_key = block.get("public_key", "")
101    if expected_public_key and signer_key != expected_public_key:
102        return False, "signer key does not match the expected public key"
103
104    try:
105        public = ed25519.Ed25519PublicKey.from_public_bytes(bytes.fromhex(signer_key))
106        public.verify(bytes.fromhex(block["value"]), canonical_bytes(manifest))
107    except Exception as exc:
108        return False, f"signature is invalid: {exc}"
109    return True, f"valid ed25519 signature by {signer_key[:16]}…"
110
111
112def load_public_hex(public_key_path: str | Path) -> str:
113    """Load a PEM public key and return its raw hex form (for --pubkey checks)."""
114    _require_crypto()
115    from cryptography.hazmat.primitives import serialization
116
117    public = serialization.load_pem_public_key(Path(public_key_path).read_bytes())
118    return _public_hex(public)