Use TsmTLSClient to speak a byte-oriented protocol over TLS. This example connects to an echo service at a specified address, checks the certificate against the service's DNS name and a local trust anchor, and exchanges one line after the handshake.
ServerName to the name in the certificate - see Download a page over HTTPS.uses
StreamSec.Mobile.TLSClient;
function OpenCheckedEchoClient(const aHost, aServerName, aCAFile: string; aPort: Word): TsmTLSClient; begin Result := TsmTLSClient.Create(nil); try if Result.TLSServer.LoadRootCertsFromFile(aCAFile) = 0 then raise eTLSClientError.CreateFmt('No trust anchors in %s', [aCAFile]); Result.Host := aHost; Result.Port := aPort; Result.ServerName := aServerName; Result.ALPNProtocols.Add('echo'); Result.Connect; if Result.NegotiatedProtocol <> 'echo' then raise eTLSClientError.Create('The server did not negotiate echo.'); except Result.Free; raise; end; end;
Host to the address to dial and Port to the service's TCP port.ServerName to the DNS name you expect from your service configuration. It is sent as SNI and checked against the certificate. For a service reached by its DNS name, leave ServerName empty to use Host for both.LoadRootCertsFromFile accepts DER or PEM. For other trust sources, see Choose which servers a client trusts.echo ALPN token identifies the application protocol used by this example. The client checks the selected token before sending its first byte.function CheckedEchoLine(const aHost, aServerName, aCAFile: string; aPort: Word; const aLine: string): string; const cMaxLineBytes = 4096; var lClient: TsmTLSClient; lChar: Char; begin if Length(aLine) > cMaxLineBytes then raise eTLSClientError.Create('Echo line exceeds 4096 bytes.'); for lChar in aLine do if (Ord(lChar) < 32) or (Ord(lChar) > 126) then raise eTLSClientError.Create('The echo line must be printable ASCII.'); lClient := OpenCheckedEchoClient(aHost, aServerName, aCAFile, aPort); try lClient.SendLine(aLine); if not lClient.ReadLine(Result, lClient.IOTimeout, cMaxLineBytes) then raise eTLSClientError.Create('No complete echo line received.'); finally lClient.Free; end; end;
For a local async echo server whose certificate carries localhost, pass 127.0.0.1 as the host, localhost as the expected name, the path to its issuing CA certificate, the listener's bound port, and hello as the line. The result is hello. The server chooses how to frame its application data. This example uses a CRLF-terminated ASCII line and limits its reply to 4096 bytes.
A certificate for another name fails even when its issuer is trusted. A certificate for the right name also fails if the chain cannot reach a trusted issuer. In either case Connect raises, the client is freed, and the line is never sent. The local TLS engine reports these failures as EOutgoingTLSAlert in StreamSec.Mobile.TlsInternalServer. Check the configured name, the chain the server sends and the CA file before reconnecting. The TLS notes describe the certificate policy used by the engine.
TsmTLSClient · StreamSec.Mobile.TLSClient · StreamSec.Mobile.TlsInternalServer