Skip to content

Commit e09017f

Browse files
committed
gh-137197: Address review comments
1 parent eaae575 commit e09017f

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Doc/library/ssl.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,7 @@ to speed up repeated connections from the same clients.
16891689
be a string in the `OpenSSL cipher list format
16901690
<https://docs.openssl.org/master/man1/ciphers/>`_.
16911691
To set allowed TLS 1.3 ciphers, use :meth:`SSLContext.set_ciphersuites`.
1692+
16921693
If no cipher can be selected (because compile-time options or other
16931694
configuration forbids use of all the specified ciphers), an
16941695
:class:`SSLError` will be raised.
@@ -1709,6 +1710,8 @@ to speed up repeated connections from the same clients.
17091710
When connected, the :meth:`SSLSocket.cipher` method of SSL sockets will
17101711
return details about the negotiated cipher.
17111712

1713+
.. versionadded:: next
1714+
17121715
.. method:: SSLContext.set_groups(groups)
17131716

17141717
Set the groups allowed for key agreement for sockets created with this
@@ -2856,10 +2859,10 @@ The TLS 1.3 protocol behaves slightly differently than previous version
28562859
of TLS/SSL. Some new TLS 1.3 features are not yet available.
28572860

28582861
- TLS 1.3 uses a disjunct set of cipher suites. All AES-GCM and ChaCha20
2859-
cipher suites are enabled by default. To restrict which TLS1.3 ciphers
2862+
cipher suites are enabled by default. To restrict which TLS 1.3 ciphers
28602863
are allowed, the method :meth:`SSLContext.set_ciphersuites` should be
28612864
called instead of :meth:`SSLContext.set_ciphers`, which only affects
2862-
ciphers in older TLS versions. The method :meth:`SSLContext.get_ciphers`
2865+
ciphers in older TLS versions. The :meth:`SSLContext.get_ciphers` method
28632866
returns information about ciphers for both TLS 1.3 and earlier versions
28642867
and the method :meth:`SSLSocket.cipher` returns information about the
28652868
negotiated cipher for both TLS 1.3 and earlier versions once a connection

Lib/test/test_ssl.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ def utc_offset(): #NOTE: ignore issues like #1647654
263263

264264
def test_wrap_socket(sock, *,
265265
cert_reqs=ssl.CERT_NONE, ca_certs=None,
266-
ciphers=None, ciphersuites=None, min_version=None,
266+
ciphers=None, ciphersuites=None,
267+
min_version=None, max_version=None,
267268
certfile=None, keyfile=None,
268269
**kwargs):
269270
if not kwargs.get("server_side"):
@@ -285,6 +286,8 @@ def test_wrap_socket(sock, *,
285286
context.set_ciphersuites(ciphersuites)
286287
if min_version is not None:
287288
context.minimum_version = min_version
289+
if max_version is not None:
290+
context.maximum_version = max_version
288291
return context.wrap_socket(sock, **kwargs)
289292

290293

@@ -2245,7 +2248,7 @@ def test_transport_eof(self):
22452248

22462249
@requires_tls_version('TLSv1_3')
22472250
class SimpleBackgroundTestsTLS_1_3(unittest.TestCase):
2248-
"""Tests that connect to a simple server running in the background"""
2251+
"""Tests that connect to a simple server running in the background."""
22492252

22502253
def setUp(self):
22512254
ciphers = [cipher['name'] for cipher in ctx.get_ciphers()
@@ -2278,17 +2281,17 @@ def test_ciphersuites(self):
22782281
s.connect(self.server_addr)
22792282
self.assertEqual(s.cipher()[0], self.matching_cipher)
22802283

2281-
# Test mismatched TLS 1.3 cipher suites
2282-
if self.matching_client != self.mismatched_cipher:
2283-
with test_wrap_socket(socket.socket(socket.AF_INET),
2284-
cert_reqs=ssl.CERT_NONE,
2285-
ciphersuites=self.mismatched_cipher,
2286-
min_version=ssl.TLSVersion.TLSv1_3) as s:
2287-
with self.assertRaises(ssl.SSLError):
2288-
s.connect(self.server_addr)
2289-
else:
2284+
def test_ciphersuite_mismatch(self):
2285+
if self.matching_cipher == self.mismatched_cipher:
22902286
self.skipTest("Multiple TLS 1.3 ciphers are not available")
22912287

2288+
with test_wrap_socket(socket.socket(socket.AF_INET),
2289+
cert_reqs=ssl.CERT_NONE,
2290+
ciphersuites=self.mismatched_cipher,
2291+
min_version=ssl.TLSVersion.TLSv1_3) as s:
2292+
with self.assertRaises(ssl.SSLError):
2293+
s.connect(self.server_addr)
2294+
22922295

22932296
@support.requires_resource('network')
22942297
class NetworkedTests(unittest.TestCase):

Modules/_ssl.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3595,7 +3595,12 @@ _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist)
35953595
{
35963596
int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist);
35973597
if (ret == 0) {
3598-
_setSSLError(get_state_ctx(self), "No cipher can be selected.", 0, __FILE__, __LINE__);
3598+
/* Clearing the error queue is necessary on some OpenSSL versions,
3599+
otherwise the error will be reported again when another SSL call
3600+
is done. */
3601+
ERR_clear_error();
3602+
PyErr_SetString(get_state_ctx(self)->PySSLErrorObject,
3603+
"No cipher can be selected.");
35993604
return NULL;
36003605
}
36013606
Py_RETURN_NONE;

0 commit comments

Comments
 (0)