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
(C) 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.
#include <C_CkXmlDSigGen.h> #include <C_CkCert.h> #include <C_CkXml.h> #include <C_CkStringBuilder.h> void ChilkatSample(void) { HCkXmlDSigGen gen; HCkCert cert; BOOL success; HCkXml xml; HCkStringBuilder sbXml; HCkStringBuilder sbExisting; int 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> gen = CkXmlDSigGen_Create(); CkXmlDSigGen_putSigLocation(gen,"EnvioDTE"); CkXmlDSigGen_putSigLocationMod(gen,0); CkXmlDSigGen_putSigNamespacePrefix(gen,""); CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#"); CkXmlDSigGen_putSignedInfoCanonAlg(gen,"C14N"); CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha1"); // -------- Reference 1 -------- CkXmlDSigGen_AddSameDocRef(gen,"NDTECHILE","sha1","","",""); // Provide a certificate + private key. (PFX password is test123) cert = CkCert_Create(); success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123"); if (success != TRUE) { printf("%s\n",CkCert_lastErrorText(cert)); CkXmlDSigGen_Dispose(gen); CkCert_Dispose(cert); return; } CkXmlDSigGen_SetX509Cert(gen,cert,TRUE); CkXmlDSigGen_putKeyInfoType(gen,"X509Data+KeyValue"); CkXmlDSigGen_putX509Type(gen,"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> xml = CkXml_Create(); CkXml_putTag(xml,"EnvioDTE"); CkXml_AddAttribute(xml,"version","1.0"); CkXml_AddAttribute(xml,"xmlns","http://www.sii.cl/SiiDte"); CkXml_AddAttribute(xml,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); CkXml_AddAttribute(xml,"xsi:schemaLocation","http://www.sii.cl/SiiDte EnvioDTE_v10.xsd"); CkXml_UpdateAttrAt(xml,"SetDTE",TRUE,"ID","NDTECHILE"); CkXml_UpdateAttrAt(xml,"SetDTE|Caratula",TRUE,"version","1.0"); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|RutEmisor","..."); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|RutEnvia","..."); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|RutReceptor","..."); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|FchResol","2014-08-22"); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|NroResol","80"); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|TmstFirmaEnv","2020-07-17T13:19:10"); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|SubTotDTE|TpoDTE","34"); CkXml_UpdateChildContent(xml,"SetDTE|Caratula|SubTotDTE|NroDTE","1"); CkXml_UpdateChildContent(xml,"SetDTE|EXISTING_SIGNED_XML","HERE"); sbXml = CkStringBuilder_Create(); CkXml_GetXmlSb(xml,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. sbExisting = CkStringBuilder_Create(); CkStringBuilder_LoadFile(sbExisting,"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). numReplaced = CkStringBuilder_Replace(sbExisting,"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>",""); CkStringBuilder_Trim(sbExisting); // Insert the existing signed XML into the new XML document to be signed. numReplaced = CkStringBuilder_Replace(sbXml,"<EXISTING_SIGNED_XML>HERE</EXISTING_SIGNED_XML>",CkStringBuilder_getAsString(sbExisting)); // 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. CkXmlDSigGen_putBehaviors(gen,"IndentedSignature,SignExistingSignatures"); // Sign the XML... success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { printf("%s\n",CkXmlDSigGen_lastErrorText(gen)); CkXmlDSigGen_Dispose(gen); CkCert_Dispose(cert); CkXml_Dispose(xml); CkStringBuilder_Dispose(sbXml); CkStringBuilder_Dispose(sbExisting); return; } // ----------------------------------------------- // Change the XML declarator to indicate iso-8859-1 (because we'll be saving using iso-8859-1) numReplaced = CkStringBuilder_Replace(sbXml,"encoding=\"utf-8\"?>","encoding=\"iso-8859-1\"?>"); // Save the signed XML to a file. success = CkStringBuilder_WriteFile(sbXml,"qa_data/xml_dsig/sii_cl/signed2.xml","iso-8859-1",FALSE); printf("%s\n",CkStringBuilder_getAsString(sbXml)); CkXmlDSigGen_Dispose(gen); CkCert_Dispose(cert); CkXml_Dispose(xml); CkStringBuilder_Dispose(sbXml); CkStringBuilder_Dispose(sbExisting); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.