@@ -143,15 +143,6 @@ static void _PySSLFixErrno(void) {
143
143
# define PY_OPENSSL_1_1_API 1
144
144
#endif
145
145
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
-
155
146
/* SNI support (client- and server-side) appeared in OpenSSL 1.0.0 and 0.9.8f
156
147
* This includes the SSL_set_SSL_CTX() function.
157
148
*/
@@ -322,13 +313,9 @@ enum py_ssl_version {
322
313
PY_SSL_VERSION_SSL2 ,
323
314
PY_SSL_VERSION_SSL3 = 1 ,
324
315
PY_SSL_VERSION_TLS , /* SSLv23 */
325
- #if HAVE_TLSv1_2
326
316
PY_SSL_VERSION_TLS1 ,
327
317
PY_SSL_VERSION_TLS1_1 ,
328
318
PY_SSL_VERSION_TLS1_2 ,
329
- #else
330
- PY_SSL_VERSION_TLS1 ,
331
- #endif
332
319
PY_SSL_VERSION_TLS_CLIENT = 0x10 ,
333
320
PY_SSL_VERSION_TLS_SERVER ,
334
321
};
@@ -3082,35 +3069,45 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3082
3069
#endif
3083
3070
3084
3071
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 :
3086
3080
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 ;
3092
3082
#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 ;
3096
3087
#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 ;
3100
3092
#endif
3101
- else if (proto_version == PY_SSL_VERSION_TLS ) /* SSLv23 */
3093
+ case PY_SSL_VERSION_TLS :
3094
+ /* SSLv23 */
3102
3095
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 :
3104
3098
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 :
3106
3101
ctx = SSL_CTX_new (TLS_server_method ());
3107
- else
3102
+ break ;
3103
+ default :
3108
3104
proto_version = -1 ;
3105
+ }
3109
3106
PySSL_END_ALLOW_THREADS
3110
3107
3111
3108
if (proto_version == -1 ) {
3112
3109
PyErr_SetString (PyExc_ValueError ,
3113
- "invalid protocol version" );
3110
+ "invalid or unsupported protocol version" );
3114
3111
return NULL ;
3115
3112
}
3116
3113
if (ctx == NULL ) {
@@ -6181,23 +6178,19 @@ PyInit__ssl(void)
6181
6178
PY_SSL_VERSION_TLS_SERVER );
6182
6179
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1" ,
6183
6180
PY_SSL_VERSION_TLS1 );
6184
- #if HAVE_TLSv1_2
6185
6181
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_1" ,
6186
6182
PY_SSL_VERSION_TLS1_1 );
6187
6183
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_2" ,
6188
6184
PY_SSL_VERSION_TLS1_2 );
6189
- #endif
6190
6185
6191
6186
/* protocol options */
6192
6187
PyModule_AddIntConstant (m , "OP_ALL" ,
6193
6188
SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS );
6194
6189
PyModule_AddIntConstant (m , "OP_NO_SSLv2" , SSL_OP_NO_SSLv2 );
6195
6190
PyModule_AddIntConstant (m , "OP_NO_SSLv3" , SSL_OP_NO_SSLv3 );
6196
6191
PyModule_AddIntConstant (m , "OP_NO_TLSv1" , SSL_OP_NO_TLSv1 );
6197
- #if HAVE_TLSv1_2
6198
6192
PyModule_AddIntConstant (m , "OP_NO_TLSv1_1" , SSL_OP_NO_TLSv1_1 );
6199
6193
PyModule_AddIntConstant (m , "OP_NO_TLSv1_2" , SSL_OP_NO_TLSv1_2 );
6200
- #endif
6201
6194
#ifdef SSL_OP_NO_TLSv1_3
6202
6195
PyModule_AddIntConstant (m , "OP_NO_TLSv1_3" , SSL_OP_NO_TLSv1_3 );
6203
6196
#else
0 commit comments