Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkCertW.h>
#include <C_CkCertChainW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkCertW sslCert;
    HCkCertChainW certChain;
    HCkCertW cert;
    int i;
    int numCerts;
    HCkCertW caCert;

    success = FALSE;

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

    http = CkHttpW_Create();

    // We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
    sslCert = CkCertW_Create();
    success = CkHttpW_GetServerCert(http,L"apple.com",443,sslCert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkCertW_Dispose(sslCert);
        return;
    }

    certChain = CkCertChainW_Create();
    success = CkCertW_BuildCertChain(sslCert,certChain);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(sslCert));
        CkHttpW_Dispose(http);
        CkCertW_Dispose(sslCert);
        CkCertChainW_Dispose(certChain);
        return;
    }

    cert = CkCertW_Create();
    i = 0;
    numCerts = CkCertChainW_getNumCerts(certChain);
    while (i < numCerts) {
        CkCertChainW_CertAt(certChain,i,cert);
        wprintf(L"SubjectDN %d: %s\n",i,CkCertW_subjectDN(cert));
        wprintf(L"IssuerDN %d: %s\n",i,CkCertW_issuerDN(cert));
        i = i + 1;
    }

    // If the certificate chain reaches the root CA cert, then the last cert in the chain
    // is the root CA cert.
    if (CkCertChainW_getReachesRoot(certChain) == TRUE) {
        caCert = CkCertW_Create();
        CkCertChainW_CertAt(certChain,numCerts - 1,caCert);
        wprintf(L"CA Root Cert: %s\n",CkCertW_subjectDN(caCert));
    }



    CkHttpW_Dispose(http);
    CkCertW_Dispose(sslCert);
    CkCertChainW_Dispose(certChain);
    CkCertW_Dispose(cert);
    CkCertW_Dispose(caCert);

    }