Zig
Zig
Add a Private Key and Certificate Chain to a PFX
See more PFX/P12 Examples
Demonstrates Pfx.AddPrivateKey, which adds a private key together with its certificate chain to the PFX object.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The chain must contain at least one certificate; the certificate at index 0 is treated as the leaf. The chain is built here with Cert.BuildCertChain.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const pfx = try chilkat.Pfx.init();
defer pfx.deinit();
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load the private key.
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
priv_key.loadPemFile("qa_data/private_key.pem") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
// Load the certificate and build its chain of authority. The chain must contain at least one
// certificate; the certificate at index 0 is treated as the leaf.
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadFromFile("qa_data/cert.pem") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
const chain = try chilkat.CertChain.init();
defer chain.deinit();
cert.buildCertChain(chain) catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// Add the private key and its certificate chain to the PFX.
pfx.addPrivateKey(priv_key, chain) catch {
std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
return;
};
std.debug.print("Private key and certificate chain added to the PFX.\n", .{});
}