Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(whatsmeow-signal): implement whatsmeow's Signal protocol with libsignal (#8534)
Wolf Mermelstein3f6d0168a157
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 0bccf37ca8..bb34552cea 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -13786,6 +13786,13 @@"crates/webhook_signature","crates/workspace-hack"],+ "whatsapp_fake_server": [+ "crates/whatsapp_fake_server",+ "crates/whatsmeow",+ "crates/whatsmeow_proto",+ "crates/whatsmeow_signal",+ "crates/workspace-hack"+ ],"whatsmeow": ["crates/whatsmeow","crates/whatsmeow_proto",@@ -13795,6 +13802,12 @@"crates/whatsmeow_proto","crates/workspace-hack"],+ "whatsmeow_signal": [+ "crates/whatsmeow",+ "crates/whatsmeow_proto",+ "crates/whatsmeow_signal",+ "crates/workspace-hack"+ ],"work_feed": ["crates/activity","crates/agent",diff --git a/Cargo.lock b/Cargo.lockindex f745e2fea7..dedb9cce35 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -11343,6 +11343,33 @@ dependencies = ["redox_syscall 0.7.5",]+[[package]]+name = "libsignal-core"+version = "0.1.0"+source = "git+https://github.com/signalapp/libsignal?tag=v0.105.2#b369755533ef5a3fae9b31b989114f22e074dfab"+dependencies = [+ "curve25519-dalek 5.0.0",+ "derive_more 2.1.1",+ "displaydoc",+ "libsignal-debug",+ "log",+ "rand 0.9.4",+ "sha2 0.11.0",+ "subtle",+ "thiserror 2.0.18",+ "uuid",+ "x25519-dalek 3.0.0",+ "zerocopy",+]++[[package]]+name = "libsignal-debug"+version = "0.105.2"+source = "git+https://github.com/signalapp/libsignal?tag=v0.105.2#b369755533ef5a3fae9b31b989114f22e074dfab"+dependencies = [+ "cfg-if",+]+[[package]]name = "libsqlite3-sys"version = "0.30.1"@@ -21418,6 +21445,33 @@ version = "0.1.12"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88"+[[package]]+name = "whatsapp_fake_server"+version = "0.1.0"+dependencies = [+ "aes-gcm 0.10.3",+ "async-trait",+ "axum",+ "base64 0.22.1",+ "chrono",+ "futures",+ "hmac 0.12.1",+ "nom 8.0.0",+ "prost 0.14.3",+ "rand 0.9.4",+ "reqwest 0.13.4",+ "serde_json",+ "sha2 0.10.9",+ "thiserror 2.0.18",+ "tokio",+ "tokio-tungstenite 0.28.0",+ "tokio-util",+ "tracing",+ "whatsmeow",+ "whatsmeow_signal",+ "workspace-hack",+]+[[package]]name = "whatsmeow"version = "0.1.0"@@ -21455,7 +21509,7 @@ dependencies = ["uuid","whatsmeow_proto","workspace-hack",- "x25519-dalek",+ "x25519-dalek 2.0.1",][[package]]@@ -21468,6 +21522,35 @@ dependencies = ["workspace-hack",]+[[package]]+name = "whatsmeow_signal"+version = "0.1.0"+dependencies = [+ "aes 0.8.4",+ "async-trait",+ "cbc 0.1.2",+ "hex",+ "hkdf 0.12.4",+ "hmac 0.12.1",+ "libsignal-core",+ "prost 0.14.3",+ "prost-build 0.14.3",+ "protox",+ "rand 0.9.4",+ "rootcause",+ "serde",+ "serde_json",+ "sha2 0.10.9",+ "subtle",+ "thiserror 2.0.18",+ "tokio",+ "tokio-util",+ "tracing",+ "whatsapp_fake_server",+ "whatsmeow",+ "workspace-hack",+]+[[package]]name = "whoami"version = "1.6.1"@@ -22192,11 +22275,13 @@ dependencies = ["crypto-common 0.2.2","ctutils","curve25519-dalek 4.1.3",+ "curve25519-dalek 5.0.0","data-encoding","derive_more 2.1.1","derive_more-impl","digest 0.10.7","digest 0.11.3",+ "displaydoc","doppleganger","doppleganger-core","either",@@ -22388,6 +22473,17 @@ dependencies = ["zeroize",]+[[package]]+name = "x25519-dalek"+version = "3.0.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e7e8131a03190127fb2263afc72b322ecadae46b6ff8c6f399ff5d02f5559af6"+dependencies = [+ "curve25519-dalek 5.0.0",+ "rand_core 0.10.1",+ "zeroize",+]+[[package]]name = "xmlparser"version = "0.13.6"diff --git a/Cargo.toml b/Cargo.tomlindex 69304f9865..8f471bcb4a 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -125,8 +125,10 @@ members = ["crates/unfurl","crates/user_api_key","crates/webhook_signature",+ "crates/whatsapp_fake_server","crates/whatsmeow","crates/whatsmeow_proto",+ "crates/whatsmeow_signal","crates/work_feed","crates/worker-rs-otel","crates/workspace-hack",@@ -286,6 +288,8 @@ jsonwebtoken = { git = "https://github.com/whutchinson98/jsonwebtoken", featureslambda_runtime = "0.13.0"lazy-regex = "3.6.1"lazy_static = "1.5.0"+# Signal's curve25519 keys and XEdDSA; see crates/whatsmeow_signal/AGENTS.md.+libsignal-core = { git = "https://github.com/signalapp/libsignal", tag = "v0.105.2" }livekit-api = { version = "0.4.16", default-features = false, features = ["access-token","rustls-tls-webpki-roots",diff --git a/crates/whatsapp_fake_server/AGENTS.md b/crates/whatsapp_fake_server/AGENTS.mdnew file mode 100644index 0000000000..d616715a19--- /dev/null+++ b/crates/whatsapp_fake_server/AGENTS.md@@ -0,0 +1,52 @@+# whatsapp_fake_server++## What it is++Test support for WhatsApp linked devices: a fake WhatsApp server that the real+whatsmeow port (`crates/whatsmeow`) connects to over a local websocket, and fake+phones (the other people) with real Signal sessions from `whatsmeow_signal`.+Lets the session service and its tests run end to end without WhatsApp.++## How it works++- `FakeWhatsApp::start()` binds a websocket port (noise XX responder, binary XML)+ and a plain-HTTP media port on `127.0.0.1`.+- The server's noise certificate chain is signed by a test root key, not+ WhatsApp's. Clients must use `test_signal()` (`TestSignal`): libsignal with+ WhatsApp's root key swapped for the test root when verifying. Nothing else is+ faked; prekey, pairing and message crypto are real.+- `add_user` creates a user with a phone (device 0, a `FakePhone` in process)+ that publishes prekeys. `FakePhone::scan_qr` approves a client's QR code; the+ client signs, gets the 515 restart and logs in as device 1, 2, ….+- Routing is LID-first like WhatsApp today: groups use `addressing_mode="lid"`,+ usync returns LIDs, delivered stanzas carry `participant_pn` / `sender_pn`.+- A message handed to a connection stays unacked until the device sends a+ receipt or message ack; if the connection drops it goes back to the offline+ queue and is delivered again on the next login, then `<ib><offline/>`.++## Start here++- `src/lib.rs`: the `FakeWhatsApp` API (users, groups, kick, drop connection,+ delivery counters, waiters).+- `src/state.rs`: routing, IQ handlers (encrypt, usync, w:g2, w:m), offline queues.+- `src/phone.rs`: fake phones: send/receive with real Signal, QR approval.+- `src/connection.rs`: handshake, login, `conflict replaced`, pairing.++## Checks++- `cargo test -p whatsapp_fake_server`++## Gotchas++- Never point it at real WhatsApp; it only listens on `127.0.0.1`.+- Waiting is event driven (`Shared::wait_for` re-checks on every state change);+ don't add sleep loops.+- Media: clients set `ClientConfig::media_base_url` to `media_base_url()`+ (plain HTTP); phones upload and download with whatsmeow's own media crypto.+- Unhandled IQs get an empty `result`; add a handler in `State::handle_iq` when+ the port starts parsing a response.++## Related++- `crates/whatsmeow/AGENTS.md`, `crates/whatsmeow_signal/AGENTS.md`,+ `crates/whatsapp_e2e/AGENTS.md`diff --git a/crates/whatsapp_fake_server/CLAUDE.md b/crates/whatsapp_fake_server/CLAUDE.mdnew file mode 120000index 0000000000..47dc3e3d86--- /dev/null+++ b/crates/whatsapp_fake_server/CLAUDE.md@@ -0,0 +1 @@+AGENTS.md\ No newline at end of filediff --git a/crates/whatsapp_fake_server/Cargo.toml b/crates/whatsapp_fake_server/Cargo.tomlnew file mode 100644index 0000000000..0ebbdbce23--- /dev/null+++ b/crates/whatsapp_fake_server/Cargo.toml@@ -0,0 +1,32 @@+[package]+edition = "2024"+name = "whatsapp_fake_server"+publish = false+version = "0.1.0"+description = "Test support: a fake WhatsApp server and fake phones that speak the real protocol, for testing the whatsmeow port and the session service without WhatsApp."++[dependencies]+aes-gcm = { workspace = true }+async-trait = { workspace = true }+axum = { workspace = true }+base64 = { workspace = true }+chrono = { workspace = true }+futures = { workspace = true }+hmac = { workspace = true }+nom = { workspace = true }+prost = { workspace = true }+rand = { workspace = true }+reqwest = { workspace = true }+serde_json = { workspace = true }+sha2 = { workspace = true }+thiserror = { workspace = true }+tokio = { workspace = true, features = ["macros", "net", "rt", "sync", "time"] }+tokio-tungstenite = { workspace = true }+tokio-util = { workspace = true, features = ["rt"] }+tracing = { workspace = true }+whatsmeow = { path = "../whatsmeow", features = ["testing"] }+whatsmeow_signal = { path = "../whatsmeow_signal" }+workspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+tokio = { workspace = true, features = ["macros", "rt-multi-thread", "test-util"] }diff --git a/crates/whatsapp_fake_server/src/connection.rs b/crates/whatsapp_fake_server/src/connection.rsnew file mode 100644index 0000000000..f472e36590--- /dev/null+++ b/crates/whatsapp_fake_server/src/connection.rs@@ -0,0 +1,546 @@+//! One client connection: the noise handshake as responder, then encrypted+//! binary-XML nodes in both directions.++use std::sync::Arc;++use aes_gcm::Aes256Gcm;+use futures::stream::{SplitSink, SplitStream};+use futures::{SinkExt, StreamExt};+use hmac::{Hmac, Mac};+use prost::Message;+use sha2::Sha256;+use tokio::net::TcpStream;+use tokio::sync::mpsc;+use tokio_tungstenite::WebSocketStream;+use tokio_tungstenite::tungstenite::Message as WebSocketMessage;+use whatsmeow::binary::{Node, marshal, unmarshal, unpack};+use whatsmeow::pair::ADV_DEVICE_SIGNATURE_PREFIX;+use whatsmeow::proto::wa_adv::AdvSignedDeviceIdentity;+use whatsmeow::proto::wa_wa6::{ClientPayload, HandshakeMessage, handshake_message};+use whatsmeow::signal::CurveSignatures;+use whatsmeow::socket::{NOISE_START_PATTERN, NoiseHandshake, WA_CONN_HEADER, generate_iv};+use whatsmeow::util::gcmutil;+use whatsmeow::util::keys::new_key_pair;+use whatsmeow_signal::LibsignalCurveSignatures;++use crate::Shared;+use crate::stanza::{attr_string, children, content_bytes, node, now, text};+use crate::state::{+ ConnectionEntry, DeviceAddress, DeviceKind, Outbound, Pairing, device_jid, iq_result,+};++type Socket = WebSocketStream<TcpStream>;++/// How long a closed connection waits for the client's side of the close.+const CLOSE_GRACE: std::time::Duration = std::time::Duration::from_secs(5);++/// Splits websocket messages into frames: a 3-byte big-endian length, then+/// the frame. The first message also starts with the connection header.+#[derive(Default)]+struct FrameReader {+ header_seen: bool,+ buffer: Vec<u8>,+ frames: std::collections::VecDeque<Vec<u8>>,+}++impl FrameReader {+ fn push(&mut self, data: &[u8]) {+ let mut data = data;+ if !self.header_seen {+ self.header_seen = true;+ data = data.strip_prefix(&WA_CONN_HEADER[..]).unwrap_or(data);+ }+ self.buffer.extend_from_slice(data);+ while self.buffer.len() >= 3 {+ let length = usize::from(self.buffer[0]) << 16+ | usize::from(self.buffer[1]) << 8+ | usize::from(self.buffer[2]);+ if self.buffer.len() < 3 + length {+ break;+ }+ self.frames.push_back(self.buffer[3..3 + length].to_vec());+ self.buffer.drain(..3 + length);+ }+ }+}++async fn read_frame(stream: &mut SplitStream<Socket>, reader: &mut FrameReader) -> Option<Vec<u8>> {+ loop {+ if let Some(frame) = reader.frames.pop_front() {+ return Some(frame);+ }+ match stream.next().await? {+ Ok(WebSocketMessage::Binary(data)) => reader.push(&data),+ Ok(WebSocketMessage::Close(_)) | Err(_) => return None,+ Ok(_) => {}+ }+ }+}++fn frame(data: &[u8]) -> WebSocketMessage {+ let length = u32::try_from(data.len()).unwrap_or(u32::MAX).to_be_bytes();+ let mut message = length[1..].to_vec();+ message.extend_from_slice(data);+ WebSocketMessage::Binary(message.into())+}++struct Established {+ sink: SplitSink<Socket, WebSocketMessage>,+ stream: SplitStream<Socket>,+ reader: FrameReader,+ write_key: Aes256Gcm,+ read_key: Aes256Gcm,+ client_static: [u8; 32],+ payload: ClientPayload,+}++async fn handshake(shared: &Shared, socket: Socket) -> Option<Established> {+ let (mut sink, mut stream) = socket.split();+ let mut reader = FrameReader::default();+ let hello =+ HandshakeMessage::decode(read_frame(&mut stream, &mut reader).await?.as_slice()).ok()?;+ let client_ephemeral: [u8; 32] = hello.client_hello?.ephemeral?.try_into().ok()?;+ let server_ephemeral = new_key_pair();+ let server_static = &shared.identity.static_key;++ let mut noise = NoiseHandshake::start(NOISE_START_PATTERN, &WA_CONN_HEADER).ok()?;+ noise.authenticate(&client_ephemeral);+ noise.authenticate(&server_ephemeral.public);+ noise+ .mix_shared_secret_into_key(server_ephemeral.private, client_ephemeral)+ .ok()?;+ let static_ciphertext = noise.encrypt(&server_static.public).ok()?;+ noise+ .mix_shared_secret_into_key(server_static.private, client_ephemeral)+ .ok()?;+ let certificate_ciphertext = noise.encrypt(&shared.identity.certificate_chain).ok()?;+ let server_hello = HandshakeMessage {+ server_hello: Some(handshake_message::ServerHello {+ ephemeral: Some(server_ephemeral.public.to_vec()),+ r#static: Some(static_ciphertext),+ payload: Some(certificate_ciphertext),+ ..Default::default()+ }),+ ..Default::default()+ };+ sink.send(frame(&server_hello.encode_to_vec())).await.ok()?;++ let finish = HandshakeMessage::decode(read_frame(&mut stream, &mut reader).await?.as_slice())+ .ok()?+ .client_finish?;+ let client_static: [u8; 32] = noise.decrypt(&finish.r#static?).ok()?.try_into().ok()?;+ noise+ .mix_shared_secret_into_key(server_ephemeral.private, client_static)+ .ok()?;+ let payload = ClientPayload::decode(noise.decrypt(&finish.payload?).ok()?.as_slice()).ok()?;+ let (client_write, client_read) = noise.transport_keys();+ Some(Established {+ sink,+ stream,+ reader,+ write_key: gcmutil::prepare(&client_read).ok()?,+ read_key: gcmutil::prepare(&client_write).ok()?,+ client_static,+ payload,+ })+}++/// Serves one accepted TCP connection until it closes.+pub(crate) async fn serve(shared: Arc<Shared>, tcp: TcpStream) {+ let Ok(socket) = tokio_tungstenite::accept_async(tcp).await else {+ tracing::debug!("fake server websocket upgrade failed");+ return;+ };+ let Some(established) = handshake(&shared, socket).await else {+ tracing::debug!("fake server noise handshake failed");+ return;+ };+ let Established {+ mut sink,+ mut stream,+ mut reader,+ write_key,+ read_key,+ client_static,+ payload,+ } = established;++ let (outbound, mut outbound_queue) = mpsc::unbounded_channel();+ let connection_id = {+ let mut state = shared.lock();+ state.next_connection += 1;+ let id = state.next_connection;+ state.connections.insert(+ id,+ ConnectionEntry {+ outbound,+ device: None,+ pairing: None,+ },+ );+ id+ };++ let writer_done = tokio_util::sync::CancellationToken::new();+ let writer_guard = writer_done.clone().drop_guard();+ let writer = tokio::spawn(async move {+ let _done = writer_guard;+ let mut counter = 0u32;+ while let Some(item) = outbound_queue.recv().await {+ match item {+ Outbound::Node(node) => {+ let Ok(plaintext) = marshal(&node) else {+ tracing::warn!(tag = %node.tag, "fake server failed to marshal a node");+ continue;+ };+ let Ok(ciphertext) =+ gcmutil::seal(&write_key, &generate_iv(counter), &plaintext, &[])+ else {+ break;+ };+ counter += 1;+ if sink.send(frame(&ciphertext)).await.is_err() {+ break;+ }+ }+ Outbound::Close => {+ let _ = sink.close().await;+ break;+ }+ }+ }+ });++ match (payload.username, payload.device) {+ (Some(username), device) if payload.device_pairing_data.is_none() => {+ shared.login(+ connection_id,+ DeviceAddress {+ phone: username.to_string(),+ device: u16::try_from(device.unwrap_or_default()).unwrap_or_default(),+ },+ client_static,+ );+ }+ _ => shared.start_pairing(connection_id),+ }++ let mut counter = 0u32;+ loop {+ // After the server closes, keep reading until the client finishes the+ // close handshake: dropping the socket while the client still has+ // unread frames (a 515 restart, a conflict) makes the kernel send an+ // RST, which discards them.+ let ciphertext = tokio::select! {+ frame = read_frame(&mut stream, &mut reader) => frame,+ () = async {+ writer_done.cancelled().await;+ tokio::time::sleep(CLOSE_GRACE).await;+ } => None,+ };+ let Some(ciphertext) = ciphertext else {+ break;+ };+ let Ok(plaintext) = gcmutil::open(&read_key, &generate_iv(counter), &ciphertext, &[])+ else {+ tracing::warn!("fake server failed to decrypt a frame");+ break;+ };+ counter += 1;+ let Some(node) = unpack(&plaintext)+ .ok()+ .and_then(|data| unmarshal(&data).ok())+ else {+ tracing::warn!("fake server failed to parse a node");+ continue;+ };+ shared.handle_node(connection_id, &node);+ }++ shared.connection_closed(connection_id);+ writer.abort();+}++impl Shared {+ /// Sends `<pair-device>` with fresh refs to a connection that registers.+ fn start_pairing(&self, connection: u64) {+ let mut state = self.lock();+ let refs: Vec<String> = (0..6)+ .map(|_| {+ let random: [u8; 12] = rand::random();+ let mut reference = String::from("2@");+ for byte in random {+ reference.push_str(&format!("{byte:02x}"));+ }+ reference+ })+ .collect();+ for reference in &refs {+ state.pairing_refs.insert(reference.clone(), connection);+ }+ let request = node(+ "iq",+ vec![+ ("from", crate::stanza::jid(&whatsmeow::types::SERVER_JID)),+ ("id", text(crate::stanza::random_id())),+ ("type", text("set")),+ ("xmlns", text("md")),+ ],+ children(vec![node(+ "pair-device",+ vec![],+ children(+ refs.iter()+ .map(|reference| {+ node("ref", vec![], crate::stanza::bytes(reference.as_bytes()))+ })+ .collect(),+ ),+ )]),+ );+ if let Some(entry) = state.connections.get_mut(&connection) {+ entry.pairing = Some(Pairing { refs });+ }+ state.send(connection, request);+ drop(state);+ self.changed();+ }++ /// Logs a paired device in, replacing an older connection of the device.+ fn login(&self, connection: u64, address: DeviceAddress, client_static: [u8; 32]) {+ let mut state = self.lock();+ let known = matches!(+ state.device(&address).map(|device| &device.kind),+ Some(DeviceKind::Linked { noise_key }) if *noise_key == client_static+ );+ if !known {+ state.send(+ connection,+ node("failure", vec![("reason", text("401"))], None),+ );+ state.close(connection);+ return;+ }+ let previous = state.device(&address).and_then(|device| device.connection);+ if let Some(previous) = previous {+ state.send(+ previous,+ node(+ "stream:error",+ vec![],+ children(vec![node(+ "conflict",+ vec![("type", text("replaced"))],+ None,+ )]),+ ),+ );+ state.close(previous);+ if let Some(entry) = state.connections.get_mut(&previous) {+ entry.device = None;+ }+ state.detach(&address);+ }+ if let Some(entry) = state.connections.get_mut(&connection) {+ entry.device = Some(address.clone());+ }+ let lid = state.jid_of(&address, true);+ state.send(+ connection,+ node(+ "success",+ vec![("t", now()), ("lid", crate::stanza::jid(&lid))],+ None,+ ),+ );+ let queued: Vec<Node> = state+ .device_mut(&address)+ .map(|device| {+ device.connection = Some(connection);+ device.offline.drain(..).collect()+ })+ .unwrap_or_default();+ let count = queued.len();+ for node in queued {+ state.deliver(&address, node);+ }+ state.send(+ connection,+ node(+ "ib",+ vec![],+ children(vec![node(+ "offline",+ vec![("count", text(count.to_string()))],+ None,+ )]),+ ),+ );+ drop(state);+ self.changed();+ }++ fn connection_closed(&self, connection: u64) {+ let mut state = self.lock();+ let Some(entry) = state.connections.remove(&connection) else {+ return;+ };+ if let Some(pairing) = entry.pairing {+ for reference in pairing.refs {+ state.pairing_refs.remove(&reference);+ }+ }+ if let Some(address) = entry.device+ && state+ .device(&address)+ .is_some_and(|device| device.connection == Some(connection))+ {+ state.detach(&address);+ }+ drop(state);+ self.changed();+ }++ fn handle_node(&self, connection: u64, node: &Node) {+ let mut state = self.lock();+ let Some(entry) = state.connections.get(&connection) else {+ return;+ };+ let Some(address) = entry.device.clone() else {+ drop(state);+ self.handle_pairing_node(connection, node);+ return;+ };+ match node.tag.as_str() {+ "iq" => {+ let response = state.handle_iq(&address, node);+ state.send(connection, response);+ }+ "message" => {+ state+ .submitted+ .push((address.clone(), attr_string(node, "id")));+ let ack = state.route_message(&address, node);+ if !state.withheld_acks.contains(&address) {+ state.send(connection, ack);+ }+ }+ "receipt" => {+ state.acked(&address, &attr_string(node, "id"));+ state.route_receipt(&address, node);+ }+ "ack" if attr_string(node, "class") == "message" => {+ state.acked(&address, &attr_string(node, "id"));+ }+ _ => {}+ }+ drop(state);+ self.changed();+ }++ /// Nodes from a connection that hasn't logged in: the client's answer to+ /// a pair-success, or anything else (answered with an empty result).+ fn handle_pairing_node(&self, connection: u64, request: &Node) {+ if request.tag != "iq" {+ return;+ }+ let iq_type = attr_string(request, "type");+ let id = attr_string(request, "id");+ let mut state = self.lock();+ if iq_type == "get" || iq_type == "set" {+ let response = iq_result(request, vec![]);+ state.send(connection, response);+ return;+ }+ let Some(pending) = state.pending_pairs.remove(&id) else {+ return;+ };+ let result = if iq_type == "result" {+ verify_device_signature(+ request,+ &pending.details,+ &pending.identity_key,+ &pending.account_public,+ )+ } else {+ Err(crate::PairError::Rejected(+ request+ .get_optional_child_by_tag(&["error"])+ .map(|error| attr_string(error, "text"))+ .unwrap_or_default(),+ ))+ };+ let result = result.map(|()| {+ if let Some(user) = state.users.get_mut(&pending.address.phone) {+ user.devices.insert(+ pending.address.device,+ crate::state::DeviceRecord {+ kind: DeviceKind::Linked {+ noise_key: pending.noise_key,+ },+ keys: None,+ connection: None,+ offline: Default::default(),+ unacked: Vec::new(),+ },+ );+ }+ if let Some(entry) = state.connections.get_mut(&pending.connection)+ && let Some(pairing) = entry.pairing.take()+ {+ for reference in pairing.refs {+ state.pairing_refs.remove(&reference);+ }+ }+ state.send(+ pending.connection,+ node("stream:error", vec![("code", text("515"))], None),+ );+ state.close(pending.connection);+ device_jid(&pending.address.phone, pending.address.device, false)+ });+ let _ = pending.reply.send(result);+ drop(state);+ self.changed();+ }+}++/// Checks the device's signature in the client's `<pair-device-sign>`.+fn verify_device_signature(+ response: &Node,+ details: &[u8],+ identity_key: &[u8; 32],+ account_public: &[u8; 32],+) -> Result<(), crate::PairError> {+ let signed = response+ .get_optional_child_by_tag(&["pair-device-sign", "device-identity"])+ .and_then(content_bytes)+ .and_then(|data| AdvSignedDeviceIdentity::decode(data).ok())+ .ok_or(crate::PairError::BadDeviceSignature)?;+ let signature: [u8; 64] = signed+ .device_signature+ .as_deref()+ .and_then(|signature| signature.try_into().ok())+ .ok_or(crate::PairError::BadDeviceSignature)?;+ let message = [+ &ADV_DEVICE_SIGNATURE_PREFIX[..],+ details,+ identity_key,+ account_public,+ ]+ .concat();+ if LibsignalCurveSignatures::new().verify_signature(identity_key, &message, &signature) {+ Ok(())+ } else {+ Err(crate::PairError::BadDeviceSignature)+ }+}++/// The HMAC of a signed device identity under the device's ADV secret.+pub(crate) fn adv_hmac(adv_secret: &[u8], signed: &[u8]) -> Vec<u8> {+ let Ok(mut mac) = <Hmac<Sha256> as Mac>::new_from_slice(adv_secret) else {+ return Vec::new();+ };+ mac.update(signed);+ mac.finalize().into_bytes().to_vec()+}diff --git a/crates/whatsapp_fake_server/src/lib.rs b/crates/whatsapp_fake_server/src/lib.rsnew file mode 100644index 0000000000..a17da2597e--- /dev/null+++ b/crates/whatsapp_fake_server/src/lib.rs@@ -0,0 +1,582 @@+//! A fake WhatsApp server for tests, and fake phones that use it.+//!+//! [`FakeWhatsApp::start`] listens on local ports: a websocket that speaks+//! WhatsApp's multidevice protocol (noise handshake, binary XML, pairing,+//! prekeys, usync, groups, message routing, receipts, offline queues) and a+//! plain-HTTP media host. Real whatsmeow clients connect to+//! [`FakeWhatsApp::websocket_url`] with [`test_signal`] as their Signal+//! implementation.+//!+//! The other people in a conversation are [`FakePhone`]s: in-process+//! primary devices with real libsignal sessions, so what a client sends is+//! really encrypted and really decrypted. Nothing here talks to WhatsApp.++#![deny(missing_docs)]++mod connection;+mod media;+mod phone;+mod signal;+mod stanza;+mod state;+#[cfg(test)]+mod test;++use std::net::SocketAddr;+use std::sync::{Arc, Mutex, MutexGuard, PoisonError};+use std::time::Duration;++use tokio::net::TcpListener;+use tokio::sync::{mpsc, watch};+use tokio_util::sync::CancellationToken;+use tokio_util::task::TaskTracker;+use whatsmeow::WebSocketDialer;+use whatsmeow::signal::SignalError;+use whatsmeow::store::{MemoryStore, StoreError};+use whatsmeow::types::Jid;+use whatsmeow_signal::LibsignalCurveSignatures;++pub use phone::{FakePhone, ReceivedMessage, ReceivedReceipt};+pub use signal::{TestSignal, test_root_key, test_signal};++use crate::phone::PhoneCore;+use crate::signal::ServerIdentity;+use crate::stanza::{children, jid, node, now, random_id, text};+use crate::state::{+ DeviceAddress, DeviceKeys, DeviceKind, DeviceRecord, Group, State, User, device_jid, group_jid,+};++/// How many one-time prekeys a fake phone publishes.+const PHONE_PRE_KEYS: u32 = 200;++/// Why a phone couldn't approve a pairing QR code.+#[derive(Debug, thiserror::Error, PartialEq, Eq)]+pub enum PairError {+ /// The string isn't a WhatsApp pairing link.+ #[error("not a pairing QR code")]+ MalformedQr,+ /// No connection is showing this QR code (expired, or already used).+ #[error("no client is showing this QR code")]+ UnknownQr,+ /// The client answered the pair-success with an error.+ #[error("the client rejected the pairing: {0}")]+ Rejected(String),+ /// The client's device signature didn't verify.+ #[error("the client's device signature is invalid")]+ BadDeviceSignature,+ /// The client didn't answer the pair-success.+ #[error("the client didn't confirm the pairing")]+ NoConfirmation,+}++/// Why a phone couldn't send a message.+#[derive(Debug, thiserror::Error)]+pub enum SendError {+ /// The chat isn't a known user or group.+ #[error("unknown chat")]+ UnknownChat,+ /// The phone's user isn't a member of the group.+ #[error("not a member of the group")]+ NotInGroup,+ /// The server rejected the message with an error code.+ #[error("the server rejected the message: {0}")]+ Rejected(String),+ /// Signal encryption failed.+ #[error("signal: {0}")]+ Signal(#[source] SignalError),+ /// The phone's key store failed.+ #[error("store: {0}")]+ Store(#[source] StoreError),+ /// Uploading an attachment to the media host failed.+ #[error("media upload: {0}")]+ Upload(#[source] whatsmeow::upload::UploadError),+}++impl SendError {+ fn signal(error: SignalError) -> SendError {+ SendError::Signal(error)+ }++ fn store(error: StoreError) -> SendError {+ SendError::Store(error)+ }+}++/// State shared by the listeners, connections and phones.+pub(crate) struct Shared {+ state: Mutex<State>,+ identity: ServerIdentity,+ /// Bumped on every change, so tests can wait for conditions.+ generation: watch::Sender<u64>,+ tasks: TaskTracker,+ cancel: CancellationToken,+}++impl Shared {+ pub(crate) fn lock(&self) -> MutexGuard<'_, State> {+ // Every mutation is a few map operations under the lock; a panic in+ // one leaves the maps usable.+ self.state.lock().unwrap_or_else(PoisonError::into_inner)+ }++ pub(crate) fn changed(&self) {+ self.generation.send_modify(|generation| *generation += 1);+ }++ pub(crate) fn media_host(&self) -> String {+ self.lock().media_host.clone()+ }++ /// Waits until `check` returns `Some`, re-checking after every change.+ pub(crate) async fn wait_for<Found>(+ &self,+ timeout: Duration,+ check: impl Fn() -> Option<Found>,+ ) -> Option<Found> {+ let mut changes = self.generation.subscribe();+ let wait = async {+ loop {+ if let Some(found) = check() {+ return found;+ }+ if changes.changed().await.is_err() {+ std::future::pending::<()>().await;+ }+ }+ };+ tokio::time::timeout(timeout, wait).await.ok()+ }+}++/// A running fake WhatsApp server. Dropping it stops the listeners and+/// every connection.+pub struct FakeWhatsApp {+ shared: Arc<Shared>,+ websocket_address: SocketAddr,+}++impl Drop for FakeWhatsApp {+ fn drop(&mut self) {+ self.shared.cancel.cancel();+ }+}++impl FakeWhatsApp {+ /// Starts the server on random local ports.+ pub async fn start() -> std::io::Result<FakeWhatsApp> {+ let websocket_listener = TcpListener::bind("127.0.0.1:0").await?;+ let media_listener = TcpListener::bind("127.0.0.1:0").await?;+ let websocket_address = websocket_listener.local_addr()?;+ let media_address = media_listener.local_addr()?;+ let shared = Arc::new(Shared {+ state: Mutex::new(State {+ media_host: media_address.to_string(),+ ..State::default()+ }),+ identity: ServerIdentity::generate(),+ generation: watch::Sender::new(0),+ tasks: TaskTracker::new(),+ cancel: CancellationToken::new(),+ });++ let accepting = Arc::clone(&shared);+ shared.tasks.spawn(async move {+ loop {+ let accepted = tokio::select! {+ () = accepting.cancel.cancelled() => return,+ accepted = websocket_listener.accept() => accepted,+ };+ let Ok((tcp, _)) = accepted else {+ continue;+ };+ let connection = Arc::clone(&accepting);+ let cancel = accepting.cancel.clone();+ accepting.tasks.spawn(async move {+ tokio::select! {+ () = cancel.cancelled() => {}+ () = connection::serve(connection.clone(), tcp) => {}+ }+ });+ }+ });++ let media = media::router(Arc::clone(&shared));+ let cancel = shared.cancel.clone();+ shared.tasks.spawn(async move {+ let served = axum::serve(media_listener, media)+ .with_graceful_shutdown(async move { cancel.cancelled().await })+ .await;+ if let Err(error) = served {+ tracing::warn!(error = %error, "fake media server stopped");+ }+ });++ Ok(FakeWhatsApp {+ shared,+ websocket_address,+ })+ }++ /// The websocket URL to dial instead of `wss://web.whatsapp.com/ws/chat`.+ pub fn websocket_url(&self) -> String {+ format!("ws://{}/ws/chat", self.websocket_address)+ }++ /// A dialer for [`whatsmeow::ClientConfig::dialer`].+ pub fn dialer(&self) -> Arc<WebSocketDialer> {+ Arc::new(WebSocketDialer {+ url: self.websocket_url(),+ })+ }++ /// `host:port` of the plain-HTTP media host the server hands out in+ /// media connections.+ pub fn media_host(&self) -> String {+ self.shared.media_host()+ }++ /// The base URL of the media host, for [`whatsmeow::ClientConfig::media_base_url`].+ pub fn media_base_url(&self) -> String {+ format!("http://{}", self.media_host())+ }++ /// Adds a user with a phone (device 0) that publishes prekeys, and+ /// returns the phone.+ pub async fn add_user(&self, phone: &str, push_name: &str) -> FakePhone {+ let store = Arc::new(MemoryStore::default());+ let signatures = LibsignalCurveSignatures::new();+ let device = store.new_device(&signatures);+ let pre_keys = match device.pre_keys.get_or_gen_pre_keys(PHONE_PRE_KEYS).await {+ Ok(pre_keys) => pre_keys,+ Err(error) => {+ tracing::error!(error = %error, "memory store failed to generate prekeys");+ Vec::new()+ }+ };+ if let Some(last) = pre_keys.last()+ && let Err(error) = device.pre_keys.mark_pre_keys_as_uploaded(last.key_id).await+ {+ tracing::error!(error = %error, "memory store failed to mark prekeys");+ }+ let keys = DeviceKeys {+ registration_id: device.registration_id,+ identity: device.identity_key.public,+ signed_pre_key: (+ device.signed_pre_key.key_id,+ device.signed_pre_key.key_pair.public,+ device.signed_pre_key.signature.unwrap_or([0; 64]),+ ),+ pre_keys: pre_keys+ .iter()+ .map(|key| (key.key_id, key.key_pair.public))+ .collect(),+ };+ let account = device.identity_key.clone();+ let (inbox, mut deliveries) = mpsc::unbounded_channel();+ let lid = {+ let mut state = self.shared.lock();+ let lid = format!("{}", 100_000_000_000_000 + state.number());+ state.lids.insert(lid.clone(), phone.to_owned());+ let mut devices = std::collections::BTreeMap::new();+ devices.insert(+ 0,+ DeviceRecord {+ kind: DeviceKind::Phone(inbox),+ keys: Some(keys),+ connection: None,+ offline: Default::default(),+ unacked: Vec::new(),+ },+ );+ state.users.insert(+ phone.to_owned(),+ User {+ phone: phone.to_owned(),+ lid: lid.clone(),+ push_name: push_name.to_owned(),+ account,+ devices,+ next_device: 0,+ },+ );+ lid+ };+ let fake_phone = FakePhone {+ shared: Arc::clone(&self.shared),+ core: Arc::new(PhoneCore::new(phone.to_owned(), lid, device)),+ };+ let worker = fake_phone.clone();+ let cancel = self.shared.cancel.clone();+ self.shared.tasks.spawn(async move {+ loop {+ let delivered = tokio::select! {+ () = cancel.cancelled() => return,+ delivered = deliveries.recv() => delivered,+ };+ let Some(delivered) = delivered else {+ return;+ };+ worker.handle(delivered).await;+ }+ });+ self.shared.changed();+ fake_phone+ }++ /// Creates a group of the given phones' users (the first is the+ /// creator) and notifies their linked devices; returns the group JID.+ pub fn create_group(&self, subject: &str, members: &[&FakePhone]) -> Jid {+ let mut state = self.shared.lock();+ let id = format!("120363{:012}", state.number());+ let created = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|elapsed| elapsed.as_secs())+ .unwrap_or_default();+ let group = Group {+ id: id.clone(),+ subject: subject.to_owned(),+ creator: members+ .first()+ .map(|phone| phone.phone().to_owned())+ .unwrap_or_default(),+ created,+ members: members+ .iter()+ .map(|phone| phone.phone().to_owned())+ .collect(),+ };+ let group_node = state.group_node(&group);+ let creator = state+ .users+ .get(&group.creator)+ .map(|user| device_jid(&user.lid, 0, true))+ .unwrap_or_default();+ let targets: Vec<DeviceAddress> = group+ .members+ .iter()+ .filter_map(|member| state.users.get(member))+ .flat_map(|user| {+ user.devices.keys().map(|device| DeviceAddress {+ phone: user.phone.clone(),+ device: *device,+ })+ })+ .collect();+ state.groups.insert(id.clone(), group);+ let group = group_jid(&id);+ for target in targets {+ let notification = node(+ "notification",+ vec![+ ("from", jid(&group)),+ ("id", text(random_id())),+ ("t", now()),+ ("type", text("w:gp2")),+ ("participant", jid(&creator)),+ ],+ children(vec![node(+ "create",+ vec![],+ children(vec![group_node.clone()]),+ )]),+ );+ state.deliver(&target, notification);+ }+ drop(state);+ self.shared.changed();+ group+ }++ /// The linked devices of a user, as phone-number device JIDs.+ pub fn linked_devices(&self, phone: &FakePhone) -> Vec<Jid> {+ let state = self.shared.lock();+ state+ .users+ .get(phone.phone())+ .map(|user| {+ user.devices+ .keys()+ .filter(|device| **device != 0)+ .map(|device| device_jid(&user.phone, *device, false))+ .collect()+ })+ .unwrap_or_default()+ }++ /// Whether a linked device is logged in right now.+ pub fn is_online(&self, device: &Jid) -> bool {+ let state = self.shared.lock();+ state+ .resolve(device)+ .and_then(|address| state.device(&address))+ .is_some_and(|record| record.connection.is_some())+ }++ /// Waits until a linked device is logged in.+ pub async fn wait_until_online(&self, device: &Jid, timeout: Duration) -> bool {+ self.shared+ .wait_for(timeout, || self.is_online(device).then_some(()))+ .await+ .is_some()+ }++ /// Waits until a linked device is no longer logged in.+ pub async fn wait_until_offline(&self, device: &Jid, timeout: Duration) -> bool {+ self.shared+ .wait_for(timeout, || (!self.is_online(device)).then_some(()))+ .await+ .is_some()+ }++ /// Waits until a linked device has uploaded prekeys, which a client does+ /// right after its first login.+ pub async fn wait_until_keys_uploaded(&self, device: &Jid, timeout: Duration) -> bool {+ self.shared+ .wait_for(timeout, || {+ let state = self.shared.lock();+ let address = state.resolve(device)?;+ state.device(&address)?.keys.as_ref().map(|_| ())+ })+ .await+ .is_some()+ }++ /// How many times a message was handed to a linked device's connection+ /// (redeliveries after a dropped connection count again).+ pub fn delivery_count(&self, device: &Jid, message_id: &str) -> usize {+ let state = self.shared.lock();+ let Some(address) = state.resolve(device) else {+ return 0;+ };+ state+ .delivered+ .iter()+ .filter(|(target, id)| *target == address && id == message_id)+ .count()+ }++ /// How many times a linked device submitted a message with this ID.+ pub fn submission_count(&self, device: &Jid, message_id: &str) -> usize {+ let state = self.shared.lock();+ let Some(address) = state.resolve(device) else {+ return 0;+ };+ state+ .submitted+ .iter()+ .filter(|(sender, id)| *sender == address && id == message_id)+ .count()+ }++ /// IDs of the messages a linked device submitted, in order.+ pub fn submissions(&self, device: &Jid) -> Vec<String> {+ let state = self.shared.lock();+ let Some(address) = state.resolve(device) else {+ return Vec::new();+ };+ state+ .submitted+ .iter()+ .filter(|(sender, _)| *sender == address)+ .map(|(_, id)| id.clone())+ .collect()+ }++ /// Waits until a linked device has submitted at least `count` messages.+ pub async fn wait_for_submissions(+ &self,+ device: &Jid,+ count: usize,+ timeout: Duration,+ ) -> bool {+ self.shared+ .wait_for(timeout, || {+ (self.submissions(device).len() >= count).then_some(())+ })+ .await+ .is_some()+ }++ /// While set, messages a linked device submits are still routed but the+ /// server never acks them, as when the connection dies right after the+ /// submission reached WhatsApp.+ pub fn withhold_acks(&self, device: &Jid, withhold: bool) {+ let mut state = self.shared.lock();+ let Some(address) = state.resolve(device) else {+ return;+ };+ if withhold {+ state.withheld_acks.insert(address);+ } else {+ state.withheld_acks.remove(&address);+ }+ }++ /// Messages queued for a linked device while it is offline or unacked.+ pub fn pending_count(&self, device: &Jid) -> usize {+ let state = self.shared.lock();+ state+ .resolve(device)+ .and_then(|address| state.device(&address))+ .map_or(0, |record| record.offline.len() + record.unacked.len())+ }++ /// Waits until a linked device has acked everything delivered to it and+ /// nothing is queued for it.+ pub async fn wait_until_acked(&self, device: &Jid, timeout: Duration) -> bool {+ self.shared+ .wait_for(timeout, || (self.pending_count(device) == 0).then_some(()))+ .await+ .is_some()+ }++ /// Closes a linked device's connection, as a network failure would. The+ /// client reconnects on its own.+ pub fn drop_connection(&self, device: &Jid) {+ let state = self.shared.lock();+ if let Some(connection) = state+ .resolve(device)+ .and_then(|address| state.device(&address))+ .and_then(|record| record.connection)+ {+ state.close(connection);+ }+ }++ /// Removes a linked device, as "Log out" on the phone does: the device+ /// gets a `device_removed` stream error and can't log in again.+ pub fn kick(&self, device: &Jid) {+ let mut state = self.shared.lock();+ let Some(address) = state.resolve(device).filter(|address| address.device != 0) else {+ return;+ };+ let connection = state.device(&address).and_then(|record| record.connection);+ if let Some(connection) = connection {+ state.send(+ connection,+ node(+ "stream:error",+ vec![("code", text("401"))],+ children(vec![node(+ "conflict",+ vec![("type", text("device_removed"))],+ None,+ )]),+ ),+ );+ state.close(connection);+ }+ if let Some(user) = state.users.get_mut(&address.phone) {+ user.devices.remove(&address.device);+ }+ drop(state);+ self.shared.changed();+ }++ /// Stops the listeners and closes every connection.+ pub async fn shutdown(self) {+ self.shared.cancel.cancel();+ self.shared.tasks.close();+ self.shared.tasks.wait().await;+ }+}diff --git a/crates/whatsapp_fake_server/src/media.rs b/crates/whatsapp_fake_server/src/media.rsnew file mode 100644index 0000000000..9b401d0d37--- /dev/null+++ b/crates/whatsapp_fake_server/src/media.rs@@ -0,0 +1,41 @@+//! The media host: uploads land in memory and downloads read them back,+//! over plain HTTP on a local port.++use std::sync::Arc;++use axum::Router;+use axum::body::Bytes;+use axum::extract::{Path, State};+use axum::http::StatusCode;+use axum::response::{IntoResponse, Response};+use axum::routing::{get, post};++use crate::Shared;++/// The router: `POST /mms/{type}/{token}` uploads (also with a+/// `newsletter`/other prefix), `GET /v/{id}` downloads.+pub(crate) fn router(shared: Arc<Shared>) -> Router {+ Router::new()+ .route("/{prefix}/{media_type}/{token}", post(upload))+ .route("/v/fake/{id}", get(download))+ .with_state(shared)+}++async fn upload(+ State(shared): State<Arc<Shared>>,+ Path((_prefix, _media_type, _token)): Path<(String, String, String)>,+ body: Bytes,+) -> Response {+ let id = crate::stanza::random_id();+ let direct_path = format!("/v/fake/{id}?ccb=11-4");+ shared.lock().media.insert(id, body.to_vec());+ let url = format!("http://{}{direct_path}", shared.media_host());+ axum::Json(serde_json::json!({ "url": url, "direct_path": direct_path })).into_response()+}++async fn download(State(shared): State<Arc<Shared>>, Path(id): Path<String>) -> Response {+ match shared.lock().media.get(&id) {+ Some(data) => data.clone().into_response(),+ None => StatusCode::NOT_FOUND.into_response(),+ }+}diff --git a/crates/whatsapp_fake_server/src/phone.rs b/crates/whatsapp_fake_server/src/phone.rsnew file mode 100644index 0000000000..e88ad15b48--- /dev/null+++ b/crates/whatsapp_fake_server/src/phone.rs@@ -0,0 +1,821 @@+//! Fake phones: the primary device (device 0) of each fake user, played in+//! process with real Signal sessions and sender keys from `whatsmeow_signal`.+//!+//! A phone sends and receives like any WhatsApp device: it fetches prekey+//! bundles from the server, encrypts per device, distributes sender keys in+//! groups, decrypts what it receives and sends delivery receipts. It also+//! approves pairing QR codes, like scanning one in "Linked devices".++use std::sync::{Arc, Mutex, PoisonError};+use std::time::Duration;++use base64::Engine;+use base64::engine::general_purpose::STANDARD;+use nom::IResult;+use nom::Parser;+use nom::bytes::complete::{is_not, tag, take_until};+use nom::multi::separated_list1;+use prost::Message as _;+use tokio::sync::oneshot;+use whatsmeow::binary::Node;+use whatsmeow::download::{DownloadError, MediaHttpClient, MediaType, download};+use whatsmeow::mediaconn::{MediaConn, MediaConnHost};+use whatsmeow::pair::ADV_ACCOUNT_SIGNATURE_PREFIX;+use whatsmeow::proto::wa_adv::{+ AdvDeviceIdentity, AdvSignedDeviceIdentity, AdvSignedDeviceIdentityHmac,+};+use whatsmeow::proto::wa_e2e::{+ DeviceSentMessage, ImageMessage, Message, SenderKeyDistributionMessage,+};+use whatsmeow::signal::{+ CiphertextType, CurveSignatures, SenderKeyName, SignalAddress, SignalProtocol,+ SignalProtocolStore,+};+use whatsmeow::store::Device;+use whatsmeow::types::{GROUP_SERVER, Jid, SERVER_JID};+use whatsmeow::upload::upload;+use whatsmeow_signal::{LibsignalCurveSignatures, LibsignalProtocol};++use crate::connection::adv_hmac;+use crate::stanza::{+ attr_jid, attr_string, bytes, children, content_bytes, jid, node, now, random_id, text,+};+use crate::state::{DeviceAddress, PendingPair, device_jid, group_jid};+use crate::{PairError, SendError, Shared};++/// How long a phone waits for the client to sign a pair-success.+const PAIR_CONFIRMATION_TIMEOUT: Duration = Duration::from_secs(30);++/// A message a phone received and decrypted.+#[derive(Clone, Debug)]+pub struct ReceivedMessage {+ /// The message ID.+ pub id: String,+ /// The chat: the group, or the other user in a direct chat.+ pub chat: Jid,+ /// The sending device, as a LID device JID.+ pub sender: Jid,+ /// The sending user's phone number JID.+ pub sender_phone: Jid,+ /// The decrypted message.+ pub message: Message,+}++impl ReceivedMessage {+ /// The message text, for plain and extended text messages.+ pub fn text(&self) -> Option<&str> {+ self.message.conversation.as_deref().or_else(|| {+ self.message+ .extended_text_message+ .as_ref()+ .and_then(|extended| extended.text.as_deref())+ })+ }+}++/// A receipt a phone received for a message it sent.+#[derive(Clone, Debug, PartialEq, Eq)]+pub struct ReceivedReceipt {+ /// The message ID.+ pub id: String,+ /// The device that sent the receipt.+ pub from: Jid,+ /// The receipt type; empty for delivery receipts.+ pub receipt_type: String,+}++#[derive(Default)]+pub(crate) struct Inbox {+ pub(crate) messages: Vec<ReceivedMessage>,+ pub(crate) receipts: Vec<ReceivedReceipt>,+ /// Why stanzas failed to decrypt, for test diagnostics.+ pub(crate) failures: Vec<String>,+}++/// One fake user's phone.+pub(crate) struct PhoneCore {+ pub(crate) phone: String,+ pub(crate) lid: String,+ pub(crate) device: Device,+ signal: LibsignalProtocol,+ /// Serializes Signal operations, like whatsmeow's send lock.+ crypto: tokio::sync::Mutex<()>,+ pub(crate) inbox: Mutex<Inbox>,+}++impl PhoneCore {+ pub(crate) fn new(phone: String, lid: String, device: Device) -> PhoneCore {+ PhoneCore {+ phone,+ lid,+ device,+ signal: LibsignalProtocol::new(),+ crypto: tokio::sync::Mutex::new(()),+ inbox: Mutex::new(Inbox::default()),+ }+ }++ fn inbox(&self) -> std::sync::MutexGuard<'_, Inbox> {+ self.inbox.lock().unwrap_or_else(PoisonError::into_inner)+ }++ fn address(&self) -> DeviceAddress {+ DeviceAddress {+ phone: self.phone.clone(),+ device: 0,+ }+ }++ fn own_lid(&self) -> Jid {+ device_jid(&self.lid, 0, true)+ }+}++/// A fake user's phone. Cheap to clone.+#[derive(Clone)]+pub struct FakePhone {+ pub(crate) shared: Arc<Shared>,+ pub(crate) core: Arc<PhoneCore>,+}++impl FakePhone {+ /// The user's phone number JID (`<phone>@s.whatsapp.net`).+ pub fn jid(&self) -> Jid {+ device_jid(&self.core.phone, 0, false)+ }++ /// The user's LID JID (`<lid>@lid`).+ pub fn lid(&self) -> Jid {+ self.core.own_lid()+ }++ /// The phone number.+ pub fn phone(&self) -> &str {+ &self.core.phone+ }++ /// Every message the phone received so far.+ pub fn received(&self) -> Vec<ReceivedMessage> {+ self.core.inbox().messages.clone()+ }++ /// Every receipt the phone received so far.+ pub fn receipts(&self) -> Vec<ReceivedReceipt> {+ self.core.inbox().receipts.clone()+ }++ /// Why received stanzas failed to decrypt, if any did.+ pub fn failures(&self) -> Vec<String> {+ self.core.inbox().failures.clone()+ }++ /// Waits until the phone received a message matching `predicate`.+ pub async fn wait_for_message(+ &self,+ timeout: Duration,+ predicate: impl Fn(&ReceivedMessage) -> bool,+ ) -> Option<ReceivedMessage> {+ let core = Arc::clone(&self.core);+ self.shared+ .wait_for(timeout, move || {+ core.inbox()+ .messages+ .iter()+ .find(|message| predicate(message))+ .cloned()+ })+ .await+ }++ /// Waits until the phone received a receipt matching `predicate`.+ pub async fn wait_for_receipt(+ &self,+ timeout: Duration,+ predicate: impl Fn(&ReceivedReceipt) -> bool,+ ) -> Option<ReceivedReceipt> {+ let core = Arc::clone(&self.core);+ self.shared+ .wait_for(timeout, move || {+ core.inbox()+ .receipts+ .iter()+ .find(|receipt| predicate(receipt))+ .cloned()+ })+ .await+ }++ /// Sends a text message to a group or user; returns its ID.+ pub async fn send_text(&self, chat: &Jid, text: &str) -> Result<String, SendError> {+ self.send_message(+ chat,+ Message {+ conversation: Some(text.to_owned()),+ ..Message::default()+ },+ )+ .await+ }++ /// Uploads an image to the media host and sends it with a caption to a+ /// group or user; returns the message ID.+ pub async fn send_image(+ &self,+ chat: &Jid,+ image: &[u8],+ caption: &str,+ ) -> Result<String, SendError> {+ let uploaded = upload(+ &self.media_http(),+ &self.media_conn(),+ image,+ MediaType::Image,+ )+ .await+ .map_err(SendError::Upload)?;+ let message = Message {+ image_message: Some(Box::new(ImageMessage {+ url: Some(uploaded.url),+ direct_path: Some(uploaded.direct_path),+ media_key: Some(uploaded.media_key),+ file_enc_sha256: Some(uploaded.file_enc_sha256),+ file_sha256: Some(uploaded.file_sha256),+ file_length: Some(uploaded.file_length),+ mimetype: Some("image/jpeg".to_owned()),+ caption: Some(caption.to_owned()),+ ..ImageMessage::default()+ })),+ ..Message::default()+ };+ self.send_message(chat, message).await+ }++ /// Downloads and decrypts the image of a received message.+ pub async fn download_image(+ &self,+ received: &ReceivedMessage,+ ) -> Result<Vec<u8>, DownloadError> {+ let image = received+ .message+ .image_message+ .as_deref()+ .ok_or(DownloadError::NoUrlPresent)?;+ download(&self.media_http(), &self.media_conn(), image).await+ }++ fn media_http(&self) -> MediaHttpClient {+ MediaHttpClient::new(reqwest::Client::new(), String::new(), false)+ .with_base_url(&format!("http://{}", self.shared.media_host()))+ }++ fn media_conn(&self) -> MediaConn {+ MediaConn {+ auth: "fake-media-auth".to_owned(),+ auth_ttl: 21_600,+ ttl: 300,+ max_buckets: 12,+ fetched_at: chrono::Utc::now(),+ hosts: vec![MediaConnHost {+ hostname: self.shared.media_host(),+ }],+ }+ }++ /// Sends a message to a group or user; returns its ID.+ #[tracing::instrument(name = "fake_whatsapp.phone_send", skip_all, err)]+ pub async fn send_message(&self, chat: &Jid, message: Message) -> Result<String, SendError> {+ let id = random_id();+ let _crypto = self.core.crypto.lock().await;+ let stanza = if chat.server == GROUP_SERVER {+ self.group_stanza(chat, &id, &message).await?+ } else {+ self.direct_stanza(chat, &id, &message).await?+ };+ let ack = self+ .shared+ .lock()+ .route_message(&self.core.address(), &stanza);+ self.shared.changed();+ match attr_string(&ack, "error").as_str() {+ "" => Ok(id),+ code => Err(SendError::Rejected(code.to_owned())),+ }+ }++ async fn group_stanza(+ &self,+ chat: &Jid,+ id: &str,+ message: &Message,+ ) -> Result<Node, SendError> {+ let targets = {+ let state = self.shared.lock();+ let group = state.groups.get(&chat.user).ok_or(SendError::UnknownChat)?;+ if !group.members.contains(&self.core.phone) {+ return Err(SendError::NotInGroup);+ }+ let mut targets = Vec::new();+ for member in &group.members {+ if let Some(user) = state.users.get(member) {+ for device in user.devices.keys() {+ let address = DeviceAddress {+ phone: member.clone(),+ device: *device,+ };+ if address != self.core.address() {+ targets.push(state.jid_of(&address, true));+ }+ }+ }+ }+ targets+ };+ let store = self.core.device.signal_store(None);+ let name = SenderKeyName {+ group_id: chat.to_string(),+ sender: self.core.own_lid().signal_address(),+ };+ let distribution = self+ .core+ .signal+ .create_sender_key_distribution_message(&store, &name)+ .await+ .map_err(SendError::signal)?;+ let distribution_plaintext = Message {+ sender_key_distribution_message: Some(SenderKeyDistributionMessage {+ group_id: Some(chat.to_string()),+ axolotl_sender_key_distribution_message: Some(distribution),+ }),+ ..Message::default()+ }+ .encode_to_vec();+ let mut participants = Vec::new();+ for target in targets {+ participants.extend(+ self.encrypt_for(&store, &target, &distribution_plaintext)+ .await?,+ );+ }+ let group_ciphertext = self+ .core+ .signal+ .group_encrypt(&store, &name, &pad(&message.encode_to_vec()))+ .await+ .map_err(SendError::signal)?;+ Ok(node(+ "message",+ vec![+ ("id", text(id)),+ ("to", jid(chat)),+ ("type", text(message_type(message))),+ ],+ children(vec![+ node("participants", vec![], children(participants)),+ node(+ "enc",+ vec![("v", text("2")), ("type", text("skmsg"))],+ bytes(group_ciphertext),+ ),+ ]),+ ))+ }++ async fn direct_stanza(+ &self,+ chat: &Jid,+ id: &str,+ message: &Message,+ ) -> Result<Node, SendError> {+ let (recipient, targets) = {+ let state = self.shared.lock();+ let phone = state.phone_of(chat).ok_or(SendError::UnknownChat)?;+ let mut targets = Vec::new();+ for user in [&phone, &self.core.phone] {+ if let Some(record) = state.users.get(user) {+ for device in record.devices.keys() {+ let address = DeviceAddress {+ phone: user.clone(),+ device: *device,+ };+ if address != self.core.address() {+ targets.push((user == &self.core.phone, state.jid_of(&address, true)));+ }+ }+ }+ }+ let lid = state+ .users+ .get(&phone)+ .map(|user| user.lid.clone())+ .unwrap_or_default();+ (device_jid(&lid, 0, true), targets)+ };+ let plaintext = message.encode_to_vec();+ let own_plaintext = Message {+ device_sent_message: Some(Box::new(DeviceSentMessage {+ destination_jid: Some(recipient.to_string()),+ message: Some(Box::new(message.clone())),+ phash: None,+ })),+ ..Message::default()+ }+ .encode_to_vec();+ let store = self.core.device.signal_store(None);+ let mut participants = Vec::new();+ for (own, target) in targets {+ let plaintext = if own { &own_plaintext } else { &plaintext };+ participants.extend(self.encrypt_for(&store, &target, plaintext).await?);+ }+ Ok(node(+ "message",+ vec![+ ("id", text(id)),+ ("to", jid(&recipient)),+ ("type", text(message_type(message))),+ ],+ children(vec![node("participants", vec![], children(participants))]),+ ))+ }++ /// `<to jid><enc/></to>` for one device, starting a session from a+ /// prekey bundle if there is none. `None` when the device has no keys.+ async fn encrypt_for(+ &self,+ store: &dyn SignalProtocolStore,+ target: &Jid,+ plaintext: &[u8],+ ) -> Result<Option<Node>, SendError> {+ let address = target.signal_address();+ if !store+ .contains_session(&address)+ .await+ .map_err(SendError::store)?+ {+ let bundle_node = self.shared.lock().pre_key_bundle_node(target);+ let Ok(bundle) =+ whatsmeow::prekeys::node_to_pre_key_bundle(u32::from(target.device), &bundle_node)+ else {+ tracing::warn!("fake phone found no prekeys for a device, skipping it");+ return Ok(None);+ };+ self.core+ .signal+ .process_bundle(store, &address, &bundle)+ .await+ .map_err(SendError::signal)?;+ }+ let ciphertext = self+ .core+ .signal+ .encrypt(store, &address, &pad(plaintext))+ .await+ .map_err(SendError::signal)?;+ let enc_type = match ciphertext.kind {+ CiphertextType::PreKey => "pkmsg",+ CiphertextType::Whisper => "msg",+ };+ Ok(Some(node(+ "to",+ vec![("jid", jid(target))],+ children(vec![node(+ "enc",+ vec![("v", text("2")), ("type", text(enc_type))],+ bytes(ciphertext.serialized),+ )]),+ )))+ }++ /// Approves a pairing QR code shown by a client, like scanning it in+ /// "Linked devices". Returns the new device's JID once the client+ /// signed the pairing; the client then reconnects and logs in.+ #[tracing::instrument(name = "fake_whatsapp.scan_qr", skip_all, err)]+ pub async fn scan_qr(&self, qr: &str) -> Result<Jid, PairError> {+ let fields = parse_qr(qr).ok_or(PairError::MalformedQr)?;+ let (reference, noise, identity, adv_secret) =+ (&fields[0], &fields[1], &fields[2], &fields[3]);+ let noise_key: [u8; 32] = decode_key(noise)?;+ let identity_key: [u8; 32] = decode_key(identity)?;+ let adv_secret = STANDARD+ .decode(adv_secret)+ .map_err(|_| PairError::MalformedQr)?;++ let (reply, confirmation) = oneshot::channel();+ {+ let mut state = self.shared.lock();+ let connection = *state+ .pairing_refs+ .get(reference)+ .ok_or(PairError::UnknownQr)?;+ let user = state+ .users+ .get_mut(&self.core.phone)+ .ok_or(PairError::UnknownQr)?;+ user.next_device += 1;+ let device = user.next_device;+ let account = user.account.clone();+ let lid = user.lid.clone();+ let details = AdvDeviceIdentity {+ raw_id: Some(rand::random()),+ timestamp: Some(+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|elapsed| elapsed.as_secs())+ .unwrap_or_default(),+ ),+ key_index: Some(u32::from(device)),+ ..AdvDeviceIdentity::default()+ }+ .encode_to_vec();+ let account_message =+ [&ADV_ACCOUNT_SIGNATURE_PREFIX[..], &details, &identity_key].concat();+ let signed = AdvSignedDeviceIdentity {+ details: Some(details.clone()),+ account_signature_key: Some(account.public.to_vec()),+ account_signature: Some(+ LibsignalCurveSignatures::new()+ .calculate_signature(&account.private, &account_message)+ .to_vec(),+ ),+ device_signature: None,+ }+ .encode_to_vec();+ let container = AdvSignedDeviceIdentityHmac {+ hmac: Some(adv_hmac(&adv_secret, &signed)),+ details: Some(signed),+ account_type: None,+ }+ .encode_to_vec();+ let iq_id = random_id();+ let pair_success = node(+ "iq",+ vec![+ ("from", jid(&SERVER_JID)),+ ("id", text(&iq_id)),+ ("type", text("set")),+ ("xmlns", text("md")),+ ("t", now()),+ ],+ children(vec![node(+ "pair-success",+ vec![],+ children(vec![+ node("device-identity", vec![], bytes(container)),+ node(+ "device",+ vec![+ ("jid", jid(&device_jid(&self.core.phone, device, false))),+ ("lid", jid(&device_jid(&lid, device, true))),+ ],+ None,+ ),+ node("platform", vec![("name", text("android"))], None),+ ]),+ )]),+ );+ state.pending_pairs.insert(+ iq_id,+ PendingPair {+ connection,+ address: DeviceAddress {+ phone: self.core.phone.clone(),+ device,+ },+ noise_key,+ identity_key,+ account_public: account.public,+ details,+ reply,+ },+ );+ state.send(connection, pair_success);+ }+ match tokio::time::timeout(PAIR_CONFIRMATION_TIMEOUT, confirmation).await {+ Ok(Ok(result)) => result,+ Ok(Err(_)) | Err(_) => Err(PairError::NoConfirmation),+ }+ }++ /// Handles one node delivered to the phone.+ pub(crate) async fn handle(&self, delivered: Node) {+ match delivered.tag.as_str() {+ "message" => self.receive_message(&delivered).await,+ "receipt" => {+ let receipt = ReceivedReceipt {+ id: attr_string(&delivered, "id"),+ from: attr_jid(&delivered, "participant")+ .or_else(|| attr_jid(&delivered, "from"))+ .unwrap_or_default(),+ receipt_type: attr_string(&delivered, "type"),+ };+ self.core.inbox().receipts.push(receipt);+ }+ _ => {}+ }+ self.shared.changed();+ }++ async fn receive_message(&self, stanza: &Node) {+ let from = attr_jid(stanza, "from").unwrap_or_default();+ let group = from.server == GROUP_SERVER;+ let sender_jid = if group {+ attr_jid(stanza, "participant").unwrap_or_default()+ } else {+ from.clone()+ };+ let (sender, sender_phone) = {+ let state = self.shared.lock();+ match state.resolve(&sender_jid) {+ Some(address) => (+ state.jid_of(&address, true),+ device_jid(&address.phone, 0, false),+ ),+ None => {+ drop(state);+ self.fail("message from an unknown device");+ return;+ }+ }+ };+ let id = attr_string(stanza, "id");+ let sender_address = sender.signal_address();+ let _crypto = self.core.crypto.lock().await;+ let store = self.core.device.signal_store(None);+ for enc in stanza+ .get_children()+ .iter()+ .filter(|child| child.tag == "enc")+ {+ let Some(ciphertext) = content_bytes(enc) else {+ continue;+ };+ let decrypted = match attr_string(enc, "type").as_str() {+ "pkmsg" => {+ self.core+ .signal+ .decrypt_pre_key_message(&store, &sender_address, ciphertext)+ .await+ }+ "msg" => {+ self.core+ .signal+ .decrypt_message(&store, &sender_address, ciphertext)+ .await+ }+ "skmsg" if group => {+ let name = SenderKeyName {+ group_id: from.to_string(),+ sender: sender_address.clone(),+ };+ self.core+ .signal+ .group_decrypt(&store, &name, ciphertext)+ .await+ }+ _ => continue,+ };+ let plaintext = match decrypted {+ Ok(plaintext) => plaintext,+ Err(error) => {+ self.fail(&format!("decrypting {}: {error}", attr_string(enc, "type")));+ continue;+ }+ };+ let Some(message) = unpad(&plaintext).and_then(|data| Message::decode(data).ok())+ else {+ self.fail("undecodable plaintext");+ continue;+ };+ self.process_message(+ &store,+ &id,+ &from,+ &sender,+ &sender_phone,+ &sender_address,+ message,+ )+ .await;+ }+ drop(_crypto);+ let mut receipt_attrs = vec![("id", text(&id)), ("to", jid(&from))];+ if group {+ receipt_attrs.push(("participant", jid(&sender_jid)));+ }+ let receipt = node("receipt", receipt_attrs, None);+ self.shared+ .lock()+ .route_receipt(&self.core.address(), &receipt);+ }++ #[expect(+ clippy::too_many_arguments,+ reason = "the parts of one received stanza"+ )]+ async fn process_message(+ &self,+ store: &dyn SignalProtocolStore,+ id: &str,+ from: &Jid,+ sender: &Jid,+ sender_phone: &Jid,+ sender_address: &SignalAddress,+ mut message: Message,+ ) {+ if let Some(distribution) = message.sender_key_distribution_message.take() {+ let group_id = distribution.group_id.unwrap_or_else(|| from.to_string());+ let name = SenderKeyName {+ group_id,+ sender: sender_address.clone(),+ };+ let result = self+ .core+ .signal+ .process_sender_key_distribution_message(+ store,+ &name,+ &distribution+ .axolotl_sender_key_distribution_message+ .unwrap_or_default(),+ )+ .await;+ if let Err(error) = result {+ self.fail(&format!("processing a sender key: {error}"));+ }+ }+ let mut chat = if from.server == GROUP_SERVER {+ group_jid(&from.user)+ } else {+ from.to_non_ad()+ };+ if let Some(sent) = message.device_sent_message.take() {+ if let Some(destination) = sent+ .destination_jid+ .and_then(|destination| whatsmeow::types::parse_jid(&destination).ok())+ {+ chat = destination;+ }+ message = sent.message.map(|inner| *inner).unwrap_or_default();+ }+ if message == Message::default() {+ return;+ }+ self.core.inbox().messages.push(ReceivedMessage {+ id: id.to_owned(),+ chat,+ sender: sender.clone(),+ sender_phone: sender_phone.clone(),+ message,+ });+ }++ fn fail(&self, reason: &str) {+ tracing::warn!(reason, "fake phone failed to process a stanza");+ self.core.inbox().failures.push(reason.to_owned());+ }+}++/// The message type attribute whatsmeow sends.+fn message_type(message: &Message) -> &'static str {+ if message.reaction_message.is_some() {+ "reaction"+ } else if message.conversation.is_some() || message.extended_text_message.is_some() {+ "text"+ } else {+ "media"+ }+}++/// whatsmeow's padding: 1–15 bytes, each holding the padding length.+fn pad(plaintext: &[u8]) -> Vec<u8> {+ let mut length = rand::random::<u8>() & 0x0f;+ if length == 0 {+ length = 0x0f;+ }+ let mut padded = plaintext.to_vec();+ padded.extend(std::iter::repeat_n(length, usize::from(length)));+ padded+}++fn unpad(plaintext: &[u8]) -> Option<&[u8]> {+ let length = usize::from(*plaintext.last()?);+ plaintext.get(..plaintext.len().checked_sub(length)?)+}++fn decode_key(value: &str) -> Result<[u8; 32], PairError> {+ STANDARD+ .decode(value)+ .ok()+ .and_then(|key| key.try_into().ok())+ .ok_or(PairError::MalformedQr)+}++/// The comma-separated fields after the `#` of a pairing QR link:+/// ref, noise key, identity key, ADV secret, client type.+fn parse_qr(qr: &str) -> Option<Vec<String>> {+ fn fields(input: &str) -> IResult<&str, Vec<&str>> {+ let (input, _) = (take_until("#"), tag("#")).parse(input)?;+ separated_list1(tag(","), is_not(",")).parse(input)+ }+ let (_, fields) = fields(qr).ok()?;+ (fields.len() >= 4).then(|| fields.into_iter().map(str::to_owned).collect())+}diff --git a/crates/whatsapp_fake_server/src/signal.rs b/crates/whatsapp_fake_server/src/signal.rsnew file mode 100644index 0000000000..28b66a319e--- /dev/null+++ b/crates/whatsapp_fake_server/src/signal.rs@@ -0,0 +1,223 @@+//! The Signal implementation a client must use to talk to the fake server,+//! and the server's noise certificate chain.+//!+//! The client verifies the server's noise certificate against WhatsApp's+//! root key ([`WA_CERT_PUB_KEY`]), whose private half only WhatsApp has. The+//! fake server signs its chain with [`test_root_key`] instead, and+//! [`TestSignal`] is libsignal with one change: a signature checked against+//! WhatsApp's root key is checked against the test root key. Everything else+//! (prekey signatures, pairing signatures, sessions, sender keys) is the real+//! libsignal from `whatsmeow_signal`.++use std::sync::Arc;++use async_trait::async_trait;+use prost::Message;+use whatsmeow::Signal;+use whatsmeow::handshake::{WA_CERT_ISSUER_SERIAL, WA_CERT_PUB_KEY};+use whatsmeow::proto::wa_cert::CertChain;+use whatsmeow::proto::wa_cert::cert_chain::NoiseCertificate;+use whatsmeow::proto::wa_cert::cert_chain::noise_certificate::Details;+use whatsmeow::signal::{+ CiphertextMessage, CurveSignatures, PreKeyBundle, SenderKeyName, SignalAddress, SignalError,+ SignalProtocol, SignalProtocolStore,+};+use whatsmeow::util::keys::{KeyPair, new_key_pair, new_key_pair_from_private_key};+use whatsmeow_signal::{LibsignalCurveSignatures, LibsignalProtocol};++/// The private key of the test root that stands in for WhatsApp's root.+const TEST_ROOT_PRIVATE_KEY: [u8; 32] = [+ 0x58, 0x0b, 0x6c, 0x2e, 0x91, 0x3a, 0x47, 0xd2, 0x1f, 0x88, 0x05, 0x6e, 0xc4, 0x39, 0xa7, 0x12,+ 0x7d, 0x50, 0xe3, 0x9b, 0x26, 0x41, 0xbf, 0x0c, 0x73, 0xd8, 0x6a, 0x15, 0xf0, 0x2c, 0x94, 0x4e,+];++/// The test root key pair: the fake server's intermediate certificate is+/// signed with it.+pub fn test_root_key() -> KeyPair {+ new_key_pair_from_private_key(TEST_ROOT_PRIVATE_KEY)+}++/// libsignal, except that WhatsApp's root key is replaced by+/// [`test_root_key`] when verifying signatures. Hand it to+/// [`whatsmeow::Client::new`] for clients that connect to the fake server.+pub struct TestSignal {+ protocol: LibsignalProtocol,+ signatures: LibsignalCurveSignatures,+ root_public: [u8; 32],+}++impl Default for TestSignal {+ fn default() -> Self {+ TestSignal {+ protocol: LibsignalProtocol::new(),+ signatures: LibsignalCurveSignatures::new(),+ root_public: test_root_key().public,+ }+ }+}++/// A [`TestSignal`] as the trait object [`whatsmeow::Client::new`] takes.+pub fn test_signal() -> Arc<dyn Signal> {+ Arc::new(TestSignal::default())+}++impl CurveSignatures for TestSignal {+ fn calculate_signature(&self, private_key: &[u8; 32], message: &[u8]) -> [u8; 64] {+ self.signatures.calculate_signature(private_key, message)+ }++ fn verify_signature(+ &self,+ public_key: &[u8; 32],+ message: &[u8],+ signature: &[u8; 64],+ ) -> bool {+ let public_key = if *public_key == WA_CERT_PUB_KEY {+ &self.root_public+ } else {+ public_key+ };+ self.signatures+ .verify_signature(public_key, message, signature)+ }+}++#[async_trait]+impl SignalProtocol for TestSignal {+ async fn process_bundle(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ bundle: &PreKeyBundle,+ ) -> Result<(), SignalError> {+ self.protocol.process_bundle(store, address, bundle).await+ }++ async fn encrypt(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ plaintext: &[u8],+ ) -> Result<CiphertextMessage, SignalError> {+ self.protocol.encrypt(store, address, plaintext).await+ }++ async fn decrypt_pre_key_message(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.protocol+ .decrypt_pre_key_message(store, address, ciphertext)+ .await+ }++ async fn decrypt_message(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.protocol+ .decrypt_message(store, address, ciphertext)+ .await+ }++ async fn create_sender_key_distribution_message(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ ) -> Result<Vec<u8>, SignalError> {+ self.protocol+ .create_sender_key_distribution_message(store, name)+ .await+ }++ async fn process_sender_key_distribution_message(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ message: &[u8],+ ) -> Result<(), SignalError> {+ self.protocol+ .process_sender_key_distribution_message(store, name, message)+ .await+ }++ async fn group_encrypt(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ plaintext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.protocol.group_encrypt(store, name, plaintext).await+ }++ async fn group_decrypt(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.protocol.group_decrypt(store, name, ciphertext).await+ }+}++/// The fake server's noise identity: its static key and the certificate+/// chain that binds it to the test root.+pub(crate) struct ServerIdentity {+ pub(crate) static_key: KeyPair,+ pub(crate) certificate_chain: Vec<u8>,+}++impl ServerIdentity {+ pub(crate) fn generate() -> ServerIdentity {+ const NOT_BEFORE: u64 = 1_600_000_000;+ const NOT_AFTER: u64 = 4_000_000_000;+ const INTERMEDIATE_SERIAL: u32 = 7;+ let signatures = LibsignalCurveSignatures::new();+ let root = test_root_key();+ let intermediate = new_key_pair();+ let static_key = new_key_pair();+ let intermediate_details = Details {+ serial: Some(INTERMEDIATE_SERIAL),+ issuer_serial: Some(WA_CERT_ISSUER_SERIAL),+ key: Some(intermediate.public.to_vec()),+ not_before: Some(NOT_BEFORE),+ not_after: Some(NOT_AFTER),+ }+ .encode_to_vec();+ let leaf_details = Details {+ serial: Some(INTERMEDIATE_SERIAL + 1),+ issuer_serial: Some(INTERMEDIATE_SERIAL),+ key: Some(static_key.public.to_vec()),+ not_before: Some(NOT_BEFORE),+ not_after: Some(NOT_AFTER),+ }+ .encode_to_vec();+ let certificate_chain = CertChain {+ intermediate: Some(NoiseCertificate {+ signature: Some(+ signatures+ .calculate_signature(&root.private, &intermediate_details)+ .to_vec(),+ ),+ details: Some(intermediate_details),+ }),+ leaf: Some(NoiseCertificate {+ signature: Some(+ signatures+ .calculate_signature(&intermediate.private, &leaf_details)+ .to_vec(),+ ),+ details: Some(leaf_details),+ }),+ }+ .encode_to_vec();+ ServerIdentity {+ static_key,+ certificate_chain,+ }+ }+}diff --git a/crates/whatsapp_fake_server/src/stanza.rs b/crates/whatsapp_fake_server/src/stanza.rsnew file mode 100644index 0000000000..5c472446dc--- /dev/null+++ b/crates/whatsapp_fake_server/src/stanza.rs@@ -0,0 +1,89 @@+//! Small helpers for building and reading binary XML nodes.++use whatsmeow::binary::{AttributeValue, Attrs, Node, NodeContent};+use whatsmeow::types::{Jid, parse_jid};++/// A node with attributes and optional content.+pub(crate) fn node(+ tag: &str,+ attrs: Vec<(&str, AttributeValue)>,+ content: Option<NodeContent>,+) -> Node {+ Node {+ tag: tag.to_owned(),+ attrs: attrs+ .into_iter()+ .map(|(key, value)| (key.to_owned(), value))+ .collect::<Attrs>(),+ content,+ }+}++/// Content of child nodes.+pub(crate) fn children(nodes: Vec<Node>) -> Option<NodeContent> {+ Some(NodeContent::Nodes(nodes))+}++/// Content of bytes.+pub(crate) fn bytes(data: impl Into<Vec<u8>>) -> Option<NodeContent> {+ Some(NodeContent::Bytes(data.into()))+}++/// A string attribute value.+pub(crate) fn text(value: impl Into<String>) -> AttributeValue {+ AttributeValue::String(value.into())+}++/// A JID attribute value.+pub(crate) fn jid(value: &Jid) -> AttributeValue {+ AttributeValue::Jid(value.clone())+}++/// A string attribute, or empty.+pub(crate) fn attr_string(node: &Node, key: &str) -> String {+ match node.attrs.get(key) {+ Some(AttributeValue::String(value)) => value.clone(),+ Some(AttributeValue::Jid(jid)) => jid.to_string(),+ None => String::new(),+ }+}++/// A JID attribute, parsing strings too.+pub(crate) fn attr_jid(node: &Node, key: &str) -> Option<Jid> {+ match node.attrs.get(key)? {+ AttributeValue::Jid(jid) => Some(jid.clone()),+ AttributeValue::String(value) => parse_jid(value).ok(),+ }+}++/// The node's bytes content.+pub(crate) fn content_bytes(node: &Node) -> Option<&[u8]> {+ match &node.content {+ Some(NodeContent::Bytes(data)) => Some(data),+ _ => None,+ }+}++/// The bytes content of the named child.+pub(crate) fn child_bytes<'node>(node: &'node Node, tag: &str) -> Option<&'node [u8]> {+ content_bytes(node.get_optional_child_by_tag(&[tag])?)+}++/// The current Unix time in seconds, as an attribute value.+pub(crate) fn now() -> AttributeValue {+ let seconds = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|elapsed| elapsed.as_secs())+ .unwrap_or_default();+ AttributeValue::String(seconds.to_string())+}++/// A random message or stanza ID in WhatsApp's style.+pub(crate) fn random_id() -> String {+ let random: [u8; 8] = rand::random();+ let mut id = String::from("3EB0");+ for byte in random {+ id.push_str(&format!("{byte:02X}"));+ }+ id+}diff --git a/crates/whatsapp_fake_server/src/state.rs b/crates/whatsapp_fake_server/src/state.rsnew file mode 100644index 0000000000..521b71beaf--- /dev/null+++ b/crates/whatsapp_fake_server/src/state.rs@@ -0,0 +1,744 @@+//! The server's world: users and their devices, groups, connections, and+//! how stanzas are routed between devices.+//!+//! Everything here is synchronous and runs under one lock; connections and+//! phones hand nodes in and get nodes queued for delivery.++use std::collections::{BTreeMap, HashMap, VecDeque};++use tokio::sync::{mpsc, oneshot};+use whatsmeow::binary::{AttributeValue, Node, NodeContent};+use whatsmeow::types::{DEFAULT_USER_SERVER, GROUP_SERVER, HIDDEN_USER_SERVER, Jid, SERVER_JID};+use whatsmeow::util::keys::KeyPair;++use crate::stanza::{attr_jid, attr_string, bytes, children, jid, node, now, text};++/// A user's device, by phone number and device number.+#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]+pub(crate) struct DeviceAddress {+ pub(crate) phone: String,+ pub(crate) device: u16,+}++/// What a connection's writer task should do next.+#[derive(Debug)]+pub(crate) enum Outbound {+ /// Encrypt and send a node.+ Node(Node),+ /// Close the websocket.+ Close,+}++/// The keys a device uploaded, for prekey bundles.+#[derive(Clone, Debug)]+pub(crate) struct DeviceKeys {+ pub(crate) registration_id: u32,+ pub(crate) identity: [u8; 32],+ pub(crate) signed_pre_key: (u32, [u8; 32], [u8; 64]),+ pub(crate) pre_keys: VecDeque<(u32, [u8; 32])>,+}++pub(crate) enum DeviceKind {+ /// The user's phone, played in process by a [`crate::FakePhone`]; nodes+ /// for it go to its inbox.+ Phone(mpsc::UnboundedSender<Node>),+ /// A linked device: a real client that connects over the websocket.+ Linked { noise_key: [u8; 32] },+}++pub(crate) struct DeviceRecord {+ pub(crate) kind: DeviceKind,+ pub(crate) keys: Option<DeviceKeys>,+ /// The connection the device is logged in on.+ pub(crate) connection: Option<u64>,+ /// Nodes waiting for the device to log in.+ pub(crate) offline: VecDeque<Node>,+ /// Messages sent on the current connection that the device hasn't+ /// acked yet; they go back to `offline` if the connection drops.+ pub(crate) unacked: Vec<(String, Node)>,+}++pub(crate) struct User {+ pub(crate) phone: String,+ pub(crate) lid: String,+ pub(crate) push_name: String,+ /// The account key: the phone's identity key, which signs linked devices.+ pub(crate) account: KeyPair,+ pub(crate) devices: BTreeMap<u16, DeviceRecord>,+ pub(crate) next_device: u16,+}++pub(crate) struct Group {+ pub(crate) id: String,+ pub(crate) subject: String,+ pub(crate) creator: String,+ pub(crate) created: u64,+ /// Members by phone number, in join order.+ pub(crate) members: Vec<String>,+}++/// A connection that is pairing a new device.+pub(crate) struct Pairing {+ pub(crate) refs: Vec<String>,+}++pub(crate) struct ConnectionEntry {+ pub(crate) outbound: mpsc::UnboundedSender<Outbound>,+ pub(crate) device: Option<DeviceAddress>,+ pub(crate) pairing: Option<Pairing>,+}++/// A pair-success the server sent, waiting for the client to sign it.+pub(crate) struct PendingPair {+ pub(crate) connection: u64,+ pub(crate) address: DeviceAddress,+ pub(crate) noise_key: [u8; 32],+ pub(crate) identity_key: [u8; 32],+ pub(crate) account_public: [u8; 32],+ pub(crate) details: Vec<u8>,+ pub(crate) reply: oneshot::Sender<Result<Jid, crate::PairError>>,+}++#[derive(Default)]+pub(crate) struct State {+ pub(crate) users: BTreeMap<String, User>,+ /// LID user → phone number.+ pub(crate) lids: HashMap<String, String>,+ pub(crate) groups: BTreeMap<String, Group>,+ pub(crate) connections: HashMap<u64, ConnectionEntry>,+ /// Pairing ref → connection.+ pub(crate) pairing_refs: HashMap<String, u64>,+ /// Pair-success IQ ID → the pairing waiting for the client.+ pub(crate) pending_pairs: HashMap<String, PendingPair>,+ pub(crate) next_connection: u64,+ pub(crate) next_number: u64,+ /// Uploaded media by path.+ pub(crate) media: HashMap<String, Vec<u8>>,+ /// Every message stanza delivered to a linked device, as (device, id).+ pub(crate) delivered: Vec<(DeviceAddress, String)>,+ /// `host:port` of the media HTTP server.+ pub(crate) media_host: String,+ /// Every message stanza a linked device submitted, as (device, id).+ pub(crate) submitted: Vec<(DeviceAddress, String)>,+ /// Devices whose submitted messages are routed but never acked.+ pub(crate) withheld_acks: std::collections::HashSet<DeviceAddress>,+}++/// A device JID in the phone-number or LID domain.+pub(crate) fn device_jid(user: &str, device: u16, lid: bool) -> Jid {+ Jid {+ user: user.to_owned(),+ raw_agent: 0,+ device,+ integrator: 0,+ server: if lid {+ HIDDEN_USER_SERVER+ } else {+ DEFAULT_USER_SERVER+ }+ .to_owned(),+ }+}++/// A group JID.+pub(crate) fn group_jid(id: &str) -> Jid {+ Jid {+ user: id.to_owned(),+ server: GROUP_SERVER.to_owned(),+ ..Jid::default()+ }+}++impl State {+ /// A fresh number for LIDs and group IDs.+ pub(crate) fn number(&mut self) -> u64 {+ self.next_number += 1;+ self.next_number+ }++ /// The phone number of a user JID in either domain.+ pub(crate) fn phone_of(&self, user: &Jid) -> Option<String> {+ match user.server.as_str() {+ DEFAULT_USER_SERVER => self+ .users+ .contains_key(&user.user)+ .then(|| user.user.clone()),+ HIDDEN_USER_SERVER => self.lids.get(&user.user).cloned(),+ _ => None,+ }+ }++ /// The device a device JID in either domain names.+ pub(crate) fn resolve(&self, device: &Jid) -> Option<DeviceAddress> {+ let phone = self.phone_of(device)?;+ self.users+ .get(&phone)?+ .devices+ .contains_key(&device.device)+ .then_some(DeviceAddress {+ phone,+ device: device.device,+ })+ }++ /// The device's JID in the given domain.+ pub(crate) fn jid_of(&self, address: &DeviceAddress, lid: bool) -> Jid {+ match (lid, self.users.get(&address.phone)) {+ (true, Some(user)) => device_jid(&user.lid, address.device, true),+ _ => device_jid(&address.phone, address.device, false),+ }+ }++ pub(crate) fn device_mut(&mut self, address: &DeviceAddress) -> Option<&mut DeviceRecord> {+ self.users+ .get_mut(&address.phone)?+ .devices+ .get_mut(&address.device)+ }++ pub(crate) fn device(&self, address: &DeviceAddress) -> Option<&DeviceRecord> {+ self.users.get(&address.phone)?.devices.get(&address.device)+ }++ /// Sends a node on a connection.+ pub(crate) fn send(&self, connection: u64, node: Node) {+ if let Some(entry) = self.connections.get(&connection) {+ let _ = entry.outbound.send(Outbound::Node(node));+ }+ }++ /// Closes a connection's websocket.+ pub(crate) fn close(&self, connection: u64) {+ if let Some(entry) = self.connections.get(&connection) {+ let _ = entry.outbound.send(Outbound::Close);+ }+ }++ /// Delivers a node to a device: a phone gets it now, a linked device on+ /// its connection, or when it next logs in.+ pub(crate) fn deliver(&mut self, target: &DeviceAddress, node: Node) {+ let Some(device) = self.device_mut(target) else {+ return;+ };+ match &device.kind {+ DeviceKind::Phone(inbox) => {+ let _ = inbox.send(node);+ }+ DeviceKind::Linked { .. } => match device.connection {+ Some(connection) => {+ if node.tag == "message" {+ device+ .unacked+ .push((attr_string(&node, "id"), node.clone()));+ self.delivered+ .push((target.clone(), attr_string(&node, "id")));+ }+ self.send(connection, node);+ }+ None => device.offline.push_back(node),+ },+ }+ }++ /// A device acked (or receipted) a message.+ pub(crate) fn acked(&mut self, address: &DeviceAddress, id: &str) {+ if let Some(device) = self.device_mut(address) {+ device.unacked.retain(|(unacked, _)| unacked != id);+ }+ }++ /// Detaches a device from its connection; unacked messages are queued+ /// again, ahead of anything queued since.+ pub(crate) fn detach(&mut self, address: &DeviceAddress) {+ if let Some(device) = self.device_mut(address) {+ device.connection = None;+ for (_, node) in device.unacked.drain(..).rev() {+ device.offline.push_front(node);+ }+ }+ }++ /// The group node of a group info or list response.+ pub(crate) fn group_node(&self, group: &Group) -> Node {+ let participants = group+ .members+ .iter()+ .filter_map(|phone| self.users.get(phone))+ .map(|user| {+ let mut attrs = vec![+ ("jid", jid(&device_jid(&user.lid, 0, true))),+ ("phone_number", jid(&device_jid(&user.phone, 0, false))),+ ];+ if user.phone == group.creator {+ attrs.push(("type", text("superadmin")));+ }+ node("participant", attrs, None)+ })+ .collect();+ let creator_lid = self+ .users+ .get(&group.creator)+ .map(|user| device_jid(&user.lid, 0, true))+ .unwrap_or_default();+ node(+ "group",+ vec![+ ("id", text(&group.id)),+ ("subject", text(&group.subject)),+ ("s_t", text(group.created.to_string())),+ ("s_o", jid(&creator_lid)),+ ("creation", text(group.created.to_string())),+ ("creator", jid(&creator_lid)),+ ("creator_pn", jid(&device_jid(&group.creator, 0, false))),+ ("addressing_mode", text("lid")),+ ("size", text(group.members.len().to_string())),+ ],+ children(participants),+ )+ }++ /// Routes a `<message>` stanza a device sent. Returns the server ack for+ /// the sender.+ pub(crate) fn route_message(&mut self, sender: &DeviceAddress, stanza: &Node) -> Node {+ let id = attr_string(stanza, "id");+ let to = attr_jid(stanza, "to").unwrap_or_default();+ let timestamp = now();+ let ack = node(+ "ack",+ vec![+ ("class", text("message")),+ ("id", text(&id)),+ ("from", jid(&to)),+ ("t", timestamp.clone()),+ ],+ None,+ );+ let push_name = self+ .users+ .get(&sender.phone)+ .map(|user| user.push_name.clone())+ .unwrap_or_default();+ let direct: Vec<(Jid, Node)> = stanza+ .get_optional_child_by_tag(&["participants"])+ .map(|participants| {+ participants+ .get_children()+ .iter()+ .filter(|child| child.tag == "to")+ .filter_map(|child| {+ let target = attr_jid(child, "jid")?;+ let enc = child.get_optional_child_by_tag(&["enc"])?.clone();+ Some((target, enc))+ })+ .collect()+ })+ .unwrap_or_default();+ let device_identity = stanza+ .get_optional_child_by_tag(&["device-identity"])+ .cloned();+ let with_identity = |mut content: Vec<Node>| {+ let pre_key = content+ .iter()+ .any(|enc| attr_string(enc, "type") == "pkmsg");+ if pre_key && let Some(identity) = &device_identity {+ content.push(identity.clone());+ }+ content+ };+ let message_type = attr_string(stanza, "type");++ if to.server == GROUP_SERVER {+ let Some(group) = self.groups.get(&to.user) else {+ return with_error(ack, 404);+ };+ if !group.members.contains(&sender.phone) {+ return with_error(ack, 403);+ }+ let sender_key = stanza+ .get_children()+ .iter()+ .find(|child| child.tag == "enc" && attr_string(child, "type") == "skmsg")+ .cloned();+ let mut targets = Vec::new();+ for member in &group.members {+ if let Some(user) = self.users.get(member) {+ for device in user.devices.keys() {+ targets.push(DeviceAddress {+ phone: member.clone(),+ device: *device,+ });+ }+ }+ }+ let participant = self.jid_of(sender, true);+ let participant_pn = device_jid(&sender.phone, 0, false);+ for target in targets.into_iter().filter(|target| target != sender) {+ let mut content: Vec<Node> = direct+ .iter()+ .filter(|(jid, _)| self.resolve(jid).as_ref() == Some(&target))+ .map(|(_, enc)| enc.clone())+ .collect();+ content = with_identity(content);+ content.extend(sender_key.clone());+ if content.is_empty() {+ continue;+ }+ let delivery = node(+ "message",+ vec![+ ("from", jid(&to)),+ ("participant", jid(&participant)),+ ("participant_pn", jid(&participant_pn)),+ ("addressing_mode", text("lid")),+ ("id", text(&id)),+ ("t", timestamp.clone()),+ ("type", text(&message_type)),+ ("notify", text(&push_name)),+ ],+ children(content),+ );+ self.deliver(&target, delivery);+ }+ return ack;+ }++ let Some(_) = self.phone_of(&to) else {+ return with_error(ack, 404);+ };+ for (target_jid, enc) in direct {+ let Some(target) = self.resolve(&target_jid) else {+ continue;+ };+ if &target == sender {+ continue;+ }+ let lid = target_jid.server == HIDDEN_USER_SERVER;+ let from = self.jid_of(sender, lid);+ let mut attrs = vec![+ ("from", jid(&from)),+ ("id", text(&id)),+ ("t", timestamp.clone()),+ ("type", text(&message_type)),+ ("notify", text(&push_name)),+ ];+ if target.phone == sender.phone {+ attrs.push(("recipient", jid(&to)));+ if let Some(pn) = stanza.attrs.get("peer_recipient_pn") {+ attrs.push(("peer_recipient_pn", pn.clone()));+ }+ } else if lid {+ attrs.push(("sender_pn", jid(&device_jid(&sender.phone, 0, false))));+ } else if let Some(user) = self.users.get(&sender.phone) {+ attrs.push(("sender_lid", jid(&device_jid(&user.lid, 0, true))));+ }+ let delivery = node("message", attrs, children(with_identity(vec![enc])));+ self.deliver(&target, delivery);+ }+ ack+ }++ /// Routes a `<receipt>` a device sent to the device it is about.+ pub(crate) fn route_receipt(&mut self, sender: &DeviceAddress, receipt: &Node) {+ let Some(to) = attr_jid(receipt, "to") else {+ return;+ };+ let (target, from, participant) = if to.server == GROUP_SERVER {+ let Some(target) = attr_jid(receipt, "participant").and_then(|jid| self.resolve(&jid))+ else {+ return;+ };+ (target, to.clone(), Some(self.jid_of(sender, true)))+ } else {+ let Some(target) = self.resolve(&to) else {+ return;+ };+ let lid = to.server == HIDDEN_USER_SERVER;+ (target, self.jid_of(sender, lid), None)+ };+ let mut attrs = vec![+ ("from", jid(&from)),+ ("id", text(attr_string(receipt, "id"))),+ ("t", now()),+ ];+ if let Some(participant) = &participant {+ attrs.push(("participant", jid(participant)));+ }+ let receipt_type = attr_string(receipt, "type");+ if !receipt_type.is_empty() {+ attrs.push(("type", text(receipt_type)));+ }+ if let Some(recipient) = receipt.attrs.get("recipient") {+ attrs.push(("recipient", recipient.clone()));+ }+ let content = receipt.content.clone();+ let mut delivery = node("receipt", attrs, None);+ delivery.content = content;+ self.deliver(&target, delivery);+ }++ /// Answers an IQ from a logged-in device.+ pub(crate) fn handle_iq(&mut self, from: &DeviceAddress, iq: &Node) -> Node {+ let xmlns = attr_string(iq, "xmlns");+ let iq_type = attr_string(iq, "type");+ let first_child = iq.get_children().first().map(|child| child.tag.clone());+ match (xmlns.as_str(), iq_type.as_str(), first_child.as_deref()) {+ ("encrypt", "get", Some("count")) => {+ let count = self+ .device(from)+ .and_then(|device| device.keys.as_ref())+ .map_or(0, |keys| keys.pre_keys.len());+ iq_result(+ iq,+ vec![node(+ "count",+ vec![("value", text(count.to_string()))],+ None,+ )],+ )+ }+ ("encrypt", "set", _) => match parse_upload(iq) {+ Some(uploaded) => {+ if let Some(device) = self.device_mut(from) {+ match &mut device.keys {+ Some(keys) if keys.identity == uploaded.identity => {+ keys.registration_id = uploaded.registration_id;+ keys.signed_pre_key = uploaded.signed_pre_key;+ keys.pre_keys.extend(uploaded.pre_keys);+ }+ keys => *keys = Some(uploaded),+ }+ }+ iq_result(iq, vec![])+ }+ None => iq_error(iq, 400, "bad-request"),+ },+ ("encrypt", "get", Some("key")) => {+ let users = iq+ .get_child_by_tag(&["key"])+ .get_children()+ .iter()+ .filter_map(|user| attr_jid(user, "jid"))+ .map(|target| self.pre_key_bundle_node(&target))+ .collect();+ iq_result(iq, vec![node("list", vec![], children(users))])+ }+ ("usync", "get", _) => self.usync(iq),+ ("w:g2", "get", Some("query")) => {+ let group = attr_jid(iq, "to")+ .and_then(|to| self.groups.get(&to.user))+ .filter(|group| group.members.contains(&from.phone));+ match group {+ Some(group) => iq_result(iq, vec![self.group_node(group)]),+ None => iq_error(iq, 404, "item-not-found"),+ }+ }+ ("w:g2", "get", Some("participating")) => {+ let groups = self+ .groups+ .values()+ .filter(|group| group.members.contains(&from.phone))+ .map(|group| self.group_node(group))+ .collect();+ iq_result(iq, vec![node("groups", vec![], children(groups))])+ }+ ("w:m", "set", Some("media_conn")) => iq_result(+ iq,+ vec![node(+ "media_conn",+ vec![+ ("auth", text("fake-media-auth")),+ ("ttl", text("300")),+ ("auth_ttl", text("21600")),+ ("max_buckets", text("12")),+ ],+ children(vec![node(+ "host",+ vec![("hostname", text(&self.media_host))],+ None,+ )]),+ )],+ ),+ _ => iq_result(iq, vec![]),+ }+ }++ /// `<user>` with a prekey bundle, consuming one one-time prekey.+ pub(crate) fn pre_key_bundle_node(&mut self, target: &Jid) -> Node {+ let address = self.resolve(target);+ let keys = address+ .as_ref()+ .and_then(|address| self.device_mut(address))+ .and_then(|device| device.keys.as_mut());+ let Some(keys) = keys else {+ return node(+ "user",+ vec![("jid", jid(target))],+ children(vec![node(+ "error",+ vec![("code", text("404")), ("text", text("item-not-found"))],+ None,+ )]),+ );+ };+ let mut content = vec![+ node(+ "registration",+ vec![],+ bytes(keys.registration_id.to_be_bytes()),+ ),+ node("type", vec![], bytes([whatsmeow::signal::DJB_TYPE])),+ node("identity", vec![], bytes(keys.identity)),+ ];+ let (signed_id, signed_public, signature) = keys.signed_pre_key;+ content.push(node(+ "skey",+ vec![],+ children(vec![+ node("id", vec![], bytes(&signed_id.to_be_bytes()[1..])),+ node("value", vec![], bytes(signed_public)),+ node("signature", vec![], bytes(signature)),+ ]),+ ));+ if let Some((id, public)) = keys.pre_keys.pop_front() {+ content.push(node(+ "key",+ vec![],+ children(vec![+ node("id", vec![], bytes(&id.to_be_bytes()[1..])),+ node("value", vec![], bytes(public)),+ ]),+ ));+ }+ node("user", vec![("jid", jid(target))], children(content))+ }++ fn usync(&self, iq: &Node) -> Node {+ let usync = iq.get_child_by_tag(&["usync"]);+ let query: Vec<String> = usync+ .get_child_by_tag(&["query"])+ .get_children()+ .iter()+ .map(|child| child.tag.clone())+ .collect();+ let users = usync+ .get_child_by_tag(&["list"])+ .get_children()+ .iter()+ .filter_map(|user| {+ let requested = attr_jid(user, "jid")?;+ let phone = self.phone_of(&requested)?;+ let record = self.users.get(&phone)?;+ let lid = requested.server == HIDDEN_USER_SERVER;+ let mut content = Vec::new();+ for tag in &query {+ match tag.as_str() {+ "devices" => {+ let devices = record+ .devices+ .keys()+ .map(|device| {+ let mut attrs = vec![("id", text(device.to_string()))];+ if *device != 0 {+ attrs.push(("key-index", text(device.to_string())));+ }+ node("device", attrs, None)+ })+ .collect();+ content.push(node(+ "devices",+ vec![],+ children(vec![+ node("device-list", vec![], children(devices)),+ node("key-index-list", vec![("ts", now())], None),+ ]),+ ));+ }+ "lid" => content.push(node(+ "lid",+ vec![("val", jid(&device_jid(&record.lid, 0, true)))],+ None,+ )),+ "contact" => {+ content.push(node("contact", vec![("type", text("in"))], None));+ }+ other => content.push(node(other, vec![], None)),+ }+ }+ let user_jid = device_jid(if lid { &record.lid } else { &record.phone }, 0, lid);+ Some(node(+ "user",+ vec![("jid", jid(&user_jid))],+ children(content),+ ))+ })+ .collect();+ let mut attrs = vec![];+ for key in ["sid", "mode", "context", "index", "last"] {+ if let Some(value) = usync.attrs.get(key) {+ attrs.push((key, value.clone()));+ }+ }+ iq_result(+ iq,+ vec![node(+ "usync",+ attrs,+ children(vec![node("list", vec![], children(users))]),+ )],+ )+ }+}++fn with_error(mut ack: Node, code: u16) -> Node {+ ack.attrs+ .insert("error".to_owned(), AttributeValue::String(code.to_string()));+ ack+}++/// An `<iq type="result">` answering a request.+pub(crate) fn iq_result(request: &Node, content: Vec<Node>) -> Node {+ let mut attrs = vec![("type", text("result")), ("from", jid(&SERVER_JID))];+ if let Some(to) = attr_jid(request, "to")+ && to != *SERVER_JID+ {+ attrs[1] = ("from", jid(&to));+ }+ if let Some(id) = request.attrs.get("id") {+ attrs.push(("id", id.clone()));+ }+ let content = (!content.is_empty()).then_some(NodeContent::Nodes(content));+ node("iq", attrs, content)+}++/// An `<iq type="error">` answering a request.+pub(crate) fn iq_error(request: &Node, code: u16, message: &str) -> Node {+ let mut response = iq_result(request, vec![]);+ response.attrs.insert("type".to_owned(), text("error"));+ response.content = children(vec![node(+ "error",+ vec![("code", text(code.to_string())), ("text", text(message))],+ None,+ )]);+ response+}++fn parse_upload(iq: &Node) -> Option<DeviceKeys> {+ use crate::stanza::child_bytes;+ let registration_id = u32::from_be_bytes(child_bytes(iq, "registration")?.try_into().ok()?);+ let identity: [u8; 32] = child_bytes(iq, "identity")?.try_into().ok()?;+ let signed =+ whatsmeow::prekeys::node_to_pre_key(iq.get_optional_child_by_tag(&["skey"])?).ok()?;+ let pre_keys = iq+ .get_optional_child_by_tag(&["list"])?+ .get_children()+ .iter()+ .filter_map(|key| whatsmeow::prekeys::node_to_pre_key(key).ok())+ .map(|key| (key.key_id, key.public))+ .collect();+ Some(DeviceKeys {+ registration_id,+ identity,+ signed_pre_key: (signed.key_id, signed.public, signed.signature?),+ pre_keys,+ })+}diff --git a/crates/whatsapp_fake_server/src/test.rs b/crates/whatsapp_fake_server/src/test.rsnew file mode 100644index 0000000000..339fe49961--- /dev/null+++ b/crates/whatsapp_fake_server/src/test.rs@@ -0,0 +1,400 @@+//! The whatsmeow client against the fake server over real local websockets,+//! with real Signal sessions on both sides.++use std::sync::Arc;+use std::time::Duration;++use tokio::sync::mpsc;+use tokio_util::sync::CancellationToken;+use whatsmeow::store::{Device, MemoryStore};+use whatsmeow::types::Jid;+use whatsmeow::{Client, ClientConfig, Event, QrChannelItem};+use whatsmeow_signal::LibsignalCurveSignatures;++use super::*;++const TIMEOUT: Duration = Duration::from_secs(20);++struct Linked {+ client: Arc<Client>,+ events: mpsc::UnboundedReceiver<Event>,+ device: Jid,+ store: Arc<MemoryStore>,+}++fn new_client(+ server: &FakeWhatsApp,+ device: Device,+) -> (Arc<Client>, mpsc::UnboundedReceiver<Event>) {+ let config = ClientConfig {+ dialer: server.dialer(),+ media_base_url: Some(server.media_base_url()),+ ..ClientConfig::default()+ };+ let client = Client::new(device, test_signal(), config);+ let events = client.subscribe();+ (client, events)+}++async fn next_event(+ events: &mut mpsc::UnboundedReceiver<Event>,+ matches: impl Fn(&Event) -> bool,+) -> Event {+ tokio::time::timeout(TIMEOUT, async {+ loop {+ let event = events.recv().await.expect("the event channel is open");+ if matches(&event) {+ return event;+ }+ }+ })+ .await+ .expect("the event arrives")+}++/// Pairs a new client by scanning its first QR code with `phone`, then+/// waits for it to log in and upload prekeys.+async fn link(server: &FakeWhatsApp, phone: &FakePhone) -> Linked {+ let store = Arc::new(MemoryStore::default());+ let device = store.new_device(&LibsignalCurveSignatures::new());+ let (client, mut events) = new_client(server, device);+ let mut qr = client+ .get_qr_channel(CancellationToken::new())+ .await+ .expect("the QR channel opens before connecting");+ client.connect().await.expect("the client connects");+ let Some(QrChannelItem::Code { code, .. }) = qr.recv().await else {+ panic!("expected a QR code");+ };+ let device = phone+ .scan_qr(&code)+ .await+ .expect("the phone approves the QR code");+ assert!(+ server.wait_until_online(&device, TIMEOUT).await,+ "the device logs in"+ );+ next_event(&mut events, |event| matches!(event, Event::Connected(_))).await;+ assert!(server.wait_until_keys_uploaded(&device, TIMEOUT).await);+ Linked {+ client,+ events,+ device,+ store,+ }+}++#[tokio::test]+async fn a_client_pairs_by_qr_and_logs_in() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let linked = link(&server, &alice).await;++ assert_eq!(linked.device.user, "15550000001");+ assert_eq!(linked.device.device, 1);+ assert_eq!(linked.client.get_own_id().await, linked.device);+ assert_eq!(linked.client.get_own_lid().await.user, alice.lid().user);+ assert_eq!(server.linked_devices(&alice), vec![linked.device.clone()]);+ assert_eq!(linked.store.saved_devices(), vec![linked.device.clone()]);+}++#[tokio::test]+async fn a_second_login_of_the_same_device_replaces_the_first() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let mut first = link(&server, &alice).await;++ // A second client with the same device keys, as another replica that+ // loaded the same stored device.+ let device = clone_device(&first).await;+ let (second, mut second_events) = new_client(&server, device);+ second.connect().await.unwrap();+ next_event(&mut second_events, |event| {+ matches!(event, Event::Connected(_))+ })+ .await;++ next_event(&mut first.events, |event| {+ matches!(event, Event::StreamReplaced(_))+ })+ .await;+ assert!(server.is_online(&first.device));+}++#[tokio::test]+async fn a_kicked_device_is_logged_out() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let mut linked = link(&server, &alice).await;+ server.kick(&linked.device);+ next_event(&mut linked.events, |event| {+ matches!(event, Event::LoggedOut(_))+ })+ .await;+ assert!(server.linked_devices(&alice).is_empty());+}++/// A device with the same keys and identity as the linked one, backed by the+/// same memory store.+async fn clone_device(linked: &Linked) -> Device {+ let original = linked.client.store.read().await;+ let mut device = linked.store.new_device(&LibsignalCurveSignatures::new());+ device.noise_key = original.noise_key.clone();+ device.identity_key = original.identity_key.clone();+ device.signed_pre_key = original.signed_pre_key.clone();+ device.registration_id = original.registration_id;+ device.adv_secret_key = original.adv_secret_key.clone();+ device.id = original.id.clone();+ device.lid = original.lid.clone();+ device.account = original.account.clone();+ device.platform = original.platform.clone();+ device.push_name = original.push_name.clone();+ device.initialized = true;+ device+}++#[tokio::test]+async fn phones_exchange_direct_and_group_messages() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let bob = server.add_user("15550000002", "Bob").await;+ let carol = server.add_user("15550000003", "Carol").await;++ let id = alice.send_text(&bob.jid(), "hi bob").await.unwrap();+ let received = bob+ .wait_for_message(TIMEOUT, |message| message.id == id)+ .await+ .expect("bob receives the direct message");+ assert_eq!(received.text(), Some("hi bob"));+ assert_eq!(received.sender_phone, alice.jid());+ alice+ .wait_for_receipt(TIMEOUT, |receipt| receipt.id == id)+ .await+ .expect("alice gets a delivery receipt");++ // Bob answers on the session alice's prekey message started.+ let reply = bob.send_text(&alice.jid(), "hi alice").await.unwrap();+ let received = alice+ .wait_for_message(TIMEOUT, |message| message.id == reply)+ .await+ .expect("alice receives the reply");+ assert_eq!(received.text(), Some("hi alice"));++ let group = server.create_group("Friends", &[&alice, &bob, &carol]);+ let first = carol.send_text(&group, "first").await.unwrap();+ let second = carol.send_text(&group, "second").await.unwrap();+ for phone in [&alice, &bob] {+ for (id, text) in [(&first, "first"), (&second, "second")] {+ let received = phone+ .wait_for_message(TIMEOUT, |message| &message.id == id)+ .await+ .expect("every member receives the group message");+ assert_eq!(received.text(), Some(text));+ assert_eq!(received.chat, group);+ }+ }+ assert!(alice.failures().is_empty() && bob.failures().is_empty());+}++fn text_message(text: &str) -> whatsmeow::proto::wa_e2e::Message {+ whatsmeow::proto::wa_e2e::Message {+ conversation: Some(text.to_owned()),+ ..Default::default()+ }+}++/// The next message event whose text is `text`.+async fn next_text(+ events: &mut mpsc::UnboundedReceiver<Event>,+ text: &str,+) -> whatsmeow::types::events::Message {+ let event = next_event(events, |event| {+ matches!(event, Event::Message(message)+ if message.message.as_ref().and_then(|message| message.conversation.as_deref()) == Some(text))+ })+ .await;+ let Event::Message(message) = event else {+ unreachable!("matched a message event");+ };+ *message+}++#[tokio::test]+async fn a_client_and_a_phone_exchange_direct_messages() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let bob = server.add_user("15550000002", "Bob").await;+ let mut linked = link(&server, &alice).await;++ let sent = linked+ .client+ .send_message(+ &bob.jid(),+ text_message("hello from macro"),+ Default::default(),+ )+ .await+ .expect("the server acks the message");+ let received = bob+ .wait_for_message(TIMEOUT, |message| message.id == sent.id)+ .await+ .expect("bob's phone decrypts the message");+ assert_eq!(received.text(), Some("hello from macro"));+ assert_eq!(received.sender_phone, alice.jid());+ let own = alice+ .wait_for_message(TIMEOUT, |message| message.id == sent.id)+ .await+ .expect("alice's own phone gets the device-sent copy");+ assert_eq!(own.chat.user, bob.lid().user);++ let reply = bob.send_text(&alice.jid(), "hello back").await.unwrap();+ let message = next_text(&mut linked.events, "hello back").await;+ assert_eq!(message.info.id, reply);+ assert_eq!(message.info.source.sender.user, bob.lid().user);+ assert!(bob.failures().is_empty() && alice.failures().is_empty());+}++#[tokio::test]+async fn a_client_and_phones_talk_in_a_group() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let bob = server.add_user("15550000002", "Bob").await;+ let carol = server.add_user("15550000003", "Carol").await;+ let mut linked = link(&server, &alice).await;+ let group = server.create_group("Book club", &[&bob, &alice, &carol]);++ let groups = linked.client.get_joined_groups().await.unwrap();+ assert_eq!(groups.len(), 1);+ assert_eq!(groups[0].jid, group);+ assert_eq!(groups[0].name.name, "Book club");++ let incoming = bob.send_text(&group, "welcome").await.unwrap();+ let message = next_text(&mut linked.events, "welcome").await;+ assert_eq!(message.info.id, incoming);+ assert_eq!(message.info.source.chat, group);+ assert!(message.info.source.is_group);++ let sent = linked+ .client+ .send_message(&group, text_message("thanks"), Default::default())+ .await+ .unwrap();+ for phone in [&alice, &bob, &carol] {+ let received = phone+ .wait_for_message(TIMEOUT, |message| message.id == sent.id)+ .await+ .expect("every member's phone decrypts the group message");+ assert_eq!(received.text(), Some("thanks"));+ assert_eq!(received.chat, group);+ assert_eq!(received.sender.user, alice.lid().user);+ assert_eq!(received.sender.device, linked.device.device);+ }+ // A second message reuses the sender key.+ let again = linked+ .client+ .send_message(&group, text_message("again"), Default::default())+ .await+ .unwrap();+ bob.wait_for_message(TIMEOUT, |message| message.id == again.id)+ .await+ .expect("the second group message decrypts");+ bob.wait_for_receipt(TIMEOUT, |receipt| {+ receipt.id == incoming && receipt.from.device == linked.device.device+ })+ .await+ .expect("the linked client sends a delivery receipt");+ for phone in [&alice, &bob, &carol] {+ assert!(phone.failures().is_empty(), "{:?}", phone.failures());+ }+}++#[tokio::test]+async fn messages_sent_while_offline_arrive_on_reconnect() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let bob = server.add_user("15550000002", "Bob").await;+ let mut linked = link(&server, &alice).await;+ let group = server.create_group("Offline", &[&alice, &bob]);++ linked.client.disconnect().await;+ assert!(server.wait_until_offline(&linked.device, TIMEOUT).await);+ let first = bob.send_text(&group, "while you were out").await.unwrap();+ let second = bob.send_text(&group, "and another").await.unwrap();+ assert_eq!(server.pending_count(&linked.device), 2);++ linked.client.connect().await.unwrap();+ let message = next_text(&mut linked.events, "while you were out").await;+ assert_eq!(message.info.id, first);+ let message = next_text(&mut linked.events, "and another").await;+ assert_eq!(message.info.id, second);+ next_event(&mut linked.events, |event| {+ matches!(event, Event::OfflineSyncCompleted(_))+ })+ .await;+ assert_eq!(server.delivery_count(&linked.device, &first), 1);+ for id in [&first, &second] {+ bob.wait_for_receipt(TIMEOUT, |receipt| {+ &receipt.id == id && receipt.from.device == linked.device.device+ })+ .await+ .expect("the linked client receipts each offline message");+ }+ assert_eq!(server.pending_count(&linked.device), 0);+}++#[tokio::test]+async fn images_travel_both_ways_through_the_media_host() {+ let server = FakeWhatsApp::start().await.unwrap();+ let alice = server.add_user("15550000001", "Alice").await;+ let bob = server.add_user("15550000002", "Bob").await;+ let mut linked = link(&server, &alice).await;+ let group = server.create_group("Photos", &[&alice, &bob]);++ let photo = b"not really a jpeg, but encrypted like one".repeat(50);+ let incoming = bob.send_image(&group, &photo, "look").await.unwrap();+ let event = next_event(&mut linked.events, |event| {+ matches!(event, Event::Message(message) if message.info.id == incoming+ && message.message.as_ref().is_some_and(|message| message.image_message.is_some()))+ })+ .await;+ let Event::Message(message) = event else {+ unreachable!("matched a message event");+ };+ let image = message+ .message+ .as_ref()+ .and_then(|message| message.image_message.as_deref())+ .expect("the client receives an image message");+ assert_eq!(image.caption.as_deref(), Some("look"));+ assert_eq!(linked.client.download(image).await.unwrap(), photo);++ let reply = b"a photo from macro".repeat(20);+ let uploaded = linked+ .client+ .upload(&reply, whatsmeow::download::MediaType::Image)+ .await+ .expect("the client uploads to the fake media host");+ let message = whatsmeow::proto::wa_e2e::Message {+ image_message: Some(Box::new(whatsmeow::proto::wa_e2e::ImageMessage {+ url: Some(uploaded.url),+ direct_path: Some(uploaded.direct_path),+ media_key: Some(uploaded.media_key),+ file_enc_sha256: Some(uploaded.file_enc_sha256),+ file_sha256: Some(uploaded.file_sha256),+ file_length: Some(uploaded.file_length),+ mimetype: Some("image/jpeg".to_owned()),+ ..Default::default()+ })),+ ..Default::default()+ };+ let sent = linked+ .client+ .send_message(&group, message, Default::default())+ .await+ .unwrap();+ let received = bob+ .wait_for_message(TIMEOUT, |message| message.id == sent.id)+ .await+ .expect("bob's phone receives the image");+ assert_eq!(bob.download_image(&received).await.unwrap(), reply);+}diff --git a/crates/whatsmeow_signal/AGENTS.md b/crates/whatsmeow_signal/AGENTS.mdnew file mode 100644index 0000000000..949ff5a190--- /dev/null+++ b/crates/whatsmeow_signal/AGENTS.md@@ -0,0 +1,58 @@+# whatsmeow_signal++## What it is++The Signal protocol for WhatsApp linked devices: `Libsignal` implements the+whatsmeow port's `signal::{CurveSignatures, SignalProtocol}` traits+(`crates/whatsmeow/src/signal.rs`), so it is a `whatsmeow::Signal`;+`libsignal()` returns it as the `Arc<dyn whatsmeow::Signal>` a `Client`+takes. `LibsignalProtocol` and `LibsignalCurveSignatures` are older aliases.+AGPL-3.0-only.++## How it works++- XEdDSA and Curve25519 keys: `libsignal-core`, unmodified, git tag `v0.105.2`+ (the root `Cargo.toml` pins it).+- X3DH, Double Ratchet and sender keys: libsignal's `rust/protocol` modules+ vendored from tag `v0.74.1` into `src/protocol/` and patched (`NOTICE` and+ each file header list the changes). Official libsignal can't be used as a+ dependency for these: since v0.75 it rejects bundles and pre-key messages+ without Kyber (WhatsApp has none); every version writes version-3 messages+ with the byte `0x34`, which whatsmeow's MAC check rejects ("no valid+ sessions" — it recomputes the MAC over `0x33`); and its sender key wire+ format (distribution UUID + chain ID) is not WhatsApp's (32-bit key ID).+- Records cross the store as libsignal's `RecordStructure` and+ `SenderKeyRecordStructure` protobufs (`proto/storage.proto`, field numbers+ unchanged). whatsmeow's Go stores keep JSON records, so a Go whatsmeow+ database can't be imported as is.++## Start here++- `src/lib.rs`: the trait implementations and error mapping.+- `src/interop_test.rs` + `testdata/generator/README.md`: the Go interop rig.+- `tests/client_end_to_end.rs`: the real whatsmeow `Client` on `Libsignal`+ against `whatsapp_fake_server` (a dev-dependency cycle cargo allows; it+ must stay an integration test so both sides share one crate instance).++## Checks++- `cargo test -p whatsmeow_signal` (replays `testdata/interop_transcript.json`)+- After touching `src/protocol/`: also run the live Go test, see+ `testdata/generator/README.md`.++## Gotchas++- Don't upgrade the vendored modules to a newer libsignal tag wholesale: newer+ code assumes Kyber and SPQR. Port fixes by hand and record them in `NOTICE`.+- Never log or put in errors a `SignalAddress`: its name is a phone number+ or LID.+- `LibsignalProtocol::seeded` / `LibsignalCurveSignatures::seeded` exist only+ under `cfg(test)`; production randomness is `StdRng::from_os_rng` per call.+- The transcript replay pins Rust's exact output for its seeds; a `rand`+ upgrade can change `StdRng` and require re-recording.+- Do not take code from whatsapp-rust, wa-agent, libsignal-node or+ go.mau.fi/libsignal (GPL); the Go oracle only calls it.++## Related++- `crates/whatsmeow/AGENTS.md` (the trait boundary)diff --git a/crates/whatsmeow_signal/CLAUDE.md b/crates/whatsmeow_signal/CLAUDE.mdnew file mode 120000index 0000000000..47dc3e3d86--- /dev/null+++ b/crates/whatsmeow_signal/CLAUDE.md@@ -0,0 +1 @@+AGENTS.md\ No newline at end of filediff --git a/crates/whatsmeow_signal/Cargo.toml b/crates/whatsmeow_signal/Cargo.tomlnew file mode 100644index 0000000000..72a83d5744--- /dev/null+++ b/crates/whatsmeow_signal/Cargo.toml@@ -0,0 +1,36 @@+[package]+edition = "2024"+name = "whatsmeow_signal"+publish = false+version = "0.1.0"+license = "AGPL-3.0-only"+description = "The Signal protocol for the whatsmeow port, built on Signal's libsignal and patched for WhatsApp's wire format."++[dependencies]+aes = { workspace = true }+async-trait = { workspace = true }+cbc = { workspace = true }+hkdf = { workspace = true }+hmac = { workspace = true }+libsignal-core = { workspace = true }+prost = { workspace = true }+rand = { workspace = true }+rootcause = { workspace = true }+sha2 = { workspace = true }+subtle = { workspace = true }+thiserror = { workspace = true }+tracing = { workspace = true }+whatsmeow = { path = "../whatsmeow" }+workspace-hack = { version = "0.1", path = "../workspace-hack" }++[build-dependencies]+prost-build = { workspace = true }+protox = { workspace = true }++[dev-dependencies]+hex = { workspace = true }+serde = { workspace = true }+serde_json = { workspace = true }+tokio = { workspace = true, features = ["macros", "rt", "time"] }+tokio-util = { workspace = true }+whatsapp_fake_server = { path = "../whatsapp_fake_server" }diff --git a/crates/whatsmeow_signal/NOTICE b/crates/whatsmeow_signal/NOTICEnew file mode 100644index 0000000000..bffe2c274e--- /dev/null+++ b/crates/whatsmeow_signal/NOTICE@@ -0,0 +1,44 @@+whatsmeow_signal includes code from libsignal+(https://github.com/signalapp/libsignal), Copyright 2020-2025 Signal+Messenger, LLC, licensed under the GNU Affero General Public License v3.0+only (SPDX-License-Identifier: AGPL-3.0-only).++libsignal-core is used unmodified as a dependency (tag v0.105.2). The+following files are copied from libsignal tag v0.74.1 (rust/protocol) and+modified by Macro so they speak WhatsApp's dialect of the Signal protocol:++ proto/wire.proto <- rust/protocol/src/proto/wire.proto+ proto/storage.proto <- rust/protocol/src/proto/storage.proto+ src/protocol/consts.rs <- rust/protocol/src/consts.rs+ src/protocol/crypto.rs <- rust/protocol/src/crypto.rs, rust/crypto/src/aes_cbc.rs+ src/protocol/error.rs <- rust/protocol/src/error.rs+ src/protocol/group_cipher.rs<- rust/protocol/src/group_cipher.rs+ src/protocol/messages.rs <- rust/protocol/src/protocol.rs+ src/protocol/proto.rs <- rust/protocol/src/proto.rs+ src/protocol/ratchet.rs <- rust/protocol/src/ratchet.rs, ratchet/keys.rs, ratchet/params.rs+ src/protocol/sender_keys.rs <- rust/protocol/src/sender_keys.rs+ src/protocol/session.rs <- rust/protocol/src/session.rs+ src/protocol/session_cipher.rs <- rust/protocol/src/session_cipher.rs+ src/protocol/session_state.rs <- rust/protocol/src/state/session.rs++Modifications, in summary (each file's header lists its own):++- Session version 3 only: X3DH without Kyber pre-keys, no post-quantum+ ratchet (SPQR), no PQXDH key derivation label.+- Messages are written with the version byte 0x33, as WhatsApp and+ go.mau.fi/libsignal write it; libsignal writes 0x34 for version 3, which+ fails the MAC whatsmeow recomputes over its own re-serialization.+- Sender key messages and distribution messages use WhatsApp's protobuf+ layout (a 32-bit key ID in field 1) instead of a distribution UUID and+ chain ID; sender keys are named by group and sender.+- A pre-key ID of 0 in a pre-key message means "no one-time pre-key".+- Unacknowledged sessions never expire (go.mau.fi/libsignal has no limit).+- Message keys are stored in full (cipher key, MAC key, IV), never as seeds;+ the pre-key timestamp and sender key version fields are not stored.+- Creating a sender key distribution message replaces an empty record.+- AES-256-CBC comes from the RustCrypto `cbc` crate instead of libsignal's+ `signal-crypto`.+- Stores are the whatsmeow port's `SignalProtocolStore`; addresses are kept+ out of errors and logs; logging uses `tracing`.+- Sealed sender, fingerprints, incremental MAC, Kyber/KEM, plaintext content+ and decryption-error messages are not included.diff --git a/crates/whatsmeow_signal/build.rs b/crates/whatsmeow_signal/build.rsnew file mode 100644index 0000000000..69965f2253--- /dev/null+++ b/crates/whatsmeow_signal/build.rs@@ -0,0 +1,13 @@+//! Compiles the vendored libsignal `.proto` files with the pure-Rust `protox`+//! compiler, so no `protoc` binary is needed, and generates prost types.++const PROTO_FILES: [&str; 2] = ["proto/wire.proto", "proto/storage.proto"];++fn main() {+ println!("cargo:rerun-if-changed=proto");+ let descriptors =+ protox::compile(PROTO_FILES, ["proto"]).expect("the vendored .proto files compile");+ prost_build::Config::new()+ .compile_fds(descriptors)+ .expect("prost can write generated code into OUT_DIR");+}diff --git a/crates/whatsmeow_signal/proto/storage.proto b/crates/whatsmeow_signal/proto/storage.protonew file mode 100644index 0000000000..e6ff13bb17--- /dev/null+++ b/crates/whatsmeow_signal/proto/storage.proto@@ -0,0 +1,97 @@+//+// Copyright 2020 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Modified for WhatsApp by Macro: the Kyber, post-quantum ratchet, pre-key+// timestamp, message key seed and sender key version fields are gone (their+// numbers are reserved), since WhatsApp sessions are version 3 only. Records+// stay readable by libsignal's own storage.proto. See NOTICE.++syntax = "proto3";++package signal.proto.storage;++message SessionStructure {+ message Chain {+ bytes sender_ratchet_key = 1;+ bytes sender_ratchet_key_private = 2;++ message ChainKey {+ uint32 index = 1;+ bytes key = 2;+ }++ ChainKey chain_key = 3;++ message MessageKey {+ uint32 index = 1;+ bytes cipher_key = 2;+ bytes mac_key = 3;+ bytes iv = 4;+ reserved 5; // seed+ }++ repeated MessageKey message_keys = 4;+ }++ message PendingPreKey {+ optional uint32 pre_key_id = 1;+ int32 signed_pre_key_id = 3;+ bytes base_key = 2;+ reserved 4; // timestamp+ }++ uint32 session_version = 1;+ bytes local_identity_public = 2;+ bytes remote_identity_public = 3;++ bytes root_key = 4;+ uint32 previous_counter = 5;++ Chain sender_chain = 6;+ // The order is significant; keys at the end are "older" and will get trimmed.+ repeated Chain receiver_chains = 7;++ reserved 8; // pending key exchange, never used by WhatsApp+ PendingPreKey pending_pre_key = 9;++ uint32 remote_registration_id = 10;+ uint32 local_registration_id = 11;++ reserved 12; // no longer used+ bytes alice_base_key = 13;+ reserved 14, 15; // pending Kyber pre-key, post-quantum ratchet state+}++message RecordStructure {+ SessionStructure current_session = 1;+ // The order is significant; sessions at the end are "older" and will get trimmed.+ repeated /*SessionStructure*/ bytes previous_sessions = 2;+}++message SenderKeyStateStructure {+ message SenderChainKey {+ uint32 iteration = 1;+ bytes seed = 2;+ }++ message SenderMessageKey {+ uint32 iteration = 1;+ bytes seed = 2;+ }++ message SenderSigningKey {+ bytes public = 1;+ bytes private = 2;+ }++ reserved 5; // message version, always 3+ uint32 chain_id = 1;+ SenderChainKey sender_chain_key = 2;+ SenderSigningKey sender_signing_key = 3;+ repeated SenderMessageKey sender_message_keys = 4;+}++message SenderKeyRecordStructure {+ repeated SenderKeyStateStructure sender_key_states = 1;+}diff --git a/crates/whatsmeow_signal/proto/wire.proto b/crates/whatsmeow_signal/proto/wire.protonew file mode 100644index 0000000000..edae7e75db--- /dev/null+++ b/crates/whatsmeow_signal/proto/wire.proto@@ -0,0 +1,40 @@+//+// Copyright 2020-2022 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Modified for WhatsApp by Macro: the Kyber and post-quantum ratchet fields+// are gone, and the sender key messages use WhatsApp's layout (a 32-bit key+// ID in field 1 instead of a distribution UUID plus chain ID). See NOTICE.++syntax = "proto2";++package signal.proto.wire;++message SignalMessage {+ optional bytes ratchet_key = 1;+ optional uint32 counter = 2;+ optional uint32 previous_counter = 3;+ optional bytes ciphertext = 4;+}++message PreKeySignalMessage {+ optional uint32 pre_key_id = 1;+ optional bytes base_key = 2;+ optional bytes identity_key = 3;+ optional bytes message = 4; // SignalMessage+ optional uint32 registration_id = 5;+ optional uint32 signed_pre_key_id = 6;+}++message SenderKeyMessage {+ optional uint32 id = 1;+ optional uint32 iteration = 2;+ optional bytes ciphertext = 3;+}++message SenderKeyDistributionMessage {+ optional uint32 id = 1;+ optional uint32 iteration = 2;+ optional bytes chain_key = 3;+ optional bytes signing_key = 4;+}diff --git a/crates/whatsmeow_signal/src/interop_test.rs b/crates/whatsmeow_signal/src/interop_test.rsnew file mode 100644index 0000000000..b5a7c01702--- /dev/null+++ b/crates/whatsmeow_signal/src/interop_test.rs@@ -0,0 +1,696 @@+//! Interop with go.mau.fi/libsignal v0.2.2, the Signal implementation+//! whatsmeow uses, through the Go oracle in `testdata/generator`.+//!+//! One scripted conversation runs Rust devices against Go devices. Run live+//! (`cargo test -p whatsmeow_signal -- --ignored`), every Go call goes to the+//! oracle and the exchange can be recorded to+//! `testdata/interop_transcript.json`. The default test replays that+//! recording: Rust's randomness is seeded, so it must send exactly the bytes+//! Go accepted, and it must decrypt exactly the bytes Go produced.++use std::collections::{BTreeSet, VecDeque};+use std::io::{BufRead, BufReader, Write};+use std::path::PathBuf;+use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};++use libsignal_core::curve::KeyPair;+use rand::SeedableRng;+use rand::rngs::StdRng;+use serde::{Deserialize, Serialize};+use serde_json::{Value, json};+use whatsmeow::signal::{+ CiphertextType, CurveSignatures, PreKeyBundle, SenderKeyName, SignalAddress, SignalError,+ SignalProtocol,+};++use crate::Libsignal;+use crate::test_store::TestStore;++const TRANSCRIPT: &str = "testdata/interop_transcript.json";+const PROTOCOL_SEED: u64 = 0x5167_6e61_6c01;+const SIGNATURE_SEED: u64 = 0x5167_6e61_6c02;+const KEY_SEED: u64 = 0x5167_6e61_6c03;+const GROUP: &str = "[email protected]";++#[derive(Serialize, Deserialize)]+struct Exchange {+ request: Value,+ response: Value,+}++/// The Go side of the conversation.+trait Oracle {+ fn call(&mut self, request: Value) -> Value;+}++/// The Go oracle process, recording every exchange.+struct LiveOracle {+ child: Child,+ stdin: ChildStdin,+ stdout: BufReader<ChildStdout>,+ exchanges: Vec<Exchange>,+}++impl LiveOracle {+ fn start() -> Self {+ let mut child = Command::new("go")+ .args(["run", "."])+ .current_dir(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/generator"))+ .stdin(Stdio::piped())+ .stdout(Stdio::piped())+ .spawn()+ .expect("`go` is on PATH (nix shell nixpkgs#go)");+ let stdin = child.stdin.take().expect("stdin is piped");+ let stdout = BufReader::new(child.stdout.take().expect("stdout is piped"));+ Self {+ child,+ stdin,+ stdout,+ exchanges: Vec::new(),+ }+ }++ fn finish(mut self) -> Vec<Exchange> {+ drop(self.stdin);+ assert!(self.child.wait().expect("the oracle exits").success());+ self.exchanges+ }+}++impl Oracle for LiveOracle {+ fn call(&mut self, request: Value) -> Value {+ writeln!(self.stdin, "{request}").expect("the oracle reads requests");+ let mut line = String::new();+ self.stdout+ .read_line(&mut line)+ .expect("the oracle answers");+ let response: Value = serde_json::from_str(&line)+ .unwrap_or_else(|error| panic!("the oracle answered {line:?}: {error}"));+ self.exchanges.push(Exchange {+ request,+ response: response.clone(),+ });+ response+ }+}++/// A recorded conversation: each request must match the recorded one.+struct ReplayOracle {+ exchanges: VecDeque<Exchange>,+}++impl Oracle for ReplayOracle {+ fn call(&mut self, request: Value) -> Value {+ let exchange = self+ .exchanges+ .pop_front()+ .expect("the transcript has an answer for every request");+ assert_eq!(request, exchange.request, "Rust sent different bytes");+ exchange.response+ }+}++/// The response, or a panic with the oracle's error.+fn ok(response: Value) -> Value {+ assert!(+ response.get("error").is_none(),+ "go.mau.fi/libsignal failed: {response}"+ );+ response+}++fn bytes(response: &Value, field: &str) -> Vec<u8> {+ hex::decode(response[field].as_str().expect("the field is a hex string"))+ .expect("the field is hex")+}++fn key(response: &Value, field: &str) -> [u8; 32] {+ bytes(response, field)+ .try_into()+ .expect("the key is 32 bytes")+}++fn peer(address: &SignalAddress) -> Value {+ json!({ "name": address.name, "device": address.device_id })+}++fn bundle_json(bundle: &PreKeyBundle) -> Value {+ let mut value = json!({+ "registration_id": bundle.registration_id,+ "device_id": bundle.device_id,+ "signed_pre_key": {+ "id": bundle.signed_pre_key_id,+ "public": hex::encode(bundle.signed_pre_key_public),+ },+ "signed_pre_key_signature": hex::encode(bundle.signed_pre_key_signature),+ "identity_key": hex::encode(bundle.identity_key),+ });+ if let Some((id, public)) = bundle.pre_key {+ value["pre_key"] = json!({ "id": id, "public": hex::encode(public) });+ }+ value+}++fn bundle_from_json(value: &Value) -> PreKeyBundle {+ let id = |field: &Value| {+ u32::try_from(field.as_u64().expect("the ID is a number")).expect("the ID fits in u32")+ };+ PreKeyBundle {+ registration_id: id(&value["registration_id"]),+ device_id: id(&value["device_id"]),+ pre_key: value+ .get("pre_key")+ .map(|pre_key| (id(&pre_key["id"]), key(pre_key, "public"))),+ signed_pre_key_id: id(&value["signed_pre_key"]["id"]),+ signed_pre_key_public: key(&value["signed_pre_key"], "public"),+ signed_pre_key_signature: bytes(value, "signed_pre_key_signature")+ .try_into()+ .expect("the signature is 64 bytes"),+ identity_key: key(value, "identity_key"),+ }+}++fn kind_name(kind: CiphertextType) -> &'static str {+ match kind {+ CiphertextType::PreKey => "pkmsg",+ CiphertextType::Whisper => "msg",+ }+}++/// Go encrypts `plaintext` from `device` to `to`; returns (type, ciphertext).+fn go_encrypt(+ oracle: &mut dyn Oracle,+ device: &str,+ to: &SignalAddress,+ plaintext: &[u8],+) -> (String, Vec<u8>) {+ let response = ok(oracle.call(json!({+ "op": "encrypt", "device": device, "peer": peer(to), "plaintext": hex::encode(plaintext),+ })));+ (+ response["type"]+ .as_str()+ .expect("the type is a string")+ .to_owned(),+ bytes(&response, "ciphertext"),+ )+}++fn go_decrypt(+ oracle: &mut dyn Oracle,+ device: &str,+ from: &SignalAddress,+ kind: CiphertextType,+ ciphertext: &[u8],+) -> Value {+ oracle.call(json!({+ "op": "decrypt", "device": device, "peer": peer(from),+ "type": kind_name(kind), "ciphertext": hex::encode(ciphertext),+ }))+}++async fn xeddsa(oracle: &mut dyn Oracle, signatures: &Libsignal) {+ let mut generator = StdRng::seed_from_u64(KEY_SEED);+ let mut sign_bits = BTreeSet::new();+ for index in 0..8 {+ let pair = KeyPair::generate(&mut generator);+ let public: [u8; 32] = pair+ .public_key+ .public_key_bytes()+ .try_into()+ .expect("32-byte public key");+ let private: [u8; 32] = pair+ .private_key+ .serialize()+ .try_into()+ .expect("32-byte private key");+ let message = format!("xeddsa vector {index}").into_bytes();++ let go_signature: [u8; 64] = bytes(+ &ok(oracle.call(json!({+ "op": "sign", "private_key": hex::encode(private), "message": hex::encode(&message),+ }))),+ "signature",+ )+ .try_into()+ .expect("64-byte signature");+ sign_bits.insert(go_signature[63] & 0x80);+ assert!(signatures.verify_signature(&public, &message, &go_signature));+ assert!(!signatures.verify_signature(&public, b"another message", &go_signature));++ let rust_signature = signatures.calculate_signature(&private, &message);+ assert_eq!(rust_signature[63] & 0x80, go_signature[63] & 0x80);+ let verified = ok(oracle.call(json!({+ "op": "verify", "public_key": hex::encode(public), "message": hex::encode(&message),+ "signature": hex::encode(rust_signature),+ })));+ assert_eq!(verified["valid"], json!(true));+ let mut flipped = rust_signature;+ flipped[63] ^= 0x80;+ let verified = ok(oracle.call(json!({+ "op": "verify", "public_key": hex::encode(public), "message": hex::encode(&message),+ "signature": hex::encode(flipped),+ })));+ assert_eq!(verified["valid"], json!(false));+ }+ assert_eq!(+ sign_bits,+ BTreeSet::from([0x00, 0x80]),+ "the vectors cover both sign bits"+ );+}++/// Rust starts sessions from Go bundles; then a long back and forth.+async fn rust_initiates(oracle: &mut dyn Oracle, protocol: &Libsignal, alice: &TestStore) {+ let alice_address = SignalAddress::new("rust-alice".to_owned(), 1);+ let bob_address = SignalAddress::new("go-bob".to_owned(), 2);+ ok(oracle.call(json!({ "op": "new_device", "device": "bob", "registration_id": 2002 })));+ let bundle = ok(oracle.call(json!({+ "op": "make_bundle", "device": "bob", "signed_pre_key_id": 1, "pre_key_id": 31,+ })));+ let bundle = bundle_from_json(&bundle["bundle"]);+ protocol+ .process_bundle(alice, &bob_address, &bundle)+ .await+ .expect("Rust processes Go's bundle");++ let first = protocol+ .encrypt(alice, &bob_address, b"first")+ .await+ .expect("Rust encrypts");+ let second = protocol+ .encrypt(alice, &bob_address, b"second")+ .await+ .expect("Rust encrypts");+ assert_eq!(first.kind, CiphertextType::PreKey);+ for (message, expected) in [(&second, &b"second"[..]), (&first, b"first")] {+ let decrypted = ok(go_decrypt(+ oracle,+ "bob",+ &alice_address,+ message.kind,+ &message.serialized,+ ));+ assert_eq!(bytes(&decrypted, "plaintext"), expected);+ }++ let replies: Vec<(String, Vec<u8>)> = [&b"reply one"[..], b"reply two", b"reply three"]+ .into_iter()+ .map(|plaintext| go_encrypt(oracle, "bob", &alice_address, plaintext))+ .collect();+ for (index, expected) in [+ (0, &b"reply one"[..]),+ (2, b"reply three"),+ (1, b"reply two"),+ ] {+ assert_eq!(replies[index].0, "msg");+ assert_eq!(+ protocol+ .decrypt_message(alice, &bob_address, &replies[index].1)+ .await+ .expect("Rust decrypts Go's reply"),+ expected+ );+ }+ assert!(matches!(+ protocol+ .decrypt_message(alice, &bob_address, &replies[1].1)+ .await,+ Err(SignalError::OldCounter)+ ));+ let (_, untouched) = go_encrypt(oracle, "bob", &alice_address, b"untouched");+ let mut bad_mac = untouched.clone();+ let last = bad_mac.len() - 1;+ bad_mac[last] ^= 0x01;+ assert!(matches!(+ protocol+ .decrypt_message(alice, &bob_address, &bad_mac)+ .await,+ Err(SignalError::Internal(_))+ ));+ assert_eq!(+ protocol+ .decrypt_message(alice, &bob_address, &untouched)+ .await+ .expect("the untouched message still decrypts"),+ b"untouched"+ );++ // Several ratchet turns in both directions, each out of order.+ for round in 0..4 {+ let rust_messages = [+ format!("rust round {round} a"),+ format!("rust round {round} b"),+ ];+ let mut sent = Vec::new();+ for plaintext in &rust_messages {+ let message = protocol+ .encrypt(alice, &bob_address, plaintext.as_bytes())+ .await+ .expect("Rust encrypts");+ assert_eq!(message.kind, CiphertextType::Whisper);+ sent.push(message);+ }+ for index in [1, 0] {+ let decrypted = ok(go_decrypt(+ oracle,+ "bob",+ &alice_address,+ sent[index].kind,+ &sent[index].serialized,+ ));+ assert_eq!(+ bytes(&decrypted, "plaintext"),+ rust_messages[index].as_bytes()+ );+ }+ let duplicate = go_decrypt(+ oracle,+ "bob",+ &alice_address,+ sent[0].kind,+ &sent[0].serialized,+ );+ assert!(duplicate.get("error").is_some(), "Go rejects a duplicate");++ let go_messages = [format!("go round {round} a"), format!("go round {round} b")];+ let received: Vec<(String, Vec<u8>)> = go_messages+ .iter()+ .map(|plaintext| go_encrypt(oracle, "bob", &alice_address, plaintext.as_bytes()))+ .collect();+ for index in [1, 0] {+ assert_eq!(+ protocol+ .decrypt_message(alice, &bob_address, &received[index].1)+ .await+ .expect("Rust decrypts Go"),+ go_messages[index].as_bytes()+ );+ }+ }++ // A bundle without a one-time pre-key.+ let erin_address = SignalAddress::new("go-erin".to_owned(), 5);+ ok(oracle.call(json!({ "op": "new_device", "device": "erin", "registration_id": 5005 })));+ let bundle = ok(oracle.call(json!({+ "op": "make_bundle", "device": "erin", "signed_pre_key_id": 9,+ })));+ protocol+ .process_bundle(alice, &erin_address, &bundle_from_json(&bundle["bundle"]))+ .await+ .expect("Rust processes a bundle without a one-time pre-key");+ let hello = protocol+ .encrypt(alice, &erin_address, b"hello erin")+ .await+ .expect("Rust encrypts");+ let decrypted = ok(go_decrypt(+ oracle,+ "erin",+ &alice_address,+ hello.kind,+ &hello.serialized,+ ));+ assert_eq!(bytes(&decrypted, "plaintext"), b"hello erin");+}++/// Go starts sessions from Rust bundles.+async fn go_initiates(oracle: &mut dyn Oracle, protocol: &Libsignal, carol: &TestStore) {+ let carol_address = SignalAddress::new("rust-carol".to_owned(), 3);+ let dave_address = SignalAddress::new("go-dave".to_owned(), 4);+ ok(oracle.call(json!({ "op": "new_device", "device": "dave", "registration_id": 4004 })));+ ok(oracle.call(json!({+ "op": "process_bundle", "device": "dave", "peer": peer(&carol_address),+ "bundle": bundle_json(&carol.bundle(3, Some(77))),+ })));+ let (first_kind, first) = go_encrypt(oracle, "dave", &carol_address, b"from go 1");+ let (_, second) = go_encrypt(oracle, "dave", &carol_address, b"from go 2");+ assert_eq!(first_kind, "pkmsg");+ assert_eq!(+ protocol+ .decrypt_pre_key_message(carol, &dave_address, &second)+ .await+ .expect("Rust decrypts Go's second pre-key message"),+ b"from go 2"+ );+ assert!(+ !carol.has_pre_key(77),+ "the used one-time pre-key is removed"+ );+ assert_eq!(+ protocol+ .decrypt_pre_key_message(carol, &dave_address, &first)+ .await+ .expect("Rust decrypts Go's first pre-key message"),+ b"from go 1"+ );++ let reply = protocol+ .encrypt(carol, &dave_address, b"reply from rust")+ .await+ .expect("Rust replies");+ assert_eq!(reply.kind, CiphertextType::Whisper);+ let decrypted = ok(go_decrypt(+ oracle,+ "dave",+ &carol_address,+ reply.kind,+ &reply.serialized,+ ));+ assert_eq!(bytes(&decrypted, "plaintext"), b"reply from rust");+ let (kind, after) = go_encrypt(oracle, "dave", &carol_address, b"after the reply");+ assert_eq!(kind, "msg");+ assert_eq!(+ protocol+ .decrypt_message(carol, &dave_address, &after)+ .await+ .expect("Rust decrypts"),+ b"after the reply"+ );++ // A Rust bundle without a one-time pre-key.+ let frank_address = SignalAddress::new("go-frank".to_owned(), 6);+ ok(oracle.call(json!({ "op": "new_device", "device": "frank", "registration_id": 6006 })));+ ok(oracle.call(json!({+ "op": "process_bundle", "device": "frank", "peer": peer(&carol_address),+ "bundle": bundle_json(&carol.bundle(3, None)),+ })));+ let (_, hello) = go_encrypt(oracle, "frank", &carol_address, b"no one-time key");+ assert_eq!(+ protocol+ .decrypt_pre_key_message(carol, &frank_address, &hello)+ .await+ .expect("Rust decrypts"),+ b"no one-time key"+ );+}++async fn groups(oracle: &mut dyn Oracle, protocol: &Libsignal, alice: &TestStore) {+ let alice_key = SenderKeyName {+ group_id: GROUP.to_owned(),+ sender: SignalAddress::new("rust-alice".to_owned(), 1),+ };+ let bob_key = SenderKeyName {+ group_id: GROUP.to_owned(),+ sender: SignalAddress::new("go-bob".to_owned(), 2),+ };++ // Rust sends to the group.+ let distribution = protocol+ .create_sender_key_distribution_message(alice, &alice_key)+ .await+ .expect("Rust creates its sender key");+ ok(oracle.call(json!({+ "op": "process_sender_key", "device": "bob", "group": GROUP,+ "peer": peer(&alice_key.sender), "message": hex::encode(&distribution),+ })));+ let mut sent = Vec::new();+ for plaintext in ["group zero", "group one", "group two"] {+ sent.push(+ protocol+ .group_encrypt(alice, &alice_key, plaintext.as_bytes())+ .await+ .expect("Rust encrypts for the group"),+ );+ }+ for (index, expected) in [(2, "group two"), (0, "group zero"), (1, "group one")] {+ let decrypted = ok(oracle.call(json!({+ "op": "group_decrypt", "device": "bob", "group": GROUP,+ "peer": peer(&alice_key.sender), "ciphertext": hex::encode(&sent[index]),+ })));+ assert_eq!(bytes(&decrypted, "plaintext"), expected.as_bytes());+ }++ // Go sends to the group.+ let go_distribution = ok(oracle.call(json!({+ "op": "create_sender_key", "device": "bob", "group": GROUP, "peer": peer(&bob_key.sender),+ })));+ protocol+ .process_sender_key_distribution_message(+ alice,+ &bob_key,+ &bytes(&go_distribution, "message"),+ )+ .await+ .expect("Rust stores Go's sender key");+ let received: Vec<Vec<u8>> = ["go group zero", "go group one", "go group two"]+ .into_iter()+ .map(|plaintext| {+ bytes(+ &ok(oracle.call(json!({+ "op": "group_encrypt", "device": "bob", "group": GROUP,+ "peer": peer(&bob_key.sender), "plaintext": hex::encode(plaintext),+ }))),+ "ciphertext",+ )+ })+ .collect();+ for (index, expected) in [+ (1, "go group one"),+ (2, "go group two"),+ (0, "go group zero"),+ ] {+ assert_eq!(+ protocol+ .group_decrypt(alice, &bob_key, &received[index])+ .await+ .expect("Rust decrypts Go's group message"),+ expected.as_bytes()+ );+ }+ assert!(matches!(+ protocol.group_decrypt(alice, &bob_key, &received[0]).await,+ Err(SignalError::OldCounter)+ ));+ let mut forged = received[2].clone();+ let last = forged.len() - 1;+ forged[last] ^= 0x01;+ assert!(matches!(+ protocol.group_decrypt(alice, &bob_key, &forged).await,+ Err(SignalError::Internal(_))+ ));++ // Both sides rotate; a message under each old key is still in flight.+ let rust_in_flight = protocol+ .group_encrypt(alice, &alice_key, b"rust in flight")+ .await+ .expect("Rust encrypts under the old key");+ alice.delete_sender_key(&alice_key);+ let rotated = protocol+ .create_sender_key_distribution_message(alice, &alice_key)+ .await+ .expect("Rust creates a new sender key");+ assert_ne!(rotated, distribution);+ ok(oracle.call(json!({+ "op": "process_sender_key", "device": "bob", "group": GROUP,+ "peer": peer(&alice_key.sender), "message": hex::encode(&rotated),+ })));+ let rust_rotated = protocol+ .group_encrypt(alice, &alice_key, b"rust rotated")+ .await+ .expect("Rust encrypts under the new key");+ for (message, expected) in [+ (&rust_rotated, &b"rust rotated"[..]),+ (&rust_in_flight, b"rust in flight"),+ ] {+ let decrypted = ok(oracle.call(json!({+ "op": "group_decrypt", "device": "bob", "group": GROUP,+ "peer": peer(&alice_key.sender), "ciphertext": hex::encode(message),+ })));+ assert_eq!(bytes(&decrypted, "plaintext"), expected);+ }++ let go_in_flight = bytes(+ &ok(oracle.call(json!({+ "op": "group_encrypt", "device": "bob", "group": GROUP,+ "peer": peer(&bob_key.sender), "plaintext": hex::encode("go in flight"),+ }))),+ "ciphertext",+ );+ ok(oracle.call(json!({+ "op": "delete_sender_key", "device": "bob", "group": GROUP, "peer": peer(&bob_key.sender),+ })));+ let go_rotated_distribution = ok(oracle.call(json!({+ "op": "create_sender_key", "device": "bob", "group": GROUP, "peer": peer(&bob_key.sender),+ })));+ protocol+ .process_sender_key_distribution_message(+ alice,+ &bob_key,+ &bytes(&go_rotated_distribution, "message"),+ )+ .await+ .expect("Rust stores Go's new sender key");+ let go_rotated = bytes(+ &ok(oracle.call(json!({+ "op": "group_encrypt", "device": "bob", "group": GROUP,+ "peer": peer(&bob_key.sender), "plaintext": hex::encode("go rotated"),+ }))),+ "ciphertext",+ );+ assert_eq!(+ protocol+ .group_decrypt(alice, &bob_key, &go_rotated)+ .await+ .expect("Rust decrypts under Go's new key"),+ b"go rotated"+ );+ assert_eq!(+ protocol+ .group_decrypt(alice, &bob_key, &go_in_flight)+ .await+ .expect("Rust decrypts under Go's old key"),+ b"go in flight"+ );+}++async fn conversation(oracle: &mut dyn Oracle) {+ let protocol = Libsignal::seeded(PROTOCOL_SEED);+ let signatures = Libsignal::seeded(SIGNATURE_SEED);+ let mut generator = StdRng::seed_from_u64(KEY_SEED);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let carol = TestStore::new(&mut generator, &signatures, 3003, &[77]);++ xeddsa(oracle, &signatures).await;+ rust_initiates(oracle, &protocol, &alice).await;+ go_initiates(oracle, &protocol, &carol).await;+ groups(oracle, &protocol, &alice).await;+}++#[tokio::test]+async fn replays_the_recorded_go_conversation() {+ let transcript =+ std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(TRANSCRIPT))+ .expect("the transcript is committed");+ let mut oracle = ReplayOracle {+ exchanges: serde_json::from_str(&transcript).expect("the transcript parses"),+ };+ conversation(&mut oracle).await;+ assert!(+ oracle.exchanges.is_empty(),+ "the conversation used the whole transcript"+ );+}++#[tokio::test]+#[ignore = "needs Go; run locally (see testdata/generator/README.md)"]+async fn talks_to_go_libsignal() {+ let mut oracle = LiveOracle::start();+ conversation(&mut oracle).await;+ oracle.finish();+}++#[tokio::test]+#[ignore = "needs Go; rewrites testdata/interop_transcript.json"]+async fn record_go_transcript() {+ let mut oracle = LiveOracle::start();+ conversation(&mut oracle).await;+ let exchanges = oracle.finish();+ let mut transcript =+ serde_json::to_string_pretty(&exchanges).expect("the transcript serializes");+ transcript.push('\n');+ std::fs::write(+ PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(TRANSCRIPT),+ transcript,+ )+ .expect("the transcript is writable");+}diff --git a/crates/whatsmeow_signal/src/lib.rs b/crates/whatsmeow_signal/src/lib.rsnew file mode 100644index 0000000000..aafe77975c--- /dev/null+++ b/crates/whatsmeow_signal/src/lib.rs@@ -0,0 +1,267 @@+//! The Signal protocol for the whatsmeow port: [`Libsignal`] implements+//! [`whatsmeow::signal::CurveSignatures`] and+//! [`whatsmeow::signal::SignalProtocol`], so [`libsignal`] is a+//! [`whatsmeow::Signal`].+//!+//! Curve25519 keys and XEdDSA signatures come from Signal's `libsignal-core`+//! unmodified. The Double Ratchet, X3DH and sender keys are Signal's+//! `libsignal-protocol` modules, vendored and patched in [`protocol`] because+//! WhatsApp still speaks session version 3 (X3DH, no Kyber) and an older+//! sender key wire format, which current libsignal releases refuse. See+//! `AGENTS.md` and `NOTICE`.+//!+//! Session and sender key records cross the store boundary as libsignal's+//! `RecordStructure` and `SenderKeyRecordStructure` protobufs.+#![deny(missing_docs)]++#[cfg(test)]+mod interop_test;+mod protocol;+mod store;+#[cfg(test)]+mod test;+#[cfg(test)]+mod test_store;++use std::sync::{Arc, Mutex, PoisonError};++use async_trait::async_trait;+use libsignal_core::curve::{PrivateKey, PublicKey};+use rand::SeedableRng;+use rand::rngs::StdRng;+use whatsmeow::signal::{+ CiphertextMessage, CiphertextType, CurveSignatures, PreKeyBundle, SenderKeyName, SignalAddress,+ SignalError, SignalProtocol, SignalProtocolStore,+};++use crate::protocol::error::SignalProtocolError;+use crate::protocol::messages::{+ self, PreKeySignalMessage, SenderKeyDistributionMessage, SignalMessage,+};+use crate::protocol::{group_cipher, session, session_cipher};+use crate::store::ProtocolStore;++/// Where an operation's randomness comes from: the operating system, or (in+/// tests) a seeded generator so outputs can be replayed byte for byte.+#[derive(Default)]+struct RandomSource {+ seeded: Option<Mutex<StdRng>>,+}++impl RandomSource {+ /// A generator for one operation. Operations hold it across awaits, so it+ /// is an owned, `Send` CSPRNG rather than the thread-local one.+ fn generator(&self) -> StdRng {+ match &self.seeded {+ Some(seeded) => {+ StdRng::from_rng(&mut *seeded.lock().unwrap_or_else(PoisonError::into_inner))+ }+ None => StdRng::from_os_rng(),+ }+ }++ #[cfg(test)]+ fn seeded(seed: u64) -> Self {+ Self {+ seeded: Some(Mutex::new(StdRng::seed_from_u64(seed))),+ }+ }+}++/// libsignal for the whatsmeow port: XEdDSA signatures as libsignal's+/// `curve25519` module makes them (the Edwards sign bit in the top bit of the+/// last byte), and the protocol operations whatsmeow runs (X3DH session+/// setup, the Double Ratchet, sender keys for groups) in WhatsApp's wire+/// format. Implements both halves of [`whatsmeow::Signal`]; see+/// [`libsignal`] for the trait object a client takes.+#[derive(Default)]+pub struct Libsignal {+ random: RandomSource,+}++/// The older name of [`Libsignal`] as the protocol half.+pub type LibsignalProtocol = Libsignal;++/// The older name of [`Libsignal`] as the signatures half.+pub type LibsignalCurveSignatures = Libsignal;++/// [`Libsignal`] as the trait object [`whatsmeow::Client::new`] takes.+pub fn libsignal() -> Arc<dyn whatsmeow::Signal> {+ Arc::new(Libsignal::new())+}++impl Libsignal {+ /// Uses randomness from the operating system.+ pub fn new() -> Self {+ Self::default()+ }++ #[cfg(test)]+ fn seeded(seed: u64) -> Self {+ Self {+ random: RandomSource::seeded(seed),+ }+ }+}++impl CurveSignatures for Libsignal {+ fn calculate_signature(&self, private_key: &[u8; 32], message: &[u8]) -> [u8; 64] {+ let private_key = PrivateKey::deserialize(private_key)+ .expect("libsignal accepts every 32-byte Curve25519 private key");+ let signature = private_key+ .calculate_signature(message, &mut self.random.generator())+ .expect("libsignal signs with every Curve25519 private key");+ (*signature)+ .try_into()+ .expect("XEdDSA signatures are 64 bytes")+ }++ fn verify_signature(+ &self,+ public_key: &[u8; 32],+ message: &[u8],+ signature: &[u8; 64],+ ) -> bool {+ PublicKey::from_djb_public_key_bytes(public_key)+ .is_ok_and(|public_key| public_key.verify_signature(message, signature))+ }+}++fn signal_error(error: SignalProtocolError) -> SignalError {+ match error {+ SignalProtocolError::UntrustedIdentity => SignalError::UntrustedIdentity,+ SignalProtocolError::DuplicatedMessage(_, _) => SignalError::OldCounter,+ SignalProtocolError::NoSenderKeyState => SignalError::NoSenderKeyForUser,+ SignalProtocolError::Store(error) => SignalError::Store(error),+ error => SignalError::Internal(rootcause::Report::new(error).into_dynamic()),+ }+}++#[async_trait]+impl SignalProtocol for Libsignal {+ async fn process_bundle(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ bundle: &PreKeyBundle,+ ) -> Result<(), SignalError> {+ session::process_prekey_bundle(+ address,+ &ProtocolStore(store),+ bundle,+ &mut self.random.generator(),+ )+ .await+ .map_err(signal_error)+ }++ async fn encrypt(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ plaintext: &[u8],+ ) -> Result<CiphertextMessage, SignalError> {+ let message = session_cipher::message_encrypt(plaintext, address, &ProtocolStore(store))+ .await+ .map_err(signal_error)?;+ Ok(match message {+ messages::CiphertextMessage::SignalMessage(message) => CiphertextMessage {+ kind: CiphertextType::Whisper,+ serialized: message.serialized().to_vec(),+ },+ messages::CiphertextMessage::PreKeySignalMessage(message) => CiphertextMessage {+ kind: CiphertextType::PreKey,+ serialized: message.serialized().to_vec(),+ },+ })+ }++ async fn decrypt_pre_key_message(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ let message = PreKeySignalMessage::try_from(ciphertext).map_err(signal_error)?;+ session_cipher::message_decrypt_prekey(+ &message,+ address,+ &ProtocolStore(store),+ &mut self.random.generator(),+ )+ .await+ .map_err(signal_error)+ }++ async fn decrypt_message(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ let message = SignalMessage::try_from(ciphertext).map_err(signal_error)?;+ session_cipher::message_decrypt_signal(+ &message,+ address,+ &ProtocolStore(store),+ &mut self.random.generator(),+ )+ .await+ .map_err(signal_error)+ }++ async fn create_sender_key_distribution_message(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ ) -> Result<Vec<u8>, SignalError> {+ let message = group_cipher::create_sender_key_distribution_message(+ name,+ &ProtocolStore(store),+ &mut self.random.generator(),+ )+ .await+ .map_err(signal_error)?;+ Ok(message.serialized().to_vec())+ }++ async fn process_sender_key_distribution_message(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ message: &[u8],+ ) -> Result<(), SignalError> {+ let message = SenderKeyDistributionMessage::try_from(message).map_err(signal_error)?;+ group_cipher::process_sender_key_distribution_message(name, &message, &ProtocolStore(store))+ .await+ .map_err(signal_error)+ }++ async fn group_encrypt(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ plaintext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ let message = group_cipher::group_encrypt(+ &ProtocolStore(store),+ name,+ plaintext,+ &mut self.random.generator(),+ )+ .await+ .map_err(signal_error)?;+ Ok(message.serialized().to_vec())+ }++ async fn group_decrypt(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ group_cipher::group_decrypt(ciphertext, &ProtocolStore(store), name)+ .await+ .map_err(signal_error)+ }+}diff --git a/crates/whatsmeow_signal/src/protocol/consts.rs b/crates/whatsmeow_signal/src/protocol/consts.rsnew file mode 100644index 0000000000..b35c02553e--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/consts.rs@@ -0,0 +1,13 @@+//+// Copyright 2020 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/consts.rs and modified by+// Macro: the unacknowledged-session age limit is gone, as go.mau.fi/libsignal+// has none. See NOTICE.++pub(crate) const MAX_FORWARD_JUMPS: usize = 25_000;+pub(crate) const MAX_MESSAGE_KEYS: usize = 2000;+pub(crate) const MAX_RECEIVER_CHAINS: usize = 5;+pub(crate) const ARCHIVED_STATES_MAX_LENGTH: usize = 40;+pub(crate) const MAX_SENDER_KEY_STATES: usize = 5;diff --git a/crates/whatsmeow_signal/src/protocol/crypto.rs b/crates/whatsmeow_signal/src/protocol/crypto.rsnew file mode 100644index 0000000000..d9172bc071--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/crypto.rs@@ -0,0 +1,64 @@+//+// Copyright 2020 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/crypto.rs and+// rust/crypto/src/aes_cbc.rs, modified by Macro: AES-256-CBC comes from the+// RustCrypto `cbc` crate instead of libsignal's `signal-crypto`, and the+// AES-CTR helpers (sealed sender only) are gone. See NOTICE.++use aes::Aes256;+use aes::cipher::block_padding::Pkcs7;+use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit};+use hmac::{Hmac, Mac};+use sha2::Sha256;++#[derive(Debug)]+pub(crate) enum EncryptionError {+ /// The key or IV is the wrong length.+ BadKeyOrIv,+}++#[derive(Debug)]+pub(crate) enum DecryptionError {+ /// The key or IV is the wrong length.+ BadKeyOrIv,+ /// Either the input is malformed, or the padding is wrong.+ ///+ /// These cases should not be distinguished; message corruption can cause either problem.+ BadCiphertext(&'static str),+}++pub(crate) fn aes_256_cbc_encrypt(+ ptext: &[u8],+ key: &[u8],+ iv: &[u8],+) -> Result<Vec<u8>, EncryptionError> {+ let encryptor = cbc::Encryptor::<Aes256>::new_from_slices(key, iv)+ .map_err(|_| EncryptionError::BadKeyOrIv)?;+ Ok(encryptor.encrypt_padded_vec_mut::<Pkcs7>(ptext))+}++pub(crate) fn aes_256_cbc_decrypt(+ ctext: &[u8],+ key: &[u8],+ iv: &[u8],+) -> Result<Vec<u8>, DecryptionError> {+ if ctext.is_empty() || !ctext.len().is_multiple_of(16) {+ return Err(DecryptionError::BadCiphertext(+ "ciphertext length must be a non-zero multiple of 16",+ ));+ }+ let decryptor = cbc::Decryptor::<Aes256>::new_from_slices(key, iv)+ .map_err(|_| DecryptionError::BadKeyOrIv)?;+ decryptor+ .decrypt_padded_vec_mut::<Pkcs7>(ctext)+ .map_err(|_| DecryptionError::BadCiphertext("failed to decrypt"))+}++pub(crate) fn hmac_sha256(key: &[u8], input: &[u8]) -> [u8; 32] {+ let mut hmac =+ Hmac::<Sha256>::new_from_slice(key).expect("HMAC-SHA256 should accept any size key");+ hmac.update(input);+ hmac.finalize().into_bytes().into()+}diff --git a/crates/whatsmeow_signal/src/protocol/error.rs b/crates/whatsmeow_signal/src/protocol/error.rsnew file mode 100644index 0000000000..d790971b7e--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/error.rs@@ -0,0 +1,87 @@+//+// Copyright 2020-2021 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/error.rs and modified by+// Macro: variants for features WhatsApp doesn't use (Kyber, sealed sender,+// fingerprints, FFI) are gone, and addresses are kept out of the messages so+// errors can be logged without leaking phone numbers. See NOTICE.++use libsignal_core::curve::{CurveError, KeyType};+use whatsmeow::store::StoreError;++use super::messages::CiphertextMessageType;++pub(crate) type Result<T> = std::result::Result<T, SignalProtocolError>;++#[derive(Debug, thiserror::Error)]+pub(crate) enum SignalProtocolError {+ #[error("invalid argument: {0}")]+ InvalidArgument(String),+ #[error("invalid state for call to {0} to succeed: {1}")]+ InvalidState(&'static str, String),++ #[error("protobuf encoding was invalid")]+ InvalidProtobufEncoding,++ #[error("ciphertext serialized bytes were too short <{0}>")]+ CiphertextMessageTooShort(usize),+ #[error("ciphertext version was too old <{0}>")]+ LegacyCiphertextVersion(u8),+ #[error("ciphertext version was unrecognized <{0}>")]+ UnrecognizedCiphertextVersion(u8),+ #[error("unrecognized message version <{0}>")]+ UnrecognizedMessageVersion(u32),++ #[error("no key type identifier")]+ NoKeyTypeIdentifier,+ #[error("bad key type <{0:#04x}>")]+ BadKeyType(u8),+ #[error("bad key length <{1}> for key with type <{0}>")]+ BadKeyLength(KeyType, usize),+ #[error("invalid key agreement output (all-zero shared secret)")]+ InvalidKeyAgreement,++ #[error("invalid signature detected")]+ SignatureValidationFailed,++ #[error("untrusted identity")]+ UntrustedIdentity,++ #[error("invalid prekey identifier")]+ InvalidPreKeyId,+ #[error("invalid signed prekey identifier")]+ InvalidSignedPreKeyId,++ #[error("invalid MAC key length <{0}>")]+ InvalidMacKeyLength(usize),++ #[error("missing sender key state")]+ NoSenderKeyState,++ #[error("session not found")]+ SessionNotFound,+ #[error("invalid session: {0}")]+ InvalidSessionStructure(&'static str),+ #[error("invalid sender key session")]+ InvalidSenderKeySession,++ #[error("message with old counter {0} / {1}")]+ DuplicatedMessage(u32, u32),+ #[error("invalid {0:?} message: {1}")]+ InvalidMessage(CiphertextMessageType, &'static str),++ #[error(transparent)]+ Store(#[from] StoreError),+}++impl From<CurveError> for SignalProtocolError {+ fn from(error: CurveError) -> Self {+ match error {+ CurveError::NoKeyTypeIdentifier => Self::NoKeyTypeIdentifier,+ CurveError::BadKeyType(raw) => Self::BadKeyType(raw),+ CurveError::BadKeyLength(key_type, len) => Self::BadKeyLength(key_type, len),+ CurveError::InvalidKeyAgreement => Self::InvalidKeyAgreement,+ }+ }+}diff --git a/crates/whatsmeow_signal/src/protocol/group_cipher.rs b/crates/whatsmeow_signal/src/protocol/group_cipher.rsnew file mode 100644index 0000000000..7d91ad0674--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/group_cipher.rs@@ -0,0 +1,226 @@+//+// Copyright 2020-2021 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/group_cipher.rs and+// modified by Macro: sender keys are named by the whatsmeow port's+// `SenderKeyName` (group and sender) instead of a distribution UUID, states+// are found by WhatsApp's key ID, and the stores are the port's+// `SignalProtocolStore`. Creating a distribution message replaces an empty+// record, as go.mau.fi/libsignal does. See NOTICE.++use libsignal_core::curve::KeyPair;+use rand::{CryptoRng, Rng};+use whatsmeow::signal::SenderKeyName;++use super::consts;+use super::crypto;+use super::error::{Result, SignalProtocolError};+use super::messages::{+ CiphertextMessageType, SENDERKEY_MESSAGE_CURRENT_VERSION, SenderKeyDistributionMessage,+ SenderKeyMessage,+};+use super::sender_keys::{SenderKeyRecord, SenderKeyState, SenderMessageKey};+use crate::store::ProtocolStore;++pub(crate) async fn group_encrypt<R: Rng + CryptoRng>(+ store: &ProtocolStore<'_>,+ name: &SenderKeyName,+ plaintext: &[u8],+ csprng: &mut R,+) -> Result<SenderKeyMessage> {+ let mut record = store+ .load_sender_key(name)+ .await?+ .ok_or(SignalProtocolError::NoSenderKeyState)?;++ let sender_key_state = record+ .sender_key_state_mut()+ .map_err(|_| SignalProtocolError::InvalidSenderKeySession)?;++ let sender_chain_key = sender_key_state+ .sender_chain_key()+ .ok_or(SignalProtocolError::InvalidSenderKeySession)?;++ let message_keys = sender_chain_key.sender_message_key();++ let ciphertext =+ crypto::aes_256_cbc_encrypt(plaintext, message_keys.cipher_key(), message_keys.iv())+ .map_err(|_| SignalProtocolError::InvalidSenderKeySession)?;++ let signing_key = sender_key_state+ .signing_key_private()+ .map_err(|_| SignalProtocolError::InvalidSenderKeySession)?;++ let skm = SenderKeyMessage::new(+ SENDERKEY_MESSAGE_CURRENT_VERSION,+ sender_key_state.key_id(),+ message_keys.iteration(),+ ciphertext.into_boxed_slice(),+ csprng,+ &signing_key,+ )?;++ sender_key_state.set_sender_chain_key(sender_chain_key.next()?);++ store.store_sender_key(name, &record).await?;++ Ok(skm)+}++fn get_sender_key(state: &mut SenderKeyState, iteration: u32) -> Result<SenderMessageKey> {+ let sender_chain_key = state+ .sender_chain_key()+ .ok_or(SignalProtocolError::InvalidSenderKeySession)?;+ let current_iteration = sender_chain_key.iteration();++ if current_iteration > iteration {+ return match state.remove_sender_message_key(iteration) {+ Some(smk) => Ok(smk),+ None => Err(SignalProtocolError::DuplicatedMessage(+ current_iteration,+ iteration,+ )),+ };+ }++ let jump = (iteration - current_iteration) as usize;+ if jump > consts::MAX_FORWARD_JUMPS {+ return Err(SignalProtocolError::InvalidMessage(+ CiphertextMessageType::SenderKey,+ "message from too far into the future",+ ));+ }++ let mut sender_chain_key = sender_chain_key;++ while sender_chain_key.iteration() < iteration {+ state.add_sender_message_key(&sender_chain_key.sender_message_key());+ sender_chain_key = sender_chain_key.next()?;+ }++ state.set_sender_chain_key(sender_chain_key.next()?);+ Ok(sender_chain_key.sender_message_key())+}++pub(crate) async fn group_decrypt(+ skm_bytes: &[u8],+ store: &ProtocolStore<'_>,+ name: &SenderKeyName,+) -> Result<Vec<u8>> {+ let skm = SenderKeyMessage::try_from(skm_bytes)?;++ let mut record = store+ .load_sender_key(name)+ .await?+ .ok_or(SignalProtocolError::NoSenderKeyState)?;++ let Some(sender_key_state) = record.sender_key_state_for_key_id(skm.key_id()) else {+ tracing::warn!(+ key_id = skm.key_id(),+ "no sender key state for the message's key ID"+ );+ return Err(SignalProtocolError::NoSenderKeyState);+ };++ if skm.message_version() != SENDERKEY_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::UnrecognizedMessageVersion(+ skm.message_version() as u32,+ ));+ }++ let signing_key = sender_key_state+ .signing_key_public()+ .map_err(|_| SignalProtocolError::InvalidSenderKeySession)?;+ if !skm.verify_signature(&signing_key) {+ return Err(SignalProtocolError::SignatureValidationFailed);+ }++ let sender_key = get_sender_key(sender_key_state, skm.iteration())?;++ let plaintext = match crypto::aes_256_cbc_decrypt(+ skm.ciphertext(),+ sender_key.cipher_key(),+ sender_key.iv(),+ ) {+ Ok(plaintext) => plaintext,+ Err(crypto::DecryptionError::BadKeyOrIv) => {+ return Err(SignalProtocolError::InvalidSenderKeySession);+ }+ Err(crypto::DecryptionError::BadCiphertext(reason)) => {+ tracing::warn!(reason, "sender key decryption failed");+ return Err(SignalProtocolError::InvalidMessage(+ CiphertextMessageType::SenderKey,+ "decryption failed",+ ));+ }+ };++ store.store_sender_key(name, &record).await?;++ Ok(plaintext)+}++pub(crate) async fn process_sender_key_distribution_message(+ name: &SenderKeyName,+ skdm: &SenderKeyDistributionMessage,+ store: &ProtocolStore<'_>,+) -> Result<()> {+ let mut sender_key_record = store+ .load_sender_key(name)+ .await?+ .unwrap_or_else(SenderKeyRecord::new_empty);++ sender_key_record.add_sender_key_state(+ skdm.key_id(),+ skdm.iteration(),+ skdm.chain_key(),+ *skdm.signing_key(),+ None,+ );+ store.store_sender_key(name, &sender_key_record).await+}++pub(crate) async fn create_sender_key_distribution_message<R: Rng + CryptoRng>(+ name: &SenderKeyName,+ store: &ProtocolStore<'_>,+ csprng: &mut R,+) -> Result<SenderKeyDistributionMessage> {+ let sender_key_record = match store.load_sender_key(name).await? {+ Some(record) if record.sender_key_state().is_ok() => record,+ _ => {+ // libsignal-protocol-java uses 31-bit integers for sender key chain IDs+ let key_id = csprng.random::<u32>() >> 1;+ let iteration = 0;+ let sender_key: [u8; 32] = csprng.random();+ let signing_key = KeyPair::generate(csprng);+ let mut record = SenderKeyRecord::new_empty();+ record.add_sender_key_state(+ key_id,+ iteration,+ &sender_key,+ signing_key.public_key,+ Some(signing_key.private_key),+ );+ store.store_sender_key(name, &record).await?;+ record+ }+ };++ let state = sender_key_record+ .sender_key_state()+ .map_err(|_| SignalProtocolError::InvalidSenderKeySession)?;+ let sender_chain_key = state+ .sender_chain_key()+ .ok_or(SignalProtocolError::InvalidSenderKeySession)?;++ Ok(SenderKeyDistributionMessage::new(+ SENDERKEY_MESSAGE_CURRENT_VERSION,+ state.key_id(),+ sender_chain_key.iteration(),+ sender_chain_key.seed().to_vec(),+ state+ .signing_key_public()+ .map_err(|_| SignalProtocolError::InvalidSenderKeySession)?,+ ))+}diff --git a/crates/whatsmeow_signal/src/protocol/messages.rs b/crates/whatsmeow_signal/src/protocol/messages.rsnew file mode 100644index 0000000000..800679fb69--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/messages.rs@@ -0,0 +1,573 @@+//+// Copyright 2020-2021 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/protocol.rs and modified+// by Macro for WhatsApp's wire format (see NOTICE):+// - the version byte is `0x33` (version 3 in both nibbles), as WhatsApp and+// go.mau.fi/libsignal write it; libsignal writes its current version (4) in+// the low nibble, which breaks the MAC whatsmeow recomputes;+// - no Kyber payload and no post-quantum ratchet field;+// - sender key messages carry a 32-bit key ID instead of a distribution UUID+// and chain ID;+// - a pre-key ID of 0 means "no one-time pre-key", as in go.mau.fi/libsignal;+// - the decryption-error and plaintext content types are gone.++use hmac::{Hmac, Mac};+use libsignal_core::curve::{PrivateKey, PublicKey};+use prost::Message;+use rand::{CryptoRng, Rng};+use sha2::Sha256;+use subtle::ConstantTimeEq;++use super::error::{Result, SignalProtocolError};+use super::proto;++/// The only Double Ratchet message version WhatsApp speaks (X3DH, no Kyber).+pub(crate) const CIPHERTEXT_MESSAGE_CURRENT_VERSION: u8 = 3;+pub(crate) const SENDERKEY_MESSAGE_CURRENT_VERSION: u8 = 3;++fn version_byte(message_version: u8, current_version: u8) -> u8 {+ ((message_version & 0xF) << 4) | current_version+}++#[derive(Debug)]+pub(crate) enum CiphertextMessage {+ SignalMessage(SignalMessage),+ PreKeySignalMessage(PreKeySignalMessage),+}++#[derive(Copy, Clone, Eq, PartialEq, Debug)]+pub(crate) enum CiphertextMessageType {+ Whisper,+ PreKey,+ SenderKey,+}++#[derive(Debug, Clone)]+pub(crate) struct SignalMessage {+ message_version: u8,+ sender_ratchet_key: PublicKey,+ counter: u32,+ ciphertext: Box<[u8]>,+ serialized: Box<[u8]>,+}++impl SignalMessage {+ const MAC_LENGTH: usize = 8;++ #[expect(+ clippy::too_many_arguments,+ reason = "mirrors libsignal's constructor, which takes every field of the message"+ )]+ pub(crate) fn new(+ message_version: u8,+ mac_key: &[u8],+ sender_ratchet_key: PublicKey,+ counter: u32,+ previous_counter: u32,+ ciphertext: &[u8],+ sender_identity_key: &PublicKey,+ receiver_identity_key: &PublicKey,+ ) -> Result<Self> {+ let message = proto::wire::SignalMessage {+ ratchet_key: Some(sender_ratchet_key.serialize().into_vec()),+ counter: Some(counter),+ previous_counter: Some(previous_counter),+ ciphertext: Some(Vec::<u8>::from(ciphertext)),+ };+ let mut serialized = Vec::with_capacity(1 + message.encoded_len() + Self::MAC_LENGTH);+ serialized.push(version_byte(+ message_version,+ CIPHERTEXT_MESSAGE_CURRENT_VERSION,+ ));+ message+ .encode(&mut serialized)+ .expect("can always append to a buffer");+ let mac = Self::compute_mac(+ sender_identity_key,+ receiver_identity_key,+ mac_key,+ &serialized,+ )?;+ serialized.extend_from_slice(&mac);+ Ok(Self {+ message_version,+ sender_ratchet_key,+ counter,+ ciphertext: ciphertext.into(),+ serialized: serialized.into_boxed_slice(),+ })+ }++ pub(crate) fn message_version(&self) -> u8 {+ self.message_version+ }++ pub(crate) fn sender_ratchet_key(&self) -> &PublicKey {+ &self.sender_ratchet_key+ }++ pub(crate) fn counter(&self) -> u32 {+ self.counter+ }++ pub(crate) fn serialized(&self) -> &[u8] {+ &self.serialized+ }++ pub(crate) fn body(&self) -> &[u8] {+ &self.ciphertext+ }++ pub(crate) fn verify_mac(+ &self,+ sender_identity_key: &PublicKey,+ receiver_identity_key: &PublicKey,+ mac_key: &[u8],+ ) -> Result<bool> {+ let our_mac = &Self::compute_mac(+ sender_identity_key,+ receiver_identity_key,+ mac_key,+ &self.serialized[..self.serialized.len() - Self::MAC_LENGTH],+ )?;+ let their_mac = &self.serialized[self.serialized.len() - Self::MAC_LENGTH..];+ Ok(our_mac.ct_eq(their_mac).into())+ }++ fn compute_mac(+ sender_identity_key: &PublicKey,+ receiver_identity_key: &PublicKey,+ mac_key: &[u8],+ message: &[u8],+ ) -> Result<[u8; Self::MAC_LENGTH]> {+ if mac_key.len() != 32 {+ return Err(SignalProtocolError::InvalidMacKeyLength(mac_key.len()));+ }+ let mut mac = Hmac::<Sha256>::new_from_slice(mac_key)+ .expect("HMAC-SHA256 should accept any size key");++ mac.update(sender_identity_key.serialize().as_ref());+ mac.update(receiver_identity_key.serialize().as_ref());+ mac.update(message);+ let mut result = [0u8; Self::MAC_LENGTH];+ result.copy_from_slice(&mac.finalize().into_bytes()[..Self::MAC_LENGTH]);+ Ok(result)+ }+}++impl AsRef<[u8]> for SignalMessage {+ fn as_ref(&self) -> &[u8] {+ &self.serialized+ }+}++impl TryFrom<&[u8]> for SignalMessage {+ type Error = SignalProtocolError;++ fn try_from(value: &[u8]) -> Result<Self> {+ if value.len() < SignalMessage::MAC_LENGTH + 1 {+ return Err(SignalProtocolError::CiphertextMessageTooShort(value.len()));+ }+ let message_version = value[0] >> 4;+ if message_version < CIPHERTEXT_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::LegacyCiphertextVersion(+ message_version,+ ));+ }+ if message_version > CIPHERTEXT_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::UnrecognizedCiphertextVersion(+ message_version,+ ));+ }++ let proto_structure =+ proto::wire::SignalMessage::decode(&value[1..value.len() - SignalMessage::MAC_LENGTH])+ .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)?;++ let sender_ratchet_key = proto_structure+ .ratchet_key+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let sender_ratchet_key = PublicKey::deserialize(&sender_ratchet_key)?;+ let counter = proto_structure+ .counter+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let ciphertext = proto_structure+ .ciphertext+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?+ .into_boxed_slice();++ Ok(SignalMessage {+ message_version,+ sender_ratchet_key,+ counter,+ ciphertext,+ serialized: Box::from(value),+ })+ }+}++#[derive(Debug, Clone)]+pub(crate) struct PreKeySignalMessage {+ message_version: u8,+ registration_id: u32,+ pre_key_id: Option<u32>,+ signed_pre_key_id: u32,+ base_key: PublicKey,+ identity_key: PublicKey,+ message: SignalMessage,+ serialized: Box<[u8]>,+}++impl PreKeySignalMessage {+ pub(crate) fn new(+ message_version: u8,+ registration_id: u32,+ pre_key_id: Option<u32>,+ signed_pre_key_id: u32,+ base_key: PublicKey,+ identity_key: PublicKey,+ message: SignalMessage,+ ) -> Self {+ let proto_message = proto::wire::PreKeySignalMessage {+ registration_id: Some(registration_id),+ pre_key_id,+ signed_pre_key_id: Some(signed_pre_key_id),+ base_key: Some(base_key.serialize().into_vec()),+ identity_key: Some(identity_key.serialize().into_vec()),+ message: Some(Vec::from(message.as_ref())),+ };+ let mut serialized = Vec::with_capacity(1 + proto_message.encoded_len());+ serialized.push(version_byte(+ message_version,+ CIPHERTEXT_MESSAGE_CURRENT_VERSION,+ ));+ proto_message+ .encode(&mut serialized)+ .expect("can always append to a Vec");+ Self {+ message_version,+ registration_id,+ pre_key_id,+ signed_pre_key_id,+ base_key,+ identity_key,+ message,+ serialized: serialized.into_boxed_slice(),+ }+ }++ pub(crate) fn message_version(&self) -> u8 {+ self.message_version+ }++ pub(crate) fn registration_id(&self) -> u32 {+ self.registration_id+ }++ pub(crate) fn pre_key_id(&self) -> Option<u32> {+ self.pre_key_id+ }++ pub(crate) fn signed_pre_key_id(&self) -> u32 {+ self.signed_pre_key_id+ }++ pub(crate) fn base_key(&self) -> &PublicKey {+ &self.base_key+ }++ pub(crate) fn identity_key(&self) -> &PublicKey {+ &self.identity_key+ }++ pub(crate) fn message(&self) -> &SignalMessage {+ &self.message+ }++ pub(crate) fn serialized(&self) -> &[u8] {+ &self.serialized+ }+}++impl TryFrom<&[u8]> for PreKeySignalMessage {+ type Error = SignalProtocolError;++ fn try_from(value: &[u8]) -> Result<Self> {+ if value.is_empty() {+ return Err(SignalProtocolError::CiphertextMessageTooShort(value.len()));+ }++ let message_version = value[0] >> 4;+ if message_version < CIPHERTEXT_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::LegacyCiphertextVersion(+ message_version,+ ));+ }+ if message_version > CIPHERTEXT_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::UnrecognizedCiphertextVersion(+ message_version,+ ));+ }++ let proto_structure = proto::wire::PreKeySignalMessage::decode(&value[1..])+ .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)?;++ let base_key = proto_structure+ .base_key+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let identity_key = proto_structure+ .identity_key+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let message = proto_structure+ .message+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let signed_pre_key_id = proto_structure+ .signed_pre_key_id+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;++ Ok(PreKeySignalMessage {+ message_version,+ registration_id: proto_structure.registration_id.unwrap_or(0),+ pre_key_id: proto_structure.pre_key_id.filter(|id| *id != 0),+ signed_pre_key_id,+ base_key: PublicKey::deserialize(&base_key)?,+ identity_key: PublicKey::deserialize(&identity_key)?,+ message: SignalMessage::try_from(message.as_ref())?,+ serialized: Box::from(value),+ })+ }+}++#[derive(Debug, Clone)]+pub(crate) struct SenderKeyMessage {+ message_version: u8,+ key_id: u32,+ iteration: u32,+ ciphertext: Box<[u8]>,+ serialized: Box<[u8]>,+}++impl SenderKeyMessage {+ const SIGNATURE_LEN: usize = 64;++ pub(crate) fn new<R: CryptoRng + Rng>(+ message_version: u8,+ key_id: u32,+ iteration: u32,+ ciphertext: Box<[u8]>,+ csprng: &mut R,+ signature_key: &PrivateKey,+ ) -> Result<Self> {+ let proto_message = proto::wire::SenderKeyMessage {+ id: Some(key_id),+ iteration: Some(iteration),+ ciphertext: Some(ciphertext.to_vec()),+ };+ let proto_message_len = proto_message.encoded_len();+ let mut serialized = Vec::with_capacity(1 + proto_message_len + Self::SIGNATURE_LEN);+ serialized.push(version_byte(+ message_version,+ SENDERKEY_MESSAGE_CURRENT_VERSION,+ ));+ proto_message+ .encode(&mut serialized)+ .expect("can always append to a buffer");+ let signature = signature_key.calculate_signature(&serialized, csprng)?;+ serialized.extend_from_slice(&signature[..]);+ Ok(Self {+ message_version: SENDERKEY_MESSAGE_CURRENT_VERSION,+ key_id,+ iteration,+ ciphertext,+ serialized: serialized.into_boxed_slice(),+ })+ }++ pub(crate) fn verify_signature(&self, signature_key: &PublicKey) -> bool {+ signature_key.verify_signature(+ &self.serialized[..self.serialized.len() - Self::SIGNATURE_LEN],+ &self.serialized[self.serialized.len() - Self::SIGNATURE_LEN..],+ )+ }++ pub(crate) fn message_version(&self) -> u8 {+ self.message_version+ }++ pub(crate) fn key_id(&self) -> u32 {+ self.key_id+ }++ pub(crate) fn iteration(&self) -> u32 {+ self.iteration+ }++ pub(crate) fn ciphertext(&self) -> &[u8] {+ &self.ciphertext+ }++ pub(crate) fn serialized(&self) -> &[u8] {+ &self.serialized+ }+}++impl TryFrom<&[u8]> for SenderKeyMessage {+ type Error = SignalProtocolError;++ fn try_from(value: &[u8]) -> Result<Self> {+ if value.len() < 1 + Self::SIGNATURE_LEN {+ return Err(SignalProtocolError::CiphertextMessageTooShort(value.len()));+ }+ let message_version = value[0] >> 4;+ if message_version < SENDERKEY_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::LegacyCiphertextVersion(+ message_version,+ ));+ }+ if message_version > SENDERKEY_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::UnrecognizedCiphertextVersion(+ message_version,+ ));+ }+ let proto_structure =+ proto::wire::SenderKeyMessage::decode(&value[1..value.len() - Self::SIGNATURE_LEN])+ .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)?;++ let key_id = proto_structure+ .id+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let iteration = proto_structure+ .iteration+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let ciphertext = proto_structure+ .ciphertext+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?+ .into_boxed_slice();++ Ok(SenderKeyMessage {+ message_version,+ key_id,+ iteration,+ ciphertext,+ serialized: Box::from(value),+ })+ }+}++#[derive(Debug, Clone)]+pub(crate) struct SenderKeyDistributionMessage {+ key_id: u32,+ iteration: u32,+ chain_key: Vec<u8>,+ signing_key: PublicKey,+ serialized: Box<[u8]>,+}++impl SenderKeyDistributionMessage {+ pub(crate) fn new(+ message_version: u8,+ key_id: u32,+ iteration: u32,+ chain_key: Vec<u8>,+ signing_key: PublicKey,+ ) -> Self {+ let proto_message = proto::wire::SenderKeyDistributionMessage {+ id: Some(key_id),+ iteration: Some(iteration),+ chain_key: Some(chain_key.clone()),+ signing_key: Some(signing_key.serialize().to_vec()),+ };+ let mut serialized = Vec::with_capacity(1 + proto_message.encoded_len());+ serialized.push(version_byte(+ message_version,+ SENDERKEY_MESSAGE_CURRENT_VERSION,+ ));+ proto_message+ .encode(&mut serialized)+ .expect("can always append to a buffer");++ Self {+ key_id,+ iteration,+ chain_key,+ signing_key,+ serialized: serialized.into_boxed_slice(),+ }+ }++ pub(crate) fn key_id(&self) -> u32 {+ self.key_id+ }++ pub(crate) fn iteration(&self) -> u32 {+ self.iteration+ }++ pub(crate) fn chain_key(&self) -> &[u8] {+ &self.chain_key+ }++ pub(crate) fn signing_key(&self) -> &PublicKey {+ &self.signing_key+ }++ pub(crate) fn serialized(&self) -> &[u8] {+ &self.serialized+ }+}++impl TryFrom<&[u8]> for SenderKeyDistributionMessage {+ type Error = SignalProtocolError;++ fn try_from(value: &[u8]) -> Result<Self> {+ // The message contains at least a X25519 key and a chain key+ if value.len() < 1 + 32 + 32 {+ return Err(SignalProtocolError::CiphertextMessageTooShort(value.len()));+ }++ let message_version = value[0] >> 4;++ if message_version < SENDERKEY_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::LegacyCiphertextVersion(+ message_version,+ ));+ }+ if message_version > SENDERKEY_MESSAGE_CURRENT_VERSION {+ return Err(SignalProtocolError::UnrecognizedCiphertextVersion(+ message_version,+ ));+ }++ let proto_structure = proto::wire::SenderKeyDistributionMessage::decode(&value[1..])+ .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)?;++ let key_id = proto_structure+ .id+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let iteration = proto_structure+ .iteration+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let chain_key = proto_structure+ .chain_key+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;+ let signing_key = proto_structure+ .signing_key+ .ok_or(SignalProtocolError::InvalidProtobufEncoding)?;++ if chain_key.len() != 32 || signing_key.len() != 33 {+ return Err(SignalProtocolError::InvalidProtobufEncoding);+ }++ let signing_key = PublicKey::deserialize(&signing_key)?;++ Ok(SenderKeyDistributionMessage {+ key_id,+ iteration,+ chain_key,+ signing_key,+ serialized: Box::from(value),+ })+ }+}diff --git a/crates/whatsmeow_signal/src/protocol/mod.rs b/crates/whatsmeow_signal/src/protocol/mod.rsnew file mode 100644index 0000000000..6b132114bb--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/mod.rs@@ -0,0 +1,18 @@+// SPDX-License-Identifier: AGPL-3.0-only++//! Signal's libsignal protocol modules, vendored from libsignal v0.74.1 (the+//! last release that still speaks X3DH) and patched for WhatsApp's wire+//! format. Every file names its upstream source and what changed; `NOTICE`+//! lists the changes in one place.++mod consts;+pub(crate) mod crypto;+pub(crate) mod error;+pub(crate) mod group_cipher;+pub(crate) mod messages;+mod proto;+mod ratchet;+pub(crate) mod sender_keys;+pub(crate) mod session;+pub(crate) mod session_cipher;+pub(crate) mod session_state;diff --git a/crates/whatsmeow_signal/src/protocol/proto.rs b/crates/whatsmeow_signal/src/protocol/proto.rsnew file mode 100644index 0000000000..833d98e574--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/proto.rs@@ -0,0 +1,14 @@+//+// Copyright 2020 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/proto.rs and modified by+// Macro: only the wire and storage protos are kept. See NOTICE.++pub(crate) mod storage {+ include!(concat!(env!("OUT_DIR"), "/signal.proto.storage.rs"));+}++pub(crate) mod wire {+ include!(concat!(env!("OUT_DIR"), "/signal.proto.wire.rs"));+}diff --git a/crates/whatsmeow_signal/src/protocol/ratchet.rs b/crates/whatsmeow_signal/src/protocol/ratchet.rsnew file mode 100644index 0000000000..dd85faa0be--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/ratchet.rs@@ -0,0 +1,287 @@+//+// Copyright 2020 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/ratchet.rs,+// ratchet/keys.rs and ratchet/params.rs, modified by Macro: X3DH only (no+// Kyber, no post-quantum ratchet), and message keys are always derived and+// stored in full, never as seeds. See NOTICE.++use libsignal_core::curve::{KeyPair, PrivateKey, PublicKey};+use rand::{CryptoRng, Rng};++use super::crypto;+use super::error::Result;+use super::messages::CIPHERTEXT_MESSAGE_CURRENT_VERSION;+use super::session_state::SessionState;++/// The local and remote keys Alice (the side that processed a pre-key bundle)+/// starts a session from.+pub(crate) struct AliceSignalProtocolParameters {+ pub(crate) our_identity_key_pair: KeyPair,+ pub(crate) our_base_key_pair: KeyPair,+ pub(crate) their_identity_key: PublicKey,+ pub(crate) their_signed_pre_key: PublicKey,+ pub(crate) their_one_time_pre_key: Option<PublicKey>,+ pub(crate) their_ratchet_key: PublicKey,+}++/// The local and remote keys Bob (the side that received a pre-key message)+/// starts a session from.+pub(crate) struct BobSignalProtocolParameters {+ pub(crate) our_identity_key_pair: KeyPair,+ pub(crate) our_signed_pre_key_pair: KeyPair,+ pub(crate) our_one_time_pre_key_pair: Option<KeyPair>,+ pub(crate) our_ratchet_key_pair: KeyPair,+ pub(crate) their_identity_key: PublicKey,+ pub(crate) their_base_key: PublicKey,+}++fn derive_keys(secret_input: &[u8]) -> (RootKey, ChainKey) {+ let mut secrets = [0; 64];+ hkdf::Hkdf::<sha2::Sha256>::new(None, secret_input)+ .expand(b"WhisperText", &mut secrets)+ .expect("valid length");+ let (root_key_bytes, chain_key_bytes) = secrets.split_at(32);++ let root_key = RootKey::new(root_key_bytes.try_into().expect("correct length"));+ let chain_key = ChainKey::new(chain_key_bytes.try_into().expect("correct length"), 0);+ (root_key, chain_key)+}++pub(crate) fn initialize_alice_session<R: Rng + CryptoRng>(+ parameters: &AliceSignalProtocolParameters,+ csprng: &mut R,+) -> Result<SessionState> {+ let local_identity = parameters.our_identity_key_pair.public_key;++ let sending_ratchet_key = KeyPair::generate(csprng);++ let mut secrets = Vec::with_capacity(32 * 5);++ secrets.extend_from_slice(&[0xFFu8; 32]); // "discontinuity bytes"++ let our_base_private_key = parameters.our_base_key_pair.private_key;++ secrets.extend_from_slice(+ ¶meters+ .our_identity_key_pair+ .private_key+ .calculate_agreement(¶meters.their_signed_pre_key)?,+ );++ secrets.extend_from_slice(+ &our_base_private_key.calculate_agreement(¶meters.their_identity_key)?,+ );++ secrets.extend_from_slice(+ &our_base_private_key.calculate_agreement(¶meters.their_signed_pre_key)?,+ );++ if let Some(their_one_time_prekey) = ¶meters.their_one_time_pre_key {+ secrets+ .extend_from_slice(&our_base_private_key.calculate_agreement(their_one_time_prekey)?);+ }++ let (root_key, chain_key) = derive_keys(&secrets);++ let (sending_chain_root_key, sending_chain_chain_key) = root_key.create_chain(+ ¶meters.their_ratchet_key,+ &sending_ratchet_key.private_key,+ )?;++ let session = SessionState::new(+ CIPHERTEXT_MESSAGE_CURRENT_VERSION,+ &local_identity,+ ¶meters.their_identity_key,+ &sending_chain_root_key,+ ¶meters.our_base_key_pair.public_key,+ )+ .with_receiver_chain(¶meters.their_ratchet_key, &chain_key)+ .with_sender_chain(&sending_ratchet_key, &sending_chain_chain_key);++ Ok(session)+}++pub(crate) fn initialize_bob_session(+ parameters: &BobSignalProtocolParameters,+) -> Result<SessionState> {+ let local_identity = parameters.our_identity_key_pair.public_key;++ let mut secrets = Vec::with_capacity(32 * 5);++ secrets.extend_from_slice(&[0xFFu8; 32]); // "discontinuity bytes"++ secrets.extend_from_slice(+ ¶meters+ .our_signed_pre_key_pair+ .private_key+ .calculate_agreement(¶meters.their_identity_key)?,+ );++ secrets.extend_from_slice(+ ¶meters+ .our_identity_key_pair+ .private_key+ .calculate_agreement(¶meters.their_base_key)?,+ );++ secrets.extend_from_slice(+ ¶meters+ .our_signed_pre_key_pair+ .private_key+ .calculate_agreement(¶meters.their_base_key)?,+ );++ if let Some(our_one_time_pre_key_pair) = ¶meters.our_one_time_pre_key_pair {+ secrets.extend_from_slice(+ &our_one_time_pre_key_pair+ .private_key+ .calculate_agreement(¶meters.their_base_key)?,+ );+ }++ let (root_key, chain_key) = derive_keys(&secrets);++ let session = SessionState::new(+ CIPHERTEXT_MESSAGE_CURRENT_VERSION,+ &local_identity,+ ¶meters.their_identity_key,+ &root_key,+ ¶meters.their_base_key,+ )+ .with_sender_chain(¶meters.our_ratchet_key_pair, &chain_key);++ Ok(session)+}++#[derive(Clone, Copy)]+pub(crate) struct MessageKeys {+ cipher_key: [u8; 32],+ mac_key: [u8; 32],+ iv: [u8; 16],+ counter: u32,+}++impl MessageKeys {+ pub(crate) fn derive_keys(input_key_material: &[u8], counter: u32) -> Self {+ let mut okm = [0; 80];+ hkdf::Hkdf::<sha2::Sha256>::new(None, input_key_material)+ .expand(b"WhisperMessageKeys", &mut okm)+ .expect("valid output length");++ MessageKeys {+ cipher_key: okm[0..32].try_into().expect("correct length"),+ mac_key: okm[32..64].try_into().expect("correct length"),+ iv: okm[64..80].try_into().expect("correct length"),+ counter,+ }+ }++ pub(crate) fn new(cipher_key: [u8; 32], mac_key: [u8; 32], iv: [u8; 16], counter: u32) -> Self {+ Self {+ cipher_key,+ mac_key,+ iv,+ counter,+ }+ }++ pub(crate) fn cipher_key(&self) -> &[u8; 32] {+ &self.cipher_key+ }++ pub(crate) fn mac_key(&self) -> &[u8; 32] {+ &self.mac_key+ }++ pub(crate) fn iv(&self) -> &[u8; 16] {+ &self.iv+ }++ pub(crate) fn counter(&self) -> u32 {+ self.counter+ }+}++#[derive(Clone, Debug)]+pub(crate) struct ChainKey {+ key: [u8; 32],+ index: u32,+}++impl ChainKey {+ const MESSAGE_KEY_SEED: [u8; 1] = [0x01u8];+ const CHAIN_KEY_SEED: [u8; 1] = [0x02u8];++ pub(crate) fn new(key: [u8; 32], index: u32) -> Self {+ Self { key, index }+ }++ pub(crate) fn key(&self) -> &[u8; 32] {+ &self.key+ }++ pub(crate) fn index(&self) -> u32 {+ self.index+ }++ pub(crate) fn next_chain_key(&self) -> Self {+ Self {+ key: self.calculate_base_material(Self::CHAIN_KEY_SEED),+ index: self.index + 1,+ }+ }++ pub(crate) fn message_keys(&self) -> MessageKeys {+ MessageKeys::derive_keys(+ &self.calculate_base_material(Self::MESSAGE_KEY_SEED),+ self.index,+ )+ }++ fn calculate_base_material(&self, seed: [u8; 1]) -> [u8; 32] {+ crypto::hmac_sha256(&self.key, &seed)+ }+}++#[derive(Clone, Debug)]+pub(crate) struct RootKey {+ key: [u8; 32],+}++impl RootKey {+ pub(crate) fn new(key: [u8; 32]) -> Self {+ Self { key }+ }++ pub(crate) fn key(&self) -> &[u8; 32] {+ &self.key+ }++ pub(crate) fn create_chain(+ self,+ their_ratchet_key: &PublicKey,+ our_ratchet_key: &PrivateKey,+ ) -> Result<(RootKey, ChainKey)> {+ let shared_secret = our_ratchet_key.calculate_agreement(their_ratchet_key)?;+ let mut derived_secret_bytes = [0; 64];+ hkdf::Hkdf::<sha2::Sha256>::new(Some(&self.key), &shared_secret)+ .expand(b"WhisperRatchet", &mut derived_secret_bytes)+ .expect("valid output length");++ Ok((+ RootKey {+ key: derived_secret_bytes[0..32]+ .try_into()+ .expect("correct length"),+ },+ ChainKey {+ key: derived_secret_bytes[32..64]+ .try_into()+ .expect("correct length"),+ index: 0,+ },+ ))+ }+}diff --git a/crates/whatsmeow_signal/src/protocol/sender_keys.rs b/crates/whatsmeow_signal/src/protocol/sender_keys.rsnew file mode 100644index 0000000000..45401f612a--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/sender_keys.rs@@ -0,0 +1,331 @@+//+// Copyright 2020-2021 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/sender_keys.rs and+// modified by Macro: the state has no message version (WhatsApp sender keys+// are always version 3) and is looked up by WhatsApp's key ID. See NOTICE.++use std::collections::VecDeque;++use libsignal_core::curve::{PrivateKey, PublicKey};+use prost::Message;++use super::consts;+use super::crypto::hmac_sha256;+use super::error::SignalProtocolError;+use super::proto::storage as storage_proto;++/// A distinct error type to keep from accidentally propagating deserialization errors.+#[derive(Debug)]+pub(crate) struct InvalidSessionError(&'static str);++impl std::fmt::Display for InvalidSessionError {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ self.0.fmt(f)+ }+}++#[derive(Debug, Clone)]+pub(crate) struct SenderMessageKey {+ iteration: u32,+ iv: Vec<u8>,+ cipher_key: Vec<u8>,+ seed: Vec<u8>,+}++impl SenderMessageKey {+ pub(crate) fn new(iteration: u32, seed: Vec<u8>) -> Self {+ let mut derived = [0; 48];+ hkdf::Hkdf::<sha2::Sha256>::new(None, &seed)+ .expand(b"WhisperGroup", &mut derived)+ .expect("valid output length");+ Self {+ iteration,+ seed,+ iv: derived[0..16].to_vec(),+ cipher_key: derived[16..48].to_vec(),+ }+ }++ fn from_protobuf(smk: storage_proto::sender_key_state_structure::SenderMessageKey) -> Self {+ Self::new(smk.iteration, smk.seed)+ }++ pub(crate) fn iteration(&self) -> u32 {+ self.iteration+ }++ pub(crate) fn iv(&self) -> &[u8] {+ &self.iv+ }++ pub(crate) fn cipher_key(&self) -> &[u8] {+ &self.cipher_key+ }++ fn as_protobuf(&self) -> storage_proto::sender_key_state_structure::SenderMessageKey {+ storage_proto::sender_key_state_structure::SenderMessageKey {+ iteration: self.iteration,+ seed: self.seed.clone(),+ }+ }+}++#[derive(Debug, Clone)]+pub(crate) struct SenderChainKey {+ iteration: u32,+ chain_key: Vec<u8>,+}++impl SenderChainKey {+ const MESSAGE_KEY_SEED: u8 = 0x01;+ const CHAIN_KEY_SEED: u8 = 0x02;++ pub(crate) fn new(iteration: u32, chain_key: Vec<u8>) -> Self {+ Self {+ iteration,+ chain_key,+ }+ }++ pub(crate) fn iteration(&self) -> u32 {+ self.iteration+ }++ pub(crate) fn seed(&self) -> &[u8] {+ &self.chain_key+ }++ pub(crate) fn next(&self) -> Result<SenderChainKey, SignalProtocolError> {+ let new_iteration = self.iteration.checked_add(1).ok_or_else(|| {+ SignalProtocolError::InvalidState(+ "sender_chain_key_next",+ "Sender chain is too long".into(),+ )+ })?;++ Ok(SenderChainKey::new(+ new_iteration,+ self.get_derivative(Self::CHAIN_KEY_SEED),+ ))+ }++ pub(crate) fn sender_message_key(&self) -> SenderMessageKey {+ SenderMessageKey::new(self.iteration, self.get_derivative(Self::MESSAGE_KEY_SEED))+ }++ fn get_derivative(&self, label: u8) -> Vec<u8> {+ let label = [label];+ hmac_sha256(&self.chain_key, &label).to_vec()+ }++ fn as_protobuf(&self) -> storage_proto::sender_key_state_structure::SenderChainKey {+ storage_proto::sender_key_state_structure::SenderChainKey {+ iteration: self.iteration,+ seed: self.chain_key.clone(),+ }+ }+}++#[derive(Debug, Clone)]+pub(crate) struct SenderKeyState {+ state: storage_proto::SenderKeyStateStructure,+}++impl SenderKeyState {+ fn new(+ key_id: u32,+ iteration: u32,+ chain_key: &[u8],+ signature_key: PublicKey,+ signature_private_key: Option<PrivateKey>,+ ) -> SenderKeyState {+ let state = storage_proto::SenderKeyStateStructure {+ chain_id: key_id,+ sender_chain_key: Some(+ SenderChainKey::new(iteration, chain_key.to_vec()).as_protobuf(),+ ),+ sender_signing_key: Some(+ storage_proto::sender_key_state_structure::SenderSigningKey {+ public: signature_key.serialize().to_vec(),+ private: match signature_private_key {+ None => vec![],+ Some(k) => k.serialize().to_vec(),+ },+ },+ ),+ sender_message_keys: vec![],+ };++ Self { state }+ }++ pub(crate) fn key_id(&self) -> u32 {+ self.state.chain_id+ }++ pub(crate) fn sender_chain_key(&self) -> Option<SenderChainKey> {+ let sender_chain = self.state.sender_chain_key.as_ref()?;+ Some(SenderChainKey::new(+ sender_chain.iteration,+ sender_chain.seed.clone(),+ ))+ }++ pub(crate) fn set_sender_chain_key(&mut self, chain_key: SenderChainKey) {+ self.state.sender_chain_key = Some(chain_key.as_protobuf());+ }++ pub(crate) fn signing_key_public(&self) -> Result<PublicKey, InvalidSessionError> {+ if let Some(ref signing_key) = self.state.sender_signing_key {+ PublicKey::try_from(&signing_key.public[..])+ .map_err(|_| InvalidSessionError("invalid public signing key"))+ } else {+ Err(InvalidSessionError("missing signing key"))+ }+ }++ pub(crate) fn signing_key_private(&self) -> Result<PrivateKey, InvalidSessionError> {+ if let Some(ref signing_key) = self.state.sender_signing_key {+ PrivateKey::deserialize(&signing_key.private)+ .map_err(|_| InvalidSessionError("invalid private signing key"))+ } else {+ Err(InvalidSessionError("missing signing key"))+ }+ }++ pub(crate) fn add_sender_message_key(&mut self, sender_message_key: &SenderMessageKey) {+ self.state+ .sender_message_keys+ .push(sender_message_key.as_protobuf());+ while self.state.sender_message_keys.len() > consts::MAX_MESSAGE_KEYS {+ self.state.sender_message_keys.remove(0);+ }+ }++ pub(crate) fn remove_sender_message_key(&mut self, iteration: u32) -> Option<SenderMessageKey> {+ let index = self+ .state+ .sender_message_keys+ .iter()+ .position(|x| x.iteration == iteration)?;+ let smk = self.state.sender_message_keys.remove(index);+ Some(SenderMessageKey::from_protobuf(smk))+ }+}++#[derive(Debug, Clone)]+pub(crate) struct SenderKeyRecord {+ states: VecDeque<SenderKeyState>,+}++impl SenderKeyRecord {+ pub(crate) fn new_empty() -> Self {+ Self {+ states: VecDeque::with_capacity(consts::MAX_SENDER_KEY_STATES),+ }+ }++ pub(crate) fn deserialize(buf: &[u8]) -> Result<SenderKeyRecord, SignalProtocolError> {+ let skr = storage_proto::SenderKeyRecordStructure::decode(buf)+ .map_err(|_| SignalProtocolError::InvalidProtobufEncoding)?;++ Ok(Self {+ states: skr+ .sender_key_states+ .into_iter()+ .map(|state| SenderKeyState { state })+ .collect(),+ })+ }++ pub(crate) fn sender_key_state(&self) -> Result<&SenderKeyState, InvalidSessionError> {+ self.states+ .front()+ .ok_or(InvalidSessionError("empty sender key state"))+ }++ pub(crate) fn sender_key_state_mut(+ &mut self,+ ) -> Result<&mut SenderKeyState, InvalidSessionError> {+ self.states+ .front_mut()+ .ok_or(InvalidSessionError("empty sender key state"))+ }++ pub(crate) fn sender_key_state_for_key_id(+ &mut self,+ key_id: u32,+ ) -> Option<&mut SenderKeyState> {+ self.states+ .iter_mut()+ .find(|state| state.key_id() == key_id)+ }++ pub(crate) fn add_sender_key_state(+ &mut self,+ key_id: u32,+ iteration: u32,+ chain_key: &[u8],+ signature_key: PublicKey,+ signature_private_key: Option<PrivateKey>,+ ) {+ let existing_state = self.remove_state(key_id, signature_key);++ if self.remove_states_with_key_id(key_id) > 0 {+ tracing::warn!(+ key_id,+ "removed a matching key ID found with a different public key"+ );+ }++ let state = match existing_state {+ None => SenderKeyState::new(+ key_id,+ iteration,+ chain_key,+ signature_key,+ signature_private_key,+ ),+ Some(state) => state,+ };++ while self.states.len() >= consts::MAX_SENDER_KEY_STATES {+ self.states.pop_back();+ }++ self.states.push_front(state);+ }++ /// Remove the state with the matching `key_id` and `signature_key`.+ ///+ /// Skips any bad protobufs.+ fn remove_state(&mut self, key_id: u32, signature_key: PublicKey) -> Option<SenderKeyState> {+ let index = self.states.iter().position(|state| {+ state.key_id() == key_id && state.signing_key_public().ok() == Some(signature_key)+ })?;++ self.states.remove(index)+ }++ /// Returns the number of removed states.+ ///+ /// Skips any bad protobufs.+ fn remove_states_with_key_id(&mut self, key_id: u32) -> usize {+ let initial_length = self.states.len();+ self.states.retain(|state| state.key_id() != key_id);+ initial_length - self.states.len()+ }++ pub(crate) fn serialize(&self) -> Vec<u8> {+ storage_proto::SenderKeyRecordStructure {+ sender_key_states: self+ .states+ .iter()+ .map(|state| state.state.clone())+ .collect(),+ }+ .encode_to_vec()+ }+}diff --git a/crates/whatsmeow_signal/src/protocol/session.rs b/crates/whatsmeow_signal/src/protocol/session.rsnew file mode 100644index 0000000000..c2a21aa1e7--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/session.rs@@ -0,0 +1,139 @@+//+// Copyright 2020-2022 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/session.rs and modified+// by Macro: X3DH only (no Kyber pre-keys, no post-quantum ratchet), bundles+// come from the whatsmeow port's `PreKeyBundle`, and the stores are the port's+// `SignalProtocolStore`. See NOTICE.++use libsignal_core::curve::{KeyPair, PublicKey};+use rand::{CryptoRng, Rng};+use whatsmeow::signal::{PreKeyBundle, SignalAddress};++use super::error::{Result, SignalProtocolError};+use super::messages::PreKeySignalMessage;+use super::ratchet::{self, AliceSignalProtocolParameters, BobSignalProtocolParameters};+use super::session_state::SessionRecord;+use crate::store::ProtocolStore;++/// Sets up the session a pre-key message starts, if it isn't set up yet.+///+/// Returns the one-time pre-key the message used, which the caller removes+/// once the message decrypted. The caller also saves the sender's identity+/// then.+pub(crate) async fn process_prekey(+ message: &PreKeySignalMessage,+ remote_address: &SignalAddress,+ session_record: &mut SessionRecord,+ store: &ProtocolStore<'_>,+) -> Result<Option<u32>> {+ if !store+ .is_trusted_identity(remote_address, message.identity_key())+ .await?+ {+ return Err(SignalProtocolError::UntrustedIdentity);+ }++ if session_record.promote_matching_session(+ message.message_version() as u32,+ &message.base_key().serialize(),+ )? {+ // We've already set up a session for this message, we can exit early.+ return Ok(None);+ }++ let our_signed_pre_key_pair = store.signed_pre_key(message.signed_pre_key_id())?;++ let our_one_time_pre_key_pair = match message.pre_key_id() {+ Some(pre_key_id) => Some(store.pre_key(pre_key_id).await?),+ None => {+ tracing::warn!("processing PreKey message which had no one-time prekey");+ None+ }+ };++ let parameters = BobSignalProtocolParameters {+ our_identity_key_pair: store.identity_key_pair()?,+ our_signed_pre_key_pair,+ our_one_time_pre_key_pair,+ our_ratchet_key_pair: our_signed_pre_key_pair,+ their_identity_key: *message.identity_key(),+ their_base_key: *message.base_key(),+ };++ let mut new_session = ratchet::initialize_bob_session(¶meters)?;++ new_session.set_local_registration_id(store.local_registration_id());+ new_session.set_remote_registration_id(message.registration_id());++ session_record.promote_state(new_session);++ Ok(message.pre_key_id())+}++pub(crate) async fn process_prekey_bundle<R: Rng + CryptoRng>(+ remote_address: &SignalAddress,+ store: &ProtocolStore<'_>,+ bundle: &PreKeyBundle,+ csprng: &mut R,+) -> Result<()> {+ let their_identity_key = PublicKey::from_djb_public_key_bytes(&bundle.identity_key)?;++ if !store+ .is_trusted_identity(remote_address, &their_identity_key)+ .await?+ {+ return Err(SignalProtocolError::UntrustedIdentity);+ }++ let their_signed_prekey = PublicKey::from_djb_public_key_bytes(&bundle.signed_pre_key_public)?;+ if !their_identity_key.verify_signature(+ &their_signed_prekey.serialize(),+ &bundle.signed_pre_key_signature,+ ) {+ return Err(SignalProtocolError::SignatureValidationFailed);+ }++ let mut session_record = store+ .load_session(remote_address)+ .await?+ .unwrap_or_else(SessionRecord::new_fresh);++ let our_base_key_pair = KeyPair::generate(csprng);++ let their_one_time_pre_key = bundle+ .pre_key+ .map(|(id, public)| {+ Ok::<_, SignalProtocolError>((id, PublicKey::from_djb_public_key_bytes(&public)?))+ })+ .transpose()?;++ let parameters = AliceSignalProtocolParameters {+ our_identity_key_pair: store.identity_key_pair()?,+ our_base_key_pair,+ their_identity_key,+ their_signed_pre_key: their_signed_prekey,+ their_one_time_pre_key: their_one_time_pre_key.map(|(_, key)| key),+ their_ratchet_key: their_signed_prekey,+ };++ let mut session = ratchet::initialize_alice_session(¶meters, csprng)?;++ session.set_unacknowledged_pre_key_message(+ their_one_time_pre_key.map(|(id, _)| id),+ bundle.signed_pre_key_id,+ &our_base_key_pair.public_key,+ );++ session.set_local_registration_id(store.local_registration_id());+ session.set_remote_registration_id(bundle.registration_id);++ store+ .save_identity(remote_address, &their_identity_key)+ .await?;++ session_record.promote_state(session);++ store.store_session(remote_address, &session_record).await+}diff --git a/crates/whatsmeow_signal/src/protocol/session_cipher.rs b/crates/whatsmeow_signal/src/protocol/session_cipher.rsnew file mode 100644index 0000000000..67f98f7bdb--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/session_cipher.rs@@ -0,0 +1,388 @@+//+// Copyright 2020-2022 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/session_cipher.rs and+// modified by Macro: no post-quantum ratchet or Kyber payload, no expiry of+// unacknowledged sessions (go.mau.fi/libsignal has none), the stores are the+// port's `SignalProtocolStore`, and the failure logs leave out addresses and+// keys. See NOTICE.++use libsignal_core::curve::{KeyPair, PublicKey};+use rand::{CryptoRng, Rng};+use whatsmeow::signal::SignalAddress;++use super::consts::MAX_FORWARD_JUMPS;+use super::crypto;+use super::error::{Result, SignalProtocolError};+use super::messages::{+ CiphertextMessage, CiphertextMessageType, PreKeySignalMessage, SignalMessage,+};+use super::ratchet::{ChainKey, MessageKeys};+use super::session;+use super::session_state::{SessionRecord, SessionState};+use crate::store::ProtocolStore;++pub(crate) async fn message_encrypt(+ ptext: &[u8],+ remote_address: &SignalAddress,+ store: &ProtocolStore<'_>,+) -> Result<CiphertextMessage> {+ let mut session_record = store+ .load_session(remote_address)+ .await?+ .ok_or(SignalProtocolError::SessionNotFound)?;+ let session_state = session_record+ .session_state_mut()+ .ok_or(SignalProtocolError::SessionNotFound)?;++ let chain_key = session_state.get_sender_chain_key()?;+ let message_keys = chain_key.message_keys();++ let sender_ephemeral = session_state.sender_ratchet_key()?;+ let previous_counter = session_state.previous_counter();+ let session_version = session_state+ .session_version()+ .try_into()+ .map_err(|_| SignalProtocolError::InvalidSessionStructure("version does not fit in u8"))?;++ let local_identity_key = session_state.local_identity_key()?;+ let their_identity_key = session_state.remote_identity_key()?.ok_or_else(|| {+ SignalProtocolError::InvalidState("message_encrypt", "no remote identity key".to_owned())+ })?;++ let ctext = crypto::aes_256_cbc_encrypt(ptext, message_keys.cipher_key(), message_keys.iv())+ .map_err(|_| {+ SignalProtocolError::InvalidSessionStructure("invalid sender chain message keys")+ })?;++ let signal_message = SignalMessage::new(+ session_version,+ message_keys.mac_key(),+ sender_ephemeral,+ chain_key.index(),+ previous_counter,+ &ctext,+ &local_identity_key,+ &their_identity_key,+ )?;++ let message = match session_state.unacknowledged_pre_key_message_items()? {+ Some(items) => CiphertextMessage::PreKeySignalMessage(PreKeySignalMessage::new(+ session_version,+ session_state.local_registration_id(),+ items.pre_key_id,+ items.signed_pre_key_id,+ items.base_key,+ local_identity_key,+ signal_message,+ )),+ None => CiphertextMessage::SignalMessage(signal_message),+ };++ session_state.set_sender_chain_key(&chain_key.next_chain_key());++ // XXX why is this check after everything else?!!+ if !store+ .is_trusted_identity(remote_address, &their_identity_key)+ .await?+ {+ return Err(SignalProtocolError::UntrustedIdentity);+ }++ // XXX this could be combined with the above call to the identity store (in a new API)+ store+ .save_identity(remote_address, &their_identity_key)+ .await?;++ store.store_session(remote_address, &session_record).await?;+ Ok(message)+}++pub(crate) async fn message_decrypt_prekey<R: Rng + CryptoRng>(+ ciphertext: &PreKeySignalMessage,+ remote_address: &SignalAddress,+ store: &ProtocolStore<'_>,+ csprng: &mut R,+) -> Result<Vec<u8>> {+ let mut session_record = store+ .load_session(remote_address)+ .await?+ .unwrap_or_else(SessionRecord::new_fresh);++ let pre_key_used =+ session::process_prekey(ciphertext, remote_address, &mut session_record, store).await?;++ let ptext = decrypt_message_with_record(+ &mut session_record,+ ciphertext.message(),+ CiphertextMessageType::PreKey,+ csprng,+ )?;++ store+ .save_identity(remote_address, ciphertext.identity_key())+ .await?;++ store.store_session(remote_address, &session_record).await?;++ if let Some(pre_key_id) = pre_key_used {+ store.remove_pre_key(pre_key_id).await?;+ }++ Ok(ptext)+}++pub(crate) async fn message_decrypt_signal<R: Rng + CryptoRng>(+ ciphertext: &SignalMessage,+ remote_address: &SignalAddress,+ store: &ProtocolStore<'_>,+ csprng: &mut R,+) -> Result<Vec<u8>> {+ let mut session_record = store+ .load_session(remote_address)+ .await?+ .ok_or(SignalProtocolError::SessionNotFound)?;++ let ptext = decrypt_message_with_record(+ &mut session_record,+ ciphertext,+ CiphertextMessageType::Whisper,+ csprng,+ )?;++ // Why are we performing this check after decryption instead of before?+ let their_identity_key = session_record+ .session_state()+ .and_then(|state| state.remote_identity_key().ok().flatten())+ .ok_or(SignalProtocolError::InvalidSessionStructure(+ "successfully decrypted; must have a remote identity key",+ ))?;++ if !store+ .is_trusted_identity(remote_address, &their_identity_key)+ .await?+ {+ return Err(SignalProtocolError::UntrustedIdentity);+ }++ store+ .save_identity(remote_address, &their_identity_key)+ .await?;++ store.store_session(remote_address, &session_record).await?;++ Ok(ptext)+}++fn decrypt_message_with_record<R: Rng + CryptoRng>(+ record: &mut SessionRecord,+ ciphertext: &SignalMessage,+ original_message_type: CiphertextMessageType,+ csprng: &mut R,+) -> Result<Vec<u8>> {+ if let Some(current_state) = record.session_state() {+ let mut current_state = current_state.clone();+ let result = decrypt_message_with_state(+ &mut current_state,+ ciphertext,+ original_message_type,+ csprng,+ );++ match result {+ Ok(ptext) => {+ record.set_session_state(current_state); // update the state+ return Ok(ptext);+ }+ Err(SignalProtocolError::DuplicatedMessage(_, _)) => {+ return result;+ }+ Err(error) => {+ tracing::warn!(?error, message_type = ?original_message_type, "failed to decrypt with the current session");+ if original_message_type == CiphertextMessageType::PreKey {+ // A PreKey message creates a session and then decrypts a Whisper message+ // using that session. No need to check older sessions.+ return Err(SignalProtocolError::InvalidMessage(+ original_message_type,+ "decryption failed",+ ));+ }+ }+ }+ }++ // Try some old sessions:+ let mut updated_session = None;++ for (idx, previous) in record.previous_session_states().enumerate() {+ let mut previous = previous?;++ let result =+ decrypt_message_with_state(&mut previous, ciphertext, original_message_type, csprng);++ match result {+ Ok(ptext) => {+ updated_session = Some((ptext, idx, previous));+ break;+ }+ Err(SignalProtocolError::DuplicatedMessage(_, _)) => {+ return result;+ }+ Err(error) => {+ tracing::warn!(?error, message_type = ?original_message_type, previous_session = idx, "failed to decrypt with a previous session");+ }+ }+ }++ if let Some((ptext, idx, updated_session)) = updated_session {+ record.promote_old_session(idx, updated_session);+ Ok(ptext)+ } else {+ Err(SignalProtocolError::InvalidMessage(+ original_message_type,+ "decryption failed",+ ))+ }+}++fn decrypt_message_with_state<R: Rng + CryptoRng>(+ state: &mut SessionState,+ ciphertext: &SignalMessage,+ original_message_type: CiphertextMessageType,+ csprng: &mut R,+) -> Result<Vec<u8>> {+ // Check for a completely empty or invalid session state before we do anything else.+ let _ = state.root_key().map_err(|_| {+ SignalProtocolError::InvalidMessage(+ original_message_type,+ "No session available to decrypt",+ )+ })?;++ let ciphertext_version = ciphertext.message_version() as u32;+ if ciphertext_version != state.session_version() {+ return Err(SignalProtocolError::UnrecognizedMessageVersion(+ ciphertext_version,+ ));+ }++ let their_ephemeral = ciphertext.sender_ratchet_key();+ let counter = ciphertext.counter();+ let chain_key = get_or_create_chain_key(state, their_ephemeral, csprng)?;+ let message_keys = get_or_create_message_key(+ state,+ their_ephemeral,+ original_message_type,+ &chain_key,+ counter,+ )?;++ let their_identity_key =+ state+ .remote_identity_key()?+ .ok_or(SignalProtocolError::InvalidSessionStructure(+ "cannot decrypt without remote identity key",+ ))?;++ let mac_valid = ciphertext.verify_mac(+ &their_identity_key,+ &state.local_identity_key()?,+ message_keys.mac_key(),+ )?;++ if !mac_valid {+ return Err(SignalProtocolError::InvalidMessage(+ original_message_type,+ "MAC verification failed",+ ));+ }++ let ptext = match crypto::aes_256_cbc_decrypt(+ ciphertext.body(),+ message_keys.cipher_key(),+ message_keys.iv(),+ ) {+ Ok(ptext) => ptext,+ Err(crypto::DecryptionError::BadKeyOrIv) => {+ return Err(SignalProtocolError::InvalidSessionStructure(+ "invalid receiver chain message keys",+ ));+ }+ Err(crypto::DecryptionError::BadCiphertext(reason)) => {+ tracing::warn!(reason, "failed to decrypt 1:1 message");+ return Err(SignalProtocolError::InvalidMessage(+ original_message_type,+ "failed to decrypt",+ ));+ }+ };++ state.clear_unacknowledged_pre_key_message();++ Ok(ptext)+}++fn get_or_create_chain_key<R: Rng + CryptoRng>(+ state: &mut SessionState,+ their_ephemeral: &PublicKey,+ csprng: &mut R,+) -> Result<ChainKey> {+ if let Some(chain) = state.get_receiver_chain_key(their_ephemeral)? {+ return Ok(chain);+ }++ let root_key = state.root_key()?;+ let our_ephemeral = state.sender_ratchet_private_key()?;+ let receiver_chain = root_key.create_chain(their_ephemeral, &our_ephemeral)?;+ let our_new_ephemeral = KeyPair::generate(csprng);+ let sender_chain = receiver_chain+ .0+ .create_chain(their_ephemeral, &our_new_ephemeral.private_key)?;++ state.set_root_key(&sender_chain.0);+ state.add_receiver_chain(their_ephemeral, &receiver_chain.1);++ let current_index = state.get_sender_chain_key()?.index();+ let previous_index = current_index.saturating_sub(1);+ state.set_previous_counter(previous_index);+ state.set_sender_chain(&our_new_ephemeral, &sender_chain.1);++ Ok(receiver_chain.1)+}++fn get_or_create_message_key(+ state: &mut SessionState,+ their_ephemeral: &PublicKey,+ original_message_type: CiphertextMessageType,+ chain_key: &ChainKey,+ counter: u32,+) -> Result<MessageKeys> {+ let chain_index = chain_key.index();++ if chain_index > counter {+ return match state.get_message_keys(their_ephemeral, counter)? {+ Some(keys) => Ok(keys),+ None => Err(SignalProtocolError::DuplicatedMessage(chain_index, counter)),+ };+ }++ let jump = (counter - chain_index) as usize;++ if jump > MAX_FORWARD_JUMPS && !state.session_with_self()? {+ return Err(SignalProtocolError::InvalidMessage(+ original_message_type,+ "message from too far into the future",+ ));+ }++ let mut chain_key = chain_key.clone();++ while chain_key.index() < counter {+ state.set_message_keys(their_ephemeral, chain_key.message_keys())?;+ chain_key = chain_key.next_chain_key();+ }++ state.set_receiver_chain_key(their_ephemeral, &chain_key.next_chain_key())?;+ Ok(chain_key.message_keys())+}diff --git a/crates/whatsmeow_signal/src/protocol/session_state.rs b/crates/whatsmeow_signal/src/protocol/session_state.rsnew file mode 100644index 0000000000..61910770eb--- /dev/null+++ b/crates/whatsmeow_signal/src/protocol/session_state.rs@@ -0,0 +1,523 @@+//+// Copyright 2020-2022 Signal Messenger, LLC.+// SPDX-License-Identifier: AGPL-3.0-only+//+// Vendored from libsignal v0.74.1 rust/protocol/src/state/session.rs and+// modified by Macro: no Kyber or post-quantum ratchet state, no pre-key+// timestamp (go.mau.fi/libsignal never expires an unacknowledged session),+// identity keys are plain curve public keys, and the logging-only accessors+// are gone. See NOTICE.++use libsignal_core::curve::{KeyPair, PrivateKey, PublicKey};+use prost::Message;+use subtle::ConstantTimeEq;++use super::consts;+use super::error::SignalProtocolError;+use super::proto::storage::{RecordStructure, SessionStructure, session_structure};+use super::ratchet::{ChainKey, MessageKeys, RootKey};++/// A distinct error type to keep from accidentally propagating deserialization errors.+#[derive(Debug)]+pub(crate) struct InvalidSessionError(&'static str);++impl std::fmt::Display for InvalidSessionError {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ self.0.fmt(f)+ }+}++impl From<InvalidSessionError> for SignalProtocolError {+ fn from(e: InvalidSessionError) -> Self {+ Self::InvalidSessionStructure(e.0)+ }+}++#[derive(Debug, Clone)]+pub(crate) struct UnacknowledgedPreKeyMessageItems {+ pub(crate) pre_key_id: Option<u32>,+ pub(crate) signed_pre_key_id: u32,+ pub(crate) base_key: PublicKey,+}++#[derive(Clone, Debug)]+pub(crate) struct SessionState {+ session: SessionStructure,+}++impl SessionState {+ pub(crate) fn new(+ version: u8,+ our_identity: &PublicKey,+ their_identity: &PublicKey,+ root_key: &RootKey,+ alice_base_key: &PublicKey,+ ) -> Self {+ Self {+ session: SessionStructure {+ session_version: version as u32,+ local_identity_public: our_identity.serialize().into_vec(),+ remote_identity_public: their_identity.serialize().into_vec(),+ root_key: root_key.key().to_vec(),+ previous_counter: 0,+ sender_chain: None,+ receiver_chains: vec![],+ pending_pre_key: None,+ remote_registration_id: 0,+ local_registration_id: 0,+ alice_base_key: alice_base_key.serialize().into_vec(),+ },+ }+ }++ pub(crate) fn alice_base_key(&self) -> &[u8] {+ &self.session.alice_base_key+ }++ pub(crate) fn session_version(&self) -> u32 {+ match self.session.session_version {+ 0 => 2,+ v => v,+ }+ }++ pub(crate) fn remote_identity_key(&self) -> Result<Option<PublicKey>, InvalidSessionError> {+ match self.session.remote_identity_public.len() {+ 0 => Ok(None),+ _ => Ok(Some(+ PublicKey::deserialize(&self.session.remote_identity_public)+ .map_err(|_| InvalidSessionError("invalid remote identity key"))?,+ )),+ }+ }++ pub(crate) fn local_identity_key(&self) -> Result<PublicKey, InvalidSessionError> {+ PublicKey::deserialize(&self.session.local_identity_public)+ .map_err(|_| InvalidSessionError("invalid local identity key"))+ }++ pub(crate) fn session_with_self(&self) -> Result<bool, InvalidSessionError> {+ if let Some(remote_id) = self.remote_identity_key()? {+ return Ok(remote_id == self.local_identity_key()?);+ }++ // If remote ID is not set then we can't be sure but treat as non-self+ Ok(false)+ }++ pub(crate) fn previous_counter(&self) -> u32 {+ self.session.previous_counter+ }++ pub(crate) fn set_previous_counter(&mut self, ctr: u32) {+ self.session.previous_counter = ctr;+ }++ pub(crate) fn root_key(&self) -> Result<RootKey, InvalidSessionError> {+ let root_key_bytes = self.session.root_key[..]+ .try_into()+ .map_err(|_| InvalidSessionError("invalid root key"))?;+ Ok(RootKey::new(root_key_bytes))+ }++ pub(crate) fn set_root_key(&mut self, root_key: &RootKey) {+ self.session.root_key = root_key.key().to_vec();+ }++ pub(crate) fn sender_ratchet_key(&self) -> Result<PublicKey, InvalidSessionError> {+ match self.session.sender_chain {+ None => Err(InvalidSessionError("missing sender chain")),+ Some(ref c) => PublicKey::deserialize(&c.sender_ratchet_key)+ .map_err(|_| InvalidSessionError("invalid sender chain ratchet key")),+ }+ }++ pub(crate) fn sender_ratchet_private_key(&self) -> Result<PrivateKey, InvalidSessionError> {+ match self.session.sender_chain {+ None => Err(InvalidSessionError("missing sender chain")),+ Some(ref c) => PrivateKey::deserialize(&c.sender_ratchet_key_private)+ .map_err(|_| InvalidSessionError("invalid sender chain private ratchet key")),+ }+ }++ fn get_receiver_chain(+ &self,+ sender: &PublicKey,+ ) -> Result<Option<(session_structure::Chain, usize)>, InvalidSessionError> {+ for (idx, chain) in self.session.receiver_chains.iter().enumerate() {+ // If we compared bytes directly it would be faster, but may miss non-canonical points.+ // It's unclear if supporting such points is desirable.+ let chain_ratchet_key = PublicKey::deserialize(&chain.sender_ratchet_key)+ .map_err(|_| InvalidSessionError("invalid receiver chain ratchet key"))?;++ if &chain_ratchet_key == sender {+ return Ok(Some((chain.clone(), idx)));+ }+ }++ Ok(None)+ }++ pub(crate) fn get_receiver_chain_key(+ &self,+ sender: &PublicKey,+ ) -> Result<Option<ChainKey>, InvalidSessionError> {+ match self.get_receiver_chain(sender)? {+ None => Ok(None),+ Some((chain, _)) => match chain.chain_key {+ None => Err(InvalidSessionError("missing receiver chain key")),+ Some(c) => {+ let chain_key_bytes = c.key[..]+ .try_into()+ .map_err(|_| InvalidSessionError("invalid receiver chain key"))?;+ Ok(Some(ChainKey::new(chain_key_bytes, c.index)))+ }+ },+ }+ }++ pub(crate) fn add_receiver_chain(&mut self, sender: &PublicKey, chain_key: &ChainKey) {+ let chain_key = session_structure::chain::ChainKey {+ index: chain_key.index(),+ key: chain_key.key().to_vec(),+ };++ let chain = session_structure::Chain {+ sender_ratchet_key: sender.serialize().to_vec(),+ sender_ratchet_key_private: vec![],+ chain_key: Some(chain_key),+ message_keys: vec![],+ };++ self.session.receiver_chains.push(chain);++ if self.session.receiver_chains.len() > consts::MAX_RECEIVER_CHAINS {+ tracing::info!(+ chain_count = self.session.receiver_chains.len(),+ "trimming excessive receiver chain"+ );+ self.session.receiver_chains.remove(0);+ }+ }++ pub(crate) fn with_receiver_chain(mut self, sender: &PublicKey, chain_key: &ChainKey) -> Self {+ self.add_receiver_chain(sender, chain_key);+ self+ }++ pub(crate) fn set_sender_chain(&mut self, sender: &KeyPair, next_chain_key: &ChainKey) {+ let chain_key = session_structure::chain::ChainKey {+ index: next_chain_key.index(),+ key: next_chain_key.key().to_vec(),+ };++ let new_chain = session_structure::Chain {+ sender_ratchet_key: sender.public_key.serialize().to_vec(),+ sender_ratchet_key_private: sender.private_key.serialize().to_vec(),+ chain_key: Some(chain_key),+ message_keys: vec![],+ };++ self.session.sender_chain = Some(new_chain);+ }++ pub(crate) fn with_sender_chain(mut self, sender: &KeyPair, next_chain_key: &ChainKey) -> Self {+ self.set_sender_chain(sender, next_chain_key);+ self+ }++ pub(crate) fn get_sender_chain_key(&self) -> Result<ChainKey, InvalidSessionError> {+ let sender_chain = self+ .session+ .sender_chain+ .as_ref()+ .ok_or(InvalidSessionError("missing sender chain"))?;++ let chain_key = sender_chain+ .chain_key+ .as_ref()+ .ok_or(InvalidSessionError("missing sender chain key"))?;++ let chain_key_bytes = chain_key.key[..]+ .try_into()+ .map_err(|_| InvalidSessionError("invalid sender chain key"))?;++ Ok(ChainKey::new(chain_key_bytes, chain_key.index))+ }++ pub(crate) fn set_sender_chain_key(&mut self, next_chain_key: &ChainKey) {+ let chain_key = session_structure::chain::ChainKey {+ index: next_chain_key.index(),+ key: next_chain_key.key().to_vec(),+ };++ let new_chain = match self.session.sender_chain.take() {+ None => session_structure::Chain {+ sender_ratchet_key: vec![],+ sender_ratchet_key_private: vec![],+ chain_key: Some(chain_key),+ message_keys: vec![],+ },+ Some(mut c) => {+ c.chain_key = Some(chain_key);+ c+ }+ };++ self.session.sender_chain = Some(new_chain);+ }++ pub(crate) fn get_message_keys(+ &mut self,+ sender: &PublicKey,+ counter: u32,+ ) -> Result<Option<MessageKeys>, InvalidSessionError> {+ if let Some(mut chain_and_index) = self.get_receiver_chain(sender)? {+ let message_key_idx = chain_and_index+ .0+ .message_keys+ .iter()+ .position(|m| m.index == counter);++ if let Some(position) = message_key_idx {+ let message_key = chain_and_index.0.message_keys.remove(position);+ let keys = MessageKeys::new(+ message_key+ .cipher_key+ .as_slice()+ .try_into()+ .map_err(|_| InvalidSessionError("invalid message cipher key"))?,+ message_key+ .mac_key+ .as_slice()+ .try_into()+ .map_err(|_| InvalidSessionError("invalid message MAC key"))?,+ message_key+ .iv+ .as_slice()+ .try_into()+ .map_err(|_| InvalidSessionError("invalid message IV"))?,+ message_key.index,+ );++ // Update with message key removed+ self.session.receiver_chains[chain_and_index.1] = chain_and_index.0;+ return Ok(Some(keys));+ }+ }++ Ok(None)+ }++ pub(crate) fn set_message_keys(+ &mut self,+ sender: &PublicKey,+ message_keys: MessageKeys,+ ) -> Result<(), InvalidSessionError> {+ let (mut updated_chain, index) = self.get_receiver_chain(sender)?.ok_or(+ InvalidSessionError("missing receiver chain for message keys"),+ )?;+ updated_chain.message_keys.insert(+ 0,+ session_structure::chain::MessageKey {+ index: message_keys.counter(),+ cipher_key: message_keys.cipher_key().to_vec(),+ mac_key: message_keys.mac_key().to_vec(),+ iv: message_keys.iv().to_vec(),+ },+ );++ if updated_chain.message_keys.len() > consts::MAX_MESSAGE_KEYS {+ updated_chain.message_keys.pop();+ }++ self.session.receiver_chains[index] = updated_chain;++ Ok(())+ }++ pub(crate) fn set_receiver_chain_key(+ &mut self,+ sender: &PublicKey,+ chain_key: &ChainKey,+ ) -> Result<(), InvalidSessionError> {+ let (mut updated_chain, index) = self+ .get_receiver_chain(sender)?+ .ok_or(InvalidSessionError("missing receiver chain for chain key"))?;+ updated_chain.chain_key = Some(session_structure::chain::ChainKey {+ index: chain_key.index(),+ key: chain_key.key().to_vec(),+ });++ self.session.receiver_chains[index] = updated_chain;++ Ok(())+ }++ pub(crate) fn set_unacknowledged_pre_key_message(+ &mut self,+ pre_key_id: Option<u32>,+ signed_pre_key_id: u32,+ base_key: &PublicKey,+ ) {+ self.session.pending_pre_key = Some(session_structure::PendingPreKey {+ pre_key_id,+ signed_pre_key_id: signed_pre_key_id as i32,+ base_key: base_key.serialize().to_vec(),+ });+ }++ pub(crate) fn unacknowledged_pre_key_message_items(+ &self,+ ) -> Result<Option<UnacknowledgedPreKeyMessageItems>, InvalidSessionError> {+ match &self.session.pending_pre_key {+ Some(pending_pre_key) => Ok(Some(UnacknowledgedPreKeyMessageItems {+ pre_key_id: pending_pre_key.pre_key_id,+ signed_pre_key_id: pending_pre_key.signed_pre_key_id as u32,+ base_key: PublicKey::deserialize(&pending_pre_key.base_key)+ .map_err(|_| InvalidSessionError("invalid pending PreKey message base key"))?,+ })),+ None => Ok(None),+ }+ }++ pub(crate) fn clear_unacknowledged_pre_key_message(&mut self) {+ self.session.pending_pre_key = None;+ }++ pub(crate) fn set_remote_registration_id(&mut self, registration_id: u32) {+ self.session.remote_registration_id = registration_id;+ }++ pub(crate) fn set_local_registration_id(&mut self, registration_id: u32) {+ self.session.local_registration_id = registration_id;+ }++ pub(crate) fn local_registration_id(&self) -> u32 {+ self.session.local_registration_id+ }+}++#[derive(Clone, Debug)]+pub(crate) struct SessionRecord {+ current_session: Option<SessionState>,+ previous_sessions: Vec<Vec<u8>>,+}++impl SessionRecord {+ pub(crate) fn new_fresh() -> Self {+ Self {+ current_session: None,+ previous_sessions: Vec::new(),+ }+ }++ pub(crate) fn deserialize(bytes: &[u8]) -> Result<Self, SignalProtocolError> {+ let record = RecordStructure::decode(bytes)+ .map_err(|_| InvalidSessionError("failed to decode session record protobuf"))?;++ Ok(Self {+ current_session: record+ .current_session+ .map(|session| SessionState { session }),+ previous_sessions: record.previous_sessions,+ })+ }++ /// If there's a session with a matching version and `alice_base_key`, ensures that it is the+ /// current session, promoting if necessary.+ ///+ /// Returns `Ok(true)` if such a session was found, `Ok(false)` if not, and+ /// `Err(InvalidSessionError)` if an invalid session was found during the search (whether+ /// current or not).+ pub(crate) fn promote_matching_session(+ &mut self,+ version: u32,+ alice_base_key: &[u8],+ ) -> Result<bool, InvalidSessionError> {+ if let Some(current_session) = &self.current_session+ && current_session.session_version() == version+ && bool::from(alice_base_key.ct_eq(current_session.alice_base_key()))+ {+ return Ok(true);+ }++ let mut session_to_promote = None;+ for (i, previous) in self.previous_session_states().enumerate() {+ let previous = previous?;+ if previous.session_version() == version+ && bool::from(alice_base_key.ct_eq(previous.alice_base_key()))+ {+ session_to_promote = Some((i, previous));+ break;+ }+ }++ if let Some((i, state)) = session_to_promote {+ self.promote_old_session(i, state);+ return Ok(true);+ }++ Ok(false)+ }++ pub(crate) fn session_state(&self) -> Option<&SessionState> {+ self.current_session.as_ref()+ }++ pub(crate) fn session_state_mut(&mut self) -> Option<&mut SessionState> {+ self.current_session.as_mut()+ }++ pub(crate) fn set_session_state(&mut self, session: SessionState) {+ self.current_session = Some(session);+ }++ pub(crate) fn previous_session_states(+ &self,+ ) -> impl ExactSizeIterator<Item = Result<SessionState, InvalidSessionError>> + '_ {+ self.previous_sessions.iter().map(|bytes| {+ Ok(SessionState {+ session: SessionStructure::decode(&bytes[..]).map_err(|_| {+ InvalidSessionError("failed to decode previous session protobuf")+ })?,+ })+ })+ }++ pub(crate) fn promote_old_session(+ &mut self,+ old_session: usize,+ updated_session: SessionState,+ ) {+ self.previous_sessions.remove(old_session);+ self.promote_state(updated_session)+ }++ pub(crate) fn promote_state(&mut self, new_state: SessionState) {+ self.archive_current_state();+ self.current_session = Some(new_state);+ }++ fn archive_current_state(&mut self) {+ if let Some(mut current_session) = self.current_session.take() {+ if self.previous_sessions.len() >= consts::ARCHIVED_STATES_MAX_LENGTH {+ self.previous_sessions.pop();+ }+ current_session.clear_unacknowledged_pre_key_message();+ self.previous_sessions+ .insert(0, current_session.session.encode_to_vec());+ }+ }++ pub(crate) fn serialize(&self) -> Vec<u8> {+ RecordStructure {+ current_session: self+ .current_session+ .as_ref()+ .map(|state| state.session.clone()),+ previous_sessions: self.previous_sessions.clone(),+ }+ .encode_to_vec()+ }+}diff --git a/crates/whatsmeow_signal/src/store.rs b/crates/whatsmeow_signal/src/store.rsnew file mode 100644index 0000000000..361e7127f4--- /dev/null+++ b/crates/whatsmeow_signal/src/store.rs@@ -0,0 +1,116 @@+//! Typed access to the port's [`SignalProtocolStore`], which hands keys and+//! records across as raw bytes.++use libsignal_core::curve::{KeyPair, PrivateKey, PublicKey};+use whatsmeow::signal::{SenderKeyName, SignalAddress, SignalProtocolStore};++use crate::protocol::error::{Result, SignalProtocolError};+use crate::protocol::sender_keys::SenderKeyRecord;+use crate::protocol::session_state::SessionRecord;++/// Converts the raw (public, private) Curve25519 pair the store returns.+pub(crate) fn key_pair(public: &[u8; 32], private: &[u8; 32]) -> Result<KeyPair> {+ Ok(KeyPair::new(+ PublicKey::from_djb_public_key_bytes(public)?,+ PrivateKey::deserialize(private)?,+ ))+}++/// Converts a curve public key to the raw 32 bytes the store keeps.+pub(crate) fn raw_public_key(key: &PublicKey) -> Result<[u8; 32]> {+ key.public_key_bytes()+ .try_into()+ .map_err(|_| SignalProtocolError::InvalidArgument("public key is not 32 bytes".into()))+}++pub(crate) struct ProtocolStore<'a>(pub(crate) &'a dyn SignalProtocolStore);++impl ProtocolStore<'_> {+ pub(crate) fn identity_key_pair(&self) -> Result<KeyPair> {+ let (public, private) = self.0.get_identity_key_pair();+ key_pair(&public, &private)+ }++ pub(crate) fn local_registration_id(&self) -> u32 {+ self.0.get_local_registration_id()+ }++ pub(crate) async fn is_trusted_identity(+ &self,+ address: &SignalAddress,+ identity_key: &PublicKey,+ ) -> Result<bool> {+ Ok(self+ .0+ .is_trusted_identity(address, raw_public_key(identity_key)?)+ .await?)+ }++ pub(crate) async fn save_identity(+ &self,+ address: &SignalAddress,+ identity_key: &PublicKey,+ ) -> Result<()> {+ Ok(self+ .0+ .save_identity(address, raw_public_key(identity_key)?)+ .await?)+ }++ pub(crate) async fn load_session(+ &self,+ address: &SignalAddress,+ ) -> Result<Option<SessionRecord>> {+ match self.0.load_session(address).await? {+ Some(bytes) => Ok(Some(SessionRecord::deserialize(&bytes)?)),+ None => Ok(None),+ }+ }++ pub(crate) async fn store_session(+ &self,+ address: &SignalAddress,+ record: &SessionRecord,+ ) -> Result<()> {+ Ok(self.0.store_session(address, record.serialize()).await?)+ }++ pub(crate) async fn pre_key(&self, id: u32) -> Result<KeyPair> {+ let (public, private) = self+ .0+ .load_pre_key(id)+ .await?+ .ok_or(SignalProtocolError::InvalidPreKeyId)?;+ key_pair(&public, &private)+ }++ pub(crate) async fn remove_pre_key(&self, id: u32) -> Result<()> {+ Ok(self.0.remove_pre_key(id).await?)+ }++ pub(crate) fn signed_pre_key(&self, id: u32) -> Result<KeyPair> {+ let (public, private, _signature) = self+ .0+ .load_signed_pre_key(id)+ .ok_or(SignalProtocolError::InvalidSignedPreKeyId)?;+ key_pair(&public, &private)+ }++ pub(crate) async fn load_sender_key(+ &self,+ name: &SenderKeyName,+ ) -> Result<Option<SenderKeyRecord>> {+ match self.0.load_sender_key(name).await? {+ Some(bytes) => Ok(Some(SenderKeyRecord::deserialize(&bytes)?)),+ None => Ok(None),+ }+ }++ pub(crate) async fn store_sender_key(+ &self,+ name: &SenderKeyName,+ record: &SenderKeyRecord,+ ) -> Result<()> {+ Ok(self.0.store_sender_key(name, record.serialize()).await?)+ }+}diff --git a/crates/whatsmeow_signal/src/test.rs b/crates/whatsmeow_signal/src/test.rsnew file mode 100644index 0000000000..b7868ea7c7--- /dev/null+++ b/crates/whatsmeow_signal/src/test.rs@@ -0,0 +1,512 @@+use std::sync::Arc;++use rand::SeedableRng;+use rand::rngs::StdRng;+use whatsmeow::signal::{+ CiphertextType, CurveSignatures, PreKeyBundle, SenderKeyName, SignalAddress, SignalError,+ SignalProtocol, SignalProtocolStore,+};+use whatsmeow::store::MemoryStore;++use libsignal_core::curve::KeyPair;++use crate::Libsignal;+use crate::protocol::crypto;+use crate::protocol::messages::{SenderKeyDistributionMessage, SenderKeyMessage};+use crate::protocol::sender_keys::SenderChainKey;+use crate::test_store::TestStore;++#[tokio::test]+async fn one_to_one_conversation_survives_reordering_duplicates_and_skips() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(1);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[7]);+ let alice_address = SignalAddress::new("alice".to_owned(), 1);+ let bob_address = SignalAddress::new("bob".to_owned(), 2);++ protocol+ .process_bundle(&alice, &bob_address, &bob.bundle(2, Some(7)))+ .await+ .expect("alice starts a session from bob's bundle");+ let first = protocol+ .encrypt(&alice, &bob_address, b"first")+ .await+ .expect("alice encrypts");+ let second = protocol+ .encrypt(&alice, &bob_address, b"second")+ .await+ .expect("alice encrypts");+ assert_eq!(first.kind, CiphertextType::PreKey);+ assert_eq!(second.kind, CiphertextType::PreKey);+ assert_eq!(first.serialized[0], 0x33);++ // The second pre-key message arrives first; both set up the same session.+ assert_eq!(+ protocol+ .decrypt_pre_key_message(&bob, &alice_address, &second.serialized)+ .await+ .expect("bob decrypts the second message"),+ b"second"+ );+ assert!(!bob.has_pre_key(7), "a used one-time pre-key is removed");+ assert_eq!(+ protocol+ .decrypt_pre_key_message(&bob, &alice_address, &first.serialized)+ .await+ .expect("bob decrypts the first message late"),+ b"first"+ );+ assert!(matches!(+ protocol+ .decrypt_pre_key_message(&bob, &alice_address, &first.serialized)+ .await,+ Err(SignalError::OldCounter)+ ));++ let reply_one = protocol+ .encrypt(&bob, &alice_address, b"reply one")+ .await+ .expect("bob replies");+ let reply_two = protocol+ .encrypt(&bob, &alice_address, b"reply two")+ .await+ .expect("bob replies");+ let reply_three = protocol+ .encrypt(&bob, &alice_address, b"reply three")+ .await+ .expect("bob replies");+ assert_eq!(reply_one.kind, CiphertextType::Whisper);+ assert_eq!(reply_one.serialized[0], 0x33);++ // Skip reply two, then go back for it.+ assert_eq!(+ protocol+ .decrypt_message(&alice, &bob_address, &reply_three.serialized)+ .await+ .expect("alice decrypts reply three"),+ b"reply three"+ );+ assert_eq!(+ protocol+ .decrypt_message(&alice, &bob_address, &reply_one.serialized)+ .await+ .expect("alice decrypts reply one"),+ b"reply one"+ );+ assert_eq!(+ protocol+ .decrypt_message(&alice, &bob_address, &reply_two.serialized)+ .await+ .expect("alice decrypts the skipped reply two"),+ b"reply two"+ );+ assert!(matches!(+ protocol+ .decrypt_message(&alice, &bob_address, &reply_two.serialized)+ .await,+ Err(SignalError::OldCounter)+ ));++ // Bob's reply acknowledged the session, so alice now sends plain messages.+ let third = protocol+ .encrypt(&alice, &bob_address, b"third")+ .await+ .expect("alice encrypts");+ assert_eq!(third.kind, CiphertextType::Whisper);+ assert_eq!(+ protocol+ .decrypt_message(&bob, &alice_address, &third.serialized)+ .await+ .expect("bob decrypts"),+ b"third"+ );+}++#[tokio::test]+async fn session_without_a_one_time_pre_key() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(2);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[]);+ let alice_address = SignalAddress::new("alice".to_owned(), 1);+ let bob_address = SignalAddress::new("bob".to_owned(), 2);++ protocol+ .process_bundle(&alice, &bob_address, &bob.bundle(2, None))+ .await+ .expect("alice starts a session from a bundle without a one-time pre-key");+ let message = protocol+ .encrypt(&alice, &bob_address, b"hello")+ .await+ .expect("alice encrypts");+ assert_eq!(+ protocol+ .decrypt_pre_key_message(&bob, &alice_address, &message.serialized)+ .await+ .expect("bob decrypts"),+ b"hello"+ );+}++#[tokio::test]+async fn bad_signed_pre_key_signature_rejects_the_bundle() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(3);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[]);+ let bob_address = SignalAddress::new("bob".to_owned(), 2);+ let mut bundle = bob.bundle(2, None);+ bundle.signed_pre_key_signature[10] ^= 1;++ assert!(matches!(+ protocol.process_bundle(&alice, &bob_address, &bundle).await,+ Err(SignalError::Internal(_))+ ));+ assert!(alice.session(&bob_address).is_none());+}++#[tokio::test]+async fn bad_mac_is_rejected_and_the_session_survives() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(4);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[]);+ let alice_address = SignalAddress::new("alice".to_owned(), 1);+ let bob_address = SignalAddress::new("bob".to_owned(), 2);+ protocol+ .process_bundle(&alice, &bob_address, &bob.bundle(2, None))+ .await+ .expect("alice starts a session");+ let hello = protocol+ .encrypt(&alice, &bob_address, b"hello")+ .await+ .expect("alice encrypts");+ protocol+ .decrypt_pre_key_message(&bob, &alice_address, &hello.serialized)+ .await+ .expect("bob decrypts");+ let reply = protocol+ .encrypt(&bob, &alice_address, b"reply")+ .await+ .expect("bob replies");++ let mut bad_mac = reply.serialized.clone();+ let last = bad_mac.len() - 1;+ bad_mac[last] ^= 0x01;+ let mut bad_body = reply.serialized.clone();+ bad_body[last - 10] ^= 0x01;+ let record_before = alice.session(&bob_address);+ for forged in [bad_mac, bad_body] {+ assert!(matches!(+ protocol+ .decrypt_message(&alice, &bob_address, &forged)+ .await,+ Err(SignalError::Internal(_))+ ));+ }+ assert_eq!(alice.session(&bob_address), record_before);++ assert_eq!(+ protocol+ .decrypt_message(&alice, &bob_address, &reply.serialized)+ .await+ .expect("the untouched message still decrypts"),+ b"reply"+ );+}++#[tokio::test]+async fn changed_identity_is_untrusted() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(5);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[]);+ let reinstalled_bob = TestStore::new(&mut generator, &signatures, 2003, &[]);+ let bob_address = SignalAddress::new("bob".to_owned(), 2);++ protocol+ .process_bundle(&alice, &bob_address, &bob.bundle(2, None))+ .await+ .expect("alice trusts bob's first identity");+ assert!(matches!(+ protocol+ .process_bundle(&alice, &bob_address, &reinstalled_bob.bundle(2, None))+ .await,+ Err(SignalError::UntrustedIdentity)+ ));+}++#[tokio::test]+async fn group_messages_survive_reordering_and_rotation() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(6);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[]);+ let alice_key = SenderKeyName {+ group_id: "[email protected]".to_owned(),+ sender: SignalAddress::new("alice".to_owned(), 1),+ };++ let distribution = protocol+ .create_sender_key_distribution_message(&alice, &alice_key)+ .await+ .expect("alice creates her sender key");+ assert_eq!(distribution[0], 0x33);+ assert_eq!(+ protocol+ .create_sender_key_distribution_message(&alice, &alice_key)+ .await+ .expect("alice reuses her sender key"),+ distribution+ );+ protocol+ .process_sender_key_distribution_message(&bob, &alice_key, &distribution)+ .await+ .expect("bob stores alice's sender key");++ let mut sent = Vec::new();+ for plaintext in [&b"zero"[..], b"one", b"two", b"three"] {+ sent.push(+ protocol+ .group_encrypt(&alice, &alice_key, plaintext)+ .await+ .expect("alice encrypts for the group"),+ );+ }+ assert_eq!(sent[0][0], 0x33);+ for (index, expected) in [(2, &b"two"[..]), (0, b"zero"), (3, b"three"), (1, b"one")] {+ assert_eq!(+ protocol+ .group_decrypt(&bob, &alice_key, &sent[index])+ .await+ .expect("bob decrypts out of order"),+ expected+ );+ }+ assert!(matches!(+ protocol.group_decrypt(&bob, &alice_key, &sent[2]).await,+ Err(SignalError::OldCounter)+ ));++ // Rotate: alice forgets her sender key and distributes a new one. A+ // message under the old key that was still in flight decrypts too.+ let in_flight = protocol+ .group_encrypt(&alice, &alice_key, b"in flight")+ .await+ .expect("alice encrypts under the old key");+ alice.delete_sender_key(&alice_key);+ let rotated = protocol+ .create_sender_key_distribution_message(&alice, &alice_key)+ .await+ .expect("alice creates a new sender key");+ assert_ne!(rotated, distribution);+ protocol+ .process_sender_key_distribution_message(&bob, &alice_key, &rotated)+ .await+ .expect("bob stores the new sender key");+ let after_rotation = protocol+ .group_encrypt(&alice, &alice_key, b"after rotation")+ .await+ .expect("alice encrypts under the new key");+ assert_eq!(+ protocol+ .group_decrypt(&bob, &alice_key, &after_rotation)+ .await+ .expect("bob decrypts under the new key"),+ b"after rotation"+ );+ assert_eq!(+ protocol+ .group_decrypt(&bob, &alice_key, &in_flight)+ .await+ .expect("bob still decrypts under the old key"),+ b"in flight"+ );+}++#[tokio::test]+async fn forged_group_messages_are_rejected() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(7);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[]);+ let alice_key = SenderKeyName {+ group_id: "[email protected]".to_owned(),+ sender: SignalAddress::new("alice".to_owned(), 1),+ };+ let distribution = protocol+ .create_sender_key_distribution_message(&alice, &alice_key)+ .await+ .expect("alice creates her sender key");+ protocol+ .process_sender_key_distribution_message(&bob, &alice_key, &distribution)+ .await+ .expect("bob stores alice's sender key");+ let message = protocol+ .group_encrypt(&alice, &alice_key, b"hello group")+ .await+ .expect("alice encrypts");++ // Mallory knows alice's chain key (she was in the group) but not her+ // signing key: she encrypts correctly under alice's key ID and signs+ // with her own key.+ let parsed = SenderKeyDistributionMessage::try_from(&distribution[..])+ .expect("alice's distribution message parses");+ let message_key =+ SenderChainKey::new(parsed.iteration(), parsed.chain_key().to_vec()).sender_message_key();+ let mallory_signing_key = KeyPair::generate(&mut generator);+ let signed_by_mallory = SenderKeyMessage::new(+ 3,+ parsed.key_id(),+ message_key.iteration(),+ crypto::aes_256_cbc_encrypt(b"forged", message_key.cipher_key(), message_key.iv())+ .expect("valid key and IV")+ .into_boxed_slice(),+ &mut generator,+ &mallory_signing_key.private_key,+ )+ .expect("mallory signs");+ let mut bad_signature = message.clone();+ let last = bad_signature.len() - 1;+ bad_signature[last] ^= 0x01;+ let mut bad_body = message.clone();+ bad_body[10] ^= 0x01;++ for forged in [+ signed_by_mallory.serialized().to_vec(),+ bad_signature,+ bad_body,+ ] {+ assert!(matches!(+ protocol.group_decrypt(&bob, &alice_key, &forged).await,+ Err(SignalError::Internal(_))+ ));+ }+ assert_eq!(+ protocol+ .group_decrypt(&bob, &alice_key, &message)+ .await+ .expect("the untouched message still decrypts"),+ b"hello group"+ );+}++#[tokio::test]+async fn group_message_without_a_sender_key() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(8);+ let alice = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let bob = TestStore::new(&mut generator, &signatures, 2002, &[]);+ let alice_key = SenderKeyName {+ group_id: "[email protected]".to_owned(),+ sender: SignalAddress::new("alice".to_owned(), 1),+ };+ protocol+ .create_sender_key_distribution_message(&alice, &alice_key)+ .await+ .expect("alice creates her sender key");+ let message = protocol+ .group_encrypt(&alice, &alice_key, b"hello group")+ .await+ .expect("alice encrypts");++ assert!(matches!(+ protocol.group_decrypt(&bob, &alice_key, &message).await,+ Err(SignalError::NoSenderKeyForUser)+ ));+}++#[test]+fn signatures_verify_only_the_signed_message() {+ let signatures = Libsignal::new();+ let mut generator = StdRng::seed_from_u64(9);+ let store = TestStore::new(&mut generator, &signatures, 1001, &[]);+ let (public, private) = store.get_identity_key_pair();++ let signature = signatures.calculate_signature(&private, b"message");+ assert!(signatures.verify_signature(&public, b"message", &signature));+ assert!(!signatures.verify_signature(&public, b"massage", &signature));+ let mut flipped = signature;+ flipped[63] ^= 0x80;+ assert!(!signatures.verify_signature(&public, b"message", &flipped));+}++/// The real store path: two devices of the port's in-memory store.+#[tokio::test]+async fn whatsmeow_memory_store_devices_talk() {+ let protocol = Libsignal::new();+ let signatures = Libsignal::new();+ let alice_device = Arc::new(MemoryStore::default()).new_device(&signatures);+ let bob_device = Arc::new(MemoryStore::default()).new_device(&signatures);+ let alice = alice_device.signal_store(None);+ let bob = bob_device.signal_store(None);+ let alice_address = SignalAddress::new("alice".to_owned(), 1);+ let bob_address = SignalAddress::new("bob".to_owned(), 2);+ let one_time = bob_device+ .pre_keys+ .gen_one_pre_key()+ .await+ .expect("bob generates a pre-key");+ let bundle = PreKeyBundle {+ registration_id: bob_device.registration_id,+ device_id: 2,+ pre_key: Some((one_time.key_id, one_time.key_pair.public)),+ signed_pre_key_id: bob_device.signed_pre_key.key_id,+ signed_pre_key_public: bob_device.signed_pre_key.key_pair.public,+ signed_pre_key_signature: bob_device+ .signed_pre_key+ .signature+ .expect("the signed pre-key is signed"),+ identity_key: bob_device.identity_key.public,+ };++ protocol+ .process_bundle(&alice, &bob_address, &bundle)+ .await+ .expect("alice starts a session");+ assert!(+ alice+ .contains_session(&bob_address)+ .await+ .expect("the store answers")+ );+ let hello = protocol+ .encrypt(&alice, &bob_address, b"hello")+ .await+ .expect("alice encrypts");+ assert_eq!(+ protocol+ .decrypt_pre_key_message(&bob, &alice_address, &hello.serialized)+ .await+ .expect("bob decrypts"),+ b"hello"+ );+ assert!(+ bob_device+ .pre_keys+ .get_pre_key(one_time.key_id)+ .await+ .expect("the store answers")+ .is_none()+ );+ let reply = protocol+ .encrypt(&bob, &alice_address, b"reply")+ .await+ .expect("bob replies");+ assert_eq!(+ protocol+ .decrypt_message(&alice, &bob_address, &reply.serialized)+ .await+ .expect("alice decrypts"),+ b"reply"+ );+}diff --git a/crates/whatsmeow_signal/src/test_store.rs b/crates/whatsmeow_signal/src/test_store.rsnew file mode 100644index 0000000000..93d05c7fdd--- /dev/null+++ b/crates/whatsmeow_signal/src/test_store.rs@@ -0,0 +1,200 @@+//! An in-memory [`SignalProtocolStore`] whose keys come from a caller's+//! generator, so tests can replay a conversation byte for byte.++use std::collections::HashMap;+use std::sync::{Mutex, MutexGuard, PoisonError};++use async_trait::async_trait;+use libsignal_core::curve::KeyPair;+use rand::rngs::StdRng;+use whatsmeow::signal::{+ CurveSignatures, DJB_TYPE, PreKeyBundle, SenderKeyName, SignalAddress, SignalProtocolStore,+};+use whatsmeow::store::StoreError;++fn raw_key_pair(generator: &mut StdRng) -> ([u8; 32], [u8; 32]) {+ let pair = KeyPair::generate(generator);+ (+ pair.public_key+ .public_key_bytes()+ .try_into()+ .expect("curve25519 public keys are 32 bytes"),+ pair.private_key+ .serialize()+ .try_into()+ .expect("curve25519 private keys are 32 bytes"),+ )+}++#[derive(Default)]+struct State {+ pre_keys: HashMap<u32, ([u8; 32], [u8; 32])>,+ identities: HashMap<String, [u8; 32]>,+ sessions: HashMap<String, Vec<u8>>,+ sender_keys: HashMap<(String, String), Vec<u8>>,+}++pub(crate) struct TestStore {+ identity: ([u8; 32], [u8; 32]),+ registration_id: u32,+ signed_pre_key_id: u32,+ signed_pre_key: ([u8; 32], [u8; 32]),+ signed_pre_key_signature: [u8; 64],+ state: Mutex<State>,+}++impl TestStore {+ /// A device with a signed pre-key with ID 1 and one-time pre-keys with+ /// the given IDs.+ pub(crate) fn new(+ generator: &mut StdRng,+ signatures: &impl CurveSignatures,+ registration_id: u32,+ pre_key_ids: &[u32],+ ) -> Self {+ let identity = raw_key_pair(generator);+ let signed_pre_key = raw_key_pair(generator);+ let mut signed_public = [DJB_TYPE; 33];+ signed_public[1..].copy_from_slice(&signed_pre_key.0);+ let signed_pre_key_signature = signatures.calculate_signature(&identity.1, &signed_public);+ let pre_keys = pre_key_ids+ .iter()+ .map(|id| (*id, raw_key_pair(generator)))+ .collect();+ Self {+ identity,+ registration_id,+ signed_pre_key_id: 1,+ signed_pre_key,+ signed_pre_key_signature,+ state: Mutex::new(State {+ pre_keys,+ ..State::default()+ }),+ }+ }++ fn state(&self) -> MutexGuard<'_, State> {+ self.state.lock().unwrap_or_else(PoisonError::into_inner)+ }++ /// The bundle another device starts a session from.+ pub(crate) fn bundle(&self, device_id: u32, pre_key_id: Option<u32>) -> PreKeyBundle {+ PreKeyBundle {+ registration_id: self.registration_id,+ device_id,+ pre_key: pre_key_id.map(|id| (id, self.state().pre_keys[&id].0)),+ signed_pre_key_id: self.signed_pre_key_id,+ signed_pre_key_public: self.signed_pre_key.0,+ signed_pre_key_signature: self.signed_pre_key_signature,+ identity_key: self.identity.0,+ }+ }++ pub(crate) fn has_pre_key(&self, id: u32) -> bool {+ self.state().pre_keys.contains_key(&id)+ }++ pub(crate) fn session(&self, address: &SignalAddress) -> Option<Vec<u8>> {+ self.state().sessions.get(&address.to_string()).cloned()+ }++ pub(crate) fn set_session(&self, address: &SignalAddress, record: Vec<u8>) {+ self.state().sessions.insert(address.to_string(), record);+ }++ /// Forgets a sender key, as whatsmeow does to rotate its own.+ pub(crate) fn delete_sender_key(&self, name: &SenderKeyName) {+ self.state()+ .sender_keys+ .remove(&(name.group_id.clone(), name.sender.to_string()));+ }+}++#[async_trait]+impl SignalProtocolStore for TestStore {+ fn get_identity_key_pair(&self) -> ([u8; 32], [u8; 32]) {+ self.identity+ }++ fn get_local_registration_id(&self) -> u32 {+ self.registration_id+ }++ async fn save_identity(+ &self,+ address: &SignalAddress,+ identity_key: [u8; 32],+ ) -> Result<(), StoreError> {+ self.state()+ .identities+ .insert(address.to_string(), identity_key);+ Ok(())+ }++ /// Trust on first use, like whatsmeow's SQL store.+ async fn is_trusted_identity(+ &self,+ address: &SignalAddress,+ identity_key: [u8; 32],+ ) -> Result<bool, StoreError> {+ Ok(self+ .state()+ .identities+ .get(&address.to_string())+ .is_none_or(|known| *known == identity_key))+ }++ async fn load_pre_key(&self, id: u32) -> Result<Option<([u8; 32], [u8; 32])>, StoreError> {+ Ok(self.state().pre_keys.get(&id).copied())+ }++ async fn remove_pre_key(&self, id: u32) -> Result<(), StoreError> {+ self.state().pre_keys.remove(&id);+ Ok(())+ }++ async fn load_session(&self, address: &SignalAddress) -> Result<Option<Vec<u8>>, StoreError> {+ Ok(self.session(address))+ }++ async fn store_session(+ &self,+ address: &SignalAddress,+ record: Vec<u8>,+ ) -> Result<(), StoreError> {+ self.set_session(address, record);+ Ok(())+ }++ async fn contains_session(&self, address: &SignalAddress) -> Result<bool, StoreError> {+ Ok(self.state().sessions.contains_key(&address.to_string()))+ }++ fn load_signed_pre_key(&self, id: u32) -> Option<([u8; 32], [u8; 32], [u8; 64])> {+ (id == self.signed_pre_key_id).then_some((+ self.signed_pre_key.0,+ self.signed_pre_key.1,+ self.signed_pre_key_signature,+ ))+ }++ async fn store_sender_key(+ &self,+ name: &SenderKeyName,+ record: Vec<u8>,+ ) -> Result<(), StoreError> {+ self.state()+ .sender_keys+ .insert((name.group_id.clone(), name.sender.to_string()), record);+ Ok(())+ }++ async fn load_sender_key(&self, name: &SenderKeyName) -> Result<Option<Vec<u8>>, StoreError> {+ Ok(self+ .state()+ .sender_keys+ .get(&(name.group_id.clone(), name.sender.to_string()))+ .cloned())+ }+}diff --git a/crates/whatsmeow_signal/testdata/generator/README.md b/crates/whatsmeow_signal/testdata/generator/README.mdnew file mode 100644index 0000000000..6b6665ec67--- /dev/null+++ b/crates/whatsmeow_signal/testdata/generator/README.md@@ -0,0 +1,34 @@+# go.mau.fi/libsignal oracle++Test tooling for `crates/whatsmeow_signal`; not built in CI.++`main.go` runs [go.mau.fi/libsignal](https://github.com/tulir/libsignal-protocol-go)+v0.2.2, the version whatsmeow `b2b1e71d45e2ff171153b025b5a42f475bb30ef8`+pins, behind a JSON-lines protocol on stdin/stdout: one request per line+(`new_device`, `make_bundle`, `process_bundle`, `encrypt`, `decrypt`,+`create_sender_key`, `process_sender_key`, `delete_sender_key`,+`group_encrypt`, `group_decrypt`, `sign`, `verify`), one response per line.+Bytes are hex. Each device keeps its stores in memory, serialized the way+whatsmeow serializes them, and trusts identities on first use like+whatsmeow's SQL store.++`src/interop_test.rs` drives it through one conversation: XEdDSA signatures+both ways (both sign-bit values), pre-key bundles and pre-key messages in both+directions, with and without a one-time pre-key, several ratchet turns with+out-of-order and duplicate messages, and sender keys in both directions with+rotation. Rust's randomness is seeded, so the conversation is deterministic on+the Rust side; `testdata/interop_transcript.json` records every request and+response, and `cargo test` replays it.++Go is on `PATH`, or `nix shell nixpkgs#go`. From the repository root:++```sh+# Talk to Go live.+cargo test -p whatsmeow_signal talks_to_go_libsignal -- --ignored+# Regenerate the transcript (after changing the conversation or the seeds).+cargo test -p whatsmeow_signal record_go_transcript -- --ignored+```++The replay also fails when Rust's output changes for the same seed, for+example after a `rand` upgrade changes `StdRng`; regenerate the transcript+then, after checking the live test passes.diff --git a/crates/whatsmeow_signal/testdata/generator/go.mod b/crates/whatsmeow_signal/testdata/generator/go.modnew file mode 100644index 0000000000..d35b754c36--- /dev/null+++ b/crates/whatsmeow_signal/testdata/generator/go.mod@@ -0,0 +1,11 @@+module github.com/macro-inc/macro/crates/whatsmeow_signal/testdata/generator++go 1.25.0++require go.mau.fi/libsignal v0.2.2++require (+ filippo.io/edwards25519 v1.2.0 // indirect+ golang.org/x/crypto v0.52.0 // indirect+ google.golang.org/protobuf v1.36.11 // indirect+)diff --git a/crates/whatsmeow_signal/testdata/generator/go.sum b/crates/whatsmeow_signal/testdata/generator/go.sumnew file mode 100644index 0000000000..3f1dba5198--- /dev/null+++ b/crates/whatsmeow_signal/testdata/generator/go.sum@@ -0,0 +1,10 @@+filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo=+filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc=+github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=+github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=+go.mau.fi/libsignal v0.2.2 h1:QV+XdzQkm3x3aSG7FcqfGSZuFXz83pRZPBFaPygHbOU=+go.mau.fi/libsignal v0.2.2/go.mod h1:CRlIQg2J8uYTfDFvNoO8/KcZjs5cey0vbc6oj/bssY0=+golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=+golang.org/x/crypto v0.52.0/go.mod h1:1QgfPxDqh0T2M/elOJtp9RvuR95kVjir0e6/BvEmGbc=+google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=+google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=diff --git a/crates/whatsmeow_signal/testdata/generator/main.go b/crates/whatsmeow_signal/testdata/generator/main.gonew file mode 100644index 0000000000..2dd3ea64eb--- /dev/null+++ b/crates/whatsmeow_signal/testdata/generator/main.go@@ -0,0 +1,463 @@+// Command generator is a test oracle for crates/whatsmeow_signal: it runs+// go.mau.fi/libsignal v0.2.2, the Signal implementation whatsmeow+// b2b1e71d45e2ff171153b025b5a42f475bb30ef8 uses, behind a line-oriented JSON+// protocol on stdin/stdout. The Rust interop test drives it as the other side+// of a conversation and records the exchange; see README.md.+//+// Test tooling only; never built in CI.+package main++import (+ "bufio"+ "bytes"+ "context"+ "encoding/hex"+ "encoding/json"+ "fmt"+ "os"++ "go.mau.fi/libsignal/ecc"+ "go.mau.fi/libsignal/groups"+ groupRecord "go.mau.fi/libsignal/groups/state/record"+ "go.mau.fi/libsignal/keys/identity"+ "go.mau.fi/libsignal/keys/prekey"+ "go.mau.fi/libsignal/protocol"+ "go.mau.fi/libsignal/serialize"+ "go.mau.fi/libsignal/session"+ "go.mau.fi/libsignal/state/record"+ "go.mau.fi/libsignal/util/keyhelper"+ "go.mau.fi/libsignal/util/optional"+)++// The serializer whatsmeow uses (store.SignalProtobufSerializer).+var serializer = serialize.NewProtoBufSerializer()++// device is one Go party: an in-memory version of whatsmeow's store.Device.+type device struct {+ identity *identity.KeyPair+ registrationID uint32+ identities map[string][32]byte+ preKeys map[uint32]*record.PreKey+ signedPreKeys map[uint32]*record.SignedPreKey+ sessions map[string][]byte+ senderKeys map[string][]byte+}++func (d *device) GetIdentityKeyPair() *identity.KeyPair { return d.identity }+func (d *device) GetLocalRegistrationID() uint32 { return d.registrationID }++func (d *device) SaveIdentity(_ context.Context, address *protocol.SignalAddress, key *identity.Key) error {+ d.identities[address.String()] = key.PublicKey().PublicKey()+ return nil+}++// IsTrustedIdentity trusts on first use, like whatsmeow's SQL store.+func (d *device) IsTrustedIdentity(_ context.Context, address *protocol.SignalAddress, key *identity.Key) (bool, error) {+ known, ok := d.identities[address.String()]+ return !ok || known == key.PublicKey().PublicKey(), nil+}++func (d *device) LoadPreKey(_ context.Context, id uint32) (*record.PreKey, error) {+ return d.preKeys[id], nil+}+func (d *device) StorePreKey(_ context.Context, id uint32, key *record.PreKey) error {+ d.preKeys[id] = key+ return nil+}+func (d *device) ContainsPreKey(_ context.Context, id uint32) (bool, error) {+ _, ok := d.preKeys[id]+ return ok, nil+}+func (d *device) RemovePreKey(_ context.Context, id uint32) error {+ delete(d.preKeys, id)+ return nil+}++func (d *device) LoadSession(_ context.Context, address *protocol.SignalAddress) (*record.Session, error) {+ raw, ok := d.sessions[address.String()]+ if !ok {+ return record.NewSession(serializer.Session, serializer.State), nil+ }+ return record.NewSessionFromBytes(raw, serializer.Session, serializer.State)+}+func (d *device) GetSubDeviceSessions(context.Context, string) ([]uint32, error) {+ panic("not implemented")+}+func (d *device) StoreSession(_ context.Context, address *protocol.SignalAddress, session *record.Session) error {+ d.sessions[address.String()] = session.Serialize()+ return nil+}+func (d *device) ContainsSession(_ context.Context, address *protocol.SignalAddress) (bool, error) {+ _, ok := d.sessions[address.String()]+ return ok, nil+}+func (d *device) DeleteSession(_ context.Context, address *protocol.SignalAddress) error {+ delete(d.sessions, address.String())+ return nil+}+func (d *device) DeleteAllSessions(context.Context) error {+ d.sessions = map[string][]byte{}+ return nil+}++func (d *device) LoadSignedPreKey(_ context.Context, id uint32) (*record.SignedPreKey, error) {+ return d.signedPreKeys[id], nil+}+func (d *device) LoadSignedPreKeys(context.Context) ([]*record.SignedPreKey, error) {+ panic("not implemented")+}+func (d *device) StoreSignedPreKey(_ context.Context, id uint32, key *record.SignedPreKey) error {+ d.signedPreKeys[id] = key+ return nil+}+func (d *device) ContainsSignedPreKey(_ context.Context, id uint32) (bool, error) {+ _, ok := d.signedPreKeys[id]+ return ok, nil+}+func (d *device) RemoveSignedPreKey(_ context.Context, id uint32) error {+ delete(d.signedPreKeys, id)+ return nil+}++func senderKeyID(name *protocol.SenderKeyName) string {+ return name.GroupID() + "|" + name.Sender().String()+}++func (d *device) StoreSenderKey(_ context.Context, name *protocol.SenderKeyName, key *groupRecord.SenderKey) error {+ d.senderKeys[senderKeyID(name)] = key.Serialize()+ return nil+}++func (d *device) LoadSenderKey(_ context.Context, name *protocol.SenderKeyName) (*groupRecord.SenderKey, error) {+ raw, ok := d.senderKeys[senderKeyID(name)]+ if !ok {+ return groupRecord.NewSenderKey(serializer.SenderKeyRecord, serializer.SenderKeyState), nil+ }+ return groupRecord.NewSenderKeyFromBytes(raw, serializer.SenderKeyRecord, serializer.SenderKeyState)+}++// hexBytes is a byte slice that travels as a hex string.+type hexBytes []byte++func (h hexBytes) MarshalJSON() ([]byte, error) { return json.Marshal(hex.EncodeToString(h)) }+func (h *hexBytes) UnmarshalJSON(data []byte) error {+ var text string+ if err := json.Unmarshal(data, &text); err != nil {+ return err+ }+ decoded, err := hex.DecodeString(text)+ *h = decoded+ return err+}++type address struct {+ Name string `json:"name"`+ Device uint32 `json:"device"`+}++func (a address) signal() *protocol.SignalAddress { return protocol.NewSignalAddress(a.Name, a.Device) }++type keyWithID struct {+ ID uint32 `json:"id"`+ Public hexBytes `json:"public"`+}++type bundle struct {+ RegistrationID uint32 `json:"registration_id"`+ DeviceID uint32 `json:"device_id"`+ PreKey *keyWithID `json:"pre_key,omitempty"`+ SignedPreKey keyWithID `json:"signed_pre_key"`+ SignedPreKeySignature hexBytes `json:"signed_pre_key_signature"`+ IdentityKey hexBytes `json:"identity_key"`+}++// request is one line of input. Every field but Op is optional; each op+// reads the ones it needs.+type request struct {+ Op string `json:"op"`+ Device string `json:"device,omitempty"`+ RegistrationID uint32 `json:"registration_id,omitempty"`+ Peer *address `json:"peer,omitempty"`+ Bundle *bundle `json:"bundle,omitempty"`+ SignedPreKeyID uint32 `json:"signed_pre_key_id,omitempty"`+ PreKeyID uint32 `json:"pre_key_id,omitempty"`+ Group string `json:"group,omitempty"`+ Type string `json:"type,omitempty"`+ Plaintext hexBytes `json:"plaintext,omitempty"`+ Ciphertext hexBytes `json:"ciphertext,omitempty"`+ Message hexBytes `json:"message,omitempty"`+ PrivateKey hexBytes `json:"private_key,omitempty"`+ PublicKey hexBytes `json:"public_key,omitempty"`+ Signature hexBytes `json:"signature,omitempty"`+}++// response is one line of output: Error is set when the operation failed.+type response struct {+ Error string `json:"error,omitempty"`+ Bundle *bundle `json:"bundle,omitempty"`+ Type string `json:"type,omitempty"`+ Plaintext hexBytes `json:"plaintext,omitempty"`+ Ciphertext hexBytes `json:"ciphertext,omitempty"`+ Message hexBytes `json:"message,omitempty"`+ Signature hexBytes `json:"signature,omitempty"`+ Valid *bool `json:"valid,omitempty"`+ IdentityKey hexBytes `json:"identity_key,omitempty"`+}++type oracle struct {+ devices map[string]*device+}++func (o *oracle) device(name string) (*device, error) {+ d, ok := o.devices[name]+ if !ok {+ return nil, fmt.Errorf("unknown device %q", name)+ }+ return d, nil+}++func publicKey(raw []byte) (ecc.ECPublicKeyable, error) {+ if len(raw) != 32 {+ return nil, fmt.Errorf("public key is %d bytes, want 32", len(raw))+ }+ return ecc.NewDjbECPublicKey([32]byte(raw)), nil+}++func (o *oracle) handle(ctx context.Context, req request) (response, error) {+ switch req.Op {+ case "new_device":+ identityKey, err := keyhelper.GenerateIdentityKeyPair()+ if err != nil {+ return response{}, err+ }+ o.devices[req.Device] = &device{+ identity: identityKey,+ registrationID: req.RegistrationID,+ identities: map[string][32]byte{},+ preKeys: map[uint32]*record.PreKey{},+ signedPreKeys: map[uint32]*record.SignedPreKey{},+ sessions: map[string][]byte{},+ senderKeys: map[string][]byte{},+ }+ public := identityKey.PublicKey().PublicKey().PublicKey()+ return response{IdentityKey: public[:]}, nil++ case "make_bundle":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ signed, err := keyhelper.GenerateSignedPreKey(d.identity, req.SignedPreKeyID, serializer.SignedPreKeyRecord)+ if err != nil {+ return response{}, err+ }+ d.signedPreKeys[req.SignedPreKeyID] = signed+ signedPublic := signed.KeyPair().PublicKey().PublicKey()+ signature := signed.Signature()+ identityPublic := d.identity.PublicKey().PublicKey().PublicKey()+ out := &bundle{+ RegistrationID: d.registrationID,+ DeviceID: 0,+ SignedPreKey: keyWithID{ID: req.SignedPreKeyID, Public: signedPublic[:]},+ SignedPreKeySignature: signature[:],+ IdentityKey: identityPublic[:],+ }+ if req.PreKeyID != 0 {+ pair, err := ecc.GenerateKeyPair()+ if err != nil {+ return response{}, err+ }+ d.preKeys[req.PreKeyID] = record.NewPreKey(req.PreKeyID, pair, serializer.PreKeyRecord)+ preKeyPublic := pair.PublicKey().PublicKey()+ out.PreKey = &keyWithID{ID: req.PreKeyID, Public: preKeyPublic[:]}+ }+ return response{Bundle: out}, nil++ case "process_bundle":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ b := req.Bundle+ signedPreKey, err := publicKey(b.SignedPreKey.Public)+ if err != nil {+ return response{}, err+ }+ identityKey, err := publicKey(b.IdentityKey)+ if err != nil {+ return response{}, err+ }+ preKeyID := optional.NewEmptyUint32()+ var preKey ecc.ECPublicKeyable+ if b.PreKey != nil {+ preKeyID = optional.NewOptionalUint32(b.PreKey.ID)+ if preKey, err = publicKey(b.PreKey.Public); err != nil {+ return response{}, err+ }+ }+ goBundle := prekey.NewBundle(b.RegistrationID, b.DeviceID, preKeyID, b.SignedPreKey.ID,+ preKey, signedPreKey, [64]byte(b.SignedPreKeySignature), identity.NewKey(identityKey))+ builder := session.NewBuilderFromSignal(d, req.Peer.signal(), serializer)+ return response{}, builder.ProcessBundle(ctx, goBundle)++ case "encrypt":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ builder := session.NewBuilderFromSignal(d, req.Peer.signal(), serializer)+ message, err := session.NewCipher(builder, req.Peer.signal()).Encrypt(ctx, req.Plaintext)+ if err != nil {+ return response{}, err+ }+ kind := "msg"+ if message.Type() == protocol.PREKEY_TYPE {+ kind = "pkmsg"+ }+ return response{Type: kind, Ciphertext: message.Serialize()}, nil++ case "decrypt":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ builder := session.NewBuilderFromSignal(d, req.Peer.signal(), serializer)+ cipher := session.NewCipher(builder, req.Peer.signal())+ var plaintext []byte+ switch req.Type {+ case "pkmsg":+ message, err := protocol.NewPreKeySignalMessageFromBytes(req.Ciphertext, serializer.PreKeySignalMessage, serializer.SignalMessage)+ if err != nil {+ return response{}, err+ }+ if plaintext, err = cipher.DecryptMessage(ctx, message); err != nil {+ return response{}, err+ }+ case "msg":+ message, err := protocol.NewSignalMessageFromBytes(req.Ciphertext, serializer.SignalMessage)+ if err != nil {+ return response{}, err+ }+ if plaintext, err = cipher.Decrypt(ctx, message); err != nil {+ return response{}, err+ }+ default:+ return response{}, fmt.Errorf("unknown message type %q", req.Type)+ }+ return response{Plaintext: plaintext}, nil++ case "create_sender_key":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ name := protocol.NewSenderKeyName(req.Group, req.Peer.signal())+ message, err := groups.NewGroupSessionBuilder(d, serializer).Create(ctx, name)+ if err != nil {+ return response{}, err+ }+ return response{Message: message.Serialize()}, nil++ case "process_sender_key":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ name := protocol.NewSenderKeyName(req.Group, req.Peer.signal())+ message, err := protocol.NewSenderKeyDistributionMessageFromBytes(req.Message, serializer.SenderKeyDistributionMessage)+ if err != nil {+ return response{}, err+ }+ return response{}, groups.NewGroupSessionBuilder(d, serializer).Process(ctx, name, message)++ case "delete_sender_key":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ delete(d.senderKeys, senderKeyID(protocol.NewSenderKeyName(req.Group, req.Peer.signal())))+ return response{}, nil++ case "group_encrypt":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ name := protocol.NewSenderKeyName(req.Group, req.Peer.signal())+ builder := groups.NewGroupSessionBuilder(d, serializer)+ message, err := groups.NewGroupCipher(builder, name, d).Encrypt(ctx, req.Plaintext)+ if err != nil {+ return response{}, err+ }+ return response{Ciphertext: message.SignedSerialize()}, nil++ case "group_decrypt":+ d, err := o.device(req.Device)+ if err != nil {+ return response{}, err+ }+ name := protocol.NewSenderKeyName(req.Group, req.Peer.signal())+ message, err := protocol.NewSenderKeyMessageFromBytes(req.Ciphertext, serializer.SenderKeyMessage)+ if err != nil {+ return response{}, err+ }+ builder := groups.NewGroupSessionBuilder(d, serializer)+ plaintext, err := groups.NewGroupCipher(builder, name, d).Decrypt(ctx, message)+ if err != nil {+ return response{}, err+ }+ return response{Plaintext: plaintext}, nil++ case "sign":+ if len(req.PrivateKey) != 32 {+ return response{}, fmt.Errorf("private key is %d bytes, want 32", len(req.PrivateKey))+ }+ signature := ecc.CalculateSignature(ecc.NewDjbECPrivateKey([32]byte(req.PrivateKey)), req.Message)+ return response{Signature: signature[:]}, nil++ case "verify":+ key, err := publicKey(req.PublicKey)+ if err != nil {+ return response{}, err+ }+ if len(req.Signature) != 64 {+ return response{}, fmt.Errorf("signature is %d bytes, want 64", len(req.Signature))+ }+ valid := ecc.VerifySignature(key, req.Message, [64]byte(req.Signature))+ return response{Valid: &valid}, nil+ }+ return response{}, fmt.Errorf("unknown op %q", req.Op)+}++func main() {+ ctx := context.Background()+ o := &oracle{devices: map[string]*device{}}+ scanner := bufio.NewScanner(os.Stdin)+ scanner.Buffer(make([]byte, 1<<20), 1<<24)+ out := bufio.NewWriter(os.Stdout)+ // libsignal's default logger prints to stdout; keep stdout for responses.+ os.Stdout = os.Stderr+ for scanner.Scan() {+ var req request+ decoder := json.NewDecoder(bytes.NewReader(scanner.Bytes()))+ decoder.DisallowUnknownFields()+ resp := response{}+ if err := decoder.Decode(&req); err != nil {+ resp.Error = "bad request: " + err.Error()+ } else if r, err := o.handle(ctx, req); err != nil {+ resp = response{Error: err.Error()}+ } else {+ resp = r+ }+ line, err := json.Marshal(resp)+ if err != nil {+ panic(err)+ }+ out.Write(line)+ out.WriteByte('\n')+ out.Flush()+ }+ if err := scanner.Err(); err != nil {+ panic(err)+ }+}diff --git a/crates/whatsmeow_signal/testdata/interop_transcript.json b/crates/whatsmeow_signal/testdata/interop_transcript.jsonnew file mode 100644index 0000000000..82af6a5ca4--- /dev/null+++ b/crates/whatsmeow_signal/testdata/interop_transcript.json@@ -0,0 +1,1083 @@+[+ {+ "request": {+ "op": "sign",+ "private_key": "98794fee6600db481805b60c5e84c91af5a5e8dad1494bcd94a3f4a0a8d57f6d",+ "message": "78656464736120766563746f722030"+ },+ "response": {+ "signature": "f30560b17f2977351301cf8a9ee11d44d00257de0cc77704c294a206aaca7b5c693e586305f401dca3825a69356b70bdc911dbd09a0fcdac1569e2c2cf935d81"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "67d700369576d97a468a564a0bce976082531ddb9325b708befd4c5d0c52bd58",+ "message": "78656464736120766563746f722030",+ "signature": "478e404e7da17de4e255bc0286420ba56090d2bca2873ac4bbd5a9ae23d3fd878203cd4a1f562031b6208bcd7bca01175f764da7e032350ffda66c92c0a94984"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "67d700369576d97a468a564a0bce976082531ddb9325b708befd4c5d0c52bd58",+ "message": "78656464736120766563746f722030",+ "signature": "478e404e7da17de4e255bc0286420ba56090d2bca2873ac4bbd5a9ae23d3fd878203cd4a1f562031b6208bcd7bca01175f764da7e032350ffda66c92c0a94904"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "sign",+ "private_key": "50d38c411257de87f2eee4a9f64f1c6e48e36c05c486ecfc37ab3c3d67bcd143",+ "message": "78656464736120766563746f722031"+ },+ "response": {+ "signature": "c91f6e6f0bb44c7ed91e95fdb4b3b898b5415a54b4ef1f132b21d312974dd2863ee03007fc48296011e7a36382447dcb62ec8bc6e6864127e42dbf21bef02c84"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "5a050f9cb38bffdbaacd2e8dc8c18213c7d73f6927a87e2e1a893925de2a5730",+ "message": "78656464736120766563746f722031",+ "signature": "3c723d48df1a28f6b7fb90fc39d56047b5bf4eaf802abe245622add91537f80385e62a550ceea489263ed1ac0498a53a05781528f126c973fd60dedb6237bd80"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "5a050f9cb38bffdbaacd2e8dc8c18213c7d73f6927a87e2e1a893925de2a5730",+ "message": "78656464736120766563746f722031",+ "signature": "3c723d48df1a28f6b7fb90fc39d56047b5bf4eaf802abe245622add91537f80385e62a550ceea489263ed1ac0498a53a05781528f126c973fd60dedb6237bd00"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "sign",+ "private_key": "f87966a3be87b9df013af166bb6c89db61dbd45f79a3df4311f240a6a2b86372",+ "message": "78656464736120766563746f722032"+ },+ "response": {+ "signature": "c62367943af8032bd1aa197dee33966200a12d6ff639579e59126563a99c9998f1604a81ab4f0c433c10cc47a1e27e2a944ddddc22d32e4fdf368ee55279df81"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "b49d368af1f08d9df2fb4757ce34fb7794d3940beffe4559e6a5d9c15d7dac02",+ "message": "78656464736120766563746f722032",+ "signature": "e792d7e49f3d904796fdc977b42d0bf7591de147d3ecd88fede8569d3b27a5fa578d8e8d4fcd53124240ae8c85f1179c0b888cc2e8a03dcc34f77d4c0edad283"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "b49d368af1f08d9df2fb4757ce34fb7794d3940beffe4559e6a5d9c15d7dac02",+ "message": "78656464736120766563746f722032",+ "signature": "e792d7e49f3d904796fdc977b42d0bf7591de147d3ecd88fede8569d3b27a5fa578d8e8d4fcd53124240ae8c85f1179c0b888cc2e8a03dcc34f77d4c0edad203"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "sign",+ "private_key": "d05c51efdb962b2cb5c765f11160312c609b3434cb65a1935478970b2713e27b",+ "message": "78656464736120766563746f722033"+ },+ "response": {+ "signature": "cb04d54f3fcb8982900a7dc58a4c934eb6de39fb05fd7d8675928c474675abe1ae63aca756b5782fc0926fd60970547c8e57c098069e9bf55d7fdd63320fe900"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "16088dbd1d2d92977f06a45a45eda993c05d1bb72d3c40d3776ac95b32619b1b",+ "message": "78656464736120766563746f722033",+ "signature": "1336bae178c031bbb22650d3312c6d6bc08a4864eaae453e093307bdb268c65b3904fa5c0b96330988f778cce81e72c2c0e158600e813b5c8ec6e574395b3f08"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "16088dbd1d2d92977f06a45a45eda993c05d1bb72d3c40d3776ac95b32619b1b",+ "message": "78656464736120766563746f722033",+ "signature": "1336bae178c031bbb22650d3312c6d6bc08a4864eaae453e093307bdb268c65b3904fa5c0b96330988f778cce81e72c2c0e158600e813b5c8ec6e574395b3f88"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "sign",+ "private_key": "4892236c0435453656a85b7d7c11dc6dfb7414416d9e8a37e4c7235faeec4f70",+ "message": "78656464736120766563746f722034"+ },+ "response": {+ "signature": "1f8ea0aa351bef3ca2db09b9c236ad465a13518e349724f5bc294a74f30e0a078271a95c51de3067db0ca480ed4a7d4b6a823ee86c2836bdf26fc99f391ef18b"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "be50d9f9199a8076e2b903de6d852d62f7bdbfb9c6723044893d15ddace29343",+ "message": "78656464736120766563746f722034",+ "signature": "a23fa3e4fa66b743c5d324a19780c58535b6bf345a51438ccce04effacd10096ef0c7436a01ca847d2eb693d1243985db98755d25fa9a4c95c185deed5abbf8b"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "be50d9f9199a8076e2b903de6d852d62f7bdbfb9c6723044893d15ddace29343",+ "message": "78656464736120766563746f722034",+ "signature": "a23fa3e4fa66b743c5d324a19780c58535b6bf345a51438ccce04effacd10096ef0c7436a01ca847d2eb693d1243985db98755d25fa9a4c95c185deed5abbf0b"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "sign",+ "private_key": "609e703a43e61244e7eea359be9771095b5382131359554ca13a31406d963070",+ "message": "78656464736120766563746f722035"+ },+ "response": {+ "signature": "9b5b7835355649462835ecba3fbe90c3d2f73b9a8f7a95cd1716796ba6b972ae5eb9cf7fb2b7f65c42505bb15372e8fa942848b5063c61c751d5a2981b874084"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "ff0a95398ebd71a38d9d17cc9b2432fcec90a4bc9dd0a74f68c5ba0bf1cf7642",+ "message": "78656464736120766563746f722035",+ "signature": "e9820a242c362cdbabc1955c1047939c11c7f70ccd3b620a6f12b85380036c6ac3375aa049245a7163072459089d6b4d60af51ce0301027913f676c9d8c1168b"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "ff0a95398ebd71a38d9d17cc9b2432fcec90a4bc9dd0a74f68c5ba0bf1cf7642",+ "message": "78656464736120766563746f722035",+ "signature": "e9820a242c362cdbabc1955c1047939c11c7f70ccd3b620a6f12b85380036c6ac3375aa049245a7163072459089d6b4d60af51ce0301027913f676c9d8c1160b"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "sign",+ "private_key": "a091d09e42f8fdc195dea2476b54d90c9706b62e13192153cea0d507d1602778",+ "message": "78656464736120766563746f722036"+ },+ "response": {+ "signature": "63ae8b899fa6533e90a73dc311644ee744718eba2d6f10c7bf049b36ac43367ee725ab3ffe7a7034ad4bda94ba554545f6c5eb560751d113d337ce1d482ca384"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "9ab50b6af4130b9f903c56b5969c6d5d1f40252e4714f952c7a812efce2db34f",+ "message": "78656464736120766563746f722036",+ "signature": "765f7f9ea0fdbbbdb17c70a0d645c82127ca8020bff577ad3beda3fadb0e89e8e3d4245e45b430d1f3f21c429d91dbd2252bf02f9ed551193e256fd689535f8f"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "9ab50b6af4130b9f903c56b5969c6d5d1f40252e4714f952c7a812efce2db34f",+ "message": "78656464736120766563746f722036",+ "signature": "765f7f9ea0fdbbbdb17c70a0d645c82127ca8020bff577ad3beda3fadb0e89e8e3d4245e45b430d1f3f21c429d91dbd2252bf02f9ed551193e256fd689535f0f"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "sign",+ "private_key": "b0bf8903323f8f2a48c56afe6a89a0b11c59aaaffc64e826fcf96dba2380284b",+ "message": "78656464736120766563746f722037"+ },+ "response": {+ "signature": "c80825b79bfa108a2ffe921f9f1f31a00ec3715fc6947f5823c692c2bc15a7ca53de05a0537455849d9436b57ac45045d44ca1b35b048a4156a4a3f95cb68380"+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "e1a6eae61e5a81ff2aeb0da87c64e2b17eafad82edc986921cf2b33515ca6665",+ "message": "78656464736120766563746f722037",+ "signature": "474e4c1ac46d06fbc2b5c6ddec5c1032a4de00bbe321f36ac5afcc892a472a4d90e0edcbfb08913b7052c876743f0aa7f12eca3cd2d065b8cee85d0a88c5f982"+ },+ "response": {+ "valid": true+ }+ },+ {+ "request": {+ "op": "verify",+ "public_key": "e1a6eae61e5a81ff2aeb0da87c64e2b17eafad82edc986921cf2b33515ca6665",+ "message": "78656464736120766563746f722037",+ "signature": "474e4c1ac46d06fbc2b5c6ddec5c1032a4de00bbe321f36ac5afcc892a472a4d90e0edcbfb08913b7052c876743f0aa7f12eca3cd2d065b8cee85d0a88c5f902"+ },+ "response": {+ "valid": false+ }+ },+ {+ "request": {+ "op": "new_device",+ "device": "bob",+ "registration_id": 2002+ },+ "response": {+ "identity_key": "18ad2964c025b62f53ab61fc5aef89c5f594e6ff2ab8559cd59b0ce72f892660"+ }+ },+ {+ "request": {+ "op": "make_bundle",+ "device": "bob",+ "signed_pre_key_id": 1,+ "pre_key_id": 31+ },+ "response": {+ "bundle": {+ "registration_id": 2002,+ "device_id": 0,+ "pre_key": {+ "id": 31,+ "public": "a64d01fc93974045c6f74f71181d8d3468b188eeeeb046dad2fd564b64df773c"+ },+ "signed_pre_key": {+ "id": 1,+ "public": "e3c7219d36a2c679f30b6a88b7bf9919ab89c32f60386937896c97bf6aa6d065"+ },+ "signed_pre_key_signature": "86bb906a900054303b458e1c2c22aa73c4462be02c16d4bf63efd84e3f099692e336f3dd2384a7a1adade220ef45396657cc487d990182643ac64fe83a24d282",+ "identity_key": "18ad2964c025b62f53ab61fc5aef89c5f594e6ff2ab8559cd59b0ce72f892660"+ }+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "pkmsg",+ "ciphertext": "33081f122105ea76c35b1d420e61c2a97151ec7e653870fff09ec8efb628995eb43d26bde0591a210567d700369576d97a468a564a0bce976082531ddb9325b708befd4c5d0c52bd582242330a2105f7896bdcfda1b2baa6479e74eeff5aea4e04a3bb6536cb5ff34485a422c4b907100118002210aa56967e6e9daf2f54a8694397b9449d471ffd3a938f9e1828e9073001"+ },+ "response": {+ "plaintext": "7365636f6e64"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "pkmsg",+ "ciphertext": "33081f122105ea76c35b1d420e61c2a97151ec7e653870fff09ec8efb628995eb43d26bde0591a210567d700369576d97a468a564a0bce976082531ddb9325b708befd4c5d0c52bd582242330a2105f7896bdcfda1b2baa6479e74eeff5aea4e04a3bb6536cb5ff34485a422c4b90710001800221022ecbb14cf5c9d5f83f6ccc660cc14cee0cde738f208883e28e9073001"+ },+ "response": {+ "plaintext": "6669727374"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "7265706c79206f6e65"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105f84d35937209f8eaeba4e6b59c9fefd904c071285f17378abbfe461ed2dfe36f100018ffffffff0f2210d61d417cfbc3326fa5e08c51e09b15e64f4d2747030f1bc8"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "7265706c792074776f"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105f84d35937209f8eaeba4e6b59c9fefd904c071285f17378abbfe461ed2dfe36f100118ffffffff0f2210e033478f686754e9ebb2132dbae41bf4e669263facbe42eb"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "7265706c79207468726565"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105f84d35937209f8eaeba4e6b59c9fefd904c071285f17378abbfe461ed2dfe36f100218ffffffff0f2210e32466e42123fb94f7ad2c908a7c49b5bad879de93261316"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "756e746f7563686564"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105f84d35937209f8eaeba4e6b59c9fefd904c071285f17378abbfe461ed2dfe36f100318ffffffff0f2210dbbed1f0aa3edd3fab3080638c243565ade7dde7269889bf"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a2105257bdcdd4d89d59ed043ca110247747fc38c59213cacde4f3f2ba02926592e74100118012210d581eee2be368e6bf3c0bd97f9da8edccfcfd86af26643d4"+ },+ "response": {+ "plaintext": "7275737420726f756e6420302062"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a2105257bdcdd4d89d59ed043ca110247747fc38c59213cacde4f3f2ba02926592e741000180122109b2476c08f9d1b43890d1a64acb09f3735e7855682d5f12d"+ },+ "response": {+ "plaintext": "7275737420726f756e6420302061"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a2105257bdcdd4d89d59ed043ca110247747fc38c59213cacde4f3f2ba02926592e741000180122109b2476c08f9d1b43890d1a64acb09f3735e7855682d5f12d"+ },+ "response": {+ "error": "failed to get or create message keys: received message with old counter (index: 2, count: 0)"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420302061"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a21051e5fe01f71bc846c0aa96ac734f05ab78eb7c3985546f8f01ed5ec2c85b6696310001803221095fa43ebce5b303248c88573ed19f0b0e7a509f8535b7895"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420302062"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a21051e5fe01f71bc846c0aa96ac734f05ab78eb7c3985546f8f01ed5ec2c85b6696310011803221032d8053e74da394f381892067fdf96570b218cdd4ff265e3"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a21056434d50a9fe312bd129a5d80c082d1ccf2c6f409802f04af361fbda644944a781001180122101bdad0a0f65d672cd8116dcb4b02200f84ce91e5ac1a65f9"+ },+ "response": {+ "plaintext": "7275737420726f756e6420312062"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a21056434d50a9fe312bd129a5d80c082d1ccf2c6f409802f04af361fbda644944a781000180122109d1cc2d6e2e2f7640e201f8fceb1f20020139f60afe095a7"+ },+ "response": {+ "plaintext": "7275737420726f756e6420312061"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a21056434d50a9fe312bd129a5d80c082d1ccf2c6f409802f04af361fbda644944a781000180122109d1cc2d6e2e2f7640e201f8fceb1f20020139f60afe095a7"+ },+ "response": {+ "error": "failed to get or create message keys: received message with old counter (index: 2, count: 0)"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420312061"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105a051652de004c03ad67e0820fc3eedf8b7196206304bef3d03b7e44870f5a8741000180122100ebb345df1c796cb36da00a7af0e87df9ed520ef0a4c3af8"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420312062"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105a051652de004c03ad67e0820fc3eedf8b7196206304bef3d03b7e44870f5a874100118012210d318fdabe49177f26e310dd887a76266783219f23bb33c35"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a210505b36302adc577ac75fe312c99dfb0944ad83b780509446c46790b585c4c0066100118012210632a2da0a9ba8b8a32ba66ac83afa6aaa874c934272cc85f"+ },+ "response": {+ "plaintext": "7275737420726f756e6420322062"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a210505b36302adc577ac75fe312c99dfb0944ad83b780509446c46790b585c4c006610001801221086266558c4b9cbd65673b2eb03dded06671eff0616306ec4"+ },+ "response": {+ "plaintext": "7275737420726f756e6420322061"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a210505b36302adc577ac75fe312c99dfb0944ad83b780509446c46790b585c4c006610001801221086266558c4b9cbd65673b2eb03dded06671eff0616306ec4"+ },+ "response": {+ "error": "failed to get or create message keys: received message with old counter (index: 2, count: 0)"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420322061"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105c9ac61ef10ec82c4d59fa1bf26c8ffe6b75b7e300fd1ccbb844174708bf8c33c100018012210147c66fdcb5c267f76ec09f4308c3ed375771e4d351bd9aa"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420322062"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a2105c9ac61ef10ec82c4d59fa1bf26c8ffe6b75b7e300fd1ccbb844174708bf8c33c10011801221041130563ffbf154e70b7f728bef5affc658e0e4fef799ed2"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a21057974bb9d8aa9bbd3f860bd5186dcf446c28f0570a47358f4b6252295bd3286691001180122100651b4244270cb7d670462597489024e2cbd21e5136c933d"+ },+ "response": {+ "plaintext": "7275737420726f756e6420332062"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a21057974bb9d8aa9bbd3f860bd5186dcf446c28f0570a47358f4b6252295bd328669100018012210e0e73e83d12307edd5ff173779ceec7df8fe99d845fd6f34"+ },+ "response": {+ "plaintext": "7275737420726f756e6420332061"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "msg",+ "ciphertext": "330a21057974bb9d8aa9bbd3f860bd5186dcf446c28f0570a47358f4b6252295bd328669100018012210e0e73e83d12307edd5ff173779ceec7df8fe99d845fd6f34"+ },+ "response": {+ "error": "failed to get or create message keys: received message with old counter (index: 2, count: 0)"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420332061"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a21053c259080f67c7573c1b6921de13101a5871076631a9e8e7dfae2d128cf3e1c25100018012210f568008618dd28f06db0e5c1e7b57be7e86d8dc13a0badda"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "bob",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "plaintext": "676f20726f756e6420332062"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a21053c259080f67c7573c1b6921de13101a5871076631a9e8e7dfae2d128cf3e1c2510011801221028da8a15e2d4bc16814d53362a709d37520460bdbc0f217b"+ }+ },+ {+ "request": {+ "op": "new_device",+ "device": "erin",+ "registration_id": 5005+ },+ "response": {+ "identity_key": "72f3c6f08f37c330d7f7231e90883417f685f0f8469a88b6adff773994797d60"+ }+ },+ {+ "request": {+ "op": "make_bundle",+ "device": "erin",+ "signed_pre_key_id": 9+ },+ "response": {+ "bundle": {+ "registration_id": 5005,+ "device_id": 0,+ "signed_pre_key": {+ "id": 9,+ "public": "73d82ad0430d4ccac709fb38161b76cae25d47c8eaac9acc62b209aa8688515d"+ },+ "signed_pre_key_signature": "2a16e7efbcb73b3616658db21e711023778c89c2903da44bca3858c553a78407cd1d591be8fa22ea0599b3c76e65bda3461c4a175ad268da76377aa0eaed6d0a",+ "identity_key": "72f3c6f08f37c330d7f7231e90883417f685f0f8469a88b6adff773994797d60"+ }+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "erin",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "type": "pkmsg",+ "ciphertext": "33122105da396cb0177d63c0f159fdc1f2e036a2abb4f4435ff9c353095e339f8235567f1a210567d700369576d97a468a564a0bce976082531ddb9325b708befd4c5d0c52bd582242330a2105f931770ac04cc3160128b04abc77122f5a26ea5aa4a396f37df7c6ace8b6bb4c100018002210d6340f1ad5a5630c503813ffc01413521f9ed5020c4617ec28e9073009"+ },+ "response": {+ "plaintext": "68656c6c6f206572696e"+ }+ },+ {+ "request": {+ "op": "new_device",+ "device": "dave",+ "registration_id": 4004+ },+ "response": {+ "identity_key": "8030cc631c47c3d010e229aea32eb98d1a2a554e3b570a19d61c87d1bfca127a"+ }+ },+ {+ "request": {+ "op": "process_bundle",+ "device": "dave",+ "peer": {+ "name": "rust-carol",+ "device": 3+ },+ "bundle": {+ "registration_id": 3003,+ "device_id": 3,+ "signed_pre_key": {+ "id": 1,+ "public": "16088dbd1d2d92977f06a45a45eda993c05d1bb72d3c40d3776ac95b32619b1b"+ },+ "signed_pre_key_signature": "c381cd5ed9886ac6ae25a6ad26549461756e755fd97e781a6d5717047683b5ff275bd8d39827e87bd139218ba26cd8f1c2173b2b40fbee991f51fdd327966780",+ "identity_key": "b49d368af1f08d9df2fb4757ce34fb7794d3940beffe4559e6a5d9c15d7dac02",+ "pre_key": {+ "id": 77,+ "public": "be50d9f9199a8076e2b903de6d852d62f7bdbfb9c6723044893d15ddace29343"+ }+ }+ },+ "response": {}+ },+ {+ "request": {+ "op": "encrypt",+ "device": "dave",+ "peer": {+ "name": "rust-carol",+ "device": 3+ },+ "plaintext": "66726f6d20676f2031"+ },+ "response": {+ "type": "pkmsg",+ "ciphertext": "33084d1221054cc53d7f7d793ae26afed38b9ff87bce8b649c67e619eb8cb08698c30e526a151a21058030cc631c47c3d010e229aea32eb98d1a2a554e3b570a19d61c87d1bfca127a2242330a21050d3abbb9beb49e1815bee89caa0310f51b35a23275640f6e1d9e1e9c499c39131000180022101e367205fc13369ba2488c3c2cb1f3c5b6bf08b5f7e2468f28a41f3001"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "dave",+ "peer": {+ "name": "rust-carol",+ "device": 3+ },+ "plaintext": "66726f6d20676f2032"+ },+ "response": {+ "type": "pkmsg",+ "ciphertext": "33084d1221054cc53d7f7d793ae26afed38b9ff87bce8b649c67e619eb8cb08698c30e526a151a21058030cc631c47c3d010e229aea32eb98d1a2a554e3b570a19d61c87d1bfca127a2242330a21050d3abbb9beb49e1815bee89caa0310f51b35a23275640f6e1d9e1e9c499c3913100118002210a0d62e80cfb1e2354163e8d59782336e19309e50ac60b1c328a41f3001"+ }+ },+ {+ "request": {+ "op": "decrypt",+ "device": "dave",+ "peer": {+ "name": "rust-carol",+ "device": 3+ },+ "type": "msg",+ "ciphertext": "330a2105903b2527798a3f4ad96ab04aa4cbe59d281b39acb3c41bd75254e1a15ce094681000180022104bfdaca3c36e8dccb1818cf1f343126360c6792afcd8ebe7"+ },+ "response": {+ "plaintext": "7265706c792066726f6d2072757374"+ }+ },+ {+ "request": {+ "op": "encrypt",+ "device": "dave",+ "peer": {+ "name": "rust-carol",+ "device": 3+ },+ "plaintext": "616674657220746865207265706c79"+ },+ "response": {+ "type": "msg",+ "ciphertext": "330a21056e7557e38cf49e7c427b658e5b6033c291d9b6fe4b7a38c51c57648ca571242510001801221013a00dd59a0da7dfcebc73bbb8d08393369d00e885044851"+ }+ },+ {+ "request": {+ "op": "new_device",+ "device": "frank",+ "registration_id": 6006+ },+ "response": {+ "identity_key": "44b5a5dc64ad860e42b329b08d709047b02f7daa3d0c0b91afc1f78d753b973f"+ }+ },+ {+ "request": {+ "op": "process_bundle",+ "device": "frank",+ "peer": {+ "name": "rust-carol",+ "device": 3+ },+ "bundle": {+ "registration_id": 3003,+ "device_id": 3,+ "signed_pre_key": {+ "id": 1,+ "public": "16088dbd1d2d92977f06a45a45eda993c05d1bb72d3c40d3776ac95b32619b1b"+ },+ "signed_pre_key_signature": "c381cd5ed9886ac6ae25a6ad26549461756e755fd97e781a6d5717047683b5ff275bd8d39827e87bd139218ba26cd8f1c2173b2b40fbee991f51fdd327966780",+ "identity_key": "b49d368af1f08d9df2fb4757ce34fb7794d3940beffe4559e6a5d9c15d7dac02"+ }+ },+ "response": {}+ },+ {+ "request": {+ "op": "encrypt",+ "device": "frank",+ "peer": {+ "name": "rust-carol",+ "device": 3+ },+ "plaintext": "6e6f206f6e652d74696d65206b6579"+ },+ "response": {+ "type": "pkmsg",+ "ciphertext": "33122105fe2d7309ee52cc53aef6e0ed0f41d933d1c3c09d195b8d8260cea59fe47abf6d1a210544b5a5dc64ad860e42b329b08d709047b02f7daa3d0c0b91afc1f78d753b973f2242330a210570d67b2cc96a1bdb777f889d609d0809ae18a8215c4b2c5e30e904b0b6215a61100018002210f19e769fd8e1271bdc529a50e9cf589bdaa6f8f2f75f52fb28f62e3001"+ }+ },+ {+ "request": {+ "op": "process_sender_key",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "message": "3308f9a7f5ad0410001a209720db3adac4a286d528b4253e9204d5cad0e725c54a33396b0728fd928bfda52221050989b31482ee6d51964ab120aa669c0855ebe9bb1b4be3ec7c1c18f84e541c07"+ },+ "response": {}+ },+ {+ "request": {+ "op": "group_decrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "ciphertext": "3308f9a7f5ad0410021a10e5e7cf50d89c8f0dadd668e64e32c1d09851ead7fa579a7f05b8d4f4e2236c36eeba1cf6c7bedc068993b8b25bca85b90c84c471854bed569c3bbeb436d12679b0e4708358b85e79ff9307c9765e370b"+ },+ "response": {+ "plaintext": "67726f75702074776f"+ }+ },+ {+ "request": {+ "op": "group_decrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "ciphertext": "3308f9a7f5ad0410001a10fbfeb5cb710a62c43609bce59cc027b6a1669d539e06cf65dbfaf8cff1e63ad426bc270d0657b90574b22159efda800744376abe8732ee1469a7d4b63838e7ce4895f015f242c69613107b31ff1e9c0a"+ },+ "response": {+ "plaintext": "67726f7570207a65726f"+ }+ },+ {+ "request": {+ "op": "group_decrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "ciphertext": "3308f9a7f5ad0410011a1095b1cb59eb4b24cec630f9c1042d9e7000507ac1d054f6a2adcdc645fa023e71795a4020f93432815f32f29add4b8430014dac374195a4fe5a1e91feaac9e234d1cd45f70fc684a92faa2b8424363006"+ },+ "response": {+ "plaintext": "67726f7570206f6e65"+ }+ },+ {+ "request": {+ "op": "create_sender_key",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ }+ },+ "response": {+ "message": "33088a86fb820710001a208d1a904f411604da9c846e7338063717ca9ee36149fd32a28c7235efc4cfebb82221059ef08a17cb0f0780cd246209b311cd262e7f505c7e0e8298d20e2881d9c8b95b"+ }+ },+ {+ "request": {+ "op": "group_encrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ },+ "plaintext": "676f2067726f7570207a65726f"+ },+ "response": {+ "ciphertext": "33088a86fb820710001a108e7fa59dd85be2a5b6759790c0e12f1e7cd5ead0df6d7c3212ce2012c00b93768c4276187b7fdef727002278703ddfdb689295f25e07a0adc931c3cad7ad6683a5f5301b4494b66eb61f62d2259b3a05"+ }+ },+ {+ "request": {+ "op": "group_encrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ },+ "plaintext": "676f2067726f7570206f6e65"+ },+ "response": {+ "ciphertext": "33088a86fb820710011a10afe5d06eaf567f1632802c4df4602abc96c1cde6afe47279778cf5ad56d988519032fd776fcb7f3721b0d6580d4705e326ed3079ab94848ac4ab6c96bff579010f06b9a804dd08ecee050326e3011d07"+ }+ },+ {+ "request": {+ "op": "group_encrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ },+ "plaintext": "676f2067726f75702074776f"+ },+ "response": {+ "ciphertext": "33088a86fb820710021a107afbd4a9ed45968d8c2732712e1c38a4ff8363d4e4989b95475b0fda13e051addd8f0bf1021382b51b54f6c4f8a89c15a02d5ac6ec5545fd1796fb4f0762761ffb5611853ea2592f7ac37c56444a4305"+ }+ },+ {+ "request": {+ "op": "process_sender_key",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "message": "3308cfe58e800110001a208f480ffbdabac1c2f03e948c24f0a16d55c36dbdec0b6c9b9daf738a53519e2c2221059df7ad32c88f726557bc6224bedc7ce61a84a10036445b66032e1418adcc327b"+ },+ "response": {}+ },+ {+ "request": {+ "op": "group_decrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "ciphertext": "3308cfe58e800110001a1085adbd3bf6d5cffecd9a584f644fb788b1d330d3d807ccbc8e6ac445b5ad9a0770f03516daf34494d64e9e22929f1ab5bdd7b903677a46a70927f858056d9049e6d26244738444a6b567d001c25b9400"+ },+ "response": {+ "plaintext": "7275737420726f7461746564"+ }+ },+ {+ "request": {+ "op": "group_decrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "rust-alice",+ "device": 1+ },+ "ciphertext": "3308f9a7f5ad0410031a10603a2510e96648d2f9c9b8e9a32a2af685569d63fc25c996c84a43e8c21134aa39344f061bbe9c86a9dd6ca7eaf8ec408ddf992727fd05937113b40bfe14f0257a031948a6d52e07d6b552fd2ff4bb08"+ },+ "response": {+ "plaintext": "7275737420696e20666c69676874"+ }+ },+ {+ "request": {+ "op": "group_encrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ },+ "plaintext": "676f20696e20666c69676874"+ },+ "response": {+ "ciphertext": "33088a86fb820710031a107779500f81912cbda9e1da594b81e1a274badd21d69dd87e207b6a6d1dd51cf8bff17307470007a8561bfbb163d2283dd92428e96fb538890fcf6f51c13ca91de448fb845b315f5f130e3b03ba996f00"+ }+ },+ {+ "request": {+ "op": "delete_sender_key",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ }+ },+ "response": {}+ },+ {+ "request": {+ "op": "create_sender_key",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ }+ },+ "response": {+ "message": "3308c48aaaad0b10001a20c4bc8c17d5d83e3bd96e65a8964a17167f6621c59047ae19404e0e083b6cc0462221055a23bae95a990ea3cbfc5d393033940ecb145db5e288bdb5832a26d0f942250e"+ }+ },+ {+ "request": {+ "op": "group_encrypt",+ "device": "bob",+ "group": "[email protected]",+ "peer": {+ "name": "go-bob",+ "device": 2+ },+ "plaintext": "676f20726f7461746564"+ },+ "response": {+ "ciphertext": "3308c48aaaad0b10001a10bdae9581b52b4446b370126c81259684d272803707fd2381f28a05754e204b4c6303f9b0f1663a268ed4aebe86ea2c743395385754ead53ac62d9c1cbfd14b15fa099ac334b851e6a41a3af352413583"+ }+ }+]diff --git a/crates/whatsmeow_signal/tests/client_end_to_end.rs b/crates/whatsmeow_signal/tests/client_end_to_end.rsnew file mode 100644index 0000000000..29ecbd08aa--- /dev/null+++ b/crates/whatsmeow_signal/tests/client_end_to_end.rs@@ -0,0 +1,253 @@+//! The real whatsmeow `Client` running on [`Libsignal`] against the fake+//! WhatsApp server, whose phones run their own Signal sessions: direct and+//! group messages, both directions.+//!+//! The fake server signs its noise certificate with a test root key instead+//! of WhatsApp's, so [`TestRoot`] wraps [`Libsignal`] and checks signatures+//! by WhatsApp's root key against the test root. Every other call goes to+//! [`Libsignal`] unchanged.++use std::sync::Arc;+use std::time::Duration;++use async_trait::async_trait;+use tokio::sync::mpsc;+use tokio_util::sync::CancellationToken;+use whatsapp_fake_server::{FakePhone, FakeWhatsApp, test_root_key};+use whatsmeow::handshake::WA_CERT_PUB_KEY;+use whatsmeow::proto::wa_e2e::Message;+use whatsmeow::signal::{+ CiphertextMessage, CurveSignatures, PreKeyBundle, SenderKeyName, SignalAddress, SignalError,+ SignalProtocol, SignalProtocolStore,+};+use whatsmeow::store::MemoryStore;+use whatsmeow::types::Jid;+use whatsmeow::{Client, ClientConfig, Event, QrChannelItem};+use whatsmeow_signal::Libsignal;++const TIMEOUT: Duration = Duration::from_secs(20);++struct TestRoot {+ libsignal: Libsignal,+ root_public: [u8; 32],+}++impl CurveSignatures for TestRoot {+ fn calculate_signature(&self, private_key: &[u8; 32], message: &[u8]) -> [u8; 64] {+ self.libsignal.calculate_signature(private_key, message)+ }++ fn verify_signature(+ &self,+ public_key: &[u8; 32],+ message: &[u8],+ signature: &[u8; 64],+ ) -> bool {+ let public_key = if *public_key == WA_CERT_PUB_KEY {+ &self.root_public+ } else {+ public_key+ };+ self.libsignal+ .verify_signature(public_key, message, signature)+ }+}++#[async_trait]+impl SignalProtocol for TestRoot {+ async fn process_bundle(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ bundle: &PreKeyBundle,+ ) -> Result<(), SignalError> {+ self.libsignal.process_bundle(store, address, bundle).await+ }++ async fn encrypt(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ plaintext: &[u8],+ ) -> Result<CiphertextMessage, SignalError> {+ self.libsignal.encrypt(store, address, plaintext).await+ }++ async fn decrypt_pre_key_message(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.libsignal+ .decrypt_pre_key_message(store, address, ciphertext)+ .await+ }++ async fn decrypt_message(+ &self,+ store: &dyn SignalProtocolStore,+ address: &SignalAddress,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.libsignal+ .decrypt_message(store, address, ciphertext)+ .await+ }++ async fn create_sender_key_distribution_message(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ ) -> Result<Vec<u8>, SignalError> {+ self.libsignal+ .create_sender_key_distribution_message(store, name)+ .await+ }++ async fn process_sender_key_distribution_message(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ message: &[u8],+ ) -> Result<(), SignalError> {+ self.libsignal+ .process_sender_key_distribution_message(store, name, message)+ .await+ }++ async fn group_encrypt(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ plaintext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.libsignal.group_encrypt(store, name, plaintext).await+ }++ async fn group_decrypt(+ &self,+ store: &dyn SignalProtocolStore,+ name: &SenderKeyName,+ ciphertext: &[u8],+ ) -> Result<Vec<u8>, SignalError> {+ self.libsignal.group_decrypt(store, name, ciphertext).await+ }+}++fn text_message(text: &str) -> Message {+ Message {+ conversation: Some(text.to_owned()),+ ..Default::default()+ }+}++/// The next message event whose text is `text`.+async fn next_text(+ events: &mut mpsc::UnboundedReceiver<Event>,+ text: &str,+) -> whatsmeow::types::events::Message {+ tokio::time::timeout(TIMEOUT, async {+ loop {+ if let Event::Message(message) = events.recv().await.expect("the event channel is open")+ && message+ .message+ .as_ref()+ .and_then(|message| message.conversation.as_deref())+ == Some(text)+ {+ return *message;+ }+ }+ })+ .await+ .expect("the message arrives")+}++/// Links a client on [`Libsignal`] as a device of `phone`.+async fn link(+ server: &FakeWhatsApp,+ phone: &FakePhone,+) -> (Arc<Client>, mpsc::UnboundedReceiver<Event>, Jid) {+ let libsignal = Libsignal::new();+ let device = Arc::new(MemoryStore::default()).new_device(&libsignal);+ let signal = Arc::new(TestRoot {+ libsignal,+ root_public: test_root_key().public,+ });+ let config = ClientConfig {+ dialer: server.dialer(),+ ..ClientConfig::default()+ };+ let client = Client::new(device, signal, config);+ let mut events = client.subscribe();+ let mut qr = client+ .get_qr_channel(CancellationToken::new())+ .await+ .expect("the QR channel opens before connecting");+ client.connect().await.expect("the client connects");+ let Some(QrChannelItem::Code { code, .. }) = qr.recv().await else {+ panic!("expected a QR code");+ };+ let device = phone+ .scan_qr(&code)+ .await+ .expect("the phone approves the QR code");+ assert!(server.wait_until_online(&device, TIMEOUT).await);+ tokio::time::timeout(TIMEOUT, async {+ while !matches!(+ events.recv().await.expect("the event channel is open"),+ Event::Connected(_)+ ) {}+ })+ .await+ .expect("the client connects");+ assert!(server.wait_until_keys_uploaded(&device, TIMEOUT).await);+ (client, events, device)+}++#[tokio::test]+async fn a_libsignal_client_sends_and_receives_direct_and_group_messages() {+ let server = FakeWhatsApp::start().await.expect("the fake server starts");+ let alice = server.add_user("15550000001", "Alice").await;+ let bob = server.add_user("15550000002", "Bob").await;+ let carol = server.add_user("15550000003", "Carol").await;+ let (client, mut events, device) = link(&server, &alice).await;++ // 1:1: the client starts the session from bob's prekeys.+ let sent = client+ .send_message(&bob.jid(), text_message("hello bob"), Default::default())+ .await+ .expect("the server acks the message");+ let received = bob+ .wait_for_message(TIMEOUT, |message| message.id == sent.id)+ .await+ .expect("bob's phone decrypts the message");+ assert_eq!(received.text(), Some("hello bob"));+ let reply = bob.send_text(&alice.jid(), "hello alice").await.unwrap();+ assert_eq!(next_text(&mut events, "hello alice").await.info.id, reply);++ // Groups: receive under a phone's sender key, send under the client's.+ let group = server.create_group("Book club", &[&alice, &bob, &carol]);+ let incoming = carol.send_text(&group, "welcome").await.unwrap();+ let message = next_text(&mut events, "welcome").await;+ assert_eq!(message.info.id, incoming);+ assert_eq!(message.info.source.chat, group);+ for text in ["thanks", "again"] {+ let sent = client+ .send_message(&group, text_message(text), Default::default())+ .await+ .expect("the server acks the group message");+ for phone in [&alice, &bob, &carol] {+ let received = phone+ .wait_for_message(TIMEOUT, |message| message.id == sent.id)+ .await+ .expect("every member's phone decrypts the group message");+ assert_eq!(received.text(), Some(text));+ assert_eq!(received.sender.device, device.device);+ }+ }+ for phone in [&alice, &bob, &carol] {+ assert!(phone.failures().is_empty(), "{:?}", phone.failures());+ }+}diff --git a/crates/workspace-hack/Cargo.toml b/crates/workspace-hack/Cargo.tomlindex 188c1c7fbe..da66ea6e9a 100644--- a/crates/workspace-hack/Cargo.toml+++ b/crates/workspace-hack/Cargo.toml@@ -49,8 +49,9 @@ crypto-bigint = { version = "0.5", features = ["generic-array", "zeroize"] }crypto-common-6f8ce4dd05d13bba = { package = "crypto-common", version = "0.2", default-features = false, features = ["getrandom"] }crypto-common-c65f7effa3be6d31 = { package = "crypto-common", version = "0.1", default-features = false, features = ["getrandom", "std"] }ctutils = { version = "0.4", default-features = false, features = ["subtle"] }-curve25519-dalek = { version = "4", default-features = false, features = ["alloc", "digest", "precomputed-tables", "zeroize"] }-derive_more = { version = "2", features = ["add", "add_assign", "display", "from"] }+curve25519-dalek-164d15cefe24d7eb = { package = "curve25519-dalek", version = "4", default-features = false, features = ["alloc", "digest", "precomputed-tables", "zeroize"] }+curve25519-dalek-cdf1610d3e1514e9 = { package = "curve25519-dalek", version = "5", features = ["rand_core"] }+derive_more = { version = "2", features = ["add", "add_assign", "display", "from", "into", "try_from"] }digest-93f6ce9d446188ac = { package = "digest", version = "0.10", features = ["mac", "oid", "std"] }digest-a6292c17cd707f01 = { package = "digest", version = "0.11", features = ["alloc", "mac", "oid", "rand_core", "zeroize"] }doppleganger = { version = "0.5", default-features = false, features = ["chrono", "uuid"] }@@ -208,11 +209,13 @@ crypto-bigint = { version = "0.5", features = ["generic-array", "zeroize"] }crypto-common-6f8ce4dd05d13bba = { package = "crypto-common", version = "0.2", default-features = false, features = ["getrandom"] }crypto-common-c65f7effa3be6d31 = { package = "crypto-common", version = "0.1", default-features = false, features = ["getrandom", "std"] }ctutils = { version = "0.4", default-features = false, features = ["subtle"] }-curve25519-dalek = { version = "4", default-features = false, features = ["alloc", "digest", "precomputed-tables", "zeroize"] }-derive_more = { version = "2", features = ["add", "add_assign", "display", "from"] }-derive_more-impl = { version = "2", features = ["add", "add_assign", "display", "from"] }+curve25519-dalek-164d15cefe24d7eb = { package = "curve25519-dalek", version = "4", default-features = false, features = ["alloc", "digest", "precomputed-tables", "zeroize"] }+curve25519-dalek-cdf1610d3e1514e9 = { package = "curve25519-dalek", version = "5", features = ["rand_core"] }+derive_more = { version = "2", features = ["add", "add_assign", "display", "from", "into", "try_from"] }+derive_more-impl = { version = "2", features = ["add", "add_assign", "display", "from", "into", "try_from"] }digest-93f6ce9d446188ac = { package = "digest", version = "0.10", features = ["mac", "oid", "std"] }digest-a6292c17cd707f01 = { package = "digest", version = "0.11", features = ["alloc", "mac", "oid", "rand_core", "zeroize"] }+displaydoc = { version = "0.2" }doppleganger = { version = "0.5", default-features = false, features = ["chrono", "uuid"] }doppleganger-core = { version = "0.5", default-features = false, features = ["chrono", "uuid"] }either = { version = "1", features = ["serde", "use_std"] }diff --git a/deny.toml b/deny.tomlindex d4e74eb4ea..707664c3f0 100644--- a/deny.toml+++ b/deny.toml@@ -142,6 +142,9 @@ confidence-threshold = 0.8exceptions = [# JSON Schema URI parsing; the published license is MIT No Attribution.{ allow = ["MIT-0"], crate = "[email protected]" },+ # Signal's libsignal, used by whatsmeow_signal; AGPL like this repository.+ { allow = ["AGPL-3.0-only"], crate = "libsignal-core" },+ { allow = ["AGPL-3.0-only"], crate = "libsignal-debug" },# Each entry is the crate and version constraint, and its specific allow# list#{ allow = ["Zlib"], crate = "adler32" },diff --git a/nix-support/root-cargo-output-hashes.nix b/nix-support/root-cargo-output-hashes.nixindex 8a10f609ab..509afbcae9 100644--- a/nix-support/root-cargo-output-hashes.nix+++ b/nix-support/root-cargo-output-hashes.nix@@ -1,4 +1,6 @@{+ "git+https://github.com/signalapp/libsignal?tag=v0.105.2#b369755533ef5a3fae9b31b989114f22e074dfab" =+ "sha256-TPdxsLDH9ecoR1Cw2TROSLSd8XJdgauv3lmkf13YsKU=";"git+https://github.com/signalapp/tungstenite-rs?rev=a1837ccfe96b6895a57352d7f0112e4557516c27#a1837ccfe96b6895a57352d7f0112e4557516c27" ="sha256-yGGTDdIpvW+skch46bHAa201WolofJC5KKwCcPfDTVs=";"git+https://github.com/tursodatabase/turso?rev=79163249538197d01dec5ea7f65519454ed792e2#79163249538197d01dec5ea7f65519454ed792e2" =