Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oSslCert
LOCAL oCertChain
LOCAL oCert
LOCAL i
LOCAL nNumCerts
LOCAL oCaCert

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

//  We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
oSslCert := CreateObject("Chilkat.Cert")
nSuccess := oHttp:GetServerCert("apple.com", 443, oSslCert)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oSslCert:destroy()
    RETURN
ENDIF

oCertChain := CreateObject("Chilkat.CertChain")
nSuccess := oSslCert:BuildCertChain(oCertChain)
IF (nSuccess == 0)
    ? oSslCert:LastErrorText
    oHttp:destroy()
    oSslCert:destroy()
    oCertChain:destroy()
    RETURN
ENDIF

oCert := CreateObject("Chilkat.Cert")
i := 0
nNumCerts := oCertChain:NumCerts
DO WHILE i < nNumCerts
    oCertChain:CertAt(i, oCert)
    ? "SubjectDN " + Str(i) + ": " + oCert:SubjectDN
    ? "IssuerDN " + Str(i) + ": " + oCert:IssuerDN
    i := i + 1
ENDDO

//  If the certificate chain reaches the root CA cert, then the last cert in the chain
//  is the root CA cert.
IF (oCertChain:ReachesRoot == 1)
    oCaCert := CreateObject("Chilkat.Cert")
    oCertChain:CertAt(nNumCerts - 1, oCaCert)
    ? "CA Root Cert: " + oCaCert:SubjectDN
ENDIF

oHttp:destroy()
oSslCert:destroy()
oCertChain:destroy()
oCert:destroy()
oCaCert:destroy()