@@ -298,6 +298,57 @@ def test_sslcontext_verify_full(self):
298298 res = self ._query_and_fetchone (self .SSL_STATE_SQL )
299299 self .assertEqual (res [0 ], 'Server' )
300300
301+ def test_tls13_support_auto_negotiation (self ):
302+ """
303+ Verify that the client supports TLS 1.3 negotiation.
304+ If the server supports TLS 1.3, the connection should establish using it.
305+ If the server supports only TLS 1.2, the connection should still succeed.
306+ """
307+
308+ # Set up server certificates and enable TLS
309+ CA_cert = self ._generate_and_set_certificates ()
310+
311+ # Create SSL context allowing both TLS 1.2 and 1.3
312+ ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
313+ ssl_context .verify_mode = ssl .CERT_REQUIRED
314+ ssl_context .check_hostname = True
315+ ssl_context .load_verify_locations (cadata = CA_cert )
316+
317+ # Assign SSL context to connection info
318+ self ._conn_info ['ssl' ] = ssl_context
319+
320+ with self ._connect () as conn :
321+ cur = conn .cursor ()
322+ res = self ._query_and_fetchone (self .SSL_STATE_SQL )
323+ self .assertEqual (res [0 ], 'Server' )
324+
325+ # Try to get the negotiated TLS version from the socket
326+ tls_version = None
327+ try :
328+ if hasattr (conn ._socket , "_sslobj" ):
329+ tls_version = conn ._socket ._sslobj .version ()
330+ elif hasattr (conn ._socket , "version" ):
331+ tls_version = conn ._socket .version ()
332+ except Exception :
333+ pass
334+
335+ # Log version for debug (optional)
336+ print (f"Negotiated TLS version: { tls_version } " )
337+
338+ # Ensure TLS negotiation was successful
339+ self .assertIsNotNone (tls_version , "Could not determine negotiated TLS version" )
340+
341+ # Accept both 1.2 and 1.3, but prefer 1.3 if available
342+ self .assertIn (
343+ tls_version , ("TLSv1.2" , "TLSv1.3" ),
344+ msg = f"Unexpected TLS version negotiated: { tls_version } "
345+ )
346+
347+ if tls_version == "TLSv1.3" :
348+ print ("TLS 1.3 is successfully negotiated and supported." )
349+ else :
350+ print ("Fell back to TLS 1.2 (TLS 1.3 not supported by server)." )
351+
301352 def test_sslcontext_mutual_TLS (self ):
302353 # Setting certificates with TLS configuration
303354 CA_cert = self ._generate_and_set_certificates (mutual_mode = True )
0 commit comments