-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[APT-10658] Better EncryptedSharedPreferences Resilience #52
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ad0659
[APT-10658] Better EncryptedSharedPreference Resilience
kabliz f1d892f
[APT-10650] Better DrmInfo Resilience
kabliz d38093f
[APT-10658] Better SharedPref Resilience
kabliz 845c397
Merge pull request #53 from scribd/kabliz/APT-10650-3-try-to-play
kabliz be81c0e
[APT-10658] Better SharedPref Resilience
kabliz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Armadillo/src/main/java/com/scribd/armadillo/extensions/SharedPrefExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.scribd.armadillo.extensions | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.security.keystore.KeyGenParameterSpec | ||
import android.security.keystore.KeyProperties.BLOCK_MODE_GCM | ||
import android.security.keystore.KeyProperties.ENCRYPTION_PADDING_NONE | ||
import android.security.keystore.KeyProperties.PURPOSE_DECRYPT | ||
import android.security.keystore.KeyProperties.PURPOSE_ENCRYPT | ||
import android.util.Log | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKeys | ||
import com.scribd.armadillo.Constants.Keys.ANDROID_KEYSTORE_NAME | ||
import java.io.File | ||
import java.security.KeyStore | ||
|
||
fun SharedPreferences.deleteEncryptedSharedPreference(context: Context, filename: String, keystoreAlias: String) { | ||
val tag = "DeletingSharedPrefs" | ||
try { | ||
//maybe deletes the shared preference file, this is not guaranteed to work. | ||
val sharedPrefsFile = File( | ||
(context.filesDir.getParent()?.plus("/shared_prefs/")) + filename + ".xml" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's worth adding a comment that this file is only a guess at the filepath, and may not actually exist depending on SDK version and OEM, and what happens if we do/don't delete it. |
||
) | ||
|
||
edit().clear().commit() | ||
|
||
if (sharedPrefsFile.exists()) { | ||
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}") | ||
} | ||
|
||
val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE_NAME) | ||
keyStore.load(null) | ||
keyStore.deleteEntry(keystoreAlias) | ||
} catch (e: Exception) { | ||
Log.e(tag, "Error occurred while trying to reset shared prefs", e) | ||
} | ||
} | ||
|
||
fun createEncryptedSharedPrefKeyStoreWithRetry(context: Context, fileName: String, keystoreAlias: String): SharedPreferences? { | ||
val firstAttempt = createEncryptedSharedPrefsKeyStore(context = context, fileName = fileName, keystoreAlias = keystoreAlias) | ||
return if(firstAttempt != null) { | ||
firstAttempt | ||
} else { | ||
context.getSharedPreferences(fileName, Context.MODE_PRIVATE).deleteEncryptedSharedPreference( | ||
context = context, | ||
filename = fileName, | ||
keystoreAlias = keystoreAlias | ||
) | ||
createEncryptedSharedPrefsKeyStore(context = context, fileName = fileName, keystoreAlias = keystoreAlias) | ||
} | ||
} | ||
|
||
fun createEncryptedSharedPrefsKeyStore(context: Context, fileName: String, keystoreAlias: String) | ||
: SharedPreferences? { | ||
val keySpec = KeyGenParameterSpec.Builder(keystoreAlias, PURPOSE_ENCRYPT or PURPOSE_DECRYPT) | ||
.setKeySize(256) | ||
.setBlockModes(BLOCK_MODE_GCM) | ||
.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 { | ||
EncryptedSharedPreferences.create( | ||
fileName, | ||
keys, | ||
context, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
) | ||
} catch(ex: Exception) { | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we try to put it in legacy if secure doesn't exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be failing insecure.