atagen

matrix-rust-sdk

public
Star0
Watch0
ssh://brewery@pls.lobotomise.me:33/atagen/matrix-rust-sdk.git
bindings/matrix-sdk-crypto-ffi/src/lib.rsRust1219 lines49.5 KB
1
//! Uniffi based bindings for the `matrix-sdk-crypto` crate.
2
//!
3
//! This crate can be used to introduce E2EE support into an existing Matrix
4
//! client or client library in any of the language targets Uniffi supports.
5
6
#![warn(missing_docs)]
7
#![allow(unused_qualifications)]
8
9
mod backup_recovery_key;
10
mod dehydrated_devices;
11
mod device;
12
mod error;
13
mod logger;
14
mod machine;
15
mod responses;
16
mod users;
17
mod verification;
18
19
use std::{
20
    collections::{BTreeMap, HashMap},
21
    sync::Arc,
22
    time::Duration,
23
};
24
25
use anyhow::Context as _;
26
pub use backup_recovery_key::{
27
    BackupRecoveryKey, DecodeError, MegolmV1BackupKey, PassphraseInfo, PkDecryptionError,
28
};
29
pub use device::Device;
30
pub use error::{
31
    CryptoStoreError, DecryptionError, KeyImportError, SecretImportError, SignatureError,
32
};
33
use js_int::UInt;
34
pub use logger::{Logger, set_logger};
35
pub use machine::{KeyRequestPair, OlmMachine, SignatureVerification};
36
use matrix_sdk_common::deserialized_responses::{ShieldState as RustShieldState, ShieldStateCode};
37
use matrix_sdk_crypto::{
38
    CollectStrategy, EncryptionSettings as RustEncryptionSettings,
39
    olm::{IdentityKeys, InboundGroupSession, SenderData, Session},
40
    store::{
41
        CryptoStore,
42
        types::{
43
            Changes, DehydratedDeviceKey as InnerDehydratedDeviceKey, PendingChanges,
44
            RoomSettings as RustRoomSettings,
45
        },
46
    },
47
    types::{
48
        DeviceKey, DeviceKeys, EventEncryptionAlgorithm as RustEventEncryptionAlgorithm, SigningKey,
49
    },
50
};
51
use matrix_sdk_sqlite::SqliteCryptoStore;
52
pub use responses::{
53
    BootstrapCrossSigningResult, DeviceLists, KeysImportResult, OutgoingVerificationRequest,
54
    Request, RequestType, SignatureUploadRequest, UploadSigningKeysRequest,
55
};
56
use ruma::{
57
    DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId,
58
    RoomId, SecondsSinceUnixEpoch, UserId,
59
    events::room::history_visibility::HistoryVisibility as RustHistoryVisibility,
60
};
61
use serde::{Deserialize, Serialize};
62
use tokio::runtime::Runtime;
63
pub use users::UserIdentity;
64
pub use verification::{
65
    CancelInfo, ConfirmVerificationResult, QrCode, QrCodeListener, QrCodeState,
66
    RequestVerificationResult, Sas, SasListener, SasState, ScanResult, StartSasResult,
67
    Verification, VerificationRequest, VerificationRequestListener, VerificationRequestState,
68
};
69
use vodozemac::{Curve25519PublicKey, Ed25519PublicKey};
70
71
use crate::dehydrated_devices::DehydrationError;
72
73
/// Struct collecting data that is important to migrate to the rust-sdk
74
#[derive(Deserialize, Serialize, uniffi::Record)]
75
pub struct MigrationData {
76
    /// The pickled version of the Olm Account
77
    account: PickledAccount,
78
    /// The list of pickleds Olm Sessions.
79
    sessions: Vec<PickledSession>,
80
    /// The list of Megolm inbound group sessions.
81
    inbound_group_sessions: Vec<PickledInboundGroupSession>,
82
    /// The Olm pickle key that was used to pickle all the Olm objects.
83
    pickle_key: Vec<u8>,
84
    /// The backup version that is currently active.
85
    backup_version: Option<String>,
86
    // The backup recovery key, as a base58 encoded string.
87
    backup_recovery_key: Option<String>,
88
    /// The private cross signing keys.
89
    cross_signing: CrossSigningKeyExport,
90
    /// The list of users that the Rust SDK should track.
91
    tracked_users: Vec<String>,
92
    /// Map of room settings
93
    room_settings: HashMap<String, RoomSettings>,
94
}
95
96
/// Struct collecting data that is important to migrate sessions to the rust-sdk
97
#[derive(uniffi::Record)]
98
pub struct SessionMigrationData {
99
    /// The user id that the data belongs to.
100
    user_id: String,
101
    /// The device id that the data belongs to.
102
    device_id: String,
103
    /// The Curve25519 public key of the Account that owns this data.
104
    curve25519_key: String,
105
    /// The Ed25519 public key of the Account that owns this data.
106
    ed25519_key: String,
107
    /// The list of pickleds Olm Sessions.
108
    sessions: Vec<PickledSession>,
109
    /// The list of pickled Megolm inbound group sessions.
110
    inbound_group_sessions: Vec<PickledInboundGroupSession>,
111
    /// The Olm pickle key that was used to pickle all the Olm objects.
112
    pickle_key: Vec<u8>,
113
}
114
115
/// A pickled version of an `Account`.
116
///
117
/// Holds all the information that needs to be stored in a database to restore
118
/// an account.
119
#[derive(Debug, Deserialize, Serialize, uniffi::Record)]
120
pub struct PickledAccount {
121
    /// The user id of the account owner.
122
    pub user_id: String,
123
    /// The device ID of the account owner.
124
    pub device_id: String,
125
    /// The pickled version of the Olm account.
126
    pub pickle: String,
127
    /// Was the account shared.
128
    pub shared: bool,
129
    /// The number of uploaded one-time keys we have on the server.
130
    pub uploaded_signed_key_count: i64,
131
}
132
133
/// A pickled version of a `Session`.
134
///
135
/// Holds all the information that needs to be stored in a database to restore
136
/// a Session.
137
#[derive(Debug, Deserialize, Serialize, uniffi::Record)]
138
pub struct PickledSession {
139
    /// The pickle string holding the Olm Session.
140
    pub pickle: String,
141
    /// The curve25519 key of the other user that we share this session with.
142
    pub sender_key: String,
143
    /// Was the session created using a fallback key.
144
    pub created_using_fallback_key: bool,
145
    /// Unix timestamp (in seconds) when the session was created.
146
    pub creation_time: u64,
147
    /// Unix timestamp (in seconds) when the session was last used.
148
    pub last_use_time: u64,
149
}
150
151
/// A pickled version of an `InboundGroupSession`.
152
///
153
/// Holds all the information that needs to be stored in a database to restore
154
/// an InboundGroupSession.
155
#[derive(Debug, Deserialize, Serialize, uniffi::Record)]
156
pub struct PickledInboundGroupSession {
157
    /// The pickle string holding the InboundGroupSession.
158
    pub pickle: String,
159
    /// The public curve25519 key of the account that sent us the session
160
    pub sender_key: String,
161
    /// The public ed25519 key of the account that sent us the session.
162
    pub signing_key: HashMap<String, String>,
163
    /// The id of the room that the session is used in.
164
    pub room_id: String,
165
    /// The list of claimed ed25519 that forwarded us this key. Will be empty if
166
    /// we directly received this session.
167
    pub forwarding_chains: Vec<String>,
168
    /// Flag remembering if the session was directly sent to us by the sender
169
    /// or if it was imported.
170
    pub imported: bool,
171
    /// Flag remembering if the session has been backed up.
172
    pub backed_up: bool,
173
}
174
175
/// Error type for the migration process.
176
#[derive(Debug, thiserror::Error, uniffi::Error)]
177
pub enum MigrationError {
178
    /// Generic catch all error variant.
179
    #[error("error migrating database: {error_message}")]
180
    Generic {
181
        /// The error message
182
        error_message: String,
183
    },
184
}
185
186
impl From<anyhow::Error> for MigrationError {
187
    fn from(e: anyhow::Error) -> MigrationError {
188
        MigrationError::Generic { error_message: e.to_string() }
189
    }
190
}
191
192
/// Migrate a libolm based setup to a vodozemac based setup stored in a SQLite
193
/// store.
194
///
195
/// # Arguments
196
///
197
/// * `data` - The data that should be migrated over to the SQLite store.
198
///
199
/// * `path` - The path where the SQLite store should be created.
200
///
201
/// * `passphrase` - The passphrase that should be used to encrypt the data at
202
///   rest in the SQLite store. **Warning**, if no passphrase is given, the
203
///   store and all its data will remain unencrypted.
204
///
205
/// * `progress_listener` - A callback that can be used to introspect the
206
///   progress of the migration.
207
#[matrix_sdk_ffi_macros::export]
208
pub fn migrate(
209
    data: MigrationData,
210
    path: String,
211
    passphrase: Option<String>,
212
    progress_listener: Box<dyn ProgressListener>,
213
) -> Result<(), MigrationError> {
214
    let runtime = Runtime::new().context("initializing tokio runtime")?;
215
    runtime.block_on(async move {
216
        migrate_data(data, &path, passphrase, progress_listener).await?;
217
        Ok(())
218
    })
219
}
220
221
async fn migrate_data(
222
    mut data: MigrationData,
223
    path: &str,
224
    passphrase: Option<String>,
225
    progress_listener: Box<dyn ProgressListener>,
226
) -> anyhow::Result<()> {
227
    use matrix_sdk_crypto::{olm::PrivateCrossSigningIdentity, store::types::BackupDecryptionKey};
228
    use vodozemac::olm::Account;
229
    use zeroize::Zeroize;
230
231
    // The total steps here include all the sessions/inbound group sessions and
232
    // additionally some static number of steps:
233
    //
234
    // 1. opening the store
235
    // 2. the Account
236
    // 3. the cross signing keys
237
    // 4. the tracked users
238
    // 5. the final save operation
239
    let total_steps = 5 + data.sessions.len() + data.inbound_group_sessions.len();
240
    let mut processed_steps = 0;
241
    let listener = |progress: usize, total: usize| {
242
        progress_listener.on_progress(progress as i32, total as i32)
243
    };
244
245
    let store = SqliteCryptoStore::open(path, passphrase.as_deref()).await?;
246
247
    processed_steps += 1;
248
    listener(processed_steps, total_steps);
249
250
    let user_id = parse_user_id(&data.account.user_id)?;
251
    let device_id: OwnedDeviceId = data.account.device_id.into();
252
253
    let account = Account::from_libolm_pickle(&data.account.pickle, &data.pickle_key)?;
254
    let pickle = account.pickle();
255
    let identity_keys = Arc::new(account.identity_keys());
256
    let pickled_account = matrix_sdk_crypto::olm::PickledAccount {
257
        user_id: parse_user_id(&data.account.user_id)?,
258
        device_id: device_id.clone(),
259
        pickle,
260
        dehydrated: false, // dehydrated devices are never involved in migration
261
        shared: data.account.shared,
262
        uploaded_signed_key_count: data.account.uploaded_signed_key_count as u64,
263
        creation_local_time: MilliSecondsSinceUnixEpoch::now(),
264
        fallback_key_creation_timestamp: Some(MilliSecondsSinceUnixEpoch::now()),
265
    };
266
    let account = matrix_sdk_crypto::olm::Account::from_pickle(pickled_account)?;
267
268
    processed_steps += 1;
269
    listener(processed_steps, total_steps);
270
271
    let (sessions, inbound_group_sessions) = collect_sessions(
272
        processed_steps,
273
        total_steps,
274
        &listener,
275
        &data.pickle_key,
276
        user_id.clone(),
277
        device_id,
278
        identity_keys,
279
        data.sessions,
280
        data.inbound_group_sessions,
281
    )?;
282
283
    let backup_decryption_key = data
284
        .backup_recovery_key
285
        .map(|k| BackupDecryptionKey::from_base58(k.as_str()))
286
        .transpose()?;
287
288
    let cross_signing = PrivateCrossSigningIdentity::empty((*user_id).into());
289
    cross_signing
290
        .import_secrets_unchecked(
291
            data.cross_signing.master_key.as_deref(),
292
            data.cross_signing.self_signing_key.as_deref(),
293
            data.cross_signing.user_signing_key.as_deref(),
294
        )
295
        .await?;
296
297
    data.cross_signing.master_key.zeroize();
298
    data.cross_signing.self_signing_key.zeroize();
299
    data.cross_signing.user_signing_key.zeroize();
300
301
    processed_steps += 1;
302
    listener(processed_steps, total_steps);
303
304
    let tracked_users: Vec<_> = data
305
        .tracked_users
306
        .into_iter()
307
        .filter_map(|s| parse_user_id(&s).ok().map(|u| (u, true)))
308
        .collect();
309
310
    let tracked_users: Vec<_> = tracked_users.iter().map(|(u, d)| (&**u, *d)).collect();
311
    store.save_tracked_users(tracked_users.as_slice()).await?;
312
313
    processed_steps += 1;
314
    listener(processed_steps, total_steps);
315
316
    let mut room_settings = HashMap::new();
317
    for (room_id, settings) in data.room_settings {
318
        let room_id = RoomId::parse(room_id)?;
319
        room_settings.insert(room_id, settings.into());
320
    }
321
322
    store.save_pending_changes(PendingChanges { account: Some(account) }).await?;
323
324
    let changes = Changes {
325
        private_identity: Some(cross_signing),
326
        sessions,
327
        inbound_group_sessions,
328
        backup_decryption_key,
329
        backup_version: data.backup_version,
330
        room_settings,
331
        ..Default::default()
332
    };
333
334
    save_changes(processed_steps, total_steps, &listener, changes, &store).await
335
}
336
337
async fn save_changes(
338
    mut processed_steps: usize,
339
    total_steps: usize,
340
    listener: &dyn Fn(usize, usize),
341
    changes: Changes,
342
    store: &SqliteCryptoStore,
343
) -> anyhow::Result<()> {
344
    store.save_changes(changes).await?;
345
346
    processed_steps += 1;
347
    listener(processed_steps, total_steps);
348
349
    Ok(())
350
}
351
352
/// Migrate sessions and group sessions of a libolm based setup to a vodozemac
353
/// based setup stored in a SQLite store.
354
///
355
/// This method allows you to migrate a subset of the data, it should only be
356
/// used after the [`migrate()`] method has been already used.
357
///
358
/// # Arguments
359
///
360
/// * `data` - The data that should be migrated over to the SQLite store.
361
///
362
/// * `path` - The path where the SQLite store should be created.
363
///
364
/// * `passphrase` - The passphrase that should be used to encrypt the data at
365
///   rest in the SQLite store. **Warning**, if no passphrase is given, the
366
///   store and all its data will remain unencrypted.
367
///
368
/// * `progress_listener` - A callback that can be used to introspect the
369
///   progress of the migration.
370
#[matrix_sdk_ffi_macros::export]
371
pub fn migrate_sessions(
372
    data: SessionMigrationData,
373
    path: String,
374
    passphrase: Option<String>,
375
    progress_listener: Box<dyn ProgressListener>,
376
) -> Result<(), MigrationError> {
377
    let runtime = Runtime::new().context("initializing tokio runtime")?;
378
    runtime.block_on(migrate_session_data(data, &path, passphrase, progress_listener))?;
379
    Ok(())
380
}
381
382
async fn migrate_session_data(
383
    data: SessionMigrationData,
384
    path: &str,
385
    passphrase: Option<String>,
386
    progress_listener: Box<dyn ProgressListener>,
387
) -> anyhow::Result<()> {
388
    let store = SqliteCryptoStore::open(path, passphrase.as_deref()).await?;
389
390
    let listener = |progress: usize, total: usize| {
391
        progress_listener.on_progress(progress as i32, total as i32)
392
    };
393
394
    let total_steps = 1 + data.sessions.len() + data.inbound_group_sessions.len();
395
    let processed_steps = 0;
396
397
    let user_id = UserId::parse(data.user_id)?;
398
    let device_id: OwnedDeviceId = data.device_id.into();
399
400
    let identity_keys = IdentityKeys {
401
        ed25519: Ed25519PublicKey::from_base64(&data.ed25519_key)?,
402
        curve25519: Curve25519PublicKey::from_base64(&data.curve25519_key)?,
403
    }
404
    .into();
405
406
    let (sessions, inbound_group_sessions) = collect_sessions(
407
        processed_steps,
408
        total_steps,
409
        &listener,
410
        &data.pickle_key,
411
        user_id,
412
        device_id,
413
        identity_keys,
414
        data.sessions,
415
        data.inbound_group_sessions,
416
    )?;
417
418
    let changes = Changes { sessions, inbound_group_sessions, ..Default::default() };
419
    save_changes(processed_steps, total_steps, &listener, changes, &store).await
420
}
421
422
#[allow(clippy::too_many_arguments)]
423
fn collect_sessions(
424
    mut processed_steps: usize,
425
    total_steps: usize,
426
    listener: &dyn Fn(usize, usize),
427
    pickle_key: &[u8],
428
    user_id: OwnedUserId,
429
    device_id: OwnedDeviceId,
430
    identity_keys: Arc<IdentityKeys>,
431
    session_pickles: Vec<PickledSession>,
432
    group_session_pickles: Vec<PickledInboundGroupSession>,
433
) -> anyhow::Result<(Vec<Session>, Vec<InboundGroupSession>)> {
434
    let mut sessions = Vec::new();
435
436
    // Create a DeviceKeys struct with enough information to get a working
437
    // Session, but we will won't actually use the Sessions (and we'll clear
438
    // the session cache after migration) so we don't need to worry about
439
    // signatures.
440
    let device_keys = DeviceKeys::new(
441
        user_id,
442
        device_id.clone(),
443
        Default::default(),
444
        BTreeMap::from([
445
            (
446
                DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &device_id),
447
                DeviceKey::Ed25519(identity_keys.ed25519),
448
            ),
449
            (
450
                DeviceKeyId::from_parts(DeviceKeyAlgorithm::Curve25519, &device_id),
451
                DeviceKey::Curve25519(identity_keys.curve25519),
452
            ),
453
        ]),
454
        Default::default(),
455
    );
