crypto: Pass the user identity to the SAS object when doing verifications.

This commit is contained in:
Damir Jelić 2020-08-18 13:37:02 +02:00
parent f96437a242
commit 6d0b73cb3d
5 changed files with 39 additions and 12 deletions

View file

@ -61,7 +61,7 @@ impl Device {
/// # });
/// ```
pub async fn start_verification(&self) -> Result<Sas> {
let (sas, request) = self.inner.start_verification();
let (sas, request) = self.inner.start_verification().await?;
let request = ToDeviceRequest {
event_type: request.event_type,
txn_id: &request.txn_id,

View file

@ -72,8 +72,10 @@ impl Device {
/// Start a interactive verification with this `Device`
///
/// Returns a `Sas` object and to-device request that needs to be sent out.
pub fn start_verification(&self) -> (Sas, OwnedToDeviceRequest) {
self.verification_machine.start_sas(self.inner.clone())
pub async fn start_verification(&self) -> StoreResult<(Sas, OwnedToDeviceRequest)> {
self.verification_machine
.start_sas(self.inner.clone())
.await
}
/// Set the trust state of the device to the given state.

View file

@ -2063,7 +2063,7 @@ pub(crate) mod test {
assert!(!bob_device.is_trusted());
let (alice_sas, request) = bob_device.start_verification();
let (alice_sas, request) = bob_device.start_verification().await.unwrap();
let mut event = request_to_event(alice.user_id(), &request);
bob.handle_verification_event(&mut event).await;

View file

@ -45,8 +45,19 @@ impl VerificationMachine {
}
}
pub fn start_sas(&self, device: ReadOnlyDevice) -> (Sas, OwnedToDeviceRequest) {
let (sas, content) = Sas::start(self.account.clone(), device.clone(), self.store.clone());
pub async fn start_sas(
&self,
device: ReadOnlyDevice,
) -> Result<(Sas, OwnedToDeviceRequest), CryptoStoreError> {
let identity = self.store.get_user_identity(device.user_id()).await?;
let (sas, content) = Sas::start(
self.account.clone(),
device.clone(),
self.store.clone(),
identity,
);
let request = content_to_request(
device.user_id(),
device.device_id(),
@ -56,7 +67,7 @@ impl VerificationMachine {
self.verifications
.insert(sas.flow_id().to_owned(), sas.clone());
(sas, request)
Ok((sas, request))
}
pub fn get_sas(&self, transaction_id: &str) -> Option<Sas> {
@ -128,7 +139,13 @@ impl VerificationMachine {
.get_device(&e.sender, &e.content.from_device)
.await?
{
match Sas::from_start_event(self.account.clone(), d, self.store.clone(), e) {
match Sas::from_start_event(
self.account.clone(),
d,
self.store.clone(),
e,
None,
) {
Ok(s) => {
self.verifications
.insert(e.content.transaction_id.clone(), s);
@ -231,7 +248,7 @@ mod test {
.unwrap();
let machine = VerificationMachine::new(alice, Arc::new(Box::new(store)));
let (bob_sas, start_content) = Sas::start(bob, alice_device, bob_store);
let (bob_sas, start_content) = Sas::start(bob, alice_device, bob_store, None);
machine
.receive_event(&mut wrap_any_to_device_content(
bob_sas.user_id(),

View file

@ -33,7 +33,10 @@ use matrix_sdk_common::{
identifiers::{DeviceId, UserId},
};
use crate::{Account, CryptoStore, CryptoStoreError, ReadOnlyDevice, TrustState};
use crate::{
user_identity::UserIdentities, Account, CryptoStore, CryptoStoreError, ReadOnlyDevice,
TrustState,
};
pub use helpers::content_to_request;
use sas_state::{
@ -47,6 +50,7 @@ pub struct Sas {
store: Arc<Box<dyn CryptoStore>>,
account: Account,
other_device: ReadOnlyDevice,
other_identity: Option<UserIdentities>,
flow_id: Arc<String>,
}
@ -101,6 +105,7 @@ impl Sas {
account: Account,
other_device: ReadOnlyDevice,
store: Arc<Box<dyn CryptoStore>>,
other_identity: Option<UserIdentities>,
) -> (Sas, StartEventContent) {
let (inner, content) = InnerSas::start(account.clone(), other_device.clone());
let flow_id = inner.verification_flow_id();
@ -111,6 +116,7 @@ impl Sas {
store,
other_device,
flow_id,
other_identity,
};
(sas, content)
@ -131,6 +137,7 @@ impl Sas {
other_device: ReadOnlyDevice,
store: Arc<Box<dyn CryptoStore>>,
event: &ToDeviceEvent<StartEventContent>,
other_identity: Option<UserIdentities>,
) -> Result<Sas, AnyToDeviceEventContent> {
let inner = InnerSas::from_start_event(account.clone(), other_device.clone(), event)?;
let flow_id = inner.verification_flow_id();
@ -138,6 +145,7 @@ impl Sas {
inner: Arc::new(Mutex::new(inner)),
account,
other_device,
other_identity,
store,
flow_id,
})
@ -683,10 +691,10 @@ mod test {
.await
.unwrap();
let (alice, content) = Sas::start(alice, bob_device, alice_store);
let (alice, content) = Sas::start(alice, bob_device, alice_store, None);
let event = wrap_to_device_event(alice.user_id(), content);
let bob = Sas::from_start_event(bob, alice_device, bob_store, &event).unwrap();
let bob = Sas::from_start_event(bob, alice_device, bob_store, &event, None).unwrap();
let mut event = wrap_any_to_device_content(
bob.user_id(),
get_content_from_request(&bob.accept().unwrap()),