diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2018-07-10 16:39:00 +0530 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2018-07-10 07:09:00 -0400 |
commit | 40baa274f979d29310ef54493d05cd2e52043417 (patch) | |
tree | f2e770a635243cc005fa933607d1cefee059227a | |
parent | 20ae2c8084812bd218d2b1d2972774963c4d2041 (diff) | |
download | cryptography-40baa274f979d29310ef54493d05cd2e52043417.tar.gz cryptography-40baa274f979d29310ef54493d05cd2e52043417.tar.bz2 cryptography-40baa274f979d29310ef54493d05cd2e52043417.zip |
fix a memory leak when calling X25519PrivateKey.public_key() (#4326)
-rw-r--r-- | src/cryptography/hazmat/backends/openssl/x25519.py | 4 | ||||
-rw-r--r-- | tests/hazmat/backends/test_openssl_memleak.py | 8 |
2 files changed, 12 insertions, 0 deletions
diff --git a/src/cryptography/hazmat/backends/openssl/x25519.py b/src/cryptography/hazmat/backends/openssl/x25519.py index f92b184b..5599c2fd 100644 --- a/src/cryptography/hazmat/backends/openssl/x25519.py +++ b/src/cryptography/hazmat/backends/openssl/x25519.py @@ -42,6 +42,10 @@ class _X25519PrivateKey(object): evp_pkey = self._backend._lib.d2i_PUBKEY_bio( bio, self._backend._ffi.NULL ) + self._backend.openssl_assert(evp_pkey != self._backend._ffi.NULL) + evp_pkey = self._backend._ffi.gc( + evp_pkey, self._backend._lib.EVP_PKEY_free + ) return _X25519PublicKey(self._backend, evp_pkey) def exchange(self, peer_public_key): diff --git a/tests/hazmat/backends/test_openssl_memleak.py b/tests/hazmat/backends/test_openssl_memleak.py index cd453541..5cb7cbc7 100644 --- a/tests/hazmat/backends/test_openssl_memleak.py +++ b/tests/hazmat/backends/test_openssl_memleak.py @@ -225,3 +225,11 @@ class TestOpenSSLMemoryLeaks(object): from cryptography.hazmat.primitives.asymmetric import ec ec.derive_private_key(1, ec.SECP256R1(), backend) """)) + + def test_x25519_pubkey_from_private_key(self): + assert_no_memory_leaks(textwrap.dedent(""" + def func(): + from cryptography.hazmat.primitives.asymmetric import x25519 + private_key = x25519.X25519PrivateKey.generate() + private_key.public_key() + """)) |