Page
Library
Module
Module type
Parameter
Class
Class type
Source
DkimSourceDKIM signature and verification for emails.
This module implements the DomainKeys Identified Mail (DKIM) protocol as specified by RFC 6376. DKIM allows a sending domain to cryptographically sign outgoing email so that receiving mail servers can verify the message has not been altered in transit and was authorized by the domain owner.
The library processes emails in a single streaming pass for both verification and signing. Memory consumption stays proportional to the size of an input chunk, not the size of the entire message.
Two signature algorithms are supported: RSA and ED25519. Two hash functions are available: SHA1 and SHA256. Both "simple" and "relaxed" canonicalization methods are implemented for headers and body.
The signature algorithm used to produce or verify a DKIM signature. RSA is the most widely deployed algorithm. ED25519 is a newer, smaller-key alternative introduced by RFC 8463.
The hash function used when computing the digest of the message headers and body. SHA256 is recommended. SHA1 is supported for compatibility with older signatures but is considered weak.
The canonicalization algorithm applied to headers or body before hashing. `Simple leaves the content untouched (modulo trailing CRLF rules). `Relaxed normalizes whitespace and lowercases header field names, which makes the signature more tolerant of minor modifications by intermediate mail servers.
The method used to retrieve the public key of the signing domain. The only method defined by RFC 6376 is a DNS TXT record query.
An existentially packed hash algorithm from Digestif.
An existentially packed hash value: a pair of a Digestif hash algorithm and the corresponding digest.
A DKIM record, parameterized by whether it carries a cryptographic signature. A value of type signed t was parsed from an existing DKIM-Signature header field and carries both the original signature bytes and the body hash. A value of type unsigned t is one that has been constructed programmatically and is ready to be signed.
The payload of a signed DKIM record: the raw signature string together with the body hash. The private annotation prevents construction outside this module while still allowing pattern-matching.
The phantom type for an unsigned DKIM record. Values of this type are created by v and consumed by Sign.signer.
A private key used for signing. It must match the algorithm declared in the DKIM record: an RSA key for `RSA and an Ed25519 key for `ED25519.
val v :
?version:int ->
?fields:Mrmime.Field_name.t list ->
selector:[ `raw ] Domain_name.t ->
?algorithm:algorithm ->
?hash:hash ->
?canonicalization:(canonicalization * canonicalization) ->
?length:int ->
?query:query ->
?timestamp:int64 ->
?expiration:int64 ->
[ `raw ] Domain_name.t ->
unsigned tv ~selector domain creates an unsigned DKIM record for the given domain and selector. The selector determines which DNS TXT record (published at selector._domainkey.domain) holds the corresponding public key.
Optional parameters let you override the defaults:
version must be 1 (the only version defined by the RFC).fields lists the header field names to include in the signature; it defaults to [From] and From is always included even if omitted.algorithm defaults to `RSA.hash defaults to `SHA256. canonicalization is a pair of (header, body) methods, defaulting to (`Relaxed, `Relaxed).length restricts the number of body octets covered by the signature. (this parameter is not supported)query defaults to `DNS `TXT.timestamp and expiration are Unix timestamps expressed as int64 values.fields dkim returns the list of header field names that are (or will be) included in the signature.
timestamp dkim returns the signature creation time, if present. The value is a Unix timestamp.
expire dkim returns the signature expiration time, if present. A verifier should treat the signature as invalid after this time.
body dkim returns the raw hash of the canonicalized body as recorded in the signed DKIM record (the bh= tag).
domain dkim returns the signing domain identity (the d= tag).
selector dkim returns the selector (the s= tag) used to locate the public key in DNS.
domain_name dkim computes the full DNS name used to retrieve the public key: selector._domainkey.domain. Returns an error if the resulting domain name is invalid.
canonicalization dkim returns the pair (header_canon, body_canon) of canonicalization algorithms declared in the DKIM record.
hash_algorithm dkim returns the hash algorithm used for digesting.
signature_and_hash dkim returns the raw signature-and-hash payload carried by the record. For a signed t, this is the pair of the signature bytes and the body hash.
algorithm dkim returns the signature algorithm (RSA or ED25519).
of_string str parses a DKIM-Signature header field value from the given string. The string should contain the tag-value list without the field name itself (that is, everything after "DKIM-Signature:").
of_unstrctrd u parses a DKIM-Signature from an Unstrctrd.t value obtained from the mrmime header parser.
with_canonicalization dkim (hdr, body) returns a copy of dkim with the canonicalization algorithms replaced.
with_signature_and_hash dkim payload returns a copy of dkim carrying the given signature-and-hash payload. This is used internally to transition between signed and unsigned states.
with_selector dkim ~selector returns a copy of dkim with the selector replaced.
with_expiration dkim exp returns a copy of dkim with the expiration time replaced. Pass None to remove the expiration.
A domain key is the public-side record published in DNS. It contains the public key material and metadata (acceptable hash algorithms, service types, flags) that a verifier needs.
domain_key_of_string str parses a domain key from the content of a DNS TXT record. The string is the raw value returned by the DNS resolver.
domain_key_of_dkim ~key dkim constructs a domain_key from a private key and a DKIM value. The resulting value is what you would publish in DNS so that verifiers can check signatures produced with key.
domain_key_to_string ?with_version dk serializes a domain key into the tag-value format expected in a DNS TXT record. with_version writes the domain-key version (by default) to v=DKIM1.
equal_domain_key a b is true if a and b carry the same public key material and metadata.
public_key dk returns the raw DER-encoded public key bytes.
The following values are exposed for advanced use cases such as building custom DKIM processing pipelines or integrating with other email authentication mechanisms (SPF, DMARC, ARC).
An heterogeneous map holding the parsed tag-value pairs of a DKIM-Signature header field.
The DKIM-Signature field name as a Mrmime.Field_name.t value.
remove_signature_of_dkim u strips the b=... tag value from the unstructured representation u of a DKIM-Signature header. The b= tag key and its trailing semicolon are preserved; only the signature data between them is removed. This is the transformation required by the RFC before hashing the DKIM-Signature header itself.
uniq u collapses runs of consecutive whitespace in u into a single whitespace element, preserving the first one.
trim u strips leading and trailing whitespace from u and collapses internal whitespace to a single space.
of_unstrctrd_to_map u parses the unstructured content of a DKIM-Signature header into a heterogeneous map of tag-value pairs.
map_to_t map converts a parsed tag-value map into a typed signed t record. Returns an error if required tags are missing or values are malformed.