Sample code for 30+ languages & platforms
Lua

Use HTTPS Client Certificate from .cer and .key Files

See more HTTP Examples

Demonstrates how to load a cert + private key from .cer and .key (base64) files and use it for mutual TLS authentication (client-side certificate).

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 http = chilkat.Http.new()

local cert = chilkat.Cert.new()
local privKey = chilkat.PrivateKey.new()

-- Load any type of certificate (.cer, .p7b, .pem, etc.) by calling LoadFromFile.
local success = cert:load_from_file("qa_data/certs/sample_cert_a.cer")
if not success then
    print(cert.last_error_text)
    return
end

-- Load the private key.
local bd = chilkat.BinData.new()
success = bd:load_file("qa_data/certs/sample_key_a.key")
success = privKey:load_any_format(bd, "privateKeyPasswordIfNecessary")
if not success then
    print(privKey.last_error_text)
    return
end

-- Associate the private key with the cert.
success = cert:set_private_key(privKey)
if not success then
    print(cert.last_error_text)
    return
end

-- Set the certificate to be used for mutual TLS authentication
-- (i.e. sets the client-side certificate for two-way TLS authentication)
success = http:set_ssl_client_cert(cert)
if not success then
    print(http.last_error_text)
    return
end

-- At this point, the HTTP object instance is setup with the client-side cert, and any SSL/TLS
-- connection will automatically use it if the server demands a client-side cert.