crypto: Throw decode errors when creating a recovery key
This commit is contained in:
parent
ba7aa3513b
commit
a3af73261c
@ -21,6 +21,13 @@ pub enum PkDecryptionError {
|
|||||||
Olm(#[from] OlmPkDecryptionError),
|
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
|
/// Struct containing info about the way the backup key got derived from a
|
||||||
/// passphrase.
|
/// passphrase.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -56,21 +63,19 @@ impl BackupRecoveryKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Try to create a [`BackupRecoveryKey`] from a base 64 encoded string.
|
/// Try to create a [`BackupRecoveryKey`] from a base 64 encoded string.
|
||||||
pub fn from_base64(key: String) -> Self {
|
pub fn from_base64(key: String) -> Result<Self, DecodeError> {
|
||||||
Self {
|
Ok(Self {
|
||||||
// TODO remove the unwrap
|
inner: RecoveryKey::from_base64(&key)?,
|
||||||
inner: RecoveryKey::from_base64(&key).unwrap(),
|
|
||||||
passphrase_info: None,
|
passphrase_info: None,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to create a [`BackupRecoveryKey`] from a base 58 encoded string.
|
/// Try to create a [`BackupRecoveryKey`] from a base 58 encoded string.
|
||||||
pub fn from_base58(key: String) -> Self {
|
pub fn from_base58(key: String) -> Result<Self, DecodeError> {
|
||||||
Self {
|
Ok(Self {
|
||||||
// TODO remove the unwrap
|
inner: RecoveryKey::from_base58(&key)?,
|
||||||
inner: RecoveryKey::from_base58(&key).unwrap(),
|
|
||||||
passphrase_info: None,
|
passphrase_info: None,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new [`BackupRecoveryKey`] from the given passphrase.
|
/// Create a new [`BackupRecoveryKey`] from the given passphrase.
|
||||||
@ -90,12 +95,7 @@ impl BackupRecoveryKey {
|
|||||||
let mut key = [0u8; Self::KEY_SIZE];
|
let mut key = [0u8; Self::KEY_SIZE];
|
||||||
let rounds = rounds as u32;
|
let rounds = rounds as u32;
|
||||||
|
|
||||||
pbkdf2::<Hmac<Sha512>>(
|
pbkdf2::<Hmac<Sha512>>(passphrase.as_bytes(), salt.as_bytes(), rounds, &mut key);
|
||||||
passphrase.as_bytes(),
|
|
||||||
salt.as_bytes(),
|
|
||||||
rounds,
|
|
||||||
&mut key,
|
|
||||||
);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
inner: RecoveryKey::from_bytes(key),
|
inner: RecoveryKey::from_bytes(key),
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
trivial_casts,
|
trivial_casts,
|
||||||
trivial_numeric_casts,
|
trivial_numeric_casts,
|
||||||
unused_extern_crates,
|
unused_extern_crates,
|
||||||
unused_import_braces,
|
unused_import_braces
|
||||||
)]
|
)]
|
||||||
|
|
||||||
//! TODO
|
//! TODO
|
||||||
@ -17,7 +17,9 @@ mod responses;
|
|||||||
mod users;
|
mod users;
|
||||||
mod verification;
|
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 device::Device;
|
||||||
pub use error::{
|
pub use error::{
|
||||||
CryptoStoreError, DecryptionError, KeyImportError, SecretImportError, SignatureError,
|
CryptoStoreError, DecryptionError, KeyImportError, SecretImportError, SignatureError,
|
||||||
|
@ -395,15 +395,20 @@ dictionary RoomKeyCounts {
|
|||||||
i64 backed_up;
|
i64 backed_up;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
[Error]
|
||||||
|
enum DecodeError {
|
||||||
|
"Decode",
|
||||||
|
};
|
||||||
|
|
||||||
interface BackupRecoveryKey {
|
interface BackupRecoveryKey {
|
||||||
constructor();
|
constructor();
|
||||||
[Name=from_base64]
|
|
||||||
constructor(string key);
|
|
||||||
[Name=from_passphrase]
|
[Name=from_passphrase]
|
||||||
constructor(string passphrase, string salt, i32 rounds);
|
constructor(string passphrase, string salt, i32 rounds);
|
||||||
[Name=new_from_passphrase]
|
[Name=new_from_passphrase]
|
||||||
constructor(string passphrase);
|
constructor(string passphrase);
|
||||||
[Name=from_base58]
|
[Name=from_base64, Throws=DecodeError]
|
||||||
|
constructor(string key);
|
||||||
|
[Name=from_base58, Throws=DecodeError]
|
||||||
constructor(string key);
|
constructor(string key);
|
||||||
string to_base58();
|
string to_base58();
|
||||||
string to_base64();
|
string to_base64();
|
||||||
|
Loading…
Reference in New Issue
Block a user