4
4
import datetime
5
5
import dateutil .parser
6
6
import pytz
7
+ import six
7
8
from OpenSSL import crypto
8
9
from os .path import join
9
10
from os import remove
@@ -154,10 +155,13 @@ def create_certificate(self, cert_info, request=False, valid_from=0,
154
155
tmp_cert = crypto .dump_certificate (crypto .FILETYPE_PEM , cert )
155
156
tmp_key = None
156
157
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' )
157
162
tmp_key = crypto .dump_privatekey (crypto .FILETYPE_PEM , k ,
158
163
cipher_passphrase ["cipher" ],
159
- cipher_passphrase [
160
- "passphrase" ])
164
+ passphrase )
161
165
else :
162
166
tmp_key = crypto .dump_privatekey (crypto .FILETYPE_PEM , k )
163
167
if write_to_file :
@@ -190,7 +194,7 @@ def write_str_to_file(self, file, str_data):
190
194
f .close ()
191
195
192
196
def read_str_from_file (self , file , type = "pem" ):
193
- f = open (file )
197
+ f = open (file , 'rt' )
194
198
str_data = f .read ()
195
199
f .close ()
196
200
@@ -257,7 +261,10 @@ def create_cert_signed_certificate(self, sign_cert_str, sign_key_str,
257
261
cert .set_pubkey (req_cert .get_pubkey ())
258
262
cert .sign (ca_key , hash_alg )
259
263
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' )
261
268
262
269
def verify_chain (self , cert_chain_str_list , cert_str ):
263
270
"""
@@ -327,6 +334,8 @@ def verify(self, signing_cert_str, cert_str):
327
334
"signed certificate." )
328
335
329
336
cert_algorithm = cert .get_signature_algorithm ()
337
+ if six .PY3 :
338
+ cert_algorithm = cert_algorithm .decode ('ascii' )
330
339
331
340
cert_asn1 = crypto .dump_certificate (crypto .FILETYPE_ASN1 , cert )
332
341
@@ -342,7 +351,9 @@ def verify(self, signing_cert_str, cert_str):
342
351
343
352
signature_payload = cert_signature_decoded .payload
344
353
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 ' )):
346
357
return (False ,
347
358
"The certificate should not contain any unused bits." )
348
359
@@ -355,4 +366,4 @@ def verify(self, signing_cert_str, cert_str):
355
366
except crypto .Error as e :
356
367
return False , "Certificate is incorrectly signed."
357
368
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 )
0 commit comments