Sample code for 30+ languages & platforms
Zig

Validate PDF Signatures

See more PDF Signatures Examples

This example demonstrates how to validate the signatures in a PDF and also shows how to get information from each signature.

Note: This example requires Chilkat v9.5.0.85 or greater.

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 example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();

    // Load a PDF that has cryptographic signatures to be validated
    pdf.loadFile("qa_data/pdf/sign_testing_1/helloSigned2.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // Each time we verify a signature, information about the signature is written into
    // sigInfo (replacing whatever sigInfo previously contained).
    const sig_info = try chilkat.JsonObject.init();
    defer sig_info.deinit();
    sig_info.setEmitCompact(false);

    // Iterate over each signature and validate each.
    const num_signatures = pdf.getNumSignatures();
    var validated: bool = false;
    var i: i32 = 0;
    while (i < num_signatures) {
        validated = if (pdf.verifySignature(i, sig_info)) true else |_| false;
        std.debug.print("Signature {d} validated: {}\n", .{ i, validated });
        std.debug.print("{s}\n", .{try sig_info.emit(alloc)});
        i = i + 1;
    }

    std.debug.print("Finished.\n", .{});

    // When VerifySignature validates a signature, a lot of information is deposited into the JSON sigInfo object.
    // The information can vary depending on what was included in the signature (for example, various authenticated attributes
    // and unauthenticated attributes may or may not be included).
    // Here is a sample of the information you'll see.
    //
    // The following online tool can be used to generate code to parse any given JSON.
    // Generate Parsing Code from JSON

    // {
    //   "validated": true,
    //   "signatureDictionary": {         <--- This is the contents of the PDF Signature Dictionary for this signature.
    //     "/ByteRange": [
    //       0,
    //       154682,
    //       170512,
    //       3233
    //     ],
    //     "/Contents": "<hex_data>",
    //     "/Filter": "/Adobe.PPKLite",        <--- The meaning of the Signature Dictionary entries are defined in the PDF format specification document.
    //     "/M": "D:20201006110216-05'00'",
    //     "/Name": "yubikey rsa 1024 authentication",
    //     "/Prop_Build": {
    //       "/App": {
    //         "/Name": "/Adobe#20Acrobat#20Pro#20DC",
    //         "/OS": [
    //           "/Win"
    //         ],
    //         "/R": 1313792,
    //         "/REx": "2020.012.20048",
    //         "/TrustedMode": true
    //       },
    //       "/Filter": {
    //         "/Date": "Sep 11 2020 16:30:54",
    //         "/Name": "/Adobe.PPKLite",
    //         "/R": 131104,
    //         "/V": 2
    //       },
    //       "/PubSec": {
    //         "/Date": "Sep 11 2020 16:30:54",
    //         "/NonEFontNoWarn": true,
    //         "/R": 131105
    //       }
    //     },
    //     "/SubFilter": "/adbe.pkcs7.detached",
    //     "/Type": "/Sig"
    //   },
    //   "pkcs7": {          <--- This is the content of the CMS signature.
    //     "verify": {
    //       "certs": [      <--- Each signing certificate is listed here (by issuer common name and signing cert's serail number (in hex))
    //         {
    //           "issuerCN": "yubikey rsa 1024 authentication",
    //           "serial": "66BE58138D761E92BC594A722932657BE26D421F"
    //         }
    //       ],
    //       "digestAlgorithms": [
    //         "sha256"
    //       ],
    //       "signerInfo": [     <--- contains data from each SignerInfo
    //         {
    //           "cert": {
    //             "serialNumber": "66BE58138D761E92BC594A722932657BE26D421F",
    //             "issuerCN": "yubikey rsa 1024 authentication",
    //             "digestAlgOid": "2.16.840.1.101.3.4.2.1",
    //             "digestAlgName": "SHA256"
    //           },
    //           "contentType": "1.2.840.113549.1.7.1",
    //           "messageDigest": "btQOuSEvC31mdRFHtyEUPw8R9NuKfk0XPcQ6Lcmn6pk=",
    //           "signingAlgOid": "1.2.840.113549.1.1.11",
    //           "signingAlgName": "RSA-SHA256-PKCSV-1_5",
    //           "authAttr": {     <--- CMS authenticated attributes are contained here.
    //             "1.2.840.113583.1.1.8": {
    //               "der": "MAA="
    //             },
    //             "1.2.840.113549.1.9.3": {
    //               "name": "contentType",
    //               "oid": "1.2.840.113549.1.7.1"
    //             },
    //             "1.2.840.113549.1.9.4": {
    //               "name": "messageDigest",
    //               "digest": "btQOuSEvC31mdRFHtyEUPw8R9NuKfk0XPcQ6Lcmn6pk="
    //             }
    //           },
    //           "unauthAttr": {    <--- CMS unauthenticated attributes are contained here.
    //             "1.2.840.113549.1.9.16.2.14": {
    //               "name": "timestampToken",
    //               "der": "MIIOvAYJKo ... Es/70g=",
    //               "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": "201006160423Z",
    //                     "messageDigest": "Atv5Rj3kidB8IR6CplYiX3o6De/k8SC6JJ6uUPAGO0g=",
    //                     "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": "201006160423Z"
    //                       },
    //                       "1.2.840.113549.1.9.16.2.12": {
    //                         "name": "signingCertificate",
    //                         "der": "MBowGDAWBBQDJb1QXtqWMC3CL0+gHkwovig0xQ=="
    //                       },
    //                       "1.2.840.113549.1.9.4": {
    //                         "name": "messageDigest",
    //                         "digest": "Atv5Rj3kidB8IR6CplYiX3o6De/k8SC6JJ6uUPAGO0g="
    //                       }
    //                     }
    //                   }
    //                 ]
    //               },
    //               "timestampSignatureVerified": true,
    //               "tstInfo": {
    //                 "tsaPolicyId": "2.16.840.1.114412.7.1",
    //                 "messageImprint": {
    //                   "hashAlg": "sha256",
    //                   "digest": "gLJtrRWUSDfjzDkF1MfWG1wyHA6FrUJLkWMGRG+eMlA=",
    //                   "digestMatches": true
    //                 },
    //                 "serialNumber": "00CE57E1113970607EF63B1D1160545321",
    //                 "genTime": "20201006160423Z"
    //               }
    //             }
    //           }
    //         }
    //       ],
    //       "pkcs7": {
    //         "verify": {
    //           "certs": [
    //             {
    //               "issuerCN": "DigiCert SHA2 Assured ID Timestamping CA",
    //               "serial": "04CD3F8568AE76C61BB0FE7160CCA76D"
    //             },
    //             {
    //               "issuerCN": "DigiCert Assured ID Root CA",
    //               "serial": "0AA125D6D6321B7E41E405DA3697C215"
    //             }
    //           ]
    //         }
    //       }
    //     }
    //   }
    // }

    // **** The point of this code is to show how to get at each desired piece of information contained in the signature.
    // **** If your signature contains additional information not shown here, then you can use the online tool to generate the parse code.
    // **** It is likely you're only interested in a few items of information, and therefore you wouldn't copy all of this code, but might
    // **** choose to use bits and pieces to get the information you find important.

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

    // Imagine that the "json" object contains the information obtained by validating a signature...
    // The code below was generated using the online tool:  Generate Parsing Code from JSON

    const unauth_attr_timestamp_token_tst_info_gen_time = try chilkat.DtObj.init();
    defer unauth_attr_timestamp_token_tst_info_gen_time.deinit();
    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 int_val: i32 = 0;
    var str_val: [:0]const u8 = "";
    var issuer_cn: [:0]const u8 = "";
    var serial: [: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_attr1_2_840_113583_1_1_8_der: [:0]const u8 = "";
    var auth_attr_content_type_name: [:0]const u8 = "";
    var auth_attr_content_type_oid: [:0]const u8 = "";
    var auth_attr_message_digest_name: [:0]const u8 = "";
    var auth_attr_message_digest_digest: [: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 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    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 = "";

    validated = json.boolOf("validated");
    _ = try json.stringOf(alloc, "signatureDictionary./Contents");
    _ = try json.stringOf(alloc, "signatureDictionary./Filter");
    _ = try json.stringOf(alloc, "signatureDictionary./M");
    _ = try json.stringOf(alloc, "signatureDictionary./Name");
    _ = try json.stringOf(alloc, "signatureDictionary./Prop_Build./App./Name");
    _ = json.intOf("signatureDictionary./Prop_Build./App./R");
    _ = try json.stringOf(alloc, "signatureDictionary./Prop_Build./App./REx");
    _ = json.boolOf("signatureDictionary./Prop_Build./App./TrustedMode");
    _ = try json.stringOf(alloc, "signatureDictionary./Prop_Build./Filter./Date");
    _ = try json.stringOf(alloc, "signatureDictionary./Prop_Build./Filter./Name");
    _ = json.intOf("signatureDictionary./Prop_Build./Filter./R");
    _ = json.intOf("signatureDictionary./Prop_Build./Filter./V");
    _ = try json.stringOf(alloc, "signatureDictionary./Prop_Build./PubSec./Date");
    _ = json.boolOf("signatureDictionary./Prop_Build./PubSec./NonEFontNoWarn");
    _ = json.intOf("signatureDictionary./Prop_Build./PubSec./R");
    _ = try json.stringOf(alloc, "signatureDictionary./SubFilter");
    _ = try json.stringOf(alloc, "signatureDictionary./Type");
    i = 0;
    var count_i: i32 = json.sizeOfArray("signatureDictionary./ByteRange");
    while (i < count_i) {
        json.setI(i);
        int_val = json.intOf("signatureDictionary./ByteRange[i]");
        i = i + 1;
    }

    i = 0;
    count_i = json.sizeOfArray("signatureDictionary./Prop_Build./App./OS");
    while (i < count_i) {
        json.setI(i);
        str_val = try json.stringOf(alloc, "signatureDictionary./Prop_Build./App./OS[i]");
        i = i + 1;
    }

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

    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_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");
        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_attr1_2_840_113583_1_1_8_der = try json.stringOf(alloc, "pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113583.1.1.8\".der");
        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_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");
        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");
        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");
        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");
        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 {};
        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_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");
            json.dtOf("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingTime", false, signing_time) catch {};
            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_signing_time_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.5\".name");
            json.dtOf("pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].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].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].authAttr.\"1.2.840.113549.1.9.16.2.12\".name");
            auth_attr_signing_certificate_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.12\".der");
            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");
            j = j + 1;
        }

        i = i + 1;
    }

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