2020-02-25 13:24:18 +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.
|
|
|
|
|
2020-08-13 09:06:26 +00:00
|
|
|
#[cfg(feature = "sqlite_cryptostore")]
|
2020-03-18 14:50:32 +00:00
|
|
|
use std::path::Path;
|
2020-07-30 23:22:48 +00:00
|
|
|
use std::{
|
2020-08-11 10:22:14 +00:00
|
|
|
collections::{BTreeMap, HashSet},
|
2020-07-30 23:22:48 +00:00
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
mem,
|
|
|
|
result::Result as StdResult,
|
2020-08-04 09:23:24 +00:00
|
|
|
sync::Arc,
|
2020-03-12 14:41:11 +00:00
|
|
|
};
|
2020-04-21 08:41:08 +00:00
|
|
|
|
2020-08-11 10:22:14 +00:00
|
|
|
use dashmap::DashMap;
|
2020-08-12 10:48:22 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
use tracing::{debug, error, info, instrument, trace, warn};
|
2020-08-11 10:22:14 +00:00
|
|
|
|
2020-04-21 08:41:08 +00:00
|
|
|
use api::r0::{
|
2020-08-15 01:09:13 +00:00
|
|
|
keys::{claim_keys, get_keys, upload_keys, OneTimeKey},
|
2020-04-23 08:52:47 +00:00
|
|
|
sync::sync_events::Response as SyncResponse,
|
2020-08-10 10:39:00 +00:00
|
|
|
to_device::{
|
|
|
|
send_event_to_device::IncomingRequest as OwnedToDeviceRequest, DeviceIdOrAllDevices,
|
|
|
|
},
|
2020-04-21 08:41:08 +00:00
|
|
|
};
|
2020-07-30 23:22:48 +00:00
|
|
|
use matrix_sdk_common::{
|
|
|
|
api,
|
2020-08-15 01:09:13 +00:00
|
|
|
encryption::DeviceKeys,
|
2020-07-30 23:22:48 +00:00
|
|
|
events::{
|
|
|
|
forwarded_room_key::ForwardedRoomKeyEventContent,
|
|
|
|
room::{encrypted::EncryptedEventContent, message::MessageEventContent},
|
|
|
|
room_key::RoomKeyEventContent,
|
|
|
|
room_key_request::RoomKeyRequestEventContent,
|
2020-08-15 01:09:13 +00:00
|
|
|
AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent, ToDeviceEvent,
|
|
|
|
},
|
|
|
|
identifiers::{
|
|
|
|
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, RoomId, UserId,
|
2020-07-30 23:22:48 +00:00
|
|
|
},
|
|
|
|
uuid::Uuid,
|
|
|
|
Raw,
|
2020-03-12 14:41:11 +00:00
|
|
|
};
|
2020-03-10 12:02:14 +00:00
|
|
|
|
2020-08-13 09:06:26 +00:00
|
|
|
#[cfg(feature = "sqlite_cryptostore")]
|
2020-08-02 12:05:43 +00:00
|
|
|
use super::store::sqlite::SqliteStore;
|
|
|
|
use super::{
|
2020-08-17 14:17:28 +00:00
|
|
|
device::{DeviceWrap, ReadOnlyDevice, UserDevicesWrap},
|
2020-08-02 12:05:43 +00:00
|
|
|
error::{EventError, MegolmError, MegolmResult, OlmError, OlmResult},
|
|
|
|
olm::{
|
2020-08-13 12:37:33 +00:00
|
|
|
Account, EncryptionSettings, GroupSessionKey, IdentityKeys, InboundGroupSession,
|
|
|
|
OlmMessage, OutboundGroupSession,
|
2020-08-02 12:05:43 +00:00
|
|
|
},
|
|
|
|
store::{memorystore::MemoryStore, Result as StoreResult},
|
2020-08-04 09:23:24 +00:00
|
|
|
verification::{Sas, VerificationMachine},
|
2020-08-17 13:34:05 +00:00
|
|
|
CryptoStore,
|
2020-08-02 12:05:43 +00:00
|
|
|
};
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// A map from the algorithm and device id to a one-time key.
|
|
|
|
///
|
|
|
|
/// These keys need to be periodically uploaded to the server.
|
2020-08-10 10:39:00 +00:00
|
|
|
pub type OneTimeKeys = BTreeMap<DeviceKeyId, OneTimeKey>;
|
2020-03-10 12:41:14 +00:00
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// State machine implementation of the Olm/Megolm encryption protocol used for
|
|
|
|
/// Matrix end to end encryption.
|
2020-08-11 10:22:14 +00:00
|
|
|
#[derive(Clone)]
|
2020-03-11 09:04:04 +00:00
|
|
|
pub struct OlmMachine {
|
2020-02-25 13:24:18 +00:00
|
|
|
/// The unique user id that owns this account.
|
2020-04-28 13:05:20 +00:00
|
|
|
user_id: UserId,
|
2020-02-25 13:24:18 +00:00
|
|
|
/// The unique device id of the device that holds this account.
|
2020-07-18 12:51:19 +00:00
|
|
|
device_id: Box<DeviceId>,
|
2020-02-25 13:24:18 +00:00
|
|
|
/// Our underlying Olm Account holding our identity keys.
|
2020-08-11 13:49:04 +00:00
|
|
|
account: Account,
|
2020-03-18 14:50:32 +00:00
|
|
|
/// Store for the encryption keys.
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Persists all the encryption keys so a client can resume the session
|
2020-03-18 14:50:32 +00:00
|
|
|
/// without the need to create new keys.
|
2020-08-11 13:39:50 +00:00
|
|
|
store: Arc<Box<dyn CryptoStore>>,
|
2020-04-08 13:06:57 +00:00
|
|
|
/// The currently active outbound group sessions.
|
2020-08-11 10:22:14 +00:00
|
|
|
outbound_group_sessions: Arc<DashMap<RoomId, OutboundGroupSession>>,
|
2020-07-28 13:37:20 +00:00
|
|
|
/// A state machine that is responsible to handle and keep track of SAS
|
|
|
|
/// verification flows.
|
|
|
|
verification_machine: VerificationMachine,
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 13:49:04 +00:00
|
|
|
#[cfg(not(tarpaulin_include))]
|
2020-04-23 09:37:47 +00:00
|
|
|
impl std::fmt::Debug for OlmMachine {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("OlmMachine")
|
|
|
|
.field("user_id", &self.user_id)
|
|
|
|
.field("device_id", &self.device_id)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:24:18 +00:00
|
|
|
impl OlmMachine {
|
2020-04-09 14:24:40 +00:00
|
|
|
const MAX_TO_DEVICE_MESSAGES: usize = 20;
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Create a new memory based OlmMachine.
|
|
|
|
///
|
|
|
|
/// The created machine will keep the encryption keys only in memory and
|
|
|
|
/// once the object is dropped the keys will be lost.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The unique id of the user that owns this machine.
|
|
|
|
///
|
|
|
|
/// * `device_id` - The unique id of the device that owns this machine.
|
2020-07-10 13:43:32 +00:00
|
|
|
pub fn new(user_id: &UserId, device_id: &DeviceId) -> Self {
|
2020-07-28 13:37:20 +00:00
|
|
|
let store: Box<dyn CryptoStore> = Box::new(MemoryStore::new());
|
2020-08-11 13:39:50 +00:00
|
|
|
let store = Arc::new(store);
|
2020-07-28 13:37:20 +00:00
|
|
|
let account = Account::new(user_id, device_id);
|
|
|
|
|
2020-04-30 11:16:10 +00:00
|
|
|
OlmMachine {
|
2020-03-11 09:04:04 +00:00
|
|
|
user_id: user_id.clone(),
|
2020-07-18 12:51:19 +00:00
|
|
|
device_id: device_id.into(),
|
2020-07-28 13:37:20 +00:00
|
|
|
account: account.clone(),
|
|
|
|
store: store.clone(),
|
2020-08-11 10:22:14 +00:00
|
|
|
outbound_group_sessions: Arc::new(DashMap::new()),
|
2020-07-28 13:37:20 +00:00
|
|
|
verification_machine: VerificationMachine::new(account, store),
|
2020-04-30 11:16:10 +00:00
|
|
|
}
|
2020-03-18 14:50:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:12:51 +00:00
|
|
|
/// Create a new OlmMachine with the given [`CryptoStore`].
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
|
|
|
/// The created machine will keep the encryption keys only in memory and
|
|
|
|
/// once the object is dropped the keys will be lost.
|
|
|
|
///
|
|
|
|
/// If the store already contains encryption keys for the given user/device
|
|
|
|
/// pair those will be re-used. Otherwise new ones will be created and
|
|
|
|
/// stored.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The unique id of the user that owns this machine.
|
|
|
|
///
|
|
|
|
/// * `device_id` - The unique id of the device that owns this machine.
|
|
|
|
///
|
|
|
|
/// * `store` - A `Cryptostore` implementation that will be used to store
|
|
|
|
/// the encryption keys.
|
2020-08-12 13:12:51 +00:00
|
|
|
///
|
|
|
|
/// [`Cryptostore`]: trait.CryptoStore.html
|
2020-04-30 15:10:12 +00:00
|
|
|
pub async fn new_with_store(
|
2020-05-25 12:21:04 +00:00
|
|
|
user_id: UserId,
|
2020-07-18 12:51:19 +00:00
|
|
|
device_id: Box<DeviceId>,
|
2020-08-11 13:08:07 +00:00
|
|
|
store: Box<dyn CryptoStore>,
|
2020-07-14 11:11:44 +00:00
|
|
|
) -> StoreResult<Self> {
|
2020-03-18 15:05:59 +00:00
|
|
|
let account = match store.load_account().await? {
|
2020-03-19 12:55:04 +00:00
|
|
|
Some(a) => {
|
|
|
|
debug!("Restored account");
|
|
|
|
a
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
debug!("Creating a new account");
|
2020-07-10 13:43:32 +00:00
|
|
|
Account::new(&user_id, &device_id)
|
2020-03-19 12:55:04 +00:00
|
|
|
}
|
2020-03-18 15:05:59 +00:00
|
|
|
};
|
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
let store = Arc::new(store);
|
2020-07-28 13:37:20 +00:00
|
|
|
let verification_machine = VerificationMachine::new(account.clone(), store.clone());
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
Ok(OlmMachine {
|
2020-07-10 13:43:32 +00:00
|
|
|
user_id,
|
|
|
|
device_id,
|
2020-04-10 13:28:43 +00:00
|
|
|
account,
|
2020-07-28 13:37:20 +00:00
|
|
|
store,
|
2020-08-11 10:22:14 +00:00
|
|
|
outbound_group_sessions: Arc::new(DashMap::new()),
|
2020-07-28 13:37:20 +00:00
|
|
|
verification_machine,
|
2020-03-18 13:15:56 +00:00
|
|
|
})
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Create a new machine with the default crypto store.
|
|
|
|
///
|
|
|
|
/// The default store uses a SQLite database to store the encryption keys.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The unique id of the user that owns this machine.
|
|
|
|
///
|
|
|
|
/// * `device_id` - The unique id of the device that owns this machine.
|
2020-08-13 09:06:26 +00:00
|
|
|
#[cfg(feature = "sqlite_cryptostore")]
|
2020-08-12 13:12:51 +00:00
|
|
|
#[instrument(skip(path, passphrase))]
|
2020-08-13 09:06:26 +00:00
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))]
|
2020-04-30 15:10:12 +00:00
|
|
|
pub async fn new_with_default_store<P: AsRef<Path>>(
|
|
|
|
user_id: &UserId,
|
2020-07-21 08:48:15 +00:00
|
|
|
device_id: &DeviceId,
|
2020-04-30 15:10:12 +00:00
|
|
|
path: P,
|
2020-05-25 12:21:04 +00:00
|
|
|
passphrase: &str,
|
2020-07-14 11:11:44 +00:00
|
|
|
) -> StoreResult<Self> {
|
2020-04-30 15:10:12 +00:00
|
|
|
let store =
|
|
|
|
SqliteStore::open_with_passphrase(&user_id, device_id, path, passphrase).await?;
|
|
|
|
|
2020-07-18 12:51:19 +00:00
|
|
|
OlmMachine::new_with_store(user_id.to_owned(), device_id.into(), Box::new(store)).await
|
2020-04-30 15:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:14:16 +00:00
|
|
|
/// The unique user id that owns this `OlmMachine` instance.
|
2020-04-30 12:07:49 +00:00
|
|
|
pub fn user_id(&self) -> &UserId {
|
2020-04-28 13:05:20 +00:00
|
|
|
&self.user_id
|
|
|
|
}
|
|
|
|
|
2020-08-12 13:14:16 +00:00
|
|
|
/// The unique device id that identifies this `OlmMachine`.
|
2020-04-30 12:07:49 +00:00
|
|
|
pub fn device_id(&self) -> &DeviceId {
|
2020-04-28 13:05:20 +00:00
|
|
|
&self.device_id
|
|
|
|
}
|
|
|
|
|
2020-08-12 13:14:16 +00:00
|
|
|
/// Get the public parts of our Olm identity keys.
|
2020-04-30 12:07:49 +00:00
|
|
|
pub fn identity_keys(&self) -> &IdentityKeys {
|
2020-04-28 13:05:20 +00:00
|
|
|
self.account.identity_keys()
|
|
|
|
}
|
|
|
|
|
2020-08-12 13:14:16 +00:00
|
|
|
/// Should device or one-time keys be uploaded to the server.
|
2020-08-12 13:12:51 +00:00
|
|
|
///
|
|
|
|
/// This needs to be checked periodically, ideally after every sync request.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// # use matrix_sdk_crypto::OlmMachine;
|
|
|
|
/// # use matrix_sdk_common::identifiers::UserId;
|
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # let alice = UserId::try_from("@alice:example.org").unwrap();
|
|
|
|
/// # let machine = OlmMachine::new(&alice, "DEVICEID".into());
|
|
|
|
/// # block_on(async {
|
|
|
|
/// if machine.should_upload_keys().await {
|
|
|
|
/// let request = machine
|
|
|
|
/// .keys_for_upload()
|
|
|
|
/// .await
|
|
|
|
/// .unwrap();
|
|
|
|
///
|
|
|
|
/// // Upload the keys here.
|
|
|
|
/// }
|
|
|
|
/// # });
|
|
|
|
/// ```
|
2020-03-18 14:50:32 +00:00
|
|
|
pub async fn should_upload_keys(&self) -> bool {
|
2020-07-13 14:46:51 +00:00
|
|
|
self.account.should_upload_keys().await
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 13:49:04 +00:00
|
|
|
/// Get the underlying Olm account of the machine.
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn account(&self) -> &Account {
|
|
|
|
&self.account
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Update the count of one-time keys that are currently on the server.
|
2020-08-12 13:14:16 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `count` - The key count of the signed one-time keys that we have on
|
|
|
|
/// the server. This should be fetched from the server using a sync
|
|
|
|
/// response.
|
2020-08-10 12:11:55 +00:00
|
|
|
fn update_key_count(&self, count: u64) {
|
2020-07-13 13:49:16 +00:00
|
|
|
self.account.update_uploaded_key_count(count);
|
2020-04-28 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:36:11 +00:00
|
|
|
/// Receive a successful keys upload response.
|
2020-02-25 13:24:18 +00:00
|
|
|
///
|
2020-02-25 16:36:11 +00:00
|
|
|
/// # Arguments
|
2020-02-25 13:24:18 +00:00
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `response` - The keys upload response of the request that the client
|
2020-03-11 09:04:04 +00:00
|
|
|
/// performed.
|
2020-03-19 12:55:04 +00:00
|
|
|
#[instrument]
|
2020-03-18 14:50:32 +00:00
|
|
|
pub async fn receive_keys_upload_response(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-08-10 10:39:00 +00:00
|
|
|
response: &upload_keys::Response,
|
2020-04-30 11:16:10 +00:00
|
|
|
) -> OlmResult<()> {
|
2020-04-10 13:28:43 +00:00
|
|
|
if !self.account.shared() {
|
2020-03-19 12:55:04 +00:00
|
|
|
debug!("Marking account as shared");
|
|
|
|
}
|
2020-04-10 13:28:43 +00:00
|
|
|
self.account.mark_as_shared();
|
2020-03-11 09:04:04 +00:00
|
|
|
|
2020-02-25 13:24:18 +00:00
|
|
|
let one_time_key_count = response
|
|
|
|
.one_time_key_counts
|
2020-08-10 10:39:00 +00:00
|
|
|
.get(&DeviceKeyAlgorithm::SignedCurve25519);
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-03-11 09:09:00 +00:00
|
|
|
let count: u64 = one_time_key_count.map_or(0, |c| (*c).into());
|
2020-03-19 12:55:04 +00:00
|
|
|
debug!(
|
|
|
|
"Updated uploaded one-time key count {} -> {}, marking keys as published",
|
2020-07-13 13:49:16 +00:00
|
|
|
self.account.uploaded_key_count(),
|
2020-03-19 12:55:04 +00:00
|
|
|
count
|
|
|
|
);
|
2020-04-28 13:47:49 +00:00
|
|
|
self.update_key_count(count);
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-04-10 13:28:43 +00:00
|
|
|
self.account.mark_keys_as_published().await;
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.save_account(self.account.clone()).await?;
|
2020-03-26 11:24:53 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
Ok(())
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Get the user/device pairs for which no Olm session exists.
|
|
|
|
///
|
|
|
|
/// Returns a map from the user id, to a map from the device id to a key
|
|
|
|
/// algorithm.
|
|
|
|
///
|
|
|
|
/// This can be used to make a key claiming request to the server.
|
|
|
|
///
|
|
|
|
/// Sessions need to be established between devices so group sessions for a
|
|
|
|
/// room can be shared with them.
|
|
|
|
///
|
|
|
|
/// This should be called every time a group session needs to be shared.
|
|
|
|
///
|
|
|
|
/// The response of a successful key claiming requests needs to be passed to
|
2020-08-12 11:11:51 +00:00
|
|
|
/// the `OlmMachine` with the [`receive_keys_claim_response`].
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `users` - The list of users that we should check if we lack a session
|
|
|
|
/// with one of their devices.
|
2020-08-12 11:11:51 +00:00
|
|
|
///
|
|
|
|
/// [`receive_keys_claim_response`]: #method.receive_keys_claim_response
|
2020-04-03 08:20:03 +00:00
|
|
|
pub async fn get_missing_sessions(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-04-03 15:00:37 +00:00
|
|
|
users: impl Iterator<Item = &UserId>,
|
2020-08-10 10:39:00 +00:00
|
|
|
) -> OlmResult<BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeyAlgorithm>>> {
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut missing = BTreeMap::new();
|
2020-04-03 08:20:03 +00:00
|
|
|
|
|
|
|
for user_id in users {
|
2020-08-11 13:39:50 +00:00
|
|
|
let user_devices = self.store.get_user_devices(user_id).await?;
|
2020-04-03 08:20:03 +00:00
|
|
|
|
|
|
|
for device in user_devices.devices() {
|
2020-08-10 10:39:00 +00:00
|
|
|
let sender_key = if let Some(k) = device.get_key(DeviceKeyAlgorithm::Curve25519) {
|
2020-04-03 08:20:03 +00:00
|
|
|
k
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
let sessions = self.store.get_sessions(sender_key).await?;
|
2020-04-03 08:20:03 +00:00
|
|
|
|
|
|
|
let is_missing = if let Some(sessions) = sessions {
|
|
|
|
sessions.lock().await.is_empty()
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
if is_missing {
|
2020-04-03 15:00:37 +00:00
|
|
|
if !missing.contains_key(user_id) {
|
2020-04-30 15:10:12 +00:00
|
|
|
let _ = missing.insert(user_id.clone(), BTreeMap::new());
|
2020-04-03 08:20:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 15:00:37 +00:00
|
|
|
let user_map = missing.get_mut(user_id).unwrap();
|
2020-08-10 10:39:00 +00:00
|
|
|
let _ = user_map.insert(
|
|
|
|
device.device_id().into(),
|
|
|
|
DeviceKeyAlgorithm::SignedCurve25519,
|
|
|
|
);
|
2020-04-03 08:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:26:03 +00:00
|
|
|
Ok(missing)
|
2020-04-03 08:20:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Receive a successful key claim response and create new Olm sessions with
|
|
|
|
/// the claimed keys.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The response containing the claimed one-time keys.
|
2020-04-02 14:07:21 +00:00
|
|
|
pub async fn receive_keys_claim_response(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-08-10 10:39:00 +00:00
|
|
|
response: &claim_keys::Response,
|
2020-04-30 11:16:10 +00:00
|
|
|
) -> OlmResult<()> {
|
2020-04-02 14:07:21 +00:00
|
|
|
// TODO log the failures here
|
|
|
|
|
|
|
|
for (user_id, user_devices) in &response.one_time_keys {
|
|
|
|
for (device_id, key_map) in user_devices {
|
2020-08-17 14:17:28 +00:00
|
|
|
let device = match self.store.get_device(&user_id, device_id).await {
|
2020-07-25 00:31:52 +00:00
|
|
|
Ok(Some(d)) => d,
|
|
|
|
Ok(None) => {
|
|
|
|
warn!(
|
|
|
|
"Tried to create an Olm session for {} {}, but the device is unknown",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
2020-04-02 14:07:21 +00:00
|
|
|
}
|
2020-07-15 13:39:56 +00:00
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"Tried to create an Olm session for {} {}, but \
|
|
|
|
can't fetch the device from the store {:?}",
|
|
|
|
user_id, device_id, e
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-02 14:07:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
info!("Creating outbound Session for {} {}", user_id, device_id);
|
|
|
|
|
2020-07-15 13:39:56 +00:00
|
|
|
let session = match self.account.create_outbound_session(device, &key_map).await {
|
2020-04-02 14:07:21 +00:00
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
2020-07-15 13:39:56 +00:00
|
|
|
warn!("{:?}", e);
|
2020-04-02 14:07:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
if let Err(e) = self.store.save_sessions(&[session]).await {
|
2020-04-02 14:07:21 +00:00
|
|
|
error!("Failed to store newly created Olm session {}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO if this session was created because a previous one was
|
|
|
|
// wedged queue up a dummy event to be sent out.
|
|
|
|
// TODO if this session was created because of a key request,
|
|
|
|
// mark the forwarding keys to be sent out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-12 13:14:16 +00:00
|
|
|
/// Handle the device keys part of a key query response.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `device_keys_map` - A map holding the device keys of the users for
|
|
|
|
/// which the key query was done.
|
|
|
|
///
|
|
|
|
/// Returns a list of devices that changed. Changed here means either
|
|
|
|
/// they are new, one of their properties has changed or they got deleted.
|
2020-07-14 11:08:57 +00:00
|
|
|
async fn handle_devices_from_key_query(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-07-18 12:51:19 +00:00
|
|
|
device_keys_map: &BTreeMap<UserId, BTreeMap<Box<DeviceId>, DeviceKeys>>,
|
2020-08-17 14:17:28 +00:00
|
|
|
) -> StoreResult<Vec<ReadOnlyDevice>> {
|
2020-04-03 08:21:14 +00:00
|
|
|
let mut changed_devices = Vec::new();
|
|
|
|
|
2020-07-14 11:08:57 +00:00
|
|
|
for (user_id, device_map) in device_keys_map {
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.update_tracked_user(user_id, false).await?;
|
2020-04-02 09:14:23 +00:00
|
|
|
|
|
|
|
for (device_id, device_keys) in device_map.iter() {
|
|
|
|
// We don't need our own device in the device store.
|
|
|
|
if user_id == &self.user_id && device_id == &self.device_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_id != &device_keys.user_id || device_id != &device_keys.device_id {
|
|
|
|
warn!(
|
|
|
|
"Mismatch in device keys payload of device {} from user {}",
|
|
|
|
device_keys.device_id, device_keys.user_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
let device = self.store.get_device(&user_id, device_id).await?;
|
2020-04-02 09:14:23 +00:00
|
|
|
|
2020-07-14 09:17:09 +00:00
|
|
|
let device = if let Some(mut device) = device {
|
|
|
|
if let Err(e) = device.update_device(device_keys) {
|
|
|
|
warn!(
|
|
|
|
"Failed to update the device keys for {} {}: {:?}",
|
|
|
|
user_id, device_id, e
|
|
|
|
);
|
|
|
|
continue;
|
2020-04-21 07:44:44 +00:00
|
|
|
}
|
2020-07-14 09:17:09 +00:00
|
|
|
device
|
2020-04-02 09:14:23 +00:00
|
|
|
} else {
|
2020-08-17 14:17:28 +00:00
|
|
|
let device = match ReadOnlyDevice::try_from(device_keys) {
|
2020-07-14 09:17:09 +00:00
|
|
|
Ok(d) => d,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"Failed to create a new device for {} {}: {:?}",
|
|
|
|
user_id, device_id, e
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2020-04-21 07:44:44 +00:00
|
|
|
info!("Adding a new device to the device store {:?}", device);
|
|
|
|
device
|
|
|
|
};
|
|
|
|
|
|
|
|
changed_devices.push(device);
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
2020-04-02 12:52:15 +00:00
|
|
|
|
2020-07-18 12:51:19 +00:00
|
|
|
let current_devices: HashSet<&DeviceId> =
|
|
|
|
device_map.keys().map(|id| id.as_ref()).collect();
|
2020-08-11 13:39:50 +00:00
|
|
|
let stored_devices = self.store.get_user_devices(&user_id).await.unwrap();
|
2020-04-09 14:22:25 +00:00
|
|
|
let stored_devices_set: HashSet<&DeviceId> = stored_devices.keys().collect();
|
2020-04-02 12:52:15 +00:00
|
|
|
|
|
|
|
let deleted_devices = stored_devices_set.difference(¤t_devices);
|
|
|
|
|
2020-04-21 07:44:44 +00:00
|
|
|
for device_id in deleted_devices {
|
|
|
|
if let Some(device) = stored_devices.get(device_id) {
|
|
|
|
device.mark_as_deleted();
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.delete_device(device).await?;
|
2020-04-21 07:44:44 +00:00
|
|
|
}
|
2020-04-02 12:52:15 +00:00
|
|
|
}
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
2020-04-03 08:21:14 +00:00
|
|
|
|
2020-07-14 11:08:57 +00:00
|
|
|
Ok(changed_devices)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a successful keys query response.
|
|
|
|
///
|
|
|
|
/// Returns a list of devices newly discovered devices and devices that
|
|
|
|
/// changed.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The keys query response of the request that the client
|
|
|
|
/// performed.
|
|
|
|
pub async fn receive_keys_query_response(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-08-10 10:39:00 +00:00
|
|
|
response: &get_keys::Response,
|
2020-08-17 14:17:28 +00:00
|
|
|
) -> OlmResult<Vec<ReadOnlyDevice>> {
|
2020-07-14 11:08:57 +00:00
|
|
|
let changed_devices = self
|
|
|
|
.handle_devices_from_key_query(&response.device_keys)
|
|
|
|
.await?;
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.save_devices(&changed_devices).await?;
|
2020-04-03 08:21:14 +00:00
|
|
|
|
2020-04-21 07:44:44 +00:00
|
|
|
Ok(changed_devices)
|
2020-04-01 13:37:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:14:16 +00:00
|
|
|
/// Get a request to upload E2EE keys to the server.
|
2020-03-10 13:06:30 +00:00
|
|
|
///
|
|
|
|
/// Returns an empty error if no keys need to be uploaded.
|
2020-08-12 11:11:51 +00:00
|
|
|
///
|
|
|
|
/// The response of a successful key upload requests needs to be passed to
|
|
|
|
/// the [`OlmMachine`] with the [`receive_keys_upload_response`].
|
|
|
|
///
|
|
|
|
/// [`receive_keys_upload_response`]: #method.receive_keys_upload_response
|
|
|
|
/// [`OlmMachine`]: struct.OlmMachine.html
|
2020-08-12 13:12:51 +00:00
|
|
|
pub async fn keys_for_upload(&self) -> StdResult<upload_keys::Request, ()> {
|
|
|
|
let (device_keys, one_time_keys) = self.account.keys_for_upload().await?;
|
|
|
|
|
|
|
|
Ok(upload_keys::Request {
|
|
|
|
device_keys,
|
|
|
|
one_time_keys,
|
|
|
|
})
|
2020-03-10 13:06:30 +00:00
|
|
|
}
|
2020-03-12 14:41:11 +00:00
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Try to decrypt an Olm message.
|
|
|
|
///
|
|
|
|
/// This try to decrypt an Olm message using all the sessions we share
|
|
|
|
/// have with the given sender.
|
|
|
|
async fn try_decrypt_olm_message(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-04-22 13:15:08 +00:00
|
|
|
sender: &UserId,
|
2020-03-21 15:41:48 +00:00
|
|
|
sender_key: &str,
|
|
|
|
message: &OlmMessage,
|
2020-04-30 11:16:10 +00:00
|
|
|
) -> OlmResult<Option<String>> {
|
2020-08-11 13:39:50 +00:00
|
|
|
let s = self.store.get_sessions(sender_key).await?;
|
2020-03-21 15:41:48 +00:00
|
|
|
|
2020-04-22 13:15:08 +00:00
|
|
|
// We don't have any existing sessions, return early.
|
2020-03-21 15:41:48 +00:00
|
|
|
let sessions = if let Some(s) = s {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
2020-04-23 09:37:47 +00:00
|
|
|
let mut session_to_save = None;
|
|
|
|
let mut plaintext = None;
|
|
|
|
|
2020-04-14 12:05:18 +00:00
|
|
|
for session in &mut *sessions.lock().await {
|
2020-03-21 15:41:48 +00:00
|
|
|
let mut matches = false;
|
|
|
|
|
2020-04-22 10:54:49 +00:00
|
|
|
// If this is a pre-key message check if it was encrypted for our
|
|
|
|
// session, if it wasn't decryption will fail so no need to try.
|
2020-03-21 15:41:48 +00:00
|
|
|
if let OlmMessage::PreKey(m) = &message {
|
2020-04-14 12:05:18 +00:00
|
|
|
matches = session.matches(sender_key, m.clone()).await?;
|
2020-04-22 10:54:49 +00:00
|
|
|
|
2020-03-21 15:41:48 +00:00
|
|
|
if !matches {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 12:05:18 +00:00
|
|
|
let ret = session.decrypt(message.clone()).await;
|
2020-03-21 15:41:48 +00:00
|
|
|
|
|
|
|
if let Ok(p) = ret {
|
2020-04-23 09:37:47 +00:00
|
|
|
plaintext = Some(p);
|
|
|
|
session_to_save = Some(session.clone());
|
2020-04-22 10:54:49 +00:00
|
|
|
|
2020-04-23 09:37:47 +00:00
|
|
|
break;
|
2020-03-21 15:41:48 +00:00
|
|
|
} else {
|
2020-04-22 10:54:49 +00:00
|
|
|
// Decryption failed with a matching session, the session is
|
|
|
|
// likely wedged and needs to be rotated.
|
2020-03-21 15:41:48 +00:00
|
|
|
if matches {
|
2020-04-22 10:54:49 +00:00
|
|
|
warn!(
|
|
|
|
"Found a matching Olm session yet decryption failed
|
|
|
|
for sender {} and sender_key {}",
|
|
|
|
sender, sender_key
|
|
|
|
);
|
2020-03-21 15:41:48 +00:00
|
|
|
return Err(OlmError::SessionWedged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 09:37:47 +00:00
|
|
|
if let Some(session) = session_to_save {
|
|
|
|
// Decryption was successful, save the new ratchet state of the
|
|
|
|
// session that was used to decrypt the message.
|
|
|
|
trace!("Saved the new session state for {}", sender);
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.save_sessions(&[session]).await?;
|
2020-04-23 09:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(plaintext)
|
2020-03-21 15:41:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:14:16 +00:00
|
|
|
/// Decrypt an Olm message, creating a new Olm session if possible.
|
2020-03-21 15:41:48 +00:00
|
|
|
async fn decrypt_olm_message(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-04-22 13:15:08 +00:00
|
|
|
sender: &UserId,
|
2020-03-21 15:41:48 +00:00
|
|
|
sender_key: &str,
|
|
|
|
message: OlmMessage,
|
2020-07-26 14:58:27 +00:00
|
|
|
) -> OlmResult<(Raw<AnyToDeviceEvent>, String)> {
|
2020-04-22 10:54:49 +00:00
|
|
|
// First try to decrypt using an existing session.
|
|
|
|
let plaintext = if let Some(p) = self
|
2020-04-30 15:10:12 +00:00
|
|
|
.try_decrypt_olm_message(sender, sender_key, &message)
|
2020-04-22 10:54:49 +00:00
|
|
|
.await?
|
|
|
|
{
|
2020-04-30 15:10:12 +00:00
|
|
|
// Decryption succeeded, de-structure the plaintext out of the
|
2020-04-22 10:54:49 +00:00
|
|
|
// Option.
|
2020-03-21 15:41:48 +00:00
|
|
|
p
|
|
|
|
} else {
|
2020-04-22 10:54:49 +00:00
|
|
|
// Decryption failed with every known session, let's try to create a
|
|
|
|
// new session.
|
2020-03-21 15:41:48 +00:00
|
|
|
let mut session = match &message {
|
2020-04-22 13:15:08 +00:00
|
|
|
// A new session can only be created using a pre-key message,
|
|
|
|
// return with an error if it isn't one.
|
2020-04-22 10:54:49 +00:00
|
|
|
OlmMessage::Message(_) => {
|
|
|
|
warn!(
|
|
|
|
"Failed to decrypt a non-pre-key message with all
|
|
|
|
available sessions {} {}",
|
|
|
|
sender, sender_key
|
|
|
|
);
|
|
|
|
return Err(OlmError::SessionWedged);
|
|
|
|
}
|
|
|
|
|
2020-03-21 15:41:48 +00:00
|
|
|
OlmMessage::PreKey(m) => {
|
2020-04-22 13:15:08 +00:00
|
|
|
// Create the new session.
|
2020-04-22 10:54:49 +00:00
|
|
|
let session = match self
|
2020-04-10 14:18:55 +00:00
|
|
|
.account
|
2020-04-10 13:28:43 +00:00
|
|
|
.create_inbound_session(sender_key, m.clone())
|
2020-04-22 10:54:49 +00:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"Failed to create a new Olm session for {} {}
|
|
|
|
from a prekey message: {}",
|
|
|
|
sender, sender_key, e
|
|
|
|
);
|
|
|
|
return Err(OlmError::SessionWedged);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-22 13:15:08 +00:00
|
|
|
// Save the account since we remove the one-time key that
|
|
|
|
// was used to create this session.
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.save_account(self.account.clone()).await?;
|
2020-04-10 14:18:55 +00:00
|
|
|
session
|
2020-03-21 15:41:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-22 13:15:08 +00:00
|
|
|
// Decrypt our message, this shouldn't fail since we're using a
|
|
|
|
// newly created Session.
|
2020-04-14 12:05:18 +00:00
|
|
|
let plaintext = session.decrypt(message).await?;
|
2020-04-22 10:54:49 +00:00
|
|
|
|
2020-04-22 13:15:08 +00:00
|
|
|
// Save the new ratcheted state of the session.
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.save_sessions(&[session]).await?;
|
2020-03-26 10:22:40 +00:00
|
|
|
plaintext
|
2020-03-21 15:41:48 +00:00
|
|
|
};
|
|
|
|
|
2020-03-24 15:21:06 +00:00
|
|
|
trace!("Successfully decrypted a Olm message: {}", plaintext);
|
2020-04-22 10:54:49 +00:00
|
|
|
|
2020-04-22 13:15:08 +00:00
|
|
|
Ok(self.parse_decrypted_to_device_event(sender, &plaintext)?)
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Parse a decrypted Olm message, check that the plaintext and encrypted
|
|
|
|
/// senders match and that the message was meant for us.
|
2020-04-22 13:15:08 +00:00
|
|
|
fn parse_decrypted_to_device_event(
|
|
|
|
&self,
|
|
|
|
sender: &UserId,
|
|
|
|
plaintext: &str,
|
2020-07-26 14:58:27 +00:00
|
|
|
) -> OlmResult<(Raw<AnyToDeviceEvent>, String)> {
|
2020-04-22 13:15:08 +00:00
|
|
|
// TODO make the errors a bit more specific.
|
|
|
|
let decrypted_json: Value = serde_json::from_str(&plaintext)?;
|
|
|
|
|
|
|
|
let encrytped_sender = decrypted_json
|
|
|
|
.get("sender")
|
|
|
|
.cloned()
|
2020-04-30 12:07:49 +00:00
|
|
|
.ok_or_else(|| EventError::MissingField("sender".to_string()))?;
|
2020-04-22 13:15:08 +00:00
|
|
|
let encrytped_sender: UserId = serde_json::from_value(encrytped_sender)?;
|
|
|
|
let recipient = decrypted_json
|
|
|
|
.get("recipient")
|
|
|
|
.cloned()
|
2020-04-30 12:07:49 +00:00
|
|
|
.ok_or_else(|| EventError::MissingField("recipient".to_string()))?;
|
2020-04-22 13:15:08 +00:00
|
|
|
let recipient: UserId = serde_json::from_value(recipient)?;
|
|
|
|
|
2020-08-10 10:39:00 +00:00
|
|
|
let recipient_keys: BTreeMap<DeviceKeyAlgorithm, String> = serde_json::from_value(
|
2020-04-22 13:15:08 +00:00
|
|
|
decrypted_json
|
|
|
|
.get("recipient_keys")
|
|
|
|
.cloned()
|
2020-04-30 12:07:49 +00:00
|
|
|
.ok_or_else(|| EventError::MissingField("recipient_keys".to_string()))?,
|
2020-04-22 13:15:08 +00:00
|
|
|
)?;
|
2020-08-10 10:39:00 +00:00
|
|
|
let keys: BTreeMap<DeviceKeyAlgorithm, String> = serde_json::from_value(
|
2020-04-22 13:15:08 +00:00
|
|
|
decrypted_json
|
|
|
|
.get("keys")
|
|
|
|
.cloned()
|
2020-04-30 12:07:49 +00:00
|
|
|
.ok_or_else(|| EventError::MissingField("keys".to_string()))?,
|
2020-04-22 13:15:08 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
if recipient != self.user_id || sender != &encrytped_sender {
|
2020-04-30 12:07:49 +00:00
|
|
|
return Err(EventError::MissmatchedSender.into());
|
2020-04-22 13:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.account.identity_keys().ed25519()
|
|
|
|
!= recipient_keys
|
2020-08-10 10:39:00 +00:00
|
|
|
.get(&DeviceKeyAlgorithm::Ed25519)
|
2020-04-30 11:16:10 +00:00
|
|
|
.ok_or(EventError::MissingSigningKey)?
|
2020-04-22 13:15:08 +00:00
|
|
|
{
|
2020-04-30 12:07:49 +00:00
|
|
|
return Err(EventError::MissmatchedKeys.into());
|
2020-04-22 13:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let signing_key = keys
|
2020-08-10 10:39:00 +00:00
|
|
|
.get(&DeviceKeyAlgorithm::Ed25519)
|
2020-04-30 11:16:10 +00:00
|
|
|
.ok_or(EventError::MissingSigningKey)?;
|
2020-04-22 13:15:08 +00:00
|
|
|
|
|
|
|
Ok((
|
2020-07-26 14:58:27 +00:00
|
|
|
Raw::from(serde_json::from_value::<AnyToDeviceEvent>(decrypted_json)?),
|
2020-04-22 13:15:08 +00:00
|
|
|
signing_key.to_owned(),
|
|
|
|
))
|
2020-03-21 15:41:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 14:41:11 +00:00
|
|
|
/// Decrypt a to-device event.
|
|
|
|
///
|
|
|
|
/// Returns a decrypted `ToDeviceEvent` if the decryption was successful,
|
|
|
|
/// an error indicating why decryption failed otherwise.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The to-device event that should be decrypted.
|
2020-03-21 15:41:48 +00:00
|
|
|
async fn decrypt_to_device_event(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &ToDeviceEvent<EncryptedEventContent>,
|
2020-07-26 14:58:27 +00:00
|
|
|
) -> OlmResult<Raw<AnyToDeviceEvent>> {
|
2020-03-19 12:55:04 +00:00
|
|
|
info!("Decrypting to-device event");
|
2020-03-21 15:41:48 +00:00
|
|
|
|
|
|
|
let content = if let EncryptedEventContent::OlmV1Curve25519AesSha2(c) = &event.content {
|
|
|
|
c
|
|
|
|
} else {
|
|
|
|
warn!("Error, unsupported encryption algorithm");
|
2020-04-30 12:07:49 +00:00
|
|
|
return Err(EventError::UnsupportedAlgorithm.into());
|
2020-03-21 15:41:48 +00:00
|
|
|
};
|
|
|
|
|
2020-04-10 13:28:43 +00:00
|
|
|
let identity_keys = self.account.identity_keys();
|
2020-03-21 15:41:48 +00:00
|
|
|
let own_key = identity_keys.curve25519();
|
|
|
|
let own_ciphertext = content.ciphertext.get(own_key);
|
|
|
|
|
2020-04-22 10:54:49 +00:00
|
|
|
// Try to find a ciphertext that was meant for our device.
|
2020-03-21 15:41:48 +00:00
|
|
|
if let Some(ciphertext) = own_ciphertext {
|
2020-03-23 15:14:10 +00:00
|
|
|
let message_type: u8 = ciphertext
|
|
|
|
.message_type
|
|
|
|
.try_into()
|
2020-04-30 11:16:10 +00:00
|
|
|
.map_err(|_| EventError::UnsupportedOlmType)?;
|
2020-04-22 10:54:49 +00:00
|
|
|
|
|
|
|
// Create a OlmMessage from the ciphertext and the type.
|
2020-03-21 15:41:48 +00:00
|
|
|
let message =
|
|
|
|
OlmMessage::from_type_and_ciphertext(message_type.into(), ciphertext.body.clone())
|
2020-04-30 11:16:10 +00:00
|
|
|
.map_err(|_| EventError::UnsupportedOlmType)?;
|
2020-03-21 15:41:48 +00:00
|
|
|
|
2020-04-22 10:54:49 +00:00
|
|
|
// Decrypt the OlmMessage and get a Ruma event out of it.
|
2020-05-06 10:15:15 +00:00
|
|
|
let (decrypted_event, signing_key) = self
|
2020-04-22 13:15:08 +00:00
|
|
|
.decrypt_olm_message(&event.sender, &content.sender_key, message)
|
2020-03-24 16:25:01 +00:00
|
|
|
.await?;
|
2020-04-22 10:54:49 +00:00
|
|
|
|
2020-03-24 16:25:01 +00:00
|
|
|
debug!("Decrypted a to-device event {:?}", decrypted_event);
|
2020-04-22 10:54:49 +00:00
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
// Handle the decrypted event, e.g. fetch out Megolm sessions out of
|
2020-04-22 10:54:49 +00:00
|
|
|
// the event.
|
2020-05-06 10:15:15 +00:00
|
|
|
if let Some(event) = self
|
|
|
|
.handle_decrypted_to_device_event(
|
|
|
|
&content.sender_key,
|
|
|
|
&signing_key,
|
|
|
|
&decrypted_event,
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
// Some events may have sensitive data e.g. private keys, while we
|
2020-07-03 19:29:10 +00:00
|
|
|
// want to notify our users that a private key was received we
|
2020-05-06 10:15:15 +00:00
|
|
|
// don't want them to be able to do silly things with it. Handling
|
|
|
|
// events modifies them and returns a modified one, so replace it
|
|
|
|
// here if we get one.
|
|
|
|
Ok(event)
|
|
|
|
} else {
|
|
|
|
Ok(decrypted_event)
|
|
|
|
}
|
2020-03-23 15:14:10 +00:00
|
|
|
} else {
|
|
|
|
warn!("Olm event doesn't contain a ciphertext for our key");
|
2020-04-30 12:07:49 +00:00
|
|
|
Err(EventError::MissingCiphertext.into())
|
2020-03-23 15:14:10 +00:00
|
|
|
}
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Create a group session from a room key and add it to our crypto store.
|
2020-04-22 13:15:08 +00:00
|
|
|
async fn add_room_key(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-04-22 13:15:08 +00:00
|
|
|
sender_key: &str,
|
|
|
|
signing_key: &str,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &mut ToDeviceEvent<RoomKeyEventContent>,
|
2020-07-26 14:58:27 +00:00
|
|
|
) -> OlmResult<Option<Raw<AnyToDeviceEvent>>> {
|
2020-03-24 15:21:06 +00:00
|
|
|
match event.content.algorithm {
|
2020-08-15 01:09:13 +00:00
|
|
|
EventEncryptionAlgorithm::MegolmV1AesSha2 => {
|
2020-04-10 15:02:30 +00:00
|
|
|
let session_key = GroupSessionKey(mem::take(&mut event.content.session_key));
|
2020-04-10 12:00:03 +00:00
|
|
|
|
2020-03-25 10:32:40 +00:00
|
|
|
let session = InboundGroupSession::new(
|
|
|
|
sender_key,
|
2020-03-27 16:01:21 +00:00
|
|
|
signing_key,
|
2020-04-03 15:00:37 +00:00
|
|
|
&event.content.room_id,
|
2020-04-10 12:00:03 +00:00
|
|
|
session_key,
|
2020-03-25 10:32:40 +00:00
|
|
|
)?;
|
2020-08-11 13:39:50 +00:00
|
|
|
let _ = self.store.save_inbound_group_session(session).await?;
|
2020-05-06 10:15:15 +00:00
|
|
|
|
2020-07-26 14:58:27 +00:00
|
|
|
let event = Raw::from(AnyToDeviceEvent::RoomKey(event.clone()));
|
2020-05-06 10:15:15 +00:00
|
|
|
Ok(Some(event))
|
2020-03-25 10:32:40 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
warn!(
|
|
|
|
"Received room key with unsupported key algorithm {}",
|
|
|
|
event.content.algorithm
|
|
|
|
);
|
2020-05-06 10:15:15 +00:00
|
|
|
Ok(None)
|
2020-03-24 16:25:01 +00:00
|
|
|
}
|
2020-03-24 15:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Create a new outbound group session.
|
|
|
|
///
|
|
|
|
/// This also creates a matching inbound group session and saves that one in
|
|
|
|
/// the store.
|
2020-08-13 12:37:33 +00:00
|
|
|
async fn create_outbound_group_session(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
settings: EncryptionSettings,
|
|
|
|
) -> OlmResult<()> {
|
|
|
|
let (outbound, inbound) = self
|
|
|
|
.account
|
|
|
|
.create_group_session_pair(room_id, settings)
|
|
|
|
.await
|
|
|
|
.map_err(|_| EventError::UnsupportedAlgorithm)?;
|
2020-04-08 13:06:57 +00:00
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
let _ = self.store.save_inbound_group_session(inbound).await?;
|
2020-04-08 13:06:57 +00:00
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
let _ = self
|
|
|
|
.outbound_group_sessions
|
2020-06-25 11:31:30 +00:00
|
|
|
.insert(room_id.to_owned(), outbound);
|
2020-04-08 13:06:57 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:37:33 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
async fn create_outnbound_group_session_with_defaults(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> OlmResult<()> {
|
|
|
|
self.create_outbound_group_session(room_id, EncryptionSettings::default())
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2020-08-11 10:22:14 +00:00
|
|
|
/// Get an outbound group session for a room, if one exists.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The id of the room for which we should get the outbound
|
|
|
|
/// group session.
|
|
|
|
fn get_outbound_group_session(&self, room_id: &RoomId) -> Option<OutboundGroupSession> {
|
|
|
|
#[allow(clippy::map_clone)]
|
|
|
|
self.outbound_group_sessions.get(room_id).map(|s| s.clone())
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Encrypt a room message for the given room.
|
|
|
|
///
|
|
|
|
/// Beware that a group session needs to be shared before this method can be
|
2020-08-13 09:04:37 +00:00
|
|
|
/// called using the [`share_group_session`] method.
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
|
|
|
/// Since group sessions can expire or become invalid if the room membership
|
|
|
|
/// changes client authors should check with the
|
2020-08-12 11:28:16 +00:00
|
|
|
/// [`should_share_group_session`] method if a new group session needs to
|
2020-04-30 15:10:12 +00:00
|
|
|
/// be shared.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The id of the room for which the message should be
|
|
|
|
/// encrypted.
|
|
|
|
///
|
|
|
|
/// * `content` - The plaintext content of the message that should be
|
|
|
|
/// encrypted.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if a group session for the given room wasn't shared beforehand.
|
2020-08-12 11:28:16 +00:00
|
|
|
///
|
|
|
|
/// [`should_share_group_session`]: #method.should_share_group_session
|
|
|
|
/// [`share_group_session`]: #method.share_group_session
|
2020-04-09 14:24:40 +00:00
|
|
|
pub async fn encrypt(
|
2020-04-10 09:44:09 +00:00
|
|
|
&self,
|
2020-04-09 14:24:40 +00:00
|
|
|
room_id: &RoomId,
|
|
|
|
content: MessageEventContent,
|
2020-04-30 11:16:10 +00:00
|
|
|
) -> MegolmResult<EncryptedEventContent> {
|
2020-08-11 10:22:14 +00:00
|
|
|
let session = if let Some(s) = self.get_outbound_group_session(room_id) {
|
2020-04-10 09:44:09 +00:00
|
|
|
s
|
|
|
|
} else {
|
|
|
|
panic!("Session wasn't created nor shared");
|
|
|
|
};
|
2020-04-09 14:24:40 +00:00
|
|
|
|
|
|
|
if session.expired() {
|
2020-08-13 13:59:17 +00:00
|
|
|
panic!("Session expired");
|
2020-04-09 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 12:32:59 +00:00
|
|
|
Ok(session.encrypt(content).await)
|
2020-04-09 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
/// Encrypt the given event for the given Device
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `reciepient_device` - The device that the event should be encrypted
|
|
|
|
/// for.
|
|
|
|
///
|
|
|
|
/// * `event_type` - The type of the event.
|
|
|
|
///
|
|
|
|
/// * `content` - The content of the event that should be encrypted.
|
2020-04-09 14:24:40 +00:00
|
|
|
async fn olm_encrypt(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-08-17 14:17:28 +00:00
|
|
|
recipient_device: &ReadOnlyDevice,
|
2020-04-09 14:24:40 +00:00
|
|
|
event_type: EventType,
|
|
|
|
content: Value,
|
2020-04-30 11:16:10 +00:00
|
|
|
) -> OlmResult<EncryptedEventContent> {
|
2020-08-10 10:39:00 +00:00
|
|
|
let sender_key = if let Some(k) = recipient_device.get_key(DeviceKeyAlgorithm::Curve25519) {
|
2020-07-21 12:13:10 +00:00
|
|
|
k
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Trying to encrypt a Megolm session for user {} on device {}, \
|
|
|
|
but the device doesn't have a curve25519 key",
|
|
|
|
recipient_device.user_id(),
|
|
|
|
recipient_device.device_id()
|
|
|
|
);
|
|
|
|
return Err(EventError::MissingSenderKey.into());
|
|
|
|
};
|
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
let mut session = if let Some(s) = self.store.get_sessions(sender_key).await? {
|
2020-07-21 12:13:10 +00:00
|
|
|
let session = &s.lock().await[0];
|
|
|
|
session.clone()
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Trying to encrypt a Megolm session for user {} on device {}, \
|
|
|
|
but no Olm session is found",
|
|
|
|
recipient_device.user_id(),
|
|
|
|
recipient_device.device_id()
|
|
|
|
);
|
|
|
|
return Err(OlmError::MissingSession);
|
|
|
|
};
|
|
|
|
|
2020-07-21 10:46:06 +00:00
|
|
|
let message = session.encrypt(recipient_device, event_type, content).await;
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.save_sessions(&[session]).await?;
|
2020-07-11 10:05:52 +00:00
|
|
|
|
2020-07-21 10:03:05 +00:00
|
|
|
message
|
2020-04-09 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:46:31 +00:00
|
|
|
/// Should the client share a group session for the given room.
|
|
|
|
///
|
|
|
|
/// Returns true if a session needs to be shared before room messages can be
|
|
|
|
/// encrypted, false if one is already shared and ready to encrypt room
|
|
|
|
/// messages.
|
|
|
|
///
|
|
|
|
/// This should be called every time a new room message wants to be sent out
|
|
|
|
/// since group sessions can expire at any time.
|
|
|
|
pub fn should_share_group_session(&self, room_id: &RoomId) -> bool {
|
2020-04-28 08:47:08 +00:00
|
|
|
let session = self.outbound_group_sessions.get(room_id);
|
2020-04-10 09:46:31 +00:00
|
|
|
|
|
|
|
match session {
|
|
|
|
Some(s) => !s.shared() || s.expired(),
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 09:55:12 +00:00
|
|
|
/// Invalidate the currently active outbound group session for the given
|
|
|
|
/// room.
|
|
|
|
///
|
|
|
|
/// Returns true if a session was invalidated, false if there was no session
|
|
|
|
/// to invalidate.
|
2020-08-11 10:22:14 +00:00
|
|
|
pub fn invalidate_group_session(&self, room_id: &RoomId) -> bool {
|
2020-05-14 09:55:12 +00:00
|
|
|
self.outbound_group_sessions.remove(room_id).is_some()
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:48:00 +00:00
|
|
|
/// Get to-device requests to share a group session with users in a room.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `room_id` - The room id of the room where the group session will be
|
|
|
|
/// used.
|
|
|
|
///
|
|
|
|
/// `users` - The list of users that should receive the group session.
|
2020-08-16 14:25:48 +00:00
|
|
|
pub async fn share_group_session<S>(
|
2020-08-11 10:22:14 +00:00
|
|
|
&self,
|
2020-04-08 13:06:57 +00:00
|
|
|
room_id: &RoomId,
|
2020-08-16 14:25:48 +00:00
|
|
|
users: impl Iterator<Item = &UserId>,
|
2020-08-13 12:37:33 +00:00
|
|
|
encryption_settings: S,
|
2020-08-10 10:39:00 +00:00
|
|
|
) -> OlmResult<Vec<OwnedToDeviceRequest>>
|
2020-04-08 13:06:57 +00:00
|
|
|
where
|
2020-08-13 12:37:33 +00:00
|
|
|
S: Into<EncryptionSettings> + Sized,
|
2020-04-08 13:06:57 +00:00
|
|
|
{
|
2020-08-13 12:37:33 +00:00
|
|
|
self.create_outbound_group_session(room_id, encryption_settings.into())
|
|
|
|
.await?;
|
2020-07-21 09:12:20 +00:00
|
|
|
let session = self.outbound_group_sessions.get(room_id).unwrap();
|
2020-04-10 07:58:13 +00:00
|
|
|
|
2020-07-21 09:12:20 +00:00
|
|
|
if session.shared() {
|
2020-04-10 07:58:13 +00:00
|
|
|
panic!("Session is already shared");
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:38:20 +00:00
|
|
|
// TODO don't mark the session as shared automatically, only when all
|
2020-07-11 10:05:52 +00:00
|
|
|
// the requests are done, failure to send these requests will likely end
|
|
|
|
// up in wedged sessions. We'll need to store the requests and let the
|
|
|
|
// caller mark them as sent using an UUID.
|
2020-07-21 09:12:20 +00:00
|
|
|
session.mark_as_shared();
|
2020-07-11 10:05:52 +00:00
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
let mut devices = Vec::new();
|
2020-04-09 14:24:40 +00:00
|
|
|
|
2020-04-08 13:06:57 +00:00
|
|
|
for user_id in users {
|
2020-08-11 13:39:50 +00:00
|
|
|
for device in self.store.get_user_devices(user_id).await?.devices() {
|
2020-08-14 14:20:49 +00:00
|
|
|
if !device.is_blacklisted() {
|
|
|
|
devices.push(device.clone());
|
|
|
|
}
|
2020-04-09 14:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
let mut requests = Vec::new();
|
|
|
|
let key_content = session.as_json().await;
|
2020-04-09 14:24:40 +00:00
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
for device_map_chunk in devices.chunks(OlmMachine::MAX_TO_DEVICE_MESSAGES) {
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut messages = BTreeMap::new();
|
2020-04-09 14:24:40 +00:00
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
for device in device_map_chunk {
|
|
|
|
let encrypted = self
|
|
|
|
.olm_encrypt(&device, EventType::RoomKey, key_content.clone())
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let encrypted = match encrypted {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(OlmError::MissingSession)
|
|
|
|
| Err(OlmError::EventError(EventError::MissingSenderKey)) => {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
2020-04-09 14:24:40 +00:00
|
|
|
if !messages.contains_key(device.user_id()) {
|
2020-04-23 08:52:47 +00:00
|
|
|
messages.insert(device.user_id().clone(), BTreeMap::new());
|
2020-04-09 14:24:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let user_messages = messages.get_mut(device.user_id()).unwrap();
|
|
|
|
|
2020-05-05 13:29:25 +00:00
|
|
|
user_messages.insert(
|
2020-07-18 12:51:19 +00:00
|
|
|
DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
|
2020-07-21 12:13:10 +00:00
|
|
|
serde_json::value::to_raw_value(&encrypted)?,
|
2020-05-05 13:29:25 +00:00
|
|
|
);
|
2020-04-08 13:06:57 +00:00
|
|
|
}
|
2020-04-09 14:24:40 +00:00
|
|
|
|
2020-08-10 10:39:00 +00:00
|
|
|
requests.push(OwnedToDeviceRequest {
|
2020-04-22 13:38:42 +00:00
|
|
|
event_type: EventType::RoomEncrypted,
|
2020-04-10 07:57:10 +00:00
|
|
|
txn_id: Uuid::new_v4().to_string(),
|
2020-04-09 14:24:40 +00:00
|
|
|
messages,
|
|
|
|
});
|
2020-04-08 13:06:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 12:13:10 +00:00
|
|
|
Ok(requests)
|
2020-04-08 13:06:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 10:32:40 +00:00
|
|
|
fn add_forwarded_room_key(
|
|
|
|
&self,
|
2020-04-03 10:34:05 +00:00
|
|
|
_sender_key: &str,
|
2020-04-22 13:15:08 +00:00
|
|
|
_signing_key: &str,
|
2020-06-20 21:18:20 +00:00
|
|
|
_event: &ToDeviceEvent<ForwardedRoomKeyEventContent>,
|
2020-04-30 11:16:10 +00:00
|
|
|
) -> OlmResult<()> {
|
2020-03-25 10:32:40 +00:00
|
|
|
Ok(())
|
2020-03-24 15:21:06 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Receive and properly handle a decrypted to-device event.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `sender_key` - The sender (curve25519) key of the event sender.
|
|
|
|
///
|
|
|
|
/// * `signing_key` - The signing (ed25519) key of the event sender.
|
|
|
|
///
|
|
|
|
/// * `event` - The decrypted to-device event.
|
2020-03-30 15:07:36 +00:00
|
|
|
async fn handle_decrypted_to_device_event(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-03-24 16:25:01 +00:00
|
|
|
sender_key: &str,
|
2020-04-22 13:15:08 +00:00
|
|
|
signing_key: &str,
|
2020-07-26 14:58:27 +00:00
|
|
|
event: &Raw<AnyToDeviceEvent>,
|
|
|
|
) -> OlmResult<Option<Raw<AnyToDeviceEvent>>> {
|
2020-04-23 08:52:47 +00:00
|
|
|
let event = if let Ok(e) = event.deserialize() {
|
2020-03-24 15:21:06 +00:00
|
|
|
e
|
|
|
|
} else {
|
|
|
|
warn!("Decrypted to-device event failed to be parsed correctly");
|
2020-05-06 10:15:15 +00:00
|
|
|
return Ok(None);
|
2020-03-24 15:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyToDeviceEvent::RoomKey(mut e) => {
|
2020-05-06 10:15:15 +00:00
|
|
|
Ok(self.add_room_key(sender_key, signing_key, &mut e).await?)
|
2020-04-23 08:52:47 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyToDeviceEvent::ForwardedRoomKey(e) => {
|
2020-05-06 10:15:15 +00:00
|
|
|
self.add_forwarded_room_key(sender_key, signing_key, &e)?;
|
|
|
|
Ok(None)
|
2020-04-22 13:15:08 +00:00
|
|
|
}
|
2020-03-25 10:32:40 +00:00
|
|
|
_ => {
|
|
|
|
warn!("Received a unexpected encrypted to-device event");
|
2020-05-06 10:15:15 +00:00
|
|
|
Ok(None)
|
2020-03-25 10:32:40 +00:00
|
|
|
}
|
2020-03-24 15:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
fn handle_room_key_request(&self, _: &ToDeviceEvent<RoomKeyRequestEventContent>) {
|
2020-03-12 14:41:11 +00:00
|
|
|
// TODO handle room key requests here.
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:37:20 +00:00
|
|
|
async fn handle_verification_event(&self, mut event: &mut AnyToDeviceEvent) {
|
|
|
|
if let Err(e) = self.verification_machine.receive_event(&mut event).await {
|
|
|
|
error!("Error handling a verification event: {:?}", e);
|
|
|
|
}
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 14:24:45 +00:00
|
|
|
/// Get the to-device requests that need to be sent out.
|
2020-08-10 10:39:00 +00:00
|
|
|
pub fn outgoing_to_device_requests(&self) -> Vec<OwnedToDeviceRequest> {
|
2020-07-28 14:24:45 +00:00
|
|
|
self.verification_machine.outgoing_to_device_requests()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark an outgoing to-device requests as sent.
|
|
|
|
pub fn mark_to_device_request_as_sent(&self, request_id: &str) {
|
|
|
|
self.verification_machine.mark_requests_as_sent(request_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a `Sas` verification object with the given flow id.
|
|
|
|
pub fn get_verification(&self, flow_id: &str) -> Option<Sas> {
|
|
|
|
self.verification_machine.get_sas(flow_id)
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:32:40 +00:00
|
|
|
/// Handle a sync response and update the internal state of the Olm machine.
|
|
|
|
///
|
2020-04-30 15:10:12 +00:00
|
|
|
/// This will decrypt to-device events but will not touch events in the room
|
|
|
|
/// timeline.
|
|
|
|
///
|
2020-08-12 13:14:16 +00:00
|
|
|
/// To decrypt an event from the room timeline call [`decrypt_room_event`].
|
2020-03-25 10:32:40 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The sync latest sync response.
|
2020-08-12 13:14:16 +00:00
|
|
|
///
|
|
|
|
/// [`decrypt_room_event`]: #method.decrypt_room_event
|
2020-04-30 15:10:12 +00:00
|
|
|
#[instrument(skip(response))]
|
2020-08-10 12:11:55 +00:00
|
|
|
pub async fn receive_sync_response(&self, response: &mut SyncResponse) {
|
2020-08-03 14:18:35 +00:00
|
|
|
self.verification_machine.garbage_collect();
|
|
|
|
|
2020-03-12 15:14:43 +00:00
|
|
|
let one_time_key_count = response
|
|
|
|
.device_one_time_keys_count
|
2020-08-10 10:39:00 +00:00
|
|
|
.get(&DeviceKeyAlgorithm::SignedCurve25519);
|
2020-03-12 15:14:43 +00:00
|
|
|
|
|
|
|
let count: u64 = one_time_key_count.map_or(0, |c| (*c).into());
|
2020-04-28 13:47:49 +00:00
|
|
|
self.update_key_count(count);
|
2020-03-12 15:14:43 +00:00
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
for user_id in &response.device_lists.changed {
|
|
|
|
if let Err(e) = self.mark_user_as_changed(&user_id).await {
|
|
|
|
error!("Error marking a tracked user as changed {:?}", e);
|
2020-05-21 12:30:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 11:38:44 +00:00
|
|
|
for event_result in &mut response.to_device.events {
|
2020-07-28 13:37:20 +00:00
|
|
|
let mut event = if let Ok(e) = event_result.deserialize() {
|
2020-03-12 14:41:11 +00:00
|
|
|
e
|
|
|
|
} else {
|
|
|
|
// Skip invalid events.
|
2020-03-31 11:38:44 +00:00
|
|
|
warn!("Received an invalid to-device event {:?}", event_result);
|
2020-03-12 14:41:11 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2020-03-19 12:55:04 +00:00
|
|
|
info!("Received a to-device event {:?}", event);
|
|
|
|
|
2020-07-28 13:37:20 +00:00
|
|
|
match &mut event {
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyToDeviceEvent::RoomEncrypted(e) => {
|
2020-07-11 19:20:02 +00:00
|
|
|
let decrypted_event = match self.decrypt_to_device_event(e).await {
|
2020-03-24 15:21:06 +00:00
|
|
|
Ok(e) => e,
|
|
|
|
Err(err) => {
|
|
|
|
warn!(
|
|
|
|
"Failed to decrypt to-device event from {} {}",
|
|
|
|
e.sender, err
|
|
|
|
);
|
|
|
|
// TODO if the session is wedged mark it for
|
|
|
|
// unwedging.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2020-03-31 11:38:44 +00:00
|
|
|
|
|
|
|
*event_result = decrypted_event;
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
2020-07-11 19:20:02 +00:00
|
|
|
AnyToDeviceEvent::RoomKeyRequest(e) => self.handle_room_key_request(e),
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyToDeviceEvent::KeyVerificationAccept(..)
|
|
|
|
| AnyToDeviceEvent::KeyVerificationCancel(..)
|
|
|
|
| AnyToDeviceEvent::KeyVerificationKey(..)
|
|
|
|
| AnyToDeviceEvent::KeyVerificationMac(..)
|
|
|
|
| AnyToDeviceEvent::KeyVerificationRequest(..)
|
|
|
|
| AnyToDeviceEvent::KeyVerificationStart(..) => {
|
2020-07-28 13:37:20 +00:00
|
|
|
self.handle_verification_event(&mut event).await;
|
2020-06-20 21:18:20 +00:00
|
|
|
}
|
2020-03-12 14:41:11 +00:00
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 14:03:10 +00:00
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Decrypt an event from a room timeline.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The event that should be decrypted.
|
2020-07-13 12:00:42 +00:00
|
|
|
///
|
|
|
|
/// * `room_id` - The ID of the room where the event was sent to.
|
2020-03-25 14:03:10 +00:00
|
|
|
pub async fn decrypt_room_event(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-07-18 12:37:43 +00:00
|
|
|
event: &SyncMessageEvent<EncryptedEventContent>,
|
2020-06-20 21:18:20 +00:00
|
|
|
room_id: &RoomId,
|
2020-07-26 14:58:27 +00:00
|
|
|
) -> MegolmResult<Raw<AnySyncRoomEvent>> {
|
2020-03-25 14:03:10 +00:00
|
|
|
let content = match &event.content {
|
|
|
|
EncryptedEventContent::MegolmV1AesSha2(c) => c,
|
2020-04-30 12:07:49 +00:00
|
|
|
_ => return Err(EventError::UnsupportedAlgorithm.into()),
|
2020-03-25 14:03:10 +00:00
|
|
|
};
|
|
|
|
|
2020-03-30 15:07:36 +00:00
|
|
|
let session = self
|
|
|
|
.store
|
2020-06-20 21:18:20 +00:00
|
|
|
.get_inbound_group_session(room_id, &content.sender_key, &content.session_id)
|
2020-03-30 15:07:36 +00:00
|
|
|
.await?;
|
2020-04-30 15:10:12 +00:00
|
|
|
// TODO check if the Olm session is wedged and re-request the key.
|
2020-04-30 11:16:10 +00:00
|
|
|
let session = session.ok_or(MegolmError::MissingSession)?;
|
2020-03-25 14:03:10 +00:00
|
|
|
|
|
|
|
// TODO check the message index.
|
|
|
|
// TODO check if this is from a verified device.
|
2020-07-13 12:00:42 +00:00
|
|
|
let (decrypted_event, _) = session.decrypt(event).await?;
|
2020-03-25 14:03:10 +00:00
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
trace!("Successfully decrypted Megolm event {:?}", decrypted_event);
|
2020-03-25 14:03:10 +00:00
|
|
|
// TODO set the encryption info on the event (is it verified, was it
|
|
|
|
// decrypted, sender key...)
|
|
|
|
|
|
|
|
Ok(decrypted_event)
|
|
|
|
}
|
2020-04-01 13:37:00 +00:00
|
|
|
|
2020-05-14 15:26:51 +00:00
|
|
|
/// Mark that the given user has changed his devices.
|
|
|
|
///
|
|
|
|
/// This will queue up the given user for a key query.
|
|
|
|
///
|
|
|
|
/// Note: The user already needs to be tracked for it to be queued up for a
|
|
|
|
/// key query.
|
|
|
|
///
|
|
|
|
/// Returns true if the user was queued up for a key query, false otherwise.
|
2020-08-15 13:51:04 +00:00
|
|
|
async fn mark_user_as_changed(&self, user_id: &UserId) -> StoreResult<bool> {
|
2020-08-11 13:39:50 +00:00
|
|
|
if self.store.is_user_tracked(user_id) {
|
|
|
|
self.store.update_tracked_user(user_id, true).await?;
|
2020-05-15 13:33:30 +00:00
|
|
|
Ok(true)
|
2020-05-14 15:26:51 +00:00
|
|
|
} else {
|
2020-05-15 13:33:30 +00:00
|
|
|
Ok(false)
|
2020-05-14 15:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:37:00 +00:00
|
|
|
/// Update the tracked users.
|
|
|
|
///
|
2020-04-30 15:10:12 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `users` - An iterator over user ids that should be marked for
|
|
|
|
/// tracking.
|
|
|
|
///
|
2020-05-14 15:26:51 +00:00
|
|
|
/// This will mark users that weren't seen before for a key query and
|
|
|
|
/// tracking.
|
|
|
|
///
|
2020-04-01 13:37:00 +00:00
|
|
|
/// If the user is already known to the Olm machine it will not be
|
|
|
|
/// considered for a key query.
|
2020-08-10 12:11:55 +00:00
|
|
|
pub async fn update_tracked_users<'a, I>(&self, users: I)
|
2020-04-01 13:37:00 +00:00
|
|
|
where
|
2020-04-03 15:00:37 +00:00
|
|
|
I: IntoIterator<Item = &'a UserId>,
|
2020-04-01 13:37:00 +00:00
|
|
|
{
|
|
|
|
for user in users {
|
2020-08-11 13:39:50 +00:00
|
|
|
if self.store.is_user_tracked(user) {
|
2020-05-15 13:33:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-01 13:37:00 +00:00
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
if let Err(e) = self.store.update_tracked_user(user, true).await {
|
2020-05-15 13:33:30 +00:00
|
|
|
warn!("Error storing users for tracking {}", e);
|
2020-04-01 13:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:10:12 +00:00
|
|
|
/// Should the client perform a key query request.
|
2020-07-28 13:03:44 +00:00
|
|
|
pub async fn should_query_keys(&self) -> bool {
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.has_users_for_key_query()
|
2020-04-01 13:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the set of users that we need to query keys for.
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
|
|
|
/// Returns a hash set of users that need to be queried for keys.
|
2020-08-12 11:28:16 +00:00
|
|
|
///
|
|
|
|
/// The response of a successful key query requests needs to be passed to
|
|
|
|
/// the [`OlmMachine`] with the [`receive_keys_query_response`].
|
|
|
|
///
|
|
|
|
/// [`OlmMachine`]: struct.OlmMachine.html
|
|
|
|
/// [`receive_keys_query_response`]: #method.receive_keys_query_response
|
2020-07-28 13:03:44 +00:00
|
|
|
pub async fn users_for_key_query(&self) -> HashSet<UserId> {
|
2020-08-11 13:39:50 +00:00
|
|
|
self.store.users_for_key_query()
|
2020-04-01 13:37:00 +00:00
|
|
|
}
|
2020-08-12 10:48:22 +00:00
|
|
|
|
|
|
|
/// Get a specific device of a user.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The unique id of the user that the device belongs to.
|
|
|
|
///
|
|
|
|
/// * `device_id` - The unique id of the device.
|
|
|
|
///
|
|
|
|
/// Returns a `Device` if one is found and the crypto store didn't throw an
|
|
|
|
/// error.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// # use matrix_sdk_crypto::OlmMachine;
|
|
|
|
/// # use matrix_sdk_common::identifiers::UserId;
|
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # let alice = UserId::try_from("@alice:example.org").unwrap();
|
|
|
|
/// # let machine = OlmMachine::new(&alice, "DEVICEID".into());
|
|
|
|
/// # block_on(async {
|
|
|
|
/// let device = machine.get_device(&alice, "DEVICEID".into()).await;
|
|
|
|
///
|
|
|
|
/// println!("{:?}", device);
|
|
|
|
/// # });
|
|
|
|
/// ```
|
2020-08-17 13:34:05 +00:00
|
|
|
pub async fn get_device(&self, user_id: &UserId, device_id: &DeviceId) -> Option<DeviceWrap> {
|
|
|
|
let device = self
|
|
|
|
.store
|
2020-08-12 10:48:22 +00:00
|
|
|
.get_device(user_id, device_id)
|
|
|
|
.await
|
|
|
|
.ok()
|
2020-08-17 13:34:05 +00:00
|
|
|
.flatten()?;
|
|
|
|
|
|
|
|
Some(DeviceWrap {
|
|
|
|
inner: device,
|
|
|
|
verification_machine: self.verification_machine.clone(),
|
|
|
|
})
|
2020-08-12 10:48:22 +00:00
|
|
|
}
|
2020-08-12 15:16:27 +00:00
|
|
|
|
|
|
|
/// Get a map holding all the devices of an user.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The unique id of the user that the devices belong to.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// # use matrix_sdk_crypto::OlmMachine;
|
|
|
|
/// # use matrix_sdk_common::identifiers::UserId;
|
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # let alice = UserId::try_from("@alice:example.org").unwrap();
|
|
|
|
/// # let machine = OlmMachine::new(&alice, "DEVICEID".into());
|
|
|
|
/// # block_on(async {
|
|
|
|
/// let devices = machine.get_user_devices(&alice).await.unwrap();
|
|
|
|
///
|
|
|
|
/// for device in devices.devices() {
|
|
|
|
/// println!("{:?}", device);
|
|
|
|
/// }
|
|
|
|
/// # });
|
|
|
|
/// ```
|
2020-08-17 13:34:05 +00:00
|
|
|
pub async fn get_user_devices(&self, user_id: &UserId) -> StoreResult<UserDevicesWrap> {
|
|
|
|
let devices = self.store.get_user_devices(user_id).await?;
|
|
|
|
|
|
|
|
Ok(UserDevicesWrap {
|
|
|
|
inner: devices,
|
|
|
|
verification_machine: self.verification_machine.clone(),
|
|
|
|
})
|
2020-08-12 15:16:27 +00:00
|
|
|
}
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-08-14 12:10:29 +00:00
|
|
|
pub(crate) mod test {
|
2020-04-27 14:31:28 +00:00
|
|
|
static USER_ID: &str = "@bob:example.org";
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-07-11 19:06:21 +00:00
|
|
|
use matrix_sdk_common::js_int::uint;
|
2020-07-30 23:22:48 +00:00
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
time::SystemTime,
|
|
|
|
};
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-04-29 07:48:00 +00:00
|
|
|
use http::Response;
|
2020-03-10 12:02:14 +00:00
|
|
|
use serde_json::json;
|
2020-08-13 13:54:42 +00:00
|
|
|
use tempfile::tempdir;
|
2020-03-10 12:02:14 +00:00
|
|
|
|
2020-07-30 23:22:48 +00:00
|
|
|
use crate::{
|
|
|
|
machine::{OlmMachine, OneTimeKeys},
|
2020-08-13 14:46:11 +00:00
|
|
|
verification::test::request_to_event,
|
2020-08-17 14:17:28 +00:00
|
|
|
verify_json, EncryptionSettings, ReadOnlyDevice,
|
2020-04-29 07:48:00 +00:00
|
|
|
};
|
2020-07-30 23:22:48 +00:00
|
|
|
|
|
|
|
use matrix_sdk_common::{
|
2020-08-10 10:39:00 +00:00
|
|
|
api::r0::{keys, to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest},
|
2020-07-30 23:22:48 +00:00
|
|
|
events::{
|
|
|
|
room::{
|
|
|
|
encrypted::EncryptedEventContent,
|
|
|
|
message::{MessageEventContent, TextMessageEventContent},
|
|
|
|
},
|
|
|
|
AnySyncMessageEvent, AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent,
|
|
|
|
ToDeviceEvent, Unsigned,
|
2020-04-28 12:48:49 +00:00
|
|
|
},
|
2020-08-14 12:08:53 +00:00
|
|
|
identifiers::{
|
|
|
|
event_id, room_id, user_id, DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId,
|
|
|
|
},
|
2020-07-30 23:22:48 +00:00
|
|
|
Raw,
|
2020-04-27 16:27:33 +00:00
|
|
|
};
|
2020-06-22 19:40:51 +00:00
|
|
|
use matrix_sdk_test::test_json;
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-04-27 14:31:28 +00:00
|
|
|
fn alice_id() -> UserId {
|
2020-08-04 22:56:26 +00:00
|
|
|
user_id!("@alice:example.org")
|
2020-04-27 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 12:51:19 +00:00
|
|
|
fn alice_device_id() -> Box<DeviceId> {
|
|
|
|
"JLAFKJWSCS".into()
|
2020-04-27 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 12:02:14 +00:00
|
|
|
fn user_id() -> UserId {
|
|
|
|
UserId::try_from(USER_ID).unwrap()
|
|
|
|
}
|
|
|
|
|
2020-08-14 12:10:29 +00:00
|
|
|
pub fn response_from_file(json: &serde_json::Value) -> Response<Vec<u8>> {
|
2020-06-22 19:40:51 +00:00
|
|
|
Response::builder()
|
|
|
|
.status(200)
|
|
|
|
.body(json.to_string().as_bytes().to_vec())
|
|
|
|
.unwrap()
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn keys_upload_response() -> keys::upload_keys::Response {
|
2020-06-22 19:40:51 +00:00
|
|
|
let data = response_from_file(&test_json::KEYS_UPLOAD);
|
2020-02-25 13:24:18 +00:00
|
|
|
keys::upload_keys::Response::try_from(data).expect("Can't parse the keys upload response")
|
|
|
|
}
|
|
|
|
|
2020-04-21 07:45:46 +00:00
|
|
|
fn keys_query_response() -> keys::get_keys::Response {
|
2020-06-22 19:40:51 +00:00
|
|
|
let data = response_from_file(&test_json::KEYS_QUERY);
|
2020-04-21 07:45:46 +00:00
|
|
|
keys::get_keys::Response::try_from(data).expect("Can't parse the keys upload response")
|
|
|
|
}
|
|
|
|
|
2020-08-10 10:39:00 +00:00
|
|
|
fn to_device_requests_to_content(requests: Vec<OwnedToDeviceRequest>) -> EncryptedEventContent {
|
2020-04-28 12:48:49 +00:00
|
|
|
let to_device_request = &requests[0];
|
|
|
|
|
2020-07-26 14:58:27 +00:00
|
|
|
let content: Raw<EncryptedEventContent> = serde_json::from_str(
|
2020-04-28 12:48:49 +00:00
|
|
|
to_device_request
|
|
|
|
.messages
|
|
|
|
.values()
|
2020-04-30 12:29:58 +00:00
|
|
|
.next()
|
2020-04-28 12:48:49 +00:00
|
|
|
.unwrap()
|
|
|
|
.values()
|
2020-04-30 12:29:58 +00:00
|
|
|
.next()
|
2020-04-28 12:48:49 +00:00
|
|
|
.unwrap()
|
|
|
|
.get(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
content.deserialize().unwrap()
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:31:28 +00:00
|
|
|
async fn get_prepared_machine() -> (OlmMachine, OneTimeKeys) {
|
2020-08-10 12:15:47 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-07-13 13:49:16 +00:00
|
|
|
machine.account.update_uploaded_key_count(0);
|
2020-08-12 13:12:51 +00:00
|
|
|
let request = machine
|
2020-04-21 07:45:46 +00:00
|
|
|
.keys_for_upload()
|
|
|
|
.await
|
|
|
|
.expect("Can't prepare initial key upload");
|
|
|
|
let response = keys_upload_response();
|
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-08-12 13:12:51 +00:00
|
|
|
(machine, request.one_time_keys.unwrap())
|
2020-04-27 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_machine_after_query() -> (OlmMachine, OneTimeKeys) {
|
2020-08-10 12:15:47 +00:00
|
|
|
let (machine, otk) = get_prepared_machine().await;
|
2020-04-27 14:31:28 +00:00
|
|
|
let response = keys_query_response();
|
|
|
|
|
2020-04-21 07:45:46 +00:00
|
|
|
machine
|
2020-04-27 14:31:28 +00:00
|
|
|
.receive_keys_query_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(machine, otk)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_machine_pair() -> (OlmMachine, OlmMachine, OneTimeKeys) {
|
|
|
|
let (bob, otk) = get_prepared_machine().await;
|
|
|
|
|
|
|
|
let alice_id = alice_id();
|
|
|
|
let alice_device = alice_device_id();
|
2020-04-30 11:16:10 +00:00
|
|
|
let alice = OlmMachine::new(&alice_id, &alice_device);
|
2020-04-27 14:31:28 +00:00
|
|
|
|
2020-08-17 14:17:28 +00:00
|
|
|
let alice_deivce = ReadOnlyDevice::from_machine(&alice).await;
|
|
|
|
let bob_device = ReadOnlyDevice::from_machine(&bob).await;
|
2020-08-11 13:39:50 +00:00
|
|
|
alice.store.save_devices(&[bob_device]).await.unwrap();
|
|
|
|
bob.store.save_devices(&[alice_deivce]).await.unwrap();
|
2020-04-27 14:31:28 +00:00
|
|
|
|
|
|
|
(alice, bob, otk)
|
2020-04-21 07:45:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 16:27:33 +00:00
|
|
|
async fn get_machine_pair_with_session() -> (OlmMachine, OlmMachine) {
|
2020-08-10 12:15:47 +00:00
|
|
|
let (alice, bob, one_time_keys) = get_machine_pair().await;
|
2020-04-27 16:27:33 +00:00
|
|
|
|
|
|
|
let mut bob_keys = BTreeMap::new();
|
|
|
|
|
2020-05-06 13:00:16 +00:00
|
|
|
let one_time_key = one_time_keys.iter().next().unwrap();
|
2020-04-27 16:27:33 +00:00
|
|
|
let mut keys = BTreeMap::new();
|
|
|
|
keys.insert(one_time_key.0.clone(), one_time_key.1.clone());
|
|
|
|
bob_keys.insert(bob.device_id.clone(), keys);
|
|
|
|
|
|
|
|
let mut one_time_keys = BTreeMap::new();
|
|
|
|
one_time_keys.insert(bob.user_id.clone(), bob_keys);
|
|
|
|
|
|
|
|
let response = keys::claim_keys::Response {
|
|
|
|
failures: BTreeMap::new(),
|
|
|
|
one_time_keys,
|
|
|
|
};
|
|
|
|
|
|
|
|
alice.receive_keys_claim_response(&response).await.unwrap();
|
|
|
|
|
|
|
|
(alice, bob)
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:48:49 +00:00
|
|
|
async fn get_machine_pair_with_setup_sessions() -> (OlmMachine, OlmMachine) {
|
2020-08-10 12:15:47 +00:00
|
|
|
let (alice, bob) = get_machine_pair_with_session().await;
|
2020-04-28 12:48:49 +00:00
|
|
|
|
|
|
|
let bob_device = alice
|
|
|
|
.store
|
|
|
|
.get_device(&bob.user_id, &bob.device_id)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = ToDeviceEvent {
|
2020-04-28 12:48:49 +00:00
|
|
|
sender: alice.user_id.clone(),
|
|
|
|
content: alice
|
2020-07-21 12:13:10 +00:00
|
|
|
.olm_encrypt(&bob_device, EventType::Dummy, json!({}))
|
2020-04-28 12:48:49 +00:00
|
|
|
.await
|
|
|
|
.unwrap(),
|
|
|
|
};
|
|
|
|
|
|
|
|
bob.decrypt_to_device_event(&event).await.unwrap();
|
|
|
|
|
|
|
|
(alice, bob)
|
|
|
|
}
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn create_olm_machine() {
|
2020-07-10 13:43:32 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-03-18 14:50:32 +00:00
|
|
|
assert!(machine.should_upload_keys().await);
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:24:54 +00:00
|
|
|
#[tokio::test]
|
2020-02-25 13:24:18 +00:00
|
|
|
async fn receive_keys_upload_response() {
|
2020-08-10 12:15:47 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-02-25 13:24:18 +00:00
|
|
|
let mut response = keys_upload_response();
|
|
|
|
|
2020-02-25 13:36:09 +00:00
|
|
|
response
|
|
|
|
.one_time_key_counts
|
2020-08-10 10:39:00 +00:00
|
|
|
.remove(&DeviceKeyAlgorithm::SignedCurve25519)
|
2020-02-25 13:36:09 +00:00
|
|
|
.unwrap();
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
assert!(machine.should_upload_keys().await);
|
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(machine.should_upload_keys().await);
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-07-11 19:06:21 +00:00
|
|
|
response
|
|
|
|
.one_time_key_counts
|
2020-08-10 10:39:00 +00:00
|
|
|
.insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(10));
|
2020-03-18 14:50:32 +00:00
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(machine.should_upload_keys().await);
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-07-11 19:06:21 +00:00
|
|
|
response
|
|
|
|
.one_time_key_counts
|
2020-08-10 10:39:00 +00:00
|
|
|
.insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50));
|
2020-03-18 14:50:32 +00:00
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(!machine.should_upload_keys().await);
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:24:54 +00:00
|
|
|
#[tokio::test]
|
2020-02-25 13:24:18 +00:00
|
|
|
async fn generate_one_time_keys() {
|
2020-08-10 12:15:47 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-02-25 13:24:18 +00:00
|
|
|
|
|
|
|
let mut response = keys_upload_response();
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
assert!(machine.should_upload_keys().await);
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(machine.should_upload_keys().await);
|
2020-07-13 14:46:51 +00:00
|
|
|
assert!(machine.account.generate_one_time_keys().await.is_ok());
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-07-11 19:06:21 +00:00
|
|
|
response
|
|
|
|
.one_time_key_counts
|
2020-08-10 10:39:00 +00:00
|
|
|
.insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50));
|
2020-03-18 14:50:32 +00:00
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-07-13 14:46:51 +00:00
|
|
|
assert!(machine.account.generate_one_time_keys().await.is_err());
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_device_key_signing() {
|
2020-07-10 13:43:32 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-07-10 15:10:34 +00:00
|
|
|
let mut device_keys = machine.account.device_keys().await;
|
2020-04-10 13:28:43 +00:00
|
|
|
let identity_keys = machine.account.identity_keys();
|
2020-02-25 16:36:11 +00:00
|
|
|
let ed25519_key = identity_keys.ed25519();
|
|
|
|
|
2020-07-14 10:49:40 +00:00
|
|
|
let ret = verify_json(
|
2020-02-25 16:36:11 +00:00
|
|
|
&machine.user_id,
|
2020-08-14 12:08:53 +00:00
|
|
|
&DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, machine.device_id()),
|
2020-02-25 16:36:11 +00:00
|
|
|
ed25519_key,
|
2020-03-10 12:02:14 +00:00
|
|
|
&mut json!(&mut device_keys),
|
2020-02-25 16:36:11 +00:00
|
|
|
);
|
|
|
|
assert!(ret.is_ok());
|
|
|
|
}
|
2020-02-25 16:49:43 +00:00
|
|
|
|
2020-05-14 09:55:12 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn tests_session_invalidation() {
|
2020-08-11 10:22:14 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-08-04 22:56:26 +00:00
|
|
|
let room_id = room_id!("!test:example.org");
|
2020-05-14 09:55:12 +00:00
|
|
|
|
|
|
|
machine
|
2020-08-13 12:37:33 +00:00
|
|
|
.create_outnbound_group_session_with_defaults(&room_id)
|
2020-05-14 09:55:12 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(machine.outbound_group_sessions.get(&room_id).is_some());
|
|
|
|
|
|
|
|
machine.invalidate_group_session(&room_id);
|
|
|
|
|
|
|
|
assert!(machine.outbound_group_sessions.get(&room_id).is_none());
|
|
|
|
}
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_invalid_signature() {
|
2020-07-10 13:43:32 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-02-25 16:49:43 +00:00
|
|
|
|
2020-07-10 15:10:34 +00:00
|
|
|
let mut device_keys = machine.account.device_keys().await;
|
2020-02-25 16:49:43 +00:00
|
|
|
|
2020-07-14 10:49:40 +00:00
|
|
|
let ret = verify_json(
|
2020-02-25 16:49:43 +00:00
|
|
|
&machine.user_id,
|
2020-08-14 12:08:53 +00:00
|
|
|
&DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, machine.device_id()),
|
2020-02-25 16:49:43 +00:00
|
|
|
"fake_key",
|
2020-03-10 12:02:14 +00:00
|
|
|
&mut json!(&mut device_keys),
|
2020-02-25 16:49:43 +00:00
|
|
|
);
|
|
|
|
assert!(ret.is_err());
|
|
|
|
}
|
2020-02-29 10:13:57 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_one_time_key_signing() {
|
2020-07-13 13:49:16 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
|
|
|
machine.account.update_uploaded_key_count(49);
|
2020-02-29 10:13:57 +00:00
|
|
|
|
2020-07-13 14:46:51 +00:00
|
|
|
let mut one_time_keys = machine.account.signed_one_time_keys().await.unwrap();
|
2020-04-10 13:28:43 +00:00
|
|
|
let identity_keys = machine.account.identity_keys();
|
2020-02-29 10:13:57 +00:00
|
|
|
let ed25519_key = identity_keys.ed25519();
|
|
|
|
|
2020-05-06 13:00:16 +00:00
|
|
|
let mut one_time_key = one_time_keys.values_mut().next().unwrap();
|
2020-02-29 10:13:57 +00:00
|
|
|
|
2020-07-14 10:49:40 +00:00
|
|
|
let ret = verify_json(
|
2020-02-29 10:13:57 +00:00
|
|
|
&machine.user_id,
|
2020-08-14 12:08:53 +00:00
|
|
|
&DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, machine.device_id()),
|
2020-02-29 10:13:57 +00:00
|
|
|
ed25519_key,
|
2020-03-10 12:41:14 +00:00
|
|
|
&mut json!(&mut one_time_key),
|
2020-02-29 10:13:57 +00:00
|
|
|
);
|
|
|
|
assert!(ret.is_ok());
|
|
|
|
}
|
2020-03-10 13:06:30 +00:00
|
|
|
|
2020-03-16 12:24:54 +00:00
|
|
|
#[tokio::test]
|
2020-03-10 13:06:30 +00:00
|
|
|
async fn test_keys_for_upload() {
|
2020-08-10 12:15:47 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), &alice_device_id());
|
2020-07-13 13:49:16 +00:00
|
|
|
machine.account.update_uploaded_key_count(0);
|
2020-03-10 13:06:30 +00:00
|
|
|
|
2020-04-10 13:28:43 +00:00
|
|
|
let identity_keys = machine.account.identity_keys();
|
2020-03-10 13:06:30 +00:00
|
|
|
let ed25519_key = identity_keys.ed25519();
|
|
|
|
|
2020-08-12 13:12:51 +00:00
|
|
|
let mut request = machine
|
2020-03-10 13:06:30 +00:00
|
|
|
.keys_for_upload()
|
2020-03-18 14:50:32 +00:00
|
|
|
.await
|
2020-03-10 13:06:30 +00:00
|
|
|
.expect("Can't prepare initial key upload");
|
|
|
|
|
2020-07-14 10:49:40 +00:00
|
|
|
let ret = verify_json(
|
2020-03-10 13:06:30 +00:00
|
|
|
&machine.user_id,
|
2020-08-14 12:08:53 +00:00
|
|
|
&DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, machine.device_id()),
|
2020-03-10 13:06:30 +00:00
|
|
|
ed25519_key,
|
2020-08-12 13:12:51 +00:00
|
|
|
&mut json!(&mut request.one_time_keys.as_mut().unwrap().values_mut().next()),
|
2020-03-10 13:06:30 +00:00
|
|
|
);
|
|
|
|
assert!(ret.is_ok());
|
|
|
|
|
2020-07-14 10:49:40 +00:00
|
|
|
let ret = verify_json(
|
2020-03-10 13:06:30 +00:00
|
|
|
&machine.user_id,
|
2020-08-14 12:08:53 +00:00
|
|
|
&DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, machine.device_id()),
|
2020-03-10 13:06:30 +00:00
|
|
|
ed25519_key,
|
2020-08-12 13:12:51 +00:00
|
|
|
&mut json!(&mut request.device_keys.unwrap()),
|
2020-03-10 13:06:30 +00:00
|
|
|
);
|
|
|
|
assert!(ret.is_ok());
|
|
|
|
|
|
|
|
let mut response = keys_upload_response();
|
|
|
|
response.one_time_key_counts.insert(
|
2020-08-10 10:39:00 +00:00
|
|
|
DeviceKeyAlgorithm::SignedCurve25519,
|
2020-08-12 13:12:51 +00:00
|
|
|
(request.one_time_keys.unwrap().len() as u64)
|
|
|
|
.try_into()
|
|
|
|
.unwrap(),
|
2020-03-10 13:06:30 +00:00
|
|
|
);
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-03-10 13:06:30 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
let ret = machine.keys_for_upload().await;
|
2020-03-10 13:06:30 +00:00
|
|
|
assert!(ret.is_err());
|
|
|
|
}
|
2020-04-21 07:45:46 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_keys_query() {
|
2020-08-10 12:15:47 +00:00
|
|
|
let (machine, _) = get_prepared_machine().await;
|
2020-04-21 07:45:46 +00:00
|
|
|
let response = keys_query_response();
|
2020-08-04 22:56:26 +00:00
|
|
|
let alice_id = user_id!("@alice:example.org");
|
2020-07-22 18:43:47 +00:00
|
|
|
let alice_device_id: &DeviceId = "JLAFKJWSCS".into();
|
2020-04-21 07:45:46 +00:00
|
|
|
|
2020-08-11 13:39:50 +00:00
|
|
|
let alice_devices = machine.store.get_user_devices(&alice_id).await.unwrap();
|
2020-04-21 07:45:46 +00:00
|
|
|
assert!(alice_devices.devices().peekable().peek().is_none());
|
|
|
|
|
|
|
|
machine
|
|
|
|
.receive_keys_query_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let device = machine
|
|
|
|
.store
|
2020-07-22 18:43:47 +00:00
|
|
|
.get_device(&alice_id, alice_device_id)
|
2020-04-21 07:45:46 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(device.user_id(), &alice_id);
|
2020-07-22 18:43:47 +00:00
|
|
|
assert_eq!(device.device_id(), alice_device_id);
|
2020-04-21 07:45:46 +00:00
|
|
|
}
|
2020-04-27 14:31:28 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_missing_sessions_calculation() {
|
2020-08-10 12:15:47 +00:00
|
|
|
let (machine, _) = get_machine_after_query().await;
|
2020-04-27 14:31:28 +00:00
|
|
|
|
|
|
|
let alice = alice_id();
|
|
|
|
let alice_device = alice_device_id();
|
|
|
|
|
|
|
|
let missing_sessions = machine
|
|
|
|
.get_missing_sessions([alice.clone()].iter())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(missing_sessions.contains_key(&alice));
|
|
|
|
let user_sessions = missing_sessions.get(&alice).unwrap();
|
|
|
|
assert!(user_sessions.contains_key(&alice_device));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-04-27 16:27:33 +00:00
|
|
|
async fn test_session_creation() {
|
2020-08-10 12:15:47 +00:00
|
|
|
let (alice_machine, bob_machine, one_time_keys) = get_machine_pair().await;
|
2020-04-27 14:31:28 +00:00
|
|
|
|
|
|
|
let mut bob_keys = BTreeMap::new();
|
|
|
|
|
2020-05-06 13:00:16 +00:00
|
|
|
let one_time_key = one_time_keys.iter().next().unwrap();
|
2020-04-27 14:31:28 +00:00
|
|
|
let mut keys = BTreeMap::new();
|
|
|
|
keys.insert(one_time_key.0.clone(), one_time_key.1.clone());
|
|
|
|
bob_keys.insert(bob_machine.device_id.clone(), keys);
|
|
|
|
|
|
|
|
let mut one_time_keys = BTreeMap::new();
|
|
|
|
one_time_keys.insert(bob_machine.user_id.clone(), bob_keys);
|
|
|
|
|
|
|
|
let response = keys::claim_keys::Response {
|
|
|
|
failures: BTreeMap::new(),
|
|
|
|
one_time_keys,
|
|
|
|
};
|
|
|
|
|
|
|
|
alice_machine
|
|
|
|
.receive_keys_claim_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let session = alice_machine
|
|
|
|
.store
|
|
|
|
.get_sessions(bob_machine.account.identity_keys().curve25519())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(!session.lock().await.is_empty())
|
|
|
|
}
|
2020-04-27 16:27:33 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_olm_encryption() {
|
2020-08-10 12:15:47 +00:00
|
|
|
let (alice, bob) = get_machine_pair_with_session().await;
|
2020-04-27 16:27:33 +00:00
|
|
|
|
|
|
|
let bob_device = alice
|
|
|
|
.store
|
|
|
|
.get_device(&bob.user_id, &bob.device_id)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = ToDeviceEvent {
|
2020-04-27 16:27:33 +00:00
|
|
|
sender: alice.user_id.clone(),
|
|
|
|
content: alice
|
2020-07-21 12:13:10 +00:00
|
|
|
.olm_encrypt(&bob_device, EventType::Dummy, json!({}))
|
2020-04-27 16:27:33 +00:00
|
|
|
.await
|
|
|
|
.unwrap(),
|
|
|
|
};
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = bob
|
|
|
|
.decrypt_to_device_event(&event)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.deserialize()
|
|
|
|
.unwrap();
|
2020-04-27 16:27:33 +00:00
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
if let AnyToDeviceEvent::Dummy(e) = event {
|
2020-04-27 16:27:33 +00:00
|
|
|
assert_eq!(e.sender, alice.user_id);
|
|
|
|
} else {
|
2020-06-20 21:18:20 +00:00
|
|
|
panic!("Wrong event type found {:?}", event);
|
2020-04-27 16:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 08:47:08 +00:00
|
|
|
|
2020-05-05 13:29:25 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_room_key_sharing() {
|
2020-08-11 10:22:14 +00:00
|
|
|
let (alice, bob) = get_machine_pair_with_session().await;
|
2020-05-05 13:29:25 +00:00
|
|
|
|
2020-08-04 22:56:26 +00:00
|
|
|
let room_id = room_id!("!test:example.org");
|
2020-05-05 13:29:25 +00:00
|
|
|
|
|
|
|
let to_device_requests = alice
|
2020-08-13 12:37:33 +00:00
|
|
|
.share_group_session(
|
|
|
|
&room_id,
|
|
|
|
[bob.user_id.clone()].iter(),
|
|
|
|
EncryptionSettings::default(),
|
|
|
|
)
|
2020-05-05 13:29:25 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = ToDeviceEvent {
|
2020-05-05 13:29:25 +00:00
|
|
|
sender: alice.user_id.clone(),
|
|
|
|
content: to_device_requests_to_content(to_device_requests),
|
|
|
|
};
|
|
|
|
|
|
|
|
let alice_session = alice.outbound_group_sessions.get(&room_id).unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = bob
|
|
|
|
.decrypt_to_device_event(&event)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.deserialize()
|
|
|
|
.unwrap();
|
2020-05-05 13:29:25 +00:00
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
if let AnyToDeviceEvent::RoomKey(event) = event {
|
|
|
|
assert_eq!(event.sender, alice.user_id);
|
|
|
|
assert!(event.content.session_key.is_empty());
|
2020-05-05 13:29:25 +00:00
|
|
|
} else {
|
2020-06-20 21:18:20 +00:00
|
|
|
panic!("expected RoomKeyEvent found {:?}", event);
|
2020-05-05 13:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let session = bob
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
|
|
|
&room_id,
|
|
|
|
alice.account.identity_keys().curve25519(),
|
|
|
|
alice_session.session_id(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
assert!(session.unwrap().is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_megolm_encryption() {
|
2020-08-11 10:22:14 +00:00
|
|
|
let (alice, bob) = get_machine_pair_with_setup_sessions().await;
|
2020-08-04 22:56:26 +00:00
|
|
|
let room_id = room_id!("!test:example.org");
|
2020-05-05 13:29:25 +00:00
|
|
|
|
|
|
|
let to_device_requests = alice
|
2020-08-13 12:37:33 +00:00
|
|
|
.share_group_session(
|
|
|
|
&room_id,
|
|
|
|
[bob.user_id().clone()].iter(),
|
|
|
|
EncryptionSettings::default(),
|
|
|
|
)
|
2020-05-05 13:29:25 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = ToDeviceEvent {
|
|
|
|
sender: alice.user_id.clone(),
|
2020-05-05 13:29:25 +00:00
|
|
|
content: to_device_requests_to_content(to_device_requests),
|
|
|
|
};
|
|
|
|
|
|
|
|
bob.decrypt_to_device_event(&event).await.unwrap();
|
|
|
|
|
|
|
|
let plaintext = "It is a secret to everybody";
|
|
|
|
|
2020-07-25 00:23:10 +00:00
|
|
|
let content = MessageEventContent::Text(TextMessageEventContent::plain(plaintext));
|
2020-05-05 13:29:25 +00:00
|
|
|
|
|
|
|
let encrypted_content = alice.encrypt(&room_id, content.clone()).await.unwrap();
|
|
|
|
|
2020-07-18 12:37:43 +00:00
|
|
|
let event = SyncMessageEvent {
|
2020-08-04 22:56:26 +00:00
|
|
|
event_id: event_id!("$xxxxx:example.org"),
|
2020-05-05 13:29:25 +00:00
|
|
|
origin_server_ts: SystemTime::now(),
|
|
|
|
sender: alice.user_id().clone(),
|
|
|
|
content: encrypted_content,
|
2020-07-18 12:37:43 +00:00
|
|
|
unsigned: Unsigned::default(),
|
2020-05-05 13:29:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let decrypted_event = bob
|
2020-06-20 21:18:20 +00:00
|
|
|
.decrypt_room_event(&event, &room_id)
|
2020-05-05 13:29:25 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.deserialize()
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
match decrypted_event {
|
2020-07-18 12:37:43 +00:00
|
|
|
AnySyncRoomEvent::Message(AnySyncMessageEvent::RoomMessage(SyncMessageEvent {
|
2020-06-20 21:18:20 +00:00
|
|
|
sender,
|
|
|
|
content,
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
assert_eq!(&sender, alice.user_id());
|
|
|
|
if let MessageEventContent::Text(c) = &content {
|
|
|
|
assert_eq!(&c.body, plaintext);
|
|
|
|
} else {
|
|
|
|
panic!("Decrypted event has a missmatched content");
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 13:29:25 +00:00
|
|
|
_ => panic!("Decrypted room event has the wrong type"),
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 13:54:42 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_machine_with_default_store() {
|
|
|
|
let tmpdir = tempdir().unwrap();
|
|
|
|
|
|
|
|
let machine = OlmMachine::new_with_default_store(
|
|
|
|
&user_id(),
|
|
|
|
&alice_device_id(),
|
|
|
|
tmpdir.as_ref(),
|
|
|
|
"test",
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let user_id = machine.user_id().to_owned();
|
|
|
|
let device_id = machine.device_id().to_owned();
|
|
|
|
let ed25519_key = machine.identity_keys().ed25519().to_owned();
|
|
|
|
|
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&keys_upload_response())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
drop(machine);
|
|
|
|
|
|
|
|
let machine = OlmMachine::new_with_default_store(
|
|
|
|
&user_id,
|
|
|
|
&alice_device_id(),
|
|
|
|
tmpdir.as_ref(),
|
|
|
|
"test",
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(&user_id, machine.user_id());
|
|
|
|
assert_eq!(&*device_id, machine.device_id());
|
|
|
|
assert_eq!(ed25519_key, machine.identity_keys().ed25519());
|
|
|
|
}
|
2020-08-13 14:46:11 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn interactive_verification() {
|
|
|
|
let (alice, bob) = get_machine_pair_with_setup_sessions().await;
|
|
|
|
|
|
|
|
let bob_device = alice
|
|
|
|
.get_device(bob.user_id(), bob.device_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(!bob_device.is_trusted());
|
|
|
|
|
2020-08-17 13:34:05 +00:00
|
|
|
let (alice_sas, request) = bob_device.start_verification();
|
2020-08-13 14:46:11 +00:00
|
|
|
|
|
|
|
let mut event = request_to_event(alice.user_id(), &request);
|
|
|
|
bob.handle_verification_event(&mut event).await;
|
|
|
|
|
|
|
|
let bob_sas = bob.get_verification(alice_sas.flow_id()).unwrap();
|
|
|
|
|
|
|
|
assert!(alice_sas.emoji().is_none());
|
|
|
|
assert!(bob_sas.emoji().is_none());
|
|
|
|
|
|
|
|
let mut event = bob_sas
|
|
|
|
.accept()
|
|
|
|
.map(|r| request_to_event(bob.user_id(), &r))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
alice.handle_verification_event(&mut event).await;
|
|
|
|
|
|
|
|
let mut event = alice
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.iter()
|
|
|
|
.next()
|
|
|
|
.map(|r| request_to_event(alice.user_id(), &r))
|
|
|
|
.unwrap();
|
|
|
|
bob.handle_verification_event(&mut event).await;
|
|
|
|
|
|
|
|
let mut event = bob
|
|
|
|
.outgoing_to_device_requests()
|
|
|
|
.iter()
|
|
|
|
.next()
|
|
|
|
.map(|r| request_to_event(bob.user_id(), &r))
|
|
|
|
.unwrap();
|
|
|
|
alice.handle_verification_event(&mut event).await;
|
|
|
|
|
|
|
|
assert!(alice_sas.emoji().is_some());
|
|
|
|
assert!(bob_sas.emoji().is_some());
|
|
|
|
|
|
|
|
assert_eq!(alice_sas.emoji(), bob_sas.emoji());
|
|
|
|
assert_eq!(alice_sas.decimals(), bob_sas.decimals());
|
|
|
|
|
|
|
|
let mut event = bob_sas
|
|
|
|
.confirm()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.map(|r| request_to_event(bob.user_id(), &r))
|
|
|
|
.unwrap();
|
|
|
|
alice.handle_verification_event(&mut event).await;
|
|
|
|
|
|
|
|
assert!(!alice_sas.is_done());
|
|
|
|
assert!(!bob_sas.is_done());
|
|
|
|
|
|
|
|
let mut event = alice_sas
|
|
|
|
.confirm()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.map(|r| request_to_event(alice.user_id(), &r))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(alice_sas.is_done());
|
|
|
|
assert!(bob_device.is_trusted());
|
|
|
|
|
|
|
|
let alice_device = bob
|
|
|
|
.get_device(alice.user_id(), alice.device_id())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(!alice_device.is_trusted());
|
|
|
|
bob.handle_verification_event(&mut event).await;
|
|
|
|
assert!(bob_sas.is_done());
|
|
|
|
assert!(alice_device.is_trusted());
|
|
|
|
}
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|