Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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

Lua

    -- In the following call to loadlib, change the path (./chilkat.dll) to the relative or absolute directory where the chilkat.dll, chilkat.so, or chilkat.dylib is located.
    chilkat = assert(package.loadlib("./chilkat.dll", "luaopen_chilkat"))()
    print(chilkat._VERSION)

    local success = false

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

    local http = chilkat.newHttp{}

    --  We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
    local sslCert = chilkat.newCert{}
    success = http:GetServerCert("apple.com",443,sslCert)
    if success == false then
        print(http:LastErrorText())

    end

    local certChain = chilkat.newCertChain{}
    success = sslCert:BuildCertChain(certChain)
    if success == false then
        print(sslCert:LastErrorText())

    end

    local cert = chilkat.newCert{}
    local i = 0
    local numCerts = certChain:NumCerts()
    while i < numCerts do
        certChain:CertAt(i,cert)
        print("SubjectDN ", i, ": ", cert:SubjectDN())
        print("IssuerDN ", i, ": ", cert:IssuerDN())
        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:ReachesRoot() == true then
        local caCert = chilkat.newCert{}
        certChain:CertAt(numCerts - 1,caCert)
        print("CA Root Cert: ", caCert:SubjectDN())
    end