|
19 | 19 | import traceback
|
20 | 20 | import weakref
|
21 | 21 | import platform
|
| 22 | +import re |
22 | 23 | import functools
|
23 | 24 | from contextlib import closing
|
24 | 25 |
|
@@ -159,6 +160,36 @@ def f(*args, **kwargs):
|
159 | 160 | else:
|
160 | 161 | return func
|
161 | 162 |
|
| 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 | + |
162 | 193 | needs_sni = unittest.skipUnless(ssl.HAS_SNI, "SNI support needed for this test")
|
163 | 194 |
|
164 | 195 |
|
@@ -2351,6 +2382,7 @@ def test_protocol_sslv2(self):
|
2351 | 2382 | client_options=ssl.OP_NO_TLSv1)
|
2352 | 2383 |
|
2353 | 2384 | @skip_if_broken_ubuntu_ssl
|
| 2385 | + @skip_if_openssl_cnf_minprotocol_gt_tls1 |
2354 | 2386 | def test_protocol_sslv23(self):
|
2355 | 2387 | """Connecting to an SSLv23 server with various client options"""
|
2356 | 2388 | if support.verbose:
|
@@ -2428,6 +2460,7 @@ def test_protocol_tlsv1(self):
|
2428 | 2460 | @skip_if_broken_ubuntu_ssl
|
2429 | 2461 | @unittest.skipUnless(hasattr(ssl, "PROTOCOL_TLSv1_1"),
|
2430 | 2462 | "TLS version 1.1 not supported.")
|
| 2463 | + @skip_if_openssl_cnf_minprotocol_gt_tls1 |
2431 | 2464 | def test_protocol_tlsv1_1(self):
|
2432 | 2465 | """Connecting to a TLSv1.1 server with various client options.
|
2433 | 2466 | Testing against older TLS versions."""
|
|
0 commit comments