38
38
HOST = support .HOST
39
39
IS_LIBRESSL = ssl .OPENSSL_VERSION .startswith ('LibreSSL' )
40
40
IS_OPENSSL_1_1 = not IS_LIBRESSL and ssl .OPENSSL_VERSION_INFO >= (1 , 1 , 0 )
41
+ IS_OPENSSL_3_0_0 = not IS_LIBRESSL and ssl .OPENSSL_VERSION_INFO >= (3 , 0 , 0 )
41
42
PY_SSL_DEFAULT_CIPHERS = sysconfig .get_config_var ('PY_SSL_DEFAULT_CIPHERS' )
42
43
43
44
def data_file (* name ):
@@ -148,8 +149,8 @@ def f(*args, **kwargs):
148
149
else :
149
150
return func
150
151
151
- def skip_if_openssl_cnf_minprotocol_gt_tls1 (func ):
152
- """Skip a test if the OpenSSL config MinProtocol is > TLSv1.
152
+ def skip_if_openssl_cnf_minprotocol_gt_tls11 (func ):
153
+ """Skip a test if the OpenSSL config MinProtocol is > TLSv1.1.
153
154
154
155
OS distros with an /etc/ssl/openssl.cnf and MinProtocol set often do so to
155
156
require TLSv1.2 or higher (Debian Buster). Some of our tests for older
@@ -160,14 +161,16 @@ def skip_if_openssl_cnf_minprotocol_gt_tls1(func):
160
161
"""
161
162
@functools .wraps (func )
162
163
def f (* args , ** kwargs ):
164
+ if IS_OPENSSL_3_0_0 :
165
+ raise unittest .SkipTest ('OpenSSL 3 effectively disables TLS < 1.2' )
163
166
openssl_cnf = os .environ .get ("OPENSSL_CONF" , "/etc/ssl/openssl.cnf" )
164
167
try :
165
168
with open (openssl_cnf , "r" ) as config :
166
169
for line in config :
167
170
match = re .match (r"MinProtocol\s*=\s*(TLSv\d+\S*)" , line )
168
171
if match :
169
172
tls_ver = match .group (1 )
170
- if tls_ver > "TLSv1" :
173
+ if tls_ver > "TLSv1.1 " :
171
174
raise unittest .SkipTest (
172
175
"%s has MinProtocol = %s which is > TLSv1." %
173
176
(openssl_cnf , tls_ver ))
@@ -1421,7 +1424,7 @@ def test__create_stdlib_context(self):
1421
1424
self ._assert_context_options (ctx )
1422
1425
1423
1426
def test_check_hostname (self ):
1424
- ctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
1427
+ ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1425
1428
self .assertFalse (ctx .check_hostname )
1426
1429
1427
1430
# Requires CERT_REQUIRED or CERT_OPTIONAL
@@ -1479,7 +1482,7 @@ def test_lib_reason(self):
1479
1482
def test_subclass (self ):
1480
1483
# Check that the appropriate SSLError subclass is raised
1481
1484
# (this only tests one of them)
1482
- ctx = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
1485
+ ctx = ssl .SSLContext (ssl .PROTOCOL_TLS )
1483
1486
with socket .socket () as s :
1484
1487
s .bind (("127.0.0.1" , 0 ))
1485
1488
s .listen ()
@@ -2422,7 +2425,8 @@ def test_echo(self):
2422
2425
if support .verbose :
2423
2426
sys .stdout .write ("\n " )
2424
2427
for protocol in PROTOCOLS :
2425
- if protocol in {ssl .PROTOCOL_TLS_CLIENT , ssl .PROTOCOL_TLS_SERVER }:
2428
+ if protocol in {ssl .PROTOCOL_TLS_CLIENT , ssl .PROTOCOL_TLS_SERVER ,
2429
+ ssl .PROTOCOL_TLSv1 , ssl .PROTOCOL_TLSv1_1 }:
2426
2430
continue
2427
2431
with self .subTest (protocol = ssl ._PROTOCOL_NAMES [protocol ]):
2428
2432
context = ssl .SSLContext (protocol )
@@ -2513,10 +2517,10 @@ def test_crl_check(self):
2513
2517
if support .verbose :
2514
2518
sys .stdout .write ("\n " )
2515
2519
2516
- server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
2520
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
2517
2521
server_context .load_cert_chain (SIGNED_CERTFILE )
2518
2522
2519
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
2523
+ context = ssl .SSLContext (ssl .PROTOCOL_TLS )
2520
2524
context .verify_mode = ssl .CERT_REQUIRED
2521
2525
context .load_verify_locations (SIGNING_CA )
2522
2526
tf = getattr (ssl , "VERIFY_X509_TRUSTED_FIRST" , 0 )
@@ -2554,10 +2558,10 @@ def test_check_hostname(self):
2554
2558
if support .verbose :
2555
2559
sys .stdout .write ("\n " )
2556
2560
2557
- server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
2561
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
2558
2562
server_context .load_cert_chain (SIGNED_CERTFILE )
2559
2563
2560
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
2564
+ context = ssl .SSLContext (ssl .PROTOCOL_TLS )
2561
2565
context .verify_mode = ssl .CERT_REQUIRED
2562
2566
context .check_hostname = True
2563
2567
context .load_verify_locations (SIGNING_CA )
@@ -2687,7 +2691,7 @@ def test_protocol_sslv2(self):
2687
2691
client_options = ssl .OP_NO_TLSv1 )
2688
2692
2689
2693
@skip_if_broken_ubuntu_ssl
2690
- @skip_if_openssl_cnf_minprotocol_gt_tls1
2694
+ @skip_if_openssl_cnf_minprotocol_gt_tls11
2691
2695
def test_protocol_sslv23 (self ):
2692
2696
"""Connecting to an SSLv23 server with various client options"""
2693
2697
if support .verbose :
@@ -2748,6 +2752,7 @@ def test_protocol_sslv3(self):
2748
2752
False , client_options = ssl .OP_NO_SSLv2 )
2749
2753
2750
2754
@skip_if_broken_ubuntu_ssl
2755
+ @skip_if_openssl_cnf_minprotocol_gt_tls11
2751
2756
def test_protocol_tlsv1 (self ):
2752
2757
"""Connecting to a TLSv1 server with various client options"""
2753
2758
if support .verbose :
@@ -2765,7 +2770,7 @@ def test_protocol_tlsv1(self):
2765
2770
@skip_if_broken_ubuntu_ssl
2766
2771
@unittest .skipUnless (hasattr (ssl , "PROTOCOL_TLSv1_1" ),
2767
2772
"TLS version 1.1 not supported." )
2768
- @skip_if_openssl_cnf_minprotocol_gt_tls1
2773
+ @skip_if_openssl_cnf_minprotocol_gt_tls11
2769
2774
def test_protocol_tlsv1_1 (self ):
2770
2775
"""Connecting to a TLSv1.1 server with various client options.
2771
2776
Testing against older TLS versions."""
@@ -2813,7 +2818,7 @@ def test_starttls(self):
2813
2818
msgs = (b"msg 1" , b"MSG 2" , b"STARTTLS" , b"MSG 3" , b"msg 4" , b"ENDTLS" , b"msg 5" , b"msg 6" )
2814
2819
2815
2820
server = ThreadedEchoServer (CERTFILE ,
2816
- ssl_version = ssl .PROTOCOL_TLSv1 ,
2821
+ ssl_version = ssl .PROTOCOL_TLS ,
2817
2822
starttls_server = True ,
2818
2823
chatty = True ,
2819
2824
connectionchatty = True )
@@ -2841,7 +2846,7 @@ def test_starttls(self):
2841
2846
sys .stdout .write (
2842
2847
" client: read %r from server, starting TLS...\n "
2843
2848
% msg )
2844
- conn = test_wrap_socket (s , ssl_version = ssl .PROTOCOL_TLSv1 )
2849
+ conn = test_wrap_socket (s , ssl_version = ssl .PROTOCOL_TLS )
2845
2850
wrapped = True
2846
2851
elif indata == b"ENDTLS" and msg .startswith (b"ok" ):
2847
2852
# ENDTLS ok, switch back to clear text
@@ -2928,7 +2933,7 @@ def test_recv_send(self):
2928
2933
2929
2934
server = ThreadedEchoServer (CERTFILE ,
2930
2935
certreqs = ssl .CERT_NONE ,
2931
- ssl_version = ssl .PROTOCOL_TLSv1 ,
2936
+ ssl_version = ssl .PROTOCOL_TLS ,
2932
2937
cacerts = CERTFILE ,
2933
2938
chatty = True ,
2934
2939
connectionchatty = False )
@@ -2938,7 +2943,7 @@ def test_recv_send(self):
2938
2943
certfile = CERTFILE ,
2939
2944
ca_certs = CERTFILE ,
2940
2945
cert_reqs = ssl .CERT_NONE ,
2941
- ssl_version = ssl .PROTOCOL_TLSv1 )
2946
+ ssl_version = ssl .PROTOCOL_TLS )
2942
2947
s .connect ((HOST , server .port ))
2943
2948
# helper methods for standardising recv* method signatures
2944
2949
def _recv_into ():
@@ -3080,7 +3085,7 @@ def test_recv_zero(self):
3080
3085
def test_nonblocking_send (self ):
3081
3086
server = ThreadedEchoServer (CERTFILE ,
3082
3087
certreqs = ssl .CERT_NONE ,
3083
- ssl_version = ssl .PROTOCOL_TLSv1 ,
3088
+ ssl_version = ssl .PROTOCOL_TLS ,
3084
3089
cacerts = CERTFILE ,
3085
3090
chatty = True ,
3086
3091
connectionchatty = False )
@@ -3090,7 +3095,7 @@ def test_nonblocking_send(self):
3090
3095
certfile = CERTFILE ,
3091
3096
ca_certs = CERTFILE ,
3092
3097
cert_reqs = ssl .CERT_NONE ,
3093
- ssl_version = ssl .PROTOCOL_TLSv1 )
3098
+ ssl_version = ssl .PROTOCOL_TLS )
3094
3099
s .connect ((HOST , server .port ))
3095
3100
s .setblocking (False )
3096
3101
@@ -3236,14 +3241,14 @@ def test_version_basic(self):
3236
3241
Basic tests for SSLSocket.version().
3237
3242
More tests are done in the test_protocol_*() methods.
3238
3243
"""
3239
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3244
+ context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3240
3245
with ThreadedEchoServer (CERTFILE ,
3241
- ssl_version = ssl .PROTOCOL_TLSv1 ,
3246
+ ssl_version = ssl .PROTOCOL_TLS ,
3242
3247
chatty = False ) as server :
3243
3248
with context .wrap_socket (socket .socket ()) as s :
3244
3249
self .assertIs (s .version (), None )
3245
3250
s .connect ((HOST , server .port ))
3246
- self .assertEqual (s .version (), 'TLSv1' )
3251
+ self .assertEqual (s .version (), 'TLSv1.3 ' )
3247
3252
self .assertIs (s .version (), None )
3248
3253
3249
3254
@unittest .skipUnless (ssl .HAS_TLSv1_3 ,
@@ -3293,7 +3298,7 @@ def test_tls_unique_channel_binding(self):
3293
3298
3294
3299
server = ThreadedEchoServer (CERTFILE ,
3295
3300
certreqs = ssl .CERT_NONE ,
3296
- ssl_version = ssl .PROTOCOL_TLSv1 ,
3301
+ ssl_version = ssl .PROTOCOL_TLS ,
3297
3302
cacerts = CERTFILE ,
3298
3303
chatty = True ,
3299
3304
connectionchatty = False )
@@ -3303,7 +3308,7 @@ def test_tls_unique_channel_binding(self):
3303
3308
certfile = CERTFILE ,
3304
3309
ca_certs = CERTFILE ,
3305
3310
cert_reqs = ssl .CERT_NONE ,
3306
- ssl_version = ssl .PROTOCOL_TLSv1 )
3311
+ ssl_version = ssl .PROTOCOL_TLS )
3307
3312
s .connect ((HOST , server .port ))
3308
3313
# get the data
3309
3314
cb_data = s .get_channel_binding ("tls-unique" )
@@ -3313,7 +3318,10 @@ def test_tls_unique_channel_binding(self):
3313
3318
3314
3319
# check if it is sane
3315
3320
self .assertIsNotNone (cb_data )
3316
- self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3321
+ if s .version () == 'TLSv1.3' :
3322
+ self .assertEqual (len (cb_data ), 48 )
3323
+ else :
3324
+ self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3317
3325
3318
3326
# and compare with the peers version
3319
3327
s .write (b"CB tls-unique\n " )
@@ -3328,7 +3336,7 @@ def test_tls_unique_channel_binding(self):
3328
3336
certfile = CERTFILE ,
3329
3337
ca_certs = CERTFILE ,
3330
3338
cert_reqs = ssl .CERT_NONE ,
3331
- ssl_version = ssl .PROTOCOL_TLSv1 )
3339
+ ssl_version = ssl .PROTOCOL_TLS )
3332
3340
s .connect ((HOST , server .port ))
3333
3341
new_cb_data = s .get_channel_binding ("tls-unique" )
3334
3342
if support .verbose :
@@ -3337,15 +3345,18 @@ def test_tls_unique_channel_binding(self):
3337
3345
# is it really unique
3338
3346
self .assertNotEqual (cb_data , new_cb_data )
3339
3347
self .assertIsNotNone (cb_data )
3340
- self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3348
+ if s .version () == 'TLSv1.3' :
3349
+ self .assertEqual (len (cb_data ), 48 )
3350
+ else :
3351
+ self .assertEqual (len (cb_data ), 12 ) # True for TLSv1
3341
3352
s .write (b"CB tls-unique\n " )
3342
3353
peer_data_repr = s .read ().strip ()
3343
3354
self .assertEqual (peer_data_repr ,
3344
3355
repr (new_cb_data ).encode ("us-ascii" ))
3345
3356
s .close ()
3346
3357
3347
3358
def test_compression (self ):
3348
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3359
+ context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3349
3360
context .load_cert_chain (CERTFILE )
3350
3361
stats = server_params_test (context , context ,
3351
3362
chatty = True , connectionchatty = True )
@@ -3356,7 +3367,7 @@ def test_compression(self):
3356
3367
@unittest .skipUnless (hasattr (ssl , 'OP_NO_COMPRESSION' ),
3357
3368
"ssl.OP_NO_COMPRESSION needed for this test" )
3358
3369
def test_compression_disabled (self ):
3359
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3370
+ context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3360
3371
context .load_cert_chain (CERTFILE )
3361
3372
context .options |= ssl .OP_NO_COMPRESSION
3362
3373
stats = server_params_test (context , context ,
@@ -3365,7 +3376,7 @@ def test_compression_disabled(self):
3365
3376
3366
3377
def test_dh_params (self ):
3367
3378
# Check we can get a connection with ephemeral Diffie-Hellman
3368
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3379
+ context = ssl .SSLContext (ssl .PROTOCOL_TLSv1_2 )
3369
3380
context .load_cert_chain (CERTFILE )
3370
3381
context .load_dh_params (DHFILE )
3371
3382
context .set_ciphers ("kEDH" )
@@ -3378,7 +3389,7 @@ def test_dh_params(self):
3378
3389
3379
3390
def test_selected_alpn_protocol (self ):
3380
3391
# selected_alpn_protocol() is None unless ALPN is used.
3381
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3392
+ context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3382
3393
context .load_cert_chain (CERTFILE )
3383
3394
stats = server_params_test (context , context ,
3384
3395
chatty = True , connectionchatty = True )
@@ -3387,9 +3398,9 @@ def test_selected_alpn_protocol(self):
3387
3398
@unittest .skipUnless (ssl .HAS_ALPN , "ALPN support required" )
3388
3399
def test_selected_alpn_protocol_if_server_uses_alpn (self ):
3389
3400
# selected_alpn_protocol() is None unless ALPN is used by the client.
3390
- client_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3401
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3391
3402
client_context .load_verify_locations (CERTFILE )
3392
- server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3403
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3393
3404
server_context .load_cert_chain (CERTFILE )
3394
3405
server_context .set_alpn_protocols (['foo' , 'bar' ])
3395
3406
stats = server_params_test (client_context , server_context ,
@@ -3440,7 +3451,7 @@ def test_alpn_protocols(self):
3440
3451
3441
3452
def test_selected_npn_protocol (self ):
3442
3453
# selected_npn_protocol() is None unless NPN is used
3443
- context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3454
+ context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3444
3455
context .load_cert_chain (CERTFILE )
3445
3456
stats = server_params_test (context , context ,
3446
3457
chatty = True , connectionchatty = True )
@@ -3476,11 +3487,11 @@ def test_npn_protocols(self):
3476
3487
self .assertEqual (server_result , expected , msg % (server_result , "server" ))
3477
3488
3478
3489
def sni_contexts (self ):
3479
- server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3490
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3480
3491
server_context .load_cert_chain (SIGNED_CERTFILE )
3481
- other_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3492
+ other_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3482
3493
other_context .load_cert_chain (SIGNED_CERTFILE2 )
3483
- client_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3494
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3484
3495
client_context .verify_mode = ssl .CERT_REQUIRED
3485
3496
client_context .load_verify_locations (SIGNING_CA )
3486
3497
return server_context , other_context , client_context
@@ -3579,9 +3590,9 @@ def cb_wrong_return_type(ssl_sock, server_name, initial_context):
3579
3590
self .assertIn ("TypeError" , stderr .getvalue ())
3580
3591
3581
3592
def test_shared_ciphers (self ):
3582
- server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3593
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3583
3594
server_context .load_cert_chain (SIGNED_CERTFILE )
3584
- client_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3595
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS )
3585
3596
client_context .verify_mode = ssl .CERT_REQUIRED
3586
3597
client_context .load_verify_locations (SIGNING_CA )
3587
3598
if ssl .OPENSSL_VERSION_INFO >= (1 , 0 , 2 ):
@@ -3641,9 +3652,9 @@ def test_sendfile(self):
3641
3652
self .assertEqual (s .recv (1024 ), TEST_DATA )
3642
3653
3643
3654
def test_session (self ):
3644
- server_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3655
+ server_context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
3645
3656
server_context .load_cert_chain (SIGNED_CERTFILE )
3646
- client_context = ssl .SSLContext (ssl .PROTOCOL_TLSv1 )
3657
+ client_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
3647
3658
client_context .verify_mode = ssl .CERT_REQUIRED
3648
3659
client_context .load_verify_locations (SIGNING_CA )
3649
3660
0 commit comments