matrix-sdk: Add encryption info to our sync events.

master
Damir Jelić 2021-04-28 11:48:20 +02:00
parent 5ed0c7a7b3
commit cff90b1480
6 changed files with 329 additions and 260 deletions

View File

@ -81,7 +81,12 @@ async fn login(
let client = &client_ref;
let initial = &initial_ref;
for event in &response.to_device.events {
for event in response
.to_device
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
match event {
AnyToDeviceEvent::KeyVerificationStart(e) => {
let sas = client
@ -124,7 +129,12 @@ async fn login(
if !initial.load(Ordering::SeqCst) {
for (_room_id, room_info) in response.rooms.join {
for event in room_info.timeline.events {
for event in room_info
.timeline
.events
.iter()
.filter_map(|e| e.event.deserialize().ok())
{
if let AnySyncRoomEvent::Message(event) = event {
match event {
AnySyncMessageEvent::RoomMessage(m) => {

View File

@ -75,50 +75,95 @@ impl Handler {
pub(crate) async fn handle_sync(&self, response: &SyncResponse) {
for (room_id, room_info) in &response.rooms.join {
if let Some(room) = self.get_room(room_id) {
for event in &room_info.ephemeral.events {
self.handle_ephemeral_event(room.clone(), event).await;
for event in room_info
.ephemeral
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
self.handle_ephemeral_event(room.clone(), &event).await;
}
for event in &room_info.account_data.events {
self.handle_account_data_event(room.clone(), event).await;
for event in room_info
.account_data
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
self.handle_account_data_event(room.clone(), &event).await;
}
for event in &room_info.state.events {
self.handle_state_event(room.clone(), event).await;
for event in room_info
.state
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
self.handle_state_event(room.clone(), &event).await;
}
for event in &room_info.timeline.events {
self.handle_timeline_event(room.clone(), event).await;
for event in room_info
.timeline
.events
.iter()
.filter_map(|e| e.event.deserialize().ok())
{
self.handle_timeline_event(room.clone(), &event).await;
}
}
}
for (room_id, room_info) in &response.rooms.leave {
if let Some(room) = self.get_room(room_id) {
for event in &room_info.account_data.events {
self.handle_account_data_event(room.clone(), event).await;
for event in room_info
.account_data
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
self.handle_account_data_event(room.clone(), &event).await;
}
for event in &room_info.state.events {
self.handle_state_event(room.clone(), event).await;
for event in room_info
.state
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
self.handle_state_event(room.clone(), &event).await;
}
for event in &room_info.timeline.events {
self.handle_timeline_event(room.clone(), event).await;
for event in room_info
.timeline
.events
.iter()
.filter_map(|e| e.event.deserialize().ok())
{
self.handle_timeline_event(room.clone(), &event).await;
}
}
}
for (room_id, room_info) in &response.rooms.invite {
if let Some(room) = self.get_room(room_id) {
for event in &room_info.invite_state.events {
self.handle_stripped_state_event(room.clone(), event).await;
for event in room_info
.invite_state
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
self.handle_stripped_state_event(room.clone(), &event).await;
}
}
}
for event in &response.presence.events {
self.on_presence_event(event).await;
for event in response
.presence
.events
.iter()
.filter_map(|e| e.deserialize().ok())
{
self.on_presence_event(&event).await;
}
for (room_id, notifications) in &response.notifications {

View File

@ -37,15 +37,13 @@ use matrix_sdk_common::{
use matrix_sdk_common::{
api::r0::{self as api, push::get_notifications::Notification},
deserialized_responses::{
AccountData, AmbiguityChanges, Ephemeral, InviteState, InvitedRoom, JoinedRoom, LeftRoom,
MemberEvent, MembersResponse, Presence, Rooms, State, StrippedMemberEvent, SyncResponse,
Timeline,
AmbiguityChanges, JoinedRoom, LeftRoom, MemberEvent, MembersResponse, Rooms,
StrippedMemberEvent, SyncResponse, SyncRoomEvent, Timeline,
},
events::{
presence::PresenceEvent,
room::member::{MemberEventContent, MembershipState},
AnyBasicEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent,
AnyToDeviceEvent, EventContent, EventType, StateEvent,
AnyBasicEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventContent,
EventType, StateEvent,
},
identifiers::{RoomId, UserId},
instant::Instant,
@ -432,15 +430,12 @@ impl BaseClient {
let mut push_context = self.get_push_room_context(room, room_info, changes).await?;
for event in ruma_timeline.events {
match hoist_room_event_prev_content(&event) {
Ok(mut e) => {
#[cfg(not(feature = "encryption"))]
let raw_event = event;
#[cfg(feature = "encryption")]
let mut raw_event = event;
let mut event: SyncRoomEvent = event.into();
match hoist_room_event_prev_content(&event.event) {
Ok(e) => {
#[allow(clippy::single_match)]
match &mut e {
match &e {
AnySyncRoomEvent::State(s) => match s {
AnySyncStateEvent::RoomMember(member) => {
if let Ok(member) = MemberEvent::try_from(member.clone()) {
@ -487,18 +482,10 @@ impl BaseClient {
encrypted,
)) => {
if let Some(olm) = self.olm_machine().await {
if let Ok(raw_decrypted) =
if let Ok(decrypted) =
olm.decrypt_room_event(encrypted, room_id).await
{
match raw_decrypted.deserialize() {
Ok(decrypted) => {
e = decrypted;
raw_event = raw_decrypted;
}
Err(e) => {
warn!("Error deserializing a decrypted event {:?} ", e)
}
}
event = decrypted;
}
}
}
@ -517,14 +504,14 @@ impl BaseClient {
}
if let Some(context) = &push_context {
let actions = push_rules.get_actions(&raw_event, &context).to_vec();
let actions = push_rules.get_actions(&event.event, &context).to_vec();
if actions.iter().any(|a| matches!(a, Action::Notify)) {
changes.add_notification(
room_id,
Notification::new(
actions,
raw_event,
event.event.clone(),
false,
room_id.clone(),
SystemTime::now(),
@ -537,13 +524,13 @@ impl BaseClient {
// Requires the possibility to associate custom data with events and to
// store them.
}
timeline.events.push(e);
}
Err(e) => {
warn!("Error deserializing event {:?}", e);
}
}
timeline.events.push(event);
}
Ok(timeline)
@ -552,19 +539,17 @@ impl BaseClient {
#[allow(clippy::type_complexity)]
fn handle_invited_state(
&self,
events: Vec<Raw<AnyStrippedStateEvent>>,
events: &[Raw<AnyStrippedStateEvent>],
room_info: &mut RoomInfo,
) -> (
InviteState,
BTreeMap<UserId, StrippedMemberEvent>,
BTreeMap<String, BTreeMap<String, AnyStrippedStateEvent>>,
) {
events.into_iter().fold(
(InviteState::default(), BTreeMap::new(), BTreeMap::new()),
|(mut state, mut members, mut state_events), e| {
events.iter().fold(
(BTreeMap::new(), BTreeMap::new()),
|(mut members, mut state_events), e| {
match e.deserialize() {
Ok(e) => {
state.events.push(e.clone());
if let AnyStrippedStateEvent::RoomMember(member) = e {
match StrippedMemberEvent::try_from(member) {
@ -591,7 +576,7 @@ impl BaseClient {
);
}
}
(state, members, state_events)
(members, state_events)
},
)
}
@ -600,10 +585,9 @@ impl BaseClient {
&self,
changes: &mut StateChanges,
ambiguity_cache: &mut AmbiguityCache,
events: Vec<Raw<AnySyncStateEvent>>,
events: &[Raw<AnySyncStateEvent>],
room_info: &mut RoomInfo,
) -> StoreResult<(State, BTreeSet<UserId>)> {
let mut state = State::default();
) -> StoreResult<BTreeSet<UserId>> {
let mut members = BTreeMap::new();
let mut state_events = BTreeMap::new();
let mut user_ids = BTreeSet::new();
@ -611,21 +595,19 @@ impl BaseClient {
let room_id = room_info.room_id.clone();
for event in
events
.into_iter()
.filter_map(|e| match hoist_and_deserialize_state_event(&e) {
Ok(e) => Some(e),
Err(err) => {
warn!(
"Couldn't deserialize state event for room {}: {:?} {:#?}",
room_id, err, e
);
None
}
})
for event in events
.iter()
.filter_map(|e| match hoist_and_deserialize_state_event(&e) {
Ok(e) => Some(e),
Err(err) => {
warn!(
"Couldn't deserialize state event for room {}: {:?} {:#?}",
room_id, err, e
);
None
}
})
{
state.events.push(event.clone());
room_info.handle_state_event(&event.content());
if let AnySyncStateEvent::RoomMember(member) = event {
@ -667,7 +649,7 @@ impl BaseClient {
changes.profiles.insert(room_id.as_ref().clone(), profiles);
changes.state.insert(room_id.as_ref().clone(), state_events);
Ok((state, user_ids))
Ok(user_ids)
}
async fn handle_room_account_data(
@ -675,22 +657,16 @@ impl BaseClient {
room_id: &RoomId,
events: &[Raw<AnyBasicEvent>],
changes: &mut StateChanges,
) -> AccountData {
) {
let events: Vec<AnyBasicEvent> =
events.iter().filter_map(|e| e.deserialize().ok()).collect();
for event in &events {
changes.add_room_account_data(room_id, event.clone());
}
AccountData { events }
}
async fn handle_account_data(
&self,
events: Vec<Raw<AnyBasicEvent>>,
changes: &mut StateChanges,
) {
async fn handle_account_data(&self, events: &[Raw<AnyBasicEvent>], changes: &mut StateChanges) {
let events: Vec<AnyBasicEvent> =
events.iter().filter_map(|e| e.deserialize().ok()).collect();
@ -769,29 +745,17 @@ impl BaseClient {
// decryptes to-device events, but leaves room events alone.
// This makes sure that we have the deryption keys for the room
// events at hand.
o.receive_sync_changes(&to_device, &device_lists, &device_one_time_keys_count)
o.receive_sync_changes(to_device, &device_lists, &device_one_time_keys_count)
.await?
} else {
to_device
.events
.into_iter()
.filter_map(|e| e.deserialize().ok())
.collect::<Vec<AnyToDeviceEvent>>()
.into()
}
};
#[cfg(not(feature = "encryption"))]
let to_device = to_device
.events
.into_iter()
.filter_map(|e| e.deserialize().ok())
.collect::<Vec<AnyToDeviceEvent>>()
.into();
let mut changes = StateChanges::new(next_batch.clone());
let mut ambiguity_cache = AmbiguityCache::new(self.store.clone());
self.handle_account_data(account_data.events, &mut changes)
self.handle_account_data(&account_data.events, &mut changes)
.await;
let push_rules = self.get_push_rules(&changes).await?;
@ -809,11 +773,11 @@ impl BaseClient {
room_info.update_summary(&new_info.summary);
room_info.set_prev_batch(new_info.timeline.prev_batch.as_deref());
let (state, mut user_ids) = self
let mut user_ids = self
.handle_state(
&mut changes,
&mut ambiguity_cache,
new_info.state.events,
&new_info.state.events,
&mut room_info,
)
.await?;
@ -834,8 +798,7 @@ impl BaseClient {
)
.await?;
let account_data = self
.handle_room_account_data(&room_id, &new_info.account_data.events, &mut changes)
self.handle_room_account_data(&room_id, &new_info.account_data.events, &mut changes)
.await;
#[cfg(feature = "encryption")]
@ -859,18 +822,15 @@ impl BaseClient {
let notification_count = new_info.unread_notifications.into();
room_info.update_notification_count(notification_count);
let ephemeral = Ephemeral {
events: new_info
.ephemeral
.events
.into_iter()
.filter_map(|e| e.deserialize().ok())
.collect(),
};
new_rooms.join.insert(
room_id,
JoinedRoom::new(timeline, state, account_data, ephemeral, notification_count),
JoinedRoom::new(
timeline,
new_info.state,
new_info.account_data,
new_info.ephemeral,
notification_count,
),
);
changes.add_room(room_info);
@ -884,11 +844,11 @@ impl BaseClient {
let mut room_info = room.clone_info();
room_info.mark_as_left();
let (state, mut user_ids) = self
let mut user_ids = self
.handle_state(
&mut changes,
&mut ambiguity_cache,
new_info.state.events,
&new_info.state.events,
&mut room_info,
)
.await?;
@ -905,14 +865,14 @@ impl BaseClient {
)
.await?;
let account_data = self
.handle_room_account_data(&room_id, &new_info.account_data.events, &mut changes)
self.handle_room_account_data(&room_id, &new_info.account_data.events, &mut changes)
.await;
changes.add_room(room_info);
new_rooms
.leave
.insert(room_id, LeftRoom::new(timeline, state, account_data));
new_rooms.leave.insert(
room_id,
LeftRoom::new(timeline, new_info.state, new_info.account_data),
);
}
for (room_id, new_info) in rooms.invite {
@ -929,31 +889,25 @@ impl BaseClient {
let room = self.store.get_or_create_stripped_room(&room_id).await;
let mut room_info = room.clone_info();
let (state, members, state_events) =
self.handle_invited_state(new_info.invite_state.events, &mut room_info);
let (members, state_events) =
self.handle_invited_state(&new_info.invite_state.events, &mut room_info);
changes.stripped_members.insert(room_id.clone(), members);
changes.stripped_state.insert(room_id.clone(), state_events);
changes.add_stripped_room(room_info);
let room = InvitedRoom {
invite_state: state,
};
new_rooms.invite.insert(room_id, room);
new_rooms.invite.insert(room_id, new_info);
}
let presence: BTreeMap<UserId, PresenceEvent> = presence
changes.presence = presence
.events
.into_iter()
.iter()
.filter_map(|e| {
let event = e.deserialize().ok()?;
Some((event.sender.clone(), event))
})
.collect();
changes.presence = presence;
changes.ambiguity_maps = ambiguity_cache.cache;
self.store.save_changes(&changes).await?;
@ -965,12 +919,8 @@ impl BaseClient {
let response = SyncResponse {
next_batch,
rooms: new_rooms,
presence: Presence {
events: changes.presence.into_iter().map(|(_, v)| v).collect(),
},
account_data: AccountData {
events: changes.account_data.into_iter().map(|(_, e)| e).collect(),
},
presence,
account_data,
to_device,
device_lists,
device_one_time_keys_count: device_one_time_keys_count

View File

@ -1,3 +1,10 @@
use ruma::{
api::client::r0::sync::sync_events::{
AccountData, Ephemeral, InvitedRoom, Presence, State, ToDevice,
},
serde::Raw,
DeviceIdBox,
};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, convert::TryFrom, time::SystemTime};
@ -9,9 +16,8 @@ use super::{
},
},
events::{
presence::PresenceEvent, room::member::MemberEventContent, AnyBasicEvent,
AnyStrippedStateEvent, AnySyncEphemeralRoomEvent, AnySyncRoomEvent, AnySyncStateEvent,
AnyToDeviceEvent, StateEvent, StrippedStateEvent, SyncStateEvent, Unsigned,
room::member::MemberEventContent, AnySyncRoomEvent, StateEvent, StrippedStateEvent,
SyncStateEvent, Unsigned,
},
identifiers::{DeviceKeyAlgorithm, EventId, RoomId, UserId},
};
@ -37,6 +43,72 @@ pub struct AmbiguityChanges {
pub changes: BTreeMap<RoomId, BTreeMap<EventId, AmbiguityChange>>,
}
/// The verification state of the device that sent an event to us.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum VerificationState {
/// The device is trusted.
Trusted,
/// The device is not trusted.
Untrusted,
/// The device is not known to us.
UnknownDevice,
}
/// The algorithm specific information of a decrypted event.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum AlgorithmInfo {
/// The info if the event was encrypted using m.megolm.v1.aes-sha2
MegolmV1AesSha2 {
/// The curve25519 key of the device that created the megolm decryption
/// key originally.
curve25519_key: String,
/// The signing keys that have created the megolm key that was used to
/// decrypt this session. This map will usually contain a signle ed25519
/// key.
sender_claimed_keys: BTreeMap<DeviceKeyAlgorithm, String>,
/// Chain of curve25519 keys through which this session was forwarded,
/// via m.forwarded_room_key events.
forwarding_curve25519_key_chain: Vec<String>,
},
}
/// Struct containing information on how an event was decrypted.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EncryptionInfo {
/// The user ID of the event sender, note this is untrusted data unless the
/// `verification_state` is as well trusted.
pub sender: UserId,
/// The device ID of the device that sent us the event, note this is
/// untrusted data unless `verification_state` is as well trusted.
pub sender_device: DeviceIdBox,
/// Information about the algorithm that was used to encrypt the event.
pub algorithm_info: AlgorithmInfo,
/// The verification state of the device that sent us the event, note this
/// is the state of the device at the time of decryption. It may change in
/// the future if a device gets verified or deleted.
pub verification_state: VerificationState,
}
/// A customized version of a room event comming from a sync that holds optional
/// decryption info.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SyncRoomEvent {
/// The actual event.
pub event: Raw<AnySyncRoomEvent>,
/// The encryption info about the event. Will be `None` if the event was not
/// encrypted.
pub encryption_info: Option<EncryptionInfo>,
}
impl From<Raw<AnySyncRoomEvent>> for SyncRoomEvent {
fn from(inner: Raw<AnySyncRoomEvent>) -> Self {
Self {
encryption_info: None,
event: inner,
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct SyncResponse {
/// The batch token to supply in the `since` param of the next `/sync` request.
@ -71,33 +143,6 @@ impl SyncResponse {
}
}
/// Updates to the presence status of other users.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Presence {
/// A list of events.
pub events: Vec<PresenceEvent>,
}
/// Data that the user has attached to either the account or a specific room.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AccountData {
/// The list of account data events.
pub events: Vec<AnyBasicEvent>,
}
/// Messages sent dirrectly between devices.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ToDevice {
/// A list of events.
pub events: Vec<AnyToDeviceEvent>,
}
impl From<Vec<AnyToDeviceEvent>> for ToDevice {
fn from(events: Vec<AnyToDeviceEvent>) -> Self {
Self { events }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Rooms {
/// The rooms that the user has left or been banned from.
@ -144,20 +189,6 @@ impl JoinedRoom {
}
}
/// Updates to the rooms that the user has been invited to.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct InvitedRoom {
/// The state of a room that the user has been invited to.
pub invite_state: InviteState,
}
/// The state of a room that the user has been invited to.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct InviteState {
/// A list of state events.
pub events: Vec<AnyStrippedStateEvent>,
}
/// Counts of unread notifications for a room.
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub struct UnreadNotificationsCount {
@ -179,13 +210,6 @@ impl From<RumaUnreadNotificationsCount> for UnreadNotificationsCount {
}
}
/// The ephemeral events in the room that aren't recorded in the timeline or
/// state of the room. e.g. typing.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Ephemeral {
pub events: Vec<AnySyncEphemeralRoomEvent>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LeftRoom {
/// The timeline of messages and state changes in the room up to the point
@ -220,7 +244,7 @@ pub struct Timeline {
pub prev_batch: Option<String>,
/// A list of events.
pub events: Vec<AnySyncRoomEvent>,
pub events: Vec<SyncRoomEvent>,
}
impl Timeline {
@ -233,13 +257,6 @@ impl Timeline {
}
}
/// State events in the room.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct State {
/// A list of state events.
pub events: Vec<AnySyncStateEvent>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(
try_from = "SyncStateEvent<MemberEventContent>",

View File

@ -27,14 +27,13 @@ use matrix_sdk_common::{
upload_keys,
upload_signatures::Request as UploadSignaturesRequest,
},
sync::sync_events::{DeviceLists, ToDevice as RumaToDevice},
sync::sync_events::{DeviceLists, ToDevice},
},
assign,
deserialized_responses::ToDevice,
deserialized_responses::{AlgorithmInfo, EncryptionInfo, SyncRoomEvent, VerificationState},
events::{
room::encrypted::EncryptedEventContent, room_key::RoomKeyEventContent,
AnyMessageEventContent, AnySyncRoomEvent, AnyToDeviceEvent, SyncMessageEvent,
ToDeviceEvent,
AnyMessageEventContent, AnyToDeviceEvent, SyncMessageEvent, ToDeviceEvent,
},
identifiers::{
DeviceId, DeviceIdBox, DeviceKeyAlgorithm, EventEncryptionAlgorithm, EventId, RoomId,
@ -42,7 +41,7 @@ use matrix_sdk_common::{
},
locks::Mutex,
uuid::Uuid,
Raw, UInt,
UInt,
};
#[cfg(feature = "sled_cryptostore")]
@ -802,7 +801,7 @@ impl OlmMachine {
/// [`decrypt_room_event`]: #method.decrypt_room_event
pub async fn receive_sync_changes(
&self,
to_device_events: &RumaToDevice,
to_device_events: ToDevice,
changed_devices: &DeviceLists,
one_time_keys_counts: &BTreeMap<DeviceKeyAlgorithm, UInt>,
) -> OlmResult<ToDevice> {
@ -826,14 +825,14 @@ impl OlmMachine {
let mut events = Vec::new();
for event_result in &to_device_events.events {
let mut event = match event_result.deserialize() {
for mut raw_event in to_device_events.events {
let event = match raw_event.deserialize() {
Ok(e) => e,
Err(e) => {
// Skip invalid events.
warn!(
"Received an invalid to-device event {:?} {:?}",
e, event_result
e, raw_event
);
continue;
}
@ -841,9 +840,9 @@ impl OlmMachine {
info!("Received a to-device event {:?}", event);
match &mut event {
match event {
AnyToDeviceEvent::RoomEncrypted(e) => {
let decrypted = match self.decrypt_to_device_event(e).await {
let decrypted = match self.decrypt_to_device_event(&e).await {
Ok(e) => e,
Err(err) => {
warn!(
@ -885,12 +884,10 @@ impl OlmMachine {
changes.inbound_group_sessions.push(group_session);
}
if let Some(e) = decrypted.deserialized_event {
event = e;
}
raw_event = decrypted.event;
}
AnyToDeviceEvent::RoomKeyRequest(e) => {
self.key_request_machine.receive_incoming_key_request(e)
self.key_request_machine.receive_incoming_key_request(&e)
}
AnyToDeviceEvent::KeyVerificationAccept(..)
| AnyToDeviceEvent::KeyVerificationCancel(..)
@ -903,7 +900,7 @@ impl OlmMachine {
_ => continue,
}
events.push(event);
events.push(raw_event);
}
let changed_sessions = self
@ -915,7 +912,10 @@ impl OlmMachine {
self.store.save_changes(changes).await?;
Ok(ToDevice { events })
let mut to_device = ToDevice::new();
to_device.events = events;
Ok(to_device)
}
/// Request a room key from our devices.
@ -950,6 +950,44 @@ impl OlmMachine {
.await?)
}
async fn get_encryption_info(
&self,
session: &InboundGroupSession,
sender: &UserId,
device_id: &DeviceId,
) -> StoreResult<EncryptionInfo> {
let verification_state = if let Some(device) =
self.get_device(sender, device_id).await?.filter(|d| {
d.get_key(DeviceKeyAlgorithm::Curve25519)
.map(|k| k == session.sender_key())
.unwrap_or(false)
}) {
if (self.user_id() == device.user_id() && self.device_id() == device.device_id())
|| device.is_trusted()
{
VerificationState::Trusted
} else {
VerificationState::Untrusted
}
} else {
VerificationState::UnknownDevice
};
let sender = sender.clone();
let device_id = device_id.to_owned();
Ok(EncryptionInfo {
sender,
sender_device: device_id,
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
curve25519_key: session.sender_key().to_owned(),
sender_claimed_keys: session.signing_keys().to_owned(),
forwarding_curve25519_key_chain: session.forwading_key_chain().to_vec(),
},
verification_state,
})
}
/// Decrypt an event from a room timeline.
///
/// # Arguments
@ -961,7 +999,7 @@ impl OlmMachine {
&self,
event: &SyncMessageEvent<EncryptedEventContent>,
room_id: &RoomId,
) -> MegolmResult<Raw<AnySyncRoomEvent>> {
) -> MegolmResult<SyncRoomEvent> {
let content = match &event.content {
EncryptedEventContent::MegolmV1AesSha2(c) => c,
_ => return Err(EventError::UnsupportedAlgorithm.into()),
@ -989,8 +1027,6 @@ impl OlmMachine {
"Successfully decrypted a Megolm event {:?}",
decrypted_event
);
// TODO set the encryption info on the event (is it verified, was it
// decrypted, sender key...)
if let Ok(e) = decrypted_event.deserialize() {
self.verification_machine
@ -998,7 +1034,14 @@ impl OlmMachine {
.await?;
}
Ok(decrypted_event)
let encryption_info = self
.get_encryption_info(&session, &event.sender, &content.device_id)
.await?;
Ok(SyncRoomEvent {
encryption_info: Some(encryption_info),
event: decrypted_event,
})
}
/// Update the tracked users.
@ -1815,23 +1858,24 @@ pub(crate) mod test {
.decrypt_room_event(&event, &room_id)
.await
.unwrap()
.event
.deserialize()
.unwrap();
match decrypted_event {
AnySyncRoomEvent::Message(AnySyncMessageEvent::RoomMessage(SyncMessageEvent {
sender,
content,
..
})) => {
assert_eq!(&sender, alice.user_id());
if let MessageType::Text(c) = &content.msgtype {
assert_eq!(&c.body, plaintext);
} else {
panic!("Decrypted event has a missmatched content");
}
if let AnySyncRoomEvent::Message(AnySyncMessageEvent::RoomMessage(SyncMessageEvent {
sender,
content,
..
})) = decrypted_event
{
assert_eq!(&sender, alice.user_id());
if let MessageType::Text(c) = &content.msgtype {
assert_eq!(&c.body, plaintext);
} else {
panic!("Decrypted event has a missmatched content");
}
_ => panic!("Decrypted room event has the wrong type"),
} else {
panic!("Decrypted room event has the wrong type")
}
}

View File

@ -61,9 +61,9 @@ pub struct InboundGroupSession {
session_id: Arc<str>,
first_known_index: u32,
pub(crate) sender_key: Arc<str>,
pub(crate) signing_key: Arc<BTreeMap<DeviceKeyAlgorithm, String>>,
pub(crate) signing_keys: Arc<BTreeMap<DeviceKeyAlgorithm, String>>,
pub(crate) room_id: Arc<RoomId>,
forwarding_chains: Arc<Mutex<Option<Vec<String>>>>,
forwarding_chains: Arc<Vec<String>>,
imported: Arc<bool>,
}
@ -104,10 +104,10 @@ impl InboundGroupSession {
history_visibility: history_visibility.into(),
sender_key: sender_key.to_owned().into(),
first_known_index,
signing_key: Arc::new(keys),
room_id: Arc::new(room_id.clone()),
forwarding_chains: Arc::new(Mutex::new(None)),
imported: Arc::new(false),
signing_keys: keys.into(),
room_id: room_id.clone().into(),
forwarding_chains: Vec::new().into(),
imported: false.into(),
})
}
@ -152,15 +152,15 @@ impl InboundGroupSession {
);
Ok(InboundGroupSession {
inner: Arc::new(Mutex::new(session)),
inner: Mutex::new(session).into(),
session_id: content.session_id.as_str().into(),
sender_key: content.sender_key.as_str().into(),
first_known_index,
history_visibility: None.into(),
signing_key: Arc::new(sender_claimed_key),
room_id: Arc::new(content.room_id.clone()),
forwarding_chains: Arc::new(Mutex::new(Some(forwarding_chains))),
imported: Arc::new(true),
signing_keys: sender_claimed_key.into(),
room_id: content.room_id.clone().into(),
forwarding_chains: forwarding_chains.into(),
imported: true.into(),
})
}
@ -176,9 +176,9 @@ impl InboundGroupSession {
PickledInboundGroupSession {
pickle: InboundGroupSessionPickle::from(pickle),
sender_key: self.sender_key.to_string(),
signing_key: (&*self.signing_key).clone(),
signing_key: (&*self.signing_keys).clone(),
room_id: (&*self.room_id).clone(),
forwarding_chains: self.forwarding_chains.lock().await.clone(),
forwarding_chains: self.forwading_key_chain().to_vec(),
imported: *self.imported,
history_visibility: self.history_visibility.as_ref().clone(),
}
@ -197,6 +197,20 @@ impl InboundGroupSession {
&self.sender_key
}
/// Get the map of signing keys this session was received from.
pub fn signing_keys(&self) -> &BTreeMap<DeviceKeyAlgorithm, String> {
&self.signing_keys
}
/// Get the list of ed25519 keys that this session was forwarded through.
///
/// Each ed25519 key represents a single device. If device A forwards the
/// session to device B and device B to C this list will contain the ed25519
/// keys of A and B.
pub fn forwading_key_chain(&self) -> &[String] {
&self.forwarding_chains
}
/// Export this session at the given message index.
pub async fn export_at_index(&self, message_index: u32) -> ExportedRoomKey {
let message_index = std::cmp::max(self.first_known_index(), message_index);
@ -214,14 +228,8 @@ impl InboundGroupSession {
room_id: (&*self.room_id).clone(),
sender_key: (&*self.sender_key).to_owned(),
session_id: self.session_id().to_owned(),
forwarding_curve25519_key_chain: self
.forwarding_chains
.lock()
.await
.as_ref()
.cloned()
.unwrap_or_default(),
sender_claimed_keys: (&*self.signing_key).clone(),
forwarding_curve25519_key_chain: self.forwading_key_chain().to_vec(),
sender_claimed_keys: (&*self.signing_keys).clone(),
session_key,
}
}
@ -246,15 +254,15 @@ impl InboundGroupSession {
let session_id = session.session_id();
Ok(InboundGroupSession {
inner: Arc::new(Mutex::new(session)),
inner: Mutex::new(session).into(),
session_id: session_id.into(),
sender_key: pickle.sender_key.into(),
history_visibility: pickle.history_visibility.into(),
first_known_index,
signing_key: Arc::new(pickle.signing_key),
room_id: Arc::new(pickle.room_id),
forwarding_chains: Arc::new(Mutex::new(pickle.forwarding_chains)),
imported: Arc::new(pickle.imported),
signing_keys: pickle.signing_key.into(),
room_id: pickle.room_id.into(),
forwarding_chains: pickle.forwarding_chains.into(),
imported: pickle.imported.into(),
})
}
@ -379,7 +387,8 @@ pub struct PickledInboundGroupSession {
pub room_id: RoomId,
/// The list of claimed ed25519 that forwarded us this key. Will be None if
/// we dirrectly received this session.
pub forwarding_chains: Option<Vec<String>>,
#[serde(default)]
pub forwarding_chains: Vec<String>,
/// Flag remembering if the session was dirrectly sent to us by the sender
/// or if it was imported.
pub imported: bool,
@ -411,21 +420,15 @@ impl TryFrom<ExportedRoomKey> for InboundGroupSession {
let session = OlmInboundGroupSession::import(&key.session_key.0)?;
let first_known_index = session.first_known_index();
let forwarding_chains = if key.forwarding_curve25519_key_chain.is_empty() {
None
} else {
Some(key.forwarding_curve25519_key_chain)
};
Ok(InboundGroupSession {
inner: Arc::new(Mutex::new(session)),
session_id: key.session_id.into(),
sender_key: key.sender_key.into(),
history_visibility: None.into(),
first_known_index,
signing_key: Arc::new(key.sender_claimed_keys),
signing_keys: Arc::new(key.sender_claimed_keys),
room_id: Arc::new(key.room_id),
forwarding_chains: Arc::new(Mutex::new(forwarding_chains)),
forwarding_chains: Arc::new(key.forwarding_curve25519_key_chain),
imported: Arc::new(true),
})
}