From f21dc24a266a9e45088fc71daee97cbe9b11d4a4 Mon Sep 17 00:00:00 2001 From: Andrey Rakhmatullin Date: Tue, 30 Jul 2019 18:16:12 +0500 Subject: [PATCH] Fix memory handling and error handling in utils.ssl.get_temp_key_info. --- scrapy/utils/ssl.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scrapy/utils/ssl.py b/scrapy/utils/ssl.py index 5db1608bf..e54232abd 100644 --- a/scrapy/utils/ssl.py +++ b/scrapy/utils/ssl.py @@ -23,12 +23,12 @@ def get_temp_key_info(ssl_object): # adapted from OpenSSL apps/s_cb.c::ssl_print_tmp_key() temp_key_p = pyOpenSSLutil.ffi.new("EVP_PKEY **") - pyOpenSSLutil.lib.SSL_get_server_tmp_key(ssl_object, temp_key_p) - if temp_key_p == pyOpenSSLutil.ffi.NULL: + if not pyOpenSSLutil.lib.SSL_get_server_tmp_key(ssl_object, temp_key_p): return None - temp_key = temp_key_p[0] - pyOpenSSLutil.ffi.gc(temp_key, pyOpenSSLutil.lib.EVP_PKEY_free) + if temp_key == pyOpenSSLutil.ffi.NULL: + return None + temp_key = pyOpenSSLutil.ffi.gc(temp_key, pyOpenSSLutil.lib.EVP_PKEY_free) key_info = [] key_type = pyOpenSSLutil.lib.EVP_PKEY_id(temp_key) if key_type == pyOpenSSLutil.lib.EVP_PKEY_RSA: @@ -38,7 +38,7 @@ def get_temp_key_info(ssl_object): elif key_type == pyOpenSSLutil.lib.EVP_PKEY_EC: key_info.append('ECDH') ec_key = pyOpenSSLutil.lib.EVP_PKEY_get1_EC_KEY(temp_key) - pyOpenSSLutil.ffi.gc(ec_key, pyOpenSSLutil.lib.EC_KEY_free) + ec_key = pyOpenSSLutil.ffi.gc(ec_key, pyOpenSSLutil.lib.EC_KEY_free) nid = pyOpenSSLutil.lib.EC_GROUP_get_curve_name(pyOpenSSLutil.lib.EC_KEY_get0_group(ec_key)) cname = pyOpenSSLutil.lib.EC_curve_nid2nist(nid) if cname == pyOpenSSLutil.ffi.NULL: