From df9da7539a7bf0ab473269dc398c33526e35d49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 3 Aug 2020 14:33:15 +0200 Subject: [PATCH] crypto: Expose some more SAS info publicly. --- matrix_sdk/src/sas.rs | 17 +++++++- matrix_sdk_crypto/src/verification/machine.rs | 40 ++++++++++++++++--- matrix_sdk_crypto/src/verification/sas/mod.rs | 21 +++++++++- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/matrix_sdk/src/sas.rs b/matrix_sdk/src/sas.rs index d9f700d8..2f995ef9 100644 --- a/matrix_sdk/src/sas.rs +++ b/matrix_sdk/src/sas.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use url::Url; -use matrix_sdk_base::{Sas as BaseSas, Session}; +use matrix_sdk_base::{Device, Sas as BaseSas, Session}; use matrix_sdk_common::locks::RwLock; use crate::{error::Result, http_client::HttpClient}; @@ -66,4 +66,19 @@ impl Sas { pub fn decimals(&self) -> Option<(u32, u32, u32)> { self.inner.decimals() } + + /// Is the verification process done. + pub fn is_done(&self) -> bool { + self.inner.is_done() + } + + /// Is the verification process canceled. + pub fn is_canceled(&self) -> bool { + self.inner.is_canceled() + } + + /// Get the other users device that we're veryfying. + pub fn other_device(&self) -> Device { + self.inner.other_device() + } } diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index c9573980..5c1eb64b 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -16,6 +16,8 @@ use std::sync::Arc; use dashmap::DashMap; +use tracing::{trace, warn}; + use matrix_sdk_common::{ api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, events::{AnyToDeviceEvent, AnyToDeviceEventContent}, @@ -61,7 +63,7 @@ impl VerificationMachine { .insert(request.txn_id.clone(), request); } - fn recieve_event_helper(&self, sas: &Sas, event: &mut AnyToDeviceEvent) { + fn receive_event_helper(&self, sas: &Sas, event: &mut AnyToDeviceEvent) { if let Some(c) = sas.receive_event(event) { self.queue_up_content(sas.other_user_id(), sas.other_device_id(), c); } @@ -83,8 +85,16 @@ impl VerificationMachine { &self, event: &mut AnyToDeviceEvent, ) -> Result<(), CryptoStoreError> { + trace!("Received a key verification event {:?}", event); + match event { AnyToDeviceEvent::KeyVerificationStart(e) => { + trace!( + "Received a m.key.verification start event from {} {}", + e.sender, + e.content.from_device + ); + if let Some(d) = self .store .read() @@ -97,26 +107,44 @@ impl VerificationMachine { self.verifications .insert(e.content.transaction_id.clone(), s); } - Err(c) => self.queue_up_content(&e.sender, &e.content.from_device, c), + Err(c) => { + warn!( + "Can't start key verification with {} {}, canceling: {:?}", + e.sender, e.content.from_device, c + ); + self.queue_up_content(&e.sender, &e.content.from_device, c) + } } - }; + } else { + warn!( + "Received a key verification start event from an unknown device {} {}", + e.sender, e.content.from_device + ); + } } AnyToDeviceEvent::KeyVerificationCancel(e) => { self.verifications.remove(&e.content.transaction_id); } AnyToDeviceEvent::KeyVerificationAccept(e) => { if let Some(s) = self.get_sas(&e.content.transaction_id) { - self.recieve_event_helper(&s, event) + self.receive_event_helper(&s, event) }; } AnyToDeviceEvent::KeyVerificationKey(e) => { if let Some(s) = self.get_sas(&e.content.transaction_id) { - self.recieve_event_helper(&s, event) + self.receive_event_helper(&s, event) }; } AnyToDeviceEvent::KeyVerificationMac(e) => { if let Some(s) = self.get_sas(&e.content.transaction_id) { - self.recieve_event_helper(&s, event) + self.receive_event_helper(&s, event); + if s.is_done() { + if !s.mark_device_as_verified().await? { + if let Some(r) = s.cancel() { + self.outgoing_to_device_messages.insert(r.txn_id.clone(), r); + } + } + } }; } _ => (), diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index 7c9b793e..b1d99f7b 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -16,6 +16,7 @@ mod helpers; mod sas_state; use std::sync::{Arc, Mutex}; +use tracing::{info, trace, warn}; use matrix_sdk_common::{ api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, @@ -67,6 +68,11 @@ impl Sas { self.other_device.device_id() } + /// Get the device of the other user. + pub fn other_device(&self) -> Device { + self.other_device.clone() + } + /// Get the unique ID that identifies this SAS verification flow. pub fn flow_id(&self) -> &str { &self.flow_id @@ -162,7 +168,7 @@ impl Sas { })) } - async fn mark_device_as_verified(&self) -> Result { + pub(crate) async fn mark_device_as_verified(&self) -> Result { let device = self .store .read() @@ -208,6 +214,11 @@ impl Sas { self.inner.lock().unwrap().is_done() } + /// Is the SAS flow done. + pub fn is_canceled(&self) -> bool { + self.inner.lock().unwrap().is_canceled() + } + /// Get the emoji version of the short auth string. /// /// Returns None if we can't yet present the short auth string, otherwise a @@ -399,6 +410,14 @@ impl InnerSas { } } + fn is_canceled(&self) -> bool { + if let InnerSas::Canceled(_) = self { + true + } else { + false + } + } + fn verification_flow_id(&self) -> Arc { match self { InnerSas::Created(s) => s.verification_flow_id.clone(),