Sample code for 30+ languages & platforms
PureBasic

Load PEM and List Certificates

Demonstrates how to load a PEM containing certificates and accesses each individual certificate.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPem.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    success = 0

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

    ; Load the .p7b from a file.
    success = CkPem::ckLoadPemFile(pem,"/Users/chilkat/testData/p7b/cacert_mozilla.pem")
    If success <> 1
        Debug CkPem::ckLastErrorText(pem)
        CkPem::ckDispose(pem)
        ProcedureReturn
    EndIf

    i.i
    numCerts.i = CkPem::ckNumCerts(pem)
    If numCerts = 0
        Debug ("Error: Expected the PEM to contain certificates.")
        CkPem::ckDispose(pem)
        ProcedureReturn
    EndIf

    ; Access each certificate and show the Subject CN (Common Name)
    For i = 1 To numCerts
        cert.i = CkPem::ckGetCert(pem,i - 1)
        Debug Str(i) + ": " + CkCert::ckSubjectCN(cert)
        CkCert::ckDispose(cert)

    Next


    CkPem::ckDispose(pem)


    ProcedureReturn
EndProcedure