Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mail.MessageAuth

AUTHENTICATION OF THE MESSAGE, not of the user. Nothing here has anything to
  do with SMTP AUTH; this is the inbound gate that decides whether a message
  really comes from where its From: header claims, and the outbound step that
  lets someone else make the same decision about ours.

  TWO HALVES:

  * tSmtpMessageAuthenticator runs SPF over the connection, verifies every DKIM
    signature on the message, and hands both to DMARC for the only judgement
    that is actually about the visible sender. It returns a disposition and the
    Authentication-Results field that records why.

  * tSmtpDkimAgent signs on the way out. It is an iSmtpDeliveryAgent DECORATOR
    around whatever agent would otherwise have delivered, so it slots into the
    spool without the spool knowing anything about DKIM, and it runs on a spool
    worker thread rather than on the reactor thread that is holding a client's
    250.

  WHY SIGNING HAPPENS AT DELIVERY AND NOT AT ACCEPTANCE: the spool may split one
  accepted message across several destination domains, and each copy is
  delivered on its own schedule. Signing per delivery gives each copy its own
  t=, costs one signature per attempt, and - the point - keeps a public-key
  operation off the thread that is still talking to the client.

  ORDER MATTERS ON THE WAY IN. The body hash covers the message as received, so
  verification must run BEFORE anything is prepended to it. The
  Authentication-Results field is added afterwards, and a downstream verifier is
  unaffected because DKIM selects header fields from the bottom up.

  AND A WARNING ABOUT THAT FIELD: Authentication-Results is only meaningful if
  the reader trusts the host that wrote it. This unit strips one that arrived
  from outside (StripInboundResults, on by default) - otherwise a sender can
  simply assert its own "dkim=pass" and anything downstream that reads headers
  rather than re-verifying will believe it.

  WHAT DNS DID NOT ANSWER IS SAID ON THE MESSAGE. Every check that went on
  without its DNS answer is listed in tSmtpAuthReport.Relaxed, and
  SmtpStampDnsWarnings turns the list into X-DNS-Warning fields, after
  renaming every such field that arrived from outside naming this host.

  AND SO IS WHAT WAS TAKEN AWAY. A field that arrived claiming to come from
  this host - Authentication-Results, X-DNS-Warning or X-Stripped-Fields,
  its first token read as RFC 8601 reads an authserv-id - is kept as
  evidence under another name, cSmtpForgedFieldPrefix in front of its own,
  so that nothing reads it as the field it was; an Authentication-Results
  bearing a version this unit does not support, whoever it names, is
  removed. One X-Stripped-Fields field says which, how many and why.
  SmtpStripForgedFields does the renaming, the removing and the counting in
  one place, so a host can score the forgeries (tSpamContext.ForgedStamps)
  before it stamps anything.

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

Classes, interfaces and records

classeSmtpAuthError

inherits from Exception

(no public members)

recordtSmtpStrippedFields

HOW MANY FIELDS WERE TAKEN AWAY from one message, by name and reason. A field names this host when SmtpFieldNamesHost says so; the first three are those, the forgeries SmtpStrippedForged counts, each RENAMED with cSmtpForgedFieldPrefix; the fourth were removed. Zero it with FillChar before the first use: a host keeps one per message.

AuthResults: Integer;
Authentication-Results naming this host, renamed when StripInboundResults is on.
DnsWarnings: Integer;
X-DNS-Warning naming this host, renamed.
StripNotes: Integer;
X-Stripped-Fields naming this host: a copy of this host's own note, renamed.
UnsupportedVersion: Integer;
Authentication-Results bearing a version this unit does not support, naming any other host - or this one with StripInboundResults off. Not a forgery of this host.

recordtSmtpDnsRelaxation

One check that went on without its DNS answer.

Check: string;
spf, dkim, dmarc, or a spam check's CheckName.
Name: string;
The name asked for that got no answer.
Relaxed: string;
What was not applied, in a few words.

recordtSmtpAuthReport

Everything the checks concluded, kept whole so a policy handler can second guess any part of it.

Disposition: tSmtpAuthDisposition; Code: Integer; Text: string; Reason: string; FromDomain: string;
RFC5322.From, the identity DMARC aligns against.
FromReason: string; FromStatus: tDmarcFromStatus; Spf: tSpfResult; SpfDomain: string; Dkim: tDkimVerdictArray; Dmarc: tDmarcVerdict; DkimPassed: Boolean;
True when at least one signature verified.
DkimBroken: Boolean;
True when a signature was present and none verified.
SpfTempErrorName: string;
The name SPF asked for that did not answer, when Spf is srTempError.
Relaxed: tSmtpDnsRelaxations;
EVERY CHECK ABOVE THAT DNS DID NOT ANSWER, whatever the disposition: an SPF temperror, each DKIM key that could not be fetched, a DMARC record that could not be retrieved, and a DMARC pass decided without it. A deferred message carries them too, because OnReport may still accept it; the server stamps them as X-DNS-Warning fields on what it delivers.

classtSmtpMessageAuthenticator

