Search in sources :

Example 11 with InvalidMessageException

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

the class ConversationActivity method sendMessage.

private void sendMessage() {
    try {
        Recipients recipients = getRecipients();
        if (recipients == null) {
            throw new RecipientFormattingException("Badly formatted");
        }
        boolean forceSms = sendButton.isManualSelection() && sendButton.getSelectedTransport().isSms();
        int subscriptionId = sendButton.getSelectedTransport().getSimSubscriptionId().or(-1);
        long expiresIn = recipients.getExpireMessages() * 1000;
        Log.w(TAG, "isManual Selection: " + sendButton.isManualSelection());
        Log.w(TAG, "forceSms: " + forceSms);
        if ((!recipients.isSingleRecipient() || recipients.isEmailRecipient()) && !isMmsEnabled) {
            handleManualMmsRequired();
        } else if (attachmentManager.isAttachmentPresent() || !recipients.isSingleRecipient() || recipients.isGroupRecipient() || recipients.isEmailRecipient()) {
            sendMediaMessage(forceSms, expiresIn, subscriptionId);
        } else {
            sendTextMessage(forceSms, expiresIn, subscriptionId);
        }
    } catch (RecipientFormattingException ex) {
        Toast.makeText(ConversationActivity.this, R.string.ConversationActivity_recipient_is_not_a_valid_sms_or_email_address_exclamation, Toast.LENGTH_LONG).show();
        Log.w(TAG, ex);
    } catch (InvalidMessageException ex) {
        Toast.makeText(ConversationActivity.this, R.string.ConversationActivity_message_is_empty_exclamation, Toast.LENGTH_SHORT).show();
        Log.w(TAG, ex);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) Recipients(org.thoughtcrime.securesms.recipients.Recipients) RecipientFormattingException(org.thoughtcrime.securesms.recipients.RecipientFormattingException)

Example 12 with InvalidMessageException

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

the class MasterCipher method decryptBytes.

public byte[] decryptBytes(@NonNull byte[] decodedBody) throws InvalidMessageException {
    try {
        Mac mac = getMac(masterSecret.getMacKey());
        byte[] encryptedBody = verifyMacBody(mac, decodedBody);
        Cipher cipher = getDecryptingCipher(masterSecret.getEncryptionKey(), encryptedBody);
        byte[] encrypted = getDecryptedBody(cipher, encryptedBody);
        return encrypted;
    } catch (GeneralSecurityException ge) {
        throw new InvalidMessageException(ge);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) Mac(javax.crypto.Mac)

Example 13 with InvalidMessageException

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

the class MasterCipher method verifyMacBody.

private byte[] verifyMacBody(@NonNull Mac hmac, @NonNull byte[] encryptedAndMac) throws InvalidMessageException {
    if (encryptedAndMac.length < hmac.getMacLength()) {
        throw new InvalidMessageException("length(encrypted body + MAC) < length(MAC)");
    }
    byte[] encrypted = new byte[encryptedAndMac.length - hmac.getMacLength()];
    System.arraycopy(encryptedAndMac, 0, encrypted, 0, encrypted.length);
    byte[] remoteMac = new byte[hmac.getMacLength()];
    System.arraycopy(encryptedAndMac, encryptedAndMac.length - remoteMac.length, remoteMac, 0, remoteMac.length);
    byte[] localMac = hmac.doFinal(encrypted);
    if (!Arrays.equals(remoteMac, localMac))
        throw new InvalidMessageException("MAC doesen't match.");
    return encrypted;
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException)

Example 14 with InvalidMessageException

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

the class AsymmetricMasterCipher method decryptBytes.

public byte[] decryptBytes(byte[] combined) throws IOException, InvalidMessageException {
    try {
        byte[][] parts = Util.split(combined, PublicKey.KEY_SIZE, combined.length - PublicKey.KEY_SIZE);
        PublicKey theirPublicKey = new PublicKey(parts[0], 0);
        ECPrivateKey ourPrivateKey = asymmetricMasterSecret.getPrivateKey();
        byte[] secret = Curve.calculateAgreement(theirPublicKey.getKey(), ourPrivateKey);
        MasterCipher masterCipher = getMasterCipherForSecret(secret);
        return masterCipher.decryptBytes(parts[1]);
    } catch (InvalidKeyException e) {
        throw new InvalidMessageException(e);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 15 with InvalidMessageException

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

the class ConversationActivity method onRecorderFinished.

@Override
public void onRecorderFinished() {
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(20);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    ListenableFuture<Pair<Uri, Long>> future = audioRecorder.stopRecording();
    future.addListener(new ListenableFuture.Listener<Pair<Uri, Long>>() {

        @Override
        public void onSuccess(@NonNull final Pair<Uri, Long> result) {
            try {
                boolean forceSms = sendButton.isManualSelection() && sendButton.getSelectedTransport().isSms();
                int subscriptionId = sendButton.getSelectedTransport().getSimSubscriptionId().or(-1);
                long expiresIn = recipients.getExpireMessages() * 1000;
                AudioSlide audioSlide = new AudioSlide(ConversationActivity.this, result.first, result.second, ContentType.AUDIO_AAC);
                SlideDeck slideDeck = new SlideDeck();
                slideDeck.addSlide(audioSlide);
                sendMediaMessage(forceSms, "", slideDeck, expiresIn, subscriptionId).addListener(new AssertedSuccessListener<Void>() {

                    @Override
                    public void onSuccess(Void nothing) {
                        new AsyncTask<Void, Void, Void>() {

                            @Override
                            protected Void doInBackground(Void... params) {
                                PersistentBlobProvider.getInstance(ConversationActivity.this).delete(result.first);
                                return null;
                            }
                        }.execute();
                    }
                });
            } catch (InvalidMessageException e) {
                Log.w(TAG, e);
                Toast.makeText(ConversationActivity.this, R.string.ConversationActivity_error_sending_voice_message, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(ExecutionException e) {
            Toast.makeText(ConversationActivity.this, R.string.ConversationActivity_unable_to_record_audio, Toast.LENGTH_LONG).show();
        }
    });
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) AudioSlide(org.thoughtcrime.securesms.mms.AudioSlide) SlideDeck(org.thoughtcrime.securesms.mms.SlideDeck) Uri(android.net.Uri) AssertedSuccessListener(org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener) ListenableFuture(org.thoughtcrime.securesms.util.concurrent.ListenableFuture) Vibrator(android.os.Vibrator) ExecutionException(java.util.concurrent.ExecutionException) Pair(android.util.Pair)

Aggregations

InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)18 IOException (java.io.IOException)7 File (java.io.File)4 InputStream (java.io.InputStream)3 MasterCipher (org.thoughtcrime.securesms.crypto.MasterCipher)3 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)3 MmsException (ws.com.google.android.mms.MmsException)3 Cursor (android.database.Cursor)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 Pair (android.util.Pair)2 FileInputStream (java.io.FileInputStream)2 LinkedList (java.util.LinkedList)2 AsymmetricMasterSecret (org.thoughtcrime.securesms.crypto.AsymmetricMasterSecret)2 EncryptingSmsDatabase (org.thoughtcrime.securesms.database.EncryptingSmsDatabase)2 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)2 MmsDatabase (org.thoughtcrime.securesms.database.MmsDatabase)2 Recipients (org.thoughtcrime.securesms.recipients.Recipients)2 DuplicateMessageException (org.whispersystems.libsignal.DuplicateMessageException)2 IdentityKey (org.whispersystems.libsignal.IdentityKey)2 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)2