|
|
@ -19,101 +19,86 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) :
|
|
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
companion object {
|
|
|
|
private val tableName = "loki_pre_key_bundle_database"
|
|
|
|
private val tableName = "loki_pre_key_bundle_database"
|
|
|
|
private val pubKey = "pub_key"
|
|
|
|
private val hexEncodedPublicKey = "public_key"
|
|
|
|
private val preKeyId = "pre_key_id"
|
|
|
|
private val preKeyID = "pre_key_id"
|
|
|
|
private val preKeyPublic = "pre_key_public"
|
|
|
|
private val preKeyPublic = "pre_key_public"
|
|
|
|
private val signedPreKeyId = "signed_pre_key_id"
|
|
|
|
private val signedPreKeyID = "signed_pre_key_id"
|
|
|
|
private val signedPreKeyPublic = "signed_pre_key_public"
|
|
|
|
private val signedPreKeyPublic = "signed_pre_key_public"
|
|
|
|
private val signedPreKeySignature = "signed_pre_key_signature"
|
|
|
|
private val signedPreKeySignature = "signed_pre_key_signature"
|
|
|
|
private val identityKey = "identity_key"
|
|
|
|
private val identityKey = "identity_key"
|
|
|
|
private val deviceId = "device_id"
|
|
|
|
private val deviceID = "device_id"
|
|
|
|
private val registrationId = "registration_id"
|
|
|
|
private val registrationID = "registration_id"
|
|
|
|
|
|
|
|
@JvmStatic val createTableCommand = "CREATE TABLE $tableName (" + "$hexEncodedPublicKey TEXT PRIMARY KEY," + "$preKeyID INTEGER," +
|
|
|
|
@JvmStatic
|
|
|
|
"$preKeyPublic TEXT NOT NULL," + "$signedPreKeyID INTEGER," + "$signedPreKeyPublic TEXT NOT NULL," +
|
|
|
|
val createTableCommand = "CREATE TABLE $tableName (" +
|
|
|
|
"$signedPreKeySignature TEXT," + "$identityKey TEXT NOT NULL," + "$deviceID INTEGER," + "$registrationID INTEGER" + ");"
|
|
|
|
"$pubKey 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" +
|
|
|
|
|
|
|
|
");"
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Generate a `PreKeyBundle` for the given contact.
|
|
|
|
* 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.
|
|
|
|
* This generated bundle shouldn't be stored locally since this is used to generate bundles to send to other users.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param pubKey String The hex encoded public key of the contact.
|
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
|
|
|
* @return PreKeyBundle? A pre key bundle or `null` if something went wrong.
|
|
|
|
* @return PreKeyBundle? A pre key bundle or `null` if something went wrong.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun generatePreKeyBundle(pubKey: String): PreKeyBundle? {
|
|
|
|
fun generatePreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? {
|
|
|
|
val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context)
|
|
|
|
val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context)
|
|
|
|
val signedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null
|
|
|
|
val signedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null
|
|
|
|
|
|
|
|
val preKeyRecord = DatabaseFactory.getLokiContactPreKeyDatabase(context).getOrCreatePreKey(hexEncodedPublicKey)
|
|
|
|
val preKeyRecord = DatabaseFactory.getLokiContactPreKeyDatabase(context).getOrCreatePreKey(pubKey)
|
|
|
|
val registrationID = TextSecurePreferences.getLocalRegistrationId(context)
|
|
|
|
val registrationId = TextSecurePreferences.getLocalRegistrationId(context)
|
|
|
|
if (registrationID == 0) return null
|
|
|
|
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)
|
|
|
|
val deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID
|
|
|
|
|
|
|
|
return PreKeyBundle(registrationId, deviceId,preKeyRecord.id, preKeyRecord.keyPair.publicKey, signedPreKey.id, signedPreKey.keyPair.publicKey, signedPreKey.signature, identityKeyPair.publicKey)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get the `PreKeyBundle` associated with the given contact.
|
|
|
|
* Get the `PreKeyBundle` associated with the given contact.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param pubKey String The hex encoded public key of the contact.
|
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
|
|
|
* @return PreKeyBundle? The pre key bundle or `null` if it doesn't exist.
|
|
|
|
* @return PreKeyBundle? The pre key bundle or `null` if it doesn't exist.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun getPreKeyBundle(pubKey: String): PreKeyBundle? {
|
|
|
|
fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? {
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor ->
|
|
|
|
return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { cursor ->
|
|
|
|
val registrationId = cursor.getInt(registrationId)
|
|
|
|
val registrationID = cursor.getInt(registrationID)
|
|
|
|
val deviceId = cursor.getInt(deviceId)
|
|
|
|
val deviceID = cursor.getInt(deviceID)
|
|
|
|
val preKeyId = cursor.getInt(preKeyId)
|
|
|
|
val preKeyID = cursor.getInt(preKeyID)
|
|
|
|
val preKey = Curve.decodePoint(cursor.getBase64EncodedData(preKeyPublic), 0)
|
|
|
|
val preKey = Curve.decodePoint(cursor.getBase64EncodedData(preKeyPublic), 0)
|
|
|
|
val signedPreKeyId = cursor.getInt(signedPreKeyId)
|
|
|
|
val signedPreKeyID = cursor.getInt(signedPreKeyID)
|
|
|
|
val signedPreKey = Curve.decodePoint(cursor.getBase64EncodedData(signedPreKeyPublic), 0)
|
|
|
|
val signedPreKey = Curve.decodePoint(cursor.getBase64EncodedData(signedPreKeyPublic), 0)
|
|
|
|
val signedPreKeySignature = cursor.getBase64EncodedData(signedPreKeySignature)
|
|
|
|
val signedPreKeySignature = cursor.getBase64EncodedData(signedPreKeySignature)
|
|
|
|
val identityKey = IdentityKey(cursor.getBase64EncodedData(identityKey), 0)
|
|
|
|
val identityKey = IdentityKey(cursor.getBase64EncodedData(identityKey), 0)
|
|
|
|
|
|
|
|
PreKeyBundle(registrationID, deviceID, preKeyID, preKey, signedPreKeyID, signedPreKey, signedPreKeySignature, identityKey)
|
|
|
|
PreKeyBundle(registrationId, deviceId, preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, identityKey)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Set the `PreKeyBundle` for the given contact.
|
|
|
|
* Set the `PreKeyBundle` for the given contact.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param pubKey String The hex encoded public key of the contact.
|
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
|
|
|
* @param preKeyBundle PreKeyBundle The pre key bundle.
|
|
|
|
* @param preKeyBundle PreKeyBundle The pre key bundle.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun setPreKeyBundle(pubKey: String, preKeyBundle: PreKeyBundle) {
|
|
|
|
fun setPreKeyBundle(hexEncodedPublicKey: String, preKeyBundle: PreKeyBundle) {
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
val contentValues = ContentValues()
|
|
|
|
val contentValues = ContentValues()
|
|
|
|
contentValues.put(registrationId, preKeyBundle.registrationId)
|
|
|
|
contentValues.put(registrationID, preKeyBundle.registrationId)
|
|
|
|
contentValues.put(deviceId, preKeyBundle.deviceId)
|
|
|
|
contentValues.put(deviceID, preKeyBundle.deviceId)
|
|
|
|
contentValues.put(preKeyId, preKeyBundle.preKeyId)
|
|
|
|
contentValues.put(preKeyID, preKeyBundle.preKeyId)
|
|
|
|
contentValues.put(preKeyPublic, Base64.encodeBytes(preKeyBundle.preKey.serialize()))
|
|
|
|
contentValues.put(preKeyPublic, Base64.encodeBytes(preKeyBundle.preKey.serialize()))
|
|
|
|
contentValues.put(signedPreKeyId, preKeyBundle.signedPreKeyId)
|
|
|
|
contentValues.put(signedPreKeyID, preKeyBundle.signedPreKeyId)
|
|
|
|
contentValues.put(signedPreKeyPublic, Base64.encodeBytes(preKeyBundle.signedPreKey.serialize()))
|
|
|
|
contentValues.put(signedPreKeyPublic, Base64.encodeBytes(preKeyBundle.signedPreKey.serialize()))
|
|
|
|
contentValues.put(signedPreKeySignature, Base64.encodeBytes(preKeyBundle.signedPreKeySignature))
|
|
|
|
contentValues.put(signedPreKeySignature, Base64.encodeBytes(preKeyBundle.signedPreKeySignature))
|
|
|
|
contentValues.put(identityKey, Base64.encodeBytes(preKeyBundle.identityKey.serialize()))
|
|
|
|
contentValues.put(identityKey, Base64.encodeBytes(preKeyBundle.identityKey.serialize()))
|
|
|
|
contentValues.put(Companion.pubKey, pubKey)
|
|
|
|
contentValues.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
|
|
|
|
|
|
|
|
|
|
|
|
database.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE)
|
|
|
|
database.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Remove the `PreKeyBundle` for the given contact.
|
|
|
|
* Remove the `PreKeyBundle` for the given contact.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param pubKey String The hex encoded public key of the contact.
|
|
|
|
* @param hexEncodedPublicKey String The hex encoded public key of the contact.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun removePreKeyBundle(pubKey: String) {
|
|
|
|
fun removePreKeyBundle(hexEncodedPublicKey: String) {
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
database.delete(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey))
|
|
|
|
database.delete(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey ))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|