2013-07-12 17:40:41 -07:00
|
|
|
package org.thoughtcrime.securesms.transport;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
2013-08-21 19:34:11 -07:00
|
|
|
import android.util.Pair;
|
2013-07-12 17:40:41 -07:00
|
|
|
|
2013-08-21 17:25:19 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
2013-08-19 10:07:07 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.KeyExchangeProcessor;
|
2013-07-12 17:40:41 -07:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2013-07-17 15:13:00 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.PartParser;
|
2013-08-21 17:25:19 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2013-08-31 09:28:49 -07:00
|
|
|
import org.whispersystems.textsecure.push.RawTransportDetails;
|
2013-07-12 17:40:41 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2013-07-17 15:13:00 -07:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2013-08-21 17:25:19 -07:00
|
|
|
import org.whispersystems.textsecure.crypto.IdentityKey;
|
|
|
|
import org.whispersystems.textsecure.crypto.IdentityKeyPair;
|
2013-08-21 19:34:11 -07:00
|
|
|
import org.whispersystems.textsecure.crypto.KeyUtil;
|
2013-08-21 17:25:19 -07:00
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2013-08-21 17:32:54 -07:00
|
|
|
import org.whispersystems.textsecure.crypto.MessageCipher;
|
2013-08-21 17:25:19 -07:00
|
|
|
import org.whispersystems.textsecure.crypto.protocol.PreKeyBundleMessage;
|
2013-08-22 20:23:05 -07:00
|
|
|
import org.whispersystems.textsecure.push.OutgoingPushMessage;
|
2013-08-19 10:07:07 -07:00
|
|
|
import org.whispersystems.textsecure.push.PreKeyEntity;
|
2013-07-18 17:42:45 -07:00
|
|
|
import org.whispersystems.textsecure.push.PushAttachmentData;
|
2013-07-12 17:40:41 -07:00
|
|
|
import org.whispersystems.textsecure.push.PushServiceSocket;
|
2013-08-21 19:34:11 -07:00
|
|
|
import org.whispersystems.textsecure.push.PushTransportDetails;
|
2013-07-12 17:40:41 -07:00
|
|
|
import org.whispersystems.textsecure.push.RateLimitException;
|
|
|
|
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2013-07-17 15:13:00 -07:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
import ws.com.google.android.mms.pdu.PduBody;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
2013-07-12 17:40:41 -07:00
|
|
|
|
|
|
|
public class PushTransport extends BaseTransport {
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
|
|
|
|
public PushTransport(Context context, MasterSecret masterSecret) {
|
|
|
|
this.context = context.getApplicationContext();
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deliver(SmsMessageRecord message) throws IOException {
|
|
|
|
try {
|
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
String password = TextSecurePreferences.getPushServerPassword(context);
|
|
|
|
PushServiceSocket socket = new PushServiceSocket(context, localNumber, password);
|
|
|
|
|
2013-08-21 19:34:11 -07:00
|
|
|
Recipient recipient = message.getIndividualRecipient();
|
|
|
|
String plaintext = message.getBody().getBody();
|
|
|
|
String recipientCanonicalNumber = PhoneNumberFormatter.formatNumber(recipient.getNumber(),
|
|
|
|
localNumber);
|
|
|
|
|
2013-08-29 17:01:30 -07:00
|
|
|
Pair<Integer, byte[]> typeAndCiphertext = getEncryptedMessage(socket, recipient, recipientCanonicalNumber, plaintext);
|
2013-08-21 19:34:11 -07:00
|
|
|
|
|
|
|
socket.sendMessage(recipientCanonicalNumber, typeAndCiphertext.second, typeAndCiphertext.first);
|
2013-07-12 17:40:41 -07:00
|
|
|
|
|
|
|
context.sendBroadcast(constructSentIntent(context, message.getId(), message.getType()));
|
|
|
|
} catch (RateLimitException e) {
|
|
|
|
Log.w("PushTransport", e);
|
|
|
|
throw new IOException("Rate limit exceeded.");
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 15:13:00 -07:00
|
|
|
|
|
|
|
public void deliver(SendReq message, List<String> destinations) throws IOException {
|
|
|
|
try {
|
2013-07-18 17:42:45 -07:00
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
String password = TextSecurePreferences.getPushServerPassword(context);
|
|
|
|
PushServiceSocket socket = new PushServiceSocket(context, localNumber, password);
|
2013-08-29 17:01:30 -07:00
|
|
|
byte[] messageText = PartParser.getMessageText(message.getBody()).getBytes();
|
2013-07-18 17:42:45 -07:00
|
|
|
List<PushAttachmentData> attachments = getAttachmentsFromBody(message.getBody());
|
2013-07-17 15:13:00 -07:00
|
|
|
|
2013-08-29 17:01:30 -07:00
|
|
|
List<byte[]> messagesList = new LinkedList<byte[]>();
|
|
|
|
List<List<PushAttachmentData>> attachmentsList = new LinkedList<List<PushAttachmentData>>();
|
|
|
|
|
|
|
|
for (String recipient : destinations) {
|
|
|
|
messagesList.add(messageText);
|
|
|
|
attachmentsList.add(attachments);
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.sendMessage(destinations, messagesList, attachmentsList,
|
|
|
|
OutgoingPushMessage.TYPE_MESSAGE_PLAINTEXT);
|
2013-07-17 15:13:00 -07:00
|
|
|
} catch (RateLimitException e) {
|
|
|
|
Log.w("PushTransport", e);
|
|
|
|
throw new IOException("Rate limit exceeded.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:42:45 -07:00
|
|
|
private List<PushAttachmentData> getAttachmentsFromBody(PduBody body) {
|
|
|
|
List<PushAttachmentData> attachments = new LinkedList<PushAttachmentData>();
|
2013-07-17 15:13:00 -07:00
|
|
|
|
|
|
|
for (int i=0;i<body.getPartsNum();i++) {
|
|
|
|
String contentType = Util.toIsoString(body.getPart(i).getContentType());
|
|
|
|
|
|
|
|
if (ContentType.isImageType(contentType) ||
|
|
|
|
ContentType.isAudioType(contentType) ||
|
|
|
|
ContentType.isVideoType(contentType))
|
|
|
|
{
|
2013-07-18 17:42:45 -07:00
|
|
|
attachments.add(new PushAttachmentData(contentType, body.getPart(i).getData()));
|
2013-07-17 15:13:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachments;
|
|
|
|
}
|
2013-08-19 10:07:07 -07:00
|
|
|
|
2013-08-29 17:01:30 -07:00
|
|
|
private Pair<Integer, byte[]> getEncryptedMessage(PushServiceSocket socket, Recipient recipient,
|
2013-08-21 19:34:11 -07:00
|
|
|
String canonicalRecipientNumber, String plaintext)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
if (KeyUtil.isNonPrekeySessionFor(context, masterSecret, recipient)) {
|
|
|
|
Log.w("PushTransport", "Sending standard ciphertext message...");
|
2013-08-29 17:01:30 -07:00
|
|
|
byte[] ciphertext = getEncryptedMessageForExistingSession(recipient, plaintext);
|
|
|
|
return new Pair<Integer, byte[]>(OutgoingPushMessage.TYPE_MESSAGE_CIPHERTEXT, ciphertext);
|
2013-08-21 19:34:11 -07:00
|
|
|
} else if (KeyUtil.isSessionFor(context, recipient)) {
|
|
|
|
Log.w("PushTransport", "Sending prekeybundle ciphertext message for existing session...");
|
2013-08-29 17:01:30 -07:00
|
|
|
byte[] ciphertext = getEncryptedPrekeyBundleMessageForExistingSession(recipient, plaintext);
|
|
|
|
return new Pair<Integer, byte[]>(OutgoingPushMessage.TYPE_MESSAGE_PREKEY_BUNDLE, ciphertext);
|
2013-08-21 19:34:11 -07:00
|
|
|
} else {
|
|
|
|
Log.w("PushTransport", "Sending prekeybundle ciphertext message for new session...");
|
2013-08-29 17:01:30 -07:00
|
|
|
byte[] ciphertext = getEncryptedPrekeyBundleMessageForNewSession(socket, recipient, canonicalRecipientNumber, plaintext);
|
|
|
|
return new Pair<Integer, byte[]>(OutgoingPushMessage.TYPE_MESSAGE_PREKEY_BUNDLE, ciphertext);
|
2013-08-21 19:34:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 17:01:30 -07:00
|
|
|
private byte[] getEncryptedPrekeyBundleMessageForExistingSession(Recipient recipient,
|
2013-08-21 19:34:11 -07:00
|
|
|
String plaintext)
|
|
|
|
{
|
|
|
|
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
|
|
|
|
IdentityKey identityKey = identityKeyPair.getPublicKey();
|
|
|
|
|
|
|
|
MessageCipher message = new MessageCipher(context, masterSecret, identityKeyPair, new RawTransportDetails());
|
|
|
|
byte[] bundledMessage = message.encrypt(recipient, plaintext.getBytes());
|
|
|
|
|
|
|
|
PreKeyBundleMessage preKeyBundleMessage = new PreKeyBundleMessage(identityKey, bundledMessage);
|
|
|
|
return preKeyBundleMessage.serialize();
|
|
|
|
}
|
|
|
|
|
2013-08-29 17:01:30 -07:00
|
|
|
private byte[] getEncryptedPrekeyBundleMessageForNewSession(PushServiceSocket socket,
|
2013-08-21 19:34:11 -07:00
|
|
|
Recipient recipient,
|
|
|
|
String canonicalRecipientNumber,
|
|
|
|
String plaintext)
|
2013-08-21 17:25:19 -07:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
|
|
|
|
IdentityKey identityKey = identityKeyPair.getPublicKey();
|
|
|
|
PreKeyEntity preKey = socket.getPreKey(canonicalRecipientNumber);
|
|
|
|
KeyExchangeProcessor processor = new KeyExchangeProcessor(context, masterSecret, recipient);
|
|
|
|
|
2013-08-19 10:07:07 -07:00
|
|
|
processor.processKeyExchangeMessage(preKey);
|
|
|
|
|
2013-08-21 17:32:54 -07:00
|
|
|
MessageCipher message = new MessageCipher(context, masterSecret, identityKeyPair, new RawTransportDetails());
|
2013-08-21 17:25:19 -07:00
|
|
|
byte[] bundledMessage = message.encrypt(recipient, plaintext.getBytes());
|
|
|
|
|
|
|
|
PreKeyBundleMessage preKeyBundleMessage = new PreKeyBundleMessage(identityKey, bundledMessage);
|
2013-08-21 19:34:11 -07:00
|
|
|
return preKeyBundleMessage.serialize();
|
2013-08-19 10:07:07 -07:00
|
|
|
}
|
|
|
|
|
2013-08-29 17:01:30 -07:00
|
|
|
private byte[] getEncryptedMessageForExistingSession(Recipient recipient, String plaintext)
|
2013-08-21 17:25:19 -07:00
|
|
|
throws IOException
|
|
|
|
{
|
2013-08-21 19:34:11 -07:00
|
|
|
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
|
|
|
|
MessageCipher messageCipher = new MessageCipher(context, masterSecret, identityKeyPair,
|
|
|
|
new PushTransportDetails());
|
|
|
|
|
2013-08-29 17:01:30 -07:00
|
|
|
return messageCipher.encrypt(recipient, plaintext.getBytes());
|
2013-08-19 10:07:07 -07:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:40:41 -07:00
|
|
|
}
|