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

Load Certificate from PFX (PKCS#12)

See more Certificates Examples

Loads a digital certificate (and private key, if available) from a PFX file.(also known as PKCS#12)

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    // Load from the PFX file
    const pfx_filename = "/Users/chilkat/testData/pfx/chilkat_ssl_pwd_is_test.pfx";
    const pfx_password = "test";

    // A PFX typically contains certificates in the chain of authentication.
    // The Chilkat cert object will choose the certificate w/
    // private key farthest from the root authority cert.
    // To access all the certificates in a PFX, use the
    // Chilkat certificate store object instead.
    cert.loadPfxFile(pfx_filename, pfx_password) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Get some information about the digital certificate,
    // then get the private key...

    // DN = "Distinguished Name"
    std.debug.print("SubjectDN:{s}\n", .{try cert.getSubjectDN(alloc)});

    std.debug.print("Common Name:{s}\n", .{try cert.getSubjectCN(alloc)});
    std.debug.print("Issuer Common Name:{s}\n", .{try cert.getIssuerCN(alloc)});

    std.debug.print("Serial Number:{s}\n", .{try cert.getSerialNumber(alloc)});

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    cert.getPrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // The private key object may be used in any Chilkat methods
    // (in other objects/classes) that expect a private key argument.

    // In this case, save the private key to a PKCS8 Encrypted PEM format file:
    const pem_password = "secret";
    const pem_path = "/Users/chilkat/testData/pem/chilkat_privKey.pem";
    priv_key.savePkcs8EncryptedPemFile(pem_password, pem_path) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Private key saved to PKCS8 Encrypted PEM...\n", .{});
}