Skip to content

Commit 9b1ff41

Browse files
authored
Merge pull request #301 from felps-dev/master
Atualiza criptografia A1 para utilizar Cryptography no Lugar de OpenSSL
2 parents 3a902f6 + 8286c8b commit 9b1ff41

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

pynfe/entidades/certificado.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# -*- coding: utf-8 -*-
22

3-
from .base import Entidade
4-
from OpenSSL import crypto
5-
import tempfile
63
import os
4+
import tempfile
5+
6+
from cryptography.hazmat.primitives.serialization import (
7+
Encoding,
8+
NoEncryption,
9+
PrivateFormat,
10+
pkcs12,
11+
)
12+
13+
from .base import Entidade
714

815

916
class Certificado(Entidade):
@@ -49,43 +56,52 @@ def separar_arquivo(self, senha, caminho=False):
4956
"Falha ao abrir arquivo do certificado digital A1. Causa desconhecida."
5057
) from exc
5158

59+
if not isinstance(senha, bytes):
60+
senha = str.encode(senha)
61+
5262
# Carrega o arquivo .pfx, erro pode ocorrer se a senha estiver errada ou formato invalido.
5363
try:
54-
pkcs12 = crypto.load_pkcs12(cert_conteudo, senha)
55-
except crypto.Error as exc:
56-
raise Exception(
57-
"Falha ao carregar certificado digital A1. Verifique a senha do"
58-
" certificado."
59-
) from exc
60-
except Exception as exc:
61-
raise Exception(
62-
"Falha ao carregar certificado digital A1. Causa desconhecida."
63-
) from exc
64+
(
65+
chave,
66+
cert,
67+
) = pkcs12.load_key_and_certificates(
68+
cert_conteudo, senha
69+
)[:2]
70+
except Exception as e:
71+
if "invalid password" in str(e).lower():
72+
raise Exception(
73+
"Falha ao carregar certificado digital A1. Verifique a senha do"
74+
" certificado."
75+
) from e
76+
else:
77+
raise Exception(
78+
"Falha ao carregar certificado digital A1. Causa desconhecida."
79+
) from e
6480

6581
if caminho:
66-
cert = crypto.dump_certificate(
67-
crypto.FILETYPE_PEM, pkcs12.get_certificate()
68-
)
69-
chave = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkcs12.get_privatekey())
7082
# cria arquivos temporarios
7183
with tempfile.NamedTemporaryFile(delete=False) as arqcert:
72-
arqcert.write(cert)
84+
arqcert.write(cert.public_bytes(Encoding.PEM))
7385
with tempfile.NamedTemporaryFile(delete=False) as arqchave:
74-
arqchave.write(chave)
86+
arqchave.write(
87+
chave.private_bytes(
88+
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
89+
)
90+
)
7591
self.arquivos_temp.append(arqchave.name)
7692
self.arquivos_temp.append(arqcert.name)
7793
return arqchave.name, arqcert.name
7894
else:
7995
# Certificado
80-
cert = crypto.dump_certificate(
81-
crypto.FILETYPE_PEM, pkcs12.get_certificate()
82-
).decode("utf-8")
96+
cert = cert.public_bytes(Encoding.PEM).decode("utf-8")
8397
cert = cert.replace("\n", "")
8498
cert = cert.replace("-----BEGIN CERTIFICATE-----", "")
8599
cert = cert.replace("-----END CERTIFICATE-----", "")
86100

87101
# Chave, string decodificada da chave privada
88-
chave = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkcs12.get_privatekey())
102+
chave = chave.private_bytes(
103+
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
104+
)
89105

90106
return chave, cert
91107

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Dependencias basicas
2-
pyopenssl>=23.0.0
32
requests
43
lxml
54
signxml
6-
5+
cryptography
76
# Opcional para NFS-e
87
#-r requirements-nfse.txt

0 commit comments

Comments
 (0)