Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mail.Dkim

WHAT DKIM ANSWERS, AND WHERE IT DIFFERS FROM SPF.

  A DKIM signature says: the domain in d= took responsibility for this message,
  and the parts of it covered by h= plus the body have not changed since. Unlike
  SPF (StreamSec.Mail.Spf) it survives forwarding, because it authenticates
  CONTENT rather than a connection - which is also why it is the identity DMARC
  can align against a human-visible From: header.

  WHAT A PASS DOES NOT MEAN. It does not mean the message is honest, only that
  d= signed it - and d= may be a domain the signer registered five minutes ago.
  It does not cover anything absent from h=, so a header not listed there can be
  ADDED by anyone downstream without breaking the signature. And it says nothing
  about the envelope: the SMTP MAIL FROM is not signed.

  THE VERIFIER IS THE ATTACK SURFACE, and three of its rules are load-bearing:

  * From: MUST be covered. RFC 6376 sec.5.4 requires it, and a signature that
    omits it authenticates a message whose apparent author anyone may rewrite.
    This unit REFUSES such a signature rather than reporting a pass.
  * l= is a body-length limit, and everything past it is unsigned - an attacker
    can APPEND arbitrary content to a validly signed message. Mail clients show
    the appended part. tDkimVerifier.AllowBodyLength is False here: an l=
    signature is refused unless a caller deliberately turns it on.
  * A header named in h= may occur MORE times in the message than h= accounts
    for. The unsigned extra instance is the one many clients display. Header
    fields are therefore taken from the BOTTOM UP (sec.5.4.2), and a name listed
    more times than it occurs contributes NOTHING further - which is what makes
    "oversigning" (listing a header more often than it appears) able to nail a
    header down against later addition.

  ALGORITHM FLOOR. rsa-sha1 is recognised only so it can be refused: RFC 8301
  removed it from DKIM entirely. RSA keys below cDkimMinKeyBits are refused for
  the same reason.

  This unit uses only the DSI interfaces; the IMPLEMENTATIONS that register the
  providers - StreamSec.DSI.IFC (RSA), StreamSec.DSI.EdDSA, StreamSec.DSI.Arith,
  stSHA256 and stSHA512 - are linked by the APPLICATION, exactly as elsewhere in
  this library.

  Key lookup goes through iDnsResolver, so the whole verifier runs against a
  scripted resolver with no network - and the vectors at the bottom are RFC 6376
  Appendix A and RFC 8463 Appendix A, i.e. signatures produced by someone else.

Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)

Classes, interfaces and records

classeDkimError

inherits from Exception

(no public members)

classtDkimSignature

A parsed DKIM-Signature field.

constructor Create; destructor Destroy; override; property Headers: TStringList read fHeaders;
The h= list, in order, lowercased.
property Tags[const aName: string]: string read GetTag;

classtDkimKeyRecord

A parsed <selector>._domainkey.<domain> TXT record.

constructor Create; destructor Destroy; override; function Parse(const aText: string): Boolean; function Testing: Boolean;
t=y - the domain is testing; a verifier must not treat a fail as policy.
function StrictIdentity: Boolean;
t=s - i= may not be a subdomain of d=.
property Tags[const aName: string]: string read GetTag;

recordtDkimVerdict

What a single signature came to, with enough detail to write an Authentication-Results header and to say WHY.

Result: tDkimResult; Domain: string; Selector: string; Identity: string; Algorithm: tDkimAlgorithm; KeyBits: Integer; Reason: string; Testing: Boolean;

classtDkimVerifier

Verifies every DKIM-Signature on a message. ONE MESSAGE PER CALL; Verify resets everything it needs.

