@@ -547,7 +547,7 @@ def test_openssl_version(self):
547
547
else :
548
548
openssl_ver = f"OpenSSL { major :d} .{ minor :d} .{ fix :d} "
549
549
self .assertTrue (
550
- s .startswith ((openssl_ver , libressl_ver )),
550
+ s .startswith ((openssl_ver , libressl_ver , "AWS-LC" )),
551
551
(s , t , hex (n ))
552
552
)
553
553
@@ -1378,24 +1378,30 @@ def test_load_cert_chain(self):
1378
1378
with self .assertRaises (OSError ) as cm :
1379
1379
ctx .load_cert_chain (NONEXISTINGCERT )
1380
1380
self .assertEqual (cm .exception .errno , errno .ENOENT )
1381
- with self .assertRaisesRegex (ssl .SSLError , "PEM lib" ):
1381
+ with self .assertRaisesRegex (ssl .SSLError , "PEM ( lib|routines) " ):
1382
1382
ctx .load_cert_chain (BADCERT )
1383
- with self .assertRaisesRegex (ssl .SSLError , "PEM lib" ):
1383
+ with self .assertRaisesRegex (ssl .SSLError , "PEM ( lib|routines) " ):
1384
1384
ctx .load_cert_chain (EMPTYCERT )
1385
1385
# Separate key and cert
1386
1386
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
1387
1387
ctx .load_cert_chain (ONLYCERT , ONLYKEY )
1388
1388
ctx .load_cert_chain (certfile = ONLYCERT , keyfile = ONLYKEY )
1389
1389
ctx .load_cert_chain (certfile = BYTES_ONLYCERT , keyfile = BYTES_ONLYKEY )
1390
- with self .assertRaisesRegex (ssl .SSLError , "PEM lib" ):
1390
+ with self .assertRaisesRegex (ssl .SSLError , "PEM ( lib|routines) " ):
1391
1391
ctx .load_cert_chain (ONLYCERT )
1392
- with self .assertRaisesRegex (ssl .SSLError , "PEM lib" ):
1392
+ with self .assertRaisesRegex (ssl .SSLError , "PEM ( lib|routines) " ):
1393
1393
ctx .load_cert_chain (ONLYKEY )
1394
- with self .assertRaisesRegex (ssl .SSLError , "PEM lib" ):
1394
+ with self .assertRaisesRegex (ssl .SSLError , "PEM ( lib|routines) " ):
1395
1395
ctx .load_cert_chain (certfile = ONLYKEY , keyfile = ONLYCERT )
1396
1396
# Mismatching key and cert
1397
1397
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
1398
- with self .assertRaisesRegex (ssl .SSLError , "key values mismatch" ):
1398
+ # Allow for flexible libssl error messages.
1399
+ regex = re .compile (r"""(
1400
+ key values mismatch # OpenSSL
1401
+ |
1402
+ KEY_VALUES_MISMATCH # AWS-LC
1403
+ )""" , re .X )
1404
+ with self .assertRaisesRegex (ssl .SSLError , regex ):
1399
1405
ctx .load_cert_chain (CAFILE_CACERT , ONLYKEY )
1400
1406
# Password protected key and cert
1401
1407
ctx .load_cert_chain (CERTFILE_PROTECTED , password = KEY_PASSWORD )
@@ -1463,7 +1469,7 @@ def test_load_verify_locations(self):
1463
1469
with self .assertRaises (OSError ) as cm :
1464
1470
ctx .load_verify_locations (NONEXISTINGCERT )
1465
1471
self .assertEqual (cm .exception .errno , errno .ENOENT )
1466
- with self .assertRaisesRegex (ssl .SSLError , "PEM lib" ):
1472
+ with self .assertRaisesRegex (ssl .SSLError , "PEM ( lib|routines) " ):
1467
1473
ctx .load_verify_locations (BADCERT )
1468
1474
ctx .load_verify_locations (CERTFILE , CAPATH )
1469
1475
ctx .load_verify_locations (CERTFILE , capath = BYTES_CAPATH )
@@ -1862,9 +1868,10 @@ def test_lib_reason(self):
1862
1868
with self .assertRaises (ssl .SSLError ) as cm :
1863
1869
ctx .load_dh_params (CERTFILE )
1864
1870
self .assertEqual (cm .exception .library , 'PEM' )
1865
- self .assertEqual (cm .exception .reason , 'NO_START_LINE' )
1871
+ regex = "(NO_START_LINE|UNSUPPORTED_PUBLIC_KEY_TYPE)"
1872
+ self .assertRegex (cm .exception .reason , regex )
1866
1873
s = str (cm .exception )
1867
- self .assertTrue (s . startswith ( "[PEM: NO_START_LINE] no start line" ) , s )
1874
+ self .assertTrue (" NO_START_LINE" in s , s )
1868
1875
1869
1876
def test_subclass (self ):
1870
1877
# Check that the appropriate SSLError subclass is raised
@@ -2041,7 +2048,13 @@ def test_connect_fail(self):
2041
2048
s = test_wrap_socket (socket .socket (socket .AF_INET ),
2042
2049
cert_reqs = ssl .CERT_REQUIRED )
2043
2050
self .addCleanup (s .close )
2044
- self .assertRaisesRegex (ssl .SSLError , "certificate verify failed" ,
2051
+ # Allow for flexible libssl error messages.
2052
+ regex = re .compile (r"""(
2053
+ certificate verify failed # OpenSSL
2054
+ |
2055
+ CERTIFICATE_VERIFY_FAILED # AWS-LC
2056
+ )""" , re .X )
2057
+ self .assertRaisesRegex (ssl .SSLError , regex ,
2045
2058
s .connect , self .server_addr )
2046
2059
2047
2060
def test_connect_ex (self ):
@@ -2109,7 +2122,13 @@ def test_connect_with_context_fail(self):
2109
2122
server_hostname = SIGNED_CERTFILE_HOSTNAME
2110
2123
)
2111
2124
self .addCleanup (s .close )
2112
- self .assertRaisesRegex (ssl .SSLError , "certificate verify failed" ,
2125
+ # Allow for flexible libssl error messages.
2126
+ regex = re .compile (r"""(
2127
+ certificate verify failed # OpenSSL
2128
+ |
2129
+ CERTIFICATE_VERIFY_FAILED # AWS-LC
2130
+ )""" , re .X )
2131
+ self .assertRaisesRegex (ssl .SSLError , regex ,
2113
2132
s .connect , self .server_addr )
2114
2133
2115
2134
def test_connect_capath (self ):
@@ -2329,14 +2348,16 @@ def test_bio_handshake(self):
2329
2348
self .assertIsNone (sslobj .version ())
2330
2349
self .assertIsNone (sslobj .shared_ciphers ())
2331
2350
self .assertRaises (ValueError , sslobj .getpeercert )
2332
- if 'tls-unique' in ssl .CHANNEL_BINDING_TYPES :
2351
+ # tls-unique is not defined for TLSv1.3
2352
+ # https://datatracker.ietf.org/doc/html/rfc8446#appendix-C.5
2353
+ if 'tls-unique' in ssl .CHANNEL_BINDING_TYPES and sslobj .version () != "TLSv1.3" :
2333
2354
self .assertIsNone (sslobj .get_channel_binding ('tls-unique' ))
2334
2355
self .ssl_io_loop (sock , incoming , outgoing , sslobj .do_handshake )
2335
2356
self .assertTrue (sslobj .cipher ())
2336
2357
self .assertIsNone (sslobj .shared_ciphers ())
2337
2358
self .assertIsNotNone (sslobj .version ())
2338
2359
self .assertTrue (sslobj .getpeercert ())
2339
- if 'tls-unique' in ssl .CHANNEL_BINDING_TYPES :
2360
+ if 'tls-unique' in ssl .CHANNEL_BINDING_TYPES and sslobj . version () != "TLSv1.3" :
2340
2361
self .assertTrue (sslobj .get_channel_binding ('tls-unique' ))
2341
2362
try :
2342
2363
self .ssl_io_loop (sock , incoming , outgoing , sslobj .unwrap )
@@ -3058,11 +3079,16 @@ def test_crl_check(self):
3058
3079
client_context .verify_flags |= ssl .VERIFY_CRL_CHECK_LEAF
3059
3080
3060
3081
server = ThreadedEchoServer (context = server_context , chatty = True )
3082
+ # Allow for flexible libssl error messages.
3083
+ regex = re .compile (r"""(
3084
+ certificate verify failed # OpenSSL
3085
+ |
3086
+ CERTIFICATE_VERIFY_FAILED # AWS-LC
3087
+ )""" , re .X )
3061
3088
with server :
3062
3089
with client_context .wrap_socket (socket .socket (),
3063
3090
server_hostname = hostname ) as s :
3064
- with self .assertRaisesRegex (ssl .SSLError ,
3065
- "certificate verify failed" ):
3091
+ with self .assertRaisesRegex (ssl .SSLError , regex ):
3066
3092
s .connect ((HOST , server .port ))
3067
3093
3068
3094
# now load a CRL file. The CRL file is signed by the CA.
@@ -3093,12 +3119,16 @@ def test_check_hostname(self):
3093
3119
3094
3120
# incorrect hostname should raise an exception
3095
3121
server = ThreadedEchoServer (context = server_context , chatty = True )
3122
+ # Allow for flexible libssl error messages.
3123
+ regex = re .compile (r"""(
3124
+ certificate verify failed # OpenSSL
3125
+ |
3126
+ CERTIFICATE_VERIFY_FAILED # AWS-LC
3127
+ )""" , re .X )
3096
3128
with server :
3097
3129
with client_context .wrap_socket (socket .socket (),
3098
3130
server_hostname = "invalid" ) as s :
3099
- with self .assertRaisesRegex (
3100
- ssl .CertificateError ,
3101
- "Hostname mismatch, certificate is not valid for 'invalid'." ):
3131
+ with self .assertRaisesRegex (ssl .CertificateError , regex ):
3102
3132
s .connect ((HOST , server .port ))
3103
3133
3104
3134
# missing server_hostname arg should cause an exception, too
@@ -3291,21 +3321,15 @@ def test_wrong_cert_tls13(self):
3291
3321
suppress_ragged_eofs = False ) as s :
3292
3322
# TLS 1.3 perform client cert exchange after handshake
3293
3323
s .connect ((HOST , server .port ))
3294
- try :
3324
+ with self .assertRaisesRegex (
3325
+ ssl .SSLError ,
3326
+ 'alert unknown ca|EOF occurred|TLSV1_ALERT_UNKNOWN_CA'
3327
+ ):
3328
+ # TLS 1.3 perform client cert exchange after handshake
3295
3329
s .write (b'data' )
3296
3330
s .read (1000 )
3297
3331
s .write (b'should have failed already' )
3298
3332
s .read (1000 )
3299
- except ssl .SSLError as e :
3300
- if support .verbose :
3301
- sys .stdout .write ("\n SSLError is %r\n " % e )
3302
- except OSError as e :
3303
- if e .errno != errno .ECONNRESET :
3304
- raise
3305
- if support .verbose :
3306
- sys .stdout .write ("\n socket.error is %r\n " % e )
3307
- else :
3308
- self .fail ("Use of invalid cert should have failed!" )
3309
3333
3310
3334
def test_rude_shutdown (self ):
3311
3335
"""A brutal shutdown of an SSL server should raise an OSError
@@ -3363,13 +3387,21 @@ def test_ssl_cert_verify_error(self):
3363
3387
server_hostname = SIGNED_CERTFILE_HOSTNAME ) as s :
3364
3388
try :
3365
3389
s .connect ((HOST , server .port ))
3390
+ self .fail ("Expected connection failure" )
3366
3391
except ssl .SSLError as e :
3367
3392
msg = 'unable to get local issuer certificate'
3368
3393
self .assertIsInstance (e , ssl .SSLCertVerificationError )
3369
3394
self .assertEqual (e .verify_code , 20 )
3370
3395
self .assertEqual (e .verify_message , msg )
3371
- self .assertIn (msg , repr (e ))
3372
- self .assertIn ('certificate verify failed' , repr (e ))
3396
+ # Allow for flexible libssl error messages.
3397
+ regex = f"({ msg } |CERTIFICATE_VERIFY_FAILED)"
3398
+ self .assertRegex (repr (e ), regex )
3399
+ regex = re .compile (r"""(
3400
+ certificate verify failed # OpenSSL
3401
+ |
3402
+ CERTIFICATE_VERIFY_FAILED # AWS-LC
3403
+ )""" , re .X )
3404
+ self .assertRegex (repr (e ), regex )
3373
3405
3374
3406
@requires_tls_version ('SSLv2' )
3375
3407
def test_protocol_sslv2 (self ):
@@ -3916,7 +3948,7 @@ def test_no_shared_ciphers(self):
3916
3948
server_hostname = hostname ) as s :
3917
3949
with self .assertRaises (OSError ):
3918
3950
s .connect ((HOST , server .port ))
3919
- self .assertIn ("no shared cipher " , server .conn_errors [0 ])
3951
+ self .assertIn ("NO_SHARED_CIPHER " , server .conn_errors [0 ])
3920
3952
3921
3953
def test_version_basic (self ):
3922
3954
"""
@@ -4004,7 +4036,7 @@ def test_min_max_version_mismatch(self):
4004
4036
server_hostname = hostname ) as s :
4005
4037
with self .assertRaises (ssl .SSLError ) as e :
4006
4038
s .connect ((HOST , server .port ))
4007
- self .assertIn ( " alert" , str (e .exception ))
4039
+ self .assertRegex ( "( alert|ALERT) " , str (e .exception ))
4008
4040
4009
4041
@requires_tls_version ('SSLv3' )
4010
4042
def test_min_max_version_sslv3 (self ):
@@ -4046,6 +4078,10 @@ def test_tls_unique_channel_binding(self):
4046
4078
4047
4079
client_context , server_context , hostname = testing_context ()
4048
4080
4081
+ # tls-unique is not defined for TLSv1.3
4082
+ # https://datatracker.ietf.org/doc/html/rfc8446#appendix-C.5
4083
+ client_context .maximum_version = ssl .TLSVersion .TLSv1_2
4084
+
4049
4085
server = ThreadedEchoServer (context = server_context ,
4050
4086
chatty = True ,
4051
4087
connectionchatty = False )
@@ -4132,7 +4168,7 @@ def test_dh_params(self):
4132
4168
cipher = stats ["cipher" ][0 ]
4133
4169
parts = cipher .split ("-" )
4134
4170
if "ADH" not in parts and "EDH" not in parts and "DHE" not in parts :
4135
- self .fail ("Non-DH cipher : " + cipher [0 ])
4171
+ self .fail ("Non-DH key exchange : " + cipher [0 ])
4136
4172
4137
4173
def test_ecdh_curve (self ):
4138
4174
# server secp384r1, client auto
@@ -4299,8 +4335,9 @@ def cb_raising(ssl_sock, server_name, initial_context):
4299
4335
chatty = False ,
4300
4336
sni_name = 'supermessage' )
4301
4337
4302
- self .assertEqual (cm .exception .reason ,
4303
- 'SSLV3_ALERT_HANDSHAKE_FAILURE' )
4338
+ # Allow for flexible libssl error messages.
4339
+ regex = "(SSLV3_ALERT_HANDSHAKE_FAILURE|NO_PRIVATE_VALUE)"
4340
+ self .assertRegex (regex , cm .exception .reason )
4304
4341
self .assertEqual (catch .unraisable .exc_type , ZeroDivisionError )
4305
4342
4306
4343
def test_sni_callback_wrong_return_type (self ):
0 commit comments