fix(wallet-switch): cluster A — identity/secret teardown on wallet switch

From the wallet-switching audit (HIGH-severity cross-wallet leaks):

- Chat identity leak: the full-node switch (switchToWallet) and seed-migration
  adopt reset state_ but NOT the HushChat identity, so wallet A's decrypted
  conversations surfaced under wallet B and outgoing chat was signed with A's
  keypair. Factor the existing teardown into App::resetChatSession() and call it
  on both wallet-change paths (the lite path already reset it via
  rebuildLiteWallet, which now uses the helper too).
- Global PIN vault: the PIN quick-unlock vault was a single vault.dat, so after
  a switch wallet A's stored passphrase was offered/applied to encrypted wallet
  B. SecureVault is now scoped per wallet (vault-<walletfile>.dat); the default
  wallet keeps the legacy vault.dat for back-compat. vault_ is constructed for
  the active wallet and re-scoped on switch, so B has its own (empty) vault.
- Lock-screen state: switching now clears the carried-over failed-attempt
  counter + lockout timer and secure-zeroes the passphrase/PIN entry buffers so
  the previous wallet's unlock state can't apply to the new one.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-15 12:29:45 -05:00
parent 6f0f95f4fb
commit f546d3e2b1
5 changed files with 67 additions and 21 deletions

View File

@@ -9,6 +9,7 @@
#include <fstream>
#include <filesystem>
#include <cctype>
#include <cstring>
#include <algorithm>
#include "../util/logger.h"
@@ -31,18 +32,31 @@ static constexpr uint8_t VAULT_VERSION = 0x01;
static constexpr unsigned long long ARGON2_MEMLIMIT = crypto_pwhash_MEMLIMIT_MODERATE;
static constexpr unsigned long long ARGON2_OPSLIMIT = crypto_pwhash_OPSLIMIT_MODERATE;
SecureVault::SecureVault() {
SecureVault::SecureVault(const std::string& walletFile) {
// Ensure libsodium is initialized
if (sodium_init() < 0) {
// sodium_init returns 0 on success, 1 if already initialized, -1 on failure
// We'll proceed anyway — the functions will fail gracefully
}
setWalletScope(walletFile);
}
SecureVault::~SecureVault() = default;
std::string SecureVault::getVaultPath() {
return (fs::path(Platform::getConfigDir()) / "vault.dat").string();
void SecureVault::setWalletScope(const std::string& walletFile) {
// The default wallet keeps the legacy global "vault.dat" (empty scope) for backward compatibility;
// any other wallet gets a filesystem-safe per-wallet tag so its vault is separate.
if (walletFile.empty() || walletFile == "wallet.dat") { scope_.clear(); return; }
std::string s;
s.reserve(walletFile.size());
for (unsigned char c : walletFile)
s += (std::isalnum(c) || c == '-' || c == '_') ? static_cast<char>(c) : '_';
scope_ = s;
}
std::string SecureVault::getVaultPath() const {
const std::string name = scope_.empty() ? std::string("vault.dat") : ("vault-" + scope_ + ".dat");
return (fs::path(Platform::getConfigDir()) / name).string();
}
bool SecureVault::hasVault() const {

View File

@@ -29,9 +29,15 @@ namespace util {
*/
class SecureVault {
public:
SecureVault();
// `walletFile` scopes the vault to a specific wallet.dat so one wallet's PIN passphrase is never
// offered for another. The default wallet ("wallet.dat" / empty) keeps the legacy global vault.dat
// path for backward compatibility; other wallets get their own vault-<scope>.dat.
explicit SecureVault(const std::string& walletFile = "");
~SecureVault();
// Re-scope to a different wallet (called when the active wallet changes).
void setWalletScope(const std::string& walletFile);
/**
* @brief Check if a vault file exists on disk
*/
@@ -78,15 +84,17 @@ public:
static void secureZero(void* ptr, size_t len);
/**
* @brief Get the vault file path
* @brief Get this vault's file path (scoped to its wallet)
*/
static std::string getVaultPath();
std::string getVaultPath() const;
private:
// Derive a 32-byte key from PIN + salt using Argon2id
bool deriveKey(const std::string& pin,
const uint8_t* salt, size_t saltLen,
uint8_t* key, size_t keyLen) const;
std::string scope_; // "" = default/legacy wallet (vault.dat); else a sanitized wallet-file tag
};
} // namespace util