Contents · Forum Home · StreamSec Tools 4.1.5.364 documentation

Require client certificates (mutual TLS)

With mutual TLS the client proves who it is with a certificate, in the same handshake in which the server proves who it is. Only clients holding a certificate from your CA get a connection at all; there is no password to guess or leak.

This page builds on Serve HTTPS from a PFX file.

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.

The units

uses
  System.SysUtils,
  stSecUtils,                          // tDualSecretKey
  StreamSec.Http.Server,               // iHttpRequest, iHttpConnectionInfo
  StreamSec.Mobile.TlsInternalServer,  // TsmSimpleTLSInternalServer
  StreamSec.Mobile.HTTPSClient,        // TsmHTTPSClient
  TaskHttpsServer;                     // the server of "Serve HTTPS from a PFX file"

The server

constructor tTaskMtlsServer.Create(const aPfxFile, aPfxPassword,
  aClientCAFile: string);
begin
  inherited Create(aPfxFile, aPfxPassword);
  fClientCAFile := aClientCAFile;
end;

procedure tTaskMtlsServer.ConfigureEngine(Sender: TObject; aIndex: Integer;
  aEngine: TsmSimpleTLSInternalServer);
begin
  inherited;   // the server's own certificate and key, from its PFX
  { Ask every client for a certificate, and refuse one without. }
  aEngine.Options.RequestClientCertificate := True;
  aEngine.Options.RequireClientCertificate := True;
  { Trust only the CA that issues your clients' certificates. Never the
    public web roots: any public CA could then issue a "client". }
  if aEngine.LoadRootCertsFromFile(fClientCAFile) = 0 then
    raise Exception.CreateFmt('No certificate in %s', [fClientCAFile]);
end;

Asking for a client certificate and requiring one is in fact what a server engine does unless told otherwise; the plain HTTPS server turns it off, and this one turns it back on. What the server must be given is the CA it trusts to issue client certificates.

Who is the client?

procedure tTaskMtlsServer.Request(Sender: TObject; const aRequest: iHttpRequest;
  const aResponse: iHttpResponse);
var
  lPeer: iHttpConnectionInfo;
begin
  { The certificate was verified during the handshake; what is left here is
    deciding what this client may do. Key that decision on the issuer and
    serial number, which name exactly one certificate - not on the subject
    text alone. }
  lPeer := aRequest.Connection;
  if (aRequest.Path = '/whoami') and lPeer.PeerCertificatePresent then begin
    aResponse.SetContentType('text/plain; charset=utf-8');
    aResponse.SetContentText(lPeer.PeerSubjectCN + ' ' + lPeer.PeerSerialNumber);
  end
  else
    inherited;
end;

aRequest.Connection describes the TLS connection the request arrived on: PeerCertificatePresent, PeerSubject, PeerSubjectCN, PeerIssuer and PeerSerialNumber.

The client

function NewMtlsClient(const aHost, aServerCAFile, aClientPfxFile,
  aClientPfxPassword: string; aPort: Integer): TsmHTTPSClient;
begin
  Result := TsmHTTPSClient.Create(nil);
  try
    { What the client trusts for the server, as for any HTTPS client... }
    if Result.TLSServer.LoadRootCertsFromFile(aServerCAFile) = 0 then
      raise eHTTPSClientError.CreateFmt('No certificate in %s', [aServerCAFile]);
    { ...and the identity it presents: its certificate and private key. }
    Result.TLSServer.ImportFromPFX(aClientPfxFile,
                                   tDualSecretKey.CreateForPFX(aClientPfxPassword));
    Result.Host := aHost;
    Result.Port := aPort;
  except
    Result.Free;
    raise;
  end;
end;

The client's PFX holds its certificate and private key. The same code works for a TsmAdvancedHTTPSClient: import the PFX into the shared engine, and every client using that engine presents the certificate.

The TLS notes describe the client-certificate options in full, including TLS 1.2.

Reference

TsmAsyncTLSServer · TsmHTTPSClient · StreamSec.Mobile.AsyncTLSServer · StreamSec.Http.Server

Related tasks