From 49fa34e997771fe74170fa03153d5663ab011363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 29 Apr 2021 13:05:08 +0200 Subject: [PATCH] rust: Switch to the new encryption info branch of the rust-sdk --- rust-sdk/Cargo.toml | 5 +++-- rust-sdk/src/machine.rs | 39 +++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index 7f0ab29e30..c1c1d6b6a5 100644 --- a/rust-sdk/Cargo.toml +++ b/rust-sdk/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["cdylib", "lib"] name = "matrix_crypto" [dependencies] +serde = "1.0.64" serde_json = "1.0.64" http = "0.2.4" @@ -23,11 +24,11 @@ features = ["lax_deserialize"] [dependencies.matrix-sdk-common] git = "https://github.com/matrix-org/matrix-rust-sdk/" -branch = "corroded-android" +branch = "encryption-info-v2" [dependencies.matrix-sdk-crypto] git = "https://github.com/matrix-org/matrix-rust-sdk/" -branch = "corroded-android" +branch = "encryption-info-v2" features = ["sled_cryptostore"] [dependencies.tokio] diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 4edf15d79d..7e67befe05 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -4,7 +4,8 @@ use std::{ io::Cursor, }; -use serde_json::{json, value::RawValue}; +use serde::{Deserialize, Serialize}; +use serde_json::value::RawValue; use tokio::runtime::Runtime; use matrix_sdk_common::{ @@ -16,8 +17,11 @@ use matrix_sdk_common::{ sync::sync_events::{DeviceLists as RumaDeviceLists, ToDevice}, to_device::send_event_to_device::Response as ToDeviceResponse, }, - deserialized_responses::events::{AlgorithmInfo, SyncMessageEvent}, - events::{room::encrypted::EncryptedEventContent, AnyMessageEventContent, EventContent}, + deserialized_responses::AlgorithmInfo, + events::{ + room::encrypted::EncryptedEventContent, AnyMessageEventContent, EventContent, + SyncMessageEvent, + }, identifiers::{DeviceKeyAlgorithm, RoomId, UserId}, uuid::Uuid, IncomingResponse, UInt, @@ -47,8 +51,13 @@ pub struct Sas { pub request: Request, } +/// A pair of outgoing room key requests, both of those are sendToDevice +/// requests. pub struct KeyRequestPair { + /// The optional cancellation, this is None if no previous key request was + /// sent out for this key, thus it doesn't need to be cancelled. pub cancellation: Option, + /// The actual key request. pub key_request: Request, } @@ -228,7 +237,7 @@ impl OlmMachine { .runtime .block_on( self.inner - .receive_sync_changes(&events, &device_changes, &key_counts), + .receive_sync_changes(events, &device_changes, &key_counts), ) .unwrap(); @@ -398,6 +407,17 @@ impl OlmMachine { event: &str, room_id: &str, ) -> Result { + // Element Android wants only the content and the type and will create a + // decrypted event with those two itself, this struct makes sure we + // throw away all the other fields. + #[derive(Deserialize, Serialize)] + struct Event<'a> { + #[serde(rename = "type")] + event_type: String, + #[serde(borrow)] + content: &'a RawValue, + } + let event: SyncMessageEvent = serde_json::from_str(event)?; let room_id = RoomId::try_from(room_id)?; @@ -406,15 +426,10 @@ impl OlmMachine { .block_on(self.inner.decrypt_room_event(&event, &room_id))?; let encryption_info = decrypted - .encryption_info() + .encryption_info .expect("Decrypted event didn't contain any encryption info"); - let content = decrypted.content(); - - let clear_event = json!({ - "type": content.event_type(), - "content": content, - }); + let event_json: Event = serde_json::from_str(decrypted.event.json().get())?; Ok(match &encryption_info.algorithm_info { AlgorithmInfo::MegolmV1AesSha2 { @@ -422,7 +437,7 @@ impl OlmMachine { sender_claimed_keys, forwarding_curve25519_key_chain, } => DecryptedEvent { - clear_event: serde_json::to_string(&clear_event)?, + clear_event: serde_json::to_string(&event_json)?, sender_curve25519_key: curve25519_key.to_owned(), claimed_ed25519_key: sender_claimed_keys .get(&DeviceKeyAlgorithm::Ed25519)