crypto: Expose some more SAS info publicly.

This commit is contained in:
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 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()
}
}

View file

@ -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);
}
}
}
};
}
_ => (),

View file

@ -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<bool, CryptoStoreError> {
pub(crate) async fn mark_device_as_verified(&self) -> Result<bool, CryptoStoreError> {
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<String> {
match self {
InnerSas::Created(s) => s.verification_flow_id.clone(),