chat: send and receive as ratchet frames through the outbox payload
Chat plaintext is ratchet-encrypted once, before the first delivery attempt,
and the frame is what the outbox stores and retries, so a retried message
re-sends identical bytes instead of advancing the sending chain twice.
- dispatch: Kind::Ratchet for the `rat ` prefix, matched before the unknown
[a-z]{3} prefix rule
- node: encode_chat_frame / ingest_chat_frame (the wire halves of Node::send
and handle_incoming), bootstrap derived from the peers' static prekey DH
because the Noise handshake is frozen and carries no ratchet key
- outbox: nullable outbox.payload + Outbox::send_payload; NULL payload keeps
the previous row-for-row behaviour
- store: additive outbox.payload migration, enqueue_outbox_payload,
OutboxEntry::payload
- add_friend_payload: a changed prekey clears the (prekey-bound) ratchet state
tests/ratchet_wire.rs (5) covers one frame per send, byte-identical retries,
out-of-order delivery, replay, tampering and fresh-friend both directions.
tests/tui_spec.rs and crates/onionwire-sdk/src/lib.rs only gained a
`payload: None` field in their OutboxEntry test fixtures: the card's required
field on a pub struct cannot be added without it.
This commit is contained in:
parent
14fccd5396
commit
69a15fe909
9 changed files with 619 additions and 13 deletions
55
src/store.rs
55
src/store.rs
|
|
@ -154,6 +154,12 @@ pub struct OutboxEntry {
|
|||
pub friend_pk: Vec<u8>,
|
||||
pub message_id: i64,
|
||||
pub body: Vec<u8>,
|
||||
/// What to put on the wire when it is set, instead of `body`. A chat send
|
||||
/// stores its already-encoded ratchet frame here so a retry re-sends the
|
||||
/// identical bytes instead of re-encrypting (which would burn a second
|
||||
/// chain key and land as a second chat line at the peer). `None` means the
|
||||
/// row predates payloads or is a locator/profile/payment row: send `body`.
|
||||
pub payload: Option<Vec<u8>>,
|
||||
pub attempts: u32,
|
||||
pub next_attempt_at: i64,
|
||||
pub created_at: i64,
|
||||
|
|
@ -272,7 +278,8 @@ impl Store {
|
|||
status TEXT NOT NULL CHECK(status IN ('queued','sending','sent','failed')),
|
||||
last_error TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
updated_at INTEGER NOT NULL,
|
||||
payload BLOB
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS outbox_due ON outbox (status, next_attempt_at);
|
||||
CREATE TABLE IF NOT EXISTS ratchet_sessions (
|
||||
|
|
@ -350,6 +357,14 @@ impl Store {
|
|||
"hs_nickname",
|
||||
"ALTER TABLE self ADD COLUMN hs_nickname TEXT NOT NULL DEFAULT 'ow0'",
|
||||
)?;
|
||||
// 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.
|
||||
self.add_column_if_missing(
|
||||
"outbox",
|
||||
"payload",
|
||||
"ALTER TABLE outbox ADD COLUMN payload BLOB",
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -903,6 +918,39 @@ impl Store {
|
|||
Ok(self.conn.last_insert_rowid())
|
||||
}
|
||||
|
||||
/// Queue one already-stored chat message for retry, with the exact bytes to
|
||||
/// put on the wire. Chat uses this: the frame is encoded once, before the
|
||||
/// first attempt, and every retry re-sends these bytes, so a retried
|
||||
/// message cannot arrive as a second chat line under a new key.
|
||||
///
|
||||
/// Narrower than [`Self::enqueue_outbox`] on purpose: a payload row is only
|
||||
/// ever queued by the first attempt failing, so it is created at
|
||||
/// `attempts = 1` and takes no attempt count.
|
||||
pub fn enqueue_outbox_payload(
|
||||
&self,
|
||||
friend_pk: &[u8],
|
||||
message_id: i64,
|
||||
payload: &[u8],
|
||||
next_attempt_at: i64,
|
||||
last_error: &str,
|
||||
now: i64,
|
||||
) -> Result<i64> {
|
||||
let friend_id: i64 = self
|
||||
.conn
|
||||
.query_row(
|
||||
"SELECT id FROM friends WHERE pubkey = ?1",
|
||||
params![friend_pk],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.map_err(|_| Error("friend not found".into()))?;
|
||||
self.conn.execute(
|
||||
"INSERT INTO outbox (friend_id, message_id, attempts, next_attempt_at, status, last_error, created_at, updated_at, payload)
|
||||
VALUES (?1, ?2, 1, ?3, 'queued', ?4, ?5, ?5, ?6)",
|
||||
params![friend_id, message_id, next_attempt_at, last_error, now, payload],
|
||||
)?;
|
||||
Ok(self.conn.last_insert_rowid())
|
||||
}
|
||||
|
||||
/// Rows whose backoff has elapsed: the timer trigger for the worker.
|
||||
pub fn due_outbox(&self, now: i64, limit: i64) -> Result<Vec<OutboxEntry>> {
|
||||
self.outbox_query(
|
||||
|
|
@ -1072,6 +1120,7 @@ impl Store {
|
|||
row.get::<_, i64>(8)?,
|
||||
row.get::<_, String>(9)?,
|
||||
row.get::<_, Vec<u8>>(10)?,
|
||||
row.get::<_, Option<Vec<u8>>>(11)?,
|
||||
))
|
||||
})?;
|
||||
let mut out = Vec::new();
|
||||
|
|
@ -1088,6 +1137,7 @@ impl Store {
|
|||
m_friend_id,
|
||||
dir,
|
||||
blob,
|
||||
payload,
|
||||
) = row?;
|
||||
let aad = message_aad(m_friend_id, &dir, message_id);
|
||||
let body = crate::backup::aead_decrypt(&self.msg_key, &blob, &aad)
|
||||
|
|
@ -1097,6 +1147,7 @@ impl Store {
|
|||
friend_pk,
|
||||
message_id,
|
||||
body,
|
||||
payload,
|
||||
attempts: attempts as u32,
|
||||
next_attempt_at,
|
||||
created_at,
|
||||
|
|
@ -1431,7 +1482,7 @@ fn mkdir_700(path: &Path) -> Result<()> {
|
|||
/// Base projection for outbox rows: the body comes from the `messages` row the
|
||||
/// outbox row points at, so it stays under that row's AEAD + AAD.
|
||||
const OUTBOX_SELECT: &str = "SELECT o.id, f.pubkey, o.message_id, o.attempts, o.next_attempt_at, \
|
||||
o.status, o.last_error, o.created_at, m.friend_id, m.dir, m.plaintext \
|
||||
o.status, o.last_error, o.created_at, m.friend_id, m.dir, m.plaintext, o.payload \
|
||||
FROM outbox o JOIN friends f ON f.id = o.friend_id JOIN messages m ON m.id = o.message_id";
|
||||
|
||||
/// AAD: `owmsg1` || friend_id_le64 || dir || 0x00 || row_id_le64
|
||||
|
|
|
|||
Loading…
Reference in a new issue