Search in sources :

Example 6 with BadGroupIdException

use of org.thoughtcrime.securesms.groups.BadGroupIdException in project Signal-Android by signalapp.

the class ReviewCardDialogFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    try {
        initializeViewModel();
    } catch (BadGroupIdException e) {
        throw new IllegalStateException(e);
    }
    TextView description = view.findViewById(R.id.description);
    RecyclerView recycler = view.findViewById(R.id.recycler);
    ReviewCardAdapter adapter = new ReviewCardAdapter(getNoGroupsInCommonResId(), getGroupsInCommonResId(), new AdapterCallbacks());
    recycler.setAdapter(adapter);
    viewModel.getReviewCards().observe(getViewLifecycleOwner(), cards -> {
        adapter.submitList(cards);
        description.setText(getString(getDescriptionResId(), cards.size()));
    });
    viewModel.getReviewEvents().observe(getViewLifecycleOwner(), this::onReviewEvent);
}
Also used : TextView(android.widget.TextView) RecyclerView(androidx.recyclerview.widget.RecyclerView) BadGroupIdException(org.thoughtcrime.securesms.groups.BadGroupIdException)

Example 7 with BadGroupIdException

use of org.thoughtcrime.securesms.groups.BadGroupIdException in project Signal-Android by signalapp.

the class MessageContentProcessor method handleMessage.

