simple_ssl_context() is now a helper exposed in module sfa.util.ssl
[sfa.git] / sfa / trust / certificate.py
index fb515e5..a0e3a70 100644 (file)
@@ -45,7 +45,7 @@
 #
 
 
-from __future__ import print_function
+
 
 import functools
 import os
@@ -57,8 +57,6 @@ import OpenSSL
 # M2Crypto is imported on the fly to minimize crashes
 # import M2Crypto
 
-from sfa.util.py23 import PY3
-
 from sfa.util.faults import (CertExpired, CertMissingParent,
                              CertNotSignedByParent)
 from sfa.util.sfalogging import logger
@@ -117,7 +115,7 @@ def convert_public_key(key):
 
     (ssh_f, ssh_fn) = tempfile.mkstemp()
     ssl_fn = tempfile.mktemp()
-    os.write(ssh_f, key)
+    os.write(ssh_f, key.encode())
     os.close(ssh_f)
 
     cmd = keyconvert_path + " " + ssh_fn + " " + ssl_fn
@@ -182,7 +180,7 @@ class Keypair:
     # @param filename name of file to store the keypair in
 
     def save_to_file(self, filename):
-        with open(filename, 'w') as output:
+        with open(filename, 'wb') as output:
             output.write(self.as_pem())
         self.filename = filename
 
@@ -206,12 +204,13 @@ class Keypair:
                 OpenSSL.crypto.FILETYPE_PEM, string,
                 functools.partial(glo_passphrase_callback, self, string))
             self.m2key = M2Crypto.EVP.load_key_string(
-                string, functools.partial(glo_passphrase_callback,
-                                          self, string))
+                string.encode(encoding="utf-8"),
+                functools.partial(glo_passphrase_callback, self, string))
         else:
             self.key = OpenSSL.crypto.load_privatekey(
                 OpenSSL.crypto.FILETYPE_PEM, string)
-            self.m2key = M2Crypto.EVP.load_key_string(string)
+            self.m2key = M2Crypto.EVP.load_key_string(
+                string.encode(encoding="utf-8"))
 
     ##
     #  Load the public key from a string. No private key is loaded.
@@ -358,8 +357,8 @@ class Certificate:
     # @param create If create==True, then also create a blank X509 certificate.
     # @param subject If subject!=None, then create a blank certificate and set
     #     it's subject name.
-    # @param string If string!=None, load the certficate from the string.
-    # @param filename If filename!=None, load the certficiate from the file.
+    # @param string If string!=None, load the certificate from the string.
+    # @param filename If filename!=None, load the certificate from the file.
     # @param isCA If !=None, set whether this cert is for a CA
 
     def __init__(self, lifeDays=1825, create=False, subject=None, string=None,
@@ -472,7 +471,7 @@ class Certificate:
             return ""
         string = OpenSSL.crypto.dump_certificate(
             OpenSSL.crypto.FILETYPE_PEM, self.x509)
-        if PY3 and isinstance(string, bytes):
+        if isinstance(string, bytes):
             string = string.decode()
         if save_parents and self.parent:
             string = string + self.parent.save_to_string(save_parents)
@@ -489,7 +488,7 @@ class Certificate:
             f = filep
         else:
             f = open(filename, 'w')
-        if PY3 and isinstance(string, bytes):
+        if isinstance(string, bytes):
             string = string.decode()
         f.write(string)
         f.close()
@@ -521,7 +520,7 @@ class Certificate:
                 req = OpenSSL.crypto.X509Req()
                 reqSubject = req.get_subject()
                 if isinstance(subject, dict):
-                    for key in reqSubject.keys():
+                    for key in list(reqSubject.keys()):
                         setattr(reqSubject, key, subject[key])
                 else:
                     setattr(reqSubject, "CN", subject)
@@ -548,7 +547,7 @@ class Certificate:
         req = OpenSSL.crypto.X509Req()
         subj = req.get_subject()
         if isinstance(name, dict):
-            for key in name.keys():
+            for key in list(name.keys()):
                 setattr(subj, key, name[key])
         else:
             setattr(subj, "CN", name)
@@ -676,6 +675,11 @@ class Certificate:
 #            raise "Cannot add extension {} which had val {} with new val {}"\
 #                  .format(name, oldExtVal, value)
 
+        if isinstance(name, str):
+            name = name.encode()
+        if isinstance(value, str):
+            value = value.encode()
+
         ext = OpenSSL.crypto.X509Extension(name, critical, value)
         self.x509.add_extensions([ext])
 
@@ -799,7 +803,7 @@ class Certificate:
         return result
 
     ##
-    # Set the parent certficiate.
+    # Set the parent certificate.
     #
     # @param p certificate object.
 
@@ -937,7 +941,7 @@ class Certificate:
         return triples
 
     def get_data_names(self):
-        return self.data.keys()
+        return list(self.data.keys())
 
     def get_all_datas(self):
         triples = self.get_extensions()