crypto: Encrypt room keys for a room key share request in parallel
parent
91c326e970
commit
a8bc619dca
|
@ -29,6 +29,8 @@ serde_json = "1.0.61"
|
||||||
zeroize = { version = "1.2.0", features = ["zeroize_derive"] }
|
zeroize = { version = "1.2.0", features = ["zeroize_derive"] }
|
||||||
|
|
||||||
# Misc dependencies
|
# Misc dependencies
|
||||||
|
tokio = { version = "1.1.0", default-features = false, features = ["rt", "rt-multi-thread"] }
|
||||||
|
futures = "0.3.12"
|
||||||
sled = { version = "0.34.6", optional = true }
|
sled = { version = "0.34.6", optional = true }
|
||||||
thiserror = "1.0.23"
|
thiserror = "1.0.23"
|
||||||
tracing = "0.1.22"
|
tracing = "0.1.22"
|
||||||
|
@ -44,7 +46,6 @@ byteorder = "1.4.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1.1.0", default-features = false, features = ["rt-multi-thread", "macros"] }
|
tokio = { version = "1.1.0", default-features = false, features = ["rt-multi-thread", "macros"] }
|
||||||
futures = "0.3.12"
|
|
||||||
proptest = "0.10.1"
|
proptest = "0.10.1"
|
||||||
serde_json = "1.0.61"
|
serde_json = "1.0.61"
|
||||||
tempfile = "3.2.0"
|
tempfile = "3.2.0"
|
||||||
|
|
|
@ -17,6 +17,10 @@ use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use futures::future::join_all;
|
||||||
|
|
||||||
|
use tokio;
|
||||||
|
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use matrix_sdk_common::{
|
use matrix_sdk_common::{
|
||||||
api::r0::to_device::DeviceIdOrAllDevices,
|
api::r0::to_device::DeviceIdOrAllDevices,
|
||||||
|
@ -195,28 +199,46 @@ impl GroupSessionManager {
|
||||||
let mut messages = BTreeMap::new();
|
let mut messages = BTreeMap::new();
|
||||||
let mut changed_sessions = Vec::new();
|
let mut changed_sessions = Vec::new();
|
||||||
|
|
||||||
for device in devices {
|
let encrypt = |device: Device, content: Value| async move {
|
||||||
|
let mut message = BTreeMap::new();
|
||||||
|
|
||||||
let encrypted = device.encrypt(EventType::RoomKey, content.clone()).await;
|
let encrypted = device.encrypt(EventType::RoomKey, content.clone()).await;
|
||||||
|
|
||||||
let (used_session, encrypted) = match encrypted {
|
let used_session = match encrypted {
|
||||||
Ok(c) => c,
|
Ok((session, encrypted)) => {
|
||||||
// TODO we'll want to create m.room_key.withheld here.
|
message
|
||||||
Err(OlmError::MissingSession)
|
|
||||||
| Err(OlmError::EventError(EventError::MissingSenderKey)) => {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
|
|
||||||
changed_sessions.push(used_session);
|
|
||||||
|
|
||||||
messages
|
|
||||||
.entry(device.user_id().clone())
|
.entry(device.user_id().clone())
|
||||||
.or_insert_with(BTreeMap::new)
|
.or_insert_with(BTreeMap::new)
|
||||||
.insert(
|
.insert(
|
||||||
DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
|
DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
|
||||||
serde_json::value::to_raw_value(&encrypted)?,
|
serde_json::value::to_raw_value(&encrypted)?,
|
||||||
);
|
);
|
||||||
|
Some(session)
|
||||||
|
}
|
||||||
|
// TODO we'll want to create m.room_key.withheld here.
|
||||||
|
Err(OlmError::MissingSession)
|
||||||
|
| Err(OlmError::EventError(EventError::MissingSenderKey)) => None,
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok((used_session, message))
|
||||||
|
};
|
||||||
|
|
||||||
|
let tasks: Vec<_> = devices
|
||||||
|
.iter()
|
||||||
|
.map(|d| tokio::spawn(encrypt(d.clone(), content.clone())))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let results = join_all(tasks).await;
|
||||||
|
|
||||||
|
for result in results {
|
||||||
|
let (used_session, message) = result.expect("Encryption task paniced")?;
|
||||||
|
|
||||||
|
if let Some(session) = used_session {
|
||||||
|
changed_sessions.push(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
messages.extend(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
|
|
Loading…
Reference in New Issue