@@ -1826,6 +1826,7 @@ def test_connect_capath(self):
1826
1826
s .connect (self .server_addr )
1827
1827
cert = s .getpeercert ()
1828
1828
self .assertTrue (cert )
1829
+
1829
1830
# Same with a bytes `capath` argument
1830
1831
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1831
1832
ctx .verify_mode = ssl .CERT_REQUIRED
@@ -1841,8 +1842,6 @@ def test_connect_cadata(self):
1841
1842
der = ssl .PEM_cert_to_DER_cert (pem )
1842
1843
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1843
1844
ctx .verify_mode = ssl .CERT_REQUIRED
1844
- # TODO: fix TLSv1.3 support
1845
- ctx .options |= ssl .OP_NO_TLSv1_3
1846
1845
ctx .load_verify_locations (cadata = pem )
1847
1846
with ctx .wrap_socket (socket .socket (socket .AF_INET )) as s :
1848
1847
s .connect (self .server_addr )
@@ -1852,8 +1851,6 @@ def test_connect_cadata(self):
1852
1851
# same with DER
1853
1852
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1854
1853
ctx .verify_mode = ssl .CERT_REQUIRED
1855
- # TODO: fix TLSv1.3 support
1856
- ctx .options |= ssl .OP_NO_TLSv1_3
1857
1854
ctx .load_verify_locations (cadata = der )
1858
1855
with ctx .wrap_socket (socket .socket (socket .AF_INET )) as s :
1859
1856
s .connect (self .server_addr )
@@ -2109,11 +2106,21 @@ def wrap_conn(self):
2109
2106
self .sock , server_side = True )
2110
2107
self .server .selected_npn_protocols .append (self .sslconn .selected_npn_protocol ())
2111
2108
self .server .selected_alpn_protocols .append (self .sslconn .selected_alpn_protocol ())
2112
- except (ssl . SSLError , ConnectionResetError , OSError ) as e :
2109
+ except (ConnectionResetError , BrokenPipeError ) as e :
2113
2110
# We treat ConnectionResetError as though it were an
2114
2111
# SSLError - OpenSSL on Ubuntu abruptly closes the
2115
2112
# connection when asked to use an unsupported protocol.
2116
2113
#
2114
+ # BrokenPipeError is raised in TLS 1.3 mode, when OpenSSL
2115
+ # tries to send session tickets after handshake.
2116
+ # https://github.com/openssl/openssl/issues/6342
2117
+ self .server .conn_errors .append (str (e ))
2118
+ if self .server .chatty :
2119
+ handle_error ("\n server: bad connection attempt from " + repr (self .addr ) + ":\n " )
2120
+ self .running = False
2121
+ self .close ()
2122
+ return False
2123
+ except (ssl .SSLError , OSError ) as e :
2117
2124
# OSError may occur with wrong protocols, e.g. both
2118
2125
# sides use PROTOCOL_TLS_SERVER.
2119
2126
#
@@ -2220,11 +2227,22 @@ def run(self):
2220
2227
sys .stdout .write (" server: read %r (%s), sending back %r (%s)...\n "
2221
2228
% (msg , ctype , msg .lower (), ctype ))
2222
2229
self .write (msg .lower ())
2230
+ except ConnectionResetError :
2231
+ # XXX: OpenSSL 1.1.1 sometimes raises ConnectionResetError
2232
+ # when connection is not shut down gracefully.
2233
+ if self .server .chatty and support .verbose :
2234
+ sys .stdout .write (
2235
+ " Connection reset by peer: {}\n " .format (
2236
+ self .addr )
2237
+ )
2238
+ self .close ()
2239
+ self .running = False
2223
2240
except OSError :
2224
2241
if self .server .chatty :
2225
2242
handle_error ("Test server failure:\n " )
2226
2243
self .close ()
2227
2244
self .running = False
2245
+
2228
2246
# normally, we'd just stop here, but for the test
2229
2247
# harness, we want to stop the server
2230
2248
self .server .stop ()
@@ -2299,6 +2317,11 @@ def run(self):
2299
2317
pass
2300
2318
except KeyboardInterrupt :
2301
2319
self .stop ()
2320
+ except BaseException as e :
2321
+ if support .verbose and self .chatty :
2322
+ sys .stdout .write (
2323
+ ' connection handling failed: ' + repr (e ) + '\n ' )
2324
+
2302
2325
self .sock .close ()
2303
2326
2304
2327
def stop (self ):
@@ -2745,8 +2768,6 @@ def test_check_hostname_idn(self):
2745
2768
2746
2769
server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
2747
2770
server_context .load_cert_chain (IDNSANSFILE )
2748
- # TODO: fix TLSv1.3 support
2749
- server_context .options |= ssl .OP_NO_TLSv1_3
2750
2771
2751
2772
context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
2752
2773
context .verify_mode = ssl .CERT_REQUIRED
@@ -2797,7 +2818,7 @@ def test_check_hostname_idn(self):
2797
2818
with self .assertRaises (ssl .CertificateError ):
2798
2819
s .connect ((HOST , server .port ))
2799
2820
2800
- def test_wrong_cert (self ):
2821
+ def test_wrong_cert_tls12 (self ):
2801
2822
"""Connecting when the server rejects the client's certificate
2802
2823
2803
2824
Launch a server with CERT_REQUIRED, and check that trying to
@@ -2808,9 +2829,8 @@ def test_wrong_cert(self):
2808
2829
client_context .load_cert_chain (WRONG_CERT )
2809
2830
# require TLS client authentication
2810
2831
server_context .verify_mode = ssl .CERT_REQUIRED
2811
- # TODO: fix TLSv1.3 support
2812
- # With TLS 1.3, test fails with exception in server thread
2813
- server_context .options |= ssl .OP_NO_TLSv1_3
2832
+ # TLS 1.3 has different handshake
2833
+ client_context .maximum_version = ssl .TLSVersion .TLSv1_2
2814
2834
2815
2835
server = ThreadedEchoServer (
2816
2836
context = server_context , chatty = True , connectionchatty = True ,
@@ -2835,6 +2855,36 @@ def test_wrong_cert(self):
2835
2855
else :
2836
2856
self .fail ("Use of invalid cert should have failed!" )
2837
2857
2858
+ @unittest .skipUnless (ssl .HAS_TLSv1_3 , "Test needs TLS 1.3" )
2859
+ def test_wrong_cert_tls13 (self ):
2860
+ client_context , server_context , hostname = testing_context ()
2861
+ client_context .load_cert_chain (WRONG_CERT )
2862
+ server_context .verify_mode = ssl .CERT_REQUIRED
2863
+ server_context .minimum_version = ssl .TLSVersion .TLSv1_3
2864
+ client_context .minimum_version = ssl .TLSVersion .TLSv1_3
2865
+
2866
+ server = ThreadedEchoServer (
2867
+ context = server_context , chatty = True , connectionchatty = True ,
2868
+ )
2869
+ with server , \
2870
+ client_context .wrap_socket (socket .socket (),
2871
+ server_hostname = hostname ) as s :
2872
+ # TLS 1.3 perform client cert exchange after handshake
2873
+ s .connect ((HOST , server .port ))
2874
+ try :
2875
+ s .write (b'data' )
2876
+ s .read (4 )
2877
+ except ssl .SSLError as e :
2878
+ if support .verbose :
2879
+ sys .stdout .write ("\n SSLError is %r\n " % e )
2880
+ except OSError as e :
2881
+ if e .errno != errno .ECONNRESET :
2882
+ raise
2883
+ if support .verbose :
2884
+ sys .stdout .write ("\n socket.error is %r\n " % e )
2885
+ else :
2886
+ self .fail ("Use of invalid cert should have failed!" )
2887
+
2838
2888
def test_rude_shutdown (self ):
2839
2889
"""A brutal shutdown of an SSL server should raise an OSError
2840
2890
in the client when attempting handshake.
@@ -3405,14 +3455,16 @@ def serve():
3405
3455
# Block on the accept and wait on the connection to close.
3406
3456
evt .set ()
3407
3457
remote , peer = server .accept ()
3408
- remote .recv (1 )
3458
+ remote .send ( remote . recv (4 ) )
3409
3459
3410
3460
t = threading .Thread (target = serve )
3411
3461
t .start ()
3412
3462
# Client wait until server setup and perform a connect.
3413
3463
evt .wait ()
3414
3464
client = context .wrap_socket (socket .socket ())
3415
3465
client .connect ((host , port ))
3466
+ client .send (b'data' )
3467
+ client .recv ()
3416
3468
client_addr = client .getsockname ()
3417
3469
client .close ()
3418
3470
t .join ()
@@ -3465,7 +3517,7 @@ def test_version_basic(self):
3465
3517
self .assertIs (s .version (), None )
3466
3518
self .assertIs (s ._sslobj , None )
3467
3519
s .connect ((HOST , server .port ))
3468
- if ssl . OPENSSL_VERSION_INFO >= ( 1 , 1 , 1 ) :
3520
+ if IS_OPENSSL_1_1_1 and ssl . HAS_TLSv1_3 :
3469
3521
self .assertEqual (s .version (), 'TLSv1.3' )
3470
3522
elif ssl .OPENSSL_VERSION_INFO >= (1 , 0 , 2 ):
3471
3523
self .assertEqual (s .version (), 'TLSv1.2' )
@@ -3574,8 +3626,6 @@ def test_tls_unique_channel_binding(self):
3574
3626
sys .stdout .write ("\n " )
3575
3627
3576
3628
client_context , server_context , hostname = testing_context ()
3577
- # TODO: fix TLSv1.3 support
3578
- client_context .options |= ssl .OP_NO_TLSv1_3
3579
3629
3580
3630
server = ThreadedEchoServer (context = server_context ,
3581
3631
chatty = True ,
@@ -3594,7 +3644,10 @@ def test_tls_unique_channel_binding(self):
3594
3644
3595
3645
# check if it is sane
3596
3646
self .assertIsNotNone (cb_data )
3597
- self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3647
+ if s .version () == 'TLSv1.3' :
3648
+ self .assertEqual (len (cb_data ), 48 )
3649
+ else :
3650
+ self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3598
3651
3599
3652
# and compare with the peers version
3600
3653
s .write (b"CB tls-unique\n " )
@@ -3616,7 +3669,10 @@ def test_tls_unique_channel_binding(self):
3616
3669
# is it really unique
3617
3670
self .assertNotEqual (cb_data , new_cb_data )
3618
3671
self .assertIsNotNone (cb_data )
3619
- self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3672
+ if s .version () == 'TLSv1.3' :
3673
+ self .assertEqual (len (cb_data ), 48 )
3674
+ else :
3675
+ self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3620
3676
s .write (b"CB tls-unique\n " )
3621
3677
peer_data_repr = s .read ().strip ()
3622
3678
self .assertEqual (peer_data_repr ,
0 commit comments