element-android icon indicating copy to clipboard operation
element-android copied to clipboard

[SDK] Invalid recovery key

Open LukaRusadze opened this issue 10 months ago • 0 comments

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.


// 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.

LukaRusadze avatar Mar 07 '25 10:03 LukaRusadze