Sample code for 30+ languages & platforms
Zig

ScMinidriver - Import a Certificate and Private Key to a Smart Card or USB Token

See more ScMinidriver Examples

Demonstrates how to import a certificate and its private key to a key container on a smart card or USB token.

Note: This functionality was introduced in Chilkat v9.5.0.87.

Note: The ScMinidriver functionality is for Windows-only because ScMinidriver DLLs only exist on Windows.

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;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const scmd = try chilkat.ScMinidriver.init();
    defer scmd.deinit();

    // Reader names (smart card readers or USB tokens) can be discovered
    // via List Readers or Find Smart Cards
    const reader_name = "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0";
    success = if (scmd.acquireContext(reader_name)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
        return;
    }

    // If successful, the name of the currently inserted smart card is available:
    std.debug.print("Card name: {s}\n", .{try scmd.getCardName(alloc)});

    // To import a cert + private key, we'll need to be PIN authenticated.
    // For more details about smart card PIN authentication, see the Smart Card PIN Authentication Example
    const pin_id = "user";
    const retval = scmd.pinAuthenticate(pin_id, "000000");
    if (retval != 0) {
        std.debug.print("PIN Authentication failed.\n", .{});
        scmd.deleteContext() catch {};
        return;
    }

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

    // Load the cert + private key from a .p12/.pfx
    // We got this .p12 from https://badssl.com/download/
    const password = "badssl.com";
    success = if (cert.loadPfxFile("qa_data/pfx/badssl.com-client.p12", password)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        scmd.deleteContext() catch {};
        return;
    }

    // Let's import this certificate as the "signature" key/cert in key container #6.
    const container_index = 6;
    const key_spec = "sig";
    success = if (scmd.importCert(cert, container_index, key_spec, pin_id)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
    } else {
        std.debug.print("Successfully imported the cert + private key onto the smart card.\n", .{});
    }

    // When finished with operations that required authentication, you may if you wish, deauthenticate the session.
    scmd.pinDeauthenticate("user") catch {
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
    };

    // Delete the context when finished with the card.
    scmd.deleteContext() catch {
        std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
    };
}