constructor Create(const aResolver: iDnsResolver); function Authenticate(const aEnvelope: iSmtpEnvelope; const aData: tBytes): tSmtpAuthReport;
Runs every check over a message as received. aData must be the message EXACTLY as it arrived - anything prepended first invalidates the body hash.
function BuildResultsHeader(const aReport: tSmtpAuthReport): string;
The RFC 8601 field recording aReport, CRLF-terminated.
function StampMessage(const aData: tBytes; const aReport: tSmtpAuthReport): tBytes; overload;
aData with the results field prepended, and with any inbound Authentication-Results claiming to be from this host renamed first - cSmtpForgedFieldPrefix; its authserv-id read as SmtpAuthServId reads it - when StripInboundResults is on, and any bearing a version this unit does not support removed... Full description »
function StampMessage(const aData: tBytes; const aReport: tSmtpAuthReport; var aStripped: tSmtpStrippedFields): tBytes; property Receiver: string read fReceiver write fReceiver;
This host's name, for the Authentication-Results authserv-id.
property RejectBrokenSignature: Boolean read fRejectBrokenSignature write fRejectBrokenSignature;
A message carrying signatures of which NONE verified is refused - of those whose key came; one whose key did not defers the message while the key could still decide it. Default True. Note this is stricter than RFC 6376 sec.6.1, which says to ignore a failed signature and carry on: mailing lists routinely break signatures by editing what they forward, and those messages will be refused here.
property RejectAnyBrokenSignature: Boolean read fRejectAnyBrokenSignature write fRejectAnyBrokenSignature;
Stricter still: refuse when ANY signature failed, even though another one verified. Default False, because a second signature added by a forwarder or list is the normal case and refusing it rejects a great deal of legitimate mail.
property EnforceDmarc: Boolean read fEnforceDmarc write fEnforceDmarc;
Honour a published p=reject / p=quarantine. Default True. THIS is what makes a MISSING signature a refusal: the sending domain's own DNS is what says a signature should have been there.
property RequirePublishedPolicy: Boolean read fRequirePublishedPolicy write fRequirePublishedPolicy;
Refuse mail from any domain that publishes NO DMARC record at all. Default False - most of the internet publishes nothing, and turning this on rejects it.
property CheckSpf: Boolean read fCheckSpf write fCheckSpf; property StripInboundResults: Boolean read fStripInboundResults write fStripInboundResults;
Rename an inbound Authentication-Results field bearing this host's own authserv-id (cSmtpForgedFieldPrefix), so that it is one no longer. Full description »
property FailClosedOnTempError: Boolean read fFailClosedOnTempError write fFailClosedOnTempError;
Turn "could not check" into 451 rather than an accept, WHEN THE ANSWER COULD STILL CHANGE THE DMARC VERDICT: a DNS failure on the DMARC record, or, under a DMARC refusal, an SPF temperror or a DKIM key that could not be retrieved for an identifier aligned under the record's aspf= or adkim=; and,... Full description »
property MinKeyBits: Integer read fMinKeyBits write fMinKeyBits; property AllowBodyLength: Boolean read fAllowBodyLength write fAllowBodyLength; property RejectMissingFrom: Boolean read fRejectMissingFrom write fRejectMissingFrom;
Refuse a message with NO From: header. RFC 5322 requires one, but plenty of automated mail omits it and DMARC simply does not apply without one, so this is off by default. A message with TWO From: fields is refused regardless - that is not malformed, it is engineered.
property Resolver: iDnsResolver read fResolver write fResolver; property OnReport: tSmtpAuthReportEvent read fOnReport write fOnReport;
The last word. Everything above only computes a recommendation.

classtSmtpDkimAgent

inherits from TInterfacedObject

implements iSmtpDeliveryAgent

Signs on the way out, then hands the signed octets to the agent that would otherwise have been called.

constructor Create(const aInner: iSmtpDeliveryAgent); destructor Destroy; override; function Deliver(const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aError: string): tSmtpDeliveryOutcome; property Signer: tDkimSigner read fSigner;
Configure Domain, Selector, Algorithm and Key on this.
property Enabled: Boolean read fEnabled write fEnabled;
False passes everything through untouched, which is what an unconfigured relay must do - failing to sign is not a reason to fail to deliver.
property Inner: iSmtpDeliveryAgent read fInner write fInner;

Types (enumerations, sets, aliases)

arraytSmtpDnsRelaxations

  tSmtpDnsRelaxations = array of tSmtpDnsRelaxation;

enumtSmtpAuthDisposition

What the relay should do with the message.

  tSmtpAuthDisposition = (
    { Deliver it. }
    sadAccept,
    { Deliver, but the caller should mark it - there is no spam folder here, so
      the relay treats this as accept plus a header unless told otherwise. }
    sadQuarantine,
    { Refuse permanently: 5xx. }
    sadReject,
    { Refuse temporarily: 4xx. Used when something could not be CHECKED, which
      is not the same as having failed. }
    sadDefer);

proctSmtpAuthReportEvent

  tSmtpAuthReportEvent = procedure (Sender: TObject;
                                    const aEnvelope: iSmtpEnvelope;
                                    const aReport: tSmtpAuthReport;
                                    var aDisposition: tSmtpAuthDisposition;
                                    var aCode: Integer;
                                    var aText: string) of object;

Routines