From 63d1e7087ad2df06cfe1c18d53db4288ed23e30f Mon Sep 17 00:00:00 2001 From: Anton Chekulaev Date: Thu, 10 Dec 2020 12:02:23 +1100 Subject: [PATCH] Session request removed. Private chats no longer use any encryption. --- .../securesms/ApplicationContext.java | 62 ++++++---- .../migration/WorkManagerFactoryMappings.java | 3 +- .../securesms/jobs/JobManagerFactories.java | 2 - .../protocol/SessionRequestMessageSendJob.kt | 107 ------------------ .../api/SignalServiceMessageSender.java | 6 +- 5 files changed, 41 insertions(+), 139 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index babb511f96..52059ca6b5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -33,6 +33,7 @@ import com.google.firebase.iid.FirebaseInstanceId; import org.conscrypt.Conscrypt; import org.jetbrains.annotations.NotNull; +import org.session.libsignal.libsignal.state.SessionRecord; import org.signal.aesgcmprovider.AesGcmProvider; import org.thoughtcrime.securesms.components.TypingStatusRepository; import org.thoughtcrime.securesms.components.TypingStatusSender; @@ -68,7 +69,6 @@ import org.thoughtcrime.securesms.loki.database.LokiThreadDatabase; import org.thoughtcrime.securesms.loki.database.LokiUserDatabase; import org.thoughtcrime.securesms.loki.database.SharedSenderKeysDatabase; import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocol; -import org.thoughtcrime.securesms.loki.protocol.SessionRequestMessageSendJob; import org.thoughtcrime.securesms.loki.protocol.SessionResetImplementation; import org.thoughtcrime.securesms.loki.utilities.Broadcaster; import org.thoughtcrime.securesms.loki.utilities.UiModeUtilities; @@ -595,39 +595,51 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc Runtime.getRuntime().exit(0); } - public boolean hasSentSessionRequestExpired(@NotNull String publicKey) { - LokiAPIDatabase apiDB = DatabaseFactory.getLokiAPIDatabase(this); - Long timestamp = apiDB.getSessionRequestSentTimestamp(publicKey); - if (timestamp != null) { - long expiration = timestamp + TTLUtilities.getTTL(TTLUtilities.MessageType.SessionRequest); - return new Date().getTime() > expiration; - } else { - return false; - } - } +// public boolean hasSentSessionRequestExpired(@NotNull String publicKey) { +// LokiAPIDatabase apiDB = DatabaseFactory.getLokiAPIDatabase(this); +// Long timestamp = apiDB.getSessionRequestSentTimestamp(publicKey); +// if (timestamp != null) { +// long expiration = timestamp + TTLUtilities.getTTL(TTLUtilities.MessageType.SessionRequest); +// return new Date().getTime() > expiration; +// } else { +// return false; +// } +// } @Override public void sendSessionRequestIfNeeded(@NotNull String publicKey) { +// // It's never necessary to establish a session with self +// String userPublicKey = TextSecurePreferences.getLocalNumber(this); +// if (publicKey.equals(userPublicKey)) { return; } +// // Check that we don't already have a session +// SignalProtocolAddress address = new SignalProtocolAddress(publicKey, SignalServiceAddress.DEFAULT_DEVICE_ID); +// boolean hasSession = new TextSecureSessionStore(this).containsSession(address); +// if (hasSession) { return; } +// // Check that we didn't already send a session request +// LokiAPIDatabase apiDB = DatabaseFactory.getLokiAPIDatabase(this); +// boolean hasSentSessionRequest = (apiDB.getSessionRequestSentTimestamp(publicKey) != null); +// boolean hasSentSessionRequestExpired = hasSentSessionRequestExpired(publicKey); +// if (hasSentSessionRequestExpired) { +// apiDB.setSessionRequestSentTimestamp(publicKey, 0); +// } +// if (hasSentSessionRequest && !hasSentSessionRequestExpired) { return; } +// // Send the session request +// long timestamp = new Date().getTime(); +// apiDB.setSessionRequestSentTimestamp(publicKey, timestamp); +// SessionRequestMessageSendJob job = new SessionRequestMessageSendJob(publicKey, timestamp); +// jobManager.add(job); + + // It's never necessary to establish a session with self String userPublicKey = TextSecurePreferences.getLocalNumber(this); if (publicKey.equals(userPublicKey)) { return; } // Check that we don't already have a session SignalProtocolAddress address = new SignalProtocolAddress(publicKey, SignalServiceAddress.DEFAULT_DEVICE_ID); - boolean hasSession = new TextSecureSessionStore(this).containsSession(address); - if (hasSession) { return; } - // Check that we didn't already send a session request - LokiAPIDatabase apiDB = DatabaseFactory.getLokiAPIDatabase(this); - boolean hasSentSessionRequest = (apiDB.getSessionRequestSentTimestamp(publicKey) != null); - boolean hasSentSessionRequestExpired = hasSentSessionRequestExpired(publicKey); - if (hasSentSessionRequestExpired) { - apiDB.setSessionRequestSentTimestamp(publicKey, 0); + TextSecureSessionStore sessionStore = new TextSecureSessionStore(this); + boolean hasSession = sessionStore.containsSession(address); + if (!hasSession) { + sessionStore.storeSession(address, new SessionRecord()); } - if (hasSentSessionRequest && !hasSentSessionRequestExpired) { return; } - // Send the session request - long timestamp = new Date().getTime(); - apiDB.setSessionRequestSentTimestamp(publicKey, timestamp); - SessionRequestMessageSendJob job = new SessionRequestMessageSendJob(publicKey, timestamp); - jobManager.add(job); } @Override diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobmanager/migration/WorkManagerFactoryMappings.java b/app/src/main/java/org/thoughtcrime/securesms/jobmanager/migration/WorkManagerFactoryMappings.java index 22d860a1a3..8d1a780c65 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobmanager/migration/WorkManagerFactoryMappings.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobmanager/migration/WorkManagerFactoryMappings.java @@ -36,11 +36,11 @@ import org.thoughtcrime.securesms.loki.api.PrepareAttachmentAudioExtrasJob; import org.thoughtcrime.securesms.loki.api.ResetThreadSessionJob; import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob; import org.thoughtcrime.securesms.loki.protocol.NullMessageSendJob; -import org.thoughtcrime.securesms.loki.protocol.SessionRequestMessageSendJob; import java.util.HashMap; import java.util.Map; +//TODO AC: Looks like we don't use it anymore. Make sure it's abandoned and delete. public class WorkManagerFactoryMappings { private static final Map FACTORY_MAP = new HashMap() {{ @@ -69,7 +69,6 @@ public class WorkManagerFactoryMappings { put(RotateProfileKeyJob.class.getName(), RotateProfileKeyJob.KEY); put(SendDeliveryReceiptJob.class.getName(), SendDeliveryReceiptJob.KEY); put(SendReadReceiptJob.class.getName(), SendReadReceiptJob.KEY); - put(SessionRequestMessageSendJob.class.getName(), SessionRequestMessageSendJob.KEY); put(SmsReceiveJob.class.getName(), SmsReceiveJob.KEY); put(SmsSendJob.class.getName(), SmsSendJob.KEY); put(SmsSentJob.class.getName(), SmsSentJob.KEY); diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java index 17f8dc8e7e..93c2ae5be5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java @@ -18,7 +18,6 @@ import org.thoughtcrime.securesms.loki.api.PrepareAttachmentAudioExtrasJob; import org.thoughtcrime.securesms.loki.api.ResetThreadSessionJob; import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob; import org.thoughtcrime.securesms.loki.protocol.NullMessageSendJob; -import org.thoughtcrime.securesms.loki.protocol.SessionRequestMessageSendJob; import java.util.Arrays; import java.util.HashMap; @@ -54,7 +53,6 @@ public final class JobManagerFactories { put(RotateProfileKeyJob.KEY, new RotateProfileKeyJob.Factory()); put(SendDeliveryReceiptJob.KEY, new SendDeliveryReceiptJob.Factory()); put(SendReadReceiptJob.KEY, new SendReadReceiptJob.Factory()); - put(SessionRequestMessageSendJob.KEY, new SessionRequestMessageSendJob.Factory()); put(SmsReceiveJob.KEY, new SmsReceiveJob.Factory()); put(SmsSendJob.KEY, new SmsSendJob.Factory()); put(SmsSentJob.KEY, new SmsSentJob.Factory()); diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt deleted file mode 100644 index b06ece16c3..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt +++ /dev/null @@ -1,107 +0,0 @@ -package org.thoughtcrime.securesms.loki.protocol - -import com.google.protobuf.ByteString -import org.thoughtcrime.securesms.ApplicationContext -import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil -import org.thoughtcrime.securesms.database.Address -import org.thoughtcrime.securesms.database.DatabaseFactory -import org.thoughtcrime.securesms.jobmanager.Data -import org.thoughtcrime.securesms.jobmanager.Job -import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint -import org.thoughtcrime.securesms.jobs.BaseJob -import org.thoughtcrime.securesms.logging.Log -import org.thoughtcrime.securesms.recipients.Recipient -import org.session.libsignal.service.api.push.SignalServiceAddress -import org.session.libsignal.service.internal.push.SignalServiceProtos -import org.session.libsignal.service.loki.protocol.meta.TTLUtilities -import java.io.IOException -import java.security.SecureRandom -import java.util.* -import java.util.concurrent.TimeUnit - -class SessionRequestMessageSendJob private constructor(parameters: Parameters, private val publicKey: String, private val timestamp: Long) : BaseJob(parameters) { - - companion object { - const val KEY = "SessionRequestMessageSendJob" - } - - constructor(publicKey: String, timestamp: Long) : this(Parameters.Builder() - .addConstraint(NetworkConstraint.KEY) - .setQueue(KEY) - .setLifespan(TimeUnit.DAYS.toMillis(1)) - .setMaxAttempts(1) - .build(), - publicKey, - timestamp) - - override fun serialize(): Data { - return Data.Builder().putString("publicKey", publicKey).putLong("timestamp", timestamp).build() - } - - override fun getFactoryKey(): String { return KEY } - - public override fun onRun() { - // Prepare - val contentMessage = SignalServiceProtos.Content.newBuilder() - // Attach the pre key bundle message -// val preKeyBundleMessage = SignalServiceProtos.PreKeyBundleMessage.newBuilder() -// val preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(publicKey) ?: return -// preKeyBundleMessage.identityKey = ByteString.copyFrom(preKeyBundle.identityKey.serialize()) -// preKeyBundleMessage.deviceId = preKeyBundle.deviceId -// preKeyBundleMessage.preKeyId = preKeyBundle.preKeyId -// preKeyBundleMessage.signedKeyId = preKeyBundle.signedPreKeyId -// preKeyBundleMessage.preKey = ByteString.copyFrom(preKeyBundle.preKey.serialize()) -// preKeyBundleMessage.signedKey = ByteString.copyFrom(preKeyBundle.signedPreKey.serialize()) -// preKeyBundleMessage.signature = ByteString.copyFrom(preKeyBundle.signedPreKeySignature) -// contentMessage.preKeyBundleMessage = preKeyBundleMessage.build() - // Attach the null message - val nullMessage = SignalServiceProtos.NullMessage.newBuilder() - val sr = SecureRandom() - val paddingSize = sr.nextInt(512) - val padding = ByteArray(paddingSize) - sr.nextBytes(padding) - nullMessage.padding = ByteString.copyFrom(padding) - contentMessage.nullMessage = nullMessage.build() - // Send the result - val serializedContentMessage = contentMessage.build().toByteArray() - val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender() - val address = SignalServiceAddress(publicKey) - val recipient = Recipient.from(context, Address.fromSerialized(publicKey), false) - val udAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient) - val ttl = TTLUtilities.getTTL(TTLUtilities.MessageType.SessionRequest) - try { - messageSender.sendMessage(0, address, udAccess.get().targetUnidentifiedAccess, - Date().time, serializedContentMessage, false, ttl, false, - true, false, false, false) - } catch (e: Exception) { - Log.d("Loki", "Failed to send session request to: $publicKey due to error: $e.") - throw e - } - } - - public override fun onShouldRetry(e: Exception): Boolean { - // Disable since we have our own retrying - return false - } - - override fun onCanceled() { - // Update the DB on fail if this is still the most recently sent session request (should always be true) - val apiDB = DatabaseFactory.getLokiAPIDatabase(context) - if (apiDB.getSessionRequestSentTimestamp(publicKey) == timestamp) { - apiDB.setSessionRequestSentTimestamp(publicKey, 0) - } - } - - class Factory : Job.Factory { - - override fun create(parameters: Parameters, data: Data): SessionRequestMessageSendJob { - try { - val publicKey = data.getString("publicKey") - val timestamp = data.getLong("timestamp") - return SessionRequestMessageSendJob(parameters, publicKey, timestamp) - } catch (e: IOException) { - throw AssertionError(e) - } - } - } -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/api/SignalServiceMessageSender.java b/libsignal/src/main/java/org/session/libsignal/service/api/SignalServiceMessageSender.java index d2b8eea8d8..a4ab23e6b1 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/api/SignalServiceMessageSender.java +++ b/libsignal/src/main/java/org/session/libsignal/service/api/SignalServiceMessageSender.java @@ -495,7 +495,7 @@ public class SignalServiceMessageSender { return container.setReceiptMessage(builder).build().toByteArray(); } - private byte[] createMessageContent(SignalServiceDataMessage message, SignalServiceAddress recipient) + private byte[] createMessageContent(SignalServiceDataMessage message, SignalServiceAddress recipient) throws IOException { Content.Builder container = Content.newBuilder(); @@ -1186,8 +1186,8 @@ public class SignalServiceMessageSender { final String senderID; if (type == SignalServiceProtos.Envelope.Type.CLOSED_GROUP_CIPHERTEXT) { senderID = recipient.getNumber(); - } else if (type == SignalServiceProtos.Envelope.Type.UNIDENTIFIED_SENDER) { - senderID = ""; +// } else if (type == SignalServiceProtos.Envelope.Type.UNIDENTIFIED_SENDER) { +// senderID = ""; } else { senderID = userPublicKey; }