Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkPfx.pb"

Procedure ChilkatExample()

    success.i = 0

    pfx.i = CkPfx::ckCreate()
    If pfx.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  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.
    sbPem.i = CkStringBuilder::ckCreate()
    If sbPem.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bLoaded.i = CkStringBuilder::ckLoadFile(sbPem,"qa_data/identity.pem")
    If bLoaded <> 1
        Debug "Failed to load the PEM file."
        CkPfx::ckDispose(pfx)
        CkStringBuilder::ckDispose(sbPem)
        ProcedureReturn
    EndIf

    pemText.s = CkStringBuilder::ckGetAsString(sbPem)
    If CkStringBuilder::ckLastMethodSuccess(sbPem) = 0
        Debug CkStringBuilder::ckLastErrorText(sbPem)
        CkPfx::ckDispose(pfx)
        CkStringBuilder::ckDispose(sbPem)
        ProcedureReturn
    EndIf

    ;  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.
    password.s = "myPemPassword"
    success = CkPfx::ckLoadPem(pfx,pemText,password)
    If success = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        CkStringBuilder::ckDispose(sbPem)
        ProcedureReturn
    EndIf

    Debug "Loaded " + Str(CkPfx::ckNumPrivateKeys(pfx)) + " private key(s)."


    CkPfx::ckDispose(pfx)
    CkStringBuilder::ckDispose(sbPem)


    ProcedureReturn
EndProcedure