Search in sources :

Example 1 with IncomingPreKeyBundleMessage

use of org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage in project Signal-Android by WhisperSystems.

the class PushDecryptJob method handleUntrustedIdentityMessage.

private void handleUntrustedIdentityMessage(@NonNull MasterSecretUnion masterSecret, @NonNull SignalServiceEnvelope envelope, @NonNull Optional<Long> smsMessageId) {
    try {
        EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
        Recipients recipients = RecipientFactory.getRecipientsFromString(context, envelope.getSource(), false);
        long recipientId = recipients.getPrimaryRecipient().getRecipientId();
        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(envelope.getSource(), envelope.getSourceDevice(), envelope.getTimestamp(), encoded, Optional.<SignalServiceGroup>absent(), 0);
        if (!smsMessageId.isPresent()) {
            IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded, envelope.hasLegacyMessage());
            Optional<InsertResult> insertResult = database.insertMessageInbox(masterSecret, bundleMessage);
            if (insertResult.isPresent()) {
                database.setMismatchedIdentity(insertResult.get().getMessageId(), recipientId, identityKey);
                MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), insertResult.get().getThreadId());
            }
        } else {
            database.updateMessageBody(masterSecret, smsMessageId.get(), encoded);
            database.markAsPreKeyBundle(smsMessageId.get());
            database.setMismatchedIdentity(smsMessageId.get(), recipientId, 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) Recipients(org.thoughtcrime.securesms.recipients.Recipients) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) EncryptingSmsDatabase(org.thoughtcrime.securesms.database.EncryptingSmsDatabase) IncomingPreKeyBundleMessage(org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage)

Example 2 with IncomingPreKeyBundleMessage

use of org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage 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)

Aggregations

InsertResult (org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult)2 IncomingPreKeyBundleMessage (org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage)2 IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)2 IdentityKey (org.whispersystems.libsignal.IdentityKey)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 InvalidVersionException (org.whispersystems.libsignal.InvalidVersionException)2 PreKeySignalMessage (org.whispersystems.libsignal.protocol.PreKeySignalMessage)2 Address (org.thoughtcrime.securesms.database.Address)1 EncryptingSmsDatabase (org.thoughtcrime.securesms.database.EncryptingSmsDatabase)1 SmsDatabase (org.thoughtcrime.securesms.database.SmsDatabase)1 Recipients (org.thoughtcrime.securesms.recipients.Recipients)1 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)1