summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-12-11 14:44:32 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-12-15 15:49:49 -0600
commit68481c3e78d08b7defdd716b72b7563fb0ee5469 (patch)
tree34f32a3ae7b7f2a98dc800909636cf515a7e8759
parentd9fc7252f9470f6f9f6c05047e2fcf1c5c34667a (diff)
downloadcryptography-68481c3e78d08b7defdd716b72b7563fb0ee5469.tar.gz
move mem_bio creation/reading to backend
-rw-r--r--src/cryptography/hazmat/backends/openssl/backend.py22
-rw-r--r--src/cryptography/hazmat/backends/openssl/x509.py20
2 files changed, 24 insertions, 18 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/backend.py b/src/cryptography/hazmat/backends/openssl/backend.py
index 19d149b51..10341fa21 100644
--- a/src/cryptography/hazmat/backends/openssl/backend.py
+++ b/src/cryptography/hazmat/backends/openssl/backend.py
@@ -447,6 +447,28 @@ class Backend(object):
return _MemoryBIO(self._ffi.gc(bio, self._lib.BIO_free), data_char_p)
+ def _create_mem_bio(self):
+ """
+ Creates an empty memory BIO.
+ """
+ bio_method = self._lib.BIO_s_mem()
+ assert bio_method != self._ffi.NULL
+ bio = self._lib.BIO_new(bio_method)
+ assert bio != self._ffi.NULL
+ bio = self._ffi.gc(bio, self._lib.BIO_free)
+ return bio
+
+ def _read_mem_bio(self, bio):
+ """
+ Reads a memory BIO. This only works on memory BIOs.
+ """
+ buf = self._ffi.new("char **")
+ buf_len = self._lib.BIO_get_mem_data(bio, buf)
+ assert buf_len > 0
+ assert buf[0] != self._ffi.NULL
+ bio_data = self._ffi.buffer(buf[0], buf_len)[:]
+ return bio_data
+
def _evp_pkey_to_private_key(self, evp_pkey):
"""
Return the appropriate type of PrivateKey given an evp_pkey cdata
diff --git a/src/cryptography/hazmat/backends/openssl/x509.py b/src/cryptography/hazmat/backends/openssl/x509.py
index a348630f0..532785acf 100644
--- a/src/cryptography/hazmat/backends/openssl/x509.py
+++ b/src/cryptography/hazmat/backends/openssl/x509.py
@@ -25,30 +25,14 @@ class _X509Certificate(object):
self._backend = backend
self._x509 = x509
- def _create_bio(self):
- bio_method = self._backend._lib.BIO_s_mem()
- assert bio_method != self._backend._ffi.NULL
- bio = self._backend._lib.BIO_new(bio_method)
- assert bio != self._backend._ffi.NULL
- bio = self._backend._ffi.gc(bio, self._backend._lib.BIO_free)
- return bio
-
- def _read_bio(self, bio):
- buf = self._backend._ffi.new("char **")
- buf_len = self._backend._lib.BIO_get_mem_data(bio, buf)
- assert buf_len > 0
- assert buf[0] != self._backend._ffi.NULL
- bio_data = self._backend._ffi.buffer(buf[0], buf_len)[:]
- return bio_data
-
def fingerprint(self, algorithm):
h = hashes.Hash(algorithm, self._backend)
- bio = self._create_bio()
+ bio = self._backend._create_mem_bio()
res = self._backend._lib.i2d_X509_bio(
bio, self._x509
)
assert res == 1
- der = self._read_bio(bio)
+ der = self._backend._read_mem_bio(bio)
h.update(der)
return h.finalize()