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
(DataFlex) 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.
Use ChilkatAx-win32.pkg Procedure Test Handle hoPdf Boolean iSuccess Variant vSigInfo Handle hoSigInfo Integer iNumSignatures Boolean iValidated Integer i Handle hoJson Variant vUnauthAttrTimestampTokenTstInfoGenTime Handle hoUnauthAttrTimestampTokenTstInfoGenTime Variant vSigningTime Handle hoSigningTime Variant vAuthAttrSigningTimeUtctime Handle hoAuthAttrSigningTimeUtctime Integer iIntVal String sStrVal String sIssuerCN String sSerial String sCertSerialNumber String sCertIssuerCN String sCertDigestAlgOid String sCertDigestAlgName String sContentType String sMessageDigest String sSigningAlgOid String sSigningAlgName String sAuthAttr1_2_840_113583_1_1_8Der String sAuthAttrContentTypeName String sAuthAttrContentTypeOid String sAuthAttrMessageDigestName String sAuthAttrMessageDigestDigest String sUnauthAttrTimestampTokenName String sUnauthAttrTimestampTokenDer Boolean iUnauthAttrTimestampTokenTimestampSignatureVerified String sUnauthAttrTimestampTokenTstInfoTsaPolicyId String sUnauthAttrTimestampTokenTstInfoMessageImprintHashAlg String sUnauthAttrTimestampTokenTstInfoMessageImprintDigest Boolean iUnauthAttrTimestampTokenTstInfoMessageImprintDigestMatches String sUnauthAttrTimestampTokenTstInfoSerialNumber Integer j Integer iCount_j String sAuthAttrSigningTimeName String sAuthAttrSigningCertificateName String sAuthAttrSigningCertificateDer String sSignatureDictionary_Contents String sSignatureDictionary_Filter String sSignatureDictionary_M String sSignatureDictionary_Name String sSignatureDictionary_Prop_Build_App_Name Integer iSignatureDictionary_Prop_Build_App_R String sSignatureDictionary_Prop_Build_App_REx Boolean iSignatureDictionary_Prop_Build_App_TrustedMode String sSignatureDictionary_Prop_Build_Filter_Date String sSignatureDictionary_Prop_Build_Filter_Name Integer iSignatureDictionary_Prop_Build_Filter_R Integer iSignatureDictionary_Prop_Build_Filter_V String sSignatureDictionary_Prop_Build_PubSec_Date Boolean iSignatureDictionary_Prop_Build_PubSec_NonEFontNoWarn Integer iSignatureDictionary_Prop_Build_PubSec_R String sSignatureDictionary_SubFilter String sSignatureDictionary_Type Integer iCount_i String sTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatPdf)) To hoPdf If (Not(IsComObjectCreated(hoPdf))) Begin Send CreateComObject of hoPdf End // Load a PDF that has cryptographic signatures to be validated Get ComLoadFile Of hoPdf "qa_data/pdf/sign_testing_1/helloSigned2.pdf" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoPdf To sTemp1 Showln sTemp1 Procedure_Return End // Each time we verify a signature, information about the signature is written into // sigInfo (replacing whatever sigInfo previously contained). Get Create (RefClass(cComChilkatJsonObject)) To hoSigInfo If (Not(IsComObjectCreated(hoSigInfo))) Begin Send CreateComObject of hoSigInfo End Set ComEmitCompact Of hoSigInfo To False // Iterate over each signature and validate each. Get ComNumSignatures Of hoPdf To iNumSignatures Move False To iValidated Move 0 To i While (i < iNumSignatures) Get pvComObject of hoSigInfo to vSigInfo Get ComVerifySignature Of hoPdf i vSigInfo To iValidated Showln "Signature " i " validated: " iValidated Get ComEmit Of hoSigInfo To sTemp1 Showln sTemp1 Move (i + 1) To i Loop Showln "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. Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End // 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 Get Create (RefClass(cComChilkatDtObj)) To hoUnauthAttrTimestampTokenTstInfoGenTime If (Not(IsComObjectCreated(hoUnauthAttrTimestampTokenTstInfoGenTime))) Begin Send CreateComObject of hoUnauthAttrTimestampTokenTstInfoGenTime End Get Create (RefClass(cComChilkatDtObj)) To hoSigningTime If (Not(IsComObjectCreated(hoSigningTime))) Begin Send CreateComObject of hoSigningTime End Get Create (RefClass(cComChilkatDtObj)) To hoAuthAttrSigningTimeUtctime If (Not(IsComObjectCreated(hoAuthAttrSigningTimeUtctime))) Begin Send CreateComObject of hoAuthAttrSigningTimeUtctime End Get ComBoolOf Of hoJson "validated" To iValidated Get ComStringOf Of hoJson "signatureDictionary./Contents" To sSignatureDictionary_Contents Get ComStringOf Of hoJson "signatureDictionary./Filter" To sSignatureDictionary_Filter Get ComStringOf Of hoJson "signatureDictionary./M" To sSignatureDictionary_M Get ComStringOf Of hoJson "signatureDictionary./Name" To sSignatureDictionary_Name Get ComStringOf Of hoJson "signatureDictionary./Prop_Build./App./Name" To sSignatureDictionary_Prop_Build_App_Name Get ComIntOf Of hoJson "signatureDictionary./Prop_Build./App./R" To iSignatureDictionary_Prop_Build_App_R Get ComStringOf Of hoJson "signatureDictionary./Prop_Build./App./REx" To sSignatureDictionary_Prop_Build_App_REx Get ComBoolOf Of hoJson "signatureDictionary./Prop_Build./App./TrustedMode" To iSignatureDictionary_Prop_Build_App_TrustedMode Get ComStringOf Of hoJson "signatureDictionary./Prop_Build./Filter./Date" To sSignatureDictionary_Prop_Build_Filter_Date Get ComStringOf Of hoJson "signatureDictionary./Prop_Build./Filter./Name" To sSignatureDictionary_Prop_Build_Filter_Name Get ComIntOf Of hoJson "signatureDictionary./Prop_Build./Filter./R" To iSignatureDictionary_Prop_Build_Filter_R Get ComIntOf Of hoJson "signatureDictionary./Prop_Build./Filter./V" To iSignatureDictionary_Prop_Build_Filter_V Get ComStringOf Of hoJson "signatureDictionary./Prop_Build./PubSec./Date" To sSignatureDictionary_Prop_Build_PubSec_Date Get ComBoolOf Of hoJson "signatureDictionary./Prop_Build./PubSec./NonEFontNoWarn" To iSignatureDictionary_Prop_Build_PubSec_NonEFontNoWarn Get ComIntOf Of hoJson "signatureDictionary./Prop_Build./PubSec./R" To iSignatureDictionary_Prop_Build_PubSec_R Get ComStringOf Of hoJson "signatureDictionary./SubFilter" To sSignatureDictionary_SubFilter Get ComStringOf Of hoJson "signatureDictionary./Type" To sSignatureDictionary_Type Move 0 To i Get ComSizeOfArray Of hoJson "signatureDictionary./ByteRange" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComIntOf Of hoJson "signatureDictionary./ByteRange[i]" To iIntVal Move (i + 1) To i Loop Move 0 To i Get ComSizeOfArray Of hoJson "signatureDictionary./Prop_Build./App./OS" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComStringOf Of hoJson "signatureDictionary./Prop_Build./App./OS[i]" To sStrVal Move (i + 1) To i Loop Move 0 To i Get ComSizeOfArray Of hoJson "pkcs7.verify.certs" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComStringOf Of hoJson "pkcs7.verify.certs[i].issuerCN" To sIssuerCN Get ComStringOf Of hoJson "pkcs7.verify.certs[i].serial" To sSerial Move (i + 1) To i Loop Move 0 To i Get ComSizeOfArray Of hoJson "pkcs7.verify.digestAlgorithms" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComStringOf Of hoJson "pkcs7.verify.digestAlgorithms[i]" To sStrVal Move (i + 1) To i Loop Move 0 To i Get ComSizeOfArray Of hoJson "pkcs7.verify.signerInfo" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].cert.serialNumber" To sCertSerialNumber Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].cert.issuerCN" To sCertIssuerCN Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].cert.digestAlgOid" To sCertDigestAlgOid Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].cert.digestAlgName" To sCertDigestAlgName Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].contentType" To sContentType Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].messageDigest" To sMessageDigest Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].signingAlgOid" To sSigningAlgOid Get ComStringOf Of hoJson "pkcs7.verify.signerInfo[i].signingAlgName" To sSigningAlgName Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113583.1.1.8".der' To sAuthAttr1_2_840_113583_1_1_8Der Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.3".name' To sAuthAttrContentTypeName Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.3".oid' To sAuthAttrContentTypeOid Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.4".name' To sAuthAttrMessageDigestName Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].authAttr."1.2.840.113549.1.9.4".digest' To sAuthAttrMessageDigestDigest Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".name' To sUnauthAttrTimestampTokenName Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".der' To sUnauthAttrTimestampTokenDer Get ComBoolOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".timestampSignatureVerified' To iUnauthAttrTimestampTokenTimestampSignatureVerified Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.tsaPolicyId' To sUnauthAttrTimestampTokenTstInfoTsaPolicyId Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.messageImprint.hashAlg' To sUnauthAttrTimestampTokenTstInfoMessageImprintHashAlg Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.messageImprint.digest' To sUnauthAttrTimestampTokenTstInfoMessageImprintDigest Get ComBoolOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.messageImprint.digestMatches' To iUnauthAttrTimestampTokenTstInfoMessageImprintDigestMatches Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.serialNumber' To sUnauthAttrTimestampTokenTstInfoSerialNumber Get pvComObject of hoUnauthAttrTimestampTokenTstInfoGenTime to vUnauthAttrTimestampTokenTstInfoGenTime Get ComDtOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".tstInfo.genTime' False vUnauthAttrTimestampTokenTstInfoGenTime To iSuccess Move 0 To j Get ComSizeOfArray Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.digestAlgorithms' To iCount_j While (j < iCount_j) Set ComJ Of hoJson To j Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.digestAlgorithms[j]' To sStrVal Move (j + 1) To j Loop Move 0 To j Get ComSizeOfArray Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo' To iCount_j While (j < iCount_j) Set ComJ Of hoJson To j Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.serialNumber' To sCertSerialNumber Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.issuerCN' To sCertIssuerCN Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.digestAlgOid' To sCertDigestAlgOid Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].cert.digestAlgName' To sCertDigestAlgName Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].contentType' To sContentType Get pvComObject of hoSigningTime to vSigningTime Get ComDtOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].signingTime' False vSigningTime To iSuccess Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].messageDigest' To sMessageDigest Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].signingAlgOid' To sSigningAlgOid Get ComStringOf Of hoJson 'pkcs7.verify.signerInfo[i].unauthAttr."1.2.840.113549.1.9.16.2.14".verify.signerInfo[j].signingAlgName' To sSigningAlgName Get ComStringOf Of hoJson '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' To sAuthAttrContentTypeName Get ComStringOf Of hoJson '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' To sAuthAttrContentTypeOid Get ComStringOf Of hoJson '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' To sAuthAttrSigningTimeName Get pvComObject of hoAuthAttrSigningTimeUtctime to vAuthAttrSigningTimeUtctime Get ComDtOf Of hoJson '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 vAuthAttrSigningTimeUtctime To iSuccess Get ComStringOf Of hoJson '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' To sAuthAttrSigningCertificateName Get ComStringOf Of hoJson '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' To sAuthAttrSigningCertificateDer Get ComStringOf Of hoJson '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' To sAuthAttrMessageDigestName Get ComStringOf Of hoJson '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' To sAuthAttrMessageDigestDigest Move (j + 1) To j Loop Move (i + 1) To i Loop Move 0 To i Get ComSizeOfArray Of hoJson "pkcs7.verify.pkcs7.verify.certs" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComStringOf Of hoJson "pkcs7.verify.pkcs7.verify.certs[i].issuerCN" To sIssuerCN Get ComStringOf Of hoJson "pkcs7.verify.pkcs7.verify.certs[i].serial" To sSerial Move (i + 1) To i Loop End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.