Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mail.POP3Engine

The server side of RFC 1939, with no socket, no TLS and no threads in it:
  octets go in through Receive, reply octets come out through OnOutput, and
  every question about storage is delegated to an iPop3ServerPolicy. Same shape
  as StreamSec.Mail.SMTPEngine, and for the same reason - the framing is the
  security-critical half, and a parser that cannot be driven without a socket is
  one whose hostile-input cases never get tested. Pop3ServerSelfTest at the
  bottom drives them.

  THE DOT-STUFFER IS THE SECURITY-CRITICAL CODE, and it is on the way OUT.
  SMTP's rule is about where a message ENDS on input; POP3's mirror image is
  about where a multi-line response ends on output. A body line consisting of a
  single '.' that is not doubled terminates the response early, and every octet
  after it is read by the client AS PROTOCOL. That is response splitting, and it
  is this protocol's equivalent of SMTP smuggling. Two rules follow:

    - THE TERMINATOR IS EMITTED, NEVER INFERRED. If the stored message does not
      end with CRLF the engine appends one, then '.' CRLF. A message with no
      trailing newline cannot merge into the terminator.

    - STUFFING IS CONSERVATIVE: a leading '.' is doubled after CRLF, after a
      bare LF, AND after a bare CR. The strict reading - stuff only after CRLF -
      is right for a strict client and leaves a LAX one, which treats a bare LF
      as a line break, seeing an unstuffed '.' line and ending the message
      early. The cost of the conservative rule is one octet inside a message
      that was already malformed; the cost of the strict one is a
      response-splitting hole against a real client. Take the octet.

  THE ENGINE OWNS POP3 SEMANTICS, THE POLICY OWNS STORAGE. Message numbers are
  frozen when the maildrop opens and never move; DELE only MARKS, RSET clears
  the marks, and the marks are committed exactly once, in UPDATE state, reached
  only by QUIT from TRANSACTION. A dropped connection therefore deletes nothing
  (RFC 1939 sec.6), and that is arranged here rather than trusted to a policy.

  WHAT IT WILL NOT DO. APOP is not implemented and cannot be: it is an MD5 keyed
  digest over the greeting timestamp and the shared secret, so the server must
  hold something the password can be recovered from or re-derived with, and
  StreamSec.Mail.Users stores an Argon2id verifier by design. The same argument
  rules out CRAM-MD5 and DIGEST-MD5. What is left - USER/PASS and SASL PLAIN and
  LOGIN - all send the password itself, so the engine refuses every one of them
  on a cleartext channel UNCONDITIONALLY, whatever the configuration says, and
  CAPA does not advertise what will be refused.

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

Classes, interfaces and records

classePop3ServerError

inherits from Exception

(no public members)

classtPop3Session

(no public members)

interfaceiPop3ServerPolicy

EVERY DECISION THE ENGINE DOES NOT MAKE ITSELF. The engine owns framing, sequencing, the frozen numbering and the deletion marks; who may sign in and what the messages are belong to the application, and they arrive here.

CALLED ON WHATEVER THREAD DROVE Receive - one connection's thread, on this family's cleartext transport - so blocking is permitted.

function Greeting(const aSession: tPop3Session): string;
The greeting text, without '+OK '. '' takes the engine's default. IT MUST NOT LOOK LIKE AN APOP TIMESTAMP: a '<...>' at the end of the greeting is how a client detects APOP, and this engine cannot answer it.
function Authenticate(const aSession: tPop3Session; const aUser: string; const aPassword: iSecretKey; var aWhy: string): Boolean;
The credential, in wiped storage. RUN ONLY OVER A SECURE CHANNEL - the engine refuses the mechanism outright otherwise, so an implementation never sees a password that crossed cleartext. Full description »
function OpenMaildrop(const aSession: tPop3Session; var aWhy: string): Boolean;
OPENS THE MAILDROP AND FREEZES IT. Called once, immediately after Authenticate succeeds, and the message numbering is whatever this establishes. False - with aWhy - is the RFC 1939 sec.3 "unable to open" case, of which an exclusive lock held by another session is the usual one.
function MessageCount(const aSession: tPop3Session): Integer; function MessageOctets(const aSession: tPop3Session; aIndex: Integer): Int64; function MessageUidl(const aSession: tPop3Session; aIndex: Integer): string;
Persistent and unique, 1..70 octets of 0x21..0x7E. The engine validates and substitutes rather than trusting this - see Pop3SafeUidl.
function ReadMessage(const aSession: tPop3Session; aIndex: Integer; out aData: tBytes): Boolean; function ReadHead(const aSession: tPop3Session; aIndex, aBodyLines: Integer; out aData: tBytes): Boolean;
Headers, plus at most aBodyLines lines of body. aBodyLines = 0 is legal and means headers and nothing else.
function Commit(const aSession: tPop3Session; const aDeleted: TArray<Integer>; var aWhy: string): Boolean;
UPDATE state, and the ONLY place a deletion becomes real. aDeleted is ascending message numbers. Anything that must be durable must be durable before this returns, because the +OK that follows is the promise.
procedure SessionClosed(const aSession: tPop3Session);

classtPop3Limits

inherits from TPersistent

The bounds one session enforces, as a TPersistent so a component can publish it as a sub-object. 0 MEANS UNBOUNDED on every one of them; a negative normalises to 0 on the way in.

