Keep the line-oriented protocol of an Indy TIdTCPClient and use StreamSec to authenticate the server and encrypt the connection. The example sends ping to a TLS echo service and reads the reply through Indy's IOHandler.
For Indy 10 with Delphi 13, add smTLSId10D37IOHandler.pas from the TLS\Indy directory of the Delphi 13 demos to the project. This is the adapter unit that provides TsmTLSIdIOHandlerSocket.
uses
System.SysUtils, IdTCPClient, StreamSec.Mobile.StreamSecII,
StreamSec.Mobile.TlsInternalServer, smTLSId10D37IOHandler;
procedure ConfigureIndyTls(aEngine: TsmSimpleTLSInternalServer; const aCAFile: string); begin aEngine.ClientOrServer := cosClientSide; aEngine.LocalExplicitTrustByDefault := True; aEngine.Options.VerifyServerName := [vsnDNS]; aEngine.Options.SessionResumption := False; if aEngine.LoadRootCertsFromFile(aCAFile) = 0 then raise Exception.CreateFmt('No trust anchors in %s', [aCAFile]); aEngine.TLSSetupClient; end;
Supply a DER or PEM CA certificate obtained from a trusted source. LocalExplicitTrustByDefault marks the roots you load as trusted. This example performs a full handshake on each call so the certificate is checked against the CA file supplied for that call. See Choose which servers a client trusts for other trust sources.
function OpenIndyTlsClient(const aHost, aCAFile: string; aPort: Word): TIdTCPClient; var lEngine: TsmSimpleTLSInternalServer; lIO: TsmTLSIdIOHandlerSocket; begin Result := TIdTCPClient.Create(nil); try lEngine := TsmSimpleTLSInternalServer.Create(Result); ConfigureIndyTls(lEngine, aCAFile); lIO := TsmTLSIdIOHandlerSocket.Create(Result); lIO.TLSServer := lEngine; Result.IOHandler := lIO; lIO.PassThrough := False; Result.Host := aHost; Result.Port := aPort; Result.ConnectTimeout := 5000; Result.ReadTimeout := 5000; Result.Connect; if (lIO.TLSSocket = nil) or not lIO.TLSSocket.Connected then raise Exception.Create('TLS connection closed during the handshake'); except Result.Free; raise; end; end;
Assign the engine to the adapter's TLSServer property and the adapter to the Indy client's IOHandler. Set PassThrough to False before Connect for a service that starts TLS as soon as the TCP connection opens. The returned client owns both components.
For this Indy adapter's direct TLS connection, set TIdTCPClient.Host to the service's DNS name. That is the name the adapter checks when VerifyServerName includes vsnDNS.
function IndyEchoPing(const aHost, aCAFile: string; aPort: Word): string; var lClient: TIdTCPClient; begin lClient := OpenIndyTlsClient(aHost, aCAFile, aPort); try lClient.IOHandler.WriteLn('ping'); Result := lClient.IOHandler.ReadLn(#10, lClient.ReadTimeout, 256); if lClient.IOHandler.ReadLnTimedOut then raise Exception.Create('Timed out waiting for the echo reply'); finally lClient.Free; end; end;
For the async echo server with a certificate for localhost, pass localhost, its issuing CA file, and the bound port to IndyEchoPing. The reply is ping. An untrusted issuer or a different DNS name fails during the handshake, before the line is sent.
TsmSimpleTLSInternalServer · StreamSec.Mobile.TlsInternalServer · StreamSec.Mobile.StreamSecII