15
15
//! A set of helper functions for creating [`OlmMachine`]s, and pairs of
16
16
//! interconnected machines.
17
17
18
- use std:: collections:: BTreeMap ;
18
+ use std:: { collections:: BTreeMap , ops :: Deref , sync :: Arc } ;
19
19
20
20
use as_variant:: as_variant;
21
21
use matrix_sdk_test:: { ruma_response_from_json, test_json} ;
@@ -32,11 +32,14 @@ use ruma::{
32
32
user_id, DeviceId , OwnedOneTimeKeyId , TransactionId , UserId ,
33
33
} ;
34
34
use serde_json:: json;
35
+ use tokio:: sync:: Mutex ;
35
36
36
37
use crate :: {
37
- store:: { Changes , MemoryStore } ,
38
- types:: { events:: ToDeviceEvent , requests:: AnyOutgoingRequest } ,
39
- CrossSigningBootstrapRequests , DeviceData , OlmMachine ,
38
+ olm:: PrivateCrossSigningIdentity ,
39
+ store:: { Changes , CryptoStoreWrapper , MemoryStore } ,
40
+ types:: { events:: ToDeviceEvent , requests:: AnyOutgoingRequest , DeviceKeys } ,
41
+ verification:: VerificationMachine ,
42
+ Account , CrossSigningBootstrapRequests , Device , DeviceData , OlmMachine , OtherUserIdentityData ,
40
43
} ;
41
44
42
45
/// These keys need to be periodically uploaded to the server.
@@ -276,3 +279,69 @@ pub fn bootstrap_requests_to_keys_query_response(
276
279
277
280
ruma_response_from_json ( & kq_response)
278
281
}
282
+
283
+ /// Create a [`VerificationMachine`] which won't do any useful verification.
284
+ ///
285
+ /// Helper for [`create_signed_device_of_unverified_user`] and
286
+ /// [`create_unsigned_device`].
287
+ fn dummy_verification_machine ( ) -> VerificationMachine {
288
+ let account = Account :: new ( user_id ! ( "@TEST_USER:example.com" ) ) ;
289
+ VerificationMachine :: new (
290
+ account. deref ( ) . clone ( ) ,
291
+ Arc :: new ( Mutex :: new ( PrivateCrossSigningIdentity :: new ( account. user_id ( ) . to_owned ( ) ) ) ) ,
292
+ Arc :: new ( CryptoStoreWrapper :: new (
293
+ account. user_id ( ) ,
294
+ account. device_id ( ) ,
295
+ MemoryStore :: new ( ) ,
296
+ ) ) ,
297
+ )
298
+ }
299
+
300
+ /// Wrap the given [`DeviceKeys`] into a [`Device`], with no known owner
301
+ /// identity.
302
+ pub fn create_unsigned_device ( device_keys : DeviceKeys ) -> Device {
303
+ Device {
304
+ inner : DeviceData :: try_from ( & device_keys) . unwrap ( ) ,
305
+ verification_machine : dummy_verification_machine ( ) ,
306
+ own_identity : None ,
307
+ device_owner_identity : None ,
308
+ }
309
+ }
310
+
311
+ /// Sign the given [`DeviceKeys`] with a cross-signing identity, and wrap it up
312
+ /// as a [`Device`] with that identity.
313
+ pub async fn create_signed_device_of_unverified_user (
314
+ mut device_keys : DeviceKeys ,
315
+ device_owner_identity : & PrivateCrossSigningIdentity ,
316
+ ) -> Device {
317
+ {
318
+ let self_signing = device_owner_identity. self_signing_key . lock ( ) . await ;
319
+ let self_signing = self_signing. as_ref ( ) . unwrap ( ) ;
320
+ self_signing. sign_device ( & mut device_keys) . unwrap ( ) ;
321
+ }
322
+
323
+ let public_identity = OtherUserIdentityData :: from_private ( device_owner_identity) . await ;
324
+
325
+ let device = Device {
326
+ inner : DeviceData :: try_from ( & device_keys) . unwrap ( ) ,
327
+ verification_machine : dummy_verification_machine ( ) ,
328
+ own_identity : None ,
329
+ device_owner_identity : Some ( public_identity. into ( ) ) ,
330
+ } ;
331
+ assert ! ( device. is_cross_signed_by_owner( ) ) ;
332
+ device
333
+ }
334
+
335
+ /// Sign a public user identity with our own user-signing key.
336
+ pub async fn sign_user_identity_data (
337
+ signer_private_identity : & PrivateCrossSigningIdentity ,
338
+ other_user_identity : & mut OtherUserIdentityData ,
339
+ ) {
340
+ let user_signing = signer_private_identity. user_signing_key . lock ( ) . await ;
341
+
342
+ let user_signing = user_signing. as_ref ( ) . unwrap ( ) ;
343
+ let master = user_signing. sign_user ( & * other_user_identity) . unwrap ( ) ;
344
+ other_user_identity. master_key = Arc :: new ( master. try_into ( ) . unwrap ( ) ) ;
345
+
346
+ user_signing. public_key ( ) . verify_master_key ( other_user_identity. master_key ( ) ) . unwrap ( ) ;
347
+ }
0 commit comments