Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) Validate PDF SignaturesSee more PDF Signatures ExamplesThis 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.
#include <C_CkPdf.h> #include <C_CkJsonObject.h> #include <C_CkDtObj.h> void ChilkatSample(void) { HCkPdf pdf; BOOL success; HCkJsonObject sigInfo; int numSignatures; BOOL validated; int i; HCkJsonObject json; HCkDtObj unauthAttrTimestampTokenTstInfoGenTime; HCkDtObj signingTime; HCkDtObj authAttrSigningTimeUtctime; int intVal; const char *strVal; const char *issuerCN; const char *serial; const char *certSerialNumber; const char *certIssuerCN; const char *certDigestAlgOid; const char *certDigestAlgName; const char *contentType; const char *messageDigest; const char *signingAlgOid; const char *signingAlgName; const char *authAttr1_2_840_113583_1_1_8Der; const char *authAttrContentTypeName; const char *authAttrContentTypeOid; const char *authAttrMessageDigestName; const char *authAttrMessageDigestDigest; const char *unauthAttrTimestampTokenName; const char *unauthAttrTimestampTokenDer; BOOL unauthAttrTimestampTokenTimestampSignatureVerified; const char *unauthAttrTimestampTokenTstInfoTsaPolicyId; const char *unauthAttrTimestampTokenTstInfoMessageImprintHashAlg; const char *unauthAttrTimestampTokenTstInfoMessageImprintDigest; BOOL unauthAttrTimestampTokenTstInfoMessageImprintDigestMatches; const char *unauthAttrTimestampTokenTstInfoSerialNumber; int j; int count_j; const char *authAttrSigningTimeName; const char *authAttrSigningCertificateName; const char *authAttrSigningCertificateDer; const char *signatureDictionary_Contents; const char *signatureDictionary_Filter; const char *signatureDictionary_M; const char *signatureDictionary_Name; const char *signatureDictionary_Prop_Build_App_Name; int signatureDictionary_Prop_Build_App_R; const char *signatureDictionary_Prop_Build_App_REx; BOOL signatureDictionary_Prop_Build_App_TrustedMode; const char *signatureDictionary_Prop_Build_Filter_Date; const char *signatureDictionary_Prop_Build_Filter_Name; int signatureDictionary_Prop_Build_Filter_R; int signatureDictionary_Prop_Build_Filter_V; const char *signatureDictionary_Prop_Build_PubSec_Date; BOOL signatureDictionary_Prop_Build_PubSec_NonEFontNoWarn; int signatureDictionary_Prop_Build_PubSec_R; const char *signatureDictionary_SubFilter; const char *signatureDictionary_Type; int count_i; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. pdf = CkPdf_Create(); // Load a PDF that has cryptographic signatures to be validated success = CkPdf_LoadFile(pdf,"qa_data/pdf/sign_testing_1/helloSigned2.pdf"); if (success == FALSE) { printf("%s\n",CkPdf_lastErrorText(pdf)); CkPdf_Dispose(pdf); return; } // Each time we verify a signature, information about the signature is written into // sigInfo (replacing whatever sigInfo previously contained). sigInfo = CkJsonObject_Create(); CkJsonObject_putEmitCompact(sigInfo,FALSE); // Iterate over each signature and validate each. numSignatures = CkPdf_getNumSignatures(pdf); validated = FALSE; i = 0; while (i < numSignatures) { validated = CkPdf_VerifySignature(pdf,i,sigInfo); printf("Signature %d validated: %d\n",i,validated); printf("%s\n",CkJsonObject_emit(sigInfo)); i = i + 1; } printf("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. json = CkJsonObject_Create(); // 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 // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat. // See this example explaining how this memory should be used: const char * functions. unauthAttrTimestampTokenTstInfoGenTime = CkDtObj_Create(); signingTime = CkDtObj_Create(); authAttrSigningTimeUtctime = CkDtObj_Create(); validated = CkJsonObject_BoolOf(json,"validated"); signatureDictionary_Contents = CkJsonObject_stringOf(json,"signatureDictionary./Contents"); signatureDictionary_Filter = CkJsonObject_stringOf(json,"signatureDictionary./Filter"); signatureDictionary_M = CkJsonObject_stringOf(json,"signatureDictionary./M"); signatureDictionary_Name = CkJsonObject_stringOf(json,"signatureDictionary./Name"); signatureDictionary_Prop_Build_App_Name = CkJsonObject_stringOf(json,"signatureDictionary./Prop_Build./App./Name"); signatureDictionary_Prop_Build_App_R = CkJsonObject_IntOf(json,"signatureDictionary./Prop_Build./App./R"); signatureDictionary_Prop_Build_App_REx = CkJsonObject_stringOf(json,"signatureDictionary./Prop_Build./App./REx"); signatureDictionary_Prop_Build_App_TrustedMode = CkJsonObject_BoolOf(json,"signatureDictionary./Prop_Build./App./TrustedMode"); signatureDictionary_Prop_Build_Filter_Date = CkJsonObject_stringOf(json,"signatureDictionary./Prop_Build./Filter./Date"); signatureDictionary_Prop_Build_Filter_Name = CkJsonObject_stringOf(json,"signatureDictionary./Prop_Build./Filter./Name"); signatureDictionary_Prop_Build_Filter_R = CkJsonObject_IntOf(json,"signatureDictionary./Prop_Build./Filter./R"); signatureDictionary_Prop_Build_Filter_V = CkJsonObject_IntOf(json,"signatureDictionary./Prop_Build./Filter./V"); signatureDictionary_Prop_Build_PubSec_Date = CkJsonObject_stringOf(json,"signatureDictionary./Prop_Build./PubSec./Date"); signatureDictionary_Prop_Build_PubSec_NonEFontNoWarn = CkJsonObject_BoolOf(json,"signatureDictionary./Prop_Build./PubSec./NonEFontNoWarn"); signatureDictionary_Prop_Build_PubSec_R = CkJsonObject_IntOf(json,"signatureDictionary./Prop_Build./PubSec./R"); signatureDictionary_SubFilter = CkJsonObject_stringOf(json,"signatureDictionary./SubFilter"); signatureDictionary_Type = CkJsonObject_stringOf(json,"signatureDictionary./Type"); i = 0; count_i = CkJsonObject_SizeOfArray(json,"signatureDictionary./ByteRange"); while (i < count_i) { CkJsonObject_putI(json,i); intVal = CkJsonObject_IntOf(json,"signatureDictionary./ByteRange[i]"); i = i + 1; } i = 0; count_i = CkJsonObject_SizeOfArray(json,"signatureDictionary./Prop_Build./App./OS"); while (i < count_i) { CkJsonObject_putI(json,i); strVal = CkJsonObject_stringOf(json,"signatureDictionary./Prop_Build./App./OS[i]"); i = i + 1; } i = 0; count_i = CkJsonObject_SizeOfArray(json,"pkcs7.verify.certs"); while (i < count_i) { CkJsonObject_putI(json,i); issuerCN = CkJsonObject_stringOf(json,"pkcs7.verify.certs[i].issuerCN"); serial = CkJsonObject_stringOf(json,"pkcs7.verify.certs[i].serial"); i = i + 1; } i = 0; count_i = CkJsonObject_SizeOfArray(json,"pkcs7.verify.digestAlgorithms"); while (i < count_i) { CkJsonObject_putI(json,i); strVal = CkJsonObject_stringOf(json,"pkcs7.verify.digestAlgorithms[i]"); i = i + 1; } i = 0; count_i = CkJsonObject_SizeOfArray(json,"pkcs7.verify.signerInfo"); while (i < count_i) { CkJsonObject_putI(json,i); certSerialNumber = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].cert.serialNumber"); certIssuerCN = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].cert.issuerCN"); certDigestAlgOid = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].cert.digestAlgOid"); certDigestAlgName = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].cert.digestAlgName"); contentType = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].contentType"); messageDigest = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].messageDigest"); signingAlgOid = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].signingAlgOid"); signingAlgName = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].signingAlgName"); authAttr1_2_840_113583_1_1_8Der = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113583.1.1.8\".der"); authAttrContentTypeName = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".name"); authAttrContentTypeOid = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.3\".oid"); authAttrMessageDigestName = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".name"); authAttrMessageDigestDigest = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].authAttr.\"1.2.840.113549.1.9.4\".digest"); unauthAttrTimestampTokenName = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".name"); unauthAttrTimestampTokenDer = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".der"); unauthAttrTimestampTokenTimestampSignatureVerified = CkJsonObject_BoolOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".timestampSignatureVerified"); unauthAttrTimestampTokenTstInfoTsaPolicyId = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.tsaPolicyId"); unauthAttrTimestampTokenTstInfoMessageImprintHashAlg = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.hashAlg"); unauthAttrTimestampTokenTstInfoMessageImprintDigest = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.digest"); unauthAttrTimestampTokenTstInfoMessageImprintDigestMatches = CkJsonObject_BoolOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.messageImprint.digestMatches"); unauthAttrTimestampTokenTstInfoSerialNumber = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.serialNumber"); CkJsonObject_DtOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".tstInfo.genTime",FALSE,unauthAttrTimestampTokenTstInfoGenTime); j = 0; count_j = CkJsonObject_SizeOfArray(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms"); while (j < count_j) { CkJsonObject_putJ(json,j); strVal = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.digestAlgorithms[j]"); j = j + 1; } j = 0; count_j = CkJsonObject_SizeOfArray(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo"); while (j < count_j) { CkJsonObject_putJ(json,j); certSerialNumber = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.serialNumber"); certIssuerCN = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.issuerCN"); certDigestAlgOid = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgOid"); certDigestAlgName = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].cert.digestAlgName"); contentType = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].contentType"); CkJsonObject_DtOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingTime",FALSE,signingTime); messageDigest = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].messageDigest"); signingAlgOid = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgOid"); signingAlgName = CkJsonObject_stringOf(json,"pkcs7.verify.signerInfo[i].unauthAttr.\"1.2.840.113549.1.9.16.2.14\".verify.signerInfo[j].signingAlgName"); authAttrContentTypeName = CkJsonObject_stringOf(json,"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"); authAttrContentTypeOid = CkJsonObject_stringOf(json,"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"); authAttrSigningTimeName = CkJsonObject_stringOf(json,"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"); CkJsonObject_DtOf(json,"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,authAttrSigningTimeUtctime); authAttrSigningCertificateName = CkJsonObject_stringOf(json,"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"); authAttrSigningCertificateDer = CkJsonObject_stringOf(json,"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"); authAttrMessageDigestName = CkJsonObject_stringOf(json,"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"); authAttrMessageDigestDigest = CkJsonObject_stringOf(json,"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 = CkJsonObject_SizeOfArray(json,"pkcs7.verify.pkcs7.verify.certs"); while (i < count_i) { CkJsonObject_putI(json,i); issuerCN = CkJsonObject_stringOf(json,"pkcs7.verify.pkcs7.verify.certs[i].issuerCN"); serial = CkJsonObject_stringOf(json,"pkcs7.verify.pkcs7.verify.certs[i].serial"); i = i + 1; } CkPdf_Dispose(pdf); CkJsonObject_Dispose(sigInfo); CkJsonObject_Dispose(json); CkDtObj_Dispose(unauthAttrTimestampTokenTstInfoGenTime); CkDtObj_Dispose(signingTime); CkDtObj_Dispose(authAttrSigningTimeUtctime); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.