Sample code for 30+ languages & platforms
DataFlex

Sign Manifest File to Generate a Passbook .pkpass file

See more Digital Signatures Examples

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

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoManifest
    Handle hoCrypt
    Handle hoZip
    String sFileHash
    String sFilePath
    Variant vSbJson
    Handle hoSbJson
    String sManifestPath
    Variant vCert
Vault    Handle hoCertVault
    Variant vAppleWwdrCert
    Handle hoAppleWwdrCert
    String sPfxPath
    String sPfxPassword
    Variant vCert
    Handle hoCert
    Handle hoJsonSignedAttrs
    String sSigPath
    String sTemp1

    Move False To iSuccess

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

    // ---------------------------------------------------------------------------------------------
    // Note: Chilkat also has the capability to do everything in-memory (no files would be involved).  
    // See this example:  Sign Manifest File to Generate a Passbook .pkpass in Memory
    // ---------------------------------------------------------------------------------------------

    // 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 "qa_data/p7s/pass-wallet/example.pkpass" To iSuccess
    // Set the AppendFromDir property to prevent that relative paths from being stored in the .pkpass archive.
    Set ComAppendFromDir Of hoZip To "qa_data/p7s/pass-wallet/"

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

    Move "qa_data/p7s/pass-wallet/icon.png" To sFilePath
    Get ComHashFileENC Of hoCrypt sFilePath To sFileHash
    Get ComAddFile Of hoZip "icon.png" False To iSuccess
    Get ComUpdateString Of hoManifest '"icon.png"' sFileHash To iSuccess

    Move "qa_data/p7s/pass-wallet/icon@2x.png" To sFilePath
    Get ComHashFileENC Of hoCrypt sFilePath To sFileHash
    Get ComAddFile Of hoZip "icon@2x.png" False To iSuccess
    Get ComUpdateString Of hoManifest '"icon@2x.png"' sFileHash To iSuccess

    Move "qa_data/p7s/pass-wallet/logo.png" To sFilePath
    Get ComHashFileENC Of hoCrypt sFilePath To sFileHash
    Get ComAddFile Of hoZip "logo.png" False To iSuccess
    Get ComUpdateString Of hoManifest '"logo.png"' sFileHash To iSuccess

    Move "qa_data/p7s/pass-wallet/logo@2x.png" To sFilePath
    Get ComHashFileENC Of hoCrypt sFilePath To sFileHash
    Get ComAddFile Of hoZip "logo@2x.png" False To iSuccess
    Get ComUpdateString Of hoManifest '"logo@2x.png"' sFileHash To iSuccess

    Move "qa_data/p7s/pass-wallet/pass.json" To sFilePath
    Get ComHashFileENC Of hoCrypt sFilePath To sFileHash
    Get ComAddFile Of hoZip "pass.json" False To iSuccess
    Get ComUpdateString Of hoManifest '"pass.json"' sFileHash To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJson
    If (Not(IsComObjectCreated(hoSbJson))) Begin
        Send CreateComObject of hoSbJson
    End
    Get pvComObject of hoSbJson to vSbJson
    Get ComEmitSb Of hoManifest vSbJson To iSuccess
    Move "qa_data/p7s/pass-wallet/manifest.json" To sManifestPath
    Get ComWriteFile Of hoSbJson sManifestPath "utf-8" False To iSuccess
    Get ComAddFile Of hoZip "manifest.json" False 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 file (.pfx or .p12).
    Move "qa_data/pfx/cert_test123.pfx" To sPfxPath
    Move "test123" To sPfxPassword

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert sPfxPath 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 file to produce a file named "signature".
    Move "qa_data/p7s/pass-wallet/signature" To sSigPath

    // Create the "signature" file.
    Get ComCreateP7S Of hoCrypt sManifestPath sSigPath To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddFile Of hoZip "signature" False 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).
    Get ComWriteZipAndClose Of hoZip To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Success."


End_Procedure