@@ -2251,6 +2251,17 @@ PySSL_dealloc(PySSLSocket *self)
22512251 PyTypeObject * tp = Py_TYPE (self );
22522252 PyObject_GC_UnTrack (self );
22532253 if (self -> ssl ) {
2254+ // If we free the SSL socket object without having called SSL_shutdown,
2255+ // OpenSSL will invalidate the linked SSL session object. While this
2256+ // behavior is strictly RFC-compliant, it makes session reuse less
2257+ // likely and it would also break compatibility with older stdlib
2258+ // versions (which used an ugly workaround of duplicating the
2259+ // SSL_SESSION object).
2260+ // Therefore, we ensure the socket is marked as shutdown in any case.
2261+ //
2262+ // See elaborate explanation at
2263+ // https://github.com/python/cpython/pull/123249#discussion_r1766164530
2264+ SSL_set_shutdown (self -> ssl , SSL_SENT_SHUTDOWN );
22542265 SSL_free (self -> ssl );
22552266 }
22562267 Py_XDECREF (self -> Socket );
@@ -2795,48 +2806,6 @@ _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self)
27952806#endif
27962807}
27972808
2798- static SSL_SESSION *
2799- _ssl_session_dup (SSL_SESSION * session ) {
2800- SSL_SESSION * newsession = NULL ;
2801- int slen ;
2802- unsigned char * senc = NULL , * p ;
2803- const unsigned char * const_p ;
2804-
2805- if (session == NULL ) {
2806- PyErr_SetString (PyExc_ValueError , "Invalid session" );
2807- goto error ;
2808- }
2809-
2810- /* get length */
2811- slen = i2d_SSL_SESSION (session , NULL );
2812- if (slen == 0 || slen > 0xFF00 ) {
2813- PyErr_SetString (PyExc_ValueError , "i2d() failed" );
2814- goto error ;
2815- }
2816- if ((senc = PyMem_Malloc (slen )) == NULL ) {
2817- PyErr_NoMemory ();
2818- goto error ;
2819- }
2820- p = senc ;
2821- if (!i2d_SSL_SESSION (session , & p )) {
2822- PyErr_SetString (PyExc_ValueError , "i2d() failed" );
2823- goto error ;
2824- }
2825- const_p = senc ;
2826- newsession = d2i_SSL_SESSION (NULL , & const_p , slen );
2827- if (newsession == NULL ) {
2828- PyErr_SetString (PyExc_ValueError , "d2i() failed" );
2829- goto error ;
2830- }
2831- PyMem_Free (senc );
2832- return newsession ;
2833- error :
2834- if (senc != NULL ) {
2835- PyMem_Free (senc );
2836- }
2837- return NULL ;
2838- }
2839-
28402809static PyObject *
28412810PySSL_get_session (PySSLSocket * self , void * closure ) {
28422811 /* get_session can return sessions from a server-side connection,
@@ -2865,11 +2834,8 @@ PySSL_get_session(PySSLSocket *self, void *closure) {
28652834}
28662835
28672836static int PySSL_set_session (PySSLSocket * self , PyObject * value ,
2868- void * closure )
2869- {
2837+ void * closure ) {
28702838 PySSLSession * pysess ;
2871- SSL_SESSION * session ;
2872- int result ;
28732839
28742840 if (!Py_IS_TYPE (value , get_state_sock (self )-> PySSLSession_Type )) {
28752841 PyErr_SetString (PyExc_TypeError , "Value is not a SSLSession." );
@@ -2892,14 +2858,7 @@ static int PySSL_set_session(PySSLSocket *self, PyObject *value,
28922858 "Cannot set session after handshake." );
28932859 return -1 ;
28942860 }
2895- /* duplicate session */
2896- if ((session = _ssl_session_dup (pysess -> session )) == NULL ) {
2897- return -1 ;
2898- }
2899- result = SSL_set_session (self -> ssl , session );
2900- /* free duplicate, SSL_set_session() bumps ref count */
2901- SSL_SESSION_free (session );
2902- if (result == 0 ) {
2861+ if (SSL_set_session (self -> ssl , pysess -> session ) == 0 ) {
29032862 _setSSLError (get_state_sock (self ), NULL , 0 , __FILE__ , __LINE__ );
29042863 return -1 ;
29052864 }
0 commit comments