crypto: Throw exceptions when restoring a recovery key from a passphrase

This commit is contained in:
Damir Jelić 2021-11-12 11:58:14 +01:00
parent e5af7e6109
commit 097f05af57
1 changed files with 15 additions and 30 deletions

View File

@ -398,15 +398,10 @@ internal class RustKeyBackupService @Inject constructor(
cryptoCoroutineScope.launch { cryptoCoroutineScope.launch {
try { try {
val key = recoveryKeyFromPassword(password, keysBackupVersion) val key = recoveryKeyFromPassword(password, keysBackupVersion)
if (key == null) {
Timber.w("trustKeysBackupVersionWithPassphrase: Key backup is missing required data")
callback.onFailure(IllegalArgumentException("Missing element"))
} else {
checkRecoveryKey(key, keysBackupVersion) checkRecoveryKey(key, keysBackupVersion)
trustKeysBackupVersion(keysBackupVersion, true, callback) trustKeysBackupVersion(keysBackupVersion, true, callback)
}
} catch (exception: Throwable) { } catch (exception: Throwable) {
Timber.w(exception)
callback.onFailure(exception) callback.onFailure(exception)
} }
} }
@ -595,15 +590,7 @@ internal class RustKeyBackupService @Inject constructor(
cryptoCoroutineScope.launch(coroutineDispatchers.main) { cryptoCoroutineScope.launch(coroutineDispatchers.main) {
runCatching { runCatching {
val recoveryKey = withContext(coroutineDispatchers.crypto) { val recoveryKey = withContext(coroutineDispatchers.crypto) {
val key = recoveryKeyFromPassword(password, keysBackupVersion) recoveryKeyFromPassword(password, keysBackupVersion)
if (key == null) {
Timber.w("trustKeysBackupVersionWithPassphrase: Key backup is missing required data")
throw IllegalArgumentException("Missing element")
} else {
key
}
} }
restoreBackup(keysBackupVersion, recoveryKey, roomId, sessionId, stepProgressListener) restoreBackup(keysBackupVersion, recoveryKey, roomId, sessionId, stepProgressListener)
@ -772,22 +759,20 @@ internal class RustKeyBackupService @Inject constructor(
* @return the recovery key if successful, null in other cases * @return the recovery key if successful, null in other cases
*/ */
@WorkerThread @WorkerThread
private fun recoveryKeyFromPassword(password: String, keysBackupData: KeysVersionResult): BackupRecoveryKey? { private fun recoveryKeyFromPassword(password: String, keysBackupData: KeysVersionResult): BackupRecoveryKey {
val authData = getMegolmBackupAuthData(keysBackupData) val authData = getMegolmBackupAuthData(keysBackupData)
if (authData == null) { return when {
Timber.w("recoveryKeyFromPassword: invalid parameter") authData == null -> {
return null throw IllegalArgumentException("recoveryKeyFromPassword: invalid parameter")
}
authData.privateKeySalt.isNullOrBlank() || authData.privateKeyIterations == null -> {
throw java.lang.IllegalArgumentException("recoveryKeyFromPassword: Salt and/or iterations not found in key backup auth data")
}
else -> {
BackupRecoveryKey.fromPassphrase(password, authData.privateKeySalt, authData.privateKeyIterations)
} }
if (authData.privateKeySalt.isNullOrBlank()
|| authData.privateKeyIterations == null) {
Timber.w("recoveryKeyFromPassword: Salt and/or iterations not found in key backup auth data")
return null
} }
return BackupRecoveryKey.fromPassphrase(password, authData.privateKeySalt, authData.privateKeyIterations)
} }
/** /**