We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Yet another building on PyPy(5.8) issue
Hi,

I'm trying to build cryptography (tried both 1.9 and master) against PyPy 5.8beta0.
I'm running on osx sierra.

I've looked at several issues here, and have tried several suggested solutions, but none worked for me 😞 

I'm trying to link against brew openssl.
`brew list --versions openssl` -> `openssl 1.0.2j 1.0.2k 1.0.2l`

I've built pypy's openssl module without any special problem

```
env PYTHONPATH=/Users/omerba/Workspace/pypy \                                                                                                                               
       DYLD_LIBRARY_PATH="/Users/omerba/anaconda/lib" \
       CFLAGS="-I/usr/local/opt/openssl/include" \
       LDFLAGS="-L/usr/local/opt/openssl/lib" \
       /Users/omerba/Workspace/pypy/pypy3-c pypy/tool/build_cffi_imports.py
```
But when i try to build cryptography (in cryptography's dir):
```
 env PYTHONPATH=/Users/omerba/Workspace/pypy \                                                                                                                           
       DYLD_LIBRARY_PATH="/Users/omerba/anaconda/lib" \
       CFLAGS="-I/usr/local/opt/openssl/include" \
       LDFLAGS="-L/usr/local/opt/openssl/lib" \
       /Users/omerba/Workspace/pypy/pypy3-c setup.py install
```
I get:
```
running install
running bdist_egg
running egg_info
writing src/cryptography.egg-info/PKG-INFO
writing dependency_links to src/cryptography.egg-info/dependency_links.txt
writing entry points to src/cryptography.egg-info/entry_points.txt
writing requirements to src/cryptography.egg-info/requires.txt
writing top-level names to src/cryptography.egg-info/top_level.txt
reading manifest file 'src/cryptography.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching 'docs/_build'
warning: no previously-included files matching '*' found under directory 'vectors'
writing manifest file 'src/cryptography.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.12-x86_64/egg
running install_lib
running build_py
running build_ext
generating cffi module 'build/temp.macosx-10.12-x86_64-3.5/_padding.c'
already up-to-date
generating cffi module 'build/temp.macosx-10.12-x86_64-3.5/_constant_time.c'
already up-to-date
generating cffi module 'build/temp.macosx-10.12-x86_64-3.5/_openssl.c'
building '_openssl' extension
cc -pthread -DNDEBUG -O2 -I/usr/local/opt/openssl/include -fPIC -I/Users/omerba/Workspace/pypy/include -c build/temp.macosx-10.12-x86_64-3.5/_openssl.c -o build/temp.macosx-10.12-x86_64-3.5/build/temp.macosx-10.12-x86_64-3.5/_openssl.o
build/temp.macosx-10.12-x86_64-3.5/_openssl.c:2503:9: warning: comparison of function 'getentropy' not equal to a null pointer is always true [-Wtautological-pointer-compare]
    if (getentropy != NULL) {
        ^~~~~~~~~~    ~~~~
build/temp.macosx-10.12-x86_64-3.5/_openssl.c:2503:9: note: prefix with the address-of operator to silence this warning
    if (getentropy != NULL) {
        ^
        &
build/temp.macosx-10.12-x86_64-3.5/_openssl.c:3454:22: warning: comparison of constant 1152921504606846975 with expression of type 'unsigned int' is always false [-Wtautological-constant-out-of-range-compare]
        _ssl_locks = PyMem_New(PyThread_type_lock, _ssl_locks_count);
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/omerba/Workspace/pypy/include/pymem.h:38:10: note: expanded from macro 'PyMem_New'
  ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
     ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
build/temp.macosx-10.12-x86_64-3.5/_openssl.c:74852:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
3 warnings generated.
cc -pthread -shared -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include build/temp.macosx-10.12-x86_64-3.5/build/temp.macosx-10.12-x86_64-3.5/_openssl.o -lssl -lcrypto -o build/lib.macosx-10.12-x86_64-3.5/cryptography/hazmat/bindings/_openssl.pypy3-58-x86_64-darwin.so
clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
Undefined symbols for architecture x86_64:
  "_PyPyErr_NoMemory", referenced from:
      __setup_ssl_threads in _openssl.o
  "_PyPyMem_Free", referenced from:
      __setup_ssl_threads in _openssl.o
  "_PyPyMem_Malloc", referenced from:
      __setup_ssl_threads in _openssl.o
  "_PyPyThread_acquire_lock", referenced from:
      __ssl_thread_locking_function in _openssl.o
  "_PyPyThread_allocate_lock", referenced from:
      __setup_ssl_threads in _openssl.o
  "_PyPyThread_free_lock", referenced from:
      __setup_ssl_threads in _openssl.o
  "_PyPyThread_release_lock", referenced from:
      __ssl_thread_locking_function in _openssl.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'cc' failed with exit status 1
```

Googling the issue only leads to [this](https://bitbucket.org/pypy/pypy/issues/2538/_ssl_buildpy-fails-on-macos-with-the-py35) exact same issue on the PyPy repo.

But the PyPy team seemed to have resolved it by making some changes to their cffi backend.
I'll admit that I tried to shamelessly copy these changes to cryptography's _cffi_src dir - which made the package install successfully, but then when I actually tried to use it:

`from cryptography.hazmat.backends import default_backend, openssl`

it blows up.. 

```
AttributeErrorTraceback (most recent call last)
<ipython-input-5-cc19c86b2edb> in <module>()
      1 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
----> 2 from cryptography.hazmat.backends import default_backend, openssl

~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/backends/openssl/__init__.py in <module>()
      5 from __future__ import absolute_import, division, print_function
      6 
----> 7 from cryptography.hazmat.backends.openssl.backend import backend
      8 
      9 

~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/backends/openssl/backend.py in <module>()
     47     _CertificateSigningRequest, _RevokedCertificate
     48 )
---> 49 from cryptography.hazmat.bindings.openssl import binding
     50 from cryptography.hazmat.primitives import hashes, serialization
     51 from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa

~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in <module>()
    154 # condition registering the OpenSSL locks. On Python 3.4+ the import lock
    155 # is per module so this approach will not work.
--> 156 Binding.init_static_locks()

~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in init_static_locks(cls)
    135     def init_static_locks(cls):
    136         with cls._lock_init_lock:
--> 137             cls._ensure_ffi_initialized()
    138             # Use Python's implementation if available, importing _ssl triggers
    139             # the setup for this.

~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in _ensure_ffi_initialized(cls)
    122         with cls._init_lock:
    123             if not cls._lib_loaded:
--> 124                 cls.lib = build_conditional_library(lib, CONDITIONAL_NAMES)
    125                 cls._lib_loaded = True
    126                 # initialize the SSL library

~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in build_conditional_library(lib, conditional_names)
     82     excluded_names = set()
     83     for condition, names in conditional_names.items():
---> 84         if not getattr(lib, condition):
     85             excluded_names |= set(names)
     86 

AttributeError: cffi library 'cryptography.hazmat.bindings._openssl' has no function, constant or global variable named 'Cryptography_HAS_DTLS'
```

Thanks for your work guys!

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/_cffi_src/openssl/callbacks.py`
Content:
```
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4 
5 from __future__ import absolute_import, division, print_function
6 
7 INCLUDES = """
8 #include <openssl/ssl.h>
9 #include <openssl/x509.h>
10 #include <openssl/x509_vfy.h>
11 #include <openssl/crypto.h>
12 
13 #include <pythread.h>
14 """
15 
16 TYPES = """
17 typedef struct {
18     char *password;
19     int length;
20     int called;
21     int error;
22     int maxsize;
23 } CRYPTOGRAPHY_PASSWORD_DATA;
24 """
25 
26 FUNCTIONS = """
27 int _setup_ssl_threads(void);
28 int Cryptography_pem_password_cb(char *, int, int, void *);
29 """
30 
31 MACROS = """
32 """
33 
34 CUSTOMIZATIONS = """
35 /* This code is derived from the locking code found in the Python _ssl module's
36    locking callback for OpenSSL.
37 
38    Copyright 2001-2016 Python Software Foundation; All Rights Reserved.
39 */
40 
41 static unsigned int _ssl_locks_count = 0;
42 static PyThread_type_lock *_ssl_locks = NULL;
43 
44 static void _ssl_thread_locking_function(int mode, int n, const char *file,
45                                          int line) {
46     /* this function is needed to perform locking on shared data
47        structures. (Note that OpenSSL uses a number of global data
48        structures that will be implicitly shared whenever multiple
49        threads use OpenSSL.) Multi-threaded applications will
50        crash at random if it is not set.
51 
52        locking_function() must be able to handle up to
53        CRYPTO_num_locks() different mutex locks. It sets the n-th
54        lock if mode & CRYPTO_LOCK, and releases it otherwise.
55 
56        file and line are the file number of the function setting the
57        lock. They can be useful for debugging.
58     */
59 
60     if ((_ssl_locks == NULL) ||
61         (n < 0) || ((unsigned)n >= _ssl_locks_count)) {
62         return;
63     }
64 
65     if (mode & CRYPTO_LOCK) {
66         PyThread_acquire_lock(_ssl_locks[n], 1);
67     } else {
68         PyThread_release_lock(_ssl_locks[n]);
69     }
70 }
71 
72 int _setup_ssl_threads(void) {
73     unsigned int i;
74 
75     if (_ssl_locks == NULL) {
76         _ssl_locks_count = CRYPTO_num_locks();
77         _ssl_locks = PyMem_New(PyThread_type_lock, _ssl_locks_count);
78         if (_ssl_locks == NULL) {
79             PyErr_NoMemory();
80             return 0;
81         }
82         memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count);
83         for (i = 0;  i < _ssl_locks_count;  i++) {
84             _ssl_locks[i] = PyThread_allocate_lock();
85             if (_ssl_locks[i] == NULL) {
86                 unsigned int j;
87                 for (j = 0;  j < i;  j++) {
88                     PyThread_free_lock(_ssl_locks[j]);
89                 }
90                 PyMem_Free(_ssl_locks);
91                 return 0;
92             }
93         }
94         CRYPTO_set_locking_callback(_ssl_thread_locking_function);
95     }
96     return 1;
97 }
98 
99 typedef struct {
100     char *password;
101     int length;
102     int called;
103     int error;
104     int maxsize;
105 } CRYPTOGRAPHY_PASSWORD_DATA;
106 
107 int Cryptography_pem_password_cb(char *buf, int size,
108                                   int rwflag, void *userdata) {
109     /* The password cb is only invoked if OpenSSL decides the private
110        key is encrypted. So this path only occurs if it needs a password */
111     CRYPTOGRAPHY_PASSWORD_DATA *st = (CRYPTOGRAPHY_PASSWORD_DATA *)userdata;
112     st->called += 1;
113     st->maxsize = size;
114     if (st->length == 0) {
115         st->error = -1;
116         return 0;
117     } else if (st->length < size) {
118         memcpy(buf, st->password, st->length);
119         return st->length;
120     } else {
121         st->error = -2;
122         return 0;
123     }
124 }
125 """
126
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/_cffi_src/openssl/callbacks.py b/src/_cffi_src/openssl/callbacks.py
--- a/src/_cffi_src/openssl/callbacks.py
+++ b/src/_cffi_src/openssl/callbacks.py
@@ -10,7 +10,13 @@
 #include <openssl/x509_vfy.h>
 #include <openssl/crypto.h>
 
-#include <pythread.h>
+#ifdef _WIN32
+#include <Windows.h>
+#else
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#endif
 """
 
 TYPES = """
@@ -36,10 +42,47 @@
    locking callback for OpenSSL.
 
    Copyright 2001-2016 Python Software Foundation; All Rights Reserved.
+
+   It has been subsequently modified to use cross platform locking without
+   using CPython APIs by Armin Rigo of the PyPy project.
 */
 
+#ifdef _WIN32
+typedef CRITICAL_SECTION Cryptography_mutex;
+static __inline void cryptography_mutex_init(Cryptography_mutex *mutex) {
+    InitializeCriticalSection(mutex);
+}
+static __inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {
+    EnterCriticalSection(mutex);
+}
+static __inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {
+    LeaveCriticalSection(mutex);
+}
+#else
+typedef pthread_mutex_t Cryptography_mutex;
+#define ASSERT_STATUS(call)                                             \
+    if ((call) != 0) {                                                  \
+        perror("Fatal error in callback initialization: " #call);       \
+        abort();                                                        \
+    }
+static inline void cryptography_mutex_init(Cryptography_mutex *mutex) {
+#if !defined(pthread_mutexattr_default)
+#  define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
+#endif
+    ASSERT_STATUS(pthread_mutex_init(mutex, pthread_mutexattr_default));
+}
+static inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {
+    ASSERT_STATUS(pthread_mutex_lock(mutex));
+}
+static inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {
+    ASSERT_STATUS(pthread_mutex_unlock(mutex));
+}
+#endif
+
+
+
 static unsigned int _ssl_locks_count = 0;
-static PyThread_type_lock *_ssl_locks = NULL;
+static Cryptography_mutex *_ssl_locks = NULL;
 
 static void _ssl_thread_locking_function(int mode, int n, const char *file,
                                          int line) {
@@ -63,35 +106,32 @@
     }
 
     if (mode & CRYPTO_LOCK) {
-        PyThread_acquire_lock(_ssl_locks[n], 1);
+        cryptography_mutex_lock(_ssl_locks + n);
     } else {
-        PyThread_release_lock(_ssl_locks[n]);
+        cryptography_mutex_unlock(_ssl_locks + n);
+    }
+}
+
+static void init_mutexes(void) {
+    int i;
+    for (i = 0; i < _ssl_locks_count; i++) {
+        cryptography_mutex_init(_ssl_locks + i);
     }
 }
 
-int _setup_ssl_threads(void) {
-    unsigned int i;
 
+int _setup_ssl_threads(void) {
     if (_ssl_locks == NULL) {
         _ssl_locks_count = CRYPTO_num_locks();
-        _ssl_locks = PyMem_New(PyThread_type_lock, _ssl_locks_count);
+        _ssl_locks = calloc(_ssl_locks_count, sizeof(Cryptography_mutex));
         if (_ssl_locks == NULL) {
-            PyErr_NoMemory();
             return 0;
         }
-        memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count);
-        for (i = 0;  i < _ssl_locks_count;  i++) {
-            _ssl_locks[i] = PyThread_allocate_lock();
-            if (_ssl_locks[i] == NULL) {
-                unsigned int j;
-                for (j = 0;  j < i;  j++) {
-                    PyThread_free_lock(_ssl_locks[j]);
-                }
-                PyMem_Free(_ssl_locks);
-                return 0;
-            }
-        }
+        init_mutexes();
         CRYPTO_set_locking_callback(_ssl_thread_locking_function);
+#ifndef _WIN32
+        pthread_atfork(NULL, NULL, &init_mutexes);
+#endif
     }
     return 1;
 }

{"golden_diff": "diff --git a/src/_cffi_src/openssl/callbacks.py b/src/_cffi_src/openssl/callbacks.py\n--- a/src/_cffi_src/openssl/callbacks.py\n+++ b/src/_cffi_src/openssl/callbacks.py\n@@ -10,7 +10,13 @@\n #include <openssl/x509_vfy.h>\n #include <openssl/crypto.h>\n \n-#include <pythread.h>\n+#ifdef _WIN32\n+#include <Windows.h>\n+#else\n+#include <stdio.h>\n+#include <stdlib.h>\n+#include <pthread.h>\n+#endif\n \"\"\"\n \n TYPES = \"\"\"\n@@ -36,10 +42,47 @@\n    locking callback for OpenSSL.\n \n    Copyright 2001-2016 Python Software Foundation; All Rights Reserved.\n+\n+   It has been subsequently modified to use cross platform locking without\n+   using CPython APIs by Armin Rigo of the PyPy project.\n */\n \n+#ifdef _WIN32\n+typedef CRITICAL_SECTION Cryptography_mutex;\n+static __inline void cryptography_mutex_init(Cryptography_mutex *mutex) {\n+    InitializeCriticalSection(mutex);\n+}\n+static __inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {\n+    EnterCriticalSection(mutex);\n+}\n+static __inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {\n+    LeaveCriticalSection(mutex);\n+}\n+#else\n+typedef pthread_mutex_t Cryptography_mutex;\n+#define ASSERT_STATUS(call)                                             \\\n+    if ((call) != 0) {                                                  \\\n+        perror(\"Fatal error in callback initialization: \" #call);       \\\n+        abort();                                                        \\\n+    }\n+static inline void cryptography_mutex_init(Cryptography_mutex *mutex) {\n+#if !defined(pthread_mutexattr_default)\n+#  define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)\n+#endif\n+    ASSERT_STATUS(pthread_mutex_init(mutex, pthread_mutexattr_default));\n+}\n+static inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {\n+    ASSERT_STATUS(pthread_mutex_lock(mutex));\n+}\n+static inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {\n+    ASSERT_STATUS(pthread_mutex_unlock(mutex));\n+}\n+#endif\n+\n+\n+\n static unsigned int _ssl_locks_count = 0;\n-static PyThread_type_lock *_ssl_locks = NULL;\n+static Cryptography_mutex *_ssl_locks = NULL;\n \n static void _ssl_thread_locking_function(int mode, int n, const char *file,\n                                          int line) {\n@@ -63,35 +106,32 @@\n     }\n \n     if (mode & CRYPTO_LOCK) {\n-        PyThread_acquire_lock(_ssl_locks[n], 1);\n+        cryptography_mutex_lock(_ssl_locks + n);\n     } else {\n-        PyThread_release_lock(_ssl_locks[n]);\n+        cryptography_mutex_unlock(_ssl_locks + n);\n+    }\n+}\n+\n+static void init_mutexes(void) {\n+    int i;\n+    for (i = 0; i < _ssl_locks_count; i++) {\n+        cryptography_mutex_init(_ssl_locks + i);\n     }\n }\n \n-int _setup_ssl_threads(void) {\n-    unsigned int i;\n \n+int _setup_ssl_threads(void) {\n     if (_ssl_locks == NULL) {\n         _ssl_locks_count = CRYPTO_num_locks();\n-        _ssl_locks = PyMem_New(PyThread_type_lock, _ssl_locks_count);\n+        _ssl_locks = calloc(_ssl_locks_count, sizeof(Cryptography_mutex));\n         if (_ssl_locks == NULL) {\n-            PyErr_NoMemory();\n             return 0;\n         }\n-        memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count);\n-        for (i = 0;  i < _ssl_locks_count;  i++) {\n-            _ssl_locks[i] = PyThread_allocate_lock();\n-            if (_ssl_locks[i] == NULL) {\n-                unsigned int j;\n-                for (j = 0;  j < i;  j++) {\n-                    PyThread_free_lock(_ssl_locks[j]);\n-                }\n-                PyMem_Free(_ssl_locks);\n-                return 0;\n-            }\n-        }\n+        init_mutexes();\n         CRYPTO_set_locking_callback(_ssl_thread_locking_function);\n+#ifndef _WIN32\n+        pthread_atfork(NULL, NULL, &init_mutexes);\n+#endif\n     }\n     return 1;\n }\n", "issue": "Yet another building on PyPy(5.8) issue\nHi,\r\n\r\nI'm trying to build cryptography (tried both 1.9 and master) against PyPy 5.8beta0.\r\nI'm running on osx sierra.\r\n\r\nI've looked at several issues here, and have tried several suggested solutions, but none worked for me \ud83d\ude1e \r\n\r\nI'm trying to link against brew openssl.\r\n`brew list --versions openssl` -> `openssl 1.0.2j 1.0.2k 1.0.2l`\r\n\r\nI've built pypy's openssl module without any special problem\r\n\r\n```\r\nenv PYTHONPATH=/Users/omerba/Workspace/pypy \\                                                                                                                               \r\n       DYLD_LIBRARY_PATH=\"/Users/omerba/anaconda/lib\" \\\r\n       CFLAGS=\"-I/usr/local/opt/openssl/include\" \\\r\n       LDFLAGS=\"-L/usr/local/opt/openssl/lib\" \\\r\n       /Users/omerba/Workspace/pypy/pypy3-c pypy/tool/build_cffi_imports.py\r\n```\r\nBut when i try to build cryptography (in cryptography's dir):\r\n```\r\n env PYTHONPATH=/Users/omerba/Workspace/pypy \\                                                                                                                           \r\n       DYLD_LIBRARY_PATH=\"/Users/omerba/anaconda/lib\" \\\r\n       CFLAGS=\"-I/usr/local/opt/openssl/include\" \\\r\n       LDFLAGS=\"-L/usr/local/opt/openssl/lib\" \\\r\n       /Users/omerba/Workspace/pypy/pypy3-c setup.py install\r\n```\r\nI get:\r\n```\r\nrunning install\r\nrunning bdist_egg\r\nrunning egg_info\r\nwriting src/cryptography.egg-info/PKG-INFO\r\nwriting dependency_links to src/cryptography.egg-info/dependency_links.txt\r\nwriting entry points to src/cryptography.egg-info/entry_points.txt\r\nwriting requirements to src/cryptography.egg-info/requires.txt\r\nwriting top-level names to src/cryptography.egg-info/top_level.txt\r\nreading manifest file 'src/cryptography.egg-info/SOURCES.txt'\r\nreading manifest template 'MANIFEST.in'\r\nno previously-included directories found matching 'docs/_build'\r\nwarning: no previously-included files matching '*' found under directory 'vectors'\r\nwriting manifest file 'src/cryptography.egg-info/SOURCES.txt'\r\ninstalling library code to build/bdist.macosx-10.12-x86_64/egg\r\nrunning install_lib\r\nrunning build_py\r\nrunning build_ext\r\ngenerating cffi module 'build/temp.macosx-10.12-x86_64-3.5/_padding.c'\r\nalready up-to-date\r\ngenerating cffi module 'build/temp.macosx-10.12-x86_64-3.5/_constant_time.c'\r\nalready up-to-date\r\ngenerating cffi module 'build/temp.macosx-10.12-x86_64-3.5/_openssl.c'\r\nbuilding '_openssl' extension\r\ncc -pthread -DNDEBUG -O2 -I/usr/local/opt/openssl/include -fPIC -I/Users/omerba/Workspace/pypy/include -c build/temp.macosx-10.12-x86_64-3.5/_openssl.c -o build/temp.macosx-10.12-x86_64-3.5/build/temp.macosx-10.12-x86_64-3.5/_openssl.o\r\nbuild/temp.macosx-10.12-x86_64-3.5/_openssl.c:2503:9: warning: comparison of function 'getentropy' not equal to a null pointer is always true [-Wtautological-pointer-compare]\r\n    if (getentropy != NULL) {\r\n        ^~~~~~~~~~    ~~~~\r\nbuild/temp.macosx-10.12-x86_64-3.5/_openssl.c:2503:9: note: prefix with the address-of operator to silence this warning\r\n    if (getentropy != NULL) {\r\n        ^\r\n        &\r\nbuild/temp.macosx-10.12-x86_64-3.5/_openssl.c:3454:22: warning: comparison of constant 1152921504606846975 with expression of type 'unsigned int' is always false [-Wtautological-constant-out-of-range-compare]\r\n        _ssl_locks = PyMem_New(PyThread_type_lock, _ssl_locks_count);\r\n                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n/Users/omerba/Workspace/pypy/include/pymem.h:38:10: note: expanded from macro 'PyMem_New'\r\n  ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \\\r\n     ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\nbuild/temp.macosx-10.12-x86_64-3.5/_openssl.c:74852:1: warning: control reaches end of non-void function [-Wreturn-type]\r\n}\r\n^\r\n3 warnings generated.\r\ncc -pthread -shared -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include build/temp.macosx-10.12-x86_64-3.5/build/temp.macosx-10.12-x86_64-3.5/_openssl.o -lssl -lcrypto -o build/lib.macosx-10.12-x86_64-3.5/cryptography/hazmat/bindings/_openssl.pypy3-58-x86_64-darwin.so\r\nclang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]\r\nUndefined symbols for architecture x86_64:\r\n  \"_PyPyErr_NoMemory\", referenced from:\r\n      __setup_ssl_threads in _openssl.o\r\n  \"_PyPyMem_Free\", referenced from:\r\n      __setup_ssl_threads in _openssl.o\r\n  \"_PyPyMem_Malloc\", referenced from:\r\n      __setup_ssl_threads in _openssl.o\r\n  \"_PyPyThread_acquire_lock\", referenced from:\r\n      __ssl_thread_locking_function in _openssl.o\r\n  \"_PyPyThread_allocate_lock\", referenced from:\r\n      __setup_ssl_threads in _openssl.o\r\n  \"_PyPyThread_free_lock\", referenced from:\r\n      __setup_ssl_threads in _openssl.o\r\n  \"_PyPyThread_release_lock\", referenced from:\r\n      __ssl_thread_locking_function in _openssl.o\r\nld: symbol(s) not found for architecture x86_64\r\nclang: error: linker command failed with exit code 1 (use -v to see invocation)\r\nerror: command 'cc' failed with exit status 1\r\n```\r\n\r\nGoogling the issue only leads to [this](https://bitbucket.org/pypy/pypy/issues/2538/_ssl_buildpy-fails-on-macos-with-the-py35) exact same issue on the PyPy repo.\r\n\r\nBut the PyPy team seemed to have resolved it by making some changes to their cffi backend.\r\nI'll admit that I tried to shamelessly copy these changes to cryptography's _cffi_src dir - which made the package install successfully, but then when I actually tried to use it:\r\n\r\n`from cryptography.hazmat.backends import default_backend, openssl`\r\n\r\nit blows up.. \r\n\r\n```\r\nAttributeErrorTraceback (most recent call last)\r\n<ipython-input-5-cc19c86b2edb> in <module>()\r\n      1 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes\r\n----> 2 from cryptography.hazmat.backends import default_backend, openssl\r\n\r\n~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/backends/openssl/__init__.py in <module>()\r\n      5 from __future__ import absolute_import, division, print_function\r\n      6 \r\n----> 7 from cryptography.hazmat.backends.openssl.backend import backend\r\n      8 \r\n      9 \r\n\r\n~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/backends/openssl/backend.py in <module>()\r\n     47     _CertificateSigningRequest, _RevokedCertificate\r\n     48 )\r\n---> 49 from cryptography.hazmat.bindings.openssl import binding\r\n     50 from cryptography.hazmat.primitives import hashes, serialization\r\n     51 from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa\r\n\r\n~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in <module>()\r\n    154 # condition registering the OpenSSL locks. On Python 3.4+ the import lock\r\n    155 # is per module so this approach will not work.\r\n--> 156 Binding.init_static_locks()\r\n\r\n~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in init_static_locks(cls)\r\n    135     def init_static_locks(cls):\r\n    136         with cls._lock_init_lock:\r\n--> 137             cls._ensure_ffi_initialized()\r\n    138             # Use Python's implementation if available, importing _ssl triggers\r\n    139             # the setup for this.\r\n\r\n~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in _ensure_ffi_initialized(cls)\r\n    122         with cls._init_lock:\r\n    123             if not cls._lib_loaded:\r\n--> 124                 cls.lib = build_conditional_library(lib, CONDITIONAL_NAMES)\r\n    125                 cls._lib_loaded = True\r\n    126                 # initialize the SSL library\r\n\r\n~/Workspace/pypy/site-packages/cryptography-1.9-py3.5-macosx-10.12-x86_64.egg/cryptography/hazmat/bindings/openssl/binding.py in build_conditional_library(lib, conditional_names)\r\n     82     excluded_names = set()\r\n     83     for condition, names in conditional_names.items():\r\n---> 84         if not getattr(lib, condition):\r\n     85             excluded_names |= set(names)\r\n     86 \r\n\r\nAttributeError: cffi library 'cryptography.hazmat.bindings._openssl' has no function, constant or global variable named 'Cryptography_HAS_DTLS'\r\n```\r\n\r\nThanks for your work guys!\n", "before_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nINCLUDES = \"\"\"\n#include <openssl/ssl.h>\n#include <openssl/x509.h>\n#include <openssl/x509_vfy.h>\n#include <openssl/crypto.h>\n\n#include <pythread.h>\n\"\"\"\n\nTYPES = \"\"\"\ntypedef struct {\n    char *password;\n    int length;\n    int called;\n    int error;\n    int maxsize;\n} CRYPTOGRAPHY_PASSWORD_DATA;\n\"\"\"\n\nFUNCTIONS = \"\"\"\nint _setup_ssl_threads(void);\nint Cryptography_pem_password_cb(char *, int, int, void *);\n\"\"\"\n\nMACROS = \"\"\"\n\"\"\"\n\nCUSTOMIZATIONS = \"\"\"\n/* This code is derived from the locking code found in the Python _ssl module's\n   locking callback for OpenSSL.\n\n   Copyright 2001-2016 Python Software Foundation; All Rights Reserved.\n*/\n\nstatic unsigned int _ssl_locks_count = 0;\nstatic PyThread_type_lock *_ssl_locks = NULL;\n\nstatic void _ssl_thread_locking_function(int mode, int n, const char *file,\n                                         int line) {\n    /* this function is needed to perform locking on shared data\n       structures. (Note that OpenSSL uses a number of global data\n       structures that will be implicitly shared whenever multiple\n       threads use OpenSSL.) Multi-threaded applications will\n       crash at random if it is not set.\n\n       locking_function() must be able to handle up to\n       CRYPTO_num_locks() different mutex locks. It sets the n-th\n       lock if mode & CRYPTO_LOCK, and releases it otherwise.\n\n       file and line are the file number of the function setting the\n       lock. They can be useful for debugging.\n    */\n\n    if ((_ssl_locks == NULL) ||\n        (n < 0) || ((unsigned)n >= _ssl_locks_count)) {\n        return;\n    }\n\n    if (mode & CRYPTO_LOCK) {\n        PyThread_acquire_lock(_ssl_locks[n], 1);\n    } else {\n        PyThread_release_lock(_ssl_locks[n]);\n    }\n}\n\nint _setup_ssl_threads(void) {\n    unsigned int i;\n\n    if (_ssl_locks == NULL) {\n        _ssl_locks_count = CRYPTO_num_locks();\n        _ssl_locks = PyMem_New(PyThread_type_lock, _ssl_locks_count);\n        if (_ssl_locks == NULL) {\n            PyErr_NoMemory();\n            return 0;\n        }\n        memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count);\n        for (i = 0;  i < _ssl_locks_count;  i++) {\n            _ssl_locks[i] = PyThread_allocate_lock();\n            if (_ssl_locks[i] == NULL) {\n                unsigned int j;\n                for (j = 0;  j < i;  j++) {\n                    PyThread_free_lock(_ssl_locks[j]);\n                }\n                PyMem_Free(_ssl_locks);\n                return 0;\n            }\n        }\n        CRYPTO_set_locking_callback(_ssl_thread_locking_function);\n    }\n    return 1;\n}\n\ntypedef struct {\n    char *password;\n    int length;\n    int called;\n    int error;\n    int maxsize;\n} CRYPTOGRAPHY_PASSWORD_DATA;\n\nint Cryptography_pem_password_cb(char *buf, int size,\n                                  int rwflag, void *userdata) {\n    /* The password cb is only invoked if OpenSSL decides the private\n       key is encrypted. So this path only occurs if it needs a password */\n    CRYPTOGRAPHY_PASSWORD_DATA *st = (CRYPTOGRAPHY_PASSWORD_DATA *)userdata;\n    st->called += 1;\n    st->maxsize = size;\n    if (st->length == 0) {\n        st->error = -1;\n        return 0;\n    } else if (st->length < size) {\n        memcpy(buf, st->password, st->length);\n        return st->length;\n    } else {\n        st->error = -2;\n        return 0;\n    }\n}\n\"\"\"\n", "path": "src/_cffi_src/openssl/callbacks.py"}], "after_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nINCLUDES = \"\"\"\n#include <openssl/ssl.h>\n#include <openssl/x509.h>\n#include <openssl/x509_vfy.h>\n#include <openssl/crypto.h>\n\n#ifdef _WIN32\n#include <Windows.h>\n#else\n#include <stdio.h>\n#include <stdlib.h>\n#include <pthread.h>\n#endif\n\"\"\"\n\nTYPES = \"\"\"\ntypedef struct {\n    char *password;\n    int length;\n    int called;\n    int error;\n    int maxsize;\n} CRYPTOGRAPHY_PASSWORD_DATA;\n\"\"\"\n\nFUNCTIONS = \"\"\"\nint _setup_ssl_threads(void);\nint Cryptography_pem_password_cb(char *, int, int, void *);\n\"\"\"\n\nMACROS = \"\"\"\n\"\"\"\n\nCUSTOMIZATIONS = \"\"\"\n/* This code is derived from the locking code found in the Python _ssl module's\n   locking callback for OpenSSL.\n\n   Copyright 2001-2016 Python Software Foundation; All Rights Reserved.\n\n   It has been subsequently modified to use cross platform locking without\n   using CPython APIs by Armin Rigo of the PyPy project.\n*/\n\n#ifdef _WIN32\ntypedef CRITICAL_SECTION Cryptography_mutex;\nstatic __inline void cryptography_mutex_init(Cryptography_mutex *mutex) {\n    InitializeCriticalSection(mutex);\n}\nstatic __inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {\n    EnterCriticalSection(mutex);\n}\nstatic __inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {\n    LeaveCriticalSection(mutex);\n}\n#else\ntypedef pthread_mutex_t Cryptography_mutex;\n#define ASSERT_STATUS(call)                                             \\\n    if ((call) != 0) {                                                  \\\n        perror(\"Fatal error in callback initialization: \" #call);       \\\n        abort();                                                        \\\n    }\nstatic inline void cryptography_mutex_init(Cryptography_mutex *mutex) {\n#if !defined(pthread_mutexattr_default)\n#  define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)\n#endif\n    ASSERT_STATUS(pthread_mutex_init(mutex, pthread_mutexattr_default));\n}\nstatic inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {\n    ASSERT_STATUS(pthread_mutex_lock(mutex));\n}\nstatic inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {\n    ASSERT_STATUS(pthread_mutex_unlock(mutex));\n}\n#endif\n\n\n\nstatic unsigned int _ssl_locks_count = 0;\nstatic Cryptography_mutex *_ssl_locks = NULL;\n\nstatic void _ssl_thread_locking_function(int mode, int n, const char *file,\n                                         int line) {\n    /* this function is needed to perform locking on shared data\n       structures. (Note that OpenSSL uses a number of global data\n       structures that will be implicitly shared whenever multiple\n       threads use OpenSSL.) Multi-threaded applications will\n       crash at random if it is not set.\n\n       locking_function() must be able to handle up to\n       CRYPTO_num_locks() different mutex locks. It sets the n-th\n       lock if mode & CRYPTO_LOCK, and releases it otherwise.\n\n       file and line are the file number of the function setting the\n       lock. They can be useful for debugging.\n    */\n\n    if ((_ssl_locks == NULL) ||\n        (n < 0) || ((unsigned)n >= _ssl_locks_count)) {\n        return;\n    }\n\n    if (mode & CRYPTO_LOCK) {\n        cryptography_mutex_lock(_ssl_locks + n);\n    } else {\n        cryptography_mutex_unlock(_ssl_locks + n);\n    }\n}\n\nstatic void init_mutexes(void) {\n    int i;\n    for (i = 0; i < _ssl_locks_count; i++) {\n        cryptography_mutex_init(_ssl_locks + i);\n    }\n}\n\n\nint _setup_ssl_threads(void) {\n    if (_ssl_locks == NULL) {\n        _ssl_locks_count = CRYPTO_num_locks();\n        _ssl_locks = calloc(_ssl_locks_count, sizeof(Cryptography_mutex));\n        if (_ssl_locks == NULL) {\n            return 0;\n        }\n        init_mutexes();\n        CRYPTO_set_locking_callback(_ssl_thread_locking_function);\n#ifndef _WIN32\n        pthread_atfork(NULL, NULL, &init_mutexes);\n#endif\n    }\n    return 1;\n}\n\ntypedef struct {\n    char *password;\n    int length;\n    int called;\n    int error;\n    int maxsize;\n} CRYPTOGRAPHY_PASSWORD_DATA;\n\nint Cryptography_pem_password_cb(char *buf, int size,\n                                  int rwflag, void *userdata) {\n    /* The password cb is only invoked if OpenSSL decides the private\n       key is encrypted. So this path only occurs if it needs a password */\n    CRYPTOGRAPHY_PASSWORD_DATA *st = (CRYPTOGRAPHY_PASSWORD_DATA *)userdata;\n    st->called += 1;\n    st->maxsize = size;\n    if (st->length == 0) {\n        st->error = -1;\n        return 0;\n    } else if (st->length < size) {\n        memcpy(buf, st->password, st->length);\n        return st->length;\n    } else {\n        st->error = -2;\n        return 0;\n    }\n}\n\"\"\"\n", "path": "src/_cffi_src/openssl/callbacks.py"}]}