Sample code for 30+ languages & platforms
C++

Verify SSL Server Certificate

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server and verify its SSL certificate.

Chilkat C++ Downloads

C++
#include <CkSocket.h>
#include <CkCert.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkSocket socket;

    bool ssl = true;
    int maxWaitMillisec = 20000;

    // The SSL server hostname may be an IP address, a domain name,
    // or "localhost". 
    const char *sslServerHost = 0;
    sslServerHost = "www.paypal.com";
    int sslServerPort = 443;

    // Connect to the SSL server:
    success = socket.Connect(sslServerHost,sslServerPort,ssl,maxWaitMillisec);
    if (success == false) {
        std::cout << socket.lastErrorText() << "\r\n";
        return;
    }

    CkCert cert;

    bool bExpired;
    bool bRevoked;
    bool bSignatureVerified;
    bool bTrustedRoot;

    success = socket.GetServerCert(cert);
    if (success != false) {

        std::cout << "Server Certificate:" << "\r\n";
        std::cout << "Distinguished Name: " << cert.subjectDN() << "\r\n";
        std::cout << "Common Name: " << cert.subjectCN() << "\r\n";
        std::cout << "Issuer Distinguished Name: " << cert.issuerDN() << "\r\n";
        std::cout << "Issuer Common Name: " << cert.issuerCN() << "\r\n";

        bExpired = cert.get_Expired();
        bRevoked = cert.get_Revoked();
        bSignatureVerified = cert.get_SignatureVerified();
        bTrustedRoot = cert.get_TrustedRoot();

        std::cout << "Expired: " << bExpired << "\r\n";
        std::cout << "Revoked: " << bRevoked << "\r\n";
        std::cout << "Signature Verified: " << bSignatureVerified << "\r\n";
        std::cout << "Trusted Root: " << bTrustedRoot << "\r\n";

    }

    // Close the connection with the server
    // Wait a max of 20 seconds (20000 millsec)
    success = socket.Close(20000);
    }