Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Renew a DigiCert Certificate from an EST-enabled profile

See more Certificates Examples

Demonstrates how to renew a certificate from an EST-enabled profile in DigiCert​​®​​ Trust Lifecycle Manager. (The certificate must be within the renewal window configured in the certificate profile. The CSR must have same Subject DN values as the original 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.

-- The example below duplicates the following OpenSSL commands:
--
-- # Name of certificate as argument 1
--
-- # Make new key
-- openssl ecparam -name prime256v1 -genkey -noout -out ${1}.key.pem
--
-- # Make csr
-- openssl req -new -sha256 -key ${1}.key.pem -out ${1}.p10.csr -subj "/CN=${1}"
--
-- # Request new cert
-- curl -v --cacert data/ca.pem --cert data/${1}.pem --key data/${1}.key.pem
--     --data-binary @${1}.p10.csr -o ${1}.p7.b64 -H "Content-Type: application/pkcs10" https://clientauth.demo.one.digicert.com/.well-known/est/IOT/simplereenroll
--
-- # Convert to PEM
-- openssl base64 -d -in ${1}.p7.b64 | openssl pkcs7 -inform DER -outform PEM -print_certs -out ${1}.pem

-- ------------------------------------------------------------------------------------------------------------------

-- Create a Fortuna PRNG and seed it with system entropy.
-- This will be our source of random data for generating the ECC private key.
local fortuna = chilkat.Prng.new()
local entropy = fortuna:get_entropy(32, "base64")
local success = fortuna:add_entropy(entropy, "base64")

local ec = chilkat.Ecc.new()

-- Generate a random EC private key on the prime256v1 curve.
local privKey = chilkat.PrivateKey.new()
success = ec:gen_key("prime256v1", fortuna, privKey)
if not success then
    print(ec.last_error_text)
    return
end

-- Create the CSR object and set properties.
local csr = chilkat.Csr.new()

-- Specify your CN
csr.common_name = "mysubdomain.mydomain.com"

-- Create the CSR using the private key.
local bdCsr = chilkat.BinData.new()
success = csr:gen_csr_bd(privKey, bdCsr)
if not success then
    print(csr.last_error_text)
    return
end

-- Save the private key and CSR to files.
privKey:save_pkcs8_encrypted_pem_file("password", "c:/temp/qa_output/ec_privkey.pem")

bdCsr:write_file("c:/temp/qa_output/csr.pem")

-- ----------------------------------------------------------------------
-- Now do the CURL request to POST the CSR and get the new certificate.

local http = chilkat.Http.new()

local tlsClientCert = chilkat.Cert.new()
success = tlsClientCert:load_from_file("data/myTlsClientCert.pem")
if not success then
    print(tlsClientCert.last_error_text)
    return
end

local bdTlsClientCertPrivKey = chilkat.BinData.new()
success = bdTlsClientCertPrivKey:load_file("data/myTlsClientCert.key.pem")
if not success then
    print("Failed to load data/myTlsClientCert.key.pem")
    return
end

local tlsClientCertPrivKey = chilkat.PrivateKey.new()
success = tlsClientCertPrivKey:load_any_format(bdTlsClientCertPrivKey, "")
if not success then
    print(tlsClientCertPrivKey.last_error_text)
    return
end

success = tlsClientCert:set_private_key(tlsClientCertPrivKey)
if not success then
    print(tlsClientCert.last_error_text)
    return
end

http:set_ssl_client_cert(tlsClientCert)

http.require_ssl_cert_verify = true

-- The body of the HTTP request contains the binary CSR.
local resp = chilkat.HttpResponse.new()
local url = "https://clientauth.demo.one.digicert.com/.well-known/est/IOT/simplereenroll"
success = http:http_bd("POST", url, bdCsr, "application/pkcs10", resp)
if not success then
    print(http.last_error_text)
    return
end

if resp.status_code ~= 200 then
    print("response status code = " .. resp.status_code)
    print(resp.body_str)
    print("Failed")
    return
end

-- The response is the Base64 DER of the new certificate.
local myNewCert = chilkat.Cert.new()
success = myNewCert:load_from_base64(resp.body_str)
if not success then
    print(myNewCert.last_error_text)
    print("Cert data = " .. resp.body_str)
    print("Failed.")
    return
end

success = myNewCert:save_to_file("c:/temp/qa_output/myNewCert.cer")
if not success then
    print(myNewCert.last_error_text)
    print("Failed.")
    return
end

print("Success.")