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

View File

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

View File

@ -278,32 +278,34 @@ DEFAULT_GLOBAL = {
# ~ </Signature>
# ~ </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>
<UUID>{uuid}</UUID>
</Folios>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue />
</Reference>
</SignedInfo>
<SignatureValue />
<KeyInfo>
<X509Data>
<X509IssuerSerial>
<X509IssuerName />
<X509SerialNumber />
</X509IssuerSerial>
<X509Certificate />
</X509Data>
</KeyInfo>
</Signature>
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}">
<Folios>
<UUID>{uuid}</UUID>
</Folios>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue/>
</Reference>
</SignedInfo>
<SignatureValue/>
<KeyInfo>
<X509Data>
<X509IssuerSerial>
<X509IssuerName/>
<X509SerialNumber/>
</X509IssuerSerial>
<X509Certificate/>
</X509Data>
</KeyInfo>
</Signature>
</Cancelacion>
"""