@@ -2392,7 +2392,10 @@ def wrap_conn(self):
23922392 sys .stdout .write (" client cert is " + pprint .pformat (cert ) + "\n " )
23932393 cert_binary = self .sslconn .getpeercert (True )
23942394 if support .verbose and self .server .chatty :
2395- sys .stdout .write (" cert binary is " + str (len (cert_binary )) + " bytes\n " )
2395+ if cert_binary is None :
2396+ sys .stdout .write (" client did not provide a cert\n " )
2397+ else :
2398+ sys .stdout .write (f" cert binary is { len (cert_binary )} b\n " )
23962399 cipher = self .sslconn .cipher ()
23972400 if support .verbose and self .server .chatty :
23982401 sys .stdout .write (" server: connection cipher is now " + str (cipher ) + "\n " )
@@ -2490,31 +2493,22 @@ def run(self):
24902493 sys .stdout .write (" server: read %r (%s), sending back %r (%s)...\n "
24912494 % (msg , ctype , msg .lower (), ctype ))
24922495 self .write (msg .lower ())
2493- except (ConnectionResetError , ConnectionAbortedError ):
2494- # XXX: OpenSSL 1.1.1 sometimes raises ConnectionResetError
2495- # when connection is not shut down gracefully.
2496+ except OSError as e :
2497+ # handles SSLError and socket errors
24962498 if self .server .chatty and support .verbose :
2497- sys .stdout .write (
2498- " Connection reset by peer: {}\n " .format (
2499- self .addr )
2500- )
2501- self .close ()
2502- self .running = False
2503- except ssl .SSLError as err :
2504- # On Windows sometimes test_pha_required_nocert receives the
2505- # PEER_DID_NOT_RETURN_A_CERTIFICATE exception
2506- # before the 'tlsv13 alert certificate required' exception.
2507- # If the server is stopped when PEER_DID_NOT_RETURN_A_CERTIFICATE
2508- # is received test_pha_required_nocert fails with ConnectionResetError
2509- # because the underlying socket is closed
2510- if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' == err .reason :
2511- if self .server .chatty and support .verbose :
2512- sys .stdout .write (err .args [1 ])
2513- # test_pha_required_nocert is expecting this exception
2514- raise ssl .SSLError ('tlsv13 alert certificate required' )
2515- except OSError :
2516- if self .server .chatty :
2517- handle_error ("Test server failure:\n " )
2499+ if isinstance (e , ConnectionError ):
2500+ # OpenSSL 1.1.1 sometimes raises
2501+ # ConnectionResetError when connection is not
2502+ # shut down gracefully.
2503+ print (
2504+ f" Connection reset by peer: { self .addr } "
2505+ )
2506+ else :
2507+ handle_error ("Test server failure:\n " )
2508+ try :
2509+ self .write (b"ERROR\n " )
2510+ except OSError :
2511+ pass
25182512 self .close ()
25192513 self .running = False
25202514
@@ -4496,24 +4490,30 @@ def test_pha_required_nocert(self):
44964490 server_context .verify_mode = ssl .CERT_REQUIRED
44974491 client_context .post_handshake_auth = True
44984492
4499- # Ignore expected SSLError in ConnectionHandler of ThreadedEchoServer
4500- # (it is only raised sometimes on Windows)
4501- with support .catch_threading_exception () as cm :
4502- server = ThreadedEchoServer (context = server_context , chatty = False )
4503- with server :
4504- with client_context .wrap_socket (socket .socket (),
4505- server_hostname = hostname ) as s :
4506- s .connect ((HOST , server .port ))
4507- s .write (b'PHA' )
4493+ def msg_cb (conn , direction , version , content_type , msg_type , data ):
4494+ if support .verbose and content_type == _TLSContentType .ALERT :
4495+ info = (conn , direction , version , content_type , msg_type , data )
4496+ sys .stdout .write (f"TLS: { info !r} \n " )
4497+
4498+ server_context ._msg_callback = msg_cb
4499+ client_context ._msg_callback = msg_cb
4500+
4501+ server = ThreadedEchoServer (context = server_context , chatty = True )
4502+ with server :
4503+ with client_context .wrap_socket (socket .socket (),
4504+ server_hostname = hostname ) as s :
4505+ s .connect ((HOST , server .port ))
4506+ s .write (b'PHA' )
4507+ with self .assertRaisesRegex (
4508+ ssl .SSLError ,
4509+ 'tlsv13 alert certificate required'
4510+ ):
45084511 # receive CertificateRequest
45094512 self .assertEqual (s .recv (1024 ), b'OK\n ' )
45104513 # send empty Certificate + Finish
45114514 s .write (b'HASCERT' )
45124515 # receive alert
4513- with self .assertRaisesRegex (
4514- ssl .SSLError ,
4515- 'tlsv13 alert certificate required' ):
4516- s .recv (1024 )
4516+ s .recv (1024 )
45174517
45184518 def test_pha_optional (self ):
45194519 if support .verbose :
0 commit comments