Search in sources :

Example 1 with AutomaticSessionResetJob

use of org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob in project Signal-Android by WhisperSystems.

the class MessageContentProcessor method handleExceptionMessage.

private void handleExceptionMessage(@NonNull MessageState messageState, @NonNull ExceptionMetadata e, long timestamp, @NonNull Optional<Long> smsMessageId) {
    Recipient sender = Recipient.external(context, e.sender);
    if (sender.isBlocked()) {
        warn("Ignoring exception content from blocked sender, message state:" + messageState);
        return;
    }
    switch(messageState) {
        case INVALID_VERSION:
            warn(String.valueOf(timestamp), "Handling invalid version.");
            handleInvalidVersionMessage(e.sender, e.senderDevice, timestamp, smsMessageId);
            break;
        case LEGACY_MESSAGE:
            warn(String.valueOf(timestamp), "Handling legacy message.");
            handleLegacyMessage(e.sender, e.senderDevice, timestamp, smsMessageId);
            break;
        case DUPLICATE_MESSAGE:
            warn(String.valueOf(timestamp), "Duplicate message. Dropping.");
            break;
        case UNSUPPORTED_DATA_MESSAGE:
            warn(String.valueOf(timestamp), "Handling unsupported data message.");
            handleUnsupportedDataMessage(e.sender, e.senderDevice, Optional.fromNullable(e.groupId), timestamp, smsMessageId);
            break;
        case CORRUPT_MESSAGE:
        case NO_SESSION:
            warn(String.valueOf(timestamp), "Discovered old enqueued bad encrypted message. Scheduling reset.");
            ApplicationDependencies.getJobManager().add(new AutomaticSessionResetJob(sender.getId(), e.senderDevice, timestamp));
            break;
        default:
            throw new AssertionError("Not handled " + messageState + ". (" + timestamp + ")");
    }
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) AutomaticSessionResetJob(org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob)

Example 2 with AutomaticSessionResetJob

use of org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob in project Signal-Android by WhisperSystems.

the class MessageDecryptionUtil method decrypt.

/**
 * Takes a {@link SignalServiceEnvelope} and returns a {@link DecryptionResult}, which has either
 * a plaintext {@link SignalServiceContent} or information about an error that happened.
 *
 * Excluding the data updated in our protocol stores that results from decrypting a message, this
 * method is side-effect free, preferring to return the decryption results to be handled by the
 * caller.
 */
