Zig Requires Chilkat v11.0.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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.
const fortuna = try chilkat.Prng.init();
defer fortuna.deinit();
const entropy = try fortuna.getEntropy(alloc, 32, "base64");
try fortuna.addEntropy(entropy, "base64");
const ec = try chilkat.Ecc.init();
defer ec.deinit();
// Generate a random EC private key on the prime256v1 curve.
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
ec.genKey("prime256v1", fortuna, priv_key) catch {
std.debug.print("{s}\n", .{try ec.getLastErrorText(alloc)});
return;
};
// Create the CSR object and set properties.
const csr = try chilkat.Csr.init();
defer csr.deinit();
// Specify your CN
csr.setCommonName("mysubdomain.mydomain.com");
// Create the CSR using the private key.
const bd_csr = try chilkat.BinData.init();
defer bd_csr.deinit();
csr.genCsrBd(priv_key, bd_csr) catch {
std.debug.print("{s}\n", .{try csr.getLastErrorText(alloc)});
return;
};
// Save the private key and CSR to files.
priv_key.savePkcs8EncryptedPemFile("password", "c:/temp/qa_output/ec_privkey.pem") catch {};
bd_csr.writeFile("c:/temp/qa_output/csr.pem") catch {};
// ----------------------------------------------------------------------
// Now do the CURL request to POST the CSR and get the new certificate.
const http = try chilkat.Http.init();
defer http.deinit();
const tls_client_cert = try chilkat.Cert.init();
defer tls_client_cert.deinit();
tls_client_cert.loadFromFile("data/myTlsClientCert.pem") catch {
std.debug.print("{s}\n", .{try tls_client_cert.getLastErrorText(alloc)});
return;
};
const bd_tls_client_cert_priv_key = try chilkat.BinData.init();
defer bd_tls_client_cert_priv_key.deinit();
bd_tls_client_cert_priv_key.loadFile("data/myTlsClientCert.key.pem") catch {
std.debug.print("Failed to load data/myTlsClientCert.key.pem\n", .{});
return;
};
const tls_client_cert_priv_key = try chilkat.PrivateKey.init();
defer tls_client_cert_priv_key.deinit();
tls_client_cert_priv_key.loadAnyFormat(bd_tls_client_cert_priv_key, "") catch {
std.debug.print("{s}\n", .{try tls_client_cert_priv_key.getLastErrorText(alloc)});
return;
};
tls_client_cert.setPrivateKey(tls_client_cert_priv_key) catch {
std.debug.print("{s}\n", .{try tls_client_cert.getLastErrorText(alloc)});
return;
};
http.setSslClientCert(tls_client_cert) catch {};
http.setRequireSslCertVerify(true);
// The body of the HTTP request contains the binary CSR.
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
const url = "https://clientauth.demo.one.digicert.com/.well-known/est/IOT/simplereenroll";
http.httpBd("POST", url, bd_csr, "application/pkcs10", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
if (resp.getStatusCode() != 200) {
std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("Failed\n", .{});
return;
}
// The response is the Base64 DER of the new certificate.
const my_new_cert = try chilkat.Cert.init();
defer my_new_cert.deinit();
my_new_cert.loadFromBase64(try resp.getBodyStr(alloc)) catch {
std.debug.print("{s}\n", .{try my_new_cert.getLastErrorText(alloc)});
std.debug.print("Cert data = {s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("Failed.\n", .{});
return;
};
my_new_cert.saveToFile("c:/temp/qa_output/myNewCert.cer") catch {
std.debug.print("{s}\n", .{try my_new_cert.getLastErrorText(alloc)});
std.debug.print("Failed.\n", .{});
return;
};
std.debug.print("Success.\n", .{});
}