NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
tls.hpp
Go to the documentation of this file.
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 
32 
33 #include <websocketpp/uri.hpp>
34 
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<
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 
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  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) {
190  }
191  m_context = m_tls_init_handler(m_hdl);
192 
193  if (!m_context) {
195  }
196  m_socket = lib::make_shared<socket_type>(
197  _WEBSOCKETPP_REF(*service),*m_context);
198 
199  if (m_socket_init_handler) {
200  m_socket_init_handler(m_hdl, get_socket());
201  }
202 
203  m_io_service = service;
204  m_strand = strand;
205  m_is_server = is_server;
206 
207  return lib::error_code();
208  }
209 
211 
222  void set_uri(uri_ptr u) {
223  m_uri = u;
224  }
225 
227 
235  void pre_init(init_handler callback) {
236  // TODO: is this the best way to check whether this function is
237  // available in the version of OpenSSL being used?
238  // TODO: consider case where host is an IP address
239 #if OPENSSL_VERSION_NUMBER >= 0x90812f
240  if (!m_is_server) {
241  // For clients on systems with a suitable OpenSSL version, set the
242  // TLS SNI hostname header so connecting to TLS servers using SNI
243  // will work.
244  long res = SSL_set_tlsext_host_name(
245  get_socket().native_handle(), m_uri->get_host().c_str());
246  if (!(1 == res)) {
248  }
249  }
250 #endif
251 
252  callback(lib::error_code());
253  }
254 
256 
263  void post_init(init_handler callback) {
265 
266  // TLS handshake
267  if (m_strand) {
268  m_socket->async_handshake(
269  get_handshake_type(),
270  m_strand->wrap(lib::bind(
272  callback,
273  lib::placeholders::_1
274  ))
275  );
276  } else {
277  m_socket->async_handshake(
278  get_handshake_type(),
279  lib::bind(
281  callback,
282  lib::placeholders::_1
283  )
284  );
285  }
286  }
287 
289 
296  m_hdl = hdl;
297  }
298 
299  void handle_init(init_handler callback,lib::asio::error_code const & ec) {
300  if (ec) {
302  } else {
303  m_ec = lib::error_code();
304  }
305 
306  callback(m_ec);
307  }
308 
309  lib::error_code get_ec() const {
310  return m_ec;
311  }
312 
314 
322  lib::asio::error_code cancel_socket() {
323  lib::asio::error_code ec;
324  get_raw_socket().cancel(ec);
325  return ec;
326  }
327 
329  if (m_strand) {
330  m_socket->async_shutdown(m_strand->wrap(callback));
331  } else {
332  m_socket->async_shutdown(callback);
333  }
334  }
335 
336 public:
338 
356  template <typename ErrorCodeType>
357  static
358  lib::error_code translate_ec(ErrorCodeType ec) {
359  if (ec.category() == lib::asio::error::get_ssl_category()) {
360  // We know it is a TLS related error, but otherwise don't know more.
361  // Pass through as TLS generic.
363  } else {
364  // We don't know any more information about this error so pass
365  // through
367  }
368  }
369 
370  static
373  lib::error_code translate_ec(lib::error_code ec) {
374  return ec;
375  }
376 private:
377  socket_type::handshake_type get_handshake_type() {
378  if (m_is_server) {
380  } else {
382  }
383  }
384 
385  io_service_ptr m_io_service;
386  strand_ptr m_strand;
387  context_ptr m_context;
388  socket_ptr m_socket;
389  uri_ptr m_uri;
390  bool m_is_server;
391 
392  lib::error_code m_ec;
393 
394  connection_hdl m_hdl;
395  socket_init_handler m_socket_init_handler;
396  tls_init_handler m_tls_init_handler;
397 };
398 
400 
404 class endpoint {
405 public:
407  typedef endpoint type;
408 
414 
415  explicit endpoint() {}
416 
418 
421  bool is_secure() const {
422  return true;
423  }
424 
426 
433  void set_socket_init_handler(socket_init_handler h) {
434  m_socket_init_handler = h;
435  }
436 
438 
447  m_tls_init_handler = h;
448  }
449 protected:
451 
459  lib::error_code init(socket_con_ptr scon) {
460  scon->set_socket_init_handler(m_socket_init_handler);
461  scon->set_tls_init_handler(m_tls_init_handler);
462  return lib::error_code();
463  }
464 
465 private:
466  socket_init_handler m_socket_init_handler;
467  tls_init_handler m_tls_init_handler;
468 };
469 
470 } // namespace tls_socket
471 } // namespace asio
472 } // namespace transport
473 } // namespace websocketpp
474 
475 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
void async_shutdown(socket::shutdown_handler callback)
Definition: tls.hpp:328
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
TLS enabled Asio connection socket component.
Definition: tls.hpp:63
websocketpp::server< websocketpp::config::asio > server
socket_type & get_socket()
Retrieve a pointer to the wrapped socket.
Definition: tls.hpp:119
static lib::error_code translate_ec(ErrorCodeType ec)
Translate any security policy specific information about an error code.
Definition: tls.hpp:358
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:217
lib::asio::error_code cancel_socket()
Cancel all async operations on this socket.
Definition: tls.hpp:322
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: tls.hpp:235
#define _WEBSOCKETPP_REF(x)
Definition: functional.hpp:94
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:295
bool is_secure() const
Check whether or not this connection is secure.
Definition: tls.hpp:95
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
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
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: tls.hpp:459
underlying transport pass through
Definition: connection.hpp:153
void handle_init(init_handler callback, lib::asio::error_code const &ec)
Definition: tls.hpp:299
socket_type::next_layer_type & get_next_layer()
Retrieve a pointer to the layer below the ssl stream.
Definition: tls.hpp:111
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: tls.hpp:158
connection socket_con_type
The type of the corresponding connection socket component.
Definition: tls.hpp:410
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:446
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
void set_uri(uri_ptr u)
Set hostname hook.
Definition: tls.hpp:222
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:433
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: tls.hpp:75
The application was prompted to provide a TLS context and it was empty or otherwise invalid...
Definition: base.hpp:90
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:171
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: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:407
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
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:144
lib::function< void(lib::asio::error_code const &)> shutdown_handler
Definition: base.hpp:67
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:352
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: tls.hpp:131
lib::asio::ssl::stream< lib::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:263
TLS enabled Asio endpoint socket component.
Definition: tls.hpp:404
static lib::error_code translate_ec(lib::error_code ec)
Overload of translate_ec to catch cases where lib::error_code is the same type as lib::asio::error_co...
Definition: tls.hpp:373
bool is_secure() const
Checks whether the endpoint creates secure connections.
Definition: tls.hpp:421
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
socket_con_type::ptr socket_con_ptr
The type of a shared pointer to the corresponding connection socket component.
Definition: tls.hpp:413
websocketpp::client< websocketpp::config::asio_client > client
====== WARNING ======== This example is presently used as a scratch space.
lib::error_code make_error_code(error::value e)
Definition: base.hpp:147
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