Sample code for 30+ languages & platforms
Delphi ActiveX

Get a Received Client Certificate (Server Side)

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetRcvdClientCert, which copies a client certificate presented during the TLS handshake of an accepted server-side connection into a Cert object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Use NumReceivedClientCerts to determine the valid index range. Client certificates are supplied only when the server requests them for mutual TLS.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
socket: TChilkatSocket;
pfxPassword: WideString;
cert: TChilkatCert;
connectedSock: TChilkatSocket;
numClientCerts: Integer;
clientCert: TChilkatCert;
i: Integer;

begin
success := 0;

socket := TChilkatSocket.Create(Self);

//  The file path is relative to the application's current working directory.  An absolute path
//  may also be used.  Supply the path appropriate to your own environment.
//  The PFX password should come from a secure source rather than being hard-coded.
pfxPassword := 'myPfxPassword';
cert := TChilkatCert.Create(Self);
success := cert.LoadPfxFile('qa_data/server.pfx',pfxPassword);
if (success = 0) then
  begin
    Memo1.Lines.Add(cert.LastErrorText);
    Exit;
  end;

success := socket.InitSslServer(cert.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(socket.LastErrorText);
    Exit;
  end;
success := socket.BindAndListen(5000,25);
if (success = 0) then
  begin
    Memo1.Lines.Add(socket.LastErrorText);
    Exit;
  end;

//  Accept a client connection.  The accepted connection carries any client certificate presented
//  during the TLS handshake.
connectedSock := TChilkatSocket.Create(Self);
success := socket.AcceptNext(20000,connectedSock.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(socket.LastErrorText);
    Exit;
  end;

//  Copy each received client certificate and inspect it.
numClientCerts := connectedSock.NumReceivedClientCerts;
clientCert := TChilkatCert.Create(Self);

for i := 0 to numClientCerts - 1 do
  begin
    success := connectedSock.GetRcvdClientCert(i,clientCert.ControlInterface);
    if (success = 0) then
      begin
        Memo1.Lines.Add(connectedSock.LastErrorText);
        Exit;
      end;
    Memo1.Lines.Add('Client certificate subject: ' + clientCert.SubjectCN);
  end;