@@ -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
*/
@@ -324,13 +315,9 @@ enum py_ssl_version {
324
315
PY_SSL_VERSION_SSL2 ,
325
316
PY_SSL_VERSION_SSL3 = 1 ,
326
317
PY_SSL_VERSION_TLS , /* SSLv23 */
327
- #if HAVE_TLSv1_2
328
318
PY_SSL_VERSION_TLS1 ,
329
319
PY_SSL_VERSION_TLS1_1 ,
330
320
PY_SSL_VERSION_TLS1_2 ,
331
- #else
332
- PY_SSL_VERSION_TLS1 ,
333
- #endif
334
321
PY_SSL_VERSION_TLS_CLIENT = 0x10 ,
335
322
PY_SSL_VERSION_TLS_SERVER ,
336
323
};
@@ -3030,35 +3017,45 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3030
3017
#endif
3031
3018
3032
3019
PySSL_BEGIN_ALLOW_THREADS
3033
- if (proto_version == PY_SSL_VERSION_TLS1 )
3020
+ switch (proto_version ) {
3021
+ #if defined(SSL3_VERSION ) && !defined(OPENSSL_NO_SSL3 )
3022
+ case PY_SSL_VERSION_SSL3 :
3023
+ ctx = SSL_CTX_new (SSLv3_method ());
3024
+ break ;
3025
+ #endif
3026
+ #if defined(TLS1_VERSION ) && !defined(OPENSSL_NO_TLS1 )
3027
+ case PY_SSL_VERSION_TLS1 :
3034
3028
ctx = SSL_CTX_new (TLSv1_method ());
3035
- #if HAVE_TLSv1_2
3036
- else if (proto_version == PY_SSL_VERSION_TLS1_1 )
3037
- ctx = SSL_CTX_new (TLSv1_1_method ());
3038
- else if (proto_version == PY_SSL_VERSION_TLS1_2 )
3039
- ctx = SSL_CTX_new (TLSv1_2_method ());
3029
+ break ;
3040
3030
#endif
3041
- #ifndef OPENSSL_NO_SSL3
3042
- else if (proto_version == PY_SSL_VERSION_SSL3 )
3043
- ctx = SSL_CTX_new (SSLv3_method ());
3031
+ #if defined(TLS1_1_VERSION ) && !defined(OPENSSL_NO_TLS1_1 )
3032
+ case PY_SSL_VERSION_TLS1_1 :
3033
+ ctx = SSL_CTX_new (TLSv1_1_method ());
3034
+ break ;
3044
3035
#endif
3045
- #ifndef OPENSSL_NO_SSL2
3046
- else if (proto_version == PY_SSL_VERSION_SSL2 )
3047
- ctx = SSL_CTX_new (SSLv2_method ());
3036
+ #if defined(TLS1_2_VERSION ) && !defined(OPENSSL_NO_TLS1_2 )
3037
+ case PY_SSL_VERSION_TLS1_2 :
3038
+ ctx = SSL_CTX_new (TLSv1_2_method ());
3039
+ break ;
3048
3040
#endif
3049
- else if (proto_version == PY_SSL_VERSION_TLS ) /* SSLv23 */
3041
+ case PY_SSL_VERSION_TLS :
3042
+ /* SSLv23 */
3050
3043
ctx = SSL_CTX_new (TLS_method ());
3051
- else if (proto_version == PY_SSL_VERSION_TLS_CLIENT )
3044
+ break ;
3045
+ case PY_SSL_VERSION_TLS_CLIENT :
3052
3046
ctx = SSL_CTX_new (TLS_client_method ());
3053
- else if (proto_version == PY_SSL_VERSION_TLS_SERVER )
3047
+ break ;
3048
+ case PY_SSL_VERSION_TLS_SERVER :
3054
3049
ctx = SSL_CTX_new (TLS_server_method ());
3055
- else
3050
+ break ;
3051
+ default :
3056
3052
proto_version = -1 ;
3053
+ }
3057
3054
PySSL_END_ALLOW_THREADS
3058
3055
3059
3056
if (proto_version == -1 ) {
3060
3057
PyErr_SetString (PyExc_ValueError ,
3061
- "invalid protocol version" );
3058
+ "invalid or unsupported protocol version" );
3062
3059
return NULL ;
3063
3060
}
3064
3061
if (ctx == NULL ) {
@@ -6055,23 +6052,19 @@ PyInit__ssl(void)
6055
6052
PY_SSL_VERSION_TLS_SERVER );
6056
6053
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1" ,
6057
6054
PY_SSL_VERSION_TLS1 );
6058
- #if HAVE_TLSv1_2
6059
6055
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_1" ,
6060
6056
PY_SSL_VERSION_TLS1_1 );
6061
6057
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_2" ,
6062
6058
PY_SSL_VERSION_TLS1_2 );
6063
- #endif
6064
6059
6065
6060
/* protocol options */
6066
6061
PyModule_AddIntConstant (m , "OP_ALL" ,
6067
6062
SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS );
6068
6063
PyModule_AddIntConstant (m , "OP_NO_SSLv2" , SSL_OP_NO_SSLv2 );
6069
6064
PyModule_AddIntConstant (m , "OP_NO_SSLv3" , SSL_OP_NO_SSLv3 );
6070
6065
PyModule_AddIntConstant (m , "OP_NO_TLSv1" , SSL_OP_NO_TLSv1 );
6071
- #if HAVE_TLSv1_2
6072
6066
PyModule_AddIntConstant (m , "OP_NO_TLSv1_1" , SSL_OP_NO_TLSv1_1 );
6073
6067
PyModule_AddIntConstant (m , "OP_NO_TLSv1_2" , SSL_OP_NO_TLSv1_2 );
6074
- #endif
6075
6068
#ifdef SSL_OP_NO_TLSv1_3
6076
6069
PyModule_AddIntConstant (m , "OP_NO_TLSv1_3" , SSL_OP_NO_TLSv1_3 );
6077
6070
#else
0 commit comments