From 29bba0b2cac2b900256f96f89af40791200fc92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 14 Jun 2021 17:44:51 +0200 Subject: [PATCH] crypto: Allow accepting key request while specifying our supported methods --- .../src/verification/requests.rs | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/matrix_sdk_crypto/src/verification/requests.rs b/matrix_sdk_crypto/src/verification/requests.rs index ea704f67..6d3571ea 100644 --- a/matrix_sdk_crypto/src/verification/requests.rs +++ b/matrix_sdk_crypto/src/verification/requests.rs @@ -266,11 +266,19 @@ impl VerificationRequest { } } - /// Accept the verification request. - pub fn accept(&self) -> Option { + /// Accept the verification request signaling that our client supports the + /// given verification methods. + /// + /// # Arguments + /// + /// * `methods` - The methods that we should advertise as supported by us. + pub fn accept_with_methods( + &self, + methods: Vec, + ) -> Option { let mut inner = self.inner.lock().unwrap(); - inner.accept().map(|c| match c { + inner.accept(methods).map(|c| match c { OutgoingContent::ToDevice(content) => { ToDeviceRequest::new(&self.other_user(), inner.other_device_id(), content).into() } @@ -280,6 +288,19 @@ impl VerificationRequest { }) } + /// Accept the verification request. + /// + /// This method will accept the request and signal that it supports the + /// `m.sas.v1`, the `m.qr_code.show.v1`, and `m.reciprocate.v1` method. + /// + /// If QR code scanning should be supported or QR code showing shouldn't be + /// supported the [`accept_with_methods()`] method should be used instead. + /// + /// [`accept_with_methods()`]: #method.accept_with_methods + pub fn accept(&self) -> Option { + self.accept_with_methods(SUPPORTED_METHODS.to_vec()) + } + /// Cancel the verification request pub fn cancel(&self) -> Option { let mut inner = self.inner.lock().unwrap(); @@ -400,9 +421,9 @@ impl InnerRequest { } } - fn accept(&mut self) -> Option { + fn accept(&mut self, methods: Vec) -> Option { if let InnerRequest::Requested(s) = self { - let (state, content) = s.clone().accept(); + let (state, content) = s.clone().accept(methods); *self = InnerRequest::Ready(state); Some(content) @@ -512,7 +533,7 @@ impl RequestState { account, other_user_id: other_user_id.to_owned(), private_cross_signing_identity: private_identity, - state: Created { methods: SUPPORTED_METHODS.to_vec() }, + state: Created { our_methods: SUPPORTED_METHODS.to_vec() }, verification_cache: cache, store, flow_id: flow_id.to_owned().into(), @@ -541,7 +562,8 @@ impl RequestState { store: self.store, other_user_id: self.other_user_id, state: Ready { - methods: content.methods().to_owned(), + their_methods: content.methods().to_owned(), + our_methods: self.state.our_methods, other_device_id: content.from_device().into(), }, } @@ -550,14 +572,14 @@ impl RequestState { #[derive(Clone, Debug)] struct Created { - /// The verification methods supported by the sender. - pub methods: Vec, + /// The verification methods supported by us. + pub our_methods: Vec, } #[derive(Clone, Debug)] struct Requested { /// The verification methods supported by the sender. - pub methods: Vec, + pub their_methods: Vec, /// The device id of the device that responded to the verification request. pub other_device_id: DeviceIdBox, @@ -582,13 +604,13 @@ impl RequestState { flow_id: flow_id.to_owned().into(), other_user_id: sender.clone(), state: Requested { - methods: content.methods().to_owned(), + their_methods: content.methods().to_owned(), other_device_id: content.from_device().into(), }, } } - fn accept(self) -> (RequestState, OutgoingContent) { + fn accept(self, methods: Vec) -> (RequestState, OutgoingContent) { let state = RequestState { account: self.account.clone(), store: self.store, @@ -597,7 +619,8 @@ impl RequestState { flow_id: self.flow_id.clone(), other_user_id: self.other_user_id, state: Ready { - methods: SUPPORTED_METHODS.to_vec(), + their_methods: self.state.their_methods, + our_methods: methods.clone(), other_device_id: self.state.other_device_id.clone(), }, }; @@ -606,7 +629,7 @@ impl RequestState { FlowId::ToDevice(i) => { AnyToDeviceEventContent::KeyVerificationReady(ReadyToDeviceEventContent::new( self.account.device_id().to_owned(), - SUPPORTED_METHODS.to_vec(), + methods, i.to_owned(), )) .into() @@ -615,7 +638,7 @@ impl RequestState { r.to_owned(), AnyMessageEventContent::KeyVerificationReady(ReadyEventContent::new( self.account.device_id().to_owned(), - SUPPORTED_METHODS.to_vec(), + methods, Relation::new(e.to_owned()), )), ) @@ -628,8 +651,11 @@ impl RequestState { #[derive(Clone, Debug)] struct Ready { - /// The verification methods supported by the sender. - pub methods: Vec, + /// The verification methods supported by the other side. + pub their_methods: Vec, + + /// The verification methods supported by the us. + pub our_methods: Vec, /// The device id of the device that responded to the verification request. pub other_device_id: DeviceIdBox,