Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

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

loPem = createobject("CkPem")

// Load the PEM from a file.
// If the PEM is encrypted, provide a password.  Otherwise pass an empty string for the password.
lcPassword = "myPassword"
llSuccess = loPem.LoadPemFile("../myPemFiles/myPem.pem",lcPassword)
if (llSuccess = .F.) then
    ? loPem.LastErrorText
    release loPem
    return
endif

// Note: If the app already has the PEM pre-loaded in a string variable, then load it 
// by calling LoadPem instead.  
lcPemContent = "... the PEM contents ..."
llSuccess = loPem.LoadPem(lcPemContent,lcPassword)
// Check for success as before..

// Iterate over the private keys.
lnNumPrivateKeys = loPem.NumPrivateKeys
i = 0

loPrivKey = createobject("CkPrivateKey")
do while i < lnNumPrivateKeys
    loPem.PrivateKeyAt(i,loPrivKey)
    ? "Private Key " + str(i) + " is " + str(loPrivKey.BitLength) + " in length"
    i = i + 1
enddo

// Iterate over the certificates.
loCert = createobject("CkCert")
lnNumCerts = loPem.NumCerts
i = 0
do while i < lnNumCerts
    loPem.CertAt(i,loCert)
    ? "Certificate " + str(i) + " : " + loCert.SubjectDN
    i = i + 1
enddo


release loPem
release loPrivKey
release loCert