Fix session handling

This commit is contained in:
Niels Andriesse 2019-07-23 10:35:03 +10:00
parent ecd60b8723
commit a925b17419
9 changed files with 37 additions and 33 deletions

View File

@ -93,7 +93,7 @@
android:supportsRtl="true"
tools:replace="android:allowBackup"
android:allowBackup="false"
android:theme="@style/TextSecure.LightTheme"
android:theme="@style/TextSecure.DarkTheme"
android:largeHeap="true">
<meta-data
@ -308,7 +308,7 @@
<activity android:name=".RegistrationActivity"
android:launchMode="singleTask"
android:theme="@style/TextSecure.LightRegistrationTheme"
android:theme="@style/TextSecure.DarkRegistrationTheme"
android:windowSoftInputMode="stateUnchanged"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>

View File

@ -5,7 +5,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:background="@color/loki_darkest_gray"
android:fillViewport="true"
tools:context=".RegistrationActivity">

View File

@ -2,6 +2,7 @@
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/loki_darkest_gray"
android:layout_width="match_parent"
android:layout_height="match_parent">

View File

@ -94,7 +94,7 @@ public class IdentityKeyUtil {
save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(publicKey.serialize()));
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(keyPair.getPrivateKey().serialize()));
} catch (Exception e) {
Log.d("Loki", "Couldn't restore key pair from seed due to error: " + e.getMessage());
Log.d("Loki", "Couldn't restore key pair from seed due to error: " + e.getMessage() + ".");
}
}

View File

@ -102,14 +102,12 @@ public class PreKeyUtil {
}
public synchronized static List<PreKeyRecord> generatePreKeys(Context context, int amount) {
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
List<PreKeyRecord> records = new LinkedList<>();
int preKeyIDOffset = TextSecurePreferences.getNextPreKeyId(context);
for (int i = 0; i < amount; i++) {
int preKeyID = (preKeyIDOffset + i) % Medium.MAX_VALUE;
ECKeyPair keyPair = Curve.generateKeyPair();
PreKeyRecord record = new PreKeyRecord(preKeyID, keyPair);
preKeyStore.storePreKey(preKeyID, record);
records.add(record);
}
TextSecurePreferences.setNextPreKeyId(context, (preKeyIDOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);

View File

@ -92,8 +92,8 @@ public class TextSecureSessionStore implements LokiSessionDatabaseProtocol {
}
}
public void archiveAllSessions(@NonNull String name) {
SignalProtocolAddress address = new SignalProtocolAddress(name, -1);
public void archiveAllSessions(@NonNull String hexEncodedPublicKey) {
SignalProtocolAddress address = new SignalProtocolAddress(hexEncodedPublicKey, -1);
archiveSiblingSessions(address);
}

View File

@ -244,9 +244,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
LokiPreKeyRecordDatabase lokiPreKeyRecordDatabase = DatabaseFactory.getLokiPreKeyRecordDatabase(context);
SignalServiceAddress localAddress = new SignalServiceAddress(TextSecurePreferences.getLocalNumber(context));
LokiServiceCipher cipher = new LokiServiceCipher(localAddress, axolotlStore, lokiThreadDatabase, lokiPreKeyRecordDatabase, UnidentifiedAccessUtil.getCertificateValidator());
/* Loki - Original code
SignalServiceCipher cipher = new SignalServiceCipher(localAddress, axolotlStore, UnidentifiedAccessUtil.getCertificateValidator());
*/
// Loki - Handle session reset logic
if (!envelope.isFriendRequest() && cipher.getSessionStatus(envelope) == null && envelope.isPreKeySignalMessage()) {
cipher.validateBackgroundMessage(envelope, envelope.getContent());
}
SignalServiceContent content = cipher.decrypt(envelope);
@ -255,7 +257,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
return;
}
// Loki - Handle Loki specific logic if needed
// Loki - Store pre key bundle if needed
if (content.lokiMessage.isPresent()) {
LokiServiceMessage lokiMessage = content.lokiMessage.get();
if (lokiMessage.getPreKeyBundleMessage() != null) {
@ -272,6 +274,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
}
}
// Loki - Get the sender display name if needed
Optional<String> senderDisplayName = content.senderDisplayName;
if (senderDisplayName.isPresent()) {
DatabaseFactory.getLokiUserDisplayNameDatabase(context).setDisplayName(envelope.getSource(), senderDisplayName.get());
@ -299,7 +302,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
handleNeedsDeliveryReceipt(content, message);
}
// Loki - Handle friend request logic if needed
// Loki - Handle friend request logic
handleFriendRequestIfNeeded(envelope, content, message);
} else if (content.getSyncMessage().isPresent()) {
TextSecurePreferences.setMultiDevice(context, true);
@ -336,6 +339,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
if (envelope.isPreKeySignalMessage()) {
ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob());
}
// Loki - Handle session reset logic
if (!envelope.isFriendRequest()) {
cipher.handleSessionResetRequestIfNeeded(envelope, cipher.getSessionStatus(envelope));
}
} catch (ProtocolInvalidVersionException e) {
Log.w(TAG, e);
handleInvalidVersionMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId);
@ -471,7 +479,6 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
if (threadId != null) {
TextSecureSessionStore sessionStore = new TextSecureSessionStore(context);
LokiThreadDatabase lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context);
// sessionStore.deleteAllSessions(content.getSender());
Log.d("Loki", "Received a session reset request from: " + content.getSender() + "; archiving the session.");

