@@ -963,6 +963,26 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
963963 SSL_set_mode (self -> ssl ,
964964 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY );
965965
966+ #ifdef TLS1_3_VERSION
967+ if (sslctx -> post_handshake_auth == 1 ) {
968+ if (socket_type == PY_SSL_SERVER ) {
969+ /* bpo-37428: OpenSSL does not ignore SSL_VERIFY_POST_HANDSHAKE.
970+ * Set SSL_VERIFY_POST_HANDSHAKE flag only for server sockets and
971+ * only in combination with SSL_VERIFY_PEER flag. */
972+ int mode = SSL_get_verify_mode (self -> ssl );
973+ if (mode & SSL_VERIFY_PEER ) {
974+ int (* verify_cb )(int , X509_STORE_CTX * ) = NULL ;
975+ verify_cb = SSL_get_verify_callback (self -> ssl );
976+ mode |= SSL_VERIFY_POST_HANDSHAKE ;
977+ SSL_set_verify (self -> ssl , mode , verify_cb );
978+ }
979+ } else {
980+ /* client socket */
981+ SSL_set_post_handshake_auth (self -> ssl , 1 );
982+ }
983+ }
984+ #endif
985+
966986 if (server_hostname != NULL ) {
967987 if (_ssl_configure_hostname (self , server_hostname ) < 0 ) {
968988 Py_DECREF (self );
@@ -2986,10 +3006,10 @@ _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n)
29863006 "invalid value for verify_mode" );
29873007 return -1 ;
29883008 }
2989- #ifdef TLS1_3_VERSION
2990- if ( self -> post_handshake_auth )
2991- mode |= SSL_VERIFY_POST_HANDSHAKE ;
2992- #endif
3009+
3010+ /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for
3011+ * server sockets and SSL_set_post_handshake_auth() for client. */
3012+
29933013 /* keep current verify cb */
29943014 verify_cb = SSL_CTX_get_verify_callback (self -> ctx );
29953015 SSL_CTX_set_verify (self -> ctx , mode , verify_cb );
@@ -3735,8 +3755,6 @@ get_post_handshake_auth(PySSLContext *self, void *c) {
37353755#if TLS1_3_VERSION
37363756static int
37373757set_post_handshake_auth (PySSLContext * self , PyObject * arg , void * c ) {
3738- int (* verify_cb )(int , X509_STORE_CTX * ) = NULL ;
3739- int mode = SSL_CTX_get_verify_mode (self -> ctx );
37403758 if (arg == NULL ) {
37413759 PyErr_SetString (PyExc_AttributeError , "cannot delete attribute" );
37423760 return -1 ;
@@ -3748,17 +3766,8 @@ set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) {
37483766 }
37493767 self -> post_handshake_auth = pha ;
37503768
3751- /* client-side socket setting, ignored by server-side */
3752- SSL_CTX_set_post_handshake_auth (self -> ctx , pha );
3753-
3754- /* server-side socket setting, ignored by client-side */
3755- verify_cb = SSL_CTX_get_verify_callback (self -> ctx );
3756- if (pha ) {
3757- mode |= SSL_VERIFY_POST_HANDSHAKE ;
3758- } else {
3759- mode ^= SSL_VERIFY_POST_HANDSHAKE ;
3760- }
3761- SSL_CTX_set_verify (self -> ctx , mode , verify_cb );
3769+ /* bpo-37428: newPySSLSocket() sets SSL_VERIFY_POST_HANDSHAKE flag for
3770+ * server sockets and SSL_set_post_handshake_auth() for client. */
37623771
37633772 return 0 ;
37643773}
0 commit comments