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
(PureBasic) Validate a .pkpass ArchiveOpens a .pkpass archive (which is just a .zip renamed to .pkpass) and validates the contents. The hashes in the manifest are compared with the computed hash values for each individual file. If all computed hash values match, then the signature is verified.
IncludeFile "CkJsonObject.pb" IncludeFile "CkCrypt2.pb" IncludeFile "CkZip.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkZipEntry.pb" IncludeFile "CkBinData.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. crypt.i = CkCrypt2::ckCreate() If crypt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf zip.i = CkZip::ckCreate() If zip.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkZip::ckOpenZip(zip,"qa_data/pkpass/invalid.pkpass") If success = 0 Debug CkZip::ckLastErrorText(zip) CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) ProcedureReturn EndIf ; Get the contents of the manifest.json file, which contains something like this: ; { ; "icon.png" : "0296b01347b3173e98438a003b0e88986340b2d8", ; "logo.png" : "25de09e2d3b01ce1fe00c2ca9a90a2be1aaa05cf", ; "icon@2x.png" : "5afd9585b08c65fdf105a90c8bd643407cba2787", ; "pass.json" : "145ea5a5db784fff485126c77ecf7a1fc2a88ee7", ; "strip@2x.png" : "468fa7bc93e6b55342b56fda09bdce7c829d7d46", ; "strip.png" : "736d01f84cb73d06e8a9932e43076d68f19461ff" ; } ent.i = CkZip::ckGetEntryByName(zip,"manifest.json") If CkZip::ckLastMethodSuccess(zip) = 0 Debug "manifest.json entry not found." CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) ProcedureReturn EndIf ; Get the exact content of the manifest.json for later signature verification. bdManifest.i = CkBinData::ckCreate() If bdManifest.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkZipEntry::ckUnzipToBd(ent,bdManifest) json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::setCkEmitCompact(json, 0) CkJsonObject::ckLoad(json,CkZipEntry::ckUnzipToString(ent,0,"utf-8")) Debug CkJsonObject::ckEmit(json) CkZipEntry::ckDispose(ent) ; For each file in the JSON, get the filename and hex hash value. CkCrypt2::setCkEncodingMode(crypt, "hexlower") CkCrypt2::setCkHashAlgorithm(crypt, "sha1") someHashesFailed.i = 0 filename.s sbHashHex.i = CkStringBuilder::ckCreate() If sbHashHex.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bdFileData.i = CkBinData::ckCreate() If bdFileData.i = 0 Debug "Failed to create object." ProcedureReturn EndIf numMembers.i = CkJsonObject::ckSize(json) i.i = 0 While i < numMembers filename = CkJsonObject::ckNameAt(json,i) CkStringBuilder::ckClear(sbHashHex) CkStringBuilder::ckAppend(sbHashHex,CkJsonObject::ckStringAt(json,i)) ent = CkZip::ckGetEntryByName(zip,filename) If CkZip::ckLastMethodSuccess(zip) = 0 Debug filename + " not found in the pkpass file." CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) CkBinData::ckDispose(bdManifest) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbHashHex) CkBinData::ckDispose(bdFileData) ProcedureReturn EndIf ; Get the data for this file. CkBinData::ckClear(bdFileData) success = CkZipEntry::ckUnzipToBd(ent,bdFileData) computedHashHex.s = CkCrypt2::ckHashBdENC(crypt,bdFileData) If CkStringBuilder::ckContentsEqual(sbHashHex,computedHashHex,0) = 0 Debug "Computed hash does not match stored hash for " + filename Debug " computed: " + computedHashHex Debug " stored: " + CkStringBuilder::ckGetAsString(sbHashHex) someHashesFailed = 1 Else Debug "hash verified for " + filename + "(" + computedHashHex + ")" EndIf CkZipEntry::ckDispose(ent) i = i + 1 Wend If someHashesFailed = 1 Debug "Some hashes failed." CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) CkBinData::ckDispose(bdManifest) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbHashHex) CkBinData::ckDispose(bdFileData) ProcedureReturn EndIf ; Let's verify the signature.. ; First get the signature. ent = CkZip::ckGetEntryByName(zip,"signature") If CkZip::ckLastMethodSuccess(zip) = 0 Debug "signature not found in the pkpass file." CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) CkBinData::ckDispose(bdManifest) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbHashHex) CkBinData::ckDispose(bdFileData) ProcedureReturn EndIf bdSignature.i = CkBinData::ckCreate() If bdSignature.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkZipEntry::ckUnzipToBd(ent,bdSignature) CkZipEntry::ckDispose(ent) ; Show the contents of the signature in base64 encoding. Debug "Signature:" Debug CkBinData::ckGetEncoded(bdSignature,"base64_mime") Debug "----" ; Verify the signature against the manifest.json CkCrypt2::setCkEncodingMode(crypt, "base64") verified.i = CkCrypt2::ckVerifyBdENC(crypt,bdManifest,CkBinData::ckGetEncoded(bdSignature,"base64")) If verified = 0 Debug CkCrypt2::ckLastErrorText(crypt) EndIf Debug "signature verified = " + Str(verified) CkCrypt2::ckDispose(crypt) CkZip::ckDispose(zip) CkBinData::ckDispose(bdManifest) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbHashHex) CkBinData::ckDispose(bdFileData) CkBinData::ckDispose(bdSignature) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.