From 51c65b9a0b31740a5f87e8ef74f8a103d583e8ee Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 13 Sep 2013 11:43:57 -0500 Subject: API refactor to separate cffi declarations * Moved to structure similar to OpenTLS * api modified to load the cffi modules * cffi verify methods broken out * Jarret Raim and I pair programmed this --- AUTHORS.rst | 1 + cryptography/bindings/openssl/api.py | 71 +++++++++++++++----------- cryptography/bindings/openssl/cffi/__init__.py | 0 cryptography/bindings/openssl/cffi/evp.py | 39 ++++++++++++++ cryptography/bindings/openssl/cffi/opensslv.py | 23 +++++++++ 5 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 cryptography/bindings/openssl/cffi/__init__.py create mode 100644 cryptography/bindings/openssl/cffi/evp.py create mode 100644 cryptography/bindings/openssl/cffi/opensslv.py diff --git a/AUTHORS.rst b/AUTHORS.rst index 9d499dc7..1aa37e48 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -7,4 +7,5 @@ AUTHORS * Laurens Van Houtven <_@lvh.io> * Christian Heimes * Paul Kehrer +* Jarret Raim diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py index 917c1846..f2369406 100644 --- a/cryptography/bindings/openssl/api.py +++ b/cryptography/bindings/openssl/api.py @@ -22,40 +22,51 @@ class API(object): """ OpenSSL API wrapper. """ + # TODO: is there a way to enumerate the files in the cffi module + # rather than hardcode them? + _modules = [ + 'evp', + 'opensslv', + ] def __init__(self): - ffi = cffi.FFI() - self._populate_ffi(ffi) - self._ffi = ffi - self._lib = ffi.verify(""" - #include - #include - """) + self._ffi = cffi.FFI() + self.INCLUDES, self.TYPES, self.FUNCTIONS = [], [], [] + self._import() + self._define() + self._verify() + self._lib.OpenSSL_add_all_algorithms() - def _populate_ffi(self, ffi): - ffi.cdef(""" - typedef struct { - ...; - } EVP_CIPHER_CTX; - typedef ... EVP_CIPHER; - typedef ... ENGINE; - - static char *const OPENSSL_VERSION_TEXT; - - void OpenSSL_add_all_algorithms(); - - const EVP_CIPHER *EVP_get_cipherbyname(const char *); - int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, - ENGINE *, unsigned char *, unsigned char *); - int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int); - int EVP_EncryptUpdate(EVP_CIPHER_CTX *, unsigned char *, int *, - unsigned char *, int); - int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *); - int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *); - const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *); - int EVP_CIPHER_block_size(const EVP_CIPHER *); - """) + def _import(self): + "import all library definitions" + for name in self._modules: + module = __import__('cryptography.bindings.openssl.cffi.' + name, + fromlist=['*']) + self._import_definitions(module, 'INCLUDES') + self._import_definitions(module, 'TYPES') + self._import_definitions(module, 'FUNCTIONS') + + def _import_definitions(self, module, name): + "import defintions named definitions from module" + container = getattr(self, name) + for definition in getattr(module, name, ()): + if definition not in container: + container.append(definition) + + def _define(self): + "parse function definitions" + for typedef in self.TYPES: + self._ffi.cdef(typedef) + for function in self.FUNCTIONS: + self._ffi.cdef(function) + + def _verify(self): + "load openssl, create function attributes" + self._lib = self._ffi.verify( + source="\n".join(self.INCLUDES), + libraries=['crypto'] + ) def openssl_version_text(self): """ diff --git a/cryptography/bindings/openssl/cffi/__init__.py b/cryptography/bindings/openssl/cffi/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cryptography/bindings/openssl/cffi/evp.py b/cryptography/bindings/openssl/cffi/evp.py new file mode 100644 index 00000000..be72a265 --- /dev/null +++ b/cryptography/bindings/openssl/cffi/evp.py @@ -0,0 +1,39 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +INCLUDES = [ + "#include " +] + +TEARDOWN = [ +] + +TYPES = [ + "typedef struct { ...; } EVP_CIPHER_CTX;", + "typedef ... EVP_CIPHER;", + "typedef ... ENGINE;", +] + +FUNCTIONS = [ + "void OpenSSL_add_all_algorithms();", + "const EVP_CIPHER *EVP_get_cipherbyname(const char *);", + "int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, \ + ENGINE *, unsigned char *, unsigned char *);", + "int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);", + "int EVP_EncryptUpdate(EVP_CIPHER_CTX *, unsigned char *, int *, \ + unsigned char *, int);", + "int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *);", + "int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);", + "const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *);", + "int EVP_CIPHER_block_size(const EVP_CIPHER *);", +] diff --git a/cryptography/bindings/openssl/cffi/opensslv.py b/cryptography/bindings/openssl/cffi/opensslv.py new file mode 100644 index 00000000..33212b89 --- /dev/null +++ b/cryptography/bindings/openssl/cffi/opensslv.py @@ -0,0 +1,23 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +INCLUDES = [ + "#include " +] + +TYPES = [ + "static char *const OPENSSL_VERSION_TEXT;" +] + +FUNCTIONS = [ +] -- cgit v1.2.3 From 39a45c5741ea5c62af2fd530d8fb3ebc2055116a Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 13 Sep 2013 20:58:09 -0500 Subject: Various PR review fixes * Changed some single quotes to double quotes * Moved the files in the cffi package back into the openssl package * evp, opensslv * Changed attrs from ALL CAPS * Fixed up docstrings * Stopped using fromlist=['*'] * No fallback on definition imports. You must supply includes, types, functions * Change includes, types, functions to be strings rather than lists for now * Removed teardown since we're not using it right now --- cryptography/bindings/openssl/api.py | 39 +++++++++++++------------- cryptography/bindings/openssl/cffi/__init__.py | 0 cryptography/bindings/openssl/cffi/evp.py | 39 -------------------------- cryptography/bindings/openssl/cffi/opensslv.py | 23 --------------- cryptography/bindings/openssl/evp.py | 36 ++++++++++++++++++++++++ cryptography/bindings/openssl/opensslv.py | 23 +++++++++++++++ 6 files changed, 78 insertions(+), 82 deletions(-) delete mode 100644 cryptography/bindings/openssl/cffi/__init__.py delete mode 100644 cryptography/bindings/openssl/cffi/evp.py delete mode 100644 cryptography/bindings/openssl/cffi/opensslv.py create mode 100644 cryptography/bindings/openssl/evp.py create mode 100644 cryptography/bindings/openssl/opensslv.py diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py index f2369406..c5da9fb1 100644 --- a/cryptography/bindings/openssl/api.py +++ b/cryptography/bindings/openssl/api.py @@ -16,22 +16,21 @@ from __future__ import absolute_import, division, print_function from cryptography.primitives import interfaces import cffi +import sys class API(object): """ OpenSSL API wrapper. """ - # TODO: is there a way to enumerate the files in the cffi module - # rather than hardcode them? _modules = [ - 'evp', - 'opensslv', + "evp", + "opensslv", ] def __init__(self): self._ffi = cffi.FFI() - self.INCLUDES, self.TYPES, self.FUNCTIONS = [], [], [] + self.includes, self.types, self.functions = [], [], [] self._import() self._define() self._verify() @@ -39,32 +38,32 @@ class API(object): self._lib.OpenSSL_add_all_algorithms() def _import(self): - "import all library definitions" + """ + Import all library definitions + """ for name in self._modules: - module = __import__('cryptography.bindings.openssl.cffi.' + name, - fromlist=['*']) - self._import_definitions(module, 'INCLUDES') - self._import_definitions(module, 'TYPES') - self._import_definitions(module, 'FUNCTIONS') + __import__('cryptography.bindings.openssl.' + name) + module = sys.modules['cryptography.bindings.openssl.' + name] + self._import_definitions(module, 'includes') + self._import_definitions(module, 'types') + self._import_definitions(module, 'functions') def _import_definitions(self, module, name): - "import defintions named definitions from module" + """ + Import definitions named definitions from module + """ container = getattr(self, name) - for definition in getattr(module, name, ()): - if definition not in container: - container.append(definition) + container.append(getattr(module, name)) def _define(self): - "parse function definitions" - for typedef in self.TYPES: + for typedef in self.types: self._ffi.cdef(typedef) - for function in self.FUNCTIONS: + for function in self.functions: self._ffi.cdef(function) def _verify(self): - "load openssl, create function attributes" self._lib = self._ffi.verify( - source="\n".join(self.INCLUDES), + source="\n".join(self.includes), libraries=['crypto'] ) diff --git a/cryptography/bindings/openssl/cffi/__init__.py b/cryptography/bindings/openssl/cffi/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/cryptography/bindings/openssl/cffi/evp.py b/cryptography/bindings/openssl/cffi/evp.py deleted file mode 100644 index be72a265..00000000 --- a/cryptography/bindings/openssl/cffi/evp.py +++ /dev/null @@ -1,39 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -INCLUDES = [ - "#include " -] - -TEARDOWN = [ -] - -TYPES = [ - "typedef struct { ...; } EVP_CIPHER_CTX;", - "typedef ... EVP_CIPHER;", - "typedef ... ENGINE;", -] - -FUNCTIONS = [ - "void OpenSSL_add_all_algorithms();", - "const EVP_CIPHER *EVP_get_cipherbyname(const char *);", - "int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, \ - ENGINE *, unsigned char *, unsigned char *);", - "int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);", - "int EVP_EncryptUpdate(EVP_CIPHER_CTX *, unsigned char *, int *, \ - unsigned char *, int);", - "int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *);", - "int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);", - "const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *);", - "int EVP_CIPHER_block_size(const EVP_CIPHER *);", -] diff --git a/cryptography/bindings/openssl/cffi/opensslv.py b/cryptography/bindings/openssl/cffi/opensslv.py deleted file mode 100644 index 33212b89..00000000 --- a/cryptography/bindings/openssl/cffi/opensslv.py +++ /dev/null @@ -1,23 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -INCLUDES = [ - "#include " -] - -TYPES = [ - "static char *const OPENSSL_VERSION_TEXT;" -] - -FUNCTIONS = [ -] diff --git a/cryptography/bindings/openssl/evp.py b/cryptography/bindings/openssl/evp.py new file mode 100644 index 00000000..0d969cf8 --- /dev/null +++ b/cryptography/bindings/openssl/evp.py @@ -0,0 +1,36 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +includes = """ + #include +""" + +types = """ + typedef struct { ...; } EVP_CIPHER_CTX; + typedef ... EVP_CIPHER; + typedef ... ENGINE; +""" + +functions = """ + void OpenSSL_add_all_algorithms(); + const EVP_CIPHER *EVP_get_cipherbyname(const char *); + int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, + ENGINE *, unsigned char *, unsigned char *); + int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int); + int EVP_EncryptUpdate(EVP_CIPHER_CTX *, unsigned char *, int *, + unsigned char *, int); + int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *); + int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *); + const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *); + int EVP_CIPHER_block_size(const EVP_CIPHER *); +""" diff --git a/cryptography/bindings/openssl/opensslv.py b/cryptography/bindings/openssl/opensslv.py new file mode 100644 index 00000000..ace7bded --- /dev/null +++ b/cryptography/bindings/openssl/opensslv.py @@ -0,0 +1,23 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +includes = """ + #include +""" + +types = """ + static char *const OPENSSL_VERSION_TEXT; +""" + + +functions = "" -- cgit v1.2.3 From e1b2d3cb863e5d157da1c1766dc3f86ac3bb29e9 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 13 Sep 2013 21:26:21 -0500 Subject: remove import function, rename attrs to upper in modules --- cryptography/bindings/openssl/api.py | 13 +++---------- cryptography/bindings/openssl/evp.py | 6 +++--- cryptography/bindings/openssl/opensslv.py | 6 +++--- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py index c5da9fb1..bc2b4ae4 100644 --- a/cryptography/bindings/openssl/api.py +++ b/cryptography/bindings/openssl/api.py @@ -44,16 +44,9 @@ class API(object): for name in self._modules: __import__('cryptography.bindings.openssl.' + name) module = sys.modules['cryptography.bindings.openssl.' + name] - self._import_definitions(module, 'includes') - self._import_definitions(module, 'types') - self._import_definitions(module, 'functions') - - def _import_definitions(self, module, name): - """ - Import definitions named definitions from module - """ - container = getattr(self, name) - container.append(getattr(module, name)) + self.includes.append(module.INCLUDES) + self.types.append(module.TYPES) + self.functions.append(module.FUNCTIONS) def _define(self): for typedef in self.types: diff --git a/cryptography/bindings/openssl/evp.py b/cryptography/bindings/openssl/evp.py index 0d969cf8..7795e935 100644 --- a/cryptography/bindings/openssl/evp.py +++ b/cryptography/bindings/openssl/evp.py @@ -11,17 +11,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -includes = """ +INCLUDES = """ #include """ -types = """ +TYPES = """ typedef struct { ...; } EVP_CIPHER_CTX; typedef ... EVP_CIPHER; typedef ... ENGINE; """ -functions = """ +FUNCTIONS = """ void OpenSSL_add_all_algorithms(); const EVP_CIPHER *EVP_get_cipherbyname(const char *); int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, diff --git a/cryptography/bindings/openssl/opensslv.py b/cryptography/bindings/openssl/opensslv.py index ace7bded..a8f0b9b8 100644 --- a/cryptography/bindings/openssl/opensslv.py +++ b/cryptography/bindings/openssl/opensslv.py @@ -11,13 +11,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -includes = """ +INCLUDES = """ #include """ -types = """ +TYPES = """ static char *const OPENSSL_VERSION_TEXT; """ -functions = "" +FUNCTIONS = "" -- cgit v1.2.3