4fbfec
From c9ed53c284a6747f17366eab71ba8922e33910e2 Mon Sep 17 00:00:00 2001
4fbfec
From: Lumir Balhar <lbalhar@redhat.com>
4fbfec
Date: Wed, 28 Aug 2019 14:55:26 +0200
4fbfec
Subject: [PATCH] Backported patch from:
4fbfec
 https://github.com/urllib3/urllib3/commit/6a626be4ff623c25270e20db9002705bf4504e4e
4fbfec
4fbfec
Enable TLS 1.3 post-handshake authentication
4fbfec
---
4fbfec
 src/urllib3/util/ssl_.py |  7 +++++++
4fbfec
 test/test_ssl.py         | 15 +++++++++++++++
4fbfec
 2 files changed, 22 insertions(+)
4fbfec
4fbfec
diff --git a/src/urllib3/util/ssl_.py b/src/urllib3/util/ssl_.py
4fbfec
index 5ae4358..7dc4a5a 100644
4fbfec
--- a/src/urllib3/util/ssl_.py
4fbfec
+++ b/src/urllib3/util/ssl_.py
4fbfec
@@ -280,6 +280,13 @@ def create_urllib3_context(ssl_version=None, cert_reqs=None,
4fbfec
 
4fbfec
     context.options |= options
4fbfec
 
4fbfec
+    # Enable post-handshake authentication for TLS 1.3, see GH #1634. PHA is
4fbfec
+    # necessary for conditional client cert authentication with TLS 1.3.
4fbfec
+    # The attribute is None for OpenSSL <= 1.1.0 or does not exist in older
4fbfec
+    # versions of Python.
4fbfec
+    if getattr(context, "post_handshake_auth", None) is not None:
4fbfec
+        context.post_handshake_auth = True
4fbfec
+
4fbfec
     context.verify_mode = cert_reqs
4fbfec
     if getattr(context, 'check_hostname', None) is not None:  # Platform-specific: Python 3.2
4fbfec
         # We do our own verification, including fingerprints and alternative
4fbfec
diff --git a/test/test_ssl.py b/test/test_ssl.py
4fbfec
index 6a46b4f..3a99522 100644
4fbfec
--- a/test/test_ssl.py
4fbfec
+++ b/test/test_ssl.py
4fbfec
@@ -125,3 +125,18 @@ def test_wrap_socket_default_loads_default_certs(monkeypatch):
4fbfec
     ssl_.ssl_wrap_socket(sock)
4fbfec
 
4fbfec
     context.load_default_certs.assert_called_with()
4fbfec
+
4fbfec
+
4fbfec
+@pytest.mark.parametrize(
4fbfec
+    ["pha", "expected_pha"], [(None, None), (False, True), (True, True)]
4fbfec
+)
4fbfec
+def test_create_urllib3_context_pha(monkeypatch, pha, expected_pha):
4fbfec
+    context = mock.create_autospec(ssl_.SSLContext)
4fbfec
+    context.set_ciphers = mock.Mock()
4fbfec
+    context.options = 0
4fbfec
+    context.post_handshake_auth = pha
4fbfec
+    monkeypatch.setattr(ssl_, "SSLContext", lambda *_, **__: context)
4fbfec
+
4fbfec
+    assert ssl_.create_urllib3_context() is context
4fbfec
+
4fbfec
+    assert context.post_handshake_auth == expected_pha
4fbfec
-- 
4fbfec
2.21.0
4fbfec