constructor Create(const aResolver: iDnsResolver); function Verify(const aData: tBytes): tDkimVerdictArray;
Verifies aData, a complete RFC 5322 message with CRLF line endings. Returns one verdict per DKIM-Signature found, in the order found, and drNone with an empty array when there are none.
function Summarise(const aVerdicts: tDkimVerdictArray): tDkimResult;
The single result for the message: drPass if ANY signature passed (RFC 6376 sec.6.1 - signatures are independent), otherwise the most informative failure.
property AllowBodyLength: Boolean read fAllowBodyLength write fAllowBodyLength;
l= signatures. False - the default - refuses them; see the header for why that is not paranoia.
property RequireFromSigned: Boolean read fRequireFromSigned write fRequireFromSigned;
Refuse a signature that does not cover From:. Default True; turning it off makes a pass close to meaningless.
property MinKeyBits: Integer read fMinKeyBits write fMinKeyBits; property MaxSignatures: Integer read fMaxSignatures write fMaxSignatures;
A message may carry many signatures; each costs a DNS lookup and a public key operation, so the count is bounded.
property CurrentTime: Int64 read fNow write fNow;
Unix time used for t=/x=. Zero means "use the clock"; a test sets it.
property Resolver: iDnsResolver read fResolver write fResolver;

classtDkimSigner

Produces a DKIM-Signature header for an outbound message.

constructor Create; destructor Destroy; override; function Sign(const aData: tBytes): string;
Returns the complete 'DKIM-Signature: ...'#13#10 field to PREPEND to aData. Raises on a configuration fault; a caller that cannot sign should send unsigned rather than not send.
function SignMessage(const aData: tBytes): tBytes;
Signs and returns the whole message with the field prepended.
property Domain: string read fDomain write fDomain; property Selector: string read fSelector write fSelector; property Identity: string read fIdentity write fIdentity; property Algorithm: tDkimAlgorithm read fAlgorithm write fAlgorithm; property HeaderCanon: tDkimCanon read fHeaderCanon write fHeaderCanon; property BodyCanon: tDkimCanon read fBodyCanon write fBodyCanon; property SignHeaders: TStringList read fSignHeaders;
Which header fields to cover. From: is always covered whether or not it is listed.
property Oversign: Boolean read fOversign write fOversign;
List each covered header ONE EXTRA TIME, so that adding a second instance downstream breaks the signature instead of going unnoticed. Default True.
property ExpirySeconds: Integer read fExpirySeconds write fExpirySeconds;
x= expiry, in seconds from now. 0 = no x= tag.
property Key: iUnknown read fKey write fKey;
An RSA private key for rsa-sha256 or an Ed25519 private key for ed25519-sha256: a key COMPONENT (TstRSAKey, TstEdDSAKey) or a bare iIFPrivateKey / iEdDSAPrivateKey.
property CurrentTime: Int64 read fNow write fNow;

Types (enumerations, sets, aliases)

enumtDkimResult

RFC 8601 sec.2.7.1 - the values that go in an Authentication-Results header. The two errors are NOT interchangeable: drTempError is a DNS failure and may succeed later (a 4xx if anything), drPermError is a broken signature or policy and never will.

  tDkimResult = (
    { No DKIM-Signature at all. }
    drNone,
    { A signature verified. }
    drPass,
    { A signature was present and did NOT verify. }
    drFail,
    { Syntactically valid and cryptographically fine, but refused by local
      policy - an unsigned From:, an l= truncation, a key below the floor, or a
      withdrawn algorithm. Deliberately distinct from drFail, because the
      signature itself was not the problem. }
    drPolicy,
    { Present but unevaluable in a way that is nobody's fault. }
    drNeutral,
    { DNS said "ask again". }
    drTempError,
    { Malformed signature, missing key, unusable key record. }
    drPermError);

enumtDkimCanon

  tDkimCanon = (dcSimple, dcRelaxed);

enumtDkimAlgorithm

  tDkimAlgorithm = (
    dkaNone,
    dkaRsaSha256,
    dkaEd25519Sha256,
    { Recognised ONLY so it can be refused - see the header. }
    dkaRsaSha1);

arraytDkimVerdictArray

  tDkimVerdictArray = array of tDkimVerdict;

Routines