crypto: Expose some more SAS info publicly.

master
Damir Jelić 2020-08-03 14:33:15 +02:00
parent 1787d2ebe6
commit df9da7539a
3 changed files with 70 additions and 8 deletions

View File

@ -16,7 +16,7 @@ use std::sync::Arc;
use url::Url; 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 matrix_sdk_common::locks::RwLock;
use crate::{error::Result, http_client::HttpClient}; use crate::{error::Result, http_client::HttpClient};
@ -66,4 +66,19 @@ impl Sas {
pub fn decimals(&self) -> Option<(u32, u32, u32)> { pub fn decimals(&self) -> Option<(u32, u32, u32)> {
self.inner.decimals() 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()
}
} }

View File

@ -16,6 +16,8 @@ use std::sync::Arc;
use dashmap::DashMap; use dashmap::DashMap;
use tracing::{trace, warn};
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, api::r0::to_device::send_event_to_device::Request as ToDeviceRequest,
events::{AnyToDeviceEvent, AnyToDeviceEventContent}, events::{AnyToDeviceEvent, AnyToDeviceEventContent},
@ -61,7 +63,7 @@ impl VerificationMachine {
.insert(request.txn_id.clone(), request); .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) { if let Some(c) = sas.receive_event(event) {
self.queue_up_content(sas.other_user_id(), sas.other_device_id(), c); self.queue_up_content(sas.other_user_id(), sas.other_device_id(), c);
} }
@ -83,8 +85,16 @@ impl VerificationMachine {
&self, &self,
event: &mut AnyToDeviceEvent, event: &mut AnyToDeviceEvent,
) -> Result<(), CryptoStoreError> { ) -> Result<(), CryptoStoreError> {
trace!("Received a key verification event {:?}", event);
match event { match event {
AnyToDeviceEvent::KeyVerificationStart(e) => { AnyToDeviceEvent::KeyVerificationStart(e) => {
trace!(
"Received a m.key.verification start event from {} {}",
e.sender,
e.content.from_device
);
if let Some(d) = self if let Some(d) = self
.store .store
.read() .read()
@ -97,26 +107,44 @@ impl VerificationMachine {
self.verifications self.verifications
.insert(e.content.transaction_id.clone(), s); .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) => { AnyToDeviceEvent::KeyVerificationCancel(e) => {
self.verifications.remove(&e.content.transaction_id); self.verifications.remove(&e.content.transaction_id);
} }
AnyToDeviceEvent::KeyVerificationAccept(e) => { AnyToDeviceEvent::KeyVerificationAccept(e) => {
if let Some(s) = self.get_sas(&e.content.transaction_id) { 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) => { AnyToDeviceEvent::KeyVerificationKey(e) => {
if let Some(s) = self.get_sas(&e.content.transaction_id) { 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) => { AnyToDeviceEvent::KeyVerificationMac(e) => {
if let Some(s) = self.get_sas(&e.content.transaction_id) { 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);
}
}
}
}; };
} }
_ => (), _ => (),

View File

@ -16,6 +16,7 @@ mod helpers;
mod sas_state; mod sas_state;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use tracing::{info, trace, warn};
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, api::r0::to_device::send_event_to_device::Request as ToDeviceRequest,
@ -67,6 +68,11 @@ impl Sas {
self.other_device.device_id() 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. /// Get the unique ID that identifies this SAS verification flow.
pub fn flow_id(&self) -> &str { pub fn flow_id(&self) -> &str {
&self.flow_id &self.flow_id
@ -162,7 +168,7 @@ impl Sas {
})) }))
} }
async fn mark_device_as_verified(&self) -> Result<bool, CryptoStoreError> { pub(crate) async fn mark_device_as_verified(&self) -> Result<bool, CryptoStoreError> {
let device = self let device = self
.store .store
.read() .read()
@ -208,6 +214,11 @@ impl Sas {
self.inner.lock().unwrap().is_done() 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. /// Get the emoji version of the short auth string.
/// ///
/// Returns None if we can't yet present the short auth string, otherwise a /// 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<String> { fn verification_flow_id(&self) -> Arc<String> {
match self { match self {
InnerSas::Created(s) => s.verification_flow_id.clone(), InnerSas::Created(s) => s.verification_flow_id.clone(),