View File

@ -2,7 +2,6 @@ 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
@ -73,17 +72,17 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) :
fun setPreKeyBundle(hexEncodedPublicKey: String, preKeyBundle: PreKeyBundle) {
val database = databaseHelper.writableDatabase
val contentValues = ContentValues(9)
contentValues.put(registrationID, preKeyBundle.registrationId)
contentValues.put(deviceID, preKeyBundle.deviceId)
contentValues.put(preKeyID, preKeyBundle.preKeyId)
contentValues.put(preKeyPublic, Base64.encodeBytes(preKeyBundle.preKey.serialize()))
contentValues.put(signedPreKeyID, preKeyBundle.signedPreKeyId)
contentValues.put(signedPreKeyPublic, Base64.encodeBytes(preKeyBundle.signedPreKey.serialize()))
contentValues.put(signedPreKeySignature, Base64.encodeBytes(preKeyBundle.signedPreKeySignature))
contentValues.put(identityKey, Base64.encodeBytes(preKeyBundle.identityKey.serialize()))
contentValues.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
database.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE)
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()))
values.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
database.insertOrUpdate(tableName, values, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey ))
}
override fun removePreKeyBundle(hexEncodedPublicKey: String) {

View File

@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.loki
import android.content.ContentValues
import android.content.Context
import net.sqlcipher.database.SQLiteDatabase
import org.thoughtcrime.securesms.crypto.PreKeyUtil
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
@ -13,9 +12,9 @@ class LokiPreKeyRecordDatabase(context: Context, helper: SQLCipherOpenHelper) :
companion object {
private val tableName = "loki_pre_key_record_database"
private val preKeyID = "pre_key_id"
private val hexEncodedPublicKey = "public_key"
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($preKeyID INTEGER PRIMARY KEY, $hexEncodedPublicKey TEXT);"
private val preKeyID = "pre_key_id"
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($hexEncodedPublicKey TEXT PRIMARY KEY, $preKeyID INTEGER);"
}
fun hasPreKey(hexEncodedPublicKey: String): Boolean {
@ -36,14 +35,14 @@ class LokiPreKeyRecordDatabase(context: Context, helper: SQLCipherOpenHelper) :
}
private fun generateAndStorePreKeyRecord(hexEncodedPublicKey: String): PreKeyRecord {
val preKeyRecords = PreKeyUtil.generatePreKeys(context, 1)
PreKeyUtil.storePreKeyRecords(context, preKeyRecords)
val record = preKeyRecords.first()
val records = PreKeyUtil.generatePreKeys(context, 1)
PreKeyUtil.storePreKeyRecords(context, records)
val record = records.first()
val database = databaseHelper.writableDatabase
val values = ContentValues(2)
values.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
values.put(preKeyID, record.id)
database.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE)
database.insertOrUpdate(tableName, values, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey ))
return record
}
}