From 119350f24bbe24715c7f3b2b8f2dc0f44ff0ada0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=BD=BC?= <110387028+XiaoTong6666@users.noreply.github.com> Date: Fri, 5 Dec 2025 03:01:16 +0800 Subject: [PATCH] Correctly handle `deleteKey` for software keys (#42) This resolves an issue introduced in 733e64c where a `deleteKey` transaction for a software-generated key was incorrectly passed through to the hardware keystore. Since the hardware is unaware of such keys, this results in inconsistent state management. The success reply is formatted correctly without a result code, per the AIDL interface specification. Reference: https://cs.android.com/android/platform/superproject/main/+/main:out/soong/.intermediates/system/hardware/interfaces/keystore2/aidl/android.system.keystore2-V6-java-source/gen/android/system/keystore2/IKeystoreSecurityLevel.java;l=406 --- .../interception/keystore/InterceptorUtils.kt | 8 ++++++-- .../interception/keystore/Keystore2Interceptor.kt | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/InterceptorUtils.kt b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/InterceptorUtils.kt index 861525b..c793de2 100644 --- a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/InterceptorUtils.kt +++ b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/InterceptorUtils.kt @@ -42,11 +42,15 @@ object InterceptorUtils { } /** Creates an `OverrideReply` parcel that indicates success with no data. */ - fun createSuccessReply(): BinderInterceptor.TransactionResult.OverrideReply { + fun createSuccessReply( + writeResultCode: Boolean = true + ): BinderInterceptor.TransactionResult.OverrideReply { val parcel = Parcel.obtain().apply { writeNoException() - writeInt(KeyStore.NO_ERROR) + if (writeResultCode) { + writeInt(KeyStore.NO_ERROR) + } } return BinderInterceptor.TransactionResult.OverrideReply(0, parcel) } diff --git a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt index 8140925..904e8ef 100644 --- a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt +++ b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt @@ -104,7 +104,13 @@ object Keystore2Interceptor : AbstractKeystoreInterceptor() { val keyId = KeyIdentifier(callingUid, descriptor.alias) if (code == DELETE_KEY_TRANSACTION) { - KeyMintSecurityLevelInterceptor.cleanupKeyData(keyId) + if (KeyMintSecurityLevelInterceptor.getGeneratedKeyResponse(keyId) != null) { + KeyMintSecurityLevelInterceptor.cleanupKeyData(keyId) + SystemLogger.info( + "[TX_ID: $txId] Deleted cached keypair ${descriptor.alias}, replying with empty response." + ) + return InterceptorUtils.createSuccessReply(writeResultCode = false) + } return TransactionResult.ContinueAndSkipPost }