Skip to content

Commit 7615610

Browse files
committed
LibTLS: Notify on_ready_to_read after handling fatal errors
The `on_ready_to_read` callback on the underlying socket will be called for various reasons which do not always guarantee that the next read operation will be successful. For example, the server might have sent an alert or a TCP RST. We handle fatal errors on the SSL connection before calling to the user so that `can_read_without_blocking` does not falsely advertise. The same checks should be performed there, but it is not possible due to the function being const.
1 parent c82e016 commit 7615610

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Libraries/LibTLS/TLSv12.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ TLSv12::TLSv12(NonnullOwnPtr<Core::TCPSocket> socket, SSL_CTX* ssl_ctx, SSL* ssl
154154
, m_socket(move(socket))
155155
{
156156
m_socket->on_ready_to_read = [this] {
157+
// There is something to read on the underlying TCP connection. This doesn't mean there is actual data to read from the SSL connection.
158+
// For example, we might have received an alert or a connection reset.
159+
160+
char buffer[1];
161+
auto ret = SSL_peek(m_ssl, buffer, 1);
162+
if (ret <= 0) {
163+
switch (SSL_get_error(m_ssl, ret)) {
164+
case SSL_ERROR_SSL:
165+
case SSL_ERROR_SYSCALL:
166+
handle_fatal_error();
167+
break;
168+
default:
169+
break;
170+
}
171+
}
172+
173+
// Now that we handled possible fatal errors, we can notify the user that there is data to read.
157174
if (on_ready_to_read)
158175
on_ready_to_read();
159176
};

0 commit comments

Comments
 (0)