Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mobile.TLSClient

The TLS half of a client, with no opinion about what runs on top of it. A
  protocol client - HTTPS, SMTP submission, IMAP, POP3, LDAP, NNTP - descends
  from TsmCustomTLSClient and gets the connection, the handshake, the record
  layer, the timeouts and a byte (or line) oriented API; it supplies the
  commands.

  IMPLICIT TLS IS THE DEFAULT AND THE RECOMMENDED PATH. RFC 8314 asks clients to
  prefer connecting to a port that is TLS from the first octet (465 / 993 / 995)
  over connecting in cleartext and upgrading, because STARTTLS stripping,
  command injection and credential sniffing are all impossible when there is no
  cleartext phase to attack.

  StartTLS EXISTS ANYWAY, and only because one deployment fact overrides the
  preference: Microsoft 365 does not offer implicit TLS for SMTP submission at
  all - its documented client-submission settings are port 587 (or 25) with
  STARTTLS, and it says in terms that a client defaulting to 465 cannot submit
  to it. SMTP relay between servers is likewise STARTTLS on port 25, which is
  what MTA-STS and DANE are built on, and RFC 8314 explicitly puts relay out of
  scope. So an SMTP client without STARTTLS cannot send through the largest
  business mail platform there is. IMAP and POP3 have no such gap: 993 and 995
  are universal, and a client that only speaks implicit TLS is a complete IMAP
  or POP3 client.

  WHAT StartTLS IS NOT: it is not a subsystem. This unit knows no protocol
  commands - not STARTTLS, not STLS, not AUTH TLS - and implements no capability
  parsing, no opportunistic-TLS policy and no downgrade. The descendant sends
  its own command, reads its own reply, and then calls StartTLS; only the TIMING
  of the handshake differs from the implicit case, so only the timing is here.

  CONCURRENCY: one operation at a time. The lock exists so that a reader thread
  and a writer thread cannot corrupt the record layer - not to provide
  concurrency - because a TLS layer's read path is also a WRITER (alerts,
  KeyUpdate replies and post-handshake messages all emit through the same
  record layer and bump the same sequence number). See the fLock comment.

Feature notes: TLS implementation notes (RFC map)

Classes, interfaces and records

classeTLSClientError

inherits from Exception

(no public members)

classTsmCustomTLSClient

inherits from TComponent

constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Connect; virtual;
Opens the TCP connection and, when ImplicitTLS is set (the default), runs the handshake before returning. With ImplicitTLS False it returns with the connection in cleartext for a descendant to upgrade with StartTLS. » member page
property DeadlineTick: UInt64 read fDeadlineTick write fDeadlineTick;
Absolute monotonic stTlsUtils.GetTickCount64 deadline, shared by connect, handshake and all later I/O. Zero preserves existing timeout behaviour. With a deadline the socket is non-blocking. Pass a resolved Host literal to also bound connection setup: OS name resolution cannot be interrupted. » member page
procedure StartTLS;
Upgrades an open cleartext connection to TLS. The descendant has already exchanged whatever its protocol requires (STARTTLS / STLS / AUTH TLS) and read the affirmative reply - inside the BeginUpgradeDialogue window when RequireTLS is in force. Full description »
procedure Disconnect; virtual;
Sends close_notify where possible and tears the connection down. Safe to call when already closed. » member page
function GetChannelBinding(out aBinding: tBytes): Boolean; virtual;
THE RFC 9266 tls-exporter CHANNEL BINDING FOR THIS CONNECTION - the value an application protocol that authenticates INSIDE the tunnel binds that authentication to, so an answer produced on one TLS channel cannot be spent on another. Full description »
procedure Send(const aData: TBytes); overload;
Sends application data. Blocks until every octet has been handed to the socket. Raises on a cleartext connection while RequireTLS is in force and no upgrade dialogue is open. » member page
procedure Send(const aText: string); overload; procedure SendLine(const aLine: string);
Sends aLine followed by CRLF - the framing every text-based protocol on this base uses. » member page
function Receive(aTimeout: Cardinal = 0): TBytes;
Returns whatever plaintext is available, waiting up to aTimeout (or IOTimeout when aTimeout is 0). An empty result means the timeout expired or the peer closed; test PeerClosed. Raises on a cleartext connection while RequireTLS is in force and no upgrade dialogue is open. » member page
function ReadLine(out aLine: string; aTimeout: Cardinal = 0): Boolean; overload;
Reads one CRLF- or LF-terminated line, without the terminator. False on timeout or a clean close with no complete line buffered. Provided here rather than in each of SMTP/POP3/IMAP/NNTP because all of them need exactly this and would otherwise each grow their own copy. Shares Receive's RequireTLS gate. » member page
function ReadLine(out aLine: string; aTimeout: Cardinal; aMaxBytes: Integer): Boolean; overload;
Rejects an accumulated FIRST line exceeding aMaxBytes, even before LF. CRLF is excluded from the count. Other lines buffered behind that first one are checked when consumed, not counted against it. Zero is unlimited, preserving the original overload. » member page
property State: tTLSClientState read GetState; property Connected: Boolean read GetConnected; property Secure: Boolean read GetSecure;
True only when the TLS handshake has completed. Test it before sending credentials on a connection that started in cleartext. » member page
property PeerClosed: Boolean read GetPeerClosed;
True once the peer has closed cleanly; buffered plaintext may still be readable. » member page
property NegotiatedProtocol: string read GetNegotiatedProtocol;
The ALPN protocol actually negotiated, or '' when the peer selected none. Read this rather than the offered list: when a server sends no ALPN at all the offer survives intact, so its first entry is whatever we asked for rather than anything the peer consented to. » member page
property ContentLayer: TCustomTLS_ContentLayer read GetContentLayer;
The live session, or nil. For everything this class does not mirror - cipher suite, version, peer certificate, resumption state. » member page
property Host: string read fHost write SetHost;
Host name or address of the server. When ServerName is empty this is also the name the server certificate must match, so prefer a DNS name over a literal address. » member page
property SourceIP: string read fSourceIP write SetSourceIP;
Local source literal, bound before connecting with an ephemeral port. Empty lets the OS select it. A failed bind aborts without an unbound retry. Set only while disconnected. » member page
property Port: Word read fPort write SetPort; property ServerName: string read fServerName write SetServerName;
The name sent as SNI and required of the server certificate. Leave it empty to use Host; set it when connecting to an address, or through a tunnel, whose certificate carries a different name. » member page
property ImplicitTLS: Boolean read fImplicitTLS write fImplicitTLS default True;
True (the default) connects and handshakes in one step - RFC 8314's preferred shape, and the only one with no cleartext phase to attack. Full description »
property RequireTLS: Boolean read fRequireTLS write fRequireTLS default True;
True (the default): nothing crosses the cleartext phase except an upgrade dialogue. The public Send/SendLine/Receive/ReadLine RAISE while the connection is cleartext, except inside the protected BeginUpgradeDialogue/EndUpgradeDialogue window a descendant opens to exchange the bare minimum its pro... Full description »
property ConnectTimeout: Cardinal read fConnectTimeout write fConnectTimeout default cTLSClientDefaultConnectTimeout; property IOTimeout: Cardinal read fIOTimeout write fIOTimeout default cTLSClientDefaultIOTimeout;
Per-receive wait. A stall surfaces as an empty Receive rather than an exception, so a caller that needs a deadline across several reads must keep its own. » member page
property ALPNProtocols: TStringList read fALPNProtocols write SetALPNProtocols;
ALPN protocol names offered. The list IS ordered and is sent in this order, which RFC 7301 defines as the client's preference - but the choice is the SERVER's, and a server is free to ignore the order entirely. Full description »
property OnConfigureLayer: tTLSClientLayerEvent read fOnConfigureLayer write fOnConfigureLayer; property OnHandshakeDone: tTLSClientLayerEvent read fOnHandshakeDone write fOnHandshakeDone;

classTsmTLSClient — component reference

inherits from TsmCustomTLSClient

The self-contained tier: it CREATES AND OWNS the TLS configuration component, so a client dropped on a form works with no other component wired up. Reach into TLSServer for trust anchors (LoadRootCertsFromFile), a client identity for mutual TLS (ImportFromPFX) and policy (Options). Bare name rather than an adjective, matching the client families in this library - the adjective marks the tier that takes an external engine.

property TLSServer: TsmSimpleTLSInternalServer read GetTLSServer write SetOwnTLSServer;
Created and owned here; the setter REFUSES a foreign component rather than ignoring one. An inert setter would be quieter and worse: changing an existing form's component class from the advanced tier to this one leaves a resolvable TLSServer reference in the DFM, and silently discarding it hands the user an unexplained certificate-not-trusted at run time. » member page

classTsmAdvancedTLSClient — component reference

inherits from TsmCustomTLSClient

The tier that takes an EXTERNAL engine, so several clients can share one set of trust anchors and one client identity. Leaving TLSServer nil falls back to the process-wide GlobalServer, which is what StreamSec.Http.Client does and what makes this work with no wiring in an application that already has one.

property TLSServer;

Types (enumerations, sets, aliases)

enumtTLSClientState

tcsPlaintext is only ever reached deliberately: either ImplicitTLS is False (the caller intends to upgrade) or the caller is talking to a cleartext service on purpose - which requires the equally deliberate RequireTLS := False, or every public I/O call raises. Secure is the property to test before sending anything that matters.

  tTLSClientState = (tcsClosed, tcsPlaintext, tcsSecure);

proctTLSClientLayerEvent

  tTLSClientLayerEvent = procedure (Sender: TObject;
                                    const aLayer: TCustomTLS_ContentLayer) of object;