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