2020-09-17 14:09:08 +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.
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
//
|
2020-10-07 09:42:27 +00:00
|
|
|
// handle the case where we can't create a session with a device. clearing our
|
|
|
|
// stale key share requests that we'll never be able to handle.
|
2020-09-17 14:09:08 +00:00
|
|
|
//
|
|
|
|
// If we don't trust the device store an object that remembers the request and
|
|
|
|
// let the users introspect that object.
|
2020-09-28 07:27:16 +00:00
|
|
|
|
2020-10-07 10:42:39 +00:00
|
|
|
use dashmap::{mapref::entry::Entry, DashMap, DashSet};
|
2020-09-17 12:16:43 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-10-01 10:15:13 +00:00
|
|
|
use serde_json::value::to_raw_value;
|
|
|
|
use std::{collections::BTreeMap, sync::Arc};
|
2020-09-28 12:12:08 +00:00
|
|
|
use thiserror::Error;
|
2020-10-19 16:23:48 +00:00
|
|
|
use tracing::{error, info, trace, warn};
|
2020-09-17 12:16:43 +00:00
|
|
|
|
|
|
|
use matrix_sdk_common::{
|
|
|
|
api::r0::to_device::DeviceIdOrAllDevices,
|
|
|
|
events::{
|
2020-12-04 12:35:56 +00:00
|
|
|
forwarded_room_key::ForwardedRoomKeyToDeviceEventContent,
|
|
|
|
room_key_request::{Action, RequestedKeyInfo, RoomKeyRequestToDeviceEventContent},
|
2020-09-24 09:18:01 +00:00
|
|
|
AnyToDeviceEvent, EventType, ToDeviceEvent,
|
2020-09-17 12:16:43 +00:00
|
|
|
},
|
2020-10-06 14:38:42 +00:00
|
|
|
identifiers::{DeviceId, DeviceIdBox, EventEncryptionAlgorithm, RoomId, UserId},
|
2020-09-17 12:16:43 +00:00
|
|
|
uuid::Uuid,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2020-10-06 14:38:42 +00:00
|
|
|
error::{OlmError, OlmResult},
|
2021-04-15 13:19:21 +00:00
|
|
|
olm::{InboundGroupSession, Session, ShareState},
|
2020-09-17 12:16:43 +00:00
|
|
|
requests::{OutgoingRequest, ToDeviceRequest},
|
2021-04-15 13:19:21 +00:00
|
|
|
session_manager::GroupSessionCache,
|
2021-04-15 10:45:51 +00:00
|
|
|
store::{Changes, CryptoStoreError, Store},
|
2020-10-01 10:15:13 +00:00
|
|
|
Device,
|
2020-09-17 12:16:43 +00:00
|
|
|
};
|
|
|
|
|
2020-09-28 12:12:08 +00:00
|
|
|
/// An error describing why a key share request won't be honored.
|
2020-09-28 12:51:57 +00:00
|
|
|
#[derive(Debug, Clone, Error, PartialEq)]
|
2020-09-28 12:12:08 +00:00
|
|
|
pub enum KeyshareDecision {
|
|
|
|
/// The key request is from a device that we don't own, we're only sharing
|
|
|
|
/// sessions that we know the requesting device already was supposed to get.
|
|
|
|
#[error("can't find an active outbound group session")]
|
|
|
|
MissingOutboundSession,
|
|
|
|
/// The key request is from a device that we don't own and the device wasn't
|
|
|
|
/// meant to receive the session in the original key share.
|
|
|
|
#[error("outbound session wasn't shared with the requesting device")]
|
|
|
|
OutboundSessionNotShared,
|
|
|
|
/// The key request is from a device we own, yet we don't trust it.
|
|
|
|
#[error("requesting device isn't trusted")]
|
|
|
|
UntrustedDevice,
|
|
|
|
}
|
|
|
|
|
2020-10-07 10:42:39 +00:00
|
|
|
/// A queue where we store room key requests that we want to serve but the
|
|
|
|
/// device that requested the key doesn't share an Olm session with us.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct WaitQueue {
|
2020-12-04 12:35:56 +00:00
|
|
|
requests_waiting_for_session: Arc<
|
|
|
|
DashMap<(UserId, DeviceIdBox, String), ToDeviceEvent<RoomKeyRequestToDeviceEventContent>>,
|
|
|
|
>,
|
2020-10-07 10:42:39 +00:00
|
|
|
requests_ids_waiting: Arc<DashMap<(UserId, DeviceIdBox), DashSet<String>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WaitQueue {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
requests_waiting_for_session: Arc::new(DashMap::new()),
|
|
|
|
requests_ids_waiting: Arc::new(DashMap::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.requests_ids_waiting.is_empty() && self.requests_waiting_for_session.is_empty()
|
|
|
|
}
|
|
|
|
|
2020-12-04 12:35:56 +00:00
|
|
|
fn insert(&self, device: &Device, event: &ToDeviceEvent<RoomKeyRequestToDeviceEventContent>) {
|
2020-10-07 10:42:39 +00:00
|
|
|
let key = (
|
|
|
|
device.user_id().to_owned(),
|
|
|
|
device.device_id().into(),
|
|
|
|
event.content.request_id.to_owned(),
|
|
|
|
);
|
|
|
|
self.requests_waiting_for_session.insert(key, event.clone());
|
|
|
|
|
|
|
|
let key = (device.user_id().to_owned(), device.device_id().into());
|
|
|
|
self.requests_ids_waiting
|
|
|
|
.entry(key)
|
|
|
|
.or_insert_with(DashSet::new)
|
|
|
|
.insert(event.content.request_id.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
device_id: &DeviceId,
|
|
|
|
) -> Vec<(
|
|
|
|
(UserId, DeviceIdBox, String),
|
2020-12-04 12:35:56 +00:00
|
|
|
ToDeviceEvent<RoomKeyRequestToDeviceEventContent>,
|
2020-10-07 10:42:39 +00:00
|
|
|
)> {
|
|
|
|
self.requests_ids_waiting
|
|
|
|
.remove(&(user_id.to_owned(), device_id.into()))
|
|
|
|
.map(|(_, request_ids)| {
|
|
|
|
request_ids
|
|
|
|
.iter()
|
|
|
|
.filter_map(|id| {
|
|
|
|
let key = (user_id.to_owned(), device_id.into(), id.to_owned());
|
|
|
|
self.requests_waiting_for_session.remove(&key)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 12:16:43 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-09-18 11:45:00 +00:00
|
|
|
pub(crate) struct KeyRequestMachine {
|
2020-09-17 12:16:43 +00:00
|
|
|
user_id: Arc<UserId>,
|
|
|
|
device_id: Arc<DeviceIdBox>,
|
|
|
|
store: Store,
|
2021-04-15 13:19:21 +00:00
|
|
|
outbound_group_sessions: GroupSessionCache,
|
2020-09-17 12:16:43 +00:00
|
|
|
outgoing_to_device_requests: Arc<DashMap<Uuid, OutgoingRequest>>,
|
2020-12-04 12:35:56 +00:00
|
|
|
incoming_key_requests: Arc<
|
|
|
|
DashMap<(UserId, DeviceIdBox, String), ToDeviceEvent<RoomKeyRequestToDeviceEventContent>>,
|
|
|
|
>,
|
2020-10-07 10:42:39 +00:00
|
|
|
wait_queue: WaitQueue,
|
2020-10-06 14:38:42 +00:00
|
|
|
users_for_key_claim: Arc<DashMap<UserId, DashSet<DeviceIdBox>>>,
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
/// A struct describing an outgoing key request.
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct OutgoingKeyRequest {
|
2021-04-19 13:00:21 +00:00
|
|
|
/// The user we requested the key from
|
|
|
|
pub request_recipient: UserId,
|
2021-04-15 10:45:51 +00:00
|
|
|
/// The unique id of the key request.
|
|
|
|
pub request_id: Uuid,
|
|
|
|
/// The info of the requested key.
|
|
|
|
pub info: RequestedKeyInfo,
|
|
|
|
/// Has the request been sent out.
|
|
|
|
pub sent_out: bool,
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 15:48:37 +00:00
|
|
|
impl OutgoingKeyRequest {
|
2021-04-19 13:00:21 +00:00
|
|
|
fn to_request(&self, own_device_id: &DeviceId) -> Result<OutgoingRequest, serde_json::Error> {
|
|
|
|
let content = RoomKeyRequestToDeviceEventContent {
|
|
|
|
action: Action::Request,
|
|
|
|
request_id: self.request_id.to_string(),
|
|
|
|
requesting_device_id: own_device_id.to_owned(),
|
|
|
|
body: Some(self.info.clone()),
|
|
|
|
};
|
|
|
|
|
|
|
|
wrap_key_request_content(self.request_recipient.clone(), self.request_id, &content)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_cancelation(
|
2021-04-15 15:48:37 +00:00
|
|
|
&self,
|
|
|
|
own_device_id: &DeviceId,
|
|
|
|
) -> Result<OutgoingRequest, serde_json::Error> {
|
|
|
|
let content = RoomKeyRequestToDeviceEventContent {
|
2021-04-19 13:00:21 +00:00
|
|
|
action: Action::CancelRequest,
|
2021-04-15 15:48:37 +00:00
|
|
|
request_id: self.request_id.to_string(),
|
|
|
|
requesting_device_id: own_device_id.to_owned(),
|
2021-04-19 13:00:21 +00:00
|
|
|
body: None,
|
2021-04-15 15:48:37 +00:00
|
|
|
};
|
|
|
|
|
2021-04-19 13:00:21 +00:00
|
|
|
let id = Uuid::new_v4();
|
|
|
|
wrap_key_request_content(self.request_recipient.clone(), id, &content)
|
2021-04-15 15:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
impl PartialEq for OutgoingKeyRequest {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.request_id == other.request_id
|
|
|
|
&& self.info.algorithm == other.info.algorithm
|
|
|
|
&& self.info.room_id == other.info.room_id
|
|
|
|
&& self.info.session_id == other.info.session_id
|
|
|
|
&& self.info.sender_key == other.info.sender_key
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 11:45:00 +00:00
|
|
|
fn wrap_key_request_content(
|
|
|
|
recipient: UserId,
|
|
|
|
id: Uuid,
|
2020-12-04 12:35:56 +00:00
|
|
|
content: &RoomKeyRequestToDeviceEventContent,
|
2020-09-18 11:45:00 +00:00
|
|
|
) -> Result<OutgoingRequest, serde_json::Error> {
|
|
|
|
let mut messages = BTreeMap::new();
|
|
|
|
|
|
|
|
messages
|
|
|
|
.entry(recipient)
|
|
|
|
.or_insert_with(BTreeMap::new)
|
|
|
|
.insert(DeviceIdOrAllDevices::AllDevices, to_raw_value(content)?);
|
|
|
|
|
|
|
|
Ok(OutgoingRequest {
|
|
|
|
request_id: id,
|
|
|
|
request: Arc::new(
|
|
|
|
ToDeviceRequest {
|
|
|
|
event_type: EventType::RoomKeyRequest,
|
|
|
|
txn_id: id,
|
|
|
|
messages,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-17 12:16:43 +00:00
|
|
|
impl KeyRequestMachine {
|
2020-09-29 08:09:47 +00:00
|
|
|
pub fn new(
|
|
|
|
user_id: Arc<UserId>,
|
|
|
|
device_id: Arc<DeviceIdBox>,
|
|
|
|
store: Store,
|
2021-04-15 13:19:21 +00:00
|
|
|
outbound_group_sessions: GroupSessionCache,
|
2020-10-09 09:36:31 +00:00
|
|
|
users_for_key_claim: Arc<DashMap<UserId, DashSet<DeviceIdBox>>>,
|
2020-09-29 08:09:47 +00:00
|
|
|
) -> Self {
|
2020-09-17 14:09:08 +00:00
|
|
|
Self {
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
store,
|
2020-09-29 08:09:47 +00:00
|
|
|
outbound_group_sessions,
|
2021-04-15 17:40:24 +00:00
|
|
|
outgoing_to_device_requests: DashMap::new().into(),
|
|
|
|
incoming_key_requests: DashMap::new().into(),
|
2020-10-07 10:42:39 +00:00
|
|
|
wait_queue: WaitQueue::new(),
|
2020-10-09 09:36:31 +00:00
|
|
|
users_for_key_claim,
|
2020-09-17 14:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
/// Load stored outgoing requests that were not yet sent out.
|
|
|
|
async fn load_outgoing_requests(&self) -> Result<Vec<OutgoingRequest>, CryptoStoreError> {
|
|
|
|
self.store
|
|
|
|
.get_unsent_key_requests()
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
2021-04-15 15:48:37 +00:00
|
|
|
.filter(|i| !i.sent_out)
|
2021-04-15 17:40:24 +00:00
|
|
|
.map(|info| {
|
2021-04-19 13:00:21 +00:00
|
|
|
info.to_request(self.device_id())
|
2021-04-15 17:40:24 +00:00
|
|
|
.map_err(CryptoStoreError::from)
|
2021-04-15 15:48:37 +00:00
|
|
|
})
|
2021-04-15 17:40:24 +00:00
|
|
|
.collect()
|
2021-04-15 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 12:12:08 +00:00
|
|
|
/// Our own user id.
|
|
|
|
pub fn user_id(&self) -> &UserId {
|
2020-09-18 11:45:00 +00:00
|
|
|
&self.user_id
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:17:09 +00:00
|
|
|
/// Our own device id.
|
|
|
|
pub fn device_id(&self) -> &DeviceId {
|
|
|
|
&self.device_id
|
|
|
|
}
|
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
pub async fn outgoing_to_device_requests(
|
|
|
|
&self,
|
|
|
|
) -> Result<Vec<OutgoingRequest>, CryptoStoreError> {
|
|
|
|
let mut key_requests = self.load_outgoing_requests().await?;
|
|
|
|
let key_forwards: Vec<OutgoingRequest> = self
|
|
|
|
.outgoing_to_device_requests
|
2020-09-18 11:45:00 +00:00
|
|
|
.iter()
|
2021-04-15 17:40:24 +00:00
|
|
|
.map(|i| i.value().clone())
|
|
|
|
.collect();
|
|
|
|
key_requests.extend(key_forwards);
|
|
|
|
|
|
|
|
Ok(key_requests)
|
2020-09-18 11:45:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 07:27:16 +00:00
|
|
|
/// Receive a room key request event.
|
2020-12-04 12:35:56 +00:00
|
|
|
pub fn receive_incoming_key_request(
|
|
|
|
&self,
|
|
|
|
event: &ToDeviceEvent<RoomKeyRequestToDeviceEventContent>,
|
|
|
|
) {
|
2020-09-28 07:27:16 +00:00
|
|
|
let sender = event.sender.clone();
|
|
|
|
let device_id = event.content.requesting_device_id.clone();
|
|
|
|
let request_id = event.content.request_id.clone();
|
2020-10-06 14:38:42 +00:00
|
|
|
|
2020-09-28 07:27:16 +00:00
|
|
|
self.incoming_key_requests
|
|
|
|
.insert((sender, device_id, request_id), event.clone());
|
|
|
|
}
|
|
|
|
|
2020-09-28 11:32:30 +00:00
|
|
|
/// Handle all the incoming key requests that are queued up and empty our
|
|
|
|
/// key request queue.
|
2020-10-20 15:19:37 +00:00
|
|
|
pub async fn collect_incoming_key_requests(&self) -> OlmResult<Vec<Session>> {
|
|
|
|
let mut changed_sessions = Vec::new();
|
2021-04-15 10:45:51 +00:00
|
|
|
|
2020-09-28 07:27:16 +00:00
|
|
|
for item in self.incoming_key_requests.iter() {
|
|
|
|
let event = item.value();
|
2020-10-20 15:19:37 +00:00
|
|
|
if let Some(s) = self.handle_key_request(event).await? {
|
|
|
|
changed_sessions.push(s);
|
|
|
|
}
|
2020-09-28 07:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.incoming_key_requests.clear();
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok(changed_sessions)
|
2020-09-28 07:27:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 10:42:39 +00:00
|
|
|
/// Store the key share request for later, once we get an Olm session with
|
|
|
|
/// the given device [`retry_keyshare`](#method.retry_keyshare) should be
|
|
|
|
/// called.
|
2020-10-06 14:38:42 +00:00
|
|
|
fn handle_key_share_without_session(
|
|
|
|
&self,
|
|
|
|
device: Device,
|
2020-12-04 12:35:56 +00:00
|
|
|
event: &ToDeviceEvent<RoomKeyRequestToDeviceEventContent>,
|
2020-10-06 14:38:42 +00:00
|
|
|
) {
|
|
|
|
self.users_for_key_claim
|
|
|
|
.entry(device.user_id().to_owned())
|
|
|
|
.or_insert_with(DashSet::new)
|
|
|
|
.insert(device.device_id().into());
|
2020-10-07 10:42:39 +00:00
|
|
|
self.wait_queue.insert(&device, event);
|
2020-10-06 14:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retry keyshares for a device that previously didn't have an Olm session
|
|
|
|
/// with us.
|
|
|
|
///
|
2020-10-07 09:42:27 +00:00
|
|
|
/// This should be only called if the given user/device got a new Olm
|
|
|
|
/// session.
|
|
|
|
///
|
2020-10-06 14:38:42 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The user id of the device that we created the Olm session
|
|
|
|
/// with.
|
|
|
|
///
|
|
|
|
/// * `device_id` - The device id of the device that got the Olm session.
|
|
|
|
pub fn retry_keyshare(&self, user_id: &UserId, device_id: &DeviceId) {
|
2020-10-07 15:56:29 +00:00
|
|
|
if let Entry::Occupied(e) = self.users_for_key_claim.entry(user_id.to_owned()) {
|
|
|
|
e.get().remove(device_id);
|
2020-10-07 10:42:39 +00:00
|
|
|
|
2020-10-07 15:56:29 +00:00
|
|
|
if e.get().is_empty() {
|
|
|
|
e.remove();
|
2020-10-06 14:38:42 +00:00
|
|
|
}
|
2020-10-07 10:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (key, event) in self.wait_queue.remove(user_id, device_id) {
|
|
|
|
if !self.incoming_key_requests.contains_key(&key) {
|
|
|
|
self.incoming_key_requests.insert(key, event);
|
|
|
|
}
|
2020-10-06 14:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:12:08 +00:00
|
|
|
/// Handle a single incoming key request.
|
2020-09-28 07:27:16 +00:00
|
|
|
async fn handle_key_request(
|
|
|
|
&self,
|
2020-12-04 12:35:56 +00:00
|
|
|
event: &ToDeviceEvent<RoomKeyRequestToDeviceEventContent>,
|
2020-10-20 15:19:37 +00:00
|
|
|
) -> OlmResult<Option<Session>> {
|
2021-04-13 15:17:09 +00:00
|
|
|
// Some servers might send to-device events to ourselves if we send one
|
|
|
|
// out using a wildcard instead of a specific device as a recipient.
|
|
|
|
//
|
|
|
|
// Check if we're the sender of this key request event and ignore it if
|
|
|
|
// so.
|
|
|
|
if &event.sender == self.user_id()
|
|
|
|
&& &*event.content.requesting_device_id == self.device_id()
|
|
|
|
{
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:46:23 +00:00
|
|
|
let key_info = match &event.content.action {
|
2020-09-28 12:12:08 +00:00
|
|
|
Action::Request => {
|
|
|
|
if let Some(info) = &event.content.body {
|
|
|
|
info
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Received a key request from {} {} with a request \
|
|
|
|
action, but no key info was found",
|
|
|
|
event.sender, event.content.requesting_device_id
|
|
|
|
);
|
2020-10-20 15:19:37 +00:00
|
|
|
return Ok(None);
|
2020-09-28 12:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We ignore cancellations here since there's nothing to serve.
|
2020-10-20 15:19:37 +00:00
|
|
|
Action::CancelRequest => return Ok(None),
|
2020-11-20 18:46:23 +00:00
|
|
|
action => {
|
|
|
|
warn!("Unknown room key request action: {:?}", action);
|
2020-11-26 12:24:57 +00:00
|
|
|
return Ok(None);
|
2020-11-20 18:46:23 +00:00
|
|
|
}
|
2020-09-28 12:12:08 +00:00
|
|
|
};
|
|
|
|
|
2020-09-28 07:27:16 +00:00
|
|
|
let session = self
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
|
|
|
&key_info.room_id,
|
|
|
|
&key_info.sender_key,
|
|
|
|
&key_info.session_id,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let session = if let Some(s) = session {
|
|
|
|
s
|
|
|
|
} else {
|
2020-09-28 12:12:08 +00:00
|
|
|
info!(
|
|
|
|
"Received a key request from {} {} for an unknown inbound group session {}.",
|
|
|
|
&event.sender, &event.content.requesting_device_id, &key_info.session_id
|
|
|
|
);
|
2020-10-20 15:19:37 +00:00
|
|
|
return Ok(None);
|
2020-09-28 07:27:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let device = self
|
|
|
|
.store
|
2020-10-01 10:15:13 +00:00
|
|
|
.get_device(&event.sender, &event.content.requesting_device_id)
|
|
|
|
.await?;
|
2020-09-28 07:27:16 +00:00
|
|
|
|
|
|
|
if let Some(device) = device {
|
2021-04-20 09:47:11 +00:00
|
|
|
match self.should_share_key(&device, &session).await {
|
2021-01-28 13:07:51 +00:00
|
|
|
Err(e) => {
|
|
|
|
info!(
|
|
|
|
"Received a key request from {} {} that we won't serve: {}",
|
|
|
|
device.user_id(),
|
|
|
|
device.device_id(),
|
|
|
|
e
|
|
|
|
);
|
2020-10-20 15:19:37 +00:00
|
|
|
|
2021-01-28 13:07:51 +00:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Ok(message_index) => {
|
|
|
|
info!(
|
|
|
|
"Serving a key request for {} from {} {} with message_index {:?}.",
|
|
|
|
key_info.session_id,
|
|
|
|
device.user_id(),
|
|
|
|
device.device_id(),
|
|
|
|
message_index,
|
|
|
|
);
|
2020-10-20 15:19:37 +00:00
|
|
|
|
2021-01-28 13:07:51 +00:00
|
|
|
match self.share_session(&session, &device, message_index).await {
|
|
|
|
Ok(s) => Ok(Some(s)),
|
|
|
|
Err(OlmError::MissingSession) => {
|
|
|
|
info!(
|
|
|
|
"Key request from {} {} is missing an Olm session, \
|
|
|
|
putting the request in the wait queue",
|
|
|
|
device.user_id(),
|
|
|
|
device.device_id()
|
|
|
|
);
|
|
|
|
self.handle_key_share_without_session(device, event);
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
2020-10-06 14:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 07:27:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-28 12:12:08 +00:00
|
|
|
warn!(
|
|
|
|
"Received a key request from an unknown device {} {}.",
|
|
|
|
&event.sender, &event.content.requesting_device_id
|
|
|
|
);
|
2020-09-28 07:27:16 +00:00
|
|
|
self.store.update_tracked_user(&event.sender, true).await?;
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok(None)
|
|
|
|
}
|
2020-09-28 07:27:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
async fn share_session(
|
|
|
|
&self,
|
|
|
|
session: &InboundGroupSession,
|
|
|
|
device: &Device,
|
2021-01-28 13:07:51 +00:00
|
|
|
message_index: Option<u32>,
|
2020-10-20 15:19:37 +00:00
|
|
|
) -> OlmResult<Session> {
|
2021-01-28 13:07:51 +00:00
|
|
|
let (used_session, content) = device
|
|
|
|
.encrypt_session(session.clone(), message_index)
|
|
|
|
.await?;
|
2020-09-28 11:32:30 +00:00
|
|
|
|
|
|
|
let id = Uuid::new_v4();
|
|
|
|
let mut messages = BTreeMap::new();
|
|
|
|
|
|
|
|
messages
|
|
|
|
.entry(device.user_id().to_owned())
|
|
|
|
.or_insert_with(BTreeMap::new)
|
|
|
|
.insert(
|
|
|
|
DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
|
2020-09-29 12:18:03 +00:00
|
|
|
to_raw_value(&content)?,
|
2020-09-28 11:32:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let request = OutgoingRequest {
|
|
|
|
request_id: id,
|
|
|
|
request: Arc::new(
|
|
|
|
ToDeviceRequest {
|
2020-09-29 15:36:56 +00:00
|
|
|
event_type: EventType::RoomEncrypted,
|
2020-09-28 11:32:30 +00:00
|
|
|
txn_id: id,
|
|
|
|
messages,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
};
|
2020-09-28 07:27:16 +00:00
|
|
|
|
2020-09-28 11:32:30 +00:00
|
|
|
self.outgoing_to_device_requests.insert(id, request);
|
2020-09-29 12:18:03 +00:00
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok(used_session)
|
2020-09-28 07:27:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 11:32:30 +00:00
|
|
|
/// Check if it's ok to share a session with the given device.
|
|
|
|
///
|
|
|
|
/// The logic for this currently is as follows:
|
|
|
|
///
|
|
|
|
/// * Share any session with our own devices as long as they are trusted.
|
|
|
|
///
|
|
|
|
/// * Share with devices of other users only sessions that were meant to be
|
|
|
|
/// shared with them in the first place, in other words if an outbound
|
|
|
|
/// session still exists and the session was shared with that user/device
|
|
|
|
/// pair.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `device` - The device that is requesting a session from us.
|
|
|
|
///
|
2021-04-15 09:19:14 +00:00
|
|
|
/// * `session` - The session that was requested to be shared.
|
2021-04-20 09:47:11 +00:00
|
|
|
async fn should_share_key(
|
2020-09-28 07:27:16 +00:00
|
|
|
&self,
|
2020-09-28 11:32:30 +00:00
|
|
|
device: &Device,
|
2021-04-15 09:19:14 +00:00
|
|
|
session: &InboundGroupSession,
|
2021-01-28 13:07:51 +00:00
|
|
|
) -> Result<Option<u32>, KeyshareDecision> {
|
2021-04-14 12:30:53 +00:00
|
|
|
let outbound_session = self
|
|
|
|
.outbound_group_sessions
|
2021-04-20 11:35:47 +00:00
|
|
|
.get_with_id(session.room_id(), session.session_id())
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
|
|
|
.ok()
|
2021-04-20 11:35:47 +00:00
|
|
|
.flatten();
|
2021-04-14 12:30:53 +00:00
|
|
|
|
|
|
|
let own_device_check = || {
|
2020-09-28 11:32:30 +00:00
|
|
|
if device.trust_state() {
|
2021-01-28 13:07:51 +00:00
|
|
|
Ok(None)
|
2020-09-28 11:32:30 +00:00
|
|
|
} else {
|
2020-09-28 12:12:08 +00:00
|
|
|
Err(KeyshareDecision::UntrustedDevice)
|
2020-09-28 11:32:30 +00:00
|
|
|
}
|
2021-04-14 12:30:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// If we have a matching outbound session we can check the list of
|
|
|
|
// users/devices that received the session, if it wasn't shared check if
|
|
|
|
// it's our own device and if it's trusted.
|
|
|
|
if let Some(outbound) = outbound_session {
|
2021-01-28 13:07:51 +00:00
|
|
|
if let ShareState::Shared(message_index) =
|
|
|
|
outbound.is_shared_with(device.user_id(), device.device_id())
|
|
|
|
{
|
|
|
|
Ok(Some(message_index))
|
2021-04-14 12:30:53 +00:00
|
|
|
} else if device.user_id() == self.user_id() {
|
|
|
|
own_device_check()
|
2020-09-28 11:32:30 +00:00
|
|
|
} else {
|
2020-09-28 13:07:57 +00:00
|
|
|
Err(KeyshareDecision::OutboundSessionNotShared)
|
2020-09-28 11:32:30 +00:00
|
|
|
}
|
2021-04-14 12:30:53 +00:00
|
|
|
// Else just check if it's one of our own devices that requested the key and
|
|
|
|
// check if the device is trusted.
|
|
|
|
} else if device.user_id() == self.user_id() {
|
|
|
|
own_device_check()
|
|
|
|
// Otherwise, there's not enough info to decide if we can safely share
|
|
|
|
// the session.
|
2020-09-28 13:07:57 +00:00
|
|
|
} else {
|
|
|
|
Err(KeyshareDecision::MissingOutboundSession)
|
2020-09-28 11:32:30 +00:00
|
|
|
}
|
2020-09-28 07:27:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 09:47:11 +00:00
|
|
|
/// Check if it's ok, or rather if it makes sense to automatically request
|
|
|
|
/// a key from our other devices.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `key_info` - The info of our key request containing information about
|
|
|
|
/// the key we wish to request.
|
|
|
|
async fn should_request_key(
|
|
|
|
&self,
|
|
|
|
key_info: &RequestedKeyInfo,
|
|
|
|
) -> Result<bool, CryptoStoreError> {
|
|
|
|
let request = self.store.get_key_request_by_info(&key_info).await?;
|
|
|
|
|
|
|
|
// Don't send out duplicate requests, users can re-request them if they
|
|
|
|
// think a second request might succeed.
|
|
|
|
if request.is_none() {
|
|
|
|
let devices = self.store.get_user_devices(self.user_id()).await?;
|
|
|
|
|
|
|
|
// Devices will only respond to key requests if the devices are
|
|
|
|
// verified, if the device isn't verified by us it's unlikely that
|
|
|
|
// we're verified by them either. Don't request keys if there isn't
|
|
|
|
// at least one verified device.
|
|
|
|
if devices.is_any_verified() {
|
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 14:09:08 +00:00
|
|
|
/// Create a new outgoing key request for the key with the given session id.
|
|
|
|
///
|
|
|
|
/// This will queue up a new to-device request and store the key info so
|
|
|
|
/// once we receive a forwarded room key we can check that it matches the
|
|
|
|
/// key we requested.
|
|
|
|
///
|
2021-04-19 13:00:21 +00:00
|
|
|
/// This method will return a cancel request and a new key request if the
|
|
|
|
/// key was already requested, otherwise it will return just the key
|
|
|
|
/// request.
|
2020-09-17 14:09:08 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
2021-04-19 13:00:21 +00:00
|
|
|
///
|
2020-09-17 14:09:08 +00:00
|
|
|
/// * `room_id` - The id of the room where the key is used in.
|
|
|
|
///
|
|
|
|
/// * `sender_key` - The curve25519 key of the sender that owns the key.
|
|
|
|
///
|
|
|
|
/// * `session_id` - The id that uniquely identifies the session.
|
2021-04-19 13:00:21 +00:00
|
|
|
pub async fn request_key(
|
2020-09-17 12:16:43 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
sender_key: &str,
|
|
|
|
session_id: &str,
|
2021-04-19 13:00:21 +00:00
|
|
|
) -> Result<(Option<OutgoingRequest>, OutgoingRequest), CryptoStoreError> {
|
2020-09-17 12:16:43 +00:00
|
|
|
let key_info = RequestedKeyInfo {
|
|
|
|
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
|
|
|
|
room_id: room_id.to_owned(),
|
|
|
|
sender_key: sender_key.to_owned(),
|
|
|
|
session_id: session_id.to_owned(),
|
|
|
|
};
|
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
let request = self.store.get_key_request_by_info(&key_info).await?;
|
2020-09-17 12:16:43 +00:00
|
|
|
|
2021-04-19 13:00:21 +00:00
|
|
|
if let Some(request) = request {
|
|
|
|
let cancel = request.to_cancelation(self.device_id())?;
|
|
|
|
let request = request.to_request(self.device_id())?;
|
|
|
|
|
|
|
|
Ok((Some(cancel), request))
|
|
|
|
} else {
|
|
|
|
let request = self.request_key_helper(key_info).await?;
|
|
|
|
|
|
|
|
Ok((None, request))
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
2021-04-19 13:00:21 +00:00
|
|
|
}
|
2020-09-17 12:16:43 +00:00
|
|
|
|
2021-04-19 13:00:21 +00:00
|
|
|
async fn request_key_helper(
|
|
|
|
&self,
|
|
|
|
key_info: RequestedKeyInfo,
|
|
|
|
) -> Result<OutgoingRequest, CryptoStoreError> {
|
2020-09-17 12:16:43 +00:00
|
|
|
info!("Creating new outgoing room key request {:#?}", key_info);
|
|
|
|
|
2021-04-19 13:00:21 +00:00
|
|
|
let request = OutgoingKeyRequest {
|
|
|
|
request_recipient: self.user_id().to_owned(),
|
|
|
|
request_id: Uuid::new_v4(),
|
2021-04-15 15:48:37 +00:00
|
|
|
info: key_info,
|
2020-09-17 12:16:43 +00:00
|
|
|
sent_out: false,
|
|
|
|
};
|
|
|
|
|
2021-04-19 13:00:21 +00:00
|
|
|
let outgoing_request = request.to_request(self.device_id())?;
|
|
|
|
self.save_outgoing_key_info(request).await?;
|
|
|
|
|
|
|
|
Ok(outgoing_request)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new outgoing key request for the key with the given session id.
|
|
|
|
///
|
|
|
|
/// This will queue up a new to-device request and store the key info so
|
|
|
|
/// once we receive a forwarded room key we can check that it matches the
|
|
|
|
/// key we requested.
|
|
|
|
///
|
|
|
|
/// This does nothing if a request for this key has already been sent out.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
/// * `room_id` - The id of the room where the key is used in.
|
|
|
|
///
|
|
|
|
/// * `sender_key` - The curve25519 key of the sender that owns the key.
|
|
|
|
///
|
|
|
|
/// * `session_id` - The id that uniquely identifies the session.
|
|
|
|
pub async fn create_outgoing_key_request(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
sender_key: &str,
|
|
|
|
session_id: &str,
|
|
|
|
) -> Result<(), CryptoStoreError> {
|
|
|
|
let key_info = RequestedKeyInfo {
|
|
|
|
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
|
|
|
|
room_id: room_id.to_owned(),
|
|
|
|
sender_key: sender_key.to_owned(),
|
|
|
|
session_id: session_id.to_owned(),
|
|
|
|
};
|
|
|
|
|
2021-04-20 09:47:11 +00:00
|
|
|
if self.should_request_key(&key_info).await? {
|
2021-04-19 13:00:21 +00:00
|
|
|
self.request_key_helper(key_info).await?;
|
|
|
|
}
|
2020-09-17 14:09:08 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 14:09:08 +00:00
|
|
|
/// Save an outgoing key info.
|
|
|
|
async fn save_outgoing_key_info(
|
|
|
|
&self,
|
2021-04-15 10:45:51 +00:00
|
|
|
info: OutgoingKeyRequest,
|
2020-09-17 14:09:08 +00:00
|
|
|
) -> Result<(), CryptoStoreError> {
|
2021-04-15 10:45:51 +00:00
|
|
|
let mut changes = Changes::default();
|
|
|
|
changes.key_requests.push(info);
|
|
|
|
self.store.save_changes(changes).await?;
|
2020-09-17 14:09:08 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 14:09:08 +00:00
|
|
|
/// Get an outgoing key info that matches the forwarded room key content.
|
|
|
|
async fn get_key_info(
|
|
|
|
&self,
|
2020-12-04 12:35:56 +00:00
|
|
|
content: &ForwardedRoomKeyToDeviceEventContent,
|
2021-04-15 10:45:51 +00:00
|
|
|
) -> Result<Option<OutgoingKeyRequest>, CryptoStoreError> {
|
|
|
|
let info = RequestedKeyInfo {
|
|
|
|
algorithm: content.algorithm.clone(),
|
|
|
|
room_id: content.room_id.clone(),
|
|
|
|
sender_key: content.sender_key.clone(),
|
|
|
|
session_id: content.session_id.clone(),
|
|
|
|
};
|
2020-09-17 12:16:43 +00:00
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
self.store.get_key_request_by_info(&info).await
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 14:09:08 +00:00
|
|
|
/// Delete the given outgoing key info.
|
2021-04-15 10:45:51 +00:00
|
|
|
async fn delete_key_info(&self, info: &OutgoingKeyRequest) -> Result<(), CryptoStoreError> {
|
2020-09-17 12:16:43 +00:00
|
|
|
self.store
|
2021-04-15 10:45:51 +00:00
|
|
|
.delete_outgoing_key_request(info.request_id)
|
|
|
|
.await
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 14:09:08 +00:00
|
|
|
/// Mark the outgoing request as sent.
|
2021-04-15 10:45:51 +00:00
|
|
|
pub async fn mark_outgoing_request_as_sent(&self, id: Uuid) -> Result<(), CryptoStoreError> {
|
|
|
|
let info = self.store.get_outgoing_key_request(id).await?;
|
2020-09-17 12:16:43 +00:00
|
|
|
|
|
|
|
if let Some(mut info) = info {
|
|
|
|
trace!("Marking outgoing key request as sent {:#?}", info);
|
|
|
|
info.sent_out = true;
|
2021-04-15 10:45:51 +00:00
|
|
|
self.save_outgoing_key_info(info).await?;
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
2020-09-17 14:09:08 +00:00
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
self.outgoing_to_device_requests.remove(&id);
|
|
|
|
|
2020-09-17 14:09:08 +00:00
|
|
|
Ok(())
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
/// Mark the given outgoing key info as done.
|
2020-09-17 14:09:08 +00:00
|
|
|
///
|
2020-10-20 15:19:37 +00:00
|
|
|
/// This will queue up a request cancelation.
|
2021-04-15 10:45:51 +00:00
|
|
|
async fn mark_as_done(&self, key_info: OutgoingKeyRequest) -> Result<(), CryptoStoreError> {
|
2020-09-17 14:09:08 +00:00
|
|
|
// TODO perhaps only remove the key info if the first known index is 0.
|
2020-09-18 11:45:00 +00:00
|
|
|
trace!(
|
|
|
|
"Successfully received a forwarded room key for {:#?}",
|
|
|
|
key_info
|
|
|
|
);
|
|
|
|
|
2020-09-17 14:09:08 +00:00
|
|
|
self.outgoing_to_device_requests
|
|
|
|
.remove(&key_info.request_id);
|
2020-10-20 15:19:37 +00:00
|
|
|
// TODO return the key info instead of deleting it so the sync handler
|
|
|
|
// can delete it in one transaction.
|
2020-09-18 11:45:00 +00:00
|
|
|
self.delete_key_info(&key_info).await?;
|
|
|
|
|
2021-04-19 13:00:21 +00:00
|
|
|
let request = key_info.to_cancelation(self.device_id())?;
|
|
|
|
self.outgoing_to_device_requests
|
|
|
|
.insert(request.request_id, request);
|
2020-09-18 11:45:00 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-09-17 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a forwarded room key event.
|
2020-09-18 11:45:00 +00:00
|
|
|
pub async fn receive_forwarded_room_key(
|
2020-09-17 12:16:43 +00:00
|
|
|
&self,
|
2020-09-17 14:09:08 +00:00
|
|
|
sender_key: &str,
|
2020-12-04 12:35:56 +00:00
|
|
|
event: &mut ToDeviceEvent<ForwardedRoomKeyToDeviceEventContent>,
|
2021-01-19 11:59:31 +00:00
|
|
|
) -> Result<(Option<AnyToDeviceEvent>, Option<InboundGroupSession>), CryptoStoreError> {
|
2020-09-17 14:09:08 +00:00
|
|
|
let key_info = self.get_key_info(&event.content).await?;
|
2020-09-17 12:16:43 +00:00
|
|
|
|
|
|
|
if let Some(info) = key_info {
|
2020-09-24 09:18:01 +00:00
|
|
|
let session = InboundGroupSession::from_forwarded_key(sender_key, &mut event.content)?;
|
2020-09-17 14:09:08 +00:00
|
|
|
|
|
|
|
let old_session = self
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
|
|
|
session.room_id(),
|
|
|
|
&session.sender_key,
|
|
|
|
session.session_id(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// If we have a previous session, check if we have a better version
|
|
|
|
// and store the new one if so.
|
2020-10-20 15:19:37 +00:00
|
|
|
let session = if let Some(old_session) = old_session {
|
2020-12-02 10:12:46 +00:00
|
|
|
let first_old_index = old_session.first_known_index();
|
|
|
|
let first_index = session.first_known_index();
|
2020-09-17 14:09:08 +00:00
|
|
|
|
|
|
|
if first_old_index > first_index {
|
2020-10-20 15:19:37 +00:00
|
|
|
self.mark_as_done(info).await?;
|
|
|
|
Some(session)
|
|
|
|
} else {
|
|
|
|
None
|
2020-09-17 14:09:08 +00:00
|
|
|
}
|
|
|
|
// If we didn't have a previous session, store it.
|
|
|
|
} else {
|
2020-10-20 15:19:37 +00:00
|
|
|
self.mark_as_done(info).await?;
|
|
|
|
Some(session)
|
|
|
|
};
|
|
|
|
|
2021-02-17 15:01:51 +00:00
|
|
|
if let Some(s) = &session {
|
|
|
|
info!(
|
|
|
|
"Received a forwarded room key from {} for room {} with session id {}",
|
|
|
|
event.sender,
|
|
|
|
s.room_id(),
|
|
|
|
s.session_id()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok((
|
2021-01-19 11:59:31 +00:00
|
|
|
Some(AnyToDeviceEvent::ForwardedRoomKey(event.clone())),
|
2020-10-20 15:19:37 +00:00
|
|
|
session,
|
|
|
|
))
|
2020-09-17 12:16:43 +00:00
|
|
|
} else {
|
|
|
|
info!(
|
|
|
|
"Received a forwarded room key from {}, but no key info was found.",
|
|
|
|
event.sender,
|
|
|
|
);
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok((None, None))
|
2020-09-17 12:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 14:55:33 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-09-29 08:09:47 +00:00
|
|
|
use dashmap::DashMap;
|
2020-09-17 15:13:42 +00:00
|
|
|
use matrix_sdk_common::{
|
2020-09-29 08:24:54 +00:00
|
|
|
api::r0::to_device::DeviceIdOrAllDevices,
|
|
|
|
events::{
|
2020-12-04 12:35:56 +00:00
|
|
|
forwarded_room_key::ForwardedRoomKeyToDeviceEventContent,
|
|
|
|
room::encrypted::EncryptedEventContent,
|
|
|
|
room_key_request::RoomKeyRequestToDeviceEventContent, AnyToDeviceEvent, ToDeviceEvent,
|
2020-09-29 08:24:54 +00:00
|
|
|
},
|
2020-09-17 15:13:42 +00:00
|
|
|
identifiers::{room_id, user_id, DeviceIdBox, RoomId, UserId},
|
2020-10-22 16:04:29 +00:00
|
|
|
locks::Mutex,
|
2020-09-17 15:13:42 +00:00
|
|
|
};
|
2020-09-17 14:55:33 +00:00
|
|
|
use matrix_sdk_test::async_test;
|
2020-09-17 15:13:42 +00:00
|
|
|
use std::{convert::TryInto, sync::Arc};
|
2020-09-17 14:55:33 +00:00
|
|
|
|
|
|
|
use crate::{
|
2020-09-28 12:51:57 +00:00
|
|
|
identities::{LocalTrust, ReadOnlyDevice},
|
2020-10-22 16:04:29 +00:00
|
|
|
olm::{Account, PrivateCrossSigningIdentity, ReadOnlyAccount},
|
2021-04-15 13:19:21 +00:00
|
|
|
session_manager::GroupSessionCache,
|
2021-04-14 12:30:53 +00:00
|
|
|
store::{Changes, CryptoStore, MemoryStore, Store},
|
2020-10-01 10:15:13 +00:00
|
|
|
verification::VerificationMachine,
|
2020-09-17 14:55:33 +00:00
|
|
|
};
|
|
|
|
|
2020-10-01 10:15:13 +00:00
|
|
|
use super::{KeyRequestMachine, KeyshareDecision};
|
2020-09-17 14:55:33 +00:00
|
|
|
|
|
|
|
fn alice_id() -> UserId {
|
|
|
|
user_id!("@alice:example.org")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn alice_device_id() -> DeviceIdBox {
|
|
|
|
"JLAFKJWSCS".into()
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:51:57 +00:00
|
|
|
fn bob_id() -> UserId {
|
|
|
|
user_id!("@bob:example.org")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bob_device_id() -> DeviceIdBox {
|
|
|
|
"ILMLKASTES".into()
|
|
|
|
}
|
|
|
|
|
2021-04-20 09:47:11 +00:00
|
|
|
fn alice2_device_id() -> DeviceIdBox {
|
|
|
|
"ILMLKASTES".into()
|
|
|
|
}
|
|
|
|
|
2020-09-17 14:55:33 +00:00
|
|
|
fn room_id() -> RoomId {
|
|
|
|
room_id!("!test:example.org")
|
|
|
|
}
|
|
|
|
|
2020-09-29 10:03:41 +00:00
|
|
|
fn account() -> ReadOnlyAccount {
|
|
|
|
ReadOnlyAccount::new(&alice_id(), &alice_device_id())
|
2020-09-17 14:55:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 10:03:41 +00:00
|
|
|
fn bob_account() -> ReadOnlyAccount {
|
|
|
|
ReadOnlyAccount::new(&bob_id(), &bob_device_id())
|
2020-09-28 12:51:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 09:47:11 +00:00
|
|
|
fn alice_2_account() -> ReadOnlyAccount {
|
|
|
|
ReadOnlyAccount::new(&alice_id(), &alice2_device_id())
|
|
|
|
}
|
|
|
|
|
2020-09-29 08:24:54 +00:00
|
|
|
fn bob_machine() -> KeyRequestMachine {
|
|
|
|
let user_id = Arc::new(bob_id());
|
2020-10-01 10:15:13 +00:00
|
|
|
let account = ReadOnlyAccount::new(&user_id, &alice_device_id());
|
|
|
|
let store: Arc<Box<dyn CryptoStore>> = Arc::new(Box::new(MemoryStore::new()));
|
2020-10-22 16:04:29 +00:00
|
|
|
let identity = Arc::new(Mutex::new(PrivateCrossSigningIdentity::empty(bob_id())));
|
2020-10-30 10:34:55 +00:00
|
|
|
let verification = VerificationMachine::new(account, identity.clone(), store.clone());
|
|
|
|
let store = Store::new(user_id.clone(), identity, store, verification);
|
2021-04-15 13:19:21 +00:00
|
|
|
let session_cache = GroupSessionCache::new(store.clone());
|
2020-09-29 08:24:54 +00:00
|
|
|
|
|
|
|
KeyRequestMachine::new(
|
|
|
|
user_id,
|
|
|
|
Arc::new(bob_device_id()),
|
|
|
|
store,
|
2021-04-15 13:19:21 +00:00
|
|
|
session_cache,
|
2020-10-09 09:36:31 +00:00
|
|
|
Arc::new(DashMap::new()),
|
2020-09-29 08:24:54 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-01 10:15:13 +00:00
|
|
|
async fn get_machine() -> KeyRequestMachine {
|
2020-09-28 07:27:16 +00:00
|
|
|
let user_id = Arc::new(alice_id());
|
2020-10-01 10:15:13 +00:00
|
|
|
let account = ReadOnlyAccount::new(&user_id, &alice_device_id());
|
|
|
|
let device = ReadOnlyDevice::from_account(&account).await;
|
|
|
|
let store: Arc<Box<dyn CryptoStore>> = Arc::new(Box::new(MemoryStore::new()));
|
2020-10-22 16:04:29 +00:00
|
|
|
let identity = Arc::new(Mutex::new(PrivateCrossSigningIdentity::empty(alice_id())));
|
2020-10-30 10:34:55 +00:00
|
|
|
let verification = VerificationMachine::new(account, identity.clone(), store.clone());
|
|
|
|
let store = Store::new(user_id.clone(), identity, store, verification);
|
2020-10-01 10:15:13 +00:00
|
|
|
store.save_devices(&[device]).await.unwrap();
|
2021-04-15 13:19:21 +00:00
|
|
|
let session_cache = GroupSessionCache::new(store.clone());
|
2020-09-17 14:55:33 +00:00
|
|
|
|
2020-09-29 08:09:47 +00:00
|
|
|
KeyRequestMachine::new(
|
|
|
|
user_id,
|
|
|
|
Arc::new(alice_device_id()),
|
|
|
|
store,
|
2021-04-15 13:19:21 +00:00
|
|
|
session_cache,
|
2020-10-09 09:36:31 +00:00
|
|
|
Arc::new(DashMap::new()),
|
2020-09-29 08:09:47 +00:00
|
|
|
)
|
2020-09-17 14:55:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 10:15:13 +00:00
|
|
|
#[async_test]
|
|
|
|
async fn create_machine() {
|
|
|
|
let machine = get_machine().await;
|
2020-09-17 14:55:33 +00:00
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
assert!(machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
2020-09-17 14:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_test]
|
2021-04-20 09:47:11 +00:00
|
|
|
async fn re_request_keys() {
|
2020-10-01 10:15:13 +00:00
|
|
|
let machine = get_machine().await;
|
2020-09-17 14:55:33 +00:00
|
|
|
let account = account();
|
|
|
|
|
2021-04-19 13:00:21 +00:00
|
|
|
let (_, session) = account
|
|
|
|
.create_group_session_pair_with_defaults(&room_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
|
|
|
let (cancel, request) = machine
|
|
|
|
.request_key(session.room_id(), &session.sender_key, session.session_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(cancel.is_none());
|
|
|
|
|
|
|
|
machine
|
|
|
|
.mark_outgoing_request_as_sent(request.request_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (cancel, _) = machine
|
|
|
|
.request_key(session.room_id(), &session.sender_key, session.session_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(cancel.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_test]
|
2021-04-20 09:47:11 +00:00
|
|
|
async fn create_key_request() {
|
2021-04-19 13:00:21 +00:00
|
|
|
let machine = get_machine().await;
|
|
|
|
let account = account();
|
2021-04-20 09:47:11 +00:00
|
|
|
let second_account = alice_2_account();
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&second_account).await;
|
|
|
|
|
|
|
|
// We need a trusted device, otherwise we won't request keys
|
|
|
|
alice_device.set_trust_state(LocalTrust::Verified);
|
|
|
|
machine.store.save_devices(&[alice_device]).await.unwrap();
|
2021-04-19 13:00:21 +00:00
|
|
|
|
2020-09-17 14:55:33 +00:00
|
|
|
let (_, session) = account
|
2020-09-18 16:55:17 +00:00
|
|
|
.create_group_session_pair_with_defaults(&room_id())
|
2020-09-17 14:55:33 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
assert!(machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
2020-09-17 14:55:33 +00:00
|
|
|
machine
|
|
|
|
.create_outgoing_key_request(
|
|
|
|
session.room_id(),
|
|
|
|
&session.sender_key,
|
|
|
|
session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-04-15 17:40:24 +00:00
|
|
|
assert!(!machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
|
|
|
assert_eq!(
|
|
|
|
machine.outgoing_to_device_requests().await.unwrap().len(),
|
|
|
|
1
|
|
|
|
);
|
2020-09-17 14:55:33 +00:00
|
|
|
|
|
|
|
machine
|
|
|
|
.create_outgoing_key_request(
|
|
|
|
session.room_id(),
|
|
|
|
&session.sender_key,
|
|
|
|
session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
let requests = machine.outgoing_to_device_requests().await.unwrap();
|
|
|
|
assert_eq!(requests.len(), 1);
|
2020-09-17 14:55:33 +00:00
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
let request = requests.get(0).unwrap();
|
2020-09-17 14:55:33 +00:00
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
machine
|
|
|
|
.mark_outgoing_request_as_sent(request.request_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
2020-09-17 14:55:33 +00:00
|
|
|
}
|
2020-09-17 15:13:42 +00:00
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn receive_forwarded_key() {
|
2020-10-01 10:15:13 +00:00
|
|
|
let machine = get_machine().await;
|
2020-09-17 15:13:42 +00:00
|
|
|
let account = account();
|
|
|
|
|
2021-04-20 09:47:11 +00:00
|
|
|
let second_account = alice_2_account();
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&second_account).await;
|
|
|
|
|
|
|
|
// We need a trusted device, otherwise we won't request keys
|
|
|
|
alice_device.set_trust_state(LocalTrust::Verified);
|
|
|
|
machine.store.save_devices(&[alice_device]).await.unwrap();
|
|
|
|
|
2020-09-17 15:13:42 +00:00
|
|
|
let (_, session) = account
|
2020-09-18 16:55:17 +00:00
|
|
|
.create_group_session_pair_with_defaults(&room_id())
|
2020-09-17 15:13:42 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
machine
|
|
|
|
.create_outgoing_key_request(
|
|
|
|
session.room_id(),
|
|
|
|
&session.sender_key,
|
|
|
|
session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
let requests = machine.outgoing_to_device_requests().await.unwrap();
|
|
|
|
let request = requests.get(0).unwrap();
|
2020-09-17 15:13:42 +00:00
|
|
|
let id = request.request_id;
|
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
machine.mark_outgoing_request_as_sent(id).await.unwrap();
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2021-01-28 13:07:51 +00:00
|
|
|
let export = session.export_at_index(10).await;
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2020-12-04 12:35:56 +00:00
|
|
|
let content: ForwardedRoomKeyToDeviceEventContent = export.try_into().unwrap();
|
2020-09-17 15:13:42 +00:00
|
|
|
|
|
|
|
let mut event = ToDeviceEvent {
|
|
|
|
sender: alice_id(),
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
machine
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
|
|
|
session.room_id(),
|
|
|
|
&session.sender_key,
|
|
|
|
session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none()
|
|
|
|
);
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
let (_, first_session) = machine
|
2020-09-17 15:13:42 +00:00
|
|
|
.receive_forwarded_room_key(&session.sender_key, &mut event)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-20 15:19:37 +00:00
|
|
|
let first_session = first_session.unwrap();
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2020-12-02 10:12:46 +00:00
|
|
|
assert_eq!(first_session.first_known_index(), 10);
|
2020-10-20 15:19:37 +00:00
|
|
|
|
|
|
|
machine
|
2020-09-17 15:13:42 +00:00
|
|
|
.store
|
2020-10-20 15:19:37 +00:00
|
|
|
.save_inbound_group_sessions(&[first_session.clone()])
|
2020-09-17 15:13:42 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-09-18 11:45:00 +00:00
|
|
|
// Get the cancel request.
|
|
|
|
let request = machine.outgoing_to_device_requests.iter().next().unwrap();
|
|
|
|
let id = request.request_id;
|
|
|
|
drop(request);
|
2021-04-15 10:45:51 +00:00
|
|
|
machine.mark_outgoing_request_as_sent(id).await.unwrap();
|
2020-09-18 11:45:00 +00:00
|
|
|
|
2020-09-17 15:13:42 +00:00
|
|
|
machine
|
|
|
|
.create_outgoing_key_request(
|
|
|
|
session.room_id(),
|
|
|
|
&session.sender_key,
|
|
|
|
session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
let requests = machine.outgoing_to_device_requests().await.unwrap();
|
|
|
|
let request = &requests[0];
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2021-04-15 17:40:24 +00:00
|
|
|
machine
|
|
|
|
.mark_outgoing_request_as_sent(request.request_id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2021-01-28 13:07:51 +00:00
|
|
|
let export = session.export_at_index(15).await;
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2020-12-04 12:35:56 +00:00
|
|
|
let content: ForwardedRoomKeyToDeviceEventContent = export.try_into().unwrap();
|
2020-09-17 15:13:42 +00:00
|
|
|
|
|
|
|
let mut event = ToDeviceEvent {
|
|
|
|
sender: alice_id(),
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
let (_, second_session) = machine
|
2020-09-17 15:13:42 +00:00
|
|
|
.receive_forwarded_room_key(&session.sender_key, &mut event)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
assert!(second_session.is_none());
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2021-01-28 13:07:51 +00:00
|
|
|
let export = session.export_at_index(0).await;
|
2020-09-17 15:13:42 +00:00
|
|
|
|
2020-12-04 12:35:56 +00:00
|
|
|
let content: ForwardedRoomKeyToDeviceEventContent = export.try_into().unwrap();
|
2020-09-17 15:13:42 +00:00
|
|
|
|
|
|
|
let mut event = ToDeviceEvent {
|
|
|
|
sender: alice_id(),
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
let (_, second_session) = machine
|
2020-09-17 15:13:42 +00:00
|
|
|
.receive_forwarded_room_key(&session.sender_key, &mut event)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-12-02 10:12:46 +00:00
|
|
|
assert_eq!(second_session.unwrap().first_known_index(), 0);
|
2020-09-17 15:13:42 +00:00
|
|
|
}
|
2020-09-28 12:51:57 +00:00
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn should_share_key_test() {
|
2020-10-01 10:15:13 +00:00
|
|
|
let machine = get_machine().await;
|
2020-09-28 12:51:57 +00:00
|
|
|
let account = account();
|
|
|
|
|
2020-10-01 10:15:13 +00:00
|
|
|
let own_device = machine
|
|
|
|
.store
|
|
|
|
.get_device(&alice_id(), &alice_device_id())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2020-09-28 12:51:57 +00:00
|
|
|
|
2021-04-14 12:30:53 +00:00
|
|
|
let (outbound, inbound) = account
|
|
|
|
.create_group_session_pair_with_defaults(&room_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-09-28 12:51:57 +00:00
|
|
|
// We don't share keys with untrusted devices.
|
|
|
|
assert_eq!(
|
|
|
|
machine
|
2021-04-20 09:47:11 +00:00
|
|
|
.should_share_key(&own_device, &inbound)
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
2020-09-28 12:51:57 +00:00
|
|
|
.expect_err("Should not share with untrusted"),
|
|
|
|
KeyshareDecision::UntrustedDevice
|
|
|
|
);
|
|
|
|
own_device.set_trust_state(LocalTrust::Verified);
|
|
|
|
// Now we do want to share the keys.
|
2021-04-15 13:19:21 +00:00
|
|
|
assert!(machine
|
2021-04-20 09:47:11 +00:00
|
|
|
.should_share_key(&own_device, &inbound)
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
|
|
|
.is_ok());
|
2020-09-28 12:51:57 +00:00
|
|
|
|
2020-10-01 10:15:13 +00:00
|
|
|
let bob_device = ReadOnlyDevice::from_account(&bob_account()).await;
|
|
|
|
machine.store.save_devices(&[bob_device]).await.unwrap();
|
|
|
|
|
|
|
|
let bob_device = machine
|
|
|
|
.store
|
|
|
|
.get_device(&bob_id(), &bob_device_id())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2020-09-28 12:51:57 +00:00
|
|
|
|
|
|
|
// We don't share sessions with other user's devices if no outbound
|
|
|
|
// session was provided.
|
|
|
|
assert_eq!(
|
|
|
|
machine
|
2021-04-20 09:47:11 +00:00
|
|
|
.should_share_key(&bob_device, &inbound)
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
2020-09-28 12:51:57 +00:00
|
|
|
.expect_err("Should not share with other."),
|
|
|
|
KeyshareDecision::MissingOutboundSession
|
|
|
|
);
|
|
|
|
|
2021-04-14 12:30:53 +00:00
|
|
|
let mut changes = Changes::default();
|
|
|
|
|
|
|
|
changes.outbound_group_sessions.push(outbound.clone());
|
|
|
|
changes.inbound_group_sessions.push(inbound.clone());
|
|
|
|
machine.store.save_changes(changes).await.unwrap();
|
2021-04-15 13:19:21 +00:00
|
|
|
machine.outbound_group_sessions.insert(outbound.clone());
|
2020-09-28 12:51:57 +00:00
|
|
|
|
|
|
|
// We don't share sessions with other user's devices if the session
|
|
|
|
// wasn't shared in the first place.
|
|
|
|
assert_eq!(
|
|
|
|
machine
|
2021-04-20 09:47:11 +00:00
|
|
|
.should_share_key(&bob_device, &inbound)
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
2020-09-28 12:51:57 +00:00
|
|
|
.expect_err("Should not share with other unless shared."),
|
|
|
|
KeyshareDecision::OutboundSessionNotShared
|
|
|
|
);
|
|
|
|
|
|
|
|
bob_device.set_trust_state(LocalTrust::Verified);
|
|
|
|
|
|
|
|
// We don't share sessions with other user's devices if the session
|
|
|
|
// wasn't shared in the first place even if the device is trusted.
|
|
|
|
assert_eq!(
|
|
|
|
machine
|
2021-04-20 09:47:11 +00:00
|
|
|
.should_share_key(&bob_device, &inbound)
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
2020-09-28 12:51:57 +00:00
|
|
|
.expect_err("Should not share with other unless shared."),
|
|
|
|
KeyshareDecision::OutboundSessionNotShared
|
|
|
|
);
|
|
|
|
|
2021-04-15 09:19:14 +00:00
|
|
|
// We now share the session, since it was shared before.
|
2021-04-14 12:30:53 +00:00
|
|
|
outbound.mark_shared_with(bob_device.user_id(), bob_device.device_id());
|
2021-04-15 13:19:21 +00:00
|
|
|
assert!(machine
|
2021-04-20 09:47:11 +00:00
|
|
|
.should_share_key(&bob_device, &inbound)
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
|
|
|
.is_ok());
|
2021-04-15 09:19:14 +00:00
|
|
|
|
|
|
|
// But we don't share some other session that doesn't match our outbound
|
|
|
|
// session
|
|
|
|
let (_, other_inbound) = account
|
|
|
|
.create_group_session_pair_with_defaults(&room_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
machine
|
2021-04-20 09:47:11 +00:00
|
|
|
.should_share_key(&bob_device, &other_inbound)
|
2021-04-15 13:19:21 +00:00
|
|
|
.await
|
2021-04-15 09:19:14 +00:00
|
|
|
.expect_err("Should not share with other unless shared."),
|
|
|
|
KeyshareDecision::MissingOutboundSession
|
|
|
|
);
|
2020-09-28 12:51:57 +00:00
|
|
|
}
|
2020-09-29 08:24:54 +00:00
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn key_share_cycle() {
|
2020-10-01 10:15:13 +00:00
|
|
|
let alice_machine = get_machine().await;
|
2020-09-29 12:18:03 +00:00
|
|
|
let alice_account = Account {
|
|
|
|
inner: account(),
|
|
|
|
store: alice_machine.store.clone(),
|
|
|
|
};
|
2020-09-29 08:24:54 +00:00
|
|
|
|
|
|
|
let bob_machine = bob_machine();
|
|
|
|
let bob_account = bob_account();
|
|
|
|
|
2021-04-20 09:47:11 +00:00
|
|
|
let second_account = alice_2_account();
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&second_account).await;
|
|
|
|
|
|
|
|
// We need a trusted device, otherwise we won't request keys
|
|
|
|
alice_device.set_trust_state(LocalTrust::Verified);
|
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_devices(&[alice_device])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-09-29 08:24:54 +00:00
|
|
|
// Create Olm sessions for our two accounts.
|
|
|
|
let (alice_session, bob_session) = alice_account.create_session_for(&bob_account).await;
|
|
|
|
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&alice_account).await;
|
|
|
|
let bob_device = ReadOnlyDevice::from_account(&bob_account).await;
|
|
|
|
|
|
|
|
// Populate our stores with Olm sessions and a Megolm session.
|
|
|
|
|
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_sessions(&[alice_session])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_devices(&[bob_device])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
bob_machine
|
|
|
|
.store
|
|
|
|
.save_sessions(&[bob_session])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
bob_machine
|
|
|
|
.store
|
|
|
|
.save_devices(&[alice_device])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (group_session, inbound_group_session) = bob_account
|
|
|
|
.create_group_session_pair_with_defaults(&room_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
bob_machine
|
|
|
|
.store
|
|
|
|
.save_inbound_group_sessions(&[inbound_group_session])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Alice wants to request the outbound group session from bob.
|
|
|
|
alice_machine
|
|
|
|
.create_outgoing_key_request(
|
|
|
|
&room_id(),
|
|
|
|
bob_account.identity_keys.curve25519(),
|
|
|
|
group_session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
group_session.mark_shared_with(&alice_id(), &alice_device_id());
|
|
|
|
|
|
|
|
// Put the outbound session into bobs store.
|
|
|
|
bob_machine
|
|
|
|
.outbound_group_sessions
|
2021-04-15 13:19:21 +00:00
|
|
|
.insert(group_session.clone());
|
2020-09-29 08:24:54 +00:00
|
|
|
|
|
|
|
// Get the request and convert it into a event.
|
2021-04-15 17:40:24 +00:00
|
|
|
let requests = alice_machine.outgoing_to_device_requests().await.unwrap();
|
|
|
|
let request = &requests[0];
|
2020-09-29 08:24:54 +00:00
|
|
|
let id = request.request_id;
|
|
|
|
let content = request
|
|
|
|
.request
|
|
|
|
.to_device()
|
|
|
|
.unwrap()
|
|
|
|
.messages
|
|
|
|
.get(&alice_id())
|
|
|
|
.unwrap()
|
|
|
|
.get(&DeviceIdOrAllDevices::AllDevices)
|
|
|
|
.unwrap();
|
2020-12-04 12:35:56 +00:00
|
|
|
let content: RoomKeyRequestToDeviceEventContent =
|
|
|
|
serde_json::from_str(content.get()).unwrap();
|
2020-09-29 08:24:54 +00:00
|
|
|
|
|
|
|
alice_machine
|
2021-04-15 10:45:51 +00:00
|
|
|
.mark_outgoing_request_as_sent(id)
|
2020-09-29 08:24:54 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let event = ToDeviceEvent {
|
|
|
|
sender: alice_id(),
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bob doesn't have any outgoing requests.
|
|
|
|
assert!(bob_machine.outgoing_to_device_requests.is_empty());
|
|
|
|
|
|
|
|
// Receive the room key request from alice.
|
|
|
|
bob_machine.receive_incoming_key_request(&event);
|
|
|
|
bob_machine.collect_incoming_key_requests().await.unwrap();
|
|
|
|
// Now bob does have an outgoing request.
|
|
|
|
assert!(!bob_machine.outgoing_to_device_requests.is_empty());
|
|
|
|
|
|
|
|
// Get the request and convert it to a encrypted to-device event.
|
2021-04-15 17:40:24 +00:00
|
|
|
let requests = bob_machine.outgoing_to_device_requests().await.unwrap();
|
|
|
|
let request = &requests[0];
|
2020-09-29 08:24:54 +00:00
|
|
|
|
|
|
|
let id = request.request_id;
|
|
|
|
let content = request
|
|
|
|
.request
|
|
|
|
.to_device()
|
|
|
|
.unwrap()
|
|
|
|
.messages
|
|
|
|
.get(&alice_id())
|
|
|
|
.unwrap()
|
|
|
|
.get(&DeviceIdOrAllDevices::DeviceId(alice_device_id()))
|
|
|
|
.unwrap();
|
|
|
|
let content: EncryptedEventContent = serde_json::from_str(content.get()).unwrap();
|
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
bob_machine.mark_outgoing_request_as_sent(id).await.unwrap();
|
2020-09-29 08:24:54 +00:00
|
|
|
|
2020-09-29 12:33:07 +00:00
|
|
|
let event = ToDeviceEvent {
|
2020-09-29 08:24:54 +00:00
|
|
|
sender: bob_id(),
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
|
2020-09-29 12:18:03 +00:00
|
|
|
// Check that alice doesn't have the session.
|
|
|
|
assert!(alice_machine
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
2020-10-07 09:42:27 +00:00
|
|
|
&room_id(),
|
|
|
|
&bob_account.identity_keys().curve25519(),
|
|
|
|
group_session.session_id()
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
let decrypted = alice_account.decrypt_to_device_event(&event).await.unwrap();
|
2020-10-07 09:42:27 +00:00
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
if let AnyToDeviceEvent::ForwardedRoomKey(mut e) = decrypted.event.deserialize().unwrap() {
|
2020-10-20 15:19:37 +00:00
|
|
|
let (_, session) = alice_machine
|
2020-12-01 11:41:11 +00:00
|
|
|
.receive_forwarded_room_key(&decrypted.sender_key, &mut e)
|
2020-10-07 09:42:27 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-20 15:19:37 +00:00
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_inbound_group_sessions(&[session.unwrap()])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-07 09:42:27 +00:00
|
|
|
} else {
|
|
|
|
panic!("Invalid decrypted event type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that alice now does have the session.
|
|
|
|
let session = alice_machine
|
|
|
|
.store
|
2020-12-01 11:41:11 +00:00
|
|
|
.get_inbound_group_session(
|
|
|
|
&room_id(),
|
|
|
|
&decrypted.sender_key,
|
|
|
|
group_session.session_id(),
|
|
|
|
)
|
2020-10-07 09:42:27 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(session.session_id(), group_session.session_id())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn key_share_cycle_without_session() {
|
|
|
|
let alice_machine = get_machine().await;
|
|
|
|
let alice_account = Account {
|
|
|
|
inner: account(),
|
|
|
|
store: alice_machine.store.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let bob_machine = bob_machine();
|
|
|
|
let bob_account = bob_account();
|
|
|
|
|
2021-04-20 09:47:11 +00:00
|
|
|
let second_account = alice_2_account();
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&second_account).await;
|
|
|
|
|
|
|
|
// We need a trusted device, otherwise we won't request keys
|
|
|
|
alice_device.set_trust_state(LocalTrust::Verified);
|
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_devices(&[alice_device])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-10-07 09:42:27 +00:00
|
|
|
// Create Olm sessions for our two accounts.
|
|
|
|
let (alice_session, bob_session) = alice_account.create_session_for(&bob_account).await;
|
|
|
|
|
|
|
|
let alice_device = ReadOnlyDevice::from_account(&alice_account).await;
|
|
|
|
let bob_device = ReadOnlyDevice::from_account(&bob_account).await;
|
|
|
|
|
|
|
|
// Populate our stores with Olm sessions and a Megolm session.
|
|
|
|
|
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_devices(&[bob_device])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
bob_machine
|
|
|
|
.store
|
|
|
|
.save_devices(&[alice_device])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (group_session, inbound_group_session) = bob_account
|
|
|
|
.create_group_session_pair_with_defaults(&room_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
bob_machine
|
|
|
|
.store
|
|
|
|
.save_inbound_group_sessions(&[inbound_group_session])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Alice wants to request the outbound group session from bob.
|
|
|
|
alice_machine
|
|
|
|
.create_outgoing_key_request(
|
|
|
|
&room_id(),
|
|
|
|
bob_account.identity_keys.curve25519(),
|
|
|
|
group_session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
group_session.mark_shared_with(&alice_id(), &alice_device_id());
|
|
|
|
|
|
|
|
// Put the outbound session into bobs store.
|
|
|
|
bob_machine
|
|
|
|
.outbound_group_sessions
|
2021-04-15 13:19:21 +00:00
|
|
|
.insert(group_session.clone());
|
2020-10-07 09:42:27 +00:00
|
|
|
|
|
|
|
// Get the request and convert it into a event.
|
2021-04-15 17:40:24 +00:00
|
|
|
let requests = alice_machine.outgoing_to_device_requests().await.unwrap();
|
|
|
|
let request = &requests[0];
|
2020-10-07 09:42:27 +00:00
|
|
|
let id = request.request_id;
|
|
|
|
let content = request
|
|
|
|
.request
|
|
|
|
.to_device()
|
|
|
|
.unwrap()
|
|
|
|
.messages
|
|
|
|
.get(&alice_id())
|
|
|
|
.unwrap()
|
|
|
|
.get(&DeviceIdOrAllDevices::AllDevices)
|
|
|
|
.unwrap();
|
2020-12-04 12:35:56 +00:00
|
|
|
let content: RoomKeyRequestToDeviceEventContent =
|
|
|
|
serde_json::from_str(content.get()).unwrap();
|
2020-10-07 09:42:27 +00:00
|
|
|
|
|
|
|
alice_machine
|
2021-04-15 10:45:51 +00:00
|
|
|
.mark_outgoing_request_as_sent(id)
|
2020-10-07 09:42:27 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let event = ToDeviceEvent {
|
|
|
|
sender: alice_id(),
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bob doesn't have any outgoing requests.
|
2021-04-15 17:40:24 +00:00
|
|
|
assert!(bob_machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
2020-10-07 10:42:39 +00:00
|
|
|
assert!(bob_machine.users_for_key_claim.is_empty());
|
|
|
|
assert!(bob_machine.wait_queue.is_empty());
|
2020-10-07 09:42:27 +00:00
|
|
|
|
|
|
|
// Receive the room key request from alice.
|
|
|
|
bob_machine.receive_incoming_key_request(&event);
|
|
|
|
bob_machine.collect_incoming_key_requests().await.unwrap();
|
|
|
|
// Bob doens't have an outgoing requests since we're lacking a session.
|
2021-04-15 17:40:24 +00:00
|
|
|
assert!(bob_machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
2020-10-07 10:42:39 +00:00
|
|
|
assert!(!bob_machine.users_for_key_claim.is_empty());
|
|
|
|
assert!(!bob_machine.wait_queue.is_empty());
|
2020-10-07 09:42:27 +00:00
|
|
|
|
|
|
|
// We create a session now.
|
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_sessions(&[alice_session])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
bob_machine
|
|
|
|
.store
|
|
|
|
.save_sessions(&[bob_session])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
bob_machine.retry_keyshare(&alice_id(), &alice_device_id());
|
2020-10-07 10:42:39 +00:00
|
|
|
assert!(bob_machine.users_for_key_claim.is_empty());
|
2020-10-07 09:42:27 +00:00
|
|
|
bob_machine.collect_incoming_key_requests().await.unwrap();
|
|
|
|
// Bob now has an outgoing requests.
|
2021-04-15 17:40:24 +00:00
|
|
|
assert!(!bob_machine
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_empty());
|
2020-10-07 10:42:39 +00:00
|
|
|
assert!(bob_machine.wait_queue.is_empty());
|
2020-10-07 09:42:27 +00:00
|
|
|
|
|
|
|
// Get the request and convert it to a encrypted to-device event.
|
2021-04-15 17:40:24 +00:00
|
|
|
let requests = bob_machine.outgoing_to_device_requests().await.unwrap();
|
|
|
|
|
|
|
|
let request = &requests[0];
|
2020-10-07 09:42:27 +00:00
|
|
|
|
|
|
|
let id = request.request_id;
|
|
|
|
let content = request
|
|
|
|
.request
|
|
|
|
.to_device()
|
|
|
|
.unwrap()
|
|
|
|
.messages
|
|
|
|
.get(&alice_id())
|
|
|
|
.unwrap()
|
|
|
|
.get(&DeviceIdOrAllDevices::DeviceId(alice_device_id()))
|
|
|
|
.unwrap();
|
|
|
|
let content: EncryptedEventContent = serde_json::from_str(content.get()).unwrap();
|
|
|
|
|
2021-04-15 10:45:51 +00:00
|
|
|
bob_machine.mark_outgoing_request_as_sent(id).await.unwrap();
|
2020-10-07 09:42:27 +00:00
|
|
|
|
|
|
|
let event = ToDeviceEvent {
|
|
|
|
sender: bob_id(),
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check that alice doesn't have the session.
|
|
|
|
assert!(alice_machine
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
2020-09-29 12:18:03 +00:00
|
|
|
&room_id(),
|
|
|
|
&bob_account.identity_keys().curve25519(),
|
|
|
|
group_session.session_id()
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
let decrypted = alice_account.decrypt_to_device_event(&event).await.unwrap();
|
2020-09-29 12:18:03 +00:00
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
if let AnyToDeviceEvent::ForwardedRoomKey(mut e) = decrypted.event.deserialize().unwrap() {
|
2020-10-20 15:19:37 +00:00
|
|
|
let (_, session) = alice_machine
|
2020-12-01 11:41:11 +00:00
|
|
|
.receive_forwarded_room_key(&decrypted.sender_key, &mut e)
|
2020-09-29 12:18:03 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-20 15:19:37 +00:00
|
|
|
alice_machine
|
|
|
|
.store
|
|
|
|
.save_inbound_group_sessions(&[session.unwrap()])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-29 12:18:03 +00:00
|
|
|
} else {
|
|
|
|
panic!("Invalid decrypted event type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that alice now does have the session.
|
|
|
|
let session = alice_machine
|
|
|
|
.store
|
2020-12-01 11:41:11 +00:00
|
|
|
.get_inbound_group_session(
|
|
|
|
&room_id(),
|
|
|
|
&decrypted.sender_key,
|
|
|
|
group_session.session_id(),
|
|
|
|
)
|
2020-09-29 12:18:03 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(session.session_id(), group_session.session_id())
|
2020-09-29 08:24:54 +00:00
|
|
|
}
|
2020-09-17 14:55:33 +00:00
|
|
|
}
|