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

A DKIM signing key: RSA (2048, 3072 or 4096 bits) or Ed25519 (RFC 8463).

RSA keys under 2048 bits can be read (to inspect or replace an old key) but Karta.Auth.DKIM.Signer refuses to sign with them (RFC 8301 §3.2 recommends 2048 bits).

Summary

Types

t()

private and public are the :public_key RSA records for RSA keys, and the 32-byte seed and public key for Ed25519.

Functions

The DKIM key record (RFC 6376 §3.6.1, RFC 8463 §4): v=DKIM1; k=rsa; p=<SubjectPublicKeyInfo> or v=DKIM1; k=ed25519; p=<the 32-octet key>, base64.

dns_record/1 cut into character-strings of at most 255 octets, for a TXT record.

Reads a PEM private key: PKCS #8 (RSA or Ed25519) or PKCS #1 (-----BEGIN RSA PRIVATE KEY-----). RSA keys must have at least 1024 bits. Anything else is {:error, :invalid_key}.

Generates an RSA key of 2048, 3072 or 4096 bits, or an Ed25519 key.

Signs the canonicalised header data of a DKIM signature: RSASSA-PKCS1-v1_5 with SHA-256, or Ed25519 over the SHA-256 hash of the data (RFC 8463 §3).

The key as PKCS #8 PEM (-----BEGIN PRIVATE KEY-----).

Types

algorithm()

@type algorithm() :: :rsa | :ed25519

t()

@type t() :: %Karta.Auth.DKIM.Key{
  algorithm: algorithm(),
  bits: pos_integer(),
  private: term(),
  public: term()
}

private and public are the :public_key RSA records for RSA keys, and the 32-byte seed and public key for Ed25519.

Functions

dns_record(key)

@spec dns_record(t()) :: String.t()

The DKIM key record (RFC 6376 §3.6.1, RFC 8463 §4): v=DKIM1; k=rsa; p=<SubjectPublicKeyInfo> or v=DKIM1; k=ed25519; p=<the 32-octet key>, base64.

dns_strings(key)

@spec dns_strings(t()) :: [String.t()]

dns_record/1 cut into character-strings of at most 255 octets, for a TXT record.

from_pem(pem)

@spec from_pem(binary()) :: {:ok, t()} | {:error, :invalid_key}

Reads a PEM private key: PKCS #8 (RSA or Ed25519) or PKCS #1 (-----BEGIN RSA PRIVATE KEY-----). RSA keys must have at least 1024 bits. Anything else is {:error, :invalid_key}.

generate(algorithm, bits \\ nil)

@spec generate(algorithm(), pos_integer() | nil) :: t()

Generates an RSA key of 2048, 3072 or 4096 bits, or an Ed25519 key.

sign(key, data)

@spec sign(t(), iodata()) :: binary()

Signs the canonicalised header data of a DKIM signature: RSASSA-PKCS1-v1_5 with SHA-256, or Ed25519 over the SHA-256 hash of the data (RFC 8463 §3).

to_pem(key)

@spec to_pem(t()) :: binary()

The key as PKCS #8 PEM (-----BEGIN PRIVATE KEY-----).