Skip to content

Commit d1bd6e7

Browse files
tiranmiss-islington
authored andcommitted
bpo-37440: Enable TLS 1.3 post-handshake auth in http.client (GH-14448)
Post-handshake authentication is required for conditional client cert authentication with TLS 1.3. https://bugs.python.org/issue37440
1 parent f0f5930 commit d1bd6e7

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

Doc/library/http.client.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ The module provides the following classes:
9494
:func:`ssl._create_unverified_context` can be passed to the *context*
9595
parameter.
9696

97+
.. versionchanged:: 3.8
98+
This class now enables TLS 1.3
99+
:attr:`ssl.SSLContext.post_handshake_auth` for the default *context* or
100+
when *cert_file* is passed with a custom *context*.
101+
97102
.. deprecated:: 3.6
98103

99104
*key_file* and *cert_file* are deprecated in favor of *context*.

Lib/http/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,9 @@ def __init__(self, host, port=None, key_file=None, cert_file=None,
13581358
self.cert_file = cert_file
13591359
if context is None:
13601360
context = ssl._create_default_https_context()
1361+
# enable PHA for TLS 1.3 connections if available
1362+
if context.post_handshake_auth is not None:
1363+
context.post_handshake_auth = True
13611364
will_verify = context.verify_mode != ssl.CERT_NONE
13621365
if check_hostname is None:
13631366
check_hostname = context.check_hostname
@@ -1366,6 +1369,10 @@ def __init__(self, host, port=None, key_file=None, cert_file=None,
13661369
"either CERT_OPTIONAL or CERT_REQUIRED")
13671370
if key_file or cert_file:
13681371
context.load_cert_chain(cert_file, key_file)
1372+
# cert and key file means the user wants to authenticate.
1373+
# enable TLS 1.3 PHA implicitly even for custom contexts.
1374+
if context.post_handshake_auth is not None:
1375+
context.post_handshake_auth = True
13691376
self._context = context
13701377
if check_hostname is not None:
13711378
self._context.check_hostname = check_hostname

Lib/test/test_httplib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,24 @@ def test_host_port(self):
17451745
self.assertEqual(h, c.host)
17461746
self.assertEqual(p, c.port)
17471747

1748+
def test_tls13_pha(self):
1749+
import ssl
1750+
if not ssl.HAS_TLSv1_3:
1751+
self.skipTest('TLS 1.3 support required')
1752+
# just check status of PHA flag
1753+
h = client.HTTPSConnection('localhost', 443)
1754+
self.assertTrue(h._context.post_handshake_auth)
1755+
1756+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
1757+
self.assertFalse(context.post_handshake_auth)
1758+
h = client.HTTPSConnection('localhost', 443, context=context)
1759+
self.assertIs(h._context, context)
1760+
self.assertFalse(h._context.post_handshake_auth)
1761+
1762+
h = client.HTTPSConnection('localhost', 443, context=context,
1763+
cert_file=CERT_localhost)
1764+
self.assertTrue(h._context.post_handshake_auth)
1765+
17481766

17491767
class RequestBodyTest(TestCase):
17501768
"""Test cases where a request includes a message body."""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
http.client now enables TLS 1.3 post-handshake authentication for default
2+
context or if a cert_file is passed to HTTPSConnection.

0 commit comments

Comments
 (0)