2019-06-04 12:12:40 +10:00
|
|
|
package org.thoughtcrime.securesms.loki
|
|
|
|
|
|
|
|
import android.content.ContentValues
|
|
|
|
import android.content.Context
|
|
|
|
import net.sqlcipher.database.SQLiteDatabase
|
|
|
|
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
|
|
|
|
import org.thoughtcrime.securesms.util.Base64
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
import org.whispersystems.libsignal.IdentityKey
|
|
|
|
import org.whispersystems.libsignal.ecc.Curve
|
|
|
|
import org.whispersystems.libsignal.state.PreKeyBundle
|
2019-06-05 09:29:18 +10:00
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress
|
2019-06-04 12:12:40 +10:00
|
|
|
|
|
|
|
class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
|
2019-06-04 14:16:09 +10:00
|
|
|
|
2019-06-04 12:12:40 +10:00
|
|
|
companion object {
|
|
|
|
private val tableName = "loki_pre_key_bundle_database"
|
2019-06-21 10:39:05 +10:00
|
|
|
private val hexEncodedPublicKey = "public_key"
|
|
|
|
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"
|
|
|
|
@JvmStatic val createTableCommand = "CREATE TABLE $tableName (" + "$hexEncodedPublicKey TEXT PRIMARY KEY," + "$preKeyID INTEGER," +
|
|
|
|
"$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a `PreKeyBundle` for the given contact.
|
|
|
|
* This generated bundle shouldn't be stored locally since this is used to generate bundles to send to other users.
|
2019-06-04 14:16:09 +10:00
|
|
|
*
|
2019-06-21 10:39:05 +10:00
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
2019-06-04 14:16:09 +10:00
|
|
|
* @return PreKeyBundle? A pre key bundle or `null` if something went wrong.
|
2019-06-04 12:12:40 +10:00
|
|
|
*/
|
2019-06-21 10:39:05 +10:00
|
|
|
fun generatePreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? {
|
2019-06-04 12:12:40 +10:00
|
|
|
val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context)
|
2019-06-04 14:16:09 +10:00
|
|
|
val signedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null
|
2019-06-21 10:39:05 +10:00
|
|
|
val preKeyRecord = DatabaseFactory.getLokiContactPreKeyDatabase(context).getOrCreatePreKey(hexEncodedPublicKey)
|
|
|
|
val registrationID = TextSecurePreferences.getLocalRegistrationId(context)
|
|
|
|
if (registrationID == 0) return null
|
|
|
|
val deviceID = SignalServiceAddress.DEFAULT_DEVICE_ID
|
|
|
|
return PreKeyBundle(registrationID, deviceID,preKeyRecord.id, preKeyRecord.keyPair.publicKey, signedPreKey.id, signedPreKey.keyPair.publicKey, signedPreKey.signature, identityKeyPair.publicKey)
|
2019-06-04 12:12:40 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the `PreKeyBundle` associated with the given contact.
|
2019-06-04 14:16:09 +10:00
|
|
|
*
|
2019-06-21 10:39:05 +10:00
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
2019-06-04 14:16:09 +10:00
|
|
|
* @return PreKeyBundle? The pre key bundle or `null` if it doesn't exist.
|
2019-06-04 12:12:40 +10:00
|
|
|
*/
|
2019-06-21 10:39:05 +10:00
|
|
|
fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? {
|
2019-06-04 12:12:40 +10:00
|
|
|
val database = databaseHelper.readableDatabase
|
2019-06-21 10:39:05 +10:00
|
|
|
return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { cursor ->
|
|
|
|
val registrationID = cursor.getInt(registrationID)
|
|
|
|
val deviceID = cursor.getInt(deviceID)
|
|
|
|
val preKeyID = cursor.getInt(preKeyID)
|
2019-06-04 14:16:09 +10:00
|
|
|
val preKey = Curve.decodePoint(cursor.getBase64EncodedData(preKeyPublic), 0)
|
2019-06-21 10:39:05 +10:00
|
|
|
val signedPreKeyID = cursor.getInt(signedPreKeyID)
|
2019-06-04 14:16:09 +10:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-04 14:16:09 +10:00
|
|
|
* Set the `PreKeyBundle` for the given contact.
|
|
|
|
*
|
2019-06-21 10:39:05 +10:00
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
2019-06-04 14:16:09 +10:00
|
|
|
* @param preKeyBundle PreKeyBundle The pre key bundle.
|
2019-06-04 12:12:40 +10:00
|
|
|
*/
|
2019-06-21 10:39:05 +10:00
|
|
|
fun setPreKeyBundle(hexEncodedPublicKey: String, preKeyBundle: PreKeyBundle) {
|
2019-06-04 12:12:40 +10:00
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
val contentValues = ContentValues()
|
2019-06-21 10:39:05 +10:00
|
|
|
contentValues.put(registrationID, preKeyBundle.registrationId)
|
|
|
|
contentValues.put(deviceID, preKeyBundle.deviceId)
|
|
|
|
contentValues.put(preKeyID, preKeyBundle.preKeyId)
|
2019-06-04 12:12:40 +10:00
|
|
|
contentValues.put(preKeyPublic, Base64.encodeBytes(preKeyBundle.preKey.serialize()))
|
2019-06-21 10:39:05 +10:00
|
|
|
contentValues.put(signedPreKeyID, preKeyBundle.signedPreKeyId)
|
2019-06-04 12:12:40 +10:00
|
|
|
contentValues.put(signedPreKeyPublic, Base64.encodeBytes(preKeyBundle.signedPreKey.serialize()))
|
|
|
|
contentValues.put(signedPreKeySignature, Base64.encodeBytes(preKeyBundle.signedPreKeySignature))
|
|
|
|
contentValues.put(identityKey, Base64.encodeBytes(preKeyBundle.identityKey.serialize()))
|
2019-06-21 10:39:05 +10:00
|
|
|
contentValues.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
|
2019-06-04 12:12:40 +10:00
|
|
|
database.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the `PreKeyBundle` for the given contact.
|
2019-06-04 14:39:28 +10:00
|
|
|
*
|
2019-06-21 10:39:05 +10:00
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
2019-06-04 12:12:40 +10:00
|
|
|
*/
|
2019-06-21 10:39:05 +10:00
|
|
|
fun removePreKeyBundle(hexEncodedPublicKey: String) {
|
2019-06-04 12:12:40 +10:00
|
|
|
val database = databaseHelper.writableDatabase
|
2019-06-21 10:39:05 +10:00
|
|
|
database.delete(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey ))
|
2019-06-04 12:12:40 +10:00
|
|
|
}
|
|
|
|
}
|