You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

515 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
  28. #define WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP
  29. #include <sstream>
  30. #include <string>
  31. #include <websocketpp/common/asio.hpp>
  32. #include <websocketpp/common/asio_ssl.hpp>
  33. #include <websocketpp/common/connection_hdl.hpp>
  34. #include <websocketpp/common/functional.hpp>
  35. #include <websocketpp/common/memory.hpp>
  36. #include <websocketpp/transport/asio/security/base.hpp>
  37. #include <websocketpp/uri.hpp>
  38. namespace websocketpp
  39. {
  40. namespace transport
  41. {
  42. namespace asio
  43. {
  44. /// A socket policy for the asio transport that implements a TLS encrypted
  45. /// socket by wrapping with an asio::ssl::stream
  46. namespace tls_socket
  47. {
  48. /// The signature of the socket_init_handler for this socket policy
  49. typedef lib::function<void(connection_hdl, lib::asio::ssl::stream<lib::asio::ip::tcp::socket> &)> socket_init_handler;
  50. /// The signature of the tls_init_handler for this socket policy
  51. typedef lib::function<lib::shared_ptr<lib::asio::ssl::context>(connection_hdl)> tls_init_handler;
  52. /// TLS enabled Asio connection socket component
  53. /**
  54. * transport::asio::tls_socket::connection implements a secure connection socket
  55. * component that uses Asio's ssl::stream to wrap an ip::tcp::socket.
  56. */
  57. class connection : public lib::enable_shared_from_this<connection>
  58. {
  59. public:
  60. /// Type of this connection socket component
  61. typedef connection type;
  62. /// Type of a shared pointer to this connection socket component
  63. typedef lib::shared_ptr<type> ptr;
  64. /// Type of the ASIO socket being used
  65. typedef lib::asio::ssl::stream<lib::asio::ip::tcp::socket> socket_type;
  66. /// Type of a shared pointer to the ASIO socket being used
  67. typedef lib::shared_ptr<socket_type> socket_ptr;
  68. /// Type of a pointer to the ASIO io_service being used
  69. typedef lib::asio::io_service * io_service_ptr;
  70. /// Type of a pointer to the ASIO io_service strand being used
  71. typedef lib::shared_ptr<lib::asio::io_service::strand> strand_ptr;
  72. /// Type of a shared pointer to the ASIO TLS context being used
  73. typedef lib::shared_ptr<lib::asio::ssl::context> context_ptr;
  74. explicit connection()
  75. {
  76. // std::cout << "transport::asio::tls_socket::connection constructor"
  77. // << std::endl;
  78. }
  79. /// Get a shared pointer to this component
  80. ptr get_shared()
  81. {
  82. return shared_from_this();
  83. }
  84. /// Check whether or not this connection is secure
  85. /**
  86. * @return Whether or not this connection is secure
  87. */
  88. bool is_secure() const
  89. {
  90. return true;
  91. }
  92. /// Retrieve a pointer to the underlying socket
  93. /**
  94. * This is used internally. It can also be used to set socket options, etc
  95. */
  96. socket_type::lowest_layer_type & get_raw_socket()
  97. {
  98. return m_socket->lowest_layer();
  99. }
  100. /// Retrieve a pointer to the layer below the ssl stream
  101. /**
  102. * This is used internally.
  103. */
  104. socket_type::next_layer_type & get_next_layer()
  105. {
  106. return m_socket->next_layer();
  107. }
  108. /// Retrieve a pointer to the wrapped socket
  109. /**
  110. * This is used internally.
  111. */
  112. socket_type & get_socket()
  113. {
  114. return *m_socket;
  115. }
  116. /// Set the socket initialization handler
  117. /**
  118. * The socket initialization handler is called after the socket object is
  119. * created but before it is used. This gives the application a chance to
  120. * set any ASIO socket options it needs.
  121. *
  122. * @param h The new socket_init_handler
  123. */
  124. void set_socket_init_handler(socket_init_handler h)
  125. {
  126. m_socket_init_handler = h;
  127. }
  128. /// Set TLS init handler
  129. /**
  130. * The tls init handler is called when needed to request a TLS context for
  131. * the library to use. A TLS init handler must be set and it must return a
  132. * valid TLS context in order for this endpoint to be able to initialize
  133. * TLS connections
  134. *
  135. * @param h The new tls_init_handler
  136. */
  137. void set_tls_init_handler(tls_init_handler h)
  138. {
  139. m_tls_init_handler = h;
  140. }
  141. /// Get the remote endpoint address
  142. /**
  143. * The iostream transport has no information about the ultimate remote
  144. * endpoint. It will return the string "iostream transport". To indicate
  145. * this.
  146. *
  147. * TODO: allow user settable remote endpoint addresses if this seems useful
  148. *
  149. * @return A string identifying the address of the remote endpoint
  150. */
  151. std::string get_remote_endpoint(lib::error_code & ec) const
  152. {
  153. std::stringstream s;
  154. lib::asio::error_code aec;
  155. lib::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(aec);
  156. if (aec)
  157. {
  158. ec = error::make_error_code(error::pass_through);
  159. s << "Error getting remote endpoint: " << aec << " (" << aec.message() << ")";
  160. return s.str();
  161. }
  162. else
  163. {
  164. ec = lib::error_code();
  165. s << ep;
  166. return s.str();
  167. }
  168. }
  169. protected:
  170. /// Perform one time initializations
  171. /**
  172. * init_asio is called once immediately after construction to initialize
  173. * Asio components to the io_service
  174. *
  175. * @param service A pointer to the endpoint's io_service
  176. * @param strand A pointer to the connection's strand
  177. * @param is_server Whether or not the endpoint is a server or not.
  178. */
  179. lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
  180. {
  181. if (! m_tls_init_handler)
  182. {
  183. return socket::make_error_code(socket::error::missing_tls_init_handler);
  184. }
  185. m_context = m_tls_init_handler(m_hdl);
  186. if (! m_context)
  187. {
  188. return socket::make_error_code(socket::error::invalid_tls_context);
  189. }
  190. m_socket = lib::make_shared<socket_type>(_WEBSOCKETPP_REF(*service), lib::ref(*m_context));
  191. if (m_socket_init_handler)
  192. {
  193. m_socket_init_handler(m_hdl, get_socket());
  194. }
  195. m_io_service = service;
  196. m_strand = strand;
  197. m_is_server = is_server;
  198. return lib::error_code();
  199. }
  200. /// Set hostname hook
  201. /**
  202. * Called by the transport as a connection is being established to provide
  203. * the hostname being connected to to the security/socket layer.
  204. *
  205. * This socket policy uses the hostname to set the appropriate TLS SNI
  206. * header.
  207. *
  208. * @since 0.6.0
  209. *
  210. * @param u The uri to set
  211. */
  212. void set_uri(uri_ptr u)
  213. {
  214. m_uri = u;
  215. }
  216. /// Pre-initialize security policy
  217. /**
  218. * Called by the transport after a new connection is created to initialize
  219. * the socket component of the connection. This method is not allowed to
  220. * write any bytes to the wire. This initialization happens before any
  221. * proxies or other intermediate wrappers are negotiated.
  222. *
  223. * @param callback Handler to call back with completion information
  224. */
  225. void pre_init(init_handler callback)
  226. {
  227. // TODO: is this the best way to check whether this function is
  228. // available in the version of OpenSSL being used?
  229. // TODO: consider case where host is an IP address
  230. #if OPENSSL_VERSION_NUMBER >= 0x90812f
  231. if (! m_is_server)
  232. {
  233. // For clients on systems with a suitable OpenSSL version, set the
  234. // TLS SNI hostname header so connecting to TLS servers using SNI
  235. // will work.
  236. long res = SSL_set_tlsext_host_name(get_socket().native_handle(), m_uri->get_host().c_str());
  237. if (! (1 == res))
  238. {
  239. callback(socket::make_error_code(socket::error::tls_failed_sni_hostname));
  240. }
  241. }
  242. #endif
  243. callback(lib::error_code());
  244. }
  245. /// Post-initialize security policy
  246. /**
  247. * Called by the transport after all intermediate proxies have been
  248. * negotiated. This gives the security policy the chance to talk with the
  249. * real remote endpoint for a bit before the websocket handshake.
  250. *
  251. * @param callback Handler to call back with completion information
  252. */
  253. void post_init(init_handler callback)
  254. {
  255. m_ec = socket::make_error_code(socket::error::tls_handshake_timeout);
  256. // TLS handshake
  257. if (m_strand)
  258. {
  259. m_socket->async_handshake(get_handshake_type(), m_strand->wrap(lib::bind(&type::handle_init, get_shared(), callback, lib::placeholders::_1)));
  260. }
  261. else
  262. {
  263. m_socket->async_handshake(get_handshake_type(), lib::bind(&type::handle_init, get_shared(), callback, lib::placeholders::_1));
  264. }
  265. }
  266. /// Sets the connection handle
  267. /**
  268. * The connection handle is passed to any handlers to identify the
  269. * connection
  270. *
  271. * @param hdl The new handle
  272. */
  273. void set_handle(connection_hdl hdl)
  274. {
  275. m_hdl = hdl;
  276. }
  277. void handle_init(init_handler callback, const lib::asio::error_code & ec)
  278. {
  279. if (ec)
  280. {
  281. m_ec = socket::make_error_code(socket::error::tls_handshake_failed);
  282. }
  283. else
  284. {
  285. m_ec = lib::error_code();
  286. }
  287. callback(m_ec);
  288. }
  289. lib::error_code get_ec() const
  290. {
  291. return m_ec;
  292. }
  293. /// Cancel all async operations on this socket
  294. /**
  295. * Attempts to cancel all async operations on this socket and reports any
  296. * failures.
  297. *
  298. * NOTE: Windows XP and earlier do not support socket cancellation.
  299. *
  300. * @return The error that occurred, if any.
  301. */
  302. lib::asio::error_code cancel_socket()
  303. {
  304. lib::asio::error_code ec;
  305. get_raw_socket().cancel(ec);
  306. return ec;
  307. }
  308. void async_shutdown(socket::shutdown_handler callback)
  309. {
  310. if (m_strand)
  311. {
  312. m_socket->async_shutdown(m_strand->wrap(callback));
  313. }
  314. else
  315. {
  316. m_socket->async_shutdown(callback);
  317. }
  318. }
  319. public:
  320. /// Translate any security policy specific information about an error code
  321. /**
  322. * Translate_ec takes an Asio error code and attempts to convert its value
  323. * to an appropriate websocketpp error code. In the case that the Asio and
  324. * Websocketpp error types are the same (such as using boost::asio and
  325. * boost::system_error or using standalone asio and std::system_error the
  326. * code will be passed through natively.
  327. *
  328. * In the case of a mismatch (boost::asio with std::system_error) a
  329. * translated code will be returned. Any error that is determined to be
  330. * related to TLS but does not have a more specific websocketpp error code
  331. * is returned under the catch all error `tls_error`. Non-TLS related errors
  332. * are returned as the transport generic error `pass_through`
  333. *
  334. * @since 0.3.0
  335. *
  336. * @param ec The error code to translate_ec
  337. * @return The translated error code
  338. */
  339. template <typename ErrorCodeType> static lib::error_code translate_ec(ErrorCodeType ec)
  340. {
  341. if (ec.category() == lib::asio::error::get_ssl_category())
  342. {
  343. // We know it is a TLS related error, but otherwise don't know more.
  344. // Pass through as TLS generic.
  345. return make_error_code(transport::error::tls_error);
  346. }
  347. else
  348. {
  349. // We don't know any more information about this error so pass
  350. // through
  351. return make_error_code(transport::error::pass_through);
  352. }
  353. }
  354. static
  355. /// Overload of translate_ec to catch cases where lib::error_code is the
  356. /// same type as lib::asio::error_code
  357. lib::error_code
  358. translate_ec(lib::error_code ec)
  359. {
  360. return ec;
  361. }
  362. private:
  363. socket_type::handshake_type get_handshake_type()
  364. {
  365. if (m_is_server)
  366. {
  367. return lib::asio::ssl::stream_base::server;
  368. }
  369. else
  370. {
  371. return lib::asio::ssl::stream_base::client;
  372. }
  373. }
  374. io_service_ptr m_io_service;
  375. strand_ptr m_strand;
  376. context_ptr m_context;
  377. socket_ptr m_socket;
  378. uri_ptr m_uri;
  379. bool m_is_server;
  380. lib::error_code m_ec;
  381. connection_hdl m_hdl;
  382. socket_init_handler m_socket_init_handler;
  383. tls_init_handler m_tls_init_handler;
  384. };
  385. /// TLS enabled Asio endpoint socket component
  386. /**
  387. * transport::asio::tls_socket::endpoint implements a secure endpoint socket
  388. * component that uses Asio's ssl::stream to wrap an ip::tcp::socket.
  389. */
  390. class endpoint
  391. {
  392. public:
  393. /// The type of this endpoint socket component
  394. typedef endpoint type;
  395. /// The type of the corresponding connection socket component
  396. typedef connection socket_con_type;
  397. /// The type of a shared pointer to the corresponding connection socket
  398. /// component.
  399. typedef socket_con_type::ptr socket_con_ptr;
  400. explicit endpoint() {}
  401. /// Checks whether the endpoint creates secure connections
  402. /**
  403. * @return Whether or not the endpoint creates secure connections
  404. */
  405. bool is_secure() const
  406. {
  407. return true;
  408. }
  409. /// Set socket init handler
  410. /**
  411. * The socket init handler is called after a connection's socket is created
  412. * but before it is used. This gives the end application an opportunity to
  413. * set asio socket specific parameters.
  414. *
  415. * @param h The new socket_init_handler
  416. */
  417. void set_socket_init_handler(socket_init_handler h)
  418. {
  419. m_socket_init_handler = h;
  420. }
  421. /// Set TLS init handler
  422. /**
  423. * The tls init handler is called when needed to request a TLS context for
  424. * the library to use. A TLS init handler must be set and it must return a
  425. * valid TLS context in order for this endpoint to be able to initialize
  426. * TLS connections
  427. *
  428. * @param h The new tls_init_handler
  429. */
  430. void set_tls_init_handler(tls_init_handler h)
  431. {
  432. m_tls_init_handler = h;
  433. }
  434. protected:
  435. /// Initialize a connection
  436. /**
  437. * Called by the transport after a new connection is created to initialize
  438. * the socket component of the connection.
  439. *
  440. * @param scon Pointer to the socket component of the connection
  441. *
  442. * @return Error code (empty on success)
  443. */
  444. lib::error_code init(socket_con_ptr scon)
  445. {
  446. scon->set_socket_init_handler(m_socket_init_handler);
  447. scon->set_tls_init_handler(m_tls_init_handler);
  448. return lib::error_code();
  449. }
  450. private:
  451. socket_init_handler m_socket_init_handler;
  452. tls_init_handler m_tls_init_handler;
  453. };
  454. } // namespace tls_socket
  455. } // namespace asio
  456. } // namespace transport
  457. } // namespace websocketpp
  458. #endif // WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP