Skip to content

Commit 153de08

Browse files
committed
Fix OpenSSL string/bytes python3 issues
OpenSSL works a bit differently in python3 which requires delicate handling.
1 parent 9c91638 commit 153de08

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/saml2/cert.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import datetime
55
import dateutil.parser
66
import pytz
7+
import six
78
from OpenSSL import crypto
89
from os.path import join
910
from os import remove
@@ -154,10 +155,13 @@ def create_certificate(self, cert_info, request=False, valid_from=0,
154155
tmp_cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
155156
tmp_key = None
156157
if cipher_passphrase is not None:
158+
passphrase = cipher_passphrase["passphrase"]
159+
if isinstance(cipher_passphrase["passphrase"],
160+
six.string_types):
161+
passphrase = passphrase.encode('utf-8')
157162
tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k,
158163
cipher_passphrase["cipher"],
159-
cipher_passphrase[
160-
"passphrase"])
164+
passphrase)
161165
else:
162166
tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k)
163167
if write_to_file:
@@ -190,7 +194,7 @@ def write_str_to_file(self, file, str_data):
190194
f.close()
191195

192196
def read_str_from_file(self, file, type="pem"):
193-
f = open(file)
197+
f = open(file, 'rt')
194198
str_data = f.read()
195199
f.close()
196200

@@ -257,7 +261,10 @@ def create_cert_signed_certificate(self, sign_cert_str, sign_key_str,
257261
cert.set_pubkey(req_cert.get_pubkey())
258262
cert.sign(ca_key, hash_alg)
259263

260-
return crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
264+
cert_dump = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
265+
if isinstance(cert_dump, six.string_types):
266+
return cert_dump
267+
return cert_dump.decode('utf-8')
261268

262269
def verify_chain(self, cert_chain_str_list, cert_str):
263270
"""
@@ -327,6 +334,8 @@ def verify(self, signing_cert_str, cert_str):
327334
"signed certificate.")
328335

329336
cert_algorithm = cert.get_signature_algorithm()
337+
if six.PY3:
338+
cert_algorithm = cert_algorithm.decode('ascii')
330339

331340
cert_asn1 = crypto.dump_certificate(crypto.FILETYPE_ASN1, cert)
332341

@@ -342,7 +351,9 @@ def verify(self, signing_cert_str, cert_str):
342351

343352
signature_payload = cert_signature_decoded.payload
344353

345-
if signature_payload[0] != '\x00':
354+
sig_pay0 = signature_payload[0]
355+
if ((isinstance(sig_pay0, int) and sig_pay0 != 0) or
356+
(isinstance(sig_pay0, str) and sig_pay0 != '\x00')):
346357
return (False,
347358
"The certificate should not contain any unused bits.")
348359

@@ -355,4 +366,4 @@ def verify(self, signing_cert_str, cert_str):
355366
except crypto.Error as e:
356367
return False, "Certificate is incorrectly signed."
357368
except Exception as e:
358-
return False, "Certificate is not valid for an unknown reason."
369+
return False, "Certificate is not valid for an unknown reason. %s" % str(e)

tests/test_81_certificates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_validate_passphrase(self):
174174
request=True)
175175
cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
176176
req_cert_str,
177-
passphrase="qwerty")
177+
passphrase=b"qwerty")
178178

179179
valid = False
180180
try:

0 commit comments

Comments
 (0)