element-hq / element-hq/element-android
[SDK] Invalid recovery key
- Dominant language
- Kotlin
- Stars
- 3.7k
- Forks
- 917
- PR merge metrics
- No merged PRs in 30d
Description
### Description
Trying to restore keys with a RecoveryKey but getting this error: `java.security.InvalidParameterException: Invalid recovery key`
Looking deeper into the SDK isValidRecoveryKey seems to be where it goes wrong. For some reason the public keys of recoveryKey and backup are mismatched.
```kotlin
// SDK part
private fun getMegolmBackupAuthData(keysBackupData: KeysVersionResult): MegolmBackupAuthData? {
return keysBackupData
.takeIf { it.version.isNotEmpty() && it.algorithm == MXCRYPTO_ALGORITHM_MEGOLM_BACKUP }
?.getAuthDataAsMegolmBackupAuthData()
?.takeIf { it.publicKey.isNotEmpty() }
}
private fun isValidRecoveryKey(
recoveryKey: IBackupRecoveryKey,
version: KeysVersionResult
): Boolean {
val publicKey = recoveryKey.megolmV1PublicKey().publicKey
val authData = getMegolmBackupAuthData(version) ?: return false
println("------------ Keys ------------")
println(publicKey)
println(authData.publicKey)
println("------------ Keys ------------")
return authData.publicKey == publicKey
}
// Usage Part
private suspend fun recoverSecrets(recoveryCode: String) {
val backupRecoveryKey = BackupUtils.recoveryKeyFromBase58(recoveryCode)
val keysVersionResult = this.session.cryptoService().keysBackupService().keysBackupVersion
?: throw Exception("Keys backup version not found")
isValidRecoveryKey(backupRecoveryKey, keysVersionResult)
this.session.cryptoService().keysBackupService()
.restoreKeysWithRecoveryKey(keysVersionResult, backupRecoveryKey, null, null, null)
}
recoverSecrets("E*** **** **** **** **** **** **** **** **** **** **** ****")
```
Printing out the public keys we get
```
System.out com.prototype I ------------ Keys ------------
System.out com.prototype I o2RJ1aVoFbdLMqTT9IYKrVcECQf1ZI/9M1SI7XB7ngA
System.out com.prototype I HxaK0V+kyJDKksX6z3Cv4UmIKfDiSt3hgeQ46FBT+Ws
System.out com.prototype I ------------ Keys ------------
```
RecoveryKey has been just copied over from Element. I've tried regenerating the recovery key. both public keys update but are always mismatched.
Contributor guide
Assessment
This issue has not been assessed yet.