@@ -3023,7 +3023,7 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
3023
3023
/*[clinic end generated code: output=2cf0d7a0741b6bd1 input=8d58a805b95fc534]*/
3024
3024
{
3025
3025
PySSLContext * self ;
3026
- long options ;
3026
+ uint64_t options ;
3027
3027
const SSL_METHOD * method = NULL ;
3028
3028
SSL_CTX * ctx = NULL ;
3029
3029
X509_VERIFY_PARAM * params ;
@@ -3621,20 +3621,32 @@ PyDoc_STRVAR(PySSLContext_security_level_doc, "The current security level");
3621
3621
static PyObject *
3622
3622
get_options (PySSLContext * self , void * c )
3623
3623
{
3624
- return PyLong_FromLong (SSL_CTX_get_options (self -> ctx ));
3624
+ uint64_t options = SSL_CTX_get_options (self -> ctx );
3625
+ Py_BUILD_ASSERT (sizeof (unsigned long long ) >= sizeof (options ));
3626
+ return PyLong_FromUnsignedLongLong (options );
3625
3627
}
3626
3628
3627
3629
static int
3628
3630
set_options (PySSLContext * self , PyObject * arg , void * c )
3629
3631
{
3630
- long new_opts , opts , set , clear ;
3631
- long opt_no = (
3632
+ PyObject * new_opts_obj ;
3633
+ unsigned long long new_opts_arg ;
3634
+ uint64_t new_opts , opts , clear , set ;
3635
+ uint64_t opt_no = (
3632
3636
SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 |
3633
3637
SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2 | SSL_OP_NO_TLSv1_3
3634
3638
);
3635
3639
3636
- if (!PyArg_Parse (arg , "l " , & new_opts ))
3640
+ if (!PyArg_Parse (arg , "O! " , & PyLong_Type , & new_opts_obj )) {
3637
3641
return -1 ;
3642
+ }
3643
+ new_opts_arg = PyLong_AsUnsignedLongLong (new_opts_obj );
3644
+ if (new_opts_arg == (unsigned long long )-1 && PyErr_Occurred ()) {
3645
+ return -1 ;
3646
+ }
3647
+ Py_BUILD_ASSERT (sizeof (new_opts ) >= sizeof (new_opts_arg ));
3648
+ new_opts = (uint64_t )new_opts_arg ;
3649
+
3638
3650
opts = SSL_CTX_get_options (self -> ctx );
3639
3651
clear = opts & ~new_opts ;
3640
3652
set = ~opts & new_opts ;
@@ -3648,8 +3660,9 @@ set_options(PySSLContext *self, PyObject *arg, void *c)
3648
3660
if (clear ) {
3649
3661
SSL_CTX_clear_options (self -> ctx , clear );
3650
3662
}
3651
- if (set )
3663
+ if (set ) {
3652
3664
SSL_CTX_set_options (self -> ctx , set );
3665
+ }
3653
3666
return 0 ;
3654
3667
}
3655
3668
@@ -5835,10 +5848,24 @@ sslmodule_init_socketapi(PyObject *module)
5835
5848
return 0 ;
5836
5849
}
5837
5850
5851
+
5838
5852
static int
5839
- sslmodule_init_constants (PyObject * m )
5853
+ sslmodule_add_option (PyObject * m , const char * name , uint64_t value )
5840
5854
{
5855
+ Py_BUILD_ASSERT (sizeof (unsigned long long ) >= sizeof (value ));
5856
+ PyObject * obj = PyLong_FromUnsignedLongLong (value );
5857
+ if (obj == NULL ) {
5858
+ return -1 ;
5859
+ }
5860
+ int res = PyModule_AddObjectRef (m , name , obj );
5861
+ Py_DECREF (obj );
5862
+ return res ;
5863
+ }
5864
+
5841
5865
5866
+ static int
5867
+ sslmodule_init_constants (PyObject * m )
5868
+ {
5842
5869
PyModule_AddStringConstant (m , "_DEFAULT_CIPHERS" ,
5843
5870
PY_SSL_DEFAULT_CIPHER_STRING );
5844
5871
@@ -5962,40 +5989,41 @@ sslmodule_init_constants(PyObject *m)
5962
5989
PyModule_AddIntConstant (m , "PROTOCOL_TLSv1_2" ,
5963
5990
PY_SSL_VERSION_TLS1_2 );
5964
5991
5992
+ #define ADD_OPTION (NAME , VALUE ) if (sslmodule_add_option(m, NAME, (VALUE)) < 0) return -1
5993
+
5965
5994
/* protocol options */
5966
- PyModule_AddIntConstant (m , "OP_ALL" ,
5967
- SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS );
5968
- PyModule_AddIntConstant (m , "OP_NO_SSLv2" , SSL_OP_NO_SSLv2 );
5969
- PyModule_AddIntConstant (m , "OP_NO_SSLv3" , SSL_OP_NO_SSLv3 );
5970
- PyModule_AddIntConstant (m , "OP_NO_TLSv1" , SSL_OP_NO_TLSv1 );
5971
- PyModule_AddIntConstant (m , "OP_NO_TLSv1_1" , SSL_OP_NO_TLSv1_1 );
5972
- PyModule_AddIntConstant (m , "OP_NO_TLSv1_2" , SSL_OP_NO_TLSv1_2 );
5995
+ ADD_OPTION ("OP_ALL" , SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS );
5996
+ ADD_OPTION ("OP_NO_SSLv2" , SSL_OP_NO_SSLv2 );
5997
+ ADD_OPTION ("OP_NO_SSLv3" , SSL_OP_NO_SSLv3 );
5998
+ ADD_OPTION ("OP_NO_TLSv1" , SSL_OP_NO_TLSv1 );
5999
+ ADD_OPTION ("OP_NO_TLSv1_1" , SSL_OP_NO_TLSv1_1 );
6000
+ ADD_OPTION ("OP_NO_TLSv1_2" , SSL_OP_NO_TLSv1_2 );
5973
6001
#ifdef SSL_OP_NO_TLSv1_3
5974
- PyModule_AddIntConstant ( m , "OP_NO_TLSv1_3" , SSL_OP_NO_TLSv1_3 );
6002
+ ADD_OPTION ( "OP_NO_TLSv1_3" , SSL_OP_NO_TLSv1_3 );
5975
6003
#else
5976
- PyModule_AddIntConstant ( m , "OP_NO_TLSv1_3" , 0 );
6004
+ ADD_OPTION ( "OP_NO_TLSv1_3" , 0 );
5977
6005
#endif
5978
- PyModule_AddIntConstant ( m , "OP_CIPHER_SERVER_PREFERENCE" ,
6006
+ ADD_OPTION ( "OP_CIPHER_SERVER_PREFERENCE" ,
5979
6007
SSL_OP_CIPHER_SERVER_PREFERENCE );
5980
- PyModule_AddIntConstant ( m , "OP_SINGLE_DH_USE" , SSL_OP_SINGLE_DH_USE );
5981
- PyModule_AddIntConstant ( m , "OP_NO_TICKET" , SSL_OP_NO_TICKET );
6008
+ ADD_OPTION ( "OP_SINGLE_DH_USE" , SSL_OP_SINGLE_DH_USE );
6009
+ ADD_OPTION ( "OP_NO_TICKET" , SSL_OP_NO_TICKET );
5982
6010
#ifdef SSL_OP_SINGLE_ECDH_USE
5983
- PyModule_AddIntConstant ( m , "OP_SINGLE_ECDH_USE" , SSL_OP_SINGLE_ECDH_USE );
6011
+ ADD_OPTION ( "OP_SINGLE_ECDH_USE" , SSL_OP_SINGLE_ECDH_USE );
5984
6012
#endif
5985
6013
#ifdef SSL_OP_NO_COMPRESSION
5986
- PyModule_AddIntConstant ( m , "OP_NO_COMPRESSION" ,
6014
+ ADD_OPTION ( "OP_NO_COMPRESSION" ,
5987
6015
SSL_OP_NO_COMPRESSION );
5988
6016
#endif
5989
6017
#ifdef SSL_OP_ENABLE_MIDDLEBOX_COMPAT
5990
- PyModule_AddIntConstant ( m , "OP_ENABLE_MIDDLEBOX_COMPAT" ,
6018
+ ADD_OPTION ( "OP_ENABLE_MIDDLEBOX_COMPAT" ,
5991
6019
SSL_OP_ENABLE_MIDDLEBOX_COMPAT );
5992
6020
#endif
5993
6021
#ifdef SSL_OP_NO_RENEGOTIATION
5994
- PyModule_AddIntConstant ( m , "OP_NO_RENEGOTIATION" ,
6022
+ ADD_OPTION ( "OP_NO_RENEGOTIATION" ,
5995
6023
SSL_OP_NO_RENEGOTIATION );
5996
6024
#endif
5997
6025
#ifdef SSL_OP_IGNORE_UNEXPECTED_EOF
5998
- PyModule_AddIntConstant ( m , "OP_IGNORE_UNEXPECTED_EOF" ,
6026
+ ADD_OPTION ( "OP_IGNORE_UNEXPECTED_EOF" ,
5999
6027
SSL_OP_IGNORE_UNEXPECTED_EOF );
6000
6028
#endif
6001
6029
0 commit comments