@@ -963,6 +963,26 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
963
963
SSL_set_mode (self -> ssl ,
964
964
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_AUTO_RETRY );
965
965
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
+
966
986
if (server_hostname != NULL ) {
967
987
if (_ssl_configure_hostname (self , server_hostname ) < 0 ) {
968
988
Py_DECREF (self );
@@ -2986,10 +3006,10 @@ _set_verify_mode(PySSLContext *self, enum py_ssl_cert_requirements n)
2986
3006
"invalid value for verify_mode" );
2987
3007
return -1 ;
2988
3008
}
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
+
2993
3013
/* keep current verify cb */
2994
3014
verify_cb = SSL_CTX_get_verify_callback (self -> ctx );
2995
3015
SSL_CTX_set_verify (self -> ctx , mode , verify_cb );
@@ -3735,8 +3755,6 @@ get_post_handshake_auth(PySSLContext *self, void *c) {
3735
3755
#if TLS1_3_VERSION
3736
3756
static int
3737
3757
set_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 );
3740
3758
if (arg == NULL ) {
3741
3759
PyErr_SetString (PyExc_AttributeError , "cannot delete attribute" );
3742
3760
return -1 ;
@@ -3748,17 +3766,8 @@ set_post_handshake_auth(PySSLContext *self, PyObject *arg, void *c) {
3748
3766
}
3749
3767
self -> post_handshake_auth = pha ;
3750
3768
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. */
3762
3771
3763
3772
return 0 ;
3764
3773
}
0 commit comments