Zig
Zig
ScMinidriver - Get CSP Container Map for Smart Card or USB Token
See more ScMinidriver Examples
Returns the contents of the CSP container map file (cmapfile). This gives an overview of what key containers and certificates exist in the smart card from a CSP's point of view.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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 PCSC List Readers or PCSC Find Smart Cards
const reader_name = "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0";
scmd.acquireContext(reader_name) catch {
std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
return;
};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.setEmitCompact(false);
scmd.getCspContainerMap(json) catch {
std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Here is sample output:
// See below for sample code to parse the JSON.
// --------------------------------------------------------------
// Some notes:
//
// 1) Empty containers are not listed.
// 2) The "index" is the index of the key container on the card.
// Note that in the example output below, the card has 4 occupied containers
// at indexes 0, 1, 2, and 5.
// 3) If you see a key size (in bits) equal to 0, it means that key does not exist.
// 4) A container can potentially contain 2 keys + certificates -- a key for signing (also known as
// the "signature" key), and a key for authentication/authorization (also known as the
// "key exchange" key).
// 5) It is possible for a key to exist without the certificate. For example, in the results
// below there is a 2048-bit key-exchange key at index 1, but no certificate.
// 6) A certificate belonging to the key-exchange key is contained in the "kexCert" JSON member,
// a certificate belonging to the signature key is contained in the "sigCert" JSON member (not shown below).
// --------------------------------------------------------------
// {
// "containers": [
// {
// "index": 0,
// "guid": "CertReq-PIVKeyC910-205fc3c2-19fe--42448",
// "default": true,
// "sigKeySizeBits": 0,
// "kexKeySizeBits": 2048,
// "kexCert": {
// "serialNum": "15FBFBCF00010000313D",
// "issuerCN": "PIVKey Device Certificate Authority",
// "subjectCN": "PIVKey Device Certificate Authority"
// }
// },
// {
// "index": 1,
// "guid": "{7b3c32dd-e992-c58f-0822-67d72fd52d43}",
// "default": false,
// "sigKeySizeBits": 0,
// "kexKeySizeBits": 2048
// },
// {
// "index": 2,
// "guid": "{57547a77-18e7-7516-f5da-6aee13b46bbc}",
// "default": false,
// "sigKeySizeBits": 0,
// "kexKeySizeBits": 2048
// },
// {
// "index": 5,
// "guid": "{306fd6da-9954-4f75-95c0-178f607a41fb}",
// "default": false,
// "sigKeySizeBits": 0,
// "kexKeySizeBits": 2048,
// "kexCert": {
// "serialNum": "17C1B793A24D1A81490F5F768E6D23A5",
// "issuerCN": "Matt",
// "subjectCN": "Matt"
// }
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var index: i32 = 0;
var guid: [:0]const u8 = "";
var default: bool = false;
var sig_key_size_bits: i32 = 0;
var kex_key_size_bits: i32 = 0;
var kex_cert_serial_num: [:0]const u8 = "";
var kex_cert_issuer_cn: [:0]const u8 = "";
var kex_cert_subject_cn: [:0]const u8 = "";
var sig_cert_serial_num: [:0]const u8 = "";
var sig_cert_issuer_cn: [:0]const u8 = "";
var sig_cert_subject_cn: [:0]const u8 = "";
var i: i32 = 0;
const count_i = json.sizeOfArray("containers");
while (i < count_i) {
json.setI(i);
index = json.intOf("containers[i].index");
guid = try json.stringOf(alloc, "containers[i].guid");
default = json.boolOf("containers[i].default");
sig_key_size_bits = json.intOf("containers[i].sigKeySizeBits");
kex_key_size_bits = json.intOf("containers[i].kexKeySizeBits");
kex_cert_serial_num = try json.stringOf(alloc, "containers[i].kexCert.serialNum");
kex_cert_issuer_cn = try json.stringOf(alloc, "containers[i].kexCert.issuerCN");
kex_cert_subject_cn = try json.stringOf(alloc, "containers[i].kexCert.subjectCN");
sig_cert_serial_num = try json.stringOf(alloc, "containers[i].sigCert.serialNum");
sig_cert_issuer_cn = try json.stringOf(alloc, "containers[i].sigCert.issuerCN");
sig_cert_subject_cn = try json.stringOf(alloc, "containers[i].sigCert.subjectCN");
i = i + 1;
}
// Delete the context when finished with the card.
scmd.deleteContext() catch {
std.debug.print("{s}\n", .{try scmd.getLastErrorText(alloc)});
};
}