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

RFC3161 Timestamp Client - Fetch from Timestamp Authority (TSA) and Verify

See more HTTP Examples

Sends an RFC 3161 timestamp request to a TSA (Timestamp Authority) server and validates the timestamp token response.

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 sha-256 hash the data that is to be timestamped.
    // In this example, the data is the string "Hello World"

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setHashAlgorithm("sha256");
    crypt.setEncodingMode("base64");
    const base64_hash = try crypt.hashStringENC(alloc, "Hello World");

    const http = try chilkat.Http.init();
    defer http.deinit();

    const request_token = try chilkat.BinData.init();
    defer request_token.deinit();
    const optional_policy_oid = "";
    const add_nonce = false;
    const request_tsa_cert = true;

    // Create a time-stamp request token
    http.createTimestampRequest("sha256", base64_hash, optional_policy_oid, add_nonce, request_tsa_cert, request_token) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Send the time-stamp request token to the TSA.
    // This is the equivalent of the following CURL command:
    // curl -H "Content-Type: application/timestamp-query" --data-binary '@file.tsq' https://freetsa.org/tsr > file.tsr
    var tsa_url: [:0]const u8 = "https://freetsa.org/tsr";
    // Another timestamp server you could try is: http://timestamp.digicert.com
    tsa_url = "http://timestamp.digicert.com";
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpBd("POST", tsa_url, request_token, "application/timestamp-query", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Get the timestamp reply from the HTTP response object.
    const timestamp_reply = try chilkat.BinData.init();
    defer timestamp_reply.deinit();
    resp.getBodyBd(timestamp_reply) catch {};

    // Show the base64 encoded timestamp reply.
    std.debug.print("{s}\n", .{try timestamp_reply.getEncoded(alloc, "base64")});

    // Let's verify the timestamp reply against the TSA's cert, which we've previously downloaded.
    // See https://freetsa.org/index_en.php
    const tsa_cert = try chilkat.Cert.init();
    defer tsa_cert.deinit();
    tsa_cert.loadFromFile("qa_data/certs/freetsa.org.cer") catch {
        std.debug.print("{s}\n", .{try tsa_cert.getLastErrorText(alloc)});
        return;
    };

    // The VerifyTimestampReply method will return one of the following values:
    // -1:  The timestampReply does not contain a valid timestamp reply.
    // -2: The  timestampReply is a valid timestamp reply, but failed verification using the public key of the tsaCert.
    // 0:  Granted and verified.
    // 1: Granted and verified, with mods (see RFC 3161)
    // 2: Rejected.
    // 3: Waiting.
    // 4: Revocation Warning
    // 5: Revocation Notification
    const pki_status = http.verifyTimestampReply(timestamp_reply, tsa_cert);
    if (pki_status < 0) {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    }

    std.debug.print("pkiStatus = {d}\n", .{pki_status});

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    http.getLastJsonData(json);

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

    // The JSON looks like the following.

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //   "timestampReply": {
    //     "pkiStatus": {
    //       "value": 0,
    //       "meaning": "granted"
    //     }
    //   },
    //   "pkcs7": {
    //     "verify": {
    //       "digestAlgorithms": [
    //         "sha256"
    //       ],
    //       "signerInfo": [
    //         {
    //           "cert": {
    //             "serialNumber": "04CD3F8568AE76C61BB0FE7160CCA76D",
    //             "issuerCN": "DigiCert SHA2 Assured ID Timestamping CA",
    //             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
    //             "digestAlgName": "SHA256"
    //           },
    //           "contentType": "1.2.840.113549.1.9.16.1.4",
    //           "signingTime": "200405023019Z",
    //           "messageDigest": "f14zOsdnN9vyyV3HjjBiLzNDi1PF28hAFMODxNkNRZs=",
    //           "signingAlgOid": "1.2.840.113549.1.1.1",
    //           "signingAlgName": "RSA-PKCSV-1_5",
    //           "authAttr": {
    //             "1.2.840.113549.1.9.3": {
    //               "name": "contentType",
    //               "oid": "1.2.840.113549.1.9.16.1.4"
    //             },
    //             "1.2.840.113549.1.9.5": {
    //               "name": "signingTime",
    //               "utctime": "200405023019Z"
    //             },
    //             "1.2.840.113549.1.9.16.2.12": {
    //               "name": "signingCertificate",
    //               "der": "MBowGDAWBBQDJb1QXtqWMC3CL0+gHkwovig0xQ=="
    //             },
    //             "1.2.840.113549.1.9.4": {
    //               "name": "messageDigest",
    //               "digest": "f14zOsdnN9vyyV3HjjBiLzNDi1PF28hAFMODxNkNRZs="
    //             }
    //           }
    //         }
    //       ]
    //     }
    //   }
    // }

    const signing_time = try chilkat.DtObj.init();
    defer signing_time.deinit();
    const auth_attr_signing_time_utctime = try chilkat.DtObj.init();
    defer auth_attr_signing_time_utctime.deinit();
    var str_val: [:0]const u8 = "";
    var cert_serial_number: [:0]const u8 = "";
    var cert_issuer_cn: [: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 message_digest: [:0]const u8 = "";
    var signing_alg_oid: [:0]const u8 = "";
    var signing_alg_name: [:0]const u8 = "";
    var auth_attr_content_type_name: [:0]const u8 = "";
    var auth_attr_content_type_oid: [:0]const u8 = "";
    var auth_attr_signing_time_name: [:0]const u8 = "";
    var auth_attr_signing_certificate_name: [:0]const u8 = "";
    var auth_attr_signing_certificate_der: [:0]const u8 = "";
    var auth_attr_message_digest_name: [:0]const u8 = "";
    var auth_attr_message_digest_digest: [:0]const u8 = "";

    _ = json.intOf("timestampReply.pkiStatus.value");
    _ = try json.stringOf(alloc, "timestampReply.pkiStatus.meaning");
    var i: i32 = 0;
    var count_i: i32 = 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_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");
        json.dtOf("pkcs7.verify.signerInfo[i].signingTime", false, signing_time) catch {};
        message_digest = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].messageDigest");
        signing_alg_oid = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].signingAlgOid");
        signing_alg_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].signingAlgName");
        auth_attr_content_type_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".name");
        auth_attr_content_type_oid = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".oid");
        auth_attr_signing_time_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\".name");
        json.dtOf("pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\".utctime", false, auth_attr_signing_time_utctime) catch {};
        auth_attr_signing_certificate_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.12\".name");
        auth_attr_signing_certificate_der = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.12\".der");
        auth_attr_message_digest_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".name");
        auth_attr_message_digest_digest = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".digest");
        i = i + 1;
    }
}