TrinityCore
Loading...
Searching...
No Matches
SslStream.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_SSL_STREAM_H
19#define TRINITYCORE_SSL_STREAM_H
20
21#include "Define.h"
22#include "Socket.h"
24#include <boost/asio/ip/tcp.hpp>
25#include <boost/asio/ssl/stream.hpp>
26#include <boost/system/error_code.hpp>
27
28namespace Trinity::Net
29{
30namespace SslHandshakeHelpers
31{
32TC_NETWORK_API void LogFailure(boost::asio::ip::address const& ipAddress, uint16 port, boost::system::error_code const& error);
33}
34
35template <typename SocketImpl>
37{
38 explicit SslHandshakeConnectionInitializer(SocketImpl* socket) : _socket(socket) { }
39
40 void Start() override
41 {
42 _socket->underlying_stream().async_handshake(boost::asio::ssl::stream_base::server,
43 [socketRef = _socket->weak_from_this(), self = this->shared_from_this()](boost::system::error_code const& error)
44 {
45 std::shared_ptr<SocketImpl> socket = static_pointer_cast<SocketImpl>(socketRef.lock());
46 if (!socket)
47 return;
48
49 if (error)
50 {
51 SslHandshakeHelpers::LogFailure(socket->GetRemoteIpAddress(), socket->GetRemotePort(), error);
52 socket->CloseSocket();
53 return;
54 }
55
56 self->InvokeNext();
57 });
58 }
59
60private:
61 SocketImpl* _socket;
62};
63
64template<class WrappedStream = IoContextTcpSocket>
66{
67public:
68 using executor_type = typename WrappedStream::executor_type;
69
70 explicit SslStream(IoContextTcpSocket&& socket, boost::asio::ssl::context& sslContext) : _sslSocket(std::move(socket), sslContext)
71 {
72 _sslSocket.set_verify_mode(boost::asio::ssl::verify_none);
73 }
74
75 explicit SslStream(boost::asio::io_context& context, boost::asio::ssl::context& sslContext) : _sslSocket(context, sslContext)
76 {
77 _sslSocket.set_verify_mode(boost::asio::ssl::verify_none);
78 }
79
80 // adapting tcp::socket api
81 boost::asio::io_context::executor_type get_executor()
82 {
83 return _sslSocket.get_executor();
84 }
85
86 bool is_open() const
87 {
88 return _sslSocket.next_layer().is_open();
89 }
90
91 void close(boost::system::error_code& error)
92 {
93 _sslSocket.next_layer().close(error);
94 }
95
96 void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
97 {
98 _sslSocket.shutdown(shutdownError);
99 _sslSocket.next_layer().shutdown(what, shutdownError);
100 }
101
102 template<typename ConnectHandlerType>
103 decltype(auto) async_connect(boost::asio::ip::tcp::endpoint const& endpoint, ConnectHandlerType&& handler)
104 {
105 return _sslSocket.next_layer().async_connect(endpoint, std::forward<ConnectHandlerType>(handler));
106 }
107
108 template<typename MutableBufferSequence, typename ReadHandlerType>
109 decltype(auto) async_read_some(MutableBufferSequence const& buffers, ReadHandlerType&& handler)
110 {
111 return _sslSocket.async_read_some(buffers, std::forward<ReadHandlerType>(handler));
112 }
113
114 template<typename ConstBufferSequence, typename WriteHandlerType>
115 decltype(auto) async_write_some(ConstBufferSequence const& buffers, WriteHandlerType&& handler)
116 {
117 return _sslSocket.async_write_some(buffers, std::forward<WriteHandlerType>(handler));
118 }
119
120 template<typename ConstBufferSequence>
121 std::size_t write_some(ConstBufferSequence const& buffers, boost::system::error_code& error)
122 {
123 return _sslSocket.write_some(buffers, error);
124 }
125
126 template<typename WaitHandlerType>
127 decltype(auto) async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType&& handler)
128 {
129 return _sslSocket.next_layer().async_wait(type, std::forward<WaitHandlerType>(handler));
130 }
131
132 template<typename SettableSocketOption>
133 void set_option(SettableSocketOption const& option, boost::system::error_code& error)
134 {
135 _sslSocket.next_layer().set_option(option, error);
136 }
137
138 IoContextTcpSocket::endpoint_type remote_endpoint() const
139 {
140 return _sslSocket.next_layer().remote_endpoint();
141 }
142
143 // ssl api
144 template<typename HandshakeHandlerType>
145 decltype(auto) async_handshake(boost::asio::ssl::stream_base::handshake_type type, HandshakeHandlerType&& handler)
146 {
147 return _sslSocket.async_handshake(type, std::forward<HandshakeHandlerType>(handler));
148 }
149
150 void set_server_name(std::string const& serverName, boost::system::error_code& error)
151 {
152 if (!SSL_set_tlsext_host_name(_sslSocket.native_handle(), serverName.c_str()))
153 error.assign(static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
154 }
155
156protected:
157 boost::asio::ssl::stream<WrappedStream> _sslSocket;
158};
159}
160
161#endif // TRINITYCORE_SSL_STREAM_H
#define TC_NETWORK_API
Definition Define.h:117
uint16_t uint16
Definition Define.h:155
SslStream(IoContextTcpSocket &&socket, boost::asio::ssl::context &sslContext)
Definition SslStream.h:70
decltype(auto) async_connect(boost::asio::ip::tcp::endpoint const &endpoint, ConnectHandlerType &&handler)
Definition SslStream.h:103
typename WrappedStream::executor_type executor_type
Definition SslStream.h:68
decltype(auto) async_handshake(boost::asio::ssl::stream_base::handshake_type type, HandshakeHandlerType &&handler)
Definition SslStream.h:145
void set_server_name(std::string const &serverName, boost::system::error_code &error)
Definition SslStream.h:150
decltype(auto) async_read_some(MutableBufferSequence const &buffers, ReadHandlerType &&handler)
Definition SslStream.h:109
IoContextTcpSocket::endpoint_type remote_endpoint() const
Definition SslStream.h:138
SslStream(boost::asio::io_context &context, boost::asio::ssl::context &sslContext)
Definition SslStream.h:75
void set_option(SettableSocketOption const &option, boost::system::error_code &error)
Definition SslStream.h:133
decltype(auto) async_write_some(ConstBufferSequence const &buffers, WriteHandlerType &&handler)
Definition SslStream.h:115
void close(boost::system::error_code &error)
Definition SslStream.h:91
std::size_t write_some(ConstBufferSequence const &buffers, boost::system::error_code &error)
Definition SslStream.h:121
void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code &shutdownError)
Definition SslStream.h:96
boost::asio::io_context::executor_type get_executor()
Definition SslStream.h:81
decltype(auto) async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType &&handler)
Definition SslStream.h:127
boost::asio::ssl::stream< WrappedStream > _sslSocket
Definition SslStream.h:157
bool is_open() const
Definition SslStream.h:86
TC_NETWORK_API void LogFailure(boost::asio::ip::address const &ipAddress, uint16 port, boost::system::error_code const &error)
Definition SslStream.cpp:22
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket
Definition Socket.h:40
STL namespace.
SslHandshakeConnectionInitializer(SocketImpl *socket)
Definition SslStream.h:38