| 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 <cinttypes> |
| 30 | #include <stdlib.h> |
| 31 | #include <chrono> |
| 32 | #include "include_base_utils.h" |
| 33 | #include "string_tools.h" |
| 34 | #include "rpc_payment_signature.h" |
| 35 | |
| 36 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 37 | #define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc.payment" |
| 38 | |
| 39 | #define TIMESTAMP_LEEWAY (60 * 1000000) /* 60 seconds, in microseconds */ |
| 40 | |
| 41 | namespace cryptonote |
| 42 | { |
| 43 | std::string make_rpc_payment_signature(const crypto::secret_key &skey) |
| 44 | { |
| 45 | std::string s; |
| 46 | crypto::public_key pkey; |
| 47 | crypto::secret_key_to_public_key(skey, pkey); |
| 48 | crypto::signature sig; |
| 49 | const uint64_t now = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
| 50 | char ts[17]; |
| 51 | int ret = snprintf(ts, sizeof(ts), "%16.16" PRIx64, now); |
| 52 | CHECK_AND_ASSERT_MES(ret == 16, "", "snprintf failed"); |
| 53 | ts[16] = 0; |
| 54 | CHECK_AND_ASSERT_MES(strlen(ts) == 16, "", "Invalid time conversion"); |
| 55 | crypto::hash hash; |
| 56 | crypto::cn_fast_hash(ts, 16, hash); |
| 57 | crypto::generate_signature(hash, pkey, skey, sig); |
| 58 | s = epee::string_tools::pod_to_hex(pkey) + ts + epee::string_tools::pod_to_hex(sig); |
| 59 | return s; |
| 60 | } |
| 61 | |
| 62 | bool verify_rpc_payment_signature(const std::string &message, crypto::public_key &pkey, uint64_t &ts) |
| 63 | { |
| 64 | if (message.size() != 2 * sizeof(crypto::public_key) + 16 + 2 * sizeof(crypto::signature)) |
| 65 | { |
| 66 | MDEBUG("Bad message size: " << message.size()); |
| 67 | return false; |
| 68 | } |
| 69 | const std::string pkey_string = message.substr(0, 2 * sizeof(crypto::public_key)); |
| 70 | const std::string ts_string = message.substr(2 * sizeof(crypto::public_key), 16); |
| 71 | const std::string signature_string = message.substr(2 * sizeof(crypto::public_key) + 16); |
| 72 | if (!epee::string_tools::hex_to_pod(pkey_string, pkey)) |
| 73 | { |
| 74 | MDEBUG("Bad client id"); |
| 75 | return false; |
| 76 | } |
| 77 | crypto::signature signature; |
| 78 | if (!epee::string_tools::hex_to_pod(signature_string, signature)) |
| 79 | { |
| 80 | MDEBUG("Bad signature"); |
| 81 | return false; |
| 82 | } |
| 83 | crypto::hash hash; |
| 84 | crypto::cn_fast_hash(ts_string.data(), 16, hash); |
| 85 | if (!crypto::check_signature(hash, pkey, signature)) |
| 86 | { |
| 87 | MDEBUG("signature does not verify"); |
| 88 | return false; |
| 89 | } |
| 90 | char *endptr = NULL; |
| 91 | errno = 0; |
| 92 | unsigned long long ull = strtoull(ts_string.c_str(), &endptr, 16); |
| 93 | if (ull == ULLONG_MAX && errno == ERANGE) |
| 94 | { |
| 95 | MDEBUG("bad timestamp"); |
| 96 | return false; |
| 97 | } |
| 98 | ts = ull; |
| 99 | const uint64_t now = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
| 100 | if (ts > now + TIMESTAMP_LEEWAY) |
| 101 | { |
| 102 | MDEBUG("Timestamp is in the future"); |
| 103 | return false; |
| 104 | } |
| 105 | if (ts < now - TIMESTAMP_LEEWAY) |
| 106 | { |
| 107 | MDEBUG("Timestamp is too old"); |
| 108 | return false; |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | } |