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

P7S - Access Signature Information (date/time, certificate used, etc.)

See more Digital Signatures Examples

Examine a PKCS7 signature (.p7s) and get information about it.

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

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

    // First load the .p7s file into a BinData object..
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    bd.loadFile("qa_data/p7s/sample.p7s") catch {
        std.debug.print("Failed to load .p7s file.\n", .{});
        return;
    };

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    // Assuming this is a signature that contains the original data that was signed..
    crypt.opaqueVerifyBd(bd) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // Examine the last JSON data after signature verification..
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    crypt.getLastJsonData(json);

    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Sample output...
    // Go to http://tools.chilkat.io/jsonParse.cshtml
    // and paste the JSON into the online form to generate JSON parsing code.

    // {
    //   "pkcs7": {
    //     "verify": {
    //       "digestAlgorithms": [
    //         "sha256"
    //       ],
    //       "signerInfo": [
    //         {
    //           "cert": {
    //             "serialNumber": "AAC5FC48C0FD8FBB",
    //             "issuerCN": "AC ABCDEF RFB v5",
    //             "issuerDN": "",
    //             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
    //             "digestAlgName": "SHA-256"
    //           },
    //           "contentType": "1.2.840.113549.1.7.1",
    //           "signingTime": "180607195054Z",
    //           "messageDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8=",
    //           "signingAlgOid": "1.2.840.113549.1.1.11",
    //           "signerDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8="
    //         },
    //         {
    //           "cert": {
    //             "serialNumber": "324FB38ABD59723F",
    //             "issuerCN": "AC ABCDEF RFB v5",
    //             "issuerDN": "",
    //             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
    //             "digestAlgName": "SHA-256"
    //           },
    //           "contentType": "1.2.840.113549.1.7.1",
    //           "signingTime": "180608182517Z",
    //           "messageDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8=",
    //           "signingAlgOid": "1.2.840.113549.1.1.11",
    //           "signerDigest": "trzyxXbZ96z2M4mncyZ7BNMV4yIT92+5sS27Fu64iG8="
    //         }
    //       ]
    //     }
    //   }
    // }
    //

    var i: i32 = 0;
    var count_i: i32 = 0;
    var str_val: [:0]const u8 = "";
    var cert_serial_number: [:0]const u8 = "";
    var cert_issuer_cn: [:0]const u8 = "";
    var cert_issuer_dn: [:0]const u8 = "";
    var cert_digest_alg_oid: [:0]const u8 = "";
    var cert_digest_alg_name: [:0]const u8 = "";
    var content_type: [:0]const u8 = "";
    var signing_time: [:0]const u8 = "";
    var message_digest: [:0]const u8 = "";
    var signing_alg_oid: [:0]const u8 = "";
    var signer_digest: [:0]const u8 = "";
    const dt = try chilkat.DateTime.init();
    defer dt.deinit();

    i = 0;
    count_i = json.sizeOfArray("pkcs7.verify.digestAlgorithms");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "pkcs7.verify.digestAlgorithms[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("pkcs7.verify.signerInfo");
    while (i < count_i) {
        json.setI(i);
        cert_serial_number = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].cert.serialNumber");
        cert_issuer_cn = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].cert.issuerCN");
        cert_issuer_dn = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].cert.issuerDN");
        cert_digest_alg_oid = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].cert.digestAlgOid");
        cert_digest_alg_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].cert.digestAlgName");
        content_type = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].contentType");
        signing_time = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].signingTime");

        // The signingTime isin UTCTime format.
        // UTCTime values take the form of either "YYMMDDhhmm[ss]Z" or "YYMMDDhhmm[ss](+|-)hhmm"
        // Starting in Chilkat v9.5.0.77, the SetFromTimestamp method auto-recognizes the UTCTime format and parses it correctly.
        try dt.setFromTimestamp(signing_time);
        // To get the signingTime in other date/time formats, look at the online reference documentation for CkDateTime.
        // There are numerous methods such as GetAsDateTime, GetAsIso8601, GetAsUnixTime, GetAsRfc822, etc.

        message_digest = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].messageDigest");
        signing_alg_oid = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].signingAlgOid");
        signer_digest = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].signerDigest");
        i = i + 1;
    }

    // println crypt.LastErrorText;
    std.debug.print("Success.\n", .{});
}