@@ -1846,6 +1846,7 @@ def test_connect_capath(self):
1846
1846
s .connect (self .server_addr )
1847
1847
cert = s .getpeercert ()
1848
1848
self .assertTrue (cert )
1849
+
1849
1850
# Same with a bytes `capath` argument
1850
1851
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1851
1852
ctx .verify_mode = ssl .CERT_REQUIRED
@@ -1861,8 +1862,6 @@ def test_connect_cadata(self):
1861
1862
der = ssl .PEM_cert_to_DER_cert (pem )
1862
1863
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1863
1864
ctx .verify_mode = ssl .CERT_REQUIRED
1864
- # TODO: fix TLSv1.3 support
1865
- ctx .options |= ssl .OP_NO_TLSv1_3
1866
1865
ctx .load_verify_locations (cadata = pem )
1867
1866
with ctx .wrap_socket (socket .socket (socket .AF_INET )) as s :
1868
1867
s .connect (self .server_addr )
@@ -1872,8 +1871,6 @@ def test_connect_cadata(self):
1872
1871
# same with DER
1873
1872
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1874
1873
ctx .verify_mode = ssl .CERT_REQUIRED
1875
- # TODO: fix TLSv1.3 support
1876
- ctx .options |= ssl .OP_NO_TLSv1_3
1877
1874
ctx .load_verify_locations (cadata = der )
1878
1875
with ctx .wrap_socket (socket .socket (socket .AF_INET )) as s :
1879
1876
s .connect (self .server_addr )
@@ -2129,11 +2126,21 @@ def wrap_conn(self):
2129
2126
self .sock , server_side = True )
2130
2127
self .server .selected_npn_protocols .append (self .sslconn .selected_npn_protocol ())
2131
2128
self .server .selected_alpn_protocols .append (self .sslconn .selected_alpn_protocol ())
2132
- except (ssl . SSLError , ConnectionResetError , OSError ) as e :
2129
+ except (ConnectionResetError , BrokenPipeError ) as e :
2133
2130
# We treat ConnectionResetError as though it were an
2134
2131
# SSLError - OpenSSL on Ubuntu abruptly closes the
2135
2132
# connection when asked to use an unsupported protocol.
2136
2133
#
2134
+ # BrokenPipeError is raised in TLS 1.3 mode, when OpenSSL
2135
+ # tries to send session tickets after handshake.
2136
+ # https://github.com/openssl/openssl/issues/6342
2137
+ self .server .conn_errors .append (str (e ))
2138
+ if self .server .chatty :
2139
+ handle_error ("\n server: bad connection attempt from " + repr (self .addr ) + ":\n " )
2140
+ self .running = False
2141
+ self .close ()
2142
+ return False
2143
+ except (ssl .SSLError , OSError ) as e :
2137
2144
# OSError may occur with wrong protocols, e.g. both
2138
2145
# sides use PROTOCOL_TLS_SERVER.
2139
2146
#
@@ -2240,11 +2247,22 @@ def run(self):
2240
2247
sys .stdout .write (" server: read %r (%s), sending back %r (%s)...\n "
2241
2248
% (msg , ctype , msg .lower (), ctype ))
2242
2249
self .write (msg .lower ())
2250
+ except ConnectionResetError :
2251
+ # XXX: OpenSSL 1.1.1 sometimes raises ConnectionResetError
2252
+ # when connection is not shut down gracefully.
2253
+ if self .server .chatty and support .verbose :
2254
+ sys .stdout .write (
2255
+ " Connection reset by peer: {}\n " .format (
2256
+ self .addr )
2257
+ )
2258
+ self .close ()
2259
+ self .running = False
2243
2260
except OSError :
2244
2261
if self .server .chatty :
2245
2262
handle_error ("Test server failure:\n " )
2246
2263
self .close ()
2247
2264
self .running = False
2265
+
2248
2266
# normally, we'd just stop here, but for the test
2249
2267
# harness, we want to stop the server
2250
2268
self .server .stop ()
@@ -2319,6 +2337,11 @@ def run(self):
2319
2337
pass
2320
2338
except KeyboardInterrupt :
2321
2339
self .stop ()
2340
+ except BaseException as e :
2341
+ if support .verbose and self .chatty :
2342
+ sys .stdout .write (
2343
+ ' connection handling failed: ' + repr (e ) + '\n ' )
2344
+
2322
2345
self .sock .close ()
2323
2346
2324
2347
def stop (self ):
@@ -2766,8 +2789,6 @@ def test_check_hostname_idn(self):
2766
2789
2767
2790
server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
2768
2791
server_context .load_cert_chain (IDNSANSFILE )
2769
- # TODO: fix TLSv1.3 support
2770
- server_context .options |= ssl .OP_NO_TLSv1_3
2771
2792
2772
2793
context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
2773
2794
context .verify_mode = ssl .CERT_REQUIRED
@@ -2818,7 +2839,7 @@ def test_check_hostname_idn(self):
2818
2839
with self .assertRaises (ssl .CertificateError ):
2819
2840
s .connect ((HOST , server .port ))
2820
2841
2821
- def test_wrong_cert (self ):
2842
+ def test_wrong_cert_tls12 (self ):
2822
2843
"""Connecting when the server rejects the client's certificate
2823
2844
2824
2845
Launch a server with CERT_REQUIRED, and check that trying to
@@ -2829,9 +2850,8 @@ def test_wrong_cert(self):
2829
2850
client_context .load_cert_chain (WRONG_CERT )
2830
2851
# require TLS client authentication
2831
2852
server_context .verify_mode = ssl .CERT_REQUIRED
2832
- # TODO: fix TLSv1.3 support
2833
- # With TLS 1.3, test fails with exception in server thread
2834
- server_context .options |= ssl .OP_NO_TLSv1_3
2853
+ # TLS 1.3 has different handshake
2854
+ client_context .maximum_version = ssl .TLSVersion .TLSv1_2
2835
2855
2836
2856
server = ThreadedEchoServer (
2837
2857
context = server_context , chatty = True , connectionchatty = True ,
@@ -2856,6 +2876,36 @@ def test_wrong_cert(self):
2856
2876
else :
2857
2877
self .fail ("Use of invalid cert should have failed!" )
2858
2878
2879
+ @unittest .skipUnless (ssl .HAS_TLSv1_3 , "Test needs TLS 1.3" )
2880
+ def test_wrong_cert_tls13 (self ):
2881
+ client_context , server_context , hostname = testing_context ()
2882
+ client_context .load_cert_chain (WRONG_CERT )
2883
+ server_context .verify_mode = ssl .CERT_REQUIRED
2884
+ server_context .minimum_version = ssl .TLSVersion .TLSv1_3
2885
+ client_context .minimum_version = ssl .TLSVersion .TLSv1_3
2886
+
2887
+ server = ThreadedEchoServer (
2888
+ context = server_context , chatty = True , connectionchatty = True ,
2889
+ )
2890
+ with server , \
2891
+ client_context .wrap_socket (socket .socket (),
2892
+ server_hostname = hostname ) as s :
2893
+ # TLS 1.3 perform client cert exchange after handshake
2894
+ s .connect ((HOST , server .port ))
2895
+ try :
2896
+ s .write (b'data' )
2897
+ s .read (4 )
2898
+ except ssl .SSLError as e :
2899
+ if support .verbose :
2900
+ sys .stdout .write ("\n SSLError is %r\n " % e )
2901
+ except OSError as e :
2902
+ if e .errno != errno .ECONNRESET :
2903
+ raise
2904
+ if support .verbose :
2905
+ sys .stdout .write ("\n socket.error is %r\n " % e )
2906
+ else :
2907
+ self .fail ("Use of invalid cert should have failed!" )
2908
+
2859
2909
def test_rude_shutdown (self ):
2860
2910
"""A brutal shutdown of an SSL server should raise an OSError
2861
2911
in the client when attempting handshake.
@@ -3432,14 +3482,16 @@ def serve():
3432
3482
# Block on the accept and wait on the connection to close.
3433
3483
evt .set ()
3434
3484
remote , peer = server .accept ()
3435
- remote .recv (1 )
3485
+ remote .send ( remote . recv (4 ) )
3436
3486
3437
3487
t = threading .Thread (target = serve )
3438
3488
t .start ()
3439
3489
# Client wait until server setup and perform a connect.
3440
3490
evt .wait ()
3441
3491
client = context .wrap_socket (socket .socket ())
3442
3492
client .connect ((host , port ))
3493
+ client .send (b'data' )
3494
+ client .recv ()
3443
3495
client_addr = client .getsockname ()
3444
3496
client .close ()
3445
3497
t .join ()
@@ -3492,7 +3544,7 @@ def test_version_basic(self):
3492
3544
self .assertIs (s .version (), None )
3493
3545
self .assertIs (s ._sslobj , None )
3494
3546
s .connect ((HOST , server .port ))
3495
- if ssl . OPENSSL_VERSION_INFO >= ( 1 , 1 , 1 ) :
3547
+ if IS_OPENSSL_1_1_1 and ssl . HAS_TLSv1_3 :
3496
3548
self .assertEqual (s .version (), 'TLSv1.3' )
3497
3549
elif ssl .OPENSSL_VERSION_INFO >= (1 , 0 , 2 ):
3498
3550
self .assertEqual (s .version (), 'TLSv1.2' )
@@ -3601,8 +3653,6 @@ def test_tls_unique_channel_binding(self):
3601
3653
sys .stdout .write ("\n " )
3602
3654
3603
3655
client_context , server_context , hostname = testing_context ()
3604
- # TODO: fix TLSv1.3 support
3605
- client_context .options |= ssl .OP_NO_TLSv1_3
3606
3656
3607
3657
server = ThreadedEchoServer (context = server_context ,
3608
3658
chatty = True ,
@@ -3621,7 +3671,10 @@ def test_tls_unique_channel_binding(self):
3621
3671
3622
3672
# check if it is sane
3623
3673
self .assertIsNotNone (cb_data )
3624
- self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3674
+ if s .version () == 'TLSv1.3' :
3675
+ self .assertEqual (len (cb_data ), 48 )
3676
+ else :
3677
+ self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3625
3678
3626
3679
# and compare with the peers version
3627
3680
s .write (b"CB tls-unique\n " )
@@ -3643,7 +3696,10 @@ def test_tls_unique_channel_binding(self):
3643
3696
# is it really unique
3644
3697
self .assertNotEqual (cb_data , new_cb_data )
3645
3698
self .assertIsNotNone (cb_data )
3646
- self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3699
+ if s .version () == 'TLSv1.3' :
3700
+ self .assertEqual (len (cb_data ), 48 )
3701
+ else :
3702
+ self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3647
3703
s .write (b"CB tls-unique\n " )
3648
3704
peer_data_repr = s .read ().strip ()
3649
3705
self .assertEqual (peer_data_repr ,
0 commit comments