Skip to content

RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.

Car: allow Gearslip as a car host (#9)

CIS-C051bc079a79aa
diff --git a/app/src/main/java/com/rfsentinel/app/car/CarHosts.kt b/app/src/main/java/com/rfsentinel/app/car/CarHosts.kt
new file mode 100644
index 0000000..bad2674
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/car/CarHosts.kt
@@ -0,0 +1,58 @@
+package com.rfsentinel.app.car
+
+import android.annotation.SuppressLint
+import android.content.Context
+import android.content.pm.PackageManager
+import android.content.pm.Signature
+import android.os.Build
+import java.security.MessageDigest
+
+/**
+ * Car hosts besides Android Auto. Gearslip (github.com/twinksoftware/Gearslip) is an
+ * open-source Android Auto client that runs car apps without Google Play. Both of its
+ * builds are signed with one key, checked against the official release APK.
+ */
+object CarHosts {
+
+ val GEARSLIP_PACKAGES = listOf("app.seb3thehacker.gearslip", "app.seb3thehacker.gearslip.dev")
+ const val GEARSLIP_CERT_SHA256 = "4cc56ebdd13a54d260eca9b7e2b2ae6b30e2f225bfda8f508f0709edf084948b"
+
+ /** True when [packageName] is Gearslip, runs as [uid] and carries Gearslip's signing key. */
+ fun isGearslip(context: Context, packageName: String, uid: Int): Boolean {
+ if (packageName !in GEARSLIP_PACKAGES) return false
+ val pm = context.packageManager
+ if (pm.getPackagesForUid(uid)?.contains(packageName) != true) return false
+ return try {
+ signedWith(pm, packageName, hexToBytes(GEARSLIP_CERT_SHA256))
+ } catch (_: PackageManager.NameNotFoundException) {
+ false
+ }
+ }
+
+ /**
+ * Same rule as PackageManager.hasSigningCertificate: with several signers all must match,
+ * with one key any key in its rotation history counts.
+ */
+ @SuppressLint("PackageManagerGetSignatures") // Every signer is compared, not just the first.
+ @Suppress("DEPRECATION")
+ internal fun signedWith(pm: PackageManager, packageName: String, sha256: ByteArray): Boolean {
+ fun Signature.matches() =
+ MessageDigest.getInstance("SHA-256").digest(toByteArray()).contentEquals(sha256)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
+ val info = pm.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES)
+ .signingInfo ?: return false
+ return if (info.hasMultipleSigners()) {
+ info.apkContentsSigners.let { it.isNotEmpty() && it.all { s -> s.matches() } }
+ } else {
+ info.signingCertificateHistory.orEmpty().any { it.matches() }
+ }
+ }
+ // Android 8 only knows the v1/v2 signers; all of them must be Gearslip's key.
+ val signatures = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
+ ?: return false
+ return signatures.isNotEmpty() && signatures.all { it.matches() }
+ }
+
+ internal fun hexToBytes(hex: String): ByteArray =
+ ByteArray(hex.length / 2) { hex.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
+}
diff --git a/app/src/main/java/com/rfsentinel/app/car/RFSentinelCarAppService.kt b/app/src/main/java/com/rfsentinel/app/car/RFSentinelCarAppService.kt
index 257a1f4..354fb77 100644
--- a/app/src/main/java/com/rfsentinel/app/car/RFSentinelCarAppService.kt
+++ b/app/src/main/java/com/rfsentinel/app/car/RFSentinelCarAppService.kt
@@ -23,6 +23,9 @@ class RFSentinelCarAppService : CarAppService() {
} else {
HostValidator.Builder(applicationContext)
.addAllowedHosts(androidx.car.app.R.array.hosts_allowlist_sample)
+ .apply {
+ CarHosts.GEARSLIP_PACKAGES.forEach { addAllowedHost(it, CarHosts.GEARSLIP_CERT_SHA256) }
+ }
.build()
}
}
diff --git a/app/src/main/java/com/rfsentinel/app/car/RFSentinelMediaService.kt b/app/src/main/java/com/rfsentinel/app/car/RFSentinelMediaService.kt
index 8e101fb..6d949df 100644
--- a/app/src/main/java/com/rfsentinel/app/car/RFSentinelMediaService.kt
+++ b/app/src/main/java/com/rfsentinel/app/car/RFSentinelMediaService.kt
@@ -89,8 +89,9 @@ class RFSentinelMediaService : MediaBrowserServiceCompat() {
override fun onGetRoot(clientPackageName: String, clientUid: Int, rootHints: Bundle?): BrowserRoot? {
// No "resume playback" card in the phone's quick settings.
if (rootHints?.getBoolean(BrowserRoot.EXTRA_RECENT) == true) return null
- // Detections are only shown to Android Auto (and the car's own media app).
- if (clientUid != Process.myUid() && clientPackageName !in ALLOWED_CALLERS) return null
+ // Detections are only shown to Android Auto, the car's own media app and Gearslip.
+ if (clientUid != Process.myUid() && clientPackageName !in ALLOWED_CALLERS &&
+ !CarHosts.isGearslip(this, clientPackageName, clientUid)) return null
val extras = Bundle().apply {
putInt(MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_BROWSABLE,
MediaConstants.DESCRIPTION_EXTRAS_VALUE_CONTENT_STYLE_LIST_ITEM)
diff --git a/app/src/test/java/com/rfsentinel/app/car/CarHostsTest.kt b/app/src/test/java/com/rfsentinel/app/car/CarHostsTest.kt
new file mode 100644
index 0000000..d8058ee
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/car/CarHostsTest.kt
@@ -0,0 +1,70 @@
+package com.rfsentinel.app.car
+
+import android.app.Application
+import android.content.pm.PackageInfo
+import android.content.pm.Signature
+import android.content.pm.SigningInfo
+import androidx.test.core.app.ApplicationProvider
+import org.junit.Assert.assertArrayEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.Shadows.shadowOf
+import org.robolectric.annotation.Config
+import org.robolectric.shadow.api.Shadow
+
+@RunWith(RobolectricTestRunner::class)
+@Config(sdk = [35], application = Application::class)
+class CarHostsTest {
+
+ private val app = ApplicationProvider.getApplicationContext<Application>()
+
+ @Test
+ fun hexToBytesReadsTheCertificateDigest() {
+ val bytes = CarHosts.hexToBytes(CarHosts.GEARSLIP_CERT_SHA256)
+ assertArrayEquals(byteArrayOf(0x4c, 0xc5.toByte(), 0x6e), bytes.copyOf(3))
+ assert(bytes.size == 32)
+ }
+
+ @Test
+ fun otherPackagesAreNotGearslip() {
+ assertFalse(CarHosts.isGearslip(app, "com.example.reader", 10_123))
+ }
+
+ @Test
+ fun gearslipNameFromAnotherUidIsRefused() {
+ install(CarHosts.GEARSLIP_PACKAGES[0], 10_200, Signature("00"))
+ assertFalse(CarHosts.isGearslip(app, CarHosts.GEARSLIP_PACKAGES[0], 10_201))
+ }
+
+ @Test
+ fun gearslipNameWithAnotherKeyIsRefused() {
+ install(CarHosts.GEARSLIP_PACKAGES[1], 10_300, Signature("00"))
+ assertFalse(CarHosts.isGearslip(app, CarHosts.GEARSLIP_PACKAGES[1], 10_300))
+ }
+
+ @Test
+ fun matchingKeyIsAcceptedAndOtherKeyRefused() {
+ val signature = Signature("0a0b0c")
+ install("com.example.host", 10_400, signature)
+ val digest = java.security.MessageDigest.getInstance("SHA-256").digest(signature.toByteArray())
+ assertTrue(CarHosts.signedWith(app.packageManager, "com.example.host", digest))
+ assertFalse(CarHosts.signedWith(app.packageManager, "com.example.host", ByteArray(32)))
+ }
+
+ private fun install(name: String, uid: Int, signature: Signature) {
+ val pm = shadowOf(app.packageManager)
+ pm.installPackage(PackageInfo().apply {
+ packageName = name
+ @Suppress("DEPRECATION")
+ signatures = arrayOf(signature)
+ signingInfo = Shadow.newInstanceOf(SigningInfo::class.java).also {
+ shadowOf(it).setSignatures(arrayOf(signature))
+ }
+ applicationInfo = android.content.pm.ApplicationInfo().apply { packageName = name; this.uid = uid }
+ })
+ pm.setPackagesForUid(uid, name)
+ }
+}