Skip to content

Commit 68b3b1e

Browse files
committed
convert to cffi 1.0 precompile system
1 parent ca820de commit 68b3b1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+251
-362
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ _build/
33
build/
44
dist/
55
htmlcov/
6-
src/cryptography/_Cryptography_cffi_*
6+
*.so
77
.tox/
88
.cache/
99
.coverage

CHANGELOG.rst

+4

MANIFEST.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ include LICENSE.BSD
77
include README.rst
88

99
recursive-include docs *
10-
recursive-include src/cryptography/hazmat/primitives/src *.c *.h
11-
recursive-include src/cryptography/hazmat/bindings/openssl/src *.c *.h
10+
recursive-include src/_cffi_src *.py *.c *.h
1211
prune docs/_build
1312
recursive-include tests *.py
1413
recursive-exclude vectors *

setup.py

+19-56
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
requirements.append("ipaddress")
4646

4747
if platform.python_implementation() != "PyPy":
48-
requirements.append("cffi>=0.8")
48+
requirements.append("cffi>=1.1.0")
4949

5050
# If you add a new dep here you probably need to add it in the tox.ini as well
5151
test_requirements = [
@@ -75,52 +75,6 @@ def cc_is_available():
7575
)
7676

7777

78-
def get_ext_modules():
79-
from cryptography.hazmat.bindings.commoncrypto.binding import (
80-
Binding as CommonCryptoBinding
81-
)
82-
from cryptography.hazmat.bindings.openssl.binding import (
83-
Binding as OpenSSLBinding
84-
)
85-
from cryptography.hazmat.primitives import constant_time, padding
86-
87-
ext_modules = [
88-
OpenSSLBinding.ffi.verifier.get_extension(),
89-
constant_time._ffi.verifier.get_extension(),
90-
padding._ffi.verifier.get_extension()
91-
]
92-
if cc_is_available():
93-
ext_modules.append(CommonCryptoBinding.ffi.verifier.get_extension())
94-
return ext_modules
95-
96-
97-
class CFFIBuild(build):
98-
"""
99-
This class exists, instead of just providing ``ext_modules=[...]`` directly
100-
in ``setup()`` because importing cryptography requires we have several
101-
packages installed first.
102-
103-
By doing the imports here we ensure that packages listed in
104-
``setup_requires`` are already installed.
105-
"""
106-
107-
def finalize_options(self):
108-
self.distribution.ext_modules = get_ext_modules()
109-
build.finalize_options(self)
110-
111-
112-
class CFFIInstall(install):
113-
"""
114-
As a consequence of CFFIBuild and it's late addition of ext_modules, we
115-
need the equivalent for the ``install`` command to install into platlib
116-
install-dir rather than purelib.
117-
"""
118-
119-
def finalize_options(self):
120-
self.distribution.ext_modules = get_ext_modules()
121-
install.finalize_options(self)
122-
123-
12478
class PyTest(test):
12579
def finalize_options(self):
12680
test.finalize_options(self)
@@ -234,19 +188,26 @@ def argument_without_setup_requirements(argv, i):
234188
for i in range(1, len(argv))):
235189
return {
236190
"cmdclass": {
237-
"build": DummyCFFIBuild,
238-
"install": DummyCFFIInstall,
191+
"build": DummyBuild,
192+
"install": DummyInstall,
239193
"test": DummyPyTest,
240194
}
241195
}
242196
else:
197+
cffi_modules = [
198+
"src/_cffi_src/build_openssl.py:ffi",
199+
"src/_cffi_src/build_constant_time.py:ffi",
200+
"src/_cffi_src/build_padding.py:ffi",
201+
]
202+
if cc_is_available():
203+
cffi_modules.append("src/_cffi_src/build_commoncrypto.py:ffi")
204+
243205
return {
244206
"setup_requires": requirements,
245207
"cmdclass": {
246-
"build": CFFIBuild,
247-
"install": CFFIInstall,
248208
"test": PyTest,
249-
}
209+
},
210+
"cffi_modules": cffi_modules
250211
}
251212

252213

@@ -255,7 +216,7 @@ def argument_without_setup_requirements(argv, i):
255216
"free command or option.")
256217

257218

258-
class DummyCFFIBuild(build):
219+
class DummyBuild(build):
259220
"""
260221
This class makes it very obvious when ``keywords_with_side_effects()`` has
261222
incorrectly interpreted the command line arguments to ``setup.py build`` as
@@ -266,7 +227,7 @@ def run(self):
266227
raise RuntimeError(setup_requires_error)
267228

268229

269-
class DummyCFFIInstall(install):
230+
class DummyInstall(install):
270231
"""
271232
This class makes it very obvious when ``keywords_with_side_effects()`` has
272233
incorrectly interpreted the command line arguments to ``setup.py install``
@@ -327,15 +288,17 @@ def run_tests(self):
327288
],
328289

329290
package_dir={"": "src"},
330-
packages=find_packages(where="src", exclude=["tests", "tests.*"]),
291+
packages=find_packages(
292+
where="src", exclude=["_cffi_src", "_cffi_src.*", "tests", "tests.*"]
293+
),
331294
include_package_data=True,
332295

