From 7cd80e6a6dd48019fb292e49eab10eefff293132 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sat, 28 Jan 2023 09:39:31 +0100 Subject: [PATCH] [Net] Remove StreamPeerTLS.blocking_handshake option. Which was unused internally, and can be replaced by: ``` while tls.get_status() == tls.STATUS_HANDSHAKING: tls.poll() ``` --- core/io/http_client_tcp.cpp | 1 - core/io/stream_peer_tls.cpp | 16 +------------ core/io/stream_peer_tls.h | 7 ------ doc/classes/StreamPeerTLS.xml | 4 ---- modules/mbedtls/stream_peer_mbedtls.cpp | 29 +++++++++--------------- modules/websocket/wsl_peer.cpp | 1 - platform/web/export/editor_http_server.h | 1 - 7 files changed, 12 insertions(+), 47 deletions(-) diff --git a/core/io/http_client_tcp.cpp b/core/io/http_client_tcp.cpp index a36c59d11ff..3788fa501e9 100644 --- a/core/io/http_client_tcp.cpp +++ b/core/io/http_client_tcp.cpp @@ -364,7 +364,6 @@ Error HTTPClientTCP::poll() { if (!handshaking) { // Connect the StreamPeerTLS and start handshaking. tls_conn = Ref(StreamPeerTLS::create()); - tls_conn->set_blocking_handshake_enabled(false); Error err = tls_conn->connect_to_stream(tcp_connection, conn_host, tls_options); if (err != OK) { close(); diff --git a/core/io/stream_peer_tls.cpp b/core/io/stream_peer_tls.cpp index 2facf3e32e8..69877974e68 100644 --- a/core/io/stream_peer_tls.cpp +++ b/core/io/stream_peer_tls.cpp @@ -41,18 +41,8 @@ StreamPeerTLS *StreamPeerTLS::create() { return nullptr; } -bool StreamPeerTLS::available = false; - bool StreamPeerTLS::is_available() { - return available; -} - -void StreamPeerTLS::set_blocking_handshake_enabled(bool p_enabled) { - blocking_handshake = p_enabled; -} - -bool StreamPeerTLS::is_blocking_handshake_enabled() const { - return blocking_handshake; + return _create != nullptr; } void StreamPeerTLS::_bind_methods() { @@ -62,10 +52,6 @@ void StreamPeerTLS::_bind_methods() { ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerTLS::get_status); ClassDB::bind_method(D_METHOD("get_stream"), &StreamPeerTLS::get_stream); ClassDB::bind_method(D_METHOD("disconnect_from_stream"), &StreamPeerTLS::disconnect_from_stream); - ClassDB::bind_method(D_METHOD("set_blocking_handshake_enabled", "enabled"), &StreamPeerTLS::set_blocking_handshake_enabled); - ClassDB::bind_method(D_METHOD("is_blocking_handshake_enabled"), &StreamPeerTLS::is_blocking_handshake_enabled); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_handshake"), "set_blocking_handshake_enabled", "is_blocking_handshake_enabled"); BIND_ENUM_CONSTANT(STATUS_DISCONNECTED); BIND_ENUM_CONSTANT(STATUS_HANDSHAKING); diff --git a/core/io/stream_peer_tls.h b/core/io/stream_peer_tls.h index ff3ea2a7d13..5894abb7a46 100644 --- a/core/io/stream_peer_tls.h +++ b/core/io/stream_peer_tls.h @@ -41,10 +41,6 @@ protected: static StreamPeerTLS *(*_create)(); static void _bind_methods(); - static bool available; - - bool blocking_handshake = true; - public: enum Status { STATUS_DISCONNECTED, @@ -54,9 +50,6 @@ public: STATUS_ERROR_HOSTNAME_MISMATCH }; - void set_blocking_handshake_enabled(bool p_enabled); - bool is_blocking_handshake_enabled() const; - virtual void poll() = 0; virtual Error accept_stream(Ref p_base, Ref p_options) = 0; virtual Error connect_to_stream(Ref p_base, const String &p_common_name, Ref p_options) = 0; diff --git a/doc/classes/StreamPeerTLS.xml b/doc/classes/StreamPeerTLS.xml index df33baa9008..a5d882a7137 100644 --- a/doc/classes/StreamPeerTLS.xml +++ b/doc/classes/StreamPeerTLS.xml @@ -53,10 +53,6 @@ - - - - A status representing a [StreamPeerTLS] that is disconnected. diff --git a/modules/mbedtls/stream_peer_mbedtls.cpp b/modules/mbedtls/stream_peer_mbedtls.cpp index 0ea426c8b58..a9d187bd64a 100644 --- a/modules/mbedtls/stream_peer_mbedtls.cpp +++ b/modules/mbedtls/stream_peer_mbedtls.cpp @@ -80,22 +80,17 @@ void StreamPeerMbedTLS::_cleanup() { } Error StreamPeerMbedTLS::_do_handshake() { - int ret = 0; - while ((ret = mbedtls_ssl_handshake(tls_ctx->get_context())) != 0) { - if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { - // An error occurred. - ERR_PRINT("TLS handshake error: " + itos(ret)); - TLSContextMbedTLS::print_mbedtls_error(ret); - disconnect_from_stream(); - status = STATUS_ERROR; - return FAILED; - } - - // Handshake is still in progress. - if (!blocking_handshake) { - // Will retry via poll later - return OK; - } + int ret = mbedtls_ssl_handshake(tls_ctx->get_context()); + if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { + // Handshake is still in progress, will retry via poll later. + return OK; + } else if (ret != 0) { + // An error occurred. + ERR_PRINT("TLS handshake error: " + itos(ret)); + TLSContextMbedTLS::print_mbedtls_error(ret); + disconnect_from_stream(); + status = STATUS_ERROR; + return FAILED; } status = STATUS_CONNECTED; @@ -306,10 +301,8 @@ StreamPeerTLS *StreamPeerMbedTLS::_create_func() { void StreamPeerMbedTLS::initialize_tls() { _create = _create_func; - available = true; } void StreamPeerMbedTLS::finalize_tls() { - available = false; _create = nullptr; } diff --git a/modules/websocket/wsl_peer.cpp b/modules/websocket/wsl_peer.cpp index 6240f2eaeaf..8a150c8561b 100644 --- a/modules/websocket/wsl_peer.cpp +++ b/modules/websocket/wsl_peer.cpp @@ -333,7 +333,6 @@ void WSLPeer::_do_client_handshake() { // Start SSL handshake tls = Ref(StreamPeerTLS::create()); ERR_FAIL_COND_MSG(tls.is_null(), "SSL is not available in this build."); - tls->set_blocking_handshake_enabled(false); if (tls->connect_to_stream(tcp, requested_host, tls_options) != OK) { close(-1); return; // Error. diff --git a/platform/web/export/editor_http_server.h b/platform/web/export/editor_http_server.h index 80ff779949e..3f872885377 100644 --- a/platform/web/export/editor_http_server.h +++ b/platform/web/export/editor_http_server.h @@ -205,7 +205,6 @@ public: if (tls.is_null()) { tls = Ref(StreamPeerTLS::create()); peer = tls; - tls->set_blocking_handshake_enabled(false); if (tls->accept_stream(tcp, TLSOptions::server(key, cert)) != OK) { _clear_client(); return;