456
457
    for session_pickle in session_pickles {
458
        let pickle =
459
            vodozemac::olm::Session::from_libolm_pickle(&session_pickle.pickle, pickle_key)?
460
                .pickle();
461
462
        let creation_time = SecondsSinceUnixEpoch(
463
            UInt::new(session_pickle.creation_time).context("invalid creation timestamp")?,
464
        );
465
        let last_use_time = SecondsSinceUnixEpoch(
466
            UInt::new(session_pickle.last_use_time).context("invalid last use timestamp")?,
467
        );
468
469
        let pickle = matrix_sdk_crypto::olm::PickledSession {
470
            pickle,
471
            sender_key: Curve25519PublicKey::from_base64(&session_pickle.sender_key)?,
472
            created_using_fallback_key: session_pickle.created_using_fallback_key,
473
            creation_time,
474
            last_use_time,
475
        };
476
477
        let session = Session::from_pickle(device_keys.clone(), pickle)?;
478
479
        sessions.push(session);
480
        processed_steps += 1;
481
        listener(processed_steps, total_steps);
482
    }
483
484
    let mut inbound_group_sessions = Vec::new();
485
486
    for session in group_session_pickles {
487
        let pickle = vodozemac::megolm::InboundGroupSession::from_libolm_pickle(
488
            &session.pickle,
489
            pickle_key,
490
        )?
491
        .pickle();
492
493
        let sender_key = Curve25519PublicKey::from_base64(&session.sender_key)?;
494
495
        let pickle = matrix_sdk_crypto::olm::PickledInboundGroupSession {
496
            pickle,
497
            sender_key,
498
            signing_key: session
499
                .signing_key
500
                .into_iter()
501
                .map(|(k, v)| {
502
                    let algorithm = DeviceKeyAlgorithm::from(k);
503
                    let key = SigningKey::from_parts(&algorithm, v)?;
504
505
                    Ok((algorithm, key))
506
                })
507
                .collect::<anyhow::Result<_>>()?,
508
            sender_data: SenderData::legacy(),
509
            forwarder_data: None,
510
            room_id: RoomId::parse(session.room_id)?,
511
            imported: session.imported,
512
            backed_up: session.backed_up,
513
            history_visibility: None,
514
            shared_history: false,
515
            algorithm: RustEventEncryptionAlgorithm::MegolmV1AesSha2,
516
        };
517
518
        let session = matrix_sdk_crypto::olm::InboundGroupSession::from_pickle(pickle)?;
519
520
        inbound_group_sessions.push(session);
521
        processed_steps += 1;
522
        listener(processed_steps, total_steps);
523
    }
