@@ -142,6 +142,27 @@ def data_file(*name):
142
142
OP_CIPHER_SERVER_PREFERENCE = getattr (ssl , "OP_CIPHER_SERVER_PREFERENCE" , 0 )
143
143
OP_ENABLE_MIDDLEBOX_COMPAT = getattr (ssl , "OP_ENABLE_MIDDLEBOX_COMPAT" , 0 )
144
144
145
+ # Ubuntu has patched OpenSSL and changed behavior of security level 2
146
+ # see https://bugs.python.org/issue41561#msg389003
147
+ def is_ubuntu ():
148
+ try :
149
+ # Assume that any references of "ubuntu" implies Ubuntu-like distro
150
+ # The workaround is not required for 18.04, but doesn't hurt either.
151
+ with open ("/etc/os-release" , encoding = "utf-8" ) as f :
152
+ return "ubuntu" in f .read ()
153
+ except FileNotFoundError :
154
+ return False
155
+
156
+ if is_ubuntu ():
157
+ def seclevel_workaround (* ctxs ):
158
+ """"Lower security level to '1' and allow all ciphers for TLS 1.0/1"""
159
+ for ctx in ctxs :
160
+ if ctx .minimum_version <= ssl .TLSVersion .TLSv1_1 :
161
+ ctx .set_ciphers ("@SECLEVEL=1:ALL" )
162
+ else :
163
+ def seclevel_workaround (* ctxs ):
164
+ pass
165
+
145
166
146
167
def has_tls_protocol (protocol ):
147
168
"""Check if a TLS protocol is available and enabled
@@ -2778,6 +2799,8 @@ def try_protocol_combo(server_protocol, client_protocol, expect_success,
2778
2799
if client_context .protocol == ssl .PROTOCOL_TLS :
2779
2800
client_context .set_ciphers ("ALL" )
2780
2801
2802
+ seclevel_workaround (server_context , client_context )
2803
+
2781
2804
for ctx in (client_context , server_context ):
2782
2805
ctx .verify_mode = certsreqs
2783
2806
ctx .load_cert_chain (SIGNED_CERTFILE )
@@ -2820,6 +2843,7 @@ def test_echo(self):
2820
2843
with self .subTest (protocol = ssl ._PROTOCOL_NAMES [protocol ]):
2821
2844
context = ssl .SSLContext (protocol )
2822
2845
context .load_cert_chain (CERTFILE )
2846
+ seclevel_workaround (context )
2823
2847
server_params_test (context , context ,
2824
2848
chatty = True , connectionchatty = True )
2825
2849
@@ -3825,6 +3849,7 @@ def test_min_max_version_tlsv1_1(self):
3825
3849
client_context .maximum_version = ssl .TLSVersion .TLSv1_2
3826
3850
server_context .minimum_version = ssl .TLSVersion .TLSv1
3827
3851
server_context .maximum_version = ssl .TLSVersion .TLSv1_1
3852
+ seclevel_workaround (client_context , server_context )
3828
3853
3829
3854
with ThreadedEchoServer (context = server_context ) as server :
3830
3855
with client_context .wrap_socket (socket .socket (),
@@ -3841,6 +3866,8 @@ def test_min_max_version_mismatch(self):
3841
3866
server_context .maximum_version = ssl .TLSVersion .TLSv1_2
3842
3867
client_context .minimum_version = ssl .TLSVersion .TLSv1
3843
3868
client_context .maximum_version = ssl .TLSVersion .TLSv1
3869
+ seclevel_workaround (client_context , server_context )
3870
+
3844
3871
with ThreadedEchoServer (context = server_context ) as server :
3845
3872
with client_context .wrap_socket (socket .socket (),
3846
3873
server_hostname = hostname ) as s :
@@ -3855,6 +3882,8 @@ def test_min_max_version_sslv3(self):
3855
3882
server_context .minimum_version = ssl .TLSVersion .SSLv3
3856
3883
client_context .minimum_version = ssl .TLSVersion .SSLv3
3857
3884
client_context .maximum_version = ssl .TLSVersion .SSLv3
3885
+ seclevel_workaround (client_context , server_context )
3886
+
3858
3887
with ThreadedEchoServer (context = server_context ) as server :
3859
3888
with client_context .wrap_socket (socket .socket (),
3860
3889
server_hostname = hostname ) as s :
0 commit comments