Search in sources :

Example 6 with InvalidVersionException

use of org.whispersystems.libsignal.InvalidVersionException in project Signal-Android by WhisperSystems.

the class PushContentReceiveJob method onRun.

@Override
public void onRun() {
    try {
        String sessionKey = TextSecurePreferences.getSignalingKey(context);
        SignalServiceEnvelope envelope = new SignalServiceEnvelope(data, sessionKey);
        handle(envelope, true);
    } catch (IOException | InvalidVersionException e) {
        Log.w(TAG, e);
    }
}
Also used : SignalServiceEnvelope(org.whispersystems.signalservice.api.messages.SignalServiceEnvelope) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) IOException(java.io.IOException)

Example 7 with InvalidVersionException

use of org.whispersystems.libsignal.InvalidVersionException in project Signal-Android by signalapp.

the class PushContentReceiveJob method onRun.

@Override
public void onRun() {
    try {
        String sessionKey = TextSecurePreferences.getSignalingKey(context);
        SignalServiceEnvelope envelope = new SignalServiceEnvelope(data, sessionKey);
        handle(envelope);
    } catch (IOException | InvalidVersionException e) {
        Log.w(TAG, e);
    }
}
Also used : SignalServiceEnvelope(org.whispersystems.signalservice.api.messages.SignalServiceEnvelope) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) IOException(java.io.IOException)

Example 8 with InvalidVersionException

use of org.whispersystems.libsignal.InvalidVersionException in project Signal-Android by signalapp.

the class PushDecryptJob method handleMessage.

