Skip to content

Commit d4f7a71

Browse files
committed
Use built in cryptography pkcs7 signature.
Signed-off-by: Robert Stewart <[email protected]>
1 parent e08c9d1 commit d4f7a71

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-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; python_version > '3'
55
cryptography==3.3.2; python_version <= '2.7'
6-
cryptography==3.4.7; python_version > '3'
6+
cryptography==39.0.2; python_version > '3'
77
fpdf>=1.7.2
88
dbf>=0.88.019
99
Pillow>=2.0.0

wsaa.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
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.hazmat.primitives.serialization import pkcs7
58+
5759

5860
except ImportError:
5961
ex = exception_info()
@@ -116,7 +118,6 @@ def sign_tra(tra, cert=CERT, privatekey=PRIVATEKEY, passphrase=""):
116118
_lib = Binding.lib
117119
_ffi = Binding.ffi
118120
# Crear un buffer desde el texto
119-
bio_in = _lib.BIO_new_mem_buf(tra, len(tra))
120121

121122
# Leer privatekey y cert
122123
if not privatekey.startswith(b"-----BEGIN RSA PRIVATE KEY-----"):
@@ -136,42 +137,28 @@ def sign_tra(tra, cert=CERT, privatekey=PRIVATEKEY, passphrase=""):
136137
cert = open(cert).read()
137138
if isinstance(cert, str):
138139
cert = cert.encode("utf-8")
139-
cert = x509.load_pem_x509_certificate(cert, default_backend())
140+
cert = x509.load_pem_x509_certificate(cert)
140141

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)
142+
143+
p7 = pkcs7.PKCS7SignatureBuilder().set_data(
144+
tra
145+
).add_signer(
146+
cert, private_key, hashes.SHA256()
147+
).sign(
148+
serialization.Encoding.SMIME, [pkcs7.PKCS7Options.DetachedSignature]
149+
)
166150

167151
# Generar p7 en formato mail y recortar headers
168-
msg = email.message_from_string(output.decode("utf8"))
152+
msg = email.message_from_string(p7.decode("utf8"))
169153
for part in msg.walk():
170154
filename = part.get_filename()
171-
if filename == "smime.p7m":
155+
if filename == "smime.p7s":
172156
# Es la parte firmada?
173157
# Devolver CMS
174158
return part.get_payload(decode=False)
159+
finally:
160+
raise RuntimeError("Part not found")
161+
175162
else:
176163
# Firmar el texto (tra) usando OPENSSL directamente
177164
try:
@@ -642,4 +629,4 @@ def main():
642629
print("Expiro?", wsaa.Expirado())
643630

644631
if __name__=="__main__":
645-
main()
632+
main()

0 commit comments

Comments
 (0)