mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
fix: in-app update works on Android 9 and 10 (#1066)
This commit is contained in:
1 parent
0de6413914
commit
ec5a08ddf1
2 files changed
+49
-10
No files matched your search
@@ -57,7 +57,8 @@ object SelfInstaller {
|
||||
}
|
||||
@Suppress("DEPRECATION")
|
||||
val flags = if (Build.VERSION.SDK_INT >= 28) {
|
||||
PackageManager.GET_SIGNING_CERTIFICATES
|
||||
// Android 9 and 10 only collect an archive's certificates when GET_SIGNATURES is set.
|
||||
PackageManager.GET_SIGNING_CERTIFICATES or PackageManager.GET_SIGNATURES
|
||||
} else {
|
||||
PackageManager.GET_SIGNATURES
|
||||
}
|
||||
@@ -75,15 +76,18 @@ object SelfInstaller {
|
||||
throw IOException(context.getString(R.string.selfinst_downgrade, code, BuildConfig.VERSION_CODE))
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 28) {
|
||||
val signing = info.signingInfo ?: throw IOException(context.getString(R.string.selfinst_no_signing))
|
||||
val digests = signing.apkContentsSigners.orEmpty().map { cert ->
|
||||
MessageDigest.getInstance("SHA-256").digest(cert.toByteArray())
|
||||
.joinToString("") { "%02x".format(it) }
|
||||
}
|
||||
if (digests.none { it.equals(BuildConfig.RELEASE_SIGNER, ignoreCase = true) }) {
|
||||
throw IOException(context.getString(R.string.selfinst_wrong_signer))
|
||||
}
|
||||
val digests = archiveSignerDigests(info)
|
||||
if (digests.isEmpty()) throw IOException(context.getString(R.string.selfinst_no_signing))
|
||||
if (digests.none { it.equals(BuildConfig.RELEASE_SIGNER, ignoreCase = true) }) {
|
||||
throw IOException(context.getString(R.string.selfinst_wrong_signer))
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
internal fun archiveSignerDigests(info: android.content.pm.PackageInfo): List<String> {
|
||||
val certs = (if (Build.VERSION.SDK_INT >= 28) info.signingInfo?.apkContentsSigners else null) ?: info.signatures
|
||||
return certs.orEmpty().map { cert ->
|
||||
MessageDigest.getInstance("SHA-256").digest(cert.toByteArray()).joinToString("") { "%02x".format(it) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.droiddeck.launcher.update
|
||||
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.Signature
|
||||
import java.security.MessageDigest
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class SelfInstallerSignerTest {
|
||||
private val cert = byteArrayOf(1, 2, 3, 4, 5)
|
||||
private val expected = MessageDigest.getInstance("SHA-256").digest(cert).joinToString("") { "%02x".format(it) }
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun archive() = PackageInfo().apply { signatures = arrayOf(Signature(cert)) }
|
||||
|
||||
@Config(sdk = [28, 29])
|
||||
@Test fun androidNineAndTenUseTheSignaturesWhenSigningInfoIsMissing() {
|
||||
assertEquals(listOf(expected), SelfInstaller.archiveSignerDigests(archive()))
|
||||
}
|
||||
|
||||
@Config(sdk = [26])
|
||||
@Test fun androidEightIsCheckedToo() {
|
||||
assertEquals(listOf(expected), SelfInstaller.archiveSignerDigests(archive()))
|
||||
}
|
||||
|
||||
@Config(sdk = [33])
|
||||
@Test fun anArchiveWithNoCertificatesHasNoDigests() {
|
||||
assertTrue(SelfInstaller.archiveSignerDigests(PackageInfo()).isEmpty())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user