Skip to content

Commit caf6675

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

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

Doc/library/ssl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2860,7 +2860,7 @@ of TLS/SSL. Some new TLS 1.3 features are not yet available.
28602860

28612861
- TLS 1.3 uses a disjunct set of cipher suites. All AES-GCM and ChaCha20
28622862
cipher suites are enabled by default. To restrict which TLS 1.3 ciphers
2863-
are allowed, the method :meth:`SSLContext.set_ciphersuites` should be
2863+
are allowed, the :meth:`SSLContext.set_ciphersuites` method should be
28642864
called instead of :meth:`SSLContext.set_ciphers`, which only affects
28652865
ciphers in older TLS versions. The :meth:`SSLContext.get_ciphers` method
28662866
returns information about ciphers for both TLS 1.3 and earlier versions

Doc/whatsnew/3.15.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ ssl
337337
continue to be used. Both calls can be made on the same context and the
338338
selected cipher suite will depend on the TLS version negotiated when a
339339
connection is made.
340-
(Contributed by Ron Frederick in :gh:`137197`)
340+
(Contributed by Ron Frederick in :gh:`137197`.)
341341

342342

343343
tarfile

Lib/test/test_ssl.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,32 +2246,38 @@ def test_transport_eof(self):
22462246
self.assertRaises(ssl.SSLEOFError, sslobj.read)
22472247

22482248

2249-
@requires_tls_version('TLSv1_3')
22502249
class SimpleBackgroundTestsTLS_1_3(unittest.TestCase):
22512250
"""Tests that connect to a simple server running in the background."""
22522251

2252+
@requires_tls_version('TLSv1_3')
22532253
def setUp(self):
2254-
ciphers = [cipher['name'] for cipher in ctx.get_ciphers()
2254+
server_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
2255+
ciphers = [cipher['name'] for cipher in server_ctx.get_ciphers()
22552256
if cipher['protocol'] == 'TLSv1.3']
22562257

2258+
if not ciphers:
2259+
self.skipTest("No cipher supports TLSv1.3")
2260+
22572261
self.matching_cipher = ciphers[0]
2262+
# Some tests need at least two ciphers.
22582263
self.mismatched_cipher = ciphers[-1]
22592264

2260-
self.server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
2261-
self.server_context.set_ciphersuites(self.matching_cipher)
2262-
self.server_context.load_cert_chain(SIGNED_CERTFILE)
2263-
server = ThreadedEchoServer(context=self.server_context)
2265+
server_ctx.set_ciphersuites(self.matching_cipher)
2266+
server_ctx.load_cert_chain(SIGNED_CERTFILE)
2267+
server = ThreadedEchoServer(context=server_ctx)
22642268
self.enterContext(server)
22652269
self.server_addr = (HOST, server.port)
22662270

22672271
def test_ciphersuites(self):
22682272
# Test unrecognized TLS 1.3 cipher suite name
2269-
with self.assertRaisesRegex(ssl.SSLError,
2270-
"No cipher suite can be selected"):
2271-
with socket.socket(socket.AF_INET) as sock:
2272-
s = test_wrap_socket(sock, cert_reqs=ssl.CERT_NONE,
2273-
ciphersuites="XXX",
2274-
min_version=ssl.TLSVersion.TLSv1_3)
2273+
with (
2274+
socket.socket(socket.AF_INET) as sock,
2275+
self.assertRaisesRegex(ssl.SSLError,
2276+
"No cipher suite can be selected")
2277+
):
2278+
test_wrap_socket(sock, cert_reqs=ssl.CERT_NONE,
2279+
ciphersuites="XXX",
2280+
min_version=ssl.TLSVersion.TLSv1_3)
22752281

22762282
# Test successful TLS 1.3 handshake
22772283
with test_wrap_socket(socket.socket(socket.AF_INET),
@@ -2281,6 +2287,15 @@ def test_ciphersuites(self):
22812287
s.connect(self.server_addr)
22822288
self.assertEqual(s.cipher()[0], self.matching_cipher)
22832289

2290+
def test_ciphersuite_downgrade(self):
2291+
with test_wrap_socket(socket.socket(socket.AF_INET),
2292+
cert_reqs=ssl.CERT_NONE,
2293+
ciphersuites=self.matching_cipher,
2294+
min_version=ssl.TLSVersion.TLSv1_2,
2295+
max_version=ssl.TLSVersion.TLSv1_2) as s:
2296+
s.connect(self.server_addr)
2297+
self.assertEqual(s.cipher()[1], 'TLSv1.2')
2298+
22842299
def test_ciphersuite_mismatch(self):
22852300
if self.matching_cipher == self.mismatched_cipher:
22862301
self.skipTest("Multiple TLS 1.3 ciphers are not available")
@@ -2289,8 +2304,7 @@ def test_ciphersuite_mismatch(self):
22892304
cert_reqs=ssl.CERT_NONE,
22902305
ciphersuites=self.mismatched_cipher,
22912306
min_version=ssl.TLSVersion.TLSv1_3) as s:
2292-
with self.assertRaises(ssl.SSLError):
2293-
s.connect(self.server_addr)
2307+
self.assertRaises(ssl.SSLError, s.connect, self.server_addr)
22942308

22952309

22962310
@support.requires_resource('network')
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
:mod:`ssl` can now set TLS 1.3 cipher suites.
1+
:class:`~ssl.SSLContext` objects can now set TLS 1.3 cipher suites
2+
via :meth:`~ssl.SSLContext.set_ciphersuites`.

Modules/_ssl.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,8 +3618,7 @@ _ssl__SSLContext_set_ciphersuites_impl(PySSLContext *self,
36183618
const char *ciphersuites)
36193619
/*[clinic end generated code: output=9915bec58e54d76d input=2afcc3693392be41]*/
36203620
{
3621-
int ret = SSL_CTX_set_ciphersuites(self->ctx, ciphersuites);
3622-
if (ret == 0) {
3621+
if (!SSL_CTX_set_ciphersuites(self->ctx, ciphersuites)) {
36233622
_setSSLError(get_state_ctx(self), "No cipher suite can be selected.", 0, __FILE__, __LINE__);
36243623
return NULL;
36253624
}

0 commit comments

Comments
 (0)