feat(store): encrypt the whole sqlite store with SQLCipher #43
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/sqlcipher-store"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The store held the ed25519 identity secret, friend pubkeys, locators, profiles and payment rows in the clear — only chat bodies were encrypted.
onionwire.dbis now SQLCipher.Cargo.toml:rusqlite = { version = "0.36", features = ["bundled-sqlcipher-vendored-openssl"] }(no system libssl;Cargo.lockgainsopenssl-src).src/store.rs: every store is opened withPRAGMA key = "x'<64 hex>'"as the first statement after open, using the existing Argon2id output as a raw key (no second KDF pass).docs/THREAT_MODEL.md: the "keys are not encrypted / sqlcipher is out of v1" paragraphs are replaced, andsqlcipherleaves the "Out of v1" list.Key management
A key cannot be derived from a salt that lives inside the file it opens, so the per-install KDF salt moved out of
store_metaintostore_salt(16 random bytes, mode 0600, inONIONWIRE_HOME).store_meta(salt + wrapped data key) still lives inside the encrypted db and the unlock path is otherwise untouched — that is what keeps existing encrypted message rows readable after the upgrade.store_saltwritten, thenPRAGMA key+ schema.SELECT count(*) FROM sqlite_master) fails before any write.sqlite_masteris read-only, so the db and its-wal/-shmare byte-identical afterwards (asserted in a test).open_at_with_passphrase, as before.store_salt: refuses to open with an actionable message, rather than guessing.Upgrade path for v0.2.1 installs (mandatory, in place)
On open, the file header decides:
SQLite format 3\0→ v0.2.1 plaintext store. The passphrase is first proven against the oldstore_meta(a wrong one returnswrong passphraseand leaves the file untouched), then the whole file is re-encrypted (PRAGMA wal_checkpoint(TRUNCATE),ATTACH DATABASE … KEY "x'…'",SELECT sqlcipher_export('enc')). The swap only happens after the new file opens, passesintegrity_checkand unwrapsstore_meta; any failure removes the temp file and returnsErrwith the original plaintext db intact. File mode is preserved and the stale-wal/-shmare dropped.So: no manual migration, no documented-but-unproven instructions, no stranded history. The upgrade is exercised end-to-end by a test that hand-builds the exact v0.2.1 on-disk layout (plaintext identity row,
store_metawith an Argon2id-wrapped data key, row-AAD ChaCha20-Poly1305 message rows) and then asserts identity, friends, locators and both messages survive it.SQLCipher logs an internal
ERROR CORE sqlcipher_page_cipher: hmac check failedto stderr when a key is rejected; that is now suppressed (PRAGMA cipher_log_level = NONE) so a typo'd passphrase is a one-line error, not a crypto dump.src/node.rs: no change requirednode.rs:68callsStore::open_at_with_passphrase(&home, passphrase)and nothing else in the tree opens the db file (grep -rn "Connection::open|onionwire.db" src cratesfinds onlysrc/store.rs). The migration is therefore invisible to the outbox card (t_b1714349) and to the TUI. No diff for the PR description is needed here.Evidence
New tests, mapped to the acceptance criteria:
whole_store_file_is_encrypted_at_rest(+ locator)plaintext_v0_2_1_store_is_encrypted_in_place_and_keeps_historyErr, no panic, file unchangedwrong_passphrase_touches_nothing_and_leaves_store_readable,legacy_plaintext_store_with_wrong_passphrase_is_left_untouchedencrypted_store_without_salt_file_refuses_to_guessBuild time impact (measured, cold
CARGO_TARGET_DIR, 16 cores, rustc 1.98.1)+48 s for the one-time compile of the SQLCipher amalgamation + vendored OpenSSL. It is inside the CI target volume cache (
onionwire-target-ci) andCargo.lockpinsopenssl-src, so it is paid once per runner, not per run. No systemlibssl-devis needed any more than before (it is still installed byci.yml).Risk: Android
crates/onionwire-sdkandandroid/**are untouched, but the rusqlite feature is not per-consumer: the Android build now also compiles vendored OpenSSL foraarch64-linux-android(it previously only neededrustls, no OpenSSL).openssl-srcsupports Android viacargo-ndk's CC/AR env, but this is not verified here — I have no NDK on this host. Please watch theandroid-releasejob on this PR; if it fails, the fallback isbundled-sqlcipherpluslibssl-devin the Android container, which is a one-lineCargo.tomlchange on this branch.README (deferred)
README.mdis owned by open PR #38 — I did not touch it. Apply after #38 lands.Replace the whole
## Seized laptopsection (the paragraph starting "Chat bodies in sqlite are ChaCha20-Poly1305…" through the "Still plaintext on disk" bullet list and the "The message key is not wrapped withidentity_sk…" paragraph) with:And in
## Not in v1, dropsqlcipher,from the list — it stays:Scope
Touched:
Cargo.toml,Cargo.lock,src/store.rs,tests/store.rs,docs/THREAT_MODEL.md. Not touched:src/node.rs,src/session.rs,src/wallet.rs,src/pay.rs,src/tui.rs,tests/tui_spec.rs,docs/TUI.md,docs/PROTOCOL.md,README.md,android/**,crates/onionwire-sdk/**.Android risk resolved: the vendored-OpenSSL path builds for the NDK
The PR body flagged that
android-release.ymldoes not run on PRs, so the SDK cross-build was unverified. I built it locally on this host with the real NDK:So the SDK links SQLCipher (with
sqlite3_key/sqlcipher_export) andopenssl-srccross-compiles foraarch64-linux-androidwith NDK r28c. NoCargo.tomlfallback needed; I did not touchcrates/onionwire-sdkorandroid/**.Note for whoever runs the release job: the Android APK/AAR now pulls a vendored OpenSSL build for each Android ABI, so the first
android-releaserun after this lands pays that compile once per ABI in its own target volume.Note on
crates/onionwire-sdk/Cargo.lockI did not touch it (forbidden path), but flagging what my local NDK build showed: that standalone lockfile is already stale on
main— rootCargo.tomlhassha2 = "0.10"but the SDK lock'sonionwiredependency list omits it. Cargo rewrites the file at build time (the Android workflow does not use--locked), which is why it never failed. On a fresh SDK build this change will additionally makeopenssl-src/openssl-sysappear in that lockfile, exactly as they now do in the rootCargo.lock. No action needed here; just don't be surprised by the dirty lockfile in the Android container.3adfc24391e7b37ccb40Merge order 5 of 7: #38 -> #42 -> #41 -> #44 -> #43 -> #45 -> #46
First of the three core PRs: it changes the rusqlite feature set and the store key path, so #45 and #46 build on it. Includes the plaintext-db upgrade path required by the card.
Note: Forgejo refuses a formal approval from this token ("approve your own pull is not allowed" — the PRs were pushed by account
sirius, which is also the token's user). This review records the sign-off and the merge position; a human approval click is still needed. Rebased onto currentmainbefore this review, diff verified byte-identical.sirius referenced this pull request2026-09-12 18:29:01 +00:00