Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Refactor callbacks in OpenSSL::SSL#1080

Open
rhenium wants to merge 8 commits into
ruby:masterfrom
rhenium:ky/ssl-callback-errors
Open

Refactor callbacks in OpenSSL::SSL#1080
rhenium wants to merge 8 commits into
ruby:masterfrom
rhenium:ky/ssl-callback-errors

Conversation

@rhenium

@rhenium rhenium commented Jul 5, 2026

Copy link
Copy Markdown
Member

Extracted from #736


ssl: add struct ossl_ssl_data

We will need to store additional internal states in SSLSocket in the upcoming patches. Prepare a new struct for such purposes and store it in SSL's ex_data.

Refactor existing internal states that are local to C to use this new struct:

  • The back reference to the Ruby object, currently in another ex_data
  • The callback jump tag, currently in an instance variable

ssl: do not call another callback when an exception is pending

Do not attempt to execute Ruby callback when another callback has already raised an exception and we are waiting for a chance to call rb_jump_tag().

As far as I know, this currently cannot happen. However, an upcoming patch to use custom BIO_METHOD in SSLSocket may make it possible. Let's handle it explicitly.


ssl: refactor info_callback

Set the callback to SSL_CTX instead of SSL so that we do not have to configure every SSL object, similar to all other existing callbacks.

Also, wrap rb_funcall() in rb_protect() to prevent exiting from SSL_accept() prematurely.


ssl: re-raise exceptions in client_cert_cb

Re-raise exceptions raised inside client_cert_cb from the originating Ruby method, which can be any of #connect, #sysread*, or #syswrite*.


ssl: simplify looking up callback procs for session_{get,new}_cb

Look up the callback procs by checking the corresponding instance variables on SSLContext directly. The removed methods SSLSocket#session_new_cb and #session_get_cb are private and have not been part of the public interface of ruby/openssl.

Also remove the unused method SSLSocket#client_cert_cb.


ssl: refactor session_{get,new,remove}_cb

Prepare the argument array object inside rb_protect(). The callback proc is already called in it, but the array allocation can also potentially fail and needs to be done there.

rhenium added 8 commits July 5, 2026 19:49
We will need to store additional internal states in SSLSocket in the
upcoming patches. Prepare a new struct for such purposes and store it
in SSL's ex_data.

Refactor existing internal states that are local to C to use this new
struct:

  - The back reference to the Ruby object, currently in another ex_data
  - The callback jump tag, currently in an instance variable
Do not attempt to execute Ruby callback when another callback has
already raised an exception and we are waiting for a chance to call
rb_jump_tag().

As far as I know, this currently cannot happen. However, an upcoming
patch to use custom BIO_METHOD in SSLSocket may make it possible. Let's
handle it explicitly.
Set the callback to SSL_CTX instead of SSL so that we do not have to
configure every SSL object, similar to all other existing callbacks.

Also, wrap rb_funcall() in rb_protect() to prevent exiting from
SSL_accept() prematurely.
Re-raise exceptions raised inside client_cert_cb from the originating
Ruby method, which can be any of #connect, #sysread*, or #syswrite*.
Look up the callback procs by checking the corresponding instance
variables on SSLContext directly. The removed methods
SSLSocket#session_new_cb and #session_get_cb are private and have not
been part of the public interface of ruby/openssl.

Also remove the unused method SSLSocket#client_cert_cb.
Prepare the argument array object inside rb_protect(). The callback proc
is already called in it, but the array allocation can also potentially
fail and needs to be done there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant