@@ -103,8 +103,8 @@ mod _ssl {
103103 // Import compat module (OpenSSL compatibility layer)
104104 use super :: compat:: {
105105 ClientConfigOptions , MultiCertResolver , ProtocolSettings , ServerConfigOptions , SslError ,
106- create_client_config, create_server_config, curve_name_to_kx_group, extract_cipher_info ,
107- get_cipher_encryption_desc , is_blocking_io_error , normalize_cipher_name , ssl_do_handshake,
106+ create_client_config, create_server_config, curve_name_to_kx_group, is_blocking_io_error ,
107+ ssl_do_handshake,
108108 } ;
109109
110110 use super :: providers:: CryptoExt ;
@@ -336,10 +336,10 @@ mod _ssl {
336336 #[ pyattr]
337337 const _OPENSSL_API_VERSION: ( i32 , i32 , i32 , i32 , i32 ) = ( 3 , 3 , 0 , 0 , 15 ) ;
338338
339- // Default cipher list for rustls - using modern secure ciphers
340- # [ pyattr ]
341- const _DEFAULT_CIPHERS : & str =
342- "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256" ;
339+ # [ pyattr ( once ) ]
340+ fn _DEFAULT_CIPHERS ( _vm : & VirtualMachine ) -> String {
341+ cipher :: default_cipher_string ( )
342+ }
343343
344344 // Has features
345345 #[ pyattr]
@@ -1481,15 +1481,13 @@ mod _ssl {
14811481 . upcast ( )
14821482 } ) ?;
14831483
1484- // Those suites stay selected whatever the string said about them,
1485- // in front of it and in the order the provider offers them.
1486- let tls13: Vec < _ > = CryptoExt :: get_ext ( )
1487- . default_ciphers_or_provider ( )
1488- . iter ( )
1489- . filter ( |suite| suite. tls13 ( ) . is_some ( ) && !selected_ciphers. contains ( suite) )
1490- . copied ( )
1491- . collect ( ) ;
1492- let _ = selected_ciphers. splice ( ..0 , tls13) ;
1484+ // TLS 1.3 has a separate OpenSSL setter. Discard whatever this
1485+ // cipher string happened to match there, then restore exactly the
1486+ // provider defaults in their preference order.
1487+ selected_ciphers = cipher:: restore_default_tls13 (
1488+ selected_ciphers,
1489+ CryptoExt :: get_ext ( ) . default_ciphers_or_provider ( ) ,
1490+ ) ;
14931491
14941492 * self . selected_ciphers . write ( ) = Some ( selected_ciphers) ;
14951493 * self . suite_b_kx_groups . write ( ) = suite_b_kx_groups;
@@ -1499,60 +1497,28 @@ mod _ssl {
14991497
15001498 #[ pymethod]
15011499 fn get_ciphers ( & self , vm : & VirtualMachine ) -> PyListRef {
1502- // Dynamically generate cipher list from rustls ALL_CIPHER_SUITES
1503- // This automatically includes all cipher suites supported by the current rustls version
1500+ // What the last `set_ciphers` selected, or the provider defaults
1501+ // when no cipher string was given.
1502+ let selected = self . selected_ciphers . read ( ) . clone ( ) ;
1503+ let suites = selected
1504+ . unwrap_or_else ( || CryptoExt :: get_ext ( ) . default_ciphers_or_provider ( ) . to_vec ( ) ) ;
15041505
1505- let cipher_list = CryptoExt :: get_ext ( )
1506- . all_ciphers_or_default ( )
1506+ let cipher_list = suites
15071507 . iter ( )
15081508 . map ( |suite| {
1509- // Extract cipher information using unified helper
1510- let cipher_info = extract_cipher_info ( suite) ;
1511-
1512- // Convert to OpenSSL-style name
1513- // e.g., "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" -> "ECDHE-RSA-AES128-GCM-SHA256"
1514- let openssl_name = normalize_cipher_name ( & cipher_info. name ) ;
1515-
1516- // Determine key exchange and auth methods
1517- let ( kx, auth) = if cipher_info. protocol == "TLSv1.3" {
1518- // TLS 1.3 doesn't distinguish - all use modern algos
1519- ( "any" , "any" )
1520- } else if cipher_info. name . contains ( "ECDHE" ) {
1521- // TLS 1.2 with ECDHE
1522- let auth = if cipher_info. name . contains ( "ECDSA" ) {
1523- "ECDSA"
1524- } else if cipher_info. name . contains ( "RSA" ) {
1525- "RSA"
1526- } else {
1527- "any"
1528- } ;
1529- ( "ECDH" , auth)
1530- } else {
1531- ( "any" , "any" )
1532- } ;
1533-
1534- // Build description string
1535- // Format: "{name} {protocol} Kx={kx} Au={auth} Enc={enc} Mac={mac}"
1536- let enc = get_cipher_encryption_desc ( & openssl_name) ;
1537-
1538- let description = format ! (
1539- "{} {} Kx={} Au={} Enc={} Mac=AEAD" ,
1540- openssl_name, cipher_info. protocol, kx, auth, enc
1541- ) ;
1542-
1543- // Create cipher dict
1509+ let cipher = cipher:: describe ( suite) ;
15441510 let dict = vm. ctx . new_dict ( ) ;
1545- dict . set_item ( "name" , vm . ctx . new_str ( openssl_name ) . into ( ) , vm )
1546- . unwrap ( ) ;
1547- dict . set_item ( "protocol ", vm. ctx . new_str ( cipher_info . protocol ) . into ( ) , vm )
1548- . unwrap ( ) ;
1549- dict . set_item ( "id ", vm. ctx . new_int ( 0 ) . into ( ) , vm ) . unwrap ( ) ; // Placeholder ID
1550- dict . set_item ( "strength_bits ", vm. ctx . new_int ( cipher_info . bits ) . into ( ) , vm )
1551- . unwrap ( ) ;
1552- dict . set_item ( "alg_bits" , vm . ctx . new_int ( cipher_info . bits ) . into ( ) , vm )
1553- . unwrap ( ) ;
1554- dict. set_item ( "description" , vm . ctx . new_str ( description ) . into ( ) , vm )
1555- . unwrap ( ) ;
1511+ let items : [ ( & str , PyObjectRef ) ; 6 ] = [
1512+ ( "id" , vm . ctx . new_int ( cipher . id ) . into ( ) ) ,
1513+ ( "name ", vm. ctx . new_str ( cipher . name ) . into ( ) ) ,
1514+ ( "protocol" , vm . ctx . new_str ( cipher . protocol ) . into ( ) ) ,
1515+ ( "strength_bits ", vm. ctx . new_int ( cipher . bits ) . into ( ) ) ,
1516+ ( "alg_bits ", vm. ctx . new_int ( cipher . bits ) . into ( ) ) ,
1517+ ( "description" , vm . ctx . new_str ( cipher . description ) . into ( ) ) ,
1518+ ] ;
1519+ for ( key , value ) in items {
1520+ dict. set_item ( key , value , vm ) . unwrap ( ) ;
1521+ }
15561522 dict. into ( )
15571523 } )
15581524 . collect :: < Vec < _ > > ( ) ;
@@ -1788,23 +1754,7 @@ mod _ssl {
17881754 return Err ( vm. new_type_error ( "ECDH curve name must be str or bytes" ) ) ;
17891755 } ;
17901756
1791- // Validate curve name (common curves for compatibility)
1792- // rustls supports: X25519, secp256r1 (prime256v1), secp384r1
1793- let valid_curves = [
1794- "prime256v1" ,
1795- "secp256r1" ,
1796- "prime384v1" ,
1797- "secp384r1" ,
1798- "prime521v1" ,
1799- "secp521r1" ,
1800- "X25519" ,
1801- "x25519" ,
1802- "x448" , // For future compatibility
1803- ] ;
1804-
1805- if !valid_curves. contains ( & curve_name. as_str ( ) ) {
1806- return Err ( vm. new_value_error ( format ! ( "unknown curve name '{curve_name}'" ) ) ) ;
1807- }
1757+ curve_name_to_kx_group ( & curve_name) . map_err ( |error| vm. new_value_error ( error) ) ?;
18081758
18091759 // Store the curve name to be used during handshake
18101760 // This will limit the key exchange groups offered/accepted
@@ -3934,14 +3884,14 @@ mod _ssl {
39343884 } ;
39353885
39363886 // Extract cipher information outside the lock
3937- let cipher_info = extract_cipher_info ( & suite) ;
3887+ let cipher = cipher :: describe ( & suite) ;
39383888
39393889 // Note: returns a 3-tuple (name, protocol_version, bits)
39403890 // The 'description' field is part of get_ciphers() output, not cipher()
39413891 Some ( (
3942- cipher_info . name ,
3943- cipher_info . protocol . to_string ( ) ,
3944- cipher_info . bits ,
3892+ cipher . name . to_owned ( ) ,
3893+ cipher . protocol . to_owned ( ) ,
3894+ cipher . bits . into ( ) ,
39453895 ) )
39463896 }
39473897
@@ -4523,14 +4473,13 @@ mod _ssl {
45234473
45244474 let suite = conn. negotiated_cipher_suite ( ) ?;
45254475
4526- // Extract cipher information using unified helper
4527- let cipher_info = extract_cipher_info ( & suite) ;
4476+ let cipher = cipher:: describe ( & suite) ;
45284477
45294478 // Return as list with single tuple (name, version, bits)
45304479 let tuple = vm. ctx . new_tuple ( vec ! [
4531- vm. ctx. new_str( cipher_info . name) . into( ) ,
4532- vm. ctx. new_str( cipher_info . protocol) . into( ) ,
4533- vm. ctx. new_int( cipher_info . bits) . into( ) ,
4480+ vm. ctx. new_str( cipher . name) . into( ) ,
4481+ vm. ctx. new_str( cipher . protocol) . into( ) ,
4482+ vm. ctx. new_int( cipher . bits) . into( ) ,
45344483 ] ) ;
45354484 Some ( vm. ctx . new_list ( vec ! [ tuple. into( ) ] ) )
45364485 }
0 commit comments