crypto: Hook up the verification machine.

master
Damir Jelić 2020-07-28 15:37:20 +02:00
parent 7e95d85f17
commit a6fa9f99fd
3 changed files with 39 additions and 15 deletions

View File

@ -28,6 +28,7 @@ use super::store::memorystore::MemoryStore;
#[cfg(feature = "sqlite-cryptostore")] #[cfg(feature = "sqlite-cryptostore")]
use super::store::sqlite::SqliteStore; use super::store::sqlite::SqliteStore;
use super::{device::Device, store::Result as StoreResult, CryptoStore}; use super::{device::Device, store::Result as StoreResult, CryptoStore};
use crate::verification::VerificationMachine;
use matrix_sdk_common::events::{ use matrix_sdk_common::events::{
forwarded_room_key::ForwardedRoomKeyEventContent, room::encrypted::EncryptedEventContent, forwarded_room_key::ForwardedRoomKeyEventContent, room::encrypted::EncryptedEventContent,
@ -70,6 +71,9 @@ pub struct OlmMachine {
store: Arc<RwLock<Box<dyn CryptoStore>>>, store: Arc<RwLock<Box<dyn CryptoStore>>>,
/// The currently active outbound group sessions. /// The currently active outbound group sessions.
outbound_group_sessions: HashMap<RoomId, OutboundGroupSession>, outbound_group_sessions: HashMap<RoomId, OutboundGroupSession>,
/// A state machine that is responsible to handle and keep track of SAS
/// verification flows.
verification_machine: VerificationMachine,
} }
// #[cfg_attr(tarpaulin, skip)] // #[cfg_attr(tarpaulin, skip)]
@ -97,12 +101,17 @@ impl OlmMachine {
/// * `device_id` - The unique id of the device that owns this machine. /// * `device_id` - The unique id of the device that owns this machine.
#[allow(clippy::ptr_arg)] #[allow(clippy::ptr_arg)]
pub fn new(user_id: &UserId, device_id: &DeviceId) -> Self { pub fn new(user_id: &UserId, device_id: &DeviceId) -> Self {
let store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
let store = Arc::new(RwLock::new(store));
let account = Account::new(user_id, device_id);
OlmMachine { OlmMachine {
user_id: user_id.clone(), user_id: user_id.clone(),
device_id: device_id.into(), device_id: device_id.into(),
account: Account::new(user_id, &device_id), account: account.clone(),
store: Arc::new(RwLock::new(Box::new(MemoryStore::new()))), store: store.clone(),
outbound_group_sessions: HashMap::new(), 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 { Ok(OlmMachine {
user_id, user_id,
device_id, device_id,
account, account,
store: Arc::new(RwLock::new(store)), store,
outbound_group_sessions: HashMap::new(), outbound_group_sessions: HashMap::new(),
verification_machine,
}) })
} }
@ -1048,8 +1061,10 @@ impl OlmMachine {
// TODO handle room key requests here. // TODO handle room key requests here.
} }
fn handle_verification_event(&self, _: &AnyToDeviceEvent) { async fn handle_verification_event(&self, mut event: &mut AnyToDeviceEvent) {
// TODO handle to-device verification events here. 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. /// 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 { 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 e
} else { } else {
// Skip invalid events. // Skip invalid events.
@ -1088,7 +1103,7 @@ impl OlmMachine {
info!("Received a to-device event {:?}", event); info!("Received a to-device event {:?}", event);
match &event { match &mut event {
AnyToDeviceEvent::RoomEncrypted(e) => { AnyToDeviceEvent::RoomEncrypted(e) => {
let decrypted_event = match self.decrypt_to_device_event(e).await { let decrypted_event = match self.decrypt_to_device_event(e).await {
Ok(e) => e, Ok(e) => e,
@ -1112,7 +1127,7 @@ impl OlmMachine {
| AnyToDeviceEvent::KeyVerificationMac(..) | AnyToDeviceEvent::KeyVerificationMac(..)
| AnyToDeviceEvent::KeyVerificationRequest(..) | AnyToDeviceEvent::KeyVerificationRequest(..)
| AnyToDeviceEvent::KeyVerificationStart(..) => { | AnyToDeviceEvent::KeyVerificationStart(..) => {
self.handle_verification_event(&event) self.handle_verification_event(&mut event).await;
} }
_ => continue, _ => continue,
} }

View File

@ -24,6 +24,7 @@ use matrix_sdk_common::{
EventType, EventType,
}, },
identifiers::{DeviceId, UserId}, identifiers::{DeviceId, UserId},
locks::RwLock,
uuid::Uuid, uuid::Uuid,
}; };
@ -31,18 +32,18 @@ use super::Sas;
use crate::{Account, CryptoStore, CryptoStoreError}; use crate::{Account, CryptoStore, CryptoStoreError};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct VerificationMachine { pub struct VerificationMachine {
account: Account, account: Account,
store: Arc<Box<dyn CryptoStore>>, store: Arc<RwLock<Box<dyn CryptoStore>>>,
verifications: Arc<DashMap<String, Sas>>, verifications: Arc<DashMap<String, Sas>>,
outgoing_to_device_messages: Arc<DashMap<String, ToDeviceRequest>>, outgoing_to_device_messages: Arc<DashMap<String, ToDeviceRequest>>,
} }
impl VerificationMachine { impl VerificationMachine {
pub(crate) fn new(account: Account, store: Box<dyn CryptoStore>) -> Self { pub(crate) fn new(account: Account, store: Arc<RwLock<Box<dyn CryptoStore>>>) -> Self {
Self { Self {
account, account,
store: Arc::new(store), store,
verifications: Arc::new(DashMap::new()), verifications: Arc::new(DashMap::new()),
outgoing_to_device_messages: 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); 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 { match event {
AnyToDeviceEvent::KeyVerificationStart(e) => match &e.content { AnyToDeviceEvent::KeyVerificationStart(e) => match &e.content {
StartEventContent::MSasV1(content) => { StartEventContent::MSasV1(content) => {
if let Some(d) = self if let Some(d) = self
.store .store
.read()
.await
.get_device(&e.sender, &content.from_device) .get_device(&e.sender, &content.from_device)
.await? .await?
{ {
@ -143,10 +149,12 @@ impl VerificationMachine {
mod test { mod test {
use std::convert::TryFrom; use std::convert::TryFrom;
use std::sync::Arc;
use matrix_sdk_common::{ use matrix_sdk_common::{
events::AnyToDeviceEventContent, events::AnyToDeviceEventContent,
identifiers::{DeviceId, UserId}, identifiers::{DeviceId, UserId},
locks::RwLock,
}; };
use super::{Sas, VerificationMachine}; use super::{Sas, VerificationMachine};
@ -179,7 +187,7 @@ mod test {
store.save_devices(&[bob_device]).await.unwrap(); 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); let (bob_sas, start_content) = Sas::start(bob, alice_device);
machine machine
.receive_event(&mut wrap_any_to_device_content( .receive_event(&mut wrap_any_to_device_content(
@ -196,7 +204,7 @@ mod test {
fn create() { fn create() {
let alice = Account::new(&alice_id(), &alice_device_id()); let alice = Account::new(&alice_id(), &alice_device_id());
let store = MemoryStore::new(); let store = MemoryStore::new();
let _ = VerificationMachine::new(alice, Box::new(store)); let _ = VerificationMachine::new(alice, Arc::new(RwLock::new(Box::new(store))));
} }
#[tokio::test] #[tokio::test]

View File

@ -28,6 +28,7 @@ mod machine;
#[allow(dead_code)] #[allow(dead_code)]
mod sas; mod sas;
pub use machine::VerificationMachine;
pub use sas::Sas; pub use sas::Sas;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]