Sample code for 30+ languages & platforms
Delphi DLL

Enumerate Certificates in a PFX

See more PFX/P12 Examples

Demonstrates Pfx.CertAt, which copies the certificate at a zero-based index into a Cert object. The example enumerates all certificates in the PFX.

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. Valid indexes are less than NumCerts. Each retrieved certificate is an independent copy that can be inspected or used separately.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pfx: HCkPfx;
password: PWideChar;
numCerts: Integer;
cert: HCkCert;
i: Integer;

begin
success := False;

pfx := CkPfx_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.
password := 'myPfxPassword';
success := CkPfx_LoadPfxFile(pfx,'qa_data/identity.pfx',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPfx__lastErrorText(pfx));
    Exit;
  end;

//  Enumerate the certificates in the PFX by zero-based index.  Valid indexes are less than NumCerts.
//  The certificate at index 0 is typically the certificate that has a private key, and the remaining
//  certificates are the others in its certificate chain.
numCerts := CkPfx_getNumCerts(pfx);
cert := CkCert_Create();

for i := 0 to numCerts - 1 do
  begin
    success := CkPfx_CertAt(pfx,i,cert);
    if (success = False) then
      begin
        Memo1.Lines.Add(CkPfx__lastErrorText(pfx));
        Exit;
      end;
    Memo1.Lines.Add('Certificate ' + IntToStr(i) + ': ' + CkCert__subjectCN(cert));
  end;

CkPfx_Dispose(pfx);
CkCert_Dispose(cert);