Sample code for 30+ languages & platforms
PureBasic

Load a PFX from BinData

See more PFX/P12 Examples

Demonstrates Pfx.LoadPfxBd, which loads a PFX / PKCS#12 container from the bytes held in a BinData 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. This is the in-memory equivalent of LoadPfxFile, for when the PKCS#12 data is already available as bytes rather than in a file.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.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 PFX bytes into a BinData.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    ;  The PFX password should come from a secure source rather than being hard-coded.
    password.s = "myPfxPassword"

    ;  Load the PFX / PKCS#12 container from the bytes held in the BinData.
    success = CkPfx::ckLoadPfxBd(pfx,bd,password)
    If success = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "Loaded " + Str(CkPfx::ckNumCerts(pfx)) + " certificate(s)."


    CkPfx::ckDispose(pfx)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure