Skip to content

Commit 4052c3b

Browse files
committed
bpo-40457: Support OpenSSL without TLS 1.0/1.1
OpenSSL can be build without support for TLS 1.0 and 1.1. The ssl module now correctly adheres to OPENSSL_NO_TLS1 and OPENSSL_NO_TLS1_1 flags. Also update multissltest to test with latest OpenSSL and LibreSSL releases. Signed-off-by: Christian Heimes <[email protected]>
1 parent 62d618c commit 4052c3b

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
@@ -143,15 +143,6 @@ static void _PySSLFixErrno(void) {
143143
# define PY_OPENSSL_1_1_API 1
144144
#endif
145145

146-
/* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1
147-
http://www.openssl.org/news/changelog.html
148-
*/
149-
#if OPENSSL_VERSION_NUMBER >= 0x10001000L
150-
# define HAVE_TLSv1_2 1
151-
#else
152-
# define HAVE_TLSv1_2 0
153-
#endif
154-
155146
/* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f
156147
* This includes the SSL_set_SSL_CTX() function.
157148
*/
@@ -322,13 +313,9 @@ enum py_ssl_version {
322313
PY_SSL_VERSION_SSL2,
323314
PY_SSL_VERSION_SSL3=1,
324315
PY_SSL_VERSION_TLS, /* SSLv23 */
325-
#if HAVE_TLSv1_2
326316
PY_SSL_VERSION_TLS1,
327317
PY_SSL_VERSION_TLS1_1,
328318
PY_SSL_VERSION_TLS1_2,
329-
#else
330-
PY_SSL_VERSION_TLS1,
331-
#endif
332319
PY_SSL_VERSION_TLS_CLIENT=0x10,
333320
PY_SSL_VERSION_TLS_SERVER,
334321
};
@@ -3082,35 +3069,45 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
30823069
#endif
30833070

30843071
PySSL_BEGIN_ALLOW_THREADS
3085-
if (proto_version == PY_SSL_VERSION_TLS1)
3072+
switch(proto_version) {
3073+
#if defined(SSL3_VERSION) && !defined(OPENSSL_NO_SSL3)
3074+
case PY_SSL_VERSION_SSL3:
3075+
ctx = SSL_CTX_new(SSLv3_method());
3076+
break;
3077+
#endif
3078+
#if defined(TLS1_VERSION) && !defined(OPENSSL_NO_TLS1)
3079+
case PY_SSL_VERSION_TLS1:
30863080
ctx = SSL_CTX_new(TLSv1_method());
3087-
#if HAVE_TLSv1_2
3088-
else if (proto_version == PY_SSL_VERSION_TLS1_1)
3089-
ctx = SSL_CTX_new(TLSv1_1_method());
3090-
else if (proto_version == PY_SSL_VERSION_TLS1_2)
3091-
ctx = SSL_CTX_new(TLSv1_2_method());
3081+
break;
30923082
#endif
3093-
#ifndef OPENSSL_NO_SSL3
3094-
else if (proto_version == PY_SSL_VERSION_SSL3)
3095-
ctx = SSL_CTX_new(SSLv3_method());
3083+
#if defined(TLS1_1_VERSION) && !defined(OPENSSL_NO_TLS1_1)
3084+
case PY_SSL_VERSION_TLS1_1:
3085+
ctx = SSL_CTX_new(TLSv1_1_method());
3086+
break;
30963087
#endif
3097-
#ifndef OPENSSL_NO_SSL2
3098-
else if (proto_version == PY_SSL_VERSION_SSL2)
3099-
ctx = SSL_CTX_new(SSLv2_method());
3088+
#if defined(TLS1_2_VERSION) && !defined(OPENSSL_NO_TLS1_2)
3089+
case PY_SSL_VERSION_TLS1_2:
3090+
ctx = SSL_CTX_new(TLSv1_2_method());
3091+
break;
31003092
#endif
3101-
else if (proto_version == PY_SSL_VERSION_TLS) /* SSLv23 */
3093+
case PY_SSL_VERSION_TLS:
3094+
/* SSLv23 */
31023095
ctx = SSL_CTX_new(TLS_method());
3103-
else if (proto_version == PY_SSL_VERSION_TLS_CLIENT)
3096+
break;
3097+
case PY_SSL_VERSION_TLS_CLIENT:
31043098
ctx = SSL_CTX_new(TLS_client_method());
3105-
else if (proto_version == PY_SSL_VERSION_TLS_SERVER)
3099+
break;
3100+
case PY_SSL_VERSION_TLS_SERVER:
31063101
ctx = SSL_CTX_new(TLS_server_method());
3107-
else
3102+
break;
3103+
default:
31083104
proto_version = -1;
3105+
}
31093106
PySSL_END_ALLOW_THREADS
31103107

31113108
if (proto_version == -1) {
31123109
PyErr_SetString(PyExc_ValueError,
3113-
"invalid protocol version");
3110+
"invalid or unsupported protocol version");
31143111
return NULL;
31153112
}
31163113
if (ctx == NULL) {
@@ -6181,23 +6178,19 @@ PyInit__ssl(void)
61816178
PY_SSL_VERSION_TLS_SERVER);
61826179
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
61836180
PY_SSL_VERSION_TLS1);
6184-
#if HAVE_TLSv1_2
61856181
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_1",
61866182
PY_SSL_VERSION_TLS1_1);
61876183
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1_2",
61886184
PY_SSL_VERSION_TLS1_2);
6189-
#endif
61906185

61916186
/* protocol options */
61926187
PyModule_AddIntConstant(m, "OP_ALL",
61936188
SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
61946189
PyModule_AddIntConstant(m, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
61956190
PyModule_AddIntConstant(m, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
61966191
PyModule_AddIntConstant(m, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
6197-
#if HAVE_TLSv1_2
61986192
PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1);
61996193
PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2);
6200-
#endif
62016194
#ifdef SSL_OP_NO_TLSv1_3
62026195
PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3);
62036196
#else

Tools/ssl/multissltests.py

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

4343
OPENSSL_OLD_VERSIONS = [
44+
"1.0.2u",
45+
"1.1.0l",
4446
]
4547

4648
OPENSSL_RECENT_VERSIONS = [
47-
"1.0.2u",
48-
"1.1.0l",
4949
"1.1.1g",
5050
# "3.0.0-alpha2"
5151
]
5252

5353
LIBRESSL_OLD_VERSIONS = [
54+
"2.9.2",
5455
]
5556

5657
LIBRESSL_RECENT_VERSIONS = [
57-
"2.9.2",
58+
"3.1.0",
5859
]
5960

6061
# store files in ../multissl
@@ -78,7 +79,7 @@
7879
parser.add_argument(
7980
'--disable-ancient',
8081
action='store_true',
81-
help="Don't test OpenSSL < 1.0.2 and LibreSSL < 2.5.3.",
82+
help="Don't test OpenSSL and LibreSSL versions without upstream support",
8283
)
8384
parser.add_argument(
8485
'--openssl',

0 commit comments

Comments
 (0)