2020-12-09 16:18:23 +00:00
|
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
use std::{
|
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
|
time::Duration,
|
|
|
|
|
};
|
2020-12-09 16:18:23 +00:00
|
|
|
|
|
2021-09-08 15:23:10 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-06-09 14:30:15 +00:00
|
|
|
|
use matrix_qrcode::QrVerificationData;
|
2021-07-09 15:01:35 +00:00
|
|
|
|
use matrix_sdk_common::{instant::Instant, uuid::Uuid};
|
2021-09-08 15:23:10 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
|
|
|
|
use ruma::DeviceKeyAlgorithm;
|
2021-06-07 12:47:59 +00:00
|
|
|
|
use ruma::{
|
2020-12-09 16:18:23 +00:00
|
|
|
|
events::{
|
2020-12-10 16:49:28 +00:00
|
|
|
|
key::verification::{
|
2021-06-04 15:07:11 +00:00
|
|
|
|
cancel::CancelCode,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
ready::{ReadyEventContent, ReadyToDeviceEventContent},
|
|
|
|
|
request::RequestToDeviceEventContent,
|
2021-06-03 14:00:03 +00:00
|
|
|
|
start::StartMethod,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
Relation, VerificationMethod,
|
2020-12-10 16:49:28 +00:00
|
|
|
|
},
|
2020-12-09 16:18:23 +00:00
|
|
|
|
room::message::KeyVerificationRequestEventContent,
|
2021-05-24 12:54:11 +00:00
|
|
|
|
AnyMessageEventContent, AnyToDeviceEventContent,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
},
|
2021-06-19 23:35:28 +00:00
|
|
|
|
to_device::DeviceIdOrAllDevices,
|
2021-09-08 15:23:10 +00:00
|
|
|
|
DeviceId, DeviceIdBox, MilliSecondsSinceUnixEpoch, RoomId, UserId,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
};
|
2021-06-09 14:30:15 +00:00
|
|
|
|
use tracing::{info, trace, warn};
|
2020-12-09 16:18:23 +00:00
|
|
|
|
|
2021-05-13 09:26:40 +00:00
|
|
|
|
use super::{
|
2021-06-04 16:09:20 +00:00
|
|
|
|
cache::VerificationCache,
|
2021-06-04 15:07:11 +00:00
|
|
|
|
event_enums::{
|
|
|
|
|
CancelContent, DoneContent, OutgoingContent, ReadyContent, RequestContent, StartContent,
|
|
|
|
|
},
|
2021-09-08 15:23:10 +00:00
|
|
|
|
CancelInfo, Cancelled, FlowId, VerificationStore,
|
|
|
|
|
};
|
|
|
|
|
#[cfg(feature = "qrcode")]
|
|
|
|
|
use super::{
|
2021-06-09 14:30:15 +00:00
|
|
|
|
qrcode::{QrVerification, ScanError},
|
2021-09-08 15:23:10 +00:00
|
|
|
|
IdentitiesBeingVerified,
|
2021-05-13 09:26:40 +00:00
|
|
|
|
};
|
2020-12-10 16:49:28 +00:00
|
|
|
|
use crate::{
|
|
|
|
|
olm::{PrivateCrossSigningIdentity, ReadOnlyAccount},
|
2021-08-12 12:13:22 +00:00
|
|
|
|
CryptoStoreError, OutgoingVerificationRequest, ReadOnlyDevice, ReadOnlyOwnUserIdentity,
|
|
|
|
|
ReadOnlyUserIdentities, RoomMessageRequest, Sas, ToDeviceRequest,
|
2020-12-10 16:49:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-06-09 14:30:15 +00:00
|
|
|
|
const SUPPORTED_METHODS: &[VerificationMethod] = &[
|
2021-06-19 23:35:28 +00:00
|
|
|
|
VerificationMethod::SasV1,
|
2021-09-10 14:26:21 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-06-19 23:35:28 +00:00
|
|
|
|
VerificationMethod::QrCodeShowV1,
|
|
|
|
|
VerificationMethod::ReciprocateV1,
|
2021-06-09 14:30:15 +00:00
|
|
|
|
];
|
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
const VERIFICATION_TIMEOUT: Duration = Duration::from_secs(60 * 10);
|
|
|
|
|
|
2021-06-09 14:30:15 +00:00
|
|
|
|
/// An object controlling key verification requests.
|
|
|
|
|
///
|
|
|
|
|
/// Interactive verification flows usually start with a verification request,
|
|
|
|
|
/// this object lets you send and reply to such a verification request.
|
|
|
|
|
///
|
|
|
|
|
/// After the initial handshake the verification flow transitions into one of
|
|
|
|
|
/// the verification methods.
|
2020-12-09 16:18:23 +00:00
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct VerificationRequest {
|
2021-05-24 12:54:11 +00:00
|
|
|
|
verification_cache: VerificationCache,
|
|
|
|
|
account: ReadOnlyAccount,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
flow_id: Arc<FlowId>,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
other_user_id: Arc<UserId>,
|
|
|
|
|
inner: Arc<Mutex<InnerRequest>>,
|
2021-07-09 15:01:35 +00:00
|
|
|
|
creation_time: Arc<Instant>,
|
2021-06-15 19:15:11 +00:00
|
|
|
|
we_started: bool,
|
2021-07-12 16:12:02 +00:00
|
|
|
|
recipient_devices: Arc<Vec<DeviceIdBox>>,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 11:53:47 +00:00
|
|
|
|
/// A handle to a request so child verification flows can cancel the request.
|
|
|
|
|
///
|
|
|
|
|
/// A verification flow can branch off into different types of verification
|
|
|
|
|
/// flows after the initial request handshake is done.
|
|
|
|
|
///
|
|
|
|
|
/// Cancelling a QR code verification should also cancel the request. This
|
|
|
|
|
/// `RequestHandle` allows the QR code verification object to cancel the parent
|
|
|
|
|
/// `VerificationRequest` object.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub(crate) struct RequestHandle {
|
|
|
|
|
inner: Arc<Mutex<InnerRequest>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RequestHandle {
|
|
|
|
|
pub fn cancel_with_code(&self, cancel_code: &CancelCode) {
|
|
|
|
|
self.inner.lock().unwrap().cancel(true, cancel_code)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Arc<Mutex<InnerRequest>>> for RequestHandle {
|
|
|
|
|
fn from(inner: Arc<Mutex<InnerRequest>>) -> Self {
|
|
|
|
|
Self { inner }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 16:18:23 +00:00
|
|
|
|
impl VerificationRequest {
|
2021-07-19 08:49:22 +00:00
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-05-24 12:54:11 +00:00
|
|
|
|
pub(crate) fn new(
|
|
|
|
|
cache: VerificationCache,
|
2020-12-18 17:23:42 +00:00
|
|
|
|
private_cross_signing_identity: PrivateCrossSigningIdentity,
|
2021-08-04 09:07:28 +00:00
|
|
|
|
store: VerificationStore,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
flow_id: FlowId,
|
2021-06-03 15:23:40 +00:00
|
|
|
|
other_user: &UserId,
|
2021-07-12 16:12:02 +00:00
|
|
|
|
recipient_devices: Vec<DeviceIdBox>,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
methods: Option<Vec<VerificationMethod>>,
|
2021-06-03 15:23:40 +00:00
|
|
|
|
) -> Self {
|
2021-08-04 09:07:28 +00:00
|
|
|
|
let account = store.account.clone();
|
2021-06-03 15:23:40 +00:00
|
|
|
|
let inner = Mutex::new(InnerRequest::Created(RequestState::new(
|
|
|
|
|
private_cross_signing_identity,
|
|
|
|
|
cache.clone(),
|
|
|
|
|
store,
|
|
|
|
|
other_user,
|
|
|
|
|
&flow_id,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
methods,
|
2021-06-03 15:23:40 +00:00
|
|
|
|
)))
|
|
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
account,
|
|
|
|
|
verification_cache: cache,
|
|
|
|
|
flow_id: flow_id.into(),
|
|
|
|
|
inner,
|
|
|
|
|
other_user_id: other_user.to_owned().into(),
|
2021-07-09 15:01:35 +00:00
|
|
|
|
creation_time: Instant::now().into(),
|
2021-06-15 19:15:11 +00:00
|
|
|
|
we_started: true,
|
2021-07-12 16:12:02 +00:00
|
|
|
|
recipient_devices: recipient_devices.into(),
|
2021-06-03 15:23:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:30:15 +00:00
|
|
|
|
/// Create an event content that can be sent as a to-device event to request
|
|
|
|
|
/// verification from the other side. This should be used only for
|
|
|
|
|
/// self-verifications and it should be sent to the specific device that we
|
|
|
|
|
/// want to verify.
|
2021-07-12 16:12:02 +00:00
|
|
|
|
pub(crate) fn request_to_device(&self) -> ToDeviceRequest {
|
2021-07-08 10:30:30 +00:00
|
|
|
|
let inner = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
let methods = if let InnerRequest::Created(c) = &*inner {
|
|
|
|
|
c.state.our_methods.clone()
|
|
|
|
|
} else {
|
|
|
|
|
SUPPORTED_METHODS.to_vec()
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-12 16:12:02 +00:00
|
|
|
|
let content = RequestToDeviceEventContent::new(
|
2021-05-24 12:54:11 +00:00
|
|
|
|
self.account.device_id().into(),
|
|
|
|
|
self.flow_id().as_str().to_string(),
|
2021-07-08 10:30:30 +00:00
|
|
|
|
methods,
|
2021-05-24 12:54:11 +00:00
|
|
|
|
MilliSecondsSinceUnixEpoch::now(),
|
2021-07-12 16:12:02 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ToDeviceRequest::new_for_recipients(
|
|
|
|
|
self.other_user(),
|
|
|
|
|
self.recipient_devices.to_vec(),
|
|
|
|
|
AnyToDeviceEventContent::KeyVerificationRequest(content),
|
|
|
|
|
Uuid::new_v4(),
|
2021-05-24 12:54:11 +00:00
|
|
|
|
)
|
2020-12-18 17:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:30:15 +00:00
|
|
|
|
/// Create an event content that can be sent as a room event to request
|
|
|
|
|
/// verification from the other side. This should be used only for
|
|
|
|
|
/// verifications of other users and it should be sent to a room we consider
|
|
|
|
|
/// to be a DM with the other user.
|
2021-05-13 09:15:56 +00:00
|
|
|
|
pub fn request(
|
|
|
|
|
own_user_id: &UserId,
|
|
|
|
|
own_device_id: &DeviceId,
|
|
|
|
|
other_user_id: &UserId,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
methods: Option<Vec<VerificationMethod>>,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
) -> KeyVerificationRequestEventContent {
|
2021-05-18 07:07:50 +00:00
|
|
|
|
KeyVerificationRequestEventContent::new(
|
|
|
|
|
format!(
|
2021-05-13 09:15:56 +00:00
|
|
|
|
"{} is requesting to verify your key, but your client does not \
|
|
|
|
|
support in-chat key verification. You will need to use legacy \
|
|
|
|
|
key verification to verify keys.",
|
|
|
|
|
own_user_id
|
|
|
|
|
),
|
2021-07-08 10:30:30 +00:00
|
|
|
|
methods.unwrap_or_else(|| SUPPORTED_METHODS.to_vec()),
|
2021-05-18 07:07:50 +00:00
|
|
|
|
own_device_id.into(),
|
|
|
|
|
other_user_id.to_owned(),
|
|
|
|
|
)
|
2020-12-18 17:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 14:23:16 +00:00
|
|
|
|
/// Our own user id.
|
|
|
|
|
pub fn own_user_id(&self) -> &UserId {
|
|
|
|
|
self.account.user_id()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 17:23:42 +00:00
|
|
|
|
/// The id of the other user that is participating in this verification
|
|
|
|
|
/// request.
|
|
|
|
|
pub fn other_user(&self) -> &UserId {
|
|
|
|
|
&self.other_user_id
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 08:06:53 +00:00
|
|
|
|
/// The id of the other device that is participating in this verification.
|
|
|
|
|
pub fn other_device_id(&self) -> Option<DeviceIdBox> {
|
|
|
|
|
match &*self.inner.lock().unwrap() {
|
|
|
|
|
InnerRequest::Requested(r) => Some(r.state.other_device_id.clone()),
|
|
|
|
|
InnerRequest::Ready(r) => Some(r.state.other_device_id.clone()),
|
|
|
|
|
InnerRequest::Created(_)
|
|
|
|
|
| InnerRequest::Passive(_)
|
|
|
|
|
| InnerRequest::Done(_)
|
|
|
|
|
| InnerRequest::Cancelled(_) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 19:16:01 +00:00
|
|
|
|
/// Get the room id if the verification is happening inside a room.
|
|
|
|
|
pub fn room_id(&self) -> Option<&RoomId> {
|
|
|
|
|
match self.flow_id.as_ref() {
|
|
|
|
|
FlowId::ToDevice(_) => None,
|
|
|
|
|
FlowId::InRoom(r, _) => Some(r),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 07:45:47 +00:00
|
|
|
|
/// Get info about the cancellation if the verification request has been
|
|
|
|
|
/// cancelled.
|
|
|
|
|
pub fn cancel_info(&self) -> Option<CancelInfo> {
|
|
|
|
|
if let InnerRequest::Cancelled(c) = &*self.inner.lock().unwrap() {
|
|
|
|
|
Some(c.state.clone().into())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2021-06-15 19:16:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:50:35 +00:00
|
|
|
|
/// Has the verification request been answered by another device.
|
|
|
|
|
pub fn is_passive(&self) -> bool {
|
|
|
|
|
matches!(&*self.inner.lock().unwrap(), InnerRequest::Passive(_))
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:30:15 +00:00
|
|
|
|
/// Is the verification request ready to start a verification flow.
|
|
|
|
|
pub fn is_ready(&self) -> bool {
|
|
|
|
|
matches!(&*self.inner.lock().unwrap(), InnerRequest::Ready(_))
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
/// Has the verification flow timed out.
|
|
|
|
|
pub fn timed_out(&self) -> bool {
|
|
|
|
|
self.creation_time.elapsed() > VERIFICATION_TIMEOUT
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:50:35 +00:00
|
|
|
|
/// Get the supported verification methods of the other side.
|
|
|
|
|
///
|
|
|
|
|
/// Will be present only if the other side requested the verification or if
|
|
|
|
|
/// we're in the ready state.
|
2021-06-17 08:06:53 +00:00
|
|
|
|
pub fn their_supported_methods(&self) -> Option<Vec<VerificationMethod>> {
|
2021-06-14 15:50:35 +00:00
|
|
|
|
match &*self.inner.lock().unwrap() {
|
|
|
|
|
InnerRequest::Requested(r) => Some(r.state.their_methods.clone()),
|
|
|
|
|
InnerRequest::Ready(r) => Some(r.state.their_methods.clone()),
|
|
|
|
|
InnerRequest::Created(_)
|
|
|
|
|
| InnerRequest::Passive(_)
|
|
|
|
|
| InnerRequest::Done(_)
|
|
|
|
|
| InnerRequest::Cancelled(_) => None,
|
|
|
|
|
}
|
2021-06-17 08:06:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get our own supported verification methods that we advertised.
|
|
|
|
|
///
|
|
|
|
|
/// Will be present only we requested the verification or if we're in the
|
|
|
|
|
/// ready state.
|
|
|
|
|
pub fn our_supported_methods(&self) -> Option<Vec<VerificationMethod>> {
|
|
|
|
|
match &*self.inner.lock().unwrap() {
|
|
|
|
|
InnerRequest::Created(r) => Some(r.state.our_methods.clone()),
|
|
|
|
|
InnerRequest::Ready(r) => Some(r.state.our_methods.clone()),
|
|
|
|
|
InnerRequest::Requested(_)
|
|
|
|
|
| InnerRequest::Passive(_)
|
|
|
|
|
| InnerRequest::Done(_)
|
|
|
|
|
| InnerRequest::Cancelled(_) => None,
|
|
|
|
|
}
|
2021-06-14 15:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 09:15:56 +00:00
|
|
|
|
/// Get the unique ID of this verification request
|
|
|
|
|
pub fn flow_id(&self) -> &FlowId {
|
|
|
|
|
&self.flow_id
|
2020-12-18 17:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:30:15 +00:00
|
|
|
|
/// Is this a verification that is veryfying one of our own devices
|
|
|
|
|
pub fn is_self_verification(&self) -> bool {
|
|
|
|
|
self.account.user_id() == self.other_user()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 19:15:11 +00:00
|
|
|
|
/// Did we initiate the verification request
|
|
|
|
|
pub fn we_started(&self) -> bool {
|
|
|
|
|
self.we_started
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 15:07:11 +00:00
|
|
|
|
/// Has the verification flow that was started with this request finished.
|
|
|
|
|
pub fn is_done(&self) -> bool {
|
|
|
|
|
matches!(&*self.inner.lock().unwrap(), InnerRequest::Done(_))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Has the verification flow that was started with this request been
|
|
|
|
|
/// cancelled.
|
|
|
|
|
pub fn is_cancelled(&self) -> bool {
|
|
|
|
|
matches!(&*self.inner.lock().unwrap(), InnerRequest::Cancelled(_))
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 15:23:10 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-09-10 14:26:21 +00:00
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(qrcode)))]
|
2021-06-09 14:30:15 +00:00
|
|
|
|
/// Generate a QR code that can be used by another client to start a QR code
|
|
|
|
|
/// based verification.
|
|
|
|
|
pub async fn generate_qr_code(&self) -> Result<Option<QrVerification>, CryptoStoreError> {
|
2021-07-09 11:53:47 +00:00
|
|
|
|
self.inner
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.generate_qr_code(self.we_started, self.inner.clone().into())
|
|
|
|
|
.await
|
2021-06-09 14:30:15 +00:00
|
|
|
|
}
|
2021-09-08 15:23:10 +00:00
|
|
|
|
///
|
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-09-10 14:26:21 +00:00
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(qrcode)))]
|
2021-06-09 14:30:15 +00:00
|
|
|
|
/// Start a QR code verification by providing a scanned QR code for this
|
|
|
|
|
/// verification flow.
|
|
|
|
|
///
|
|
|
|
|
/// Returns a `ScanError` if the QR code isn't valid, `None` if the
|
|
|
|
|
/// verification request isn't in the ready state or we don't support QR
|
|
|
|
|
/// code verification, otherwise a newly created `QrVerification` object
|
|
|
|
|
/// which will be used for the remainder of the verification flow.
|
|
|
|
|
pub async fn scan_qr_code(
|
|
|
|
|
&self,
|
|
|
|
|
data: QrVerificationData,
|
|
|
|
|
) -> Result<Option<QrVerification>, ScanError> {
|
|
|
|
|
let state = self.inner.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
if let InnerRequest::Ready(r) = &*state {
|
2021-07-09 09:40:21 +00:00
|
|
|
|
let qr_verification = QrVerification::from_scan(
|
|
|
|
|
r.store.clone(),
|
|
|
|
|
r.private_cross_signing_identity.clone(),
|
|
|
|
|
r.other_user_id.clone(),
|
|
|
|
|
r.state.other_device_id.clone(),
|
|
|
|
|
r.flow_id.as_ref().to_owned(),
|
|
|
|
|
data,
|
|
|
|
|
self.we_started,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
Some(self.inner.clone().into()),
|
2021-07-09 09:40:21 +00:00
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
self.verification_cache.insert_qr(qr_verification.clone());
|
|
|
|
|
|
|
|
|
|
Ok(Some(qr_verification))
|
2021-06-09 14:30:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 09:15:56 +00:00
|
|
|
|
pub(crate) fn from_request(
|
2021-05-24 12:54:11 +00:00
|
|
|
|
cache: VerificationCache,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
private_cross_signing_identity: PrivateCrossSigningIdentity,
|
2021-08-04 09:07:28 +00:00
|
|
|
|
store: VerificationStore,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
sender: &UserId,
|
|
|
|
|
flow_id: FlowId,
|
2021-06-03 14:00:03 +00:00
|
|
|
|
content: &RequestContent,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
) -> Self {
|
2021-08-04 09:07:28 +00:00
|
|
|
|
let account = store.account.clone();
|
|
|
|
|
|
2020-12-09 16:18:23 +00:00
|
|
|
|
Self {
|
2021-05-27 14:11:54 +00:00
|
|
|
|
verification_cache: cache.clone(),
|
2021-05-12 15:00:47 +00:00
|
|
|
|
inner: Arc::new(Mutex::new(InnerRequest::Requested(RequestState::from_request_event(
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_cross_signing_identity,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
cache,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
store,
|
2021-05-12 15:00:47 +00:00
|
|
|
|
sender,
|
2021-05-13 09:26:40 +00:00
|
|
|
|
&flow_id,
|
2021-05-12 15:00:47 +00:00
|
|
|
|
content,
|
|
|
|
|
)))),
|
2021-05-24 12:54:11 +00:00
|
|
|
|
account,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
other_user_id: sender.to_owned().into(),
|
2021-05-13 09:15:56 +00:00
|
|
|
|
flow_id: flow_id.into(),
|
2021-06-15 19:15:11 +00:00
|
|
|
|
we_started: false,
|
2021-07-09 15:01:35 +00:00
|
|
|
|
creation_time: Instant::now().into(),
|
2021-07-12 16:12:02 +00:00
|
|
|
|
recipient_devices: vec![].into(),
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:44:51 +00:00
|
|
|
|
/// 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<VerificationMethod>,
|
|
|
|
|
) -> Option<OutgoingVerificationRequest> {
|
2021-05-13 09:15:56 +00:00
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
2021-06-14 15:44:51 +00:00
|
|
|
|
inner.accept(methods).map(|c| match c {
|
2021-05-13 09:26:40 +00:00
|
|
|
|
OutgoingContent::ToDevice(content) => {
|
2021-08-05 20:48:05 +00:00
|
|
|
|
ToDeviceRequest::new(self.other_user(), inner.other_device_id(), content).into()
|
2021-05-13 09:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
OutgoingContent::Room(room_id, content) => {
|
|
|
|
|
RoomMessageRequest { room_id, txn_id: Uuid::new_v4(), content }.into()
|
2021-05-13 09:15:56 +00:00
|
|
|
|
}
|
2021-06-10 14:23:16 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:44:51 +00:00
|
|
|
|
/// 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.
|
|
|
|
|
///
|
2021-09-10 14:26:21 +00:00
|
|
|
|
/// `m.qr_code.show.v1` will only be signaled if the `qrcode` feature is
|
|
|
|
|
/// enabled. This feature is disabled by default. If it's enabeled and QR
|
|
|
|
|
/// code scanning should be supported or QR code showing shouldn't be
|
|
|
|
|
/// supported the [`accept_with_methods()`] method should be used
|
|
|
|
|
/// instead.
|
2021-06-14 15:44:51 +00:00
|
|
|
|
///
|
|
|
|
|
/// [`accept_with_methods()`]: #method.accept_with_methods
|
|
|
|
|
pub fn accept(&self) -> Option<OutgoingVerificationRequest> {
|
|
|
|
|
self.accept_with_methods(SUPPORTED_METHODS.to_vec())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 14:23:16 +00:00
|
|
|
|
/// Cancel the verification request
|
|
|
|
|
pub fn cancel(&self) -> Option<OutgoingVerificationRequest> {
|
2021-07-09 15:01:35 +00:00
|
|
|
|
self.cancel_with_code(CancelCode::User)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cancel_with_code(&self, cancel_code: CancelCode) -> Option<OutgoingVerificationRequest> {
|
2021-06-10 14:23:16 +00:00
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
2021-07-12 16:12:02 +00:00
|
|
|
|
|
|
|
|
|
let send_to_everyone = self.we_started() && matches!(&*inner, InnerRequest::Created(_));
|
|
|
|
|
let other_device = inner.other_device_id();
|
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
inner.cancel(true, &cancel_code);
|
2021-06-10 14:23:16 +00:00
|
|
|
|
|
|
|
|
|
let content = if let InnerRequest::Cancelled(c) = &*inner {
|
|
|
|
|
Some(c.state.as_content(self.flow_id()))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-09 12:31:54 +00:00
|
|
|
|
let request = content.map(|c| match c {
|
2021-06-10 14:23:16 +00:00
|
|
|
|
OutgoingContent::ToDevice(content) => {
|
2021-07-12 16:12:02 +00:00
|
|
|
|
if send_to_everyone {
|
|
|
|
|
ToDeviceRequest::new_for_recipients(
|
2021-08-05 20:48:05 +00:00
|
|
|
|
self.other_user(),
|
2021-07-12 16:12:02 +00:00
|
|
|
|
self.recipient_devices.to_vec(),
|
|
|
|
|
content,
|
|
|
|
|
Uuid::new_v4(),
|
|
|
|
|
)
|
|
|
|
|
.into()
|
|
|
|
|
} else {
|
2021-08-05 20:48:05 +00:00
|
|
|
|
ToDeviceRequest::new(self.other_user(), other_device, content).into()
|
2021-07-12 16:12:02 +00:00
|
|
|
|
}
|
2021-06-10 14:23:16 +00:00
|
|
|
|
}
|
|
|
|
|
OutgoingContent::Room(room_id, content) => {
|
|
|
|
|
RoomMessageRequest { room_id, txn_id: Uuid::new_v4(), content }.into()
|
|
|
|
|
}
|
2021-07-09 12:31:54 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
drop(inner);
|
|
|
|
|
|
|
|
|
|
if let Some(verification) =
|
|
|
|
|
self.verification_cache.get(self.other_user(), self.flow_id().as_str())
|
|
|
|
|
{
|
|
|
|
|
match verification {
|
2021-07-09 15:01:35 +00:00
|
|
|
|
crate::Verification::SasV1(s) => s.cancel_with_code(cancel_code),
|
2021-09-08 15:23:10 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-07-09 15:01:35 +00:00
|
|
|
|
crate::Verification::QrV1(q) => q.cancel_with_code(cancel_code),
|
2021-07-09 12:31:54 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
2020-12-11 14:42:49 +00:00
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
pub(crate) fn cancel_if_timed_out(&self) -> Option<OutgoingVerificationRequest> {
|
|
|
|
|
if self.is_cancelled() || self.is_done() {
|
|
|
|
|
None
|
|
|
|
|
} else if self.timed_out() {
|
2021-07-19 07:48:22 +00:00
|
|
|
|
let request = self.cancel_with_code(CancelCode::Timeout);
|
|
|
|
|
|
|
|
|
|
if self.is_passive() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2021-08-02 05:44:03 +00:00
|
|
|
|
trace!(
|
|
|
|
|
other_user = self.other_user().as_str(),
|
|
|
|
|
flow_id = self.flow_id().as_str(),
|
|
|
|
|
"Timing a verification request out"
|
|
|
|
|
);
|
2021-07-19 07:48:22 +00:00
|
|
|
|
request
|
|
|
|
|
}
|
2021-07-09 15:01:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 16:12:02 +00:00
|
|
|
|
/// Create a key verification cancellation for devices that received the
|
|
|
|
|
/// request but either shouldn't continue in the verification or didn't get
|
|
|
|
|
/// notified that the other side cancelled.
|
|
|
|
|
///
|
|
|
|
|
/// The spec states the following[1]:
|
|
|
|
|
/// When Bob accepts or declines the verification on one of his devices
|
|
|
|
|
/// (sending either an m.key.verification.ready or m.key.verification.cancel
|
|
|
|
|
/// event), Alice will send an m.key.verification.cancel event to Bob’s
|
|
|
|
|
/// other devices with a code of m.accepted in the case where Bob accepted
|
|
|
|
|
/// the verification, or m.user in the case where Bob rejected the
|
|
|
|
|
/// verification.
|
|
|
|
|
///
|
|
|
|
|
/// Realistically sending the cancellation to Bob's other devices is only
|
|
|
|
|
/// possible if Bob accepted the verification since we don't know the device
|
|
|
|
|
/// id of Bob's device that rejected the verification.
|
|
|
|
|
///
|
|
|
|
|
/// Thus, we're sending the cancellation to all devices that received the
|
|
|
|
|
/// request in the rejection case.
|
|
|
|
|
///
|
|
|
|
|
/// [1]: https://spec.matrix.org/unstable/client-server-api/#key-verification-framework
|
|
|
|
|
pub(crate) fn cancel_for_other_devices(
|
|
|
|
|
&self,
|
|
|
|
|
code: CancelCode,
|
|
|
|
|
filter_device: Option<&DeviceId>,
|
|
|
|
|
) -> Option<ToDeviceRequest> {
|
|
|
|
|
let cancelled = Cancelled::new(true, code);
|
|
|
|
|
let cancel_content = cancelled.as_content(self.flow_id());
|
|
|
|
|
|
|
|
|
|
if let OutgoingContent::ToDevice(c) = cancel_content {
|
|
|
|
|
let recipients: Vec<DeviceIdBox> = self
|
|
|
|
|
.recipient_devices
|
|
|
|
|
.to_vec()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter(|d| if let Some(device) = filter_device { &**d != device } else { true })
|
|
|
|
|
.collect();
|
|
|
|
|
|
2021-09-15 18:28:16 +00:00
|
|
|
|
// We don't need to notify anyone if no recipients were present
|
|
|
|
|
// but we did have a filter device, since this means that only a
|
|
|
|
|
// single device received the `m.key.verification.request` and that
|
|
|
|
|
// device accepted the request.
|
|
|
|
|
if recipients.is_empty() && filter_device.is_some() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(ToDeviceRequest::new_for_recipients(
|
|
|
|
|
self.other_user(),
|
|
|
|
|
recipients,
|
|
|
|
|
c,
|
|
|
|
|
Uuid::new_v4(),
|
|
|
|
|
))
|
|
|
|
|
}
|
2021-07-12 16:12:02 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:16:40 +00:00
|
|
|
|
pub(crate) fn receive_ready(&self, sender: &UserId, content: &ReadyContent) {
|
2020-12-18 17:23:42 +00:00
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
|
|
|
|
|
2021-07-08 10:30:30 +00:00
|
|
|
|
match &*inner {
|
|
|
|
|
InnerRequest::Created(s) => {
|
2021-06-14 15:32:30 +00:00
|
|
|
|
*inner = InnerRequest::Ready(s.clone().into_ready(sender, content));
|
2021-07-12 16:12:02 +00:00
|
|
|
|
|
|
|
|
|
if let Some(request) =
|
|
|
|
|
self.cancel_for_other_devices(CancelCode::Accepted, Some(content.from_device()))
|
|
|
|
|
{
|
|
|
|
|
self.verification_cache.add_verification_request(request.into());
|
|
|
|
|
}
|
2021-06-14 15:32:30 +00:00
|
|
|
|
}
|
2021-07-08 10:30:30 +00:00
|
|
|
|
InnerRequest::Requested(s) => {
|
|
|
|
|
if sender == self.own_user_id() && content.from_device() != self.account.device_id()
|
|
|
|
|
{
|
|
|
|
|
*inner = InnerRequest::Passive(s.clone().into_passive(content))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
InnerRequest::Ready(_)
|
|
|
|
|
| InnerRequest::Passive(_)
|
|
|
|
|
| InnerRequest::Done(_)
|
|
|
|
|
| InnerRequest::Cancelled(_) => {}
|
2020-12-18 17:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 14:00:03 +00:00
|
|
|
|
pub(crate) async fn receive_start(
|
2021-05-27 14:11:54 +00:00
|
|
|
|
&self,
|
|
|
|
|
sender: &UserId,
|
2021-06-03 14:00:03 +00:00
|
|
|
|
content: &StartContent<'_>,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
) -> Result<(), CryptoStoreError> {
|
2021-05-27 14:24:00 +00:00
|
|
|
|
let inner = self.inner.lock().unwrap().clone();
|
|
|
|
|
|
|
|
|
|
if let InnerRequest::Ready(s) = inner {
|
2021-07-09 11:53:47 +00:00
|
|
|
|
s.receive_start(sender, content, self.we_started, self.inner.clone().into()).await?;
|
2021-05-27 14:15:51 +00:00
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
sender = sender.as_str(),
|
|
|
|
|
device_id = content.from_device().as_str(),
|
|
|
|
|
"Received a key verification start event but we're not yet in the ready state"
|
|
|
|
|
)
|
2020-12-11 14:42:49 +00:00
|
|
|
|
}
|
2021-05-27 14:11:54 +00:00
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 15:07:11 +00:00
|
|
|
|
pub(crate) fn receive_done(&self, sender: &UserId, content: &DoneContent<'_>) {
|
|
|
|
|
if sender == self.other_user() {
|
2021-08-02 05:44:03 +00:00
|
|
|
|
trace!(
|
|
|
|
|
other_user = self.other_user().as_str(),
|
|
|
|
|
flow_id = self.flow_id().as_str(),
|
|
|
|
|
"Marking a verification request as done"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
2021-06-04 16:49:08 +00:00
|
|
|
|
inner.receive_done(content);
|
2021-06-04 15:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn receive_cancel(&self, sender: &UserId, content: &CancelContent<'_>) {
|
|
|
|
|
if sender == self.other_user() {
|
2021-06-28 13:27:01 +00:00
|
|
|
|
trace!(
|
|
|
|
|
sender = sender.as_str(),
|
|
|
|
|
code = content.cancel_code().as_str(),
|
|
|
|
|
"Cancelling a verification request, other user has cancelled"
|
|
|
|
|
);
|
|
|
|
|
let mut inner = self.inner.lock().unwrap();
|
2021-06-25 10:54:00 +00:00
|
|
|
|
inner.cancel(false, content.cancel_code());
|
2021-07-12 16:12:02 +00:00
|
|
|
|
|
2021-07-19 07:36:21 +00:00
|
|
|
|
if self.we_started() {
|
|
|
|
|
if let Some(request) =
|
|
|
|
|
self.cancel_for_other_devices(content.cancel_code().to_owned(), None)
|
|
|
|
|
{
|
|
|
|
|
self.verification_cache.add_verification_request(request.into());
|
|
|
|
|
}
|
2021-07-12 16:12:02 +00:00
|
|
|
|
}
|
2021-06-04 15:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:49:18 +00:00
|
|
|
|
/// Transition from this verification request into a SAS verification flow.
|
|
|
|
|
pub async fn start_sas(
|
2020-12-24 14:22:51 +00:00
|
|
|
|
&self,
|
2021-06-14 15:49:18 +00:00
|
|
|
|
) -> Result<Option<(Sas, OutgoingVerificationRequest)>, CryptoStoreError> {
|
|
|
|
|
let inner = self.inner.lock().unwrap().clone();
|
|
|
|
|
|
|
|
|
|
Ok(match &inner {
|
|
|
|
|
InnerRequest::Ready(s) => {
|
|
|
|
|
if let Some((sas, content)) = s
|
|
|
|
|
.clone()
|
|
|
|
|
.start_sas(
|
|
|
|
|
s.store.clone(),
|
|
|
|
|
s.private_cross_signing_identity.clone(),
|
2021-06-25 10:51:45 +00:00
|
|
|
|
self.we_started,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
self.inner.clone().into(),
|
2021-06-14 15:49:18 +00:00
|
|
|
|
)
|
|
|
|
|
.await?
|
|
|
|
|
{
|
|
|
|
|
self.verification_cache.insert_sas(sas.clone());
|
|
|
|
|
|
|
|
|
|
let request = match content {
|
|
|
|
|
OutgoingContent::ToDevice(content) => ToDeviceRequest::new(
|
2021-08-05 20:48:05 +00:00
|
|
|
|
self.other_user(),
|
2021-06-14 15:49:18 +00:00
|
|
|
|
inner.other_device_id(),
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
.into(),
|
|
|
|
|
OutgoingContent::Room(room_id, content) => {
|
|
|
|
|
RoomMessageRequest { room_id, txn_id: Uuid::new_v4(), content }.into()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some((sas, request))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-24 14:22:51 +00:00
|
|
|
|
_ => None,
|
2021-06-14 15:49:18 +00:00
|
|
|
|
})
|
2020-12-24 14:22:51 +00:00
|
|
|
|
}
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 14:24:00 +00:00
|
|
|
|
#[derive(Clone, Debug)]
|
2020-12-09 16:18:23 +00:00
|
|
|
|
enum InnerRequest {
|
|
|
|
|
Created(RequestState<Created>),
|
|
|
|
|
Requested(RequestState<Requested>),
|
|
|
|
|
Ready(RequestState<Ready>),
|
|
|
|
|
Passive(RequestState<Passive>),
|
2021-06-04 15:07:11 +00:00
|
|
|
|
Done(RequestState<Done>),
|
|
|
|
|
Cancelled(RequestState<Cancelled>),
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InnerRequest {
|
2021-05-13 09:15:56 +00:00
|
|
|
|
fn other_device_id(&self) -> DeviceIdOrAllDevices {
|
|
|
|
|
match self {
|
|
|
|
|
InnerRequest::Created(_) => DeviceIdOrAllDevices::AllDevices,
|
|
|
|
|
InnerRequest::Requested(_) => DeviceIdOrAllDevices::AllDevices,
|
|
|
|
|
InnerRequest::Ready(r) => {
|
|
|
|
|
DeviceIdOrAllDevices::DeviceId(r.state.other_device_id.to_owned())
|
|
|
|
|
}
|
|
|
|
|
InnerRequest::Passive(_) => DeviceIdOrAllDevices::AllDevices,
|
2021-06-04 15:07:11 +00:00
|
|
|
|
InnerRequest::Done(_) => DeviceIdOrAllDevices::AllDevices,
|
|
|
|
|
InnerRequest::Cancelled(_) => DeviceIdOrAllDevices::AllDevices,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:44:51 +00:00
|
|
|
|
fn accept(&mut self, methods: Vec<VerificationMethod>) -> Option<OutgoingContent> {
|
2020-12-09 16:18:23 +00:00
|
|
|
|
if let InnerRequest::Requested(s) = self {
|
2021-06-14 15:44:51 +00:00
|
|
|
|
let (state, content) = s.clone().accept(methods);
|
2020-12-10 16:49:28 +00:00
|
|
|
|
*self = InnerRequest::Ready(state);
|
2020-12-09 16:18:23 +00:00
|
|
|
|
|
|
|
|
|
Some(content)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-11 14:42:49 +00:00
|
|
|
|
|
2021-06-04 16:49:08 +00:00
|
|
|
|
fn receive_done(&mut self, content: &DoneContent) {
|
2021-06-04 15:07:11 +00:00
|
|
|
|
*self = InnerRequest::Done(match self {
|
|
|
|
|
InnerRequest::Ready(s) => s.clone().into_done(content),
|
|
|
|
|
InnerRequest::Passive(s) => s.clone().into_done(content),
|
2021-07-09 09:50:12 +00:00
|
|
|
|
InnerRequest::Done(_)
|
|
|
|
|
| InnerRequest::Created(_)
|
|
|
|
|
| InnerRequest::Requested(_)
|
|
|
|
|
| InnerRequest::Cancelled(_) => return,
|
2021-06-04 15:07:11 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 10:54:00 +00:00
|
|
|
|
fn cancel(&mut self, cancelled_by_us: bool, cancel_code: &CancelCode) {
|
2021-08-02 05:44:03 +00:00
|
|
|
|
let print_info = || {
|
|
|
|
|
trace!(
|
|
|
|
|
cancelled_by_us = cancelled_by_us,
|
|
|
|
|
code = cancel_code.as_str(),
|
|
|
|
|
"Verification request going into the cancelled state"
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-07-19 07:48:22 +00:00
|
|
|
|
|
2021-06-04 15:07:11 +00:00
|
|
|
|
*self = InnerRequest::Cancelled(match self {
|
2021-08-02 05:44:03 +00:00
|
|
|
|
InnerRequest::Created(s) => {
|
|
|
|
|
print_info();
|
|
|
|
|
s.clone().into_canceled(cancelled_by_us, cancel_code)
|
|
|
|
|
}
|
|
|
|
|
InnerRequest::Requested(s) => {
|
|
|
|
|
print_info();
|
|
|
|
|
s.clone().into_canceled(cancelled_by_us, cancel_code)
|
|
|
|
|
}
|
|
|
|
|
InnerRequest::Ready(s) => {
|
|
|
|
|
print_info();
|
|
|
|
|
s.clone().into_canceled(cancelled_by_us, cancel_code)
|
|
|
|
|
}
|
2021-07-12 16:12:02 +00:00
|
|
|
|
InnerRequest::Passive(_) | InnerRequest::Done(_) | InnerRequest::Cancelled(_) => return,
|
2021-06-28 13:27:01 +00:00
|
|
|
|
});
|
2021-06-04 15:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 15:23:10 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-06-28 07:53:56 +00:00
|
|
|
|
async fn generate_qr_code(
|
|
|
|
|
&self,
|
|
|
|
|
we_started: bool,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
request_handle: RequestHandle,
|
2021-06-28 07:53:56 +00:00
|
|
|
|
) -> Result<Option<QrVerification>, CryptoStoreError> {
|
2021-06-09 14:30:15 +00:00
|
|
|
|
match self {
|
|
|
|
|
InnerRequest::Created(_) => Ok(None),
|
|
|
|
|
InnerRequest::Requested(_) => Ok(None),
|
2021-07-09 11:53:47 +00:00
|
|
|
|
InnerRequest::Ready(s) => s.generate_qr_code(we_started, request_handle).await,
|
2021-06-09 14:30:15 +00:00
|
|
|
|
InnerRequest::Passive(_) => Ok(None),
|
|
|
|
|
InnerRequest::Done(_) => Ok(None),
|
|
|
|
|
InnerRequest::Cancelled(_) => Ok(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
struct RequestState<S: Clone> {
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_cross_signing_identity: PrivateCrossSigningIdentity,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
verification_cache: VerificationCache,
|
2021-08-04 09:07:28 +00:00
|
|
|
|
store: VerificationStore,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
flow_id: Arc<FlowId>,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
|
|
|
|
|
/// The id of the user which is participating in this verification request.
|
|
|
|
|
pub other_user_id: UserId,
|
|
|
|
|
|
|
|
|
|
/// The verification request state we are in.
|
|
|
|
|
state: S,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 15:07:11 +00:00
|
|
|
|
impl<S: Clone> RequestState<S> {
|
|
|
|
|
fn into_done(self, _: &DoneContent) -> RequestState<Done> {
|
|
|
|
|
RequestState::<Done> {
|
|
|
|
|
private_cross_signing_identity: self.private_cross_signing_identity,
|
|
|
|
|
verification_cache: self.verification_cache,
|
|
|
|
|
store: self.store,
|
|
|
|
|
flow_id: self.flow_id,
|
|
|
|
|
other_user_id: self.other_user_id,
|
|
|
|
|
state: Done {},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 10:54:00 +00:00
|
|
|
|
fn into_canceled(
|
|
|
|
|
self,
|
|
|
|
|
cancelled_by_us: bool,
|
|
|
|
|
cancel_code: &CancelCode,
|
|
|
|
|
) -> RequestState<Cancelled> {
|
2021-06-04 15:07:11 +00:00
|
|
|
|
RequestState::<Cancelled> {
|
|
|
|
|
private_cross_signing_identity: self.private_cross_signing_identity,
|
|
|
|
|
verification_cache: self.verification_cache,
|
|
|
|
|
store: self.store,
|
|
|
|
|
flow_id: self.flow_id,
|
|
|
|
|
other_user_id: self.other_user_id,
|
2021-06-25 10:54:00 +00:00
|
|
|
|
state: Cancelled::new(cancelled_by_us, cancel_code.clone()),
|
2021-06-04 15:07:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 16:18:23 +00:00
|
|
|
|
impl RequestState<Created> {
|
2021-05-13 09:15:56 +00:00
|
|
|
|
fn new(
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_identity: PrivateCrossSigningIdentity,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
cache: VerificationCache,
|
2021-08-04 09:07:28 +00:00
|
|
|
|
store: VerificationStore,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
other_user_id: &UserId,
|
|
|
|
|
flow_id: &FlowId,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
methods: Option<Vec<VerificationMethod>>,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
) -> Self {
|
2021-07-08 10:30:30 +00:00
|
|
|
|
let our_methods = methods.unwrap_or_else(|| SUPPORTED_METHODS.to_vec());
|
|
|
|
|
|
2020-12-18 17:23:42 +00:00
|
|
|
|
Self {
|
2021-05-13 09:15:56 +00:00
|
|
|
|
other_user_id: other_user_id.to_owned(),
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_cross_signing_identity: private_identity,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
state: Created { our_methods },
|
2021-05-27 14:11:54 +00:00
|
|
|
|
verification_cache: cache,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
store,
|
|
|
|
|
flow_id: flow_id.to_owned().into(),
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
2021-06-14 15:32:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 14:00:03 +00:00
|
|
|
|
fn into_ready(self, _sender: &UserId, content: &ReadyContent) -> RequestState<Ready> {
|
2021-05-13 09:15:56 +00:00
|
|
|
|
// TODO check the flow id, and that the methods match what we suggested.
|
2020-12-09 16:18:23 +00:00
|
|
|
|
RequestState {
|
2021-05-21 10:44:10 +00:00
|
|
|
|
flow_id: self.flow_id,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
verification_cache: self.verification_cache,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_cross_signing_identity: self.private_cross_signing_identity,
|
|
|
|
|
store: self.store,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
other_user_id: self.other_user_id,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
state: Ready {
|
2021-06-14 15:44:51 +00:00
|
|
|
|
their_methods: content.methods().to_owned(),
|
|
|
|
|
our_methods: self.state.our_methods,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
other_device_id: content.from_device().into(),
|
2020-12-18 12:50:02 +00:00
|
|
|
|
},
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-05-13 09:15:56 +00:00
|
|
|
|
struct Created {
|
2021-06-14 15:44:51 +00:00
|
|
|
|
/// The verification methods supported by us.
|
|
|
|
|
pub our_methods: Vec<VerificationMethod>,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
struct Requested {
|
|
|
|
|
/// The verification methods supported by the sender.
|
2021-06-14 15:44:51 +00:00
|
|
|
|
pub their_methods: Vec<VerificationMethod>,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
|
|
|
|
|
/// The device id of the device that responded to the verification request.
|
|
|
|
|
pub other_device_id: DeviceIdBox,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RequestState<Requested> {
|
|
|
|
|
fn from_request_event(
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_identity: PrivateCrossSigningIdentity,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
cache: VerificationCache,
|
2021-08-04 09:07:28 +00:00
|
|
|
|
store: VerificationStore,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
sender: &UserId,
|
2021-05-13 09:15:56 +00:00
|
|
|
|
flow_id: &FlowId,
|
2021-06-03 14:00:03 +00:00
|
|
|
|
content: &RequestContent,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
) -> RequestState<Requested> {
|
2021-06-05 12:35:20 +00:00
|
|
|
|
// TODO only create this if we support the methods
|
2020-12-09 16:18:23 +00:00
|
|
|
|
RequestState {
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_cross_signing_identity: private_identity,
|
|
|
|
|
store,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
verification_cache: cache,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
flow_id: flow_id.to_owned().into(),
|
2020-12-09 16:18:23 +00:00
|
|
|
|
other_user_id: sender.clone(),
|
|
|
|
|
state: Requested {
|
2021-06-14 15:44:51 +00:00
|
|
|
|
their_methods: content.methods().to_owned(),
|
2021-05-13 09:15:56 +00:00
|
|
|
|
other_device_id: content.from_device().into(),
|
2020-12-18 12:50:02 +00:00
|
|
|
|
},
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 10:30:30 +00:00
|
|
|
|
fn into_passive(self, content: &ReadyContent) -> RequestState<Passive> {
|
|
|
|
|
RequestState {
|
|
|
|
|
flow_id: self.flow_id,
|
|
|
|
|
verification_cache: self.verification_cache,
|
|
|
|
|
private_cross_signing_identity: self.private_cross_signing_identity,
|
|
|
|
|
store: self.store,
|
|
|
|
|
other_user_id: self.other_user_id,
|
|
|
|
|
state: Passive { other_device_id: content.from_device().to_owned() },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:44:51 +00:00
|
|
|
|
fn accept(self, methods: Vec<VerificationMethod>) -> (RequestState<Ready>, OutgoingContent) {
|
2020-12-09 16:18:23 +00:00
|
|
|
|
let state = RequestState {
|
2021-05-21 10:44:10 +00:00
|
|
|
|
store: self.store,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
verification_cache: self.verification_cache,
|
2021-05-21 10:44:10 +00:00
|
|
|
|
private_cross_signing_identity: self.private_cross_signing_identity,
|
2021-06-14 15:28:10 +00:00
|
|
|
|
flow_id: self.flow_id.clone(),
|
2020-12-09 16:18:23 +00:00
|
|
|
|
other_user_id: self.other_user_id,
|
2020-12-10 16:49:28 +00:00
|
|
|
|
state: Ready {
|
2021-06-14 15:44:51 +00:00
|
|
|
|
their_methods: self.state.their_methods,
|
|
|
|
|
our_methods: methods.clone(),
|
2020-12-09 16:18:23 +00:00
|
|
|
|
other_device_id: self.state.other_device_id.clone(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-14 15:28:10 +00:00
|
|
|
|
let content = match self.flow_id.as_ref() {
|
2021-05-21 10:44:10 +00:00
|
|
|
|
FlowId::ToDevice(i) => {
|
|
|
|
|
AnyToDeviceEventContent::KeyVerificationReady(ReadyToDeviceEventContent::new(
|
2021-08-04 09:07:28 +00:00
|
|
|
|
state.store.account.device_id().to_owned(),
|
2021-06-14 15:44:51 +00:00
|
|
|
|
methods,
|
2021-06-14 15:28:10 +00:00
|
|
|
|
i.to_owned(),
|
2021-05-21 10:44:10 +00:00
|
|
|
|
))
|
|
|
|
|
.into()
|
|
|
|
|
}
|
2021-05-13 09:15:56 +00:00
|
|
|
|
FlowId::InRoom(r, e) => (
|
2021-06-14 15:28:10 +00:00
|
|
|
|
r.to_owned(),
|
2021-05-18 07:07:50 +00:00
|
|
|
|
AnyMessageEventContent::KeyVerificationReady(ReadyEventContent::new(
|
2021-08-04 09:07:28 +00:00
|
|
|
|
state.store.account.device_id().to_owned(),
|
2021-06-14 15:44:51 +00:00
|
|
|
|
methods,
|
2021-06-14 15:28:10 +00:00
|
|
|
|
Relation::new(e.to_owned()),
|
2021-05-18 07:07:50 +00:00
|
|
|
|
)),
|
2021-05-13 09:15:56 +00:00
|
|
|
|
)
|
|
|
|
|
.into(),
|
2020-12-09 16:18:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(state, content)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
struct Ready {
|
2021-06-14 15:44:51 +00:00
|
|
|
|
/// The verification methods supported by the other side.
|
|
|
|
|
pub their_methods: Vec<VerificationMethod>,
|
|
|
|
|
|
|
|
|
|
/// The verification methods supported by the us.
|
|
|
|
|
pub our_methods: Vec<VerificationMethod>,
|
2020-12-09 16:18:23 +00:00
|
|
|
|
|
|
|
|
|
/// The device id of the device that responded to the verification request.
|
|
|
|
|
pub other_device_id: DeviceIdBox,
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 16:49:28 +00:00
|
|
|
|
impl RequestState<Ready> {
|
2021-05-27 14:11:54 +00:00
|
|
|
|
fn to_started_sas<'a>(
|
|
|
|
|
&self,
|
2021-06-03 14:00:03 +00:00
|
|
|
|
content: &StartContent<'a>,
|
2020-12-10 16:49:28 +00:00
|
|
|
|
other_device: ReadOnlyDevice,
|
2021-08-12 12:13:22 +00:00
|
|
|
|
own_identity: Option<ReadOnlyOwnUserIdentity>,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
other_identity: Option<ReadOnlyUserIdentities>,
|
2021-06-25 10:51:45 +00:00
|
|
|
|
we_started: bool,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
request_handle: RequestHandle,
|
2020-12-11 14:42:49 +00:00
|
|
|
|
) -> Result<Sas, OutgoingContent> {
|
|
|
|
|
Sas::from_start_event(
|
2021-06-03 14:00:03 +00:00
|
|
|
|
(&*self.flow_id).to_owned(),
|
2021-05-24 12:54:11 +00:00
|
|
|
|
content,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
self.store.clone(),
|
|
|
|
|
self.private_cross_signing_identity.clone(),
|
|
|
|
|
other_device,
|
2021-08-12 12:13:22 +00:00
|
|
|
|
own_identity,
|
2020-12-11 14:42:49 +00:00
|
|
|
|
other_identity,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
Some(request_handle),
|
2021-06-25 10:51:45 +00:00
|
|
|
|
we_started,
|
2020-12-11 14:42:49 +00:00
|
|
|
|
)
|
2020-12-10 16:49:28 +00:00
|
|
|
|
}
|
2020-12-09 16:18:23 +00:00
|
|
|
|
|
2021-09-08 15:23:10 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-06-25 15:55:39 +00:00
|
|
|
|
async fn generate_qr_code(
|
|
|
|
|
&self,
|
|
|
|
|
we_started: bool,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
request_handle: RequestHandle,
|
2021-06-25 15:55:39 +00:00
|
|
|
|
) -> Result<Option<QrVerification>, CryptoStoreError> {
|
2021-06-14 15:51:13 +00:00
|
|
|
|
// If we didn't state that we support showing QR codes or if the other
|
|
|
|
|
// side doesn't support scanning QR codes bail early.
|
2021-06-19 23:35:28 +00:00
|
|
|
|
if !self.state.our_methods.contains(&VerificationMethod::QrCodeShowV1)
|
|
|
|
|
|| !self.state.their_methods.contains(&VerificationMethod::QrCodeScanV1)
|
2021-06-14 15:51:13 +00:00
|
|
|
|
{
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:30:15 +00:00
|
|
|
|
let device = if let Some(device) =
|
|
|
|
|
self.store.get_device(&self.other_user_id, &self.state.other_device_id).await?
|
|
|
|
|
{
|
|
|
|
|
device
|
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
user_id = self.other_user_id.as_str(),
|
|
|
|
|
device_id = self.state.other_device_id.as_str(),
|
|
|
|
|
"Can't create a QR code, the device that accepted the \
|
|
|
|
|
verification doesn't exist"
|
|
|
|
|
);
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let identites = IdentitiesBeingVerified {
|
|
|
|
|
private_identity: self.private_cross_signing_identity.clone(),
|
|
|
|
|
store: self.store.clone(),
|
|
|
|
|
device_being_verified: device,
|
|
|
|
|
identity_being_verified: self.store.get_user_identity(&self.other_user_id).await?,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let verification = if let Some(identity) = &identites.identity_being_verified {
|
|
|
|
|
match &identity {
|
2021-07-08 10:30:30 +00:00
|
|
|
|
ReadOnlyUserIdentities::Own(i) => {
|
2021-07-01 08:27:45 +00:00
|
|
|
|
if let Some(master_key) = i.master_key().get_first_key() {
|
|
|
|
|
if identites.can_sign_devices().await {
|
|
|
|
|
if let Some(device_key) =
|
|
|
|
|
identites.other_device().get_key(DeviceKeyAlgorithm::Ed25519)
|
|
|
|
|
{
|
|
|
|
|
Some(QrVerification::new_self(
|
|
|
|
|
self.flow_id.as_ref().to_owned(),
|
|
|
|
|
master_key.to_owned(),
|
|
|
|
|
device_key.to_owned(),
|
|
|
|
|
identites,
|
|
|
|
|
we_started,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
Some(request_handle),
|
2021-07-01 08:27:45 +00:00
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
user_id = self.other_user_id.as_str(),
|
|
|
|
|
device_id = self.state.other_device_id.as_str(),
|
|
|
|
|
"Can't create a QR code, the other device \
|
|
|
|
|
doesn't have a valid device key"
|
|
|
|
|
);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Some(QrVerification::new_self_no_master(
|
|
|
|
|
self.store.clone(),
|
|
|
|
|
self.flow_id.as_ref().to_owned(),
|
|
|
|
|
master_key.to_owned(),
|
|
|
|
|
identites,
|
|
|
|
|
we_started,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
Some(request_handle),
|
2021-07-01 08:27:45 +00:00
|
|
|
|
))
|
|
|
|
|
}
|
2021-06-09 14:30:15 +00:00
|
|
|
|
} else {
|
2021-07-01 08:27:45 +00:00
|
|
|
|
warn!(
|
|
|
|
|
user_id = self.other_user_id.as_str(),
|
|
|
|
|
device_id = self.state.other_device_id.as_str(),
|
|
|
|
|
"Can't create a QR code, our cross signing identity \
|
|
|
|
|
doesn't contain a valid master key"
|
|
|
|
|
);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-08 10:30:30 +00:00
|
|
|
|
ReadOnlyUserIdentities::Other(i) => {
|
2021-07-01 08:27:45 +00:00
|
|
|
|
if let Some(other_master) = i.master_key().get_first_key() {
|
|
|
|
|
// TODO we can get the master key from the public
|
|
|
|
|
// identity if we don't have the private one and we
|
|
|
|
|
// trust the public one.
|
|
|
|
|
if let Some(own_master) = self
|
|
|
|
|
.private_cross_signing_identity
|
|
|
|
|
.master_public_key()
|
|
|
|
|
.await
|
|
|
|
|
.and_then(|m| m.get_first_key().map(|m| m.to_owned()))
|
|
|
|
|
{
|
|
|
|
|
Some(QrVerification::new_cross(
|
|
|
|
|
self.flow_id.as_ref().to_owned(),
|
|
|
|
|
own_master,
|
|
|
|
|
other_master.to_owned(),
|
|
|
|
|
identites,
|
|
|
|
|
we_started,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
Some(request_handle),
|
2021-07-01 08:27:45 +00:00
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
user_id = self.other_user_id.as_str(),
|
|
|
|
|
device_id = self.state.other_device_id.as_str(),
|
|
|
|
|
"Can't create a QR code, we don't trust our own \
|
|
|
|
|
master key"
|
|
|
|
|
);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
user_id = self.other_user_id.as_str(),
|
|
|
|
|
device_id = self.state.other_device_id.as_str(),
|
|
|
|
|
"Can't create a QR code, the user's identity \
|
|
|
|
|
doesn't have a valid master key"
|
|
|
|
|
);
|
|
|
|
|
None
|
2021-06-09 14:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
user_id = self.other_user_id.as_str(),
|
|
|
|
|
device_id = self.state.other_device_id.as_str(),
|
|
|
|
|
"Can't create a QR code, the user doesn't have a valid cross \
|
|
|
|
|
signing identity."
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(verification) = &verification {
|
|
|
|
|
self.verification_cache.insert_qr(verification.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(verification)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 14:00:03 +00:00
|
|
|
|
async fn receive_start(
|
2021-05-27 14:11:54 +00:00
|
|
|
|
&self,
|
|
|
|
|
sender: &UserId,
|
2021-06-03 14:00:03 +00:00
|
|
|
|
content: &StartContent<'_>,
|
2021-06-25 10:51:45 +00:00
|
|
|
|
we_started: bool,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
request_handle: RequestHandle,
|
2021-05-27 14:11:54 +00:00
|
|
|
|
) -> Result<(), CryptoStoreError> {
|
|
|
|
|
info!(
|
|
|
|
|
sender = sender.as_str(),
|
|
|
|
|
device = content.from_device().as_str(),
|
|
|
|
|
"Received a new verification start event",
|
|
|
|
|
);
|
|
|
|
|
|
2021-06-07 13:34:23 +00:00
|
|
|
|
let device = if let Some(d) = self.store.get_device(sender, content.from_device()).await? {
|
2021-05-27 14:11:54 +00:00
|
|
|
|
d
|
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
sender = sender.as_str(),
|
|
|
|
|
device = content.from_device().as_str(),
|
|
|
|
|
"Received a key verification start event from an unknown device",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-07 13:34:23 +00:00
|
|
|
|
let identity = self.store.get_user_identity(sender).await?;
|
2021-08-12 12:13:22 +00:00
|
|
|
|
let own_identity = self
|
|
|
|
|
.store
|
|
|
|
|
.get_user_identity(self.store.account.user_id())
|
|
|
|
|
.await?
|
|
|
|
|
.and_then(|i| i.into_own());
|
2021-05-27 14:11:54 +00:00
|
|
|
|
|
|
|
|
|
match content.method() {
|
2021-06-25 10:51:45 +00:00
|
|
|
|
StartMethod::SasV1(_) => {
|
2021-07-09 11:53:47 +00:00
|
|
|
|
match self.to_started_sas(
|
|
|
|
|
content,
|
|
|
|
|
device.clone(),
|
2021-08-12 12:13:22 +00:00
|
|
|
|
own_identity,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
identity,
|
|
|
|
|
we_started,
|
|
|
|
|
request_handle,
|
|
|
|
|
) {
|
2021-06-25 10:51:45 +00:00
|
|
|
|
// TODO check if there is already a SAS verification, i.e. we
|
|
|
|
|
// already started one before the other side tried to do the
|
|
|
|
|
// same; ignore it if we did and we're the lexicographically
|
|
|
|
|
// smaller user ID, otherwise auto-accept the newly started one.
|
|
|
|
|
Ok(s) => {
|
|
|
|
|
info!("Started a new SAS verification.");
|
|
|
|
|
self.verification_cache.insert_sas(s);
|
|
|
|
|
}
|
|
|
|
|
Err(c) => {
|
|
|
|
|
warn!(
|
|
|
|
|
user_id = device.user_id().as_str(),
|
|
|
|
|
device_id = device.device_id().as_str(),
|
|
|
|
|
content =? c,
|
|
|
|
|
"Can't start key verification, canceling.",
|
|
|
|
|
);
|
|
|
|
|
self.verification_cache.queue_up_content(
|
|
|
|
|
device.user_id(),
|
|
|
|
|
device.device_id(),
|
|
|
|
|
c,
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-05-27 14:11:54 +00:00
|
|
|
|
}
|
2021-06-25 10:51:45 +00:00
|
|
|
|
}
|
2021-09-08 15:23:10 +00:00
|
|
|
|
#[cfg(feature = "qrcode")]
|
2021-06-09 14:30:15 +00:00
|
|
|
|
StartMethod::ReciprocateV1(_) => {
|
|
|
|
|
if let Some(qr_verification) =
|
|
|
|
|
self.verification_cache.get_qr(sender, content.flow_id())
|
|
|
|
|
{
|
|
|
|
|
if let Some(request) = qr_verification.receive_reciprocation(content) {
|
|
|
|
|
self.verification_cache.add_request(request.into())
|
|
|
|
|
}
|
|
|
|
|
trace!(
|
|
|
|
|
sender = device.user_id().as_str(),
|
|
|
|
|
device_id = device.device_id().as_str(),
|
|
|
|
|
verification =? qr_verification,
|
|
|
|
|
"Received a QR code reciprocation"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-27 14:11:54 +00:00
|
|
|
|
m => {
|
2021-06-05 12:35:20 +00:00
|
|
|
|
warn!(method =? m, "Received a key verification start event with an unsupported method")
|
2021-05-27 14:11:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 15:49:18 +00:00
|
|
|
|
async fn start_sas(
|
2020-12-10 16:49:28 +00:00
|
|
|
|
self,
|
2021-08-04 09:07:28 +00:00
|
|
|
|
store: VerificationStore,
|
2020-12-24 14:22:51 +00:00
|
|
|
|
private_identity: PrivateCrossSigningIdentity,
|
2021-06-25 10:51:45 +00:00
|
|
|
|
we_started: bool,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
request_handle: RequestHandle,
|
2021-06-14 15:49:18 +00:00
|
|
|
|
) -> Result<Option<(Sas, OutgoingContent)>, CryptoStoreError> {
|
2021-06-19 23:35:28 +00:00
|
|
|
|
if !self.state.their_methods.contains(&VerificationMethod::SasV1) {
|
2021-06-14 15:49:18 +00:00
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO signal why starting the sas flow doesn't work?
|
|
|
|
|
let other_identity = store.get_user_identity(&self.other_user_id).await?;
|
2021-08-12 12:13:22 +00:00
|
|
|
|
let own_identity = self
|
|
|
|
|
.store
|
|
|
|
|
.get_user_identity(self.store.account.user_id())
|
|
|
|
|
.await?
|
|
|
|
|
.and_then(|i| i.into_own());
|
2021-06-14 15:49:18 +00:00
|
|
|
|
|
|
|
|
|
let device = if let Some(device) =
|
|
|
|
|
self.store.get_device(&self.other_user_id, &self.state.other_device_id).await?
|
|
|
|
|
{
|
|
|
|
|
device
|
|
|
|
|
} else {
|
|
|
|
|
warn!(
|
|
|
|
|
user_id = self.other_user_id.as_str(),
|
|
|
|
|
device_id = self.state.other_device_id.as_str(),
|
|
|
|
|
"Can't start the SAS verificaiton flow, the device that \
|
|
|
|
|
accepted the verification doesn't exist"
|
|
|
|
|
);
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(Some(match self.flow_id.as_ref() {
|
2021-05-13 09:26:40 +00:00
|
|
|
|
FlowId::ToDevice(t) => {
|
2021-05-24 12:54:11 +00:00
|
|
|
|
let (sas, content) = Sas::start(
|
|
|
|
|
private_identity,
|
2021-06-14 15:49:18 +00:00
|
|
|
|
device,
|
2021-05-24 12:54:11 +00:00
|
|
|
|
store,
|
2021-08-12 12:13:22 +00:00
|
|
|
|
own_identity,
|
2021-05-24 12:54:11 +00:00
|
|
|
|
other_identity,
|
2021-06-14 15:28:10 +00:00
|
|
|
|
Some(t.to_owned()),
|
2021-06-25 10:51:45 +00:00
|
|
|
|
we_started,
|
2021-07-19 07:32:48 +00:00
|
|
|
|
Some(request_handle),
|
2021-05-24 12:54:11 +00:00
|
|
|
|
);
|
2021-06-03 14:00:03 +00:00
|
|
|
|
(sas, content)
|
2021-05-24 12:54:11 +00:00
|
|
|
|
}
|
|
|
|
|
FlowId::InRoom(r, e) => {
|
|
|
|
|
let (sas, content) = Sas::start_in_room(
|
2021-06-14 15:28:10 +00:00
|
|
|
|
e.to_owned(),
|
|
|
|
|
r.to_owned(),
|
2021-05-24 12:54:11 +00:00
|
|
|
|
private_identity,
|
2021-06-14 15:49:18 +00:00
|
|
|
|
device,
|
2021-05-24 12:54:11 +00:00
|
|
|
|
store,
|
2021-08-12 12:13:22 +00:00
|
|
|
|
own_identity,
|
2021-05-24 12:54:11 +00:00
|
|
|
|
other_identity,
|
2021-06-25 10:51:45 +00:00
|
|
|
|
we_started,
|
2021-07-09 11:53:47 +00:00
|
|
|
|
request_handle,
|
2021-05-24 12:54:11 +00:00
|
|
|
|
);
|
2021-06-03 14:00:03 +00:00
|
|
|
|
(sas, content)
|
2021-05-13 09:26:40 +00:00
|
|
|
|
}
|
2021-06-14 15:49:18 +00:00
|
|
|
|
}))
|
2020-12-10 16:49:28 +00:00
|
|
|
|
}
|
2020-12-09 16:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
struct Passive {
|
|
|
|
|
/// The device id of the device that responded to the verification request.
|
2021-09-13 08:18:23 +00:00
|
|
|
|
#[allow(dead_code)]
|
2020-12-09 16:18:23 +00:00
|
|
|
|
pub other_device_id: DeviceIdBox,
|
|
|
|
|
}
|
2020-12-18 17:23:42 +00:00
|
|
|
|
|
2021-06-04 15:07:11 +00:00
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
struct Done {}
|
|
|
|
|
|
2020-12-18 17:23:42 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
2021-07-09 15:01:35 +00:00
|
|
|
|
use std::convert::{TryFrom, TryInto};
|
2020-12-18 17:23:42 +00:00
|
|
|
|
|
|
|
|
|
use matrix_sdk_test::async_test;
|
2021-06-07 12:47:59 +00:00
|
|
|
|
use ruma::{event_id, room_id, DeviceIdBox, UserId};
|
2020-12-18 17:23:42 +00:00
|
|
|
|
|
2021-06-03 14:00:03 +00:00
|
|
|
|
use super::VerificationRequest;
|
2020-12-18 17:23:42 +00:00
|
|
|
|
use crate::{
|
|
|
|
|
olm::{PrivateCrossSigningIdentity, ReadOnlyAccount},
|
2021-05-27 14:11:54 +00:00
|
|
|
|
store::{Changes, CryptoStore, MemoryStore},
|
2021-06-03 14:00:03 +00:00
|
|
|
|
verification::{
|
2021-06-04 16:09:20 +00:00
|
|
|
|
cache::VerificationCache,
|
2021-07-12 16:12:02 +00:00
|
|
|
|
event_enums::{OutgoingContent, ReadyContent, RequestContent, StartContent},
|
2021-08-04 09:07:28 +00:00
|
|
|
|
FlowId, VerificationStore,
|
2021-06-03 14:00:03 +00:00
|
|
|
|
},
|
2020-12-24 14:22:51 +00:00
|
|
|
|
ReadOnlyDevice,
|
2020-12-18 17:23:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fn alice_id() -> UserId {
|
|
|
|
|
UserId::try_from("@alice:example.org").unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn alice_device_id() -> DeviceIdBox {
|
|
|
|
|
"JLAFKJWSCS".into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn bob_id() -> UserId {
|
|
|
|
|
UserId::try_from("@bob:example.org").unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn bob_device_id() -> DeviceIdBox {
|
|
|
|
|
"BOBDEVCIE".into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
|
async fn test_request_accepting() {
|
|
|
|
|
let event_id = event_id!("$1234localhost");
|
|
|
|
|
let room_id = room_id!("!test:localhost");
|
|
|
|
|
|
|
|
|
|
let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id());
|
|
|
|
|
let alice_store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
|
|
|
|
|
let alice_identity = PrivateCrossSigningIdentity::empty(alice_id());
|
|
|
|
|
|
2021-08-13 11:18:34 +00:00
|
|
|
|
let alice_store = VerificationStore { account: alice, inner: alice_store.into() };
|
2021-08-04 09:07:28 +00:00
|
|
|
|
|
2020-12-18 17:23:42 +00:00
|
|
|
|
let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id());
|
|
|
|
|
let bob_store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
|
|
|
|
|
let bob_identity = PrivateCrossSigningIdentity::empty(alice_id());
|
|
|
|
|
|
2021-08-04 09:07:28 +00:00
|
|
|
|
let bob_store = VerificationStore { account: bob.clone(), inner: bob_store.into() };
|
|
|
|
|
|
2021-07-08 10:30:30 +00:00
|
|
|
|
let content =
|
|
|
|
|
VerificationRequest::request(bob.user_id(), bob.device_id(), &alice_id(), None);
|
|
|
|
|
|
|
|
|
|
let flow_id = FlowId::InRoom(room_id, event_id);
|
2021-05-13 09:15:56 +00:00
|
|
|
|
|
2020-12-18 17:23:42 +00:00
|
|
|
|
let bob_request = VerificationRequest::new(
|
2021-05-24 12:54:11 +00:00
|
|
|
|
VerificationCache::new(),
|
2020-12-18 17:23:42 +00:00
|
|
|
|
bob_identity,
|
2021-08-04 09:07:28 +00:00
|
|
|
|
bob_store,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
flow_id.clone(),
|
2020-12-18 17:23:42 +00:00
|
|
|
|
&alice_id(),
|
2021-07-12 16:12:02 +00:00
|
|
|
|
vec![],
|
2021-07-08 10:30:30 +00:00
|
|
|
|
None,
|
2020-12-18 17:23:42 +00:00
|
|
|
|
);
|
|
|
|
|
|
2021-06-03 14:00:03 +00:00
|
|
|
|
let alice_request = VerificationRequest::from_request(
|
2021-05-24 12:54:11 +00:00
|
|
|
|
VerificationCache::new(),
|
2020-12-18 17:23:42 +00:00
|
|
|
|
alice_identity,
|
2021-08-13 11:18:34 +00:00
|
|
|
|
alice_store,
|
2020-12-18 17:23:42 +00:00
|
|
|
|
&bob_id(),
|
2021-06-03 14:00:03 +00:00
|
|
|
|
flow_id,
|
|
|
|
|
&(&content).into(),
|
2020-12-18 17:23:42 +00:00
|
|
|
|
);
|
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
let content: OutgoingContent = alice_request.accept().unwrap().try_into().unwrap();
|
2021-05-13 09:15:56 +00:00
|
|
|
|
let content = ReadyContent::try_from(&content).unwrap();
|
2020-12-18 17:23:42 +00:00
|
|
|
|
|
2021-06-14 15:16:40 +00:00
|
|
|
|
bob_request.receive_ready(&alice_id(), &content);
|
2020-12-18 17:23:42 +00:00
|
|
|
|
|
|
|
|
|
assert!(bob_request.is_ready());
|
|
|
|
|
assert!(alice_request.is_ready());
|
|
|
|
|
}
|
2020-12-24 14:22:51 +00:00
|
|
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
|
async fn test_requesting_until_sas() {
|
|
|
|
|
let event_id = event_id!("$1234localhost");
|
|
|
|
|
let room_id = room_id!("!test:localhost");
|
|
|
|
|
|
|
|
|
|
let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id());
|
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&alice).await;
|
|
|
|
|
|
|
|
|
|
let alice_store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
|
|
|
|
|
let alice_identity = PrivateCrossSigningIdentity::empty(alice_id());
|
|
|
|
|
|
2021-08-04 09:07:28 +00:00
|
|
|
|
let alice_store = VerificationStore { account: alice.clone(), inner: alice_store.into() };
|
|
|
|
|
|
2020-12-24 14:22:51 +00:00
|
|
|
|
let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id());
|
|
|
|
|
let bob_device = ReadOnlyDevice::from_account(&bob).await;
|
|
|
|
|
let bob_store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
|
|
|
|
|
let bob_identity = PrivateCrossSigningIdentity::empty(alice_id());
|
|
|
|
|
|
2021-08-04 09:07:28 +00:00
|
|
|
|
let bob_store = VerificationStore { account: bob.clone(), inner: bob_store.into() };
|
|
|
|
|
|
2021-05-27 14:11:54 +00:00
|
|
|
|
let mut changes = Changes::default();
|
|
|
|
|
changes.devices.new.push(bob_device.clone());
|
|
|
|
|
alice_store.save_changes(changes).await.unwrap();
|
|
|
|
|
|
2021-06-14 15:49:18 +00:00
|
|
|
|
let mut changes = Changes::default();
|
|
|
|
|
changes.devices.new.push(alice_device.clone());
|
|
|
|
|
bob_store.save_changes(changes).await.unwrap();
|
|
|
|
|
|
2021-07-08 10:30:30 +00:00
|
|
|
|
let content =
|
|
|
|
|
VerificationRequest::request(bob.user_id(), bob.device_id(), &alice_id(), None);
|
|
|
|
|
let flow_id = FlowId::from((room_id, event_id));
|
2021-05-13 09:15:56 +00:00
|
|
|
|
|
2020-12-24 14:22:51 +00:00
|
|
|
|
let bob_request = VerificationRequest::new(
|
2021-05-24 12:54:11 +00:00
|
|
|
|
VerificationCache::new(),
|
2020-12-24 14:22:51 +00:00
|
|
|
|
bob_identity,
|
2021-08-13 11:18:34 +00:00
|
|
|
|
bob_store,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
flow_id.clone(),
|
2020-12-24 14:22:51 +00:00
|
|
|
|
&alice_id(),
|
2021-07-12 16:12:02 +00:00
|
|
|
|
vec![],
|
2021-07-08 10:30:30 +00:00
|
|
|
|
None,
|
2020-12-24 14:22:51 +00:00
|
|
|
|
);
|
|
|
|
|
|
2021-06-03 14:00:03 +00:00
|
|
|
|
let alice_request = VerificationRequest::from_request(
|
2021-06-03 15:23:40 +00:00
|
|
|
|
VerificationCache::new(),
|
|
|
|
|
alice_identity,
|
2021-08-13 11:18:34 +00:00
|
|
|
|
alice_store,
|
2021-06-03 15:23:40 +00:00
|
|
|
|
&bob_id(),
|
|
|
|
|
flow_id,
|
|
|
|
|
&(&content).into(),
|
|
|
|
|
);
|
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
let content: OutgoingContent = alice_request.accept().unwrap().try_into().unwrap();
|
2021-06-03 15:23:40 +00:00
|
|
|
|
let content = ReadyContent::try_from(&content).unwrap();
|
|
|
|
|
|
2021-06-14 15:16:40 +00:00
|
|
|
|
bob_request.receive_ready(&alice_id(), &content);
|
2021-06-03 15:23:40 +00:00
|
|
|
|
|
|
|
|
|
assert!(bob_request.is_ready());
|
|
|
|
|
assert!(alice_request.is_ready());
|
|
|
|
|
|
2021-06-14 15:49:18 +00:00
|
|
|
|
let (bob_sas, request) = bob_request.start_sas().await.unwrap().unwrap();
|
2021-06-03 15:23:40 +00:00
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
let content: OutgoingContent = request.try_into().unwrap();
|
2021-06-14 15:49:18 +00:00
|
|
|
|
let content = StartContent::try_from(&content).unwrap();
|
2021-06-03 15:23:40 +00:00
|
|
|
|
let flow_id = content.flow_id().to_owned();
|
|
|
|
|
alice_request.receive_start(bob_device.user_id(), &content).await.unwrap();
|
2021-06-08 14:13:14 +00:00
|
|
|
|
let alice_sas =
|
|
|
|
|
alice_request.verification_cache.get_sas(bob_device.user_id(), &flow_id).unwrap();
|
2021-06-03 15:23:40 +00:00
|
|
|
|
|
|
|
|
|
assert!(!bob_sas.is_cancelled());
|
|
|
|
|
assert!(!alice_sas.is_cancelled());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
|
async fn test_requesting_until_sas_to_device() {
|
|
|
|
|
let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id());
|
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&alice).await;
|
|
|
|
|
|
|
|
|
|
let alice_store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
|
|
|
|
|
let alice_identity = PrivateCrossSigningIdentity::empty(alice_id());
|
|
|
|
|
|
2021-08-04 09:07:28 +00:00
|
|
|
|
let alice_store = VerificationStore { account: alice.clone(), inner: alice_store.into() };
|
|
|
|
|
|
2021-06-03 15:23:40 +00:00
|
|
|
|
let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id());
|
|
|
|
|
let bob_device = ReadOnlyDevice::from_account(&bob).await;
|
|
|
|
|
let bob_store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
|
|
|
|
|
let bob_identity = PrivateCrossSigningIdentity::empty(alice_id());
|
|
|
|
|
|
|
|
|
|
let mut changes = Changes::default();
|
|
|
|
|
changes.devices.new.push(bob_device.clone());
|
|
|
|
|
alice_store.save_changes(changes).await.unwrap();
|
|
|
|
|
|
2021-06-14 15:49:18 +00:00
|
|
|
|
let mut changes = Changes::default();
|
|
|
|
|
changes.devices.new.push(alice_device.clone());
|
|
|
|
|
bob_store.save_changes(changes).await.unwrap();
|
|
|
|
|
|
2021-08-04 09:07:28 +00:00
|
|
|
|
let bob_store = VerificationStore { account: bob.clone(), inner: bob_store.into() };
|
|
|
|
|
|
2021-07-08 10:30:30 +00:00
|
|
|
|
let flow_id = FlowId::from("TEST_FLOW_ID".to_owned());
|
|
|
|
|
|
|
|
|
|
let bob_request = VerificationRequest::new(
|
2021-06-03 15:23:40 +00:00
|
|
|
|
VerificationCache::new(),
|
|
|
|
|
bob_identity,
|
2021-08-13 11:18:34 +00:00
|
|
|
|
bob_store,
|
2021-07-08 10:30:30 +00:00
|
|
|
|
flow_id,
|
2021-06-03 15:23:40 +00:00
|
|
|
|
&alice_id(),
|
2021-07-12 16:12:02 +00:00
|
|
|
|
vec![],
|
2021-07-08 10:30:30 +00:00
|
|
|
|
None,
|
2021-06-03 15:23:40 +00:00
|
|
|
|
);
|
|
|
|
|
|
2021-07-12 16:12:02 +00:00
|
|
|
|
let request = bob_request.request_to_device();
|
|
|
|
|
let content: OutgoingContent = request.try_into().unwrap();
|
|
|
|
|
let content = RequestContent::try_from(&content).unwrap();
|
2021-06-03 15:23:40 +00:00
|
|
|
|
let flow_id = bob_request.flow_id().to_owned();
|
|
|
|
|
|
|
|
|
|
let alice_request = VerificationRequest::from_request(
|
2021-05-24 12:54:11 +00:00
|
|
|
|
VerificationCache::new(),
|
2020-12-24 14:22:51 +00:00
|
|
|
|
alice_identity,
|
2021-08-13 11:18:34 +00:00
|
|
|
|
alice_store,
|
2020-12-24 14:22:51 +00:00
|
|
|
|
&bob_id(),
|
2021-06-03 14:00:03 +00:00
|
|
|
|
flow_id,
|
2021-07-12 16:12:02 +00:00
|
|
|
|
&content,
|
2020-12-24 14:22:51 +00:00
|
|
|
|
);
|
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
let content: OutgoingContent = alice_request.accept().unwrap().try_into().unwrap();
|
2021-05-13 09:15:56 +00:00
|
|
|
|
let content = ReadyContent::try_from(&content).unwrap();
|
2020-12-24 14:22:51 +00:00
|
|
|
|
|
2021-06-14 15:16:40 +00:00
|
|
|
|
bob_request.receive_ready(&alice_id(), &content);
|
2020-12-24 14:22:51 +00:00
|
|
|
|
|
|
|
|
|
assert!(bob_request.is_ready());
|
|
|
|
|
assert!(alice_request.is_ready());
|
|
|
|
|
|
2021-06-14 15:49:18 +00:00
|
|
|
|
let (bob_sas, request) = bob_request.start_sas().await.unwrap().unwrap();
|
2020-12-24 14:22:51 +00:00
|
|
|
|
|
2021-07-09 15:01:35 +00:00
|
|
|
|
let content: OutgoingContent = request.try_into().unwrap();
|
2021-06-14 15:49:18 +00:00
|
|
|
|
let content = StartContent::try_from(&content).unwrap();
|
2021-05-27 14:11:54 +00:00
|
|
|
|
let flow_id = content.flow_id().to_owned();
|
2021-06-03 14:00:03 +00:00
|
|
|
|
alice_request.receive_start(bob_device.user_id(), &content).await.unwrap();
|
2021-06-08 14:13:14 +00:00
|
|
|
|
let alice_sas =
|
|
|
|
|
alice_request.verification_cache.get_sas(bob_device.user_id(), &flow_id).unwrap();
|
2020-12-24 14:22:51 +00:00
|
|
|
|
|
2021-05-26 09:54:22 +00:00
|
|
|
|
assert!(!bob_sas.is_cancelled());
|
|
|
|
|
assert!(!alice_sas.is_cancelled());
|
2020-12-24 14:22:51 +00:00
|
|
|
|
}
|
2020-12-18 17:23:42 +00:00
|
|
|
|
}
|