|  | 
Chilkat  HOME  Android™  AutoIt  C  C#  C++  Chilkat2-Python  CkPython  Classic ASP  DataFlex  Delphi DLL  Go  Java  Node.js  Objective-C  PHP Extension  Perl  PowerBuilder  PowerShell  PureBasic  Ruby  SQL Server  Swift  Tcl  Unicode C  Unicode C++  VB.NET  VBScript  Visual Basic 6.0  Visual FoxPro  Xojo Plugin
| (Unicode 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_CkXmlDSigGenW.h> #include <C_CkCertW.h> #include <C_CkXmlW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkXmlDSigGenW gen; HCkCertW cert; BOOL success; HCkXmlW xml; HCkStringBuilderW sbXml; HCkStringBuilderW 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 = CkXmlDSigGenW_Create(); CkXmlDSigGenW_putSigLocation(gen,L"EnvioDTE"); CkXmlDSigGenW_putSigLocationMod(gen,0); CkXmlDSigGenW_putSigNamespacePrefix(gen,L""); CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#"); CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"C14N"); CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha1"); // -------- Reference 1 -------- CkXmlDSigGenW_AddSameDocRef(gen,L"NDTECHILE",L"sha1",L"",L"",L""); // Provide a certificate + private key. (PFX password is test123) cert = CkCertW_Create(); success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); return; } CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE); CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data+KeyValue"); CkXmlDSigGenW_putX509Type(gen,L"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 = CkXmlW_Create(); CkXmlW_putTag(xml,L"EnvioDTE"); CkXmlW_AddAttribute(xml,L"version",L"1.0"); CkXmlW_AddAttribute(xml,L"xmlns",L"http://www.sii.cl/SiiDte"); CkXmlW_AddAttribute(xml,L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance"); CkXmlW_AddAttribute(xml,L"xsi:schemaLocation",L"http://www.sii.cl/SiiDte EnvioDTE_v10.xsd"); CkXmlW_UpdateAttrAt(xml,L"SetDTE",TRUE,L"ID",L"NDTECHILE"); CkXmlW_UpdateAttrAt(xml,L"SetDTE|Caratula",TRUE,L"version",L"1.0"); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|RutEmisor",L"..."); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|RutEnvia",L"..."); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|RutReceptor",L"..."); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|FchResol",L"2014-08-22"); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|NroResol",L"80"); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|TmstFirmaEnv",L"2020-07-17T13:19:10"); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|SubTotDTE|TpoDTE",L"34"); CkXmlW_UpdateChildContent(xml,L"SetDTE|Caratula|SubTotDTE|NroDTE",L"1"); CkXmlW_UpdateChildContent(xml,L"SetDTE|EXISTING_SIGNED_XML",L"HERE"); sbXml = CkStringBuilderW_Create(); CkXmlW_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 = CkStringBuilderW_Create(); CkStringBuilderW_LoadFile(sbExisting,L"qa_data/xml_dsig/sii_cl/signed1.xml",L"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 = CkStringBuilderW_Replace(sbExisting,L"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>",L""); CkStringBuilderW_Trim(sbExisting); // Insert the existing signed XML into the new XML document to be signed. numReplaced = CkStringBuilderW_Replace(sbXml,L"<EXISTING_SIGNED_XML>HERE</EXISTING_SIGNED_XML>",CkStringBuilderW_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. CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature,SignExistingSignatures"); // Sign the XML... success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen)); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xml); CkStringBuilderW_Dispose(sbXml); CkStringBuilderW_Dispose(sbExisting); return; } // ----------------------------------------------- // Change the XML declarator to indicate iso-8859-1 (because we'll be saving using iso-8859-1) numReplaced = CkStringBuilderW_Replace(sbXml,L"encoding=\"utf-8\"?>",L"encoding=\"iso-8859-1\"?>"); // Save the signed XML to a file. success = CkStringBuilderW_WriteFile(sbXml,L"qa_data/xml_dsig/sii_cl/signed2.xml",L"iso-8859-1",FALSE); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml)); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xml); CkStringBuilderW_Dispose(sbXml); CkStringBuilderW_Dispose(sbExisting); } | ||||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.