aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2013-09-13 11:43:57 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2013-09-13 11:43:57 -0500
commit51c65b9a0b31740a5f87e8ef74f8a103d583e8ee (patch)
treeabdd63374234bbc95daf043f7b7d92b9138d8f02
parentbc1994db98320fb82155df1f1da619177cc59979 (diff)
downloadcryptography-51c65b9a0b31740a5f87e8ef74f8a103d583e8ee.tar.gz
cryptography-51c65b9a0b31740a5f87e8ef74f8a103d583e8ee.tar.bz2
cryptography-51c65b9a0b31740a5f87e8ef74f8a103d583e8ee.zip
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
-rw-r--r--AUTHORS.rst1
-rw-r--r--cryptography/bindings/openssl/api.py71
-rw-r--r--cryptography/bindings/openssl/cffi/__init__.py0
-rw-r--r--cryptography/bindings/openssl/cffi/evp.py39
-rw-r--r--cryptography/bindings/openssl/cffi/opensslv.py23
5 files changed, 104 insertions, 30 deletions
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 <christian@python.org>
* Paul Kehrer <paul.l.kehrer@gmail.com>
+* Jarret Raim <jarito@gmail.com>
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 <openssl/evp.h>
- #include <openssl/opensslv.h>
- """)
+ 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
--- /dev/null
+++ b/cryptography/bindings/openssl/cffi/__init__.py
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 <openssl/evp.h>"
+]
+
+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 <openssl/opensslv.h>"
+]
+
+TYPES = [
+ "static char *const OPENSSL_VERSION_TEXT;"
+]
+
+FUNCTIONS = [
+]