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
(PowerBuilder) Apply 2nd Signature to sii.cl Factura Electrónica (Chile Servicio de Impuestos Internos)Demonstrates how to apply the 2nd signature an XML invoice according to Chilean Internal Revenue Service regulations.
integer li_rc oleobject loo_Gen oleobject loo_Cert integer li_Success oleobject loo_Xml oleobject loo_SbXml oleobject loo_SbExisting integer li_NumReplaced // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // In this example: Create sii.cl Factura Electr�nica (Chile Servicio de Impuestos Internos) // We signed XML structured like this: // <?xml version="1.0" encoding="iso-8859-1"?> // <DTE version="1.0" xmlns="http://www.sii.cl/SiiDte"> // <Documento ID="F11T22"> // ... // </Documento> // </DTE> // To get a signature like this: // <?xml version="1.0" encoding="iso-8859-1"?> // <DTE version="1.0" xmlns="http://www.sii.cl/SiiDte"> // <Documento ID="F11T22"> // ... // </Documento> // <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> // .. // </Signature> // </DTE> // Now we wish to wrap the result of the 1st example in the following and sign again: // <?xml version="1.0" encoding="iso-8859-1"?> // <EnvioDTE version="1.0" xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd"> // <SetDTE ID="NDTECHILE"> // <Caratula version="1.0"> // <RutEmisor>...</RutEmisor> // <RutEnvia>...</RutEnvia> // <RutReceptor>...</RutReceptor> // <FchResol>2014-08-22</FchResol> // <NroResol>80</NroResol> // <TmstFirmaEnv>2020-07-17T13:19:10</TmstFirmaEnv> // <SubTotDTE> // <TpoDTE>34</TpoDTE> // <NroDTE>1</NroDTE> // </SubTotDTE> // </Caratula> // <DTE version="1.0" xmlns="http://www.sii.cl/SiiDte"> // <Documento ID="F11T22">...</Documento> // <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">..</Signature> // </DTE> // </SetDTE> // </EnvioDTE> // The result will be this: // <?xml version="1.0" encoding="iso-8859-1"?> // <EnvioDTE version="1.0" xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd"> // <SetDTE ID="NDTECHILE"> // <Caratula version="1.0"> // <RutEmisor>...</RutEmisor> // <RutEnvia>...</RutEnvia> // <RutReceptor>...</RutReceptor> // <FchResol>2014-08-22</FchResol> // <NroResol>80</NroResol> // <TmstFirmaEnv>2020-07-17T13:19:10</TmstFirmaEnv> // <SubTotDTE> // <TpoDTE>34</TpoDTE> // <NroDTE>1</NroDTE> // </SubTotDTE> // </Caratula> // <DTE version="1.0" xmlns="http://www.sii.cl/SiiDte"> // <Documento ID="F11T22">...</Documento> // <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">..</Signature> // </DTE> // </SetDTE> // <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">..</Signature> // </EnvioDTE> loo_Gen = create oleobject // Use "Chilkat_9_5_0.XmlDSigGen" for versions of Chilkat < 10.0.0 li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen") if li_rc < 0 then destroy loo_Gen MessageBox("Error","Connecting to COM object failed") return end if loo_Gen.SigLocation = "EnvioDTE" loo_Gen.SigLocationMod = 0 loo_Gen.SigNamespacePrefix = "" loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" loo_Gen.SignedInfoCanonAlg = "C14N" loo_Gen.SignedInfoDigestMethod = "sha1" // -------- Reference 1 -------- loo_Gen.AddSameDocRef("NDTECHILE","sha1","","","") // Provide a certificate + private key. (PFX password is test123) loo_Cert = create oleobject // Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert") li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") if li_Success <> 1 then Write-Debug loo_Cert.LastErrorText destroy loo_Gen destroy loo_Cert return end if loo_Gen.SetX509Cert(loo_Cert,1) loo_Gen.KeyInfoType = "X509Data+KeyValue" loo_Gen.X509Type = "Certificate" // Here's the part where we must be very careful not to disturb the existing signature. // We want to encapsulate the existing signed XML like this: // <?xml version="1.0" encoding="iso-8859-1"?> // <EnvioDTE version="1.0" xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd"> // <SetDTE ID="NDTECHILE"> // <Caratula version="1.0"> // <RutEmisor>...</RutEmisor> // <RutEnvia>...</RutEnvia> // <RutReceptor>...</RutReceptor> // <FchResol>2014-08-22</FchResol> // <NroResol>80</NroResol> // <TmstFirmaEnv>2020-07-17T13:19:10</TmstFirmaEnv> // <SubTotDTE> // <TpoDTE>34</TpoDTE> // <NroDTE>1</NroDTE> // </SubTotDTE> // </Caratula> // <EXISTING_SIGNED_XML>HERE</EXISTING_SIGNED_XML> // </SetDTE> // <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">..</Signature> // </EnvioDTE> loo_Xml = create oleobject // Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml") loo_Xml.Tag = "EnvioDTE" loo_Xml.AddAttribute("version","1.0") loo_Xml.AddAttribute("xmlns","http://www.sii.cl/SiiDte") loo_Xml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance") loo_Xml.AddAttribute("xsi:schemaLocation","http://www.sii.cl/SiiDte EnvioDTE_v10.xsd") loo_Xml.UpdateAttrAt("SetDTE",1,"ID","NDTECHILE") loo_Xml.UpdateAttrAt("SetDTE|Caratula",1,"version","1.0") loo_Xml.UpdateChildContent("SetDTE|Caratula|RutEmisor","...") loo_Xml.UpdateChildContent("SetDTE|Caratula|RutEnvia","...") loo_Xml.UpdateChildContent("SetDTE|Caratula|RutReceptor","...") loo_Xml.UpdateChildContent("SetDTE|Caratula|FchResol","2014-08-22") loo_Xml.UpdateChildContent("SetDTE|Caratula|NroResol","80") loo_Xml.UpdateChildContent("SetDTE|Caratula|TmstFirmaEnv","2020-07-17T13:19:10") loo_Xml.UpdateChildContent("SetDTE|Caratula|SubTotDTE|TpoDTE","34") loo_Xml.UpdateChildContent("SetDTE|Caratula|SubTotDTE|NroDTE","1") loo_Xml.UpdateChildContent("SetDTE|EXISTING_SIGNED_XML","HERE") loo_SbXml = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder") loo_Xml.GetXmlSb(loo_SbXml) // Load our existing signed XML. // We cannot load it into a Chilkat XML object because we cannot allow anything to change, // not even indentation or whitespace. loo_SbExisting = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbExisting.ConnectToNewObject("Chilkat.StringBuilder") loo_SbExisting.LoadFile("qa_data/xml_dsig/sii_cl/signed1.xml","iso-8859-1") // However, we must remove the XML declarator, and we can trim the whitespace at the very front and back (i.e. whitespace completely outside the document). li_NumReplaced = loo_SbExisting.Replace("<?xml version=~"1.0~" encoding=~"iso-8859-1~"?>","") loo_SbExisting.Trim() // Insert the existing signed XML into the new XML document to be signed. li_NumReplaced = loo_SbXml.Replace("<EXISTING_SIGNED_XML>HERE</EXISTING_SIGNED_XML>",loo_SbExisting.GetAsString()) // We now have the XML document ready for the 2nd signature to be applied... // Make sure we add the "SignExistingSignatures" behavior so that this signature includes the original signature in the digest. loo_Gen.Behaviors = "IndentedSignature,SignExistingSignatures" // Sign the XML... li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml) if li_Success <> 1 then Write-Debug loo_Gen.LastErrorText destroy loo_Gen destroy loo_Cert destroy loo_Xml destroy loo_SbXml destroy loo_SbExisting return end if // ----------------------------------------------- // Change the XML declarator to indicate iso-8859-1 (because we'll be saving using iso-8859-1) li_NumReplaced = loo_SbXml.Replace("encoding=~"utf-8~"?>","encoding=~"iso-8859-1~"?>") // Save the signed XML to a file. li_Success = loo_SbXml.WriteFile("qa_data/xml_dsig/sii_cl/signed2.xml","iso-8859-1",0) Write-Debug loo_SbXml.GetAsString() destroy loo_Gen destroy loo_Cert destroy loo_Xml destroy loo_SbXml destroy loo_SbExisting |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.