Skip to content

Fix SSL handshake issue in localproxy #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading