From 031daf94e68aabedfddab490686516845a0a301d Mon Sep 17 00:00:00 2001 From: megamegax Date: Fri, 18 Oct 2024 11:09:38 +0200 Subject: [PATCH] fix(shared-prefs): rely on keyGenerator.generateKey for storing the key SUITEDEV-36807 Co-authored-by: LasOri <24588073+LasOri@users.noreply.github.com> --- .../core/crypto/SharedPreferenceCryptoTest.kt | 25 ++++++++++++------- .../core/crypto/SharedPreferenceCrypto.kt | 18 ++++++------- .../java/com/emarsys/EmarsysE2ETests.kt | 3 --- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/core/src/androidTest/java/com/emarsys/core/crypto/SharedPreferenceCryptoTest.kt b/core/src/androidTest/java/com/emarsys/core/crypto/SharedPreferenceCryptoTest.kt index 665d9ecc..f800fc39 100644 --- a/core/src/androidTest/java/com/emarsys/core/crypto/SharedPreferenceCryptoTest.kt +++ b/core/src/androidTest/java/com/emarsys/core/crypto/SharedPreferenceCryptoTest.kt @@ -19,6 +19,10 @@ import javax.crypto.SecretKey import javax.crypto.spec.GCMParameterSpec class SharedPreferenceCryptoTest : AnnotationSpec() { + private companion object { + const val encryptedBase64 = "Base64EncryptedBase64IV123123" + + } private lateinit var sharedPreferenceCrypto: SharedPreferenceCrypto private lateinit var mockKeyStore: KeyStore @@ -68,13 +72,11 @@ class SharedPreferenceCryptoTest : AnnotationSpec() { every { mockKeyStore.containsAlias(any()) } returns false every { mockKeyGenerator.init(any()) } just Runs every { mockKeyGenerator.generateKey() } returns mockSecretKey - every { mockKeyStore.setEntry(any(), any(), null) } just Runs val result = sharedPreferenceCrypto.getOrCreateSecretKey() result shouldBe mockSecretKey verify { mockKeyGenerator.generateKey() } - verify { mockKeyStore.setEntry(any(), any(), null) } } @Test @@ -112,7 +114,6 @@ class SharedPreferenceCryptoTest : AnnotationSpec() { @Test fun testDecrypt_Success() { - val value = "IVBase64EncryptedBase64" val ivBytes = byteArrayOf(1, 2, 3, 4) val encryptedBytes = byteArrayOf(5, 6, 7, 8) val decryptedBytes = "decrypted".toByteArray() @@ -130,24 +131,30 @@ class SharedPreferenceCryptoTest : AnnotationSpec() { } just Runs every { mockCipher.doFinal(encryptedBytes) } returns decryptedBytes - val result = sharedPreferenceCrypto.decrypt(value, mockSecretKey) + val result = sharedPreferenceCrypto.decrypt(encryptedBase64, mockSecretKey) result shouldBe "decrypted" } @Test fun testDecrypt_Exception() { - val value = "IVBase64EncryptedBase64" - + val IVValue = "Base64EncryptedBase64IV123" + val decryptedBytes = encryptedBase64.toByteArray() + every { + mockCipher.init(any(), mockSecretKey, any()) + } just Runs + every { + mockCipher.doFinal(any()) + } returns decryptedBytes every { Base64.decode( - any(), + IVValue, Base64.DEFAULT ) } throws GeneralSecurityException("Decryption failed") - val result = sharedPreferenceCrypto.decrypt(value, mockSecretKey) + val result = sharedPreferenceCrypto.decrypt(encryptedBase64, mockSecretKey) - result shouldBe value + result shouldBe encryptedBase64 } } \ No newline at end of file diff --git a/core/src/main/java/com/emarsys/core/crypto/SharedPreferenceCrypto.kt b/core/src/main/java/com/emarsys/core/crypto/SharedPreferenceCrypto.kt index a3f4b851..a0ea7850 100644 --- a/core/src/main/java/com/emarsys/core/crypto/SharedPreferenceCrypto.kt +++ b/core/src/main/java/com/emarsys/core/crypto/SharedPreferenceCrypto.kt @@ -31,13 +31,7 @@ class SharedPreferenceCrypto { .setKeySize(256) .build() keyGenerator.init(keyGenParameterSpec) - val secretKey = keyGenerator.generateKey() - keyStore.setEntry( - KEYSTORE_ALIAS, - KeyStore.SecretKeyEntry(secretKey), - null - ) - return secretKey + return keyGenerator.generateKey() } return keyStore.getKey(KEYSTORE_ALIAS, null) as SecretKey @@ -60,10 +54,9 @@ class SharedPreferenceCrypto { fun decrypt(value: String, secretKey: SecretKey): String { return try { - val ivBase64 = value.substring(0, 16) - val encryptedBase64 = value.substring(16) - val ivBytes = Base64.decode(ivBase64, Base64.DEFAULT) - val encryptedBytes = Base64.decode(encryptedBase64, Base64.DEFAULT) + val ivBytes = Base64.decode(value.substring(0, 16), Base64.DEFAULT) + val encryptedBytes = Base64.decode(value.substring(16), Base64.DEFAULT) + val cipher = Cipher.getInstance("AES/GCM/NoPadding") cipher.init(Cipher.DECRYPT_MODE, secretKey, GCMParameterSpec(128, ivBytes)) val decrypted = cipher.doFinal(encryptedBytes) @@ -71,6 +64,9 @@ class SharedPreferenceCrypto { } catch (e: GeneralSecurityException) { e.printStackTrace() value + } catch (e: IllegalArgumentException) { + e.printStackTrace() + value } } } \ No newline at end of file diff --git a/emarsys-e2e-test/src/androidTest/java/com/emarsys/EmarsysE2ETests.kt b/emarsys-e2e-test/src/androidTest/java/com/emarsys/EmarsysE2ETests.kt index ad714210..764bd8a0 100644 --- a/emarsys-e2e-test/src/androidTest/java/com/emarsys/EmarsysE2ETests.kt +++ b/emarsys-e2e-test/src/androidTest/java/com/emarsys/EmarsysE2ETests.kt @@ -64,7 +64,6 @@ class EmarsysE2ETests : AnnotationSpec() { } @Test - fun testChangeApplicationCodeFromNull() { setup(null) @@ -83,7 +82,6 @@ class EmarsysE2ETests : AnnotationSpec() { } @Test - fun testChangeApplicationCodeToNull() { var disabled = true setup(APPLICATION_CODE) @@ -107,7 +105,6 @@ class EmarsysE2ETests : AnnotationSpec() { } @Test - fun testInbox_addTag_removeTag() { setup(APPLICATION_CODE)