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

Get the Server Certificate, Certificate Chain, and Root CA Certificate

See more HTTP Examples

Demonstrates how to get the HTTP server certificate, its certificate chain, and the root CA certificate.

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.Http,
  Chilkat.CertChain,
  Chilkat.Cert;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  sslCert: TCert;
  certChain: TCertChain;
  cert: TCert;
  i: Integer;
  numCerts: Integer;
  caCert: TCert;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  //  We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
  sslCert := TCert.Create;
  success := http.GetServerCert('apple.com',443,sslCert);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  certChain := TCertChain.Create;
  success := sslCert.BuildCertChain(certChain);
  if (success = False) then
    begin
      WriteLn(sslCert.LastErrorText);
      Exit;
    end;

  cert := TCert.Create;
  i := 0;
  numCerts := certChain.NumCerts;
  while i < numCerts do
    begin
      certChain.CertAt(i,cert);
      WriteLn('SubjectDN ' + i + ': ' + cert.SubjectDN);
      WriteLn('IssuerDN ' + i + ': ' + cert.IssuerDN);
      i := i + 1;
    end;

  //  If the certificate chain reaches the root CA cert, then the last cert in the chain
  //  is the root CA cert.
  if (certChain.ReachesRoot = True) then
    begin
      caCert := TCert.Create;
      certChain.CertAt(numCerts - 1,caCert);
      WriteLn('CA Root Cert: ' + caCert.SubjectDN);
    end;


  http.Free;
  sslCert.Free;
  certChain.Free;
  cert.Free;
      caCert.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.