private void handleMessage(@NonNull SignalServiceContent content, long timestamp, @NonNull Recipient senderRecipient, @NonNull Optional<Long> smsMessageId) throws IOException, GroupChangeBusyException {
    try {
        Recipient threadRecipient = getMessageDestination(content);
        if (shouldIgnore(content, senderRecipient, threadRecipient)) {
            log(content.getTimestamp(), "Ignoring message.");
            return;
        }
        PendingRetryReceiptModel pending = ApplicationDependencies.getPendingRetryReceiptCache().get(senderRecipient.getId(), content.getTimestamp());
        long receivedTime = handlePendingRetry(pending, content, threadRecipient);
        log(String.valueOf(content.getTimestamp()), "Beginning message processing. Sender: " + formatSender(senderRecipient, content));
        if (content.getDataMessage().isPresent()) {
            GroupDatabase groupDatabase = SignalDatabase.groups();
            SignalServiceDataMessage message = content.getDataMessage().get();
            boolean isMediaMessage = message.getAttachments().isPresent() || message.getQuote().isPresent() || message.getSharedContacts().isPresent() || message.getPreviews().isPresent() || message.getSticker().isPresent() || message.getMentions().isPresent();
            Optional<GroupId> groupId = GroupUtil.idFromGroupContext(message.getGroupContext());
            boolean isGv2Message = groupId.isPresent() && groupId.get().isV2();
            if (isGv2Message) {
                if (handleGv2PreProcessing(groupId.orNull().requireV2(), content, content.getDataMessage().get().getGroupContext().get().getGroupV2().get(), senderRecipient)) {
                    return;
                }
            }
            MessageId messageId = null;
            if (isInvalidMessage(message))
                handleInvalidMessage(content.getSender(), content.getSenderDevice(), groupId, content.getTimestamp(), smsMessageId);
            else if (message.isEndSession())
                messageId = handleEndSessionMessage(content, smsMessageId, senderRecipient);
            else if (message.isGroupV1Update())
                handleGroupV1Message(content, message, smsMessageId, groupId.get().requireV1(), senderRecipient, threadRecipient, receivedTime);
            else if (message.isExpirationUpdate())
                messageId = handleExpirationUpdate(content, message, smsMessageId, groupId, senderRecipient, threadRecipient, receivedTime);
            else if (message.getReaction().isPresent())
                messageId = handleReaction(content, message, senderRecipient);
            else if (message.getRemoteDelete().isPresent())
                messageId = handleRemoteDelete(content, message, senderRecipient);
            else if (message.getPayment().isPresent())
                handlePayment(content, message, senderRecipient);
            else if (message.getStoryContext().isPresent())
                handleStoryMessage(content);
            else if (isMediaMessage)
                messageId = handleMediaMessage(content, message, smsMessageId, senderRecipient, threadRecipient, receivedTime);
            else if (message.getBody().isPresent())
                messageId = handleTextMessage(content, message, smsMessageId, groupId, senderRecipient, threadRecipient, receivedTime);
            else if (Build.VERSION.SDK_INT > 19 && message.getGroupCallUpdate().isPresent())
                handleGroupCallUpdateMessage(content, message, groupId, senderRecipient);
            if (groupId.isPresent() && groupDatabase.isUnknownGroup(groupId.get())) {
                handleUnknownGroupMessage(content, message.getGroupContext().get(), senderRecipient);
            }
            if (message.getProfileKey().isPresent()) {
                handleProfileKey(content, message.getProfileKey().get(), senderRecipient);
            }
            if (content.isNeedsReceipt() && messageId != null) {
                handleNeedsDeliveryReceipt(content, message, messageId);
            } else if (!content.isNeedsReceipt()) {
                if (RecipientUtil.shouldHaveProfileKey(context, threadRecipient)) {
                    Log.w(TAG, "Received an unsealed sender message from " + senderRecipient.getId() + ", but they should already have our profile key. Correcting.");
                    if (groupId.isPresent() && groupId.get().isV2()) {
                        Log.i(TAG, "Message was to a GV2 group. Ensuring our group profile keys are up to date.");
                        ApplicationDependencies.getJobManager().startChain(new RefreshAttributesJob(false)).then(GroupV2UpdateSelfProfileKeyJob.withQueueLimits(groupId.get().requireV2())).enqueue();
                    } else if (!threadRecipient.isGroup()) {
                        Log.i(TAG, "Message was to a 1:1. Ensuring this user has our profile key.");
                        ApplicationDependencies.getJobManager().startChain(new RefreshAttributesJob(false)).then(ProfileKeySendJob.create(context, SignalDatabase.threads().getOrCreateThreadIdFor(threadRecipient), true)).enqueue();
                    }
                }
            }
        } else if (content.getSyncMessage().isPresent()) {
            TextSecurePreferences.setMultiDevice(context, true);
            SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
            if (syncMessage.getSent().isPresent())
                handleSynchronizeSentMessage(content, syncMessage.getSent().get(), senderRecipient);
            else if (syncMessage.getRequest().isPresent())
                handleSynchronizeRequestMessage(syncMessage.getRequest().get(), content.getTimestamp());
            else if (syncMessage.getRead().isPresent())
                handleSynchronizeReadMessage(syncMessage.getRead().get(), content.getTimestamp(), senderRecipient);
            else if (syncMessage.getViewed().isPresent())
                handleSynchronizeViewedMessage(syncMessage.getViewed().get(), content.getTimestamp());
            else if (syncMessage.getViewOnceOpen().isPresent())
                handleSynchronizeViewOnceOpenMessage(syncMessage.getViewOnceOpen().get(), content.getTimestamp());
            else if (syncMessage.getVerified().isPresent())
                handleSynchronizeVerifiedMessage(syncMessage.getVerified().get());
            else if (syncMessage.getStickerPackOperations().isPresent())
                handleSynchronizeStickerPackOperation(syncMessage.getStickerPackOperations().get(), content.getTimestamp());
            else if (syncMessage.getConfiguration().isPresent())
                handleSynchronizeConfigurationMessage(syncMessage.getConfiguration().get(), content.getTimestamp());
            else if (syncMessage.getBlockedList().isPresent())
                handleSynchronizeBlockedListMessage(syncMessage.getBlockedList().get());
            else if (syncMessage.getFetchType().isPresent())
                handleSynchronizeFetchMessage(syncMessage.getFetchType().get(), content.getTimestamp());
            else if (syncMessage.getMessageRequestResponse().isPresent())
                handleSynchronizeMessageRequestResponse(syncMessage.getMessageRequestResponse().get(), content.getTimestamp());
            else if (syncMessage.getOutgoingPaymentMessage().isPresent())
                handleSynchronizeOutgoingPayment(content, syncMessage.getOutgoingPaymentMessage().get());
            else if (syncMessage.getKeys().isPresent())
                handleSynchronizeKeys(syncMessage.getKeys().get(), content.getTimestamp());
            else if (syncMessage.getContacts().isPresent())
                handleSynchronizeContacts(syncMessage.getContacts().get(), content.getTimestamp());
            else
                warn(String.valueOf(content.getTimestamp()), "Contains no known sync types...");
        } else if (content.getCallMessage().isPresent()) {
            log(String.valueOf(content.getTimestamp()), "Got call message...");
            SignalServiceCallMessage message = content.getCallMessage().get();
            Optional<Integer> destinationDeviceId = message.getDestinationDeviceId();
            if (destinationDeviceId.isPresent() && destinationDeviceId.get() != SignalStore.account().getDeviceId()) {
                log(String.valueOf(content.getTimestamp()), String.format(Locale.US, "Ignoring call message that is not for this device! intended: %d, this: %d", destinationDeviceId.get(), SignalStore.account().getDeviceId()));
                return;
            }
            if (message.getOfferMessage().isPresent())
                handleCallOfferMessage(content, message.getOfferMessage().get(), smsMessageId, senderRecipient);
            else if (message.getAnswerMessage().isPresent())
                handleCallAnswerMessage(content, message.getAnswerMessage().get(), senderRecipient);
            else if (message.getIceUpdateMessages().isPresent())
                handleCallIceUpdateMessage(content, message.getIceUpdateMessages().get(), senderRecipient);
            else if (message.getHangupMessage().isPresent())
                handleCallHangupMessage(content, message.getHangupMessage().get(), smsMessageId, senderRecipient);
            else if (message.getBusyMessage().isPresent())
                handleCallBusyMessage(content, message.getBusyMessage().get(), senderRecipient);
            else if (message.getOpaqueMessage().isPresent())
                handleCallOpaqueMessage(content, message.getOpaqueMessage().get(), senderRecipient);
        } else if (content.getReceiptMessage().isPresent()) {
            SignalServiceReceiptMessage message = content.getReceiptMessage().get();
            if (message.isReadReceipt())
                handleReadReceipt(content, message, senderRecipient);
            else if (message.isDeliveryReceipt())
                handleDeliveryReceipt(content, message, senderRecipient);
            else if (message.isViewedReceipt())
                handleViewedReceipt(content, message, senderRecipient);
        } else if (content.getTypingMessage().isPresent()) {
            handleTypingMessage(content, content.getTypingMessage().get(), senderRecipient);
        } else if (content.getDecryptionErrorMessage().isPresent()) {
            handleRetryReceipt(content, content.getDecryptionErrorMessage().get(), senderRecipient);
        } else if (content.getSenderKeyDistributionMessage().isPresent()) {
        // Already handled, here in order to prevent unrecognized message log
        } else {
            warn(String.valueOf(content.getTimestamp()), "Got unrecognized message!");
        }
        resetRecipientToPush(senderRecipient);
        if (pending != null) {
            warn(content.getTimestamp(), "Pending retry was processed. Deleting.");
            ApplicationDependencies.getPendingRetryReceiptCache().delete(pending);
        }
    } catch (StorageFailedException e) {
        warn(String.valueOf(content.getTimestamp()), e);
        handleCorruptMessage(e.getSender(), e.getSenderDevice(), timestamp, smsMessageId);
    } catch (BadGroupIdException e) {
        warn(String.valueOf(content.getTimestamp()), "Ignoring message with bad group id", e);
    }
}
Also used : PendingRetryReceiptModel(org.thoughtcrime.securesms.database.model.PendingRetryReceiptModel) SignalServiceReceiptMessage(org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) GroupId(org.thoughtcrime.securesms.groups.GroupId) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) RefreshAttributesJob(org.thoughtcrime.securesms.jobs.RefreshAttributesJob) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceCallMessage(org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage) BadGroupIdException(org.thoughtcrime.securesms.groups.BadGroupIdException) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Example 8 with BadGroupIdException

