You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.4 KiB
60 lines
2.4 KiB
# -*- coding: utf-8 -*-
|
|
|
|
import signxml
|
|
from OpenSSL import crypto
|
|
from pynfe.utils import etree
|
|
from pynfe.entidades.certificado import CertificadoA1
|
|
from pynfe.utils.flags import NAMESPACE_NFE, NAMESPACE_SIG
|
|
import subprocess
|
|
|
|
|
|
class Assinatura(object):
|
|
"""Classe abstrata responsavel por definir os metodos e logica das classes
|
|
de assinatura digital."""
|
|
|
|
certificado = None
|
|
senha = None
|
|
|
|
def __init__(self, certificado, senha):
|
|
self.certificado = certificado
|
|
self.senha = senha
|
|
|
|
def assinar(self, xml):
|
|
"""Efetua a assinatura da nota"""
|
|
pass
|
|
|
|
class AssinaturaA1(Assinatura):
|
|
"""Classe responsavel por efetuar a assinatura do certificado
|
|
digital no XML informado."""
|
|
|
|
def assinar(self, xml, retorna_string=False):
|
|
try:
|
|
# No raiz do XML de saida
|
|
raiz = etree.Element('Signature', xmlns='http://www.w3.org/2000/09/xmldsig#')
|
|
siginfo = etree.SubElement(raiz, 'SignedInfo')
|
|
etree.SubElement(siginfo, 'CanonicalizationMethod', Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315')
|
|
etree.SubElement(siginfo, 'SignatureMethod', Algorithm='http://www.w3.org/2000/09/xmldsig#rsa-sha1')
|
|
ref = etree.SubElement(siginfo, 'Reference', URI='#'+xml.findall('infNFe')[0].attrib['Id'])
|
|
trans = etree.SubElement(ref, 'Transforms')
|
|
etree.SubElement(trans, 'Transform', Algorithm='http://www.w3.org/2000/09/xmldsig#enveloped-signature')
|
|
etree.SubElement(trans, 'Transform', Algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315')
|
|
etree.SubElement(ref, 'DigestMethod', Algorithm='http://www.w3.org/2000/09/xmldsig#sha1')
|
|
etree.SubElement(ref, 'DigestValue')
|
|
etree.SubElement(raiz, 'SignatureValue')
|
|
keyinfo = etree.SubElement(raiz, 'KeyInfo')
|
|
etree.SubElement(keyinfo, 'X509Data')
|
|
|
|
xml.append(raiz)
|
|
|
|
with open('testes.xml', 'w') as arquivo:
|
|
arquivo.write(etree.tostring(xml, encoding="unicode", pretty_print=False))
|
|
|
|
subprocess.check_call('xmlsec1 --sign --pkcs12 '+self.certificado+' --pwd '+self.senha+' --crypto openssl --output funciona.xml --id-attr:Id infNFe testes.xml')
|
|
|
|
if retorna_string:
|
|
return etree.tostring(xml, encoding="unicode", pretty_print=False)
|
|
else:
|
|
return xml
|
|
except Exception as e:
|
|
raise e
|
|
|