Clear generated key cache on keybox updates for Android 12+ (#16)

Ensures that the cache of generated keys is invalidated and cleared whenever a keybox file is updated. This prevents the system from using stale certificates after a keybox change.

Co-authored-by: JingMatrix <jingmatrix@gmail.com>
This commit is contained in:
Qing
2025-11-27 23:29:43 +01:00
committed by JingMatrix
co-authored by JingMatrix
parent a6fa137e32
commit 22cbe5a9a7
2 changed files with 17 additions and 2 deletions
@@ -247,14 +247,20 @@ object ConfigurationManager {
when (path) {
TARGET_PACKAGES_FILE -> loadTargetPackages(file!!)
PATCH_LEVEL_FILE -> loadPatchLevelConfig(file!!)
// Any change to an XML file is assumed to be a keybox. The cache in KeyBoxUtils
// will handle reloading it on its next use.
// Any change to an XML file is assumed to be a keybox.
// The cache in KeyBoxManager will handle reloading it on its next use.
else ->
if (path.endsWith(".xml")) {
SystemLogger.info(
"Keybox file $path may have changed. It will be reloaded on next access."
)
KeyBoxManager.invalidateCache(path)
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
// Clear cached keys possibly containing old certificates
org.matrix.TEESimulator.interception.keystore.shim
.KeyMintSecurityLevelInterceptor
.clearAllGeneratedKeys("updating $file")
}
}
}
}
@@ -218,6 +218,15 @@ class KeyMintSecurityLevelInterceptor(
SystemLogger.debug("Remove cached attestaion key ${keyId}")
}
}
// Clears all cached keys.
fun clearAllGeneratedKeys(reason: String? = null) {
val count = generatedKeys.size
val reasonMessage = reason?.let { " due to $it" } ?: ""
generatedKeys.clear()
attestationKeys.clear()
SystemLogger.info("Cleared all cached keys ($count entries)$reasonMessage.")
}
}
}