2012-12-24 16:40:37 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2013-11-10 12:15:29 +00:00
|
|
|
* Copyright (C) 2013 Open Whisper Systems
|
2012-12-24 16:40:37 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-12-24 16:40:37 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.crypto;
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
import android.content.Context;
|
2013-09-09 01:19:05 +00:00
|
|
|
import android.content.Intent;
|
2012-12-24 16:40:37 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-08-19 17:07:07 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.protocol.KeyExchangeMessage;
|
2014-04-10 03:02:46 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.LegacyMessageException;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2013-09-09 23:46:03 +00:00
|
|
|
import org.thoughtcrime.securesms.database.PushDatabase;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2013-07-19 00:42:45 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.TextTransport;
|
2013-02-08 19:57:54 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2013-09-09 01:19:05 +00:00
|
|
|
import org.thoughtcrime.securesms.service.PushReceiver;
|
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.SmsTransportDetails;
|
2013-09-09 01:19:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-03-19 19:37:46 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.DuplicateMessageException;
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.InvalidKeyException;
|
|
|
|
import org.whispersystems.textsecure.crypto.InvalidMessageException;
|
2013-08-22 00:25:19 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.InvalidVersionException;
|
2013-09-09 01:19:05 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.SessionCipher;
|
2013-09-09 01:19:05 +00:00
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
2014-02-03 03:38:06 +00:00
|
|
|
import org.whispersystems.textsecure.storage.RecipientDevice;
|
2013-11-26 01:00:20 +00:00
|
|
|
import org.whispersystems.textsecure.storage.Session;
|
2014-02-03 03:38:06 +00:00
|
|
|
import org.whispersystems.textsecure.storage.SessionRecordV2;
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.util.Hex;
|
2013-11-26 01:00:20 +00:00
|
|
|
import org.whispersystems.textsecure.util.Util;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import java.io.IOException;
|
2013-09-09 01:19:05 +00:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executors;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
import ws.com.google.android.mms.pdu.MultimediaMessagePdu;
|
|
|
|
import ws.com.google.android.mms.pdu.PduParser;
|
2013-04-26 01:59:49 +00:00
|
|
|
import ws.com.google.android.mms.pdu.RetrieveConf;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* A work queue for processing a number of encryption operations.
|
2012-12-24 16:40:37 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class DecryptingQueue {
|
|
|
|
|
2013-09-09 01:19:05 +00:00
|
|
|
private static final Executor executor = Executors.newSingleThreadExecutor();
|
2012-12-24 16:40:37 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public static void scheduleDecryption(Context context, MasterSecret masterSecret,
|
|
|
|
long messageId, long threadId, MultimediaMessagePdu mms)
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
MmsDecryptionItem runnable = new MmsDecryptionItem(context, masterSecret, messageId, threadId, mms);
|
2013-09-09 01:19:05 +00:00
|
|
|
executor.execute(runnable);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public static void scheduleDecryption(Context context, MasterSecret masterSecret,
|
2014-02-03 03:38:06 +00:00
|
|
|
long messageId, long threadId, String originator, int deviceId,
|
2014-02-19 21:46:49 +00:00
|
|
|
String body, boolean isSecureMessage, boolean isKeyExchange,
|
|
|
|
boolean isEndSession)
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
{
|
2013-05-15 21:09:13 +00:00
|
|
|
DecryptionWorkItem runnable = new DecryptionWorkItem(context, masterSecret, messageId, threadId,
|
2014-02-03 03:38:06 +00:00
|
|
|
originator, deviceId, body,
|
2014-02-19 21:46:49 +00:00
|
|
|
isSecureMessage, isKeyExchange, isEndSession);
|
2013-09-09 01:19:05 +00:00
|
|
|
executor.execute(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void scheduleDecryption(Context context, MasterSecret masterSecret,
|
|
|
|
long messageId, IncomingPushMessage message)
|
|
|
|
{
|
|
|
|
PushDecryptionWorkItem runnable = new PushDecryptionWorkItem(context, masterSecret,
|
|
|
|
messageId, message);
|
|
|
|
executor.execute(runnable);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static void schedulePendingDecrypts(Context context, MasterSecret masterSecret) {
|
|
|
|
Log.w("DecryptingQueue", "Processing pending decrypts...");
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-09-09 23:46:03 +00:00
|
|
|
EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
PushDatabase pushDatabase = DatabaseFactory.getPushDatabase(context);
|
|
|
|
|
|
|
|
EncryptingSmsDatabase.Reader smsReader = null;
|
|
|
|
PushDatabase.Reader pushReader = null;
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
SmsMessageRecord record;
|
2013-09-09 23:46:03 +00:00
|
|
|
IncomingPushMessage message;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2013-09-09 23:46:03 +00:00
|
|
|
smsReader = smsDatabase.getDecryptInProgressMessages(masterSecret);
|
|
|
|
pushReader = pushDatabase.readerFor(pushDatabase.getPending());
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-09-09 23:46:03 +00:00
|
|
|
while ((record = smsReader.getNext()) != null) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
scheduleDecryptFromCursor(context, masterSecret, record);
|
|
|
|
}
|
2013-09-09 23:46:03 +00:00
|
|
|
|
|
|
|
while ((message = pushReader.getNext()) != null) {
|
|
|
|
if (message.isPreKeyBundle()) {
|
|
|
|
Intent intent = new Intent(context, SendReceiveService.class);
|
|
|
|
intent.setAction(SendReceiveService.RECEIVE_PUSH_ACTION);
|
|
|
|
intent.putExtra("message", message);
|
|
|
|
context.startService(intent);
|
|
|
|
|
|
|
|
pushDatabase.delete(pushReader.getCurrentId());
|
|
|
|
} else {
|
|
|
|
scheduleDecryption(context, masterSecret, pushReader.getCurrentId(), message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
2013-09-09 23:46:03 +00:00
|
|
|
if (smsReader != null)
|
|
|
|
smsReader.close();
|
|
|
|
|
|
|
|
if (pushReader != null)
|
|
|
|
pushReader.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void scheduleRogueMessages(Context context, MasterSecret masterSecret, Recipient recipient) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
SmsDatabase.Reader reader = null;
|
|
|
|
SmsMessageRecord record;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
Cursor cursor = DatabaseFactory.getSmsDatabase(context).getEncryptedRogueMessages(recipient);
|
|
|
|
reader = DatabaseFactory.getEncryptingSmsDatabase(context).readerFor(masterSecret, cursor);
|
2012-12-24 16:40:37 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
while ((record = reader.getNext()) != null) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsDecrypting(record.getId());
|
|
|
|
scheduleDecryptFromCursor(context, masterSecret, record);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
if (reader != null)
|
|
|
|
reader.close();
|
2012-12-24 16:40:37 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
private static void scheduleDecryptFromCursor(Context context, MasterSecret masterSecret,
|
|
|
|
SmsMessageRecord record)
|
|
|
|
{
|
|
|
|
long messageId = record.getId();
|
2013-05-15 21:09:13 +00:00
|
|
|
long threadId = record.getThreadId();
|
2013-04-30 18:14:01 +00:00
|
|
|
String body = record.getBody().getBody();
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
String originator = record.getIndividualRecipient().getNumber();
|
2014-02-03 03:38:06 +00:00
|
|
|
int originatorDeviceId = record.getRecipientDeviceId();
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
boolean isSecureMessage = record.isSecure();
|
2013-05-15 21:09:13 +00:00
|
|
|
boolean isKeyExchange = record.isKeyExchange();
|
2014-02-19 21:46:49 +00:00
|
|
|
boolean isEndSession = record.isEndSession();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-05-15 21:09:13 +00:00
|
|
|
scheduleDecryption(context, masterSecret, messageId, threadId,
|
2014-02-03 03:38:06 +00:00
|
|
|
originator, originatorDeviceId, body,
|
2014-02-19 21:46:49 +00:00
|
|
|
isSecureMessage, isKeyExchange, isEndSession);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-09-09 01:19:05 +00:00
|
|
|
private static class PushDecryptionWorkItem implements Runnable {
|
|
|
|
|
|
|
|
private Context context;
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
private long messageId;
|
|
|
|
private IncomingPushMessage message;
|
|
|
|
|
|
|
|
public PushDecryptionWorkItem(Context context, MasterSecret masterSecret,
|
|
|
|
long messageId, IncomingPushMessage message)
|
|
|
|
{
|
|
|
|
this.context = context;
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.messageId = messageId;
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
2013-11-26 01:00:20 +00:00
|
|
|
try {
|
2014-02-03 03:38:06 +00:00
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, message.getSource(), false);
|
|
|
|
Recipient recipient = recipients.getPrimaryRecipient();
|
|
|
|
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), message.getSourceDevice());
|
2013-09-09 01:19:05 +00:00
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
if (!SessionRecordV2.hasSession(context, masterSecret, recipientDevice)) {
|
2013-11-26 01:00:20 +00:00
|
|
|
sendResult(PushReceiver.RESULT_NO_SESSION);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-09 01:19:05 +00:00
|
|
|
|
2014-02-03 03:38:06 +00:00
|
|
|
SessionCipher sessionCipher = SessionCipher.createFor(context, masterSecret, recipientDevice);
|
2013-11-26 01:00:20 +00:00
|
|
|
byte[] plaintextBody = sessionCipher.decrypt(message.getBody());
|
2013-09-09 01:19:05 +00:00
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
message = message.withBody(plaintextBody);
|
|
|
|
sendResult(PushReceiver.RESULT_OK);
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
sendResult(PushReceiver.RESULT_DECRYPT_FAILED);
|
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
sendResult(PushReceiver.RESULT_DECRYPT_FAILED);
|
2014-03-19 19:37:46 +00:00
|
|
|
} catch (DuplicateMessageException e) {
|
|
|
|
Log.w("DecryptingQueue", e);
|
|
|
|
sendResult(PushReceiver.RESULT_DECRYPT_DUPLICATE);
|
2014-04-10 03:02:46 +00:00
|
|
|
} catch (LegacyMessageException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
sendResult(PushReceiver.RESULT_DECRYPT_FAILED);
|
2013-09-09 01:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendResult(int result) {
|
|
|
|
Intent intent = new Intent(context, SendReceiveService.class);
|
|
|
|
intent.setAction(SendReceiveService.DECRYPTED_PUSH_ACTION);
|
|
|
|
intent.putExtra("message", message);
|
|
|
|
intent.putExtra("message_id", messageId);
|
|
|
|
intent.putExtra("result", result);
|
|
|
|
context.startService(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static class MmsDecryptionItem implements Runnable {
|
|
|
|
private long messageId;
|
|
|
|
private long threadId;
|
|
|
|
private Context context;
|
|
|
|
private MasterSecret masterSecret;
|
|
|
|
private MultimediaMessagePdu pdu;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public MmsDecryptionItem(Context context, MasterSecret masterSecret,
|
|
|
|
long messageId, long threadId, MultimediaMessagePdu pdu)
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
this.context = context;
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.messageId = messageId;
|
|
|
|
this.threadId = threadId;
|
|
|
|
this.pdu = pdu;
|
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private byte[] getEncryptedData() {
|
|
|
|
for (int i=0;i<pdu.getBody().getPartsNum();i++) {
|
|
|
|
Log.w("DecryptingQueue", "Content type (" + i + "): " + new String(pdu.getBody().getPart(i).getContentType()));
|
|
|
|
if (new String(pdu.getBody().getPart(i).getContentType()).equals(ContentType.TEXT_PLAIN)) {
|
|
|
|
return pdu.getBody().getPart(i).getData();
|
|
|
|
}
|
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-02-08 19:57:54 +00:00
|
|
|
@Override
|
2011-12-20 18:20:44 +00:00
|
|
|
public void run() {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2014-02-03 03:38:06 +00:00
|
|
|
String messageFrom = pdu.getFrom().getString();
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, messageFrom, false);
|
|
|
|
Recipient recipient = recipients.getPrimaryRecipient();
|
|
|
|
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), RecipientDevice.DEFAULT_DEVICE_ID);
|
|
|
|
byte[] ciphertextPduBytes = getEncryptedData();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
if (ciphertextPduBytes == null) {
|
|
|
|
Log.w("DecryptingQueue", "No encoded PNG data found on parts.");
|
|
|
|
database.markAsDecryptFailed(messageId, threadId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
if (!Session.hasSession(context, masterSecret, recipient)) {
|
2011-12-20 18:20:44 +00:00
|
|
|
Log.w("DecryptingQueue", "No such recipient session for MMS...");
|
|
|
|
database.markAsNoSession(messageId, threadId);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
byte[] plaintextPduBytes;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
Log.w("DecryptingQueue", "Decrypting: " + Hex.toString(ciphertextPduBytes));
|
|
|
|
TextTransport transportDetails = new TextTransport();
|
2014-02-03 03:38:06 +00:00
|
|
|
SessionCipher sessionCipher = SessionCipher.createFor(context, masterSecret, recipientDevice);
|
2013-11-26 01:00:20 +00:00
|
|
|
byte[] decodedCiphertext = transportDetails.getDecodedMessage(ciphertextPduBytes);
|
|
|
|
|
|
|
|
try {
|
|
|
|
plaintextPduBytes = sessionCipher.decrypt(decodedCiphertext);
|
|
|
|
} catch (InvalidMessageException ime) {
|
|
|
|
// XXX - For some reason, Sprint seems to append a single character to the
|
|
|
|
// end of message text segments. I don't know why, so here we just try
|
|
|
|
// truncating the message by one if the MAC fails.
|
|
|
|
if (ciphertextPduBytes.length > 2) {
|
|
|
|
Log.w("DecryptingQueue", "Attempting truncated decrypt...");
|
|
|
|
byte[] truncated = Util.trim(ciphertextPduBytes, ciphertextPduBytes.length - 1);
|
|
|
|
decodedCiphertext = transportDetails.getDecodedMessage(truncated);
|
|
|
|
plaintextPduBytes = sessionCipher.decrypt(decodedCiphertext);
|
|
|
|
} else {
|
|
|
|
throw ime;
|
2013-05-25 20:17:18 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-05-16 20:48:44 +00:00
|
|
|
MultimediaMessagePdu plaintextGenericPdu = (MultimediaMessagePdu)new PduParser(plaintextPduBytes).parse();
|
|
|
|
RetrieveConf plaintextPdu = new RetrieveConf(plaintextGenericPdu.getPduHeaders(),
|
|
|
|
plaintextGenericPdu.getBody());
|
2011-12-20 18:20:44 +00:00
|
|
|
Log.w("DecryptingQueue", "Successfully decrypted MMS!");
|
2013-07-19 00:42:45 +00:00
|
|
|
database.insertSecureDecryptedMessageInbox(masterSecret, new IncomingMediaMessage(plaintextPdu), threadId);
|
2012-12-24 16:40:37 +00:00
|
|
|
database.delete(messageId);
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (RecipientFormattingException rfe) {
|
|
|
|
Log.w("DecryptingQueue", rfe);
|
|
|
|
database.markAsDecryptFailed(messageId, threadId);
|
|
|
|
} catch (InvalidMessageException ime) {
|
|
|
|
Log.w("DecryptingQueue", ime);
|
2012-12-24 16:40:37 +00:00
|
|
|
database.markAsDecryptFailed(messageId, threadId);
|
2014-03-19 19:37:46 +00:00
|
|
|
} catch (DuplicateMessageException dme) {
|
|
|
|
Log.w("DecryptingQueue", dme);
|
|
|
|
database.markAsDecryptDuplicate(messageId, threadId);
|
2014-04-10 03:02:46 +00:00
|
|
|
} catch (LegacyMessageException lme) {
|
|
|
|
Log.w("DecryptingQueue", lme);
|
|
|
|
database.markAsLegacyVersion(messageId, threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (MmsException mme) {
|
|
|
|
Log.w("DecryptingQueue", mme);
|
2012-12-24 16:40:37 +00:00
|
|
|
database.markAsDecryptFailed(messageId, threadId);
|
2013-11-01 00:23:45 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("DecryptingQueue", e);
|
|
|
|
database.markAsDecryptFailed(messageId, threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static class DecryptionWorkItem implements Runnable {
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-05-15 21:09:13 +00:00
|
|
|
private final long messageId;
|
|
|
|
private final long threadId;
|
|
|
|
private final Context context;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
private final MasterSecret masterSecret;
|
2013-05-15 21:09:13 +00:00
|
|
|
private final String body;
|
|
|
|
private final String originator;
|
2014-02-03 03:38:06 +00:00
|
|
|
private final int deviceId;
|
2013-05-15 21:09:13 +00:00
|
|
|
private final boolean isSecureMessage;
|
|
|
|
private final boolean isKeyExchange;
|
2014-02-19 21:46:49 +00:00
|
|
|
private final boolean isEndSession;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-05-15 21:09:13 +00:00
|
|
|
public DecryptionWorkItem(Context context, MasterSecret masterSecret, long messageId, long threadId,
|
2014-02-03 03:38:06 +00:00
|
|
|
String originator, int deviceId, String body, boolean isSecureMessage,
|
2014-02-19 21:46:49 +00:00
|
|
|
boolean isKeyExchange, boolean isEndSession)
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
{
|
2013-05-15 21:09:13 +00:00
|
|
|
this.context = context;
|
|
|
|
this.messageId = messageId;
|
|
|
|
this.threadId = threadId;
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.body = body;
|
|
|
|
this.originator = originator;
|
2014-02-03 03:38:06 +00:00
|
|
|
this.deviceId = deviceId;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
this.isSecureMessage = isSecureMessage;
|
2013-05-15 21:09:13 +00:00
|
|
|
this.isKeyExchange = isKeyExchange;
|
2014-02-19 21:46:49 +00:00
|
|
|
this.isEndSession = isEndSession;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleRemoteAsymmetricEncrypt() {
|
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
String plaintextBody;
|
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
try {
|
2014-02-03 03:38:06 +00:00
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, originator, false);
|
|
|
|
Recipient recipient = recipients.getPrimaryRecipient();
|
|
|
|
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), deviceId);
|
2013-08-22 00:25:19 +00:00
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
if (!Session.hasSession(context, masterSecret, recipient)) {
|
|
|
|
database.markAsNoSession(messageId);
|
2013-11-01 00:23:45 +00:00
|
|
|
return;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-11-26 01:00:20 +00:00
|
|
|
|
|
|
|
SmsTransportDetails transportDetails = new SmsTransportDetails();
|
2014-02-03 03:38:06 +00:00
|
|
|
SessionCipher sessionCipher = SessionCipher.createFor(context, masterSecret, recipientDevice);
|
2013-11-26 01:00:20 +00:00
|
|
|
byte[] decodedCiphertext = transportDetails.getDecodedMessage(body.getBytes());
|
|
|
|
byte[] paddedPlaintext = sessionCipher.decrypt(decodedCiphertext);
|
|
|
|
|
|
|
|
plaintextBody = new String(transportDetails.getStrippedPaddingMessageBody(paddedPlaintext));
|
2014-02-19 21:46:49 +00:00
|
|
|
|
|
|
|
if (isEndSession &&
|
|
|
|
"TERMINATE".equals(plaintextBody) &&
|
|
|
|
SessionRecordV2.hasSession(context, masterSecret, recipientDevice))
|
|
|
|
{
|
|
|
|
Session.abortSessionFor(context, recipient);
|
|
|
|
}
|
2013-11-26 01:00:20 +00:00
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
database.markAsDecryptFailed(messageId);
|
|
|
|
return;
|
2014-04-10 03:02:46 +00:00
|
|
|
} catch (LegacyMessageException lme) {
|
|
|
|
Log.w("DecryptionQueue", lme);
|
|
|
|
database.markAsLegacyVersion(messageId);
|
|
|
|
return;
|
2013-11-26 01:00:20 +00:00
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
database.markAsDecryptFailed(messageId);
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
database.markAsDecryptFailed(messageId);
|
|
|
|
return;
|
2014-03-19 19:37:46 +00:00
|
|
|
} catch (DuplicateMessageException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
database.markAsDecryptDuplicate(messageId);
|
|
|
|
return;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
database.updateMessageBody(masterSecret, messageId, plaintextBody);
|
2013-02-08 19:57:54 +00:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleLocalAsymmetricEncrypt() {
|
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
String plaintextBody;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
AsymmetricMasterCipher asymmetricMasterCipher = new AsymmetricMasterCipher(MasterSecretUtil.getAsymmetricMasterSecret(context, masterSecret));
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
plaintextBody = asymmetricMasterCipher.decryptBody(body);
|
2013-05-15 21:09:13 +00:00
|
|
|
|
|
|
|
if (isKeyExchange) {
|
|
|
|
handleKeyExchangeProcessing(plaintextBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
database.updateMessageBody(masterSecret, messageId, plaintextBody);
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret);
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (InvalidMessageException ime) {
|
|
|
|
Log.w("DecryptionQueue", ime);
|
|
|
|
database.markAsDecryptFailed(messageId);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("DecryptionQueue", e);
|
|
|
|
database.markAsDecryptFailed(messageId);
|
|
|
|
}
|
2013-05-15 21:09:13 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2014-03-18 00:24:00 +00:00
|
|
|
private void handleKeyExchangeProcessing(String plaintextBody) {
|
2013-07-10 01:26:18 +00:00
|
|
|
if (TextSecurePreferences.isAutoRespondKeyExchangeEnabled(context)) {
|
2013-05-15 21:09:13 +00:00
|
|
|
try {
|
2014-02-03 03:38:06 +00:00
|
|
|
Recipient recipient = RecipientFactory.getRecipientsFromString(context, originator, false).getPrimaryRecipient();
|
|
|
|
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), deviceId);
|
2014-03-18 00:24:00 +00:00
|
|
|
KeyExchangeMessage message = KeyExchangeMessage.createFor(plaintextBody);
|
2014-02-03 03:38:06 +00:00
|
|
|
KeyExchangeProcessor processor = KeyExchangeProcessor.createFor(context, masterSecret, recipientDevice, message);
|
2013-05-15 21:09:13 +00:00
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
if (processor.isStale(message)) {
|
2013-05-15 21:09:13 +00:00
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).markAsStaleKeyExchange(messageId);
|
2013-11-26 01:00:20 +00:00
|
|
|
} else if (processor.isTrusted(message)) {
|
2013-05-15 21:09:13 +00:00
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).markAsProcessedKeyExchange(messageId);
|
2013-11-26 01:00:20 +00:00
|
|
|
processor.processKeyExchangeMessage(message, threadId);
|
2013-05-15 21:09:13 +00:00
|
|
|
}
|
|
|
|
} catch (InvalidVersionException e) {
|
|
|
|
Log.w("DecryptingQueue", e);
|
2013-11-26 01:00:20 +00:00
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).markAsInvalidVersionKeyExchange(messageId);
|
2013-05-15 21:09:13 +00:00
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.w("DecryptingQueue", e);
|
2013-11-26 01:00:20 +00:00
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).markAsCorruptKeyExchange(messageId);
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("DecryptingQueue", e);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).markAsCorruptKeyExchange(messageId);
|
2014-02-03 03:38:06 +00:00
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("DecryptingQueue", e);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).markAsCorruptKeyExchange(messageId);
|
2014-04-10 03:02:46 +00:00
|
|
|
} catch (LegacyMessageException e) {
|
|
|
|
Log.w("DecryptingQueue", e);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).markAsLegacyVersion(messageId);
|
2013-05-15 21:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-12-24 16:40:37 +00:00
|
|
|
|
2013-02-08 19:57:54 +00:00
|
|
|
@Override
|
2012-12-24 16:40:37 +00:00
|
|
|
public void run() {
|
2014-02-19 21:46:49 +00:00
|
|
|
if (isSecureMessage || isEndSession) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
handleRemoteAsymmetricEncrypt();
|
|
|
|
} else {
|
|
|
|
handleLocalAsymmetricEncrypt();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|