Sample code for 30+ languages & platforms
Zig

ScMinidriver - Import a Certificate to IDPrime MD T=0 Smart Card

See more ScMinidriver Examples

Demonstrates how to import a certificate and its private key to a key container on an ID Prime MD T=0 smartcard.

Note: Requires Chilkat v9.5.0.88 or later. This example only runs on Windows because ScMinidriver is a Windows-only class.

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

    // The IDPRime MD smart card has 4 different PIN roles:
    // "user" -- Primary Card PIN
    // "admin" -- Administrator PIN
    // "3" -- Digital Signature PIN
    // "4" -- Unblock only PIN (PUK)
    // To import a certificate to the "IDPrime MD T=0" smart card, we must first PIN authenticate using "user", and then also PIN authenticate using "3" (the Digital Signature PIN)
    const pin_id = "user";
    // (Of course, use your PIN which may be different than "0000")
    var retval: i32 = scmd.pinAuthenticate(pin_id, "0000");
    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;
    }

    // Also authenticate using "3", the digital signature PIN.
    // (Of course, use your PIN which may be different than "12345678")
    retval = scmd.pinAuthenticate("3", "12345678");
    if (retval != 0) {
        std.debug.print("PIN Authentication failed.\n", .{});
        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";
    // Note the last argument (the pin ID) is "3".  This is the required PIN ID for the IDPrime MD T=0 smart card.
    success = if (scmd.importCert(cert, container_index, key_spec, "3")) 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", .{});
    }

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