Sample code for 30+ languages & platforms
PureBasic

Validate a .pkpass Archive

See more Digital Signatures Examples

Opens 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.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkCrypt2.pb"
IncludeFile "CkZip.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkZipEntry.pb"
IncludeFile "CkBinData.pb"

Procedure ChilkatExample()

    success.i = 0

    ; 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 = 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 = CkZipEntry::ckCreate()
    If ent.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckEntryOf(zip,"manifest.json",ent)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkCrypt2::ckDispose(crypt)
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(ent)
        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)

    ; 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))

        success = CkZip::ckEntryOf(zip,filename,ent)
        If success = 0
            Debug CkZip::ckLastErrorText(zip)
            CkCrypt2::ckDispose(crypt)
            CkZip::ckDispose(zip)
            CkZipEntry::ckDispose(ent)
            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

        i = i + 1
    Wend

    If someHashesFailed = 1
        Debug "Some hashes failed."
        CkCrypt2::ckDispose(crypt)
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(ent)
        CkBinData::ckDispose(bdManifest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbHashHex)
        CkBinData::ckDispose(bdFileData)
        ProcedureReturn
    EndIf

    ; Let's verify the signature..
    ; First get the signature.
    success = CkZip::ckEntryOf(zip,"signature",ent)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkCrypt2::ckDispose(crypt)
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(ent)
        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)

    ; 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)
    CkZipEntry::ckDispose(ent)
    CkBinData::ckDispose(bdManifest)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbHashHex)
    CkBinData::ckDispose(bdFileData)
    CkBinData::ckDispose(bdSignature)


    ProcedureReturn
EndProcedure