feat(store): encrypt the whole sqlite store with SQLCipher
All checks were successful
ci / test (pull_request) Successful in 3m35s

The db held the ed25519 identity secret, friend pubkeys, locators, profiles
and payment rows in the clear; only chat bodies were encrypted. Switch
rusqlite to `bundled-sqlcipher-vendored-openssl` and open every store with
`PRAGMA key = "x'<64 hex>'"` as the first statement after open, using the
existing Argon2id output as a raw key (no second KDF pass).

The per-install KDF salt moves out of the db into `store_salt` (0600) — a key
cannot be derived from a salt that lives inside the file it opens. The
per-message ChaCha20-Poly1305 + row-AAD layer is unchanged: whole-db
encryption does not defend against ciphertext swapped between rows, the AAD
does.

v0.2.1 installs are plaintext. The first open with the right passphrase
upgrades in place (WAL checkpoint, ATTACH ... KEY, sqlcipher_export) and only
swaps the file after the new one opens, passes integrity_check and unwraps
store_meta. A wrong passphrase leaves the plaintext file untouched; a
missing `store_salt` next to an encrypted db refuses to open rather than
guessing.

SQLCipher's internal "hmac check failed" ERROR on a rejected key is
suppressed (cipher_log_level = NONE) so a typo'd passphrase is not an error
dump.

docs/THREAT_MODEL.md: the store is now encrypted at rest; sqlcipher leaves
the "Out of v1" list.
This commit is contained in:
Sirius DevOps 2026-09-12 13:00:18 -04:00
commit e7b37ccb40
No known key found for this signature in database
5 changed files with 551 additions and 8 deletions

View file

@ -233,6 +233,121 @@ fn append_message_db_file_does_not_contain_plaintext() {
);
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn db_files(home: &Path) -> Vec<(String, Vec<u8>)> {
let mut out = Vec::new();
for ent in fs::read_dir(home).expect("read_dir").flatten() {
let name = ent.file_name().to_string_lossy().to_string();
if !name.starts_with("onionwire.db") {
continue;
}
out.push((name, fs::read(ent.path()).expect("read db file")));
}
out.sort();
out
}
fn store_contains(home: &Path, needle: &[u8]) -> bool {
db_files(home)
.iter()
.any(|(_, bytes)| bytes.windows(needle.len()).any(|w| w == needle))
}
#[test]
fn whole_store_file_is_encrypted_at_rest() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "at-rest-pass").expect("open");
let me = store.self_identity().unwrap();
store
.upsert_friend(&pk(7), "friend.onion", Some("alice"))
.unwrap();
store
.append_message(&pk(7), "out", b"body-at-rest-xyz")
.unwrap();
store
.append_message(&pk(7), "in", b"reply-at-rest-xyz")
.unwrap();
drop(store);
for (needle, what) in [
(b"body-at-rest-xyz".to_vec(), "chat body"),
(b"reply-at-rest-xyz".to_vec(), "inbound chat body"),
(hex(&pk(7)).into_bytes(), "friend pubkey hex"),
(b"friend.onion".to_vec(), "friend locator"),
(me.identity_sk.clone(), "identity secret key"),
] {
assert!(
!store_contains(dir.path(), &needle),
"onionwire.db* still contains the {what}"
);
}
}
#[test]
fn wrong_passphrase_touches_nothing_and_leaves_store_readable() {
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 before = db_files(dir.path());
assert!(!before.is_empty(), "expected store files on disk");
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}"
);
assert_eq!(
db_files(dir.path()),
before,
"a failed unlock must not modify the store files"
);
let store = Store::open_at_with_passphrase(dir.path(), "right-pass").expect("reopen");
assert_eq!(
store.list_messages(&pk(1)).unwrap()[0].plaintext,
b"secret-chat-body"
);
}
#[test]
fn encrypted_store_without_salt_file_refuses_to_guess() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "at-rest-pass").expect("open");
store.upsert_friend(&pk(1), "b.onion", None).unwrap();
let me = store.self_identity().unwrap();
drop(store);
fs::remove_file(dir.path().join("store_salt")).expect("remove salt");
let err = match Store::open_at_with_passphrase(dir.path(), "at-rest-pass") {
Ok(_) => panic!("an encrypted store without its salt must not open"),
Err(e) => e,
};
assert!(err.to_string().contains("store_salt"), "got {err}");
// The salt is only a salt: putting it back restores access, no data lost.
fs::write(dir.path().join("store_salt"), [0u8; 16]).unwrap();
let err = match Store::open_at_with_passphrase(dir.path(), "at-rest-pass") {
Ok(_) => panic!("a wrong salt must not silently open the store"),
Err(e) => e,
};
assert!(
err.to_string().to_ascii_lowercase().contains("passphrase"),
"a wrong salt must not silently open the store: {err}"
);
assert_eq!(me.identity_sk.len(), 32);
}
#[test]
fn wrong_passphrase_cannot_open_or_list() {
let dir = tempfile::tempdir().expect("tempdir");