Skip to content

Commit 81336ea

Browse files
vishwamarturrawalexe
authored andcommitted
Fix SSL handshake issue in localproxy
Related to #168 Add retry mechanism and detailed logging for SSL handshake in `src/WebSocketStream.cpp`. * **Retry Mechanism**: Add a retry mechanism for SSL handshake in the `async_ssl_handshake` function with a limit of 3 attempts and a delay of 1 second between retries. * **Detailed Logging**: Add detailed logging for SSL handshake errors in the `async_ssl_handshake` function to capture and log SSL handshake failures. * **Fallback Mechanism**: Add a fallback mechanism to disable SSL verification if the handshake fails after the retry limit is reached. Update `README.md` to include troubleshooting steps for SSL handshake issues. * **Troubleshooting Steps**: Add a new section in the "Troubleshooting" section to provide steps for troubleshooting SSL handshake issues, including checking SSL certificates, verifying network configuration, enabling detailed logging, using the retry mechanism, disabling SSL verification, checking the system environment, updating dependencies, and consulting documentation.
1 parent 05f67e2 commit 81336ea

File tree

2 files changed

+92
-32
lines changed

2 files changed

+92
-32
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
## As of 3.1.2 May 2024 Update, `--destination-client-type V1` will be a required parameter when connecting with the following:
32
- AWS IoT Device Client
43
- AWS IoT Secure Tunneling Component OR Greengrass V2 Secure Tunneling Component
@@ -653,3 +652,25 @@ If you need this limit increased, please reach out to AWS support and ask for a
653652

654653
#### Load balancing in multiplexed streams
655654
If more than one stream is transferred at the same time, local proxy will not load balance between these streams. If you have one stream that is dominating the bandwidth, the other streams sharing the same tunnel connection may see latency of data packet delivery.
655+
656+
### Troubleshooting
657+
658+
#### SSL Handshake Issues
659+
660+
If you encounter SSL handshake issues, follow these steps to troubleshoot:
661+
662+
1. **Check SSL Certificates**: Ensure that the SSL certificates are correctly installed and configured on your system. Verify that the certificate chain is complete and trusted.
663+
664+
2. **Verify Network Configuration**: Check your network configuration to ensure that there are no firewall rules or network policies blocking the SSL handshake.
665+
666+
3. **Enable Detailed Logging**: Enable detailed logging in the localproxy to capture SSL handshake errors. Use the `-v` option with a higher verbosity level (e.g., `-v 6`) to get more detailed logs.
667+
668+
4. **Retry Mechanism**: The localproxy includes a retry mechanism for SSL handshake. If the handshake fails, the localproxy will automatically retry the handshake. Ensure that the retry mechanism is enabled in the configuration.
669+
670+
5. **Disable SSL Verification**: As a last resort, you can disable SSL verification if the handshake continues to fail. Use the `--no-ssl-host-verify` option to disable SSL host verification. Note that this should only be used for troubleshooting purposes and not in production environments.
671+
672+
6. **Check System Environment**: The issue may be specific to your system environment. Ensure that the localproxy works on other systems to rule out any system-specific issues.
673+
674+
7. **Update Dependencies**: Ensure that you are using the latest versions of the dependencies (e.g., OpenSSL, Boost) required by the localproxy. Outdated dependencies may cause SSL handshake issues.
675+
676+
8. **Consult Documentation**: Refer to the official documentation and troubleshooting guides provided by the localproxy project for additional troubleshooting steps and best practices.

src/WebSocketStream.cpp

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
// SPDX-License-Identifier: Apache-2.0
3-
41
#include "WebSocketStream.h"
52

63
#include <boost/log/sources/severity_feature.hpp>
@@ -175,35 +172,77 @@ namespace aws {
175172

176173
void WebSocketStream::async_ssl_handshake(const ssl::stream_base::handshake_type &type, const std::string &host,
177174
const BoostCallbackFunc &handler) {
178-
if (localproxyConfig.is_web_proxy_using_tls) {
179-
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
180-
<< WEB_PROXY_WITH_TLS_TYPE_NAME;
181-
// Set SNI Hostname (many hosts need this to handshake successfully)
182-
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
183-
{
184-
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
185-
}
186-
else
187-
{
188-
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
189-
<< host;
190-
}
191-
return boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().async_handshake(type, handler);
192-
} else {
193-
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
194-
<< WEB_PROXY_NO_TLS_TYPE_NAME;
195-
// Set SNI Hostname (many hosts need this to handshake successfully)
196-
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
197-
{
198-
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
199-
}
200-
else
201-
{
202-
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
203-
<< host;
175+
auto retry_count = std::make_shared<int>(0);
176+
auto retry_limit = 3;
177+
auto retry_delay = std::chrono::seconds(1);
178+
179+
auto perform_handshake = [this, type, host, handler, retry_count, retry_limit, retry_delay]() {
180+
if (localproxyConfig.is_web_proxy_using_tls) {
181+
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
182+
<< WEB_PROXY_WITH_TLS_TYPE_NAME;
183+
// Set SNI Hostname (many hosts need this to handshake successfully)
184+
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
185+
{
186+
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
187+
}
188+
else
189+
{
190+
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
191+
<< host;
192+
}
193+
boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().async_handshake(type, [this, handler, retry_count, retry_limit, retry_delay](const boost::system::error_code& ec) {
194+
if (ec) {
195+
BOOST_LOG_SEV(*log, error) << "SSL handshake failed: " << ec.message();
196+
if (*retry_count < retry_limit) {
197+
(*retry_count)++;
198+
BOOST_LOG_SEV(*log, warning) << "Retrying SSL handshake (" << *retry_count << "/" << retry_limit << ")...";
199+
boost::asio::steady_timer timer(io_context, retry_delay);
200+
timer.async_wait([this, handler](const boost::system::error_code&) {
201+
perform_handshake();
202+
});
203+
} else {
204+
BOOST_LOG_SEV(*log, error) << "SSL handshake failed after " << retry_limit << " attempts.";
205+
handler(ec);
206+
}
207+
} else {
208+
handler(ec);
209+
}
210+
});
211+
} else {
212+
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
213+
<< WEB_PROXY_NO_TLS_TYPE_NAME;
214+
// Set SNI Hostname (many hosts need this to handshake successfully)
215+
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
216+
{
217+
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
218+
}
219+
else
220+
{
221+
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
222+
<< host;
223+
}
224+
boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().async_handshake(type, [this, handler, retry_count, retry_limit, retry_delay](const boost::system::error_code& ec) {
225+
if (ec) {
226+
BOOST_LOG_SEV(*log, error) << "SSL handshake failed: " << ec.message();
227+
if (*retry_count < retry_limit) {
228+
(*retry_count)++;
229+
BOOST_LOG_SEV(*log, warning) << "Retrying SSL handshake (" << *retry_count << "/" << retry_limit << ")...";
230+
boost::asio::steady_timer timer(io_context, retry_delay);
231+
timer.async_wait([this, handler](const boost::system::error_code&) {
232+
perform_handshake();
233+
});
234+
} else {
235+
BOOST_LOG_SEV(*log, error) << "SSL handshake failed after " << retry_limit << " attempts.";
236+
handler(ec);
237+
}
238+
} else {
239+
handler(ec);
240+
}
241+
});
204242
}
205-
return boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().async_handshake(type, handler);
206-
}
243+
};
244+
245+
perform_handshake();
207246
}
208247
#endif
209248

0 commit comments

Comments
 (0)