Countersign.Seal (Countersign v0.1.0)

Copy Markdown View Source

Seals a PDF so it proves by itself that it was not altered, and checks such a seal.

A seal is one PAdES signature (/SubFilter /ETSI.CAdES.detached) per key, each in its own incremental update after the original bytes, which stay untouched: Ed25519 first, then SLH-DSA (FIPS 205), whose security rests on hash functions alone. Each adds an invisible signature field on page 1 and a /Sig dictionary whose /Contents is a detached CMS SignedData carrying the signer's certificate and the signed attributes content-type, message-digest and signing-certificate-v2.

  • Ed25519: id-Ed25519 (1.3.101.112) with SHA-512, as RFC 8419 requires.
  • SLH-DSA: SLH-DSA-SHA2-192s (2.16.840.1.101.3.4.3.22, NIST category 3) by default, or any of the twelve parameter sets through :slh_dsa_variant; SHA-512 for the 192 and 256 sets and SHA-256 for the 128 sets, as RFC 9814 4 requires of signed attributes.

No RSA, ECDSA or lattice-based code path exists, and the seal depends on OTP's :crypto and :public_key alone. verify/1 is the verifier of record for both signatures: today's PDF readers verify neither (poppler's NSS and GnuPG backends verify no Ed25519 CMS, and nothing reads SLH-DSA), and OpenSSL 3.5's cms -verify checks both.

Keys are passed in, never read here: Countersign.Seal.Cert.from_pem/2 turns the PEM files mix countersign.gen.seal_keys writes into the pairs seal/3 takes.

Summary

Types

A seal algorithm: :ed25519, or {:slh_dsa, variant} with one of the twelve SLH-DSA parameter sets (:slh_dsa_sha2_192s by default).

One signature verify/1 checked: certificate is the signer's DER, for a host to compare with the certificate it sealed with (or pass as :trusted); the subject alone proves nothing, since anyone can make a self-signed certificate with any name.

A certificate's DER and the matching private key, as :crypto holds it.

Functions

pdf followed by one incremental update per key in keys, Ed25519 first: {:ok, sealed}, whose prefix is pdf byte for byte.

Every Ed25519 and SLH-DSA signature in pdf's signature fields, in the order they were added, when each byte range's digest equals its message-digest attribute, the certificate is the one its signing-certificate-v2 attribute names, and the signature verifies under that certificate's key: {:ok, entries}. Only the last signature can cover the whole file, and only if nothing was appended after it. A signature by another algorithm (an RSA or ECDSA signature the PDF already carried) is not a seal and is left out.

Types

alg()

@type alg() :: :ed25519 | {:slh_dsa, atom()}

A seal algorithm: :ed25519, or {:slh_dsa, variant} with one of the twelve SLH-DSA parameter sets (:slh_dsa_sha2_192s by default).

entry()

@type entry() :: %{
  alg: alg(),
  subject: String.t(),
  certificate: binary(),
  signed_at: DateTime.t() | nil,
  covers_whole_file: boolean()
}

One signature verify/1 checked: certificate is the signer's DER, for a host to compare with the certificate it sealed with (or pass as :trusted); the subject alone proves nothing, since anyone can make a self-signed certificate with any name.

error()

@type error() ::
  :not_a_pdf
  | :encrypted
  | :xref_stream
  | :no_keys
  | :contents_overflow
  | {:key_mismatch, alg()}
  | {:unsupported, atom()}

key_pair()

@type key_pair() :: {cert_der :: binary(), private_key :: binary()}

A certificate's DER and the matching private key, as :crypto holds it.

keys()

@type keys() :: %{optional(:ed25519) => key_pair(), optional(:slh_dsa) => key_pair()}

verify_error()

@type verify_error() ::
  :malformed
  | {:digest_mismatch | :bad_signature | :certificate_mismatch | :untrusted,
     alg()}

Functions

seal(pdf, keys, opts \\ [])

@spec seal(binary(), keys(), keyword()) :: {:ok, binary()} | {:error, error()}

pdf followed by one incremental update per key in keys, Ed25519 first: {:ok, sealed}, whose prefix is pdf byte for byte.

Options

  • :slh_dsa_variant - the SLH-DSA parameter set (default :slh_dsa_sha2_192s); its certificate must be of that set
  • :signed_at - the DateTime written as each signature's /M (default: now)
  • :placeholders - %{ed25519: bytes, slh_dsa: bytes} overriding the /Contents each reserves
  • :supports - a zero-arity function answering :crypto.supports(:public_keys) (for tests)

Refusals: :not_a_pdf, :encrypted, :xref_stream (classic tables only), :no_keys, {:key_mismatch, alg} when a private key does not match its certificate, {:unsupported, variant} when this OTP cannot sign with the SLH-DSA set (never a quiet fall back to Ed25519 alone), and :contents_overflow when a signature outgrows its placeholder.

verify(pdf, opts \\ [])

@spec verify(binary(), keyword()) :: {:ok, [entry()]} | {:error, verify_error()}

Every Ed25519 and SLH-DSA signature in pdf's signature fields, in the order they were added, when each byte range's digest equals its message-digest attribute, the certificate is the one its signing-certificate-v2 attribute names, and the signature verifies under that certificate's key: {:ok, entries}. Only the last signature can cover the whole file, and only if nothing was appended after it. A signature by another algorithm (an RSA or ECDSA signature the PDF already carried) is not a seal and is left out.

Options

  • :trusted - certificate DERs; a signature by any other certificate is {:error, {:untrusted, alg}}. Without it the caller must compare each entry's certificate itself.

{:error, {:digest_mismatch, alg}} when the signed bytes changed, {:error, {:bad_signature, alg}} or {:error, {:certificate_mismatch, alg}} for a signature that does not hold, and {:error, :malformed} for a file or a signature that cannot be read.