@@ -21,9 +21,9 @@ use std::{
2121use itertools:: Itertools ;
2222use matrix_sdk_common:: {
2323 deserialized_responses:: {
24- AlgorithmInfo , DecryptedRoomEvent , DeviceLinkProblem , EncryptionInfo , UnableToDecryptInfo ,
25- UnableToDecryptReason , UnsignedDecryptionResult , UnsignedEventLocation , VerificationLevel ,
26- VerificationState ,
24+ AlgorithmInfo , DecryptedRoomEvent , DeviceLinkProblem , EncryptionInfo ,
25+ ProcessedToDeviceEvent , UnableToDecryptInfo , UnableToDecryptReason ,
26+ UnsignedDecryptionResult , UnsignedEventLocation , VerificationLevel , VerificationState ,
2727 } ,
2828 locks:: RwLock as StdRwLock ,
2929 BoxFuture ,
@@ -1286,16 +1286,15 @@ impl OlmMachine {
12861286 transaction : & mut StoreTransaction ,
12871287 changes : & mut Changes ,
12881288 mut raw_event : Raw < AnyToDeviceEvent > ,
1289- ) -> Option < Raw < AnyToDeviceEvent > > {
1289+ ) -> Option < ProcessedToDeviceEvent > {
12901290 Self :: record_message_id ( & raw_event) ;
12911291
12921292 let event: ToDeviceEvents = match raw_event. deserialize_as ( ) {
12931293 Ok ( e) => e,
12941294 Err ( e) => {
12951295 // Skip invalid events.
12961296 warn ! ( "Received an invalid to-device event: {e}" ) ;
1297-
1298- return Some ( raw_event) ;
1297+ return Some ( ProcessedToDeviceEvent :: NotProcessed ( raw_event) ) ;
12991298 }
13001299 } ;
13011300
@@ -1320,7 +1319,7 @@ impl OlmMachine {
13201319 }
13211320 }
13221321
1323- return Some ( raw_event) ;
1322+ return Some ( ProcessedToDeviceEvent :: UnableToDecrypt { event : raw_event } ) ;
13241323 }
13251324 } ;
13261325
@@ -1372,12 +1371,75 @@ impl OlmMachine {
13721371 raw_event = decrypted. result . raw_event ;
13731372 }
13741373 }
1374+
1375+ let encryption_info =
1376+ self . get_olm_encryption_info ( & e. sender , decrypted. result . sender_key ) . await ;
1377+
1378+ Some ( ProcessedToDeviceEvent :: Decrypted {
1379+ decrypted_event : raw_event,
1380+ encryption_info,
1381+ } )
13751382 }
13761383
1377- e => self . handle_to_device_event ( changes, & e) . await ,
1384+ e => {
1385+ self . handle_to_device_event ( changes, & e) . await ;
1386+ Some ( ProcessedToDeviceEvent :: PlainText ( raw_event) )
1387+ }
13781388 }
1389+ }
13791390
1380- Some ( raw_event)
1391+ /// Get the sender information for a successfully decrypted olm message.
1392+ ///
1393+ /// # Arguments
1394+ ///
1395+ /// * `sender` - The claimed user_id retrieved from the event.
1396+ ///
1397+ /// * `sender_key` - The `Curve25519PublicKey` linked to the olm session
1398+ /// that decrypted the message.
1399+ ///
1400+ /// # Returns
1401+ ///
1402+ /// A [`EncryptionInfo`] struct.
1403+ async fn get_olm_encryption_info (
1404+ & self ,
1405+ sender : & UserId ,
1406+ sender_key : Curve25519PublicKey ,
1407+ ) -> EncryptionInfo {
1408+ let device =
1409+ self . store ( ) . get_device_from_curve_key ( sender, sender_key) . await . unwrap_or ( None ) ;
1410+
1411+ let state = if let Some ( device) = & device {
1412+ if device. is_cross_signed_by_owner ( ) {
1413+ if device. is_device_owner_verified ( ) {
1414+ VerificationState :: Verified
1415+ } else {
1416+ let identity = device
1417+ . device_owner_identity
1418+ . as_ref ( )
1419+ . expect ( "This device is cross-signed, so the identity exists" ) ;
1420+ if identity. was_previously_verified ( ) {
1421+ VerificationState :: Unverified ( VerificationLevel :: VerificationViolation )
1422+ } else {
1423+ VerificationState :: Unverified ( VerificationLevel :: UnverifiedIdentity )
1424+ }
1425+ }
1426+ } else {
1427+ VerificationState :: Unverified ( VerificationLevel :: UnsignedDevice )
1428+ }
1429+ } else {
1430+ VerificationState :: Unverified ( VerificationLevel :: None ( DeviceLinkProblem :: MissingDevice ) )
1431+ } ;
1432+
1433+ EncryptionInfo {
1434+ sender : sender. to_owned ( ) ,
1435+ sender_device : device. map ( |d| d. device_id ( ) . to_owned ( ) ) ,
1436+ algorithm_info : AlgorithmInfo :: OlmV1Curve25519AesSha2 {
1437+ curve25519_key : sender_key. to_base64 ( ) ,
1438+ } ,
1439+ verification_state : state,
1440+ // Only relevant for megolm
1441+ session_id : None ,
1442+ }
13811443 }
13821444
13831445 /// Decide whether a decrypted to-device event was sent from a dehydrated
@@ -1435,7 +1497,7 @@ impl OlmMachine {
14351497 pub async fn receive_sync_changes (
14361498 & self ,
14371499 sync_changes : EncryptionSyncChanges < ' _ > ,
1438- ) -> OlmResult < ( Vec < Raw < AnyToDeviceEvent > > , Vec < RoomKeyInfo > ) > {
1500+ ) -> OlmResult < ( Vec < ProcessedToDeviceEvent > , Vec < RoomKeyInfo > ) > {
14391501 let mut store_transaction = self . inner . store . transaction ( ) . await ;
14401502
14411503 let ( events, changes) =
@@ -1464,10 +1526,18 @@ impl OlmMachine {
14641526 & self ,
14651527 transaction : & mut StoreTransaction ,
14661528 sync_changes : EncryptionSyncChanges < ' _ > ,
1467- ) -> OlmResult < ( Vec < Raw < AnyToDeviceEvent > > , Changes ) > {
1529+ ) -> OlmResult < ( Vec < ProcessedToDeviceEvent > , Changes ) > {
14681530 // Remove verification objects that have expired or are done.
1469- let mut events = self . inner . verification_machine . garbage_collect ( ) ;
1470-
1531+ let mut events: Vec < ProcessedToDeviceEvent > = self
1532+ . inner
1533+ . verification_machine
1534+ . garbage_collect ( )
1535+ . iter ( )
1536+ // These are `fake` to device events just serving as local echo
1537+ // in order for own client to react quickly to cancelled transaction.
1538+ // Just use PlainText for that.
1539+ . map ( |e| ProcessedToDeviceEvent :: PlainText ( e. clone ( ) ) )
1540+ . collect ( ) ;
14711541 // The account is automatically saved by the store transaction created by the
14721542 // caller.
14731543 let mut changes = Default :: default ( ) ;
0 commit comments