diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Cryptor.kt b/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Cryptor.kt index 560587ff66..9d55b33b24 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Cryptor.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Cryptor.kt @@ -3,6 +3,9 @@ package chat.simplex.app.views.usersettings import android.annotation.SuppressLint import android.security.keystore.KeyGenParameterSpec import android.security.keystore.KeyProperties +import chat.simplex.app.R +import chat.simplex.app.views.helpers.AlertManager +import chat.simplex.app.views.helpers.generalGetString import java.security.KeyStore import javax.crypto.* import javax.crypto.spec.GCMParameterSpec @@ -10,11 +13,24 @@ import javax.crypto.spec.GCMParameterSpec @SuppressLint("ObsoleteSdkInt") internal class Cryptor { private var keyStore: KeyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) } + private var warningShown = false - fun decryptData(data: ByteArray, iv: ByteArray, alias: String): String { + fun decryptData(data: ByteArray, iv: ByteArray, alias: String): String? { + val secretKey = getSecretKey(alias) + if (secretKey == null) { + if (!warningShown) { + // Repeated calls will not show the alert again + warningShown = true + AlertManager.shared.showAlertMsg( + title = generalGetString(R.string.wrong_passphrase), + text = generalGetString(R.string.restore_passphrase_not_found_desc) + ) + } + return null + } val cipher: Cipher = Cipher.getInstance(TRANSFORMATION) val spec = GCMParameterSpec(128, iv) - cipher.init(Cipher.DECRYPT_MODE, getSecretKey(alias), spec) + cipher.init(Cipher.DECRYPT_MODE, secretKey, spec) return String(cipher.doFinal(data)) } @@ -29,7 +45,7 @@ internal class Cryptor { keyStore.deleteEntry(alias) } - private fun createSecretKey(alias: String): SecretKey { + private fun createSecretKey(alias: String): SecretKey? { if (keyStore.containsAlias(alias)) return getSecretKey(alias) val keyGenerator: KeyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM, "AndroidKeyStore") keyGenerator.init( @@ -41,8 +57,8 @@ internal class Cryptor { return keyGenerator.generateKey() } - private fun getSecretKey(alias: String): SecretKey { - return (keyStore.getEntry(alias, null) as KeyStore.SecretKeyEntry).secretKey + private fun getSecretKey(alias: String): SecretKey? { + return (keyStore.getEntry(alias, null) as? KeyStore.SecretKeyEntry)?.secretKey } companion object { diff --git a/apps/android/app/src/main/res/values-de/strings.xml b/apps/android/app/src/main/res/values-de/strings.xml index 3af1cec183..0f5afa1b46 100644 --- a/apps/android/app/src/main/res/values-de/strings.xml +++ b/apps/android/app/src/main/res/values-de/strings.xml @@ -682,6 +682,7 @@ Bitte geben Sie das vorherige Passwort ein, nachdem Sie die Datenbanksicherung wiederhergestellt haben. Diese Aktion kann nicht rückgängig gemacht werden. Wiederherstellen Fehler bei der Wiederherstellung der Datenbank + ***Passphrase not found in Keystore, please enter it manually. This may have happened if you restored the app\'s data using a backup tool. If it\'s not the case, please, contact developers. Chat wurde beendet diff --git a/apps/android/app/src/main/res/values-ru/strings.xml b/apps/android/app/src/main/res/values-ru/strings.xml index 3ddd6de490..27e6d64a6b 100644 --- a/apps/android/app/src/main/res/values-ru/strings.xml +++ b/apps/android/app/src/main/res/values-ru/strings.xml @@ -682,6 +682,7 @@ Введите предыдущий пароль после восстановления резервной копии. Это действие нельзя отменить. Восстановить Ошибка при восстановлении базы данных + Пароль не найден в Keystore, пожалуйста, введите его вручную. Это могло произойти, если вы восстановили данные приложения с помощью инструмента резервного копирования. Если это не так, пожалуйста, свяжитесь с разработчиками. Чат остановлен diff --git a/apps/android/app/src/main/res/values/strings.xml b/apps/android/app/src/main/res/values/strings.xml index b5f8895d77..c251ad1867 100644 --- a/apps/android/app/src/main/res/values/strings.xml +++ b/apps/android/app/src/main/res/values/strings.xml @@ -682,6 +682,7 @@ Please enter the previous password after restoring database backup. This action can not be undone. Restore Restore database error + Passphrase not found in Keystore, please enter it manually. This may have happened if you restored the app\'s data using a backup tool. If it\'s not the case, please, contact developers. Chat is stopped