From a3af73261cdeea0f97681d0128482e4ae1fe2f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 15 Nov 2021 10:31:55 +0100 Subject: [PATCH] crypto: Throw decode errors when creating a recovery key --- rust-sdk/src/backup_recovery_key.rs | 32 ++++++++++++++--------------- rust-sdk/src/lib.rs | 6 ++++-- rust-sdk/src/olm.udl | 11 +++++++--- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/rust-sdk/src/backup_recovery_key.rs b/rust-sdk/src/backup_recovery_key.rs index 3eb82a30c5..67b9c16290 100644 --- a/rust-sdk/src/backup_recovery_key.rs +++ b/rust-sdk/src/backup_recovery_key.rs @@ -21,6 +21,13 @@ pub enum PkDecryptionError { Olm(#[from] OlmPkDecryptionError), } +#[derive(Debug, Error)] +pub enum DecodeError { + /// An error happened while decoding the recovery key. + #[error(transparent)] + Decode(#[from] matrix_sdk_crypto::backups::DecodeError), +} + /// Struct containing info about the way the backup key got derived from a /// passphrase. #[derive(Debug, Clone)] @@ -56,21 +63,19 @@ impl BackupRecoveryKey { } /// Try to create a [`BackupRecoveryKey`] from a base 64 encoded string. - pub fn from_base64(key: String) -> Self { - Self { - // TODO remove the unwrap - inner: RecoveryKey::from_base64(&key).unwrap(), + pub fn from_base64(key: String) -> Result { + Ok(Self { + inner: RecoveryKey::from_base64(&key)?, passphrase_info: None, - } + }) } /// Try to create a [`BackupRecoveryKey`] from a base 58 encoded string. - pub fn from_base58(key: String) -> Self { - Self { - // TODO remove the unwrap - inner: RecoveryKey::from_base58(&key).unwrap(), + pub fn from_base58(key: String) -> Result { + Ok(Self { + inner: RecoveryKey::from_base58(&key)?, passphrase_info: None, - } + }) } /// Create a new [`BackupRecoveryKey`] from the given passphrase. @@ -90,12 +95,7 @@ impl BackupRecoveryKey { let mut key = [0u8; Self::KEY_SIZE]; let rounds = rounds as u32; - pbkdf2::>( - passphrase.as_bytes(), - salt.as_bytes(), - rounds, - &mut key, - ); + pbkdf2::>(passphrase.as_bytes(), salt.as_bytes(), rounds, &mut key); Self { inner: RecoveryKey::from_bytes(key), diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index 301eebf776..0add76b18c 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -3,7 +3,7 @@ trivial_casts, trivial_numeric_casts, unused_extern_crates, - unused_import_braces, + unused_import_braces )] //! TODO @@ -17,7 +17,9 @@ mod responses; mod users; mod verification; -pub use backup_recovery_key::{BackupKey, BackupRecoveryKey, PassphraseInfo, PkDecryptionError}; +pub use backup_recovery_key::{ + BackupKey, BackupRecoveryKey, DecodeError, PassphraseInfo, PkDecryptionError, +}; pub use device::Device; pub use error::{ CryptoStoreError, DecryptionError, KeyImportError, SecretImportError, SignatureError, diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 977b171e9c..10ca65fc0c 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -395,15 +395,20 @@ dictionary RoomKeyCounts { i64 backed_up; }; +[Error] +enum DecodeError { + "Decode", +}; + interface BackupRecoveryKey { constructor(); - [Name=from_base64] - constructor(string key); [Name=from_passphrase] constructor(string passphrase, string salt, i32 rounds); [Name=new_from_passphrase] constructor(string passphrase); - [Name=from_base58] + [Name=from_base64, Throws=DecodeError] + constructor(string key); + [Name=from_base58, Throws=DecodeError] constructor(string key); string to_base58(); string to_base64();