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
|
|
@ -42,7 +42,7 @@ impl BackupKeys {
|
|||
}
|
||||
}
|
||||
|
||||
fn kdf(passphrase: &str, salt: &[u8]) -> Result<[u8; 32], String> {
|
||||
pub(crate) fn kdf(passphrase: &str, salt: &[u8]) -> Result<[u8; 32], String> {
|
||||
if passphrase.is_empty() {
|
||||
return Err("empty passphrase".into());
|
||||
}
|
||||
|
|
@ -94,3 +94,26 @@ pub fn open(passphrase: &str, blob: &[u8]) -> Result<BackupKeys, String> {
|
|||
.map_err(|_| "wrong passphrase or corrupt backup".to_string())?;
|
||||
BackupKeys::from_bytes(&pt)
|
||||
}
|
||||
|
||||
pub(crate) fn aead_encrypt(key: &[u8; 32], plaintext: &[u8]) -> Result<Vec<u8>, String> {
|
||||
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
|
||||
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
|
||||
let ct = cipher
|
||||
.encrypt(&nonce, plaintext)
|
||||
.map_err(|_| "encrypt failed".to_string())?;
|
||||
let mut out = Vec::with_capacity(NONCE_LEN + ct.len());
|
||||
out.extend_from_slice(&nonce);
|
||||
out.extend_from_slice(&ct);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub(crate) fn aead_decrypt(key: &[u8; 32], blob: &[u8]) -> Result<Vec<u8>, String> {
|
||||
if blob.len() < NONCE_LEN + 16 {
|
||||
return Err("ciphertext length".into());
|
||||
}
|
||||
let nonce = Nonce::from_slice(&blob[..NONCE_LEN]);
|
||||
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
|
||||
cipher
|
||||
.decrypt(nonce, &blob[NONCE_LEN..])
|
||||
.map_err(|_| "wrong passphrase or corrupt".to_string())
|
||||
}
|
||||
|
|
|
|||
23
src/main.rs
23
src/main.rs
|
|
@ -1,4 +1,5 @@
|
|||
use onionwire::tui::AppExit;
|
||||
use std::io::{self, Write};
|
||||
|
||||
fn print_help() {
|
||||
let v = env!("CARGO_PKG_VERSION");
|
||||
|
|
@ -34,8 +35,9 @@ async fn main() {
|
|||
|
||||
async fn boot() -> Result<(), String> {
|
||||
let home = onionwire::Store::home_dir().map_err(|e| e.to_string())?;
|
||||
let pass = store_passphrase()?;
|
||||
eprintln!("onionwire: bootstrapping Arti…");
|
||||
let node = onionwire::node::Node::start(home.clone()).await?;
|
||||
let node = onionwire::node::Node::start_with_passphrase(home.clone(), &pass).await?;
|
||||
let handle = tokio::runtime::Handle::current();
|
||||
let exit = tokio::task::spawn_blocking(move || onionwire::tui::run(node, handle))
|
||||
.await
|
||||
|
|
@ -46,3 +48,22 @@ async fn boot() -> Result<(), String> {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store_passphrase() -> Result<String, String> {
|
||||
match std::env::var("ONIONWIRE_STORE_PASSPHRASE") {
|
||||
Ok(p) if p.is_empty() => Err("empty passphrase".into()),
|
||||
Ok(p) => Ok(p),
|
||||
Err(_) => {
|
||||
eprint!("onionwire: store passphrase: ");
|
||||
let _ = io::stderr().flush();
|
||||
let mut s = String::new();
|
||||
io::stdin().read_line(&mut s).map_err(|e| e.to_string())?;
|
||||
let s = s.trim_end_matches(['\n', '\r']).to_string();
|
||||
if s.is_empty() {
|
||||
Err("empty passphrase".into())
|
||||
} else {
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
src/node.rs
14
src/node.rs
|
|
@ -50,7 +50,19 @@ pub struct Node {
|
|||
|
||||
impl Node {
|
||||
pub async fn start(home: PathBuf) -> Result<Arc<Self>, String> {
|
||||
let store = Store::open_at(&home).map_err(|e| e.to_string())?;
|
||||
let pass = std::env::var("ONIONWIRE_STORE_PASSPHRASE")
|
||||
.map_err(|_| "ONIONWIRE_STORE_PASSPHRASE required".to_string())?;
|
||||
if pass.is_empty() {
|
||||
return Err("empty passphrase".into());
|
||||
}
|
||||
Self::start_with_passphrase(home, &pass).await
|
||||
}
|
||||
|
||||
pub async fn start_with_passphrase(
|
||||
home: PathBuf,
|
||||
passphrase: &str,
|
||||
) -> Result<Arc<Self>, String> {
|
||||
let store = Store::open_at_with_passphrase(&home, passphrase).map_err(|e| e.to_string())?;
|
||||
let me = store.self_identity().map_err(|e| e.to_string())?;
|
||||
let keys = Keys::from_self(&me).map_err(|e| e.to_string())?;
|
||||
let nickname = store.hs_nickname().map_err(|e| e.to_string())?;
|
||||
|
|
|
|||
149
src/store.rs
149
src/store.rs
|
|
@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use ed25519_dalek::SigningKey;
|
||||
use rand::RngCore;
|
||||
use rand::rngs::OsRng;
|
||||
use rusqlite::{Connection, OptionalExtension, params};
|
||||
use x25519_dalek::{PublicKey as X25519Public, StaticSecret};
|
||||
|
|
@ -33,8 +34,15 @@ impl From<std::io::Error> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<String> for Error {
|
||||
fn from(e: String) -> Self {
|
||||
Self(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Store {
|
||||
conn: Connection,
|
||||
msg_key: [u8; 32],
|
||||
}
|
||||
|
||||
pub struct SelfIdentity {
|
||||
|
|
@ -100,6 +108,13 @@ impl Store {
|
|||
}
|
||||
|
||||
pub fn open_at(home: &Path) -> Result<Self> {
|
||||
Self::open_at_with_passphrase(home, &passphrase_from_env()?)
|
||||
}
|
||||
|
||||
pub fn open_at_with_passphrase(home: &Path, passphrase: &str) -> Result<Self> {
|
||||
if passphrase.is_empty() {
|
||||
return Err(Error("empty passphrase".into()));
|
||||
}
|
||||
mkdir_700(home)?;
|
||||
mkdir_700(&home.join("arti"))?;
|
||||
let db_path = home.join("onionwire.db");
|
||||
|
|
@ -153,6 +168,11 @@ impl Store {
|
|||
xmr_addr TEXT NOT NULL DEFAULT '',
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS store_meta (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
kdf_salt BLOB NOT NULL,
|
||||
wrapped_key BLOB NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS payments (
|
||||
id INTEGER PRIMARY KEY,
|
||||
friend_id INTEGER NOT NULL REFERENCES friends(id),
|
||||
|
|
@ -172,9 +192,13 @@ impl Store {
|
|||
if check != "ok" {
|
||||
return Err(Error(format!("integrity_check: {check}")));
|
||||
}
|
||||
let store = Self { conn };
|
||||
let mut store = Self {
|
||||
conn,
|
||||
msg_key: [0u8; 32],
|
||||
};
|
||||
store.migrate()?;
|
||||
store.ensure_self()?;
|
||||
store.unlock_messages(passphrase)?;
|
||||
Ok(store)
|
||||
}
|
||||
|
||||
|
|
@ -234,6 +258,66 @@ impl Store {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn unlock_messages(&mut self, passphrase: &str) -> Result<()> {
|
||||
let row: Option<(Vec<u8>, Vec<u8>)> = self
|
||||
.conn
|
||||
.query_row(
|
||||
"SELECT kdf_salt, wrapped_key FROM store_meta WHERE id = 1",
|
||||
[],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
match row {
|
||||
Some((salt, wrapped)) => {
|
||||
let wrap_key = crate::backup::kdf(passphrase, &salt)?;
|
||||
let raw = crate::backup::aead_decrypt(&wrap_key, &wrapped)
|
||||
.map_err(|_| Error("wrong passphrase".into()))?;
|
||||
if raw.len() != 32 {
|
||||
return Err(Error("wrapped message key length".into()));
|
||||
}
|
||||
self.msg_key.copy_from_slice(&raw);
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
let mut data_key = [0u8; 32];
|
||||
OsRng.fill_bytes(&mut data_key);
|
||||
let mut salt = [0u8; 16];
|
||||
OsRng.fill_bytes(&mut salt);
|
||||
let wrap_key = crate::backup::kdf(passphrase, &salt)?;
|
||||
let wrapped = crate::backup::aead_encrypt(&wrap_key, &data_key)?;
|
||||
self.conn.execute(
|
||||
"INSERT INTO store_meta (id, kdf_salt, wrapped_key) VALUES (1, ?1, ?2)",
|
||||
params![salt.as_slice(), wrapped],
|
||||
)?;
|
||||
self.msg_key = data_key;
|
||||
self.reencrypt_legacy_messages()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn reencrypt_legacy_messages(&self) -> Result<()> {
|
||||
let mut stmt = self.conn.prepare("SELECT id, plaintext FROM messages")?;
|
||||
let rows: Vec<(i64, Vec<u8>)> = stmt
|
||||
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?
|
||||
.collect::<std::result::Result<_, _>>()?;
|
||||
drop(stmt);
|
||||
if rows.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let tx = self.conn.unchecked_transaction()?;
|
||||
for (id, plain) in rows {
|
||||
let blob = crate::backup::aead_encrypt(&self.msg_key, &plain)?;
|
||||
tx.execute(
|
||||
"UPDATE messages SET plaintext = ?1 WHERE id = ?2",
|
||||
params![blob, id],
|
||||
)?;
|
||||
}
|
||||
tx.commit()?;
|
||||
let _ = self.conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE)");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_column_if_missing(&self, table: &str, column: &str, ddl: &str) -> Result<()> {
|
||||
let sql =
|
||||
format!("SELECT COUNT(*) FROM pragma_table_info('{table}') WHERE name = '{column}'");
|
||||
|
|
@ -562,9 +646,10 @@ impl Store {
|
|||
|row| row.get(0),
|
||||
)
|
||||
.map_err(|_| Error("friend not found".into()))?;
|
||||
let blob = crate::backup::aead_encrypt(&self.msg_key, plaintext)?;
|
||||
self.conn.execute(
|
||||
"INSERT INTO messages (friend_id, dir, plaintext, created_at) VALUES (?1, ?2, ?3, ?4)",
|
||||
params![friend_id, dir, plaintext, unix_now()],
|
||||
params![friend_id, dir, blob, unix_now()],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -577,14 +662,14 @@ impl Store {
|
|||
ORDER BY m.id",
|
||||
)?;
|
||||
let rows = stmt.query_map(params![friend_pk], |row| {
|
||||
Ok(Message {
|
||||
dir: row.get(0)?,
|
||||
plaintext: row.get(1)?,
|
||||
})
|
||||
Ok((row.get::<_, String>(0)?, row.get::<_, Vec<u8>>(1)?))
|
||||
})?;
|
||||
let mut out = Vec::new();
|
||||
for row in rows {
|
||||
out.push(row?);
|
||||
let (dir, blob) = row?;
|
||||
let plaintext = crate::backup::aead_decrypt(&self.msg_key, &blob)
|
||||
.map_err(|_| Error("message decrypt failed".into()))?;
|
||||
out.push(Message { dir, plaintext });
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
|
@ -657,6 +742,14 @@ fn home_dir() -> Result<PathBuf> {
|
|||
Ok(PathBuf::from(home).join(".local/share/onionwire"))
|
||||
}
|
||||
|
||||
fn passphrase_from_env() -> Result<String> {
|
||||
match std::env::var("ONIONWIRE_STORE_PASSPHRASE") {
|
||||
Ok(p) if p.is_empty() => Err(Error("empty passphrase".into())),
|
||||
Ok(p) => Ok(p),
|
||||
Err(_) => Err(Error("ONIONWIRE_STORE_PASSPHRASE required".into())),
|
||||
}
|
||||
}
|
||||
|
||||
fn mkdir_700(path: &Path) -> Result<()> {
|
||||
fs::create_dir_all(path)?;
|
||||
let mut perms = fs::metadata(path)?.permissions();
|
||||
|
|
@ -675,3 +768,45 @@ fn unix_now() -> i64 {
|
|||
fn fingerprint(pubkey: &[u8]) -> String {
|
||||
pubkey.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod at_rest {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn first_unlock_reencrypts_legacy_plaintext_rows() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "migrate-pass").unwrap();
|
||||
let pk = [1u8; 32];
|
||||
store.upsert_friend(&pk, "a.onion", None).unwrap();
|
||||
store
|
||||
.append_message(&pk, "out", b"legacy-plain-xyz")
|
||||
.unwrap();
|
||||
store.conn.execute("DELETE FROM store_meta", []).unwrap();
|
||||
store
|
||||
.conn
|
||||
.execute(
|
||||
"UPDATE messages SET plaintext = ?1",
|
||||
params![b"legacy-plain-xyz".as_slice()],
|
||||
)
|
||||
.unwrap();
|
||||
let _ = store.conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE)");
|
||||
drop(store);
|
||||
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "migrate-pass").unwrap();
|
||||
let msgs = store.list_messages(&pk).unwrap();
|
||||
assert_eq!(msgs[0].plaintext, b"legacy-plain-xyz");
|
||||
drop(store);
|
||||
let needle = b"legacy-plain-xyz";
|
||||
for name in ["onionwire.db", "onionwire.db-wal", "onionwire.db-shm"] {
|
||||
let path = dir.path().join(name);
|
||||
let Ok(bytes) = std::fs::read(&path) else {
|
||||
continue;
|
||||
};
|
||||
assert!(
|
||||
!bytes.windows(needle.len()).any(|w| w == needle),
|
||||
"{name} still contains legacy plaintext"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
107
src/tui.rs
107
src/tui.rs
|
|
@ -31,6 +31,15 @@ Offline friends CANNOT find you until they F3-paste your new invite.\n\
|
|||
Type ROTATE to confirm Esc to cancel"
|
||||
}
|
||||
|
||||
pub fn quit_screen_text() -> &'static str {
|
||||
"Quit OnionWire?\n\
|
||||
Message history can be cleared; identity and friends stay.\n\
|
||||
This is not /wipe-all.\n\
|
||||
Type CLEAR then Enter to clear history and quit.\n\
|
||||
Type QUIT then Enter to quit without clearing.\n\
|
||||
Esc to cancel"
|
||||
}
|
||||
|
||||
/// Full Main-screen wordmark. Every line is <= 80 ASCII characters.
|
||||
pub fn wordmark_banner() -> &'static str {
|
||||
"\
|
||||
|
|
@ -71,7 +80,7 @@ OnionWire keys\n\
|
|||
/backup /path encrypted identity export\n\
|
||||
/restore /path overwrite self keys\n\
|
||||
? this help\n\
|
||||
Ctrl-Q quit\n\
|
||||
Ctrl-Q quit; type CLEAR or QUIT\n\
|
||||
\n\
|
||||
Esc closes this overlay"
|
||||
}
|
||||
|
|
@ -415,6 +424,52 @@ impl RotatePrompt {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum QuitDecision {
|
||||
Pending,
|
||||
ClearAndQuit,
|
||||
Quit,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct QuitPrompt {
|
||||
buf: String,
|
||||
}
|
||||
|
||||
impl QuitPrompt {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn on_esc(&self) -> QuitDecision {
|
||||
QuitDecision::Cancel
|
||||
}
|
||||
|
||||
pub fn on_char(&mut self, c: char) -> QuitDecision {
|
||||
if c == '\n' {
|
||||
return match self.buf.as_str() {
|
||||
"CLEAR" => QuitDecision::ClearAndQuit,
|
||||
"QUIT" => QuitDecision::Quit,
|
||||
_ => QuitDecision::Pending,
|
||||
};
|
||||
}
|
||||
if c == '\u{8}' {
|
||||
self.buf.pop();
|
||||
return QuitDecision::Pending;
|
||||
}
|
||||
if !c.is_ascii_alphabetic() {
|
||||
return QuitDecision::Pending;
|
||||
}
|
||||
self.buf.push(c);
|
||||
QuitDecision::Pending
|
||||
}
|
||||
|
||||
pub fn typed(&self) -> &str {
|
||||
&self.buf
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BackupKind {
|
||||
Backup,
|
||||
|
|
@ -558,6 +613,9 @@ enum Screen {
|
|||
first: Option<String>,
|
||||
buf: String,
|
||||
},
|
||||
Quit {
|
||||
prompt: QuitPrompt,
|
||||
},
|
||||
}
|
||||
|
||||
impl App {
|
||||
|
|
@ -591,7 +649,12 @@ impl App {
|
|||
continue;
|
||||
}
|
||||
if key.code == KeyCode::Char('q') && key.modifiers.contains(KeyModifiers::CONTROL) {
|
||||
return Ok(AppExit::Quit);
|
||||
if !matches!(self.screen, Screen::Quit { .. }) {
|
||||
self.screen = Screen::Quit {
|
||||
prompt: QuitPrompt::new(),
|
||||
};
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(exit) = self.handle_key(key)? {
|
||||
return Ok(exit);
|
||||
|
|
@ -624,6 +687,7 @@ impl App {
|
|||
}
|
||||
|
||||
let mut wipe_confirm = None;
|
||||
let mut quit_clear = None;
|
||||
let mut profile_save = None;
|
||||
let mut open_profile = false;
|
||||
let mut pay_cmd = None;
|
||||
|
|
@ -810,10 +874,31 @@ impl App {
|
|||
}
|
||||
_ => {}
|
||||
},
|
||||
Screen::Quit { prompt } => match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Enter => match prompt.on_char('\n') {
|
||||
QuitDecision::ClearAndQuit => quit_clear = Some(true),
|
||||
QuitDecision::Quit => quit_clear = Some(false),
|
||||
_ => {}
|
||||
},
|
||||
KeyCode::Backspace => {
|
||||
prompt.on_char('\u{8}');
|
||||
}
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
prompt.on_char(c);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
if let Some(kind) = wipe_confirm {
|
||||
return self.confirm_wipe(kind);
|
||||
}
|
||||
if let Some(clear) = quit_clear {
|
||||
if clear {
|
||||
self.node.wipe_messages()?;
|
||||
}
|
||||
return Ok(Some(AppExit::Quit));
|
||||
}
|
||||
if open_profile {
|
||||
self.open_profile()?;
|
||||
}
|
||||
|
|
@ -1098,6 +1183,7 @@ impl App {
|
|||
Screen::Passphrase {
|
||||
kind, first, buf, ..
|
||||
} => draw_passphrase(f, *kind, first.is_some(), buf),
|
||||
Screen::Quit { prompt } => draw_quit(f, prompt.typed()),
|
||||
}
|
||||
if self.help_open {
|
||||
draw_text_overlay(f, "? help", help_overlay_text());
|
||||
|
|
@ -1242,7 +1328,10 @@ impl App {
|
|||
|
||||
fn chat_lines(&self) -> Vec<String> {
|
||||
let Some(friend) = self.friends.get(self.selected) else {
|
||||
return vec!["no friends yet".into(), "F3 paste an invite to add one".into()];
|
||||
return vec![
|
||||
"no friends yet".into(),
|
||||
"F3 paste an invite to add one".into(),
|
||||
];
|
||||
};
|
||||
match self.node.list_messages(&friend.pubkey) {
|
||||
Ok(msgs) if msgs.is_empty() => vec!["no messages".into()],
|
||||
|
|
@ -1342,6 +1431,7 @@ fn footer_hints(screen: &Screen) -> &'static str {
|
|||
BackupKind::Restore => "type RESTORE Esc cancel ? help",
|
||||
},
|
||||
Screen::Passphrase { .. } => "Enter submit Esc cancel ? help",
|
||||
Screen::Quit { .. } => "CLEAR or QUIT then Enter Esc cancel ? help",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1428,6 +1518,17 @@ fn draw_wipe(f: &mut Frame, kind: WipeKind, typed: &str) {
|
|||
);
|
||||
}
|
||||
|
||||
fn draw_quit(f: &mut Frame, typed: &str) {
|
||||
let body = format!("{}\n\n{typed}", quit_screen_text());
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
.style(Style::default().fg(C_TEXT))
|
||||
.wrap(Wrap { trim: false })
|
||||
.block(themed_block("(o) quit")),
|
||||
f.area(),
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_profile(f: &mut Frame, editor: &ProfileEditor) {
|
||||
let labels = ["name", "bio", "xmr"];
|
||||
let values = [editor.display_name(), editor.bio(), editor.xmr_addr()];
|
||||
|
|
|
|||
Loading…
Reference in a new issue