private void handleMessage(SignalServiceEnvelope envelope, Optional<Long> smsMessageId) {
    try {
        GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
        SignalProtocolStore axolotlStore = new SignalProtocolStoreImpl(context);
        SignalServiceAddress localAddress = new SignalServiceAddress(TextSecurePreferences.getLocalNumber(context));
        SignalServiceCipher cipher = new SignalServiceCipher(localAddress, axolotlStore);
        SignalServiceContent content = cipher.decrypt(envelope);
        if (content.getDataMessage().isPresent()) {
            SignalServiceDataMessage message = content.getDataMessage().get();
            if (message.isEndSession())
                handleEndSessionMessage(envelope, message, smsMessageId);
            else if (message.isGroupUpdate())
                handleGroupMessage(envelope, message, smsMessageId);
            else if (message.isExpirationUpdate())
                handleExpirationUpdate(envelope, message, smsMessageId);
            else if (message.getAttachments().isPresent())
                handleMediaMessage(envelope, message, smsMessageId);
            else if (message.getBody().isPresent())
                handleTextMessage(envelope, message, smsMessageId);
            if (message.getGroupInfo().isPresent() && groupDatabase.isUnknownGroup(GroupUtil.getEncodedId(message.getGroupInfo().get().getGroupId(), false))) {
                handleUnknownGroupMessage(envelope, message.getGroupInfo().get());
            }
            if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
                handleProfileKey(envelope, message);
            }
        } else if (content.getSyncMessage().isPresent()) {
            SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
            if (syncMessage.getSent().isPresent())
                handleSynchronizeSentMessage(envelope, syncMessage.getSent().get());
            else if (syncMessage.getRequest().isPresent())
                handleSynchronizeRequestMessage(syncMessage.getRequest().get());
            else if (syncMessage.getRead().isPresent())
                handleSynchronizeReadMessage(syncMessage.getRead().get(), envelope.getTimestamp());
            else if (syncMessage.getVerified().isPresent())
                handleSynchronizeVerifiedMessage(syncMessage.getVerified().get());
            else
                Log.w(TAG, "Contains no known sync types...");
        } else if (content.getCallMessage().isPresent()) {
            Log.w(TAG, "Got call message...");
            SignalServiceCallMessage message = content.getCallMessage().get();
            if (message.getOfferMessage().isPresent())
                handleCallOfferMessage(envelope, message.getOfferMessage().get(), smsMessageId);
            else if (message.getAnswerMessage().isPresent())
                handleCallAnswerMessage(envelope, message.getAnswerMessage().get());
            else if (message.getIceUpdateMessages().isPresent())
                handleCallIceUpdateMessage(envelope, message.getIceUpdateMessages().get());
            else if (message.getHangupMessage().isPresent())
                handleCallHangupMessage(envelope, message.getHangupMessage().get(), smsMessageId);
            else if (message.getBusyMessage().isPresent())
                handleCallBusyMessage(envelope, message.getBusyMessage().get());
        } else if (content.getReceiptMessage().isPresent()) {
            SignalServiceReceiptMessage message = content.getReceiptMessage().get();
            if (message.isReadReceipt())
                handleReadReceipt(envelope, message);
            else if (message.isDeliveryReceipt())
                handleDeliveryReceipt(envelope, message);
        } else {
            Log.w(TAG, "Got unrecognized message...");
        }
        if (envelope.isPreKeySignalMessage()) {
            ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob(context));
        }
    } catch (InvalidVersionException e) {
        Log.w(TAG, e);
        handleInvalidVersionMessage(envelope, smsMessageId);
    } catch (InvalidMessageException | InvalidKeyIdException | InvalidKeyException | MmsException e) {
        Log.w(TAG, e);
        handleCorruptMessage(envelope, smsMessageId);
    } catch (NoSessionException e) {
        Log.w(TAG, e);
        handleNoSessionMessage(envelope, smsMessageId);
    } catch (LegacyMessageException e) {
        Log.w(TAG, e);
        handleLegacyMessage(envelope, smsMessageId);
    } catch (DuplicateMessageException e) {
        Log.w(TAG, e);
        handleDuplicateMessage(envelope, smsMessageId);
    } catch (UntrustedIdentityException e) {
        Log.w(TAG, e);
        handleUntrustedIdentityMessage(envelope, smsMessageId);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) SignalServiceReceiptMessage(org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) SignalServiceContent(org.whispersystems.signalservice.api.messages.SignalServiceContent) NoSessionException(org.whispersystems.libsignal.NoSessionException) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SignalProtocolStore(org.whispersystems.libsignal.state.SignalProtocolStore) MmsException(org.thoughtcrime.securesms.mms.MmsException) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) SignalProtocolStoreImpl(org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalServiceCallMessage(org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException)

Example 9 with InvalidVersionException

use of org.whispersystems.libsignal.InvalidVersionException in project Signal-Android by signalapp.

the class PushDecryptJob method handleUntrustedIdentityMessage.

private void handleUntrustedIdentityMessage(@NonNull SignalServiceEnvelope envelope, @NonNull Optional<Long> smsMessageId) {
    try {
        SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
        Address sourceAddress = Address.fromExternal(context, envelope.getSource());
        byte[] serialized = envelope.hasLegacyMessage() ? envelope.getLegacyMessage() : envelope.getContent();
        PreKeySignalMessage whisperMessage = new PreKeySignalMessage(serialized);
        IdentityKey identityKey = whisperMessage.getIdentityKey();
        String encoded = Base64.encodeBytes(serialized);
        IncomingTextMessage textMessage = new IncomingTextMessage(sourceAddress, envelope.getSourceDevice(), envelope.getTimestamp(), encoded, Optional.absent(), 0);
        if (!smsMessageId.isPresent()) {
            IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded, envelope.hasLegacyMessage());
            Optional<InsertResult> insertResult = database.insertMessageInbox(bundleMessage);
            if (insertResult.isPresent()) {
                database.setMismatchedIdentity(insertResult.get().getMessageId(), sourceAddress, identityKey);
                MessageNotifier.updateNotification(context, insertResult.get().getThreadId());
            }
        } else {
            database.updateMessageBody(smsMessageId.get(), encoded);
            database.markAsPreKeyBundle(smsMessageId.get());
            database.setMismatchedIdentity(smsMessageId.get(), sourceAddress, identityKey);
        }
    } catch (InvalidMessageException | InvalidVersionException e) {
        throw new AssertionError(e);
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IdentityKey(org.whispersystems.libsignal.IdentityKey) Address(org.thoughtcrime.securesms.database.Address) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) IncomingPreKeyBundleMessage(org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase)

Example 10 with InvalidVersionException

use of org.whispersystems.libsignal.InvalidVersionException in project Conversations by siacs.

the class XmppAxolotlSession method processReceiving.

@Nullable
byte[] processReceiving(List<AxolotlKey> possibleKeys) throws CryptoFailedException {
    byte[] plaintext = null;
    FingerprintStatus status = getTrust();
    if (!status.isCompromised()) {
        Iterator<AxolotlKey> iterator = possibleKeys.iterator();
        while (iterator.hasNext()) {
            AxolotlKey encryptedKey = iterator.next();
            try {
                if (encryptedKey.prekey) {
                    PreKeySignalMessage preKeySignalMessage = new PreKeySignalMessage(encryptedKey.key);
                    Optional<Integer> optionalPreKeyId = preKeySignalMessage.getPreKeyId();
                    IdentityKey identityKey = preKeySignalMessage.getIdentityKey();
                    if (!optionalPreKeyId.isPresent()) {
                        if (iterator.hasNext()) {
                            continue;
                        }
                        throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId");
                    }
                    preKeyId = optionalPreKeyId.get();
                    if (this.identityKey != null && !this.identityKey.equals(identityKey)) {
                        if (iterator.hasNext()) {
                            continue;
                        }
                        throw new CryptoFailedException("Received PreKeyWhisperMessage but preexisting identity key changed.");
                    }
                    this.identityKey = identityKey;
                    plaintext = cipher.decrypt(preKeySignalMessage);
                } else {
                    SignalMessage signalMessage = new SignalMessage(encryptedKey.key);
                    try {
                        plaintext = cipher.decrypt(signalMessage);
                    } catch (InvalidMessageException | NoSessionException e) {
                        if (iterator.hasNext()) {
                            Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring crypto exception because possible keys left to try", e);
                            continue;
                        }
                        throw new BrokenSessionException(this.remoteAddress, e);
                    }
                    // better safe than sorry because we use that to do special after prekey handling
                    preKeyId = null;
                }
            } catch (InvalidVersionException | InvalidKeyException | LegacyMessageException | InvalidMessageException | DuplicateMessageException | InvalidKeyIdException | UntrustedIdentityException e) {
                if (iterator.hasNext()) {
                    Log.w(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring crypto exception because possible keys left to try", e);
                    continue;
                }
                throw new CryptoFailedException("Error decrypting SignalMessage", e);
            }
            if (iterator.hasNext()) {
                break;
            }
        }
        if (!status.isActive()) {
            setTrust(status.toActive());
        // TODO: also (re)add to device list?
        }
    } else {
        throw new CryptoFailedException("not encrypting omemo message from fingerprint " + getFingerprint() + " because it was marked as compromised");
    }
    return plaintext;
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IdentityKey(org.whispersystems.libsignal.IdentityKey) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) SignalMessage(org.whispersystems.libsignal.protocol.SignalMessage) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) NoSessionException(org.whispersystems.libsignal.NoSessionException) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException) Nullable(androidx.annotation.Nullable)

