Skip to content

Commit

Permalink
fix a memory leak in ec derive_private_key
Browse files Browse the repository at this point in the history
fixes pyca#4095
  • Loading branch information
reaperhulk committed Feb 4, 2018
1 parent 15cc998 commit 56b420c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/cryptography/hazmat/backends/openssl/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/hazmat/backends/test_openssl_memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"""))

0 comments on commit 56b420c

Please sign in to comment.