Skip to content

Commit 266b700

Browse files
authored
Assume OpenSSL supports keylogging
Since version 3.10, CPython requires OpenSSL 1.1.1 or higher. Therefore, support for keylogging can be assumed.
1 parent 7a468a1 commit 266b700

3 files changed

Lines changed: 10 additions & 18 deletions

File tree

Doc/library/ssl.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ purposes.
146146
*cadata* is given) or uses :meth:`SSLContext.load_default_certs` to load
147147
default CA certificates.
148148

149-
When :attr:`~SSLContext.keylog_filename` is supported and the environment
150-
variable :envvar:`SSLKEYLOGFILE` is set, :func:`create_default_context`
151-
enables key logging.
149+
When the environment variable :envvar:`SSLKEYLOGFILE` is set,
150+
:func:`create_default_context` enables key logging by setting
151+
:attr:`~SSLContext.keylog_filename` to the variable's value.
152152

153153
The default settings for this context include
154154
:data:`VERIFY_X509_PARTIAL_CHAIN` and :data:`VERIFY_X509_STRICT`.

Lib/ssl.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,9 @@ def create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None,
721721
# root CA certificates for the given purpose. This may fail silently.
722722
context.load_default_certs(purpose)
723723
# OpenSSL 1.1.1 keylog file
724-
if hasattr(context, 'keylog_filename'):
725-
keylogfile = os.environ.get('SSLKEYLOGFILE')
726-
if keylogfile and not sys.flags.ignore_environment:
727-
context.keylog_filename = keylogfile
724+
keylogfile = os.environ.get('SSLKEYLOGFILE')
725+
if keylogfile and not sys.flags.ignore_environment:
726+
context.keylog_filename = keylogfile
728727
return context
729728

730729
def _create_unverified_context(protocol=None, *, cert_reqs=CERT_NONE,
@@ -775,10 +774,9 @@ def _create_unverified_context(protocol=None, *, cert_reqs=CERT_NONE,
775774
# root CA certificates for the given purpose. This may fail silently.
776775
context.load_default_certs(purpose)
777776
# OpenSSL 1.1.1 keylog file
778-
if hasattr(context, 'keylog_filename'):
779-
keylogfile = os.environ.get('SSLKEYLOGFILE')
780-
if keylogfile and not sys.flags.ignore_environment:
781-
context.keylog_filename = keylogfile
777+
keylogfile = os.environ.get('SSLKEYLOGFILE')
778+
if keylogfile and not sys.flags.ignore_environment:
779+
context.keylog_filename = keylogfile
782780
return context
783781

784782
# Used by http.client if no context is explicitly passed.

Lib/test/test_ssl.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@
5959
CAN_GET_SELECTED_OPENSSL_SIGALG = ssl.OPENSSL_VERSION_INFO >= (3, 5)
6060
PY_SSL_DEFAULT_CIPHERS = sysconfig.get_config_var('PY_SSL_DEFAULT_CIPHERS')
6161

62-
HAS_KEYLOG = hasattr(ssl.SSLContext, 'keylog_filename')
63-
requires_keylog = unittest.skipUnless(
64-
HAS_KEYLOG, 'test requires OpenSSL 1.1.1 with keylog callback')
65-
CAN_SET_KEYLOG = HAS_KEYLOG and os.name != "nt"
62+
CAN_SET_KEYLOG = os.name != "nt"
6663
requires_keylog_setter = unittest.skipUnless(
6764
CAN_SET_KEYLOG,
6865
"cannot set 'keylog_filename' on Windows"
@@ -5453,7 +5450,6 @@ def keylog_lines(self, fname=os_helper.TESTFN):
54535450
with open(fname) as f:
54545451
return len(list(f))
54555452

5456-
@requires_keylog
54575453
def test_keylog_defaults(self):
54585454
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
54595455
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
@@ -5481,7 +5477,6 @@ def test_keylog_defaults(self):
54815477
with self.assertRaises(TypeError):
54825478
ctx.keylog_filename = 1
54835479

5484-
@requires_keylog
54855480
def test_keylog_filename(self):
54865481
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
54875482
client_context, server_context, hostname = testing_context()
@@ -5522,7 +5517,6 @@ def test_keylog_filename(self):
55225517
client_context.keylog_filename = None
55235518
server_context.keylog_filename = None
55245519

5525-
@requires_keylog
55265520
@unittest.skipIf(sys.flags.ignore_environment,
55275521
"test is not compatible with ignore_environment")
55285522
def test_keylog_env(self):

0 commit comments

Comments
 (0)