Zig Requires Chilkat v11.0.0+
Zig
Validate CAdES-T Signature (.p7m)
See more CAdES Examples
Validates a CAdES-T CMS signature and extracts the time-stamp token and gets information about it. Also validates the time-stamp token.Chilkat Zig Downloads
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 crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
// Indicate that the CAdES-T timestamp tokens must also pass validation for the signature to be validated.
const cms_options = try chilkat.JsonObject.init();
defer cms_options.deinit();
cms_options.updateBool("ValidateTimestampTokens", true) catch {};
crypt.setCmsOptions(try cms_options.emit(alloc));
// Validate the .p7m and extract the original signed data to an output file.
// Note: The timestampToken is an unauthenticated attribute. See the code below that retrieves and parses the last JSON data.
// for details about examining timestampToken.
success = if (crypt.verifyP7M("qa_data/cades/CAdES-T/Signature-C-T-1.p7m", "qa_output/out.dat")) true else |_| false;
// Get information about the CMS signature in the last JSON data.
// The detailed results of the signature validation are available in the last JSON data.
// (If the non-success return status was caused by an error such as "file not found", then the
// last JSON data would be empty.)
const json = try chilkat.JsonObject.init();
defer json.deinit();
crypt.getLastJsonData(json);
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Here is a sample result:
// See the parsing code below..
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "pkcs7": {
// "verify": {
// "digestAlgorithms": [
// "sha256"
// ],
// "signerInfo": [
// {
// "cert": {
// "serialNumber": "00DCB814678CDB",
// "issuerCN": "LevelBCAOK",
// "issuerDN": "",
// "digestAlgOid": "2.16.840.1.101.3.4.2.1",
// "digestAlgName": "SHA256"
// },
// "contentType": "1.2.840.113549.1.7.1",
// "signingTime": "131203065741Z",
// "messageDigest": "JJZt41Nt8VsYahP+Xti4rR3vBDkUfRd6gquItl6R5Os=",
// "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.7.1"
// },
// "1.2.840.113549.1.9.5": {
// "name": "signingTime",
// "utctime": "131203065741Z"
// },
// "1.2.840.113549.1.9.4": {
// "name": "messageDigest",
// "digest": "JJZt41Nt8VsYahP+Xti4rR3vBDkUfRd6gquItl6R5Os="
// },
// "1.2.840.113549.1.9.16.2.47": {
// "name": "signingCertificateV2",
// "der": "MIGIMIGFMIGCBCBJrxOU0w0dWGsVovjLv9QDH3syB5mLVv3grSYA40x9IDBeMFOkUTBPMQswCQYDVQQGEwJGUjENMAsGA1UEChMERVRTSTEcMBoGA1UECwwTUGx1Z3Rlc3RzXzIwMTMtMjAxNDETMBEGA1UEAxMKTGV2ZWxCQ0FPSwIHANy4FGeM2w=="
// }
// },
// "unauthAttr": {
// "1.2.840.113549.1.9.16.2.14": {
// "name": "timestampToken",
// "der": "MIIL+AYJKoZI...u7CfcjURNTY=",
// "verify": {
// "digestAlgorithms": [
// "sha256"
// ],
// "signerInfo": [
// {
// "cert": {
// "serialNumber": "01AA4592D36C61",
// "issuerCN": "RootCAOK",
// "issuerDN": "",
// "digestAlgOid": "2.16.840.1.101.3.4.2.1",
// "digestAlgName": "SHA256"
// },
// "contentType": "1.2.840.113549.1.9.16.1.4",
// "messageDigest": "NSsMUrfoyCQ0OszPE1YLx1j3EyyCiBmnE5Sua6ghu/Q=",
// "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.4": {
// "name": "messageDigest",
// "digest": "NSsMUrfoyCQ0OszPE1YLx1j3EyyCiBmnE5Sua6ghu/Q="
// },
// "1.2.840.113549.1.9.16.2.47": {
// "name": "signingCertificateV2",
// "der": "MIGGMIGDMIGABCDB/np5UxvhcPnSxD2Kme+C88uXGCMWLAvFPHNvTApTWDBcMFGkTzBNMQswCQYDVQQGEwJGUjENMAsGA1UEChMERVRTSTEcMBoGA1UECwwTUGx1Z3Rlc3RzXzIwMTMtMjAxNDERMA8GA1UEAxMIUm9vdENBT0sCBwGqRZLTbGE="
// }
// }
// }
// ]
// },
// "timestampSignatureVerified": true,
// "tstInfo": {
// "tsaPolicyId": "1.3.6.1.4.1.2706.2.2.5.2.1.1.1",
// "messageImprint": {
// "hashAlg": "sha256",
// "digest": "C8xEe9NA4X1cUyHGX9zG89ipmQ2byFs3aa+Xe4Fz2P0=",
// "digestMatches": true
// },
// "serialNumber": "313E162121D922",
// "genTime": "20131203065742Z"
// }
// }
// }
// }
// ]
// }
// }
// }
//
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 = "";
const signing_time = try chilkat.DtObj.init();
defer signing_time.deinit();
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 = "";
const auth_attr_signing_time_utctime = try chilkat.DtObj.init();
defer auth_attr_signing_time_utctime.deinit();
var auth_attr_message_digest_name: [:0]const u8 = "";
var auth_attr_message_digest_digest: [:0]const u8 = "";
var auth_attr_signing_certificate_v2_name: [:0]const u8 = "";
var auth_attr_signing_certificate_v2_der: [:0]const u8 = "";
var unauth_attr_timestamp_token_name: [:0]const u8 = "";
var unauth_attr_timestamp_token_der: [:0]const u8 = "";
var unauth_attr_timestamp_token_timestamp_signature_verified: bool = false;
var unauth_attr_timestamp_token_tst_info_tsa_policy_id: [:0]const u8 = "";
var unauth_attr_timestamp_token_tst_info_message_imprint_hash_alg: [:0]const u8 = "";
var unauth_attr_timestamp_token_tst_info_message_imprint_digest: [:0]const u8 = "";
var unauth_attr_timestamp_token_tst_info_message_imprint_digest_matches: bool = false;
var unauth_attr_timestamp_token_tst_info_serial_number: [:0]const u8 = "";
const unauth_attr_timestamp_token_tst_info_gen_time = try chilkat.DtObj.init();
defer unauth_attr_timestamp_token_tst_info_gen_time.deinit();
var j: i32 = 0;
var count_j: i32 = 0;
// Iterate over the hash algorithms used in the signature.
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;
}
// For each signer...
i = 0;
count_i = json.sizeOfArray("pkcs7.verify.signerInfo");
while (i < count_i) {
json.setI(i);
// Get information about the certificate used by this signer.
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");
// Get additional information for this signer, such as the signingTime, signature algorithm, etc.
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");
// --------------------------------
// Examine authenticated attributes.
// --------------------------------
// contentType
if (json.hasMember("pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\"")) {
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");
}
// signingTime
if (json.hasMember("pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.5\"")) {
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 {};
}
// messageDigest
if (json.hasMember("pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\"")) {
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");
}
// signingCertificateV2
if (json.hasMember("pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\"")) {
auth_attr_signing_certificate_v2_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\".name");
auth_attr_signing_certificate_v2_der = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.16.2.47\".der");
}
// --------------------------------
// Examine unauthenticated attributes.
// --------------------------------
// timestampToken (the timestampToken is what makes this signature a CAdES-T)
if (json.hasMember("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\"")) {
unauth_attr_timestamp_token_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".name");
unauth_attr_timestamp_token_der = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".der");
// This is where we find out if the timestampToken's signature is valid.
unauth_attr_timestamp_token_timestamp_signature_verified = json.boolOf("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".timestampSignatureVerified");
unauth_attr_timestamp_token_tst_info_tsa_policy_id = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.tsaPolicyId");
unauth_attr_timestamp_token_tst_info_message_imprint_hash_alg = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.hashAlg");
unauth_attr_timestamp_token_tst_info_message_imprint_digest = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.digest");
// Here is where we check to see if the digest in the timestampToken's messageImprint matches the digest of the signature of this signerInfo
unauth_attr_timestamp_token_tst_info_message_imprint_digest_matches = json.boolOf("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.digestMatches");
unauth_attr_timestamp_token_tst_info_serial_number = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.serialNumber");
// Here is where we get the date/time of the timestampToken (i.e. when it was timestamped)
json.dtOf("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.genTime", false, unauth_attr_timestamp_token_tst_info_gen_time) catch {};
// The following code gets details about the validity of the timestampToken's signature...
j = 0;
count_j = json.sizeOfArray("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms");
while (j < count_j) {
json.setJ(j);
str_val = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms[j]");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo");
while (j < count_j) {
json.setJ(j);
cert_serial_number = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.serialNumber");
cert_issuer_cn = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.issuerCN");
cert_issuer_dn = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.issuerDN");
cert_digest_alg_oid = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgOid");
cert_digest_alg_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgName");
content_type = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].contentType");
message_digest = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].messageDigest");
signing_alg_oid = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgOid");
signing_alg_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgName");
auth_attr_content_type_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.3\".name");
auth_attr_content_type_oid = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.3\".oid");
auth_attr_message_digest_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.4\".name");
auth_attr_message_digest_digest = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.4\".digest");
auth_attr_signing_certificate_v2_name = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.16.2.47\".name");
auth_attr_signing_certificate_v2_der = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.16.2.47\".der");
j = j + 1;
}
}
i = i + 1;
}
if (!success) {
std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
std.debug.print("CAdES-T verification failed.\n", .{});
} else {
std.debug.print("CAdES-T signature is valid.\n", .{});
}
}