/**
         * Decrypt a received {@link org.whispersystems.textsecure.api.messages.TextSecureEnvelope}
         *
         * @param envelope The received TextSecureEnvelope
         * @return a decrypted TextSecureMessage
         * @throws InvalidVersionException
         * @throws InvalidMessageException
         * @throws InvalidKeyException
         * @throws DuplicateMessageException
         * @throws InvalidKeyIdException
         * @throws UntrustedIdentityException
         * @throws LegacyMessageException
         * @throws NoSessionException
         */
        public TextSecureContent decrypt(TextSecureEnvelope envelope)
        {
            try
            {
                TextSecureContent content = new TextSecureContent();

                if (envelope.hasLegacyMessage())
                {
                    DataMessage message = DataMessage.ParseFrom(decrypt(envelope, envelope.getLegacyMessage()));
                    content = new TextSecureContent(createTextSecureMessage(envelope, message));
                }
                else if (envelope.hasContent())
                {
                    Content message = Content.ParseFrom(decrypt(envelope, envelope.getContent()));

                    if (message.HasDataMessage)
                    {
                        content = new TextSecureContent(createTextSecureMessage(envelope, message.DataMessage));
                    }
                    else if (message.HasSyncMessage && localAddress.getNumber().Equals(envelope.getSource()))
                    {
                        content = new TextSecureContent(createSynchronizeMessage(envelope, message.SyncMessage));
                    }
                }

                return(content);
            }
            catch (InvalidProtocolBufferException e)
            {
                throw new InvalidMessageException(e);
            }
        }
Beispiel #2
0
        private void handleMessage(/*MasterSecretUnion masterSecret, */ TextSecureEnvelope envelope /*, Optional<Long> smsMessageId*/)
        {
            try
            {
                AxolotlStore      axolotlStore = new TextSecureAxolotlStore();
                TextSecureAddress localAddress = new TextSecureAddress(TextSecurePreferences.getLocalNumber());
                TextSecureCipher  cipher       = new TextSecureCipher(localAddress, axolotlStore);

                TextSecureContent content = cipher.decrypt(envelope);

                if (content.getDataMessage().HasValue)
                {
                    TextSecureDataMessage message = content.getDataMessage().ForceGetValue();

                    if (message.isEndSession())
                    {
                        handleEndSessionMessage(/*masterSecret, */ envelope, message /*, smsMessageId*/);
                    }
                    //else if (message.isGroupUpdate()) handleGroupMessage(masterSecret, envelope, message, smsMessageId);
                    //else if (message.getAttachments().isPresent()) handleMediaMessage(masterSecret, envelope, message, smsMessageId);
                    else
                    {
                        handleTextMessage(/*masterSecret, */ envelope, message /*, smsMessageId*/);
                    }
                }

                /*else if (content.getSyncMessage().HasValue) TODO: SYNC enable
                 * {
                 *  TextSecureSyncMessage syncMessage = content.getSyncMessage().ForceGetValue();
                 *
                 *  if (syncMessage.getSent().HasValue) handleSynchronizeSentMessage(masterSecret, syncMessage.getSent().ForceGetValue(), smsMessageId);
                 *  else if (syncMessage.getRequest().HasValue) handleSynchronizeRequestMessage(masterSecret, syncMessage.getRequest().ForceGetValue());
                 * }*/

                if (envelope.isPreKeyWhisperMessage())
                {
                    App.Current.Worker.AddTaskActivities(new RefreshPreKeysTask());
                    //ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob(context));
                }
            }
            catch (InvalidVersionException e)
            {
                //Log.w(TAG, e);
                //handleInvalidVersionMessage(masterSecret, envelope, smsMessageId);
            }

            /* catch (InvalidMessageException | InvalidKeyIdException | InvalidKeyException | MmsException e) {
             *   Log.w(TAG, e);
             *   handleCorruptMessage(masterSecret, envelope, smsMessageId);
             * } catch (NoSessionException e)
             * {
             *   Log.w(TAG, e);
             *   handleNoSessionMessage(masterSecret, envelope, smsMessageId);
             * }
             * catch (LegacyMessageException e)
             * {
             *   Log.w(TAG, e);
             *   handleLegacyMessage(masterSecret, envelope, smsMessageId);
             * }
             * catch (DuplicateMessageException e)
             * {
             *   Log.w(TAG, e);
             *   handleDuplicateMessage(masterSecret, envelope, smsMessageId);
             * }
             * catch (UntrustedIdentityException e)
             * {
             *   Log.w(TAG, e);
             *   handleUntrustedIdentityMessage(masterSecret, envelope, smsMessageId);
             * }*/
        }