Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Cert,
  Chilkat.Socket;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  socket: TSocket;
  pfxPassword: string;
  cert: TCert;
  connectedSock: TSocket;
  numClientCerts: Integer;
  clientCert: TCert;
  i: Integer;

begin
  success := False;

  socket := TSocket.Create;

  //  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 := TCert.Create;
  success := cert.LoadPfxFile('qa_data/server.pfx',pfxPassword);
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  success := socket.InitSslServer(cert);
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;
  success := socket.BindAndListen(5000,25);
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;

  //  Accept a client connection.  The accepted connection carries any client certificate presented
  //  during the TLS handshake.
  connectedSock := TSocket.Create;
  success := socket.AcceptNext(20000,connectedSock);
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;

  //  Copy each received client certificate and inspect it.
  numClientCerts := connectedSock.NumReceivedClientCerts;
  clientCert := TCert.Create;

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



  socket.Free;
  cert.Free;
  connectedSock.Free;
  clientCert.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.