use of org.thoughtcrime.securesms.groups.BadGroupIdException in project Signal-Android by signalapp.

the class MessageDecryptionUtil method toExceptionMetadata.

private static ExceptionMetadata toExceptionMetadata(@NonNull UnsupportedDataMessageException e) throws NoSenderException {
    String sender = e.getSender();
    if (sender == null)
        throw new NoSenderException();
    GroupId groupId = null;
    if (e.getGroup().isPresent()) {
        try {
            groupId = GroupUtil.idFromGroupContext(e.getGroup().get());
        } catch (BadGroupIdException ex) {
            Log.w(TAG, "Bad group id found in unsupported data message", ex);
        }
    }
    return new ExceptionMetadata(sender, e.getSenderDevice(), groupId);
}
Also used : ExceptionMetadata(org.thoughtcrime.securesms.messages.MessageContentProcessor.ExceptionMetadata) BadGroupIdException(org.thoughtcrime.securesms.groups.BadGroupIdException) GroupId(org.thoughtcrime.securesms.groups.GroupId)

Example 9 with BadGroupIdException

use of org.thoughtcrime.securesms.groups.BadGroupIdException in project Signal-Android by signalapp.

the class MessageDecryptionUtil method handleRetry.

@NonNull
private static Job handleRetry(@NonNull Context context, @NonNull Recipient sender, @NonNull SignalServiceEnvelope envelope, @NonNull ProtocolException protocolException) {
    ContentHint contentHint = ContentHint.fromType(protocolException.getContentHint());
    int senderDevice = protocolException.getSenderDevice();
    long receivedTimestamp = System.currentTimeMillis();
    Optional<GroupId> groupId = Optional.absent();
    if (protocolException.getGroupId().isPresent()) {
        try {
            groupId = Optional.of(GroupId.push(protocolException.getGroupId().get()));
        } catch (BadGroupIdException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] Bad groupId!");
        }
    }
    Log.w(TAG, "[" + envelope.getTimestamp() + "] Could not decrypt a message with a type of " + contentHint);
    long threadId;
    if (groupId.isPresent()) {
        Recipient groupRecipient = Recipient.externalPossiblyMigratedGroup(context, groupId.get());
        threadId = SignalDatabase.threads().getOrCreateThreadIdFor(groupRecipient);
    } else {
        threadId = SignalDatabase.threads().getOrCreateThreadIdFor(sender);
    }
    switch(contentHint) {
        case DEFAULT:
            Log.w(TAG, "[" + envelope.getTimestamp() + "] Inserting an error right away because it's " + contentHint);
            SignalDatabase.sms().insertBadDecryptMessage(sender.getId(), senderDevice, envelope.getTimestamp(), receivedTimestamp, threadId);
            break;
        case RESENDABLE:
            Log.w(TAG, "[" + envelope.getTimestamp() + "] Inserting into pending retries store because it's " + contentHint);
            ApplicationDependencies.getPendingRetryReceiptCache().insert(sender.getId(), senderDevice, envelope.getTimestamp(), receivedTimestamp, threadId);
            ApplicationDependencies.getPendingRetryReceiptManager().scheduleIfNecessary();
            break;
        case IMPLICIT:
            Log.w(TAG, "[" + envelope.getTimestamp() + "] Not inserting any error because it's " + contentHint);
            break;
    }
    byte[] originalContent;
    int envelopeType;
    if (protocolException.getUnidentifiedSenderMessageContent().isPresent()) {
        originalContent = protocolException.getUnidentifiedSenderMessageContent().get().getContent();
        envelopeType = protocolException.getUnidentifiedSenderMessageContent().get().getType();
    } else {
        originalContent = envelope.getContent();
        envelopeType = envelopeTypeToCiphertextMessageType(envelope.getType());
    }
    DecryptionErrorMessage decryptionErrorMessage = DecryptionErrorMessage.forOriginalMessage(originalContent, envelopeType, envelope.getTimestamp(), senderDevice);
    return new SendRetryReceiptJob(sender.getId(), groupId, decryptionErrorMessage);
}
Also used : DecryptionErrorMessage(org.whispersystems.libsignal.protocol.DecryptionErrorMessage) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SendRetryReceiptJob(org.thoughtcrime.securesms.jobs.SendRetryReceiptJob) Recipient(org.thoughtcrime.securesms.recipients.Recipient) BadGroupIdException(org.thoughtcrime.securesms.groups.BadGroupIdException) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) GroupId(org.thoughtcrime.securesms.groups.GroupId) NonNull(androidx.annotation.NonNull)

