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.
ServerName to the name in the certificate - see Download a page over HTTPS.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"
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.
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.
PeerIssuer with PeerSerialNumber as the key in your own list of clients. Together they name exactly one certificate; a subject name is only unique within one CA, and its text form can vary.PeerCertificatePresent is False there. Decide what a handler does in that case rather than assuming it cannot happen.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.
TsmAsyncTLSServer · TsmHTTPSClient · StreamSec.Mobile.AsyncTLSServer · StreamSec.Http.Server