Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mail.SMTPMx

DELIVERY TO THE RECIPIENT'S OWN MAIL EXCHANGER, as an iSmtpDeliveryAgent the
  spool can use in place of - or behind - tSmtpSmarthostAgent.

  THE TLS PROBLEM THIS UNIT EXISTS TO GET RIGHT. Between MTAs there is no shared
  configuration and no password, so the usual answers do not apply:

    - Opportunistic STARTTLS protects against a PASSIVE observer and nothing
      else. An active attacker strips STARTTLS from the EHLO response and the
      sender delivers in the clear, by design.
    - Verifying the certificate does NOT fix that, and asking for it is worse
      than useless. The MX name came from UNSIGNED DNS, so whoever forged the MX
      answer also chose the name that would be checked (RFC 7672 sec.1.3.1) -
      while an attacker who simply strips STARTTLS is never asked for a
      certificate at all. The check therefore catches only honest
      misconfiguration, and hands the attacker a free denial of delivery: inject
      any certificate, the delivery defers, retries expire, the mail bounces.
    - MTA-STS (RFC 8461) is the answer, because the policy arrives over a
      channel the DNS attacker does not control. StreamSec.Mail.MTASTS does the
      discovery; this unit acts on it.

  SO THE POLICY LADDER IS: whatever tSmtpMxTlsPolicy says, RAISED - never
  lowered - by what MTA-STS says for the domain. A configured smtOpportunistic
  becomes verification for a domain that publishes enforce, and stays
  opportunistic for one that publishes nothing. The reverse can never happen:
  a domain's own policy cannot be used to weaken the sender's.

  PREFERRING A TLS-CAPABLE MX among the domain's published records would be
  legitimate - all of them are receivers the domain itself designated - and this
  unit deliberately does NOT offer it. Be clear about what it would buy:
  robustness against one neglected backup MX, and nothing against an attacker,
  who can strip STARTTLS from the second host exactly as easily as from the
  first. What actually raises the posture is REQUIRING TLS, which is what
  TLSPolicy is for and what a domain publishing MTA-STS enforce gets
  automatically - so a preference switch would have been a second, weaker way to
  ask for something the policy setting already says properly.
  WHAT IS NOT OFFERED, AND WILL NOT BE: looking for some OTHER relay that
  happens to support TLS. That means finding a host willing to relay for a
  domain it does not serve - an open relay - and it is strictly worse than the
  cleartext hop it replaces: the message is encrypted to a stranger who then
  delivers it over the same unprotected last hop, having taken a plaintext copy
  on the way.

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

Classes, interfaces and records

classeSmtpMxError

inherits from Exception

(no public members)

classtMtaStsHttpsFetcher

inherits from TInterfacedObject

implements iMtaStsFetcher

THE REAL POLICY FETCHER. It is here rather than in StreamSec.Mail.MTASTS so that unit keeps no dependency on the HTTP stack and stays testable from a string - the seam exists for exactly that.

constructor Create(aTLSServer: TsmSimpleTLSInternalServer); function Fetch(const aDomain: string; out aText: string; var aWhy: string): Boolean; property TLSServer: TsmSimpleTLSInternalServer read fTLSServer write fTLSServer;
The engine whose ROOT CERTIFICATES decide whether a policy is authentic. This is the WEB PKI, not the mail trust domain: mta-sts.<domain> holds an ordinary web certificate, so an engine loaded with a private mail CA will reject every real policy. Give it its own engine with the public bundle - one more instance of the rule that one engine means one trust domain.
property Timeout: Cardinal read fTimeout write fTimeout;

classtSmtpMxAgent

inherits from TInterfacedObject

implements iSmtpDeliveryAgent

