diff --git a/matrix_sdk_crypto/src/verification/sas.rs b/matrix_sdk_crypto/src/verification/sas.rs index 2f0f896c..9be5cb01 100644 --- a/matrix_sdk_crypto/src/verification/sas.rs +++ b/matrix_sdk_crypto/src/verification/sas.rs @@ -103,7 +103,7 @@ impl Sas { account: Account, other_device: Device, event: &ToDeviceEvent, - ) -> Result { + ) -> Result { let inner = InnerSas::from_start_event(account.clone(), other_device.clone(), event)?; Ok(Sas { inner: Arc::new(Mutex::new(inner)), @@ -177,7 +177,7 @@ impl InnerSas { account: Account, other_device: Device, event: &ToDeviceEvent, - ) -> Result { + ) -> Result { match SasState::::from_start_event(account, other_device, event) { Ok(s) => Ok(InnerSas::Started(s)), Err(s) => Err(s.as_content()), @@ -224,8 +224,7 @@ impl InnerSas { ) } Err(s) => { - let content = - AnyToDeviceEventContent::KeyVerificationCancel(s.as_content()); + let content = s.as_content(); (InnerSas::Canceled(s), Some(content)) } } @@ -236,7 +235,10 @@ impl InnerSas { AnyToDeviceEvent::KeyVerificationKey(e) => match self { InnerSas::Accepted(s) => match s.into_key_received(e) { Ok(s) => (InnerSas::KeyRecieved(s), None), - Err(s) => (InnerSas::Canceled(s), None), + Err(s) => { + let content = s.as_content(); + (InnerSas::Canceled(s), Some(content)) + } }, InnerSas::Started(s) => match s.into_key_received(e) { Ok(s) => { @@ -246,18 +248,27 @@ impl InnerSas { Some(AnyToDeviceEventContent::KeyVerificationKey(content)), ) } - Err(s) => (InnerSas::Canceled(s), None), + Err(s) => { + let content = s.as_content(); + (InnerSas::Canceled(s), Some(content)) + } }, _ => (self, None), }, AnyToDeviceEvent::KeyVerificationMac(e) => match self { InnerSas::KeyRecieved(s) => match s.into_mac_received(e) { Ok(s) => (InnerSas::MacReceived(s), None), - Err(s) => (InnerSas::Canceled(s), None), + Err(s) => { + let content = s.as_content(); + (InnerSas::Canceled(s), Some(content)) + } }, InnerSas::Confirmed(s) => match s.into_done(e) { Ok(s) => (InnerSas::Done(s), None), - Err(s) => (InnerSas::Canceled(s), None), + Err(s) => { + let content = s.as_content(); + (InnerSas::Canceled(s), Some(content)) + } }, _ => (self, None), }, @@ -337,10 +348,6 @@ impl From for AcceptedProtocols { } } -// TODO each of our state transitions can fail and return a canceled state. We -// need to check the senders at each transition, the commitment, the -// verification flow id (transaction id). - /// A type level state machine modeling the Sas flow. /// /// This is the generic struc holding common data between the different states @@ -708,6 +715,9 @@ impl SasState { self, event: &mut ToDeviceEvent, ) -> Result, SasState> { + self.check_sender_and_txid(&event.sender, &event.content.transaction_id) + .map_err(|c| self.clone().cancel(c))?; + let utility = OlmUtility::new(); let commitment = utility.sha256_utf8_msg(&format!( "{}{}", @@ -975,12 +985,12 @@ impl Canceled { } impl SasState { - fn as_content(&self) -> CancelEventContent { - CancelEventContent { + fn as_content(&self) -> AnyToDeviceEventContent { + AnyToDeviceEventContent::KeyVerificationCancel(CancelEventContent { transaction_id: self.verification_flow_id.to_string(), reason: self.state.reason.to_string(), code: self.state.cancel_code.clone(), - } + }) } }