| 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 | |
| 30 | #ifndef MONERO_DEVICE_TREZOR_H |
| 31 | #define MONERO_DEVICE_TREZOR_H |
| 32 | |
| 33 | #include "trezor.hpp" |
| 34 | #include "device/device.hpp" |
| 35 | |
| 36 | #ifdef WITH_DEVICE_TREZOR |
| 37 | #include <cstddef> |
| 38 | #include <string> |
| 39 | #include <boost/scope_exit.hpp> |
| 40 | #include <boost/thread/mutex.hpp> |
| 41 | #include <boost/thread/recursive_mutex.hpp> |
| 42 | |
| 43 | #include "device/device_default.hpp" |
| 44 | #include "device/device_cold.hpp" |
| 45 | #include "cryptonote_config.h" |
| 46 | #include "device_trezor_base.hpp" |
| 47 | #endif |
| 48 | |
| 49 | namespace hw { |
| 50 | namespace trezor { |
| 51 | |
| 52 | void register_all(); |
| 53 | void register_all(std::map<std::string, std::unique_ptr<device>> ®istry); |
| 54 | |
| 55 | #ifdef WITH_DEVICE_TREZOR |
| 56 | class device_trezor; |
| 57 | |
| 58 | /** |
| 59 | * Main device |
| 60 | */ |
| 61 | class device_trezor : public hw::trezor::device_trezor_base, public hw::device_cold { |
| 62 | protected: |
| 63 | std::atomic<bool> m_live_refresh_in_progress; |
| 64 | std::chrono::steady_clock::time_point m_last_live_refresh_time; |
| 65 | std::unique_ptr<boost::thread> m_live_refresh_thread; |
| 66 | std::atomic<bool> m_live_refresh_thread_running; |
| 67 | bool m_live_refresh_enabled; |
| 68 | size_t m_num_transations_to_sign; |
| 69 | |
| 70 | unsigned client_version(); |
| 71 | void transaction_versions_check(const ::tools::wallet2::unsigned_tx_set & unsigned_tx, hw::tx_aux_data & aux_data); |
| 72 | void transaction_pre_check(std::shared_ptr<messages::monero::MoneroTransactionInitRequest> init_msg); |
| 73 | void transaction_check(const protocol::tx::TData & tdata, const hw::tx_aux_data & aux_data); |
| 74 | void device_state_initialize_unsafe() override; |
| 75 | void live_refresh_start_unsafe(); |
| 76 | void live_refresh_finish_unsafe(); |
| 77 | void live_refresh_thread_main(); |
| 78 | |
| 79 | /** |
| 80 | * Signs particular transaction idx in the unsigned set, keeps state in the signer |
| 81 | */ |
| 82 | virtual void tx_sign(wallet_shim * wallet, |
| 83 | const ::tools::wallet2::unsigned_tx_set & unsigned_tx, |
| 84 | size_t idx, |
| 85 | hw::tx_aux_data & aux_data, |
| 86 | std::shared_ptr<protocol::tx::Signer> & signer); |
| 87 | |
| 88 | public: |
| 89 | device_trezor(); |
| 90 | virtual ~device_trezor() override; |
| 91 | |
| 92 | device_trezor(const device_trezor &device) = delete ; |
| 93 | device_trezor& operator=(const device_trezor &device) = delete; |
| 94 | |
| 95 | explicit operator bool() const override {return true;} |
| 96 | |
| 97 | bool init() override; |
| 98 | bool release() override; |
| 99 | bool disconnect() override; |
| 100 | |
| 101 | device_protocol_t device_protocol() const override { return PROTOCOL_COLD; }; |
| 102 | |
| 103 | bool has_ki_cold_sync() const override { return true; } |
| 104 | bool has_tx_cold_sign() const override { return true; } |
| 105 | void set_network_type(cryptonote::network_type network_type) override { this->network_type = network_type; } |
| 106 | void set_live_refresh_enabled(bool enabled) { m_live_refresh_enabled = enabled; } |
| 107 | bool live_refresh_enabled() const { return m_live_refresh_enabled; } |
| 108 | |
| 109 | /* ======================================================================= */ |
| 110 | /* WALLET & ADDRESS */ |
| 111 | /* ======================================================================= */ |
| 112 | bool get_public_address(cryptonote::account_public_address &pubkey) override; |
| 113 | bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override; |
| 114 | void display_address(const cryptonote::subaddress_index& index, const boost::optional<crypto::hash8> &payment_id) override; |
| 115 | |
| 116 | /* ======================================================================= */ |
| 117 | /* TREZOR PROTOCOL */ |
| 118 | /* ======================================================================= */ |
| 119 | |
| 120 | /** |
| 121 | * Get address. Throws. |
| 122 | */ |
| 123 | std::shared_ptr<messages::monero::MoneroAddress> get_address( |
| 124 | const boost::optional<cryptonote::subaddress_index> & subaddress = boost::none, |
| 125 | const boost::optional<crypto::hash8> & payment_id = boost::none, |
| 126 | bool show_address = false, |
| 127 | const boost::optional<std::vector<uint32_t>> & path = boost::none, |
| 128 | const boost::optional<cryptonote::network_type> & network_type = boost::none); |
| 129 | |
| 130 | /** |
| 131 | * Get watch key from device. Throws. |
| 132 | */ |
| 133 | std::shared_ptr<messages::monero::MoneroWatchKey> get_view_key( |
| 134 | const boost::optional<std::vector<uint32_t>> & path = boost::none, |
| 135 | const boost::optional<cryptonote::network_type> & network_type = boost::none); |
| 136 | |
| 137 | /** |
| 138 | * Get_tx_key support check |
| 139 | */ |
| 140 | bool is_get_tx_key_supported() const override; |
| 141 | |
| 142 | /** |
| 143 | * Loads tx aux data |
| 144 | */ |
| 145 | void load_tx_key_data(::hw::device_cold::tx_key_data_t & res, const std::string & tx_aux_data) override; |
| 146 | |
| 147 | /** |
| 148 | * TX key load with the Trezor |
| 149 | */ |
| 150 | void get_tx_key( |
| 151 | std::vector<::crypto::secret_key> & tx_keys, |
| 152 | const ::hw::device_cold::tx_key_data_t & tx_aux_data, |
| 153 | const ::crypto::secret_key & view_key_priv) override; |
| 154 | |
| 155 | /** |
| 156 | * Key image sync with the Trezor. |
| 157 | */ |
| 158 | void ki_sync(wallet_shim * wallet, |
| 159 | const std::vector<::tools::wallet2::transfer_details> & transfers, |
| 160 | hw::device_cold::exported_key_image & ski) override; |
| 161 | |
| 162 | bool is_live_refresh_supported() const override; |
| 163 | |
| 164 | bool is_live_refresh_enabled() const; |
| 165 | |
| 166 | bool has_ki_live_refresh() const override; |
| 167 | |
| 168 | void live_refresh_start() override; |
| 169 | |
| 170 | void live_refresh( |
| 171 | const ::crypto::secret_key & view_key_priv, |
| 172 | const crypto::public_key& out_key, |
| 173 | const crypto::key_derivation& recv_derivation, |
| 174 | size_t real_output_index, |
| 175 | const cryptonote::subaddress_index& received_index, |
| 176 | cryptonote::keypair& in_ephemeral, |
| 177 | crypto::key_image& ki |
| 178 | ) override; |
| 179 | |
| 180 | void live_refresh_finish() override; |
| 181 | |
| 182 | /** |
| 183 | * Letting device know the KI computation started / ended. |
| 184 | * During refresh |
| 185 | */ |
| 186 | void computing_key_images(bool started) override; |
| 187 | |
| 188 | /** |
| 189 | * Implements hw::device interface |
| 190 | * called from generate_key_image_helper_precomp() |
| 191 | */ |
| 192 | bool compute_key_image( |
| 193 | const ::cryptonote::account_keys& ack, |
| 194 | const ::crypto::public_key& out_key, |
| 195 | const ::crypto::key_derivation& recv_derivation, |
| 196 | size_t real_output_index, |
| 197 | const ::cryptonote::subaddress_index& received_index, |
| 198 | ::cryptonote::keypair& in_ephemeral, |
| 199 | ::crypto::key_image& ki) override; |
| 200 | |
| 201 | /** |
| 202 | * Signs unsigned transaction with the Trezor. |
| 203 | */ |
| 204 | void tx_sign(wallet_shim * wallet, |
| 205 | const ::tools::wallet2::unsigned_tx_set & unsigned_tx, |
| 206 | ::tools::wallet2::signed_tx_set & signed_tx, |
| 207 | hw::tx_aux_data & aux_data) override; |
| 208 | |
| 209 | /** |
| 210 | * Requests public address, uses empty passphrase if asked for. |
| 211 | */ |
| 212 | bool get_public_address_with_no_passphrase(cryptonote::account_public_address &pubkey) override; |
| 213 | |
| 214 | /** |
| 215 | * Reset session ID, restart with a new session. |
| 216 | */ |
| 217 | virtual void reset_session() override; |
| 218 | |
| 219 | /** |
| 220 | * Returns true if device already asked for passphrase entry before (i.e., obviously supports passphrase entry) |
| 221 | */ |
| 222 | bool seen_passphrase_entry_prompt() override; |
| 223 | |
| 224 | /** |
| 225 | * Uses empty passphrase for all passphrase queries. |
| 226 | */ |
| 227 | void set_use_empty_passphrase(bool use_always_empty_passphrase) override; |
| 228 | }; |
| 229 | |
| 230 | #endif |
| 231 | |
| 232 | } |
| 233 | } |
| 234 | #endif //MONERO_DEVICE_TREZOR_H |