|
28 | 28 |
|
29 | 29 | import android.app.Application; |
30 | 30 | import android.app.Instrumentation; |
| 31 | +import android.content.SharedPreferences; |
31 | 32 |
|
32 | 33 | import com.salesforce.androidsdk.TestForceApp; |
| 34 | +import com.salesforce.androidsdk.analytics.security.Encryptor; |
| 35 | +import com.salesforce.androidsdk.app.SalesforceSDKManager; |
33 | 36 |
|
34 | 37 | import org.junit.Assert; |
35 | 38 | import org.junit.Before; |
|
40 | 43 | import androidx.test.filters.SmallTest; |
41 | 44 | import androidx.test.platform.app.InstrumentationRegistry; |
42 | 45 |
|
| 46 | +import java.security.PrivateKey; |
| 47 | +import java.security.PublicKey; |
| 48 | + |
43 | 49 | /** |
44 | 50 | * Tests for {@link SalesforceKeyGenerator}. |
45 | 51 | * |
@@ -87,4 +93,111 @@ public void testGetEncryptionKey() { |
87 | 93 | Assert.assertEquals("Encryption keys with the same name should be the same", id1Again, id1); |
88 | 94 | Assert.assertNotSame("Encryption keys with different names should be different", id2, id1); |
89 | 95 | } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testGetUniqueIdStoredUsingLegacyKeyPairAndOldCipherMode() { |
| 99 | + encryptAndStoreInPrefs("test_name", "test_value", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1); |
| 100 | + Assert.assertEquals("test_value", decryptFromPrefs("test_name", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 101 | + |
| 102 | + // Now calling getUniqueId |
| 103 | + Assert.assertEquals("test_value", SalesforceKeyGenerator.getUniqueId("test_name")); |
| 104 | + |
| 105 | + // The value should have been re-encrypted |
| 106 | + // - it should not be decryptable with the legacy key pair |
| 107 | + // - it should be decryptable with the msdk key pair |
| 108 | + Assert.assertNull(decryptFromPrefs("test_name", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 109 | + Assert.assertEquals("test_value", decryptFromPrefs("test_name", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testGetUniqueIdStoredUsingLegacyKeyPairAndNewCipherMode() { |
| 114 | + encryptAndStoreInPrefs("test_name", "test_value", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256); |
| 115 | + Assert.assertEquals("test_value", decryptFromPrefs("test_name", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 116 | + |
| 117 | + // Now calling getUniqueId |
| 118 | + Assert.assertEquals("test_value", SalesforceKeyGenerator.getUniqueId("test_name")); |
| 119 | + |
| 120 | + // The value should have been re-encrypted |
| 121 | + // - it should not be decryptable with the legacy key pair |
| 122 | + // - it should be decryptable with the msdk key pair |
| 123 | + Assert.assertNull(decryptFromPrefs("test_name", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 124 | + Assert.assertEquals("test_value", decryptFromPrefs("test_name", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testMultipleGetUniqueIdStoredUsingLegacyKeyPair() { |
| 129 | + encryptAndStoreInPrefs("test_name_1", "test_value_1", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1); |
| 130 | + encryptAndStoreInPrefs("test_name_2", "test_value_2", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1); |
| 131 | + encryptAndStoreInPrefs("test_name_3", "test_value_3", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1); |
| 132 | + Assert.assertEquals("test_value_1", decryptFromPrefs("test_name_1", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 133 | + Assert.assertEquals("test_value_2", decryptFromPrefs("test_name_2", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 134 | + Assert.assertEquals("test_value_3", decryptFromPrefs("test_name_3", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 135 | + |
| 136 | + // Now calling getUniqueId for the first one |
| 137 | + Assert.assertEquals("test_value_1", SalesforceKeyGenerator.getUniqueId("test_name_1")); |
| 138 | + |
| 139 | + // The value should have been re-encrypted |
| 140 | + // - it should not be decryptable with the legacy key pair |
| 141 | + // - it should be decryptable with the msdk key pair |
| 142 | + Assert.assertNull(decryptFromPrefs("test_name_1", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 143 | + Assert.assertEquals("test_value_1", decryptFromPrefs("test_name_1", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 144 | + |
| 145 | + // Other values should not have been re-encrypted |
| 146 | + Assert.assertEquals("test_value_2", decryptFromPrefs("test_name_2", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 147 | + Assert.assertEquals("test_value_3", decryptFromPrefs("test_name_3", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 148 | + |
| 149 | + // Now calling getUniqueId for the second one |
| 150 | + Assert.assertEquals("test_value_2", SalesforceKeyGenerator.getUniqueId("test_name_2")); |
| 151 | + |
| 152 | + // The value should have been re-encrypted |
| 153 | + // - it should not be decryptable with the legacy key pair |
| 154 | + // - it should be decryptable with the msdk key pair |
| 155 | + Assert.assertNull(decryptFromPrefs("test_name_2", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 156 | + Assert.assertEquals("test_value_2", decryptFromPrefs("test_name_2", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 157 | + |
| 158 | + // The already re-encrypted value should have been left alone |
| 159 | + Assert.assertNull(decryptFromPrefs("test_name_1", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 160 | + Assert.assertEquals("test_value_1", decryptFromPrefs("test_name_1", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 161 | + |
| 162 | + // The third one should not have been re-encrypted |
| 163 | + Assert.assertEquals("test_value_3", decryptFromPrefs("test_name_3", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 164 | + |
| 165 | + // Now calling getUniqueId for the third one |
| 166 | + Assert.assertEquals("test_value_3", SalesforceKeyGenerator.getUniqueId("test_name_3")); |
| 167 | + |
| 168 | + // The value should have been re-encrypted |
| 169 | + // - it should not be decryptable with the legacy key pair |
| 170 | + // - it should be decryptable with the msdk key pair |
| 171 | + Assert.assertNull(decryptFromPrefs("test_name_3", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 172 | + Assert.assertEquals("test_value_3", decryptFromPrefs("test_name_3", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 173 | + |
| 174 | + // The already re-encrypted values should have been left alone |
| 175 | + Assert.assertNull(decryptFromPrefs("test_name_1", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 176 | + Assert.assertEquals("test_value_1", decryptFromPrefs("test_name_1", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 177 | + Assert.assertNull(decryptFromPrefs("test_name_2", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1)); |
| 178 | + Assert.assertEquals("test_value_2", decryptFromPrefs("test_name_2", SalesforceKeyGenerator.MSDK_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_OAEP_SHA256)); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testMakeSureLegacyKeyPairNotRecreated() { |
| 184 | + encryptAndStoreInPrefs("test_name", "test_value", SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS, Encryptor.CipherMode.RSA_PKCS1); |
| 185 | + PublicKey legacyPublicKey = KeyStoreWrapper.getInstance().getRSAPublicKey(SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS); |
| 186 | + // Now calling getUniqueId |
| 187 | + Assert.assertEquals("test_value", SalesforceKeyGenerator.getUniqueId("test_name")); |
| 188 | + // The legacy key pair should NOT have been deleted or recreated |
| 189 | + Assert.assertEquals(legacyPublicKey.toString(), KeyStoreWrapper.getInstance().getRSAPublicKey(SalesforceKeyGenerator.LEGACY_KEYPAIR_ALIAS).toString()); |
| 190 | + } |
| 191 | + |
| 192 | + private void encryptAndStoreInPrefs(String name, String value, String keyPairAlias, Encryptor.CipherMode cipherMode) { |
| 193 | + PublicKey publicKey = KeyStoreWrapper.getInstance().getRSAPublicKey(keyPairAlias); |
| 194 | + String encryptedKey = Encryptor.encryptWithRSA(publicKey, value, cipherMode); |
| 195 | + SalesforceKeyGenerator.storeInSharedPrefs("id_" + name, encryptedKey); |
| 196 | + } |
| 197 | + |
| 198 | + private String decryptFromPrefs(String name, String keyPairAlias, Encryptor.CipherMode cipherMode) { |
| 199 | + PrivateKey privateKey = KeyStoreWrapper.getInstance().getRSAPrivateKey(keyPairAlias); |
| 200 | + String encryptedValue = SalesforceKeyGenerator.readFromSharedPrefs("id_" + name); |
| 201 | + return Encryptor.decryptWithRSA(privateKey, encryptedValue, cipherMode); |
| 202 | + } |
90 | 203 | } |
0 commit comments