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-03-10 12:02:14 +00:00
|
|
|
use std::collections::HashMap;
|
2020-02-25 13:24:18 +00:00
|
|
|
use std::convert::TryInto;
|
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-03-18 14:50:32 +00:00
|
|
|
use std::sync::Arc;
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-03-21 15:41:48 +00:00
|
|
|
use super::error::{OlmError, Result, SignatureError, VerificationResult};
|
2020-02-25 13:24:18 +00:00
|
|
|
use super::olm::Account;
|
2020-03-18 14:50:32 +00:00
|
|
|
#[cfg(feature = "sqlite-cryptostore")]
|
|
|
|
use super::store::sqlite::SqliteStore;
|
|
|
|
use super::store::MemoryStore;
|
|
|
|
use super::CryptoStore;
|
2020-02-25 13:24:18 +00:00
|
|
|
use crate::api;
|
|
|
|
|
|
|
|
use api::r0::keys;
|
|
|
|
|
2020-02-25 16:36:11 +00:00
|
|
|
use cjson;
|
2020-03-21 15:41:48 +00:00
|
|
|
use olm_rs::session::OlmMessage;
|
2020-02-25 16:36:11 +00:00
|
|
|
use olm_rs::utility::OlmUtility;
|
|
|
|
use serde_json::json;
|
2020-02-29 10:13:57 +00:00
|
|
|
use serde_json::Value;
|
2020-03-18 14:50:32 +00:00
|
|
|
use tokio::sync::Mutex;
|
2020-03-21 15:41:48 +00:00
|
|
|
use tracing::{debug, info, instrument, trace, warn};
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-03-10 12:41:14 +00:00
|
|
|
use ruma_client_api::r0::keys::{
|
|
|
|
AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey, SignedKey,
|
|
|
|
};
|
2020-03-12 14:41:11 +00:00
|
|
|
use ruma_client_api::r0::sync::sync_events::IncomingResponse as SyncResponse;
|
|
|
|
use ruma_events::{
|
2020-03-21 15:41:48 +00:00
|
|
|
room::encrypted::EncryptedEventContent,
|
2020-03-13 15:58:59 +00:00
|
|
|
to_device::{AnyToDeviceEvent as ToDeviceEvent, ToDeviceEncrypted, ToDeviceRoomKeyRequest},
|
2020-03-12 14:41:11 +00:00
|
|
|
Algorithm, EventResult,
|
|
|
|
};
|
2020-03-10 12:02:14 +00:00
|
|
|
use ruma_identifiers::{DeviceId, UserId};
|
|
|
|
|
2020-03-10 13:06:30 +00:00
|
|
|
pub type OneTimeKeys = HashMap<AlgorithmAndDeviceId, OneTimeKey>;
|
2020-03-10 12:41:14 +00:00
|
|
|
|
2020-03-11 09:04:04 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct OlmMachine {
|
2020-02-25 13:24:18 +00:00
|
|
|
/// The unique user id that owns this account.
|
2020-03-10 12:02:14 +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-03-10 12:02:14 +00:00
|
|
|
device_id: DeviceId,
|
2020-02-25 13:24:18 +00:00
|
|
|
/// Our underlying Olm Account holding our identity keys.
|
2020-03-18 14:50:32 +00:00
|
|
|
account: Arc<Mutex<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.
|
|
|
|
store: Box<dyn CryptoStore>,
|
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-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-03-18 14:50:32 +00:00
|
|
|
account: Arc::new(Mutex::new(Account::new())),
|
|
|
|
uploaded_signed_key_count: None,
|
|
|
|
store: Box::new(MemoryStore::new()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 =
|
|
|
|
SqliteStore::open_with_passphrase(&user_id.to_string(), device_id, path, passphrase)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
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-03-18 14:50:32 +00:00
|
|
|
Ok(OlmMachine {
|
|
|
|
user_id: user_id.clone(),
|
|
|
|
device_id: device_id.to_owned(),
|
2020-03-18 15:05:59 +00:00
|
|
|
account: Arc::new(Mutex::new(account)),
|
2020-02-26 08:36:52 +00:00
|
|
|
uploaded_signed_key_count: None,
|
2020-03-18 15:05:59 +00:00
|
|
|
store: Box::new(store),
|
2020-03-18 13:15:56 +00:00
|
|
|
})
|
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 {
|
|
|
|
if !self.account.lock().await.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-03-18 14:50:32 +00:00
|
|
|
let max_keys = self.account.lock().await.max_one_time_keys() 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<()> {
|
|
|
|
let mut account = self.account.lock().await;
|
2020-03-19 12:55:04 +00:00
|
|
|
if !account.shared {
|
|
|
|
debug!("Marking account as shared");
|
|
|
|
}
|
2020-03-18 14:50:32 +00:00
|
|
|
account.shared = true;
|
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-03-18 14:50:32 +00:00
|
|
|
account.mark_keys_as_published();
|
|
|
|
drop(account);
|
|
|
|
|
|
|
|
self.store.save_account(self.account.clone()).await?;
|
|
|
|
Ok(())
|
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, ()> {
|
|
|
|
let account = self.account.lock().await;
|
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-03-18 14:50:32 +00:00
|
|
|
let max_keys = account.max_one_time_keys() 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-02-25 13:36:09 +00:00
|
|
|
let key_count: usize = key_count
|
|
|
|
.try_into()
|
2020-03-18 14:50:32 +00:00
|
|
|
.unwrap_or_else(|_| account.max_one_time_keys());
|
2020-02-25 13:24:18 +00:00
|
|
|
|
2020-03-18 14:50:32 +00:00
|
|
|
account.generate_one_time_keys(key_count);
|
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 {
|
|
|
|
let identity_keys = self.account.lock().await.identity_keys();
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-03-10 12:02:14 +00:00
|
|
|
let mut keys = HashMap::new();
|
|
|
|
|
|
|
|
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-03-10 12:02:14 +00:00
|
|
|
let mut signatures = HashMap::new();
|
2020-02-25 16:36:11 +00:00
|
|
|
|
2020-03-10 12:02:14 +00:00
|
|
|
let mut signature = HashMap::new();
|
|
|
|
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?;
|
|
|
|
let one_time_keys = self.account.lock().await.one_time_keys();
|
2020-03-10 12:41:14 +00:00
|
|
|
let mut one_time_key_map = HashMap::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-03-10 12:41:14 +00:00
|
|
|
let mut signature_map = HashMap::new();
|
|
|
|
|
|
|
|
signature_map.insert(
|
|
|
|
AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, self.device_id.clone()),
|
|
|
|
signature,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut signatures = HashMap::new();
|
|
|
|
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 {
|
|
|
|
let account = self.account.lock().await;
|
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-03-18 14:50:32 +00:00
|
|
|
account.sign(&canonical_json)
|
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-03-18 14:50:32 +00:00
|
|
|
let shared = self.account.lock().await.shared();
|
|
|
|
|
|
|
|
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,
|
|
|
|
sender_key: &str,
|
|
|
|
message: &OlmMessage,
|
|
|
|
) -> Result<Option<String>> {
|
|
|
|
let mut s = self.store.sessions_mut(sender_key).await?;
|
|
|
|
|
|
|
|
let sessions = if let Some(s) = s {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
for session in sessions.iter_mut() {
|
|
|
|
let mut matches = false;
|
|
|
|
|
|
|
|
if let OlmMessage::PreKey(m) = &message {
|
2020-03-23 15:14:10 +00:00
|
|
|
matches = session.matches(sender_key, m.clone())?;
|
2020-03-21 15:41:48 +00:00
|
|
|
if !matches {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = session.decrypt(message.clone());
|
|
|
|
|
|
|
|
if let Ok(p) = ret {
|
|
|
|
// TODO save the session.
|
|
|
|
return Ok(Some(p));
|
|
|
|
} else {
|
|
|
|
if matches {
|
|
|
|
return Err(OlmError::SessionWedged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn decrypt_olm_message(
|
|
|
|
&mut self,
|
|
|
|
sender: &str,
|
|
|
|
sender_key: &str,
|
|
|
|
message: OlmMessage,
|
2020-03-23 15:14:10 +00:00
|
|
|
) -> Result<EventResult<ToDeviceEvent>> {
|
2020-03-21 15:41:48 +00:00
|
|
|
let plaintext = if let Some(p) = self.try_decrypt_olm_event(sender_key, &message).await? {
|
|
|
|
p
|
|
|
|
} else {
|
|
|
|
let mut session = match &message {
|
|
|
|
OlmMessage::Message(_) => return Err(OlmError::SessionWedged),
|
|
|
|
OlmMessage::PreKey(m) => {
|
|
|
|
let account = self.account.lock().await;
|
2020-03-23 15:14:10 +00:00
|
|
|
account.create_inbound_session_from(sender_key, m.clone())?
|
2020-03-21 15:41:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-23 15:14:10 +00:00
|
|
|
session.decrypt(message)?
|
2020-03-21 15:41:48 +00:00
|
|
|
// TODO save the session
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO convert the plaintext to a ruma event.
|
2020-03-23 15:14:10 +00:00
|
|
|
let mut json_plaintext = serde_json::from_str::<Value>(&plaintext)?;
|
|
|
|
let json_object = json_plaintext
|
|
|
|
.as_object_mut()
|
|
|
|
.ok_or(OlmError::NotAnObject)?;
|
|
|
|
json_object.insert("sender".to_owned(), sender.into());
|
|
|
|
|
|
|
|
Ok(serde_json::from_value::<EventResult<ToDeviceEvent>>(
|
|
|
|
json_plaintext,
|
|
|
|
)?)
|
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-19 12:55:04 +00:00
|
|
|
#[instrument]
|
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-03-23 15:14:10 +00:00
|
|
|
) -> Result<EventResult<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
|
|
|
};
|
|
|
|
|
|
|
|
let identity_keys = self.account.lock().await.identity_keys();
|
|
|
|
let own_key = identity_keys.curve25519();
|
|
|
|
let own_ciphertext = content.ciphertext.get(own_key);
|
|
|
|
|
|
|
|
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-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-03-23 15:14:10 +00:00
|
|
|
Ok(self
|
|
|
|
.decrypt_olm_message(&event.sender.to_string(), &content.sender_key, message)
|
|
|
|
.await?)
|
|
|
|
} else {
|
|
|
|
warn!("Olm event doesn't contain a ciphertext for our key");
|
|
|
|
Err(OlmError::MissingCiphertext)
|
|
|
|
}
|
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-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-12 14:41:11 +00:00
|
|
|
for event in response.to_device.events.iter() {
|
|
|
|
let event = if let EventResult::Ok(e) = event {
|
|
|
|
e
|
|
|
|
} else {
|
|
|
|
// Skip invalid events.
|
2020-03-19 12:55:04 +00:00
|
|
|
warn!("Received an invalid to-device event {:?}", event);
|
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-03-12 14:41:11 +00:00
|
|
|
match event {
|
|
|
|
ToDeviceEvent::RoomEncrypted(e) => {
|
|
|
|
// TODO put the decrypted event into a vec so we can replace
|
|
|
|
// them in the sync response.
|
2020-03-23 15:14:10 +00:00
|
|
|
let decrypted_event = self.decrypt_to_device_event(e).await;
|
|
|
|
info!("Decrypted a to-device event {:?}", 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(..)
|
|
|
|
| ToDeviceEvent::KeyVerificationStart(..) => self.handle_verification_event(event),
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-25 13:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-03-10 12:02:14 +00:00
|
|
|
static USER_ID: &str = "@test:example.org";
|
2020-02-25 13:24:18 +00:00
|
|
|
const DEVICE_ID: &str = "DEVICEID";
|
|
|
|
|
2020-02-25 13:36:09 +00:00
|
|
|
use js_int::UInt;
|
2020-02-25 13:24:18 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
2020-03-10 12:02:14 +00:00
|
|
|
use ruma_identifiers::UserId;
|
|
|
|
use serde_json::json;
|
|
|
|
|
2020-02-25 13:24:18 +00:00
|
|
|
use crate::api::r0::keys;
|
|
|
|
use crate::crypto::machine::OlmMachine;
|
|
|
|
use http::Response;
|
|
|
|
|
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-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;
|
|
|
|
let identity_keys = machine.account.lock().await.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();
|
|
|
|
let identity_keys = machine.account.lock().await.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-03-18 14:50:32 +00:00
|
|
|
let identity_keys = machine.account.lock().await.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-02-25 13:24:18 +00:00
|
|
|
}
|