@@ -33,23 +33,23 @@ use ruma::{
33
33
to_device:: DeviceIdOrAllDevices ,
34
34
user_id, DeviceId , OwnedOneTimeKeyId , TransactionId , UserId ,
35
35
} ;
36
+ use serde:: Serialize ;
36
37
use serde_json:: { json, Value } ;
37
38
use tokio:: sync:: Mutex ;
38
39
39
40
use crate :: {
40
41
machine:: tests,
41
42
olm:: PrivateCrossSigningIdentity ,
42
- session_manager:: CollectStrategy ,
43
43
store:: { types:: Changes , CryptoStoreWrapper , MemoryStore } ,
44
44
types:: {
45
- events:: ToDeviceEvent ,
45
+ events:: { room :: encrypted :: ToDeviceEncryptedEventContent , ToDeviceEvent } ,
46
46
requests:: { AnyOutgoingRequest , ToDeviceRequest } ,
47
47
DeviceKeys ,
48
48
} ,
49
49
utilities:: json_convert,
50
50
verification:: VerificationMachine ,
51
- Account , CrossSigningBootstrapRequests , DecryptionSettings , Device , DeviceData ,
52
- EncryptionSyncChanges , OlmMachine , OtherUserIdentityData , TrustRequirement ,
51
+ Account , CollectStrategy , CrossSigningBootstrapRequests , DecryptionSettings , Device ,
52
+ DeviceData , EncryptionSyncChanges , OlmMachine , OtherUserIdentityData , TrustRequirement ,
53
53
} ;
54
54
55
55
/// These keys need to be periodically uploaded to the server.
@@ -227,6 +227,58 @@ pub async fn send_and_receive_encrypted_to_device_test_helper(
227
227
decrypted[ 0 ] . clone ( )
228
228
}
229
229
230
+ /// Encrypt the given event content into the content of an
231
+ /// olm-encrypted to-device event, suppressing the `sender_device_keys` field in
232
+ /// the encrypted content.
233
+ ///
234
+ /// This is much the same as calling [`Device::encrypt`] on the recipient
235
+ /// device, other than the suppression of `sender_device_keys`.
236
+ ///
237
+ /// # Arguments
238
+ ///
239
+ /// * `sender` - The OlmMachine to use to encrypt the event.
240
+ /// * `recipient` - The recipient of the encrypted event.
241
+ /// * `event_type` - The type of the event to encrypt.
242
+ /// * `content` - The content of the event to encrypt.
243
+ pub async fn build_encrypted_to_device_content_without_sender_data (
244
+ sender : & OlmMachine ,
245
+ recipient_device : & DeviceKeys ,
246
+ event_type : & str ,
247
+ content : & impl Serialize ,
248
+ ) -> ToDeviceEncryptedEventContent {
249
+ let sender_store = & sender. inner . store ;
250
+
251
+ let sender_key = recipient_device. curve25519_key ( ) . unwrap ( ) ;
252
+ let sessions = sender_store
253
+ . get_sessions ( & sender_key. to_base64 ( ) )
254
+ . await
255
+ . expect ( "Could not get most recent session" )
256
+ . expect ( "No olm session found" ) ;
257
+ let mut olm_session = sessions. lock ( ) . await . first ( ) . unwrap ( ) . clone ( ) ;
258
+
259
+ let plaintext = serde_json:: to_string ( & json ! ( {
260
+ "sender" : sender. user_id( ) ,
261
+ "sender_device" : sender. device_id( ) ,
262
+ "keys" : { "ed25519" : sender. identity_keys( ) . ed25519. to_base64( ) } ,
263
+ "recipient" : recipient_device. user_id,
264
+ "recipient_keys" : { "ed25519" : recipient_device. ed25519_key( ) . unwrap( ) . to_base64( ) } ,
265
+ "type" : event_type,
266
+ "content" : content,
267
+ } ) )
268
+ . unwrap ( ) ;
269
+
270
+ let ciphertext = olm_session. encrypt_helper ( & plaintext) . await ;
271
+ let content =
272
+ olm_session. build_encrypted_event ( ciphertext, None ) . await . expect ( "could not encrypt" ) ;
273
+
274
+ sender_store
275
+ . save_changes ( Changes { sessions : vec ! [ olm_session] , ..Default :: default ( ) } )
276
+ . await
277
+ . expect ( "Could not save session" ) ;
278
+
279
+ content
280
+ }
281
+
230
282
/// Create a session for the two supplied Olm machines to communicate.
231
283
pub async fn build_session_for_pair (
232
284
alice : OlmMachine ,
0 commit comments