IMPORT os IMPORT util IMPORT XML FUNCTION formaracuse(rnc_c,rnc_e,e_ncf) DEFINE chFechaFirma,rnc_e,e_ncf,fileacuse,rnc_c STRING DEFINE out om.SaxDocumentHandler, i INT DEFINE a om.SaxAttributes LET fileacuse = "./recibidos/recepcion/acuses/",rnc_c CLIPPED,e_ncf CLIPPED,".xml" LET out=om.XmlWriter.createFileWriter(fileacuse) LET a=om.SaxAttributes.create() CALL out.startDocument() CALL a.addAttribute("xmlns:xs","http://www.w3.org/2001/XMLSchema") CALL out.startElement("ARECF",a) CALL a.clear() CALL out.startElement("DetalleAcusedeRecibo",a) CALL out.startElement("Version",a) CALL out.characters("1.0") CALL out.endElement("Version") CALL out.startElement("RNCEmisor",a) CALL out.characters(rnc_e) CALL out.endElement("RNCEmisor") CALL out.startElement("RNCComprador",a) CALL out.characters(rnc_c) CALL out.endElement("RNCComprador") CALL out.startElement("eNCF",a) CALL out.characters(e_ncf) CALL out.endElement("eNCF") CALL out.startElement("Estado",a) CALL out.characters("0") CALL out.endElement("Estado") LET chFechaFirma = util.Datetime.format(CURRENT, "%d-%m-%Y %H:%M:%S" ) DISPLAY "FECHA ",chFechaFirma CALL out.startElement("FechaHoraAcuseRecibo",a) CALL out.characters(chFechaFirma) CALL out.endElement("FechaHoraAcuseRecibo") CALL out.endElement("DetalleAcusedeRecibo") CALL out.endElement("ARECF") CALL out.endDocument() RETURN fileacuse END FUNCTION FUNCTION firmadocumento(fileafirmar) DEFINE doc xml.DomDocument DEFINE doc2 xml.DomDocument DEFINE root xml.DomNode DEFINE node xml.DomNode DEFINE signNode xml.DomNode DEFINE sig xml.Signature DEFINE key xml.CryptoKey DEFINE cert xml.CryptoX509 DEFINE index INTEGER, fileafirmar,filexml STRING, codsecure VARCHAR(6) # Create DomDocument object LET doc = xml.DomDocument.Create() # Notice that whitespaces are significant in cryptography, # therefore it is recommended to remove unnecessary ones CALL doc.setFeature("whitespace-in-element-content",FALSE) #DISPLAY "file ",fileafirmar LET filexml = fileafirmar CALL doc.load(fileafirmar) TRY # Create rsa key LET key = xml.CryptoKey.Create("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256") CALL key.loadPEM("onlyRSA.pem") # Create an X509 certificate with the constructor of the CryptoX509 class. LET cert = xml.CryptoX509.Create() CALL cert.setFeature("X509Certificate",TRUE) # Load the X509 certificate associated to the RSA or DSA private key into the CryptoKey object. # CALL cert.loadPEM("cert509.crt") CALL cert.loadPEM("onlyCert.pem") # Create signature object with the key to use LET sig = xml.Signature.Create() # Assign the CryptoKey object to the Signature object. CALL sig.setKey(KEY) # Assign the CryptoX509 object to the Signature object. CALL sig.setCertificate(cert) # Set XML node to be signed. In our case, the entire document # LET index = sig.createReference("", # "http://www.w3.org/2001/04/xmlenc#sha256") LET index = sig.createReference('""', "http://www.w3.org/2001/04/xmlenc#sha256") # Add enveloped method to not take the XML signature node into account # when computing the entire document. CALL sig.appendReferenceTransformation(index, "http://www.w3.org/2000/09/xmldsig#enveloped-signature",doc.getDocumentElement()) # Set canonicalization method on the XML fragment to be signed. # Compute enveloped signature CALL sig.compute(doc) # Retrieve signature document LET doc2=sig.getDocument() # Append the signature node to the original document to get # a valid enveloped signature # Notice that the enveloped signature can be added anywhere in the # original document LET signNode = doc2.getDocumentElement() # Get Signature node # Import it into the original document LET node = doc.importNode(signNode,true) # Retrieve the original document root node LET root = doc.getDocumentElement() # Append the signature node as last child of the original document CALL root.appendChild(node) # Save document with enveloped signature back to disk CALL doc.setFeature("format-pretty-print",TRUE) LET fileafirmar = "./recibidos/recepcion/firmados/",os.Path.baseName(fileafirmar) CALL doc.save(fileafirmar) RETURN fileafirmar CATCH DISPLAY "Unable to create an enveloped signature :",status," ",sqlca.sqlerrm RETURN "nofile" END TRY END FUNCTION