diff options
-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() |