Skip to content

Commit 6c80323

Browse files
authored
Merge pull request #80 from stoffu/aeon-unlock-keysfile-before-verify
wallet2: unlock keys file before calling verify_password (needed for …
2 parents 7c4f9e4 + 810b535 commit 6c80323

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

src/wallet/api/wallet.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,20 @@ void WalletImpl::keyReuseMitigation2(bool mitigation)
22062206
m_wallet->key_reuse_mitigation2(mitigation);
22072207
}
22082208

2209+
bool WalletImpl::lockKeysFile()
2210+
{
2211+
return m_wallet->lock_keys_file();
2212+
}
2213+
2214+
bool WalletImpl::unlockKeysFile()
2215+
{
2216+
return m_wallet->unlock_keys_file();
2217+
}
2218+
2219+
bool WalletImpl::isKeysFileLocked()
2220+
{
2221+
return m_wallet->is_keys_file_locked();
2222+
}
22092223
} // namespace
22102224

22112225
namespace Bitmonero = Monero;

src/wallet/api/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class WalletImpl : public Wallet
188188
virtual void segregatePreForkOutputs(bool segregate) override;
189189
virtual void segregationHeight(uint64_t height) override;
190190
virtual void keyReuseMitigation2(bool mitigation) override;
191+
virtual bool lockKeysFile() override;
192+
virtual bool unlockKeysFile() override;
193+
virtual bool isKeysFileLocked() override;
191194

192195
private:
193196
void clearStatus() const;

src/wallet/api/wallet2_api.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,12 @@ struct Wallet
900900

901901
//! Initiates a light wallet import wallet request
902902
virtual bool lightWalletImportWalletRequest(std::string &payment_id, uint64_t &fee, bool &new_request, bool &request_fulfilled, std::string &payment_address, std::string &status) = 0;
903+
904+
//! locks/unlocks the keys file; returns true on success
905+
virtual bool lockKeysFile() = 0;
906+
virtual bool unlockKeysFile() = 0;
907+
//! returns true if the keys file is locked
908+
virtual bool isKeysFileLocked() = 0;
903909
};
904910

905911
/**
@@ -1070,6 +1076,10 @@ struct WalletManager
10701076
* @param password - password to verify
10711077
* @param no_spend_key - verify only view keys?
10721078
* @return - true if password is correct
1079+
*
1080+
* @note
1081+
* This function will fail when the wallet keys file is opened because the wallet program locks the keys file.
1082+
* In this case, Wallet::unlockKeysFile() and Wallet::lockKeysFile() need to be called before and after the call to this function, respectively.
10731083
*/
10741084
virtual bool verifyWalletPassword(const std::string &keys_file_name, const std::string &password, bool no_spend_key) const = 0;
10751085

src/wallet/wallet2.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,7 +2688,7 @@ void wallet2::detach_blockchain(uint64_t height)
26882688
bool wallet2::deinit()
26892689
{
26902690
m_is_initialized=false;
2691-
m_keys_file_locker.reset();
2691+
unlock_keys_file();
26922692
return true;
26932693
}
26942694
//----------------------------------------------------------------------------------------------------
@@ -2858,12 +2858,12 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
28582858
crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.iv, &cipher[0]);
28592859
keys_file_data.account_data = cipher;
28602860

2861-
m_keys_file_locker.reset();
2861+
unlock_keys_file();
28622862
std::string buf;
28632863
r = ::serialization::dump_binary(keys_file_data, buf);
28642864
r = r && epee::file_io_utils::save_string_to_file(keys_file_name, buf); //and never touch wallet_keys_file again, only read
28652865
CHECK_AND_ASSERT_MES(r, false, "failed to generate wallet keys file " << keys_file_name);
2866-
m_keys_file_locker.reset(new tools::file_locker(m_keys_file));
2866+
lock_keys_file();
28672867

