Skip to content

Commit 3ef6344

Browse files
authored
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.
1 parent beda52e commit 3ef6344

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Lib/test/test_ssl.py

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

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

@@ -1092,7 +1105,11 @@ def test_min_max_version(self):
10921105
# Fedora override the setting to TLS 1.0.
10931106
self.assertIn(
10941107
ctx.minimum_version,
1095-
{ssl.TLSVersion.MINIMUM_SUPPORTED, ssl.TLSVersion.TLSv1}
1108+
{ssl.TLSVersion.MINIMUM_SUPPORTED,
1109+
# Fedora 29 uses TLS 1.0 by default
1110+
ssl.TLSVersion.TLSv1,
1111+
# RHEL 8 uses TLS 1.2 by default
1112+
ssl.TLSVersion.TLSv1_2}
10961113
)
10971114
self.assertEqual(
10981115
ctx.maximum_version, ssl.TLSVersion.MAXIMUM_SUPPORTED
@@ -2609,6 +2626,17 @@ def try_protocol_combo(server_protocol, client_protocol, expect_success,
26092626
server_context = ssl.SSLContext(server_protocol)
26102627
server_context.options |= server_options
26112628

2629+
min_version = PROTOCOL_TO_TLS_VERSION.get(client_protocol, None)
2630+
if (min_version is not None
2631+
# SSLContext.minimum_version is only available on recent OpenSSL
2632+
# (setter added in OpenSSL 1.1.0, getter added in OpenSSL 1.1.1)
2633+
and hasattr(server_context, 'minimum_version')
2634+
and server_protocol == ssl.PROTOCOL_TLS
2635+
and server_context.minimum_version > min_version):
2636+
# If OpenSSL configuration is strict and requires more recent TLS
2637+
# version, we have to change the minimum to test old TLS versions.
2638+
server_context.minimum_version = min_version
2639+
26122640
# NOTE: we must enable "ALL" ciphers on the client, otherwise an
26132641
# SSLv23 client will send an SSLv3 hello (rather than SSLv2)
26142642
# 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)