@@ -147,15 +147,6 @@ static void _PySSLFixErrno(void) {
147
147
# define PY_OPENSSL_1_1_API 1
148
148
#endif
149
149
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
-
159
150
/* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f
160
151
* This includes the SSL_set_SSL_CTX() function.
161
152
*/
@@ -326,13 +317,9 @@ enum py_ssl_version {
326
317
PY_SSL_VERSION_SSL2 ,
327
318
PY_SSL_VERSION_SSL3 = 1 ,
328
319
PY_SSL_VERSION_TLS , /* SSLv23 */
329
- #if HAVE_TLSv1_2
330
320
PY_SSL_VERSION_TLS1 ,
331
321
PY_SSL_VERSION_TLS1_1 ,
332
322
PY_SSL_VERSION_TLS1_2 ,
333
- #else
334
- PY_SSL_VERSION_TLS1 ,
335
- #endif
336
323
PY_SSL_VERSION_TLS_CLIENT = 0x10 ,
337
324
PY_SSL_VERSION_TLS_SERVER ,
338
325
};
@@ -3086,35 +3073,45 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3086
3073
#endif
3087
3074
3088
3075
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 :
3090
3084
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 ;
3096
3086
#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 ;
3100
3091
#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 ;
3104
3096
#endif
3105
- else if (proto_version == PY_SSL_VERSION_TLS ) /* SSLv23 */
3097
+ case PY_SSL_VERSION_TLS :
3098
+ /* SSLv23 */
3106
3099
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 :
3108
3102
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 :
3110
3105
ctx = SSL_CTX_new (TLS_server_method ());
3111
- else
3106
+ break ;
3107
+ default :
3112
3108
proto_version = -1 ;
3109
+ }
3113
3110
PySSL_END_ALLOW_THREADS
3114
3111
3115
3112
if (proto_version == -1 ) {
3116
3113
PyErr_SetString (PyExc_ValueError ,
3117
- "invalid protocol version" );
3114
+ "invalid or unsupported protocol version" );
3118
3115
return NULL ;
3119
3116
}
3120
3117
if (ctx == NULL ) {
@@ -6185,23 +6182,19 @@ PyInit__ssl(void)
6185
6182
PY_SSL_VERSION_TLS_SERVER );
6186
6183
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1" ,
6187
6184
PY_SSL_VERSION_TLS1 );
6188
- #if HAVE_TLSv1_2
6189
6185
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_1" ,
6190
6186
PY_SSL_VERSION_TLS1_1 );
6191
6187
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_2" ,
6192
6188
PY_SSL_VERSION_TLS1_2 );
6193
- #endif
6194
6189
6195
6190
/* protocol options */
6196
6191
PyModule_AddIntConstant (m , "OP_ALL" ,
6197
6192
SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS );
6198
6193
PyModule_AddIntConstant (m , "OP_NO_SSLv2" , SSL_OP_NO_SSLv2 );
6199
6194
PyModule_AddIntConstant (m , "OP_NO_SSLv3" , SSL_OP_NO_SSLv3 );
6200
6195
PyModule_AddIntConstant (m , "OP_NO_TLSv1" , SSL_OP_NO_TLSv1 );
6201
- #if HAVE_TLSv1_2
6202
6196
PyModule_AddIntConstant (m , "OP_NO_TLSv1_1" , SSL_OP_NO_TLSv1_1 );
6203
6197
PyModule_AddIntConstant (m , "OP_NO_TLSv1_2" , SSL_OP_NO_TLSv1_2 );
6204
- #endif
6205
6198
#ifdef SSL_OP_NO_TLSv1_3
6206
6199
PyModule_AddIntConstant (m , "OP_NO_TLSv1_3" , SSL_OP_NO_TLSv1_3 );
6207
6200
#else
0 commit comments