Lua
Lua
Add a Private Key and Certificate Chain to a PFX
See more PFX/P12 Examples
Demonstrates Pfx.AddPrivateKey, which adds a private key together with its certificate chain to the PFX 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. The chain must contain at least one certificate; the certificate at index 0 is treated as the leaf. The chain is built here with Cert.BuildCertChain.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local pfx = chilkat.Pfx.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.
-- Load the private key.
local privKey = chilkat.PrivateKey.new()
local success = privKey:load_pem_file("qa_data/private_key.pem")
if not success then
print(privKey.last_error_text)
return
end
-- Load the certificate and build its chain of authority. The chain must contain at least one
-- certificate; the certificate at index 0 is treated as the leaf.
local cert = chilkat.Cert.new()
success = cert:load_from_file("qa_data/cert.pem")
if not success then
print(cert.last_error_text)
return
end
local chain = chilkat.CertChain.new()
success = cert:build_cert_chain(chain)
if not success then
print(cert.last_error_text)
return
end
-- Add the private key and its certificate chain to the PFX.
success = pfx:add_private_key(privKey, chain)
if not success then
print(pfx.last_error_text)
return
end
print("Private key and certificate chain added to the PFX.")