constructor Create(const aResolver: iDnsResolver); destructor Destroy; override; function Deliver(const aEnvelope: iSmtpEnvelope; const aData: tBytes; var aError: string): tSmtpDeliveryOutcome; property MtaSts: tMtaStsResolver read fMtaSts write fMtaSts;
Optional. With none, no domain policy is discovered and the configured TLSPolicy is the whole answer - which is a supported, weaker deployment, not a broken one. The agent does NOT own it.
property TLSServer: TsmSimpleTLSInternalServer read fTLSServer write fTLSServer;
The trust domain for the MX leg. Its roots are what smtVerify checks against; with no engine and no OnConfigure the client trusts nothing, so smtVerify would fail every delivery.
property TLSPolicy: tSmtpMxTlsPolicy read fTLSPolicy write fTLSPolicy; property HeloName: string read fHeloName write fHeloName; property SourceIP: string read fSourceIP write fSourceIP;
Local source for EVERY MX attempt. Empty keeps OS selection. A failed bind defers delivery; it never retries from another local address.
property Port: Word read fPort write fPort; property ConnectTimeout: Cardinal read fConnectTimeout write fConnectTimeout; property OnLog: tSmtpMxLogEvent read fOnLog write fOnLog; property OnAttempt: tSmtpMxAttemptEvent read fOnAttempt write fOnAttempt; property OnConfigure: tSmtpMxConfigureEvent read fOnConfigure write fOnConfigure;

Types (enumerations, sets, aliases)

enumtSmtpMxTlsPolicy

HOW MUCH TLS THE MX LEG INSISTS ON. Deliberately NOT tSmtpDeliveryTlsMode: that one describes a smarthost, where implicit TLS on 465 is available and is the right default. An MX is port 25 and STARTTLS or nothing, so the questions are different ones.

  tSmtpMxTlsPolicy = (
    { Encrypt when the far end offers it, deliver in the clear when it does not,
      and DO NOT verify the certificate. THE DEFAULT, and the only honest one
      for the internet at large: refusing to deliver would lose mail to the many
      correctly-run hosts that still have no usable certificate, and verifying
      would fail on them while catching no attacker (see the header). }
    smtOpportunistic,
    { STARTTLS is REQUIRED - defer rather than deliver in the clear - but the
      certificate is still not verified. Useful for a domain you know does TLS
      and where you would rather retry than send in the clear; it stops the
      passive observer and the lazy downgrade, not a determined attacker. }
    smtEncrypt,
    { STARTTLS is required AND the certificate must validate for the MX
      hostname. Only meaningful when the MX name itself is trustworthy, i.e.
      when MTA-STS said so - which is why the effective policy is raised to this
      automatically for a domain that publishes enforce, and why setting it by
      hand for the internet at large is a way to lose mail. }
    smtVerify);

proctSmtpMxLogEvent

One line of the agent's progress: the MTA-STS verdict for a domain, an MX skipped or failed, a delivery. An exception the handler raises is swallowed and changes nothing about the delivery: neither MX fallback, nor the outcome, nor whether a failure is permanent.

  tSmtpMxLogEvent = procedure (Sender: TObject; const aText: string) of object;

proctSmtpMxConfigureEvent

Per-delivery access to the client, after everything else is set.

  tSmtpMxConfigureEvent = procedure (Sender: TObject;
                                     aClient: TsmAdvancedSMTPClient) of object;

proctSmtpMxAttemptEvent

One call for each MX attempt that has a client: every attempt but one whose CreateClient raised or returned nil. After a delivery aDelivered is True and aError nil, and the call comes before the QUIT, with the session still open. A failed attempt is reported after the client has sent QUIT (when the session was open) and disconnected, so Secure is False, and its reply code and reply text are what its last read of a reply left: they need not describe the failure. aError does - its message, and the ReplyCode of an eSMTPClientError or the ErrorCode of an EstSocketError - and aClient.Stage says how far the attempt got. aClient and aError are LENT for the call: do not keep, free or raise either. An exception the handler raises is swallowed and changes nothing about the delivery.

  tSmtpMxAttemptEvent = procedure (Sender: TObject;
    aClient: TsmAdvancedSMTPClient; aError: Exception;
    aDelivered: Boolean) of object;

Routines