Example 10 with BadGroupIdException

use of org.thoughtcrime.securesms.groups.BadGroupIdException in project Signal-Android by WhisperSystems.

the class PushProcessMessageJob method createParameters.

@WorkerThread
@NonNull
private static Parameters createParameters(@Nullable SignalServiceContent content, @Nullable ExceptionMetadata exceptionMetadata) {
    Context context = ApplicationDependencies.getApplication();
    String queueName = QUEUE_PREFIX;
    Parameters.Builder builder = new Parameters.Builder().setMaxAttempts(Parameters.UNLIMITED);
    if (content != null) {
        SignalServiceGroupContext signalServiceGroupContext = GroupUtil.getGroupContextIfPresent(content);
        if (signalServiceGroupContext != null) {
            try {
                GroupId groupId = GroupUtil.idFromGroupContext(signalServiceGroupContext);
                queueName = getQueueName(Recipient.externalPossiblyMigratedGroup(context, groupId).getId());
                if (groupId.isV2()) {
                    int localRevision = SignalDatabase.groups().getGroupV2Revision(groupId.requireV2());
                    if (signalServiceGroupContext.getGroupV2().get().getRevision() > localRevision || SignalDatabase.groups().getGroupV1ByExpectedV2(groupId.requireV2()).isPresent()) {
                        Log.i(TAG, "Adding network constraint to group-related job.");
                        builder.addConstraint(NetworkConstraint.KEY).setLifespan(TimeUnit.DAYS.toMillis(30));
                    }
                }
            } catch (BadGroupIdException e) {
                Log.w(TAG, "Bad groupId! Using default queue. ID: " + content.getTimestamp());
            }
        } else if (content.getSyncMessage().isPresent() && content.getSyncMessage().get().getSent().isPresent() && content.getSyncMessage().get().getSent().get().getDestination().isPresent()) {
            queueName = getQueueName(RecipientId.fromHighTrust(content.getSyncMessage().get().getSent().get().getDestination().get()));
        } else {
            queueName = getQueueName(RecipientId.fromHighTrust(content.getSender()));
        }
    } else if (exceptionMetadata != null) {
        Recipient recipient = exceptionMetadata.getGroupId() != null ? Recipient.externalPossiblyMigratedGroup(context, exceptionMetadata.getGroupId()) : Recipient.external(context, exceptionMetadata.getSender());
        queueName = getQueueName(recipient.getId());
    }
    builder.setQueue(queueName);
    return builder.build();
}
Also used : Context(android.content.Context) SignalServiceGroupContext(org.whispersystems.signalservice.api.messages.SignalServiceGroupContext) Recipient(org.thoughtcrime.securesms.recipients.Recipient) BadGroupIdException(org.thoughtcrime.securesms.groups.BadGroupIdException) SignalServiceGroupContext(org.whispersystems.signalservice.api.messages.SignalServiceGroupContext) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) GroupId(org.thoughtcrime.securesms.groups.GroupId) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

BadGroupIdException (org.thoughtcrime.securesms.groups.BadGroupIdException)16 GroupId (org.thoughtcrime.securesms.groups.GroupId)10 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 NonNull (androidx.annotation.NonNull)6 ContentValues (android.content.ContentValues)2 Context (android.content.Context)2 TextView (android.widget.TextView)2 WorkerThread (androidx.annotation.WorkerThread)2 Toolbar (androidx.appcompat.widget.Toolbar)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 InvalidInputException (org.signal.zkgroup.InvalidInputException)2 GroupMasterKey (org.signal.zkgroup.groups.GroupMasterKey)2 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)2 SyncMessageId (org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)2 MessageId (org.thoughtcrime.securesms.database.model.MessageId)2 PendingRetryReceiptModel (org.thoughtcrime.securesms.database.model.PendingRetryReceiptModel)2 Data (org.thoughtcrime.securesms.jobmanager.Data)2 NetworkConstraint (org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint)2 RefreshAttributesJob (org.thoughtcrime.securesms.jobs.RefreshAttributesJob)2 SendRetryReceiptJob (org.thoughtcrime.securesms.jobs.SendRetryReceiptJob)2