feat(store): encrypt chat bodies at rest; Ctrl-Q CLEAR/QUIT
All checks were successful
ci / test (pull_request) Successful in 3m13s
All checks were successful
ci / test (pull_request) Successful in 3m13s
Wrap a random 32-byte message key with Argon2id+ChaCha20-Poly1305. sqlite messages.plaintext is nonce||ciphertext; identity keys stay plaintext. Empty/wrong passphrase fails closed. Ctrl-Q types CLEAR or QUIT then Enter (Esc cancels).
This commit is contained in:
parent
1d7f8e1f35
commit
6323619d80
16 changed files with 461 additions and 35 deletions
|
|
@ -57,7 +57,7 @@ fn garbage_file_fails() {
|
|||
#[test]
|
||||
fn restore_overwrites_self_keys_friends_stay() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at(dir.path()).expect("open");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "onionwire-test").expect("open");
|
||||
store
|
||||
.upsert_friend(&[9u8; 32], "alice.onion", Some("alice"))
|
||||
.unwrap();
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use rand::rngs::OsRng;
|
|||
|
||||
fn store() -> (tempfile::TempDir, Store) {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at(dir.path()).expect("open");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "onionwire-test").expect("open");
|
||||
(dir, store)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use rand::rngs::OsRng;
|
|||
|
||||
fn store() -> (tempfile::TempDir, Store) {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at(dir.path()).expect("open");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "onionwire-test").expect("open");
|
||||
(dir, store)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
|
||||
use onionwire::qr;
|
||||
use onionwire::Store;
|
||||
use onionwire::qr;
|
||||
|
||||
static ENV_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
|
|
@ -18,6 +18,7 @@ impl TempHome {
|
|||
let guard = ENV_LOCK.lock().expect("env lock");
|
||||
unsafe {
|
||||
std::env::set_var("ONIONWIRE_HOME", dir.path());
|
||||
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "onionwire-test");
|
||||
}
|
||||
Self {
|
||||
_dir: dir,
|
||||
|
|
@ -30,6 +31,7 @@ impl Drop for TempHome {
|
|||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
std::env::remove_var("ONIONWIRE_HOME");
|
||||
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ fn pk(tag: u8) -> [u8; 32] {
|
|||
|
||||
fn store() -> (tempfile::TempDir, Store) {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at(dir.path()).expect("open");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "onionwire-test").expect("open");
|
||||
(dir, store)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ async fn rotate_changes_onion_not_identity_and_notifies_online_peer() {
|
|||
let carol_home = root.path().join("carol");
|
||||
|
||||
eprintln!("starting alice + bob…");
|
||||
unsafe {
|
||||
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "onionwire-test");
|
||||
}
|
||||
let (alice, bob) = tokio::join!(
|
||||
Node::start(alice_home.clone()),
|
||||
Node::start(bob_home.clone())
|
||||
|
|
@ -35,7 +38,7 @@ async fn rotate_changes_onion_not_identity_and_notifies_online_peer() {
|
|||
alice.add_friend_from_qr(&b_qr).expect("alice adds bob");
|
||||
bob.add_friend_from_qr(&a_qr).expect("bob adds alice");
|
||||
|
||||
let carol = Store::open_at(&carol_home).expect("carol store");
|
||||
let carol = Store::open_at_with_passphrase(&carol_home, "onionwire-test").expect("carol store");
|
||||
let a_pk = alice.identity_pk();
|
||||
carol
|
||||
.upsert_friend(&a_pk, &alice.onion(), Some("alice"))
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ impl TempHome {
|
|||
let guard = ENV_LOCK.lock().expect("env lock");
|
||||
unsafe {
|
||||
std::env::set_var("ONIONWIRE_HOME", dir.path());
|
||||
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "onionwire-test");
|
||||
}
|
||||
Self { dir, _guard: guard }
|
||||
}
|
||||
|
|
@ -33,6 +34,7 @@ impl Drop for TempHome {
|
|||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
std::env::remove_var("ONIONWIRE_HOME");
|
||||
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -121,8 +123,8 @@ fn unknown_pubkey_inserts_new_row() {
|
|||
fn open_at_two_homes_are_independent() {
|
||||
let a = tempfile::tempdir().expect("a");
|
||||
let b = tempfile::tempdir().expect("b");
|
||||
let sa = onionwire::Store::open_at(a.path()).expect("open a");
|
||||
let sb = onionwire::Store::open_at(b.path()).expect("open b");
|
||||
let sa = onionwire::Store::open_at_with_passphrase(a.path(), "a-pass").expect("open a");
|
||||
let sb = onionwire::Store::open_at_with_passphrase(b.path(), "b-pass").expect("open b");
|
||||
let ia = sa.self_identity().unwrap();
|
||||
let ib = sb.self_identity().unwrap();
|
||||
assert_ne!(ia.identity_pk, ib.identity_pk);
|
||||
|
|
@ -167,7 +169,7 @@ fn friend_prekey_lookup() {
|
|||
#[test]
|
||||
fn open_uses_wal_journal() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at(dir.path()).expect("open");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "onionwire-test").expect("open");
|
||||
assert_eq!(store.journal_mode().unwrap().to_ascii_lowercase(), "wal");
|
||||
}
|
||||
|
||||
|
|
@ -175,5 +177,87 @@ fn open_uses_wal_journal() {
|
|||
fn corrupt_db_fails_closed() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
std::fs::write(dir.path().join("onionwire.db"), b"not a sqlite database").unwrap();
|
||||
assert!(Store::open_at(dir.path()).is_err());
|
||||
assert!(Store::open_at_with_passphrase(dir.path(), "onionwire-test").is_err());
|
||||
}
|
||||
|
||||
fn home_contains_bytes(home: &Path, needle: &[u8]) -> bool {
|
||||
let Ok(rd) = fs::read_dir(home) else {
|
||||
return false;
|
||||
};
|
||||
for ent in rd.flatten() {
|
||||
let name = ent.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
if !name.starts_with("onionwire.db") {
|
||||
continue;
|
||||
}
|
||||
let Ok(bytes) = fs::read(ent.path()) else {
|
||||
continue;
|
||||
};
|
||||
if bytes.windows(needle.len()).any(|w| w == needle) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_passphrase_is_rejected() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let err = match Store::open_at_with_passphrase(dir.path(), "") {
|
||||
Ok(_) => panic!("empty passphrase should fail"),
|
||||
Err(e) => e,
|
||||
};
|
||||
assert!(
|
||||
err.to_string().to_ascii_lowercase().contains("empty"),
|
||||
"got {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn append_message_db_file_does_not_contain_plaintext() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "correct-horse").expect("open");
|
||||
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
||||
let needle = b"needle-plaintext-xyzzy-at-rest";
|
||||
store.append_message(&pk(1), "out", needle).unwrap();
|
||||
let msgs = store.list_messages(&pk(1)).unwrap();
|
||||
assert_eq!(msgs.len(), 1);
|
||||
assert_eq!(msgs[0].plaintext, needle);
|
||||
drop(store);
|
||||
assert!(
|
||||
!home_contains_bytes(dir.path(), needle),
|
||||
"sqlite files must not contain chat plaintext"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_passphrase_cannot_open_or_list() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "right-pass").expect("open");
|
||||
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
||||
store
|
||||
.append_message(&pk(1), "in", b"secret-chat-body")
|
||||
.unwrap();
|
||||
drop(store);
|
||||
let err = match Store::open_at_with_passphrase(dir.path(), "wrong-pass") {
|
||||
Ok(_) => panic!("wrong passphrase should fail"),
|
||||
Err(e) => e,
|
||||
};
|
||||
let msg = err.to_string().to_ascii_lowercase();
|
||||
assert!(
|
||||
msg.contains("passphrase") || msg.contains("decrypt") || msg.contains("wrong"),
|
||||
"got {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reopen_with_same_passphrase_decrypts() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("open");
|
||||
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
|
||||
store.append_message(&pk(1), "out", b"hello again").unwrap();
|
||||
drop(store);
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("reopen");
|
||||
let msgs = store.list_messages(&pk(1)).unwrap();
|
||||
assert_eq!(msgs[0].plaintext, b"hello again");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,9 @@ async fn alice_sends_hello_wire_bob_sqlite_has_plaintext() {
|
|||
let bob_home = root.path().join("bob");
|
||||
|
||||
eprintln!("starting alice + bob nodes…");
|
||||
unsafe {
|
||||
std::env::set_var("ONIONWIRE_STORE_PASSPHRASE", "onionwire-test");
|
||||
}
|
||||
let (alice, bob) = tokio::join!(
|
||||
Node::start(alice_home.clone()),
|
||||
Node::start(bob_home.clone())
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
//! M5: wipe messages (keep identity + friends); wipe-all is a new person.
|
||||
|
||||
use onionwire::Store;
|
||||
use onionwire::tui::{WipeDecision, WipeKind, WipePrompt, parse_slash, wipe_screen_text};
|
||||
use onionwire::tui::{
|
||||
QuitDecision, QuitPrompt, WipeDecision, WipeKind, WipePrompt, parse_slash, quit_screen_text,
|
||||
wipe_screen_text,
|
||||
};
|
||||
|
||||
fn pk(tag: u8) -> [u8; 32] {
|
||||
let mut k = [0u8; 32];
|
||||
|
|
@ -19,7 +22,7 @@ fn db_contains(home: &std::path::Path, needle: &[u8]) -> bool {
|
|||
#[test]
|
||||
fn wipe_clears_messages_keeps_self_and_friends() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let store = Store::open_at(dir.path()).expect("open");
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "onionwire-test").expect("open");
|
||||
let me = store.self_identity().unwrap();
|
||||
store
|
||||
.upsert_friend(&pk(1), "a.onion", Some("alice"))
|
||||
|
|
@ -51,7 +54,7 @@ fn wipe_clears_messages_keeps_self_and_friends() {
|
|||
fn wipe_all_removes_dir_so_next_open_is_new_identity() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let home = dir.path().join("ow");
|
||||
let store = Store::open_at(&home).expect("open");
|
||||
let store = Store::open_at_with_passphrase(&home, "onionwire-test").expect("open");
|
||||
let old_pk = store.self_identity().unwrap().identity_pk;
|
||||
store.upsert_friend(&pk(1), "a.onion", None).unwrap();
|
||||
store.append_message(&pk(1), "out", b"gone").unwrap();
|
||||
|
|
@ -61,7 +64,7 @@ fn wipe_all_removes_dir_so_next_open_is_new_identity() {
|
|||
Store::wipe_all(&home).expect("wipe-all");
|
||||
assert!(!home.exists(), "data dir gone");
|
||||
|
||||
let store2 = Store::open_at(&home).expect("reopen");
|
||||
let store2 = Store::open_at_with_passphrase(&home, "onionwire-test").expect("reopen");
|
||||
let new_pk = store2.self_identity().unwrap().identity_pk;
|
||||
assert_ne!(old_pk, new_pk, "new identity key = new person");
|
||||
assert_eq!(store2.friend_count().unwrap(), 0);
|
||||
|
|
@ -115,3 +118,39 @@ fn wipe_prompt_text_matches_spec() {
|
|||
assert!(a.contains("Type WIPEALL to confirm"));
|
||||
assert!(a.contains("Esc to cancel"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quit_requires_clear_or_quit_then_enter() {
|
||||
let mut p = QuitPrompt::new();
|
||||
assert_eq!(p.on_esc(), QuitDecision::Cancel);
|
||||
assert_eq!(
|
||||
p.on_char('\n'),
|
||||
QuitDecision::Pending,
|
||||
"Enter alone must not quit"
|
||||
);
|
||||
for c in "CLEA".chars() {
|
||||
assert_eq!(p.on_char(c), QuitDecision::Pending);
|
||||
}
|
||||
assert_eq!(p.on_char('R'), QuitDecision::Pending, "CLEAR without Enter");
|
||||
assert_eq!(p.on_char('\n'), QuitDecision::ClearAndQuit);
|
||||
|
||||
let mut q = QuitPrompt::new();
|
||||
for c in "QUIT".chars() {
|
||||
assert_eq!(q.on_char(c), QuitDecision::Pending);
|
||||
}
|
||||
assert_eq!(q.on_char('\n'), QuitDecision::Quit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quit_prompt_text_matches_spec() {
|
||||
let t = quit_screen_text();
|
||||
assert!(
|
||||
t.contains("message history can be cleared")
|
||||
|| t.contains("Message history can be cleared")
|
||||
);
|
||||
assert!(t.contains("identity") && t.contains("friends"));
|
||||
assert!(t.contains("not") && t.contains("/wipe-all"));
|
||||
assert!(t.contains("CLEAR"));
|
||||
assert!(t.contains("QUIT"));
|
||||
assert!(t.contains("Esc"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue