Sample code for 30+ languages & platforms
Lianja

Apply 2nd Signature to sii.cl Factura Electrónica (Chile Servicio de Impuestos Internos)

See more XAdES Examples

Demonstrates how to apply the 2nd signature an XML invoice according to Chilean Internal Revenue Service regulations.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// 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>

loGen = createobject("CkXmlDSigGen")

loGen.SigLocation = "EnvioDTE"
loGen.SigLocationMod = 0
loGen.SigNamespacePrefix = ""
loGen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loGen.SignedInfoCanonAlg = "C14N"
loGen.SignedInfoDigestMethod = "sha1"

// -------- Reference 1 --------
loGen.AddSameDocRef("NDTECHILE","sha1","","","")

// Provide a certificate + private key. (PFX password is test123)
loCert = createobject("CkCert")
llSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if (llSuccess <> .T.) then
    ? loCert.LastErrorText
    release loGen
    release loCert
    return
endif

loGen.SetX509Cert(loCert,.T.)

loGen.KeyInfoType = "X509Data+KeyValue"
loGen.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>

loXml = createobject("CkXml")
loXml.Tag = "EnvioDTE"
loXml.AddAttribute("version","1.0")
loXml.AddAttribute("xmlns","http://www.sii.cl/SiiDte")
loXml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
loXml.AddAttribute("xsi:schemaLocation","http://www.sii.cl/SiiDte EnvioDTE_v10.xsd")
loXml.UpdateAttrAt("SetDTE",.T.,"ID","NDTECHILE")
loXml.UpdateAttrAt("SetDTE|Caratula",.T.,"version","1.0")
loXml.UpdateChildContent("SetDTE|Caratula|RutEmisor","...")
loXml.UpdateChildContent("SetDTE|Caratula|RutEnvia","...")
loXml.UpdateChildContent("SetDTE|Caratula|RutReceptor","...")
loXml.UpdateChildContent("SetDTE|Caratula|FchResol","2014-08-22")
loXml.UpdateChildContent("SetDTE|Caratula|NroResol","80")
loXml.UpdateChildContent("SetDTE|Caratula|TmstFirmaEnv","2020-07-17T13:19:10")
loXml.UpdateChildContent("SetDTE|Caratula|SubTotDTE|TpoDTE","34")
loXml.UpdateChildContent("SetDTE|Caratula|SubTotDTE|NroDTE","1")
loXml.UpdateChildContent("SetDTE|EXISTING_SIGNED_XML","HERE")

loSbXml = createobject("CkStringBuilder")
loXml.GetXmlSb(loSbXml)

// 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.
loSbExisting = createobject("CkStringBuilder")
loSbExisting.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).
lnNumReplaced = loSbExisting.Replace('<?xml version="1.0" encoding="iso-8859-1"?>',"")
loSbExisting.Trim()

// Insert the existing signed XML into the new XML document to be signed.
lnNumReplaced = loSbXml.Replace("<EXISTING_SIGNED_XML>HERE</EXISTING_SIGNED_XML>",loSbExisting.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.
loGen.Behaviors = "IndentedSignature,SignExistingSignatures"

// Sign the XML...
llSuccess = loGen.CreateXmlDSigSb(loSbXml)
if (llSuccess <> .T.) then
    ? loGen.LastErrorText
    release loGen
    release loCert
    release loXml
    release loSbXml
    release loSbExisting
    return
endif

// -----------------------------------------------

// Change the XML declarator to indicate iso-8859-1 (because we'll be saving using iso-8859-1)
lnNumReplaced = loSbXml.Replace('encoding="utf-8"?>','encoding="iso-8859-1"?>')

// Save the signed XML to a file.
llSuccess = loSbXml.WriteFile("qa_data/xml_dsig/sii_cl/signed2.xml","iso-8859-1",.F.)

? loSbXml.GetAsString()


release loGen
release loCert
release loXml
release loSbXml
release loSbExisting