From 256243dc8c8e0b8547d794624c1d0f256f1ac5f2 Mon Sep 17 00:00:00 2001
From: Avently <7953703+avently@users.noreply.github.com>
Date: Wed, 9 Nov 2022 14:00:46 +0300
Subject: [PATCH] Restoring app's data from backup tools will still allow to
enter passphrase instead of just crashing
---
.../simplex/app/views/usersettings/Cryptor.kt | 26 +++++++++++++++----
.../app/src/main/res/values-de/strings.xml | 1 +
.../app/src/main/res/values-ru/strings.xml | 1 +
.../app/src/main/res/values/strings.xml | 1 +
4 files changed, 24 insertions(+), 5 deletions(-)
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..34f11f8287 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 warningIsShown = 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 (!warningIsShown) {
+ // Repeated calls will not show the alert again
+ warningIsShown = true
+ AlertManager.shared.showAlertMsg(
+ title = generalGetString(R.string.wrong_passphrase),
+ text = generalGetString(R.string.restored_from_backup_broken_text)
+ )
+ }
+ 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 e62e850bc0..c055ce4f6d 100644
--- a/apps/android/app/src/main/res/values-de/strings.xml
+++ b/apps/android/app/src/main/res/values-de/strings.xml
@@ -674,6 +674,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
+ ***Looks like you restored the app\'s data from a backup (using TitaniumBackup or similar tool). Encryption passphrase should be entered manually. \n\nIf it\'s not the case, please, contact devs
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 de3b4186ab..6bcde09e01 100644
--- a/apps/android/app/src/main/res/values-ru/strings.xml
+++ b/apps/android/app/src/main/res/values-ru/strings.xml
@@ -674,6 +674,7 @@
Введите предыдущий пароль после восстановления резервной копии. Это действие нельзя отменить.
Восстановить
Ошибка при восстановлении базы данных
+ Похоже, вы восстановили данные программы из бэкапа (используя TitaniumBackup или похожий инструмент). Нужно ввести пароль от базы данных вручную. \n\nЕсли причина не в этом, пожалуйста, свяжитесь с разработчиками
Чат остановлен
diff --git a/apps/android/app/src/main/res/values/strings.xml b/apps/android/app/src/main/res/values/strings.xml
index 0c0a6753ec..b81e6c3c0a 100644
--- a/apps/android/app/src/main/res/values/strings.xml
+++ b/apps/android/app/src/main/res/values/strings.xml
@@ -674,6 +674,7 @@
Please enter the previous password after restoring database backup. This action can not be undone.
Restore
Restore database error
+ Looks like you restored the app\'s data from a backup (using TitaniumBackup or similar tool). Encryption passphrase should be entered manually. \n\nIf it\'s not the case, please, contact devs
Chat is stopped