wallet: actually open the wallet file, and remember its passphrase
`monero-wallet-rpc` starts with no wallet open and keeps one only for the life of its process, so a restart, a Lock/unlock or a wallet re-point left a live child answering `-13 No wallet file` to every read while the file sat on disk. The app now opens the configured catalogue file itself (`Wallet::open_wallet`, over RPC — never `--wallet-file`/`--password` on argv, which `ps` exposes) after the child is up, and keeps the passphrase that does it, sealed with the store data key beside the RPC creds under its own AAD. - core: `Wallet::open_wallet`, `WalletConfig::wallet_passphrase` (+ `with_wallet_passphrase`, redacted Debug, kept out of the endpoint config), `Error::is_unreachable` for the one retryable class (the port binds a moment after spawn; a timeout is not a start in progress). - store: `wallet_config.sealed_passphrase` column (additive, AAD `onionwire-store-v1:wallet-passphrase`), read-modify-written so the endpoint/login survive; `wallet_save_config` keeps the stored one. - node: `start_wallet_child` opens the file, `open_wallet_with_passphrase` opens a file with a passphrase the user just typed and seals it only once a live daemon accepted it, `wallet_status` grows `wallet_open` and `needs_passphrase`. A child the RPC refuses is stopped rather than left inert; an install with no stored passphrase still starts, and says so. - tui: one-shot prompt (`WalletSetup::UnlockPass`) on a wallet that needs it, reachable from the saved-wallet picker and from `s`. - sdk: `openWalletWithPassphrase`, the two status fields, README flow. - docs: WALLET/THREAT_MODEL/README — the passphrase is kept, and the consequence (the store passphrase is sufficient to spend) is stated rather than implied. Tests: 4 new node tests drive a real child lifecycle against a loopback wallet RPC (open on restart, no open without a passphrase, refused passphrase, sealed-on-success), a `tests/wallet.rs` test asserts the bytes sent (`open_wallet` + filename + password), and `tests/store.rs` covers the sealed column and the plaintext->encrypted upgrade with it set. Mutation-checked: stubbing the two open paths fails all four.
This commit is contained in:
parent
c5725856ad
commit
ed29131a67
12 changed files with 1262 additions and 54 deletions
84
src/store.rs
84
src/store.rs
|
|
@ -294,13 +294,17 @@ impl Store {
|
|||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
-- Remote-mode wallet config the user set in-app (F7): the endpoint,
|
||||
-- the login, and the view-only flag. One row; the login is sealed
|
||||
-- with the store data key (see `save_wallet_config`).
|
||||
-- the login, the view-only flag, and the passphrase that opens the
|
||||
-- wallet file the child is pointed at. One row; the login and the
|
||||
-- passphrase are each sealed with the store data key under their
|
||||
-- own AAD (see `save_wallet_config`), so one ciphertext cannot be
|
||||
-- swapped for the other.
|
||||
CREATE TABLE IF NOT EXISTS wallet_config (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
rpc_url TEXT NOT NULL,
|
||||
sealed_login BLOB NOT NULL,
|
||||
no_spend INTEGER NOT NULL,
|
||||
sealed_passphrase BLOB,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
-- The wallet catalogue: one row per wallet file this install knows
|
||||
|
|
@ -410,26 +414,49 @@ impl Store {
|
|||
|
||||
/// Persist the remote-mode wallet config the F7 screen saved. One row,
|
||||
/// replaced in place; an empty `rpc_url` clears it (that is how a user
|
||||
/// turns the stored wallet off again).
|
||||
/// turns the stored wallet off again) — unless a wallet **passphrase** is
|
||||
/// remembered in the row, which the endpoint has nothing to do with and
|
||||
/// must survive: it belongs to the wallet file, not to the RPC endpoint.
|
||||
///
|
||||
/// The login is `user:pass` for a wallet that can spend, so it is sealed
|
||||
/// with the store data key on top of the whole-db SQLCipher layer — the
|
||||
/// same discipline as the wallet-child creds.
|
||||
/// The login is `user:pass` for a wallet that can spend and the passphrase
|
||||
/// opens the wallet file, so both are sealed with the store data key on top
|
||||
/// of the whole-db SQLCipher layer — the same discipline as the wallet-child
|
||||
/// creds — and under separate AADs, so neither ciphertext can be replayed
|
||||
/// into the other's column.
|
||||
pub fn save_wallet_config(&self, cfg: &crate::wallet::WalletConfig) -> Result<()> {
|
||||
if cfg.rpc_url.trim().is_empty() {
|
||||
if cfg.rpc_url.trim().is_empty() && cfg.wallet_passphrase.is_empty() {
|
||||
self.conn.execute("DELETE FROM wallet_config", [])?;
|
||||
return Ok(());
|
||||
}
|
||||
let sealed =
|
||||
crate::backup::aead_encrypt(&self.msg_key, cfg.rpc_login.as_bytes(), WALLET_CONFIG_AAD)
|
||||
.map_err(Error)?;
|
||||
let sealed_pass = if cfg.wallet_passphrase.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(
|
||||
crate::backup::aead_encrypt(
|
||||
&self.msg_key,
|
||||
cfg.wallet_passphrase.as_bytes(),
|
||||
WALLET_PASSPHRASE_AAD,
|
||||
)
|
||||
.map_err(Error)?,
|
||||
)
|
||||
};
|
||||
self.conn.execute(
|
||||
"INSERT INTO wallet_config (id, rpc_url, sealed_login, no_spend, updated_at)
|
||||
VALUES (1, ?1, ?2, ?3, ?4)
|
||||
"INSERT INTO wallet_config (id, rpc_url, sealed_login, no_spend, sealed_passphrase, updated_at)
|
||||
VALUES (1, ?1, ?2, ?3, ?4, ?5)
|
||||
ON CONFLICT(id) DO UPDATE SET rpc_url = excluded.rpc_url,
|
||||
sealed_login = excluded.sealed_login, no_spend = excluded.no_spend,
|
||||
sealed_passphrase = excluded.sealed_passphrase,
|
||||
updated_at = excluded.updated_at",
|
||||
params![cfg.rpc_url.trim(), sealed, cfg.no_spend as i64, unix_now()],
|
||||
params![
|
||||
cfg.rpc_url.trim(),
|
||||
sealed,
|
||||
cfg.no_spend as i64,
|
||||
sealed_pass,
|
||||
unix_now()
|
||||
],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -437,27 +464,42 @@ impl Store {
|
|||
/// Read the stored remote-mode config, or `None` when none was ever saved.
|
||||
/// A blob that does not decrypt is an error, never a guess.
|
||||
pub fn load_wallet_config(&self) -> Result<Option<crate::wallet::WalletConfig>> {
|
||||
let row: Option<(String, Vec<u8>, i64)> = self
|
||||
type WalletConfigRow = (String, Vec<u8>, i64, Option<Vec<u8>>);
|
||||
let row: Option<WalletConfigRow> = self
|
||||
.conn
|
||||
.query_row(
|
||||
"SELECT rpc_url, sealed_login, no_spend FROM wallet_config WHERE id = 1",
|
||||
"SELECT rpc_url, sealed_login, no_spend, sealed_passphrase
|
||||
FROM wallet_config WHERE id = 1",
|
||||
[],
|
||||
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
|
||||
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let Some((rpc_url, sealed, no_spend)) = row else {
|
||||
let Some((rpc_url, sealed, no_spend, sealed_pass)) = row else {
|
||||
return Ok(None);
|
||||
};
|
||||
let login = crate::backup::aead_decrypt(&self.msg_key, &sealed, WALLET_CONFIG_AAD)
|
||||
.map_err(|_| Error("wallet config decrypt failed".into()))?;
|
||||
// The login is a secret: it must not survive in a bare `String`.
|
||||
// Both are secrets: neither may survive in a bare `String`.
|
||||
let login = Zeroizing::new(
|
||||
String::from_utf8(login).map_err(|_| Error("wallet config is not utf-8".into()))?,
|
||||
);
|
||||
let wallet_passphrase = match sealed_pass {
|
||||
None => Zeroizing::new(String::new()),
|
||||
Some(blob) => {
|
||||
let plain =
|
||||
crate::backup::aead_decrypt(&self.msg_key, &blob, WALLET_PASSPHRASE_AAD)
|
||||
.map_err(|_| Error("wallet passphrase decrypt failed".into()))?;
|
||||
Zeroizing::new(
|
||||
String::from_utf8(plain)
|
||||
.map_err(|_| Error("wallet passphrase is not utf-8".into()))?,
|
||||
)
|
||||
}
|
||||
};
|
||||
Ok(Some(crate::wallet::WalletConfig {
|
||||
rpc_url,
|
||||
rpc_login: login,
|
||||
no_spend: no_spend != 0,
|
||||
wallet_passphrase,
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
@ -595,6 +637,14 @@ impl Store {
|
|||
"hs_nickname",
|
||||
"ALTER TABLE self ADD COLUMN hs_nickname TEXT NOT NULL DEFAULT 'ow0'",
|
||||
)?;
|
||||
// The wallet file's passphrase (sealed). NULL on an install that has
|
||||
// never been told one — a wallet created before this column, or a
|
||||
// remote-mode wallet the user opens themselves.
|
||||
self.add_column_if_missing(
|
||||
"wallet_config",
|
||||
"sealed_passphrase",
|
||||
"ALTER TABLE wallet_config ADD COLUMN sealed_passphrase BLOB",
|
||||
)?;
|
||||
// Opaque send payload for rows that cannot re-derive their bytes (a
|
||||
// chat send's ratchet frame). NULL means "send the message body", the
|
||||
// pre-payload behaviour, so locator/profile/payment rows are unchanged.
|
||||
|
|
@ -1813,6 +1863,10 @@ const WALLET_CHILD_AAD: &[u8] = b"onionwire/wallet-child/v1";
|
|||
/// AAD for the sealed remote-mode wallet config row. Same reasoning as the
|
||||
/// wallet-child AAD: one store key, many domains.
|
||||
const WALLET_CONFIG_AAD: &[u8] = b"onionwire/wallet-config/v1";
|
||||
/// The wallet-file passphrase's own AAD: same key and construction as the
|
||||
/// login, a different binding, so the two sealed columns are not
|
||||
/// interchangeable.
|
||||
const WALLET_PASSPHRASE_AAD: &[u8] = b"onionwire/wallet-passphrase/v1";
|
||||
|
||||
fn fingerprint(pubkey: &[u8]) -> String {
|
||||
pubkey.iter().map(|b| format!("{b:02x}")).collect()
|
||||
|
|
|
|||
Loading…
Reference in a new issue