Blame SOURCES/00326-do-not-set-PHA-verify-flag-on-client-side.patch

b7e076
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
b7e076
index 883201f..cf4d84d 100644
b7e076
--- a/Lib/test/test_ssl.py
b7e076
+++ b/Lib/test/test_ssl.py
b7e076
@@ -3891,6 +3891,37 @@ class TestPostHandshakeAuth(unittest.TestCase):
b7e076
                 s.write(b'PHA')
b7e076
                 self.assertIn(b'WRONG_SSL_VERSION', s.recv(1024))
b7e076
 
b7e076
+    def test_bpo37428_pha_cert_none(self):
b7e076
+        # verify that post_handshake_auth does not implicitly enable cert
b7e076
+        # validation.
b7e076
+        hostname = 'localhost'
b7e076
+        client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
b7e076
+        client_context.post_handshake_auth = True
b7e076
+        client_context.load_cert_chain(SIGNED_CERTFILE)
b7e076
+        # no cert validation and CA on client side
b7e076
+        client_context.check_hostname = False
b7e076
+        client_context.verify_mode = ssl.CERT_NONE
b7e076
+
b7e076
+        server_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
b7e076
+        server_context.load_cert_chain(SIGNED_CERTFILE)
b7e076
+        server_context.load_verify_locations(SIGNING_CA)
b7e076
+        server_context.post_handshake_auth = True
b7e076
+        server_context.verify_mode = ssl.CERT_REQUIRED
b7e076
+
b7e076
+        server = ThreadedEchoServer(context=server_context, chatty=False)
b7e076
+        with server:
b7e076
+            with client_context.wrap_socket(socket.socket(),
b7e076
+                                            server_hostname=hostname) as s:
b7e076
+                s.connect((HOST, server.port))
b7e076
+                s.write(b'HASCERT')
b7e076
+                self.assertEqual(s.recv(1024), b'FALSE\n')
b7e076
+                s.write(b'PHA')
b7e076
+                self.assertEqual(s.recv(1024), b'OK\n')
b7e076
+                s.write(b'HASCERT')
b7e076
+                self.assertEqual(s.recv(1024), b'TRUE\n')
b7e076
+                # server cert has not been validated
b7e076
+                self.assertEqual(s.getpeercert(), {})
b7e076
+
b7e076
 
b7e076
 def test_main(verbose=False):
b7e076
     if support.verbose:
b7e076
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
b7e076
index ec366f0..9bf1cde 100644
b7e076
--- a/Modules/_ssl.c
b7e076
+++ b/Modules/_ssl.c
b7e076
@@ -732,6 +732,26 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
b7e076
 #endif
b7e076
     SSL_set_mode(self->ssl, mode);
b7e076
 
b7e076
+#ifdef TLS1_3_VERSION
b7e076
+    if (sslctx->post_handshake_auth == 1) {
b7e076
+        if (socket_type == PY_SSL_SERVER) {
b7e076
+            /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE.
b7e076
+             * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and
b7e076
+             * only in combination with SSL_VERIFY_PEER flag. */
b7e076
+            int mode = SSL_get_verify_mode(self->ssl);
b7e076
+            if (mode & SSL_VERIFY_PEER) {
b7e076
+                int (*verify_cb)(int, X509_STORE_CTX *) = NULL;
b7e076
+                verify_cb = SSL_get_verify_callback(self->ssl);
b7e076
+                mode |= SSL_VERIFY_POST_HANDSHAKE;
b7e076
+                SSL_set_verify(self->ssl, mode, verify_cb);
b7e076
+            }
b7e076
+        } else {
b7e076
+            /* client socket */
b7e076
+            SSL_set_post_handshake_auth(self->ssl, 1);
b7e076
+        }
b7e076
+    }
b7e076
+#endif
b7e076
+
b7e076
 #if HAVE_SNI
b7e076
     if (server_hostname != NULL) {
b7e076
 /* Don't send SNI for IP addresses. We cannot simply use inet_aton() and
b7e076
@@ -2765,10 +2785,10 @@ _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n)
b7e076
                         "invalid value for verify_mode");
b7e076
         return -1;
b7e076
     }
b7e076
-#ifdef TLS1_3_VERSION
b7e076
-    if (self->post_handshake_auth)
b7e076
-        mode |= SSL_VERIFY_POST_HANDSHAKE;
b7e076
-#endif
b7e076
+
b7e076
+    /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for
b7e076
+     * server sockets and SSL_set_post_handshake_auth() for client. */
b7e076
+
b7e076
     /* keep current verify cb */
b7e076
     verify_cb = SSL_CTX_get_verify_callback(self->ctx);
b7e076
     SSL_CTX_set_verify(self->ctx, mode, verify_cb);
b7e076
@@ -3346,8 +3366,6 @@ get_post_handshake_auth(PySSLContext *self, void *c) {
b7e076
 #if TLS1_3_VERSION
b7e076
 static int
b7e076
 set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) {
b7e076
-    int (*verify_cb)(int, X509_STORE_CTX *) = NULL;
b7e076
-    int mode = SSL_CTX_get_verify_mode(self->ctx);
b7e076
     int pha = PyObject_IsTrue(arg);
b7e076
 
b7e076
     if (pha == -1) {
b7e076
@@ -3355,17 +3373,8 @@ set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) {
b7e076
     }
b7e076
     self->post_handshake_auth = pha;
b7e076
 
b7e076
-    /* client-side socket setting, ignored by server-side */
b7e076
-    SSL_CTX_set_post_handshake_auth(self->ctx, pha);
b7e076
-
b7e076
-    /* server-side socket setting, ignored by client-side */
b7e076
-    verify_cb = SSL_CTX_get_verify_callback(self->ctx);
b7e076
-    if (pha) {
b7e076
-        mode |= SSL_VERIFY_POST_HANDSHAKE;
b7e076
-    } else {
b7e076
-        mode ^= SSL_VERIFY_POST_HANDSHAKE;
b7e076
-    }
b7e076
-    SSL_CTX_set_verify(self->ctx, mode, verify_cb);
b7e076
+    /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for
b7e076
+     * server sockets and SSL_set_post_handshake_auth() for client. */
b7e076
 
b7e076
     return 0;
b7e076
 }