Sample code for 30+ languages & platforms
Lua

Add Trusted Certificate to JKS

See more Java KeyStore (JKS) Examples

Adds a trusted certificate to a Java keystore file.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local jks = chilkat.JavaKeyStore.new()

local jksPassword = "secret"
local jksPath = "/myJksTrustedCerts/cacerts.jks"

-- Load the Java keystore from a file.
local success = jks:load_file(jksPassword, jksPath)
if not success then
    print(jks.last_error_text)
    return
end

local cert = chilkat.Cert.new()

-- The cert's LoadFrommFile method can load a certificate from
-- virtually any format.  It will automatically determine the format
-- and load appropriately.
success = cert:load_from_file("/certFiles/myNewTrustedCert.pem")
if not success then
    print(cert.last_error_text)
    return
end

-- The alias can be anything.  It's basically just a label
-- used within the JKS associated with the entry.  It should
-- be unique among aliases within the JKS file.
local alias = "habanero"

success = jks:add_trusted_cert(cert, alias)
if not success then
    print(jks.last_error_text)
    return
end

-- Write the JKS containing the new certificate.
success = jks:to_file(jksPassword, jksPath)
if not success then
    print(jks.last_error_text)
    return
end

print("Added a trusted certificate to the JKS.")