Aggregations

InvalidVersionException (org.whispersystems.libsignal.InvalidVersionException)12 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)10 DuplicateMessageException (org.whispersystems.libsignal.DuplicateMessageException)8 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)8 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)8 LegacyMessageException (org.whispersystems.libsignal.LegacyMessageException)8 UntrustedIdentityException (org.whispersystems.libsignal.UntrustedIdentityException)8 PreKeySignalMessage (org.whispersystems.libsignal.protocol.PreKeySignalMessage)8 NoSessionException (org.whispersystems.libsignal.NoSessionException)7 SignalMessage (org.whispersystems.libsignal.protocol.SignalMessage)6 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)6 IdentityKey (org.whispersystems.libsignal.IdentityKey)5 SessionCipher (org.whispersystems.libsignal.SessionCipher)4 InvalidMetadataMessageException (org.signal.libsignal.metadata.InvalidMetadataMessageException)3 ProtocolDuplicateMessageException (org.signal.libsignal.metadata.ProtocolDuplicateMessageException)3 ProtocolInvalidKeyException (org.signal.libsignal.metadata.ProtocolInvalidKeyException)3 ProtocolInvalidKeyIdException (org.signal.libsignal.metadata.ProtocolInvalidKeyIdException)3 ProtocolInvalidMessageException (org.signal.libsignal.metadata.ProtocolInvalidMessageException)3 ProtocolInvalidVersionException (org.signal.libsignal.metadata.ProtocolInvalidVersionException)3 ProtocolLegacyMessageException (org.signal.libsignal.metadata.ProtocolLegacyMessageException)3