Remover nodo KeyValue en XML para cancelación

This commit is contained in:
El Mau 2021-11-25 10:22:23 -06:00
parent 425cd53df1
commit 264090f2a6
3 changed files with 38 additions and 37 deletions

View File

@ -7,6 +7,7 @@ import getpass
import subprocess import subprocess
from pathlib import Path from pathlib import Path
import lxml.etree as ET
import xmlsec import xmlsec
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives import serialization
@ -76,8 +77,8 @@ class SATCertificate(object):
self._rfc = obj.subject.get_attributes_for_oid( self._rfc = obj.subject.get_attributes_for_oid(
NameOID.X500_UNIQUE_IDENTIFIER)[0].value.split(' ')[0] NameOID.X500_UNIQUE_IDENTIFIER)[0].value.split(' ')[0]
self._serial_number2 = str(obj.serial_number) self._serial_number2 = '{0:x}'.format(obj.serial_number)
self._serial_number = '{0:x}'.format(obj.serial_number)[1::2] self._serial_number = self._serial_number2[1::2]
self._not_before = obj.not_valid_before self._not_before = obj.not_valid_before
self._not_after = obj.not_valid_after self._not_after = obj.not_valid_after
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
@ -153,25 +154,24 @@ class SATCertificate(object):
def sign_xml(self, tree): def sign_xml(self, tree):
node = xmlsec.tree.find_node(tree, xmlsec.constants.NodeSignature) node = xmlsec.tree.find_node(tree, xmlsec.constants.NodeSignature)
ctx = xmlsec.SignatureContext() ctx = xmlsec.SignatureContext()
key = xmlsec.Key.from_memory(self.key_pem, xmlsec.constants.KeyDataFormatPem) key = xmlsec.Key.from_memory(
self.key_pem, xmlsec.constants.KeyDataFormatPem)
ctx.key = key ctx.key = key
ctx.sign(node) ctx.sign(node)
node = xmlsec.tree.find_node(tree, 'X509Certificate') node = xmlsec.tree.find_node(tree, 'X509Certificate')
node.text = self.cer_txt node.text = self.cer_txt
node = xmlsec.tree.find_node(tree, 'X509IssuerName') node = xmlsec.tree.find_node(tree, 'X509IssuerName')
node.text = self.issuer node.text = self.issuer
node = xmlsec.tree.find_node(tree, 'X509SerialNumber') node = xmlsec.tree.find_node(tree, 'X509SerialNumber')
node.text = self.serial_number2 node.text = self.serial_number2
node = xmlsec.tree.find_node(tree, 'SignatureValue') node = xmlsec.tree.find_node(tree, 'SignatureValue')
node.text = node.text.replace('\n', '') node.text = node.text.replace('\n', '')
# ~ node = xmlsec.tree.find_node(tree, 'Modulus') xml_signed = ET.tostring(
# ~ node.text = node.text.replace('\n', '') tree, encoding='utf-8', xml_declaration=True).decode()
return tree return xml_signed
@property @property
def rfc(self): def rfc(self):

View File

@ -694,8 +694,7 @@ def cancel_xml_sign(invoice, auth, certificado):
} }
template = TEMPLATE_CANCEL.format(**data) template = TEMPLATE_CANCEL.format(**data)
tree = ET.fromstring(template.encode()) tree = ET.fromstring(template.encode())
tree = cert.sign_xml(tree) sign_xml = cert.sign_xml(tree)
sign_xml = ET.tostring(tree).decode()
result = pac.cancel_xml(sign_xml, auth, invoice.xml) result = pac.cancel_xml(sign_xml, auth, invoice.xml)
if pac.error: if pac.error:

View File

@ -278,32 +278,34 @@ DEFAULT_GLOBAL = {
# ~ </Signature> # ~ </Signature>
# ~ </Cancelacion> # ~ </Cancelacion>
# ~ """ # ~ """
TEMPLATE_CANCEL = """<Cancelacion RfcEmisor="{rfc}" Fecha="{fecha}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://cancelacfd.sat.gob.mx">
<Folios> TEMPLATE_CANCEL = """<Cancelacion xmlns="http://cancelacfd.sat.gob.mx" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Fecha="{fecha}" RfcEmisor="{rfc}">
<UUID>{uuid}</UUID> <Folios>
</Folios> <UUID>{uuid}</UUID>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> </Folios>
<SignedInfo> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> <SignedInfo>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<Reference URI=""> <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Transforms> <Reference URI="">
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> <Transforms>
</Transforms> <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> </Transforms>
<DigestValue /> <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
</Reference> <DigestValue/>
</SignedInfo> </Reference>
<SignatureValue /> </SignedInfo>
<KeyInfo> <SignatureValue/>
<X509Data> <KeyInfo>
<X509IssuerSerial> <X509Data>
<X509IssuerName /> <X509IssuerSerial>
<X509SerialNumber /> <X509IssuerName/>
</X509IssuerSerial> <X509SerialNumber/>
<X509Certificate /> </X509IssuerSerial>
</X509Data> <X509Certificate/>
</KeyInfo> </X509Data>
</Signature> </KeyInfo>
</Signature>
</Cancelacion> </Cancelacion>
""" """