matrix-sdk: Don't panic when importing invalid key exports

master
Damir Jelić 2021-07-19 09:21:28 +02:00
parent ead91a1e6b
commit 7433003ffa
2 changed files with 38 additions and 5 deletions

View File

@ -129,6 +129,7 @@ use ruma::{
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use crate::{ use crate::{
device::{Device, UserDevices}, device::{Device, UserDevices},
error::RoomKeyImportError,
verification::{QrVerification, SasVerification, Verification, VerificationRequest}, verification::{QrVerification, SasVerification, Verification, VerificationRequest},
}; };
use crate::{ use crate::{
@ -2491,8 +2492,12 @@ impl Client {
/// ``` /// ```
#[cfg(all(feature = "encryption", not(target_arch = "wasm32")))] #[cfg(all(feature = "encryption", not(target_arch = "wasm32")))]
#[cfg_attr(feature = "docs", doc(cfg(all(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)> { pub async fn import_keys(
let olm = self.base_client.olm_machine().await.ok_or(Error::AuthenticationRequired)?; &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 passphrase = Zeroizing::new(passphrase.to_owned());
let decrypt = move || { let decrypt = move || {
@ -2501,8 +2506,7 @@ impl Client {
}; };
let task = tokio::task::spawn_blocking(decrypt); let task = tokio::task::spawn_blocking(decrypt);
// TODO remove this unwrap. let import = task.await.expect("Task join error")?;
let import = task.await.expect("Task join error").unwrap();
Ok(olm.import_keys(import, |_, _| {}).await?) Ok(olm.import_keys(import, |_, _| {}).await?)
} }

View File

@ -18,7 +18,9 @@ use std::io::Error as IoError;
use http::StatusCode; use http::StatusCode;
#[cfg(feature = "encryption")] #[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 matrix_sdk_base::{Error as SdkBaseError, StoreError};
use reqwest::Error as ReqwestError; use reqwest::Error as ReqwestError;
use ruma::{ use ruma::{
@ -151,6 +153,33 @@ pub enum Error {
Url(#[from] UrlParseError), 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 { impl Error {
/// Try to destructure the error into an universal interactive auth info. /// Try to destructure the error into an universal interactive auth info.
/// ///