Correct misunderstanding of takeIf execution order

The previous code incorrectly assumed `takeIf` prevents the execution of the receiver statement. Since `takeIf` is an extension function, the receiver, `InterceptorUtils.getTransactCode`, was evaluated eagerly *before* the version check predicate could run.

This commit replaces the `takeIf` chain with a standard `if/else` block to ensure the reflection call is only executed when the API level supports it.

Additionally, repeated `IKeystoreService.Stub::class.java` references were refactored into a `stubBinderClass` property.
This commit is contained in:
JingMatrix
2026-01-31 12:51:09 +01:00
parent 129cec06bf
commit 23bef3f88e
@@ -27,23 +27,24 @@ import org.matrix.TEESimulator.pki.CertificateHelper
*/ */
@SuppressLint("BlockedPrivateApi") @SuppressLint("BlockedPrivateApi")
object Keystore2Interceptor : AbstractKeystoreInterceptor() { object Keystore2Interceptor : AbstractKeystoreInterceptor() {
private val stubBinderClass = IKeystoreService.Stub::class.java
// Transaction codes for the IKeystoreService interface methods we are interested in. // Transaction codes for the IKeystoreService interface methods we are interested in.
private val GET_KEY_ENTRY_TRANSACTION = private val GET_KEY_ENTRY_TRANSACTION =
InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "getKeyEntry") InterceptorUtils.getTransactCode(stubBinderClass, "getKeyEntry")
private val DELETE_KEY_TRANSACTION = private val DELETE_KEY_TRANSACTION =
InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "deleteKey") InterceptorUtils.getTransactCode(stubBinderClass, "deleteKey")
private val UPDATE_SUBCOMPONENT_TRANSACTION = private val UPDATE_SUBCOMPONENT_TRANSACTION =
InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "updateSubcomponent") InterceptorUtils.getTransactCode(stubBinderClass, "updateSubcomponent")
private val LIST_ENTRIES_TRANSACTION = private val LIST_ENTRIES_TRANSACTION =
InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "listEntries") InterceptorUtils.getTransactCode(stubBinderClass, "listEntries")
private val LIST_ENTRIES_BATCHED_TRANSACTION = private val LIST_ENTRIES_BATCHED_TRANSACTION =
InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "listEntriesBatched") if (Build.VERSION.SDK_INT >= 34)
.takeIf { Build.VERSION.SDK_INT >= 34 } InterceptorUtils.getTransactCode(stubBinderClass, "listEntriesBatched")
else null
private val transactionNames: Map<Int, String> by lazy { private val transactionNames: Map<Int, String> by lazy {
IKeystoreService.Stub::class stubBinderClass.declaredFields
.java
.declaredFields
.filter { .filter {
it.isAccessible = true it.isAccessible = true
it.type == Int::class.java && it.name.startsWith("TRANSACTION_") it.type == Int::class.java && it.name.startsWith("TRANSACTION_")