28682868
return true;
28692869
}
@@ -3087,9 +3087,13 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
30873087
* can be used prior to rewriting wallet keys file, to ensure user has entered the correct password
30883088
*
30893089
*/
3090-
bool wallet2::verify_password(const epee::wipeable_string& password) const
3090+
bool wallet2::verify_password(const epee::wipeable_string& password)
30913091
{
3092-
return verify_password(m_keys_file, password, m_watch_only || m_multisig, m_account.get_device());
3092+
// this temporary unlocking is necessary for Windows (otherwise the file couldn't be loaded).
3093+
unlock_keys_file();
3094+
bool r = verify_password(m_keys_file, password, m_watch_only || m_multisig, m_account.get_device());
3095+
lock_keys_file();
3096+
return r;
30933097
}
30943098

30953099
/*!
@@ -3996,17 +4000,17 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass
39964000
boost::system::error_code e;
39974001
bool exists = boost::filesystem::exists(m_keys_file, e);
39984002
THROW_WALLET_EXCEPTION_IF(e || !exists, error::file_not_found, m_keys_file);
3999-
m_keys_file_locker.reset(new tools::file_locker(m_keys_file));
4000-
THROW_WALLET_EXCEPTION_IF(!m_keys_file_locker->locked(), error::wallet_internal_error, "internal error: \"" + m_keys_file + "\" is opened by another wallet program");
4003+
lock_keys_file();
4004+
THROW_WALLET_EXCEPTION_IF(!is_keys_file_locked(), error::wallet_internal_error, "internal error: \"" + m_keys_file + "\" is opened by another wallet program");
40014005

40024006
// this temporary unlocking is necessary for Windows (otherwise the file couldn't be loaded).
4003-
m_keys_file_locker.reset();
4007+
unlock_keys_file();
40044008
if (!load_keys(m_keys_file, password))
40054009
{
40064010
THROW_WALLET_EXCEPTION_IF(true, error::file_read_error, m_keys_file);
40074011
}
40084012
LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str(m_nettype));
4009-
m_keys_file_locker.reset(new tools::file_locker(m_keys_file));
4013+
lock_keys_file();
40104014

40114015
//keys loaded ok!
40124016
//try to load wallet file. but even if we failed, it is not big problem
@@ -6047,6 +6051,33 @@ bool wallet2::is_output_blackballed(const crypto::public_key &output) const
60476051
catch (const std::exception &e) { return false; }
60486052
}
60496053

6054+
bool wallet2::lock_keys_file()
6055+
{
6056+
if (m_keys_file_locker)
6057+
{
6058+
MDEBUG(m_keys_file << " is already locked.");
6059+
return false;
6060+
}
6061+
m_keys_file_locker.reset(new tools::file_locker(m_keys_file));
6062+
return true;
6063+
}
6064+
6065+
bool wallet2::unlock_keys_file()
6066+
{
6067+
if (!m_keys_file_locker)
6068+
{
6069+
MDEBUG(m_keys_file << " is already unlocked.");
6070+
return false;
6071+
}
6072+
m_keys_file_locker.reset();
6073+
return true;
6074+
}
6075+
6076+
bool wallet2::is_keys_file_locked() const
6077+
{
6078+
return m_keys_file_locker->locked();
6079+
}
6080+
60506081
bool wallet2::tx_add_fake_output(std::vector<std::vector<tools::wallet2::get_outs_entry>> &outs, uint64_t global_index, const crypto::public_key& output_public_key, const rct::key& mask, uint64_t real_index, bool unlocked) const
60516082
{
60526083
if (!unlocked) // don't add locked outs

src/wallet/wallet2.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ namespace tools
609609
/*!
610610
* \brief verifies given password is correct for default wallet keys file
611611
*/
612-
bool verify_password(const epee::wipeable_string& password) const;
612+
bool verify_password(const epee::wipeable_string& password);
613613
cryptonote::account_base& get_account(){return m_account;}
614614
const cryptonote::account_base& get_account()const{return m_account;}
615615

@@ -1146,6 +1146,9 @@ namespace tools
11461146
bool unblackball_output(const crypto::public_key &output);
11471147
bool is_output_blackballed(const crypto::public_key &output) const;
11481148

1149+
bool lock_keys_file();
1150+
bool unlock_keys_file();
1151+
bool is_keys_file_locked() const;
11491152
private:
11501153
/*!
11511154
* \brief Stores wallet information to wallet file.

0 commit comments

Comments
 (0)