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

Backup Windows Current User / Personal Certificates to a .zip

See more Certificates Examples

Demonstrates how to backup the certificates in the Windows registry-based Current User certificate store (in the "Personal" Logical Store as seen in certmgr.msc), to a zip archive. Certificates having an exportable private key are exported to .pfx files. Certificates with no private key, or with a non-exportable private key, are exported to .cer files.

Obviously, this example only runs on Windows computers.

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();

    var success: bool = false;

    const cert_store = try chilkat.CertStore.init();
    defer cert_store.deinit();

    const read_only = true;
    success = if (cert_store.openCurrentUserStore(read_only)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
        return;
    }

    const pfx_password = "secret";

    var all_success: bool = true;
    var num_success: i32 = 0;

    const zip = try chilkat.Zip.init();
    defer zip.deinit();
    zip.newZip("qa_output/personalCerts.zip") catch {};

    const cert_data = try chilkat.BinData.init();
    defer cert_data.deinit();
    const sb_filename = try chilkat.StringBuilder.init();
    defer sb_filename.deinit();

    // Iterate over the certificates in the Current User store.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    const num_certs = cert_store.getNumCertificates();
    var i: i32 = 0;
    while (i < num_certs) {
        cert_store.getCert(i, cert) catch {};
        std.debug.print("DN = {s}\n", .{try cert.getSubjectDN(alloc)});

        sb_filename.setString("cert") catch {};
        sb_filename.appendInt(i + 1) catch {};

        const b_has_private_key = cert.hasPrivateKey();
        if ((b_has_private_key) and (cert.getPrivateKeyExportable())) {
            // Export to a .pfx
            success = if (cert.exportToPfxBd(pfx_password, true, cert_data)) true else |_| false;
            if (success) {
                sb_filename.append(".pfx") catch {};
                zip.addBd(try sb_filename.getAsString(alloc), cert_data) catch {};
            }
        } else {
            // Export to a .cer
            success = if (cert.exportCertDerBd(cert_data)) true else |_| false;
            if (success) {
                sb_filename.append(".cer") catch {};
                zip.addBd(try sb_filename.getAsString(alloc), cert_data) catch {};
            }
        }

        if (!success) {
            all_success = false;
        } else {
            num_success = num_success + 1;
        }

        i = i + 1;
    }

    if (num_success > 0) {
        zip.writeZipAndClose() catch {
            std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
            all_success = false;
        };
    }

    std.debug.print("All success = {}\n", .{all_success});
}