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-10-08 12:41:34 +00:00
|
|
|
use std::{collections::BTreeMap, mem, sync::Arc};
|
2020-04-21 08:41:08 +00:00
|
|
|
|
2020-08-11 10:22:14 +00:00
|
|
|
use dashmap::DashMap;
|
2020-10-19 16:23:48 +00:00
|
|
|
use tracing::{debug, error, info, trace, warn};
|
2020-08-11 10:22:14 +00:00
|
|
|
|
2020-07-30 23:22:48 +00:00
|
|
|
use matrix_sdk_common::{
|
2020-08-21 07:50:01 +00:00
|
|
|
api::r0::{
|
2020-08-21 11:34:25 +00:00
|
|
|
keys::{
|
2020-08-21 12:40:49 +00:00
|
|
|
claim_keys::{Request as KeysClaimRequest, Response as KeysClaimResponse},
|
2020-09-12 01:19:22 +00:00
|
|
|
get_keys::Response as KeysQueryResponse,
|
2020-08-21 12:40:49 +00:00
|
|
|
upload_keys,
|
2020-10-24 08:32:17 +00:00
|
|
|
upload_signatures::Request as UploadSignaturesRequest,
|
2020-08-21 11:34:25 +00:00
|
|
|
},
|
2020-08-21 07:50:01 +00:00
|
|
|
sync::sync_events::Response as SyncResponse,
|
|
|
|
},
|
2020-09-12 01:19:22 +00:00
|
|
|
assign,
|
2020-07-30 23:22:48 +00:00
|
|
|
events::{
|
2020-09-18 11:50:13 +00:00
|
|
|
room::encrypted::EncryptedEventContent, room_key::RoomKeyEventContent,
|
2020-09-30 13:43:25 +00:00
|
|
|
AnyMessageEventContent, AnySyncRoomEvent, AnyToDeviceEvent, SyncMessageEvent,
|
2020-09-29 15:53:11 +00:00
|
|
|
ToDeviceEvent,
|
2020-09-18 11:50:13 +00:00
|
|
|
},
|
|
|
|
identifiers::{
|
|
|
|
DeviceId, DeviceIdBox, DeviceKeyAlgorithm, EventEncryptionAlgorithm, RoomId, UserId,
|
2020-08-15 01:09:13 +00:00
|
|
|
},
|
2020-09-24 09:16:15 +00:00
|
|
|
js_int::UInt,
|
2020-11-26 12:24:57 +00:00
|
|
|
locks::Mutex,
|
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-09-30 13:43:25 +00:00
|
|
|
use crate::store::sqlite::SqliteStore;
|
|
|
|
use crate::{
|
2020-10-09 13:39:35 +00:00
|
|
|
error::{EventError, MegolmError, MegolmResult, OlmError, OlmResult},
|
2020-10-20 15:19:37 +00:00
|
|
|
identities::{Device, IdentityManager, UserDevices},
|
2020-09-18 11:50:13 +00:00
|
|
|
key_request::KeyRequestMachine,
|
2020-08-02 12:05:43 +00:00
|
|
|
olm::{
|
2020-09-10 13:54:41 +00:00
|
|
|
Account, EncryptionSettings, ExportedRoomKey, GroupSessionKey, IdentityKeys,
|
2020-12-01 11:41:11 +00:00
|
|
|
InboundGroupSession, OlmDecryptionInfo, PrivateCrossSigningIdentity, ReadOnlyAccount,
|
2020-08-02 12:05:43 +00:00
|
|
|
},
|
2020-10-19 14:03:01 +00:00
|
|
|
requests::{IncomingResponse, OutgoingRequest, UploadSigningKeysRequest},
|
2020-10-16 09:09:55 +00:00
|
|
|
session_manager::{GroupSessionManager, SessionManager},
|
2020-10-20 15:19:37 +00:00
|
|
|
store::{
|
|
|
|
Changes, CryptoStore, DeviceChanges, IdentityChanges, MemoryStore, Result as StoreResult,
|
|
|
|
Store,
|
|
|
|
},
|
2020-08-04 09:23:24 +00:00
|
|
|
verification::{Sas, VerificationMachine},
|
2020-09-30 13:43:25 +00:00
|
|
|
ToDeviceRequest,
|
2020-08-02 12:05:43 +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-09-18 11:50:13 +00:00
|
|
|
user_id: Arc<UserId>,
|
2020-02-25 13:24:18 +00:00
|
|
|
/// The unique device id of the device that holds this account.
|
2020-09-18 11:50:13 +00:00
|
|
|
device_id: Arc<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-10-19 14:03:01 +00:00
|
|
|
/// The private part of our cross signing identity.
|
|
|
|
/// Used to sign devices and other users, might be missing if some other
|
|
|
|
/// device bootstraped cross signing or cross signing isn't bootstrapped at
|
|
|
|
/// all.
|
|
|
|
user_identity: Arc<Mutex<PrivateCrossSigningIdentity>>,
|
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-09-16 14:03:19 +00:00
|
|
|
store: Store,
|
2020-10-08 12:41:34 +00:00
|
|
|
/// A state machine that handles Olm sessions creation.
|
|
|
|
session_manager: SessionManager,
|
2020-09-30 13:43:25 +00:00
|
|
|
/// A state machine that keeps track of our outbound group sessions.
|
|
|
|
group_session_manager: GroupSessionManager,
|
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-09-18 11:50:13 +00:00
|
|
|
/// The state machine that is responsible to handle outgoing and incoming
|
|
|
|
/// key requests.
|
|
|
|
key_request_machine: KeyRequestMachine,
|
2020-09-18 18:39:48 +00:00
|
|
|
/// State machine handling public user identities and devices, keeping track
|
|
|
|
/// of when a key query needs to be done and handling one.
|
|
|
|
identity_manager: IdentityManager,
|
2020-10-24 08:32:17 +00:00
|
|
|
cross_signing_request: Arc<Mutex<Option<UploadSignaturesRequest>>>,
|
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-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-09-18 18:39:48 +00:00
|
|
|
let device_id: DeviceIdBox = device_id.into();
|
2020-09-29 10:03:41 +00:00
|
|
|
let account = ReadOnlyAccount::new(&user_id, &device_id);
|
2020-09-18 18:39:48 +00:00
|
|
|
|
2020-10-19 14:03:01 +00:00
|
|
|
OlmMachine::new_helper(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
store,
|
|
|
|
account,
|
|
|
|
PrivateCrossSigningIdentity::empty(user_id.to_owned()),
|
|
|
|
)
|
2020-09-18 18:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_helper(
|
|
|
|
user_id: &UserId,
|
|
|
|
device_id: DeviceIdBox,
|
|
|
|
store: Box<dyn CryptoStore>,
|
2020-09-29 10:03:41 +00:00
|
|
|
account: ReadOnlyAccount,
|
2020-10-19 14:03:01 +00:00
|
|
|
user_identity: PrivateCrossSigningIdentity,
|
2020-09-18 18:39:48 +00:00
|
|
|
) -> Self {
|
2020-09-18 11:50:13 +00:00
|
|
|
let user_id = Arc::new(user_id.clone());
|
2020-10-22 16:04:29 +00:00
|
|
|
let user_identity = Arc::new(Mutex::new(user_identity));
|
2020-09-28 07:27:16 +00:00
|
|
|
|
2020-10-01 09:17:27 +00:00
|
|
|
let store = Arc::new(store);
|
2020-10-22 16:04:29 +00:00
|
|
|
let verification_machine =
|
|
|
|
VerificationMachine::new(account.clone(), user_identity.clone(), store.clone());
|
2020-10-30 10:34:55 +00:00
|
|
|
let store = Store::new(
|
|
|
|
user_id.clone(),
|
|
|
|
user_identity.clone(),
|
|
|
|
store,
|
|
|
|
verification_machine.clone(),
|
|
|
|
);
|
2020-09-18 18:39:48 +00:00
|
|
|
let device_id: Arc<DeviceIdBox> = Arc::new(device_id);
|
2020-09-29 08:09:47 +00:00
|
|
|
let outbound_group_sessions = Arc::new(DashMap::new());
|
2020-10-09 09:36:31 +00:00
|
|
|
let users_for_key_claim = Arc::new(DashMap::new());
|
|
|
|
|
2020-09-29 08:09:47 +00:00
|
|
|
let key_request_machine = KeyRequestMachine::new(
|
|
|
|
user_id.clone(),
|
|
|
|
device_id.clone(),
|
|
|
|
store.clone(),
|
2020-10-01 07:56:22 +00:00
|
|
|
outbound_group_sessions,
|
2020-10-09 09:36:31 +00:00
|
|
|
users_for_key_claim.clone(),
|
2020-09-29 08:09:47 +00:00
|
|
|
);
|
2020-10-08 09:16:02 +00:00
|
|
|
|
2020-09-30 13:43:25 +00:00
|
|
|
let account = Account {
|
|
|
|
inner: account,
|
|
|
|
store: store.clone(),
|
|
|
|
};
|
|
|
|
|
2020-10-09 09:36:31 +00:00
|
|
|
let session_manager = SessionManager::new(
|
|
|
|
account.clone(),
|
|
|
|
users_for_key_claim,
|
|
|
|
key_request_machine.clone(),
|
|
|
|
store.clone(),
|
|
|
|
);
|
2020-09-30 13:43:25 +00:00
|
|
|
let group_session_manager = GroupSessionManager::new(account.clone(), store.clone());
|
2020-10-08 09:16:02 +00:00
|
|
|
let identity_manager = IdentityManager::new(
|
|
|
|
user_id.clone(),
|
|
|
|
device_id.clone(),
|
|
|
|
store.clone(),
|
|
|
|
group_session_manager.clone(),
|
|
|
|
);
|
2020-07-28 13:37:20 +00:00
|
|
|
|
2020-04-30 11:16:10 +00:00
|
|
|
OlmMachine {
|
2020-09-18 18:39:48 +00:00
|
|
|
user_id,
|
|
|
|
device_id,
|
2020-09-30 13:43:25 +00:00
|
|
|
account,
|
2020-10-24 08:32:17 +00:00
|
|
|
user_identity,
|
2020-09-18 18:39:48 +00:00
|
|
|
store,
|
2020-10-08 12:41:34 +00:00
|
|
|
session_manager,
|
2020-09-30 13:43:25 +00:00
|
|
|
group_session_manager,
|
2020-09-18 18:39:48 +00:00
|
|
|
verification_machine,
|
|
|
|
key_request_machine,
|
|
|
|
identity_manager,
|
2020-10-24 08:32:17 +00:00
|
|
|
cross_signing_request: Arc::new(Mutex::new(None)),
|
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-09-18 18:39:48 +00:00
|
|
|
device_id: DeviceIdBox,
|
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-10-23 09:17:37 +00:00
|
|
|
let account = ReadOnlyAccount::new(&user_id, &device_id);
|
|
|
|
store.save_account(account.clone()).await?;
|
|
|
|
account
|
2020-03-19 12:55:04 +00:00
|
|
|
}
|
2020-03-18 15:05:59 +00:00
|
|
|
};
|
|
|
|
|
2020-10-22 16:04:29 +00:00
|
|
|
let identity = match store.load_identity().await? {
|
|
|
|
Some(i) => {
|
|
|
|
debug!("Restored the cross signing identity");
|
|
|
|
i
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
debug!("Creating an empty cross signing identity stub");
|
|
|
|
PrivateCrossSigningIdentity::empty(user_id.clone())
|
|
|
|
}
|
|
|
|
};
|
2020-10-19 14:03:01 +00:00
|
|
|
|
|
|
|
Ok(OlmMachine::new_helper(
|
|
|
|
&user_id, device_id, store, account, identity,
|
|
|
|
))
|
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")]
|
|
|
|
#[cfg_attr(feature = "docs", doc(cfg(r#sqlite_cryptostore)))]
|
2020-08-20 08:36:58 +00:00
|
|
|
pub async fn new_with_default_store(
|
2020-04-30 15:10:12 +00:00
|
|
|
user_id: &UserId,
|
2020-07-21 08:48:15 +00:00
|
|
|
device_id: &DeviceId,
|
2020-08-20 08:36:58 +00:00
|
|
|
path: impl AsRef<Path>,
|
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-21 11:34:25 +00:00
|
|
|
/// Get the outgoing requests that need to be sent out.
|
2020-08-24 12:49:57 +00:00
|
|
|
///
|
|
|
|
/// This returns a list of `OutGoingRequest`, those requests need to be sent
|
|
|
|
/// out to the server and the responses need to be passed back to the state
|
|
|
|
/// machine using [`mark_request_as_sent`].
|
|
|
|
///
|
|
|
|
/// [`mark_request_as_sent`]: #method.mark_request_as_sent
|
2020-08-21 11:34:25 +00:00
|
|
|
pub async fn outgoing_requests(&self) -> Vec<OutgoingRequest> {
|
|
|
|
let mut requests = Vec::new();
|
|
|
|
|
|
|
|
if let Some(r) = self.keys_for_upload().await.map(|r| OutgoingRequest {
|
|
|
|
request_id: Uuid::new_v4(),
|
2020-08-21 14:26:34 +00:00
|
|
|
request: Arc::new(r.into()),
|
2020-08-21 11:34:25 +00:00
|
|
|
}) {
|
|
|
|
requests.push(r);
|
|
|
|
}
|
|
|
|
|
2020-09-18 18:39:48 +00:00
|
|
|
if let Some(r) =
|
|
|
|
self.identity_manager
|
|
|
|
.users_for_key_query()
|
|
|
|
.await
|
|
|
|
.map(|r| OutgoingRequest {
|
|
|
|
request_id: Uuid::new_v4(),
|
|
|
|
request: Arc::new(r.into()),
|
|
|
|
})
|
|
|
|
{
|
2020-08-21 11:34:25 +00:00
|
|
|
requests.push(r);
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:26:34 +00:00
|
|
|
requests.append(&mut self.outgoing_to_device_requests());
|
2020-09-18 11:50:13 +00:00
|
|
|
requests.append(&mut self.key_request_machine.outgoing_to_device_requests());
|
2020-08-21 14:26:34 +00:00
|
|
|
|
2020-08-21 11:34:25 +00:00
|
|
|
requests
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark the request with the given request id as sent.
|
2020-08-24 12:49:57 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `request_id` - The unique id of the request that was sent out. This is
|
|
|
|
/// needed to couple the response with the now sent out request.
|
|
|
|
///
|
|
|
|
/// * `response` - The response that was received from the server after the
|
|
|
|
/// outgoing request was sent out.
|
|
|
|
pub async fn mark_request_as_sent<'a>(
|
2020-08-21 11:34:25 +00:00
|
|
|
&self,
|
|
|
|
request_id: &Uuid,
|
|
|
|
response: impl Into<IncomingResponse<'a>>,
|
|
|
|
) -> OlmResult<()> {
|
|
|
|
match response.into() {
|
2020-08-21 12:40:49 +00:00
|
|
|
IncomingResponse::KeysUpload(response) => {
|
|
|
|
self.receive_keys_upload_response(response).await?;
|
|
|
|
}
|
2020-08-21 11:34:25 +00:00
|
|
|
IncomingResponse::KeysQuery(response) => {
|
|
|
|
self.receive_keys_query_response(response).await?;
|
|
|
|
}
|
2020-08-21 12:40:49 +00:00
|
|
|
IncomingResponse::KeysClaim(response) => {
|
|
|
|
self.receive_keys_claim_response(response).await?;
|
2020-08-21 11:34:25 +00:00
|
|
|
}
|
|
|
|
IncomingResponse::ToDevice(_) => {
|
2020-09-18 11:50:13 +00:00
|
|
|
self.mark_to_device_request_as_sent(&request_id).await?;
|
2020-08-21 11:34:25 +00:00
|
|
|
}
|
2020-10-19 14:03:01 +00:00
|
|
|
IncomingResponse::SigningKeysUpload(_) => {
|
|
|
|
self.receive_cross_signing_upload_response().await?;
|
|
|
|
}
|
2020-10-27 15:39:23 +00:00
|
|
|
IncomingResponse::SignatureUpload(_) => {
|
|
|
|
self.verification_machine.mark_request_as_sent(request_id);
|
|
|
|
}
|
2020-08-21 11:34:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:03:01 +00:00
|
|
|
/// Mark the cross signing identity as shared.
|
|
|
|
async fn receive_cross_signing_upload_response(&self) -> StoreResult<()> {
|
|
|
|
self.user_identity.lock().await.mark_as_shared();
|
2020-10-22 16:04:29 +00:00
|
|
|
self.store
|
|
|
|
.save_identity((&*self.user_identity.lock().await).clone())
|
|
|
|
.await
|
2020-10-19 14:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new cross signing identity and get the upload request to push
|
|
|
|
/// the new public keys to the server.
|
|
|
|
///
|
|
|
|
/// **Warning**: This will delete any existing cross signing keys that might
|
|
|
|
/// exist on the server and thus will reset the trust between all the
|
|
|
|
/// devices.
|
|
|
|
///
|
|
|
|
/// Uploading these keys will require user interactive auth.
|
2020-10-24 08:32:17 +00:00
|
|
|
pub async fn bootstrap_cross_signing(
|
|
|
|
&self,
|
|
|
|
reset: bool,
|
|
|
|
) -> StoreResult<(UploadSigningKeysRequest, UploadSignaturesRequest)> {
|
2020-10-22 16:04:29 +00:00
|
|
|
let mut identity = self.user_identity.lock().await;
|
2020-10-24 08:32:17 +00:00
|
|
|
|
|
|
|
if identity.is_empty().await || reset {
|
|
|
|
info!("Creating new cross signing identity");
|
2020-10-26 14:27:42 +00:00
|
|
|
let (id, request, signature_request) = self.account.bootstrap_cross_signing().await;
|
2020-10-24 08:32:17 +00:00
|
|
|
|
|
|
|
*identity = id;
|
|
|
|
|
2020-10-30 12:21:14 +00:00
|
|
|
let public = identity.as_public_identity().await.expect(
|
|
|
|
"Couldn't create a public version of the identity from a new private identity",
|
|
|
|
);
|
|
|
|
|
|
|
|
let changes = Changes {
|
|
|
|
identities: IdentityChanges {
|
|
|
|
new: vec![public.into()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
self.store.save_changes(changes).await?;
|
2020-10-24 08:32:17 +00:00
|
|
|
self.store.save_identity(identity.clone()).await?;
|
|
|
|
Ok((request, signature_request))
|
|
|
|
} else {
|
|
|
|
info!("Trying to upload the existing cross signing identity");
|
|
|
|
let request = identity.as_upload_request().await;
|
|
|
|
// TODO remove this expect.
|
|
|
|
let signature_request = identity
|
2020-10-27 12:48:51 +00:00
|
|
|
.sign_account(&self.account)
|
2020-10-24 08:32:17 +00:00
|
|
|
.await
|
|
|
|
.expect("Can't sign device keys");
|
|
|
|
Ok((request, signature_request))
|
|
|
|
}
|
2020-10-19 14:03:01 +00:00
|
|
|
}
|
|
|
|
|
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-08-21 11:34:25 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
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)]
|
2020-09-29 10:03:41 +00:00
|
|
|
pub(crate) fn account(&self) -> &ReadOnlyAccount {
|
2020-08-11 13:49:04 +00:00
|
|
|
&self.account
|
|
|
|
}
|
|
|
|
|
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-08-21 11:34:25 +00:00
|
|
|
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-09-29 10:03:41 +00:00
|
|
|
self.account.receive_keys_upload_response(response).await
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 12:40:49 +00:00
|
|
|
/// Get the a key claiming request for the user/device pairs that we are
|
|
|
|
/// missing Olm sessions for.
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
2020-08-21 12:40:49 +00:00
|
|
|
/// Returns None if no key claiming request needs to be sent out.
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
|
|
|
/// Sessions need to be established between devices so group sessions for a
|
|
|
|
/// room can be shared with them.
|
|
|
|
///
|
2020-10-07 12:02:50 +00:00
|
|
|
/// This should be called every time a group session needs to be shared as
|
|
|
|
/// well as between sync calls. After a sync some devices may request room
|
|
|
|
/// keys without us having a valid Olm session with them, making it
|
|
|
|
/// impossible to server the room key request, thus it's necessary to check
|
|
|
|
/// for missing sessions between sync as well.
|
|
|
|
///
|
|
|
|
/// **Note**: Care should be taken that only one such request at a time is
|
|
|
|
/// in flight, e.g. using a lock.
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
|
|
|
/// The response of a successful key claiming requests needs to be passed to
|
2020-08-24 12:49:57 +00:00
|
|
|
/// the `OlmMachine` with the [`mark_request_as_sent`].
|
2020-04-30 15:10:12 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `users` - The list of users that we should check if we lack a session
|
2020-10-07 12:02:50 +00:00
|
|
|
/// with one of their devices. This can be an empty iterator when calling
|
|
|
|
/// this method between sync requests.
|
2020-08-12 11:11:51 +00:00
|
|
|
///
|
2020-08-24 12:49:57 +00:00
|
|
|
/// [`mark_request_as_sent`]: #method.mark_request_as_sent
|
2020-04-03 08:20:03 +00:00
|
|
|
pub async fn get_missing_sessions(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-10-07 12:07:47 +00:00
|
|
|
users: &mut impl Iterator<Item = &UserId>,
|
2020-08-21 12:40:49 +00:00
|
|
|
) -> OlmResult<Option<(Uuid, KeysClaimRequest)>> {
|
2020-10-08 12:41:34 +00:00
|
|
|
self.session_manager.get_missing_sessions(users).await
|
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-08-21 12:40:49 +00:00
|
|
|
async fn receive_keys_claim_response(&self, response: &KeysClaimResponse) -> OlmResult<()> {
|
2020-10-08 12:41:34 +00:00
|
|
|
self.session_manager
|
|
|
|
.receive_keys_claim_response(response)
|
|
|
|
.await
|
2020-04-02 14:07:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:08:57 +00:00
|
|
|
/// 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.
|
2020-08-21 11:34:25 +00:00
|
|
|
async fn receive_keys_query_response(
|
2020-08-10 12:11:55 +00:00
|
|
|
&self,
|
2020-08-21 11:34:25 +00:00
|
|
|
response: &KeysQueryResponse,
|
2020-10-20 15:19:37 +00:00
|
|
|
) -> OlmResult<(DeviceChanges, IdentityChanges)> {
|
2020-09-18 18:39:48 +00:00
|
|
|
self.identity_manager
|
|
|
|
.receive_keys_query_response(response)
|
|
|
|
.await
|
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
|
|
|
///
|
2020-08-21 07:50:01 +00:00
|
|
|
/// Returns None 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-21 11:34:25 +00:00
|
|
|
async fn keys_for_upload(&self) -> Option<upload_keys::Request> {
|
2020-08-12 13:12:51 +00:00
|
|
|
let (device_keys, one_time_keys) = self.account.keys_for_upload().await?;
|
2020-09-12 01:19:22 +00:00
|
|
|
Some(assign!(upload_keys::Request::new(), { device_keys, one_time_keys }))
|
2020-03-10 13:06:30 +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-12-01 11:41:11 +00:00
|
|
|
) -> OlmResult<OlmDecryptionInfo> {
|
|
|
|
let mut decrypted = self.account.decrypt_to_device_event(event).await?;
|
2020-09-29 12:18:03 +00:00
|
|
|
// Handle the decrypted event, e.g. fetch out Megolm sessions out of
|
|
|
|
// the event.
|
2020-12-01 11:41:11 +00:00
|
|
|
if let (Some(event), group_session) =
|
|
|
|
self.handle_decrypted_to_device_event(&decrypted).await?
|
2020-09-29 12:18:03 +00:00
|
|
|
{
|
|
|
|
// Some events may have sensitive data e.g. private keys, while we
|
|
|
|
// want to notify our users that a private key was received we
|
|
|
|
// 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.
|
2020-12-01 11:41:11 +00:00
|
|
|
decrypted.event = event;
|
|
|
|
decrypted.inbound_group_session = group_session;
|
2020-03-23 15:14:10 +00:00
|
|
|
}
|
2020-12-01 11:41:11 +00:00
|
|
|
|
|
|
|
Ok(decrypted)
|
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-10-20 15:19:37 +00:00
|
|
|
) -> OlmResult<(Option<Raw<AnyToDeviceEvent>>, Option<InboundGroupSession>)> {
|
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-07-26 14:58:27 +00:00
|
|
|
let event = Raw::from(AnyToDeviceEvent::RoomKey(event.clone()));
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok((Some(event), Some(session)))
|
2020-03-25 10:32:40 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
warn!(
|
|
|
|
"Received room key with unsupported key algorithm {}",
|
|
|
|
event.content.algorithm
|
|
|
|
);
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok((None, None))
|
2020-03-24 16:25:01 +00:00
|
|
|
}
|
2020-03-24 15:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:37:33 +00:00
|
|
|
#[cfg(test)]
|
2020-09-18 16:55:17 +00:00
|
|
|
pub(crate) async fn create_outbound_group_session_with_defaults(
|
2020-08-13 12:37:33 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> OlmResult<()> {
|
2020-10-20 15:19:37 +00:00
|
|
|
let (_, session) = self
|
|
|
|
.group_session_manager
|
2020-10-01 14:31:24 +00:00
|
|
|
.create_outbound_group_session(room_id, EncryptionSettings::default())
|
2020-10-20 15:19:37 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
self.store.save_inbound_group_sessions(&[session]).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-13 12:37:33 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2020-08-26 13:41:27 +00:00
|
|
|
content: AnyMessageEventContent,
|
2020-04-30 11:16:10 +00:00
|
|
|
) -> MegolmResult<EncryptedEventContent> {
|
2020-09-30 13:43:25 +00:00
|
|
|
self.group_session_manager.encrypt(room_id, content).await
|
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-09-30 13:43:25 +00:00
|
|
|
self.group_session_manager
|
|
|
|
.should_share_group_session(room_id)
|
2020-04-10 09:46:31 +00:00
|
|
|
}
|
|
|
|
|
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-09-30 13:43:25 +00:00
|
|
|
self.group_session_manager.invalidate_group_session(room_id)
|
2020-05-14 09:55:12 +00:00
|
|
|
}
|
|
|
|
|
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-20 08:25:05 +00:00
|
|
|
pub async fn share_group_session(
|
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-20 08:25:05 +00:00
|
|
|
encryption_settings: impl Into<EncryptionSettings>,
|
2020-10-01 14:31:24 +00:00
|
|
|
) -> OlmResult<Vec<Arc<ToDeviceRequest>>> {
|
2020-09-30 13:43:25 +00:00
|
|
|
self.group_session_manager
|
|
|
|
.share_group_session(room_id, users, encryption_settings)
|
|
|
|
.await
|
2020-04-08 13:06:57 +00:00
|
|
|
}
|
|
|
|
|
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-12-01 11:41:11 +00:00
|
|
|
decrypted: &OlmDecryptionInfo,
|
2020-10-20 15:19:37 +00:00
|
|
|
) -> OlmResult<(Option<Raw<AnyToDeviceEvent>>, Option<InboundGroupSession>)> {
|
2020-12-01 11:41:11 +00:00
|
|
|
let event = match decrypted.event.deserialize() {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"Decrypted to-device event failed to be parsed correctly {:?}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
return Ok((None, None));
|
|
|
|
}
|
2020-03-24 15:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
2020-12-01 11:41:11 +00:00
|
|
|
AnyToDeviceEvent::RoomKey(mut e) => Ok(self
|
|
|
|
.add_room_key(&decrypted.sender_key, &decrypted.signing_key, &mut e)
|
|
|
|
.await?),
|
2020-09-24 09:18:01 +00:00
|
|
|
AnyToDeviceEvent::ForwardedRoomKey(mut e) => Ok(self
|
|
|
|
.key_request_machine
|
2020-12-01 11:41:11 +00:00
|
|
|
.receive_forwarded_room_key(&decrypted.sender_key, &mut e)
|
2020-09-24 09:18:01 +00:00
|
|
|
.await?),
|
2020-03-25 10:32:40 +00:00
|
|
|
_ => {
|
2020-12-01 11:41:11 +00:00
|
|
|
warn!("Received an unexpected encrypted to-device event");
|
2020-10-20 15:19:37 +00:00
|
|
|
Ok((None, None))
|
2020-03-25 10:32:40 +00:00
|
|
|
}
|
2020-03-24 15:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-21 14:26:34 +00:00
|
|
|
fn outgoing_to_device_requests(&self) -> Vec<OutgoingRequest> {
|
2020-07-28 14:24:45 +00:00
|
|
|
self.verification_machine.outgoing_to_device_requests()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark an outgoing to-device requests as sent.
|
2020-09-18 11:50:13 +00:00
|
|
|
async fn mark_to_device_request_as_sent(&self, request_id: &Uuid) -> StoreResult<()> {
|
2020-08-24 12:49:57 +00:00
|
|
|
self.verification_machine.mark_request_as_sent(request_id);
|
2020-09-18 11:50:13 +00:00
|
|
|
self.key_request_machine
|
|
|
|
.mark_outgoing_request_as_sent(request_id)
|
|
|
|
.await?;
|
2020-10-01 14:31:24 +00:00
|
|
|
self.group_session_manager.mark_request_as_sent(request_id);
|
2020-10-09 13:39:35 +00:00
|
|
|
self.session_manager
|
|
|
|
.mark_outgoing_request_as_sent(request_id);
|
2020-09-18 11:50:13 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-07-28 14:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-10-20 15:19:37 +00:00
|
|
|
async fn update_one_time_key_count(&self, key_count: &BTreeMap<DeviceKeyAlgorithm, UInt>) {
|
|
|
|
self.account.update_uploaded_key_count(key_count).await;
|
2020-09-24 09:16:15 +00:00
|
|
|
}
|
|
|
|
|
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-10-20 15:19:37 +00:00
|
|
|
pub async fn receive_sync_response(&self, response: &mut SyncResponse) -> OlmResult<()> {
|
|
|
|
// Remove verification objects that have expired or are done.
|
2020-08-03 14:18:35 +00:00
|
|
|
self.verification_machine.garbage_collect();
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
// Always save the account, a new session might get created which also
|
|
|
|
// touches the account.
|
|
|
|
let mut changes = Changes {
|
|
|
|
account: Some(self.account.inner.clone()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
self.update_one_time_key_count(&response.device_one_time_keys_count)
|
|
|
|
.await;
|
2020-09-18 18:41:14 +00:00
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
for user_id in &response.device_lists.changed {
|
2020-09-18 18:39:48 +00:00
|
|
|
if let Err(e) = self.identity_manager.mark_user_as_changed(&user_id).await {
|
2020-06-20 21:18:20 +00:00
|
|
|
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-12-01 11:41:11 +00:00
|
|
|
let decrypted = match self.decrypt_to_device_event(e).await {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(err) => {
|
|
|
|
warn!(
|
|
|
|
"Failed to decrypt to-device event from {} {}",
|
|
|
|
e.sender, err
|
|
|
|
);
|
|
|
|
|
|
|
|
if let OlmError::SessionWedged(sender, curve_key) = err {
|
|
|
|
if let Err(e) = self
|
|
|
|
.session_manager
|
|
|
|
.mark_device_as_wedged(&sender, &curve_key)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
error!(
|
|
|
|
"Couldn't mark device from {} to be unwedged {:?}",
|
|
|
|
sender, e
|
|
|
|
);
|
2020-10-09 13:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-01 11:41:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2020-10-20 15:19:37 +00:00
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
changes.sessions.push(decrypted.session);
|
2020-10-20 15:19:37 +00:00
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
if let Some(group_session) = decrypted.inbound_group_session {
|
2020-10-20 15:19:37 +00:00
|
|
|
changes.inbound_group_sessions.push(group_session);
|
|
|
|
}
|
2020-03-31 11:38:44 +00:00
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
*event_result = decrypted.event;
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
2020-09-29 15:40:06 +00:00
|
|
|
AnyToDeviceEvent::RoomKeyRequest(e) => {
|
|
|
|
self.key_request_machine.receive_incoming_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-09-29 15:40:06 +00:00
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
let changed_sessions = self
|
2020-10-07 09:57:46 +00:00
|
|
|
.key_request_machine
|
2020-09-29 15:40:06 +00:00
|
|
|
.collect_incoming_key_requests()
|
2020-10-20 15:19:37 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
changes.sessions.extend(changed_sessions);
|
|
|
|
|
|
|
|
Ok(self.store.save_changes(changes).await?)
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
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-09-18 11:50:13 +00:00
|
|
|
let session = if let Some(s) = session {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
self.key_request_machine
|
|
|
|
.create_outgoing_key_request(room_id, &content.sender_key, &content.session_id)
|
|
|
|
.await?;
|
|
|
|
return Err(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
|
|
|
|
|
|
|
/// 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-20 08:36:58 +00:00
|
|
|
pub async fn update_tracked_users(&self, users: impl IntoIterator<Item = &UserId>) {
|
2020-09-18 18:39:48 +00:00
|
|
|
self.identity_manager.update_tracked_users(users).await
|
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-09-28 07:27:16 +00:00
|
|
|
pub async fn get_device(
|
|
|
|
&self,
|
|
|
|
user_id: &UserId,
|
|
|
|
device_id: &DeviceId,
|
|
|
|
) -> StoreResult<Option<Device>> {
|
2020-10-01 10:15:13 +00:00
|
|
|
self.store.get_device(user_id, device_id).await
|
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 15:12:39 +00:00
|
|
|
pub async fn get_user_devices(&self, user_id: &UserId) -> StoreResult<UserDevices> {
|
2020-10-01 10:15:13 +00:00
|
|
|
self.store.get_user_devices(user_id).await
|
2020-08-12 15:16:27 +00:00
|
|
|
}
|
2020-09-10 13:54:41 +00:00
|
|
|
|
|
|
|
/// Import the given room keys into our store.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `exported_keys` - A list of previously exported keys that should be
|
|
|
|
/// imported into our store. If we already have a better version of a key
|
|
|
|
/// the key will *not* be imported.
|
|
|
|
///
|
|
|
|
/// Returns the number of sessions that were imported to the store.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```no_run
|
|
|
|
/// # use std::io::Cursor;
|
|
|
|
/// # use matrix_sdk_crypto::{OlmMachine, decrypt_key_export};
|
|
|
|
/// # use matrix_sdk_common::identifiers::user_id;
|
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # let alice = user_id!("@alice:example.org");
|
|
|
|
/// # let machine = OlmMachine::new(&alice, "DEVICEID".into());
|
|
|
|
/// # block_on(async {
|
|
|
|
/// # let export = Cursor::new("".to_owned());
|
|
|
|
/// let exported_keys = decrypt_key_export(export, "1234").unwrap();
|
|
|
|
/// machine.import_keys(exported_keys).await.unwrap();
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
pub async fn import_keys(&self, mut exported_keys: Vec<ExportedRoomKey>) -> StoreResult<usize> {
|
|
|
|
let mut sessions = Vec::new();
|
|
|
|
|
|
|
|
for key in exported_keys.drain(..) {
|
|
|
|
let session = InboundGroupSession::from_export(key)?;
|
|
|
|
|
|
|
|
// Only import the session if we didn't have this session or if it's
|
|
|
|
// a better version of the same session, that is the first known
|
|
|
|
// index is lower.
|
2020-10-17 12:39:19 +00:00
|
|
|
// TODO load all sessions so we don't do a thousand small loads.
|
2020-09-10 13:54:41 +00:00
|
|
|
if let Some(existing_session) = self
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
|
|
|
&session.room_id,
|
|
|
|
&session.sender_key,
|
|
|
|
session.session_id(),
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
{
|
2020-09-10 14:07:28 +00:00
|
|
|
let first_index = session.first_known_index().await;
|
|
|
|
let existing_index = existing_session.first_known_index().await;
|
|
|
|
|
|
|
|
if first_index < existing_index {
|
2020-09-10 13:54:41 +00:00
|
|
|
sessions.push(session)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sessions.push(session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let num_sessions = sessions.len();
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
let changes = Changes {
|
|
|
|
inbound_group_sessions: sessions,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
self.store.save_changes(changes).await?;
|
|
|
|
|
2020-10-17 12:39:19 +00:00
|
|
|
info!(
|
|
|
|
"Successfully imported {} inbound group sessions",
|
|
|
|
num_sessions
|
|
|
|
);
|
2020-09-10 13:54:41 +00:00
|
|
|
|
|
|
|
Ok(num_sessions)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Export the keys that match the given predicate.
|
|
|
|
///
|
2020-09-18 12:04:39 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `predicate` - A closure that will be called for every known
|
|
|
|
/// `InboundGroupSession`, which represents a room key. If the closure
|
|
|
|
/// returns `true` the `InboundGroupSessoin` will be included in the export,
|
|
|
|
/// if the closure returns `false` it will not be included.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This method will panic if it can't get enough randomness from the OS to
|
|
|
|
/// encrypt the exported keys securely.
|
2020-09-10 13:54:41 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use matrix_sdk_crypto::{OlmMachine, encrypt_key_export};
|
|
|
|
/// # use matrix_sdk_common::identifiers::{user_id, room_id};
|
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # let alice = user_id!("@alice:example.org");
|
|
|
|
/// # let machine = OlmMachine::new(&alice, "DEVICEID".into());
|
|
|
|
/// # block_on(async {
|
|
|
|
/// let room_id = room_id!("!test:localhost");
|
|
|
|
/// let exported_keys = machine.export_keys(|s| s.room_id() == &room_id).await.unwrap();
|
|
|
|
/// let encrypted_export = encrypt_key_export(&exported_keys, "1234", 1);
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
pub async fn export_keys(
|
|
|
|
&self,
|
|
|
|
mut predicate: impl FnMut(&InboundGroupSession) -> bool,
|
|
|
|
) -> StoreResult<Vec<ExportedRoomKey>> {
|
|
|
|
let mut exported = Vec::new();
|
|
|
|
|
|
|
|
let mut sessions: Vec<InboundGroupSession> = self
|
|
|
|
.store
|
|
|
|
.get_inbound_group_sessions()
|
|
|
|
.await?
|
|
|
|
.drain(..)
|
|
|
|
.filter(|s| predicate(&s))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
for session in sessions.drain(..) {
|
|
|
|
let export = session.export().await;
|
|
|
|
exported.push(export);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(exported)
|
|
|
|
}
|
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-30 23:22:48 +00:00
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
convert::{TryFrom, TryInto},
|
2020-10-01 14:31:24 +00:00
|
|
|
sync::Arc,
|
2020-07-30 23:22:48 +00:00
|
|
|
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-19 15:55:28 +00:00
|
|
|
#[cfg(feature = "sqlite_cryptostore")]
|
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::{
|
2020-08-21 14:26:34 +00:00
|
|
|
machine::OlmMachine,
|
2020-09-04 11:14:01 +00:00
|
|
|
olm::Utility,
|
2020-08-21 14:26:34 +00:00
|
|
|
verification::test::{outgoing_request_to_event, request_to_event},
|
2020-09-04 11:14:01 +00:00
|
|
|
EncryptionSettings, ReadOnlyDevice, ToDeviceRequest,
|
2020-04-29 07:48:00 +00:00
|
|
|
};
|
2020-07-30 23:22:48 +00:00
|
|
|
|
|
|
|
use matrix_sdk_common::{
|
2020-09-03 18:02:55 +00:00
|
|
|
api::r0::keys::{claim_keys, get_keys, upload_keys, OneTimeKey},
|
2020-07-30 23:22:48 +00:00
|
|
|
events::{
|
|
|
|
room::{
|
|
|
|
encrypted::EncryptedEventContent,
|
|
|
|
message::{MessageEventContent, TextMessageEventContent},
|
|
|
|
},
|
2020-08-26 13:41:27 +00:00
|
|
|
AnyMessageEventContent, 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-08-21 12:40:49 +00:00
|
|
|
/// These keys need to be periodically uploaded to the server.
|
|
|
|
type OneTimeKeys = BTreeMap<DeviceKeyId, OneTimeKey>;
|
|
|
|
|
|
|
|
use matrix_sdk_common::js_int::uint;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-21 12:40:49 +00:00
|
|
|
fn keys_upload_response() -> upload_keys::Response {
|
2020-06-22 19:40:51 +00:00
|
|
|
let data = response_from_file(&test_json::KEYS_UPLOAD);
|
2020-08-21 12:40:49 +00:00
|
|
|
upload_keys::Response::try_from(data).expect("Can't parse the keys upload response")
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 12:40:49 +00:00
|
|
|
fn keys_query_response() -> get_keys::Response {
|
2020-06-22 19:40:51 +00:00
|
|
|
let data = response_from_file(&test_json::KEYS_QUERY);
|
2020-08-21 12:40:49 +00:00
|
|
|
get_keys::Response::try_from(data).expect("Can't parse the keys upload response")
|
2020-04-21 07:45:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 14:31:24 +00:00
|
|
|
fn to_device_requests_to_content(requests: Vec<Arc<ToDeviceRequest>>) -> 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-09-10 13:54:41 +00:00
|
|
|
pub(crate) 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-09-29 10:03:41 +00:00
|
|
|
machine.account.inner.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());
|
2020-09-18 11:50:13 +00:00
|
|
|
bob_keys.insert(bob.device_id().into(), keys);
|
2020-04-27 16:27:33 +00:00
|
|
|
|
|
|
|
let mut one_time_keys = BTreeMap::new();
|
2020-09-18 11:50:13 +00:00
|
|
|
one_time_keys.insert(bob.user_id().clone(), bob_keys);
|
2020-04-27 16:27:33 +00:00
|
|
|
|
2020-09-12 01:19:22 +00:00
|
|
|
let response = claim_keys::Response::new(one_time_keys);
|
2020-04-27 16:27:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
.get_device(&bob.user_id, &bob.device_id)
|
|
|
|
.await
|
2020-09-28 07:27:16 +00:00
|
|
|
.unwrap()
|
2020-04-28 12:48:49 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2020-10-20 15:19:37 +00:00
|
|
|
let (session, content) = bob_device
|
|
|
|
.encrypt(EventType::Dummy, json!({}))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
alice.store.save_sessions(&[session]).await.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = ToDeviceEvent {
|
2020-09-18 11:50:13 +00:00
|
|
|
sender: alice.user_id().clone(),
|
2020-10-20 15:19:37 +00:00
|
|
|
content,
|
2020-04-28 12:48:49 +00:00
|
|
|
};
|
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
let decrypted = bob.decrypt_to_device_event(&event).await.unwrap();
|
|
|
|
bob.store.save_sessions(&[decrypted.session]).await.unwrap();
|
2020-04-28 12:48:49 +00:00
|
|
|
|
|
|
|
(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-09-04 11:14:01 +00:00
|
|
|
let utility = Utility::new();
|
|
|
|
let ret = utility.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-09-18 16:55:17 +00:00
|
|
|
.create_outbound_group_session_with_defaults(&room_id)
|
2020-05-14 09:55:12 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-30 13:43:25 +00:00
|
|
|
assert!(machine
|
|
|
|
.group_session_manager
|
|
|
|
.get_outbound_group_session(&room_id)
|
|
|
|
.is_some());
|
2020-05-14 09:55:12 +00:00
|
|
|
|
|
|
|
machine.invalidate_group_session(&room_id);
|
|
|
|
|
2020-09-30 13:43:25 +00:00
|
|
|
assert!(machine
|
|
|
|
.group_session_manager
|
|
|
|
.get_outbound_group_session(&room_id)
|
|
|
|
.is_none());
|
2020-05-14 09:55:12 +00:00
|
|
|
}
|
|
|
|
|
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-09-04 11:14:01 +00:00
|
|
|
let utility = Utility::new();
|
|
|
|
let ret = utility.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());
|
2020-09-29 10:03:41 +00:00
|
|
|
machine.account.inner.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-09-04 11:14:01 +00:00
|
|
|
let utility = Utility::new();
|
|
|
|
let ret = utility.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-09-29 10:03:41 +00:00
|
|
|
machine.account.inner.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-09-04 11:14:01 +00:00
|
|
|
let utility = Utility::new();
|
|
|
|
let ret = utility.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-09-04 11:14:01 +00:00
|
|
|
let utility = Utility::new();
|
|
|
|
let ret = utility.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-08-21 07:50:01 +00:00
|
|
|
assert!(ret.is_none());
|
2020-03-10 13:06:30 +00:00
|
|
|
}
|
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();
|
|
|
|
|
2020-08-21 12:40:49 +00:00
|
|
|
let (_, missing_sessions) = machine
|
2020-10-07 12:07:47 +00:00
|
|
|
.get_missing_sessions(&mut [alice.clone()].iter())
|
2020-04-27 14:31:28 +00:00
|
|
|
.await
|
2020-08-21 12:40:49 +00:00
|
|
|
.unwrap()
|
2020-04-27 14:31:28 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2020-08-21 12:40:49 +00:00
|
|
|
assert!(missing_sessions.one_time_keys.contains_key(&alice));
|
|
|
|
let user_sessions = missing_sessions.one_time_keys.get(&alice).unwrap();
|
2020-04-27 14:31:28 +00:00
|
|
|
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());
|
2020-09-18 11:50:13 +00:00
|
|
|
bob_keys.insert(bob_machine.device_id().into(), keys);
|
2020-04-27 14:31:28 +00:00
|
|
|
|
|
|
|
let mut one_time_keys = BTreeMap::new();
|
2020-09-18 11:50:13 +00:00
|
|
|
one_time_keys.insert(bob_machine.user_id().clone(), bob_keys);
|
2020-04-27 14:31:28 +00:00
|
|
|
|
2020-09-12 01:19:22 +00:00
|
|
|
let response = claim_keys::Response::new(one_time_keys);
|
2020-04-27 14:31:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
.get_device(&bob.user_id, &bob.device_id)
|
|
|
|
.await
|
2020-09-28 07:27:16 +00:00
|
|
|
.unwrap()
|
2020-04-27 16:27:33 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = ToDeviceEvent {
|
2020-09-18 11:50:13 +00:00
|
|
|
sender: alice.user_id().clone(),
|
2020-08-20 08:18:36 +00:00
|
|
|
content: bob_device
|
|
|
|
.encrypt(EventType::Dummy, json!({}))
|
2020-04-27 16:27:33 +00:00
|
|
|
.await
|
2020-10-20 15:19:37 +00:00
|
|
|
.unwrap()
|
|
|
|
.1,
|
2020-04-27 16:27:33 +00:00
|
|
|
};
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = bob
|
|
|
|
.decrypt_to_device_event(&event)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-12-01 11:41:11 +00:00
|
|
|
.event
|
2020-06-20 21:18:20 +00:00
|
|
|
.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-09-18 11:50:13 +00:00
|
|
|
assert_eq!(&e.sender, alice.user_id());
|
2020-04-27 16:27:33 +00:00
|
|
|
} 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,
|
2020-09-18 11:50:13 +00:00
|
|
|
[bob.user_id().clone()].iter(),
|
2020-08-13 12:37:33 +00:00
|
|
|
EncryptionSettings::default(),
|
|
|
|
)
|
2020-05-05 13:29:25 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
let event = ToDeviceEvent {
|
2020-09-18 11:50:13 +00:00
|
|
|
sender: alice.user_id().clone(),
|
2020-05-05 13:29:25 +00:00
|
|
|
content: to_device_requests_to_content(to_device_requests),
|
|
|
|
};
|
|
|
|
|
2020-09-30 13:43:25 +00:00
|
|
|
let alice_session = alice
|
|
|
|
.group_session_manager
|
|
|
|
.get_outbound_group_session(&room_id)
|
|
|
|
.unwrap();
|
2020-05-05 13:29:25 +00:00
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
let decrypted = bob.decrypt_to_device_event(&event).await.unwrap();
|
2020-10-20 15:19:37 +00:00
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
bob.store.save_sessions(&[decrypted.session]).await.unwrap();
|
2020-10-20 15:19:37 +00:00
|
|
|
bob.store
|
2020-12-01 11:41:11 +00:00
|
|
|
.save_inbound_group_sessions(&[decrypted.inbound_group_session.unwrap()])
|
2020-06-20 21:18:20 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-12-01 11:41:11 +00:00
|
|
|
let event = decrypted.event.deserialize().unwrap();
|
2020-05-05 13:29:25 +00:00
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
if let AnyToDeviceEvent::RoomKey(event) = event {
|
2020-09-18 11:50:13 +00:00
|
|
|
assert_eq!(&event.sender, alice.user_id());
|
2020-06-20 21:18:20 +00:00
|
|
|
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 {
|
2020-09-18 11:50:13 +00:00
|
|
|
sender: alice.user_id().clone(),
|
2020-05-05 13:29:25 +00:00
|
|
|
content: to_device_requests_to_content(to_device_requests),
|
|
|
|
};
|
|
|
|
|
2020-12-01 11:41:11 +00:00
|
|
|
let group_session = bob
|
|
|
|
.decrypt_to_device_event(&event)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.inbound_group_session;
|
2020-10-20 15:19:37 +00:00
|
|
|
bob.store
|
|
|
|
.save_inbound_group_sessions(&[group_session.unwrap()])
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-05-05 13:29:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-08-26 13:41:27 +00:00
|
|
|
let encrypted_content = alice
|
|
|
|
.encrypt(
|
|
|
|
&room_id,
|
|
|
|
AnyMessageEventContent::RoomMessage(content.clone()),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-05-05 13:29:25 +00:00
|
|
|
|
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
|
|
|
|
2020-10-16 13:05:53 +00:00
|
|
|
#[tokio::test(threaded_scheduler)]
|
2020-08-19 15:55:28 +00:00
|
|
|
#[cfg(feature = "sqlite_cryptostore")]
|
2020-08-13 13:54:42 +00:00
|
|
|
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
|
2020-09-28 07:27:16 +00:00
|
|
|
.unwrap()
|
2020-08-13 14:46:11 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(!bob_device.is_trusted());
|
|
|
|
|
2020-08-18 11:37:02 +00:00
|
|
|
let (alice_sas, request) = bob_device.start_verification().await.unwrap();
|
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()
|
2020-08-22 11:54:36 +00:00
|
|
|
.first()
|
2020-08-21 14:26:34 +00:00
|
|
|
.map(|r| outgoing_request_to_event(alice.user_id(), r))
|
2020-08-13 14:46:11 +00:00
|
|
|
.unwrap();
|
|
|
|
bob.handle_verification_event(&mut event).await;
|
|
|
|
|
|
|
|
let mut event = bob
|
|
|
|
.outgoing_to_device_requests()
|
2020-08-22 11:54:36 +00:00
|
|
|
.first()
|
2020-08-21 14:26:34 +00:00
|
|
|
.map(|r| outgoing_request_to_event(bob.user_id(), r))
|
2020-08-13 14:46:11 +00:00
|
|
|
.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()
|
2020-10-27 15:39:23 +00:00
|
|
|
.0
|
2020-08-13 14:46:11 +00:00
|
|
|
.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()
|
2020-10-27 15:39:23 +00:00
|
|
|
.0
|
2020-08-13 14:46:11 +00:00
|
|
|
.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
|
2020-09-28 07:27:16 +00:00
|
|
|
.unwrap()
|
2020-08-13 14:46:11 +00:00
|
|
|
.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
|
|
|
}
|