Karta.Auth.DKIM.Signer (Karta v1.0.0)

DKIM signing (RFC 6376 §5) with RSA-SHA256 and Ed25519-SHA256 (RFC 8463).

sign/3 takes the message as it will be sent and one or more signing specifications, and returns one DKIM-Signature field per specification, each independent of the others (so a message can carry an Ed25519 and an RSA signature, and receivers that know only RSA still verify one). sign_message/3 returns the message with the fields in front.

A specification is a keyword list:

  • :key - a Karta.Auth.DKIM.Key (RSA keys must have at least 2048 bits)
  • :domain - d=; :selector - s=
  • :identity - i= (optional; its domain must be d= or below it)
  • :canonicalization - {header, body}, each :simple or :relaxed; default {:relaxed, :relaxed}
  • :expire_after - seconds until x=; default none

Options for the call: :now (Unix seconds; default now).

What is signed (RFC 6376 §5.4, §8.15):

  • From, Reply-To, Subject, Date, To and Cc are oversigned: listed in h= once more than they occur, so none of them can be added later, even where the message has none.
  • Sender, Message-ID, MIME-Version, the Content-* fields, In-Reply-To, References and List-Unsubscribe(-Post) are signed and oversigned when present.
  • Resent-, the other List- fields and Feedback-ID are signed when present.
  • Trace and transport fields (Received, Return-Path, DKIM-Signature, ...) are never signed.
  • l= is never used: the whole body is signed, so nothing can be appended. t= is always set.

The message must have exactly one From field and only CRLF line endings (a bare CR or LF would be signed one way and transmitted another).

sign_fields/4 signs a message whose header fields and body hashes are already known: the body of a stored message is read in pieces by Karta.Auth.DKIM.BodyHash, so the body is never held whole, and the caller has already established CRLF line endings.

Summary

Types

Why a message could not be signed.

Functions

The body canonicalisations these specifications use, in order: what a message's body has to be hashed for once, however many signatures there are.

The h= list for a header block (exposed for tests and tools).

The DKIM-Signature fields for message, one per specification, in order.

The DKIM-Signature fields for a message whose header fields are parsed and whose body hashes are known: %{body_algorithm => sha256}, one for each body canonicalisation the specifications use (see Karta.Auth.DKIM.BodyHash).

The message with its DKIM-Signature fields prepended.

Types

error()

@type error() ::
  Karta.Mail.Headers.error()
  | :bare_line_ending
  | :no_from
  | :multiple_from
  | :key_too_small
  | :bad_domain
  | :bad_selector
  | :bad_identity
  | :bad_expiration
  | :bad_canonicalization
  | :no_signers

Why a message could not be signed.

Functions

body_algorithms(specs)

@spec body_algorithms([keyword()]) :: [atom()]

The body canonicalisations these specifications use, in order: what a message's body has to be hashed for once, however many signatures there are.

header_names(fields)

@spec header_names([Karta.Mail.Header.t()]) :: [String.t()]

The h= list for a header block (exposed for tests and tools).

sign(message, specs, opts \\ [])

@spec sign(binary(), [keyword()], keyword()) :: {:ok, [binary()]} | {:error, error()}

The DKIM-Signature fields for message, one per specification, in order.

sign_fields(fields, body_hashes, specs, opts \\ [])

@spec sign_fields(
  [Karta.Mail.Header.t()],
  %{required(atom()) => binary()},
  [keyword()],
  keyword()
) ::
  {:ok, [binary()]} | {:error, error()}

The DKIM-Signature fields for a message whose header fields are parsed and whose body hashes are known: %{body_algorithm => sha256}, one for each body canonicalisation the specifications use (see Karta.Auth.DKIM.BodyHash).

sign_message(message, specs, opts \\ [])

@spec sign_message(binary(), [keyword()], keyword()) ::
  {:ok, binary()} | {:error, error()}

The message with its DKIM-Signature fields prepended.