Skip to content

Commit

Permalink
Only init our locking function if Python didnt
Browse files Browse the repository at this point in the history
Adds quite a bit of complexity, might be better to set ours
unconditionally but still import _ssl so that it doesn't overwrite ours
at some point later.
  • Loading branch information
public committed Dec 31, 2013
1 parent afe1007 commit 80108a4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 9 additions & 2 deletions cryptography/hazmat/backends/openssl/backend.py
Expand Up @@ -168,8 +168,15 @@ def _ensure_ffi_initialized(cls):
cls.ffi = ffi
cls.lib = lib

res = cls.lib.Cryptography_setup_locking()
assert res == 0
try:
__import__("_ssl")
raise ImportError
except ImportError:
pass

if cls.lib.CRYPTO_get_locking_callback() == cls.ffi.NULL:
res = cls.lib.Cryptography_setup_locking()
assert res == 0

cls.lib.OpenSSL_add_all_algorithms()
cls.lib.SSL_load_error_strings()
Expand Down
5 changes: 5 additions & 0 deletions cryptography/hazmat/backends/openssl/threads.py
Expand Up @@ -19,7 +19,10 @@
"""

FUNCTIONS = """
void (*CRYPTO_get_locking_callback(void))(int, int, const char *, int);
void CRYPTO_set_locking_callback(void (*func)(int, int, const char *, int));
static int Cryptography_setup_locking();
static void (*Cryptography_locking_function_ptr)(int, int, const char *, int);
"""

MACROS = """
Expand Down Expand Up @@ -200,6 +203,8 @@
}
}
static void (*Cryptography_locking_function_ptr)
(int, int, const char *, int) = Cryptography_locking_function;
static int Cryptography_setup_locking() {
unsigned int i;
Expand Down
20 changes: 20 additions & 0 deletions tests/hazmat/backends/test_openssl.py
Expand Up @@ -100,6 +100,26 @@ def test_handle_unknown_error(self):
0
)

def test_locking_callback_set(self):
b = Backend()

locking_cb = b.lib.CRYPTO_get_locking_callback()
assert locking_cb != b.ffi.NULL

# emulate import _ssl not setting this for some reason
b.lib.CRYPTO_set_locking_callback(b.ffi.NULL)

# force cffi to reinit
Backend.ffi = None
Backend.lib = None

# now it should get set to our one
b = Backend()
locking_cb = b.lib.CRYPTO_get_locking_callback()

assert locking_cb != b.ffi.NULL
assert locking_cb == b.lib.Cryptography_locking_function_ptr

def test_threads(self):
b = Backend()

Expand Down

0 comments on commit 80108a4

Please sign in to comment.