From 071b5066908f0d2970f2eff27495695405087741 Mon Sep 17 00:00:00 2001 From: katherine <414924+kabliz@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:55:41 -0800 Subject: [PATCH] [APT-10955] SharedPref Crash Fix Nests the MasterKeys invocation, so that it does not crash on the 2nd attempt. --- .../armadillo/extensions/SharedPrefExt.kt | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Armadillo/src/main/java/com/scribd/armadillo/extensions/SharedPrefExt.kt b/Armadillo/src/main/java/com/scribd/armadillo/extensions/SharedPrefExt.kt index 7496ed5..1922d7e 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/extensions/SharedPrefExt.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/extensions/SharedPrefExt.kt @@ -28,7 +28,7 @@ fun SharedPreferences.deleteEncryptedSharedPreference(context: Context, filename val deleted = sharedPrefsFile.delete() Log.d(tag, "resetStorage() Shared prefs file deleted: $deleted; path: ${sharedPrefsFile.absolutePath}") } else { - Log.d(tag,"resetStorage() Shared prefs file non-existent; path: ${sharedPrefsFile.absolutePath}") + Log.d(tag, "resetStorage() Shared prefs file non-existent; path: ${sharedPrefsFile.absolutePath}") } val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE_NAME) @@ -41,7 +41,7 @@ fun SharedPreferences.deleteEncryptedSharedPreference(context: Context, filename fun createEncryptedSharedPrefKeyStoreWithRetry(context: Context, fileName: String, keystoreAlias: String): SharedPreferences? { val firstAttempt = createEncryptedSharedPrefsKeyStore(context = context, fileName = fileName, keystoreAlias = keystoreAlias) - return if(firstAttempt != null) { + return if (firstAttempt != null) { firstAttempt } else { context.getSharedPreferences(fileName, Context.MODE_PRIVATE).deleteEncryptedSharedPreference( @@ -61,17 +61,18 @@ fun createEncryptedSharedPrefsKeyStore(context: Context, fileName: String, keyst .setEncryptionPaddings(ENCRYPTION_PADDING_NONE) .build() - val keys = try { - MasterKeys.getOrCreate(keySpec) - } catch (ex: Exception) { - //clear corrupted store, contents will be lost - context.getSharedPreferences(fileName, Context.MODE_PRIVATE).deleteEncryptedSharedPreference( - context = context, - filename = fileName, - keystoreAlias = keystoreAlias ) - MasterKeys.getOrCreate(keySpec) - } return try { + val keys = try { + MasterKeys.getOrCreate(keySpec) + } catch (ex: Exception) { + //clear corrupted store, contents will be lost + context.getSharedPreferences(fileName, Context.MODE_PRIVATE).deleteEncryptedSharedPreference( + context = context, + filename = fileName, + keystoreAlias = keystoreAlias) + MasterKeys.getOrCreate(keySpec) + } + EncryptedSharedPreferences.create( fileName, keys, @@ -79,7 +80,7 @@ fun createEncryptedSharedPrefsKeyStore(context: Context, fileName: String, keyst EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ) - } catch(ex: Exception) { + } catch (ex: Exception) { null } } \ No newline at end of file