websocketpp  0.4.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
tls.hpp
1 /*
2  * Copyright (c) 2014, 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 #include <websocketpp/common/connection_hdl.hpp>
33 #include <websocketpp/common/functional.hpp>
34 #include <websocketpp/common/memory.hpp>
35 
36 #include <boost/asio.hpp>
37 #include <boost/asio/ssl.hpp>
38 #include <boost/system/error_code.hpp>
39 
40 #include <string>
41 
42 namespace websocketpp {
43 namespace transport {
44 namespace asio {
47 namespace tls_socket {
48 
50 typedef lib::function<void(connection_hdl,boost::asio::ssl::stream<
51  boost::asio::ip::tcp::socket>&)> socket_init_handler;
53 typedef lib::function<lib::shared_ptr<boost::asio::ssl::context>(connection_hdl)>
55 
57 
61 class connection : public lib::enable_shared_from_this<connection> {
62 public:
64  typedef connection type;
66  typedef lib::shared_ptr<type> ptr;
67 
69  typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_type;
71  typedef lib::shared_ptr<socket_type> socket_ptr;
73  typedef boost::asio::io_service* io_service_ptr;
75  typedef lib::shared_ptr<boost::asio::io_service::strand> strand_ptr;
77  typedef lib::shared_ptr<boost::asio::ssl::context> context_ptr;
78 
79  typedef boost::system::error_code boost_error;
80 
81  explicit connection() {
82  //std::cout << "transport::asio::tls_socket::connection constructor"
83  // << std::endl;
84  }
85 
87  ptr get_shared() {
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 
119  socket_type& get_socket() {
120  return *m_socket;
121  }
122 
124 
131  void set_socket_init_handler(socket_init_handler h) {
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  boost::system::error_code bec;
162  boost::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(bec);
163 
164  if (bec) {
166  s << "Error getting remote endpoint: " << bec
167  << " (" << bec.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 = lib::make_shared<socket_type>(
197  _WEBSOCKETPP_REF(*service),lib::ref(*m_context));
198 
199  m_io_service = service;
200  m_strand = strand;
201  m_is_server = is_server;
202 
203  return lib::error_code();
204  }
205 
207 
215  void pre_init(init_handler callback) {
216  if (m_socket_init_handler) {
217  m_socket_init_handler(m_hdl,get_socket());
218  }
219 
220  callback(lib::error_code());
221  }
222 
224 
231  void post_init(init_handler callback) {
232  m_ec = socket::make_error_code(socket::error::tls_handshake_timeout);
233 
234  // TLS handshake
235  if (m_strand) {
236  m_socket->async_handshake(
237  get_handshake_type(),
238  m_strand->wrap(lib::bind(
239  &type::handle_init, get_shared(),
240  callback,
241  lib::placeholders::_1
242  ))
243  );
244  } else {
245  m_socket->async_handshake(
246  get_handshake_type(),
247  lib::bind(
248  &type::handle_init, get_shared(),
249  callback,
250  lib::placeholders::_1
251  )
252  );
253  }
254  }
255 
257 
264  m_hdl = hdl;
265  }
266 
267  void handle_init(init_handler callback,boost::system::error_code const & ec)
268  {
269  if (ec) {
270  m_ec = socket::make_error_code(socket::error::tls_handshake_failed);
271  } else {
272  m_ec = lib::error_code();
273  }
274 
275  callback(m_ec);
276  }
277 
278  lib::error_code get_ec() const {
279  return m_ec;
280  }
281 
283  void cancel_socket() {
284  get_raw_socket().cancel();
285  }
286 
287  void async_shutdown(socket_shutdown_handler callback) {
288  m_socket->async_shutdown(callback);
289  }
290 
292 
306  lib::error_code translate_ec(boost::system::error_code ec) {
307  if (ec.category() == boost::asio::error::get_ssl_category()) {
308  if (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ) {
309  return make_error_code(transport::error::tls_short_read);
310  } else {
311  // We know it is a TLS related error, but otherwise don't know
312  // more. Pass through as TLS generic.
313  return make_error_code(transport::error::tls_error);
314  }
315  } else {
316  // We don't know any more information about this error so pass
317  // through
318  return make_error_code(transport::error::pass_through);
319  }
320  }
321 private:
322  socket_type::handshake_type get_handshake_type() {
323  if (m_is_server) {
324  return boost::asio::ssl::stream_base::server;
325  } else {
326  return boost::asio::ssl::stream_base::client;
327  }
328  }
329 
330  io_service_ptr m_io_service;
331  strand_ptr m_strand;
332  context_ptr m_context;
333  socket_ptr m_socket;
334  bool m_is_server;
335 
336  lib::error_code m_ec;
337 
338  connection_hdl m_hdl;
339  socket_init_handler m_socket_init_handler;
340  tls_init_handler m_tls_init_handler;
341 };
342 
344 
348 class endpoint {
349 public:
351  typedef endpoint type;
352 
358 
359  explicit endpoint() {}
360 
362 
365  bool is_secure() const {
366  return true;
367  }
368 
370 
377  void set_socket_init_handler(socket_init_handler h) {
378  m_socket_init_handler = h;
379  }
380 
382 
391  m_tls_init_handler = h;
392  }
393 protected:
395 
403  lib::error_code init(socket_con_ptr scon) {
404  scon->set_socket_init_handler(m_socket_init_handler);
405  scon->set_tls_init_handler(m_tls_init_handler);
406  return lib::error_code();
407  }
408 
409 private:
410  socket_init_handler m_socket_init_handler;
411  tls_init_handler m_tls_init_handler;
412 };
413 
414 } // namespace tls_socket
415 } // namespace asio
416 } // namespace transport
417 } // namespace websocketpp
418 
419 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
lib::shared_ptr< boost::asio::ssl::context > context_ptr
Type of a shared pointer to the ASIO TLS context being used.
Definition: tls.hpp:77
TLS enabled Boost ASIO connection socket component.
Definition: tls.hpp:61
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
Definition: tls.hpp:119
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: tls.hpp:66
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:236
bool is_secure() const
Checks whether the endpoint creates secure connections.
Definition: tls.hpp:365
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: tls.hpp:215
bool is_secure() const
Check whether or not this connection is secure.
Definition: tls.hpp:95
connection type
Type of this connection socket component.
Definition: tls.hpp:64
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: tls.hpp:263
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
lib::shared_ptr< boost::asio::io_service::strand > strand_ptr
Type of a pointer to the ASIO io_service strand being used.
Definition: tls.hpp:75
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: tls.hpp:403
underlying transport pass through
Definition: connection.hpp:152
socket_type::next_layer_type & get_next_layer()
Retrieve a pointer to the layer below the ssl stream.
Definition: tls.hpp:111
connection socket_con_type
The type of the corresponding connection socket component.
Definition: tls.hpp:354
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: tls.hpp:158
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:390
lib::error_code translate_ec(boost::system::error_code ec)
Translate any security policy specific information about an error code.
Definition: tls.hpp:306
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the ASIO socket being used.
Definition: tls.hpp:71
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: tls.hpp:377
ptr get_shared()
Get a shared pointer to this component.
Definition: tls.hpp:87
there was an error in the underlying transport library
Definition: base.hpp:190
lib::function< lib::shared_ptr< boost::asio::ssl::context >connection_hdl)> tls_init_handler
The signature of the tls_init_handler for this socket policy.
Definition: tls.hpp:54
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
Definition: connection.hpp:116
lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
Perform one time initializations.
Definition: tls.hpp:185
socket_type::lowest_layer_type & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: tls.hpp:103
endpoint type
The type of this endpoint socket component.
Definition: tls.hpp:351
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:144
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: tls.hpp:131
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > socket_type
Type of the ASIO socket being used.
Definition: tls.hpp:69
void post_init(init_handler callback)
Post-initialize security policy.
Definition: tls.hpp:231
TLS enabled Boost ASIO endpoint socket component.
Definition: tls.hpp:348
lib::function< void(connection_hdl, boost::asio::ssl::stream< boost::asio::ip::tcp::socket > &)> socket_init_handler
The signature of the socket_init_handler for this socket policy.
Definition: tls.hpp:51
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: tls.hpp:73
void cancel_socket()
Cancel all async operations on this socket.
Definition: tls.hpp:283