#![warn(missing_docs)]
#![allow(unused_qualifications)]
mod backup_recovery_key;
mod dehydrated_devices;
mod device;
mod error;
mod logger;
mod machine;
mod responses;
mod users;
mod verification;
use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
time::Duration,
};
use anyhow::Context as _;
pub use backup_recovery_key::{
BackupRecoveryKey, DecodeError, MegolmV1BackupKey, PassphraseInfo, PkDecryptionError,
};
pub use device::Device;
pub use error::{
CryptoStoreError, DecryptionError, KeyImportError, SecretImportError, SignatureError,
};
use js_int::UInt;
pub use logger::{set_logger, Logger};
pub use machine::{KeyRequestPair, OlmMachine, SignatureVerification};
use matrix_sdk_common::deserialized_responses::{ShieldState as RustShieldState, ShieldStateCode};
use matrix_sdk_crypto::{
olm::{IdentityKeys, InboundGroupSession, SenderData, Session},
store::{
Changes, CryptoStore, DehydratedDeviceKey as InnerDehydratedDeviceKey, PendingChanges,
RoomSettings as RustRoomSettings,
},
types::{
DeviceKey, DeviceKeys, EventEncryptionAlgorithm as RustEventEncryptionAlgorithm, SigningKey,
},
CollectStrategy, EncryptionSettings as RustEncryptionSettings,
};
use matrix_sdk_sqlite::SqliteCryptoStore;
pub use responses::{
BootstrapCrossSigningResult, DeviceLists, KeysImportResult, OutgoingVerificationRequest,
Request, RequestType, SignatureUploadRequest, UploadSigningKeysRequest,
};
use ruma::{
events::room::history_visibility::HistoryVisibility as RustHistoryVisibility,
DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId,
RoomId, SecondsSinceUnixEpoch, UserId,
};
use serde::{Deserialize, Serialize};
use tokio::runtime::Runtime;
pub use users::UserIdentity;
pub use verification::{
CancelInfo, ConfirmVerificationResult, QrCode, QrCodeListener, QrCodeState,
RequestVerificationResult, Sas, SasListener, SasState, ScanResult, StartSasResult,
Verification, VerificationRequest, VerificationRequestListener, VerificationRequestState,
};
use vodozemac::{Curve25519PublicKey, Ed25519PublicKey};
use crate::dehydrated_devices::DehydrationError;
#[derive(Deserialize, Serialize, uniffi::Record)]
pub struct MigrationData {
account: PickledAccount,
sessions: Vec<PickledSession>,
inbound_group_sessions: Vec<PickledInboundGroupSession>,
pickle_key: Vec<u8>,
backup_version: Option<String>,
backup_recovery_key: Option<String>,
cross_signing: CrossSigningKeyExport,
tracked_users: Vec<String>,
room_settings: HashMap<String, RoomSettings>,
}
#[derive(uniffi::Record)]
pub struct SessionMigrationData {
user_id: String,
device_id: String,
curve25519_key: String,
ed25519_key: String,
sessions: Vec<PickledSession>,
inbound_group_sessions: Vec<PickledInboundGroupSession>,
pickle_key: Vec<u8>,
}
#[derive(Debug, Deserialize, Serialize, uniffi::Record)]
pub struct PickledAccount {
pub user_id: String,
pub device_id: String,
pub pickle: String,
pub shared: bool,
pub uploaded_signed_key_count: i64,
}
#[derive(Debug, Deserialize, Serialize, uniffi::Record)]
pub struct PickledSession {
pub pickle: String,
pub sender_key: String,
pub created_using_fallback_key: bool,
pub creation_time: u64,
pub last_use_time: u64,
}
#[derive(Debug, Deserialize, Serialize, uniffi::Record)]
pub struct PickledInboundGroupSession {
pub pickle: String,
pub sender_key: String,
pub signing_key: HashMap<String, String>,
pub room_id: String,
pub forwarding_chains: Vec<String>,
pub imported: bool,
pub backed_up: bool,
}
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum MigrationError {
#[error("error migrating database: {error_message}")]
Generic {
error_message: String,
},
}
impl From<anyhow::Error> for MigrationError {
fn from(e: anyhow::Error) -> MigrationError {
MigrationError::Generic { error_message: e.to_string() }
}
}
#[matrix_sdk_ffi_macros::export]
pub fn migrate(
data: MigrationData,
path: String,
passphrase: Option<String>,
progress_listener: Box<dyn ProgressListener>,
) -> Result<(), MigrationError> {
let runtime = Runtime::new().context("initializing tokio runtime")?;
runtime.block_on(async move {
migrate_data(data, &path, passphrase, progress_listener).await?;
Ok(())
})
}
async fn migrate_data(
mut data: MigrationData,
path: &str,
passphrase: Option<String>,
progress_listener: Box<dyn ProgressListener>,
) -> anyhow::Result<()> {
use matrix_sdk_crypto::{olm::PrivateCrossSigningIdentity, store::BackupDecryptionKey};
use vodozemac::olm::Account;
use zeroize::Zeroize;
let total_steps = 5 + data.sessions.len() + data.inbound_group_sessions.len();
let mut processed_steps = 0;
let listener = |progress: usize, total: usize| {
progress_listener.on_progress(progress as i32, total as i32)
};
let store = SqliteCryptoStore::open(path, passphrase.as_deref()).await?;
processed_steps += 1;
listener(processed_steps, total_steps);
let user_id = parse_user_id(&data.account.user_id)?;
let device_id: OwnedDeviceId = data.account.device_id.into();
let account = Account::from_libolm_pickle(&data.account.pickle, &data.pickle_key)?;
let pickle = account.pickle();
let identity_keys = Arc::new(account.identity_keys());
let pickled_account = matrix_sdk_crypto::olm::PickledAccount {
user_id: parse_user_id(&data.account.user_id)?,
device_id: device_id.clone(),
pickle,
dehydrated: false, shared: data.account.shared,
uploaded_signed_key_count: data.account.uploaded_signed_key_count as u64,
creation_local_time: MilliSecondsSinceUnixEpoch::now(),
fallback_key_creation_timestamp: Some(MilliSecondsSinceUnixEpoch::now()),
};
let account = matrix_sdk_crypto::olm::Account::from_pickle(pickled_account)?;
processed_steps += 1;
listener(processed_steps, total_steps);
let (sessions, inbound_group_sessions) = collect_sessions(
processed_steps,
total_steps,
&listener,
&data.pickle_key,
user_id.clone(),
device_id,
identity_keys,
data.sessions,
data.inbound_group_sessions,
)?;
let backup_decryption_key = data
.backup_recovery_key
.map(|k| BackupDecryptionKey::from_base58(k.as_str()))
.transpose()?;
let cross_signing = PrivateCrossSigningIdentity::empty((*user_id).into());
cross_signing
.import_secrets_unchecked(
data.cross_signing.master_key.as_deref(),
data.cross_signing.self_signing_key.as_deref(),
data.cross_signing.user_signing_key.as_deref(),
)
.await?;
data.cross_signing.master_key.zeroize();
data.cross_signing.self_signing_key.zeroize();
data.cross_signing.user_signing_key.zeroize();
processed_steps += 1;
listener(processed_steps, total_steps);
let tracked_users: Vec<_> = data
.tracked_users
.into_iter()
.filter_map(|s| parse_user_id(&s).ok().map(|u| (u, true)))
.collect();
let tracked_users: Vec<_> = tracked_users.iter().map(|(u, d)| (&**u, *d)).collect();
store.save_tracked_users(tracked_users.as_slice()).await?;
processed_steps += 1;
listener(processed_steps, total_steps);
let mut room_settings = HashMap::new();
for (room_id, settings) in data.room_settings {
let room_id = RoomId::parse(room_id)?;
room_settings.insert(room_id, settings.into());
}
store.save_pending_changes(PendingChanges { account: Some(account) }).await?;
let changes = Changes {
private_identity: Some(cross_signing),
sessions,
inbound_group_sessions,
backup_decryption_key,
backup_version: data.backup_version,
room_settings,
..Default::default()
};
save_changes(processed_steps, total_steps, &listener, changes, &store).await
}
async fn save_changes(
mut processed_steps: usize,
total_steps: usize,
listener: &dyn Fn(usize, usize),
changes: Changes,
store: &SqliteCryptoStore,
) -> anyhow::Result<()> {
store.save_changes(changes).await?;
processed_steps += 1;
listener(processed_steps, total_steps);
Ok(())
}
#[matrix_sdk_ffi_macros::export]
pub fn migrate_sessions(
data: SessionMigrationData,
path: String,
passphrase: Option<String>,
progress_listener: Box<dyn ProgressListener>,
) -> Result<(), MigrationError> {
let runtime = Runtime::new().context("initializing tokio runtime")?;
runtime.block_on(migrate_session_data(data, &path, passphrase, progress_listener))?;
Ok(())
}
async fn migrate_session_data(
data: SessionMigrationData,
path: &str,
passphrase: Option<String>,
progress_listener: Box<dyn ProgressListener>,
) -> anyhow::Result<()> {
let store = SqliteCryptoStore::open(path, passphrase.as_deref()).await?;
let listener = |progress: usize, total: usize| {
progress_listener.on_progress(progress as i32, total as i32)
};
let total_steps = 1 + data.sessions.len() + data.inbound_group_sessions.len();
let processed_steps = 0;
let user_id = UserId::parse(data.user_id)?;
let device_id: OwnedDeviceId = data.device_id.into();
let identity_keys = IdentityKeys {
ed25519: Ed25519PublicKey::from_base64(&data.ed25519_key)?,
curve25519: Curve25519PublicKey::from_base64(&data.curve25519_key)?,
}
.into();
let (sessions, inbound_group_sessions) = collect_sessions(
processed_steps,
total_steps,
&listener,
&data.pickle_key,
user_id,
device_id,
identity_keys,
data.sessions,
data.inbound_group_sessions,
)?;
let changes = Changes { sessions, inbound_group_sessions, ..Default::default() };
save_changes(processed_steps, total_steps, &listener, changes, &store).await
}
#[allow(clippy::too_many_arguments)]
fn collect_sessions(
mut processed_steps: usize,
total_steps: usize,
listener: &dyn Fn(usize, usize),
pickle_key: &[u8],
user_id: OwnedUserId,
device_id: OwnedDeviceId,
identity_keys: Arc<IdentityKeys>,
session_pickles: Vec<PickledSession>,
group_session_pickles: Vec<PickledInboundGroupSession>,
) -> anyhow::Result<(Vec<Session>, Vec<InboundGroupSession>)> {
let mut sessions = Vec::new();
let device_keys = DeviceKeys::new(
user_id,
device_id.clone(),
Default::default(),
BTreeMap::from([
(
DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &device_id),
DeviceKey::Ed25519(identity_keys.ed25519),
),
(
DeviceKeyId::from_parts(DeviceKeyAlgorithm::Curve25519, &device_id),
DeviceKey::Curve25519(identity_keys.curve25519),
),
]),
Default::default(),
);
for session_pickle in session_pickles {
let pickle =
vodozemac::olm::Session::from_libolm_pickle(&session_pickle.pickle, pickle_key)?
.pickle();
let creation_time = SecondsSinceUnixEpoch(
UInt::new(session_pickle.creation_time).context("invalid creation timestamp")?,
);
let last_use_time = SecondsSinceUnixEpoch(
UInt::new(session_pickle.last_use_time).context("invalid last use timestamp")?,
);
let pickle = matrix_sdk_crypto::olm::PickledSession {
pickle,
sender_key: Curve25519PublicKey::from_base64(&session_pickle.sender_key)?,
created_using_fallback_key: session_pickle.created_using_fallback_key,
creation_time,
last_use_time,
};
let session = Session::from_pickle(device_keys.clone(), pickle)?;
sessions.push(session);
processed_steps += 1;
listener(processed_steps, total_steps);
}
let mut inbound_group_sessions = Vec::new();
for session in group_session_pickles {
let pickle = vodozemac::megolm::InboundGroupSession::from_libolm_pickle(
&session.pickle,
pickle_key,
)?
.pickle();
let sender_key = Curve25519PublicKey::from_base64(&session.sender_key)?;
let pickle = matrix_sdk_crypto::olm::PickledInboundGroupSession {
pickle,
sender_key,
signing_key: session
.signing_key
.into_iter()
.map(|(k, v)| {
let algorithm = DeviceKeyAlgorithm::from(k);
let key = SigningKey::from_parts(&algorithm, v)?;
Ok((algorithm, key))
})
.collect::<anyhow::Result<_>>()?,
sender_data: SenderData::legacy(),
room_id: RoomId::parse(session.room_id)?,
imported: session.imported,
backed_up: session.backed_up,
history_visibility: None,
algorithm: RustEventEncryptionAlgorithm::MegolmV1AesSha2,
};
let session = matrix_sdk_crypto::olm::InboundGroupSession::from_pickle(pickle)?;
inbound_group_sessions.push(session);
processed_steps += 1;
listener(processed_steps, total_steps);
}
Ok((sessions, inbound_group_sessions))
}
#[matrix_sdk_ffi_macros::export]
pub fn migrate_room_settings(
room_settings: HashMap<String, RoomSettings>,
path: String,
passphrase: Option<String>,
) -> Result<(), MigrationError> {
let runtime = Runtime::new().context("initializing tokio runtime")?;
runtime.block_on(async move {
let store = SqliteCryptoStore::open(path, passphrase.as_deref())
.await
.context("opening sqlite crypto store")?;
let mut rust_settings = HashMap::new();
for (room_id, settings) in room_settings {
let room_id = RoomId::parse(room_id).context("parsing room ID")?;
rust_settings.insert(room_id, settings.into());
}
let changes = Changes { room_settings: rust_settings, ..Default::default() };
store.save_changes(changes).await.context("saving changes")?;
Ok(())
})
}
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait ProgressListener {
fn on_progress(&self, progress: i32, total: i32);
}
impl<T: Fn(i32, i32)> ProgressListener for T {
fn on_progress(&self, progress: i32, total: i32) {
self(progress, total)
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Enum)]
pub enum EventEncryptionAlgorithm {
OlmV1Curve25519AesSha2,
MegolmV1AesSha2,
}
impl From<EventEncryptionAlgorithm> for RustEventEncryptionAlgorithm {
fn from(a: EventEncryptionAlgorithm) -> Self {
match a {
EventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => Self::OlmV1Curve25519AesSha2,
EventEncryptionAlgorithm::MegolmV1AesSha2 => Self::MegolmV1AesSha2,
}
}
}
impl TryFrom<RustEventEncryptionAlgorithm> for EventEncryptionAlgorithm {
type Error = serde_json::Error;
fn try_from(value: RustEventEncryptionAlgorithm) -> Result<Self, Self::Error> {
match value {
RustEventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => {
Ok(Self::OlmV1Curve25519AesSha2)
}
RustEventEncryptionAlgorithm::MegolmV1AesSha2 => Ok(Self::MegolmV1AesSha2),
_ => Err(serde::de::Error::custom(format!("Unsupported algorithm {value}"))),
}
}
}
#[derive(uniffi::Enum)]
pub enum HistoryVisibility {
Invited,
Joined,
Shared,
WorldReadable,
}
impl From<HistoryVisibility> for RustHistoryVisibility {
fn from(h: HistoryVisibility) -> Self {
match h {
HistoryVisibility::Invited => Self::Invited,
HistoryVisibility::Joined => Self::Joined,
HistoryVisibility::Shared => Self::Shared,
HistoryVisibility::WorldReadable => Self::Shared,
}
}
}
#[derive(uniffi::Record)]
pub struct EncryptionSettings {
pub algorithm: EventEncryptionAlgorithm,
pub rotation_period: u64,
pub rotation_period_msgs: u64,
pub history_visibility: HistoryVisibility,
pub only_allow_trusted_devices: bool,
pub error_on_verified_user_problem: bool,
}
impl From<EncryptionSettings> for RustEncryptionSettings {
fn from(v: EncryptionSettings) -> Self {
RustEncryptionSettings {
algorithm: v.algorithm.into(),
rotation_period: Duration::from_secs(v.rotation_period),
rotation_period_msgs: v.rotation_period_msgs,
history_visibility: v.history_visibility.into(),
sharing_strategy: CollectStrategy::DeviceBasedStrategy {
only_allow_trusted_devices: v.only_allow_trusted_devices,
error_on_verified_user_problem: v.error_on_verified_user_problem,
},
}
}
}
#[derive(uniffi::Record)]
pub struct DecryptedEvent {
pub clear_event: String,
pub sender_curve25519_key: String,
pub claimed_ed25519_key: Option<String>,
pub forwarding_curve25519_chain: Vec<String>,
pub shield_state: ShieldState,
}
#[allow(missing_docs)]
#[derive(uniffi::Enum)]
pub enum ShieldColor {
Red,
Grey,
None,
}
#[derive(uniffi::Record)]
#[allow(missing_docs)]
pub struct ShieldState {
color: ShieldColor,
code: Option<ShieldStateCode>,
message: Option<String>,
}
impl From<RustShieldState> for ShieldState {
fn from(value: RustShieldState) -> Self {
match value {
RustShieldState::Red { code, message } => Self {
color: ShieldColor::Red,
code: Some(code),
message: Some(message.to_owned()),
},
RustShieldState::Grey { code, message } => Self {
color: ShieldColor::Grey,
code: Some(code),
message: Some(message.to_owned()),
},
RustShieldState::None => Self { color: ShieldColor::None, code: None, message: None },
}
}
}
#[derive(Debug, Clone, uniffi::Record)]
pub struct CrossSigningStatus {
pub has_master: bool,
pub has_self_signing: bool,
pub has_user_signing: bool,
}
#[derive(Deserialize, Serialize, uniffi::Record)]
pub struct CrossSigningKeyExport {
pub master_key: Option<String>,
pub self_signing_key: Option<String>,
pub user_signing_key: Option<String>,
}
#[derive(uniffi::Record)]
pub struct RoomKeyCounts {
pub total: i64,
pub backed_up: i64,
}
#[derive(uniffi::Object)]
pub struct BackupKeys {
recovery_key: Arc<BackupRecoveryKey>,
backup_version: String,
}
#[matrix_sdk_ffi_macros::export]
impl BackupKeys {
pub fn recovery_key(&self) -> Arc<BackupRecoveryKey> {
self.recovery_key.clone()
}
pub fn backup_version(&self) -> String {
self.backup_version.to_owned()
}
}
impl TryFrom<matrix_sdk_crypto::store::BackupKeys> for BackupKeys {
type Error = ();
fn try_from(keys: matrix_sdk_crypto::store::BackupKeys) -> Result<Self, Self::Error> {
Ok(Self {
recovery_key: BackupRecoveryKey {
inner: keys.decryption_key.ok_or(())?,
passphrase_info: None,
}
.into(),
backup_version: keys.backup_version.ok_or(())?,
})
}
}
#[derive(uniffi::Record, Clone)]
pub struct DehydratedDeviceKey {
pub(crate) inner: Vec<u8>,
}
impl DehydratedDeviceKey {
pub fn new() -> Result<Self, DehydrationError> {
let inner = InnerDehydratedDeviceKey::new()?;
Ok(inner.into())
}
pub fn from_slice(slice: &[u8]) -> Result<Self, DehydrationError> {
let inner = InnerDehydratedDeviceKey::from_slice(slice)?;
Ok(inner.into())
}
pub fn to_base64(&self) -> String {
let inner = InnerDehydratedDeviceKey::from_slice(&self.inner).unwrap();
inner.to_base64()
}
}
impl From<InnerDehydratedDeviceKey> for DehydratedDeviceKey {
fn from(pickle_key: InnerDehydratedDeviceKey) -> Self {
DehydratedDeviceKey { inner: pickle_key.into() }
}
}
impl From<matrix_sdk_crypto::store::RoomKeyCounts> for RoomKeyCounts {
fn from(count: matrix_sdk_crypto::store::RoomKeyCounts) -> Self {
Self { total: count.total as i64, backed_up: count.backed_up as i64 }
}
}
impl From<matrix_sdk_crypto::CrossSigningKeyExport> for CrossSigningKeyExport {
fn from(e: matrix_sdk_crypto::CrossSigningKeyExport) -> Self {
Self {
master_key: e.master_key.clone(),
self_signing_key: e.self_signing_key.clone(),
user_signing_key: e.user_signing_key.clone(),
}
}
}
impl From<CrossSigningKeyExport> for matrix_sdk_crypto::CrossSigningKeyExport {
fn from(e: CrossSigningKeyExport) -> Self {
matrix_sdk_crypto::CrossSigningKeyExport {
master_key: e.master_key,
self_signing_key: e.self_signing_key,
user_signing_key: e.user_signing_key,
}
}
}
impl From<matrix_sdk_crypto::CrossSigningStatus> for CrossSigningStatus {
fn from(s: matrix_sdk_crypto::CrossSigningStatus) -> Self {
Self {
has_master: s.has_master,
has_self_signing: s.has_self_signing,
has_user_signing: s.has_user_signing,
}
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize, uniffi::Record)]
pub struct RoomSettings {
pub algorithm: EventEncryptionAlgorithm,
pub only_allow_trusted_devices: bool,
}
impl TryFrom<RustRoomSettings> for RoomSettings {
type Error = serde_json::Error;
fn try_from(value: RustRoomSettings) -> Result<Self, Self::Error> {
let algorithm = value.algorithm.try_into()?;
Ok(Self { algorithm, only_allow_trusted_devices: value.only_allow_trusted_devices })
}
}
impl From<RoomSettings> for RustRoomSettings {
fn from(value: RoomSettings) -> Self {
Self {
algorithm: value.algorithm.into(),
only_allow_trusted_devices: value.only_allow_trusted_devices,
..RustRoomSettings::default()
}
}
}
fn parse_user_id(user_id: &str) -> Result<OwnedUserId, CryptoStoreError> {
ruma::UserId::parse(user_id).map_err(|e| CryptoStoreError::InvalidUserId(user_id.to_owned(), e))
}
#[matrix_sdk_ffi_macros::export]
fn version_info() -> VersionInfo {
VersionInfo {
version: matrix_sdk_crypto::VERSION.to_owned(),
vodozemac_version: matrix_sdk_crypto::vodozemac::VERSION.to_owned(),
git_description: env!("VERGEN_GIT_DESCRIBE").to_owned(),
git_sha: env!("VERGEN_GIT_SHA").to_owned(),
}
}
#[derive(uniffi::Record)]
pub struct VersionInfo {
pub version: String,
pub vodozemac_version: String,
pub git_sha: String,
pub git_description: String,
}
#[matrix_sdk_ffi_macros::export]
fn version() -> String {
matrix_sdk_crypto::VERSION.to_owned()
}
#[matrix_sdk_ffi_macros::export]
fn vodozemac_version() -> String {
vodozemac::VERSION.to_owned()
}
#[derive(uniffi::Object)]
pub struct PkEncryption {
inner: matrix_sdk_crypto::vodozemac::pk_encryption::PkEncryption,
}
#[matrix_sdk_ffi_macros::export]
impl PkEncryption {
#[uniffi::constructor]
pub fn from_base64(key: &str) -> Result<Arc<Self>, DecodeError> {
let key = vodozemac::Curve25519PublicKey::from_base64(key)
.map_err(matrix_sdk_crypto::backups::DecodeError::PublicKey)?;
let inner = vodozemac::pk_encryption::PkEncryption::from_key(key);
Ok(Self { inner }.into())
}
pub fn encrypt(&self, plaintext: &str) -> PkMessage {
use vodozemac::base64_encode;
let message = self.inner.encrypt(plaintext.as_ref());
let vodozemac::pk_encryption::Message { ciphertext, mac, ephemeral_key } = message;
PkMessage {
ciphertext: base64_encode(ciphertext),
mac: base64_encode(mac),
ephemeral_key: ephemeral_key.to_base64(),
}
}
}
#[derive(uniffi::Record)]
pub struct PkMessage {
pub ciphertext: String,
pub mac: String,
pub ephemeral_key: String,
}
uniffi::setup_scaffolding!();
#[cfg(test)]
mod tests {
use anyhow::Result;
use serde_json::{json, Value};
use tempfile::tempdir;
use super::MigrationData;
use crate::{migrate, EventEncryptionAlgorithm, OlmMachine, RoomSettings};
#[test]
fn android_migration() -> Result<()> {
let data: Value = json!({
"account":{
"user_id":"@ganfra146:matrix.org",
"device_id":"DEWRCMENGS",
"pickle":"FFGTGho89T3Xgd56l+EedOPV37s09RR8aYnS9305qPKF66LG+ly29YpCibjJOvkwm0dZwN9A2bOH/z7WscriqwZn/p0GE6YSNwLzffCy5iROzYzpYzFe0HtiyJmCQWCezvLc5lHV8YsfD00C1pKGX2R9M1wwp3/n4/3VjtTyPsdnmtwAPu4WdcPSkisCaQ3a6JaSKqv8zYzUjnpzgcpXHvPUR5d5+TzXgrVz3BeCOe8NEOWIW6xYUxFtGteYP0BczOkkJ22t7Css0tSMSrYgCll4zZUGNrd6D9b/z7KwcDnb978epsZ16DcZ/aaTxPdM5uDIkHgF/qHWerfxcaqsqs4EQfJdSgOTeqhjHBw1k0uWF2bByJLK+n7sGkYXEAuTzc4+0XvSFvu3Qp+1bHZuT7QejngRZzyxznORyBxd8la3/JjeJlehSK80OL7zSmohoYZD59S6i3tFWfopjQThJ0/eIyVOhEN/c3tfIcVr3lFEQeokgpCRNOVldhPcQWq994NHaL7jtb6yhUqT1gShY4zYayFL/VRz6nBSXXYwzrC9jho67knqXSri3lIKYevP9aOi384IvzbkinQdumc804dYwiCbs5hZppfEnfhfgiDDm+kVrJ9WaPRF4SySCTlS8jdGmBeL2CfCQ5IcZ5nK6X7tZM3tmtYwva0RuQiTNltp3XTfbMa0EoaEBximv25165hFTpzrWgoszBTpZPfgsMuWENWBcIX4AcLSk0CJ0qzPDeUwvmRcFStstGYV4drs5u5HEqovFSI48CoHPSEZfwwERCI4c/0efZ0CVEfnm8VcMv3AbnAfedD7v3QNdVwWOEhz/fGR76BQi2WjZP4MWvYRJ/vsLO5hcVWUvaJGQs5kANUFZMWuJQeJv3DmkV9kKKXnyfFUerlQ4Uk/5tp2mXiG+adHjuRp/Eeh5V/biCcIaX3rNuIY6MJaPz6SOwlFe79MMBaNwaS3j4Kh/Aq9BRw0QXdjO4CqMI4p2xCE1N5QTPdeaRTHTZ3r7mLkHX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6Ce7U67a6jqkHYHd8o0+5H4bqdi9hInZdk0+swuXs",
"created_using_fallback_key":false,
"creation_time": 1649425011424u64,
"last_use_time": 1649425011424u64
},
{
"pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4t2W/lowyrV6SXVZp+uG59im0AAfNSKjhjZuiOpQlX7MS+AOJkCNvyujJ2g3KSjLZ94IkoHxkBDHLWSjwaLPu40rfOzJPDpm0XZsR6bQrsxKOmXLGEw2qw5jOTouzMVL2gvuuTix97nSYSU8j3XvTMRUoh0AF/tUpRLcvEFZeGrdUYmTMlyTv4na+FVUalUZ+jrk8t1/sM99JNq3SY1IBSjrBq/0rCOHieiippz0sw2fe2b87id4rqj1g3R9w2MWTWEdOz3ugjMGYF1YDBQZA1tJZ/hmgppk2AU2xKQXE2X3DgSC6fC66D4",
"sender_key":"RzRROfmHNlBfzxnNCUYBfn/5oZNQ11XYjDg59hS+mV0",
"created_using_fallback_key":false,
"creation_time": 1649425011503u64,
"last_use_time": 1649425011503u64
},
{
"pickle":"cryZlFaQv0hwWe6tTgv75RExFKGnC8tMHBXJYMHOw4titbL3SS12PYHpcBPJc6hXnOnZXqrjtjYOD545fck+3utEo8cqqwWubc9tsvxGW3tOWPttLBdAW30Vn8V1M8ebqVCNVWEAb1GKjV4ni8xG7G9SlEcCjLjnF4lJpddSZkqVMFoN0ITr9aSz/eJwXpc3HLreUFXwc8LuQp7krQ4Vt1e5EE/klduqsdurZf5V14RHsmWz2lKjt7nVgtIz/dhtF5F/sGJdg8kCGaHIMSbGAPuPPpa4/Laicb/5otrYt4pg4W4KdFpSGJIcvUQNjXaOZMx3cu/RPJIOyNhx7whG1QiYAUBqAJvr",
"sender_key":"IXSZugAHig1v8MowE1jxi2wDDDfuZBeJynHlegJVwUc",
"created_using_fallback_key":false,
"creation_time": 1649425011566u64,
"last_use_time": 1649425011566u64
},
{
"pickle":"SmkDiFZjNukiarQ7XHQo25FILHsuhNOnxy56cMSQU/Y71jaGbJes4YrvN4Dfy4RSONfejEDXDkbW2JudlHHRP/rWEmnfJiGbK6ArbrG2puqIZgOecPnOUgPfCisr49p1Gmf36dPaO5lm/ZSrngfSoxahoeJJE/CcJN98sYM15XytRk2LBwc+CyYDqr4V1qxfsBt6tzJ4+tsAZeRdD0UtipQgysgH56o8N7nKTCkaZz5lfpYCl3FEgwXpLJ0MGQvtQmbORFvOLqR1jZ/EbmNGKiqDDIYsqG0sf78ii1jqfpLDBXLuYDccsg",
"sender_key":"EB9SC4jVAydKhM6/GcwMc9biKwVNywqW3TerNTrtb1M",
"created_using_fallback_key":false,
"creation_time": 1649542063182u64,
"last_use_time": 1649542063182u64
}
],
"inbound_group_sessions":[
{
"pickle":"KoA0sxDNQ7lz0vylU9zlmar0VCVQRMCfRrIfTh1bdMhlAgy8/D2ToT+oKRaKy1HiW6H862bzdpgxprlseSjmip9OfLbIuyd2YZcEinwc2666oEI/hpk4oTlE61uE1M+ThfdFf41yGCmaAP7mhjwF234ZrZ6i/F/qx42TLQ8Unc30wDJaJgyheO5eW85SD/0g0cdg2WnEKrx2/wl7Vg/YijT3JMDZ+OsdfJfSZtxBNjlG+PQ/9D31qb1eHfaovc8vFZh5QLfAFg/5rBrF1PhRsC7xOAZbUNdrTbvypNfMM4mznf84C2SzZRSMeAfg5v/YticM3Keg4eHuEj1WO9DrmRXYl6b/pITdf1xuk5euVT0pyxJpXmq41AoAZKAo1l94HGy1LG1RpruD1uQPhiponh5PGHSOf43Q",
"sender_key":"vJfH7wiYmGos3C8U1XcJ//YWSmkueAYqrmUA6/ukfAM",
"signing_key":{
"ed25519":"JGgPQRuYj3ScMdPS+A0P+k/1qS9Hr3qeKXLscI+hS78"
},
"room_id":"!AZkqtjvtwPAuyNOXEt:matrix.org",
"forwarding_chains":[
],
"imported":true,
"backed_up":true
},
{
"pickle":"9RF6GBu9CvjZZx2hxIlw2gMdKs36LFhXhLTHAPrLSjT2OTbeE/jK263+iiFdSpF7Cblp/lXzljPKJN6sL8JGzoT7ssYh56nI0kKsp7/y88z+tTOH/5NYYTmZzHYw6yy4Cmaxh0pdHDs+RQpSSIe9jhF/EJJna5jcKYXxDY52m8H4LECQzVuDlYfblCr9zoYWhQrVhiRDGy7eLhk4X6Rp0Yoek4YUKcCQArDfZ/Vf43qfHUpOJgRpm5Oyj42HA/j4xZBb5U0Fmo6YHRPt0/KuWrDfpgJSGiN0zza7641IfADg8f3WdhlPAWMyri7k4vOZMBjlwFNcMpc0wM2TaTmbi2zqXEKZy9Oh/eJqBapFx0oNWaQ1VQ++iXxGUbZhwy7x2vd6UkqUTwYeym+aP23ee3TCtnNWN0aC",
"sender_key":"EB9SC4jVAydKhM6/GcwMc9biKwVNywqW3TerNTrtb1M",
"signing_key":{
"ed25519":"1NXa5GyJ+p2ruAClEque+TL1VktrBzMW4dZFNfNGrvc"
},
"room_id":"!CWLUCoEWXSFyTCOtfL:matrix.org",
"forwarding_chains":[],
"imported":true,
"backed_up":true
}
],
"pickle_key": [17, 36, 120, 74, 95, 78, 56, 36, 62, 123, 5, 105, 74,
111, 70, 48, 51, 101, 66, 86, 116, 14, 114, 85, 85,
92, 44, 71, 89, 99, 55, 74],
"backup_version":"3",
"backup_recovery_key":"EsTHScmRV5oT1WBhe2mj2Gn3odeYantZ4NEk7L51p6L8hrmB",
"cross_signing":{
"master_key":"trnK/dBv/M2x2zZt8lnORHQqmFHWvjYE6rdlAONRUPY",
"self_signing_key":"SJhsj9jXC4hxhqS/1B3RZ65zWMHuF+1fUjWHrzVRh6w",
"user_signing_key":"LPYrV11T9Prm4ZIUxrq2a8Y/F64R1+NaGNyo6GlXjGg"
},
"tracked_users":[
"@ganfra146:matrix.org",
"@this-is-me:matrix.org",
"@Amandine:matrix.org",
"@ganfra:matrix.org",
"NotAUser%ID"
],
"room_settings": {
"!AZkqtjvtwPAuyNOXEt:matrix.org": {
"algorithm": "OlmV1Curve25519AesSha2",
"only_allow_trusted_devices": true
},
"!CWLUCoEWXSFyTCOtfL:matrix.org": {
"algorithm": "MegolmV1AesSha2",
"only_allow_trusted_devices": false
},
}
});
let migration_data: MigrationData = serde_json::from_value(data)?;
let dir = tempdir()?;
let path = dir
.path()
.to_str()
.expect("Creating a string from the tempdir path should not fail")
.to_owned();
migrate(migration_data, path.clone(), None, Box::new(|_, _| {}))?;
let machine = OlmMachine::new(
"@ganfra146:matrix.org".to_owned(),
"DEWRCMENGS".to_owned(),
path,
None,
)?;
assert_eq!(
machine.identity_keys()["ed25519"],
"JGgPQRuYj3ScMdPS+A0P+k/1qS9Hr3qeKXLscI+hS78"
);
let room_keys =
machine.runtime.block_on(machine.inner.store().export_room_keys(|_| true))?;
assert_eq!(room_keys.len(), 2);
let cross_signing_status = machine.cross_signing_status();
assert!(cross_signing_status.has_master);
assert!(cross_signing_status.has_user_signing);
assert!(cross_signing_status.has_self_signing);
let backup_keys = machine.get_backup_keys()?;
assert!(backup_keys.is_some());
let settings1 = machine.get_room_settings("!AZkqtjvtwPAuyNOXEt:matrix.org".into())?;
assert_eq!(
Some(RoomSettings {
algorithm: EventEncryptionAlgorithm::OlmV1Curve25519AesSha2,
only_allow_trusted_devices: true
}),
settings1
);
let settings2 = machine.get_room_settings("!CWLUCoEWXSFyTCOtfL:matrix.org".into())?;
assert_eq!(
Some(RoomSettings {
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
only_allow_trusted_devices: false
}),
settings2
);
let settings3 = machine.get_room_settings("!XYZ:matrix.org".into())?;
assert!(settings3.is_none());
assert!(machine.is_user_tracked("@ganfra146:matrix.org".into()).unwrap());
assert!(machine.is_user_tracked("@Amandine:matrix.org".into()).unwrap());
assert!(machine.is_user_tracked("@this-is-me:matrix.org".into()).unwrap());
assert!(machine.is_user_tracked("@ganfra:matrix.org".into()).unwrap());
Ok(())
}
}