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
(SQL Server) Signed Zip as Base64 with XAdES-BESThis example is to help companies implement a solution for sending XAdES-BES to the Polish government for reporting purposes. Specifically: Przed podpisaniem deklaracja zbiorcza (PIT-11Z, PIT-8CZ, PIT-40Z, PIT-RZ) musi zostać The example demonstrates the following:
This example will also show the reverse:
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. -- Zip the PIT-11Z.xml to create PIT-11Z.zip (not as a .zip file, but in-memory). DECLARE @sbXmlToZip int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXmlToZip OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @sbXmlToZip, 'LoadFile', @success OUT, 'qa_data/xml/PIT-11Z.xml', 'utf-8' IF @success <> 1 BEGIN PRINT 'Failed to load the XML to be zipped.' EXEC @hr = sp_OADestroy @sbXmlToZip RETURN END DECLARE @zip int -- Use "Chilkat_9_5_0.Zip" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT -- Initialize the zip object. No file is created in this call. -- It should always return 1. EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'PIT-11Z.zip' -- Add the XML to be zipped. DECLARE @entry int EXEC sp_OAMethod @sbXmlToZip, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @zip, 'AppendString', @entry OUT, 'PIT-11Z.xml', @sTmp0 EXEC @hr = sp_OADestroy @entry -- Write the zip to a BinData object. DECLARE @bdZip int -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdZip OUT EXEC sp_OAMethod @zip, 'WriteBd', @success OUT, @bdZip -- The contents of the bdZip will be retrieved in base64 format when needed below.. DECLARE @gen int -- Use "Chilkat_9_5_0.XmlDSigGen" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.XmlDSigGen', @gen OUT EXEC sp_OASetProperty @gen, 'SigLocation', '' EXEC sp_OASetProperty @gen, 'SigLocationMod', 0 EXEC sp_OASetProperty @gen, 'SigId', 'Signature_2a8df7f8-b958-40cc-83f6-edb53b837347_19' EXEC sp_OASetProperty @gen, 'SigNamespacePrefix', 'ds' EXEC sp_OASetProperty @gen, 'SigNamespaceUri', 'http://www.w3.org/2000/09/xmldsig#' EXEC sp_OASetProperty @gen, 'SigValueId', 'SignatureValue_2a8df7f8-b958-40cc-83f6-edb53b837347_52' EXEC sp_OASetProperty @gen, 'SignedInfoId', 'SignedInfo_2a8df7f8-b958-40cc-83f6-edb53b837347_41' EXEC sp_OASetProperty @gen, 'SignedInfoCanonAlg', 'C14N' EXEC sp_OASetProperty @gen, 'SignedInfoDigestMethod', 'sha1' -- Set the KeyInfoId before adding references.. EXEC sp_OASetProperty @gen, 'KeyInfoId', 'KeyInfo_2a8df7f8-b958-40cc-83f6-edb53b837347_24' -- Create an Object to be added to the Signature. DECLARE @object1 int -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Xml', @object1 OUT EXEC sp_OASetProperty @object1, 'Tag', 'xades:QualifyingProperties' EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'xmlns:xades', 'http://uri.etsi.org/01903/v1.3.2#' EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'Id', 'QualifyingProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_43' EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'Target', '#Signature_2a8df7f8-b958-40cc-83f6-edb53b837347_19' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties', 1, 'Id', 'SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties|xades:SignedSignatureProperties', 1, 'Id', 'SignedSignatureProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_0a' -- Chilkat will replace the strings "TO BE GENERATED BY CHILKAT" with actual values when the signature is created. EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime', 'TO BE GENERATED BY CHILKAT' -- Note: It may be that http://www.w3.org/2001/04/xmlenc#sha256 is needed in the following line instead of http://www.w3.org/2000/09/xmldsig#sha1 EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod', 1, 'Algorithm', 'http://www.w3.org/2000/09/xmldsig#sha1' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue', 'TO BE GENERATED BY CHILKAT' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName', 'TO BE GENERATED BY CHILKAT' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber', 'TO BE GENERATED BY CHILKAT' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties|xades:SignedDataObjectProperties', 1, 'Id', 'SignedDataObjectProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4b' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat', 1, 'ObjectReference', '#Reference1_2a8df7f8-b958-40cc-83f6-edb53b837347_27' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description', 'MIME-Version: 1.0' + CHAR(13) + CHAR(10) + 'Content-Type: application/zip' + CHAR(13) + CHAR(10) + 'Content-Transfer-Encoding: binary' + CHAR(13) + CHAR(10) + 'Content-Disposition: filename="PIT-11Z.zip"' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier', 1, 'Qualifier', 'OIDAsURI' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier', 'http://www.certum.pl/OIDAsURI/signedFile/1.2.616.1.113527.3.1.1.3.1' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Description', 'Opis formatu dokumentu oraz jego pelna nazwa' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:DocumentationReferences|xades:DocumentationReference', 'http://www.certum.pl/OIDAsURI/signedFile.pdf' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType', 'application/zip' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:CommitmentTypeId|xades:Identifier', 'http://uri.etsi.org/01903/v1.2.2#ProofOfApproval' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:AllSignedDataObjects', '' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:UnsignedProperties', 1, 'Id', 'UnsignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_55' -- Emit XML in compact (single-line) format to avoid whitespace problems. EXEC sp_OASetProperty @object1, 'EmitCompact', 1 EXEC sp_OAMethod @object1, 'GetXml', @sTmp0 OUT EXEC sp_OAMethod @gen, 'AddObject', @success OUT, '', @sTmp0, '', '' -- Create an Object to be added to the Signature. -- This is where we add the base64 representation of the PIT-11Z.zip EXEC sp_OAMethod @bdZip, 'GetEncoded', @sTmp0 OUT, 'base64' EXEC sp_OAMethod @gen, 'AddObject', @success OUT, 'Object1_2a8df7f8-b958-40cc-83f6-edb53b837347', @sTmp0, '', 'http://www.w3.org/2000/09/xmldsig#base64' -- -------- Reference 1 -------- EXEC sp_OAMethod @gen, 'AddObjectRef', @success OUT, 'Object1_2a8df7f8-b958-40cc-83f6-edb53b837347', 'sha1', 'C14N_WithComments', '', '' EXEC sp_OAMethod @gen, 'SetRefIdAttr', @success OUT, 'Object1_2a8df7f8-b958-40cc-83f6-edb53b837347', 'Reference1_2a8df7f8-b958-40cc-83f6-edb53b837347_27' -- -------- Reference 2 -------- EXEC sp_OAMethod @gen, 'AddObjectRef', @success OUT, 'SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e', 'sha1', '', '', 'http://uri.etsi.org/01903#SignedProperties' EXEC sp_OAMethod @gen, 'SetRefIdAttr', @success OUT, 'SignedProperties_2a8df7f8-b958-40cc-83f6-edb53b837347_4e', 'SignedProperties-Reference_2a8df7f8-b958-40cc-83f6-edb53b837347_28' -- Provide a certificate + private key. (PFX password is test123) DECLARE @cert int -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/pfx/cert_test123.pfx', 'test123' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert RETURN END EXEC sp_OAMethod @gen, 'SetX509Cert', @success OUT, @cert, 1 EXEC sp_OASetProperty @gen, 'KeyInfoType', 'X509Data' EXEC sp_OASetProperty @gen, 'X509Type', 'Certificate' -- This will be an enveloping signature where the Signature element -- is the XML document root, the signed data is contained within Object -- tag(s) within the Signature. -- Therefore, pass an empty sbXml to CreateXmlDsigSb. DECLARE @sbXml int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXml OUT -- The Polish government's XmlDSig implementation requires that we reproduce an attribute-sorting error. -- (This is an error in the XML canonicalization that is not noticed when both the signature-creation code and signature-verification code use -- the same XML canonicalization implementation w/ the bug.) EXEC sp_OASetProperty @gen, 'Behaviors', 'AttributeSortingBug,CompactSignedXml' -- Sign the XML... EXEC sp_OAMethod @gen, 'CreateXmlDSigSb', @success OUT, @sbXml IF @success <> 1 BEGIN EXEC sp_OAGetProperty @gen, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml RETURN END -- ----------------------------------------------- -- Save the signed XML to a file. EXEC sp_OAMethod @sbXml, 'WriteFile', @success OUT, 'qa_output/signedXml.xml', 'utf-8', 0 EXEC sp_OAMethod @sbXml, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 -- ---------------------------------------- -- Verify the signature we just produced... DECLARE @verifier int -- Use "Chilkat_9_5_0.XmlDSig" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.XmlDSig', @verifier OUT EXEC sp_OAMethod @verifier, 'LoadSignatureSb', @success OUT, @sbXml IF @success <> 1 BEGIN EXEC sp_OAGetProperty @verifier, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier RETURN END DECLARE @verified int EXEC sp_OAMethod @verifier, 'VerifySignature', @verified OUT, 1 IF @verified <> 1 BEGIN EXEC sp_OAGetProperty @verifier, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier RETURN END PRINT 'This signature was successfully verified.' -- ------------------------------------ -- Finally, let's extract the .zip from the signed XML, and then unzip the original PIT-11Z.xml from the in-memory zip. DECLARE @xml int -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT EXEC sp_OAMethod @xml, 'LoadSb', @success OUT, @sbXml, 1 -- The base64 image of the PIT-11Z.zip is in the 2nd ds:Object child of the ds:Signature (the ds:Signature is the root element of the signed XML). -- (ds:Object[0] would be the 1st ds:Object child. Index 1 is the 2nd ds:Object child.) DECLARE @strZipBase64 nvarchar(4000) EXEC sp_OAMethod @xml, 'GetChildContent', @strZipBase64 OUT, 'ds:Object[1]' EXEC sp_OAMethod @bdZip, 'Clear', @success OUT EXEC sp_OAMethod @bdZip, 'AppendEncoded', @success OUT, @strZipBase64, 'base64' EXEC sp_OAGetProperty @bdZip, 'NumBytes', @iTmp0 OUT IF @iTmp0 = 0 BEGIN PRINT 'Something went wrong.. we dont'' have any data..' EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier EXEC @hr = sp_OADestroy @xml RETURN END EXEC sp_OAMethod @zip, 'OpenBd', @success OUT, @bdZip IF @success <> 1 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier EXEC @hr = sp_OADestroy @xml RETURN END -- Get the 1st file in the zip, which should be the PIT-11Z.xml EXEC sp_OAMethod @zip, 'GetEntryByIndex', @entry OUT, 0 EXEC sp_OAGetProperty @zip, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN PRINT 'Zip contains no files...' EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier EXEC @hr = sp_OADestroy @xml RETURN END -- Get the XML: DECLARE @origXml nvarchar(4000) EXEC sp_OAMethod @entry, 'UnzipToString', @origXml OUT, 0, 'utf-8' PRINT 'Original XML extracted from base64 zip:' PRINT @origXml EXEC @hr = sp_OADestroy @entry EXEC @hr = sp_OADestroy @sbXmlToZip EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @bdZip EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier EXEC @hr = sp_OADestroy @xml END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.