51
51
CAN_GET_SELECTED_OPENSSL_GROUP = ssl .OPENSSL_VERSION_INFO >= (3 , 2 )
52
52
CAN_IGNORE_UNKNOWN_OPENSSL_GROUPS = ssl .OPENSSL_VERSION_INFO >= (3 , 3 )
53
53
CAN_GET_AVAILABLE_OPENSSL_GROUPS = ssl .OPENSSL_VERSION_INFO >= (3 , 5 )
54
+ CAN_GET_AVAILABLE_OPENSSL_SIGALGS = ssl .OPENSSL_VERSION_INFO >= (3 , 4 )
55
+ CAN_SET_CLIENT_SIGALGS = "AWS-LC" not in ssl .OPENSSL_VERSION
56
+ CAN_IGNORE_UNKNOWN_OPENSSL_SIGALGS = ssl .OPENSSL_VERSION_INFO >= (3 , 3 )
57
+ CAN_GET_SELECTED_OPENSSL_SIGALG = ssl .OPENSSL_VERSION_INFO >= (3 , 5 )
54
58
PY_SSL_DEFAULT_CIPHERS = sysconfig .get_config_var ('PY_SSL_DEFAULT_CIPHERS' )
55
59
56
60
PROTOCOL_TO_TLS_VERSION = {}
@@ -294,7 +298,8 @@ def test_wrap_socket(sock, *,
294
298
USE_SAME_TEST_CONTEXT = False
295
299
_TEST_CONTEXT = None
296
300
297
- def testing_context (server_cert = SIGNED_CERTFILE , * , server_chain = True ):
301
+ def testing_context (server_cert = SIGNED_CERTFILE , * , server_chain = True ,
302
+ client_cert = None ):
298
303
"""Create context
299
304
300
305
client_context, server_context, hostname = testing_context()
@@ -321,6 +326,10 @@ def testing_context(server_cert=SIGNED_CERTFILE, *, server_chain=True):
321
326
if server_chain :
322
327
server_context .load_verify_locations (SIGNING_CA )
323
328
329
+ if client_cert :
330
+ client_context .load_cert_chain (client_cert )
331
+ server_context .verify_mode = ssl .CERT_REQUIRED
332
+
324
333
if USE_SAME_TEST_CONTEXT :
325
334
if _TEST_CONTEXT is not None :
326
335
_TEST_CONTEXT = client_context , server_context , hostname
@@ -990,6 +999,37 @@ def test_get_groups(self):
990
999
self .assertNotIn ('P-256' , ctx .get_groups ())
991
1000
self .assertIn ('P-256' , ctx .get_groups (include_aliases = True ))
992
1001
1002
+ @unittest .skipUnless (CAN_GET_AVAILABLE_OPENSSL_SIGALGS ,
1003
+ "SSL library doesn't support getting sigalgs" )
1004
+ def test_get_sigalgs (self ):
1005
+ self .assertIn ('rsa_pss_rsae_sha256' , ssl .get_sigalgs ())
1006
+
1007
+ @unittest .skipUnless (CAN_SET_CLIENT_SIGALGS ,
1008
+ "SSL library doesn't support setting client sigalgs" )
1009
+ def test_set_client_sigalgs (self ):
1010
+ ctx = ssl .create_default_context ()
1011
+
1012
+ self .assertIsNone (ctx .set_client_sigalgs ('rsa_pss_rsae_sha256' ))
1013
+
1014
+ self .assertRaises (ssl .SSLError , ctx .set_client_sigalgs ,
1015
+ 'rsa_pss_rsae_sha256:foo' )
1016
+
1017
+ # Ignoring unknown sigalgs is only supported since OpenSSL 3.3.
1018
+ if CAN_IGNORE_UNKNOWN_OPENSSL_SIGALGS :
1019
+ self .assertIsNone (ctx .set_client_sigalgs ('rsa_pss_rsae_sha256:?foo' ))
1020
+
1021
+ def test_set_server_sigalgs (self ):
1022
+ ctx = ssl .create_default_context ()
1023
+
1024
+ self .assertIsNone (ctx .set_server_sigalgs ('rsa_pss_rsae_sha256' ))
1025
+
1026
+ self .assertRaises (ssl .SSLError , ctx .set_server_sigalgs ,
1027
+ 'rsa_pss_rsae_sha256:foo' )
1028
+
1029
+ # Ignoring unknown sigalgs is only supported since OpenSSL 3.3.
1030
+ if CAN_IGNORE_UNKNOWN_OPENSSL_SIGALGS :
1031
+ self .assertIsNone (ctx .set_server_sigalgs ('rsa_pss_rsae_sha256:?foo' ))
1032
+
993
1033
def test_options (self ):
994
1034
# Test default SSLContext options
995
1035
ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
@@ -2814,6 +2854,9 @@ def server_params_test(client_context, server_context, indata=b"FOO\n",
2814
2854
})
2815
2855
if CAN_GET_SELECTED_OPENSSL_GROUP :
2816
2856
stats .update ({'group' : s .group ()})
2857
+ if CAN_GET_SELECTED_OPENSSL_SIGALG :
2858
+ stats .update ({'client_sigalg' : s .client_sigalg ()})
2859
+ stats .update ({'server_sigalg' : s .server_sigalg ()})
2817
2860
s .close ()
2818
2861
stats ['server_alpn_protocols' ] = server .selected_alpn_protocols
2819
2862
stats ['server_shared_ciphers' ] = server .shared_ciphers
@@ -4273,6 +4316,71 @@ def test_groups(self):
4273
4316
chatty = True , connectionchatty = True ,
4274
4317
sni_name = hostname )
4275
4318
4319
+ @unittest .skipUnless (CAN_SET_CLIENT_SIGALGS ,
4320
+ "SSL library doesn't support setting client sigalgs" )
4321
+ def test_client_sigalgs (self ):
4322
+ # no mutual auth, so cient_sigalg should be None
4323
+ client_context , server_context , hostname = testing_context ()
4324
+ stats = server_params_test (client_context , server_context ,
4325
+ chatty = True , connectionchatty = True ,
4326
+ sni_name = hostname )
4327
+ if CAN_GET_SELECTED_OPENSSL_SIGALG :
4328
+ self .assertIsNone (stats ['client_sigalg' ])
4329
+
4330
+ # server auto, client rsa_pss_rsae_sha384
4331
+ sigalg = "rsa_pss_rsae_sha384"
4332
+ client_context , server_context , hostname = \
4333
+ testing_context (client_cert = SIGNED_CERTFILE )
4334
+ client_context .set_client_sigalgs (sigalg )
4335
+ stats = server_params_test (client_context , server_context ,
4336
+ chatty = True , connectionchatty = True ,
4337
+ sni_name = hostname )
4338
+ if CAN_GET_SELECTED_OPENSSL_SIGALG :
4339
+ self .assertEqual (stats ['client_sigalg' ], sigalg )
4340
+
4341
+ @unittest .skipUnless (CAN_SET_CLIENT_SIGALGS ,
4342
+ "SSL library doesn't support setting client sigalgs" )
4343
+ def test_client_sigalgs_mismatch (self ):
4344
+ client_context , server_context , hostname = \
4345
+ testing_context (client_cert = SIGNED_CERTFILE )
4346
+ client_context .set_client_sigalgs ("rsa_pss_rsae_sha256" )
4347
+ server_context .set_client_sigalgs ("rsa_pss_rsae_sha384" )
4348
+
4349
+ # Some systems return ConnectionResetError on handshake failures
4350
+ with self .assertRaises ((ssl .SSLError , ConnectionResetError )):
4351
+ server_params_test (client_context , server_context ,
4352
+ chatty = True , connectionchatty = True ,
4353
+ sni_name = hostname )
4354
+
4355
+ def test_server_sigalgs (self ):
4356
+ # server rsa_pss_rsae_sha384, client auto
4357
+ sigalg = "rsa_pss_rsae_sha384"
4358
+ client_context , server_context , hostname = testing_context ()
4359
+ server_context .set_server_sigalgs (sigalg )
4360
+ stats = server_params_test (client_context , server_context ,
4361
+ chatty = True , connectionchatty = True ,
4362
+ sni_name = hostname )
4363
+ if CAN_GET_SELECTED_OPENSSL_SIGALG :
4364
+ self .assertEqual (stats ['server_sigalg' ], sigalg )
4365
+
4366
+ # server auto, client rsa_pss_rsae_sha384
4367
+ client_context , server_context , hostname = testing_context ()
4368
+ client_context .set_server_sigalgs (sigalg )
4369
+ stats = server_params_test (client_context , server_context ,
4370
+ chatty = True , connectionchatty = True ,
4371
+ sni_name = hostname )
4372
+ if CAN_GET_SELECTED_OPENSSL_SIGALG :
4373
+ self .assertEqual (stats ['server_sigalg' ], sigalg )
4374
+
4375
+ def test_server_sigalgs_mismatch (self ):
4376
+ client_context , server_context , hostname = testing_context ()
4377
+ client_context .set_server_sigalgs ("rsa_pss_rsae_sha256" )
4378
+ server_context .set_server_sigalgs ("rsa_pss_rsae_sha384" )
4379
+ with self .assertRaises (ssl .SSLError ):
4380
+ server_params_test (client_context , server_context ,
4381
+ chatty = True , connectionchatty = True ,
4382
+ sni_name = hostname )
4383
+
4276
4384
def test_selected_alpn_protocol (self ):
4277
4385
# selected_alpn_protocol() is None unless ALPN is used.
4278
4386
client_context , server_context , hostname = testing_context ()
0 commit comments