Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
local sslCert = chilkat.Cert.new()
local success = http:get_server_cert("apple.com", 443, sslCert)
if not success then
print(http.last_error_text)
return
end
local certChain = chilkat.CertChain.new()
success = sslCert:build_cert_chain(certChain)
if not success then
print(sslCert.last_error_text)
return
end
local cert = chilkat.Cert.new()
local i = 0
local numCerts = certChain.num_certs
while i < numCerts do
certChain:cert_at(i, cert)
print("SubjectDN " .. i .. ": " .. cert.subject_dn)
print("IssuerDN " .. i .. ": " .. cert.issuer_dn)
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.reaches_root then
local caCert = chilkat.Cert.new()
certChain:cert_at(numCerts - 1, caCert)
print("CA Root Cert: " .. caCert.subject_dn)
end