@@ -2251,6 +2251,17 @@ PySSL_dealloc(PySSLSocket *self)
2251
2251
PyTypeObject * tp = Py_TYPE (self );
2252
2252
PyObject_GC_UnTrack (self );
2253
2253
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 );
2254
2265
SSL_free (self -> ssl );
2255
2266
}
2256
2267
Py_XDECREF (self -> Socket );
@@ -2795,48 +2806,6 @@ _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self)
2795
2806
#endif
2796
2807
}
2797
2808
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
-
2840
2809
static PyObject *
2841
2810
PySSL_get_session (PySSLSocket * self , void * closure ) {
2842
2811
/* get_session can return sessions from a server-side connection,
@@ -2865,11 +2834,8 @@ PySSL_get_session(PySSLSocket *self, void *closure) {
2865
2834
}
2866
2835
2867
2836
static int PySSL_set_session (PySSLSocket * self , PyObject * value ,
2868
- void * closure )
2869
- {
2837
+ void * closure ) {
2870
2838
PySSLSession * pysess ;
2871
- SSL_SESSION * session ;
2872
- int result ;
2873
2839
2874
2840
if (!Py_IS_TYPE (value , get_state_sock (self )-> PySSLSession_Type )) {
2875
2841
PyErr_SetString (PyExc_TypeError , "Value is not a SSLSession." );
@@ -2892,14 +2858,7 @@ static int PySSL_set_session(PySSLSocket *self, PyObject *value,
2892
2858
"Cannot set session after handshake." );
2893
2859
return -1 ;
2894
2860
}
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 ) {
2903
2862
_setSSLError (get_state_sock (self ), NULL , 0 , __FILE__ , __LINE__ );
2904
2863
return -1 ;
2905
2864
}
0 commit comments