Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Connect an Indy client over TLS using the StreamSec IOHandler

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.

The components on this page are normally placed on a form or a data module at design time and set up in the Object Inspector. The sample creates them in code instead, because code shows at a glance which properties are changed from their default values.

Add the adapter and configure trust

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.

Connect the Indy client

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.

Use the DNS name covered by the server certificate. Passing an IP address as the expected DNS name fails the name check even when the certificate chain is trusted.

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.

Send and receive a line

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.

Reference

TsmSimpleTLSInternalServer · StreamSec.Mobile.TlsInternalServer · StreamSec.Mobile.StreamSecII

Related tasks