From ee6b804804215276f35c56afc34093ef2a134355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 28 Jun 2021 15:28:30 +0200 Subject: [PATCH] crypto: Allow QR code verifications to get into the cancelled state as well. --- matrix_sdk_crypto/src/verification/machine.rs | 13 ++++++-- matrix_sdk_crypto/src/verification/qrcode.rs | 31 ++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 494f9b65..a2a4abab 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -287,9 +287,16 @@ impl VerificationMachine { verification.receive_cancel(event.sender(), c); } - if let Some(sas) = self.get_sas(event.sender(), flow_id.as_str()) { - // This won't produce an outgoing content - let _ = sas.receive_any_event(event.sender(), &content); + if let Some(verification) = + self.get_verification(event.sender(), flow_id.as_str()) + { + match verification { + Verification::SasV1(sas) => { + // This won't produce an outgoing content + let _ = sas.receive_any_event(event.sender(), &content); + } + Verification::QrV1(qr) => qr.receive_cancel(event.sender(), c), + } } } AnyVerificationContent::Ready(c) => { diff --git a/matrix_sdk_crypto/src/verification/qrcode.rs b/matrix_sdk_crypto/src/verification/qrcode.rs index b1e7adfd..0dd4b576 100644 --- a/matrix_sdk_crypto/src/verification/qrcode.rs +++ b/matrix_sdk_crypto/src/verification/qrcode.rs @@ -36,9 +36,10 @@ use ruma::{ DeviceId, DeviceIdBox, DeviceKeyAlgorithm, RoomId, UserId, }; use thiserror::Error; +use tracing::trace; use super::{ - event_enums::{DoneContent, OutgoingContent, OwnedStartContent, StartContent}, + event_enums::{CancelContent, DoneContent, OutgoingContent, OwnedStartContent, StartContent}, Cancelled, Done, FlowId, IdentitiesBeingVerified, VerificationResult, }; use crate::{ @@ -393,6 +394,28 @@ impl QrVerification { } } + pub(crate) fn receive_cancel(&self, sender: &UserId, content: &CancelContent<'_>) { + if sender == self.other_user_id() { + let mut state = self.state.lock().unwrap(); + + let new_state = match &*state { + InnerState::Created(s) => s.clone().into_cancelled(content), + InnerState::Scanned(s) => s.clone().into_cancelled(content), + InnerState::Confirmed(s) => s.clone().into_cancelled(content), + InnerState::Reciprocated(s) => s.clone().into_cancelled(content), + InnerState::Done(_) | InnerState::Cancelled(_) => return, + }; + + trace!( + sender = sender.as_str(), + code = content.cancel_code().as_str(), + "Cancelling a QR verification, other user has cancelled" + ); + + *state = InnerState::Cancelled(new_state); + } + } + fn generate_secret() -> String { let mut shared_secret = [0u8; SECRET_SIZE]; getrandom::getrandom(&mut shared_secret) @@ -609,6 +632,12 @@ struct QrState { state: S, } +impl QrState { + pub fn into_cancelled(self, content: &CancelContent<'_>) -> QrState { + QrState { state: Cancelled::new(false, content.cancel_code().to_owned()) } + } +} + #[derive(Clone, Debug)] struct Created { secret: String,