From dd67e59807db4d19b6e29a61733afdafd413e96c Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Wed, 26 Jun 2019 14:31:38 +1000 Subject: [PATCH] Fix pre key usage --- .../securesms/jobs/PushTextSendJob.java | 2 +- .../securesms/jobs/RefreshPreKeysJob.java | 4 ++-- .../securesms/jobs/RotateSignedPreKeyJob.java | 2 +- .../loki/LokiPreKeyBundleDatabase.kt | 24 ++++++++++++++----- .../service/RotateSignedPreKeyListener.java | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java b/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java index 32d1718374..49b9bb2e21 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java @@ -156,7 +156,7 @@ public class PushTextSendJob extends PushSendJob implements InjectableType { throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException { try { - // rotateSenderCertificateIfNecessary(); // LOKITODO: Uncomment this? + // rotateSenderCertificateIfNecessary(); SignalServiceAddress address = getPushAddress(message.getIndividualRecipient().getAddress()); Optional profileKey = getProfileKey(message.getIndividualRecipient()); diff --git a/src/org/thoughtcrime/securesms/jobs/RefreshPreKeysJob.java b/src/org/thoughtcrime/securesms/jobs/RefreshPreKeysJob.java index 0a864c40db..38532d0d54 100644 --- a/src/org/thoughtcrime/securesms/jobs/RefreshPreKeysJob.java +++ b/src/org/thoughtcrime/securesms/jobs/RefreshPreKeysJob.java @@ -55,9 +55,9 @@ public class RefreshPreKeysJob extends BaseJob implements InjectableType { @Override public void onRun() throws IOException { if (TextSecurePreferences.isSignedPreKeyRegistered(context)) { - Log.i(TAG, "Already have a signed pre key registered."); + Log.d("Loki", "A signed pre key has already been registered."); } else { - Log.i(TAG, "Registering new signed pre key..."); + Log.d("Loki", "Registering a new signed pre key."); IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context); PreKeyUtil.generateSignedPreKey(context, identityKey, true); TextSecurePreferences.setSignedPreKeyRegistered(context, true); diff --git a/src/org/thoughtcrime/securesms/jobs/RotateSignedPreKeyJob.java b/src/org/thoughtcrime/securesms/jobs/RotateSignedPreKeyJob.java index 9f1ccdb0fe..62335d9e18 100644 --- a/src/org/thoughtcrime/securesms/jobs/RotateSignedPreKeyJob.java +++ b/src/org/thoughtcrime/securesms/jobs/RotateSignedPreKeyJob.java @@ -55,7 +55,7 @@ public class RotateSignedPreKeyJob extends BaseJob implements InjectableType { IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context); SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false); - accountManager.setSignedPreKey(signedPreKeyRecord); + // accountManager.setSignedPreKey(signedPreKeyRecord); PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId()); TextSecurePreferences.setSignedPreKeyRegistered(context, true); diff --git a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt index 8836aa4483..c4fb6e2d46 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt @@ -8,11 +8,13 @@ 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.logging.Log 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 +import org.whispersystems.libsignal.util.KeyHelper import org.whispersystems.signalservice.api.push.SignalServiceAddress import org.whispersystems.signalservice.loki.messaging.LokiPreKeyBundleDatabaseProtocol @@ -35,13 +37,23 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : } fun generatePreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? { - val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context) - val signedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null - val preKeyRecord = DatabaseFactory.getLokiPreKeyRecordDatabase(context).getOrCreatePreKey(hexEncodedPublicKey) - val registrationID = TextSecurePreferences.getLocalRegistrationId(context) - if (registrationID == 0) return null + var registrationID = TextSecurePreferences.getLocalRegistrationId(context) + if (registrationID == 0) { + registrationID = KeyHelper.generateRegistrationId(false) + TextSecurePreferences.setLocalRegistrationId(context, registrationID) + } 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 preKeyRecord = DatabaseFactory.getLokiPreKeyRecordDatabase(context).getOrCreatePreKey(hexEncodedPublicKey) + val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context) + if (TextSecurePreferences.isSignedPreKeyRegistered(context)) { + 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) } override fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? { diff --git a/src/org/thoughtcrime/securesms/service/RotateSignedPreKeyListener.java b/src/org/thoughtcrime/securesms/service/RotateSignedPreKeyListener.java index 06ed207f27..d1a6198892 100644 --- a/src/org/thoughtcrime/securesms/service/RotateSignedPreKeyListener.java +++ b/src/org/thoughtcrime/securesms/service/RotateSignedPreKeyListener.java @@ -21,7 +21,7 @@ public class RotateSignedPreKeyListener extends PersistentAlarmManagerListener { @Override protected long onAlarm(Context context, long scheduledTime) { - if (scheduledTime != 0 && TextSecurePreferences.isPushRegistered(context)) { + if (scheduledTime != 0 /*&& TextSecurePreferences.isPushRegistered(context)*/) { ApplicationContext.getInstance(context) .getJobManager() .add(new RotateSignedPreKeyJob());