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) UBL XAdES Enveloped SignatureDemonstrates how to create a UBL XAdES enveloped signature.
-- 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) -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @success int SELECT @success = 1 -- -- The following code creates the XML document to be signed. -- (It is also possible to simply load the XML into the Chilkat XML object by calling the LoadXml method.) -- A sample (already signed) of this XML is available here: External link: credit-note-en16931-xades-signed.xml -- Also, you may use this online tool to generate code from sample XML: -- Generate Code to Create XML DECLARE @xmlToSign int -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlToSign OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OASetProperty @xmlToSign, 'Tag', 'CreditNote' EXEC sp_OAMethod @xmlToSign, 'AddAttribute', @success OUT, 'xmlns', 'urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2' EXEC sp_OAMethod @xmlToSign, 'AddAttribute', @success OUT, 'xmlns:cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' EXEC sp_OAMethod @xmlToSign, 'AddAttribute', @success OUT, 'xmlns:cbc', 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' EXEC sp_OAMethod @xmlToSign, 'AddAttribute', @success OUT, 'xmlns:ext', 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'ext:UBLExtensions|ext:UBLExtension', 1, 'xmlns:sac', 'urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'ext:UBLExtensions|ext:UBLExtension', 1, 'xmlns:sig', 'urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation', '' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cbc:CustomizationID', 'urn:cen.eu:en16931:2017' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cbc:ProfileID', 'P1' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cbc:ID', 'c2ad1540-cf12-4e83-b47c-906aac70242e' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cbc:IssueDate', '2009-12-15' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cbc:Note', 1, 'languageID', 'en' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cbc:Note', 'Ordered in our booth at the convention.' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cbc:DocumentCurrencyCode', 1, 'listAgencyID', '6' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cbc:DocumentCurrencyCode', 1, 'listID', 'ISO 4217 Alpha' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cbc:DocumentCurrencyCode', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cbc:AccountingCost', 'Project cost code 123' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:InvoicePeriod|cbc:StartDate', '2009-11-01' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:InvoicePeriod|cbc:EndDate', '2009-11-30' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:OrderReference|cbc:ID', '123' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:ContractDocumentReference|cbc:ID', 'Contract321' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:ContractDocumentReference|cbc:DocumentType', 'Framework agreement' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AdditionalDocumentReference|cbc:ID', 'Doc1' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AdditionalDocumentReference|cbc:DocumentType', 'Timesheet' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI', 'http://www.suppliersite.eu/sheet001.html' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AdditionalDocumentReference[1]|cbc:ID', 'Doc2' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AdditionalDocumentReference[1]|cbc:DocumentType', 'Drawing' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject', 1, 'mimeCode', 'application/pdf' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject', 'UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID', '9934:18683136487::HR99:276' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName', 'KATANCICEVA' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName', 'ZAGREB' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone', '10000' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode', 'HR' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID', 'HR18683136487' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID', 'FRE' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName', 'MINISTARSTVO FINANCIJA' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name', 'JURAJ MARKOVIC' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail', 'juraj.markovic@fina.hr' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID', '9934:49811265576::HR99:NOVO1' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName', 'GETALDICEVA 4' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName', 'ZAGREB' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone', '10000' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode', 'HR' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID', 'HR49811265576' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID', 'VAT' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName', 'NOVO1' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode', 'HR' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:PaymentMeans|cbc:PaymentMeansCode', '30' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:PaymentMeans|cbc:InstructionNote', 'Neki opis placanja' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:PaymentMeans|cbc:PaymentID', 'HR00 12456' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID', 'HR1210010051863000160' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:PaymentTerms|cbc:Note', 'Neki uvjeti placanja' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:TaxTotal|cbc:TaxAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:TaxTotal|cbc:TaxAmount', '25.00' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount', '100.00' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount', '25.00' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID', 'S' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent', '25' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID', 'VAT' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:LegalMonetaryTotal|cbc:LineExtensionAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:LegalMonetaryTotal|cbc:LineExtensionAmount', '100.00' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount', '100.00' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount', '125.00' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:LegalMonetaryTotal|cbc:PayableAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:LegalMonetaryTotal|cbc:PayableAmount', '125.00' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cbc:ID', '1' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:CreditNoteLine|cbc:CreditedQuantity', 1, 'unitCode', 'H87' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cbc:CreditedQuantity', '1.000' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:CreditNoteLine|cbc:LineExtensionAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cbc:LineExtensionAmount', '100.00' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cbc:Description', 'Neki detaljniji opis proizvoda ide ovdje' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cbc:Name', 'Neki proizvod' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode', 'HR' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID', 'S' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent', '25' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID', 'VAT' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name', 'Boja' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value', 'Plava' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name', 'Masa' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value', '1,2 Kg' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:CreditNoteLine|cac:Price|cbc:PriceAmount', 1, 'currencyID', 'HRK' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Price|cbc:PriceAmount', '100.000000' EXEC sp_OAMethod @xmlToSign, 'UpdateAttrAt', @success OUT, 'cac:CreditNoteLine|cac:Price|cbc:BaseQuantity', 1, 'unitCode', 'H87' EXEC sp_OAMethod @xmlToSign, 'UpdateChildContent', NULL, 'cac:CreditNoteLine|cac:Price|cbc:BaseQuantity', '1.000' -- Use this online tool to generate XAdES code from a sample signed XML: -- Generate Code to Creaet XAdES from Sample XAdES -- The following code was generated by the online tool. 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', 'CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation' EXEC sp_OASetProperty @gen, 'SigLocationMod', 0 EXEC sp_OASetProperty @gen, 'SigId', 'Signature-7f4c4719515a4a0a8ce4d1b983a2ec69' EXEC sp_OASetProperty @gen, 'SigNamespacePrefix', '' EXEC sp_OASetProperty @gen, 'SigNamespaceUri', 'http://www.w3.org/2000/09/xmldsig#' EXEC sp_OASetProperty @gen, 'SignedInfoCanonAlg', 'C14N' EXEC sp_OASetProperty @gen, 'SignedInfoDigestMethod', 'sha256' -- 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:ds', 'http://www.w3.org/2000/09/xmldsig#' EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'xmlns:xades', 'http://uri.etsi.org/01903/v1.3.2#' EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'Target', '#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties', 1, 'Id', 'SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc' 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|xades:DataObjectFormat', 1, 'ObjectReference', '' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description', 'document' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType', 'application/xml' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding', 'UTF-8' EXEC sp_OAMethod @object1, 'GetXml', @sTmp0 OUT EXEC sp_OAMethod @gen, 'AddObject', @success OUT, '', @sTmp0, '', '' -- -------- Reference 1 -------- EXEC sp_OAMethod @gen, 'AddSameDocRef', @success OUT, '', 'sha256', '', '', '' -- -------- Reference 2 -------- EXEC sp_OAMethod @gen, 'AddObjectRef', @success OUT, 'SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc', 'sha256', '', '', 'http://uri.etsi.org/01903#SignedProperties' -- 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 @xmlToSign 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+KeyValue' EXEC sp_OASetProperty @gen, 'X509Type', 'IssuerSerial,SubjectName,SKI,Certificate' -- Load XML to be signed... DECLARE @sbXml int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXml OUT EXEC sp_OAMethod @xmlToSign, 'GetXmlSb', @success OUT, @sbXml -- Adding the "UBLDocumentSignatures" behavior causes the following Transform to be used for the -- first Reference: -- <Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"><XPath>count(ancestor-or-self::sig:UBLDocumentSignatures | here()/ancestor::sig:UBLDocumentSignatures[1]) > count(ancestor-or-self::sig:UBLDocumentSignatures)</XPath></Transform> EXEC sp_OASetProperty @gen, 'Behaviors', 'IndentedSignature,UBLDocumentSignatures' -- 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 @xmlToSign 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 @xmlToSign 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 -- We should have just one signature.. DECLARE @numSigs int EXEC sp_OAGetProperty @verifier, 'NumSignatures', @numSigs OUT DECLARE @verifyIdx int SELECT @verifyIdx = 0 WHILE @verifyIdx < @numSigs BEGIN EXEC sp_OASetProperty @verifier, 'Selector', @verifyIdx 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 @xmlToSign 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 SELECT @verifyIdx = @verifyIdx + 1 END PRINT 'The signature was successfully verified.' EXEC @hr = sp_OADestroy @xmlToSign 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 END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.