Skip to content

Commit dba6346

Browse files
committed
[2.7] bpo-35925: Skip SSL tests that fail due to weak external certs or old TLS (pythonGH-13124)
Modern Linux distros such as Debian Buster have default OpenSSL system configurations that reject connections to servers with weak certificates by default. This causes our test suite run with external networking resources enabled to skip these tests when they encounter such a failure. Fixing the network servers is a separate issue. (cherry picked from commit 2cc0223) Changes to test_ssl.py required as 2.7 has legacy protocol tests. The test_httplib.py change is omitted from this backport as self-signed.pythontest.net's certificate was updated and the test_nntplib.py change is not applicable on 2.7. Authored-by: Gregory P. Smith [email protected]
1 parent 7b5dca8 commit dba6346

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Lib/test/test_ssl.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import traceback
2020
import weakref
2121
import platform
22+
import re
2223
import functools
2324
from contextlib import closing
2425

@@ -159,6 +160,36 @@ def f(*args, **kwargs):
159160
else:
160161
return func
161162

163+
def skip_if_openssl_cnf_minprotocol_gt_tls1(func):
164+
"""Skip a test if the OpenSSL config MinProtocol is > TLSv1.
165+
OS distros with an /etc/ssl/openssl.cnf and MinProtocol set often do so to
166+
require TLSv1.2 or higher (Debian Buster). Some of our tests for older
167+
protocol versions will fail under such a config.
168+
Alternative workaround: Run this test in a process with
169+
OPENSSL_CONF=/dev/null in the environment.
170+
"""
171+
@functools.wraps(func)
172+
def f(*args, **kwargs):
173+
openssl_cnf = os.environ.get("OPENSSL_CONF", "/etc/ssl/openssl.cnf")
174+
try:
175+
with open(openssl_cnf, "r") as config:
176+
for line in config:
177+
match = re.match(r"MinProtocol\s*=\s*(TLSv\d+\S*)", line)
178+
if match:
179+
tls_ver = match.group(1)
180+
if tls_ver > "TLSv1":
181+
raise unittest.SkipTest(
182+
"%s has MinProtocol = %s which is > TLSv1." %
183+
(openssl_cnf, tls_ver))
184+
except (EnvironmentError, UnicodeDecodeError) as err:
185+
# no config file found, etc.
186+
if support.verbose:
187+
sys.stdout.write("\n Could not scan %s for MinProtocol: %s\n"
188+
% (openssl_cnf, err))
189+
return func(*args, **kwargs)
190+
return f
191+
192+
162193
needs_sni = unittest.skipUnless(ssl.HAS_SNI, "SNI support needed for this test")
163194

164195

@@ -2351,6 +2382,7 @@ def test_protocol_sslv2(self):
23512382
client_options=ssl.OP_NO_TLSv1)
23522383

23532384
@skip_if_broken_ubuntu_ssl
2385+
@skip_if_openssl_cnf_minprotocol_gt_tls1
23542386
def test_protocol_sslv23(self):
23552387
"""Connecting to an SSLv23 server with various client options"""
23562388
if support.verbose:
@@ -2428,6 +2460,7 @@ def test_protocol_tlsv1(self):
24282460
@skip_if_broken_ubuntu_ssl
24292461
@unittest.skipUnless(hasattr(ssl, "PROTOCOL_TLSv1_1"),
24302462
"TLS version 1.1 not supported.")
2463+
@skip_if_openssl_cnf_minprotocol_gt_tls1
24312464
def test_protocol_tlsv1_1(self):
24322465
"""Connecting to a TLSv1.1 server with various client options.
24332466
Testing against older TLS versions."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Skip specific nntplib and ssl networking tests when they would otherwise fail due to a modern OS or distro with a default OpenSSL policy of rejecting connections to servers with weak certificates or disabling TLS below TLSv1.2.

0 commit comments

Comments
 (0)