Sample code for 30+ languages & platforms
PureBasic

Get the Server Certificate, Certificate Chain, and Root CA Certificate

See more HTTP Examples

Demonstrates how to get the HTTP server certificate, its certificate chain, and the root CA certificate.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkCertChain.pb"

Procedure ChilkatExample()

    success.i = 0

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

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

    ; We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
    sslCert.i = CkCert::ckCreate()
    If sslCert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckGetServerCert(http,"apple.com",443,sslCert)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkCert::ckDispose(sslCert)
        ProcedureReturn
    EndIf

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

    success = CkCert::ckBuildCertChain(sslCert,certChain)
    If success = 0
        Debug CkCert::ckLastErrorText(sslCert)
        CkHttp::ckDispose(http)
        CkCert::ckDispose(sslCert)
        CkCertChain::ckDispose(certChain)
        ProcedureReturn
    EndIf

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

    i.i = 0
    numCerts.i = CkCertChain::ckNumCerts(certChain)
    While i < numCerts
        CkCertChain::ckCertAt(certChain,i,cert)
        Debug "SubjectDN " + Str(i) + ": " + CkCert::ckSubjectDN(cert)
        Debug "IssuerDN " + Str(i) + ": " + CkCert::ckIssuerDN(cert)
        i = i + 1
    Wend

    ; If the certificate chain reaches the root CA cert, then the last cert in the chain
    ; is the root CA cert.
    If CkCertChain::ckReachesRoot(certChain) = 1
        caCert.i = CkCert::ckCreate()
        If caCert.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkCertChain::ckCertAt(certChain,numCerts - 1,caCert)
        Debug "CA Root Cert: " + CkCert::ckSubjectDN(caCert)
    EndIf



    CkHttp::ckDispose(http)
    CkCert::ckDispose(sslCert)
    CkCertChain::ckDispose(certChain)
    CkCert::ckDispose(cert)
    CkCert::ckDispose(caCert)


    ProcedureReturn
EndProcedure