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
(PowerBuilder) 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.
integer li_rc oleobject loo_Pdf integer li_Success oleobject loo_SigInfo integer li_NumSignatures integer li_Validated integer i oleobject loo_Json oleobject loo_UnauthAttrTimestampTokenTstInfoGenTime oleobject loo_SigningTime oleobject loo_AuthAttrSigningTimeUtctime integer li_IntVal string ls_StrVal string ls_IssuerCN string ls_Serial string ls_CertSerialNumber string ls_CertIssuerCN string ls_CertDigestAlgOid string ls_CertDigestAlgName string ls_ContentType string ls_MessageDigest string ls_SigningAlgOid string ls_SigningAlgName string ls_AuthAttr1_2_840_113583_1_1_8Der string ls_AuthAttrContentTypeName string ls_AuthAttrContentTypeOid string ls_AuthAttrMessageDigestName string ls_AuthAttrMessageDigestDigest string ls_UnauthAttrTimestampTokenName string ls_UnauthAttrTimestampTokenDer integer li_UnauthAttrTimestampTokenTimestampSignatureVerified string ls_UnauthAttrTimestampTokenTstInfoTsaPolicyId string ls_UnauthAttrTimestampTokenTstInfoMessageImprintHashAlg string ls_UnauthAttrTimestampTokenTstInfoMessageImprintDigest integer li_UnauthAttrTimestampTokenTstInfoMessageImprintDigestMatches string ls_UnauthAttrTimestampTokenTstInfoSerialNumber integer j integer li_Count_j string ls_AuthAttrSigningTimeName string ls_AuthAttrSigningCertificateName string ls_AuthAttrSigningCertificateDer string ls_SignatureDictionary_Contents string ls_SignatureDictionary_Filter string ls_SignatureDictionary_M string ls_SignatureDictionary_Name string ls_SignatureDictionary_Prop_Build_App_Name integer li_SignatureDictionary_Prop_Build_App_R string ls_SignatureDictionary_Prop_Build_App_REx integer li_SignatureDictionary_Prop_Build_App_TrustedMode string ls_SignatureDictionary_Prop_Build_Filter_Date string ls_SignatureDictionary_Prop_Build_Filter_Name integer li_SignatureDictionary_Prop_Build_Filter_R integer li_SignatureDictionary_Prop_Build_Filter_V string ls_SignatureDictionary_Prop_Build_PubSec_Date integer li_SignatureDictionary_Prop_Build_PubSec_NonEFontNoWarn integer li_SignatureDictionary_Prop_Build_PubSec_R string ls_SignatureDictionary_SubFilter string ls_SignatureDictionary_Type integer li_Count_i // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Pdf = create oleobject // Use "Chilkat_9_5_0.Pdf" for versions of Chilkat < 10.0.0 li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf") if li_rc < 0 then destroy loo_Pdf MessageBox("Error","Connecting to COM object failed") return end if // Load a PDF that has cryptographic signatures to be validated li_Success = loo_Pdf.LoadFile("qa_data/pdf/sign_testing_1/helloSigned2.pdf") if li_Success = 0 then Write-Debug loo_Pdf.LastErrorText destroy loo_Pdf return end if // Each time we verify a signature, information about the signature is written into // sigInfo (replacing whatever sigInfo previously contained). loo_SigInfo = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_SigInfo.ConnectToNewObject("Chilkat.JsonObject") loo_SigInfo.EmitCompact = 0 // Iterate over each signature and validate each. li_NumSignatures = loo_Pdf.NumSignatures li_Validated = 0 i = 0 do while i < li_NumSignatures li_Validated = loo_Pdf.VerifySignature(i,loo_SigInfo) Write-Debug "Signature " + string(i) + " validated: " + string(li_Validated) Write-Debug loo_SigInfo.Emit() i = i + 1 loop Write-Debug "Finished." // 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. loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") // 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 loo_UnauthAttrTimestampTokenTstInfoGenTime = create oleobject // Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0 li_rc = loo_UnauthAttrTimestampTokenTstInfoGenTime.ConnectToNewObject("Chilkat.DtObj") loo_SigningTime = create oleobject // Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0 li_rc = loo_SigningTime.ConnectToNewObject("Chilkat.DtObj") loo_AuthAttrSigningTimeUtctime = create oleobject // Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0 li_rc = loo_AuthAttrSigningTimeUtctime.ConnectToNewObject("Chilkat.DtObj") li_Validated = loo_Json.BoolOf("validated") ls_SignatureDictionary_Contents = loo_Json.StringOf("signatureDictionary./Contents") ls_SignatureDictionary_Filter = loo_Json.StringOf("signatureDictionary./Filter") ls_SignatureDictionary_M = loo_Json.StringOf("signatureDictionary./M") ls_SignatureDictionary_Name = loo_Json.StringOf("signatureDictionary./Name") ls_SignatureDictionary_Prop_Build_App_Name = loo_Json.StringOf("signatureDictionary./Prop_Build./App./Name") li_SignatureDictionary_Prop_Build_App_R = loo_Json.IntOf("signatureDictionary./Prop_Build./App./R") ls_SignatureDictionary_Prop_Build_App_REx = loo_Json.StringOf("signatureDictionary./Prop_Build./App./REx") li_SignatureDictionary_Prop_Build_App_TrustedMode = loo_Json.BoolOf("signatureDictionary./Prop_Build./App./TrustedMode") ls_SignatureDictionary_Prop_Build_Filter_Date = loo_Json.StringOf("signatureDictionary./Prop_Build./Filter./Date") ls_SignatureDictionary_Prop_Build_Filter_Name = loo_Json.StringOf("signatureDictionary./Prop_Build./Filter./Name") li_SignatureDictionary_Prop_Build_Filter_R = loo_Json.IntOf("signatureDictionary./Prop_Build./Filter./R") li_SignatureDictionary_Prop_Build_Filter_V = loo_Json.IntOf("signatureDictionary./Prop_Build./Filter./V") ls_SignatureDictionary_Prop_Build_PubSec_Date = loo_Json.StringOf("signatureDictionary./Prop_Build./PubSec./Date") li_SignatureDictionary_Prop_Build_PubSec_NonEFontNoWarn = loo_Json.BoolOf("signatureDictionary./Prop_Build./PubSec./NonEFontNoWarn") li_SignatureDictionary_Prop_Build_PubSec_R = loo_Json.IntOf("signatureDictionary./Prop_Build./PubSec./R") ls_SignatureDictionary_SubFilter = loo_Json.StringOf("signatureDictionary./SubFilter") ls_SignatureDictionary_Type = loo_Json.StringOf("signatureDictionary./Type") i = 0 li_Count_i = loo_Json.SizeOfArray("signatureDictionary./ByteRange") do while i < li_Count_i loo_Json.I = i li_IntVal = loo_Json.IntOf("signatureDictionary./ByteRange[i]") i = i + 1 loop i = 0 li_Count_i = loo_Json.SizeOfArray("signatureDictionary./Prop_Build./App./OS") do while i < li_Count_i loo_Json.I = i ls_StrVal = loo_Json.StringOf("signatureDictionary./Prop_Build./App./OS[i]") i = i + 1 loop i = 0 li_Count_i = loo_Json.SizeOfArray("pkcs7.verify.certs") do while i < li_Count_i loo_Json.I = i ls_IssuerCN = loo_Json.StringOf("pkcs7.verify.certs[i].issuerCN") ls_Serial = loo_Json.StringOf("pkcs7.verify.certs[i].serial") i = i + 1 loop i = 0 li_Count_i = loo_Json.SizeOfArray("pkcs7.verify.digestAlgorithms") do while i < li_Count_i loo_Json.I = i ls_StrVal = loo_Json.StringOf("pkcs7.verify.digestAlgorithms[i]") i = i + 1 loop i = 0 li_Count_i = loo_Json.SizeOfArray("pkcs7.verify.signerInfo") do while i < li_Count_i loo_Json.I = i ls_CertSerialNumber = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.serialNumber") ls_CertIssuerCN = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.issuerCN") ls_CertDigestAlgOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.digestAlgOid") ls_CertDigestAlgName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].cert.digestAlgName") ls_ContentType = loo_Json.StringOf("pkcs7.verify.signerInfo[i].contentType") ls_MessageDigest = loo_Json.StringOf("pkcs7.verify.signerInfo[i].messageDigest") ls_SigningAlgOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].signingAlgOid") ls_SigningAlgName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].signingAlgName") ls_AuthAttr1_2_840_113583_1_1_8Der = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113583.1.1.8~".der") ls_AuthAttrContentTypeName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.3~".name") ls_AuthAttrContentTypeOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.3~".oid") ls_AuthAttrMessageDigestName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.4~".name") ls_AuthAttrMessageDigestDigest = loo_Json.StringOf("pkcs7.verify.signerInfo[i].authAttr.~"1.2.840.113549.1.9.4~".digest") ls_UnauthAttrTimestampTokenName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".name") ls_UnauthAttrTimestampTokenDer = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".der") li_UnauthAttrTimestampTokenTimestampSignatureVerified = loo_Json.BoolOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".timestampSignatureVerified") ls_UnauthAttrTimestampTokenTstInfoTsaPolicyId = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".tstInfo.tsaPolicyId") ls_UnauthAttrTimestampTokenTstInfoMessageImprintHashAlg = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".tstInfo.messageImprint.hashAlg") ls_UnauthAttrTimestampTokenTstInfoMessageImprintDigest = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".tstInfo.messageImprint.digest") li_UnauthAttrTimestampTokenTstInfoMessageImprintDigestMatches = loo_Json.BoolOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".tstInfo.messageImprint.digestMatches") ls_UnauthAttrTimestampTokenTstInfoSerialNumber = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".tstInfo.serialNumber") loo_Json.DtOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".tstInfo.genTime",0,loo_UnauthAttrTimestampTokenTstInfoGenTime) j = 0 li_Count_j = loo_Json.SizeOfArray("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.digestAlgorithms") do while j < li_Count_j loo_Json.J = j ls_StrVal = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.digestAlgorithms[j]") j = j + 1 loop j = 0 li_Count_j = loo_Json.SizeOfArray("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo") do while j < li_Count_j loo_Json.J = j ls_CertSerialNumber = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].cert.serialNumber") ls_CertIssuerCN = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].cert.issuerCN") ls_CertDigestAlgOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].cert.digestAlgOid") ls_CertDigestAlgName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].cert.digestAlgName") ls_ContentType = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].contentType") loo_Json.DtOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].signingTime",0,loo_SigningTime) ls_MessageDigest = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].messageDigest") ls_SigningAlgOid = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].signingAlgOid") ls_SigningAlgName = loo_Json.StringOf("pkcs7.verify.signerInfo[i].unauthAttr.~"1.2.840.113549.1.9.16.2.14~".verify.signerInfo[j].signingAlgName") ls_AuthAttrContentTypeName = loo_Json.StringOf("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") ls_AuthAttrContentTypeOid = loo_Json.StringOf("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") ls_AuthAttrSigningTimeName = loo_Json.StringOf("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") loo_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",0,loo_AuthAttrSigningTimeUtctime) ls_AuthAttrSigningCertificateName = loo_Json.StringOf("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") ls_AuthAttrSigningCertificateDer = loo_Json.StringOf("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") ls_AuthAttrMessageDigestName = loo_Json.StringOf("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") ls_AuthAttrMessageDigestDigest = loo_Json.StringOf("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 loop i = i + 1 loop i = 0 li_Count_i = loo_Json.SizeOfArray("pkcs7.verify.pkcs7.verify.certs") do while i < li_Count_i loo_Json.I = i ls_IssuerCN = loo_Json.StringOf("pkcs7.verify.pkcs7.verify.certs[i].issuerCN") ls_Serial = loo_Json.StringOf("pkcs7.verify.pkcs7.verify.certs[i].serial") i = i + 1 loop destroy loo_Pdf destroy loo_SigInfo destroy loo_Json destroy loo_UnauthAttrTimestampTokenTstInfoGenTime destroy loo_SigningTime destroy loo_AuthAttrSigningTimeUtctime |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.