diff --git a/src/cryptography/hazmat/backends/openssl/backend.py b/src/cryptography/hazmat/backends/openssl/backend.py index ebcf8015fa43..85e13b57266f 100644 --- a/src/cryptography/hazmat/backends/openssl/backend.py +++ b/src/cryptography/hazmat/backends/openssl/backend.py @@ -1409,8 +1409,9 @@ def derive_elliptic_curve_private_key(self, private_value, curve): res = self._lib.EC_KEY_set_public_key(ec_cdata, point) self.openssl_assert(res == 1) - res = self._lib.EC_KEY_set_private_key( - ec_cdata, self._int_to_bn(private_value)) + private = self._int_to_bn(private_value) + private = self._ffi.gc(private, self._lib.BN_clear_free) + res = self._lib.EC_KEY_set_private_key(ec_cdata, private) self.openssl_assert(res == 1) evp_pkey = self._ec_cdata_to_evp_pkey(ec_cdata) diff --git a/tests/hazmat/backends/test_openssl_memleak.py b/tests/hazmat/backends/test_openssl_memleak.py index 6e92e3418d9d..903e615b399a 100644 --- a/tests/hazmat/backends/test_openssl_memleak.py +++ b/tests/hazmat/backends/test_openssl_memleak.py @@ -214,3 +214,11 @@ def func(): ) ).private_key(backend) """)) + + def test_ec_derive_private_key(self): + assert_no_memory_leaks(textwrap.dedent(""" + def func(): + from cryptography.hazmat.backends.openssl import backend + from cryptography.hazmat.primitives.asymmetric.ec import SECP256R1, derive_private_key + derive_private_key(1, SECP256R1(), backend) + """))