Skip to content

Add TLS extension SNI for boost asio based http_client #39

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ Gery Vessere (gery@vessere.com)
Cisco Systems
Gergely Lukacsy (glukacsy)

Ocedo GmbH
Henning Pfeiffer (megaposer)

thomasschaub
21 changes: 21 additions & 0 deletions Release/include/cpprest/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class http_client_config
, m_set_user_nativehandle_options([](native_handle)->void{})
#if !defined(_WIN32) && !defined(__cplusplus_winrt)
, m_ssl_context_callback([](boost::asio::ssl::context&)->void{})
, m_tlsext_sni_enabled(true)
#endif
#if defined(_WIN32) && !defined(__cplusplus_winrt)
, m_buffer_request(false)
Expand Down Expand Up @@ -347,6 +348,25 @@ class http_client_config
{
return m_ssl_context_callback;
}

/// <summary>
/// Gets the TLS extension server name indication (SNI) status.
/// </summary>
/// <returns>True if TLS server name indication is enabled, false otherwise.</returns>
bool is_tlsext_sni_enabled() const
{
return m_tlsext_sni_enabled;
}

/// <summary>
/// Sets the TLS extension server name indication (SNI) status.
/// </summary>
/// <param name="tlsext_sni_enabled">False to disable the TLS (ClientHello) extension for server name indication, true otherwise.</param>
/// <remarks>Note: This setting is enabled by default as it is required in most virtual hosting scenarios.</remarks>
void set_tlsext_sni_enabled(bool tlsext_sni_enabled)
{
m_tlsext_sni_enabled = tlsext_sni_enabled;
}
#endif

private:
Expand All @@ -372,6 +392,7 @@ class http_client_config

#if !defined(_WIN32) && !defined(__cplusplus_winrt)
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
bool m_tlsext_sni_enabled;
#endif
#if defined(_WIN32) && !defined(__cplusplus_winrt)
bool m_buffer_request;
Expand Down
9 changes: 9 additions & 0 deletions Release/src/http/client/http_client_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class asio_connection
template <typename HandshakeHandler, typename CertificateHandler>
void async_handshake(boost::asio::ssl::stream_base::handshake_type type,
const http_client_config &config,
const utility::string_t &host_name,
const HandshakeHandler &handshake_handler,
const CertificateHandler &cert_handler)
{
Expand All @@ -152,6 +153,13 @@ class asio_connection
{
m_ssl_stream->set_verify_mode(boost::asio::ssl::context::verify_none);
}

// Check to set host name for Server Name Indication (SNI)
if (config.is_tlsext_sni_enabled())
{
SSL_set_tlsext_host_name(m_ssl_stream->native_handle(), const_cast<char *>(host_name.data()));
}

m_ssl_stream->async_handshake(type, handshake_handler);
}

Expand Down Expand Up @@ -561,6 +569,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
const auto weakCtx = std::weak_ptr<asio_context>(shared_from_this());
m_connection->async_handshake(boost::asio::ssl::stream_base::client,
m_http_client->client_config(),
m_http_client->base_uri().host(),
boost::bind(&asio_context::handle_handshake, shared_from_this(), boost::asio::placeholders::error),

// Use a weak_ptr since the verify_callback is stored until the connection is destroyed.
Expand Down