| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include "socks.h" |
| 30 | |
| 31 | #include <algorithm> |
| 32 | #include <boost/asio/bind_executor.hpp> |
| 33 | #include <boost/asio/buffer.hpp> |
| 34 | #include <boost/asio/dispatch.hpp> |
| 35 | #include <boost/asio/read.hpp> |
| 36 | #include <boost/asio/write.hpp> |
| 37 | #include <boost/endian/arithmetic.hpp> |
| 38 | #include <boost/endian/conversion.hpp> |
| 39 | #include <cstring> |
| 40 | #include <limits> |
| 41 | #include <string> |
| 42 | |
| 43 | #include "net/net_utils_base.h" |
| 44 | #include "net/tor_address.h" |
| 45 | #include "net/i2p_address.h" |
| 46 | |
| 47 | namespace net |
| 48 | { |
| 49 | namespace socks |
| 50 | { |
| 51 | namespace |
| 52 | { |
| 53 | constexpr const std::uint8_t v4_connect_command = 1; |
| 54 | constexpr const std::uint8_t v4tor_resolve_command = 0xf0; |
| 55 | constexpr const std::uint8_t v4_request_granted = 90; |
| 56 | |
| 57 | struct v4_header |
| 58 | { |
| 59 | std::uint8_t version; |
| 60 | std::uint8_t command_code; |
| 61 | boost::endian::big_uint16_t port; |
| 62 | boost::endian::big_uint32_t ip; |
| 63 | }; |
| 64 | |
| 65 | std::size_t write_domain_header(epee::span<std::uint8_t> out, const std::uint8_t command, const std::uint16_t port, const boost::string_ref domain) |
| 66 | { |
| 67 | if (std::numeric_limits<std::size_t>::max() - sizeof(v4_header) - 2 < domain.size()) |
| 68 | return 0; |
| 69 | |
| 70 | const std::size_t buf_size = sizeof(v4_header) + domain.size() + 2; |
| 71 | if (out.size() < buf_size) |
| 72 | return 0; |
| 73 | |
| 74 | // version 4, 1 indicates invalid ip for domain extension |
| 75 | const v4_header temp{4, command, port, std::uint32_t(1)}; |
| 76 | std::memcpy(out.data(), std::addressof(temp), sizeof(temp)); |
| 77 | out.remove_prefix(sizeof(temp)); |
| 78 | |
| 79 | *(out.data()) = 0; |
| 80 | out.remove_prefix(1); |
| 81 | |
| 82 | std::memcpy(out.data(), domain.data(), domain.size()); |
| 83 | out.remove_prefix(domain.size()); |
| 84 | |
| 85 | *(out.data()) = 0; |
| 86 | return buf_size; |
| 87 | } |
| 88 | |
| 89 | struct socks_category : boost::system::error_category |
| 90 | { |
| 91 | explicit socks_category() noexcept |
| 92 | : boost::system::error_category() |
| 93 | {} |
| 94 | |
| 95 | const char* name() const noexcept override |
| 96 | { |
| 97 | return "net::socks::error_category"; |
| 98 | } |
| 99 | |
| 100 | virtual std::string message(int value) const override |
| 101 | { |
| 102 | switch (socks::error(value)) |
| 103 | { |
| 104 | case socks::error::rejected: |
| 105 | return "Socks request rejected or failed"; |
| 106 | case socks::error::identd_connection: |
| 107 | return "Socks request rejected because server cannot connect to identd on the client"; |
| 108 | case socks::error::identd_user: |
| 109 | return "Socks request rejected because the client program and identd report different user-ids"; |
| 110 | |
| 111 | case socks::error::bad_read: |
| 112 | return "Socks boost::async_read read fewer bytes than expected"; |
| 113 | case socks::error::bad_write: |
| 114 | return "Socks boost::async_write wrote fewer bytes than expected"; |
| 115 | case socks::error::unexpected_version: |
| 116 | return "Socks server returned unexpected version in reply"; |
| 117 | |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | return "Unknown net::socks::error"; |
| 122 | } |
| 123 | |
| 124 | boost::system::error_condition default_error_condition(int value) const noexcept override |
| 125 | { |
| 126 | switch (socks::error(value)) |
| 127 | { |
| 128 | case socks::error::bad_read: |
| 129 | case socks::error::bad_write: |
| 130 | return boost::system::errc::io_error; |
| 131 | case socks::error::unexpected_version: |
| 132 | return boost::system::errc::protocol_error; |
| 133 | default: |
| 134 | break; |
| 135 | }; |
| 136 | if (1 <= value && value <= 256) |
| 137 | return boost::system::errc::protocol_error; |
| 138 | |
| 139 | return boost::system::error_condition{value, *this}; |
| 140 | } |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | const boost::system::error_category& error_category() noexcept |
| 145 | { |
| 146 | static const socks_category instance{}; |
| 147 | return instance; |
| 148 | } |
| 149 | |
| 150 | struct client::completed |
| 151 | { |
| 152 | std::shared_ptr<client> self_; |
| 153 | |
| 154 | void operator()(const boost::system::error_code error, const std::size_t bytes) const |
| 155 | { |
| 156 | static_assert(1 < sizeof(self_->buffer_), "buffer too small for v4 response"); |
| 157 | |
| 158 | if (self_) |
| 159 | { |
| 160 | client& self = *self_; |
| 161 | self.buffer_size_ = std::min(bytes, sizeof(self.buffer_)); |
| 162 | |
| 163 | if (error) |
| 164 | self.done(error, std::move(self_)); |
| 165 | else if (self.buffer().size() < sizeof(v4_header)) |
| 166 | self.done(socks::error::bad_read, std::move(self_)); |
| 167 | else if (self.buffer_[0] != 0) // response version |
| 168 | self.done(socks::error::unexpected_version, std::move(self_)); |
| 169 | else if (self.buffer_[1] != v4_request_granted) |
| 170 | self.done(socks::error(int(self.buffer_[1]) + 1), std::move(self_)); |
| 171 | else |
| 172 | self.done(boost::system::error_code{}, std::move(self_)); |
| 173 | } |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | struct client::read |
| 178 | { |
| 179 | std::shared_ptr<client> self_; |
| 180 | |
| 181 | static boost::asio::mutable_buffer get_buffer(client& self) noexcept |
| 182 | { |
| 183 | static_assert(sizeof(v4_header) <= sizeof(self.buffer_), "buffer too small for v4 response"); |
| 184 | return boost::asio::buffer(self.buffer_, sizeof(v4_header)); |
| 185 | } |
| 186 | |
| 187 | void operator()(const boost::system::error_code error, const std::size_t bytes) |
| 188 | { |
| 189 | if (self_) |
| 190 | { |
| 191 | client& self = *self_; |
| 192 | if (error) |
| 193 | self.done(error, std::move(self_)); |
| 194 | else if (bytes < self.buffer().size()) |
| 195 | self.done(socks::error::bad_write, std::move(self_)); |
| 196 | else |
| 197 | boost::asio::async_read(self.proxy_, get_buffer(self), boost::asio::bind_executor(self.strand_, completed{std::move(self_)})); |
| 198 | } |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | struct client::write |
| 203 | { |
| 204 | std::shared_ptr<client> self_; |
| 205 | |
| 206 | static boost::asio::const_buffer get_buffer(client const& self) noexcept |
| 207 | { |
| 208 | return boost::asio::buffer(self.buffer_, self.buffer_size_); |
| 209 | } |
| 210 | |
| 211 | void operator()(const boost::system::error_code error) |
| 212 | { |
| 213 | if (self_) |
| 214 | { |
| 215 | client& self = *self_; |
| 216 | if (error) |
| 217 | self.done(error, std::move(self_)); |
| 218 | else |
| 219 | boost::asio::async_write(self.proxy_, get_buffer(self), boost::asio::bind_executor(self.strand_, read{std::move(self_)})); |
| 220 | } |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | client::client(stream_type::socket&& proxy, socks::version ver) |
| 225 | : proxy_(std::move(proxy)), strand_(proxy_.get_executor()), buffer_size_(0), buffer_(), ver_(ver) |
| 226 | {} |
| 227 | |
| 228 | client::~client() {} |
| 229 | |
| 230 | bool client::set_connect_command(const epee::net_utils::ipv4_network_address& address) |
| 231 | { |
| 232 | switch (socks_version()) |
| 233 | { |
| 234 | case version::v4: |
| 235 | case version::v4a: |
| 236 | case version::v4a_tor: |
| 237 | break; |
| 238 | default: |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | static_assert(sizeof(v4_header) < sizeof(buffer_), "buffer size too small for request"); |
| 243 | static_assert(0 < sizeof(buffer_), "buffer size too small for null termination"); |
| 244 | |
| 245 | // version 4 |
| 246 | const v4_header temp{4, v4_connect_command, address.port(), boost::endian::big_to_native(address.ip())}; |
| 247 | std::memcpy(std::addressof(buffer_), std::addressof(temp), sizeof(temp)); |
| 248 | buffer_[sizeof(temp)] = 0; |
| 249 | buffer_size_ = sizeof(temp) + 1; |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | bool client::set_connect_command(const boost::string_ref domain, std::uint16_t port) |
| 255 | { |
| 256 | switch (socks_version()) |
| 257 | { |
| 258 | case version::v4a: |
| 259 | case version::v4a_tor: |
| 260 | break; |
| 261 | |
| 262 | default: |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | const std::size_t buf_used = write_domain_header(buffer_, v4_connect_command, port, domain); |
| 267 | buffer_size_ = buf_used; |
| 268 | return buf_used != 0; |
| 269 | } |
| 270 | |
| 271 | bool client::set_connect_command(const net::tor_address& address) |
| 272 | { |
| 273 | if (!address.is_unknown()) |
| 274 | return set_connect_command(address.host_str(), address.port()); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | bool client::set_connect_command(const net::i2p_address& address) |
| 279 | { |
| 280 | if (!address.is_unknown()) |
| 281 | return set_connect_command(address.host_str(), address.port()); |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | bool client::set_resolve_command(boost::string_ref domain) |
| 286 | { |
| 287 | if (socks_version() != version::v4a_tor) |
| 288 | return false; |
| 289 | |
| 290 | const std::size_t buf_used = write_domain_header(buffer_, v4tor_resolve_command, 0, domain); |
| 291 | buffer_size_ = buf_used; |
| 292 | return buf_used != 0; |
| 293 | } |
| 294 | |
| 295 | bool client::connect_and_send(std::shared_ptr<client> self, const stream_type::endpoint& proxy_address) |
| 296 | { |
| 297 | if (self && !self->buffer().empty()) |
| 298 | { |
| 299 | client& alias = *self; |
| 300 | alias.proxy_.async_connect(proxy_address, boost::asio::bind_executor(alias.strand_, write{std::move(self)})); |
| 301 | return true; |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | bool client::send(std::shared_ptr<client> self) |
| 307 | { |
| 308 | if (self && !self->buffer().empty()) |
| 309 | { |
| 310 | client& alias = *self; |
| 311 | boost::asio::async_write(alias.proxy_, write::get_buffer(alias), boost::asio::bind_executor(alias.strand_, read{std::move(self)})); |
| 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | void client::async_close::operator()(boost::system::error_code error) |
| 318 | { |
| 319 | if (self_ && error != boost::system::errc::operation_canceled) |
| 320 | { |
| 321 | const std::shared_ptr<client> self = std::move(self_); |
| 322 | boost::asio::dispatch(self->strand_, [self] () |
| 323 | { |
| 324 | if (self && self->proxy_.is_open()) |
| 325 | { |
| 326 | boost::system::error_code ec; |
| 327 | self->proxy_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
| 328 | self->proxy_.close(ec); |
| 329 | } |
| 330 | }); |
| 331 | } |
| 332 | } |
| 333 | } // socks |
| 334 | } // net |