From ea657fd502cb810e77c11f2f03dc5c37be927251 Mon Sep 17 00:00:00 2001 From: leotada Date: Tue, 14 Jul 2015 18:14:54 -0300 Subject: [PATCH] =?UTF-8?q?Gera=C3=A7=C3=A3o=20e=20assinatura?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pynfe/entidades/certificado.py | 4 +--- pynfe/processamento/assinatura.py | 21 ++++++++++++++------- separarpfx.sh | 13 +++++++++++++ test.py | 13 +++++++------ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 separarpfx.sh diff --git a/pynfe/entidades/certificado.py b/pynfe/entidades/certificado.py index aeb28cb..370f636 100644 --- a/pynfe/entidades/certificado.py +++ b/pynfe/entidades/certificado.py @@ -38,9 +38,7 @@ class CertificadoA1(Certificado): caminho_chave = caminho_chave or os.path.join(self.pasta_temporaria, self.arquivo_chave) caminho_cert = caminho_cert or os.path.join(self.pasta_temporaria, self.arquivo_cert) - - import pdb - pdb.set_trace() + # Lendo o arquivo pfx no formato pkcs12 como binario pkcs12 = crypto.load_pkcs12(open(self.caminho_arquivo, 'rb').read(), senha) diff --git a/pynfe/processamento/assinatura.py b/pynfe/processamento/assinatura.py index 14e7b08..5d1ee85 100644 --- a/pynfe/processamento/assinatura.py +++ b/pynfe/processamento/assinatura.py @@ -23,19 +23,26 @@ class Assinatura(object): class AssinaturaA1(Assinatura): """Classe responsavel por efetuar a assinatura do certificado - digital no XML informado.""" + digital no XML informado. Passar XML como string.""" def assinar_nfe(self, xml): arquivo_cert = CertificadoA1(self.certificado) - key, cert = arquivo_cert.separar_arquivo(self.senha) - #cert = open("cert.pem").read() - #key = open("key.pem", "rb").read() + #key, cert = arquivo_cert.separar_arquivo(self.senha) + cert = open("cert.pem").read() + key = open("key.pem", "rb").read() + + begin = cert.find('-----BEGIN CERTIFICATE-----') + if begin < 0: + raise Exception('Formato de certificado inválido. Não encontrado tag inicial BEGIN.') + cert = cert[begin:] + # converte xml para bytes antes do parse - root = etree.parse(xml).getroot() + #root = etree.parse(xml).getroot() # caminho + root = etree.fromstring(xml) # string signer = signxml.xmldsig(root, digest_algorithm="sha1") signer.sign(method=signxml.methods.enveloped, key=key, cert=cert, algorithm="rsa-sha1", c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315', reference_uri='#NFe41150715380524000122651010000000271333611649') #verified_data = signer.verify(require_x509=True, ca_pem_file="cert.pem") - e = etree.tostring(signer.data) - open("testesig.xml", "wb").write(e) + result = etree.tostring(signer.data) + return result diff --git a/separarpfx.sh b/separarpfx.sh new file mode 100644 index 0000000..d543c62 --- /dev/null +++ b/separarpfx.sh @@ -0,0 +1,13 @@ +# To export the private key without a passphrase or password. +# Type: openssl pkcs12 -in filename.pfx -nocerts -nodes -out key.pem +# +# To Generate a public version of the private RSAkey +# Type: openssl rsa -in key.pem -out server.key +# +# To export the Certificate +# Type: openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem +# +# The directory will now have a file cert.pem and a key.pem. + +openssl pkcs12 -in JC.pfx -nocerts -nodes -out key.pem -passin pass:12345678 +openssl pkcs12 -in JC.pfx -clcerts -nokeys -out cert.pem -passin pass:12345678 \ No newline at end of file diff --git a/test.py b/test.py index e0d8fb5..00ff878 100644 --- a/test.py +++ b/test.py @@ -103,13 +103,14 @@ nota_fiscal.adicionar_produto_servico(codigo='000328', # id do produto (000328 e #_fonte_dados.adicionar_objeto(nota_fiscal) serializador = SerializacaoXML(_fonte_dados, homologacao=True) -xml = serializador.exportar(retorna_string=True) -arquivo = open('texte.xml', 'wb') -arquivo.write(xml) -certificado = "cert.pfx" +xml = serializador.exportar(retorna_string=True).decode('utf-8') +certificado = "JC.pfx" senha = '12345328' # assinatura -a = AssinaturaA1(certificado, senha) -print (a.assinar_nfe(xml)) +a1 = AssinaturaA1(certificado, senha) +xml = a1.assinar_nfe(xml) +# escreve +with open('teste.xml', 'wb') as arquivo: + arquivo.write(xml) #print serializador._serializar_nota_fiscal(nota_fiscal)