From 7433003ffa3b8943df83964714b3d62f4b78e689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 19 Jul 2021 09:21:28 +0200 Subject: [PATCH] matrix-sdk: Don't panic when importing invalid key exports --- matrix_sdk/src/client.rs | 12 ++++++++---- matrix_sdk/src/error.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 3d1d2713..f2105751 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -129,6 +129,7 @@ use ruma::{ #[cfg(feature = "encryption")] use crate::{ device::{Device, UserDevices}, + error::RoomKeyImportError, verification::{QrVerification, SasVerification, Verification, VerificationRequest}, }; use crate::{ @@ -2491,8 +2492,12 @@ impl Client { /// ``` #[cfg(all(feature = "encryption", not(target_arch = "wasm32")))] #[cfg_attr(feature = "docs", doc(cfg(all(encryption, not(target_arch = "wasm32")))))] - pub async fn import_keys(&self, path: PathBuf, passphrase: &str) -> Result<(usize, usize)> { - let olm = self.base_client.olm_machine().await.ok_or(Error::AuthenticationRequired)?; + pub async fn import_keys( + &self, + path: PathBuf, + passphrase: &str, + ) -> StdResult<(usize, usize), RoomKeyImportError> { + let olm = self.base_client.olm_machine().await.ok_or(RoomKeyImportError::StoreClosed)?; let passphrase = Zeroizing::new(passphrase.to_owned()); let decrypt = move || { @@ -2501,8 +2506,7 @@ impl Client { }; let task = tokio::task::spawn_blocking(decrypt); - // TODO remove this unwrap. - let import = task.await.expect("Task join error").unwrap(); + let import = task.await.expect("Task join error")?; Ok(olm.import_keys(import, |_, _| {}).await?) } diff --git a/matrix_sdk/src/error.rs b/matrix_sdk/src/error.rs index 968a9609..2fe22129 100644 --- a/matrix_sdk/src/error.rs +++ b/matrix_sdk/src/error.rs @@ -18,7 +18,9 @@ use std::io::Error as IoError; use http::StatusCode; #[cfg(feature = "encryption")] -use matrix_sdk_base::crypto::{CryptoStoreError, DecryptorError, MegolmError, OlmError}; +use matrix_sdk_base::crypto::{ + CryptoStoreError, DecryptorError, KeyExportError, MegolmError, OlmError, +}; use matrix_sdk_base::{Error as SdkBaseError, StoreError}; use reqwest::Error as ReqwestError; use ruma::{ @@ -151,6 +153,33 @@ pub enum Error { Url(#[from] UrlParseError), } +/// Error for the room key importing functionality. +#[cfg(feature = "encryption")] +#[cfg_attr(feature = "docs", doc(cfg(encryption)))] +#[derive(Error, Debug)] +pub enum RoomKeyImportError { + /// An error de/serializing type for the `StateStore` + #[error(transparent)] + SerdeJson(#[from] JsonError), + + /// The cryptostore isn't yet open, logging in is required to open the + /// cryptostore. + #[error("The cryptostore hasn't been yet opened, can't import yet.")] + StoreClosed, + + /// An IO error happened. + #[error(transparent)] + Io(#[from] IoError), + + /// An error occurred in the crypto store. + #[error(transparent)] + CryptoStore(#[from] CryptoStoreError), + + /// An error occurred while importing the key export. + #[error(transparent)] + Export(#[from] KeyExportError), +} + impl Error { /// Try to destructure the error into an universal interactive auth info. ///