aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2013-12-29 23:23:03 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2013-12-30 11:17:09 -0600
commit541c72021bfae49712a16069c44a32737e4b283b (patch)
tree7ce15f52e8cb33a3c58910c15633483ae6266c64
parent026168cc80c964b237ebd65a20c05679acd48332 (diff)
downloadcryptography-541c72021bfae49712a16069c44a32737e4b283b.tar.gz
cryptography-541c72021bfae49712a16069c44a32737e4b283b.tar.bz2
cryptography-541c72021bfae49712a16069c44a32737e4b283b.zip
allow the backend to register/unregister the engine via methods
-rw-r--r--cryptography/hazmat/backends/openssl/backend.py37
-rw-r--r--cryptography/hazmat/backends/openssl/urand_engine.py2
2 files changed, 34 insertions, 5 deletions
diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py
index d6a77640..0f134e52 100644
--- a/cryptography/hazmat/backends/openssl/backend.py
+++ b/cryptography/hazmat/backends/openssl/backend.py
@@ -107,6 +107,7 @@ class Backend(object):
self._cipher_registry = {}
self._register_default_ciphers()
+ self.register_urandom_engine()
@classmethod
def _ensure_ffi_initialized(cls):
@@ -171,14 +172,42 @@ class Backend(object):
cls.lib.OpenSSL_add_all_algorithms()
cls.lib.SSL_load_error_strings()
+ # Add the urandom engine to the engine list
res = cls.lib.Cryptography_add_urandom_engine()
assert res == 1
- e = cls.lib.ENGINE_by_id("urandom")
- assert e != cls.ffi.NULL
- res = cls.lib.ENGINE_init(e)
+
+ def unregister_urandom_engine(self):
+ e = self.lib.ENGINE_get_default_RAND()
+ if e != self.ffi.NULL:
+ name = self.lib.ENGINE_get_name(e)
+ assert name != self.ffi.NULL
+ if self.ffi.string(name) == "urandom_engine":
+ self.lib.ENGINE_unregister_RAND(e)
+ res = self.lib.ENGINE_free(e)
+ assert res == 1
+ self.lib.RAND_cleanup()
+
+ def register_urandom_engine(self):
+ current_rand = self.lib.ENGINE_get_default_RAND()
+ if current_rand != self.ffi.NULL:
+ name = self.lib.ENGINE_get_name(current_rand)
+ assert name != self.ffi.NULL
+ if self.ffi.string(name) == "urandom_engine":
+ res = self.lib.ENGINE_finish(current_rand)
+ assert res == 1
+ return
+
+ e = self.lib.ENGINE_by_id("urandom")
+ assert e != self.ffi.NULL
+ res = self.lib.ENGINE_init(e)
+ assert res == 1
+ res = self.lib.ENGINE_set_default_RAND(e)
+ assert res == 1
+ res = self.lib.ENGINE_finish(e)
assert res == 1
- res = cls.lib.ENGINE_set_default_RAND(e)
+ res = self.lib.ENGINE_free(e)
assert res == 1
+ self.lib.RAND_cleanup()
def openssl_version_text(self):
"""
diff --git a/cryptography/hazmat/backends/openssl/urand_engine.py b/cryptography/hazmat/backends/openssl/urand_engine.py
index 479bbb2c..0a86af3e 100644
--- a/cryptography/hazmat/backends/openssl/urand_engine.py
+++ b/cryptography/hazmat/backends/openssl/urand_engine.py
@@ -32,7 +32,7 @@ MACROS = """
CUSTOMIZATIONS = """
static const char *urandom_engine_id= "urandom";
-static const char *urandom_engine_name = "urandom engine";
+static const char *urandom_engine_name = "urandom_engine";
#ifndef _WIN32
static int urandom_fd;