WebSocket++  0.8.2
C++ websocket client/server library
tls.hpp
1 /*
2  * Copyright (c) 2015, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
29 #define WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
30 
31 #include <websocketpp/transport/asio/security/base.hpp>
32 
33 #include <websocketpp/uri.hpp>
34 
35 #include <websocketpp/common/asio_ssl.hpp>
36 #include <websocketpp/common/asio.hpp>
37 #include <websocketpp/common/connection_hdl.hpp>
38 #include <websocketpp/common/functional.hpp>
39 #include <websocketpp/common/memory.hpp>
40 
41 #include <sstream>
42 #include <string>
43 
44 namespace websocketpp {
45 namespace transport {
46 namespace asio {
49 namespace tls_socket {
50 
52 typedef lib::function<void(connection_hdl,lib::asio::ssl::stream<
53  lib::asio::ip::tcp::socket>&)> socket_init_handler;
55 typedef lib::function<lib::shared_ptr<lib::asio::ssl::context>(connection_hdl)>
57 
59 
63 class connection : public lib::enable_shared_from_this<connection> {
64 public:
66  typedef connection type;
68  typedef lib::shared_ptr<type> ptr;
69 
71  typedef lib::asio::ssl::stream<lib::asio::ip::tcp::socket> socket_type;
73  typedef lib::shared_ptr<socket_type> socket_ptr;
75  typedef lib::asio::io_service * io_service_ptr;
77  typedef lib::shared_ptr<lib::asio::io_service::strand> strand_ptr;
79  typedef lib::shared_ptr<lib::asio::ssl::context> context_ptr;
80 
81  explicit connection() {
82  //std::cout << "transport::asio::tls_socket::connection constructor"
83  // << std::endl;
84  }
85 
88  return shared_from_this();
89  }
90 
92 
95  bool is_secure() const {
96  return true;
97  }
98 
100 
103  socket_type::lowest_layer_type & get_raw_socket() {
104  return m_socket->lowest_layer();
105  }
106 
108 
111  socket_type::next_layer_type & get_next_layer() {
112  return m_socket->next_layer();
113  }
114 
116 
120  return *m_socket;
121  }
122 
124 
132  m_socket_init_handler = h;
133  }
134 
136 
145  m_tls_init_handler = h;
146  }
147 
149 
158  std::string get_remote_endpoint(lib::error_code & ec) const {
159  std::stringstream s;
160 
161  lib::asio::error_code aec;
162  lib::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(aec);
163 
164  if (aec) {
166  s << "Error getting remote endpoint: " << aec
167  << " (" << aec.message() << ")";
168  return s.str();
169  } else {
170  ec = lib::error_code();
171  s << ep;
172  return s.str();
173  }
174  }
175 protected:
177 
185  lib::error_code init_asio (io_service_ptr service, strand_ptr strand,
186  bool is_server)
187  {
188  if (!m_tls_init_handler) {
189  return socket::make_error_code(socket::error::missing_tls_init_handler);
190  }
191  m_context = m_tls_init_handler(m_hdl);
192 
193  if (!m_context) {
194  return socket::make_error_code(socket::error::invalid_tls_context);
195  }
196  m_socket.reset(new socket_type(*service, *m_context));
197 
198  if (m_socket_init_handler) {
199  m_socket_init_handler(m_hdl, get_socket());
200  }
201 
202  m_io_service = service;
203  m_strand = strand;
204  m_is_server = is_server;
205 
206  return lib::error_code();
207  }
208 
210 
221  void set_uri(uri_ptr u) {
222  m_uri = u;
223  }
224 
226 
234  void pre_init(init_handler callback) {
235  // TODO: is this the best way to check whether this function is
236  // available in the version of OpenSSL being used?
237  // TODO: consider case where host is an IP address
238 #if OPENSSL_VERSION_NUMBER >= 0x90812f
239  if (!m_is_server) {
240  // For clients on systems with a suitable OpenSSL version, set the
241  // TLS SNI hostname header so connecting to TLS servers using SNI
242  // will work.
243  long res = SSL_set_tlsext_host_name(
244  get_socket().native_handle(), m_uri->get_host().c_str());
245  if (!(1 == res)) {
246  callback(socket::make_error_code(socket::error::tls_failed_sni_hostname));
247  }
248  }
249 #endif
250 
251  callback(lib::error_code());
252  }
253 
255 
262  void post_init(init_handler callback) {
263  m_ec = socket::make_error_code(socket::error::tls_handshake_timeout);
264 
265  // TLS handshake
266  if (m_strand) {
267  m_socket->async_handshake(
268  get_handshake_type(),
269  m_strand->wrap(lib::bind(
270  &type::handle_init, get_shared(),
271  callback,
272  lib::placeholders::_1
273  ))
274  );
275  } else {
276  m_socket->async_handshake(
277  get_handshake_type(),
278  lib::bind(
279  &type::handle_init, get_shared(),
280  callback,
281  lib::placeholders::_1
282  )
283  );
284  }
285  }
286 
288 
295  m_hdl = hdl;
296  }
297 
298  void handle_init(init_handler callback,lib::asio::error_code const & ec) {
299  if (ec) {
300  m_ec = socket::make_error_code(socket::error::tls_handshake_failed);
301  } else {
302  m_ec = lib::error_code();
303  }
304 
305  callback(m_ec);
306  }
307 
308  lib::error_code get_ec() const {
309  return m_ec;
310  }
311 
313 
321  lib::asio::error_code cancel_socket() {
322  lib::asio::error_code ec;
323  get_raw_socket().cancel(ec);
324  return ec;
325  }
326 
327  void async_shutdown(socket::shutdown_handler callback) {
328  if (m_strand) {
329  m_socket->async_shutdown(m_strand->wrap(callback));
330  } else {
331  m_socket->async_shutdown(callback);
332  }
333  }
334 
335 public:
337 
355  template <typename ErrorCodeType>
356  static
357  lib::error_code translate_ec(ErrorCodeType ec) {
358  if (ec.category() == lib::asio::error::get_ssl_category()) {
359  // We know it is a TLS related error, but otherwise don't know more.
360  // Pass through as TLS generic.
361  return make_error_code(transport::error::tls_error);
362  } else {
363  // We don't know any more information about this error so pass
364  // through
365  return make_error_code(transport::error::pass_through);
366  }
367  }
368 
369  static
372  lib::error_code translate_ec(lib::error_code ec) {
373  return ec;
374  }
375 private:
376  socket_type::handshake_type get_handshake_type() {
377  if (m_is_server) {
378  return lib::asio::ssl::stream_base::server;
379  } else {
380  return lib::asio::ssl::stream_base::client;
381  }
382  }
383 
384  io_service_ptr m_io_service;
385  strand_ptr m_strand;
386  context_ptr m_context;
387  socket_ptr m_socket;
388  uri_ptr m_uri;
389  bool m_is_server;
390 
391  lib::error_code m_ec;
392 
393  connection_hdl m_hdl;
394  socket_init_handler m_socket_init_handler;
395  tls_init_handler m_tls_init_handler;
396 };
397 
399 
403 class endpoint {
404 public:
406  typedef endpoint type;
407 
413 
414  explicit endpoint() {}
415 
417 
420  bool is_secure() const {
421  return true;
422  }
423 
425 
433  m_socket_init_handler = h;
434  }
435 
437 
446  m_tls_init_handler = h;
447  }
448 protected:
450 
458  lib::error_code init(socket_con_ptr scon) {
459  scon->set_socket_init_handler(m_socket_init_handler);
460  scon->set_tls_init_handler(m_tls_init_handler);
461  return lib::error_code();
462  }
463 
464 private:
465  socket_init_handler m_socket_init_handler;
466  tls_init_handler m_tls_init_handler;
467 };
468 
469 } // namespace tls_socket
470 } // namespace asio
471 } // namespace transport
472 } // namespace websocketpp
473 
474 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
websocketpp::transport::asio::socket::error::invalid_tls_context
@ invalid_tls_context
Definition: base.hpp:90
websocketpp::transport::asio::error::make_error_code
lib::error_code make_error_code(error::value e)
Create an error code with the given value and the asio transport category.
Definition: base.hpp:217
websocketpp::transport::asio::error::pass_through
@ pass_through
there was an error in the underlying transport library
Definition: base.hpp:171
websocketpp::transport::asio::tls_socket::connection::socket_type
lib::asio::ssl::stream< lib::asio::ip::tcp::socket > socket_type
Type of the ASIO socket being used.
Definition: tls.hpp:71
websocketpp::uri_ptr
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:352
websocketpp::transport::asio::tls_socket::connection
TLS enabled Asio connection socket component.
Definition: tls.hpp:63
websocketpp::transport::asio::tls_socket::connection::is_secure
bool is_secure() const
Check whether or not this connection is secure.
Definition: tls.hpp:95
websocketpp::transport::asio::tls_socket::connection::cancel_socket
lib::asio::error_code cancel_socket()
Cancel all async operations on this socket.
Definition: tls.hpp:321
websocketpp::transport::asio::tls_socket::endpoint
TLS enabled Asio endpoint socket component.
Definition: tls.hpp:403
websocketpp::transport::asio::tls_socket::connection::get_shared
ptr get_shared()
Get a shared pointer to this component.
Definition: tls.hpp:87
websocketpp::transport::asio::tls_socket::tls_init_handler
lib::function< lib::shared_ptr< lib::asio::ssl::context >connection_hdl)> tls_init_handler
The signature of the tls_init_handler for this socket policy.
Definition: tls.hpp:56
websocketpp::transport::asio::tls_socket::connection::set_handle
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: tls.hpp:294
websocketpp::transport::asio::tls_socket::endpoint::socket_con_type
connection socket_con_type
The type of the corresponding connection socket component.
Definition: tls.hpp:409
websocketpp::transport::asio::tls_socket::connection::ptr
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: tls.hpp:68
websocketpp::transport::error::pass_through
@ pass_through
underlying transport pass through
Definition: connection.hpp:153
websocketpp::transport::asio::tls_socket::endpoint::init
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: tls.hpp:458
websocketpp::transport::asio::socket::error::tls_failed_sni_hostname
@ tls_failed_sni_hostname
Failed to set TLS SNI hostname.
Definition: base.hpp:105
websocketpp::transport::asio::tls_socket::connection::get_next_layer
socket_type::next_layer_type & get_next_layer()
Retrieve a pointer to the layer below the ssl stream.
Definition: tls.hpp:111
websocketpp::transport::asio::tls_socket::socket_init_handler
lib::function< void(connection_hdl, lib::asio::ssl::stream< lib::asio::ip::tcp::socket > &)> socket_init_handler
The signature of the socket_init_handler for this socket policy.
Definition: tls.hpp:53
websocketpp::transport::asio::tls_socket::connection::io_service_ptr
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: tls.hpp:75
websocketpp
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
websocketpp::transport::asio::tls_socket::endpoint::set_tls_init_handler
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:445
websocketpp::transport::asio::tls_socket::endpoint::set_socket_init_handler
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: tls.hpp:432
websocketpp::connection_hdl
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
Definition: connection_hdl.hpp:48
websocketpp::transport::asio::tls_socket::connection::get_remote_endpoint
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: tls.hpp:158
websocketpp::transport::asio::tls_socket::connection::init_asio
lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
Perform one time initializations.
Definition: tls.hpp:185
websocketpp::transport::asio::tls_socket::connection::get_socket
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
Definition: tls.hpp:119
websocketpp::transport::asio::tls_socket::connection::set_socket_init_handler
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: tls.hpp:131
websocketpp::transport::asio::tls_socket::endpoint::type
endpoint type
The type of this endpoint socket component.
Definition: tls.hpp:406
websocketpp::transport::asio::tls_socket::connection::set_uri
void set_uri(uri_ptr u)
Set hostname hook.
Definition: tls.hpp:221
websocketpp::transport::asio::tls_socket::connection::pre_init
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: tls.hpp:234
websocketpp::transport::asio::tls_socket::connection::post_init
void post_init(init_handler callback)
Post-initialize security policy.
Definition: tls.hpp:262
websocketpp::transport::asio::tls_socket::connection::type
connection type
Type of this connection socket component.
Definition: tls.hpp:66
websocketpp::transport::asio::tls_socket::connection::strand_ptr
lib::shared_ptr< lib::asio::io_service::strand > strand_ptr
Type of a pointer to the ASIO io_service strand being used.
Definition: tls.hpp:77
websocketpp::transport::asio::socket::error::missing_tls_init_handler
@ missing_tls_init_handler
Required tls_init handler not present.
Definition: base.hpp:99
websocketpp::transport::init_handler
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
Definition: connection.hpp:117
websocketpp::transport::asio::tls_socket::connection::translate_ec
static lib::error_code translate_ec(ErrorCodeType ec)
Translate any security policy specific information about an error code.
Definition: tls.hpp:357
websocketpp::transport::asio::tls_socket::connection::socket_ptr
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the ASIO socket being used.
Definition: tls.hpp:73
websocketpp::transport::asio::tls_socket::endpoint::socket_con_ptr
socket_con_type::ptr socket_con_ptr
Definition: tls.hpp:412
websocketpp::transport::asio::tls_socket::endpoint::is_secure
bool is_secure() const
Checks whether the endpoint creates secure connections.
Definition: tls.hpp:420
websocketpp::transport::asio::tls_socket::connection::translate_ec
static lib::error_code translate_ec(lib::error_code ec)
Definition: tls.hpp:372
websocketpp::transport::error::tls_error
@ tls_error
Other TLS error.
Definition: connection.hpp:180
websocketpp::transport::asio::tls_socket::connection::set_tls_init_handler
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:144
websocketpp::transport::asio::socket::error::tls_handshake_timeout
@ tls_handshake_timeout
TLS Handshake Timeout.
Definition: base.hpp:93
websocketpp::transport::asio::tls_socket::connection::get_raw_socket
socket_type::lowest_layer_type & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: tls.hpp:103
websocketpp::transport::asio::socket::error::tls_handshake_failed
@ tls_handshake_failed
TLS Handshake Failed.
Definition: base.hpp:102
websocketpp::transport::asio::tls_socket::connection::context_ptr
lib::shared_ptr< lib::asio::ssl::context > context_ptr
Type of a shared pointer to the ASIO TLS context being used.
Definition: tls.hpp:79