From ff65d4e342151473291a8fbcfdd8332d03257114 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 19 Sep 2019 12:52:37 +1000 Subject: [PATCH] Added Database. --- .../database/helpers/SQLCipherOpenHelper.java | 13 ++-- .../securesms/loki/LokiMultiDeviceDatabase.kt | 77 +++++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 5fedf75403..cb1d0bbb2f 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -35,12 +35,7 @@ import org.thoughtcrime.securesms.database.StickerDatabase; import org.thoughtcrime.securesms.database.ThreadDatabase; import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob; import org.thoughtcrime.securesms.logging.Log; -import org.thoughtcrime.securesms.loki.LokiAPIDatabase; -import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase; -import org.thoughtcrime.securesms.loki.LokiMessageDatabase; -import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; -import org.thoughtcrime.securesms.loki.LokiThreadDatabase; -import org.thoughtcrime.securesms.loki.LokiUserDatabase; +import org.thoughtcrime.securesms.loki.*; import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.util.TextSecurePreferences; @@ -74,6 +69,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { private static final int STICKERS = 21; private static final int lokiV1 = 22; private static final int lokiV2 = 23; + private static final int lokiV3 = 24; private static final int DATABASE_VERSION = lokiV2; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes private static final String DATABASE_NAME = "signal.db"; @@ -136,6 +132,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiThreadDatabase.getCreateSessionResetTableCommand()); db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand()); db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand()); + db.execSQL(LokiMultiDeviceDatabase.getCreateTableCommand()); executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS); @@ -498,6 +495,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand()); } + if (oldVersion < lokiV3) { + db.execSQL(LokiMultiDeviceDatabase.getCreateTableCommand()); + } + db.setTransactionSuccessful(); } finally { db.endTransaction(); diff --git a/src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt new file mode 100644 index 0000000000..081a3f247a --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/LokiMultiDeviceDatabase.kt @@ -0,0 +1,77 @@ +package org.thoughtcrime.securesms.loki + +import android.content.ContentValues +import android.content.Context +import net.sqlcipher.Cursor +import org.thoughtcrime.securesms.database.Database +import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper +import org.thoughtcrime.securesms.util.Base64 +import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation +import java.util.* + +class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { + + companion object { + // Authorisation + private val authorisation_table = "loki_multi_device_authorisation" + private val primaryDevice = "primary_device" + private val secondaryDevice = "secondary_device" + private val requestSignature = "request_signature" + private val grantSignature = "grant_signature" + @JvmStatic + val createTableCommand = "CREATE TABLE $authorisation_table(_id INTEGER PRIMARY KEY AUTOINCREMENT," + + "$primaryDevice TEXT," + + "$secondaryDevice TEXT," + + "$requestSignature TEXT NULLABLE" + + "$grantSignature TEXT NULLABLE" + + ");" + } + + fun insertOrUpdatePairingAuthorisation(authorisation: LokiPairingAuthorisation) { + val database = databaseHelper.writableDatabase + val values = ContentValues() + values.put(primaryDevice, authorisation.primaryDevicePubKey) + values.put(secondaryDevice, authorisation.secondaryDevicePubKey) + if (authorisation.requestSignature != null) { values.put(requestSignature, Base64.encodeBytes(authorisation.requestSignature)) } + if (authorisation.grantSignature != null) { values.put(grantSignature, Base64.encodeBytes(authorisation.grantSignature)) } + database.insertOrUpdate(authorisation_table, values, "$primaryDevice = ? AND $secondaryDevice = ?", arrayOf(authorisation.primaryDevicePubKey, authorisation.secondaryDevicePubKey)) + } + + fun getAuthorisationForSecondaryDevice(pubKey: String): LokiPairingAuthorisation? { + val database = databaseHelper.readableDatabase + return database.get(authorisation_table, "$secondaryDevice = ?", arrayOf(pubKey)) { cursor -> + val primaryDevicePubKey = cursor.getString(primaryDevice) + val secondaryDevicePubKey = cursor.getString(secondaryDevice) + val requestSignature: ByteArray? = if (cursor.isNull(cursor.getColumnIndexOrThrow(requestSignature))) null else cursor.getBase64EncodedData(requestSignature) + val grantSignature: ByteArray? = if (cursor.isNull(cursor.getColumnIndexOrThrow(grantSignature))) null else cursor.getBase64EncodedData(grantSignature) + LokiPairingAuthorisation(primaryDevicePubKey, secondaryDevicePubKey, requestSignature, grantSignature) + } + } + + fun getSecondaryDevices(primaryDevicePubKey: String): List { + val database = databaseHelper.readableDatabase + + var cursor: Cursor? = null + val results = LinkedList() + + try { + cursor = database.query(authorisation_table, arrayOf(secondaryDevice), "$primaryDevice = ?", arrayOf(primaryDevicePubKey), null, null, null) + if (cursor != null && cursor.moveToNext()) { + results.add(cursor.getString(secondaryDevice)) + } + } catch (e: Exception) { + // Do nothing + } finally { + cursor?.close() + } + + return results + } + + fun getPrimaryDevice(secondaryDevicePubKey: String): String? { + val database = databaseHelper.readableDatabase + return database.get(authorisation_table, "$secondaryDevice = ?", arrayOf(secondaryDevicePubKey)) { cursor -> + cursor.getString(primaryDevice) + } + } +} \ No newline at end of file