java: Implement keystore clearing for target packages during initialization

- Currently using keystore2 API.
- TODO: Add support for keystore1.

Signed-off-by: Dakkshesh <beakthoven@gmail.com>
This commit is contained in:
Dakkshesh
2025-08-31 20:48:52 +05:30
parent 1e53ffeea0
commit c285992226
4 changed files with 60 additions and 0 deletions
@@ -36,9 +36,24 @@ private fun initializeInterceptors() {
}
PkgConfig.initialize()
clearKeyStore()
Logger.i("Interceptors initialized successfully")
}
private fun clearKeyStore() {
val targetUIDs = PkgConfig.getTargetPackageUids()
Logger.i("Clearing keystore for ${targetUIDs.size} target packages")
targetUIDs.forEach { uid ->
kotlin.runCatching {
android.security.AndroidKeyStoreMaintenance.clearNamespace(0, uid.toLong())
Logger.d("Cleared keystore namespace for UID: $uid")
}.onFailure {
Logger.e("Failed to clear keystore for UID: $uid", it)
}
}
}
private fun selectKeystoreInterceptor() = when {
Build.VERSION.SDK_INT in Build.VERSION_CODES.Q..Build.VERSION_CODES.R -> {
Logger.i("Using KeystoreInterceptor for Android Q/R (SDK ${Build.VERSION.SDK_INT})")
@@ -9,6 +9,7 @@ import android.content.pm.IPackageManager
import android.os.FileObserver
import android.os.IBinder
import android.os.IInterface
import android.os.Process
import android.os.ServiceManager
import io.github.beakthoven.TrickyStoreOSS.AttestUtils.TEEStatus
import io.github.beakthoven.TrickyStoreOSS.KeyBoxUtils
@@ -174,6 +175,33 @@ object PkgConfig {
return false
}.onFailure { Logger.e("failed to get packages", it) }.getOrNull() ?: false
private fun getTargetPackages(): Set<String> {
return (hackPackages + generatePackages + packageModes.keys).toSet()
}
fun getTargetPackageUids(): Set<Int> {
val result = mutableSetOf<Int>()
val pm = getPm() ?: return emptySet()
val flags = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) 0L else 0
val userId = Process.myUid() / 100000
getTargetPackages().forEach { pkg ->
kotlin.runCatching {
val appInfo = pm.getApplicationInfo(pkg, flags, userId)
if (appInfo != null) {
result.add(appInfo.uid)
Logger.d("Got UID ${appInfo.uid} for package: $pkg")
} else {
Logger.e("ApplicationInfo is null for package: $pkg")
}
}.onFailure {
Logger.e("Failed to get UID for package: $pkg", it)
}
}
return result
}
@Volatile
var _customPatchLevel: CustomPatchLevel? = null
@@ -10,6 +10,10 @@ import android.os.IBinder;
public interface IPackageManager {
String[] getPackagesForUid(int uid);
ApplicationInfo getApplicationInfo(String packageName, int flags, int userId);
ApplicationInfo getApplicationInfo(String packageName, long flags, int userId);
PackageInfo getPackageInfo(String packageName, long flags, int userId);
PackageInfo getPackageInfo(String packageName, int flags, int userId);
@@ -0,0 +1,13 @@
/*
* Copyright 2025 Dakkshesh <beakthoven@gmail.com>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package android.security;
public class AndroidKeyStoreMaintenance {
public static int clearNamespace(int domain, long namespace) {
throw new RuntimeException("");
}
}