Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPem
LOCAL cPassword
LOCAL cPemContent
LOCAL nNumPrivateKeys
LOCAL i
LOCAL oPrivKey
LOCAL oCert
LOCAL nNumCerts

nSuccess := 0

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

oPem := CreateObject("Chilkat.Pem")

//  Load the PEM from a file.
//  If the PEM is encrypted, provide a password.  Otherwise pass an empty string for the password.
cPassword := "myPassword"
nSuccess := oPem:LoadPemFile("../myPemFiles/myPem.pem", cPassword)
IF (nSuccess == 0)
    ? oPem:LastErrorText
    oPem:destroy()
    RETURN
ENDIF

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

//  Iterate over the private keys.
nNumPrivateKeys := oPem:NumPrivateKeys
i := 0

oPrivKey := CreateObject("Chilkat.PrivateKey")
DO WHILE i < nNumPrivateKeys
    oPem:PrivateKeyAt(i, oPrivKey)
    ? "Private Key " + Str(i) + " is " + Str(oPrivKey:BitLength) + " in length"
    i := i + 1
ENDDO

//  Iterate over the certificates.
oCert := CreateObject("Chilkat.Cert")
nNumCerts := oPem:NumCerts
i := 0
DO WHILE i < nNumCerts
    oPem:CertAt(i, oCert)
    ? "Certificate " + Str(i) + " : " + oCert:SubjectDN
    i := i + 1
ENDDO

oPem:destroy()
oPrivKey:destroy()
oCert:destroy()