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
|
|
@ -143,9 +143,38 @@ impl Outbox {
|
|||
/// Try once. `Ok(Sent)` — delivered now. `Ok(Queued)` — the peer's onion is
|
||||
/// unreachable, the message is persisted and will be retried. `Err` — a
|
||||
/// protocol error or a bad local state, never retried.
|
||||
///
|
||||
/// The bytes on the wire are `plaintext`; the *retry* path is where chat
|
||||
/// differs, so chat uses [`Self::send_payload`].
|
||||
pub async fn send(&self, friend_pk: &[u8], plaintext: &[u8]) -> Result<OutboxStatus, String> {
|
||||
self.send_inner(friend_pk, plaintext, None).await
|
||||
}
|
||||
|
||||
/// Same as [`Self::send`], but the wire bytes are an opaque `payload` that
|
||||
/// a retry re-sends verbatim while the chat history keeps `plaintext`.
|
||||
///
|
||||
/// Chat calls this with its ratchet frame: encoding the frame inside the
|
||||
/// retry loop would advance the sending chain a second time and the peer
|
||||
/// would see a second chat line under a new key. The frame is encoded once
|
||||
/// by the caller, stored on the row, and replayed byte-for-byte.
|
||||
pub async fn send_payload(
|
||||
&self,
|
||||
friend_pk: &[u8],
|
||||
plaintext: &[u8],
|
||||
payload: &[u8],
|
||||
) -> Result<OutboxStatus, String> {
|
||||
self.send_inner(friend_pk, plaintext, Some(payload)).await
|
||||
}
|
||||
|
||||
async fn send_inner(
|
||||
&self,
|
||||
friend_pk: &[u8],
|
||||
plaintext: &[u8],
|
||||
payload: Option<&[u8]>,
|
||||
) -> Result<OutboxStatus, String> {
|
||||
let (onion, prekey) = self.target(friend_pk)?;
|
||||
match self.deliverer.deliver(&onion, friend_pk, &prekey, plaintext).await {
|
||||
let wire = payload.unwrap_or(plaintext);
|
||||
match self.deliverer.deliver(&onion, friend_pk, &prekey, wire).await {
|
||||
Ok(()) => {
|
||||
self.store()?.append_message(friend_pk, "out", plaintext)?;
|
||||
Ok(OutboxStatus::Sent)
|
||||
|
|
@ -155,14 +184,13 @@ impl Outbox {
|
|||
let now = unix_now();
|
||||
let store = self.store()?;
|
||||
let message_id = store.append_message_id(friend_pk, "out", plaintext)?;
|
||||
store.enqueue_outbox(
|
||||
friend_pk,
|
||||
message_id,
|
||||
1,
|
||||
now + backoff_secs(0),
|
||||
&e,
|
||||
now,
|
||||
)?;
|
||||
let next = now + backoff_secs(0);
|
||||
match payload {
|
||||
Some(payload) => store.enqueue_outbox_payload(
|
||||
friend_pk, message_id, payload, next, &e, now,
|
||||
)?,
|
||||
None => store.enqueue_outbox(friend_pk, message_id, 1, next, &e, now)?,
|
||||
};
|
||||
Ok(OutboxStatus::Queued)
|
||||
}
|
||||
}
|
||||
|
|
@ -248,7 +276,13 @@ impl Outbox {
|
|||
};
|
||||
let result = self
|
||||
.deliverer
|
||||
.deliver(&onion, &entry.friend_pk, &prekey, &entry.body)
|
||||
.deliver(
|
||||
&onion,
|
||||
&entry.friend_pk,
|
||||
&prekey,
|
||||
// Chat stores its frame here; everything else sends its body.
|
||||
entry.payload.as_deref().unwrap_or(&entry.body),
|
||||
)
|
||||
.await;
|
||||
let status = match result {
|
||||
Ok(()) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue