Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/sdk 4214/Encryption v2 #719

Merged
merged 79 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
a195d02
task(SDK-4206) - Adds AES-GCM crypt class
Anush-Shand Dec 18, 2024
4b494d4
feat(SDK-4214): fixes conflict in cherry pick
Anush-Shand Dec 19, 2024
0b233b6
chore(SDK-4214): adds arg names and when instead of if else
CTLalit Dec 26, 2024
f7583df
feat(SDK-4214): sanitises code
CTLalit Dec 26, 2024
2207291
feat(SDK-4214): replaces trim with substring
CTLalit Dec 26, 2024
aff0d53
chore(SDK-4214): code readability.
CTLalit Dec 26, 2024
38ae018
feat(SDK-4214): Simplifies encryption logic
CTLalit Dec 27, 2024
c049136
task(SDK-4214) - Adds logic for encryption migration based on new app…
Anush-Shand Dec 30, 2024
991d27c
task(SDK-4214) - Fixes migrationStep
Anush-Shand Dec 30, 2024
a6d26eb
task(SDK-4214) - Fixes migrationStep for InApp data
Anush-Shand Dec 30, 2024
2d0628b
task(SDK-4214) - Cleanup and readability
Anush-Shand Dec 30, 2024
6e3f12f
task(SDK-4214) - Cleanup and formatting
Anush-Shand Dec 30, 2024
ac38ef2
task(SDK-4214) - Improves readability
Anush-Shand Dec 30, 2024
d8a9f6f
task(SDK-4214) - More syntactic sugar
Anush-Shand Dec 30, 2024
1a91c44
task(SDK-4214) - Makes prefix and postfix more complex to make it les…
Anush-Shand Dec 30, 2024
18ca5e3
task(SDK-4214) - Adds logic for saving data if AES -> plaintext passe…
Anush-Shand Dec 30, 2024
bab4cbb
task(SDK-4214) - Addresses comments
Anush-Shand Dec 30, 2024
cd76103
task(SDK-4214) - Adds support for API 21 and 22
Anush-Shand Dec 30, 2024
d58f5a8
task(SDK-4206) - Adds AES-GCM crypt class
Anush-Shand Dec 18, 2024
1a5ba2c
feat(SDK-4214): fixes conflict in cherry pick
Anush-Shand Dec 19, 2024
c39e449
chore(SDK-4214): adds arg names and when instead of if else
CTLalit Dec 26, 2024
1ef433a
feat(SDK-4214): sanitises code
CTLalit Dec 26, 2024
301c7d1
feat(SDK-4214): replaces trim with substring
CTLalit Dec 26, 2024
ef8961c
chore(SDK-4214): code readability.
CTLalit Dec 26, 2024
b967d51
feat(SDK-4214): Simplifies encryption logic
CTLalit Dec 27, 2024
e6ebd3f
task(SDK-4214) - Adds logic for encryption migration based on new app…
Anush-Shand Dec 30, 2024
931265c
task(SDK-4214) - Fixes migrationStep
Anush-Shand Dec 30, 2024
16ff5dc
task(SDK-4214) - Fixes migrationStep for InApp data
Anush-Shand Dec 30, 2024
2e7235d
task(SDK-4214) - Cleanup and readability
Anush-Shand Dec 30, 2024
ccf969e
task(SDK-4214) - Cleanup and formatting
Anush-Shand Dec 30, 2024
4927e76
task(SDK-4214) - Improves readability
Anush-Shand Dec 30, 2024
3478caf
task(SDK-4214) - More syntactic sugar
Anush-Shand Dec 30, 2024
51b5331
task(SDK-4214) - Makes prefix and postfix more complex to make it les…
Anush-Shand Dec 30, 2024
e716536
task(SDK-4214) - Adds logic for saving data if AES -> plaintext passe…
Anush-Shand Dec 30, 2024
be361df
task(SDK-4214) - Addresses comments
Anush-Shand Dec 30, 2024
2573cd8
task(SDK-4214) - Adds support for API 21 and 22
Anush-Shand Dec 30, 2024
e16ea55
Merge remote-tracking branch 'origin/feat/SDK-4214/trunk' into feat/S…
Anush-Shand Dec 31, 2024
e862c45
feat(SDK-4214): Fixes compilation error
CTLalit Dec 31, 2024
b7b0a78
Merge remote-tracking branch 'origin/feat/SDK-4214/trunk' into feat/S…
Anush-Shand Dec 31, 2024
6cc98c7
task(SDK-4214) - Migrates to new format for cgk
Anush-Shand Jan 1, 2025
ecfbea0
task(SDK-4214) - Injects cryptHandler as necessary
Anush-Shand Jan 2, 2025
4bf92db
task(SDK-4214) - Prevents unnecessary calls to store cachedGUIDSKey i…
Anush-Shand Jan 2, 2025
b41446e
task(SDK-4214) - Prevents passing null to .decrypt()
Anush-Shand Jan 2, 2025
be91902
task(SDK-4214) - Adds algo to .decrypt()
Anush-Shand Jan 2, 2025
b3deee2
task(SDK-4214) - Adds algo to .decrypt()
Anush-Shand Jan 2, 2025
7477c44
task(SDK-4214) - Adds algo to .encrypt()
Anush-Shand Jan 2, 2025
9ba9df9
task(SDK-4214) - Reduces number of calls to .decrypt
Anush-Shand Jan 2, 2025
543b345
feat/SDK-4215/Refactoring to make Crypt feature testable
CTLalit Jan 3, 2025
3009ed6
task(SDK-4214) - Removes logic from enum class and adds to a function
Anush-Shand Jan 3, 2025
eaf271a
task(SDK-4214) - Adds todo
Anush-Shand Jan 3, 2025
9a5998f
task(SDK-4214) - Refactors LoginInfoProvider round1
Anush-Shand Jan 3, 2025
e6e870e
task(SDK-4214) - Adds a common check for isTextEncrypted()
Anush-Shand Jan 3, 2025
7a5c859
task(SDK-4214) - Adds comments
Anush-Shand Jan 3, 2025
708aa82
task(SDK-4214) - Makes migrationresult internal
Anush-Shand Jan 3, 2025
ddfff07
task(SDK-4214) - Improves comments
Anush-Shand Jan 3, 2025
ffe0de7
task(SDK-4214) - Improves comments
Anush-Shand Jan 3, 2025
ea5c228
task(SDK-4214) - Fixes migration logic, firstUpgrade should always run
Anush-Shand Jan 3, 2025
ceb5fcc
task(SDK-4214) - Adds a log to indicate no migration needed
Anush-Shand Jan 3, 2025
a30ca08
feat(SDK-4214): bubbles up crypt factory
CTLalit Jan 3, 2025
33ac099
feat(SDK-4214): makes classes internal
CTLalit Jan 3, 2025
57e0897
test(SDK-4215): fixes compilation errors in tests
CTLalit Jan 3, 2025
6ce7d10
test(SDK-4215): fixes login controller test
CTLalit Jan 3, 2025
75abc45
Merge branch 'develop' into feat/SDK-4214/trunk
Anush-Shand Jan 3, 2025
95b5b0f
task(SDK-4214) - Fixes an issue where migration was shortcircuited be…
Anush-Shand Jan 3, 2025
8855892
test(SDK-4215): fixes crypthandler test
CTLalit Jan 5, 2025
8c78871
task(SDK-4214) - Fixes an issue where migration from AES failed but o…
Anush-Shand Jan 5, 2025
9e1b7d4
Merge remote-tracking branch 'origin/feat/SDK-4214/trunk' into feat/S…
Anush-Shand Jan 5, 2025
e7653f9
feat(SDK-4215): moves pref code to repo layer
CTLalit Jan 6, 2025
3ebe726
feat(SDK-4215): moves repo out of main thread + cleanup
CTLalit Jan 6, 2025
227caa2
feat(SDK-4215): removes try catch from migration steps
CTLalit Jan 6, 2025
8b90180
feat(SDK-4215): removes try catch from migration steps 2
CTLalit Jan 6, 2025
cebf9fc
chore(SDK-4215): adds space
CTLalit Jan 6, 2025
2125890
task(SDK-4214) - Fixes an issue where IV was 16 bytes instead of 12 f…
Anush-Shand Jan 6, 2025
21f295b
task(SDK-4214) - Deletes the data if it can't be decrypted from AES
Anush-Shand Jan 6, 2025
59d7f61
test(SDK-4215): fixes test clevertapapitest
CTLalit Jan 6, 2025
7b0c461
task(SDK-4214) - Fixes logs
Anush-Shand Jan 6, 2025
b899f6f
tests(SDK-4214) - Fixes LoginInfoProviderTest
Anush-Shand Jan 6, 2025
502b3f9
tests(SDK-4214) - Fixes LoginInfoProviderTest and ignores other
Anush-Shand Jan 6, 2025
3d45ab4
task(SDK-4214) - Fixes log
Anush-Shand Jan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;

import com.clevertap.android.sdk.Constants.IdentityType;
import com.clevertap.android.sdk.cryption.CryptHandler;
import com.clevertap.android.sdk.cryption.EncryptionLevel;
import com.clevertap.android.sdk.login.LoginConstants;

import org.json.JSONObject;
Expand Down Expand Up @@ -491,7 +490,7 @@ boolean isUseGoogleAdId() {
void setCreatedPostAppLaunch() {
this.createdPostAppLaunch = true;
}
public void setEncryptionLevel(CryptHandler.EncryptionLevel encryptionLevel) {
public void setEncryptionLevel(EncryptionLevel encryptionLevel) {
this.encryptionLevel = encryptionLevel.intValue();
}
public int getEncryptionLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public interface Constants {
String KEY_I = "comms_i";
String KEY_J = "comms_j";
String CACHED_GUIDS_KEY = "cachedGUIDsKey";
String CACHED_GUIDS_LENGTH_KEY = "cachedGUIDsLengthKey";
String CACHED_VARIABLES_KEY = "variablesKey";
String MULTI_USER_PREFIX = "mt_";
String NOTIFICATION_TAG = "wzrk_pn";
Expand Down Expand Up @@ -325,6 +326,12 @@ public interface Constants {
String CRYPTION_SALT = "W1ZRCl3>";
String CRYPTION_IV = "__CL3>3Rt#P__1V_";

String AES_PREFIX = "[";
String AES_SUFFIX = "]";

String AES_GCM_PREFIX = "<ct<";
String AES_GCM_SUFFIX = ">ct>";

int FETCH_TYPE_PC = 0;
int FETCH_TYPE_FF = 1;
int FETCH_TYPE_VARIABLES = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.WorkerThread;

import com.clevertap.android.sdk.login.LoginInfoProvider;
import com.clevertap.android.sdk.task.CTExecutorFactory;
import com.clevertap.android.sdk.task.OnSuccessListener;
Expand Down Expand Up @@ -119,8 +120,7 @@ private class DeviceCachedInfo {

private String getBluetoothVersion() {
String bluetoothVersion = "none";
if (android.os.Build.VERSION.SDK_INT >= 18 &&
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
bluetoothVersion = "ble";
} else if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
bluetoothVersion = "classic";
Expand Down Expand Up @@ -548,7 +548,7 @@ public JSONObject getAppLaunchedFields() {
try {
boolean deviceIsMultiUser = false;
if (getGoogleAdID() != null) {
deviceIsMultiUser = new LoginInfoProvider(context, config, this).deviceIsMultiUser();
deviceIsMultiUser = new LoginInfoProvider(context, config).deviceIsMultiUser();
}
return CTJsonConverter.from(this, mCoreMetaData, enableNetworkInfoReporting,
deviceIsMultiUser);
Expand Down Expand Up @@ -844,9 +844,9 @@ private synchronized void fetchGoogleAdID() {
} catch (Throwable t) {
if (t.getCause() != null) {
getConfigLogger().verbose(config.getAccountId(),
"Failed to get Advertising ID: " + t.toString() + t.getCause().toString());
CTLalit marked this conversation as resolved.
Show resolved Hide resolved
"Failed to get Advertising ID: " + t + t.getCause().toString());
} else {
getConfigLogger().verbose(config.getAccountId(), "Failed to get Advertising ID: " + t.toString());
getConfigLogger().verbose(config.getAccountId(), "Failed to get Advertising ID: " + t);
}
}
if (advertisingID != null && advertisingID.trim().length() > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import androidx.annotation.WorkerThread;

import com.clevertap.android.sdk.cryption.CryptHandler;
import com.clevertap.android.sdk.cryption.CryptUtils;
import com.clevertap.android.sdk.db.BaseDatabaseManager;
import com.clevertap.android.sdk.db.DBAdapter;
import com.clevertap.android.sdk.events.EventDetail;
Expand Down Expand Up @@ -587,7 +586,7 @@ public void run() {
JSONObject jsonObjectEncrypted = new JSONObject(profile);

if (!passFlag)
CryptUtils.updateEncryptionFlagOnFailure(context, config, Constants.ENCRYPTION_FLAG_DB_SUCCESS, cryptHandler);
cryptHandler.updateMigrationFailureCount(false);

DBAdapter dbAdapter = baseDatabaseManager.loadDBAdapter(context);
long status = dbAdapter.storeUserProfile(profileID, deviceInfo.getDeviceID(), jsonObjectEncrypted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public final class Logger implements ILogger {

private int debugLevel;

Logger(int level) {
this.debugLevel = level;
}

/**
* Logs to Debug if the debug level is greater than 1.
*/
Expand Down Expand Up @@ -90,10 +94,6 @@ public static void v(String message, Throwable t) {
}
}

Logger(int level) {
this.debugLevel = level;
}

@Override
public void debug(String message) {
if (getStaticDebugLevel() > CleverTapAPI.LogLevel.INFO.intValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,20 @@ public static void removeImmediate(Context context, String key) {
}

//Preferences

@Deprecated
/*
Use the method storageKeyWithSuffix(String accountID, String key) instead.")
*/
public static String storageKeyWithSuffix(@NonNull CleverTapInstanceConfig config,@NonNull String key) {
return key + ":" + config.getAccountId();
}

public static String storageKeyWithSuffix(String accountID, @NonNull String key) {
Anush-Shand marked this conversation as resolved.
Show resolved Hide resolved
// todo - use this throughout the sdk instead of the one-above
return key + ":" + accountID;
}

@SuppressWarnings("SameParameterValue")
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
return getPreferences(context).getBoolean(key, defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,25 @@ import javax.crypto.spec.SecretKeySpec
/**
* This class implements the AES Cryption algorithm
*/
class AESCrypt : Crypt() {
class AESCrypt(accountID: String) : Crypt() {
/**
* This method returns the key-password to be used for encryption/decryption
*
* @param accountID : accountId of the current instance
* @return key-password
*/
private fun generateKeyPassword(accountID: String): String {
return APP_ID_KEY_PREFIX + accountID + APP_ID_KEY_SUFFIX
}
private val keyPassword: String = APP_ID_KEY_PREFIX + accountID + APP_ID_KEY_SUFFIX

/**
* This method is used internally to encrypt the plain text
*
* @param plainText - plainText to be encrypted
* @param accountID - accountID used for password generation
* @return encrypted text
*/
override fun encryptInternal(plainText: String, accountID: String): String? {

override fun encryptInternal(plainText: String): String? {
return performCryptOperation(
Cipher.ENCRYPT_MODE, generateKeyPassword(accountID), plainText.toByteArray(
StandardCharsets.UTF_8
)
mode = Cipher.ENCRYPT_MODE,
password = keyPassword,
text = plainText.toByteArray(StandardCharsets.UTF_8)
)?.let { encryptedBytes ->
encryptedBytes.contentToString()
}
Expand All @@ -48,12 +43,15 @@ class AESCrypt : Crypt() {
* This method is used internally to decrypt the cipher text
*
* @param cipherText - cipherText to be decrypted
* @param accountID - accountID used for password generation
* @return decrypted text
*/
override fun decryptInternal(cipherText: String, accountID: String): String? {
override fun decryptInternal(cipherText: String): String? {
return parseCipherText(cipherText)?.let { bytes ->
performCryptOperation(Cipher.DECRYPT_MODE, generateKeyPassword(accountID), bytes)
performCryptOperation(
mode = Cipher.DECRYPT_MODE,
password = keyPassword,
text = bytes
)
}?.let { decryptedBytes ->
String(decryptedBytes, StandardCharsets.UTF_8)
}
Expand All @@ -65,7 +63,7 @@ class AESCrypt : Crypt() {
* @param cipherText - cipher text to be parsed
* @return Parsed string in the form of a byte array
*/
override fun parseCipherText(cipherText: String): ByteArray? {
private fun parseCipherText(cipherText: String): ByteArray? {
return try {
// Removes the enclosing brackets, trims any leading or trailing whitespace, and then splits the resulting string based on commas
val byteStrings =
Expand Down
Loading