Sample code for 30+ languages & platforms
Ruby

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 Ruby Downloads

Ruby
require 'chilkat'

success = false

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

http = Chilkat::CkHttp.new()

# We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
sslCert = Chilkat::CkCert.new()
success = http.GetServerCert("apple.com",443,sslCert)
if (success == false)
    print http.lastErrorText() + "\n";
    exit
end

certChain = Chilkat::CkCertChain.new()
success = sslCert.BuildCertChain(certChain)
if (success == false)
    print sslCert.lastErrorText() + "\n";
    exit
end

cert = Chilkat::CkCert.new()
i = 0
numCerts = certChain.get_NumCerts()
while i < numCerts
    certChain.CertAt(i,cert)
    print "SubjectDN " + i.to_s() + ": " + cert.subjectDN() + "\n";
    print "IssuerDN " + i.to_s() + ": " + cert.issuerDN() + "\n";
    i = i + 1
end

# If the certificate chain reaches the root CA cert, then the last cert in the chain
# is the root CA cert.
if (certChain.get_ReachesRoot() == true)
    caCert = Chilkat::CkCert.new()
    certChain.CertAt(numCerts - 1,caCert)
    print "CA Root Cert: " + caCert.subjectDN() + "\n";
end