Sample code for 30+ languages & platforms
Zig Requires Chilkat v10.1.2+

Apple Keychain - List Certs on Smartcards and USB Tokens

See more Apple Keychain Examples

Iterates over the certificatse on connected smartcards and USB tokens via the Apple Keychain.

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_store = try chilkat.CertStore.init();
    defer cert_store.deinit();

    // On MacOS and iOS, the OpenSmartcard method opens the Keychain.
    // The argument passed to OpenSmartcard is ignored.
    cert_store.openSmartcard("") catch {
        std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
        return;
    };

    const num_certs = cert_store.getNumCertificates();
    std.debug.print("numCerts = {d}\n", .{num_certs});

    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    var i: i32 = 0;
    while (i < num_certs) {
        // Note: Chilkat also gets the associated private key if it exists.
        // You can simply use the cert in other places in Chilkat where a cert w/ private key is required.
        cert_store.getCert(i, cert) catch {};
        std.debug.print("{s}\n", .{try cert.getSubjectDN(alloc)});
        std.debug.print("{s}\n", .{try cert.getSubjectCN(alloc)});
        std.debug.print("{s}\n", .{try cert.getSerialNumber(alloc)});
        if (cert.isRsa()) {
            std.debug.print("key type is RSA\n", .{});
        }

        if (cert.isEcdsa()) {
            std.debug.print("key type is ECDSA\n", .{});
        }

        std.debug.print("has private key: {}\n", .{cert.hasPrivateKey()});
        std.debug.print("----\n", .{});
        i = i + 1;
    }

    cert_store.closeCertStore() catch {};
}