websocketpp  0.5.1
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 
33 #include <websocketpp/common/connection_hdl.hpp>
34 #include <websocketpp/common/functional.hpp>
35 #include <websocketpp/common/memory.hpp>
36 
37 #include <boost/asio.hpp>
38 #include <boost/asio/ssl.hpp>
39 #include <boost/system/error_code.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,boost::asio::ssl::stream<
53  boost::asio::ip::tcp::socket>&)> socket_init_handler;
55 typedef lib::function<lib::shared_ptr<boost::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 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_type;
73  typedef lib::shared_ptr<socket_type> socket_ptr;
75  typedef boost::asio::io_service* io_service_ptr;
77  typedef lib::shared_ptr<boost::asio::io_service::strand> strand_ptr;
79  typedef lib::shared_ptr<boost::asio::ssl::context> context_ptr;
80 
81  typedef boost::system::error_code boost_error;
82 
83  explicit connection() {
84  //std::cout << "transport::asio::tls_socket::connection constructor"
85  // << std::endl;
86  }
87 
89  ptr get_shared() {
90  return shared_from_this();
91  }
92 
94 
97  bool is_secure() const {
98  return true;
99  }
100 
102 
105  socket_type::lowest_layer_type& get_raw_socket() {
106  return m_socket->lowest_layer();
107  }
108 
110 
113  socket_type::next_layer_type& get_next_layer() {
114  return m_socket->next_layer();
115  }
116 
118 
121  socket_type& get_socket() {
122  return *m_socket;
123  }
124 
126 
133  void set_socket_init_handler(socket_init_handler h) {
134  m_socket_init_handler = h;
135  }
136 
138 
147  m_tls_init_handler = h;
148  }
149 
151 
160  std::string get_remote_endpoint(lib::error_code &ec) const {
161  std::stringstream s;
162 
163  boost::system::error_code bec;
164  boost::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(bec);
165 
166  if (bec) {
168  s << "Error getting remote endpoint: " << bec
169  << " (" << bec.message() << ")";
170  return s.str();
171  } else {
172  ec = lib::error_code();
173  s << ep;
174  return s.str();
175  }
176  }
177 protected:
179 
187  lib::error_code init_asio (io_service_ptr service, strand_ptr strand,
188  bool is_server)
189  {
190  if (!m_tls_init_handler) {
191  return socket::make_error_code(socket::error::missing_tls_init_handler);
192  }
193  m_context = m_tls_init_handler(m_hdl);
194 
195  if (!m_context) {
196  return socket::make_error_code(socket::error::invalid_tls_context);
197  }
198  m_socket = lib::make_shared<socket_type>(
199  _WEBSOCKETPP_REF(*service),lib::ref(*m_context));
200 
201  m_io_service = service;
202  m_strand = strand;
203  m_is_server = is_server;
204 
205  return lib::error_code();
206  }
207 
209 
217  void pre_init(init_handler callback) {
218  if (m_socket_init_handler) {
219  m_socket_init_handler(m_hdl,get_socket());
220  }
221 
222  callback(lib::error_code());
223  }
224 
226 
233  void post_init(init_handler callback) {
234  m_ec = socket::make_error_code(socket::error::tls_handshake_timeout);
235 
236  // TLS handshake
237  if (m_strand) {
238  m_socket->async_handshake(
239  get_handshake_type(),
240  m_strand->wrap(lib::bind(
241  &type::handle_init, get_shared(),
242  callback,
243  lib::placeholders::_1
244  ))
245  );
246  } else {
247  m_socket->async_handshake(
248  get_handshake_type(),
249  lib::bind(
250  &type::handle_init, get_shared(),
251  callback,
252  lib::placeholders::_1
253  )
254  );
255  }
256  }
257 
259 
266  m_hdl = hdl;
267  }
268 
269  void handle_init(init_handler callback,boost::system::error_code const & ec)
270  {
271  if (ec) {
272  m_ec = socket::make_error_code(socket::error::tls_handshake_failed);
273  } else {
274  m_ec = lib::error_code();
275  }
276 
277  callback(m_ec);
278  }
279 
280  lib::error_code get_ec() const {
281  return m_ec;
282  }
283 
285  void cancel_socket() {
286  get_raw_socket().cancel();
287  }
288 
289  void async_shutdown(socket_shutdown_handler callback) {
290  m_socket->async_shutdown(callback);
291  }
292 
294 
308  lib::error_code translate_ec(boost::system::error_code ec) {
309  if (ec.category() == boost::asio::error::get_ssl_category()) {
310  if (ERR_GET_REASON(ec.value()) == SSL_R_SHORT_READ) {
311  return make_error_code(transport::error::tls_short_read);
312  } else {
313  // We know it is a TLS related error, but otherwise don't know
314  // more. Pass through as TLS generic.
315  return make_error_code(transport::error::tls_error);
316  }
317  } else {
318  // We don't know any more information about this error so pass
319  // through
320  return make_error_code(transport::error::pass_through);
321  }
322  }
323 private:
324  socket_type::handshake_type get_handshake_type() {
325  if (m_is_server) {
326  return boost::asio::ssl::stream_base::server;
327  } else {
328  return boost::asio::ssl::stream_base::client;
329  }
330  }
331 
332  io_service_ptr m_io_service;
333  strand_ptr m_strand;
334  context_ptr m_context;
335  socket_ptr m_socket;
336  bool m_is_server;
337 
338  lib::error_code m_ec;
339 
340  connection_hdl m_hdl;
341  socket_init_handler m_socket_init_handler;
342  tls_init_handler m_tls_init_handler;
343 };
344 
346 
350 class endpoint {
351 public:
353  typedef endpoint type;
354 
360 
361  explicit endpoint() {}
362 
364 
367  bool is_secure() const {
368  return true;
369  }
370 
372 
379  void set_socket_init_handler(socket_init_handler h) {
380  m_socket_init_handler = h;
381  }
382 
384 
393  m_tls_init_handler = h;
394  }
395 protected:
397 
405  lib::error_code init(socket_con_ptr scon) {
406  scon->set_socket_init_handler(m_socket_init_handler);
407  scon->set_tls_init_handler(m_tls_init_handler);
408  return lib::error_code();
409  }
410 
411 private:
412  socket_init_handler m_socket_init_handler;
413  tls_init_handler m_tls_init_handler;
414 };
415 
416 } // namespace tls_socket
417 } // namespace asio
418 } // namespace transport
419 } // namespace websocketpp
420 
421 #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:79
TLS enabled Boost ASIO connection socket component.
Definition: tls.hpp:63
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
Definition: tls.hpp:121
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: tls.hpp:68
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:367
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: tls.hpp:217
bool is_secure() const
Check whether or not this connection is secure.
Definition: tls.hpp:97
connection type
Type of this connection socket component.
Definition: tls.hpp:66
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: tls.hpp:265
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:77
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: tls.hpp:405
underlying transport pass through
Definition: connection.hpp:153
socket_type::next_layer_type & get_next_layer()
Retrieve a pointer to the layer below the ssl stream.
Definition: tls.hpp:113
connection socket_con_type
The type of the corresponding connection socket component.
Definition: tls.hpp:356
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: tls.hpp:160
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:392
lib::error_code translate_ec(boost::system::error_code ec)
Translate any security policy specific information about an error code.
Definition: tls.hpp:308
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the ASIO socket being used.
Definition: tls.hpp:73
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: tls.hpp:379
ptr get_shared()
Get a shared pointer to this component.
Definition: tls.hpp:89
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:56
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:117
lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
Perform one time initializations.
Definition: tls.hpp:187
socket_type::lowest_layer_type & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: tls.hpp:105
endpoint type
The type of this endpoint socket component.
Definition: tls.hpp:353
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:146
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: tls.hpp:133
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > socket_type
Type of the ASIO socket being used.
Definition: tls.hpp:71
void post_init(init_handler callback)
Post-initialize security policy.
Definition: tls.hpp:233
TLS enabled Boost ASIO endpoint socket component.
Definition: tls.hpp:350
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:53
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: tls.hpp:75
void cancel_socket()
Cancel all async operations on this socket.
Definition: tls.hpp:285