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)
inherits from Exception
(no public members)
inherits from TComponent
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Connect; virtual;
property DeadlineTick: UInt64 read fDeadlineTick write fDeadlineTick;
procedure StartTLS;
procedure Disconnect; virtual;
function GetChannelBinding(out aBinding: tBytes): Boolean; virtual;
procedure Send(const aData: TBytes); overload;
procedure Send(const aText: string); overload;
procedure SendLine(const aLine: string);
function Receive(aTimeout: Cardinal = 0): TBytes;
function ReadLine(out aLine: string; aTimeout: Cardinal = 0): Boolean; overload;
function ReadLine(out aLine: string; aTimeout: Cardinal; aMaxBytes: Integer): Boolean; overload;
property State: tTLSClientState read GetState;
property Connected: Boolean read GetConnected;
property Secure: Boolean read GetSecure;
property PeerClosed: Boolean read GetPeerClosed;
property NegotiatedProtocol: string read GetNegotiatedProtocol;
property ContentLayer: TCustomTLS_ContentLayer read GetContentLayer;
property Host: string read fHost write SetHost;
property SourceIP: string read fSourceIP write SetSourceIP;
property Port: Word read fPort write SetPort;
property ServerName: string read fServerName write SetServerName;
property ImplicitTLS: Boolean read fImplicitTLS write fImplicitTLS default True;
property RequireTLS: Boolean read fRequireTLS write fRequireTLS default True;
property ConnectTimeout: Cardinal read fConnectTimeout write fConnectTimeout default cTLSClientDefaultConnectTimeout;
property IOTimeout: Cardinal read fIOTimeout write fIOTimeout default cTLSClientDefaultIOTimeout;
property ALPNProtocols: TStringList read fALPNProtocols write SetALPNProtocols;
property OnConfigureLayer: tTLSClientLayerEvent read fOnConfigureLayer write fOnConfigureLayer;
property OnHandshakeDone: tTLSClientLayerEvent read fOnHandshakeDone write fOnHandshakeDone;
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;
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;
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.