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) Create XAdES for Malaysia E-InvoiceSee more Malaysia MyInvois ExamplesThis example signs XML to create the required XAdES for Malaysia E-Invoice as described at the following web pages:
https://sdk.myinvois.hasil.gov.my/signature/
Note: This example requires Chilkat 10.0.0 or later.
-- 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 = 1 -- Load XML such as the following to be signed: -- <?xml version="1.0" encoding="utf-8"?> -- <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"> -- <ext:UBLExtensions> -- <ext:UBLExtension> -- <ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI> -- <ext:ExtensionContent> -- <sig:UBLDocumentSignatures xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"> -- <sac:SignatureInformation> -- <cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID> -- <sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoice</sbc:ReferencedSignatureID> -- </sac:SignatureInformation> -- </sig:UBLDocumentSignatures> -- </ext:ExtensionContent> -- </ext:UBLExtension> -- </ext:UBLExtensions> -- <cbc:ID>IV00000105</cbc:ID> -- <cbc:IssueDate>2024-07-06</cbc:IssueDate> -- <cbc:IssueTime>15:06:00Z</cbc:IssueTime> -- <cbc:InvoiceTypeCode listVersionID="1.1">1</cbc:InvoiceTypeCode> -- <cbc:DocumentCurrencyCode>MYR</cbc:DocumentCurrencyCode> -- <cac:BillingReference> -- <cac:AdditionalDocumentReference> -- <cbc:ID>IV00000105</cbc:ID> -- </cac:AdditionalDocumentReference> -- </cac:BillingReference> -- <cac:Signature> -- <cbc:ID>urn:oasis:names:specification:ubl:signature:Invoice</cbc:ID> -- <cbc:SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped:xades</cbc:SignatureMethod> -- </cac:Signature> -- <cac:AccountingSupplierParty> -- <cac:Party> -- <cbc:IndustryClassificationCode name="Other information technology service activities n.e.c.">62099</cbc:IndustryClassificationCode> -- <cac:PartyIdentification> -- <cbc:ID schemeID="TIN">C99999999999</cbc:ID> -- </cac:PartyIdentification> -- <cac:PartyIdentification> -- <cbc:ID schemeID="BRN">200801099999</cbc:ID> -- </cac:PartyIdentification> -- <cac:PostalAddress> -- <cbc:CityName>Cheras</cbc:CityName> -- <cbc:PostalZone>56000</cbc:PostalZone> -- <cbc:CountrySubentityCode>14</cbc:CountrySubentityCode> -- <cac:AddressLine> -- <cbc:Line>A-3, 11, Jalan 2/199a,</cbc:Line> -- </cac:AddressLine> -- <cac:AddressLine> -- <cbc:Line>Cheras, 56000 Cheras</cbc:Line> -- </cac:AddressLine> -- <cac:AddressLine> -- <cbc:Line>Wilayah Persekutuan Kuala Lumpur</cbc:Line> -- </cac:AddressLine> -- <cac:Country> -- <cbc:IdentificationCode listAgencyID="6" listID="ISO3166-1">MYS</cbc:IdentificationCode> -- </cac:Country> -- </cac:PostalAddress> -- <cac:PartyLegalEntity> -- <cbc:RegistrationName>ABC SYSTEMS SERVICES</cbc:RegistrationName> -- </cac:PartyLegalEntity> -- <cac:Contact> -- <cbc:Telephone>019-626 9999</cbc:Telephone> -- <cbc:ElectronicMail>Info@abc.com.my</cbc:ElectronicMail> -- </cac:Contact> -- </cac:Party> -- </cac:AccountingSupplierParty> -- <cac:AccountingCustomerParty> -- <cac:Party> -- <cac:PartyIdentification> -- <cbc:ID schemeID="TIN">C99999999999</cbc:ID> -- </cac:PartyIdentification> -- <cac:PartyIdentification> -- <cbc:ID schemeID="BRN">200801099999</cbc:ID> -- </cac:PartyIdentification> -- <cac:PostalAddress> -- <cbc:CityName>Cheras</cbc:CityName> -- <cbc:PostalZone>56000</cbc:PostalZone> -- <cbc:CountrySubentityCode>14</cbc:CountrySubentityCode> -- <cac:AddressLine> -- <cbc:Line>A-3, 11, Jalan 2/199a,</cbc:Line> -- </cac:AddressLine> -- <cac:AddressLine> -- <cbc:Line>Cheras, 56000 Cheras</cbc:Line> -- </cac:AddressLine> -- <cac:AddressLine> -- <cbc:Line>Cheras, 56000 Cheras</cbc:Line> -- </cac:AddressLine> -- <cac:Country> -- <cbc:IdentificationCode listAgencyID="6" listID="ISO3166-1">MYS</cbc:IdentificationCode> -- </cac:Country> -- </cac:PostalAddress> -- <cac:PartyLegalEntity> -- <cbc:RegistrationName>ABC SYSTEMS SERVICES</cbc:RegistrationName> -- </cac:PartyLegalEntity> -- <cac:Contact> -- <cbc:Telephone>019-626 9999</cbc:Telephone> -- <cbc:ElectronicMail>Info@abc.com.my</cbc:ElectronicMail> -- </cac:Contact> -- </cac:Party> -- </cac:AccountingCustomerParty> -- <cac:TaxTotal> -- <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount> -- <cac:TaxSubtotal> -- <cbc:TaxableAmount currencyID="MYR">40</cbc:TaxableAmount> -- <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount> -- <cac:TaxCategory> -- <cbc:ID>6</cbc:ID> -- <cbc:Percent>0</cbc:Percent> -- <cac:TaxScheme> -- <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">OTH</cbc:ID> -- </cac:TaxScheme> -- </cac:TaxCategory> -- </cac:TaxSubtotal> -- </cac:TaxTotal> -- <cac:LegalMonetaryTotal> -- <cbc:LineExtensionAmount currencyID="MYR">40</cbc:LineExtensionAmount> -- <cbc:TaxExclusiveAmount currencyID="MYR">40</cbc:TaxExclusiveAmount> -- <cbc:TaxInclusiveAmount currencyID="MYR">49</cbc:TaxInclusiveAmount> -- <cbc:AllowanceTotalAmount currencyID="MYR">0</cbc:AllowanceTotalAmount> -- <cbc:PayableAmount currencyID="MYR">49</cbc:PayableAmount> -- </cac:LegalMonetaryTotal> -- <cac:InvoiceLine> -- <cbc:ID>1</cbc:ID> -- <cbc:InvoicedQuantity unitCode="H87">2</cbc:InvoicedQuantity> -- <cbc:LineExtensionAmount currencyID="MYR">40</cbc:LineExtensionAmount> -- <cac:TaxTotal> -- <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount> -- <cac:TaxSubtotal> -- <cbc:TaxableAmount currencyID="MYR">40</cbc:TaxableAmount> -- <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount> -- <cac:TaxCategory> -- <cbc:ID>6</cbc:ID> -- <cbc:Percent>0</cbc:Percent> -- <cac:TaxScheme> -- <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">OTH</cbc:ID> -- </cac:TaxScheme> -- </cac:TaxCategory> -- </cac:TaxSubtotal> -- </cac:TaxTotal> -- <cac:Item> -- <cbc:Description>Computer Monitor 24 inch</cbc:Description> -- <cac:CommodityClassification> -- <cbc:ItemClassificationCode listID="CLASS">3</cbc:ItemClassificationCode> -- </cac:CommodityClassification> -- </cac:Item> -- <cac:Price> -- <cbc:PriceAmount currencyID="MYR">20</cbc:PriceAmount> -- </cac:Price> -- <cac:ItemPriceExtension> -- <cbc:Amount currencyID="MYR">40</cbc:Amount> -- </cac:ItemPriceExtension> -- </cac:InvoiceLine> -- </Invoice> 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_OAMethod @xmlToSign, 'LoadXmlFile', @success OUT, 'qa_data/xml/myinvois_notYetSigned.xml' IF @success = 0 BEGIN PRINT 'Failed to load the XML to be signed.' EXEC @hr = sp_OADestroy @xmlToSign RETURN END DECLARE @gen int -- Use "Chilkat_9_5_0.XmlDSigGen" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.XmlDSigGen', @gen OUT -- ------------------------------------------------------------------------------------------------ -- This behavior is required for the peculiarities of the MyInvois implementation. -- It requires Chilkat 10.0.0 or later. EXEC sp_OASetProperty @gen, 'Behaviors', 'MyInvois' -- ------------------------------------------------------------------------------------------------ EXEC sp_OASetProperty @gen, 'SigLocation', 'Invoice|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation' EXEC sp_OASetProperty @gen, 'SigLocationMod', 0 EXEC sp_OASetProperty @gen, 'SigId', 'signature' EXEC sp_OASetProperty @gen, 'SigNamespacePrefix', 'ds' EXEC sp_OASetProperty @gen, 'SigNamespaceUri', 'http://www.w3.org/2000/09/xmldsig#' EXEC sp_OASetProperty @gen, 'SignedInfoCanonAlg', 'C14N_11' 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:xades', 'http://uri.etsi.org/01903/v1.3.2#' EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'Target', 'signature' EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties', 1, 'Id', 'id-xades-signed-props' EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime', 'TO BE GENERATED BY CHILKAT' 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/2001/04/xmlenc#sha256' 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, 'GetXml', @sTmp0 OUT EXEC sp_OAMethod @gen, 'AddObject', @success OUT, '', @sTmp0, '', '' -- -------- Reference 1 -------- DECLARE @xml1 int -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml1 OUT EXEC sp_OASetProperty @xml1, 'Tag', 'ds:Transforms' EXEC sp_OAMethod @xml1, 'UpdateAttrAt', @success OUT, 'ds:Transform', 1, 'Algorithm', 'http://www.w3.org/TR/1999/REC-xpath-19991116' EXEC sp_OAMethod @xml1, 'UpdateChildContent', NULL, 'ds:Transform|ds:XPath', 'not(//ancestor-or-self::ext:UBLExtensions)' EXEC sp_OAMethod @xml1, 'UpdateAttrAt', @success OUT, 'ds:Transform[1]', 1, 'Algorithm', 'http://www.w3.org/TR/1999/REC-xpath-19991116' EXEC sp_OAMethod @xml1, 'UpdateChildContent', NULL, 'ds:Transform[1]|ds:XPath', 'not(//ancestor-or-self::cac:Signature)' EXEC sp_OAMethod @xml1, 'UpdateAttrAt', @success OUT, 'ds:Transform[2]', 1, 'Algorithm', 'http://www.w3.org/2006/12/xml-c14n11' EXEC sp_OAMethod @gen, 'AddSameDocRef2', @success OUT, '', 'sha256', @xml1, '' EXEC sp_OAMethod @gen, 'SetRefIdAttr', @success OUT, '', 'id-doc-signed-data' -- -------- Reference 2 -------- EXEC sp_OAMethod @gen, 'AddObjectRef', @success OUT, 'id-xades-signed-props', 'sha256', '', '', 'http://www.w3.org/2000/09/xmldsig#SignatureProperties' -- 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 @xml1 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' -- 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_OASetProperty @xmlToSign, 'EmitCompact', 1 EXEC sp_OAMethod @xmlToSign, 'GetXmlSb', @success OUT, @sbXml -- 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 @xml1 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, 'c:/temp/qa_output/signedXml.xml', 'utf-8', 0 EXEC sp_OAMethod @sbXml, 'GetAsString', @sTmp0 OUT PRINT @sTmp0 -- ---------------------------------------- -- Verify the signatures 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 @xml1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier RETURN END -- ---------------------------------------- -- Make sure to indicate that we are verifying a MyInvois signature -- This is because MyInvois does things in a non-standard way.. EXEC sp_OASetProperty @verifier, 'UncommonOptions', 'MyInvois' -- ---------------------------------------- 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 @xml1 EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbXml EXEC @hr = sp_OADestroy @verifier RETURN END SELECT @verifyIdx = @verifyIdx + 1 END PRINT 'All signatures were successfully verified.' EXEC @hr = sp_OADestroy @xmlToSign EXEC @hr = sp_OADestroy @gen EXEC @hr = sp_OADestroy @object1 EXEC @hr = sp_OADestroy @xml1 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.