Skip to content

Commit 77e8b6d

Browse files
committed
ssl: keep original SSLContext alive after servername_cb
Keep the original SSLContext in a separate instance variable to prevent it from being GC'ed while its underlying SSL_CTX is still in use. When the SNI callback accepts the provided server name, it can replace the SSL_CTX with SSL_set_SSL_CTX() and update SSLSocket#context. However, although this is poorly documented, SSL_set_SSL_CTX() does not appear to replace all uses of the original SSL_CTX with the new one. In particular, session management still uses the original SSL_CTX and therefore requires that the corresponding SSLContext object to remain alive for the lifetime of SSLSocket.
1 parent 9796ee8 commit 77e8b6d

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

‎ext/openssl/ossl_ssl.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ VALUE cSSLSocket;
3636
static VALUE eSSLErrorWaitReadable;
3737
static VALUE eSSLErrorWaitWritable;
3838

39-
static ID id_call, ID_callback_state, id_npn_protocols_encoded, id_each;
39+
static ID id_call, ID_callback_state, id_npn_protocols_encoded, id_each,
40+
id_original_context;
4041
static VALUE sym_exception, sym_wait_readable, sym_wait_writable;
4142

4243
static ID id_i_cert_store, id_i_ca_file, id_i_ca_path, id_i_verify_mode,
@@ -1694,6 +1695,7 @@ ossl_ssl_initialize(int argc, VALUE *argv, VALUE self)
16941695

16951696
GetSSLCTX(v_ctx, ctx);
16961697
rb_ivar_set(self, id_i_context, v_ctx);
1698+
rb_ivar_set(self, id_original_context, v_ctx);
16971699
ossl_sslctx_setup(v_ctx);
16981700

16991701
if (rb_respond_to(io, rb_intern("nonblock=")))
@@ -3359,6 +3361,7 @@ Init_ossl_ssl(void)
33593361

33603362
id_npn_protocols_encoded = rb_intern_const("npn_protocols_encoded");
33613363
id_each = rb_intern_const("each");
3364+
id_original_context = rb_intern_const("original_context");
33623365

33633366
#define DefIVarID(name) do \
33643367
id_i_##name = rb_intern_const("@"#name); while (0)

0 commit comments

Comments
 (0)