DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoGen
Variant vCert
Handle hoCert
Handle hoXml
Variant vSbXml
Handle hoSbXml
Handle hoSbExisting
Integer iNumReplaced
String sTemp1
Move False To iSuccess
// 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>
Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
If (Not(IsComObjectCreated(hoGen))) Begin
Send CreateComObject of hoGen
End
Set ComSigLocation Of hoGen To "EnvioDTE"
Set ComSigLocationMod Of hoGen To 0
Set ComSigNamespacePrefix Of hoGen To ""
Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#"
Set ComSignedInfoCanonAlg Of hoGen To "C14N"
Set ComSignedInfoDigestMethod Of hoGen To "sha1"
// -------- Reference 1 --------
Get ComAddSameDocRef Of hoGen "NDTECHILE" "sha1" "" "" "" To iSuccess
// Provide a certificate + private key. (PFX password is test123)
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoCert to vCert
Get ComSetX509Cert Of hoGen vCert True To iSuccess
Set ComKeyInfoType Of hoGen To "X509Data+KeyValue"
Set ComX509Type Of hoGen To "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>
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Set ComTag Of hoXml To "EnvioDTE"
Get ComAddAttribute Of hoXml "version" "1.0" To iSuccess
Get ComAddAttribute Of hoXml "xmlns" "http://www.sii.cl/SiiDte" To iSuccess
Get ComAddAttribute Of hoXml "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance" To iSuccess
Get ComAddAttribute Of hoXml "xsi:schemaLocation" "http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" To iSuccess
Get ComUpdateAttrAt Of hoXml "SetDTE" True "ID" "NDTECHILE" To iSuccess
Get ComUpdateAttrAt Of hoXml "SetDTE|Caratula" True "version" "1.0" To iSuccess
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|RutEmisor" "..."
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|RutEnvia" "..."
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|RutReceptor" "..."
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|FchResol" "2014-08-22"
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|NroResol" "80"
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|TmstFirmaEnv" "2020-07-17T13:19:10"
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|SubTotDTE|TpoDTE" "34"
Send ComUpdateChildContent To hoXml "SetDTE|Caratula|SubTotDTE|NroDTE" "1"
Send ComUpdateChildContent To hoXml "SetDTE|EXISTING_SIGNED_XML" "HERE"
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
If (Not(IsComObjectCreated(hoSbXml))) Begin
Send CreateComObject of hoSbXml
End
Get pvComObject of hoSbXml to vSbXml
Get ComGetXmlSb Of hoXml vSbXml To iSuccess
// 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.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbExisting
If (Not(IsComObjectCreated(hoSbExisting))) Begin
Send CreateComObject of hoSbExisting
End
Get ComLoadFile Of hoSbExisting "qa_data/xml_dsig/sii_cl/signed1.xml" "iso-8859-1" To iSuccess
// 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).
Get ComReplace Of hoSbExisting '<?xml version="1.0" encoding="iso-8859-1"?>' "" To iNumReplaced
Get ComTrim Of hoSbExisting To iSuccess
// Insert the existing signed XML into the new XML document to be signed.
Get ComGetAsString Of hoSbExisting To sTemp1
Get ComReplace Of hoSbXml "<EXISTING_SIGNED_XML>HERE</EXISTING_SIGNED_XML>" sTemp1 To iNumReplaced
// 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.
Set ComBehaviors Of hoGen To "IndentedSignature,SignExistingSignatures"
// Sign the XML...
Get pvComObject of hoSbXml to vSbXml
Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoGen To sTemp1
Showln sTemp1
Procedure_Return
End
// -----------------------------------------------
// Change the XML declarator to indicate iso-8859-1 (because we'll be saving using iso-8859-1)
Get ComReplace Of hoSbXml 'encoding="utf-8"?>' 'encoding="iso-8859-1"?>' To iNumReplaced
// Save the signed XML to a file.
Get ComWriteFile Of hoSbXml "qa_data/xml_dsig/sii_cl/signed2.xml" "iso-8859-1" False To iSuccess
Get ComGetAsString Of hoSbXml To sTemp1
Showln sTemp1
End_Procedure