session-android/src/org/thoughtcrime/securesms/loki/database/LokiPreKeyBundleDatabase.kt

130 lines
6.8 KiB
Kotlin
Raw Normal View History

2020-05-11 16:19:26 +10:00
package org.thoughtcrime.securesms.loki.database
2019-06-04 12:12:40 +10:00
import android.content.ContentValues
import android.content.Context
2019-10-24 12:17:58 +11:00
import net.sqlcipher.Cursor
2019-06-04 12:12:40 +10:00
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.crypto.PreKeyUtil
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
2019-06-26 14:31:38 +10:00
import org.thoughtcrime.securesms.logging.Log
2020-05-11 16:19:26 +10:00
import org.thoughtcrime.securesms.loki.utilities.get
import org.thoughtcrime.securesms.loki.utilities.getBase64EncodedData
import org.thoughtcrime.securesms.loki.utilities.getInt
import org.thoughtcrime.securesms.loki.utilities.insertOrUpdate
2019-06-04 12:12:40 +10:00
import org.thoughtcrime.securesms.util.Base64
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.IdentityKey
2020-02-18 12:28:49 +11:00
import org.whispersystems.libsignal.InvalidKeyException
2019-06-04 12:12:40 +10:00
import org.whispersystems.libsignal.ecc.Curve
import org.whispersystems.libsignal.state.PreKeyBundle
2019-06-26 14:31:38 +10:00
import org.whispersystems.libsignal.util.KeyHelper
import org.whispersystems.signalservice.api.push.SignalServiceAddress
2020-05-07 17:59:41 +10:00
import org.whispersystems.signalservice.loki.database.LokiPreKeyBundleDatabaseProtocol
2019-06-04 12:12:40 +10:00
2019-06-24 14:57:53 +10:00
class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiPreKeyBundleDatabaseProtocol {
2019-06-04 12:12:40 +10:00
companion object {
2020-07-15 12:24:43 +10:00
private val table = "loki_pre_key_bundle_database"
private val publicKey = "public_key"
2019-06-21 10:39:05 +10:00
private val preKeyID = "pre_key_id"
2019-06-04 12:12:40 +10:00
private val preKeyPublic = "pre_key_public"
2019-06-21 10:39:05 +10:00
private val signedPreKeyID = "signed_pre_key_id"
2019-06-04 12:12:40 +10:00
private val signedPreKeyPublic = "signed_pre_key_public"
private val signedPreKeySignature = "signed_pre_key_signature"
private val identityKey = "identity_key"
2019-06-21 10:39:05 +10:00
private val deviceID = "device_id"
private val registrationID = "registration_id"
2020-07-15 12:24:43 +10:00
@JvmStatic val createTableCommand = "CREATE TABLE $table (" + "$publicKey TEXT PRIMARY KEY," + "$preKeyID INTEGER," +
2019-06-21 10:39:05 +10:00
"$preKeyPublic TEXT NOT NULL," + "$signedPreKeyID INTEGER," + "$signedPreKeyPublic TEXT NOT NULL," +
"$signedPreKeySignature TEXT," + "$identityKey TEXT NOT NULL," + "$deviceID INTEGER," + "$registrationID INTEGER" + ");"
2019-06-04 12:12:40 +10:00
}
2020-07-15 12:24:43 +10:00
fun generatePreKeyBundle(publicKey: String): PreKeyBundle? {
2020-02-18 12:28:49 +11:00
var failureCount = 0
while (failureCount < 3) {
try {
2020-07-15 12:24:43 +10:00
val preKey = generatePreKeyBundle(publicKey, failureCount > 0) ?: return null
2020-02-18 12:28:49 +11:00
// Verify the bundle is correct
if (!Curve.verifySignature(preKey.identityKey.publicKey, preKey.signedPreKey.serialize(), preKey.signedPreKeySignature)) {
throw InvalidKeyException()
}
return preKey;
} catch (e: InvalidKeyException) {
failureCount += 1
}
}
2020-07-15 12:24:43 +10:00
Log.w("Loki", "Failed to generate a valid pre key bundle for: $publicKey.")
2020-02-18 12:28:49 +11:00
return null
}
2020-07-15 12:24:43 +10:00
private fun generatePreKeyBundle(publicKey: String, forceClean: Boolean): PreKeyBundle? {
if (publicKey.isEmpty()) return null
2019-06-26 14:31:38 +10:00
var registrationID = TextSecurePreferences.getLocalRegistrationId(context)
if (registrationID == 0) {
registrationID = KeyHelper.generateRegistrationId(false)
TextSecurePreferences.setLocalRegistrationId(context, registrationID)
}
2019-06-21 10:39:05 +10:00
val deviceID = SignalServiceAddress.DEFAULT_DEVICE_ID
2020-07-15 12:24:43 +10:00
val preKeyRecord = DatabaseFactory.getLokiPreKeyRecordDatabase(context).getOrCreatePreKeyRecord(publicKey)
2019-06-26 14:31:38 +10:00
val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context)
2020-02-18 12:28:49 +11:00
if (!forceClean && TextSecurePreferences.isSignedPreKeyRegistered(context)) {
2019-06-26 14:31:38 +10:00
Log.d("Loki", "A signed pre key has already been registered.")
} else {
Log.d("Loki", "Registering a new signed pre key.")
PreKeyUtil.generateSignedPreKey(context, identityKeyPair, true)
TextSecurePreferences.setSignedPreKeyRegistered(context, true)
}
val activeSignedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null
return PreKeyBundle(registrationID, deviceID, preKeyRecord.id, preKeyRecord.keyPair.publicKey, activeSignedPreKey.id, activeSignedPreKey.keyPair.publicKey, activeSignedPreKey.signature, identityKeyPair.publicKey)
2019-06-04 12:12:40 +10:00
}
2020-07-15 12:24:43 +10:00
override fun getPreKeyBundle(publicKey: String): PreKeyBundle? {
2019-06-04 12:12:40 +10:00
val database = databaseHelper.readableDatabase
2020-07-15 12:24:43 +10:00
return database.get(table, "${Companion.publicKey} = ?", arrayOf( publicKey )) { cursor ->
2019-06-21 10:39:05 +10:00
val registrationID = cursor.getInt(registrationID)
val deviceID = cursor.getInt(deviceID)
val preKeyID = cursor.getInt(preKeyID)
val preKey = Curve.decodePoint(cursor.getBase64EncodedData(preKeyPublic), 0)
2019-06-21 10:39:05 +10:00
val signedPreKeyID = cursor.getInt(signedPreKeyID)
val signedPreKey = Curve.decodePoint(cursor.getBase64EncodedData(signedPreKeyPublic), 0)
val signedPreKeySignature = cursor.getBase64EncodedData(signedPreKeySignature)
val identityKey = IdentityKey(cursor.getBase64EncodedData(identityKey), 0)
2019-06-21 10:39:05 +10:00
PreKeyBundle(registrationID, deviceID, preKeyID, preKey, signedPreKeyID, signedPreKey, signedPreKeySignature, identityKey)
2019-06-04 12:12:40 +10:00
}
}
2020-07-15 12:24:43 +10:00
fun setPreKeyBundle(publicKey: String, preKeyBundle: PreKeyBundle) {
2019-06-04 12:12:40 +10:00
val database = databaseHelper.writableDatabase
2019-07-23 10:35:03 +10:00
val values = ContentValues(9)
values.put(registrationID, preKeyBundle.registrationId)
values.put(deviceID, preKeyBundle.deviceId)
values.put(preKeyID, preKeyBundle.preKeyId)
values.put(preKeyPublic, Base64.encodeBytes(preKeyBundle.preKey.serialize()))
values.put(signedPreKeyID, preKeyBundle.signedPreKeyId)
values.put(signedPreKeyPublic, Base64.encodeBytes(preKeyBundle.signedPreKey.serialize()))
values.put(signedPreKeySignature, Base64.encodeBytes(preKeyBundle.signedPreKeySignature))
values.put(identityKey, Base64.encodeBytes(preKeyBundle.identityKey.serialize()))
2020-07-15 12:24:43 +10:00
values.put(Companion.publicKey, publicKey)
database.insertOrUpdate(table, values, "${Companion.publicKey} = ?", arrayOf( publicKey ))
2019-06-04 12:12:40 +10:00
}
2020-07-15 12:24:43 +10:00
override fun removePreKeyBundle(publicKey: String) {
2019-06-04 12:12:40 +10:00
val database = databaseHelper.writableDatabase
2020-07-15 12:24:43 +10:00
database.delete(table, "${Companion.publicKey} = ?", arrayOf( publicKey ))
2019-06-04 12:12:40 +10:00
}
2020-07-15 12:24:43 +10:00
fun hasPreKeyBundle(publicKey: String): Boolean {
val database = databaseHelper.readableDatabase
2019-10-24 12:17:58 +11:00
var cursor: Cursor? = null
return try {
2020-07-15 12:24:43 +10:00
cursor = database.query(table, null, "${Companion.publicKey} = ?", arrayOf( publicKey ), null, null, null)
2019-10-24 12:17:58 +11:00
cursor != null && cursor.count > 0
} catch (e: Exception) {
false
} finally {
cursor?.close()
}
}
2019-06-04 12:12:40 +10:00
}