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 12:22:04 -07:00
|
|
|
package org.thoughtcrime.securesms.transport;
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.telephony.SmsManager;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.SessionCipher;
|
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService;
|
|
|
|
import org.thoughtcrime.securesms.service.SmsListener;
|
|
|
|
import org.thoughtcrime.securesms.sms.MultipartMessageHandler;
|
|
|
|
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.SmsTransportDetails;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class SmsTransport {
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
|
|
|
|
public SmsTransport(Context context, MasterSecret masterSecret) {
|
|
|
|
this.context = context.getApplicationContext();
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deliver(SmsMessageRecord message) throws UndeliverableMessageException {
|
|
|
|
if (message.isSecure() || message.isKeyExchange()) {
|
|
|
|
deliverSecureMessage(message);
|
|
|
|
} else {
|
|
|
|
deliverPlaintextMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deliverSecureMessage(SmsMessageRecord message) throws UndeliverableMessageException {
|
|
|
|
MultipartMessageHandler multipartMessageHandler = new MultipartMessageHandler();
|
|
|
|
OutgoingTextMessage transportMessage = OutgoingTextMessage.from(message);
|
|
|
|
|
|
|
|
if (message.isSecure()) {
|
2013-04-30 11:14:01 -07:00
|
|
|
String encryptedMessage = getAsymmetricEncrypt(masterSecret, message.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 12:22:04 -07:00
|
|
|
message.getIndividualRecipient());
|
|
|
|
transportMessage = transportMessage.withBody(encryptedMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<String> messages = multipartMessageHandler.divideMessage(transportMessage);
|
|
|
|
ArrayList<PendingIntent> sentIntents = constructSentIntents(message.getId(), message.getType(), messages);
|
|
|
|
ArrayList<PendingIntent> deliveredIntents = constructDeliveredIntents(message.getId(), message.getType(), messages);
|
|
|
|
|
|
|
|
Log.w("SmsTransport", "Secure divide into message parts: " + messages.size());
|
|
|
|
|
|
|
|
for (int i=0;i<messages.size();i++) {
|
|
|
|
// XXX moxie@thoughtcrime.org 1/7/11 -- There's apparently a bug where for some unknown recipients
|
|
|
|
// and messages, this will throw an NPE. I have no idea why, so I'm just catching it and marking
|
|
|
|
// the message as a failure. That way at least it doesn't repeatedly crash every time you start
|
|
|
|
// the app.
|
|
|
|
try {
|
|
|
|
SmsManager.getDefault().sendTextMessage(message.getIndividualRecipient().getNumber(), null, messages.get(i),
|
|
|
|
sentIntents.get(i),
|
|
|
|
deliveredIntents == null ? null : deliveredIntents.get(i));
|
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
Log.w("SmsSender", npe);
|
|
|
|
throw new UndeliverableMessageException(npe);
|
|
|
|
} catch (IllegalArgumentException iae) {
|
|
|
|
Log.w("SmsSender", iae);
|
|
|
|
throw new UndeliverableMessageException(iae);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deliverPlaintextMessage(SmsMessageRecord message)
|
|
|
|
throws UndeliverableMessageException
|
|
|
|
{
|
2013-04-30 11:14:01 -07:00
|
|
|
ArrayList<String> messages = SmsManager.getDefault().divideMessage(message.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 12:22:04 -07:00
|
|
|
ArrayList<PendingIntent> sentIntents = constructSentIntents(message.getId(), message.getType(), messages);
|
|
|
|
ArrayList<PendingIntent> deliveredIntents = constructDeliveredIntents(message.getId(), message.getType(), messages);
|
|
|
|
String recipient = message.getIndividualRecipient().getNumber();
|
|
|
|
|
|
|
|
// XXX moxie@thoughtcrime.org 1/7/11 -- There's apparently a bug where for some unknown recipients
|
|
|
|
// and messages, this will throw an NPE. I have no idea why, so I'm just catching it and marking
|
|
|
|
// the message as a failure. That way at least it doesn't repeatedly crash every time you start
|
|
|
|
// the app.
|
|
|
|
try {
|
|
|
|
SmsManager.getDefault().sendMultipartTextMessage(recipient, null, messages, sentIntents, deliveredIntents);
|
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
Log.w("SmsTransport", npe);
|
|
|
|
throw new UndeliverableMessageException(npe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ArrayList<PendingIntent> constructSentIntents(long messageId, long type, ArrayList<String> messages) {
|
|
|
|
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(messages.size());
|
|
|
|
|
|
|
|
for (int i=0;i<messages.size();i++) {
|
|
|
|
Intent pending = new Intent(SendReceiveService.SENT_SMS_ACTION, Uri.parse("custom://" + messageId + System.currentTimeMillis()), context, SmsListener.class);
|
|
|
|
pending.putExtra("type", type);
|
|
|
|
pending.putExtra("message_id", messageId);
|
|
|
|
sentIntents.add(PendingIntent.getBroadcast(context, 0, pending, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sentIntents;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ArrayList<PendingIntent> constructDeliveredIntents(long messageId, long type, ArrayList<String> messages) {
|
|
|
|
if (!PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
.getBoolean(ApplicationPreferencesActivity.SMS_DELIVERY_REPORT_PREF, false))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<PendingIntent> deliveredIntents = new ArrayList<PendingIntent>(messages.size());
|
|
|
|
|
|
|
|
for (int i=0;i<messages.size();i++) {
|
|
|
|
Intent pending = new Intent(SendReceiveService.DELIVERED_SMS_ACTION, Uri.parse("custom://" + messageId + System.currentTimeMillis()), context, SmsListener.class);
|
|
|
|
pending.putExtra("type", type);
|
|
|
|
pending.putExtra("message_id", messageId);
|
|
|
|
deliveredIntents.add(PendingIntent.getBroadcast(context, 0, pending, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return deliveredIntents;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getAsymmetricEncrypt(MasterSecret masterSecret, String body, Recipient recipient) {
|
|
|
|
synchronized (SessionCipher.CIPHER_LOCK) {
|
|
|
|
SessionCipher cipher = new SessionCipher(context, masterSecret, recipient, new SmsTransportDetails());
|
|
|
|
return new String(cipher.encryptMessage(body.getBytes()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|