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 904e8ef..80f92ce 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 @@ -31,6 +31,8 @@ object Keystore2Interceptor : AbstractKeystoreInterceptor() { InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "getKeyEntry") private val DELETE_KEY_TRANSACTION = InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "deleteKey") + private val UPDATE_SUBCOMPONENT_TRANSACTION = + InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "updateSubcomponent") private val transactionNames: Map by lazy { IKeystoreService.Stub::class @@ -89,16 +91,23 @@ object Keystore2Interceptor : AbstractKeystoreInterceptor() { callingPid: Int, data: Parcel, ): TransactionResult { - if (code == GET_KEY_ENTRY_TRANSACTION || code == DELETE_KEY_TRANSACTION) { + if ( + code == GET_KEY_ENTRY_TRANSACTION || + code == DELETE_KEY_TRANSACTION || + code == UPDATE_SUBCOMPONENT_TRANSACTION + ) { logTransaction(txId, transactionNames[code]!!, callingUid, callingPid) + if (ConfigurationManager.shouldSkipUid(callingUid)) + return TransactionResult.ContinueAndSkipPost + + if (code == UPDATE_SUBCOMPONENT_TRANSACTION) + return handleUpdateSubcomponent(callingUid, data) + data.enforceInterface(IKeystoreService.DESCRIPTOR) val descriptor = data.readTypedObject(KeyDescriptor.CREATOR) - ?: return TransactionResult.SkipTransaction - - if (ConfigurationManager.shouldSkipUid(callingUid)) - return TransactionResult.ContinueAndSkipPost + ?: return TransactionResult.ContinueAndSkipPost SystemLogger.info("Handling ${transactionNames[code]!!} ${descriptor.alias}") val keyId = KeyIdentifier(callingUid, descriptor.alias) @@ -223,4 +232,25 @@ object Keystore2Interceptor : AbstractKeystoreInterceptor() { } return TransactionResult.SkipTransaction } + + private fun handleUpdateSubcomponent(callingUid: Int, data: Parcel): TransactionResult { + data.enforceInterface(IKeystoreService.DESCRIPTOR) + val descriptor = data.readTypedObject(KeyDescriptor.CREATOR) + val generatedKeyInfo = + KeyMintSecurityLevelInterceptor.findGeneratedKeyByKeyId(callingUid, descriptor?.nspace) + ?: return TransactionResult.ContinueAndSkipPost + + SystemLogger.info("Updating sub-component with key[${generatedKeyInfo.nspace}]") + val metadata = generatedKeyInfo.response.metadata + val publicCert = data.createByteArray() + val certificateChain = data.createByteArray() + + metadata.certificate = publicCert + metadata.certificateChain = certificateChain + SystemLogger.verbose( + "Key updated with sizes: [publicCert, certificateChain] = [${publicCert?.size}, ${certificateChain?.size}]" + ) + + return InterceptorUtils.createSuccessReply(writeResultCode = false) + } } diff --git a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt index df3623a..8b81c70 100644 --- a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt +++ b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/shim/KeyMintSecurityLevelInterceptor.kt @@ -349,9 +349,10 @@ class KeyMintSecurityLevelInterceptor( * @param nspace The unique key identifier from the operation's KeyDescriptor. * @return The matching GeneratedKeyInfo if found, otherwise null. */ - private fun findGeneratedKeyByKeyId(callingUid: Int, nspace: Long): GeneratedKeyInfo? { + fun findGeneratedKeyByKeyId(callingUid: Int, nspace: Long?): GeneratedKeyInfo? { // Iterate through all entries in the map to check both the key (for UID) and value (for // nspace). + if (nspace == null || nspace == 0L) return null return generatedKeys.entries .filter { (keyIdentifier, _) -> keyIdentifier.uid == callingUid } .find { (_, info) -> info.nspace == nspace }