crypto: Throw decode errors when creating a recovery key

This commit is contained in:
Damir Jelić 2021-11-15 10:31:55 +01:00
parent ba7aa3513b
commit a3af73261c
3 changed files with 28 additions and 21 deletions

View File

@ -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<Self, DecodeError> {
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<Self, DecodeError> {
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::<Hmac<Sha512>>(
passphrase.as_bytes(),
salt.as_bytes(),
rounds,
&mut key,
);
pbkdf2::<Hmac<Sha512>>(passphrase.as_bytes(), salt.as_bytes(), rounds, &mut key);
Self {
inner: RecoveryKey::from_bytes(key),

View File

@ -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,

View File

@ -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();