28 #ifndef WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP 29 #define WEBSOCKETPP_TRANSPORT_SECURITY_TLS_HPP 31 #include <websocketpp/transport/asio/security/base.hpp> 33 #include <websocketpp/uri.hpp> 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> 44 namespace websocketpp {
49 namespace tls_socket {
52 typedef lib::function<
void(connection_hdl,lib::asio::ssl::stream<
53 lib::asio::ip::tcp::socket>&)> socket_init_handler;
55 typedef lib::function<lib::shared_ptr<lib::asio::ssl::context>(connection_hdl)>
63 class connection :
public lib::enable_shared_from_this<connection> {
66 typedef connection type;
68 typedef lib::shared_ptr<type> ptr;
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;
81 explicit connection() {
88 return shared_from_this();
95 bool is_secure()
const {
103 socket_type::lowest_layer_type & get_raw_socket() {
104 return m_socket->lowest_layer();
111 socket_type::next_layer_type & get_next_layer() {
112 return m_socket->next_layer();
119 socket_type & get_socket() {
131 void set_socket_init_handler(socket_init_handler h) {
132 m_socket_init_handler = h;
144 void set_tls_init_handler(tls_init_handler h) {
145 m_tls_init_handler = h;
158 std::string get_remote_endpoint(lib::error_code & ec)
const {
161 lib::asio::error_code aec;
162 lib::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(aec);
165 ec = error::make_error_code(error::pass_through);
166 s <<
"Error getting remote endpoint: " << aec
167 <<
" (" << aec.message() <<
")";
170 ec = lib::error_code();
185 lib::error_code init_asio (io_service_ptr service, strand_ptr strand,
188 if (!m_tls_init_handler) {
189 return socket::make_error_code(socket::error::missing_tls_init_handler);
191 m_context = m_tls_init_handler(m_hdl);
194 return socket::make_error_code(socket::error::invalid_tls_context);
196 m_socket = lib::make_shared<socket_type>(
197 _WEBSOCKETPP_REF(*service),lib::ref(*m_context));
199 m_io_service = service;
201 m_is_server = is_server;
203 return lib::error_code();
218 void set_uri(uri_ptr u) {
231 void pre_init(init_handler callback) {
235 #if OPENSSL_VERSION_NUMBER >= 0x90812f
240 long res = SSL_set_tlsext_host_name(
241 get_socket().native_handle(), m_uri->get_host().c_str());
243 callback(socket::make_error_code(socket::error::tls_failed_sni_hostname));
248 if (m_socket_init_handler) {
249 m_socket_init_handler(m_hdl,get_socket());
252 callback(lib::error_code());
263 void post_init(init_handler callback) {
264 m_ec = socket::make_error_code(socket::error::tls_handshake_timeout);
268 m_socket->async_handshake(
269 get_handshake_type(),
270 m_strand->wrap(lib::bind(
271 &type::handle_init, get_shared(),
273 lib::placeholders::_1
277 m_socket->async_handshake(
278 get_handshake_type(),
280 &type::handle_init, get_shared(),
282 lib::placeholders::_1
295 void set_handle(connection_hdl hdl) {
299 void handle_init(init_handler callback,lib::asio::error_code
const & ec) {
301 m_ec = socket::make_error_code(socket::error::tls_handshake_failed);
303 m_ec = lib::error_code();
309 lib::error_code get_ec()
const {
322 lib::asio::error_code cancel_socket() {
323 lib::asio::error_code ec;
324 get_raw_socket().cancel(ec);
328 void async_shutdown(socket::shutdown_handler callback) {
330 m_socket->async_shutdown(m_strand->wrap(callback));
332 m_socket->async_shutdown(callback);
355 template <
typename ErrorCodeType>
356 lib::error_code translate_ec(ErrorCodeType ec) {
357 if (ec.category() == lib::asio::error::get_ssl_category()) {
360 return make_error_code(transport::error::tls_error);
364 return make_error_code(transport::error::pass_through);
370 lib::error_code translate_ec(lib::error_code ec) {
382 socket_type::handshake_type get_handshake_type() {
384 return lib::asio::ssl::stream_base::server;
386 return lib::asio::ssl::stream_base::client;
390 io_service_ptr m_io_service;
392 context_ptr m_context;
397 lib::error_code m_ec;
399 connection_hdl m_hdl;
400 socket_init_handler m_socket_init_handler;
401 tls_init_handler m_tls_init_handler;
412 typedef endpoint type;
415 typedef connection socket_con_type;
418 typedef socket_con_type::ptr socket_con_ptr;
420 explicit endpoint() {}
426 bool is_secure()
const {
438 void set_socket_init_handler(socket_init_handler h) {
439 m_socket_init_handler = h;
451 void set_tls_init_handler(tls_init_handler h) {
452 m_tls_init_handler = h;
464 lib::error_code init(socket_con_ptr scon) {
465 scon->set_socket_init_handler(m_socket_init_handler);
466 scon->set_tls_init_handler(m_tls_init_handler);
467 return lib::error_code();
471 socket_init_handler m_socket_init_handler;
472 tls_init_handler m_tls_init_handler;