Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPfx
    Integer iNumPrivateKeys
    Variant vPrivKey
    Handle hoPrivKey
    Integer i
    Variant vCert
    Handle hoCert
    Integer iNumCerts
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatPfx)) To hoPfx
    If (Not(IsComObjectCreated(hoPfx))) Begin
        Send CreateComObject of hoPfx
    End

    // Load the PKCS12 from a file
    Get ComLoadPfxFile Of hoPfx "/someDir/my.p12" "pfxFilePassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumPrivateKeys Of hoPfx To iNumPrivateKeys

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End

    Showln "Private Keys:"

    Move 0 To i
    While (i < iNumPrivateKeys)
        Get pvComObject of hoPrivKey to vPrivKey
        Get ComPrivateKeyAt Of hoPfx i vPrivKey To iSuccess

        // Do something with the private key ...

        Move (i + 1) To i
    Loop

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End

    Get ComNumCerts Of hoPfx To iNumCerts

    Showln "Certs:"
    Move 0 To i
    While (i < iNumCerts)
        Get pvComObject of hoCert to vCert
        Get ComCertAt Of hoPfx i vCert To iSuccess
        Get ComSubjectDN Of hoCert To sTemp1
        Showln sTemp1

        // 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:
        Get ComHasPrivateKey Of hoCert To bTemp1
        If (bTemp1 = True) Begin

            Showln "Has private key!"

            Get pvComObject of hoPrivKey to vPrivKey
            Get ComGetPrivateKey Of hoCert vPrivKey To iSuccess
            // ...

        End

        Move (i + 1) To i
    Loop



End_Procedure