Skip to content

Commit 801c7df

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. When the SNI callback accepts the provided server name, it may replace the SSL_CTX with SSL_set_SSL_CTX() and update SSLSocket#context. However, despite its name, SSL_set_SSL_CTX() does not use all parameters from the new SSL_CTX. In particular, callbacks set by the original SSL_CTX remain in use and therefore require the corresponding SSLContext object to stay alive.
1 parent 9796ee8 commit 801c7df

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)