matrix_sdk_crypto_ffi/
lib.rs

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
9mod backup_recovery_key;
10mod dehydrated_devices;
11mod device;
12mod error;
13mod logger;
14mod machine;
15mod responses;
16mod users;
17mod verification;
18
19use std::{
20    collections::{BTreeMap, HashMap},
21    sync::Arc,
22    time::Duration,
23};
24
25use anyhow::Context as _;
26pub use backup_recovery_key::{
27    BackupRecoveryKey, DecodeError, MegolmV1BackupKey, PassphraseInfo, PkDecryptionError,
28};
29pub use device::Device;
30pub use error::{
31    CryptoStoreError, DecryptionError, KeyImportError, SecretImportError, SignatureError,
32};
33use js_int::UInt;
34pub use logger::{set_logger, Logger};
35pub use machine::{KeyRequestPair, OlmMachine, SignatureVerification};
36use matrix_sdk_common::deserialized_responses::{ShieldState as RustShieldState, ShieldStateCode};
37use matrix_sdk_crypto::{
38    olm::{IdentityKeys, InboundGroupSession, SenderData, Session},
39    store::{
40        types::{
41            Changes, DehydratedDeviceKey as InnerDehydratedDeviceKey, PendingChanges,
42            RoomSettings as RustRoomSettings,
43        },
44        CryptoStore,
45    },
46    types::{
47        DeviceKey, DeviceKeys, EventEncryptionAlgorithm as RustEventEncryptionAlgorithm, SigningKey,
48    },
49    CollectStrategy, EncryptionSettings as RustEncryptionSettings,
50};
51use matrix_sdk_sqlite::SqliteCryptoStore;
52pub use responses::{
53    BootstrapCrossSigningResult, DeviceLists, KeysImportResult, OutgoingVerificationRequest,
54    Request, RequestType, SignatureUploadRequest, UploadSigningKeysRequest,
55};
56use ruma::{
57    events::room::history_visibility::HistoryVisibility as RustHistoryVisibility,
58    DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId,
59    RoomId, SecondsSinceUnixEpoch, UserId,
60};
61use serde::{Deserialize, Serialize};
62use tokio::runtime::Runtime;
63pub use users::UserIdentity;
64pub use verification::{
65    CancelInfo, ConfirmVerificationResult, QrCode, QrCodeListener, QrCodeState,
66    RequestVerificationResult, Sas, SasListener, SasState, ScanResult, StartSasResult,
67    Verification, VerificationRequest, VerificationRequestListener, VerificationRequestState,
68};
69use vodozemac::{Curve25519PublicKey, Ed25519PublicKey};
70
71use crate::dehydrated_devices::DehydrationError;
72
73/// Struct collecting data that is important to migrate to the rust-sdk
74#[derive(Deserialize, Serialize, uniffi::Record)]
75pub 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)]
98pub 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)]
120pub 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)]
138pub 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)]
156pub 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)]
177pub 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
186impl 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]
208pub 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
221async 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
337async 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]
371pub 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
382async 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)]
423fn 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            room_id: RoomId::parse(session.room_id)?,
510            imported: session.imported,
511            backed_up: session.backed_up,
512            history_visibility: None,
513            shared_history: false,
514            algorithm: RustEventEncryptionAlgorithm::MegolmV1AesSha2,
515        };
516
517        let session = matrix_sdk_crypto::olm::InboundGroupSession::from_pickle(pickle)?;
518
519        inbound_group_sessions.push(session);
520        processed_steps += 1;
521        listener(processed_steps, total_steps);
522    }
523
524    Ok((sessions, inbound_group_sessions))
525}
526
527/// Migrate room settings, including room algorithm and whether to block
528/// untrusted devices from legacy store to Sqlite store.
529///
530/// Note that this method should only be used if a client has already migrated
531/// account data via [migrate](#method.migrate) method, which did not include
532/// room settings. For a brand new migration, the [migrate](#method.migrate)
533/// method will take care of room settings automatically, if provided.
534///
535/// # Arguments
536///
537/// * `room_settings` - Map of room settings
538///
539/// * `path` - The path where the Sqlite store should be created.
540///
541/// * `passphrase` - The passphrase that should be used to encrypt the data at
542///   rest in the Sqlite store. **Warning**, if no passphrase is given, the
543///   store and all its data will remain unencrypted.
544#[matrix_sdk_ffi_macros::export]
545pub fn migrate_room_settings(
546    room_settings: HashMap<String, RoomSettings>,
547    path: String,
548    passphrase: Option<String>,
549) -> Result<(), MigrationError> {
550    let runtime = Runtime::new().context("initializing tokio runtime")?;
551    runtime.block_on(async move {
552        let store = SqliteCryptoStore::open(path, passphrase.as_deref())
553            .await
554            .context("opening sqlite crypto store")?;
555
556        let mut rust_settings = HashMap::new();
557        for (room_id, settings) in room_settings {
558            let room_id = RoomId::parse(room_id).context("parsing room ID")?;
559            rust_settings.insert(room_id, settings.into());
560        }
561
562        let changes = Changes { room_settings: rust_settings, ..Default::default() };
563        store.save_changes(changes).await.context("saving changes")?;
564
565        Ok(())
566    })
567}
568
569/// Callback that will be passed over the FFI to report progress
570#[matrix_sdk_ffi_macros::export(callback_interface)]
571pub trait ProgressListener {
572    /// The callback that should be called on the Rust side
573    ///
574    /// # Arguments
575    ///
576    /// * `progress` - The current number of items that have been handled
577    ///
578    /// * `total` - The total number of items that will be handled
579    fn on_progress(&self, progress: i32, total: i32);
580}
581
582impl<T: Fn(i32, i32)> ProgressListener for T {
583    fn on_progress(&self, progress: i32, total: i32) {
584        self(progress, total)
585    }
586}
587
588/// An encryption algorithm to be used to encrypt messages sent to a room.
589#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Enum)]
590pub enum EventEncryptionAlgorithm {
591    /// Olm version 1 using Curve25519, AES-256, and SHA-256.
592    OlmV1Curve25519AesSha2,
593    /// Megolm version 1 using AES-256 and SHA-256.
594    MegolmV1AesSha2,
595}
596
597impl From<EventEncryptionAlgorithm> for RustEventEncryptionAlgorithm {
598    fn from(a: EventEncryptionAlgorithm) -> Self {
599        match a {
600            EventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => Self::OlmV1Curve25519AesSha2,
601            EventEncryptionAlgorithm::MegolmV1AesSha2 => Self::MegolmV1AesSha2,
602        }
603    }
604}
605
606impl TryFrom<RustEventEncryptionAlgorithm> for EventEncryptionAlgorithm {
607    type Error = serde_json::Error;
608
609    fn try_from(value: RustEventEncryptionAlgorithm) -> Result<Self, Self::Error> {
610        match value {
611            RustEventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => {
612                Ok(Self::OlmV1Curve25519AesSha2)
613            }
614            RustEventEncryptionAlgorithm::MegolmV1AesSha2 => Ok(Self::MegolmV1AesSha2),
615            _ => Err(serde::de::Error::custom(format!("Unsupported algorithm {value}"))),
616        }
617    }
618}
619
620/// Who can see a room's history.
621#[derive(uniffi::Enum)]
622pub enum HistoryVisibility {
623    /// Previous events are accessible to newly joined members from the point
624    /// they were invited onwards.
625    ///
626    /// Events stop being accessible when the member's state changes to
627    /// something other than *invite* or *join*.
628    Invited,
629
630    /// Previous events are accessible to newly joined members from the point
631    /// they joined the room onwards.
632    /// Events stop being accessible when the member's state changes to
633    /// something other than *join*.
634    Joined,
635
636    /// Previous events are always accessible to newly joined members.
637    ///
638    /// All events in the room are accessible, even those sent when the member
639    /// was not a part of the room.
640    Shared,
641
642    /// All events while this is the `HistoryVisibility` value may be shared by
643    /// any participating homeserver with anyone, regardless of whether they
644    /// have ever joined the room.
645    WorldReadable,
646}
647
648impl From<HistoryVisibility> for RustHistoryVisibility {
649    fn from(h: HistoryVisibility) -> Self {
650        match h {
651            HistoryVisibility::Invited => Self::Invited,
652            HistoryVisibility::Joined => Self::Joined,
653            HistoryVisibility::Shared => Self::Shared,
654            HistoryVisibility::WorldReadable => Self::Shared,
655        }
656    }
657}
658
659/// Settings that should be used when a room key is shared.
660///
661/// These settings control which algorithm the room key should use, how long a
662/// room key should be used and some other important information that determines
663/// the lifetime of a room key.
664#[derive(uniffi::Record)]
665pub struct EncryptionSettings {
666    /// The encryption algorithm that should be used in the room.
667    pub algorithm: EventEncryptionAlgorithm,
668    /// Whether state event encryption is enabled.
669    #[cfg(feature = "experimental-encrypted-state-events")]
670    pub encrypt_state_events: bool,
671    /// How long can the room key be used before it should be rotated. Time in
672    /// seconds.
673    pub rotation_period: u64,
674    /// How many messages should be sent before the room key should be rotated.
675    pub rotation_period_msgs: u64,
676    /// The current history visibility of the room. The visibility will be
677    /// tracked by the room key and the key will be rotated if the visibility
678    /// changes.
679    pub history_visibility: HistoryVisibility,
680    /// Should untrusted devices receive the room key, or should they be
681    /// excluded from the conversation.
682    pub only_allow_trusted_devices: bool,
683    /// Should fail to send when a verified user has unverified devices, or when
684    /// a previously verified user replaces their identity.
685    pub error_on_verified_user_problem: bool,
686}
687
688impl From<EncryptionSettings> for RustEncryptionSettings {
689    fn from(v: EncryptionSettings) -> Self {
690        let sharing_strategy = if v.only_allow_trusted_devices {
691            CollectStrategy::OnlyTrustedDevices
692        } else if v.error_on_verified_user_problem {
693            CollectStrategy::ErrorOnVerifiedUserProblem
694        } else {
695            CollectStrategy::AllDevices
696        };
697
698        RustEncryptionSettings {
699            algorithm: v.algorithm.into(),
700            #[cfg(feature = "experimental-encrypted-state-events")]
701            encrypt_state_events: false,
702            rotation_period: Duration::from_secs(v.rotation_period),
703            rotation_period_msgs: v.rotation_period_msgs,
704            history_visibility: v.history_visibility.into(),
705            sharing_strategy,
706        }
707    }
708}
709
710/// An event that was successfully decrypted.
711#[derive(uniffi::Record)]
712pub struct DecryptedEvent {
713    /// The decrypted version of the event.
714    pub clear_event: String,
715    /// The claimed curve25519 key of the sender.
716    pub sender_curve25519_key: String,
717    /// The claimed ed25519 key of the sender.
718    pub claimed_ed25519_key: Option<String>,
719    /// The curve25519 chain of the senders that forwarded the Megolm decryption
720    /// key to us. Is empty if the key came directly from the sender of the
721    /// event.
722    pub forwarding_curve25519_chain: Vec<String>,
723    /// The shield state (color and message to display to user) for the event,
724    /// representing the event's authenticity. Computed from the properties of
725    /// the sender user identity and their Olm device.
726    ///
727    /// Note that this is computed at time of decryption, so the value reflects
728    /// the computed event authenticity at that time. Authenticity-related
729    /// properties can change later on, such as when a user identity is
730    /// subsequently verified or a device is deleted.
731    pub shield_state: ShieldState,
732}
733
734/// Take a look at [`matrix_sdk_common::deserialized_responses::ShieldState`]
735/// for more info.
736#[allow(missing_docs)]
737#[derive(uniffi::Enum)]
738pub enum ShieldColor {
739    Red,
740    Grey,
741    None,
742}
743
744/// Take a look at [`matrix_sdk_common::deserialized_responses::ShieldState`]
745/// for more info.
746#[derive(uniffi::Record)]
747#[allow(missing_docs)]
748pub struct ShieldState {
749    color: ShieldColor,
750    code: Option<ShieldStateCode>,
751    message: Option<String>,
752}
753
754impl From<RustShieldState> for ShieldState {
755    fn from(value: RustShieldState) -> Self {
756        match value {
757            RustShieldState::Red { code, message } => Self {
758                color: ShieldColor::Red,
759                code: Some(code),
760                message: Some(message.to_owned()),
761            },
762            RustShieldState::Grey { code, message } => Self {
763                color: ShieldColor::Grey,
764                code: Some(code),
765                message: Some(message.to_owned()),
766            },
767            RustShieldState::None => Self { color: ShieldColor::None, code: None, message: None },
768        }
769    }
770}
771
772/// Struct representing the state of our private cross signing keys, it shows
773/// which private cross signing keys we have locally stored.
774#[derive(Debug, Clone, uniffi::Record)]
775pub struct CrossSigningStatus {
776    /// Do we have the master key.
777    pub has_master: bool,
778    /// Do we have the self signing key, this one is necessary to sign our own
779    /// devices.
780    pub has_self_signing: bool,
781    /// Do we have the user signing key, this one is necessary to sign other
782    /// users.
783    pub has_user_signing: bool,
784}
785
786/// A struct containing private cross signing keys that can be backed up or
787/// uploaded to the secret store.
788#[derive(Deserialize, Serialize, uniffi::Record)]
789pub struct CrossSigningKeyExport {
790    /// The seed of the master key encoded as unpadded base64.
791    pub master_key: Option<String>,
792    /// The seed of the self signing key encoded as unpadded base64.
793    pub self_signing_key: Option<String>,
794    /// The seed of the user signing key encoded as unpadded base64.
795    pub user_signing_key: Option<String>,
796}
797
798/// Struct holding the number of room keys we have.
799#[derive(uniffi::Record)]
800pub struct RoomKeyCounts {
801    /// The total number of room keys.
802    pub total: i64,
803    /// The number of backed up room keys.
804    pub backed_up: i64,
805}
806
807/// Backup keys and information we load from the store.
808#[derive(uniffi::Object)]
809pub struct BackupKeys {
810    /// The recovery key as a base64 encoded string.
811    recovery_key: Arc<BackupRecoveryKey>,
812    /// The version that is used with the recovery key.
813    backup_version: String,
814}
815
816#[matrix_sdk_ffi_macros::export]
817impl BackupKeys {
818    /// Get the recovery key that we're holding on to.
819    pub fn recovery_key(&self) -> Arc<BackupRecoveryKey> {
820        self.recovery_key.clone()
821    }
822
823    /// Get the backups version that we're holding on to.
824    pub fn backup_version(&self) -> String {
825        self.backup_version.to_owned()
826    }
827}
828
829impl TryFrom<matrix_sdk_crypto::store::types::BackupKeys> for BackupKeys {
830    type Error = ();
831
832    fn try_from(keys: matrix_sdk_crypto::store::types::BackupKeys) -> Result<Self, Self::Error> {
833        Ok(Self {
834            recovery_key: BackupRecoveryKey {
835                inner: keys.decryption_key.ok_or(())?,
836                passphrase_info: None,
837            }
838            .into(),
839            backup_version: keys.backup_version.ok_or(())?,
840        })
841    }
842}
843
844/// Dehydrated device key
845#[derive(uniffi::Record, Clone)]
846pub struct DehydratedDeviceKey {
847    pub(crate) inner: Vec<u8>,
848}
849
850impl DehydratedDeviceKey {
851    /// Generates a new random pickle key.
852    pub fn new() -> Result<Self, DehydrationError> {
853        let inner = InnerDehydratedDeviceKey::new()?;
854        Ok(inner.into())
855    }
856
857    /// Creates a new dehydration pickle key from the given slice.
858    ///
859    /// Fail if the slice length is not 32.
860    pub fn from_slice(slice: &[u8]) -> Result<Self, DehydrationError> {
861        let inner = InnerDehydratedDeviceKey::from_slice(slice)?;
862        Ok(inner.into())
863    }
864
865    /// Export the [`DehydratedDeviceKey`] as a base64 encoded string.
866    pub fn to_base64(&self) -> String {
867        let inner = InnerDehydratedDeviceKey::from_slice(&self.inner).unwrap();
868        inner.to_base64()
869    }
870}
871impl From<InnerDehydratedDeviceKey> for DehydratedDeviceKey {
872    fn from(pickle_key: InnerDehydratedDeviceKey) -> Self {
873        DehydratedDeviceKey { inner: pickle_key.into() }
874    }
875}
876
877impl From<matrix_sdk_crypto::store::types::RoomKeyCounts> for RoomKeyCounts {
878    fn from(count: matrix_sdk_crypto::store::types::RoomKeyCounts) -> Self {
879        Self { total: count.total as i64, backed_up: count.backed_up as i64 }
880    }
881}
882
883impl From<matrix_sdk_crypto::CrossSigningKeyExport> for CrossSigningKeyExport {
884    fn from(e: matrix_sdk_crypto::CrossSigningKeyExport) -> Self {
885        Self {
886            master_key: e.master_key.clone(),
887            self_signing_key: e.self_signing_key.clone(),
888            user_signing_key: e.user_signing_key.clone(),
889        }
890    }
891}
892
893impl From<CrossSigningKeyExport> for matrix_sdk_crypto::CrossSigningKeyExport {
894    fn from(e: CrossSigningKeyExport) -> Self {
895        matrix_sdk_crypto::CrossSigningKeyExport {
896            master_key: e.master_key,
897            self_signing_key: e.self_signing_key,
898            user_signing_key: e.user_signing_key,
899        }
900    }
901}
902
903impl From<matrix_sdk_crypto::CrossSigningStatus> for CrossSigningStatus {
904    fn from(s: matrix_sdk_crypto::CrossSigningStatus) -> Self {
905        Self {
906            has_master: s.has_master,
907            has_self_signing: s.has_self_signing,
908            has_user_signing: s.has_user_signing,
909        }
910    }
911}
912
913/// Room encryption settings which are modified by state events or user options
914#[derive(Debug, PartialEq, Deserialize, Serialize, uniffi::Record)]
915pub struct RoomSettings {
916    /// The encryption algorithm that should be used in the room.
917    pub algorithm: EventEncryptionAlgorithm,
918    /// Whether state event encryption is enabled.
919    #[cfg(feature = "experimental-encrypted-state-events")]
920    #[serde(default)]
921    pub encrypt_state_events: bool,
922    /// Should untrusted devices receive the room key, or should they be
923    /// excluded from the conversation.
924    pub only_allow_trusted_devices: bool,
925}
926
927impl TryFrom<RustRoomSettings> for RoomSettings {
928    type Error = serde_json::Error;
929
930    fn try_from(value: RustRoomSettings) -> Result<Self, Self::Error> {
931        let algorithm = value.algorithm.try_into()?;
932        Ok(Self {
933            algorithm,
934            #[cfg(feature = "experimental-encrypted-state-events")]
935            encrypt_state_events: value.encrypt_state_events,
936            only_allow_trusted_devices: value.only_allow_trusted_devices,
937        })
938    }
939}
940
941impl From<RoomSettings> for RustRoomSettings {
942    fn from(value: RoomSettings) -> Self {
943        Self {
944            algorithm: value.algorithm.into(),
945            only_allow_trusted_devices: value.only_allow_trusted_devices,
946            ..RustRoomSettings::default()
947        }
948    }
949}
950
951fn parse_user_id(user_id: &str) -> Result<OwnedUserId, CryptoStoreError> {
952    ruma::UserId::parse(user_id).map_err(|e| CryptoStoreError::InvalidUserId(user_id.to_owned(), e))
953}
954
955#[matrix_sdk_ffi_macros::export]
956fn version_info() -> VersionInfo {
957    VersionInfo {
958        version: matrix_sdk_crypto::VERSION.to_owned(),
959        vodozemac_version: matrix_sdk_crypto::vodozemac::VERSION.to_owned(),
960        git_description: env!("VERGEN_GIT_DESCRIBE").to_owned(),
961        git_sha: env!("VERGEN_GIT_SHA").to_owned(),
962    }
963}
964
965/// Build-time information about important crates that are used.
966#[derive(uniffi::Record)]
967pub struct VersionInfo {
968    /// The version of the matrix-sdk-crypto crate.
969    pub version: String,
970    /// The version of the vodozemac crate.
971    pub vodozemac_version: String,
972    /// The Git commit hash of the crate's source tree at build time.
973    pub git_sha: String,
974    /// The build-time output of the `git describe` command of the source tree
975    /// of crate.
976    pub git_description: String,
977}
978
979#[matrix_sdk_ffi_macros::export]
980fn version() -> String {
981    matrix_sdk_crypto::VERSION.to_owned()
982}
983
984#[matrix_sdk_ffi_macros::export]
985fn vodozemac_version() -> String {
986    vodozemac::VERSION.to_owned()
987}
988
989/// The encryption component of PkEncryption support.
990///
991/// This struct can be created using a [`Curve25519PublicKey`] corresponding to
992/// a `PkDecryption` object, allowing messages to be encrypted for the
993/// associated decryption object.
994#[derive(uniffi::Object)]
995pub struct PkEncryption {
996    inner: matrix_sdk_crypto::vodozemac::pk_encryption::PkEncryption,
997}
998
999#[matrix_sdk_ffi_macros::export]
1000impl PkEncryption {
1001    /// Create a new [`PkEncryption`] object from a `Curve25519PublicKey`
1002    /// encoded as Base64.
1003    ///
1004    /// The public key should come from an existing `PkDecryption` object.
1005    /// Returns a `DecodeError` if the Curve25519 key could not be decoded
1006    /// correctly.
1007    #[uniffi::constructor]
1008    pub fn from_base64(key: &str) -> Result<Arc<Self>, DecodeError> {
1009        let key = vodozemac::Curve25519PublicKey::from_base64(key)
1010            .map_err(matrix_sdk_crypto::backups::DecodeError::PublicKey)?;
1011        let inner = vodozemac::pk_encryption::PkEncryption::from_key(key);
1012
1013        Ok(Self { inner }.into())
1014    }
1015
1016    /// Encrypt a message using this [`PkEncryption`] object.
1017    pub fn encrypt(&self, plaintext: &str) -> PkMessage {
1018        use vodozemac::base64_encode;
1019
1020        let message = self.inner.encrypt(plaintext.as_ref());
1021
1022        let vodozemac::pk_encryption::Message { ciphertext, mac, ephemeral_key } = message;
1023
1024        PkMessage {
1025            ciphertext: base64_encode(ciphertext),
1026            mac: base64_encode(mac),
1027            ephemeral_key: ephemeral_key.to_base64(),
1028        }
1029    }
1030}
1031
1032/// A message that was encrypted using a [`PkEncryption`] object.
1033#[derive(uniffi::Record)]
1034pub struct PkMessage {
1035    /// The ciphertext of the message.
1036    pub ciphertext: String,
1037    /// The message authentication code of the message.
1038    ///
1039    /// *Warning*: This does not authenticate the ciphertext.
1040    pub mac: String,
1041    /// The ephemeral Curve25519 key of the message which was used to derive the
1042    /// individual message key.
1043    pub ephemeral_key: String,
1044}
1045
1046uniffi::setup_scaffolding!();
1047
1048#[cfg(test)]
1049mod tests {
1050    use anyhow::Result;
1051    use serde_json::{json, Value};
1052    use tempfile::tempdir;
1053
1054    use super::MigrationData;
1055    use crate::{migrate, EventEncryptionAlgorithm, OlmMachine, RoomSettings};
1056
1057    #[test]
1058    fn android_migration() -> Result<()> {
1059        let data: Value = json!({
1060            "account":{
1061               "user_id":"@ganfra146:matrix.org",
1062               "device_id":"DEWRCMENGS",
1063               "pickle":"FFGTGho89T3Xgd56l+EedOPV37s09RR8aYnS9305qPKF66LG+ly29YpCibjJOvkwm0dZwN9A2bOH/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peAQNZBNAJHHfv8IzIYxPhMJdYq0YqLIGSUYu87D04cDOxJY7hgawYs+ExOWb7XkbpuRoITQd8zpwVDFlSCS+wFO+qah3Vn8RBTc6cXHO5xRWfUNj+NrEtPdVmax+9EXqXtHQyFpxaauvL96RH+mGwpKHOk3aisXbZ6gLE2mF4egGjjJOIJdHyb2ZR+kj+4GIvkoBwipDgUfr4UBXY8pvFxQOxRgtI4LgOY9Z1Aco7Mwp6qi1KoMFJW8d+gJwsgM3cMsyEeYH1n/mdpJW6VDbIWzOHkP5n+OKKNm2vJTkQFFwF9eOtGy9fNBtS4qo4jvOUJnnAPsrPbGMbBYd1dMC3daHLEwvIKCAVBn7q1Z2c4zAD5eEoY0EwZj/j8x8lGQ8TswFT81ZotW7ZBDai/YtV8mkGfuaWJRI5yHc/bV7GWLF+yrMji/jicBF5jy2UoqwxseqjgTut49FRgBH3h1qwnfYbXD3FvQljyAAgBCiZV726pFRG+sZv0FjDbq0iCKILVSEUDZgmQ",
1064               "shared":true,
1065               "uploaded_signed_key_count":50
1066            },
1067            "sessions":[
1068               {
1069                  "pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4s+SdrKUYAMUdGcYD7QukrPklEOy7fJho9YGK/jV04QdA8JABiOfD+ngJTR4V8eZdmDuG08+Q5EL79V81hQwU2fKndP0y/9nAXPUIADYq0Zrg4EsOnXz7aE+hAeBAm0IBog1s8RYUvynZ15uwjbd/OTLP+gpqpX33DwVg2leiBkQetiUSpOpZCuQ8CcZwIA0MoGCqvaT7h76VHX9JxJx+2fCMhsJMx1nhd99WJH1W9ge5CtdbC4KUP92OSxIrPOnMrNcOPJPp/paZP+HFNQ3PDL+z8pGKXmCnrXGSbd7iPHurPYESrVkBzr",
1070                  "sender_key":"WJ6Ce7U67a6jqkHYHd8o0+5H4bqdi9hInZdk0+swuXs",
1071                  "created_using_fallback_key":false,
1072                  "creation_time": 1649425011424u64,
1073                  "last_use_time": 1649425011424u64
1074               },
1075               {
1076                  "pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4t2W/lowyrV6SXVZp+uG59im0AAfNSKjhjZuiOpQlX7MS+AOJkCNvyujJ2g3KSjLZ94IkoHxkBDHLWSjwaLPu40rfOzJPDpm0XZsR6bQrsxKOmXLGEw2qw5jOTouzMVL2gvuuTix97nSYSU8j3XvTMRUoh0AF/tUpRLcvEFZeGrdUYmTMlyTv4na+FVUalUZ+jrk8t1/sM99JNq3SY1IBSjrBq/0rCOHieiippz0sw2fe2b87id4rqj1g3R9w2MWTWEdOz3ugjMGYF1YDBQZA1tJZ/hmgppk2AU2xKQXE2X3DgSC6fC66D4",
1077                  "sender_key":"RzRROfmHNlBfzxnNCUYBfn/5oZNQ11XYjDg59hS+mV0",
1078                  "created_using_fallback_key":false,
1079                  "creation_time": 1649425011503u64,
1080                  "last_use_time": 1649425011503u64
1081               },
1082               {
1083                  "pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4titbL3SS12PYHpcBPJc6hXnOnZXqrjtjYOD545fck+3utEo8cqqwWubc9tsvxGW3tOWPttLBdAW30Vn8V1M8ebqVCNVWEAb1GKjV4ni8xG7G9SlEcCjLjnF4lJpddSZkqVMFoN0ITr9aSz/eJwXpc3HLreUFXwc8LuQp7krQ4Vt1e5EE/klduqsdurZf5V14RHsmWz2lKjt7nVgtIz/dhtF5F/sGJdg8kCGaHIMSbGAPuPPpa4/Laicb/5otrYt4pg4W4KdFpSGJIcvUQNjXaOZMx3cu/RPJIOyNhx7whG1QiYAUBqAJvr",
1084                  "sender_key":"IXSZugAHig1v8MowE1jxi2wDDDfuZBeJynHlegJVwUc",
1085                  "created_using_fallback_key":false,
1086                  "creation_time": 1649425011566u64,
1087                  "last_use_time": 1649425011566u64
1088               },
1089               {
1090                  "pickle":"SmkDiFZjNukiarQ7XHQo25FILHsuhNOnxy56cMSQU/Y71jaGbJes4YrvN4Dfy4RSONfejEDXDkbW2JudlHHRP/rWEmnfJiGbK6ArbrG2puqIZgOecPnOUgPfCisr49p1Gmf36dPaO5lm/ZSrngfSoxahoeJJE/CcJN98sYM15XytRk2LBwc+CyYDqr4V1qxfsBt6tzJ4+tsAZeRdD0UtipQgysgH56o8N7nKTCkaZz5lfpYCl3FEgwXpLJ0MGQvtQmbORFvOLqR1jZ/EbmNGKiqDDIYsqG0sf78ii1jqfpLDBXLuYDccsg",
1091                  "sender_key":"EB9SC4jVAydKhM6/GcwMc9biKwVNywqW3TerNTrtb1M",
1092                  "created_using_fallback_key":false,
1093                  "creation_time": 1649542063182u64,
1094                  "last_use_time": 1649542063182u64
1095               }
1096            ],
1097            "inbound_group_sessions":[
1098               {
1099                  "pickle":"KoA0sxDNQ7lz0vylU9zlmar0VCVQRMCfRrIfTh1bdMhlAgy8/D2ToT+oKRaKy1HiW6H862bzdpgxprlseSjmip9OfLbIuyd2YZcEinwc2666oEI/hpk4oTlE61uE1M+ThfdFf41yGCmaAP7mhjwF234ZrZ6i/F/qx42TLQ8Unc30wDJaJgyheO5eW85SD/0g0cdg2WnEKrx2/wl7Vg/YijT3JMDZ+OsdfJfSZtxBNjlG+PQ/9D31qb1eHfaovc8vFZh5QLfAFg/5rBrF1PhRsC7xOAZbUNdrTbvypNfMM4mznf84C2SzZRSMeAfg5v/YticM3Keg4eHuEj1WO9DrmRXYl6b/pITdf1xuk5euVT0pyxJpXmq41AoAZKAo1l94HGy1LG1RpruD1uQPhiponh5PGHSOf43Q",
1100                  "sender_key":"vJfH7wiYmGos3C8U1XcJ//YWSmkueAYqrmUA6/ukfAM",
1101                  "signing_key":{
1102                     "ed25519":"JGgPQRuYj3ScMdPS+A0P+k/1qS9Hr3qeKXLscI+hS78"
1103                  },
1104                  "room_id":"!AZkqtjvtwPAuyNOXEt:matrix.org",
1105                  "forwarding_chains":[
1106                  ],
1107                  "imported":true,
1108                  "backed_up":true
1109               },
1110               {
1111                  "pickle":"9RF6GBu9CvjZZx2hxIlw2gMdKs36LFhXhLTHAPrLSjT2OTbeE/jK263+iiFdSpF7Cblp/lXzljPKJN6sL8JGzoT7ssYh56nI0kKsp7/y88z+tTOH/5NYYTmZzHYw6yy4Cmaxh0pdHDs+RQpSSIe9jhF/EJJna5jcKYXxDY52m8H4LECQzVuDlYfblCr9zoYWhQrVhiRDGy7eLhk4X6Rp0Yoek4YUKcCQArDfZ/Vf43qfHUpOJgRpm5Oyj42HA/j4xZBb5U0Fmo6YHRPt0/KuWrDfpgJSGiN0zza7641IfADg8f3WdhlPAWMyri7k4vOZMBjlwFNcMpc0wM2TaTmbi2zqXEKZy9Oh/eJqBapFx0oNWaQ1VQ++iXxGUbZhwy7x2vd6UkqUTwYeym+aP23ee3TCtnNWN0aC",
1112                  "sender_key":"EB9SC4jVAydKhM6/GcwMc9biKwVNywqW3TerNTrtb1M",
1113                  "signing_key":{
1114                     "ed25519":"1NXa5GyJ+p2ruAClEque+TL1VktrBzMW4dZFNfNGrvc"
1115                  },
1116                  "room_id":"!CWLUCoEWXSFyTCOtfL:matrix.org",
1117                  "forwarding_chains":[],
1118                  "imported":true,
1119                  "backed_up":true
1120               }
1121            ],
1122            "pickle_key": [17, 36, 120, 74, 95, 78, 56, 36, 62, 123, 5, 105, 74,
1123                           111, 70, 48, 51, 101, 66, 86, 116, 14, 114, 85, 85,
1124                           92, 44, 71, 89, 99, 55, 74],
1125            "backup_version":"3",
1126            "backup_recovery_key":"EsTHScmRV5oT1WBhe2mj2Gn3odeYantZ4NEk7L51p6L8hrmB",
1127            "cross_signing":{
1128               "master_key":"trnK/dBv/M2x2zZt8lnORHQqmFHWvjYE6rdlAONRUPY",
1129               "self_signing_key":"SJhsj9jXC4hxhqS/1B3RZ65zWMHuF+1fUjWHrzVRh6w",
1130               "user_signing_key":"LPYrV11T9Prm4ZIUxrq2a8Y/F64R1+NaGNyo6GlXjGg"
1131            },
1132            "tracked_users":[
1133               "@ganfra146:matrix.org",
1134               "@this-is-me:matrix.org",
1135               "@Amandine:matrix.org",
1136               "@ganfra:matrix.org",
1137               "NotAUser%ID"
1138            ],
1139            "room_settings": {
1140                "!AZkqtjvtwPAuyNOXEt:matrix.org": {
1141                    "algorithm": "OlmV1Curve25519AesSha2",
1142                    "only_allow_trusted_devices": true
1143                },
1144                "!CWLUCoEWXSFyTCOtfL:matrix.org": {
1145                    "algorithm": "MegolmV1AesSha2",
1146                    "only_allow_trusted_devices": false
1147                },
1148            }
1149        });
1150
1151        let migration_data: MigrationData = serde_json::from_value(data)?;
1152
1153        let dir = tempdir()?;
1154        let path = dir
1155            .path()
1156            .to_str()
1157            .expect("Creating a string from the tempdir path should not fail")
1158            .to_owned();
1159
1160        migrate(migration_data, path.clone(), None, Box::new(|_, _| {}))?;
1161
1162        let machine = OlmMachine::new(
1163            "@ganfra146:matrix.org".to_owned(),
1164            "DEWRCMENGS".to_owned(),
1165            path,
1166            None,
1167        )?;
1168
1169        assert_eq!(
1170            machine.identity_keys()["ed25519"],
1171            "JGgPQRuYj3ScMdPS+A0P+k/1qS9Hr3qeKXLscI+hS78"
1172        );
1173
1174        let room_keys =
1175            machine.runtime.block_on(machine.inner.store().export_room_keys(|_| true))?;
1176        assert_eq!(room_keys.len(), 2);
1177
1178        let cross_signing_status = machine.cross_signing_status();
1179        assert!(cross_signing_status.has_master);
1180        assert!(cross_signing_status.has_user_signing);
1181        assert!(cross_signing_status.has_self_signing);
1182
1183        let backup_keys = machine.get_backup_keys()?;
1184        assert!(backup_keys.is_some());
1185
1186        let settings1 = machine.get_room_settings("!AZkqtjvtwPAuyNOXEt:matrix.org".into())?;
1187        assert_eq!(
1188            Some(RoomSettings {
1189                algorithm: EventEncryptionAlgorithm::OlmV1Curve25519AesSha2,
1190                #[cfg(feature = "experimental-encrypted-state-events")]
1191                encrypt_state_events: false,
1192                only_allow_trusted_devices: true
1193            }),
1194            settings1
1195        );
1196
1197        let settings2 = machine.get_room_settings("!CWLUCoEWXSFyTCOtfL:matrix.org".into())?;
1198        assert_eq!(
1199            Some(RoomSettings {
1200                algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
1201                #[cfg(feature = "experimental-encrypted-state-events")]
1202                encrypt_state_events: false,
1203                only_allow_trusted_devices: false
1204            }),
1205            settings2
1206        );
1207
1208        let settings3 = machine.get_room_settings("!XYZ:matrix.org".into())?;
1209        assert!(settings3.is_none());
1210
1211        assert!(machine.is_user_tracked("@ganfra146:matrix.org".into()).unwrap());
1212        assert!(machine.is_user_tracked("@Amandine:matrix.org".into()).unwrap());
1213        assert!(machine.is_user_tracked("@this-is-me:matrix.org".into()).unwrap());
1214        assert!(machine.is_user_tracked("@ganfra:matrix.org".into()).unwrap());
1215
1216        Ok(())
1217    }
1218}