-
Notifications
You must be signed in to change notification settings - Fork 196
Look the SSLContext up from the SSL_CTX in the ALPN/NPN callbacks #1089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,19 @@ ossl_sslctx_mark(void *ptr) | |
| rb_gc_mark_movable((VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx)); | ||
| } | ||
|
|
||
| /* | ||
| * The SSLContext's VALUE is stored in exactly one place -- the SSL_CTX's ex_data, | ||
| * which ossl_sslctx_compact keeps up to date. Callbacks must go through here rather | ||
| * than capture their own copy, which nothing would relocate. | ||
| */ | ||
| static VALUE | ||
| ossl_sslctx_obj_from_ssl(const SSL *ssl) | ||
| { | ||
| SSL_CTX *ctx = SSL_get_SSL_CTX(ssl); | ||
|
|
||
| return (VALUE)SSL_CTX_get_ex_data(ctx, ossl_sslctx_ex_ptr_idx); | ||
| } | ||
|
|
||
| static void | ||
| ossl_sslctx_free(void *ptr) | ||
| { | ||
|
|
@@ -584,7 +597,8 @@ static int | |
| ssl_npn_advertise_cb(SSL *ssl, const unsigned char **out, unsigned int *outlen, | ||
| void *arg) | ||
| { | ||
| VALUE protocols = rb_attr_get((VALUE)arg, id_npn_protocols_encoded); | ||
| VALUE protocols = rb_attr_get(ossl_sslctx_obj_from_ssl(ssl), | ||
| id_npn_protocols_encoded); | ||
|
|
||
| *out = (const unsigned char *) RSTRING_PTR(protocols); | ||
| *outlen = RSTRING_LENINT(protocols); | ||
|
|
@@ -598,7 +612,7 @@ ssl_npn_select_cb(SSL *ssl, unsigned char **out, unsigned char *outlen, | |
| { | ||
| VALUE sslctx_obj, cb; | ||
|
|
||
| sslctx_obj = (VALUE) arg; | ||
| sslctx_obj = ossl_sslctx_obj_from_ssl(ssl); | ||
| cb = rb_attr_get(sslctx_obj, id_i_npn_select_cb); | ||
|
|
||
| return ssl_npn_select_cb_common(ssl, cb, (const unsigned char **)out, | ||
|
|
@@ -612,7 +626,7 @@ ssl_alpn_select_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, | |
| { | ||
| VALUE sslctx_obj, cb; | ||
|
|
||
| sslctx_obj = (VALUE) arg; | ||
| sslctx_obj = ossl_sslctx_obj_from_ssl(ssl); | ||
| cb = rb_attr_get(sslctx_obj, id_i_alpn_select_cb); | ||
|
|
||
| return ssl_npn_select_cb_common(ssl, cb, out, outlen, in, inlen); | ||
|
|
@@ -807,11 +821,11 @@ ossl_sslctx_setup(VALUE self) | |
| if (!NIL_P(val)) { | ||
| VALUE encoded = ssl_encode_npn_protocols(val); | ||
| rb_ivar_set(self, id_npn_protocols_encoded, encoded); | ||
| SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_npn_advertise_cb, (void *)self); | ||
| SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_npn_advertise_cb, NULL); | ||
| OSSL_Debug("SSL NPN advertise callback added"); | ||
| } | ||
| if (RTEST(rb_attr_get(self, id_i_npn_select_cb))) { | ||
| SSL_CTX_set_next_proto_select_cb(ctx, ssl_npn_select_cb, (void *) self); | ||
| SSL_CTX_set_next_proto_select_cb(ctx, ssl_npn_select_cb, NULL); | ||
| OSSL_Debug("SSL NPN select callback added"); | ||
| } | ||
| #endif | ||
|
|
@@ -827,7 +841,7 @@ ossl_sslctx_setup(VALUE self) | |
| OSSL_Debug("SSL ALPN values added"); | ||
| } | ||
| if (RTEST(rb_attr_get(self, id_i_alpn_select_cb))) { | ||
| SSL_CTX_set_alpn_select_cb(ctx, ssl_alpn_select_cb, (void *) self); | ||
| SSL_CTX_set_alpn_select_cb(ctx, ssl_alpn_select_cb, NULL); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth a second look at the SNI case: I think the lookup tracks it correctly. OpenSSL fetches the callback and its arg from the same And after an SNI swap the new context is the one we want. If you'd rather not lean on
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I had assumed the ALPN/NPN callbacks were part of the configuration copied to the Passing the original |
||
| OSSL_Debug("SSL ALPN select callback added"); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this closer to the callers?