7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2025 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for that specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use std::{str::FromStr, sync::Arc}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use futures_util::StreamExt; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | use matrix_sdk::encryption::{self, backups, recovery}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | use matrix_sdk_base::crypto::types::{BackupSecrets, RoomKeyBackupInfo}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | use ruma::OwnedUserId; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | use serde::de::Error; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | use thiserror::Error; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | use tracing::{error, info}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | use zeroize::Zeroize; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | use crate::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | client::Client, error::ClientError, ruma::AuthData, runtime::get_runtime_handle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | task_handle::TaskHandle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | pub struct Encryption { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | pub(crate) inner: matrix_sdk::encryption::Encryption, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | /// A reference to the FFI client. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | /// Note: we do this to make it so that the FFI `NotificationClient` keeps |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | /// the FFI `Client` and thus the SDK `Client` alive. Otherwise, we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | /// would need to repeat the hack done in the FFI `Client::drop` method. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | pub(crate) _client: Arc<Client>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | pub trait BackupStateListener: SyncOutsideWasm + SendOutsideWasm { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | fn on_update(&self, status: BackupState); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | pub trait BackupSteadyStateListener: SyncOutsideWasm + SendOutsideWasm { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | fn on_update(&self, status: BackupUploadState); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | pub trait RecoveryStateListener: SyncOutsideWasm + SendOutsideWasm { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | fn on_update(&self, status: RecoveryState); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | pub trait VerificationStateListener: SyncOutsideWasm + SendOutsideWasm { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | fn on_update(&self, status: VerificationState); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | pub enum BackupUploadState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | Waiting, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | Uploading { backed_up_count: u32, total_count: u32 }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | Error, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | #[derive(Debug, Error, uniffi::Error)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | #[uniffi(flat_error)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | pub enum SteadyStateError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | #[error("The backup got disabled while waiting for the room keys to be uploaded.")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | BackupDisabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | #[error("There was a connection error.")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | Connection, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | #[error("We couldn't read status updates from the upload task quickly enough.")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | Lagged, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | #[derive(Debug, Error, uniffi::Error)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | pub enum RecoveryError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | /// A backup already exists on the homeserver, the recovery subsystem does |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | /// not allow backups to be overwritten, disable recovery first. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | #[error( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | "A backup already exists on the homeserver and the method does not allow to overwrite it" |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | )] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | BackupExistsOnServer, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | /// A typical SDK error. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | #[error(transparent)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | Client { source: crate::ClientError }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | /// Error in the secret storage subsystem, except for when importing a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | /// secret. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | #[error("Error in the secret-storage subsystem: {error_message}")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | SecretStorage { error_message: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | /// Error when importing a secret from secret storage. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | #[error("Error importing a secret: {error_message}")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | Import { error_message: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 106 | impl From<matrix_sdk::encryption::recovery::RecoveryError> for RecoveryError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 107 | fn from(value: matrix_sdk::encryption::recovery::RecoveryError) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 108 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 109 | recovery::RecoveryError::BackupExistsOnServer => Self::BackupExistsOnServer, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 110 | recovery::RecoveryError::Sdk(e) => Self::Client { source: ClientError::from(e) }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 111 | recovery::RecoveryError::SecretStorage( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 112 | matrix_sdk::encryption::secret_storage::SecretStorageError::ImportError { .. }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 113 | ) => Self::Import { error_message: value.to_string() }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 114 | recovery::RecoveryError::SecretStorage(e) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 115 | Self::SecretStorage { error_message: e.to_string() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 116 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 117 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 118 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 119 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 120 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 121 | pub type Result<A, E = RecoveryError> = std::result::Result<A, E>; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 122 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 123 | impl From<matrix_sdk::encryption::backups::futures::SteadyStateError> for SteadyStateError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 124 | fn from(value: matrix_sdk::encryption::backups::futures::SteadyStateError) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 125 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 126 | backups::futures::SteadyStateError::BackupDisabled => Self::BackupDisabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 127 | backups::futures::SteadyStateError::Connection => Self::Connection, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 128 | backups::futures::SteadyStateError::Lagged => Self::Lagged, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 129 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 130 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 131 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 132 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 133 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 134 | pub enum BackupState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 135 | Unknown, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 136 | Creating, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 137 | Enabling, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 138 | Resuming, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 139 | Enabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 140 | Downloading, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 141 | Disabling, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 142 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 143 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 144 | impl From<backups::BackupState> for BackupState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 145 | fn from(value: backups::BackupState) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 146 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 147 | backups::BackupState::Unknown => Self::Unknown, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 148 | backups::BackupState::Creating => Self::Creating, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 149 | backups::BackupState::Enabling => Self::Enabling, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 150 | backups::BackupState::Resuming => Self::Resuming, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 151 | backups::BackupState::Enabled => Self::Enabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 152 | backups::BackupState::Downloading => Self::Downloading, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 153 | backups::BackupState::Disabling => Self::Disabling, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 154 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 155 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 156 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 157 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 158 | impl From<backups::UploadState> for BackupUploadState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 159 | fn from(value: backups::UploadState) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 160 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 161 | backups::UploadState::Idle => Self::Waiting, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 162 | backups::UploadState::Uploading(count) => Self::Uploading { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 163 | backed_up_count: count.backed_up.try_into().unwrap_or(u32::MAX), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 164 | total_count: count.total.try_into().unwrap_or(u32::MAX), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 165 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 166 | backups::UploadState::Error => Self::Error, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 167 | backups::UploadState::Done => Self::Done, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 168 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 169 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 170 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 171 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 172 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 173 | pub enum RecoveryState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 174 | Unknown, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 175 | Enabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 176 | Disabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 177 | Incomplete, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 178 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 179 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 180 | impl From<recovery::RecoveryState> for RecoveryState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 181 | fn from(value: recovery::RecoveryState) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 182 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 183 | recovery::RecoveryState::Unknown => Self::Unknown, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 184 | recovery::RecoveryState::Enabled => Self::Enabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 185 | recovery::RecoveryState::Disabled => Self::Disabled, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 186 | recovery::RecoveryState::Incomplete => Self::Incomplete, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 187 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 188 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 189 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 190 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 191 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 192 | pub trait EnableRecoveryProgressListener: SyncOutsideWasm + SendOutsideWasm { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 193 | fn on_update(&self, status: EnableRecoveryProgress); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 194 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 195 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 196 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 197 | pub enum EnableRecoveryProgress { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 198 | Starting, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 199 | CreatingBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 200 | CreatingRecoveryKey, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 201 | BackingUp { backed_up_count: u32, total_count: u32 }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 202 | RoomKeyUploadError, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 203 | Done { recovery_key: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 204 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 205 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 206 | impl From<recovery::EnableProgress> for EnableRecoveryProgress { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 207 | fn from(value: recovery::EnableProgress) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 208 | match &value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 209 | recovery::EnableProgress::Starting => Self::Starting, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 210 | recovery::EnableProgress::CreatingBackup => Self::CreatingBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 211 | recovery::EnableProgress::CreatingRecoveryKey => Self::CreatingRecoveryKey, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 212 | recovery::EnableProgress::BackingUp(counts) => Self::BackingUp { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 213 | backed_up_count: counts.backed_up.try_into().unwrap_or(u32::MAX), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 214 | total_count: counts.backed_up.try_into().unwrap_or(u32::MAX), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 215 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 216 | recovery::EnableProgress::RoomKeyUploadError => Self::RoomKeyUploadError, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 217 | recovery::EnableProgress::Done { recovery_key } => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 218 | Self::Done { recovery_key: recovery_key.to_owned() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 219 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 220 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 221 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 222 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 223 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 224 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 225 | pub enum VerificationState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 226 | Unknown, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 227 | Verified, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 228 | Unverified, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 229 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 230 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 231 | impl From<encryption::VerificationState> for VerificationState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 232 | fn from(value: encryption::VerificationState) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 233 | match &value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 234 | encryption::VerificationState::Unknown => Self::Unknown, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 235 | encryption::VerificationState::Verified => Self::Verified, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 236 | encryption::VerificationState::Unverified => Self::Unverified, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 237 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 238 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 239 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 240 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 241 | /// Struct containing the bundle of secrets to fully activate a new device for |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 242 | /// end-to-end encryption. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 243 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 244 | pub struct SecretsBundleWithUserId { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 245 | user_id: OwnedUserId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 246 | inner: matrix_sdk_base::crypto::types::SecretsBundle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 247 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 248 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 249 | /// Result for the check if a store has a valid secrets bundle. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 250 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 251 | pub enum DetectedSecretsBundle { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 252 | /// The store doesn't contain a secrets bundle at all. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 253 | None, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 254 | /// The store contains a bundle without a backup. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 255 | WithoutBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 256 | /// The store contains a bundle with an unused backup, the backup key in the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 257 | /// bundle isn't used on the homeserver. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 258 | UnusedBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 259 | /// The store contains a complete secrets bundle. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 260 | Complete, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 261 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 262 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 263 | /// Error type describing failures that can happen while exporting a |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 264 | /// [`SecretsBundle`] from a SQLite store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 265 | #[derive(Debug, thiserror::Error, uniffi::Error)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 266 | pub enum BundleExportError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 267 | /// The SQLite store couldn't be opened. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 268 | #[error("the store couldn't be opened: {msg}")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 269 | OpenStoreError { msg: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 270 | /// Data from the SQLite store couldn't be exported. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 271 | #[error("the bundle couldn't be exported due to a storage error: {msg}")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 272 | StoreError { msg: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 273 | /// The store doesn't contain a secrets bundle or it couldn't be read from |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 274 | /// the store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 275 | #[error("the bundle couldn't be exported: {msg}")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 276 | SecretError { msg: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 277 | /// The store is empty and doesn't contain a secrets bundle. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 278 | #[error("the store is completely empty")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 279 | StoreEmpty, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 280 | /// A JSON object couldn't be deserialized while the secrets bundle was |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 281 | /// exported. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 282 | #[error("Couldn't deserialize a JSON value: {msg}")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 283 | Json { msg: String }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 284 | /// Error returned when the secrets bundle is missing a backup key or |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 285 | /// includes one that doesn’t match the key configured for the active backup |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 286 | /// version. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 287 | #[error( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 288 | "The bundle is missing a backup key or has one that isn't the one that's currently used" |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 289 | )] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 290 | InvalidBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 291 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 292 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 293 | #[cfg(feature = "sqlite")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 294 | impl From<matrix_sdk::encryption::BundleExportError> for BundleExportError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 295 | fn from(value: matrix_sdk::encryption::BundleExportError) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 296 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 297 | matrix_sdk::encryption::BundleExportError::OpenStoreError(e) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 298 | BundleExportError::OpenStoreError { msg: e.to_string() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 299 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 300 | matrix_sdk::encryption::BundleExportError::StoreError(e) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 301 | BundleExportError::StoreError { msg: e.to_string() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 302 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 303 | matrix_sdk::encryption::BundleExportError::SecretExport(e) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 304 | BundleExportError::SecretError { msg: e.to_string() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 305 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 306 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 307 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 308 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 309 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 310 | impl From<serde_json::Error> for BundleExportError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 311 | fn from(value: serde_json::Error) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 312 | Self::Json { msg: value.to_string() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 313 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 314 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 315 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 316 | #[cfg(feature = "sqlite")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 317 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 318 | impl SecretsBundleWithUserId { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 319 | /// Attempt to export a [`SecretsBundle`] from a crypto store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 320 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 321 | /// This method can be used to retrieve a [`SecretsBundle`] from an existing |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 322 | /// `matrix-sdk`-based client in order to import the [`SecretsBundle`] in |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 323 | /// another [`Client`] instance. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 324 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 325 | /// This can be useful for migration purposes or to allow existing client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 326 | /// instances create new ones that will be fully verified. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 327 | #[uniffi::constructor] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 328 | pub async fn from_database( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 329 | database_path: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 330 | mut passphrase: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 331 | backup_info: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 332 | ) -> Result<Arc<Self>, BundleExportError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 333 | let backup_info = serde_json::from_str(backup_info)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 334 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 335 | let ret = if let Some((user_id, bundle)) = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 336 | matrix_sdk::encryption::export_secrets_bundle_from_store( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 337 | database_path, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 338 | passphrase.as_deref(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 339 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 340 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 341 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 342 | let is_backup_ok = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 343 | bundle.backup.as_ref().is_some_and(|backup| is_valid_backup(backup, &backup_info)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 344 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 345 | if is_backup_ok { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 346 | Ok(SecretsBundleWithUserId { user_id, inner: bundle }.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 347 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 348 | Err(BundleExportError::InvalidBackup) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 349 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 350 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 351 | Err(BundleExportError::StoreEmpty) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 352 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 353 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 354 | passphrase.zeroize(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 355 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 356 | ret |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 357 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 358 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 359 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 360 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 361 | impl SecretsBundleWithUserId { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 362 | /// Attempt to create a [`SecretsBundle`] from a previously JSON serialized |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 363 | /// bundle. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 364 | #[uniffi::constructor] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 365 | pub fn from_str( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 366 | user_id: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 367 | bundle: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 368 | backup_info: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 369 | ) -> Result<Arc<Self>, BundleExportError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 370 | let user_id = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 371 | OwnedUserId::from_str(user_id).map_err(|e| serde_json::Error::custom(e.to_string()))?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 372 | let bundle: matrix_sdk_base::crypto::types::SecretsBundle = serde_json::from_str(bundle)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 373 | let backup_info = serde_json::from_str(backup_info)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 374 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 375 | let is_backup_ok = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 376 | bundle.backup.as_ref().is_some_and(|backup| is_valid_backup(backup, &backup_info)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 377 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 378 | if is_backup_ok { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 379 | Ok(Self { user_id, inner: bundle }.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 380 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 381 | Err(BundleExportError::InvalidBackup) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 382 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 383 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 384 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 385 | /// Does the bundle contain a backup key. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 386 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 387 | /// Since enabling a backup is optional, the backup key might be missing |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 388 | /// from the bundle. Returns `false` if the backup key is missing, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 389 | /// otherwise `true`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 390 | pub fn contains_backup_key(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 391 | self.inner.backup.is_some() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 392 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 393 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 394 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 395 | fn is_valid_backup(secrets: &BackupSecrets, info: &RoomKeyBackupInfo) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 396 | match secrets { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 397 | BackupSecrets::MegolmBackupV1Curve25519AesSha2(secrets) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 398 | secrets.key.backup_key_matches(info) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 399 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 400 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 401 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 402 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 403 | fn check_bundle_and_info( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 404 | bundle: &matrix_sdk_base::crypto::types::SecretsBundle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 405 | info: Option<&RoomKeyBackupInfo>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 406 | ) -> DetectedSecretsBundle { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 407 | match (&bundle.backup, info) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 408 | (None, None) => DetectedSecretsBundle::WithoutBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 409 | (None, Some(_)) => DetectedSecretsBundle::WithoutBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 410 | (Some(_), None) => DetectedSecretsBundle::UnusedBackup, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 411 | (Some(backup), Some(info)) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 412 | if is_valid_backup(backup, info) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 413 | DetectedSecretsBundle::Complete |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 414 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 415 | DetectedSecretsBundle::UnusedBackup |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 416 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 417 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 418 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 419 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 420 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 421 | /// Check if a JSON encoded string contains a valid [`SecretsBundle`]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 422 | #[uniffi::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 423 | pub fn json_string_contains_secrets_bundle( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 424 | bundle: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 425 | backup_info: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 426 | ) -> Result<DetectedSecretsBundle, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 427 | let info: Option<RoomKeyBackupInfo> = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 428 | backup_info.map(|info| serde_json::from_str(&info)).transpose()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 429 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 430 | let bundle: matrix_sdk_base::crypto::types::SecretsBundle = serde_json::from_str(bundle)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 431 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 432 | Ok(check_bundle_and_info(&bundle, info.as_ref())) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 433 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 434 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 435 | /// Check if a crypto store contains a valid [`SecretsBundle`]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 436 | #[cfg(feature = "sqlite")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 437 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 438 | pub async fn database_contains_secrets_bundle( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 439 | database_path: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 440 | mut passphrase: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 441 | backup_info: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 442 | ) -> Result<DetectedSecretsBundle, BundleExportError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 443 | let info: Option<RoomKeyBackupInfo> = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 444 | backup_info.map(|info| serde_json::from_str(&info)).transpose()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 445 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 446 | let maybe_bundle = matrix_sdk::encryption::export_secrets_bundle_from_store( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 447 | database_path, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 448 | passphrase.as_deref(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 449 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 450 | .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 451 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 452 | passphrase.zeroize(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 453 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 454 | Ok(match maybe_bundle { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 455 | Some((_, bundle)) => check_bundle_and_info(&bundle, info.as_ref()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 456 | None => DetectedSecretsBundle::None, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 457 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 458 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 459 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 460 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 461 | impl Encryption { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 462 | /// Get the public ed25519 key of our own device. This is usually what is |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 463 | /// called the fingerprint of the device. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 464 | pub async fn ed25519_key(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 465 | self.inner.ed25519_key().await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 466 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 467 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 468 | /// Get the public curve25519 key of our own device in base64. This is |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 469 | /// usually what is called the identity key of the device. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 470 | pub async fn curve25519_key(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 471 | self.inner.curve25519_key().await.map(|k| k.to_base64()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 472 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 473 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 474 | pub fn backup_state_listener(&self, listener: Box<dyn BackupStateListener>) -> Arc<TaskHandle> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 475 | let mut stream = self.inner.backups().state_stream(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 476 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 477 | let stream_task = TaskHandle::new(get_runtime_handle().spawn(async move { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 478 | while let Some(state) = stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 479 | let Ok(state) = state else { continue }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 480 | listener.on_update(state.into()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 481 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 482 | })); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 483 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 484 | stream_task.into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 485 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 486 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 487 | pub fn backup_state(&self) -> BackupState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 488 | self.inner.backups().state().into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 489 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 490 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 491 | /// Does a backup exist on the server? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 492 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 493 | /// Because the homeserver doesn't notify us about changes to the backup |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 494 | /// version, the [`BackupState`] and its listener are a bit crippled. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 495 | /// The `BackupState::Unknown` state might mean there is no backup at all or |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 496 | /// a backup exists but we don't have access to it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 497 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 498 | /// Therefore it is necessary to poll the server for an answer every time |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 499 | /// you want to differentiate between those two states. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 500 | pub async fn backup_exists_on_server(&self) -> Result<bool, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 501 | Ok(self.inner.backups().fetch_exists_on_server().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 502 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 503 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 504 | pub fn recovery_state(&self) -> RecoveryState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 505 | self.inner.recovery().state().into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 506 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 507 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 508 | pub fn recovery_state_listener( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 509 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 510 | listener: Box<dyn RecoveryStateListener>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 511 | ) -> Arc<TaskHandle> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 512 | let mut stream = self.inner.recovery().state_stream(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 513 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 514 | let stream_task = TaskHandle::new(get_runtime_handle().spawn(async move { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 515 | while let Some(state) = stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 516 | listener.on_update(state.into()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 517 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 518 | })); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 519 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 520 | stream_task.into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 521 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 522 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 523 | pub async fn enable_backups(&self) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 524 | Ok(self.inner.recovery().enable_backup().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 525 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 526 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 527 | pub async fn is_last_device(&self) -> Result<bool> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 528 | Ok(self.inner.recovery().is_last_device().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 529 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 530 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 531 | /// Does the user have other devices that the current device can verify |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 532 | /// against? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 533 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 534 | /// The device must be signed by the user's cross-signing key, must have an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 535 | /// identity, and must not be a dehydrated device. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 536 | pub async fn has_devices_to_verify_against(&self) -> Result<bool, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 537 | Ok(self.inner.has_devices_to_verify_against().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 538 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 539 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 540 | pub async fn wait_for_backup_upload_steady_state( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 541 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 542 | progress_listener: Option<Box<dyn BackupSteadyStateListener>>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 543 | ) -> Result<(), SteadyStateError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 544 | let backups = self.inner.backups(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 545 | let wait_for_steady_state = backups.wait_for_steady_state(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 546 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 547 | let task = if let Some(listener) = progress_listener { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 548 | let mut progress_stream = wait_for_steady_state.subscribe_to_progress(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 549 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 550 | Some(get_runtime_handle().spawn(async move { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 551 | while let Some(progress) = progress_stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 552 | let Ok(progress) = progress else { continue }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 553 | listener.on_update(progress.into()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 554 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 555 | })) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 556 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 557 | None |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 558 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 559 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 560 | let result = wait_for_steady_state.await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 561 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 562 | if let Some(task) = task { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 563 | task.abort(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 564 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 565 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 566 | Ok(result?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 567 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 568 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 569 | pub async fn enable_recovery( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 570 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 571 | wait_for_backups_to_upload: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 572 | mut passphrase: Option<String>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 573 | progress_listener: Box<dyn EnableRecoveryProgressListener>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 574 | ) -> Result<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 575 | let recovery = self.inner.recovery(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 576 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 577 | let enable = if wait_for_backups_to_upload { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 578 | recovery.enable().wait_for_backups_to_upload() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 579 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 580 | recovery.enable() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 581 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 582 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 583 | let enable = if let Some(passphrase) = &passphrase { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 584 | enable.with_passphrase(passphrase) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 585 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 586 | enable |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 587 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 588 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 589 | let mut progress_stream = enable.subscribe_to_progress(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 590 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 591 | let task = get_runtime_handle().spawn(async move { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 592 | while let Some(progress) = progress_stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 593 | let Ok(progress) = progress else { continue }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 594 | progress_listener.on_update(progress.into()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 595 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 596 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 597 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 598 | let ret = enable.await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 599 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 600 | task.abort(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 601 | passphrase.zeroize(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 602 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 603 | Ok(ret) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 604 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 605 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 606 | pub async fn disable_recovery(&self) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 607 | Ok(self.inner.recovery().disable().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 608 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 609 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 610 | pub async fn reset_recovery_key(&self) -> Result<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 611 | Ok(self.inner.recovery().reset_key().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 612 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 613 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 614 | pub async fn recover_and_reset(&self, mut old_recovery_key: String) -> Result<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 615 | let result = self.inner.recovery().recover_and_reset(&old_recovery_key).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 616 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 617 | old_recovery_key.zeroize(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 618 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 619 | Ok(result?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 620 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 621 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 622 | /// Completely reset the current user's crypto identity: reset the cross |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 623 | /// signing keys, delete the existing backup and recovery key. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 624 | pub async fn reset_identity(&self) -> Result<Option<Arc<IdentityResetHandle>>, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 625 | if let Some(reset_handle) = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 626 | self.inner.recovery().reset_identity().await.map_err(ClientError::from_err)? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 627 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 628 | return Ok(Some(Arc::new(IdentityResetHandle { inner: reset_handle }))); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 629 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 630 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 631 | Ok(None) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 632 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 633 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 634 | /// Download identity and key backup information from Recovery |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 635 | pub async fn recover(&self, mut recovery_key: String) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 636 | let result = self.inner.recovery().recover(&recovery_key).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 637 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 638 | recovery_key.zeroize(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 639 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 640 | Ok(result?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 641 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 642 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 643 | /// Download identity and key backup information from Recovery, and, if the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 644 | /// key backup information is inconsistent, create a new key backup. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 645 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 646 | /// This will create a new key backup if: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 647 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 648 | /// * Key backup is enabled and the backup decryption key is missing from |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 649 | /// Recovery, or |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 650 | /// * Key backup is enabled and the backup decryption key does not match the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 651 | /// public key |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 652 | pub async fn recover_and_fix_backup(&self, mut recovery_key: String) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 653 | let result = self.inner.recovery().recover_and_fix_backup(&recovery_key).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 654 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 655 | recovery_key.zeroize(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 656 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 657 | Ok(result?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 658 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 659 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 660 | pub fn verification_state(&self) -> VerificationState { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 661 | self.inner.verification_state().get().into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 662 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 663 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 664 | pub fn verification_state_listener( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 665 | self: Arc<Self>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 666 | listener: Box<dyn VerificationStateListener>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 667 | ) -> Arc<TaskHandle> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 668 | let mut subscriber = self.inner.verification_state(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 669 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 670 | Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 671 | while let Some(verification_state) = subscriber.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 672 | listener.on_update(verification_state.into()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 673 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 674 | }))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 675 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 676 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 677 | /// Waits for end-to-end encryption initialization tasks to finish, if any |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 678 | /// was running in the background. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 679 | pub async fn wait_for_e2ee_initialization_tasks(&self) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 680 | self.inner.wait_for_e2ee_initialization_tasks().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 681 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 682 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 683 | /// Get the E2EE identity of a user. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 684 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 685 | /// This method always tries to fetch the identity from the store, which we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 686 | /// only have if the user is tracked, meaning that we are both members |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 687 | /// of the same encrypted room. If no user is found locally, a request will |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 688 | /// be made to the homeserver unless `fallback_to_server` is set to `false`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 689 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 690 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 691 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 692 | /// * `user_id` - The ID of the user that the identity belongs to. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 693 | /// * `fallback_to_server` - Should we request the user identity from the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 694 | /// homeserver if one isn't found locally. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 695 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 696 | /// Returns a `UserIdentity` if one is found. Returns an error if there |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 697 | /// was an issue with the crypto store or with the request to the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 698 | /// homeserver. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 699 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 700 | /// This will always return `None` if the client hasn't been logged in. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 701 | pub async fn user_identity( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 702 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 703 | user_id: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 704 | fallback_to_server: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 705 | ) -> Result<Option<Arc<UserIdentity>>, ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 706 | match self.inner.get_user_identity(user_id.as_str().try_into()?).await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 707 | Ok(Some(identity)) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 708 | return Ok(Some(Arc::new(UserIdentity { inner: identity }))); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 709 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 710 | Ok(None) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 711 | info!("No identity found in the store."); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 712 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 713 | Err(error) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 714 | error!("Failed fetching identity from the store: {error}"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 715 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 716 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 717 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 718 | info!("Requesting identity from the server."); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 719 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 720 | if fallback_to_server { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 721 | let identity = self.inner.request_user_identity(user_id.as_str().try_into()?).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 722 | Ok(identity.map(|identity| Arc::new(UserIdentity { inner: identity }))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 723 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 724 | Ok(None) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 725 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 726 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 727 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 728 | /// This method will import all the private cross-signing keys and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 729 | /// the private part of a backup key and its accompanying version into the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 730 | /// store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 731 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 732 | /// Importing all the secrets will mark the device as verified and enable |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 733 | /// backups. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 734 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 735 | /// **Warning**: Only import this from a trusted source, i.e. if an existing |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 736 | /// device is sharing this with a new device. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 737 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 738 | /// **Warning*: Only call this method right after logging in and before the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 739 | /// initial sync has been started. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 740 | pub async fn import_secrets_bundle( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 741 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 742 | secrets_bundle: &SecretsBundleWithUserId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 743 | ) -> Result<(), ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 744 | let user_id = self._client.inner.user_id().expect( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 745 | "We should have a user ID available now, this is only called once we're logged in", |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 746 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 747 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 748 | if user_id == secrets_bundle.user_id { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 749 | self.inner |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 750 | .import_secrets_bundle(&secrets_bundle.inner) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 751 | .await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 752 | .map_err(ClientError::from_err)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 753 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 754 | self.inner.wait_for_e2ee_initialization_tasks().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 755 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 756 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 757 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 758 | Err(ClientError::Generic { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 759 | msg: "Secrets bundle does not belong to the user which was logged in".to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 760 | details: None, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 761 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 762 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 763 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 764 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 765 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 766 | /// The E2EE identity of a user. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 767 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 768 | pub struct UserIdentity { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 769 | inner: matrix_sdk::encryption::identities::UserIdentity, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 770 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 771 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 772 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 773 | impl UserIdentity { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 774 | /// Remember this identity, ensuring it does not result in a pin violation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 775 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 776 | /// When we first see a user, we assume their cryptographic identity has not |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 777 | /// been tampered with by the homeserver or another entity with |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 778 | /// man-in-the-middle capabilities. We remember this identity and call this |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 779 | /// action "pinning". |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 780 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 781 | /// If the identity presented for the user changes later on, the newly |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 782 | /// presented identity is considered to be in "pin violation". This |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 783 | /// method explicitly accepts the new identity, allowing it to replace |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 784 | /// the previously pinned one and bringing it out of pin violation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 785 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 786 | /// UIs should display a warning to the user when encountering an identity |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 787 | /// which is not verified and is in pin violation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 788 | pub(crate) async fn pin(&self) -> Result<(), ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 789 | Ok(self.inner.pin().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 790 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 791 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 792 | /// Get the public part of the Master key of this user identity. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 793 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 794 | /// The public part of the Master key is usually used to uniquely identify |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 795 | /// the identity. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 796 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 797 | /// Returns None if the master key does not actually contain any keys. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 798 | pub(crate) fn master_key(&self) -> Option<String> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 799 | self.inner.master_key().get_first_key().map(|k| k.to_base64()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 800 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 801 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 802 | /// Is the user identity considered to be verified. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 803 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 804 | /// If the identity belongs to another user, our own user identity needs to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 805 | /// be verified as well for the identity to be considered to be verified. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 806 | pub fn is_verified(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 807 | self.inner.is_verified() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 808 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 809 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 810 | /// True if we verified this identity at some point in the past. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 811 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 812 | /// To reset this latch back to `false`, one must call |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 813 | /// [`UserIdentity::withdraw_verification()`]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 814 | pub fn was_previously_verified(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 815 | self.inner.was_previously_verified() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 816 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 817 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 818 | /// Remove the requirement for this identity to be verified. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 819 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 820 | /// If an identity was previously verified and is not anymore it will be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 821 | /// reported to the user. In order to remove this notice users have to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 822 | /// verify again or to withdraw the verification requirement. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 823 | pub(crate) async fn withdraw_verification(&self) -> Result<(), ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 824 | Ok(self.inner.withdraw_verification().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 825 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 826 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 827 | /// Was this identity previously verified, and is no longer? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 828 | pub fn has_verification_violation(&self) -> bool { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 829 | self.inner.has_verification_violation() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 830 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 831 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 832 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 833 | #[derive(uniffi::Object)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 834 | pub struct IdentityResetHandle { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 835 | pub(crate) inner: matrix_sdk::encryption::recovery::IdentityResetHandle, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 836 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 837 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 838 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 839 | impl IdentityResetHandle { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 840 | /// Get the underlying [`CrossSigningResetAuthType`] this identity reset |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 841 | /// process is using. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 842 | pub fn auth_type(&self) -> CrossSigningResetAuthType { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 843 | self.inner.auth_type().into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 844 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 845 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 846 | /// This method starts the identity reset process and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 847 | /// will go through the following steps: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 848 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 849 | /// 1. Disable backing up room keys and delete the active backup |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 850 | /// 2. Disable recovery and delete secret storage |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 851 | /// 3. Go through the cross-signing key reset flow |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 852 | /// 4. Finally, re-enable key backups only if they were enabled before |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 853 | pub async fn reset(&self, auth: Option<AuthData>) -> Result<(), ClientError> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 854 | self.inner.reset(auth.map(Into::into)).await.map_err(ClientError::from_err) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 855 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 856 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 857 | pub async fn cancel(&self) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 858 | self.inner.cancel().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 859 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 860 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 861 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 862 | #[derive(uniffi::Enum)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 863 | pub enum CrossSigningResetAuthType { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 864 | /// The homeserver requires user-interactive authentication. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 865 | Uiaa, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 866 | /// OAuth is used for authentication and the user needs to open a URL to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 867 | /// approve the upload of cross-signing keys. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 868 | OAuth { info: OAuthCrossSigningResetInfo }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 869 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 870 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 871 | impl From<&matrix_sdk::encryption::CrossSigningResetAuthType> for CrossSigningResetAuthType { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 872 | fn from(value: &matrix_sdk::encryption::CrossSigningResetAuthType) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 873 | match value { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 874 | encryption::CrossSigningResetAuthType::Uiaa(_) => Self::Uiaa, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 875 | encryption::CrossSigningResetAuthType::OAuth(info) => Self::OAuth { info: info.into() }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 876 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 877 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 878 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 879 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 880 | #[derive(uniffi::Record)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 881 | pub struct OAuthCrossSigningResetInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 882 | /// The URL where the user can approve the reset of the cross-signing keys. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 883 | pub approval_url: String, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 884 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 885 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 886 | impl From<&matrix_sdk::encryption::OAuthCrossSigningResetInfo> for OAuthCrossSigningResetInfo { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 887 | fn from(value: &matrix_sdk::encryption::OAuthCrossSigningResetInfo) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 888 | Self { approval_url: value.approval_url.to_string() } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 889 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 890 | } |