mirror of
https://github.com/monero-project/monero.git
synced 2026-06-12 19:11:36 -07:00
wallet: roll back unlocker count on decrypt failure
This commit is contained in:
+12
-2
@@ -1121,10 +1121,20 @@ wallet_keys_unlocker::wallet_keys_unlocker(wallet2 &w, const epee::wipeable_stri
|
||||
w.generate_chacha_key_from_password(*password, key);
|
||||
|
||||
boost::lock_guard<boost::mutex> lock(lockers_lock);
|
||||
if (lockers_per_wallet[std::addressof(w)]++ > 0)
|
||||
wallet2* w_ptr = std::addressof(w);
|
||||
if (lockers_per_wallet[w_ptr]++ > 0)
|
||||
return;
|
||||
|
||||
w.decrypt_keys(key);
|
||||
try
|
||||
{
|
||||
w.decrypt_keys(key);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
if (--lockers_per_wallet[w_ptr] == 0)
|
||||
lockers_per_wallet.erase(w_ptr);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
wallet_keys_unlocker::~wallet_keys_unlocker()
|
||||
|
||||
@@ -595,3 +595,26 @@ TEST(wallet_keys_unlocker, first_not_locked)
|
||||
ASSERT_TRUE(verify_wallet_privkeys(w1));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wallet_keys_unlocker, construction_failure_rolls_back_lock_count)
|
||||
{
|
||||
const epee::wipeable_string password("correct horse battery staple");
|
||||
const epee::wipeable_string wrong_password("correct horse battery stable");
|
||||
|
||||
tools::wallet2 w;
|
||||
w.generate("", password);
|
||||
ASSERT_TRUE(w.is_key_encryption_enabled());
|
||||
ASSERT_FALSE(w.is_unattended());
|
||||
ASSERT_FALSE(verify_wallet_privkeys(w));
|
||||
|
||||
ASSERT_ANY_THROW({
|
||||
tools::wallet_keys_unlocker ul(w, &wrong_password);
|
||||
});
|
||||
ASSERT_FALSE(verify_wallet_privkeys(w));
|
||||
|
||||
{
|
||||
tools::wallet_keys_unlocker ul(w, &password);
|
||||
ASSERT_TRUE(verify_wallet_privkeys(w));
|
||||
}
|
||||
ASSERT_FALSE(verify_wallet_privkeys(w));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user