diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-12-11 14:44:32 -0600 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-12-15 15:49:49 -0600 |
commit | 68481c3e78d08b7defdd716b72b7563fb0ee5469 (patch) | |
tree | 34f32a3ae7b7f2a98dc800909636cf515a7e8759 | |
parent | d9fc7252f9470f6f9f6c05047e2fcf1c5c34667a (diff) | |
download | cryptography-68481c3e78d08b7defdd716b72b7563fb0ee5469.tar.gz cryptography-68481c3e78d08b7defdd716b72b7563fb0ee5469.tar.bz2 cryptography-68481c3e78d08b7defdd716b72b7563fb0ee5469.zip |
move mem_bio creation/reading to backend
-rw-r--r-- | src/cryptography/hazmat/backends/openssl/backend.py | 22 | ||||
-rw-r--r-- | src/cryptography/hazmat/backends/openssl/x509.py | 20 |
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 19d149b5..10341fa2 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 a348630f..532785ac 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() |