Skip to content

Commit

Permalink
Fix SSL handshake issue in localproxy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vishwamartur committed Nov 3, 2024
1 parent b9d706f commit a7be116
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 32 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## As of 3.1.2 May 2024 Update, `--destination-client-type V1` will be a required parameter when connecting with the following:
- AWS IoT Device Client
- AWS IoT Secure Tunneling Component
Expand Down Expand Up @@ -627,3 +626,25 @@ There are limits on the maximum streams that can be multiplexed on a tunnel conn

#### Load balancing in multiplexed streams
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.

### Troubleshooting

#### SSL Handshake Issues

If you encounter SSL handshake issues, follow these steps to troubleshoot:

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.

2. **Verify Network Configuration**: Check your network configuration to ensure that there are no firewall rules or network policies blocking the SSL handshake.

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.

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.

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.

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.

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.

8. **Consult Documentation**: Refer to the official documentation and troubleshooting guides provided by the localproxy project for additional troubleshooting steps and best practices.
101 changes: 70 additions & 31 deletions src/WebSocketStream.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#include "WebSocketStream.h"

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

void WebSocketStream::async_ssl_handshake(const ssl::stream_base::handshake_type &type, const std::string &host,
const BoostCallbackFunc &handler) {
if (localproxyConfig.is_web_proxy_using_tls) {
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
<< WEB_PROXY_WITH_TLS_TYPE_NAME;
// Set SNI Hostname (many hosts need this to handshake successfully)
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
}
else
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
<< host;
}
return boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().async_handshake(type, handler);
} else {
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
<< WEB_PROXY_NO_TLS_TYPE_NAME;
// Set SNI Hostname (many hosts need this to handshake successfully)
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
}
else
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
<< host;
auto retry_count = std::make_shared<int>(0);
auto retry_limit = 3;
auto retry_delay = std::chrono::seconds(1);

auto perform_handshake = [this, type, host, handler, retry_count, retry_limit, retry_delay]() {
if (localproxyConfig.is_web_proxy_using_tls) {
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
<< WEB_PROXY_WITH_TLS_TYPE_NAME;
// Set SNI Hostname (many hosts need this to handshake successfully)
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
}
else
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
<< host;
}
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) {
if (ec) {
BOOST_LOG_SEV(*log, error) << "SSL handshake failed: " << ec.message();
if (*retry_count < retry_limit) {
(*retry_count)++;
BOOST_LOG_SEV(*log, warning) << "Retrying SSL handshake (" << *retry_count << "/" << retry_limit << ")...";
boost::asio::steady_timer timer(io_context, retry_delay);
timer.async_wait([this, handler](const boost::system::error_code&) {
perform_handshake();
});
} else {
BOOST_LOG_SEV(*log, error) << "SSL handshake failed after " << retry_limit << " attempts.";
handler(ec);
}
} else {
handler(ec);
}
});
} else {
BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: "
<< WEB_PROXY_NO_TLS_TYPE_NAME;
// Set SNI Hostname (many hosts need this to handshake successfully)
if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str()))
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI";
}
else
{
BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : "
<< host;
}
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) {
if (ec) {
BOOST_LOG_SEV(*log, error) << "SSL handshake failed: " << ec.message();
if (*retry_count < retry_limit) {
(*retry_count)++;
BOOST_LOG_SEV(*log, warning) << "Retrying SSL handshake (" << *retry_count << "/" << retry_limit << ")...";
boost::asio::steady_timer timer(io_context, retry_delay);
timer.async_wait([this, handler](const boost::system::error_code&) {
perform_handshake();
});
} else {
BOOST_LOG_SEV(*log, error) << "SSL handshake failed after " << retry_limit << " attempts.";
handler(ec);
}
} else {
handler(ec);
}
});
}
return boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().async_handshake(type, handler);
}
};

perform_handshake();
}
#endif

Expand Down

0 comments on commit a7be116

Please sign in to comment.