Lua
Lua
Get a Received Client Certificate (Server Side)
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetRcvdClientCert, which copies a client certificate presented during the TLS handshake of an accepted server-side connection into a Cert object.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. Use
NumReceivedClientCerts to determine the valid index range. Client certificates are supplied only when the server requests them for mutual TLS.Chilkat Lua Downloads
local chilkat = require("chilkat")
local socket = chilkat.Socket.new()
-- The file path is relative to the application's current working directory. An absolute path
-- may also be used. Supply the path appropriate to your own environment.
-- The PFX password should come from a secure source rather than being hard-coded.
local pfxPassword = "myPfxPassword"
local cert = chilkat.Cert.new()
local success = cert:load_pfx_file("qa_data/server.pfx", pfxPassword)
if not success then
print(cert.last_error_text)
return
end
success = socket:init_ssl_server(cert)
if not success then
print(socket.last_error_text)
return
end
success = socket:bind_and_listen(5000, 25)
if not success then
print(socket.last_error_text)
return
end
-- Accept a client connection. The accepted connection carries any client certificate presented
-- during the TLS handshake.
local connectedSock = chilkat.Socket.new()
success = socket:accept_next(20000, connectedSock)
if not success then
print(socket.last_error_text)
return
end
-- Copy each received client certificate and inspect it.
local numClientCerts = connectedSock.num_received_client_certs
local clientCert = chilkat.Cert.new()
for i = 0, numClientCerts - 1 do
success = connectedSock:get_rcvd_client_cert(i, clientCert)
if not success then
print(connectedSock.last_error_text)
return
end
print("Client certificate subject: " .. clientCert.subject_cn)
end