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

Examine Client Certificates for an Accepted TLS Connection

See more Socket/SSL/TLS Examples

Demonstrates how to access the client certificates for a TLS connection accepted by your application acting as the server.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local listenSslSocket = chilkat.Socket.new()

-- An SSL/TLS server needs a digital certificate.  This example loads it from a PFX file.
-- This is the server's certificate.

local cert = chilkat.Cert.new()
local success = cert:load_pfx_file("qa_data/serverCert/myServerCert.pfx", "pfx_password")
if not success then
    print(cert.last_error_text)
    return
end

-- To accept client client certificates in the TLS handshake,
-- we must indicate a list of acceptable client certificate root CA DN's
-- that are allowed.  (DN is an acronym for Distinguished Name.)
-- Call AddSslAcceptableClientCaDn once for each acceptable CA DN.
-- Here are a few examples so you can see the general format of a DN.
listenSslSocket:add_ssl_acceptable_client_ca_dn("C=SE, O=AddTrust AB, OU=AddTrust External TTP Network, CN=AddTrust External CA Root")
listenSslSocket:add_ssl_acceptable_client_ca_dn("O=Digital Signature Trust Co., CN=DST Root CA X3")

-- Initialize with our server's TLS certificate.
success = listenSslSocket:init_ssl_server(cert)
if not success then
    print(listenSslSocket.last_error_text)
    return
end

-- Bind and listen on a port:
local myPort = 8123
-- Allow for a max of 5 queued connect requests.
local backLog = 5
success = listenSslSocket:bind_and_listen(myPort, backLog)
if not success then
    print(listenSslSocket.last_error_text)
    return
end

-- Accept the next incoming connection.
local maxWaitMillisec = 20000

local clientSock = chilkat.Socket.new()
success = listenSslSocket:accept_next(maxWaitMillisec, clientSock)
if not success then
    print(listenSslSocket.last_error_text)
    return
end

-- Examine the client certs chain.  The 1st cert will be the client certificate, and
-- the subsequent certs will be the certs in the chain of authentication.
local numClientCerts = clientSock.num_received_client_certs
print("numClientCerts = " .. numClientCerts)

local clientCert = chilkat.Cert.new()
local i = 0
while i < numClientCerts do
    clientSock:get_rcvd_client_cert(i, clientCert)
    print(clientCert.subject_dn)
    i = i + 1
end

-- Close the connection with the client
success = clientSock:close(1000)