diff --git a/navi-pay/build.gradle b/navi-pay/build.gradle index d6c8d1c02a..8d3c17ee92 100644 --- a/navi-pay/build.gradle +++ b/navi-pay/build.gradle @@ -128,4 +128,6 @@ dependencies { implementation "com.google.accompanist:accompanist-pager:$accompanist_pager_version" implementation "com.google.accompanist:accompanist-pager-indicators:$accompanist_pager_version" + + implementation playCore.implementation } diff --git a/navi-pay/src/main/kotlin/com/navi/pay/db/SqlCipherOpenerFactory.kt b/navi-pay/src/main/kotlin/com/navi/pay/db/SqlCipherOpenerFactory.kt new file mode 100644 index 0000000000..58cce42edd --- /dev/null +++ b/navi-pay/src/main/kotlin/com/navi/pay/db/SqlCipherOpenerFactory.kt @@ -0,0 +1,138 @@ +package com.navi.pay.db + +import android.content.Context +import android.database.sqlite.SQLiteException +import androidx.sqlite.db.SupportSQLiteDatabase +import androidx.sqlite.db.SupportSQLiteOpenHelper +import com.google.android.play.core.splitinstall.SplitInstallHelper +import net.sqlcipher.database.SQLiteDatabase +import net.sqlcipher.database.SQLiteDatabaseHook +import net.sqlcipher.database.SQLiteOpenHelper +import java.io.File + +/** + * A factory to create [SupportSQLiteOpenHelper] instances that uses SQLCipher to encrypt the database. + * It is created to load the SQLCipher native libraries before opening the database using SplitInstallHelper + * Keep this updated with new SupportSQLiteOpenHelper in-case of Cipher Lin Update + * @param passphrase The passphrase to use to encrypt the database. + * @param hook The hook to use to configure the database. + * @param clearPassphrase Whether to clear the passphrase after opening the database. + */ +class SqlCipherOpenerFactory @JvmOverloads constructor( + private val passphrase: ByteArray, + private val hook: SQLiteDatabaseHook? = null, + private val clearPassphrase: Boolean = true +) : SupportSQLiteOpenHelper.Factory { + override fun create(configuration: SupportSQLiteOpenHelper.Configuration): SupportSQLiteOpenHelper { + return SqlCipherOpenerHelper(configuration, passphrase, hook, clearPassphrase) + } +} + + +class SqlCipherOpenerHelper internal constructor( + configuration: SupportSQLiteOpenHelper.Configuration, + passphrase: ByteArray?, + hook: SQLiteDatabaseHook?, + clearPassphrase: Boolean +) : SupportSQLiteOpenHelper { + private val standardHelper: SQLiteOpenHelper + private val passphrase: ByteArray? + private val clearPassphrase: Boolean + + init { + loadLibs(configuration.context, configuration.context.filesDir) + this.passphrase = passphrase + this.clearPassphrase = clearPassphrase + standardHelper = object : SQLiteOpenHelper( + configuration.context, + configuration.name, + null as SQLiteDatabase.CursorFactory?, + configuration.callback.version, + hook + ) { + override fun onCreate(db: SQLiteDatabase) { + configuration.callback.onCreate(db) + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + configuration.callback.onUpgrade(db, oldVersion, newVersion) + } + + override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + configuration.callback.onDowngrade(db, oldVersion, newVersion) + } + + override fun onOpen(db: SQLiteDatabase) { + configuration.callback.onOpen(db) + } + + override fun onConfigure(db: SQLiteDatabase) { + configuration.callback.onConfigure(db) + } + } + } + + override val databaseName: String + get() = standardHelper.databaseName + override val readableDatabase: SupportSQLiteDatabase + get() = writableDatabase + + override fun setWriteAheadLoggingEnabled(enabled: Boolean) { + standardHelper.setWriteAheadLoggingEnabled(enabled) + } + + override val writableDatabase: SupportSQLiteDatabase + get() { + val result: SQLiteDatabase + try { + result = standardHelper.getWritableDatabase(passphrase) + } catch (var8: SQLiteException) { + if (passphrase != null) { + var isCleared = true + val var4 = passphrase + val var5 = var4.size + var var6 = 0 + while (var6 < var5) { + val b = var4[var6] + isCleared = isCleared && b.toInt() == 0 + ++var6 + } + if (isCleared) { + throw IllegalStateException( + "The passphrase appears to be cleared. This happens by default the first time you use the factory to open a database, so we can remove the cleartext passphrase from memory. If you close the database yourself, please use a fresh SupportFactory to reopen it. If something else (e.g., Room) closed the database, and you cannot control that, use SupportFactory boolean constructor option to opt out of the automatic password clearing step. See the project README for more information.", + var8 + ) + } + } + throw var8 + } + if (clearPassphrase && passphrase != null) { + for (i in passphrase.indices) { + passphrase[i] = 0 + } + } + return result + } + + override fun close() { + standardHelper.close() + } +} + +/** + * Loads the SQLCipher native libraries using SplitInstallHelper + * @param context The context to use to load the libraries. + * @param workingDir The working directory to use to load the libraries. + */ +@Synchronized +fun loadLibs(context: Context, workingDir: File?) { + SQLiteDatabase.loadLibs( + context, workingDir + ) { libNames -> + val var3 = libNames.size + for (var4 in 0 until var3) { + val libName = libNames[var4] + SplitInstallHelper.loadLibrary(context, libName) + } + } +} \ No newline at end of file diff --git a/navi-pay/src/main/kotlin/com/navi/pay/network/di/NaviPayModule.kt b/navi-pay/src/main/kotlin/com/navi/pay/network/di/NaviPayModule.kt index 9eec9c2969..ee0e6d5df4 100644 --- a/navi-pay/src/main/kotlin/com/navi/pay/network/di/NaviPayModule.kt +++ b/navi-pay/src/main/kotlin/com/navi/pay/network/di/NaviPayModule.kt @@ -11,8 +11,10 @@ import android.content.Context import androidx.room.Room import com.google.gson.Gson import com.navi.base.sharedpref.PreferenceManager +import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper import com.navi.common.model.ModuleName import com.navi.common.model.NetworkInfo +import com.navi.common.utils.log import com.navi.pay.common.cache.ImageCache import com.navi.pay.common.cache.ImageCacheImpl import com.navi.pay.common.cache.NaviPayCache @@ -27,6 +29,7 @@ import com.navi.pay.common.utils.NaviPayCommonUtils import com.navi.pay.common.utils.naviPayGsonBuilder import com.navi.pay.db.NaviPayAppDatabase import com.navi.pay.db.NaviPayAppEncryptedDatabase +import com.navi.pay.db.SqlCipherOpenerFactory import com.navi.pay.management.paytocontacts.PhoneContactManager import com.navi.pay.management.paytocontacts.PhoneContactManagerImpl import com.navi.pay.network.NaviPayHttpClient @@ -37,6 +40,7 @@ import com.navi.pay.onboarding.home.VpaQRCodeManager import com.navi.pay.onboarding.home.VpaQRCodeManagerImpl import com.navi.pay.permission.utils.PermissionStateProvider import com.navi.pay.permission.utils.PermissionStateProviderImpl +import com.navi.pay.utils.ENABLE_DEFAULT_SUPPORT_FACTORY import com.navi.pay.utils.KEY_DB_ENCRYPTION import com.navi.pay.utils.NAVIPAY_NETWORK_INFO_TIMEOUT import com.navi.pay.utils.NAVI_PAY_DATABASE_NAME @@ -127,13 +131,22 @@ object NaviPayNetworkModule { value = passphrase.toString(Charsets.ISO_8859_1) ) } - + val sqlCipherOpenerFactory = try { + if (FirebaseRemoteConfigHelper.getBoolean(ENABLE_DEFAULT_SUPPORT_FACTORY)) { + SupportFactory(passphrase) + } else { + SqlCipherOpenerFactory(passphrase) + } + } catch (e: Exception) { + e.log() + SqlCipherOpenerFactory(passphrase) + } return Room.databaseBuilder( context, NaviPayAppEncryptedDatabase::class.java, NAVI_PAY_ENCRYPTED_DATABASE_NAME ) - .openHelperFactory(SupportFactory(passphrase)) + .openHelperFactory(sqlCipherOpenerFactory) .build() } diff --git a/navi-pay/src/main/kotlin/com/navi/pay/utils/NaviPayConstants.kt b/navi-pay/src/main/kotlin/com/navi/pay/utils/NaviPayConstants.kt index 4f5543e37e..d1daa5a7dc 100644 --- a/navi-pay/src/main/kotlin/com/navi/pay/utils/NaviPayConstants.kt +++ b/navi-pay/src/main/kotlin/com/navi/pay/utils/NaviPayConstants.kt @@ -211,4 +211,7 @@ const val KEY_CHECK_BALANCE_ACTION = "checkBalanceAction" const val CUSTOMER_STATUS_AFTER_ONBOARDING = "customerStatusAfterOnboarding" const val VPA_QR_CODE_IMAGES = "vpa_qr_code_images" const val QR_CODE_IMAGE_EXTENSION = ".jpeg" -const val NAVI_PAY_DEBUG_LOG = "NAVI_PAY_DEBUG_LOG" \ No newline at end of file +const val NAVI_PAY_DEBUG_LOG = "NAVI_PAY_DEBUG_LOG" + +// Firebase Config Keys +const val ENABLE_DEFAULT_SUPPORT_FACTORY = "ENABLE_DEFAULT_SUPPORT_FACTORY" \ No newline at end of file