Lua Requires Chilkat v11.0.0+
Lua
Load Certificate from PFX (PKCS#12)
See more Certificates Examples
Loads a digital certificate (and private key, if available) from a PFX file.(also known as PKCS#12)Chilkat Lua Downloads
local chilkat = require("chilkat")
local cert = chilkat.Cert.new()
-- Load from the PFX file
local pfxFilename = "/Users/chilkat/testData/pfx/chilkat_ssl_pwd_is_test.pfx"
local pfxPassword = "test"
-- A PFX typically contains certificates in the chain of authentication.
-- The Chilkat cert object will choose the certificate w/
-- private key farthest from the root authority cert.
-- To access all the certificates in a PFX, use the
-- Chilkat certificate store object instead.
local success = cert:load_pfx_file(pfxFilename, pfxPassword)
if not success then
print(cert.last_error_text)
return
end
-- Get some information about the digital certificate,
-- then get the private key...
-- DN = "Distinguished Name"
print("SubjectDN:" .. cert.subject_dn)
print("Common Name:" .. cert.subject_cn)
print("Issuer Common Name:" .. cert.issuer_cn)
print("Serial Number:" .. cert.serial_number)
local privKey = chilkat.PrivateKey.new()
success = cert:get_private_key(privKey)
if not success then
print(cert.last_error_text)
return
end
-- The private key object may be used in any Chilkat methods
-- (in other objects/classes) that expect a private key argument.
-- In this case, save the private key to a PKCS8 Encrypted PEM format file:
local pemPassword = "secret"
local pemPath = "/Users/chilkat/testData/pem/chilkat_privKey.pem"
success = privKey:save_pkcs8_encrypted_pem_file(pemPassword, pemPath)
if not success then
print(privKey.last_error_text)
return
end
print("Private key saved to PKCS8 Encrypted PEM...")