Migrate old preferences to new format and location

* Migrates the prefs from com.google.android.gms_preferences.xml
  to unified_nlp.xml
* Replaces the SHA-1 with matching SHA-256, since the UI
  code only supports SHA-256
* Removes SHA-1 support from AbstractBackendHelper,
  assuming that all old packages have migrated, and
  since they weren't fully working anyway.

Change-Id: I532883dc2fe1c71b001dc34bb9e6bef93d8681f2
This commit is contained in:
Chirayu Desai 2020-10-31 00:08:23 +05:30 committed by Marvin W
parent 463865fcfa
commit 7aa18fa570
2 changed files with 32 additions and 3 deletions

View File

@ -67,7 +67,7 @@ abstract class AbstractBackendHelper(private val TAG: String, private val contex
Log.w(TAG, "Intent is not properly resolved, can't verify signature. Aborting.")
return
}
val computedDigest = firstSignatureDigest(context, serviceIntent.getPackage(), if (signatureDigest?.length == 40) "SHA-1" else "SHA-256")
val computedDigest = firstSignatureDigest(context, serviceIntent.getPackage(), "SHA-256")
if (signatureDigest != null && signatureDigest != computedDigest) {
Log.w(TAG, "Target signature does not match selected package ($signatureDigest != $computedDigest). Aborting.")
return

View File

@ -53,13 +53,42 @@ class Preferences(private val context: Context) {
}
private fun getStringSetFromAny(key: String): Set<String>? {
migratePreference(key)
val fromNewSettings = preferences.getStringSetCompat(key)
if (fromNewSettings != null) return fromNewSettings
val fromOldSettings = oldPreferences.getStringSetCompat(key)
if (fromOldSettings != null) return fromOldSettings
return systemDefaultPreferences?.getStringSetCompat(key)
}
private fun migratePreference(key: String): Set<String>? {
val fromOldSettings = oldPreferences.getStringSetCompat(key)
if (fromOldSettings != null) {
var newSettings: MutableSet<String> = mutableSetOf<String>()
for (oldBackend in fromOldSettings) {
// Get package name and sha1
val parts = oldBackend.split("/".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
if (parts.size < 3) continue // skip unsigned
val pkgName = parts[0]
val component = parts[1]
val oldSig = parts[2]
if (oldSig?.length != 40) continue // skip if not sha1
// Get matching sha256
val sha1 = AbstractBackendHelper.firstSignatureDigest(context, pkgName, "SHA-1")
val sha256 = AbstractBackendHelper.firstSignatureDigest(context, pkgName, "SHA-256")
// If the system sha1 matches what we had stored
if (oldSig == sha1) {
// Replace it with the sha256
val newBackend = "${pkgName}/${component}/${sha256}"
newSettings.add(newBackend)
}
}
if (preferences.edit().putStringSetCompat(key, newSettings.toSet()).commit()) {
// Only delete the old preference once committed.
oldPreferences.edit().remove(key).apply()
}
}
return null
}
var locationBackends: Set<String>
get() = getStringSetFromAny(PREF_LOCATION_BACKENDS) ?: emptySet()
set(backends) {