| 1 | // Copyright (c) 2017-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 <boost/optional/optional.hpp> |
| 30 | #include <limits> |
| 31 | #include <string.h> |
| 32 | #include "memwipe.h" |
| 33 | #include "misc_log_ex.h" |
| 34 | #include "wipeable_string.h" |
| 35 | |
| 36 | static constexpr const char hex[] = u8"0123456789abcdef"; |
| 37 | |
| 38 | namespace |
| 39 | { |
| 40 | int atolower(int c) |
| 41 | { |
| 42 | if (c >= 'A' && c <= 'Z') |
| 43 | c |= 32; |
| 44 | return c; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | namespace epee |
| 49 | { |
| 50 | |
| 51 | wipeable_string::wipeable_string(const wipeable_string &other): |
| 52 | buffer(other.buffer) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | wipeable_string::wipeable_string(wipeable_string &&other) |
| 57 | { |
| 58 | if (&other == this) |
| 59 | return; |
| 60 | buffer = std::move(other.buffer); |
| 61 | } |
| 62 | |
| 63 | wipeable_string::wipeable_string(const std::string &other) |
| 64 | { |
| 65 | grow(other.size()); |
| 66 | if (size() > 0) |
| 67 | memcpy(buffer.data(), other.c_str(), size()); |
| 68 | } |
| 69 | |
| 70 | wipeable_string::wipeable_string(std::string &&other) |
| 71 | { |
| 72 | grow(other.size()); |
| 73 | if (size() > 0) |
| 74 | memcpy(buffer.data(), other.c_str(), size()); |
| 75 | if (!other.empty()) |
| 76 | { |
| 77 | memwipe(&other[0], other.size()); // we're kinda left with this again aren't we |
| 78 | other = std::string(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | wipeable_string::wipeable_string(const char *s) |
| 83 | { |
| 84 | grow(strlen(s)); |
| 85 | if (size() > 0) |
| 86 | memcpy(buffer.data(), s, size()); |
| 87 | } |
| 88 | |
| 89 | wipeable_string::wipeable_string(const char *s, size_t len) |
| 90 | { |
| 91 | grow(len); |
| 92 | memcpy(buffer.data(), s, len); |
| 93 | } |
| 94 | |
| 95 | wipeable_string::~wipeable_string() |
| 96 | { |
| 97 | wipe(); |
| 98 | } |
| 99 | |
| 100 | void wipeable_string::wipe() |
| 101 | { |
| 102 | if (!buffer.empty()) |
| 103 | memwipe(buffer.data(), buffer.size() * sizeof(char)); |
| 104 | } |
| 105 | |
| 106 | void wipeable_string::grow(size_t sz, size_t reserved) |
| 107 | { |
| 108 | if (reserved < sz) |
| 109 | reserved = sz; |
| 110 | if (reserved <= buffer.capacity()) |
| 111 | { |
| 112 | if (sz < buffer.size()) |
| 113 | memwipe(buffer.data() + sz, buffer.size() - sz); |
| 114 | buffer.resize(sz); |
| 115 | return; |
| 116 | } |
| 117 | size_t old_sz = buffer.size(); |
| 118 | std::unique_ptr<char[]> tmp{new char[old_sz]}; |
| 119 | if (old_sz > 0) |
| 120 | { |
| 121 | memcpy(tmp.get(), buffer.data(), old_sz * sizeof(char)); |
| 122 | memwipe(buffer.data(), old_sz * sizeof(char)); |
| 123 | } |
| 124 | buffer.reserve(reserved); |
| 125 | buffer.resize(sz); |
| 126 | if (old_sz > 0) |
| 127 | { |
| 128 | memcpy(buffer.data(), tmp.get(), old_sz * sizeof(char)); |
| 129 | memwipe(tmp.get(), old_sz * sizeof(char)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void wipeable_string::push_back(char c) |
| 134 | { |
| 135 | grow(size() + 1); |
| 136 | buffer.back() = c; |
| 137 | } |
| 138 | |
| 139 | void wipeable_string::operator+=(char c) |
| 140 | { |
| 141 | push_back(c); |
| 142 | } |
| 143 | |
| 144 | void wipeable_string::append(const char *ptr, size_t len) |
| 145 | { |
| 146 | const size_t orgsz = size(); |
| 147 | CHECK_AND_ASSERT_THROW_MES(orgsz < std::numeric_limits<size_t>::max() - len, "Appended data too large"); |
| 148 | grow(orgsz + len); |
| 149 | if (len > 0) |
| 150 | memcpy(data() + orgsz, ptr, len); |
| 151 | } |
| 152 | |
| 153 | void wipeable_string::operator+=(const char *s) |
| 154 | { |
| 155 | append(s, strlen(s)); |
| 156 | } |
| 157 | |
| 158 | void wipeable_string::operator+=(const epee::wipeable_string &s) |
| 159 | { |
| 160 | append(s.data(), s.size()); |
| 161 | } |
| 162 | |
| 163 | void wipeable_string::operator+=(const std::string &s) |
| 164 | { |
| 165 | append(s.c_str(), s.size()); |
| 166 | } |
| 167 | |
| 168 | void wipeable_string::trim() |
| 169 | { |
| 170 | size_t prefix = 0; |
| 171 | while (prefix < size() && data()[prefix] == ' ') |
| 172 | ++prefix; |
| 173 | if (prefix > 0) |
| 174 | memmove(buffer.data(), buffer.data() + prefix, size() - prefix); |
| 175 | |
| 176 | size_t suffix = 0; |
| 177 | while (suffix < size()-prefix && data()[size() - 1 - prefix - suffix] == ' ') |
| 178 | ++suffix; |
| 179 | |
| 180 | resize(size() - prefix - suffix); |
| 181 | } |
| 182 | |
| 183 | void wipeable_string::split(std::vector<wipeable_string> &fields) const |
| 184 | { |
| 185 | fields.clear(); |
| 186 | size_t len = size(); |
| 187 | const char *ptr = data(); |
| 188 | bool space = true; |
| 189 | while (len--) |
| 190 | { |
| 191 | const char c = *ptr++; |
| 192 | const bool space_prev = space; |
| 193 | space = std::isspace(c); |
| 194 | if (!space) |
| 195 | { |
| 196 | if (space_prev) |
| 197 | fields.push_back({}); |
| 198 | fields.back().push_back(c); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | boost::optional<epee::wipeable_string> wipeable_string::parse_hexstr() const |
| 204 | { |
| 205 | if (size() % 2 != 0) |
| 206 | return boost::none; |
| 207 | boost::optional<epee::wipeable_string> res = epee::wipeable_string(""); |
| 208 | const size_t len = size(); |
| 209 | const char *d = data(); |
| 210 | res->grow(0, len / 2); |
| 211 | for (size_t i = 0; i < len; i += 2) |
| 212 | { |
| 213 | char c = atolower(d[i]); |
| 214 | const char *ptr0 = strchr(hex, c); |
| 215 | if (!ptr0) |
| 216 | return boost::none; |
| 217 | c = atolower(d[i+1]); |
| 218 | const char *ptr1 = strchr(hex, c); |
| 219 | if (!ptr1) |
| 220 | return boost::none; |
| 221 | res->push_back(((ptr0-hex)<<4) | (ptr1-hex)); |
| 222 | } |
| 223 | return res; |
| 224 | } |
| 225 | |
| 226 | char wipeable_string::pop_back() |
| 227 | { |
| 228 | const size_t sz = size(); |
| 229 | CHECK_AND_ASSERT_THROW_MES(sz > 0, "Popping from an empty string"); |
| 230 | const char c = buffer.back(); |
| 231 | resize(sz - 1); |
| 232 | return c; |
| 233 | } |
| 234 | |
| 235 | void wipeable_string::resize(size_t sz) |
| 236 | { |
| 237 | grow(sz); |
| 238 | } |
| 239 | |
| 240 | void wipeable_string::reserve(size_t sz) |
| 241 | { |
| 242 | grow(size(), sz); |
| 243 | } |
| 244 | |
| 245 | void wipeable_string::clear() |
| 246 | { |
| 247 | resize(0); |
| 248 | } |
| 249 | |
| 250 | wipeable_string &wipeable_string::operator=(wipeable_string &&other) |
| 251 | { |
| 252 | if (&other != this) |
| 253 | buffer = std::move(other.buffer); |
| 254 | return *this; |
| 255 | } |
| 256 | |
| 257 | wipeable_string &wipeable_string::operator=(const wipeable_string &other) |
| 258 | { |
| 259 | if (&other != this) |
| 260 | buffer = other.buffer; |
| 261 | return *this; |
| 262 | } |
| 263 | |
| 264 | } |