Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Connect a TLS client with certificate checks

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.

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.
The client checks, on every connection, that the server's certificate is issued for the host name it connects to. Give it the DNS name the certificate carries, not the server's IP address: with an address the connection fails although the server's certificate is valid. To connect by address, set ServerName to the name in the certificate - see Download a page over HTTPS.

Connect to the intended service

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;

Exchange a line

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.

When the handshake fails

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.

Reference

TsmTLSClient · StreamSec.Mobile.TLSClient · StreamSec.Mobile.TlsInternalServer

Related tasks