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