From a6fa9f99fd8bd9cdbd7f2ffcab9a68ae205ba654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 28 Jul 2020 15:37:20 +0200 Subject: [PATCH] crypto: Hook up the verification machine. --- matrix_sdk_crypto/src/machine.rs | 31 ++++++++++++++----- matrix_sdk_crypto/src/verification/machine.rs | 22 ++++++++----- matrix_sdk_crypto/src/verification/mod.rs | 1 + 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 6597499a..1b490bdc 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -28,6 +28,7 @@ use super::store::memorystore::MemoryStore; #[cfg(feature = "sqlite-cryptostore")] use super::store::sqlite::SqliteStore; use super::{device::Device, store::Result as StoreResult, CryptoStore}; +use crate::verification::VerificationMachine; use matrix_sdk_common::events::{ forwarded_room_key::ForwardedRoomKeyEventContent, room::encrypted::EncryptedEventContent, @@ -70,6 +71,9 @@ pub struct OlmMachine { store: Arc>>, /// The currently active outbound group sessions. outbound_group_sessions: HashMap, + /// A state machine that is responsible to handle and keep track of SAS + /// verification flows. + verification_machine: VerificationMachine, } // #[cfg_attr(tarpaulin, skip)] @@ -97,12 +101,17 @@ impl OlmMachine { /// * `device_id` - The unique id of the device that owns this machine. #[allow(clippy::ptr_arg)] pub fn new(user_id: &UserId, device_id: &DeviceId) -> Self { + let store: Box = Box::new(MemoryStore::new()); + let store = Arc::new(RwLock::new(store)); + let account = Account::new(user_id, device_id); + OlmMachine { user_id: user_id.clone(), device_id: device_id.into(), - account: Account::new(user_id, &device_id), - store: Arc::new(RwLock::new(Box::new(MemoryStore::new()))), + account: account.clone(), + store: store.clone(), outbound_group_sessions: HashMap::new(), + verification_machine: VerificationMachine::new(account, store), } } @@ -139,12 +148,16 @@ impl OlmMachine { } }; + let store = Arc::new(RwLock::new(store)); + let verification_machine = VerificationMachine::new(account.clone(), store.clone()); + Ok(OlmMachine { user_id, device_id, account, - store: Arc::new(RwLock::new(store)), + store, outbound_group_sessions: HashMap::new(), + verification_machine, }) } @@ -1048,8 +1061,10 @@ impl OlmMachine { // TODO handle room key requests here. } - fn handle_verification_event(&self, _: &AnyToDeviceEvent) { - // TODO handle to-device verification events here. + async fn handle_verification_event(&self, mut event: &mut AnyToDeviceEvent) { + if let Err(e) = self.verification_machine.receive_event(&mut event).await { + error!("Error handling a verification event: {:?}", e); + } } /// Handle a sync response and update the internal state of the Olm machine. @@ -1078,7 +1093,7 @@ impl OlmMachine { } for event_result in &mut response.to_device.events { - let event = if let Ok(e) = event_result.deserialize() { + let mut event = if let Ok(e) = event_result.deserialize() { e } else { // Skip invalid events. @@ -1088,7 +1103,7 @@ impl OlmMachine { info!("Received a to-device event {:?}", event); - match &event { + match &mut event { AnyToDeviceEvent::RoomEncrypted(e) => { let decrypted_event = match self.decrypt_to_device_event(e).await { Ok(e) => e, @@ -1112,7 +1127,7 @@ impl OlmMachine { | AnyToDeviceEvent::KeyVerificationMac(..) | AnyToDeviceEvent::KeyVerificationRequest(..) | AnyToDeviceEvent::KeyVerificationStart(..) => { - self.handle_verification_event(&event) + self.handle_verification_event(&mut event).await; } _ => continue, } diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 532c8eb0..8b03349b 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -24,6 +24,7 @@ use matrix_sdk_common::{ EventType, }, identifiers::{DeviceId, UserId}, + locks::RwLock, uuid::Uuid, }; @@ -31,18 +32,18 @@ use super::Sas; use crate::{Account, CryptoStore, CryptoStoreError}; #[derive(Clone, Debug)] -struct VerificationMachine { +pub struct VerificationMachine { account: Account, - store: Arc>, + store: Arc>>, verifications: Arc>, outgoing_to_device_messages: Arc>, } impl VerificationMachine { - pub(crate) fn new(account: Account, store: Box) -> Self { + pub(crate) fn new(account: Account, store: Arc>>) -> Self { Self { account, - store: Arc::new(store), + store, verifications: Arc::new(DashMap::new()), outgoing_to_device_messages: Arc::new(DashMap::new()), } @@ -96,12 +97,17 @@ impl VerificationMachine { self.outgoing_to_device_messages.remove(uuid); } - async fn receive_event(&self, event: &mut AnyToDeviceEvent) -> Result<(), CryptoStoreError> { + pub async fn receive_event( + &self, + event: &mut AnyToDeviceEvent, + ) -> Result<(), CryptoStoreError> { match event { AnyToDeviceEvent::KeyVerificationStart(e) => match &e.content { StartEventContent::MSasV1(content) => { if let Some(d) = self .store + .read() + .await .get_device(&e.sender, &content.from_device) .await? { @@ -143,10 +149,12 @@ impl VerificationMachine { mod test { use std::convert::TryFrom; + use std::sync::Arc; use matrix_sdk_common::{ events::AnyToDeviceEventContent, identifiers::{DeviceId, UserId}, + locks::RwLock, }; use super::{Sas, VerificationMachine}; @@ -179,7 +187,7 @@ mod test { store.save_devices(&[bob_device]).await.unwrap(); - let machine = VerificationMachine::new(alice, Box::new(store)); + let machine = VerificationMachine::new(alice, Arc::new(RwLock::new(Box::new(store)))); let (bob_sas, start_content) = Sas::start(bob, alice_device); machine .receive_event(&mut wrap_any_to_device_content( @@ -196,7 +204,7 @@ mod test { fn create() { let alice = Account::new(&alice_id(), &alice_device_id()); let store = MemoryStore::new(); - let _ = VerificationMachine::new(alice, Box::new(store)); + let _ = VerificationMachine::new(alice, Arc::new(RwLock::new(Box::new(store)))); } #[tokio::test] diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index 7675c756..27390940 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -28,6 +28,7 @@ mod machine; #[allow(dead_code)] mod sas; +pub use machine::VerificationMachine; pub use sas::Sas; #[derive(Clone, Debug)]