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-04-23 08:52:47 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap, HashSet};
|
2020-04-08 13:06:57 +00:00
|
|
|
use std::convert::TryInto;
|
2020-04-10 15:02:30 +00:00
|
|
|
use std::mem;
|
2020-03-21 15:41:48 +00:00
|
|
|
#[cfg(feature = "sqlite-cryptostore")]
|
2020-03-18 14:50:32 +00:00
|
|
|
use std::path::Path;
|
2020-03-18 13:15:56 +00:00
|
|
|
use std::result::Result as StdResult;
|
2020-04-10 07:57:10 +00:00
|
|
|
use uuid::Uuid;
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-03-21 15:41:48 +00:00
|
|
|
use super::error::{OlmError, Result, SignatureError, VerificationResult};
|
2020-04-21 08:41:08 +00:00
|
|
|
use super::olm::{
|
2020-04-28 13:05:20 +00:00
|
|
|
Account, GroupSessionKey, IdentityKeys, InboundGroupSession, OlmMessage, OlmUtility,
|
|
|
|
OutboundGroupSession, Session,
|
2020-04-21 08:41:08 +00:00
|
|
|
};
|
2020-03-30 15:07:36 +00:00
|
|
|
use super::store::memorystore::MemoryStore;
|
2020-03-18 14:50:32 +00:00
|
|
|
#[cfg(feature = "sqlite-cryptostore")]
|
|
|
|
use super::store::sqlite::SqliteStore;
|
2020-04-02 09:14:23 +00:00
|
|
|
use super::{device::Device, CryptoStore};
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-04-21 08:41:08 +00:00
|
|
|
use crate::api;
|
|
|
|
use crate::events::{
|
2020-04-09 14:22:25 +00:00
|
|
|
collections::all::RoomEvent,
|
|
|
|
room::encrypted::{
|
|
|
|
CiphertextInfo, EncryptedEvent, EncryptedEventContent, MegolmV1AesSha2Content,
|
|
|
|
OlmV1Curve25519AesSha2Content,
|
|
|
|
},
|
|
|
|
room::message::MessageEventContent,
|
2020-03-24 15:21:06 +00:00
|
|
|
to_device::{
|
|
|
|
AnyToDeviceEvent as ToDeviceEvent, ToDeviceEncrypted, ToDeviceForwardedRoomKey,
|
|
|
|
ToDeviceRoomKey, ToDeviceRoomKeyRequest,
|
|
|
|
},
|
2020-04-23 08:52:47 +00:00
|
|
|
Algorithm, EventJson, EventType,
|
2020-03-12 14:41:11 +00:00
|
|
|
};
|
2020-04-21 08:41:08 +00:00
|
|
|
use crate::identifiers::{DeviceId, RoomId, UserId};
|
|
|
|
|
|
|
|
use api::r0::keys;
|
|
|
|
use api::r0::{
|
|
|
|
client_exchange::{send_event_to_device::Request as ToDeviceRequest, DeviceIdOrAllDevices},
|
|
|
|
keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey, SignedKey},
|
2020-04-23 08:52:47 +00:00
|
|
|
sync::sync_events::Response as SyncResponse,
|
2020-04-21 08:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use cjson;
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use tracing::{debug, error, info, instrument, trace, warn};
|
2020-03-10 12:02:14 +00:00
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
pub type OneTimeKeys = BTreeMap<AlgorithmAndDeviceId, OneTimeKey>;
|
2020-03-10 12:41:14 +00:00
|
|
|
|
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-04-28 13:05:20 +00:00
|
|
|
device_id: DeviceId,
|
2020-02-25 13:24:18 +00:00
|
|
|
/// Our underlying Olm Account holding our identity keys.
|
2020-04-28 13:05:20 +00:00
|
|
|
account: Account,
|
2020-02-26 08:36:52 +00:00
|
|
|
/// The number of signed one-time keys we have uploaded to the server. If
|
|
|
|
/// this is None, no action will be taken. After a sync request the client
|
|
|
|
/// needs to set this for us, depending on the count we will suggest the
|
|
|
|
/// client to upload new keys.
|
|
|
|
uploaded_signed_key_count: Option<u64>,
|
2020-03-18 14:50:32 +00:00
|
|
|
/// Store for the encryption keys.
|
|
|
|
/// Persists all the encrytpion keys so a client can resume the session
|
|
|
|
/// without the need to create new keys.
|
2020-03-30 15:07:36 +00:00
|
|
|
store: Box<dyn CryptoStore>,
|
2020-04-01 13:37:00 +00:00
|
|
|
/// Set of users that we need to query keys for. This is a subset of
|
|
|
|
/// the tracked users in the CryptoStore.
|
2020-04-03 15:00:37 +00:00
|
|
|
users_for_key_query: HashSet<UserId>,
|
2020-04-08 13:06:57 +00:00
|
|
|
/// The currently active outbound group sessions.
|
2020-04-28 08:47:08 +00:00
|
|
|
outbound_group_sessions: HashMap<RoomId, OutboundGroupSession>,
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 09:58:21 +00:00
|
|
|
#[cfg_attr(tarpaulin, skip)]
|
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-03-10 12:02:14 +00:00
|
|
|
const ALGORITHMS: &'static [&'static ruma_events::Algorithm] = &[
|
|
|
|
&Algorithm::OlmV1Curve25519AesSha2,
|
|
|
|
&Algorithm::MegolmV1AesSha2,
|
2020-02-25 16:36:11 +00:00
|
|
|
];
|
|
|
|
|
2020-04-09 14:24:40 +00:00
|
|
|
const MAX_TO_DEVICE_MESSAGES: usize = 20;
|
|
|
|
|
2020-02-25 13:24:18 +00:00
|
|
|
/// Create a new account.
|
2020-03-18 13:15:56 +00:00
|
|
|
pub fn new(user_id: &UserId, device_id: &str) -> Result<Self> {
|
|
|
|
Ok(OlmMachine {
|
2020-03-11 09:04:04 +00:00
|
|
|
user_id: user_id.clone(),
|
2020-02-25 13:24:18 +00:00
|
|
|
device_id: device_id.to_owned(),
|
2020-04-10 13:28:43 +00:00
|
|
|
account: Account::new(),
|
2020-03-18 14:50:32 +00:00
|
|
|
uploaded_signed_key_count: None,
|
2020-03-30 15:07:36 +00:00
|
|
|
store: Box::new(MemoryStore::new()),
|
2020-04-01 13:37:00 +00:00
|
|
|
users_for_key_query: HashSet::new(),
|
2020-04-28 08:47:08 +00:00
|
|
|
outbound_group_sessions: HashMap::new(),
|
2020-03-18 14:50:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite-cryptostore")]
|
2020-03-19 12:55:04 +00:00
|
|
|
#[instrument(skip(path, passphrase))]
|
2020-03-18 14:50:32 +00:00
|
|
|
pub async fn new_with_sqlite_store<P: AsRef<Path>>(
|
|
|
|
user_id: &UserId,
|
|
|
|
device_id: &str,
|
|
|
|
path: P,
|
|
|
|
passphrase: String,
|
|
|
|
) -> Result<Self> {
|
2020-03-18 15:05:59 +00:00
|
|
|
let mut store =
|
2020-04-03 15:00:37 +00:00
|
|
|
SqliteStore::open_with_passphrase(&user_id, device_id, path, passphrase).await?;
|
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");
|
|
|
|
Account::new()
|
|
|
|
}
|
2020-03-18 15:05:59 +00:00
|
|
|
};
|
|
|
|
|
2020-04-01 13:37:00 +00:00
|
|
|
// TODO load the tracked users here.
|
2020-03-18 14:50:32 +00:00
|
|
|
Ok(OlmMachine {
|
|
|
|
user_id: user_id.clone(),
|
|
|
|
device_id: device_id.to_owned(),
|
2020-04-10 13:28:43 +00:00
|
|
|
account,
|
2020-02-26 08:36:52 +00:00
|
|
|
uploaded_signed_key_count: None,
|
2020-03-30 15:07:36 +00:00
|
|
|
store: Box::new(store),
|
2020-04-01 13:37:00 +00:00
|
|
|
users_for_key_query: HashSet::new(),
|
2020-04-28 08:47:08 +00:00
|
|
|
outbound_group_sessions: HashMap::new(),
|
2020-03-18 13:15:56 +00:00
|
|
|
})
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 13:05:20 +00:00
|
|
|
/// The unique user id that owns this identity.
|
|
|
|
pub(crate) fn user_id(&self) -> &UserId {
|
|
|
|
&self.user_id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The unique device id of the device that holds this identity.
|
|
|
|
pub(crate) fn device_id(&self) -> &DeviceId {
|
|
|
|
&self.device_id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the public parts of the identity keys.
|
|
|
|
pub(crate) fn identity_keys(&self) -> &IdentityKeys {
|
|
|
|
self.account.identity_keys()
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:24:18 +00:00
|
|
|
/// Should account or one-time keys be uploaded to the server.
|
2020-03-18 14:50:32 +00:00
|
|
|
pub async fn should_upload_keys(&self) -> bool {
|
2020-04-10 13:28:43 +00:00
|
|
|
if !self.account.shared() {
|
2020-02-25 13:24:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a known key count, check that we have more than
|
|
|
|
// max_one_time_Keys() / 2, otherwise tell the client to upload more.
|
2020-02-26 08:36:52 +00:00
|
|
|
match self.uploaded_signed_key_count {
|
2020-02-25 13:24:18 +00:00
|
|
|
Some(count) => {
|
2020-04-10 13:28:43 +00:00
|
|
|
let max_keys = self.account.max_one_time_keys().await as u64;
|
2020-02-25 13:24:18 +00:00
|
|
|
let key_count = (max_keys / 2) - count;
|
|
|
|
key_count > 0
|
|
|
|
}
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
&mut self,
|
|
|
|
response: &keys::upload_keys::Response,
|
|
|
|
) -> Result<()> {
|
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
|
|
|
|
.get(&keys::KeyAlgorithm::SignedCurve25519);
|
|
|
|
|
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",
|
|
|
|
self.uploaded_signed_key_count.as_ref().map_or(0, |c| *c),
|
|
|
|
count
|
|
|
|
);
|
2020-03-11 09:09:00 +00:00
|
|
|
self.uploaded_signed_key_count = Some(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-04-10 14:17:31 +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-03 08:20:03 +00:00
|
|
|
pub async fn get_missing_sessions(
|
|
|
|
&mut self,
|
2020-04-03 15:00:37 +00:00
|
|
|
users: impl Iterator<Item = &UserId>,
|
2020-04-27 14:26:03 +00:00
|
|
|
) -> Result<BTreeMap<UserId, BTreeMap<DeviceId, KeyAlgorithm>>> {
|
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-04-27 14:26:03 +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-04-17 12:14:31 +00:00
|
|
|
let sender_key = if let Some(k) = device.get_key(&KeyAlgorithm::Curve25519) {
|
2020-04-03 08:20:03 +00:00
|
|
|
k
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2020-04-27 14:26:03 +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-23 08:52:47 +00:00
|
|
|
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-04-03 08:20:03 +00:00
|
|
|
user_map.insert(
|
|
|
|
device.device_id().to_owned(),
|
|
|
|
KeyAlgorithm::SignedCurve25519,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:26:03 +00:00
|
|
|
Ok(missing)
|
2020-04-03 08:20:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 14:07:21 +00:00
|
|
|
pub async fn receive_keys_claim_response(
|
|
|
|
&mut self,
|
|
|
|
response: &keys::claim_keys::Response,
|
|
|
|
) -> Result<()> {
|
|
|
|
// TODO log the failures here
|
|
|
|
|
|
|
|
for (user_id, user_devices) in &response.one_time_keys {
|
|
|
|
for (device_id, key_map) in user_devices {
|
|
|
|
let device = if let Some(d) = self
|
|
|
|
.store
|
2020-04-03 15:00:37 +00:00
|
|
|
.get_device(&user_id, device_id)
|
2020-04-02 14:07:21 +00:00
|
|
|
.await
|
|
|
|
.expect("Can't get devices")
|
|
|
|
{
|
|
|
|
d
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Tried to create an Olm session for {} {}, but the device is unknown",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2020-04-03 08:16:20 +00:00
|
|
|
let one_time_key = if let Some(k) = key_map.values().nth(0) {
|
2020-04-02 14:07:21 +00:00
|
|
|
match k {
|
|
|
|
OneTimeKey::SignedKey(k) => k,
|
|
|
|
OneTimeKey::Key(_) => {
|
|
|
|
warn!(
|
|
|
|
"Tried to create an Olm session for {} {}, but
|
|
|
|
the requested key isn't a signed curve key",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Tried to create an Olm session for {} {}, but the
|
|
|
|
signed one-time key is missing",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2020-04-17 12:14:31 +00:00
|
|
|
let signing_key = if let Some(k) = device.get_key(&KeyAlgorithm::Ed25519) {
|
2020-04-02 14:07:21 +00:00
|
|
|
k
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Tried to create an Olm session for {} {}, but the
|
|
|
|
device is missing the signing key",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if self
|
|
|
|
.verify_json(user_id, device_id, signing_key, &mut json!(&one_time_key))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
warn!(
|
|
|
|
"Failed to verify the one-time key signatures for {} {}",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-17 12:14:31 +00:00
|
|
|
let curve_key = if let Some(k) = device.get_key(&KeyAlgorithm::Curve25519) {
|
2020-04-02 14:07:21 +00:00
|
|
|
k
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Tried to create an Olm session for {} {}, but the
|
|
|
|
device is missing the curve key",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("Creating outbound Session for {} {}", user_id, device_id);
|
|
|
|
|
|
|
|
let session = match self
|
|
|
|
.account
|
2020-04-03 08:16:20 +00:00
|
|
|
.create_outbound_session(curve_key, &one_time_key)
|
2020-04-10 13:28:43 +00:00
|
|
|
.await
|
2020-04-02 14:07:21 +00:00
|
|
|
{
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(
|
|
|
|
"Error creating new Olm session for {} {}: {}",
|
|
|
|
user_id, device_id, e
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-16 08:14:23 +00:00
|
|
|
if let Err(e) = self.store.save_session(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-04-01 13:37:00 +00:00
|
|
|
/// Receive a successful keys query response.
|
|
|
|
///
|
2020-04-21 07:44:44 +00:00
|
|
|
/// Returns a list of devices newly discovered devices and devices that
|
|
|
|
/// changed.
|
|
|
|
///
|
2020-04-01 13:37:00 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The keys query response of the request that the client
|
|
|
|
/// performed.
|
|
|
|
pub async fn receive_keys_query_response(
|
|
|
|
&mut self,
|
|
|
|
response: &keys::get_keys::Response,
|
2020-04-21 07:44:44 +00:00
|
|
|
) -> Result<Vec<Device>> {
|
2020-04-03 08:21:14 +00:00
|
|
|
let mut changed_devices = Vec::new();
|
|
|
|
|
2020-04-02 09:14:23 +00:00
|
|
|
for (user_id, device_map) in &response.device_keys {
|
2020-04-03 15:00:37 +00:00
|
|
|
self.users_for_key_query.remove(&user_id);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ed_key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, device_id.to_owned());
|
|
|
|
|
|
|
|
let signing_key = if let Some(k) = device_keys.keys.get(&ed_key_id) {
|
|
|
|
k
|
|
|
|
} else {
|
2020-04-21 07:44:44 +00:00
|
|
|
warn!(
|
|
|
|
"Ed25519 identity key wasn't found for user/device {} {}",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
2020-04-02 09:14:23 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if self
|
|
|
|
.verify_json(user_id, device_id, signing_key, &mut json!(&device_keys))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
warn!(
|
|
|
|
"Failed to verify the device key signatures for {} {}",
|
|
|
|
user_id, device_id
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-21 07:44:44 +00:00
|
|
|
let device = self.store.get_device(&user_id, device_id).await?;
|
2020-04-02 09:14:23 +00:00
|
|
|
|
2020-04-21 07:44:44 +00:00
|
|
|
let device = if let Some(mut d) = device {
|
|
|
|
let stored_signing_key = d.get_key(&KeyAlgorithm::Ed25519);
|
|
|
|
|
|
|
|
if let Some(stored_signing_key) = stored_signing_key {
|
|
|
|
if stored_signing_key != signing_key {
|
|
|
|
warn!("Ed25519 key has changed for {} {}", user_id, device_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.update_device(device_keys);
|
|
|
|
|
|
|
|
d
|
2020-04-02 09:14:23 +00:00
|
|
|
} else {
|
|
|
|
let device = Device::from(device_keys);
|
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-04-09 14:22:25 +00:00
|
|
|
let current_devices: HashSet<&DeviceId> = device_map.keys().collect();
|
2020-04-03 15:00:37 +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-04-22 10:12:47 +00:00
|
|
|
// TODO change this to a bulk deletion.
|
|
|
|
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-04-22 10:12:47 +00:00
|
|
|
// TODO change this to a bulk operation.
|
2020-04-21 07:44:44 +00:00
|
|
|
for device in &changed_devices {
|
|
|
|
self.store.save_device(device.clone()).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-02-25 13:24:18 +00:00
|
|
|
/// Generate new one-time keys.
|
|
|
|
///
|
|
|
|
/// Returns the number of newly generated one-time keys. If no keys can be
|
|
|
|
/// generated returns an empty error.
|
2020-03-18 14:50:32 +00:00
|
|
|
async fn generate_one_time_keys(&self) -> StdResult<u64, ()> {
|
2020-02-26 08:36:52 +00:00
|
|
|
match self.uploaded_signed_key_count {
|
2020-02-25 13:24:18 +00:00
|
|
|
Some(count) => {
|
2020-04-10 13:28:43 +00:00
|
|
|
let max_keys = self.account.max_one_time_keys().await as u64;
|
2020-02-26 08:18:10 +00:00
|
|
|
let max_on_server = max_keys / 2;
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-02-26 08:18:10 +00:00
|
|
|
if count >= (max_on_server) {
|
2020-02-25 13:36:09 +00:00
|
|
|
return Err(());
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 08:18:10 +00:00
|
|
|
let key_count = (max_on_server) - count;
|
|
|
|
|
2020-04-10 13:28:43 +00:00
|
|
|
let max_keys = self.account.max_one_time_keys().await;
|
|
|
|
|
|
|
|
let key_count: usize = key_count.try_into().unwrap_or(max_keys);
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-04-10 13:28:43 +00:00
|
|
|
self.account.generate_one_time_keys(key_count).await;
|
2020-02-25 13:24:18 +00:00
|
|
|
Ok(key_count as u64)
|
2020-02-25 13:36:09 +00:00
|
|
|
}
|
|
|
|
None => Err(()),
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-25 13:36:09 +00:00
|
|
|
|
2020-02-25 16:36:11 +00:00
|
|
|
/// Sign the device keys and return a JSON Value to upload them.
|
2020-03-18 14:50:32 +00:00
|
|
|
async fn device_keys(&self) -> DeviceKeys {
|
2020-04-10 13:28:43 +00:00
|
|
|
let identity_keys = self.account.identity_keys();
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut keys = BTreeMap::new();
|
2020-03-10 12:02:14 +00:00
|
|
|
|
|
|
|
keys.insert(
|
|
|
|
AlgorithmAndDeviceId(KeyAlgorithm::Curve25519, self.device_id.clone()),
|
|
|
|
identity_keys.curve25519().to_owned(),
|
|
|
|
);
|
|
|
|
keys.insert(
|
|
|
|
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, self.device_id.clone()),
|
|
|
|
identity_keys.ed25519().to_owned(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let device_keys = json!({
|
2020-02-25 16:36:11 +00:00
|
|
|
"user_id": self.user_id,
|
|
|
|
"device_id": self.device_id,
|
|
|
|
"algorithms": OlmMachine::ALGORITHMS,
|
2020-03-10 12:02:14 +00:00
|
|
|
"keys": keys,
|
2020-02-25 16:36:11 +00:00
|
|
|
});
|
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut signatures = BTreeMap::new();
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut signature = BTreeMap::new();
|
2020-03-10 12:02:14 +00:00
|
|
|
signature.insert(
|
|
|
|
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, self.device_id.clone()),
|
2020-03-18 14:50:32 +00:00
|
|
|
self.sign_json(&device_keys).await,
|
2020-03-10 12:02:14 +00:00
|
|
|
);
|
|
|
|
signatures.insert(self.user_id.clone(), signature);
|
|
|
|
|
|
|
|
DeviceKeys {
|
|
|
|
user_id: self.user_id.clone(),
|
|
|
|
device_id: self.device_id.clone(),
|
|
|
|
algorithms: vec![
|
|
|
|
Algorithm::OlmV1Curve25519AesSha2,
|
|
|
|
Algorithm::MegolmV1AesSha2,
|
|
|
|
],
|
|
|
|
keys,
|
|
|
|
signatures,
|
|
|
|
unsigned: None,
|
|
|
|
}
|
2020-02-25 16:36:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 10:13:57 +00:00
|
|
|
/// Generate, sign and prepare one-time keys to be uploaded.
|
|
|
|
///
|
|
|
|
/// If no one-time keys need to be uploaded returns an empty error.
|
2020-03-18 14:50:32 +00:00
|
|
|
async fn signed_one_time_keys(&self) -> StdResult<OneTimeKeys, ()> {
|
|
|
|
let _ = self.generate_one_time_keys().await?;
|
2020-04-10 13:28:43 +00:00
|
|
|
let one_time_keys = self.account.one_time_keys().await;
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut one_time_key_map = BTreeMap::new();
|
2020-02-29 10:13:57 +00:00
|
|
|
|
|
|
|
for (key_id, key) in one_time_keys.curve25519().iter() {
|
|
|
|
let key_json = json!({
|
|
|
|
"key": key,
|
|
|
|
});
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
let signature = self.sign_json(&key_json).await;
|
2020-02-29 10:13:57 +00:00
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut signature_map = BTreeMap::new();
|
2020-03-10 12:41:14 +00:00
|
|
|
|
|
|
|
signature_map.insert(
|
|
|
|
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, self.device_id.clone()),
|
|
|
|
signature,
|
|
|
|
);
|
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut signatures = BTreeMap::new();
|
2020-03-10 12:41:14 +00:00
|
|
|
signatures.insert(self.user_id.clone(), signature_map);
|
2020-02-29 10:13:57 +00:00
|
|
|
|
2020-03-10 12:41:14 +00:00
|
|
|
let signed_key = SignedKey {
|
|
|
|
key: key.to_owned(),
|
|
|
|
signatures,
|
|
|
|
};
|
|
|
|
|
|
|
|
one_time_key_map.insert(
|
|
|
|
AlgorithmAndDeviceId(KeyAlgorithm::SignedCurve25519, key_id.to_owned()),
|
|
|
|
OneTimeKey::SignedKey(signed_key),
|
|
|
|
);
|
2020-02-29 10:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(one_time_key_map)
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:31:03 +00:00
|
|
|
/// Convert a JSON value to the canonical representation and sign the JSON
|
|
|
|
/// string.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `json` - The value that should be converted into a canonical JSON
|
|
|
|
/// string.
|
2020-03-18 14:50:32 +00:00
|
|
|
async fn sign_json(&self, json: &Value) -> String {
|
2020-02-26 08:18:10 +00:00
|
|
|
let canonical_json = cjson::to_string(json)
|
|
|
|
.unwrap_or_else(|_| panic!(format!("Can't serialize {} to canonical JSON", json)));
|
2020-04-10 13:28:43 +00:00
|
|
|
self.account.sign(&canonical_json).await
|
2020-02-25 16:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify a signed JSON object.
|
|
|
|
///
|
|
|
|
/// The object must have a signatures key associated with an object of the
|
|
|
|
/// form `user_id: {key_id: signature}`.
|
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// Returns Ok if the signature was successfully verified, otherwise an
|
|
|
|
/// SignatureError.
|
|
|
|
///
|
2020-02-25 16:36:11 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The user who signed the JSON object.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2020-02-25 16:36:11 +00:00
|
|
|
/// * `device_id` - The device that signed the JSON object.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2020-02-25 16:36:11 +00:00
|
|
|
/// * `user_key` - The public ed25519 key which was used to sign the JSON
|
|
|
|
/// object.
|
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `json` - The JSON object that should be verified.
|
2020-02-25 16:36:11 +00:00
|
|
|
fn verify_json(
|
|
|
|
&self,
|
2020-03-10 12:02:14 +00:00
|
|
|
user_id: &UserId,
|
2020-02-25 16:36:11 +00:00
|
|
|
device_id: &str,
|
|
|
|
user_key: &str,
|
|
|
|
json: &mut Value,
|
2020-03-18 13:15:56 +00:00
|
|
|
) -> VerificationResult<()> {
|
2020-02-25 16:36:11 +00:00
|
|
|
let json_object = json.as_object_mut().ok_or(SignatureError::NotAnObject)?;
|
|
|
|
let unsigned = json_object.remove("unsigned");
|
|
|
|
let signatures = json_object.remove("signatures");
|
|
|
|
|
|
|
|
let canonical_json = cjson::to_string(json_object)?;
|
|
|
|
|
|
|
|
if let Some(u) = unsigned {
|
|
|
|
json_object.insert("unsigned".to_string(), u);
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:02:14 +00:00
|
|
|
// TODO this should be part of ruma-client-api.
|
|
|
|
let key_id_string = format!("{}:{}", KeyAlgorithm::Ed25519, device_id);
|
2020-02-25 16:36:11 +00:00
|
|
|
|
|
|
|
let signatures = signatures.ok_or(SignatureError::NoSignatureFound)?;
|
|
|
|
let signature_object = signatures
|
|
|
|
.as_object()
|
|
|
|
.ok_or(SignatureError::NoSignatureFound)?;
|
|
|
|
let signature = signature_object
|
2020-03-10 12:02:14 +00:00
|
|
|
.get(&user_id.to_string())
|
2020-02-25 16:36:11 +00:00
|
|
|
.ok_or(SignatureError::NoSignatureFound)?;
|
|
|
|
let signature = signature
|
2020-03-10 12:02:14 +00:00
|
|
|
.get(key_id_string)
|
2020-02-25 16:36:11 +00:00
|
|
|
.ok_or(SignatureError::NoSignatureFound)?;
|
|
|
|
let signature = signature.as_str().ok_or(SignatureError::NoSignatureFound)?;
|
|
|
|
|
|
|
|
let utility = OlmUtility::new();
|
|
|
|
|
|
|
|
let ret = if utility
|
|
|
|
.ed25519_verify(&user_key, &canonical_json, signature)
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(SignatureError::VerificationError)
|
|
|
|
};
|
|
|
|
|
|
|
|
json_object.insert("signatures".to_string(), signatures);
|
|
|
|
|
|
|
|
ret
|
|
|
|
}
|
2020-03-10 13:06:30 +00:00
|
|
|
|
|
|
|
/// Get a tuple of device and one-time keys that need to be uploaded.
|
|
|
|
///
|
|
|
|
/// Returns an empty error if no keys need to be uploaded.
|
2020-03-18 14:50:32 +00:00
|
|
|
pub async fn keys_for_upload(
|
|
|
|
&self,
|
|
|
|
) -> StdResult<(Option<DeviceKeys>, Option<OneTimeKeys>), ()> {
|
|
|
|
if !self.should_upload_keys().await {
|
2020-03-10 13:06:30 +00:00
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
2020-04-10 13:28:43 +00:00
|
|
|
let shared = self.account.shared();
|
2020-03-18 14:50:32 +00:00
|
|
|
|
|
|
|
let device_keys = if !shared {
|
|
|
|
Some(self.device_keys().await)
|
2020-03-10 13:06:30 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
let one_time_keys: Option<OneTimeKeys> = self.signed_one_time_keys().await.ok();
|
2020-03-10 13:06:30 +00:00
|
|
|
|
|
|
|
Ok((device_keys, one_time_keys))
|
|
|
|
}
|
2020-03-12 14:41:11 +00:00
|
|
|
|
2020-03-21 15:41:48 +00:00
|
|
|
async fn try_decrypt_olm_event(
|
|
|
|
&mut self,
|
2020-04-22 13:15:08 +00:00
|
|
|
sender: &UserId,
|
2020-03-21 15:41:48 +00:00
|
|
|
sender_key: &str,
|
|
|
|
message: &OlmMessage,
|
|
|
|
) -> Result<Option<String>> {
|
2020-03-30 15:07:36 +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);
|
|
|
|
self.store.save_session(session).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(plaintext)
|
2020-03-21 15:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn decrypt_olm_message(
|
|
|
|
&mut 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-23 08:52:47 +00:00
|
|
|
) -> Result<(EventJson<ToDeviceEvent>, String)> {
|
2020-04-22 10:54:49 +00:00
|
|
|
// First try to decrypt using an existing session.
|
|
|
|
let plaintext = if let Some(p) = self
|
|
|
|
.try_decrypt_olm_event(sender, sender_key, &message)
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
// Decryption succeeded, destructure the plaintext out of the
|
|
|
|
// 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-04-10 14:18:55 +00:00
|
|
|
self.store.save_account(self.account.clone()).await?;
|
|
|
|
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-04-16 08:14:23 +00:00
|
|
|
self.store.save_session(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)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_decrypted_to_device_event(
|
|
|
|
&self,
|
|
|
|
sender: &UserId,
|
|
|
|
plaintext: &str,
|
2020-04-23 08:52:47 +00:00
|
|
|
) -> Result<(EventJson<ToDeviceEvent>, 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()
|
|
|
|
.ok_or(OlmError::MissingCiphertext)?;
|
|
|
|
let encrytped_sender: UserId = serde_json::from_value(encrytped_sender)?;
|
|
|
|
let recipient = decrypted_json
|
|
|
|
.get("recipient")
|
|
|
|
.cloned()
|
|
|
|
.ok_or(OlmError::MissingCiphertext)?;
|
|
|
|
let recipient: UserId = serde_json::from_value(recipient)?;
|
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let recipient_keys: BTreeMap<KeyAlgorithm, String> = serde_json::from_value(
|
2020-04-22 13:15:08 +00:00
|
|
|
decrypted_json
|
|
|
|
.get("recipient_keys")
|
|
|
|
.cloned()
|
|
|
|
.ok_or(OlmError::MissingCiphertext)?,
|
|
|
|
)?;
|
2020-04-23 08:52:47 +00:00
|
|
|
let keys: BTreeMap<KeyAlgorithm, String> = serde_json::from_value(
|
2020-04-22 13:15:08 +00:00
|
|
|
decrypted_json
|
|
|
|
.get("keys")
|
|
|
|
.cloned()
|
|
|
|
.ok_or(OlmError::MissingCiphertext)?,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
if recipient != self.user_id || sender != &encrytped_sender {
|
|
|
|
return Err(OlmError::MissingCiphertext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.account.identity_keys().ed25519()
|
|
|
|
!= recipient_keys
|
|
|
|
.get(&KeyAlgorithm::Ed25519)
|
|
|
|
.ok_or(OlmError::MissingCiphertext)?
|
|
|
|
{
|
|
|
|
return Err(OlmError::MissingCiphertext);
|
|
|
|
}
|
|
|
|
|
|
|
|
let signing_key = keys
|
|
|
|
.get(&KeyAlgorithm::Ed25519)
|
|
|
|
.ok_or(OlmError::MissingSigningKey)?;
|
|
|
|
|
|
|
|
Ok((
|
2020-04-23 08:52:47 +00:00
|
|
|
serde_json::from_value::<EventJson<ToDeviceEvent>>(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-03-23 15:14:10 +00:00
|
|
|
&mut self,
|
2020-03-21 15:41:48 +00:00
|
|
|
event: &ToDeviceEncrypted,
|
2020-04-23 08:52:47 +00:00
|
|
|
) -> Result<EventJson<ToDeviceEvent>> {
|
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-03-23 15:14:10 +00:00
|
|
|
return Err(OlmError::UnsupportedAlgorithm);
|
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()
|
|
|
|
.map_err(|_| OlmError::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-03-23 15:14:10 +00:00
|
|
|
.map_err(|_| OlmError::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-04-22 13:15:08 +00:00
|
|
|
let (mut decrypted_event, signing_key) = self
|
|
|
|
.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
|
|
|
|
|
|
|
// Handle the decrypted event, e.g. fetch out megolm sessions out of
|
|
|
|
// the event.
|
2020-04-22 13:15:08 +00:00
|
|
|
self.handle_decrypted_to_device_event(
|
|
|
|
&content.sender_key,
|
|
|
|
&signing_key,
|
|
|
|
&mut decrypted_event,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-03-24 16:25:01 +00:00
|
|
|
|
|
|
|
Ok(decrypted_event)
|
2020-03-23 15:14:10 +00:00
|
|
|
} else {
|
|
|
|
warn!("Olm event doesn't contain a ciphertext for our key");
|
|
|
|
Err(OlmError::MissingCiphertext)
|
|
|
|
}
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 13:15:08 +00:00
|
|
|
async fn add_room_key(
|
|
|
|
&mut self,
|
|
|
|
sender_key: &str,
|
|
|
|
signing_key: &str,
|
|
|
|
event: &mut ToDeviceRoomKey,
|
|
|
|
) -> Result<()> {
|
2020-03-24 15:21:06 +00:00
|
|
|
match event.content.algorithm {
|
2020-03-24 16:25:01 +00:00
|
|
|
Algorithm::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-03-30 15:07:36 +00:00
|
|
|
self.store.save_inbound_group_session(session).await?;
|
2020-03-25 10:32:40 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
warn!(
|
|
|
|
"Received room key with unsupported key algorithm {}",
|
|
|
|
event.content.algorithm
|
|
|
|
);
|
|
|
|
Ok(())
|
2020-03-24 16:25:01 +00:00
|
|
|
}
|
2020-03-24 15:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 13:06:57 +00:00
|
|
|
async fn create_outbound_group_session(&mut self, room_id: &RoomId) -> Result<()> {
|
|
|
|
let session = OutboundGroupSession::new(room_id);
|
2020-04-10 13:28:43 +00:00
|
|
|
let identity_keys = self.account.identity_keys();
|
2020-04-08 13:06:57 +00:00
|
|
|
|
|
|
|
let sender_key = identity_keys.curve25519();
|
|
|
|
let signing_key = identity_keys.ed25519();
|
|
|
|
|
|
|
|
let inbound_session = InboundGroupSession::new(
|
|
|
|
sender_key,
|
|
|
|
signing_key,
|
|
|
|
&room_id,
|
2020-04-10 12:00:03 +00:00
|
|
|
session.session_key().await,
|
2020-04-08 13:06:57 +00:00
|
|
|
)?;
|
|
|
|
self.store
|
|
|
|
.save_inbound_group_session(inbound_session)
|
|
|
|
.await?;
|
|
|
|
|
2020-04-28 08:47:08 +00:00
|
|
|
self.outbound_group_sessions
|
2020-04-08 13:06:57 +00:00
|
|
|
.insert(room_id.to_owned(), session);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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-22 13:22:43 +00:00
|
|
|
) -> Result<EncryptedEventContent> {
|
2020-04-28 08:47:08 +00:00
|
|
|
let session = self.outbound_group_sessions.get(room_id);
|
2020-04-09 14:24:40 +00:00
|
|
|
|
2020-04-10 09:44:09 +00:00
|
|
|
let session = if let Some(s) = session {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
panic!("Session wasn't created nor shared");
|
|
|
|
};
|
2020-04-09 14:24:40 +00:00
|
|
|
|
|
|
|
if session.expired() {
|
2020-04-10 09:44:09 +00:00
|
|
|
panic!("Session is expired");
|
2020-04-09 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:44:09 +00:00
|
|
|
let json_content = json!({
|
2020-04-09 14:24:40 +00:00
|
|
|
"content": content,
|
|
|
|
"room_id": room_id,
|
|
|
|
"type": EventType::RoomMessage,
|
|
|
|
});
|
|
|
|
|
|
|
|
let plaintext = cjson::to_string(&json_content).unwrap_or_else(|_| {
|
|
|
|
panic!(format!(
|
|
|
|
"Can't serialize {} to canonical JSON",
|
|
|
|
json_content
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
let ciphertext = session.encrypt(plaintext).await;
|
|
|
|
|
2020-04-22 13:22:43 +00:00
|
|
|
Ok(EncryptedEventContent::MegolmV1AesSha2(
|
|
|
|
MegolmV1AesSha2Content {
|
|
|
|
algorithm: Algorithm::MegolmV1AesSha2,
|
|
|
|
ciphertext,
|
|
|
|
sender_key: self.account.identity_keys().curve25519().to_owned(),
|
|
|
|
session_id: session.session_id().to_owned(),
|
|
|
|
device_id: self.device_id.to_owned(),
|
|
|
|
},
|
|
|
|
))
|
2020-04-09 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn olm_encrypt(
|
|
|
|
&mut self,
|
2020-04-14 12:05:18 +00:00
|
|
|
mut session: Session,
|
2020-04-09 14:24:40 +00:00
|
|
|
recipient_device: &Device,
|
|
|
|
event_type: EventType,
|
|
|
|
content: Value,
|
2020-04-22 13:38:42 +00:00
|
|
|
) -> Result<EncryptedEventContent> {
|
2020-04-10 13:28:43 +00:00
|
|
|
let identity_keys = self.account.identity_keys();
|
2020-04-09 14:24:40 +00:00
|
|
|
|
|
|
|
let recipient_signing_key = recipient_device
|
2020-04-17 12:14:31 +00:00
|
|
|
.get_key(&KeyAlgorithm::Ed25519)
|
2020-04-09 14:24:40 +00:00
|
|
|
.ok_or(OlmError::MissingSigningKey)?;
|
|
|
|
let recipient_sender_key = recipient_device
|
2020-04-17 12:14:31 +00:00
|
|
|
.get_key(&KeyAlgorithm::Curve25519)
|
2020-04-09 14:24:40 +00:00
|
|
|
.ok_or(OlmError::MissingSigningKey)?;
|
|
|
|
|
|
|
|
let payload = json!({
|
|
|
|
"sender": self.user_id,
|
|
|
|
"sender_device": self.device_id,
|
|
|
|
"keys": {
|
|
|
|
"ed25519": identity_keys.ed25519(),
|
|
|
|
},
|
|
|
|
"recipient": recipient_device.user_id(),
|
|
|
|
"recipient_keys": {
|
|
|
|
"ed25519": recipient_signing_key,
|
|
|
|
},
|
|
|
|
"type": event_type,
|
|
|
|
"content": content,
|
|
|
|
});
|
|
|
|
|
|
|
|
let plaintext = cjson::to_string(&payload)
|
|
|
|
.unwrap_or_else(|_| panic!(format!("Can't serialize {} to canonical JSON", payload)));
|
|
|
|
|
2020-04-14 12:05:18 +00:00
|
|
|
let ciphertext = session.encrypt(&plaintext).await.to_tuple();
|
2020-04-09 14:24:40 +00:00
|
|
|
self.store.save_session(session).await?;
|
|
|
|
|
|
|
|
let message_type: usize = ciphertext.0.into();
|
|
|
|
|
|
|
|
let ciphertext = CiphertextInfo {
|
|
|
|
body: ciphertext.1,
|
|
|
|
message_type: (message_type as u32).into(),
|
|
|
|
};
|
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let mut content = BTreeMap::new();
|
2020-04-09 14:24:40 +00:00
|
|
|
|
|
|
|
content.insert(recipient_sender_key.to_owned(), ciphertext);
|
|
|
|
|
2020-04-22 13:38:42 +00:00
|
|
|
Ok(EncryptedEventContent::OlmV1Curve25519AesSha2(
|
|
|
|
OlmV1Curve25519AesSha2Content {
|
|
|
|
algorithm: Algorithm::OlmV1Curve25519AesSha2,
|
|
|
|
sender_key: identity_keys.curve25519().to_owned(),
|
|
|
|
ciphertext: content,
|
|
|
|
},
|
|
|
|
))
|
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-04-08 13:06:57 +00:00
|
|
|
// TODO accept an algorithm here
|
2020-04-10 09:45:50 +00:00
|
|
|
pub(crate) async fn share_group_session<'a, I>(
|
2020-04-08 13:06:57 +00:00
|
|
|
&mut self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
users: I,
|
2020-04-09 14:24:40 +00:00
|
|
|
) -> Result<Vec<ToDeviceRequest>>
|
2020-04-08 13:06:57 +00:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = &'a UserId>,
|
|
|
|
{
|
2020-04-10 09:45:50 +00:00
|
|
|
self.create_outbound_group_session(room_id).await?;
|
2020-04-28 08:47:08 +00:00
|
|
|
let megolm_session = self.outbound_group_sessions.get(room_id).unwrap();
|
2020-04-10 07:58:13 +00:00
|
|
|
|
|
|
|
if megolm_session.shared() {
|
|
|
|
panic!("Session is already shared");
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:24:40 +00:00
|
|
|
let session_id = megolm_session.session_id().to_owned();
|
|
|
|
megolm_session.mark_as_shared();
|
2020-04-08 13:06:57 +00:00
|
|
|
|
|
|
|
let key_content = json!({
|
|
|
|
"algorithm": Algorithm::MegolmV1AesSha2,
|
|
|
|
"room_id": room_id,
|
2020-04-09 14:24:40 +00:00
|
|
|
"session_id": session_id.clone(),
|
|
|
|
"session_key": megolm_session.session_key().await,
|
|
|
|
"chain_index": megolm_session.message_index().await,
|
2020-04-08 13:06:57 +00:00
|
|
|
});
|
|
|
|
|
2020-04-09 14:24:40 +00:00
|
|
|
let mut user_map = Vec::new();
|
|
|
|
|
2020-04-08 13:06:57 +00:00
|
|
|
for user_id in users {
|
|
|
|
for device in self.store.get_user_devices(user_id).await?.devices() {
|
2020-04-17 12:14:31 +00:00
|
|
|
let sender_key = if let Some(k) = device.get_key(&KeyAlgorithm::Curve25519) {
|
2020-04-08 13:06:57 +00:00
|
|
|
k
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"The device {} of user {} doesn't have a curve 25519 key",
|
|
|
|
user_id,
|
|
|
|
device.device_id()
|
|
|
|
);
|
2020-04-10 09:46:31 +00:00
|
|
|
// TODO mark the user for a key query.
|
2020-04-08 13:06:57 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2020-04-09 14:24:40 +00:00
|
|
|
// TODO abort if the device isn't verified
|
|
|
|
let sessions = self.store.get_sessions(sender_key).await?;
|
|
|
|
|
|
|
|
if let Some(s) = sessions {
|
|
|
|
let session = &s.lock().await[0];
|
|
|
|
user_map.push((session.clone(), device.clone()));
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Trying to encrypt a megolm session for user
|
|
|
|
{} on device {}, but no Olm session is found",
|
|
|
|
user_id,
|
|
|
|
device.device_id()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut message_vec = Vec::new();
|
|
|
|
|
2020-04-10 09:43:03 +00:00
|
|
|
for user_map_chunk in user_map.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
|
|
|
|
|
|
|
for (session, device) in user_map_chunk {
|
|
|
|
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();
|
|
|
|
|
|
|
|
let encrypted_content = self
|
|
|
|
.olm_encrypt(
|
|
|
|
session.clone(),
|
|
|
|
&device,
|
|
|
|
EventType::RoomKey,
|
|
|
|
key_content.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-04-08 13:06:57 +00:00
|
|
|
|
2020-04-09 14:24:40 +00:00
|
|
|
user_messages.insert(
|
|
|
|
DeviceIdOrAllDevices::DeviceId(device.device_id().clone()),
|
2020-04-23 08:52:47 +00:00
|
|
|
EventJson::from(MessageEventContent::Encrypted(encrypted_content)),
|
2020-04-09 14:24:40 +00:00
|
|
|
);
|
2020-04-08 13:06:57 +00:00
|
|
|
}
|
2020-04-09 14:24:40 +00:00
|
|
|
|
|
|
|
message_vec.push(ToDeviceRequest {
|
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-04-09 14:24:40 +00:00
|
|
|
Ok(message_vec)
|
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-04-03 10:34:05 +00:00
|
|
|
_event: &ToDeviceForwardedRoomKey,
|
2020-03-25 10:32:40 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
Ok(())
|
2020-03-24 15:21:06 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:07:36 +00:00
|
|
|
async fn handle_decrypted_to_device_event(
|
2020-03-24 16:25:01 +00:00
|
|
|
&mut self,
|
|
|
|
sender_key: &str,
|
2020-04-22 13:15:08 +00:00
|
|
|
signing_key: &str,
|
2020-04-23 08:52:47 +00:00
|
|
|
event: &mut EventJson<ToDeviceEvent>,
|
2020-03-25 10:32:40 +00:00
|
|
|
) -> Result<()> {
|
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-03-25 10:32:40 +00:00
|
|
|
return Ok(());
|
2020-03-24 15:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
2020-04-23 08:52:47 +00:00
|
|
|
ToDeviceEvent::RoomKey(mut e) => {
|
|
|
|
self.add_room_key(sender_key, signing_key, &mut e).await
|
|
|
|
}
|
|
|
|
ToDeviceEvent::ForwardedRoomKey(mut e) => {
|
|
|
|
self.add_forwarded_room_key(sender_key, signing_key, &mut e)
|
2020-04-22 13:15:08 +00:00
|
|
|
}
|
2020-03-25 10:32:40 +00:00
|
|
|
_ => {
|
|
|
|
warn!("Received a unexpected encrypted to-device event");
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-24 15:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 14:41:11 +00:00
|
|
|
fn handle_room_key_request(&self, _: &ToDeviceRoomKeyRequest) {
|
|
|
|
// TODO handle room key requests here.
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_verification_event(&self, _: &ToDeviceEvent) {
|
|
|
|
// TODO handle to-device verification events here.
|
|
|
|
}
|
|
|
|
|
2020-03-21 15:41:48 +00:00
|
|
|
#[instrument(skip(response))]
|
2020-03-25 10:32:40 +00:00
|
|
|
/// Handle a sync response and update the internal state of the Olm machine.
|
|
|
|
///
|
|
|
|
/// This will decrypt to-device events but will not touch room messages.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The sync latest sync response.
|
2020-03-23 15:14:10 +00:00
|
|
|
pub async fn receive_sync_response(&mut self, response: &mut SyncResponse) {
|
2020-03-12 15:14:43 +00:00
|
|
|
let one_time_key_count = response
|
|
|
|
.device_one_time_keys_count
|
|
|
|
.get(&keys::KeyAlgorithm::SignedCurve25519);
|
|
|
|
|
|
|
|
let count: u64 = one_time_key_count.map_or(0, |c| (*c).into());
|
|
|
|
self.uploaded_signed_key_count = Some(count);
|
|
|
|
|
2020-03-31 11:38:44 +00:00
|
|
|
for event_result in &mut response.to_device.events {
|
2020-04-23 08:52:47 +00:00
|
|
|
let 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-04-23 08:52:47 +00:00
|
|
|
match &event {
|
2020-03-12 14:41:11 +00:00
|
|
|
ToDeviceEvent::RoomEncrypted(e) => {
|
2020-03-24 15:21:06 +00:00
|
|
|
let decrypted_event = match self.decrypt_to_device_event(e).await {
|
|
|
|
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
|
|
|
|
|
|
|
// TODO make sure private keys are cleared from the event
|
|
|
|
// before we replace the result.
|
|
|
|
*event_result = decrypted_event;
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
|
|
|
ToDeviceEvent::RoomKeyRequest(e) => self.handle_room_key_request(e),
|
|
|
|
ToDeviceEvent::KeyVerificationAccept(..)
|
|
|
|
| ToDeviceEvent::KeyVerificationCancel(..)
|
|
|
|
| ToDeviceEvent::KeyVerificationKey(..)
|
|
|
|
| ToDeviceEvent::KeyVerificationMac(..)
|
|
|
|
| ToDeviceEvent::KeyVerificationRequest(..)
|
2020-04-23 08:52:47 +00:00
|
|
|
| ToDeviceEvent::KeyVerificationStart(..) => self.handle_verification_event(&event),
|
2020-03-12 14:41:11 +00:00
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 14:03:10 +00:00
|
|
|
|
|
|
|
pub async fn decrypt_room_event(
|
2020-03-30 15:07:36 +00:00
|
|
|
&mut self,
|
2020-03-25 14:03:10 +00:00
|
|
|
event: &EncryptedEvent,
|
2020-04-23 08:52:47 +00:00
|
|
|
) -> Result<EventJson<RoomEvent>> {
|
2020-03-25 14:03:10 +00:00
|
|
|
let content = match &event.content {
|
|
|
|
EncryptedEventContent::MegolmV1AesSha2(c) => c,
|
|
|
|
_ => return Err(OlmError::UnsupportedAlgorithm),
|
|
|
|
};
|
|
|
|
|
|
|
|
let room_id = event.room_id.as_ref().unwrap();
|
|
|
|
|
2020-03-30 15:07:36 +00:00
|
|
|
let session = self
|
|
|
|
.store
|
2020-04-03 15:00:37 +00:00
|
|
|
.get_inbound_group_session(&room_id, &content.sender_key, &content.session_id)
|
2020-03-30 15:07:36 +00:00
|
|
|
.await?;
|
2020-03-25 14:03:10 +00:00
|
|
|
// TODO check if the olm session is wedged and re-request the key.
|
|
|
|
let session = session.ok_or(OlmError::MissingSession)?;
|
|
|
|
|
2020-04-10 14:08:47 +00:00
|
|
|
let (plaintext, _) = session.decrypt(content.ciphertext.clone()).await?;
|
2020-03-25 14:03:10 +00:00
|
|
|
// TODO check the message index.
|
|
|
|
// TODO check if this is from a verified device.
|
|
|
|
|
|
|
|
let mut decrypted_value = serde_json::from_str::<Value>(&plaintext)?;
|
|
|
|
let decrypted_object = decrypted_value
|
|
|
|
.as_object_mut()
|
|
|
|
.ok_or(OlmError::NotAnObject)?;
|
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
// TODO better number conversion here.
|
|
|
|
let server_ts = event
|
|
|
|
.origin_server_ts
|
|
|
|
.duration_since(std::time::SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.as_millis();
|
|
|
|
let server_ts: i64 = server_ts.try_into().unwrap_or_default();
|
2020-03-25 14:03:10 +00:00
|
|
|
|
|
|
|
decrypted_object.insert("sender".to_owned(), event.sender.to_string().into());
|
|
|
|
decrypted_object.insert("event_id".to_owned(), event.event_id.to_string().into());
|
|
|
|
decrypted_object.insert("origin_server_ts".to_owned(), server_ts.into());
|
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
decrypted_object.insert(
|
|
|
|
"unsigned".to_owned(),
|
|
|
|
serde_json::to_value(&event.unsigned).unwrap_or_default(),
|
|
|
|
);
|
2020-03-25 14:03:10 +00:00
|
|
|
|
2020-04-23 08:52:47 +00:00
|
|
|
let decrypted_event = serde_json::from_value::<EventJson<RoomEvent>>(decrypted_value)?;
|
2020-03-25 14:03:10 +00:00
|
|
|
trace!("Successfully decrypted megolm event {:?}", decrypted_event);
|
|
|
|
// 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.
|
|
|
|
///
|
|
|
|
/// This will only not already seen users for a key query and user tracking.
|
|
|
|
/// If the user is already known to the Olm machine it will not be
|
|
|
|
/// considered for a key query.
|
|
|
|
///
|
|
|
|
/// Use the `mark_user_as_changed()` if the user really needs a key query.
|
|
|
|
pub async fn update_tracked_users<'a, I>(&mut self, users: I)
|
|
|
|
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 {
|
|
|
|
let ret = self.store.add_user_for_tracking(user).await;
|
|
|
|
|
|
|
|
match ret {
|
|
|
|
Ok(newly_added) => {
|
|
|
|
if newly_added {
|
2020-04-03 15:00:37 +00:00
|
|
|
self.users_for_key_query.insert(user.clone());
|
2020-04-01 13:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Error storing users for tracking {}", e);
|
2020-04-03 15:00:37 +00:00
|
|
|
self.users_for_key_query.insert(user.clone());
|
2020-04-01 13:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Should a key query be done.
|
|
|
|
pub fn should_query_keys(&self) -> bool {
|
|
|
|
!self.users_for_key_query.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the set of users that we need to query keys for.
|
2020-04-03 15:00:37 +00:00
|
|
|
pub fn users_for_key_query(&self) -> HashSet<UserId> {
|
2020-04-01 13:37:00 +00:00
|
|
|
self.users_for_key_query.clone()
|
|
|
|
}
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-04-27 14:31:28 +00:00
|
|
|
static USER_ID: &str = "@bob:example.org";
|
|
|
|
static DEVICE_ID: &str = "DEVICEID";
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-02-25 13:36:09 +00:00
|
|
|
use js_int::UInt;
|
2020-04-27 14:31:28 +00:00
|
|
|
use std::collections::BTreeMap;
|
2020-02-25 13:24:18 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
2020-04-28 12:48:49 +00:00
|
|
|
use std::time::SystemTime;
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-03-10 12:02:14 +00:00
|
|
|
use serde_json::json;
|
|
|
|
|
2020-04-28 12:48:49 +00:00
|
|
|
use crate::api::r0::{client_exchange::send_event_to_device::Request as ToDeviceRequest, keys};
|
2020-04-27 14:31:28 +00:00
|
|
|
use crate::crypto::machine::{OlmMachine, OneTimeKeys};
|
|
|
|
use crate::crypto::Device;
|
2020-04-27 16:27:33 +00:00
|
|
|
use crate::events::{
|
2020-04-28 12:48:49 +00:00
|
|
|
collections::all::RoomEvent,
|
|
|
|
room::{
|
|
|
|
encrypted::{EncryptedEvent, EncryptedEventContent},
|
|
|
|
message::{MessageEventContent, TextMessageEventContent},
|
|
|
|
},
|
2020-04-27 16:27:33 +00:00
|
|
|
to_device::{AnyToDeviceEvent, ToDeviceEncrypted},
|
2020-04-28 08:47:08 +00:00
|
|
|
EventJson, EventType,
|
2020-04-27 16:27:33 +00:00
|
|
|
};
|
2020-04-28 12:48:49 +00:00
|
|
|
use crate::identifiers::{DeviceId, EventId, RoomId, UserId};
|
2020-04-27 14:31:28 +00:00
|
|
|
|
2020-02-25 13:24:18 +00:00
|
|
|
use http::Response;
|
|
|
|
|
2020-04-27 14:31:28 +00:00
|
|
|
fn alice_id() -> UserId {
|
|
|
|
UserId::try_from("@alice:example.org").unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn alice_device_id() -> DeviceId {
|
|
|
|
"JLAFKJWSCS".to_string()
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:02:14 +00:00
|
|
|
fn user_id() -> UserId {
|
|
|
|
UserId::try_from(USER_ID).unwrap()
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:24:18 +00:00
|
|
|
fn response_from_file(path: &str) -> Response<Vec<u8>> {
|
2020-02-26 08:36:52 +00:00
|
|
|
let mut file = File::open(path)
|
|
|
|
.unwrap_or_else(|_| panic!(format!("No such data file found {}", path)));
|
2020-02-25 13:24:18 +00:00
|
|
|
let mut contents = Vec::new();
|
|
|
|
file.read_to_end(&mut contents)
|
2020-02-26 08:36:52 +00:00
|
|
|
.unwrap_or_else(|_| panic!(format!("Can't read data file {}", path)));
|
2020-02-25 13:24:18 +00:00
|
|
|
|
|
|
|
Response::builder().status(200).body(contents).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn keys_upload_response() -> keys::upload_keys::Response {
|
|
|
|
let data = response_from_file("tests/data/keys_upload.json");
|
|
|
|
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 {
|
|
|
|
let data = response_from_file("tests/data/keys_query.json");
|
|
|
|
keys::get_keys::Response::try_from(data).expect("Can't parse the keys upload response")
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:48:49 +00:00
|
|
|
fn to_device_requests_to_content(requests: Vec<ToDeviceRequest>) -> EncryptedEventContent {
|
|
|
|
let to_device_request = &requests[0];
|
|
|
|
|
|
|
|
let content: EventJson<EncryptedEventContent> = serde_json::from_str(
|
|
|
|
to_device_request
|
|
|
|
.messages
|
|
|
|
.values()
|
|
|
|
.nth(0)
|
|
|
|
.unwrap()
|
|
|
|
.values()
|
|
|
|
.nth(0)
|
|
|
|
.unwrap()
|
|
|
|
.json()
|
|
|
|
.get(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
content.deserialize().unwrap()
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:31:28 +00:00
|
|
|
async fn get_prepared_machine() -> (OlmMachine, OneTimeKeys) {
|
2020-04-21 07:45:46 +00:00
|
|
|
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
|
|
|
machine.uploaded_signed_key_count = Some(0);
|
2020-04-27 14:31:28 +00:00
|
|
|
let (_, otk) = 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-04-27 14:31:28 +00:00
|
|
|
(machine, otk.unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_machine_after_query() -> (OlmMachine, OneTimeKeys) {
|
|
|
|
let (mut machine, otk) = get_prepared_machine().await;
|
|
|
|
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();
|
|
|
|
let alice = OlmMachine::new(&alice_id, &alice_device).unwrap();
|
|
|
|
|
|
|
|
let alice_deivce = Device::from(&alice);
|
|
|
|
let bob_device = Device::from(&bob);
|
|
|
|
alice.store.save_device(bob_device).await.unwrap();
|
|
|
|
bob.store.save_device(alice_deivce).await.unwrap();
|
|
|
|
|
|
|
|
(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) {
|
|
|
|
let (mut alice, bob, one_time_keys) = get_machine_pair().await;
|
|
|
|
|
|
|
|
let mut bob_keys = BTreeMap::new();
|
|
|
|
|
|
|
|
let one_time_key = one_time_keys.iter().nth(0).unwrap();
|
|
|
|
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) {
|
|
|
|
let (mut alice, mut bob) = get_machine_pair_with_session().await;
|
|
|
|
|
|
|
|
let session = alice
|
|
|
|
.store
|
|
|
|
.get_sessions(bob.account.identity_keys().curve25519())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.await[0]
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
let bob_device = alice
|
|
|
|
.store
|
|
|
|
.get_device(&bob.user_id, &bob.device_id)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let event = ToDeviceEncrypted {
|
|
|
|
sender: alice.user_id.clone(),
|
|
|
|
content: alice
|
|
|
|
.olm_encrypt(session, &bob_device, EventType::Dummy, json!({}))
|
|
|
|
.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-03-18 13:15:56 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
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-03-18 13:15:56 +00:00
|
|
|
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
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
|
|
|
|
.remove(&keys::KeyAlgorithm::SignedCurve25519)
|
|
|
|
.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-02-25 13:36:09 +00:00
|
|
|
response.one_time_key_counts.insert(
|
|
|
|
keys::KeyAlgorithm::SignedCurve25519,
|
|
|
|
UInt::try_from(10).unwrap(),
|
|
|
|
);
|
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-02-25 13:36:09 +00:00
|
|
|
response.one_time_key_counts.insert(
|
|
|
|
keys::KeyAlgorithm::SignedCurve25519,
|
|
|
|
UInt::try_from(50).unwrap(),
|
|
|
|
);
|
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-03-18 13:15:56 +00:00
|
|
|
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
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);
|
|
|
|
assert!(machine.generate_one_time_keys().await.is_err());
|
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);
|
|
|
|
assert!(machine.generate_one_time_keys().await.is_ok());
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-02-25 13:36:09 +00:00
|
|
|
response.one_time_key_counts.insert(
|
|
|
|
keys::KeyAlgorithm::SignedCurve25519,
|
|
|
|
UInt::try_from(50).unwrap(),
|
|
|
|
);
|
2020-03-18 14:50:32 +00:00
|
|
|
machine
|
|
|
|
.receive_keys_upload_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(machine.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-03-18 13:15:56 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
let mut device_keys = machine.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();
|
|
|
|
|
|
|
|
let ret = machine.verify_json(
|
|
|
|
&machine.user_id,
|
|
|
|
&machine.device_id,
|
|
|
|
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-03-18 14:50:32 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_invalid_signature() {
|
2020-03-18 13:15:56 +00:00
|
|
|
let machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
2020-02-25 16:49:43 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
let mut device_keys = machine.device_keys().await;
|
2020-02-25 16:49:43 +00:00
|
|
|
|
|
|
|
let ret = machine.verify_json(
|
|
|
|
&machine.user_id,
|
|
|
|
&machine.device_id,
|
|
|
|
"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-03-18 13:15:56 +00:00
|
|
|
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
2020-02-29 10:13:57 +00:00
|
|
|
machine.uploaded_signed_key_count = Some(49);
|
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
let mut one_time_keys = machine.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-03-10 12:41:14 +00:00
|
|
|
let mut one_time_key = one_time_keys.values_mut().nth(0).unwrap();
|
2020-02-29 10:13:57 +00:00
|
|
|
|
|
|
|
let ret = machine.verify_json(
|
|
|
|
&machine.user_id,
|
|
|
|
&machine.device_id,
|
|
|
|
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-03-18 13:15:56 +00:00
|
|
|
let mut machine = OlmMachine::new(&user_id(), DEVICE_ID).unwrap();
|
2020-03-10 13:06:30 +00:00
|
|
|
machine.uploaded_signed_key_count = Some(0);
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let (device_keys, mut one_time_keys) = machine
|
|
|
|
.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");
|
|
|
|
|
|
|
|
let ret = machine.verify_json(
|
|
|
|
&machine.user_id,
|
|
|
|
&machine.device_id,
|
|
|
|
ed25519_key,
|
|
|
|
&mut json!(&mut one_time_keys.as_mut().unwrap().values_mut().nth(0)),
|
|
|
|
);
|
|
|
|
assert!(ret.is_ok());
|
|
|
|
|
|
|
|
let ret = machine.verify_json(
|
|
|
|
&machine.user_id,
|
|
|
|
&machine.device_id,
|
|
|
|
ed25519_key,
|
|
|
|
&mut json!(&mut device_keys.unwrap()),
|
|
|
|
);
|
|
|
|
assert!(ret.is_ok());
|
|
|
|
|
|
|
|
let mut response = keys_upload_response();
|
|
|
|
response.one_time_key_counts.insert(
|
|
|
|
keys::KeyAlgorithm::SignedCurve25519,
|
|
|
|
UInt::new_wrapping(one_time_keys.unwrap().len() as u64),
|
|
|
|
);
|
|
|
|
|
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-04-27 14:31:28 +00:00
|
|
|
let (mut machine, _) = get_prepared_machine().await;
|
2020-04-21 07:45:46 +00:00
|
|
|
let response = keys_query_response();
|
|
|
|
let alice_id = UserId::try_from("@alice:example.org").unwrap();
|
|
|
|
let alice_device_id = "JLAFKJWSCS".to_owned();
|
|
|
|
|
|
|
|
let alice_devices = machine.store.get_user_devices(&alice_id).await.unwrap();
|
|
|
|
assert!(alice_devices.devices().peekable().peek().is_none());
|
|
|
|
|
|
|
|
machine
|
|
|
|
.receive_keys_query_response(&response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let device = machine
|
|
|
|
.store
|
|
|
|
.get_device(&alice_id, &alice_device_id)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(device.user_id(), &alice_id);
|
|
|
|
assert_eq!(device.device_id(), &alice_device_id);
|
|
|
|
}
|
2020-04-27 14:31:28 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_missing_sessions_calculation() {
|
|
|
|
let (mut machine, _) = get_machine_after_query().await;
|
|
|
|
|
|
|
|
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-04-27 14:31:28 +00:00
|
|
|
let (mut alice_machine, bob_machine, one_time_keys) = get_machine_pair().await;
|
|
|
|
|
|
|
|
let mut bob_keys = BTreeMap::new();
|
|
|
|
|
|
|
|
let one_time_key = one_time_keys.iter().nth(0).unwrap();
|
|
|
|
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() {
|
|
|
|
let (mut alice, mut bob) = get_machine_pair_with_session().await;
|
|
|
|
|
|
|
|
let session = alice
|
|
|
|
.store
|
|
|
|
.get_sessions(bob.account.identity_keys().curve25519())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.lock()
|
|
|
|
.await[0]
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
let bob_device = alice
|
|
|
|
.store
|
|
|
|
.get_device(&bob.user_id, &bob.device_id)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let event = ToDeviceEncrypted {
|
|
|
|
sender: alice.user_id.clone(),
|
|
|
|
content: alice
|
|
|
|
.olm_encrypt(session, &bob_device, EventType::Dummy, json!({}))
|
|
|
|
.await
|
|
|
|
.unwrap(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let event = bob.decrypt_to_device_event(&event).await.unwrap();
|
|
|
|
|
|
|
|
if let AnyToDeviceEvent::Dummy(e) = event.deserialize().unwrap() {
|
|
|
|
assert_eq!(e.sender, alice.user_id);
|
|
|
|
} else {
|
|
|
|
panic!("Event had the wrong type");
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 08:47:08 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_room_key_sharing() {
|
|
|
|
let (mut alice, mut bob) = get_machine_pair_with_session().await;
|
|
|
|
|
|
|
|
let room_id = RoomId::try_from("!test:example.org").unwrap();
|
|
|
|
|
|
|
|
let to_device_requests = alice
|
|
|
|
.share_group_session(&room_id, [bob.user_id.clone()].iter())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let event = ToDeviceEncrypted {
|
|
|
|
sender: alice.user_id.clone(),
|
2020-04-28 12:48:49 +00:00
|
|
|
content: to_device_requests_to_content(to_device_requests),
|
2020-04-28 08:47:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let alice_session = alice.outbound_group_sessions.get(&room_id).unwrap();
|
|
|
|
|
|
|
|
let event = bob.decrypt_to_device_event(&event).await.unwrap();
|
|
|
|
|
|
|
|
if let AnyToDeviceEvent::RoomKey(e) = event.deserialize().unwrap() {
|
|
|
|
assert_eq!(e.sender, alice.user_id);
|
|
|
|
} else {
|
|
|
|
panic!("Event had the wrong type");
|
|
|
|
}
|
|
|
|
|
|
|
|
let session = bob
|
|
|
|
.store
|
|
|
|
.get_inbound_group_session(
|
|
|
|
&room_id,
|
|
|
|
alice.account.identity_keys().curve25519(),
|
|
|
|
alice_session.session_id(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
2020-04-28 12:48:49 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_megolm_encryption() {
|
|
|
|
let (mut alice, mut bob) = get_machine_pair_with_setup_sessions().await;
|
|
|
|
let room_id = RoomId::try_from("!test:example.org").unwrap();
|
|
|
|
|
|
|
|
let to_device_requests = alice
|
2020-04-28 13:05:20 +00:00
|
|
|
.share_group_session(&room_id, [bob.user_id().clone()].iter())
|
2020-04-28 12:48:49 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let event = ToDeviceEncrypted {
|
2020-04-28 13:05:20 +00:00
|
|
|
sender: alice.user_id().clone(),
|
2020-04-28 12:48:49 +00:00
|
|
|
content: to_device_requests_to_content(to_device_requests),
|
|
|
|
};
|
|
|
|
|
|
|
|
bob.decrypt_to_device_event(&event).await.unwrap();
|
|
|
|
|
|
|
|
let content = MessageEventContent::Text(TextMessageEventContent::new_plain(
|
|
|
|
"It is a secret to everybody",
|
|
|
|
));
|
|
|
|
|
|
|
|
let encrypted_content = alice.encrypt(&room_id, content.clone()).await.unwrap();
|
|
|
|
|
|
|
|
let event = EncryptedEvent {
|
|
|
|
event_id: EventId::new("example.org").unwrap(),
|
|
|
|
origin_server_ts: SystemTime::now(),
|
|
|
|
room_id: Some(room_id.clone()),
|
2020-04-28 13:05:20 +00:00
|
|
|
sender: alice.user_id().clone(),
|
2020-04-28 12:48:49 +00:00
|
|
|
content: encrypted_content,
|
|
|
|
unsigned: BTreeMap::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let decrypted_event = bob
|
|
|
|
.decrypt_room_event(&event)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.deserialize()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let decrypted_event = match decrypted_event {
|
|
|
|
RoomEvent::RoomMessage(e) => e,
|
|
|
|
_ => panic!("Decrypted room event has the wrong type"),
|
|
|
|
};
|
|
|
|
|
2020-04-28 13:05:20 +00:00
|
|
|
assert_eq!(&decrypted_event.sender, alice.user_id());
|
2020-04-28 12:48:49 +00:00
|
|
|
assert_eq!(&decrypted_event.room_id, &Some(room_id));
|
|
|
|
assert_eq!(&decrypted_event.content, &content);
|
|
|
|
}
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|