wallet2: avoid double-encrypting legacy keys
What changed, and why it matters
This change fixes a bug in the Monero wallet where legacy (older format) wallet keys could be encrypted twice during an upgrade, which could corrupt the wallet file or make it unusable. The fix removes an unnecessary encrypt/decrypt step and adds a test to ensure legacy wallets upgrade correctly and end up with the expected single layer of encryption.
Apply the patch and run the new unit test to confirm legacy wallet upgrades produce correctly encrypted keys. Users with legacy wallets should upgrade with this fix to avoid potential wallet corruption.
Security signals we found
Double-encryption of wallet keys could lead to wallet corruption or inaccessibility
Legacy wallet upgrade path affected
New unit test validates correct encryption state after upgrade
Evidence from the diff
In wallet2::load_keys(), when loading a legacy wallet whose keys need encryption, the code previously called encrypt_keys() before store_keys() and decrypt_keys() afterward when m_ask_password == AskPasswordToDecrypt and the wallet was not unattended or watch-only. Because store_keys() already encrypts keys internally, this resulted in double-encryption. The patch removes the explicit encrypt_keys()/decrypt_keys() pair, relying on store_keys() to handle encryption. A new unit test constructs legacy encrypted key files in both JSON and binary formats, loads them twice, and verifies the upgraded file contains encrypted_secret_keys: 1.
Changed components
src/wallet/wallet2.cpptests/unit_tests/wallet_storage.cppInspect captured patch +91 / −4
### src/wallet/wallet2.cpp
@@ -5175,16 +5175,12 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
// Rewrite with encrypted keys if unencrypted, ignore errors
if (r && keys_to_encrypt != boost::none)
{
- if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
- encrypt_keys(keys_to_encrypt.get());
bool saved_ret = store_keys(keys_file_name, password, m_watch_only);
if (!saved_ret)
{
// just moan a bit, but not fatal
MERROR("Error saving keys file with encrypted keys, not fatal");
}
- if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
- decrypt_keys(keys_to_encrypt.get());
m_keys_file_locker.reset();
}
return r;
### tests/unit_tests/wallet_storage.cpp
@@ -31,7 +31,12 @@
#include <cctype>
+#include <rapidjson/document.h>
+#include <rapidjson/stringbuffer.h>
+#include <rapidjson/writer.h>
+
#include "file_io_utils.h"
+#include "serialization/binary_utils.h"
#include "wallet/wallet2.h"
#include "common/util.h"
@@ -178,6 +183,92 @@ TEST(wallet_balance, batched_account_balances_match_individual_balances)
}
}
+TEST(wallet_storage, upgrade_legacy_unencrypted_keys)
+{
+ const path test_dir = temp_directory_path() / unique_path("monero-legacy-keys-%%%%-%%%%");
+ ASSERT_TRUE(create_directory(test_dir));
+ struct test_dir_cleanup
+ {
+ const path dir;
+ ~test_dir_cleanup()
+ {
+ boost::system::error_code ec;
+ remove_all(dir, ec);
+ }
+ } cleanup{test_dir};
+
+ const epee::wipeable_string password("test-password");
+ cryptonote::account_base account;
+ const crypto::secret_key spend_key = account.generate();
+ const crypto::secret_key view_key = account.get_keys().m_view_secret_key;
+
+ epee::byte_slice account_data;
+ ASSERT_TRUE(epee::serialization::store_t_to_binary(account, account_data));
+
+ crypto::chacha_key key;
+ crypto::generate_chacha_key(password.data(), password.size(), key, 1);
+ for (const bool json_format : {false, true})
+ {
+ const path wallet_file = test_dir / (json_format ? "json-wallet" : "v0-wallet");
+ std::string plaintext;
+ if (json_format)
+ {
+ rapidjson::Document json;
+ json.SetObject();
+ rapidjson::Value value;
+ value.SetString(reinterpret_cast<const char*>(account_data.data()), account_data.size(), json.GetAllocator());
+ json.AddMember("key_data", value, json.GetAllocator());
+ rapidjson::StringBuffer buffer;
+ rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+ json.Accept(writer);
+ plaintext.assign(buffer.GetString(), buffer.GetSize());
+ }
+ else
+ plaintext.assign(reinterpret_cast<const char*>(account_data.data()), account_data.size());
+
+ tools::wallet2::keys_file_data legacy_keys;
+ legacy_keys.iv = crypto::rand<crypto::chacha_iv>();
+ legacy_keys.account_data.resize(plaintext.size());
+ if (json_format)
+ crypto::chacha20(plaintext.data(), plaintext.size(), key, legacy_keys.iv, &legacy_keys.account_data[0]);
+ else
+ crypto::chacha8(plaintext.data(), plaintext.size(), key, legacy_keys.iv, &legacy_keys.account_data[0]);
+
+ std::string keys_buf;
+ ASSERT_TRUE(serialization::dump_binary(legacy_keys, keys_buf));
+ ASSERT_TRUE(save_string_to_file(wallet_file.string() + ".keys", keys_buf));
+
+ for (int load = 0; load < 2; ++load)
+ {
+ {
+ tools::wallet2 wallet;
+ wallet.load(wallet_file.string(), password);
+ tools::wallet_keys_unlocker unlocker(wallet, &password);
+ EXPECT_TRUE(wallet.get_account().get_keys().m_spend_secret_key == spend_key);
+ EXPECT_TRUE(wallet.get_account().get_keys().m_view_secret_key == view_key);
+ }
+
+ if (load == 0)
+ {
+ std::string saved_buf;
+ ASSERT_TRUE(load_file_to_string(wallet_file.string() + ".keys", saved_buf));
+ tools::wallet2::keys_file_data saved_keys;
+ ASSERT_TRUE(serialization::parse_binary(saved_buf, saved_keys));
+ ASSERT_FALSE(saved_keys.account_data.empty());
+ std::string saved_json(saved_keys.account_data.size(), '\0');
+ crypto::chacha20(saved_keys.account_data.data(), saved_keys.account_data.size(), key,
+ saved_keys.iv, &saved_json[0]);
+ rapidjson::Document upgraded;
+ ASSERT_FALSE(upgraded.Parse(saved_json.c_str()).HasParseError());
+ ASSERT_TRUE(upgraded.IsObject());
+ ASSERT_TRUE(upgraded.HasMember("encrypted_secret_keys"));
+ ASSERT_TRUE(upgraded["encrypted_secret_keys"].IsInt());
+ EXPECT_EQ(1, upgraded["encrypted_secret_keys"].GetInt());
+ }
+ }
+ }
+}
+
TEST(wallet_storage, store_to_file2file)
{
const path source_wallet_file = unit_test::data_dir / "wallet_00fd416a";Why this scored 56/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.