2016-08-30 00:49:49 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.AsyncTask;
|
2017-06-07 01:03:09 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.annotation.StringRes;
|
2016-08-30 00:49:49 +00:00
|
|
|
import android.support.annotation.UiThread;
|
2017-05-20 01:01:40 +00:00
|
|
|
import android.util.Log;
|
2016-08-30 00:49:49 +00:00
|
|
|
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUnion;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.thoughtcrime.securesms.database.IdentityDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.IdentityDatabase.IdentityRecord;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult;
|
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2016-08-30 00:49:49 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingIdentityDefaultMessage;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingIdentityUpdateMessage;
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingIdentityVerifiedMessage;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.OutgoingIdentityDefaultMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.OutgoingIdentityVerifiedMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
|
2016-08-30 00:49:49 +00:00
|
|
|
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SettableFuture;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
2016-08-30 00:49:49 +00:00
|
|
|
|
2017-06-07 01:03:09 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static org.whispersystems.libsignal.SessionCipher.SESSION_LOCK;
|
|
|
|
|
2016-08-30 00:49:49 +00:00
|
|
|
public class IdentityUtil {
|
|
|
|
|
2017-05-20 01:01:40 +00:00
|
|
|
private static final String TAG = IdentityUtil.class.getSimpleName();
|
|
|
|
|
2016-08-30 00:49:49 +00:00
|
|
|
@UiThread
|
2017-06-07 01:03:09 +00:00
|
|
|
public static ListenableFuture<Optional<IdentityRecord>> getRemoteIdentityKey(final Context context, final Recipient recipient) {
|
|
|
|
final SettableFuture<Optional<IdentityRecord>> future = new SettableFuture<>();
|
2016-08-30 00:49:49 +00:00
|
|
|
|
2017-06-07 01:03:09 +00:00
|
|
|
new AsyncTask<Recipient, Void, Optional<IdentityRecord>>() {
|
2016-08-30 00:49:49 +00:00
|
|
|
@Override
|
2017-06-07 01:03:09 +00:00
|
|
|
protected Optional<IdentityRecord> doInBackground(Recipient... recipient) {
|
|
|
|
return DatabaseFactory.getIdentityDatabase(context)
|
|
|
|
.getIdentity(recipient[0].getRecipientId());
|
2016-08-30 00:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-06-07 01:03:09 +00:00
|
|
|
protected void onPostExecute(Optional<IdentityRecord> result) {
|
2016-08-30 00:49:49 +00:00
|
|
|
future.set(result);
|
|
|
|
}
|
|
|
|
}.execute(recipient);
|
|
|
|
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2017-06-07 01:03:09 +00:00
|
|
|
public static void markIdentityVerified(Context context, MasterSecretUnion masterSecret,
|
|
|
|
Recipient recipient, boolean verified, boolean remote)
|
|
|
|
{
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFor(context, recipient, true);
|
|
|
|
GroupDatabase.Reader reader = groupDatabase.getGroups();
|
|
|
|
|
|
|
|
String number = recipient.getNumber();
|
|
|
|
|
|
|
|
try {
|
|
|
|
number = Util.canonicalizeNumber(context, number);
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupDatabase.GroupRecord groupRecord;
|
|
|
|
|
|
|
|
while ((groupRecord = reader.getNext()) != null) {
|
|
|
|
if (groupRecord.getMembers().contains(number) && groupRecord.isActive()) {
|
|
|
|
SignalServiceGroup group = new SignalServiceGroup(groupRecord.getId());
|
|
|
|
|
|
|
|
if (remote) {
|
|
|
|
IncomingTextMessage incoming = new IncomingTextMessage(number, 1, time, null, Optional.of(group), 0);
|
|
|
|
|
|
|
|
if (verified) incoming = new IncomingIdentityVerifiedMessage(incoming);
|
|
|
|
else incoming = new IncomingIdentityDefaultMessage(incoming);
|
|
|
|
|
|
|
|
smsDatabase.insertMessageInbox(incoming);
|
|
|
|
} else {
|
|
|
|
Recipients groupRecipients = RecipientFactory.getRecipientsFromString(context, GroupUtil.getEncodedId(group.getGroupId()), true);
|
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(groupRecipients);
|
|
|
|
OutgoingTextMessage outgoing ;
|
|
|
|
|
|
|
|
if (verified) outgoing = new OutgoingIdentityVerifiedMessage(recipients);
|
|
|
|
else outgoing = new OutgoingIdentityDefaultMessage(recipients);
|
|
|
|
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).insertMessageOutbox(masterSecret, threadId, outgoing, false, time, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remote) {
|
|
|
|
IncomingTextMessage incoming = new IncomingTextMessage(number, 1, time, null, Optional.<SignalServiceGroup>absent(), 0);
|
|
|
|
|
|
|
|
if (verified) incoming = new IncomingIdentityVerifiedMessage(incoming);
|
|
|
|
else incoming = new IncomingIdentityDefaultMessage(incoming);
|
|
|
|
|
|
|
|
smsDatabase.insertMessageInbox(incoming);
|
|
|
|
} else {
|
|
|
|
OutgoingTextMessage outgoing;
|
|
|
|
|
|
|
|
if (verified) outgoing = new OutgoingIdentityVerifiedMessage(recipients);
|
|
|
|
else outgoing = new OutgoingIdentityDefaultMessage(recipients);
|
|
|
|
|
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
|
|
|
|
|
|
|
Log.w(TAG, "Inserting verified outbox...");
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context)
|
|
|
|
.insertMessageOutbox(masterSecret, threadId, outgoing, false, time, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void processVerifiedMessage(Context context, MasterSecretUnion masterSecret, VerifiedMessage verifiedMessage) {
|
|
|
|
synchronized (SESSION_LOCK) {
|
|
|
|
IdentityDatabase identityDatabase = DatabaseFactory.getIdentityDatabase(context);
|
|
|
|
Recipient recipient = RecipientFactory.getRecipientsFromString(context, verifiedMessage.getDestination(), true).getPrimaryRecipient();
|
|
|
|
Optional<IdentityRecord> identityRecord = identityDatabase.getIdentity(recipient.getRecipientId());
|
|
|
|
|
|
|
|
if (!identityRecord.isPresent() && verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.DEFAULT) {
|
|
|
|
Log.w(TAG, "No existing record for default status");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identityRecord.isPresent() &&
|
|
|
|
identityRecord.get().getIdentityKey().equals(verifiedMessage.getIdentityKey()) &&
|
|
|
|
identityRecord.get().getVerifiedStatus() != IdentityDatabase.VerifiedStatus.DEFAULT &&
|
|
|
|
verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.DEFAULT)
|
|
|
|
{
|
|
|
|
identityDatabase.setVerified(recipient.getRecipientId(), identityRecord.get().getIdentityKey(), IdentityDatabase.VerifiedStatus.DEFAULT);
|
|
|
|
markIdentityVerified(context, masterSecret, recipient, false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verifiedMessage.getVerified() == VerifiedMessage.VerifiedState.VERIFIED &&
|
|
|
|
(!identityRecord.isPresent() ||
|
|
|
|
(identityRecord.isPresent() && !identityRecord.get().getIdentityKey().equals(verifiedMessage.getIdentityKey())) ||
|
|
|
|
(identityRecord.isPresent() && identityRecord.get().getVerifiedStatus() != IdentityDatabase.VerifiedStatus.VERIFIED)))
|
|
|
|
{
|
|
|
|
identityDatabase.saveIdentity(recipient.getRecipientId(), verifiedMessage.getIdentityKey(),
|
|
|
|
IdentityDatabase.VerifiedStatus.VERIFIED, false, System.currentTimeMillis(), true);
|
|
|
|
markIdentityVerified(context, masterSecret, recipient, true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 01:01:40 +00:00
|
|
|
public static void markIdentityUpdate(Context context, Recipient recipient) {
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
GroupDatabase.Reader reader = groupDatabase.getGroups();
|
|
|
|
|
|
|
|
String number = recipient.getNumber();
|
|
|
|
|
|
|
|
try {
|
|
|
|
number = Util.canonicalizeNumber(context, number);
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupDatabase.GroupRecord groupRecord;
|
|
|
|
|
|
|
|
while ((groupRecord = reader.getNext()) != null) {
|
|
|
|
if (groupRecord.getMembers().contains(number) && groupRecord.isActive()) {
|
|
|
|
SignalServiceGroup group = new SignalServiceGroup(groupRecord.getId());
|
|
|
|
IncomingTextMessage incoming = new IncomingTextMessage(number, 1, time, null, Optional.of(group), 0);
|
|
|
|
IncomingIdentityUpdateMessage groupUpdate = new IncomingIdentityUpdateMessage(incoming);
|
|
|
|
|
|
|
|
smsDatabase.insertMessageInbox(groupUpdate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IncomingTextMessage incoming = new IncomingTextMessage(number, 1, time, null, Optional.<SignalServiceGroup>absent(), 0);
|
|
|
|
IncomingIdentityUpdateMessage individualUpdate = new IncomingIdentityUpdateMessage(incoming);
|
|
|
|
Optional<InsertResult> insertResult = smsDatabase.insertMessageInbox(individualUpdate);
|
|
|
|
|
|
|
|
if (insertResult.isPresent()) {
|
|
|
|
MessageNotifier.updateNotification(context, null, insertResult.get().getThreadId());
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 01:03:09 +00:00
|
|
|
|
|
|
|
public static @Nullable String getUnverifiedBannerDescription(@NonNull Context context,
|
|
|
|
@NonNull List<Recipient> unverified)
|
|
|
|
{
|
|
|
|
return getPluralizedIdentityDescription(context, unverified,
|
|
|
|
R.string.IdentityUtil_unverified_banner_one,
|
|
|
|
R.string.IdentityUtil_unverified_banner_two,
|
|
|
|
R.string.IdentityUtil_unverified_banner_many);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable String getUnverifiedSendDialogDescription(@NonNull Context context,
|
|
|
|
@NonNull List<Recipient> unverified)
|
|
|
|
{
|
|
|
|
return getPluralizedIdentityDescription(context, unverified,
|
|
|
|
R.string.IdentityUtil_unverified_dialog_one,
|
|
|
|
R.string.IdentityUtil_unverified_dialog_two,
|
|
|
|
R.string.IdentityUtil_unverified_dialog_many);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable String getUntrustedSendDialogDescription(@NonNull Context context,
|
|
|
|
@NonNull List<Recipient> untrusted)
|
|
|
|
{
|
|
|
|
return getPluralizedIdentityDescription(context, untrusted,
|
|
|
|
R.string.IdentityUtil_untrusted_dialog_one,
|
|
|
|
R.string.IdentityUtil_untrusted_dialog_two,
|
|
|
|
R.string.IdentityUtil_untrusted_dialog_many);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static @Nullable String getPluralizedIdentityDescription(@NonNull Context context,
|
|
|
|
@NonNull List<Recipient> recipients,
|
|
|
|
@StringRes int resourceOne,
|
|
|
|
@StringRes int resourceTwo,
|
|
|
|
@StringRes int resourceMany)
|
|
|
|
{
|
|
|
|
if (recipients.isEmpty()) return null;
|
|
|
|
|
|
|
|
if (recipients.size() == 1) {
|
|
|
|
String name = recipients.get(0).toShortString();
|
|
|
|
return context.getString(resourceOne, name);
|
|
|
|
} else {
|
|
|
|
String firstName = recipients.get(0).toShortString();
|
|
|
|
String secondName = recipients.get(1).toShortString();
|
|
|
|
|
|
|
|
if (recipients.size() == 2) {
|
|
|
|
return context.getString(resourceTwo, firstName, secondName);
|
|
|
|
} else {
|
|
|
|
String nMore;
|
|
|
|
|
|
|
|
if (recipients.size() == 3) {
|
|
|
|
nMore = context.getResources().getQuantityString(R.plurals.identity_others, 1);
|
|
|
|
} else {
|
|
|
|
nMore = context.getResources().getQuantityString(R.plurals.identity_others, recipients.size() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.getString(resourceMany, firstName, secondName, nMore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 00:49:49 +00:00
|
|
|
}
|