@@ -81,6 +81,7 @@ mod _ssl {
8181 ArgBytesLike , ArgMemoryBuffer , ArgStrOrBytesLike , Either , FsPath , OptionalArg ,
8282 PyComparisonValue ,
8383 } ,
84+ stdlib:: _warnings,
8485 types:: { Comparable , Constructor , PyComparisonOp } ,
8586 utils:: ToCString ,
8687 } ,
@@ -914,16 +915,24 @@ mod _ssl {
914915 ) -> PyResult < Self > {
915916 let proto = SslVersion :: try_from ( proto_version)
916917 . map_err ( |_| vm. new_value_error ( "invalid protocol version" ) ) ?;
917- let method = match proto {
918+ let ( method, deprecated_protocol ) = match proto {
918919 // SslVersion::Ssl3 => unsafe { ssl::SslMethod::from_ptr(sys::SSLv3_method()) },
919- SslVersion :: Tls => ssl:: SslMethod :: tls ( ) ,
920- SslVersion :: Tls1 => ssl:: SslMethod :: tls ( ) ,
921- SslVersion :: Tls1_1 => ssl:: SslMethod :: tls ( ) ,
922- SslVersion :: Tls1_2 => ssl:: SslMethod :: tls ( ) ,
923- SslVersion :: TlsClient => ssl:: SslMethod :: tls_client ( ) ,
924- SslVersion :: TlsServer => ssl:: SslMethod :: tls_server ( ) ,
920+ SslVersion :: Tls => ( ssl:: SslMethod :: tls ( ) , Some ( "PROTOCOL_TLS" ) ) ,
921+ SslVersion :: Tls1 => ( ssl:: SslMethod :: tls ( ) , Some ( "PROTOCOL_TLSv1" ) ) ,
922+ SslVersion :: Tls1_1 => ( ssl:: SslMethod :: tls ( ) , Some ( "PROTOCOL_TLSv1_1" ) ) ,
923+ SslVersion :: Tls1_2 => ( ssl:: SslMethod :: tls ( ) , Some ( "PROTOCOL_TLSv1_2" ) ) ,
924+ SslVersion :: TlsClient => ( ssl:: SslMethod :: tls_client ( ) , None ) ,
925+ SslVersion :: TlsServer => ( ssl:: SslMethod :: tls_server ( ) , None ) ,
925926 _ => return Err ( vm. new_value_error ( "invalid protocol version" ) ) ,
926927 } ;
928+ if let Some ( protocol_name) = deprecated_protocol {
929+ _warnings:: warn (
930+ vm. ctx . exceptions . deprecation_warning ,
931+ format ! ( "ssl.{protocol_name} is deprecated" ) ,
932+ 2 ,
933+ vm,
934+ ) ?;
935+ }
927936 let mut builder =
928937 SslContextBuilder :: new ( method) . map_err ( |e| convert_openssl_error ( vm, e) ) ?;
929938
@@ -1012,6 +1021,24 @@ mod _ssl {
10121021
10131022 #[ pyclass( flags( BASETYPE , IMMUTABLETYPE ) , with( Constructor ) ) ]
10141023 impl PySslContext {
1024+ fn warn_deprecated_tls_version ( version : i32 , vm : & VirtualMachine ) -> PyResult < ( ) > {
1025+ let version_name = match version {
1026+ PROTO_SSLv3 => Some ( "SSLv3" ) ,
1027+ PROTO_TLSv1 => Some ( "TLSv1" ) ,
1028+ PROTO_TLSv1_1 => Some ( "TLSv1_1" ) ,
1029+ _ => None ,
1030+ } ;
1031+ if let Some ( version_name) = version_name {
1032+ _warnings:: warn (
1033+ vm. ctx . exceptions . deprecation_warning ,
1034+ format ! ( "ssl.TLSVersion.{version_name} is deprecated" ) ,
1035+ 2 ,
1036+ vm,
1037+ ) ?;
1038+ }
1039+ Ok ( ( ) )
1040+ }
1041+
10151042 fn builder ( & self ) -> PyRwLockWriteGuard < ' _ , SslContextBuilder > {
10161043 self . ctx . write ( )
10171044 }
@@ -1133,14 +1160,32 @@ mod _ssl {
11331160 return Err ( vm. new_value_error ( "invalid options value" ) ) ;
11341161 }
11351162 let new_opts = new_opts as core:: ffi:: c_ulong ;
1136- let mut ctx = self . builder ( ) ;
1137- // Get current options
1138- let current = ctx. options ( ) . bits ( ) as core:: ffi:: c_ulong ;
1163+ let current = {
1164+ let ctx = self . ctx ( ) ;
1165+ unsafe { sys:: SSL_CTX_get_options ( ctx. as_ptr ( ) ) }
1166+ } ;
11391167
11401168 // Calculate options to clear and set
11411169 let clear = current & !new_opts;
11421170 let set = !current & new_opts;
11431171
1172+ let opt_no = sys:: SSL_OP_NO_SSLv2
1173+ | sys:: SSL_OP_NO_SSLv3
1174+ | sys:: SSL_OP_NO_TLSv1
1175+ | sys:: SSL_OP_NO_TLSv1_1
1176+ | sys:: SSL_OP_NO_TLSv1_2 ;
1177+ #[ cfg( ossl111) ]
1178+ let opt_no = opt_no | sys:: SSL_OP_NO_TLSv1_3 ;
1179+ if ( set & opt_no) != 0 {
1180+ _warnings:: warn (
1181+ vm. ctx . exceptions . deprecation_warning ,
1182+ "ssl.OP_NO_SSL*/ssl.OP_NO_TLS* options are deprecated" . to_owned ( ) ,
1183+ 2 ,
1184+ vm,
1185+ ) ?;
1186+ }
1187+
1188+ let mut ctx = self . builder ( ) ;
11441189 // Clear options first (using raw FFI since openssl crate doesn't expose clear_options)
11451190 if clear != 0 {
11461191 unsafe {
@@ -1247,6 +1292,8 @@ mod _ssl {
12471292 }
12481293 #[ pygetset( setter) ]
12491294 fn set_minimum_version ( & self , value : i32 , vm : & VirtualMachine ) -> PyResult < ( ) > {
1295+ Self :: warn_deprecated_tls_version ( value, vm) ?;
1296+
12501297 // Handle special values
12511298 let proto_version = match value {
12521299 -2 => {
@@ -1281,6 +1328,8 @@ mod _ssl {
12811328 }
12821329 #[ pygetset( setter) ]
12831330 fn set_maximum_version ( & self , value : i32 , vm : & VirtualMachine ) -> PyResult < ( ) > {
1331+ Self :: warn_deprecated_tls_version ( value, vm) ?;
1332+
12841333 // Handle special values
12851334 let proto_version = match value {
12861335 -1 => {
0 commit comments