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