Blame SOURCES/00327-enable-tls-1.3-PHA-in-http.client.patch

b7e076
diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
b7e076
index 2f59ece..d756916 100644
b7e076
--- a/Doc/library/http.client.rst
b7e076
+++ b/Doc/library/http.client.rst
b7e076
@@ -88,6 +88,11 @@ The module provides the following classes:
b7e076
       :func:`ssl._create_unverified_context` can be passed to the *context*
b7e076
       parameter.
b7e076
 
b7e076
+   .. versionchanged:: 3.7.4
b7e076
+      This class now enables TLS 1.3
b7e076
+      :attr:`ssl.SSLContext.post_handshake_auth` for the default *context* or
b7e076
+      when *cert_file* is passed with a custom *context*.
b7e076
+
b7e076
    .. deprecated:: 3.6
b7e076
 
b7e076
        *key_file* and *cert_file* are deprecated in favor of *context*.
b7e076
diff --git a/Lib/http/client.py b/Lib/http/client.py
b7e076
index 1a6bd8a..f0d2642 100644
b7e076
--- a/Lib/http/client.py
b7e076
+++ b/Lib/http/client.py
b7e076
@@ -1390,6 +1390,9 @@ else:
b7e076
             self.cert_file = cert_file
b7e076
             if context is None:
b7e076
                 context = ssl._create_default_https_context()
b7e076
+                # enable PHA for TLS 1.3 connections if available
b7e076
+                if context.post_handshake_auth is not None:
b7e076
+                    context.post_handshake_auth = True
b7e076
             will_verify = context.verify_mode != ssl.CERT_NONE
b7e076
             if check_hostname is None:
b7e076
                 check_hostname = context.check_hostname
b7e076
@@ -1398,6 +1401,10 @@ else:
b7e076
                                  "either CERT_OPTIONAL or CERT_REQUIRED")
b7e076
             if key_file or cert_file:
b7e076
                 context.load_cert_chain(cert_file, key_file)
b7e076
+                # cert and key file means the user wants to authenticate.
b7e076
+                # enable TLS 1.3 PHA implicitly even for custom contexts.
b7e076
+                if context.post_handshake_auth is not None:
b7e076
+                    context.post_handshake_auth = True
b7e076
             self._context = context
b7e076
             self._check_hostname = check_hostname
b7e076
 
b7e076
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
b7e076
index 714d521..5795b7a 100644
b7e076
--- a/Lib/test/test_httplib.py
b7e076
+++ b/Lib/test/test_httplib.py
b7e076
@@ -1709,6 +1709,24 @@ class HTTPSTest(TestCase):
b7e076
             self.assertEqual(h, c.host)
b7e076
             self.assertEqual(p, c.port)
b7e076
 
b7e076
+    def test_tls13_pha(self):
b7e076
+        import ssl
b7e076
+        if not ssl.HAS_TLSv1_3:
b7e076
+            self.skipTest('TLS 1.3 support required')
b7e076
+        # just check status of PHA flag
b7e076
+        h = client.HTTPSConnection('localhost', 443)
b7e076
+        self.assertTrue(h._context.post_handshake_auth)
b7e076
+
b7e076
+        context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
b7e076
+        self.assertFalse(context.post_handshake_auth)
b7e076
+        h = client.HTTPSConnection('localhost', 443, context=context)
b7e076
+        self.assertIs(h._context, context)
b7e076
+        self.assertFalse(h._context.post_handshake_auth)
b7e076
+
b7e076
+        h = client.HTTPSConnection('localhost', 443, context=context,
b7e076
+                                   cert_file=CERT_localhost)
b7e076
+        self.assertTrue(h._context.post_handshake_auth)
b7e076
+
b7e076
 
b7e076
 class RequestBodyTest(TestCase):
b7e076
     """Test cases where a request includes a message body."""