524
525
    Ok((sessions, inbound_group_sessions))
526
}
527
528
/// Migrate room settings, including room algorithm and whether to block
529
/// untrusted devices from legacy store to Sqlite store.
530
///
531
/// Note that this method should only be used if a client has already migrated
532
/// account data via [migrate](#method.migrate) method, which did not include
533
/// room settings. For a brand new migration, the [migrate](#method.migrate)
534
/// method will take care of room settings automatically, if provided.
535
///
536
/// # Arguments
537
///
538
/// * `room_settings` - Map of room settings
539
///
540
/// * `path` - The path where the Sqlite store should be created.
541
///
542
/// * `passphrase` - The passphrase that should be used to encrypt the data at
543
///   rest in the Sqlite store. **Warning**, if no passphrase is given, the
544
///   store and all its data will remain unencrypted.
545
#[matrix_sdk_ffi_macros::export]
546
pub fn migrate_room_settings(
547
    room_settings: HashMap<String, RoomSettings>,
548
    path: String,
549
    passphrase: Option<String>,
550
) -> Result<(), MigrationError> {
551
    let runtime = Runtime::new().context("initializing tokio runtime")?;
552
    runtime.block_on(async move {
553
        let store = SqliteCryptoStore::open(path, passphrase.as_deref())
554
            .await
555
            .context("opening sqlite crypto store")?;
556
557
        let mut rust_settings = HashMap::new();
558
        for (room_id, settings) in room_settings {
559
            let room_id = RoomId::parse(room_id).context("parsing room ID")?;
560
            rust_settings.insert(room_id, settings.into());
561
        }
562
563
        let changes = Changes { room_settings: rust_settings, ..Default::default() };
564
        store.save_changes(changes).await.context("saving changes")?;
565
566
        Ok(())
567
    })
568
}
569
570
/// Callback that will be passed over the FFI to report progress
571
#[matrix_sdk_ffi_macros::export(callback_interface)]
572
pub trait ProgressListener {
573
    /// The callback that should be called on the Rust side
574
    ///
575
    /// # Arguments
576
    ///
577
    /// * `progress` - The current number of items that have been handled
578
    ///
579
    /// * `total` - The total number of items that will be handled
580
    fn on_progress(&self, progress: i32, total: i32);
581
}
582
583
impl<T: Fn(i32, i32)> ProgressListener for T {
584
    fn on_progress(&self, progress: i32, total: i32) {
585
        self(progress, total)
586
    }
587
}
588
589
/// An encryption algorithm to be used to encrypt messages sent to a room.
590
#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Enum)]
591
pub enum EventEncryptionAlgorithm {
592
    /// Olm version 1 using Curve25519, AES-256, and SHA-256.
593
    OlmV1Curve25519AesSha2,
594
    /// Megolm version 1 using AES-256 and SHA-256.
595
    MegolmV1AesSha2,
596
}
597
598
impl From<EventEncryptionAlgorithm> for RustEventEncryptionAlgorithm {
599
    fn from(a: EventEncryptionAlgorithm) -> Self {
600
        match a {
601
            EventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => Self::OlmV1Curve25519AesSha2,
602
            EventEncryptionAlgorithm::MegolmV1AesSha2 => Self::MegolmV1AesSha2,
603
        }
604
    }
605
}
606
607
impl TryFrom<RustEventEncryptionAlgorithm> for EventEncryptionAlgorithm {
608
    type Error = serde_json::Error;
609
610
    fn try_from(value: RustEventEncryptionAlgorithm) -> Result<Self, Self::Error> {
611
        match value {
612
            RustEventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => {
613
                Ok(Self::OlmV1Curve25519AesSha2)
614
            }
615
            RustEventEncryptionAlgorithm::MegolmV1AesSha2 => Ok(Self::MegolmV1AesSha2),
616
            _ => Err(serde::de::Error::custom(format!("Unsupported algorithm {value}"))),
617
        }
618
    }
619
}
620
621
/// Who can see a room's history.
622
#[derive(uniffi::Enum)]
623
pub enum HistoryVisibility {
624
    /// Previous events are accessible to newly joined members from the point
625
    /// they were invited onwards.
626
    ///
627
    /// Events stop being accessible when the member's state changes to
628
    /// something other than *invite* or *join*.
629
    Invited,
630
631
    /// Previous events are accessible to newly joined members from the point
632
    /// they joined the room onwards.
633
    /// Events stop being accessible when the member's state changes to
634
    /// something other than *join*.
635
    Joined,
636
637
    /// Previous events are always accessible to newly joined members.
638
    ///
639
    /// All events in the room are accessible, even those sent when the member
640
    /// was not a part of the room.
641
    Shared,
642
643
    /// All events while this is the `HistoryVisibility` value may be shared by
644
    /// any participating homeserver with anyone, regardless of whether they
645
    /// have ever joined the room.
646
    WorldReadable,
647
}
648
649
impl From<HistoryVisibility> for RustHistoryVisibility {
650
    fn from(h: HistoryVisibility) -> Self {
651
        match h {
652
            HistoryVisibility::Invited => Self::Invited,
653
            HistoryVisibility::Joined => Self::Joined,
654
            HistoryVisibility::Shared => Self::Shared,
655
            HistoryVisibility::WorldReadable => Self::Shared,
656
        }
657
    }
658
}
659
660
/// Settings that should be used when a room key is shared.
661
///
662
/// These settings control which algorithm the room key should use, how long a
663
/// room key should be used and some other important information that determines
664
/// the lifetime of a room key.
665
#[derive(uniffi::Record)]
666
pub struct EncryptionSettings {
667
    /// The encryption algorithm that should be used in the room.
668
    pub algorithm: EventEncryptionAlgorithm,
669
    /// Whether state event encryption is enabled.
670
    #[cfg(feature = "experimental-encrypted-state-events")]
671
    pub encrypt_state_events: bool,
672
    /// How long can the room key be used before it should be rotated. Time in
673
    /// seconds.
674
    pub rotation_period: u64,
675
    /// How many messages should be sent before the room key should be rotated.
676
    pub rotation_period_msgs: u64,
677
    /// The current history visibility of the room. The visibility will be
678
    /// tracked by the room key and the key will be rotated if the visibility
679
    /// changes.
680
    pub history_visibility: HistoryVisibility,
681
    /// Should untrusted devices receive the room key, or should they be
682
    /// excluded from the conversation.
683
    pub only_allow_trusted_devices: bool,
684
    /// Should fail to send when a verified user has unverified devices, or when
685
    /// a previously verified user replaces their identity.
686
    pub error_on_verified_user_problem: bool,
687
}
688
689
impl From<EncryptionSettings> for RustEncryptionSettings {
690
    fn from(v: EncryptionSettings) -> Self {
691
        let sharing_strategy = if v.only_allow_trusted_devices {
692
            CollectStrategy::OnlyTrustedDevices
693
        } else if v.error_on_verified_user_problem {
694
            CollectStrategy::ErrorOnVerifiedUserProblem
695
        } else {
696
            CollectStrategy::AllDevices
697
        };
698
699
        RustEncryptionSettings {
700
            algorithm: v.algorithm.into(),
701
            #[cfg(feature = "experimental-encrypted-state-events")]
702
            encrypt_state_events: false,
703
            rotation_period: Duration::from_secs(v.rotation_period),
704
            rotation_period_msgs: v.rotation_period_msgs,
705
            history_visibility: v.history_visibility.into(),
706
            sharing_strategy,
707
        }
708
    }
709
}
710
711
/// An event that was successfully decrypted.
712
#[derive(uniffi::Record)]
713
pub struct DecryptedEvent {
714
    /// The decrypted version of the event.
715
    pub clear_event: String,
716
    /// The claimed curve25519 key of the sender.
717
    pub sender_curve25519_key: String,
718
    /// The claimed ed25519 key of the sender.
719
    pub claimed_ed25519_key: Option<String>,
720
    /// The curve25519 chain of the senders that forwarded the Megolm decryption
721
    /// key to us. Is empty if the key came directly from the sender of the
722
    /// event.
723
    pub forwarding_curve25519_chain: Vec<String>,
724
    /// The shield state (color and message to display to user) for the event,
725
    /// representing the event's authenticity. Computed from the properties of
726
    /// the sender user identity and their Olm device.
727
    ///
728
    /// Note that this is computed at time of decryption, so the value reflects
729
    /// the computed event authenticity at that time. Authenticity-related
730
    /// properties can change later on, such as when a user identity is
731
    /// subsequently verified or a device is deleted.
732
    pub shield_state: ShieldState,
733
}
734
735
/// Take a look at [`matrix_sdk_common::deserialized_responses::ShieldState`]
736
/// for more info.
737
#[allow(missing_docs)]
738
#[derive(uniffi::Enum)]
739
pub enum ShieldColor {
740
    Red,
741
    Grey,
742
    None,
743
}
744
745
/// Take a look at [`matrix_sdk_common::deserialized_responses::ShieldState`]
746
/// for more info.
747
#[derive(uniffi::Record)]
748
#[allow(missing_docs)]
749
pub struct ShieldState {
750
    color: ShieldColor,
751
    code: Option<ShieldStateCode>,
752
    message: Option<String>,
753
}
754
755
impl From<RustShieldState> for ShieldState {
756
    fn from(value: RustShieldState) -> Self {
757
        match value {
758
            RustShieldState::Red { code, message } => Self {
759
                color: ShieldColor::Red,
760
                code: Some(code),
761
                message: Some(message.to_owned()),
762
            },
763
            RustShieldState::Grey { code, message } => Self {
764
                color: ShieldColor::Grey,
765
                code: Some(code),
766
                message: Some(message.to_owned()),
767
            },
768
            RustShieldState::None => Self { color: ShieldColor::None, code: None, message: None },
769
        }
770
    }
771
}
772
773
/// Struct representing the state of our private cross signing keys, it shows
774
/// which private cross signing keys we have locally stored.
775
#[derive(Debug, Clone, uniffi::Record)]
776
pub struct CrossSigningStatus {
777
    /// Do we have the master key.
778
    pub has_master: bool,
779
    /// Do we have the self signing key, this one is necessary to sign our own
780
    /// devices.
781
    pub has_self_signing: bool,
782
    /// Do we have the user signing key, this one is necessary to sign other
783
    /// users.
784
    pub has_user_signing: bool,
785
}
786
787
/// A struct containing private cross signing keys that can be backed up or
788
/// uploaded to the secret store.
789
#[derive(Deserialize, Serialize, uniffi::Record)]
790
pub struct CrossSigningKeyExport {
791
    /// The seed of the master key encoded as unpadded base64.
792
    pub master_key: Option<String>,
793
    /// The seed of the self signing key encoded as unpadded base64.
794
    pub self_signing_key: Option<String>,
795
    /// The seed of the user signing key encoded as unpadded base64.
796
    pub user_signing_key: Option<String>,
797
}
798
799
/// Struct holding the number of room keys we have.
800
#[derive(uniffi::Record)]
801
pub struct RoomKeyCounts {
802
    /// The total number of room keys.
803
    pub total: i64,
804
    /// The number of backed up room keys.
805
    pub backed_up: i64,
806
}
807
808
/// Backup keys and information we load from the store.
809
#[derive(uniffi::Object)]
810
pub struct BackupKeys {
811
    /// The recovery key as a base64 encoded string.
812
    recovery_key: Arc<BackupRecoveryKey>,
813
    /// The version that is used with the recovery key.
814
    backup_version: String,
815
}
816
817
#[matrix_sdk_ffi_macros::export]
818
impl BackupKeys {
819
    /// Get the recovery key that we're holding on to.
820
    pub fn recovery_key(&self) -> Arc<BackupRecoveryKey> {
821
        self.recovery_key.clone()
822
    }
823
824
    /// Get the backups version that we're holding on to.
825
    pub fn backup_version(&self) -> String {
826
        self.backup_version.to_owned()
827
    }
828
}
829
830
impl TryFrom<matrix_sdk_crypto::store::types::BackupKeys> for BackupKeys {
831
    type Error = ();
832
833
    fn try_from(keys: matrix_sdk_crypto::store::types::BackupKeys) -> Result<Self, Self::Error> {
834
        Ok(Self {
835
            recovery_key: BackupRecoveryKey {
836
                inner: keys.decryption_key.ok_or(())?,
837
                passphrase_info: None,
838
            }
839
            .into(),
840
            backup_version: keys.backup_version.ok_or(())?,
841
        })
842
    }
843
}
844
845
/// Dehydrated device key
846
#[derive(uniffi::Record, Clone)]
847
pub struct DehydratedDeviceKey {
848
    pub(crate) inner: Vec<u8>,
849
}
850
851
impl DehydratedDeviceKey {
852
    /// Generates a new random pickle key.
853
    #[allow(clippy::new_without_default)]
854
    pub fn new() -> Self {
855
        InnerDehydratedDeviceKey::new().into()
856
    }
857
858
    /// Creates a new dehydration pickle key from the given slice.
859
    ///
860
    /// Fail if the slice length is not 32.
861
    pub fn from_slice(slice: &[u8]) -> Result<Self, DehydrationError> {
862
        let inner = InnerDehydratedDeviceKey::from_slice(slice)?;
863
        Ok(inner.into())
864
    }
865
866
    /// Export the [`DehydratedDeviceKey`] as a base64 encoded string.
867
    pub fn to_base64(&self) -> String {
868
        let inner = InnerDehydratedDeviceKey::from_slice(&self.inner).unwrap();
869
        inner.to_base64()
870
    }
871
}
872
impl From<InnerDehydratedDeviceKey> for DehydratedDeviceKey {
873
    fn from(pickle_key: InnerDehydratedDeviceKey) -> Self {
874
        DehydratedDeviceKey { inner: pickle_key.into() }
875
    }
876
}
877
878
impl From<matrix_sdk_crypto::store::types::RoomKeyCounts> for RoomKeyCounts {
879
    fn from(count: matrix_sdk_crypto::store::types::RoomKeyCounts) -> Self {
880
        Self { total: count.total as i64, backed_up: count.backed_up as i64 }
881
    }
882
}
883
884
impl From<matrix_sdk_crypto::CrossSigningKeyExport> for CrossSigningKeyExport {
885
    fn from(e: matrix_sdk_crypto::CrossSigningKeyExport) -> Self {
886
        Self {
887
            master_key: e.master_key.clone(),
888
            self_signing_key: e.self_signing_key.clone(),
889
            user_signing_key: e.user_signing_key.clone(),
890
        }
891
    }
892
}
893
894
impl From<CrossSigningKeyExport> for matrix_sdk_crypto::CrossSigningKeyExport {
895
    fn from(e: CrossSigningKeyExport) -> Self {
896
        matrix_sdk_crypto::CrossSigningKeyExport {
897
            master_key: e.master_key,
898
            self_signing_key: e.self_signing_key,
899
            user_signing_key: e.user_signing_key,
900
        }
901
    }
902
}
903
904
impl From<matrix_sdk_crypto::CrossSigningStatus> for CrossSigningStatus {
905
    fn from(s: matrix_sdk_crypto::CrossSigningStatus) -> Self {
906
        Self {
907
            has_master: s.has_master,
908
            has_self_signing: s.has_self_signing,
909
            has_user_signing: s.has_user_signing,
910
        }
911
    }
912
}
913
914
/// Room encryption settings which are modified by state events or user options
915
#[derive(Debug, PartialEq, Deserialize, Serialize, uniffi::Record)]
916
pub struct RoomSettings {
917
    /// The encryption algorithm that should be used in the room.
918
    pub algorithm: EventEncryptionAlgorithm,
919
    /// Whether state event encryption is enabled.
920
    #[cfg(feature = "experimental-encrypted-state-events")]
921
    #[serde(default)]
922
    pub encrypt_state_events: bool,
923
    /// Should untrusted devices receive the room key, or should they be
924
    /// excluded from the conversation.
925
    pub only_allow_trusted_devices: bool,
926
}
927
928
impl TryFrom<RustRoomSettings> for RoomSettings {
929
    type Error = serde_json::Error;
930
931
    fn try_from(value: RustRoomSettings) -> Result<Self, Self::Error> {
932
        let algorithm = value.algorithm.try_into()?;
933
        Ok(Self {
934
            algorithm,
935
            #[cfg(feature = "experimental-encrypted-state-events")]
936
            encrypt_state_events: value.encrypt_state_events,
937
            only_allow_trusted_devices: value.only_allow_trusted_devices,
938
        })
939
    }
940
}
941
942
impl From<RoomSettings> for RustRoomSettings {
943
    fn from(value: RoomSettings) -> Self {
944
        Self {
945
            algorithm: value.algorithm.into(),
946
            only_allow_trusted_devices: value.only_allow_trusted_devices,
947
            ..RustRoomSettings::default()
948
        }
949
    }
950
}
951
952
fn parse_user_id(user_id: &str) -> Result<OwnedUserId, CryptoStoreError> {
953
    ruma::UserId::parse(user_id).map_err(|e| CryptoStoreError::InvalidUserId(user_id.to_owned(), e))
954
}
955
956
#[matrix_sdk_ffi_macros::export]
957
fn version_info() -> VersionInfo {
958
    VersionInfo {
959
        version: matrix_sdk_crypto::VERSION.to_owned(),
960
        vodozemac_version: matrix_sdk_crypto::vodozemac::VERSION.to_owned(),
961
        git_description: env!("VERGEN_GIT_DESCRIBE").to_owned(),
962
        git_sha: env!("VERGEN_GIT_SHA").to_owned(),
963
    }
964
}
965
966
/// Build-time information about important crates that are used.
967
#[derive(uniffi::Record)]
968
pub struct VersionInfo {
969
    /// The version of the matrix-sdk-crypto crate.
970
    pub version: String,
971
    /// The version of the vodozemac crate.
972
    pub vodozemac_version: String,
973
    /// The Git commit hash of the crate's source tree at build time.
974
    pub git_sha: String,
975
    /// The build-time output of the `git describe` command of the source tree
976
    /// of crate.
977
    pub git_description: String,
978
}
979
980
#[matrix_sdk_ffi_macros::export]
981
fn version() -> String {
982
    matrix_sdk_crypto::VERSION.to_owned()
983
}
984
985
#[matrix_sdk_ffi_macros::export]
986
fn vodozemac_version() -> String {
987
    vodozemac::VERSION.to_owned()
988
}
989
990
/// The encryption component of PkEncryption support.
991
///
992
/// This struct can be created using a [`Curve25519PublicKey`] corresponding to
993
/// a `PkDecryption` object, allowing messages to be encrypted for the
994
/// associated decryption object.
995
#[derive(uniffi::Object)]
996
pub struct PkEncryption {
997
    inner: matrix_sdk_crypto::vodozemac::pk_encryption::PkEncryption,
998
}
999
1000
#[matrix_sdk_ffi_macros::export]
1001
impl PkEncryption {
1002
    /// Create a new [`PkEncryption`] object from a `Curve25519PublicKey`
1003
    /// encoded as Base64.
1004
    ///
1005
    /// The public key should come from an existing `PkDecryption` object.
1006
    /// Returns a `DecodeError` if the Curve25519 key could not be decoded
1007
    /// correctly.
1008
    #[uniffi::constructor]
1009
    pub fn from_base64(key: &str) -> Result<Arc<Self>, DecodeError> {
1010
        let key = vodozemac::Curve25519PublicKey::from_base64(key)
1011
            .map_err(matrix_sdk_crypto::backups::DecodeError::PublicKey)?;
1012
        let inner = vodozemac::pk_encryption::PkEncryption::from_key(key);
1013
1014
        Ok(Self { inner }.into())
1015
    }
1016
1017
    /// Encrypt a message using this [`PkEncryption`] object.
1018
    pub fn encrypt(&self, plaintext: &str) -> Option<PkMessage> {
1019
        use vodozemac::base64_encode;
1020
1021
        let message = self.inner.encrypt(plaintext.as_ref()).ok()?;
1022
1023
        let vodozemac::pk_encryption::Message { ciphertext, mac, ephemeral_key } = message;
1024
1025
        Some(PkMessage {
1026
            ciphertext: base64_encode(ciphertext),
1027
            mac: base64_encode(mac),
1028
            ephemeral_key: ephemeral_key.to_base64(),
1029
        })
1030
    }
1031
}
1032
1033
/// A message that was encrypted using a [`PkEncryption`] object.
1034
#[derive(uniffi::Record)]
1035
pub struct PkMessage {
1036
    /// The ciphertext of the message.
1037
    pub ciphertext: String,
1038
    /// The message authentication code of the message.
1039
    ///
1040
    /// *Warning*: This does not authenticate the ciphertext.
1041
    pub mac: String,
1042
    /// The ephemeral Curve25519 key of the message which was used to derive the
1043
    /// individual message key.
1044
    pub ephemeral_key: String,
1045
}
1046
1047
uniffi::setup_scaffolding!();
1048
1049
#[cfg(test)]
1050
mod tests {
1051
    use anyhow::Result;
1052
    use serde_json::{Value, json};
1053
    use tempfile::tempdir;
1054
1055
    use super::MigrationData;
1056
    use crate::{EventEncryptionAlgorithm, OlmMachine, RoomSettings, migrate};
1057
1058
    #[test]
1059
    fn android_migration() -> Result<()> {
1060
        let data: Value = json!({
1061
            "account":{
1062
               "user_id":"@ganfra146:matrix.org",
1063
               "device_id":"DEWRCMENGS",
1064
               "pickle":"FFGTGho89T3Xgd56l+EedOPV37s09RR8aYnS9305qPKF66LG+ly29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slU3D6x3rNGwJbQVRu0IiA3PpeAQNZBNAJHHfv8IzIYxPhMJdYq0YqLIGSUYu87D04cDOxJY7hgawYs+ExOWb7XkbpuRoITQd8zpwVDFlSCS+wFO+qah3Vn8RBTc6cXHO5xRWfUNj+NrEtPdVmax+9EXqXtHQyFpxaauvL96RH+mGwpKHOk3aisXbZ6gLE2mF4egGjjJOIJdHyb2ZR+kj+4GIvkoBwipDgUfr4UBXY8pvFxQOxRgtI4LgOY9Z1Aco7Mwp6qi1KoMFJW8d+gJwsgM3cMsyEeYH1n/mdpJW6VDbIWzOHkP5n+OKKNm2vJTkQFFwF9eOtGy9fNBtS4qo4jvOUJnnAPsrPbGMbBYd1dMC3daHLEwvIKCAVBn7q1Z2c4zAD5eEoY0EwZj/j8x8lGQ8TswFT81ZotW7ZBDai/YtV8mkGfuaWJRI5yHc/bV7GWLF+yrMji/jicBF5jy2UoqwxseqjgTut49FRgBH3h1qwnfYbXD3FvQljyAAgBCiZV726pFRG+sZv0FjDbq0iCKILVSEUDZgmQ",
1065
               "shared":true,
1066
               "uploaded_signed_key_count":50
1067
            },
1068
            "sessions":[
1069
               {
1070
                  "pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4s+SdrKUYAMUdGcYD7QukrPklEOy7fJho9YGK/jV04QdA8JABiOfD+ngJTR4V8eZdmDuG08+Q5EL79V81hQwU2fKndP0y/9nAXPUIADYq0Zrg4EsOnXz7aE+hAeBAm0IBog1s8RYUvynZ15uwjbd/OTLP+gpqpX33DwVg2leiBkQetiUSpOpZCuQ8CcZwIA0MoGCqvaT7h76VHX9JxJx+2fCMhsJMx1nhd99WJH1W9ge5CtdbC4KUP92OSxIrPOnMrNcOPJPp/paZP+HFNQ3PDL+z8pGKXmCnrXGSbd7iPHurPYESrVkBzr",
1071
                  "sender_key":"WJ6Ce7U67a6jqkHYHd8o0+5H4bqdi9hInZdk0+swuXs",
1072
                  "created_using_fallback_key":false,
1073
                  "creation_time": 1649425011424u64,
1074
                  "last_use_time": 1649425011424u64
1075
               },
1076
               {
1077
                  "pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4t2W/lowyrV6SXVZp+uG59im0AAfNSKjhjZuiOpQlX7MS+AOJkCNvyujJ2g3KSjLZ94IkoHxkBDHLWSjwaLPu40rfOzJPDpm0XZsR6bQrsxKOmXLGEw2qw5jOTouzMVL2gvuuTix97nSYSU8j3XvTMRUoh0AF/tUpRLcvEFZeGrdUYmTMlyTv4na+FVUalUZ+jrk8t1/sM99JNq3SY1IBSjrBq/0rCOHieiippz0sw2fe2b87id4rqj1g3R9w2MWTWEdOz3ugjMGYF1YDBQZA1tJZ/hmgppk2AU2xKQXE2X3DgSC6fC66D4",
1078
                  "sender_key":"RzRROfmHNlBfzxnNCUYBfn/5oZNQ11XYjDg59hS+mV0",
1079
                  "created_using_fallback_key":false,
1080
                  "creation_time": 1649425011503u64,
1081
                  "last_use_time": 1649425011503u64
1082
               },
1083
               {
1084
                  "pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4titbL3SS12PYHpcBPJc6hXnOnZXqrjtjYOD545fck+3utEo8cqqwWubc9tsvxGW3tOWPttLBdAW30Vn8V1M8ebqVCNVWEAb1GKjV4ni8xG7G9SlEcCjLjnF4lJpddSZkqVMFoN0ITr9aSz/eJwXpc3HLreUFXwc8LuQp7krQ4Vt1e5EE/klduqsdurZf5V14RHsmWz2lKjt7nVgtIz/dhtF5F/sGJdg8kCGaHIMSbGAPuPPpa4/Laicb/5otrYt4pg4W4KdFpSGJIcvUQNjXaOZMx3cu/RPJIOyNhx7whG1QiYAUBqAJvr",
1085
                  "sender_key":"IXSZugAHig1v8MowE1jxi2wDDDfuZBeJynHlegJVwUc",
1086
                  "created_using_fallback_key":false,
1087
                  "creation_time": 1649425011566u64,
1088
                  "last_use_time": 1649425011566u64
1089
               },
1090
               {
1091
                  "pickle":"SmkDiFZjNukiarQ7XHQo25FILHsuhNOnxy56cMSQU/Y71jaGbJes4YrvN4Dfy4RSONfejEDXDkbW2JudlHHRP/rWEmnfJiGbK6ArbrG2puqIZgOecPnOUgPfCisr49p1Gmf36dPaO5lm/ZSrngfSoxahoeJJE/CcJN98sYM15XytRk2LBwc+CyYDqr4V1qxfsBt6tzJ4+tsAZeRdD0UtipQgysgH56o8N7nKTCkaZz5lfpYCl3FEgwXpLJ0MGQvtQmbORFvOLqR1jZ/EbmNGKiqDDIYsqG0sf78ii1jqfpLDBXLuYDccsg",
1092
                  "sender_key":"EB9SC4jVAydKhM6/GcwMc9biKwVNywqW3TerNTrtb1M",
1093
                  "created_using_fallback_key":false,
1094
                  "creation_time": 1649542063182u64,
1095
                  "last_use_time": 1649542063182u64
1096
               }
1097
            ],
1098
            "inbound_group_sessions":[
1099
               {
1100
                  "pickle":"KoA0sxDNQ7lz0vylU9zlmar0VCVQRMCfRrIfTh1bdMhlAgy8/D2ToT+oKRaKy1HiW6H862bzdpgxprlseSjmip9OfLbIuyd2YZcEinwc2666oEI/hpk4oTlE61uE1M+ThfdFf41yGCmaAP7mhjwF234ZrZ6i/F/qx42TLQ8Unc30wDJaJgyheO5eW85SD/0g0cdg2WnEKrx2/wl7Vg/YijT3JMDZ+OsdfJfSZtxBNjlG+PQ/9D31qb1eHfaovc8vFZh5QLfAFg/5rBrF1PhRsC7xOAZbUNdrTbvypNfMM4mznf84C2SzZRSMeAfg5v/YticM3Keg4eHuEj1WO9DrmRXYl6b/pITdf1xuk5euVT0pyxJpXmq41AoAZKAo1l94HGy1LG1RpruD1uQPhiponh5PGHSOf43Q",
1101
                  "sender_key":"vJfH7wiYmGos3C8U1XcJ//YWSmkueAYqrmUA6/ukfAM",
1102
                  "signing_key":{
1103
                     "ed25519":"JGgPQRuYj3ScMdPS+A0P+k/1qS9Hr3qeKXLscI+hS78"
1104
                  },
1105
                  "room_id":"!AZkqtjvtwPAuyNOXEt:matrix.org",
1106
                  "forwarding_chains":[
1107
                  ],
1108
                  "imported":true,
1109
                  "backed_up":true
1110
               },
1111
               {
1112
                  "pickle":"9RF6GBu9CvjZZx2hxIlw2gMdKs36LFhXhLTHAPrLSjT2OTbeE/jK263+iiFdSpF7Cblp/lXzljPKJN6sL8JGzoT7ssYh56nI0kKsp7/y88z+tTOH/5NYYTmZzHYw6yy4Cmaxh0pdHDs+RQpSSIe9jhF/EJJna5jcKYXxDY52m8H4LECQzVuDlYfblCr9zoYWhQrVhiRDGy7eLhk4X6Rp0Yoek4YUKcCQArDfZ/Vf43qfHUpOJgRpm5Oyj42HA/j4xZBb5U0Fmo6YHRPt0/KuWrDfpgJSGiN0zza7641IfADg8f3WdhlPAWMyri7k4vOZMBjlwFNcMpc0wM2TaTmbi2zqXEKZy9Oh/eJqBapFx0oNWaQ1VQ++iXxGUbZhwy7x2vd6UkqUTwYeym+aP23ee3TCtnNWN0aC",
1113
                  "sender_key":"EB9SC4jVAydKhM6/GcwMc9biKwVNywqW3TerNTrtb1M",
1114
                  "signing_key":{
1115
                     "ed25519":"1NXa5GyJ+p2ruAClEque+TL1VktrBzMW4dZFNfNGrvc"
1116
                  },
1117
                  "room_id":"!CWLUCoEWXSFyTCOtfL:matrix.org",
1118
                  "forwarding_chains":[],
1119
                  "imported":true,
1120
                  "backed_up":true
1121
               }
1122
            ],
1123
            "pickle_key": [17, 36, 120, 74, 95, 78, 56, 36, 62, 123, 5, 105, 74,
1124
                           111, 70, 48, 51, 101, 66, 86, 116, 14, 114, 85, 85,
1125
                           92, 44, 71, 89, 99, 55, 74],
1126
            "backup_version":"3",
1127
            "backup_recovery_key":"EsTHScmRV5oT1WBhe2mj2Gn3odeYantZ4NEk7L51p6L8hrmB",
1128
            "cross_signing":{
1129
               "master_key":"trnK/dBv/M2x2zZt8lnORHQqmFHWvjYE6rdlAONRUPY",
1130
               "self_signing_key":"SJhsj9jXC4hxhqS/1B3RZ65zWMHuF+1fUjWHrzVRh6w",
1131
               "user_signing_key":"LPYrV11T9Prm4ZIUxrq2a8Y/F64R1+NaGNyo6GlXjGg"
1132
            },
1133
            "tracked_users":[
1134
               "@ganfra146:matrix.org",
1135
               "@this-is-me:matrix.org",
1136
               "@Amandine:matrix.org",
1137
               "@ganfra:matrix.org",
1138
               "NotAUser%ID"
1139
            ],
1140
            "room_settings": {
1141
                "!AZkqtjvtwPAuyNOXEt:matrix.org": {
1142
                    "algorithm": "OlmV1Curve25519AesSha2",
1143
                    "only_allow_trusted_devices": true
1144
                },
1145
                "!CWLUCoEWXSFyTCOtfL:matrix.org": {
1146
                    "algorithm": "MegolmV1AesSha2",
1147
                    "only_allow_trusted_devices": false
1148
                },
1149
            }
1150
        });
1151
1152
        let migration_data: MigrationData = serde_json::from_value(data)?;
1153
1154
        let dir = tempdir()?;
1155
        let path = dir
1156
            .path()
1157
            .to_str()
1158
            .expect("Creating a string from the tempdir path should not fail")
1159
            .to_owned();
1160
1161
        migrate(migration_data, path.clone(), None, Box::new(|_, _| {}))?;
1162
1163
        let machine = OlmMachine::new(
1164
            "@ganfra146:matrix.org".to_owned(),
1165
            "DEWRCMENGS".to_owned(),
1166
            path,
1167
            None,
1168
        )?;
1169
1170
        assert_eq!(
1171
            machine.identity_keys()["ed25519"],
1172
            "JGgPQRuYj3ScMdPS+A0P+k/1qS9Hr3qeKXLscI+hS78"
1173
        );
1174
1175
        let room_keys =
1176
            machine.runtime.block_on(machine.inner.store().export_room_keys(|_| true))?;
1177
        assert_eq!(room_keys.len(), 2);
1178
1179
        let cross_signing_status = machine.cross_signing_status();
1180
        assert!(cross_signing_status.has_master);
1181
        assert!(cross_signing_status.has_user_signing);
1182
        assert!(cross_signing_status.has_self_signing);
1183
1184
        let backup_keys = machine.get_backup_keys()?;
1185
        assert!(backup_keys.is_some());
1186
1187
        let settings1 = machine.get_room_settings("!AZkqtjvtwPAuyNOXEt:matrix.org".into())?;
1188
        assert_eq!(
1189
            Some(RoomSettings {
1190
                algorithm: EventEncryptionAlgorithm::OlmV1Curve25519AesSha2,
1191
                #[cfg(feature = "experimental-encrypted-state-events")]
1192
                encrypt_state_events: false,
1193
                only_allow_trusted_devices: true
1194
            }),
1195
            settings1
1196
        );
1197
1198
        let settings2 = machine.get_room_settings("!CWLUCoEWXSFyTCOtfL:matrix.org".into())?;
1199
        assert_eq!(
1200
            Some(RoomSettings {
1201
                algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
1202
                #[cfg(feature = "experimental-encrypted-state-events")]
1203
                encrypt_state_events: false,
1204
                only_allow_trusted_devices: false
1205
            }),
1206
            settings2
1207
        );
1208
1209
        let settings3 = machine.get_room_settings("!XYZ:matrix.org".into())?;
1210
        assert!(settings3.is_none());
1211
1212
        assert!(machine.is_user_tracked("@ganfra146:matrix.org".into()).unwrap());
1213
        assert!(machine.is_user_tracked("@Amandine:matrix.org".into()).unwrap());
1214
        assert!(machine.is_user_tracked("@this-is-me:matrix.org".into()).unwrap());
1215
        assert!(machine.is_user_tracked("@ganfra:matrix.org".into()).unwrap());
1216
1217
        Ok(())
1218
    }
1219
}