diff --git a/bindings/matrix-sdk-crypto-ffi/Cargo.toml b/bindings/matrix-sdk-crypto-ffi/Cargo.toml index a40c755df..01b3b01a5 100644 --- a/bindings/matrix-sdk-crypto-ffi/Cargo.toml +++ b/bindings/matrix-sdk-crypto-ffi/Cargo.toml @@ -57,7 +57,7 @@ features = ["rt-multi-thread"] [dependencies.vodozemac] git = "https://github.com/matrix-org/vodozemac/" -rev = "18bcbc3359298894415931547ea41abb75af2d4a" +rev = "779e85258ec83b10ae62ec4d848188b188aa7b42" [build-dependencies] uniffi_build = { version = "0.18.0", features = ["builtin-bindgen"] } diff --git a/bindings/matrix-sdk-crypto-js/Cargo.toml b/bindings/matrix-sdk-crypto-js/Cargo.toml index 2be13841a..a7dc906fb 100644 --- a/bindings/matrix-sdk-crypto-js/Cargo.toml +++ b/bindings/matrix-sdk-crypto-js/Cargo.toml @@ -41,5 +41,5 @@ tracing-subscriber = { version = "0.3.14", default-features = false, features = [dependencies.vodozemac] git = "https://github.com/matrix-org/vodozemac/" -rev = "18bcbc3359298894415931547ea41abb75af2d4a" +rev = "779e85258ec83b10ae62ec4d848188b188aa7b42" features = ["js"] diff --git a/bindings/matrix-sdk-crypto-nodejs/Cargo.toml b/bindings/matrix-sdk-crypto-nodejs/Cargo.toml index 2e494cf43..8de25438e 100644 --- a/bindings/matrix-sdk-crypto-nodejs/Cargo.toml +++ b/bindings/matrix-sdk-crypto-nodejs/Cargo.toml @@ -36,7 +36,7 @@ tracing-subscriber = { version = "0.3", default-features = false, features = ["t [dependencies.vodozemac] git = "https://github.com/matrix-org/vodozemac/" -rev = "18bcbc3359298894415931547ea41abb75af2d4a" +rev = "779e85258ec83b10ae62ec4d848188b188aa7b42" features = ["js"] [build-dependencies] diff --git a/crates/matrix-sdk-crypto/Cargo.toml b/crates/matrix-sdk-crypto/Cargo.toml index f4996eeb4..f527441cc 100644 --- a/crates/matrix-sdk-crypto/Cargo.toml +++ b/crates/matrix-sdk-crypto/Cargo.toml @@ -62,11 +62,11 @@ features = ["client-api-c", "rand", "canonical-json", "unstable-msc2676", "unsta [target.'cfg(not(target_arch = "wasm32"))'.dependencies.vodozemac] git = "https://github.com/matrix-org/vodozemac/" -rev = "18bcbc3359298894415931547ea41abb75af2d4a" +rev = "779e85258ec83b10ae62ec4d848188b188aa7b42" [target.'cfg(target_arch = "wasm32")'.dependencies.vodozemac] git = "https://github.com/matrix-org/vodozemac/" -rev = "18bcbc3359298894415931547ea41abb75af2d4a" +rev = "779e85258ec83b10ae62ec4d848188b188aa7b42" features = ["js"] [dev-dependencies] diff --git a/crates/matrix-sdk-crypto/src/gossiping/machine.rs b/crates/matrix-sdk-crypto/src/gossiping/machine.rs index 098a8d64b..2aedf1a40 100644 --- a/crates/matrix-sdk-crypto/src/gossiping/machine.rs +++ b/crates/matrix-sdk-crypto/src/gossiping/machine.rs @@ -36,7 +36,7 @@ use ruma::{ OwnedUserId, RoomId, TransactionId, UserId, }; use tracing::{debug, info, trace, warn}; -use vodozemac::Curve25519PublicKey; +use vodozemac::{megolm::SessionOrdering, Curve25519PublicKey}; use super::{GossipRequest, KeyForwardDecision, RequestEvent, RequestInfo, SecretInfo, WaitQueue}; use crate::{ @@ -899,10 +899,7 @@ impl GossipMachine { // If we have a previous session, check if we have a better version // and store the new one if so. let session = if let Some(old_session) = old_session { - let first_old_index = old_session.first_known_index(); - let first_index = session.first_known_index(); - - if first_old_index > first_index { + if session.compare(&old_session).await == SessionOrdering::Better { self.mark_as_done(info).await?; Some(session) } else { diff --git a/crates/matrix-sdk-crypto/src/machine.rs b/crates/matrix-sdk-crypto/src/machine.rs index 075a35419..6870c589f 100644 --- a/crates/matrix-sdk-crypto/src/machine.rs +++ b/crates/matrix-sdk-crypto/src/machine.rs @@ -43,7 +43,7 @@ use ruma::{ }; use serde_json::{value::to_raw_value, Value}; use tracing::{debug, error, info, trace, warn}; -use vodozemac::{Curve25519PublicKey, Ed25519Signature}; +use vodozemac::{megolm::SessionOrdering, Curve25519PublicKey, Ed25519Signature}; #[cfg(feature = "backups_v1")] use crate::backups::BackupMachine; @@ -1330,45 +1330,18 @@ impl OlmMachine { #[allow(unused_variables)] from_backup: bool, progress_listener: impl Fn(usize, usize), ) -> StoreResult { - type SessionIdToIndexMap = BTreeMap, u32>; - - #[derive(Debug)] - struct ShallowSessions { - inner: BTreeMap, BTreeMap>, - } - - impl ShallowSessions { - fn has_better_session(&self, session: &InboundGroupSession) -> bool { - self.inner - .get(&session.room_id) - .and_then(|m| { - m.get(&session.sender_key.to_base64()).and_then(|m| { - m.get(&session.session_id) - .map(|existing| existing <= &session.first_known_index()) - }) - }) - .unwrap_or(false) - } - } - let mut sessions = Vec::new(); - let existing_sessions = ShallowSessions { - inner: self.store.get_inbound_group_sessions().await?.into_iter().fold( - BTreeMap::new(), - |mut acc, s| { - let index = s.first_known_index(); - - acc.entry(s.room_id) - .or_default() - .entry(s.sender_key.to_base64()) - .or_default() - .insert(s.session_id, index); - - acc - }, - ), - }; + async fn new_session_better( + session: &InboundGroupSession, + old_session: Option, + ) -> bool { + if let Some(old_session) = &old_session { + session.compare(old_session).await == SessionOrdering::Better + } else { + true + } + } let total_count = exported_keys.len(); let mut keys = BTreeMap::new(); @@ -1376,10 +1349,18 @@ impl OlmMachine { for (i, key) in exported_keys.into_iter().enumerate() { match InboundGroupSession::from_export(&key) { Ok(session) => { - // Only import the session if we didn't have this session or if it's - // a better version of the same session, that is the first known - // index is lower. - if !existing_sessions.has_better_session(&session) { + let old_session = self + .store + .get_inbound_group_session( + session.room_id(), + &session.sender_key.to_base64(), + session.session_id(), + ) + .await?; + + // Only import the session if we didn't have this session or + // if it's a better version of the same session. + if new_session_better(&session, old_session).await { #[cfg(feature = "backups_v1")] if from_backup { session.mark_as_backed_up(); diff --git a/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs b/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs index f2d2f1526..755a56842 100644 --- a/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs +++ b/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs @@ -35,7 +35,7 @@ use serde_json::Value; use vodozemac::{ megolm::{ DecryptedMessage, DecryptionError, ExportedSessionKey, InboundGroupSession as InnerSession, - InboundGroupSessionPickle, MegolmMessage, + InboundGroupSessionPickle, MegolmMessage, SessionOrdering, }, Curve25519PublicKey, PickleError, }; @@ -330,6 +330,13 @@ impl InboundGroupSession { self.first_known_index } + /// Check if the `InboundGroupSession` is better than the given other + /// `InboundGroupSession` + pub async fn compare(&self, other: &InboundGroupSession) -> SessionOrdering { + let mut other = other.inner.lock().await; + self.inner.lock().await.compare(&mut other) + } + /// Decrypt the given ciphertext. /// /// Returns the decrypted plaintext or an `DecryptionError` if diff --git a/crates/matrix-sdk-qrcode/Cargo.toml b/crates/matrix-sdk-qrcode/Cargo.toml index b26375e68..f2532fcc9 100644 --- a/crates/matrix-sdk-qrcode/Cargo.toml +++ b/crates/matrix-sdk-qrcode/Cargo.toml @@ -30,4 +30,4 @@ thiserror = "1.0.30" [dependencies.vodozemac] git = "https://github.com/matrix-org/vodozemac/" -rev = "18bcbc3359298894415931547ea41abb75af2d4a" +rev = "779e85258ec83b10ae62ec4d848188b188aa7b42"