Skip to content

Commit 43b355e

Browse files
[3.7] bpo-40457: Support OpenSSL without TLS 1.0/1.1 (GH-19862) (GH-20126)
1 parent 7a89f9b commit 43b355e

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ssl module now support OpenSSL builds without TLS 1.0 and 1.1 methods.

Modules/_ssl.c

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,6 @@ static void _PySSLFixErrno(void) {
149149
# define PY_OPENSSL_1_1_API 1
150150
#endif
151151

152-
/* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1
153-
http://www.openssl.org/news/changelog.html
154-
*/
155-
#if OPENSSL_VERSION_NUMBER >= 0x10001000L
156-
# define HAVE_TLSv1_2 1
157-
#else
158-
# define HAVE_TLSv1_2 0
159-
#endif
160-
161152
/* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f
162153
* This includes the SSL_set_SSL_CTX() function.
163154
*/
@@ -324,13 +315,9 @@ enum py_ssl_version {
324315
PY_SSL_VERSION_SSL2,
325316
PY_SSL_VERSION_SSL3=1,
326317
PY_SSL_VERSION_TLS, /* SSLv23 */
327-
#if HAVE_TLSv1_2
328318
PY_SSL_VERSION_TLS1,
329319
PY_SSL_VERSION_TLS1_1,
330320
PY_SSL_VERSION_TLS1_2,
331-
#else
332-
PY_SSL_VERSION_TLS1,
333-
#endif
334321
PY_SSL_VERSION_TLS_CLIENT=0x10,
335322
PY_SSL_VERSION_TLS_SERVER,
336323
};
@@ -3030,35 +3017,45 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
30303017
#endif
30313018

30323019
PySSL_BEGIN_ALLOW_THREADS
3033-
if (proto_version == PY_SSL_VERSION_TLS1)
3020+
switch(proto_version) {
3021+
#if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3)
3022+
case PY_SSL_VERSION_SSL3:
3023+
ctx = SSL_CTX_new(SSLv3_method());
3024+
break;
3025+
#endif
3026+
#if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1)
3027+
case PY_SSL_VERSION_TLS1:
30343028
ctx = SSL_CTX_new(TLSv1_method());
3035-
#if HAVE_TLSv1_2
3036-
else if (proto_version == PY_SSL_VERSION_TLS1_1)
3037-
ctx = SSL_CTX_new(TLSv1_1_method());
3038-
else if (proto_version == PY_SSL_VERSION_TLS1_2)
3039-
ctx = SSL_CTX_new(TLSv1_2_method());
3029+
break;
30403030
#endif
3041-
#ifndef OPENSSL_NO_SSL3
3042-
else if (proto_version == PY_SSL_VERSION_SSL3)
3043-
ctx = SSL_CTX_new(SSLv3_method());
3031+
#if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1)
3032+
case PY_SSL_VERSION_TLS1_1:
3033+
ctx = SSL_CTX_new(TLSv1_1_method());
3034+
break;
30443035
#endif
3045-
#ifndef OPENSSL_NO_SSL2
3046-
else if (proto_version == PY_SSL_VERSION_SSL2)
3047-
ctx = SSL_CTX_new(SSLv2_method());
3036+
#if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2)
3037+
case PY_SSL_VERSION_TLS1_2:
3038+
ctx = SSL_CTX_new(TLSv1_2_method());
3039+
break;
30483040
#endif
3049-
else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */
3041+
case PY_SSL_VERSION_TLS:
3042+
/* SSLv23 */
30503043
ctx = SSL_CTX_new(TLS_method());
3051-
else if (proto_version == PY_SSL_VERSION_TLS_CLIENT)
3044+
break;
3045+
case PY_SSL_VERSION_TLS_CLIENT:
30523046
ctx = SSL_CTX_new(TLS_client_method());
3053-
else if (proto_version == PY_SSL_VERSION_TLS_SERVER)
3047+
break;
3048+
case PY_SSL_VERSION_TLS_SERVER:
30543049
ctx = SSL_CTX_new(TLS_server_method());
3055-
else
3050+
break;
3051+
default:
30563052
proto_version = -1;
3053+
}
30573054
PySSL_END_ALLOW_THREADS
30583055

30593056
if (proto_version == -1) {
30603057
PyErr_SetString(PyExc_ValueError,
3061-
"invalid protocol version");
3058+
"invalid or unsupported protocol version");
30623059
return NULL;
30633060
}
30643061
if (ctx == NULL) {
@@ -6055,23 +6052,19 @@ PyInit__ssl(void)
60556052
PY_SSL_VERSION_TLS_SERVER);
60566053
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
60576054
PY_SSL_VERSION_TLS1);
6058-
#if HAVE_TLSv1_2
60596055
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1",
60606056
PY_SSL_VERSION_TLS1_1);
60616057
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2",
60626058
PY_SSL_VERSION_TLS1_2);
6063-
#endif
60646059

60656060
/* protocol options */
60666061
PyModule_AddIntConstant(m, "OP_ALL",
60676062
SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
60686063
PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
60696064
PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
60706065
PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
6071-
#if HAVE_TLSv1_2
60726066
PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1);
60736067
PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2);
6074-
#endif
60756068
#ifdef SSL_OP_NO_TLSv1_3
60766069
PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3);
60776070
#else

Tools/ssl/multissltests.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,21 @@
4343
log = logging.getLogger("multissl")
4444

4545
OPENSSL_OLD_VERSIONS = [
46+
"1.0.2u",
47+
"1.1.0l",
4648
]
4749

4850
OPENSSL_RECENT_VERSIONS = [
49-
"1.0.2u",
50-
"1.1.0l",
5151
"1.1.1g",
5252
# "3.0.0-alpha2"
5353
]
5454

5555
LIBRESSL_OLD_VERSIONS = [
56+
"2.9.2",
5657
]
5758

5859
LIBRESSL_RECENT_VERSIONS = [
59-
"2.9.2",
60+
"3.1.0",
6061
]
6162

6263
# store files in ../multissl
@@ -80,7 +81,7 @@
8081
parser.add_argument(
8182
'--disable-ancient',
8283
action='store_true',
83-
help="Don't test OpenSSL < 1.0.2 and LibreSSL < 2.5.3.",
84+
help="Don't test OpenSSL and LibreSSL versions without upstream support",
8485
)
8586
parser.add_argument(
8687
'--openssl',

0 commit comments

Comments
 (0)