SQL Server
SQL Server
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 memoryChilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- 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
DECLARE @manifest int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @manifest OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
DECLARE @zip int
EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT
EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'notUsedAndNeverCreated.zip'
EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha1'
-- Return hashes as lowercase hex.
EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hexlower'
DECLARE @digestStr nvarchar(4000)
DECLARE @pngData int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @pngData OUT
-- Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
EXEC sp_OAMethod @zip, 'AddBd', @success OUT, 'icon.png', @pngData
EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"icon.png"', @digestStr
EXEC sp_OAMethod @pngData, 'Clear', @success OUT
-- Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
EXEC sp_OAMethod @zip, 'AddBd', @success OUT, 'icon@2x.png', @pngData
EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"icon@2x.png"', @digestStr
EXEC sp_OAMethod @pngData, 'Clear', @success OUT
-- Assume we load the pngData with bytes for "logo.png" from somewhere...
EXEC sp_OAMethod @zip, 'AddBd', @success OUT, 'logo.png', @pngData
EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"logo.png"', @digestStr
EXEC sp_OAMethod @pngData, 'Clear', @success OUT
-- Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
EXEC sp_OAMethod @zip, 'AddBd', @success OUT, 'logo@2x.png', @pngData
EXEC sp_OAMethod @crypt, 'HashBdENC', @digestStr OUT, @pngData
EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"logo@2x.png"', @digestStr
DECLARE @passJson nvarchar(4000)
SELECT @passJson = '{ .... }' -- Contains the contents of pass.json
EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'pass.json', @passJson, 'utf-8'
EXEC sp_OAMethod @crypt, 'HashStringENC', @digestStr OUT, @passJson
EXEC sp_OAMethod @manifest, 'UpdateString', @success OUT, '"pass.json"', @digestStr
EXEC sp_OAMethod @manifest, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'manifest.json', @sTmp0, 'utf-8'
-- Make sure we have the Apple WWDR intermediate certificate available for
-- the cert chain in the signature.
DECLARE @certVault int
EXEC @hr = sp_OACreate 'Chilkat.XmlCertVault', @certVault OUT
DECLARE @appleWwdrCert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @appleWwdrCert OUT
EXEC sp_OAMethod @appleWwdrCert, 'LoadByCommonName', @success OUT, 'Apple Worldwide Developer Relations Certification Authority'
IF @success <> 1
BEGIN
PRINT 'The Apple WWDR intermediate certificate is not installed.'
PRINT 'It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer'
PRINT 'You may alternatively load the .cer like this...'
EXEC sp_OAMethod @appleWwdrCert, 'LoadFromFile', @success OUT, 'qa_data/certs/AppleWWDRCA.cer'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @appleWwdrCert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @manifest
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @pngData
EXEC @hr = sp_OADestroy @certVault
EXEC @hr = sp_OADestroy @appleWwdrCert
RETURN
END
END
EXEC sp_OAMethod @certVault, 'AddCert', @success OUT, @appleWwdrCert
EXEC sp_OAMethod @crypt, 'UseCertVault', @success OUT, @certVault
-- Use a digital certificate and private key from a PFX
DECLARE @bdPfx int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdPfx OUT
-- Assume we loaded a PFX into bdPfx....
DECLARE @pfxPassword nvarchar(4000)
SELECT @pfxPassword = 'test123'
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @cert, 'LoadPfxBd', @success OUT, @bdPfx, @pfxPassword
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @manifest
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @pngData
EXEC @hr = sp_OADestroy @certVault
EXEC @hr = sp_OADestroy @appleWwdrCert
EXEC @hr = sp_OADestroy @bdPfx
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- Provide the signing cert (with associated private key).
EXEC sp_OAMethod @crypt, 'SetSigningCert', @success OUT, @cert
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @manifest
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @pngData
EXEC @hr = sp_OADestroy @certVault
EXEC @hr = sp_OADestroy @appleWwdrCert
EXEC @hr = sp_OADestroy @bdPfx
EXEC @hr = sp_OADestroy @cert
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.)
DECLARE @jsonSignedAttrs int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonSignedAttrs OUT
EXEC sp_OAMethod @jsonSignedAttrs, 'UpdateInt', @success OUT, 'contentType', 1
EXEC sp_OAMethod @jsonSignedAttrs, 'UpdateInt', @success OUT, 'signingTime', 1
EXEC sp_OAMethod @jsonSignedAttrs, 'Emit', @sTmp0 OUT
EXEC sp_OASetProperty @crypt, 'SigningAttributes', @sTmp0
-- Sign the manifest JSON to produce a signature
EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'
DECLARE @sig nvarchar(4000)
EXEC sp_OAMethod @manifest, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @crypt, 'SignStringENC', @sig OUT, @sTmp0
DECLARE @bdSig int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdSig OUT
EXEC sp_OAMethod @bdSig, 'AppendEncoded', @success OUT, @sig, 'base64'
EXEC sp_OAMethod @zip, 'AddBd', @success OUT, 'signature', @bdSig
-- ---------------------------------------------------------------------------------------------
-- 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
DECLARE @bdZip int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdZip OUT
EXEC sp_OAMethod @zip, 'WriteBd', @success OUT, @bdZip
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @manifest
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @pngData
EXEC @hr = sp_OADestroy @certVault
EXEC @hr = sp_OADestroy @appleWwdrCert
EXEC @hr = sp_OADestroy @bdPfx
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @jsonSignedAttrs
EXEC @hr = sp_OADestroy @bdSig
EXEC @hr = sp_OADestroy @bdZip
RETURN
END
PRINT 'Success.'
EXEC @hr = sp_OADestroy @manifest
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @pngData
EXEC @hr = sp_OADestroy @certVault
EXEC @hr = sp_OADestroy @appleWwdrCert
EXEC @hr = sp_OADestroy @bdPfx
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @jsonSignedAttrs
EXEC @hr = sp_OADestroy @bdSig
EXEC @hr = sp_OADestroy @bdZip
END
GO