Sample code for 30+ languages & platforms
JavaScript

Verify SSL Server Certificate

See more Socket/SSL/TLS Examples

Demonstrates how to connect to an SSL server and verify its SSL certificate.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var socket = new CkSocket();

var ssl = true;
var maxWaitMillisec = 20000;

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

// Connect to the SSL server:
success = socket.Connect(sslServerHost,sslServerPort,ssl,maxWaitMillisec);
if (success == false) {
    console.log(socket.LastErrorText);
    return;
}

var cert = new CkCert();

var bExpired;
var bRevoked;
var bSignatureVerified;
var bTrustedRoot;

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

    console.log("Server Certificate:");
    console.log("Distinguished Name: " + cert.SubjectDN);
    console.log("Common Name: " + cert.SubjectCN);
    console.log("Issuer Distinguished Name: " + cert.IssuerDN);
    console.log("Issuer Common Name: " + cert.IssuerCN);

    bExpired = cert.Expired;
    bRevoked = cert.Revoked;
    bSignatureVerified = cert.SignatureVerified;
    bTrustedRoot = cert.TrustedRoot;

    console.log("Expired: " + bExpired);
    console.log("Revoked: " + bRevoked);
    console.log("Signature Verified: " + bSignatureVerified);
    console.log("Trusted Root: " + bTrustedRoot);

}

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