Skip to content

Commit 3e87c63

Browse files
[3.15] gh-159387: Fix TLS 1.0–1.2 availability detection with OpenSSL 4.0 (GH-159388) (#161320)
gh-159387: Fix TLS 1.0–1.2 availability detection with OpenSSL 4.0 (GH-159388) Fix TLS 1.0–1.2 availability detection with OpenSSL 4.0 OpenSSL 4.0 removed the version-specific methods but still supports TLS 1.0–1.2 through TLS_method(). Stop reporting these versions as unavailable, and expose legacy PROTOCOL_TLSv1* constants only when their corresponding methods exist. (cherry picked from commit e7acd6a) Co-authored-by: Pengyu Lee <lipengyu@kylinos.cn>
1 parent 85736ac commit 3e87c63

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎Lib/test/test_ssl.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,6 +4072,8 @@ def test_protocol_tlsv1_1(self):
40724072
try_protocol_combo(ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1_1, False)
40734073

40744074
@requires_tls_version('TLSv1_2')
4075+
@unittest.skipUnless(hasattr(ssl, 'PROTOCOL_TLSv1_2'),
4076+
'needs ssl.PROTOCOL_TLSv1_2')
40754077
def test_protocol_tlsv1_2(self):
40764078
"""Connecting to a TLSv1.2 server with various client options.
40774079
Testing against older TLS versions."""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`ssl` reporting TLS 1.0, 1.1, and 1.2 as unavailable when built
2+
with OpenSSL 4.0, despite support for these protocol versions.

‎Modules/_ssl.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,9 @@ static void _PySSLFixErrno(void) {
170170
#endif
171171

172172
#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
173+
/* OpenSSL 4.0 removed SSLv3 and the version-specific methods,
174+
* but TLS 1.0, 1.1 and 1.2 are still available via TLS_method(). */
173175
# define OPENSSL_NO_SSL3
174-
# define OPENSSL_NO_TLS1
175-
# define OPENSSL_NO_TLS1_1
176-
# define OPENSSL_NO_TLS1_2
177176
# define OPENSSL_NO_SSL3_METHOD
178177
# define OPENSSL_NO_TLS1_METHOD
179178
# define OPENSSL_NO_TLS1_1_METHOD
@@ -7123,13 +7122,13 @@ sslmodule_init_constants(PyObject *m)
71237122
ADD_INT_CONST("PROTOCOL_TLS", PY_SSL_VERSION_TLS);
71247123
ADD_INT_CONST("PROTOCOL_TLS_CLIENT", PY_SSL_VERSION_TLS_CLIENT);
71257124
ADD_INT_CONST("PROTOCOL_TLS_SERVER", PY_SSL_VERSION_TLS_SERVER);
7126-
#ifndef OPENSSL_NO_TLS1
7125+
#if !defined(OPENSSL_NO_TLS1) && !defined(OPENSSL_NO_TLS1_METHOD)
71277126
ADD_INT_CONST("PROTOCOL_TLSv1", PY_SSL_VERSION_TLS1);
71287127
#endif
7129-
#ifndef OPENSSL_NO_TLS1_1
7128+
#if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_1_METHOD)
71307129
ADD_INT_CONST("PROTOCOL_TLSv1_1", PY_SSL_VERSION_TLS1_1);
71317130
#endif
7132-
#ifndef OPENSSL_NO_TLS1_2
7131+
#if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_2_METHOD)
71337132
ADD_INT_CONST("PROTOCOL_TLSv1_2", PY_SSL_VERSION_TLS1_2);
71347133
#endif
71357134

0 commit comments

Comments
 (0)