Skip to content

Commit 48e5164

Browse files
committed
gh-137197: Address review comments
1 parent d3375f1 commit 48e5164

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

Doc/library/ssl.rst

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,28 +1685,29 @@ to speed up repeated connections from the same clients.
16851685
.. method:: SSLContext.set_ciphers(ciphers)
16861686

16871687
Set the allowed ciphers for sockets created with this context when
1688-
connecting using TLS 1.2 and earlier. It should be a string in the `OpenSSL
1689-
cipher list format <https://docs.openssl.org/master/man1/ciphers/>`_.
1688+
connecting using TLS 1.2 and earlier. The *ciphers* argument should
1689+
be a string in the `OpenSSL cipher list format
1690+
<https://docs.openssl.org/master/man1/ciphers/>`_.
16901691
To set allowed TLS 1.3 ciphers, use :meth:`SSLContext.set_ciphersuites`.
1691-
below. If no cipher can be selected (because compile-time options or other
1692+
If no cipher can be selected (because compile-time options or other
16921693
configuration forbids use of all the specified ciphers), an
16931694
:class:`SSLError` will be raised.
16941695

16951696
.. note::
16961697
when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will
1697-
return the negotiated cipher and associated TLS version.
1698+
return details about the negotiated cipher.
16981699

16991700
.. method:: SSLContext.set_ciphersuites(ciphersuites)
17001701

17011702
Set the allowed ciphers for sockets created with this context when
1702-
connecting using TLS 1.3. It should be a colon-separate string of TLS 1.3
1703-
cipher names. If no cipher can be selected (because compile-time options
1704-
or other configuration forbids use of all the specified ciphers), an
1705-
:class:`SSLError` will be raised.
1703+
connecting using TLS 1.3. The *ciphersuites* argument should be a
1704+
colon-separate string of TLS 1.3 cipher names. If no cipher can be
1705+
selected (because compile-time options or other configuration forbids
1706+
use of all the specified ciphers), an :class:`SSLError` will be raised.
17061707

17071708
.. note::
17081709
when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will
1709-
return the negotiated cipher and associated TLS version.
1710+
return details about the negotiated cipher.
17101711

17111712
.. method:: SSLContext.set_groups(groups)
17121713

@@ -2860,8 +2861,9 @@ of TLS/SSL. Some new TLS 1.3 features are not yet available.
28602861
called instead of :meth:`SSLContext.set_ciphers`, which only affects
28612862
ciphers in older TLS versions. The method :meth:`SSLContext.get_ciphers`
28622863
returns information about ciphers for both TLS 1.3 and earlier versions
2863-
and the method :meth:`SSLSocket.cipher` returns the negotiated cipher and
2864-
the associated TLS version once a connection is established.
2864+
and the method :meth:`SSLSocket.cipher` returns information about the
2865+
negotiated cipher for both TLS 1.3 and earlier versions once a connection
2866+
is established.
28652867
- Session tickets are no longer sent as part of the initial handshake and
28662868
are handled differently. :attr:`SSLSocket.session` and :class:`SSLSession`
28672869
are not compatible with TLS 1.3.

Lib/test/test_ssl.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,11 @@ def utc_offset(): #NOTE: ignore issues like #1647654
261261
)
262262

263263

264-
def test_wrap_socket(sock, *, cert_reqs=ssl.CERT_NONE, ca_certs=None,
264+
def test_wrap_socket(sock, *,
265+
cert_reqs=ssl.CERT_NONE, ca_certs=None,
265266
ciphers=None, ciphersuites=None, min_version=None,
266-
certfile=None, keyfile=None, **kwargs):
267+
certfile=None, keyfile=None,
268+
**kwargs):
267269
if not kwargs.get("server_side"):
268270
kwargs["server_hostname"] = SIGNED_CERTFILE_HOSTNAME
269271
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
@@ -1866,6 +1868,10 @@ class SimpleBackgroundTests(unittest.TestCase):
18661868

18671869
def setUp(self):
18681870
self.server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
1871+
1872+
if has_tls_version('TLSv1_3'):
1873+
self.server_context.set_ciphersuites('TLS_AES_256_GCM_SHA384')
1874+
18691875
self.server_context.load_cert_chain(SIGNED_CERTFILE)
18701876
server = ThreadedEchoServer(context=self.server_context)
18711877
self.enterContext(server)
@@ -2112,27 +2118,32 @@ def test_ciphers(self):
21122118
cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx")
21132119
s.connect(self.server_addr)
21142120

2121+
@requires_tls_version('TLSv1_3')
21152122
def test_ciphersuites(self):
2116-
with test_wrap_socket(socket.socket(socket.AF_INET),
2117-
cert_reqs=ssl.CERT_NONE,
2118-
min_version=ssl.TLSVersion.TLSv1_3) as s:
2119-
s.connect(self.server_addr)
2120-
self.assertEqual(s.cipher()[1], "TLSv1.3")
2123+
# Test successful TLS 1.3 handshake
21212124
with test_wrap_socket(socket.socket(socket.AF_INET),
21222125
cert_reqs=ssl.CERT_NONE,
21232126
ciphersuites="TLS_AES_256_GCM_SHA384",
21242127
min_version=ssl.TLSVersion.TLSv1_3) as s:
21252128
s.connect(self.server_addr)
21262129
self.assertEqual(s.cipher(),
21272130
("TLS_AES_256_GCM_SHA384", "TLSv1.3", 256))
2128-
# Error checking can happen at instantiation or when connecting
2131+
2132+
# Test mismatched TLS 1.3 cipher suites
2133+
with test_wrap_socket(socket.socket(socket.AF_INET),
2134+
cert_reqs=ssl.CERT_NONE,
2135+
ciphersuites="TLS_AES_128_GCM_SHA256",
2136+
min_version=ssl.TLSVersion.TLSv1_3) as s:
2137+
with self.assertRaises(ssl.SSLError):
2138+
s.connect(self.server_addr)
2139+
2140+
# Test unrecognized TLS 1.3 cipher suite name
21292141
with self.assertRaisesRegex(ssl.SSLError,
21302142
"No cipher suite can be selected"):
21312143
with socket.socket(socket.AF_INET) as sock:
21322144
s = test_wrap_socket(sock, cert_reqs=ssl.CERT_NONE,
21332145
ciphersuites="XXX",
21342146
min_version=ssl.TLSVersion.TLSv1_3)
2135-
s.connect(self.server_addr)
21362147

21372148
def test_get_ca_certs_capath(self):
21382149
# capath certs are loaded on request

0 commit comments

Comments
 (0)