Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

Iterate Keys and Certs in PEM

See more PEM Examples

Demonstrates how to access each of the private keys and certs contained within a PEM.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pem: HCkPem;
password: PWideChar;
pemContent: PWideChar;
numPrivateKeys: Integer;
i: Integer;
privKey: HCkPrivateKey;
cert: HCkCert;
numCerts: Integer;

begin
success := False;

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

pem := CkPem_Create();

//  Load the PEM from a file.
//  If the PEM is encrypted, provide a password.  Otherwise pass an empty string for the password.
password := 'myPassword';
success := CkPem_LoadPemFile(pem,'../myPemFiles/myPem.pem',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPem__lastErrorText(pem));
    Exit;
  end;

//  Note: If the app already has the PEM pre-loaded in a string variable, then load it 
//  by calling LoadPem instead.  
pemContent := '... the PEM contents ...';
success := CkPem_LoadPem(pem,pemContent,password);
//  Check for success as before..

//  Iterate over the private keys.
numPrivateKeys := CkPem_getNumPrivateKeys(pem);
i := 0;

privKey := CkPrivateKey_Create();
while i < numPrivateKeys do
  begin
    CkPem_PrivateKeyAt(pem,i,privKey);
    Memo1.Lines.Add('Private Key ' + IntToStr(i) + ' is ' + IntToStr(CkPrivateKey_getBitLength(privKey)) + ' in length');
    i := i + 1;
  end;

//  Iterate over the certificates.
cert := CkCert_Create();
numCerts := CkPem_getNumCerts(pem);
i := 0;
while i < numCerts do
  begin
    CkPem_CertAt(pem,i,cert);
    Memo1.Lines.Add('Certificate ' + IntToStr(i) + ' : ' + CkCert__subjectDN(cert));
    i := i + 1;
  end;

CkPem_Dispose(pem);
CkPrivateKey_Dispose(privKey);
CkCert_Dispose(cert);