@@ -149,15 +149,6 @@ static void _PySSLFixErrno(void) {
149
149
# define PY_OPENSSL_1_1_API 1
150
150
#endif
151
151
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
-
161
152
/* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f
162
153
* This includes the SSL_set_SSL_CTX() function.
163
154
*/
@@ -328,13 +319,9 @@ enum py_ssl_version {
328
319
PY_SSL_VERSION_SSL2 ,
329
320
PY_SSL_VERSION_SSL3 = 1 ,
330
321
PY_SSL_VERSION_TLS , /* SSLv23 */
331
- #if HAVE_TLSv1_2
332
322
PY_SSL_VERSION_TLS1 ,
333
323
PY_SSL_VERSION_TLS1_1 ,
334
324
PY_SSL_VERSION_TLS1_2 ,
335
- #else
336
- PY_SSL_VERSION_TLS1 ,
337
- #endif
338
325
PY_SSL_VERSION_TLS_CLIENT = 0x10 ,
339
326
PY_SSL_VERSION_TLS_SERVER ,
340
327
};
@@ -3088,35 +3075,45 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3088
3075
#endif
3089
3076
3090
3077
PySSL_BEGIN_ALLOW_THREADS
3091
- if (proto_version == PY_SSL_VERSION_TLS1 )
3078
+ switch (proto_version ) {
3079
+ #if defined(SSL3_VERSION ) && !defined(OPENSSL_NO_SSL3 )
3080
+ case PY_SSL_VERSION_SSL3 :
3081
+ ctx = SSL_CTX_new (SSLv3_method ());
3082
+ break ;
3083
+ #endif
3084
+ #if defined(TLS1_VERSION ) && !defined(OPENSSL_NO_TLS1 )
3085
+ case PY_SSL_VERSION_TLS1 :
3092
3086
ctx = SSL_CTX_new (TLSv1_method ());
3093
- #if HAVE_TLSv1_2
3094
- else if (proto_version == PY_SSL_VERSION_TLS1_1 )
3095
- ctx = SSL_CTX_new (TLSv1_1_method ());
3096
- else if (proto_version == PY_SSL_VERSION_TLS1_2 )
3097
- ctx = SSL_CTX_new (TLSv1_2_method ());
3087
+ break ;
3098
3088
#endif
3099
- #ifndef OPENSSL_NO_SSL3
3100
- else if (proto_version == PY_SSL_VERSION_SSL3 )
3101
- ctx = SSL_CTX_new (SSLv3_method ());
3089
+ #if defined(TLS1_1_VERSION ) && !defined(OPENSSL_NO_TLS1_1 )
3090
+ case PY_SSL_VERSION_TLS1_1 :
3091
+ ctx = SSL_CTX_new (TLSv1_1_method ());
3092
+ break ;
3102
3093
#endif
3103
- #ifndef OPENSSL_NO_SSL2
3104
- else if (proto_version == PY_SSL_VERSION_SSL2 )
3105
- ctx = SSL_CTX_new (SSLv2_method ());
3094
+ #if defined(TLS1_2_VERSION ) && !defined(OPENSSL_NO_TLS1_2 )
3095
+ case PY_SSL_VERSION_TLS1_2 :
3096
+ ctx = SSL_CTX_new (TLSv1_2_method ());
3097
+ break ;
3106
3098
#endif
3107
- else if (proto_version == PY_SSL_VERSION_TLS ) /* SSLv23 */
3099
+ case PY_SSL_VERSION_TLS :
3100
+ /* SSLv23 */
3108
3101
ctx = SSL_CTX_new (TLS_method ());
3109
- else if (proto_version == PY_SSL_VERSION_TLS_CLIENT )
3102
+ break ;
3103
+ case PY_SSL_VERSION_TLS_CLIENT :
3110
3104
ctx = SSL_CTX_new (TLS_client_method ());
3111
- else if (proto_version == PY_SSL_VERSION_TLS_SERVER )
3105
+ break ;
3106
+ case PY_SSL_VERSION_TLS_SERVER :
3112
3107
ctx = SSL_CTX_new (TLS_server_method ());
3113
- else
3108
+ break ;
3109
+ default :
3114
3110
proto_version = -1 ;
3111
+ }
3115
3112
PySSL_END_ALLOW_THREADS
3116
3113
3117
3114
if (proto_version == -1 ) {
3118
3115
PyErr_SetString (PyExc_ValueError ,
3119
- "invalid protocol version" );
3116
+ "invalid or unsupported protocol version" );
3120
3117
return NULL ;
3121
3118
}
3122
3119
if (ctx == NULL ) {
@@ -6188,23 +6185,19 @@ PyInit__ssl(void)
6188
6185
PY_SSL_VERSION_TLS_SERVER );
6189
6186
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1" ,
6190
6187
PY_SSL_VERSION_TLS1 );
6191
- #if HAVE_TLSv1_2
6192
6188
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_1" ,
6193
6189
PY_SSL_VERSION_TLS1_1 );
6194
6190
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_2" ,
6195
6191
PY_SSL_VERSION_TLS1_2 );
6196
- #endif
6197
6192
6198
6193
/* protocol options */
6199
6194
PyModule_AddIntConstant (m , "OP_ALL" ,
6200
6195
SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS );
6201
6196
PyModule_AddIntConstant (m , "OP_NO_SSLv2" , SSL_OP_NO_SSLv2 );
6202
6197
PyModule_AddIntConstant (m , "OP_NO_SSLv3" , SSL_OP_NO_SSLv3 );
6203
6198
PyModule_AddIntConstant (m , "OP_NO_TLSv1" , SSL_OP_NO_TLSv1 );
6204
- #if HAVE_TLSv1_2
6205
6199
PyModule_AddIntConstant (m , "OP_NO_TLSv1_1" , SSL_OP_NO_TLSv1_1 );
6206
6200
PyModule_AddIntConstant (m , "OP_NO_TLSv1_2" , SSL_OP_NO_TLSv1_2 );
6207
- #endif
6208
6201
#ifdef SSL_OP_NO_TLSv1_3
6209
6202
PyModule_AddIntConstant (m , "OP_NO_TLSv1_3" , SSL_OP_NO_TLSv1_3 );
6210
6203
#else
0 commit comments