@NonNull
public static DecryptionResult decrypt(@NonNull Context context, @NonNull SignalServiceEnvelope envelope) {
    SignalServiceAccountDataStore protocolStore = ApplicationDependencies.getProtocolStore().aci();
    SignalServiceAddress localAddress = new SignalServiceAddress(Recipient.self().requireServiceId(), Recipient.self().requireE164());
    SignalServiceCipher cipher = new SignalServiceCipher(localAddress, SignalStore.account().getDeviceId(), protocolStore, ReentrantSessionLock.INSTANCE, UnidentifiedAccessUtil.getCertificateValidator());
    List<Job> jobs = new LinkedList<>();
    if (envelope.isPreKeySignalMessage()) {
        jobs.add(new RefreshPreKeysJob());
    }
    try {
        try {
            return DecryptionResult.forSuccess(cipher.decrypt(envelope), jobs);
        } catch (ProtocolInvalidVersionException e) {
            Log.w(TAG, String.valueOf(envelope.getTimestamp()), e);
            return DecryptionResult.forError(MessageState.INVALID_VERSION, toExceptionMetadata(e), jobs);
        } catch (ProtocolInvalidKeyIdException | ProtocolInvalidKeyException | ProtocolUntrustedIdentityException | ProtocolNoSessionException | ProtocolInvalidMessageException e) {
            Log.w(TAG, String.valueOf(envelope.getTimestamp()), e);
            Recipient sender = Recipient.external(context, e.getSender());
            if (sender.supportsMessageRetries() && Recipient.self().supportsMessageRetries() && FeatureFlags.retryReceipts()) {
                jobs.add(handleRetry(context, sender, envelope, e));
                postInternalErrorNotification(context);
            } else {
                jobs.add(new AutomaticSessionResetJob(sender.getId(), e.getSenderDevice(), envelope.getTimestamp()));
            }
            return DecryptionResult.forNoop(jobs);
        } catch (ProtocolLegacyMessageException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forError(MessageState.LEGACY_MESSAGE, toExceptionMetadata(e), jobs);
        } catch (ProtocolDuplicateMessageException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forError(MessageState.DUPLICATE_MESSAGE, toExceptionMetadata(e), jobs);
        } catch (InvalidMetadataVersionException | InvalidMetadataMessageException | InvalidMessageStructureException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forNoop(jobs);
        } catch (SelfSendException e) {
            Log.i(TAG, "Dropping UD message from self.");
            return DecryptionResult.forNoop(jobs);
        } catch (UnsupportedDataMessageException e) {
            Log.w(TAG, "[" + envelope.getTimestamp() + "] " + envelope.getSourceIdentifier() + ":" + envelope.getSourceDevice(), e);
            return DecryptionResult.forError(MessageState.UNSUPPORTED_DATA_MESSAGE, toExceptionMetadata(e), jobs);
        }
    } catch (NoSenderException e) {
        Log.w(TAG, "Invalid message, but no sender info!");
        return DecryptionResult.forNoop(jobs);
    }
}
Also used : ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) ProtocolUntrustedIdentityException(org.signal.libsignal.metadata.ProtocolUntrustedIdentityException) ProtocolInvalidVersionException(org.signal.libsignal.metadata.ProtocolInvalidVersionException) InvalidMessageStructureException(org.whispersystems.signalservice.api.InvalidMessageStructureException) SelfSendException(org.signal.libsignal.metadata.SelfSendException) ProtocolInvalidKeyIdException(org.signal.libsignal.metadata.ProtocolInvalidKeyIdException) ProtocolDuplicateMessageException(org.signal.libsignal.metadata.ProtocolDuplicateMessageException) UnsupportedDataMessageException(org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) ProtocolLegacyMessageException(org.signal.libsignal.metadata.ProtocolLegacyMessageException) SignalServiceAccountDataStore(org.whispersystems.signalservice.api.SignalServiceAccountDataStore) AutomaticSessionResetJob(org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob) RefreshPreKeysJob(org.thoughtcrime.securesms.jobs.RefreshPreKeysJob) SendRetryReceiptJob(org.thoughtcrime.securesms.jobs.SendRetryReceiptJob) Job(org.thoughtcrime.securesms.jobmanager.Job) RefreshPreKeysJob(org.thoughtcrime.securesms.jobs.RefreshPreKeysJob) ProtocolNoSessionException(org.signal.libsignal.metadata.ProtocolNoSessionException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) Recipient(org.thoughtcrime.securesms.recipients.Recipient) AutomaticSessionResetJob(org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob) LinkedList(java.util.LinkedList) InvalidMetadataMessageException(org.signal.libsignal.metadata.InvalidMetadataMessageException) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) InvalidMetadataVersionException(org.signal.libsignal.metadata.InvalidMetadataVersionException) NonNull(androidx.annotation.NonNull)

Aggregations

AutomaticSessionResetJob (org.thoughtcrime.securesms.jobs.AutomaticSessionResetJob)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 NonNull (androidx.annotation.NonNull)1 LinkedList (java.util.LinkedList)1 InvalidMetadataMessageException (org.signal.libsignal.metadata.InvalidMetadataMessageException)1 InvalidMetadataVersionException (org.signal.libsignal.metadata.InvalidMetadataVersionException)1 ProtocolDuplicateMessageException (org.signal.libsignal.metadata.ProtocolDuplicateMessageException)1 ProtocolInvalidKeyException (org.signal.libsignal.metadata.ProtocolInvalidKeyException)1 ProtocolInvalidKeyIdException (org.signal.libsignal.metadata.ProtocolInvalidKeyIdException)1 ProtocolInvalidMessageException (org.signal.libsignal.metadata.ProtocolInvalidMessageException)1 ProtocolInvalidVersionException (org.signal.libsignal.metadata.ProtocolInvalidVersionException)1 ProtocolLegacyMessageException (org.signal.libsignal.metadata.ProtocolLegacyMessageException)1 ProtocolNoSessionException (org.signal.libsignal.metadata.ProtocolNoSessionException)1 ProtocolUntrustedIdentityException (org.signal.libsignal.metadata.ProtocolUntrustedIdentityException)1 SelfSendException (org.signal.libsignal.metadata.SelfSendException)1 Job (org.thoughtcrime.securesms.jobmanager.Job)1 RefreshPreKeysJob (org.thoughtcrime.securesms.jobs.RefreshPreKeysJob)1 SendRetryReceiptJob (org.thoughtcrime.securesms.jobs.SendRetryReceiptJob)1 InvalidMessageStructureException (org.whispersystems.signalservice.api.InvalidMessageStructureException)1 SignalServiceAccountDataStore (org.whispersystems.signalservice.api.SignalServiceAccountDataStore)1