Sample code for 30+ languages & platforms
PureBasic

Load PFX/P12 File into Certificate Store Object

Demonstrates how to load a .pfx/.p12 into a certificate store object.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkCertStore.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; This only loads the contents of the PFX file into the certStore object.
    ; It is not importing the PFX into the Windows certificate stores.
    pfxPassword.s = "badssl.com"
    success = CkCertStore::ckLoadPfxFile(certStore,"qa_data/pfx/badssl.com-client.p12",pfxPassword)
    If success = 0
        Debug CkCertStore::ckLastErrorText(certStore)
        CkCertStore::ckDispose(certStore)
        ProcedureReturn
    EndIf

    ; Examine each certificate (loaded from the PFX) in this certStore object
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    numCerts.i = CkCertStore::ckNumCertificates(certStore)
    i.i = 0
    While i < numCerts
        CkCertStore::ckGetCert(certStore,i,cert)
        Debug "hasPrivateKey=" + Str(CkCert::ckHasPrivateKey(cert)) + ", " + CkCert::ckSubjectCN(cert)
        i = i + 1
    Wend


    CkCertStore::ckDispose(certStore)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure