Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v10.1.2+

Load PFX/P12 File into Certificate Store Object

Demonstrates how to load a .pfx/.p12 into a certificate store object.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.CertStore;

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

procedure RunDemo;
var
  success: Boolean;
  certStore: TCertStore;
  pfxPassword: string;
  cert: TCert;
  numCerts: Integer;
  i: Integer;

begin
  success := False;

  certStore := TCertStore.Create;

  //  This only loads the contents of the PFX file into the certStore object.
  //  It is not importing the PFX into the Windows certificate stores.
  pfxPassword := 'badssl.com';
  success := certStore.LoadPfxFile('qa_data/pfx/badssl.com-client.p12',pfxPassword);
  if (success = False) then
    begin
      WriteLn(certStore.LastErrorText);
      Exit;
    end;

  //  Examine each certificate (loaded from the PFX) in this certStore object
  cert := TCert.Create;
  numCerts := certStore.NumCertificates;
  i := 0;
  while i < numCerts do
    begin
      certStore.GetCert(i,cert);
      WriteLn('hasPrivateKey=' + cert.HasPrivateKey() + ', ' + cert.SubjectCN);
      i := i + 1;
    end;



  certStore.Free;
  cert.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.