Zig Requires Chilkat v11.0.0+
Zig
Get the Server Certificate, Certificate Chain, and Root CA Certificate
See more HTTP Examples
Demonstrates how to get the HTTP server certificate, its certificate chain, and the root CA 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 assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
const ssl_cert = try chilkat.Cert.init();
defer ssl_cert.deinit();
http.getServerCert("apple.com", 443, ssl_cert) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const cert_chain = try chilkat.CertChain.init();
defer cert_chain.deinit();
ssl_cert.buildCertChain(cert_chain) catch {
std.debug.print("{s}\n", .{try ssl_cert.getLastErrorText(alloc)});
return;
};
const cert = try chilkat.Cert.init();
defer cert.deinit();
var i: i32 = 0;
const num_certs = cert_chain.getNumCerts();
while (i < num_certs) {
cert_chain.certAt(i, cert) catch {};
std.debug.print("SubjectDN {d}: {s}\n", .{ i, try cert.getSubjectDN(alloc) });
std.debug.print("IssuerDN {d}: {s}\n", .{ i, try cert.getIssuerDN(alloc) });
i = i + 1;
}
// If the certificate chain reaches the root CA cert, then the last cert in the chain
// is the root CA cert.
if (cert_chain.getReachesRoot()) {
const ca_cert = try chilkat.Cert.init();
defer ca_cert.deinit();
cert_chain.certAt(num_certs - 1, ca_cert) catch {};
std.debug.print("CA Root Cert: {s}\n", .{try ca_cert.getSubjectDN(alloc)});
}
}