| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include "tor_address.h" |
| 31 | |
| 32 | #include <algorithm> |
| 33 | #include <boost/spirit/include/karma_generate.hpp> |
| 34 | #include <boost/spirit/include/karma_uint.hpp> |
| 35 | #include <cassert> |
| 36 | #include <cstring> |
| 37 | #include <limits> |
| 38 | |
| 39 | #include "net/error.h" |
| 40 | #include "serialization/keyvalue_serialization.h" |
| 41 | #include "storages/portable_storage.h" |
| 42 | #include "string_tools_lexical.h" |
| 43 | |
| 44 | namespace net |
| 45 | { |
| 46 | namespace |
| 47 | { |
| 48 | constexpr const char tld[] = u8".onion"; |
| 49 | constexpr const char unknown_host[] = "<unknown tor host>"; |
| 50 | |
| 51 | constexpr const unsigned v2_length = 16; |
| 52 | constexpr const unsigned v3_length = 56; |
| 53 | |
| 54 | constexpr const char base32_alphabet[] = |
| 55 | u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz234567"; |
| 56 | |
| 57 | expect<void> host_check(boost::string_ref host) noexcept |
| 58 | { |
| 59 | if (!host.ends_with(tld)) |
| 60 | return {net::error::expected_tld}; |
| 61 | |
| 62 | host.remove_suffix(sizeof(tld) - 1); |
| 63 | |
| 64 | //! \TODO v3 has checksum, base32 decoding is required to verify it |
| 65 | if (host.size() != v2_length && host.size() != v3_length) |
| 66 | return {net::error::invalid_tor_address}; |
| 67 | if (host.find_first_not_of(base32_alphabet) != boost::string_ref::npos) |
| 68 | return {net::error::invalid_tor_address}; |
| 69 | |
| 70 | return success(); |
| 71 | } |
| 72 | |
| 73 | struct tor_serialized |
| 74 | { |
| 75 | std::string host; |
| 76 | std::uint16_t port; |
| 77 | |
| 78 | BEGIN_KV_SERIALIZE_MAP() |
| 79 | KV_SERIALIZE(host) |
| 80 | KV_SERIALIZE(port) |
| 81 | END_KV_SERIALIZE_MAP() |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | tor_address::tor_address(const boost::string_ref host, const std::uint16_t port) noexcept |
| 86 | : port_(port) |
| 87 | { |
| 88 | // this is a private constructor, throw if moved to public |
| 89 | assert(host.size() < sizeof(host_)); |
| 90 | |
| 91 | const std::size_t length = std::min(sizeof(host_) - 1, host.size()); |
| 92 | std::memcpy(host_, host.data(), length); |
| 93 | std::memset(host_ + length, 0, sizeof(host_) - length); |
| 94 | } |
| 95 | |
| 96 | const char* tor_address::unknown_str() noexcept |
| 97 | { |
| 98 | return unknown_host; |
| 99 | } |
| 100 | |
| 101 | tor_address::tor_address() noexcept |
| 102 | : port_(0) |
| 103 | { |
| 104 | static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size"); |
| 105 | std::memcpy(host_, unknown_host, sizeof(unknown_host)); |
| 106 | std::memset(host_ + sizeof(unknown_host), 0, sizeof(host_) - sizeof(unknown_host)); |
| 107 | } |
| 108 | |
| 109 | expect<tor_address> tor_address::make(const boost::string_ref address, const std::uint16_t default_port) |
| 110 | { |
| 111 | boost::string_ref host = address.substr(0, address.rfind(':')); |
| 112 | const boost::string_ref port = |
| 113 | address.substr(host.size() + (host.size() == address.size() ? 0 : 1)); |
| 114 | |
| 115 | MONERO_CHECK(host_check(host)); |
| 116 | |
| 117 | std::uint16_t porti = default_port; |
| 118 | if (!port.empty() && !epee::string_tools::get_xtype_from_string(porti, std::string{port})) |
| 119 | return {net::error::invalid_port}; |
| 120 | |
| 121 | static_assert(v2_length <= v3_length, "bad internal host size"); |
| 122 | static_assert(v3_length + sizeof(tld) == sizeof(tor_address::host_), "bad internal host size"); |
| 123 | return tor_address{host, porti}; |
| 124 | } |
| 125 | |
| 126 | bool tor_address::_load(epee::serialization::portable_storage& src, epee::serialization::section* hparent) |
| 127 | { |
| 128 | tor_serialized in{}; |
| 129 | if (in._load(src, hparent) && in.host.size() < sizeof(host_) && (in.host == unknown_host || !host_check(in.host).has_error())) |
| 130 | { |
| 131 | std::memcpy(host_, in.host.data(), in.host.size()); |
| 132 | std::memset(host_ + in.host.size(), 0, sizeof(host_) - in.host.size()); |
| 133 | port_ = in.port; |
| 134 | return true; |
| 135 | } |
| 136 | static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size"); |
| 137 | std::memcpy(host_, unknown_host, sizeof(unknown_host)); // include null terminator |
| 138 | port_ = 0; |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool tor_address::store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const |
| 143 | { |
| 144 | const tor_serialized out{std::string{host_}, port_}; |
| 145 | return out.store(dest, hparent); |
| 146 | } |
| 147 | |
| 148 | tor_address::tor_address(const tor_address& rhs) noexcept |
| 149 | : port_(rhs.port_) |
| 150 | { |
| 151 | std::memcpy(host_, rhs.host_, sizeof(host_)); |
| 152 | } |
| 153 | |
| 154 | tor_address& tor_address::operator=(const tor_address& rhs) noexcept |
| 155 | { |
| 156 | if (this != std::addressof(rhs)) |
| 157 | { |
| 158 | port_ = rhs.port_; |
| 159 | std::memcpy(host_, rhs.host_, sizeof(host_)); |
| 160 | } |
| 161 | return *this; |
| 162 | } |
| 163 | |
| 164 | bool tor_address::is_unknown() const noexcept |
| 165 | { |
| 166 | static_assert(1 <= sizeof(host_), "host size too small"); |
| 167 | return host_[0] == '<'; // character is not allowed otherwise |
| 168 | } |
| 169 | |
| 170 | bool tor_address::equal(const tor_address& rhs) const noexcept |
| 171 | { |
| 172 | return port_ == rhs.port_ && is_same_host(rhs); |
| 173 | } |
| 174 | |
| 175 | bool tor_address::less(const tor_address& rhs) const noexcept |
| 176 | { |
| 177 | int res = std::strcmp(host_str(), rhs.host_str()); |
| 178 | return res < 0 || (res == 0 && port() < rhs.port()); |
| 179 | } |
| 180 | |
| 181 | bool tor_address::is_same_host(const tor_address& rhs) const noexcept |
| 182 | { |
| 183 | //! \TODO v2 and v3 should be comparable - requires base32 |
| 184 | return std::strcmp(host_str(), rhs.host_str()) == 0; |
| 185 | } |
| 186 | |
| 187 | std::string tor_address::str() const |
| 188 | { |
| 189 | const std::size_t host_length = std::strlen(host_str()); |
| 190 | const std::size_t port_length = |
| 191 | port_ == 0 ? 0 : std::numeric_limits<std::uint16_t>::digits10 + 2; |
| 192 | |
| 193 | std::string out{}; |
| 194 | out.reserve(host_length + port_length); |
| 195 | out.assign(host_str(), host_length); |
| 196 | |
| 197 | if (port_ != 0) |
| 198 | { |
| 199 | out.push_back(':'); |
| 200 | namespace karma = boost::spirit::karma; |
| 201 | karma::generate(std::back_inserter(out), karma::ushort_, port()); |
| 202 | } |
| 203 | return out; |
| 204 | } |
| 205 | } |