333296
install_requires=requirements,
334297
tests_require=test_requirements,
335298

336299
# for cffi
337300
zip_safe=False,
338-
ext_package="cryptography",
301+
ext_package="cryptography.hazmat.bindings",
339302
entry_points={
340303
"cryptography.backends": backends,
341304
},

src/_cffi_src/__init__.py

Whitespace-only changes.

src/_cffi_src/build_commoncrypto.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
from _cffi_src.utils import build_ffi_for_binding
8+
9+
10+
ffi = build_ffi_for_binding(
11+
module_name="_commoncrypto",
12+
module_prefix="_cffi_src.commoncrypto.",
13+
modules=[
14+
"cf",
15+
"common_digest",
16+
"common_hmac",
17+
"common_key_derivation",
18+
"common_cryptor",
19+
"common_symmetric_key_wrap",
20+
"secimport",
21+
"secitem",
22+
"seckey",
23+
"seckeychain",
24+
"sectransform",
25+
],
26+
extra_link_args=[
27+
"-framework", "Security", "-framework", "CoreFoundation"
28+
],
29+
)

src/_cffi_src/build_constant_time.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
import os
8+
9+
from _cffi_src.utils import build_ffi
10+
11+
12+
with open(os.path.join(
13+
os.path.dirname(__file__), "hazmat_src/constant_time.h"
14+
)) as f:
15+
types = f.read()
16+
17+
with open(os.path.join(
18+
os.path.dirname(__file__), "hazmat_src/constant_time.c"
19+
)) as f:
20+
functions = f.read()
21+
22+
ffi = build_ffi(
23+
module_name="_constant_time",
24+
cdef_source=types,
25+
verify_source=functions
26+
)

src/_cffi_src/build_openssl.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
import os
8+
import sys
9+
10+
from _cffi_src.utils import (
11+
build_ffi_for_binding
12+
)
13+
14+
15+
def _get_openssl_libraries(platform):
16+
# OpenSSL goes by a different library name on different operating systems.
17+
if platform != "win32":
18+
# In some circumstances, the order in which these libs are
19+
# specified on the linker command-line is significant;
20+
# libssl must come before libcrypto
21+
# (http://marc.info/?l=openssl-users&m=135361825921871)
22+
return ["ssl", "crypto"]
23+
else:
24+
link_type = os.environ.get("PYCA_WINDOWS_LINK_TYPE", "static")
25+
return _get_openssl_windows_libraries(link_type)
26+
27+
28+
def _get_openssl_windows_libraries(link_type):
29+
if link_type == "dynamic":
30+
return ["libeay32", "ssleay32", "advapi32"]
31+
elif link_type == "static" or link_type == "":
32+
return ["libeay32mt", "ssleay32mt", "advapi32",
33+
"crypt32", "gdi32", "user32", "ws2_32"]
34+
else:
35+
raise ValueError(
36+
"PYCA_WINDOWS_LINK_TYPE must be 'static' or 'dynamic'"
37+
)
38+
39+
40+
_OSX_PRE_INCLUDE = """
41+
#ifdef __APPLE__
42+
#include <AvailabilityMacros.h>
43+
#define __ORIG_DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER \
44+
DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
45+
#undef DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
46+
#define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
47+
#endif
48+
"""
49+
50+
_OSX_POST_INCLUDE = """
51+
#ifdef __APPLE__
52+
#undef DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
53+
#define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER \
54+
__ORIG_DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
55+
#endif
56+
"""
57+
58+
59+
ffi = build_ffi_for_binding(
60+
module_name="_openssl",
61+
module_prefix="_cffi_src.openssl.",
62+
modules=[
63+
"aes",
64+
"asn1",
65+
"bignum",
66+
"bio",
67+
"cmac",
68+
"cms",
69+
"conf",
70+
"crypto",
71+
"dh",
72+
"dsa",
73+
"ec",
74+
"ecdh",
75+
"ecdsa",
76+
"engine",
77+
"err",
78+
"evp",
79+
"hmac",
80+
"nid",
81+
"objects",
82+
"opensslv",
83+
"osrandom_engine",
84+
"pem",
85+
"pkcs7",
86+
"pkcs12",
87+
"rand",
88+
"rsa",
89+
"ssl",
90+
"x509",
91+
"x509name",
92+
"x509v3",
93+
"x509_vfy"
94+
],
95+
pre_include=_OSX_PRE_INCLUDE,
96+
post_include=_OSX_POST_INCLUDE,
97+
libraries=_get_openssl_libraries(sys.platform)
98+
)

src/_cffi_src/build_padding.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
import os
8+
9+
from _cffi_src.utils import build_ffi
10+
11+
12+
with open(os.path.join(
13+
os.path.dirname(__file__), "hazmat_src/padding.h"
14+
)) as f:
15+
types = f.read()
16+
17+
with open(os.path.join(
18+
os.path.dirname(__file__), "hazmat_src/padding.c"
19+
)) as f:
20+
functions = f.read()
21+
22+
ffi = build_ffi(
23+
module_name="_padding",
24+
cdef_source=types,
25+
verify_source=functions
26+
)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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

src/_cffi_src/openssl/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)