Lua Requires Chilkat v10.1.2+
Lua
SSL Client Certificate
See more Socket/SSL/TLS Examples
Demonstrates how to connect to an SSL server using a client-side certificate, send a simple message, receive a simple response, and disconnect.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local socket = chilkat.Socket.new()
-- Create an instance of a certificate store object, load a PFX file,
-- locate the certificate we need, and use it for signing.
-- (a PFX file may contain more than one certificate.)
local certStore = chilkat.CertStore.new()
-- The 1st argument is the filename, the 2nd arg is the
-- PFX file's password:
local success = certStore:load_pfx_file("chilkat_secret.pfx", "secret")
if not success then
print(certStore.last_error_text)
return
end
-- Find the certificate by the subject common name:
local jsonCN = chilkat.JsonObject.new()
jsonCN:update_string("CN", "cert common name")
local cert = chilkat.Cert.new()
success = certStore:find_cert(jsonCN, cert)
if not success then
print(certStore.last_error_text)
return
end
success = socket:set_ssl_client_cert(cert)
local ssl = true
local maxWaitMillisec = 20000
-- The SSL server hostname may be an IP address, a domain name,
-- or "localhost". You'll need to change this:
local sslServerHost = "123.123.88.88"
local sslServerPort = 8123
-- Connect to the SSL server:
success = socket:connect(sslServerHost, sslServerPort, ssl, maxWaitMillisec)
if not success then
print(socket.last_error_text)
return
end
-- Set maximum timeouts for reading an writing (in millisec)
socket.max_read_idle_ms = 20000
socket.max_send_idle_ms = 20000
-- Send a "Hello Server! -EOM-" message:
success = socket:send_string("Hello Server! -EOM-")
if not success then
print(socket.last_error_text)
return
end
-- The server (in this example) is going to send a "Hello Client! -EOM-"
-- message. Read it:
local receivedMsg = socket:receive_until_match("-EOM-")
if not socket.last_method_success then
print(socket.last_error_text)
return
end
-- Close the connection with the server
-- Wait a max of 20 seconds (20000 millsec)
success = socket:close(20000)
print(receivedMsg)