Sample code for 30+ languages & platforms
DataFlex

Sign Manifest File to Generate a Passbook .pkpass in Memory

Demonstrates how to create a Passbook .pkpass archive by creating a signature of a manifest file and then zipping to a .pkpass archive in memory

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoManifest
    Handle hoCrypt
    Handle hoZip
    String sDigestStr
    Variant vPngData
    Handle hoPngData
    String sPassJson
    Variant vCert
Vault    Handle hoCertVault
    Variant vAppleWwdrCert
    Handle hoAppleWwdrCert
    Variant vBdPfx
    Handle hoBdPfx
    String sPfxPassword
    Variant vCert
    Handle hoCert
    Handle hoJsonSignedAttrs
    String sSig
    Variant vBdSig
    Handle hoBdSig
    Variant vBdZip
    Handle hoBdZip
    String sTemp1

    Move False To iSuccess

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // ---------------------------------------------------------------------------------------------
    // This example is the same as Sign Manifest File to Generate a Passbook .pkpass file
    // except everything happens in memory (no input files, no output files)
    // ---------------------------------------------------------------------------------------------

    // First create the manifest.json

    Get Create (RefClass(cComChilkatJsonObject)) To hoManifest
    If (Not(IsComObjectCreated(hoManifest))) Begin
        Send CreateComObject of hoManifest
    End
    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End

    Get Create (RefClass(cComChilkatZip)) To hoZip
    If (Not(IsComObjectCreated(hoZip))) Begin
        Send CreateComObject of hoZip
    End
    Get ComNewZip Of hoZip "notUsedAndNeverCreated.zip" To iSuccess

    Set ComHashAlgorithm Of hoCrypt To "sha1"
    // Return hashes as lowercase hex.
    Set ComEncodingMode Of hoCrypt To "hexlower"

    Get Create (RefClass(cComChilkatBinData)) To hoPngData
    If (Not(IsComObjectCreated(hoPngData))) Begin
        Send CreateComObject of hoPngData
    End
    // Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
    Get pvComObject of hoPngData to vPngData
    Get ComAddBd Of hoZip "icon.png" vPngData To iSuccess
    Get pvComObject of hoPngData to vPngData
    Get ComHashBdENC Of hoCrypt vPngData To sDigestStr
    Get ComUpdateString Of hoManifest '"icon.png"' sDigestStr To iSuccess

    Get ComClear Of hoPngData To iSuccess
    // Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
    Get pvComObject of hoPngData to vPngData
    Get ComAddBd Of hoZip "icon@2x.png" vPngData To iSuccess
    Get pvComObject of hoPngData to vPngData
    Get ComHashBdENC Of hoCrypt vPngData To sDigestStr
    Get ComUpdateString Of hoManifest '"icon@2x.png"' sDigestStr To iSuccess

    Get ComClear Of hoPngData To iSuccess
    // Assume we load the pngData with bytes for "logo.png" from somewhere...
    Get pvComObject of hoPngData to vPngData
    Get ComAddBd Of hoZip "logo.png" vPngData To iSuccess
    Get pvComObject of hoPngData to vPngData
    Get ComHashBdENC Of hoCrypt vPngData To sDigestStr
    Get ComUpdateString Of hoManifest '"logo.png"' sDigestStr To iSuccess

    Get ComClear Of hoPngData To iSuccess
    // Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
    Get pvComObject of hoPngData to vPngData
    Get ComAddBd Of hoZip "logo@2x.png" vPngData To iSuccess
    Get pvComObject of hoPngData to vPngData
    Get ComHashBdENC Of hoCrypt vPngData To sDigestStr
    Get ComUpdateString Of hoManifest '"logo@2x.png"' sDigestStr To iSuccess

    Move "{ .... }" To sPassJson    //  Contains the contents of pass.json
    Get ComAddString Of hoZip "pass.json" sPassJson "utf-8" To iSuccess
    Get ComHashStringENC Of hoCrypt sPassJson To sDigestStr
    Get ComUpdateString Of hoManifest '"pass.json"' sDigestStr To iSuccess

    Get ComEmit Of hoManifest To sTemp1
    Get ComAddString Of hoZip "manifest.json" sTemp1 "utf-8" To iSuccess

    // Make sure we have the Apple WWDR intermediate certificate available for 
    // the cert chain in the signature.
    Get Create (RefClass(cComChilkatXmlCertVault)) To hoCertVault
    If (Not(IsComObjectCreated(hoCertVault))) Begin
        Send CreateComObject of hoCertVault
    End
    Get Create (RefClass(cComChilkatCert)) To hoAppleWwdrCert
    If (Not(IsComObjectCreated(hoAppleWwdrCert))) Begin
        Send CreateComObject of hoAppleWwdrCert
    End
    Get ComLoadByCommonName Of hoAppleWwdrCert "Apple Worldwide Developer Relations Certification Authority" To iSuccess
    If (iSuccess <> True) Begin
        Showln "The Apple WWDR intermediate certificate is not installed."
        Showln "It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer"
        Showln "You may alternatively load the .cer like this..."
        Get ComLoadFromFile Of hoAppleWwdrCert "qa_data/certs/AppleWWDRCA.cer" To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoAppleWwdrCert To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    End

    Get pvComObject of hoAppleWwdrCert to vAppleWwdrCert
    Get ComAddCert Of hoCertVault vAppleWwdrCert To iSuccess
    Get pvComObject of hoCertVault to vCertVault
    Get ComUseCertVault Of hoCrypt vCertVault To iSuccess

    // Use a digital certificate and private key from a PFX
    Get Create (RefClass(cComChilkatBinData)) To hoBdPfx
    If (Not(IsComObjectCreated(hoBdPfx))) Begin
        Send CreateComObject of hoBdPfx
    End
    // Assume we loaded a PFX into bdPfx....
    Move "test123" To sPfxPassword

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get pvComObject of hoBdPfx to vBdPfx
    Get ComLoadPfxBd Of hoCert vBdPfx sPfxPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Provide the signing cert (with associated private key).
    Get pvComObject of hoCert to vCert
    Get ComSetSigningCert Of hoCrypt vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Specify the signed attributes to be included.
    // (These attributes appear to not be necessary, but we're including
    // them just in case they become necessary in the future.)
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonSignedAttrs
    If (Not(IsComObjectCreated(hoJsonSignedAttrs))) Begin
        Send CreateComObject of hoJsonSignedAttrs
    End
    Get ComUpdateInt Of hoJsonSignedAttrs "contentType" 1 To iSuccess
    Get ComUpdateInt Of hoJsonSignedAttrs "signingTime" 1 To iSuccess
    Get ComEmit Of hoJsonSignedAttrs To sTemp1
    Set ComSigningAttributes Of hoCrypt To sTemp1

    // Sign the manifest JSON to produce a signature
    Set ComEncodingMode Of hoCrypt To "base64"
    Get ComEmit Of hoManifest To sTemp1
    Get ComSignStringENC Of hoCrypt sTemp1 To sSig
    Get Create (RefClass(cComChilkatBinData)) To hoBdSig
    If (Not(IsComObjectCreated(hoBdSig))) Begin
        Send CreateComObject of hoBdSig
    End
    Get ComAppendEncoded Of hoBdSig sSig "base64" To iSuccess
    Get pvComObject of hoBdSig to vBdSig
    Get ComAddBd Of hoZip "signature" vBdSig To iSuccess

    // ---------------------------------------------------------------------------------------------
    // Note: Chilkat also has the capability to do everything in-memory (no files would be involved).  
    // If this is of interest, please send email to support@chilkatsoft.com
    // ---------------------------------------------------------------------------------------------

    // Create the .pkipass archive (which is a .zip archive containing the required files).
    // the .zip is written to bdZip
    Get Create (RefClass(cComChilkatBinData)) To hoBdZip
    If (Not(IsComObjectCreated(hoBdZip))) Begin
        Send CreateComObject of hoBdZip
    End
    Get pvComObject of hoBdZip to vBdZip
    Get ComWriteBd Of hoZip vBdZip To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Success."


End_Procedure