Zig
Zig
Use HTTPS Client Certificate from .cer and .key Files
See more HTTP Examples
Demonstrates how to load a cert + private key from .cer and .key (base64) files and use it for mutual TLS authentication (client-side 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.
const http = try chilkat.Http.init();
defer http.deinit();
const cert = try chilkat.Cert.init();
defer cert.deinit();
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
// Load any type of certificate (.cer, .p7b, .pem, etc.) by calling LoadFromFile.
cert.loadFromFile("qa_data/certs/sample_cert_a.cer") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// Load the private key.
const bd = try chilkat.BinData.init();
defer bd.deinit();
try bd.loadFile("qa_data/certs/sample_key_a.key");
priv_key.loadAnyFormat(bd, "privateKeyPasswordIfNecessary") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
// Associate the private key with the cert.
cert.setPrivateKey(priv_key) catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// Set the certificate to be used for mutual TLS authentication
// (i.e. sets the client-side certificate for two-way TLS authentication)
http.setSslClientCert(cert) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// At this point, the HTTP object instance is setup with the client-side cert, and any SSL/TLS
// connection will automatically use it if the server demands a client-side cert.
}