Skip to content

Commit e8bf04d

Browse files
bpo-36037: Fix test_ssl for strict OpenSSL policy (GH-11940)
Fix test_ssl for strict OpenSSL configuration like RHEL8 strict crypto policy. Use older TLS version for minimum TLS version of the server SSL context if needed, to test TLS version older than default minimum TLS version. (cherry picked from commit 3ef6344) Co-authored-by: Victor Stinner <vstinner@redhat.com>
1 parent 64ca728 commit e8bf04d

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lib/test/test_ssl.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@
3434
IS_OPENSSL_1_1_1 = not IS_LIBRESSL and ssl.OPENSSL_VERSION_INFO >= (1, 1, 1)
3535
PY_SSL_DEFAULT_CIPHERS = sysconfig.get_config_var('PY_SSL_DEFAULT_CIPHERS')
3636

37+
PROTOCOL_TO_TLS_VERSION = {}
38+
for proto, ver in (
39+
("PROTOCOL_SSLv23", "SSLv3"),
40+
("PROTOCOL_TLSv1", "TLSv1"),
41+
("PROTOCOL_TLSv1_1", "TLSv1_1"),
42+
):
43+
try:
44+
proto = getattr(ssl, proto)
45+
ver = getattr(ssl.TLSVersion, ver)
46+
except AttributeError:
47+
continue
48+
PROTOCOL_TO_TLS_VERSION[proto] = ver
49+
3750
def data_file(*name):
3851
return os.path.join(os.path.dirname(__file__), *name)
3952

@@ -1112,7 +1125,11 @@ def test_min_max_version(self):
11121125
# Fedora override the setting to TLS 1.0.
11131126
self.assertIn(
11141127
ctx.minimum_version,
1115-
{ssl.TLSVersion.MINIMUM_SUPPORTED, ssl.TLSVersion.TLSv1}
1128+
{ssl.TLSVersion.MINIMUM_SUPPORTED,
1129+
# Fedora 29 uses TLS 1.0 by default
1130+
ssl.TLSVersion.TLSv1,
1131+
# RHEL 8 uses TLS 1.2 by default
1132+
ssl.TLSVersion.TLSv1_2}
11161133
)
11171134
self.assertEqual(
11181135
ctx.maximum_version, ssl.TLSVersion.MAXIMUM_SUPPORTED
@@ -2630,6 +2647,17 @@ def try_protocol_combo(server_protocol, client_protocol, expect_success,
26302647
server_context = ssl.SSLContext(server_protocol)
26312648
server_context.options |= server_options
26322649

2650+
min_version = PROTOCOL_TO_TLS_VERSION.get(client_protocol, None)
2651+
if (min_version is not None
2652+
# SSLContext.minimum_version is only available on recent OpenSSL
2653+
# (setter added in OpenSSL 1.1.0, getter added in OpenSSL 1.1.1)
2654+
and hasattr(server_context, 'minimum_version')
2655+
and server_protocol == ssl.PROTOCOL_TLS
2656+
and server_context.minimum_version > min_version):
2657+
# If OpenSSL configuration is strict and requires more recent TLS
2658+
# version, we have to change the minimum to test old TLS versions.
2659+
server_context.minimum_version = min_version
2660+
26332661
# NOTE: we must enable "ALL" ciphers on the client, otherwise an
26342662
# SSLv23 client will send an SSLv3 hello (rather than SSLv2)
26352663
# starting from OpenSSL 1.0.0 (see issue #8322).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix test_ssl for strict OpenSSL configuration like RHEL8 strict crypto policy.
2+
Use older TLS version for minimum TLS version of the server SSL context if
3+
needed, to test TLS version older than default minimum TLS version.

0 commit comments

Comments
 (0)