websocketpp  0.3.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) 2013, 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 <iostream>
41 #include <string>
42 
43 namespace websocketpp {
44 namespace transport {
45 namespace asio {
48 namespace tls_socket {
49 
51 typedef lib::function<void(connection_hdl,boost::asio::ssl::stream<
52  boost::asio::ip::tcp::socket>&)> socket_init_handler;
54 typedef lib::function<lib::shared_ptr<boost::asio::ssl::context>(connection_hdl)>
56 
58 
62 class connection : public lib::enable_shared_from_this<connection> {
63 public:
65  typedef connection type;
67  typedef lib::shared_ptr<type> ptr;
68 
70  typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_type;
72  typedef lib::shared_ptr<socket_type> socket_ptr;
74  typedef boost::asio::io_service* io_service_ptr;
76  typedef lib::shared_ptr<boost::asio::io_service::strand> strand_ptr;
78  typedef lib::shared_ptr<boost::asio::ssl::context> context_ptr;
79 
80  typedef boost::system::error_code boost_error;
81 
82  explicit connection() {
83  //std::cout << "transport::asio::tls_socket::connection constructor"
84  // << std::endl;
85  }
86 
88  ptr get_shared() {
89  return shared_from_this();
90  }
91 
93 
96  bool is_secure() const {
97  return true;
98  }
99 
101 
104  socket_type::lowest_layer_type& get_raw_socket() {
105  return m_socket->lowest_layer();
106  }
107 
109 
112  socket_type::next_layer_type& get_next_layer() {
113  return m_socket->next_layer();
114  }
115 
117 
120  socket_type& get_socket() {
121  return *m_socket;
122  }
123 
125 
132  void set_socket_init_handler(socket_init_handler h) {
133  m_socket_init_handler = h;
134  }
135 
137 
146  m_tls_init_handler = h;
147  }
148 
150 
159  std::string get_remote_endpoint(lib::error_code &ec) const {
160  std::stringstream s;
161 
162  boost::system::error_code bec;
163  boost::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(bec);
164 
165  if (bec) {
167  s << "Error getting remote endpoint: " << bec
168  << " (" << bec.message() << ")";
169  return s.str();
170  } else {
171  ec = lib::error_code();
172  s << ep;
173  return s.str();
174  }
175  }
176 protected:
178 
186  lib::error_code init_asio (io_service_ptr service, strand_ptr strand,
187  bool is_server)
188  {
189  if (!m_tls_init_handler) {
190  return socket::make_error_code(socket::error::missing_tls_init_handler);
191  }
192  m_context = m_tls_init_handler(m_hdl);
193 
194  if (!m_context) {
195  return socket::make_error_code(socket::error::invalid_tls_context);
196  }
197  m_socket.reset(new socket_type(*service,*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:78
TLS enabled Boost ASIO connection socket component.
Definition: tls.hpp:62
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
Definition: tls.hpp:120
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: tls.hpp:67
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:96
connection type
Type of this connection socket component.
Definition: tls.hpp:65
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:76
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:112
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:159
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:72
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:88
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:55
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:186
socket_type::lowest_layer_type & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: tls.hpp:104
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:145
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: tls.hpp:132
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > socket_type
Type of the ASIO socket being used.
Definition: tls.hpp:70
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:52
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: tls.hpp:74
void cancel_socket()
Cancel all async operations on this socket.
Definition: tls.hpp:283