Sample code for 30+ languages & platforms
DataFlex

Load Identities from PEM Text

See more PFX/P12 Examples

Demonstrates Pfx.LoadPem, which loads one or more certificate/private-key identities from PEM-formatted text and builds the contents of the Pfx object.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The password decrypts any encrypted private keys in the PEM. This builds a PFX in memory from PEM material, which can then be serialized to PKCS#12.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPfx
    Handle hoSbPem
    Boolean iBLoaded
    String sPemText
    String sPassword
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    //  The file path is relative to the application's current working directory.  An absolute path
    //  may also be used.  Supply the path appropriate to your own environment.
    //  Load the PEM text (certificates and private keys) into a string.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPem
    If (Not(IsComObjectCreated(hoSbPem))) Begin
        Send CreateComObject of hoSbPem
    End
    Get ComLoadFile Of hoSbPem "qa_data/identity.pem" To iBLoaded
    If (iBLoaded <> True) Begin
        Showln "Failed to load the PEM file."
        Procedure_Return
    End

    Get ComGetAsString Of hoSbPem To sPemText
    Get ComLastMethodSuccess Of hoSbPem To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSbPem To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The password decrypts any encrypted private keys in the PEM (use an empty string if none are
    //  encrypted).  A password should come from a secure source rather than being hard-coded.
    Move "myPemPassword" To sPassword
    Get ComLoadPem Of hoPfx sPemText sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumPrivateKeys Of hoPfx To iTemp1
    Showln "Loaded " iTemp1 " private key(s)."


End_Procedure