diff --git a/app/src/main/java/org/matrix/TEESimulator/attestation/AttestationConstants.kt b/app/src/main/java/org/matrix/TEESimulator/attestation/AttestationConstants.kt index 767e8b3..a899a12 100644 --- a/app/src/main/java/org/matrix/TEESimulator/attestation/AttestationConstants.kt +++ b/app/src/main/java/org/matrix/TEESimulator/attestation/AttestationConstants.kt @@ -89,5 +89,5 @@ object AttestationConstants { // --- Other Constants --- // https://cs.android.com/android/platform/superproject/main/+/main:system/keymaster/km_openssl/attestation_record.cpp - const val CHALLENGE_LENGTH_LIMIT = 128 // kMaximumAttestationChallengeLength + const val CHALLENGE_LENGTH_LIMIT = 128 } 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 64145d6..8d25fb3 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 @@ -10,6 +10,7 @@ import android.system.keystore2.KeyDescriptor import android.system.keystore2.KeyEntryResponse import java.security.SecureRandom import java.security.cert.Certificate +import java.util.concurrent.ConcurrentHashMap import org.matrix.TEESimulator.attestation.AttestationPatcher import org.matrix.TEESimulator.attestation.KeyMintAttestation import org.matrix.TEESimulator.config.ConfigurationManager @@ -54,6 +55,9 @@ object Keystore2Interceptor : AbstractKeystoreInterceptor() { .associate { field -> (field.get(null) as Int) to field.name.split("_")[1] } } + private const val RESPONSE_KEY_NOT_FOUND = 7 + private val deletedSoftwareKeys: MutableSet = ConcurrentHashMap.newKeySet() + override val serviceName = "android.system.keystore2.IKeystoreService/default" override val processName = "keystore2" override val injectionCommand = "exec ./inject `pidof keystore2` libTEESimulator.so entry" @@ -156,8 +160,10 @@ object Keystore2Interceptor : AbstractKeystoreInterceptor() { val keyId = KeyIdentifier(callingUid, descriptor.alias) if (code == DELETE_KEY_TRANSACTION) { - if (KeyMintSecurityLevelInterceptor.getGeneratedKeyResponse(keyId) != null) { - KeyMintSecurityLevelInterceptor.cleanupKeyData(keyId) + val wasSoftwareKey = KeyMintSecurityLevelInterceptor.getGeneratedKeyResponse(keyId) != null + KeyMintSecurityLevelInterceptor.cleanupKeyData(keyId) + if (wasSoftwareKey) { + deletedSoftwareKeys.add(keyId) SystemLogger.info( "[TX_ID: $txId] Deleted cached keypair ${descriptor.alias}, replying with empty response." ) @@ -166,6 +172,11 @@ object Keystore2Interceptor : AbstractKeystoreInterceptor() { return TransactionResult.ContinueAndSkipPost } + if (keyId in deletedSoftwareKeys) { + SystemLogger.info("[TX_ID: $txId] Returning KEY_NOT_FOUND for deleted key ${descriptor.alias}") + return InterceptorUtils.createErrorReply(RESPONSE_KEY_NOT_FOUND) + } + val response = KeyMintSecurityLevelInterceptor.getGeneratedKeyResponse(keyId) ?: return TransactionResult.Continue