Skip to content
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

Add TLS extension SNI for boost asio based http_client #39

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 ([email protected])
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_host_name(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: Can we have the variable name to reflect the type it is representing, bool here?
For eg, something like m_sni_enabled?
m_tlsext_host_name gives an impression that it is storing the host name to be set.

#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 server name indication (SNI) property.
/// </summary>
/// <returns>True if TLS server name indication is enabled, false otherwise.</returns>
bool tlsext_host_name() const
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, since this is returning a boolean value, something like is_sni_enabled() would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I was unsure whether you want to deviate from the current naming scheme in the API which does not phrase "bool-getters" as an "is_something..." question, will make the adjustments shortly.

{
return m_tlsext_host_name;
}

/// <summary>
/// Sets the TLS server name indication (SNI) property.
/// </summary>
/// <param name="tlsext_host_name">False to disable the TLS (ClientHello) extension for server name indication, true otherwise.</param>
/// <remarks>Note: This setting is required in most virtual hosting scenarios.</remarks>
void set_tlsext_host_name(bool tlsext_host_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, this is not setting the host name but enabling the set_tlsext_host_name feature.

{
m_tlsext_host_name = tlsext_host_name;
}
#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_host_name;
#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.tlsext_host_name())
{
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