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

Load PKCS12 / PFX and Access Contents

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPfx
LOCAL nNumPrivateKeys
LOCAL oPrivKey
LOCAL i
LOCAL oCert
LOCAL nNumCerts

nSuccess := 0

oPfx := CreateObject("Chilkat.Pfx")

//  Load the PKCS12 from a file
nSuccess := oPfx:LoadPfxFile("/someDir/my.p12", "pfxFilePassword")
IF (nSuccess == 0)
    ? oPfx:LastErrorText
    oPfx:destroy()
    RETURN
ENDIF

nNumPrivateKeys := oPfx:NumPrivateKeys

oPrivKey := CreateObject("Chilkat.PrivateKey")

? "Private Keys:"

i := 0
DO WHILE i < nNumPrivateKeys
    oPfx:PrivateKeyAt(i, oPrivKey)

    //  Do something with the private key ...

    i := i + 1
ENDDO

oCert := CreateObject("Chilkat.Cert")

nNumCerts := oPfx:NumCerts

? "Certs:"
i := 0
DO WHILE i < nNumCerts
    oPfx:CertAt(i, oCert)
    ? oCert:SubjectDN

    //  If the certificate has a private key (one of the private keys within the PFX)
    //  then it can also be obtained via the certificate object:
    IF (oCert:HasPrivateKey() == 1)

        ? "Has private key!"

        nSuccess := oCert:GetPrivateKey(oPrivKey)
        //  ...

    ENDIF

    i := i + 1
ENDDO

oPfx:destroy()
oPrivKey:destroy()
oCert:destroy()