Skip to content

Commit 89cd157

Browse files
committed
add error checking for SSL_new_stream and SSL_accept_stream
1 parent 629ffb4 commit 89cd157

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

ext/openssl/ossl_ssl.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,23 @@ no_exception_p(VALUE opts)
18021802
return 0;
18031803
}
18041804

1805+
static VALUE
1806+
ossl_ssl_quic_null_error(SSL *ssl, const char *funcname, VALUE opts)
1807+
{
1808+
int err = SSL_get_error(ssl, 0);
1809+
1810+
switch (err) {
1811+
case SSL_ERROR_NONE:
1812+
case SSL_ERROR_WANT_READ:
1813+
if (no_exception_p(opts))
1814+
return sym_wait_readable;
1815+
ossl_raise(eSSLErrorWaitReadable, "%s would block", funcname);
1816+
default:
1817+
ossl_raise(eSSLError, "%s", funcname);
1818+
}
1819+
}
1820+
1821+
18051822
// Provided by Ruby 3.2.0 and later in order to support the default IO#timeout.
18061823
#ifndef RUBY_IO_TIMEOUT_DEFAULT
18071824
#define RUBY_IO_TIMEOUT_DEFAULT Qnil
@@ -2829,8 +2846,15 @@ ossl_ssl_new_stream(int argc, VALUE *argv, VALUE self)
28292846
GetSSL(self, ssl);
28302847
stream_ssl = SSL_new_stream(ssl, flags);
28312848
if (!stream_ssl) {
2832-
if (flags & SSL_STREAM_FLAG_NO_BLOCK)
2833-
return Qnil;
2849+
if (flags & SSL_STREAM_FLAG_NO_BLOCK) {
2850+
switch (SSL_get_error(ssl, 0)) {
2851+
case SSL_ERROR_NONE:
2852+
case SSL_ERROR_WANT_READ:
2853+
return Qnil;
2854+
default:
2855+
ossl_raise(eSSLError, "SSL_new_stream");
2856+
}
2857+
}
28342858
ossl_raise(eSSLError, "SSL_new_stream");
28352859
}
28362860

@@ -2881,11 +2905,8 @@ ossl_ssl_accept_stream_nonblock(int argc, VALUE *argv, VALUE self)
28812905

28822906
GetSSL(self, ssl);
28832907
stream_ssl = SSL_accept_stream(ssl, SSL_ACCEPT_STREAM_NO_BLOCK);
2884-
if (!stream_ssl) {
2885-
if (no_exception_p(opts))
2886-
return sym_wait_readable;
2887-
ossl_raise(eSSLErrorWaitReadable, "accept_stream would block");
2888-
}
2908+
if (!stream_ssl)
2909+
return ossl_ssl_quic_null_error(ssl, "SSL_accept_stream", opts);
28892910

28902911
SSL_set_blocking_mode(stream_ssl, 0);
28912912
SSL_set_default_stream_mode(stream_ssl, SSL_DEFAULT_STREAM_MODE_NONE);

0 commit comments

Comments
 (0)