Contents · Forum Home · StreamSec Tools 4.1.5.364 documentation

Choose which servers a client trusts

A TLS client accepts a server when the server's certificate chains to a certificate authority the client trusts and carries the host name the client connected to. Which authorities to trust is the decision this page is about: every authority you add can vouch for any server.

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.

A server whose CA you know

Your own servers, a partner's API, a device you ship: their certificates come from a CA you can name. Trust that CA and nothing else.

{ A server whose certificate comes from a CA you know - your own servers, a
  partner's API, a device. Trust exactly that CA and nothing else. }
function NewClientForCA(const aHost, aCAFile: string; aPort: Integer): TsmHTTPSClient;
begin
  Result := TsmHTTPSClient.Create(nil);
  try
    { DER or PEM, told apart by content; a PEM bundle loads every
      certificate in it. The result is how many were loaded: 0 would leave
      the client trusting nobody, which fails at the first request instead. }
    if Result.TLSServer.LoadRootCertsFromFile(aCAFile) = 0 then
      raise eHTTPSClientError.CreateFmt('No certificate in %s', [aCAFile]);
    Result.Host := aHost;   // checked against the server's certificate
    Result.Port := aPort;
  except
    Result.Free;
    raise;
  end;
end;

A client set up this way refuses every other server, including ones with certificates from public CAs - which is the point.

A server on the public web

A client that must reach servers whose CA cannot be known in advance - any web site a user types - needs the roots of a public root program. TsmCCADBTrustAnchors holds that set, compiled into the library.

{ A server on the public web, whose CA you cannot know in advance: trust the
  roots of a public root program, compiled into the library. }
function NewPublicClient(const aHost: string): TsmHTTPSClient;
var
  lRoots: TsmCCADBTrustAnchors;
begin
  Result := TsmHTTPSClient.Create(nil);
  try
    lRoots := TsmCCADBTrustAnchors.Create(nil);
    try
      { Created in code, so it installs when told to; one dropped on a form
        with Engine set installs by itself when the form has loaded. }
      lRoots.Install(Result.TLSServer);
    finally
      lRoots.Free;   // the installed roots stay in the client's store
    end;
    Result.Host := aHost;
  except
    Result.Free;
    raise;
  end;
end;

On a form, drop a TsmCCADBTrustAnchors beside a TLS engine component - the one a TsmAdvancedHTTPSClient names as its TLSServer, for instance - and set its Engine to it; the roots go in when the form has loaded. Created in code, as above, it installs when Install is called.

Host names

Trusting the issuer is half of the check: the certificate must also be issued for the name in the client's Host, which the client checks on every connection. So connect by the DNS name the certificate carries, not by the server's IP address - a certificate issued for localhost does not match 127.0.0.1, and the connection fails although the certificate is valid. Download a page over HTTPS shows how to connect by address when you must.

Revocation

Whether a revoked certificate is refused, and what happens when the revocation status cannot be fetched, is the engine's RevocationPolicy. The TLS notes describe the policies.

Reference

TsmHTTPSClient · TsmCCADBTrustAnchors · StreamSec.Mobile.CCADB · StreamSec.Mobile.TlsInternalServer

Related tasks