constructor Create(aOwner: TComponent); virtual; procedure AssignTo(Dest: TPersistent); override; property CommandLineBytes: Integer read fCommandLineBytes write SetCommandLineBytes default cPop3MaxCommandLine; property AuthLineBytes: Integer read fAuthLineBytes write SetAuthLineBytes default cPop3MaxAuthLine; property Errors: Integer read fErrors write SetErrors default cPop3DefaultMaxErrors; property AuthFailures: Integer read fAuthFailures write SetAuthFailures default cPop3DefaultMaxAuthFailures;
Much smaller than Errors on purpose: this is the password-guessing bound.

classtPop3Session

ONE CONNECTION'S POP3 CONVERSATION.

Drive it with Start (which queues the greeting) and then Receive for every octet the peer sends. Replies leave through OnOutput, COALESCED: everything one Receive produced is emitted in a single call.

NOT THREAD-SAFE, and does not need to be: it belongs to one connection.

constructor Create(const aPolicy: iPop3ServerPolicy; aLimits: tPop3Limits = nil); destructor Destroy; override; procedure Start;
Queues the greeting. Call it once, after the transport facts below are set.
procedure Receive(aData: Pointer; aLen: Integer);
Peer octets. Safe to call with the session already closing; the data is discarded.
procedure SecureChannelEstablished;
THE TRANSPORT HAS COMPLETED THE STLS HANDSHAKE. Call it once, and only after OnStartTLSRequested fired and the handshake actually succeeded. RFC 2595 sec.4 requires the session to return to AUTHORIZATION with everything the client said in cleartext discarded, which is what this does - the pending user name above all.
property State: tPop3State read fState; property Limits: tPop3Limits read fLimits; property PendingInputLen: Integer read fInLen;
Octets received but not yet consumed as a complete line. Exposed because it is the only way to prove from outside that the STLS input discard actually happened - a smuggled command still buffered is one that will run.
property MessageCount: Integer read fCount;
Messages in the frozen maildrop, and the identity that opened it.
property AuthUser: string read fAuthUser; property SecureChannel: Boolean read fSecureChannel write fSecureChannel;
True when the channel is encrypted. THE AUTH GATE: with this False the engine neither advertises nor runs any password mechanism, whatever AuthMechanisms says.
property RemoteAddress: string read fRemoteAddress write fRemoteAddress; property ServerName: string read fServerName write fServerName; property AuthMechanisms: tPop3ServerAuths read fAuthMechanisms write fAuthMechanisms; property OfferStartTLS: Boolean read fOfferStartTLS write fOfferStartTLS;
Advertise and honour STLS (RFC 2595). It takes BOTH this and an assigned OnStartTLSRequested: a transport that cannot upgrade must never cause a +OK nobody can honour, so the advertisement and the command share one condition. Meaningless once SecureChannel is True, and suppressed there.
property OnOutput: tPop3OutputEvent read fOnOutput write fOnOutput; property OnException: tPop3ExceptionEvent read fOnException write fOnException; property OnStoreUnavailable: tPop3StoreUnavailableEvent read fOnStoreUnavailable write fOnStoreUnavailable;
Queries this session's store; exception classes do not identify it.
property OnCloseRequested: TNotifyEvent read fOnCloseRequested write fOnCloseRequested;
Raised after the final reply has been flushed. The owner closes the transport; the engine has no way to.
property OnStartTLSRequested: TNotifyEvent read fOnStartTLSRequested write fOnStartTLSRequested;
Raised after the +OK has been flushed IN CLEARTEXT - the whole of RFC 2595 sec.4: the reply that promises the handshake is itself the last cleartext octets.

Types (enumerations, sets, aliases)

enumtPop3State

RFC 1939 sec.3 states three; pssStartTLS and pssClosing are this engine's, and are appended so every existing ordinal stays put.
pssStartTLS is the window between the +OK that promises a handshake and the handshake itself. It exists to STOP THE LINE LOOP DEAD: everything the peer pipelined behind STLS has already been destroyed by then, and no further command may be read from a buffer that is about to stop being cleartext.

  tPop3State = (pssAuthorization, pssTransaction, pssUpdate,
                pssStartTLS, pssClosing);

enumtPop3ServerAuth

The SASL mechanisms this engine can run (RFC 5034). Named psa* rather than ssa*, which StreamSec.Mail.SMTPEngine already uses for its own set - a retrieval component may use both units, and Delphi would resolve a duplicate to whichever came last in the uses clause, silently. USER/PASS IS A MEMBER, not the absence of the others. It used to be modelled by emptiness, and that made the documented posture "SASL off, password login on" INEXPRESSIBLE: an operator who wanted it set [] and got a server that authenticated nobody while the port came up and logged its normal listening line. Now [psaUserPass] says exactly that, and [] still means no authentication at all - which is the family's idiom and stays fail-closed.

  tPop3ServerAuth  = (psaUserPass, psaPlain, psaLogin);

settPop3ServerAuths

  tPop3ServerAuths = set of tPop3ServerAuth;

proctPop3OutputEvent

Reply octets. No Sender, because this is an engine rather than a component - the same shape as tSmtpOutputEvent.

  tPop3OutputEvent = procedure (aData: Pointer; aLen: Integer) of object;

proctPop3ExceptionEvent

  tPop3ExceptionEvent = procedure(aException: Exception) of object;

proctPop3StoreUnavailableEvent

  tPop3StoreUnavailableEvent = function: Boolean of object;

Routines