Skip to content

Commit 205a0c8

Browse files
bump: pull request clone to fix pull PyAr#113. current commit is just the clone of the pycon commit
1 parent 27de097 commit 205a0c8

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ httplib2==0.20.4; python_version > '3'
33
pysimplesoap==1.08.14; python_version <= '2.7'
44
git+https://github.com/pysimplesoap/pysimplesoap.git@py311#pysimplesoap;
55
cryptography==3.3.2; python_version <= '2.7'
6-
cryptography==3.4.7; python_version > '3'
6+
cryptography==41.0.0; python_version > '3'
77
fpdf>=1.7.2
88
dbf>=0.88.019
99
Pillow>=2.0.0

wsaa.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from cryptography.hazmat.primitives import serialization
5555
from cryptography.hazmat.primitives.asymmetric import rsa
5656
from cryptography.hazmat.bindings.openssl.binding import Binding
57+
from cryptography.hamzat.primitives.serialization import pkcs7
5758

5859
except ImportError:
5960
ex = exception_info()
@@ -116,7 +117,6 @@ def sign_tra(tra, cert=CERT, privatekey=PRIVATEKEY, passphrase=""):
116117
_lib = Binding.lib
117118
_ffi = Binding.ffi
118119
# Crear un buffer desde el texto
119-
bio_in = _lib.BIO_new_mem_buf(tra, len(tra))
120120

121121
# Leer privatekey y cert
122122
if not privatekey.startswith(b"-----BEGIN RSA PRIVATE KEY-----"):
@@ -136,42 +136,26 @@ def sign_tra(tra, cert=CERT, privatekey=PRIVATEKEY, passphrase=""):
136136
cert = open(cert).read()
137137
if isinstance(cert, str):
138138
cert = cert.encode("utf-8")
139-
cert = x509.load_pem_x509_certificate(cert, default_backend())
140-
141-
try:
142-
# Firmar el texto (tra) usando cryptography (openssl bindings para python)
143-
p7 = _lib.PKCS7_sign(
144-
cert._x509, private_key._evp_pkey, _ffi.NULL, bio_in, 0
145-
)
146-
finally:
147-
# Liberar memoria asignada
148-
_lib.BIO_free(bio_in)
149-
# Se crea un buffer nuevo porque la firma lo consume
150-
bio_in = _lib.BIO_new_mem_buf(tra, len(tra))
151-
try:
152-
# Crear buffer de salida
153-
bio_out = _lib.BIO_new(_lib.BIO_s_mem())
154-
try:
155-
# Instanciar un SMIME
156-
_lib.SMIME_write_PKCS7(bio_out, p7, bio_in, 0)
157-
158-
# Tomar datos para la salida
159-
result_buffer = _ffi.new("char**")
160-
buffer_length = _lib.BIO_get_mem_data(bio_out, result_buffer)
161-
output = _ffi.buffer(result_buffer[0], buffer_length)[:]
162-
finally:
163-
_lib.BIO_free(bio_out)
164-
finally:
165-
_lib.BIO_free(bio_in)
139+
cert = x509.load_pem_x509_certificate(cert)
140+
141+
p7 = pkcs7.PKCS7SignatureBuilder().set_data(
142+
tra
143+
).add_signer(
144+
cert, private_key, hashes.SHA256()
145+
).sign(
146+
serialization.Encoding.SMIME, [pkcs7.PKCS7Options.DetachedSignature]
147+
)
166148

167149
# Generar p7 en formato mail y recortar headers
168-
msg = email.message_from_string(output.decode("utf8"))
150+
msg = email.message_from_string(p7.decode("utf8"))
169151
for part in msg.walk():
170152
filename = part.get_filename()
171-
if filename == "smime.p7m":
153+
if filename == "smime.p7s":
172154
# Es la parte firmada?
173155
# Devolver CMS
174156
return part.get_payload(decode=False)
157+
else:
158+
raise RuntimeError("Part not found")
175159
else:
176160
# Firmar el texto (tra) usando OPENSSL directamente
177161
try:

0 commit comments

Comments
 (0)