@@ -147,15 +147,6 @@ static void _PySSLFixErrno(void) {
147147# define PY_OPENSSL_1_1_API 1
148148#endif
149149
150- /* Openssl comes with TLSv1.1 and TLSv1.2 between 1.0.0h and 1.0.1
151- http://www.openssl.org/news/changelog.html
152- */
153- #if OPENSSL_VERSION_NUMBER >= 0x10001000L
154- # define HAVE_TLSv1_2 1
155- #else
156- # define HAVE_TLSv1_2 0
157- #endif
158-
159150/* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f
160151 * This includes the SSL_set_SSL_CTX() function.
161152 */
@@ -326,13 +317,9 @@ enum py_ssl_version {
326317 PY_SSL_VERSION_SSL2 ,
327318 PY_SSL_VERSION_SSL3 = 1 ,
328319 PY_SSL_VERSION_TLS , /* SSLv23 */
329- #if HAVE_TLSv1_2
330320 PY_SSL_VERSION_TLS1 ,
331321 PY_SSL_VERSION_TLS1_1 ,
332322 PY_SSL_VERSION_TLS1_2 ,
333- #else
334- PY_SSL_VERSION_TLS1 ,
335- #endif
336323 PY_SSL_VERSION_TLS_CLIENT = 0x10 ,
337324 PY_SSL_VERSION_TLS_SERVER ,
338325};
@@ -3086,35 +3073,45 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
30863073#endif
30873074
30883075 PySSL_BEGIN_ALLOW_THREADS
3089- if (proto_version == PY_SSL_VERSION_TLS1 )
3076+ switch (proto_version ) {
3077+ #if defined(SSL3_VERSION ) && !defined(OPENSSL_NO_SSL3 )
3078+ case PY_SSL_VERSION_SSL3 :
3079+ ctx = SSL_CTX_new (SSLv3_method ());
3080+ break ;
3081+ #endif
3082+ #if defined(TLS1_VERSION ) && !defined(OPENSSL_NO_TLS1 )
3083+ case PY_SSL_VERSION_TLS1 :
30903084 ctx = SSL_CTX_new (TLSv1_method ());
3091- #if HAVE_TLSv1_2
3092- else if (proto_version == PY_SSL_VERSION_TLS1_1 )
3093- ctx = SSL_CTX_new (TLSv1_1_method ());
3094- else if (proto_version == PY_SSL_VERSION_TLS1_2 )
3095- ctx = SSL_CTX_new (TLSv1_2_method ());
3085+ break ;
30963086#endif
3097- #ifndef OPENSSL_NO_SSL3
3098- else if (proto_version == PY_SSL_VERSION_SSL3 )
3099- ctx = SSL_CTX_new (SSLv3_method ());
3087+ #if defined(TLS1_1_VERSION ) && !defined(OPENSSL_NO_TLS1_1 )
3088+ case PY_SSL_VERSION_TLS1_1 :
3089+ ctx = SSL_CTX_new (TLSv1_1_method ());
3090+ break ;
31003091#endif
3101- #ifndef OPENSSL_NO_SSL2
3102- else if (proto_version == PY_SSL_VERSION_SSL2 )
3103- ctx = SSL_CTX_new (SSLv2_method ());
3092+ #if defined(TLS1_2_VERSION ) && !defined(OPENSSL_NO_TLS1_2 )
3093+ case PY_SSL_VERSION_TLS1_2 :
3094+ ctx = SSL_CTX_new (TLSv1_2_method ());
3095+ break ;
31043096#endif
3105- else if (proto_version == PY_SSL_VERSION_TLS ) /* SSLv23 */
3097+ case PY_SSL_VERSION_TLS :
3098+ /* SSLv23 */
31063099 ctx = SSL_CTX_new (TLS_method ());
3107- else if (proto_version == PY_SSL_VERSION_TLS_CLIENT )
3100+ break ;
3101+ case PY_SSL_VERSION_TLS_CLIENT :
31083102 ctx = SSL_CTX_new (TLS_client_method ());
3109- else if (proto_version == PY_SSL_VERSION_TLS_SERVER )
3103+ break ;
3104+ case PY_SSL_VERSION_TLS_SERVER :
31103105 ctx = SSL_CTX_new (TLS_server_method ());
3111- else
3106+ break ;
3107+ default :
31123108 proto_version = -1 ;
3109+ }
31133110 PySSL_END_ALLOW_THREADS
31143111
31153112 if (proto_version == -1 ) {
31163113 PyErr_SetString (PyExc_ValueError ,
3117- "invalid protocol version" );
3114+ "invalid or unsupported protocol version" );
31183115 return NULL ;
31193116 }
31203117 if (ctx == NULL ) {
@@ -6185,23 +6182,19 @@ PyInit__ssl(void)
61856182 PY_SSL_VERSION_TLS_SERVER );
61866183 PyModule_AddIntConstant (m , "PROTOCOL_TLSv1" ,
61876184 PY_SSL_VERSION_TLS1 );
6188- #if HAVE_TLSv1_2
61896185 PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_1" ,
61906186 PY_SSL_VERSION_TLS1_1 );
61916187 PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_2" ,
61926188 PY_SSL_VERSION_TLS1_2 );
6193- #endif
61946189
61956190 /* protocol options */
61966191 PyModule_AddIntConstant (m , "OP_ALL" ,
61976192 SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS );
61986193 PyModule_AddIntConstant (m , "OP_NO_SSLv2" , SSL_OP_NO_SSLv2 );
61996194 PyModule_AddIntConstant (m , "OP_NO_SSLv3" , SSL_OP_NO_SSLv3 );
62006195 PyModule_AddIntConstant (m , "OP_NO_TLSv1" , SSL_OP_NO_TLSv1 );
6201- #if HAVE_TLSv1_2
62026196 PyModule_AddIntConstant (m , "OP_NO_TLSv1_1" , SSL_OP_NO_TLSv1_1 );
62036197 PyModule_AddIntConstant (m , "OP_NO_TLSv1_2" , SSL_OP_NO_TLSv1_2 );
6204- #endif
62056198#ifdef SSL_OP_NO_TLSv1_3
62066199 PyModule_AddIntConstant (m , "OP_NO_TLSv1_3" , SSL_OP_NO_TLSv1_3 );
62076200#else
0 commit comments