From 9d5726f084ac85018a734556fc44f32b06cfd441 Mon Sep 17 00:00:00 2001 From: Anton Tkachev Date: Fri, 22 Mar 2024 14:28:57 +0400 Subject: [PATCH] AND-6517 [Biometrics] Added locking when storing data to "AuthenticatedStorage" --- .../authentication/storage/AuthenticatedStorage.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tangem-sdk-core/src/main/java/com/tangem/common/authentication/storage/AuthenticatedStorage.kt b/tangem-sdk-core/src/main/java/com/tangem/common/authentication/storage/AuthenticatedStorage.kt index 8a027588..173067c0 100644 --- a/tangem-sdk-core/src/main/java/com/tangem/common/authentication/storage/AuthenticatedStorage.kt +++ b/tangem-sdk-core/src/main/java/com/tangem/common/authentication/storage/AuthenticatedStorage.kt @@ -6,6 +6,8 @@ import com.tangem.common.authentication.keystore.MasterKeyConfigs import com.tangem.common.services.secure.SecureStorage import com.tangem.crypto.operations.AESCipherOperations import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import javax.crypto.SecretKey @@ -20,6 +22,8 @@ class AuthenticatedStorage( private val keystoreManager: KeystoreManager, ) { + private val storeMutex = Mutex() + /** * Retrieves and decrypts data from the storage after necessary user authentication. * @@ -166,10 +170,12 @@ class AuthenticatedStorage( * @param keyAlias The unique identifier which will be associated with the encrypted data. * @param data The plain data to be encrypted and stored. */ - suspend fun store(keyAlias: String, data: ByteArray) = withContext(Dispatchers.IO) { - val encryptedData = encrypt(keyAlias, data) + suspend fun store(keyAlias: String, data: ByteArray) = storeMutex.withLock { + withContext(Dispatchers.IO) { + val encryptedData = encrypt(keyAlias, data) - secureStorage.store(encryptedData, keyAlias) + secureStorage.store(encryptedData, keyAlias) + } } /**