session-android/src/org/thoughtcrime/securesms/service/SmsReceiver.java

147 lines
6.3 KiB
Java
Raw Normal View History

2012-10-30 00:34:14 +00:00
/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
2012-10-30 00:34:14 +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-10-30 00:34:14 +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.service;
2012-10-30 00:34:14 +00:00
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.util.Pair;
2012-10-30 00:34:14 +00:00
2011-12-20 18:20:44 +00:00
import org.thoughtcrime.securesms.crypto.DecryptingQueue;
import org.whispersystems.textsecure.crypto.InvalidKeyException;
2011-12-20 18:20:44 +00:00
import org.thoughtcrime.securesms.crypto.InvalidVersionException;
import org.thoughtcrime.securesms.crypto.KeyExchangeMessage;
import org.thoughtcrime.securesms.crypto.KeyExchangeProcessor;
import org.whispersystems.textsecure.crypto.MasterSecret;
2011-12-20 18:20:44 +00:00
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
import org.thoughtcrime.securesms.database.SmsDatabase;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
2011-12-20 18:20:44 +00:00
import org.thoughtcrime.securesms.protocol.WirePrefix;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.sms.IncomingKeyExchangeMessage;
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
2013-05-03 23:09:06 +00:00
import org.thoughtcrime.securesms.sms.MultipartSmsMessageHandler;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import java.util.List;
2011-12-20 18:20:44 +00:00
public class SmsReceiver {
2013-05-03 23:09:06 +00:00
private MultipartSmsMessageHandler multipartMessageHandler = new MultipartSmsMessageHandler();
2012-10-30 00:34:14 +00:00
2011-12-20 18:20:44 +00:00
private final Context context;
2012-10-30 00:34:14 +00:00
2013-02-15 03:15:40 +00:00
public SmsReceiver(Context context) {
this.context = context;
2011-12-20 18:20:44 +00:00
}
2012-10-30 00:34:14 +00:00
private IncomingTextMessage assembleMessageFragments(List<IncomingTextMessage> messages) {
IncomingTextMessage message = new IncomingTextMessage(messages);
if (WirePrefix.isEncryptedMessage(message.getMessageBody()) ||
WirePrefix.isKeyExchange(message.getMessageBody()))
{
return multipartMessageHandler.processPotentialMultipartMessage(message);
2011-12-20 18:20:44 +00:00
} else {
return message;
2011-12-20 18:20:44 +00:00
}
}
2012-10-30 00:34:14 +00:00
private Pair<Long, Long> storeSecureMessage(MasterSecret masterSecret, IncomingTextMessage message) {
Pair<Long, Long> messageAndThreadId = DatabaseFactory.getEncryptingSmsDatabase(context)
.insertMessageInbox(masterSecret, message);
2012-10-30 00:34:14 +00:00
if (masterSecret != null) {
DecryptingQueue.scheduleDecryption(context, masterSecret, messageAndThreadId.first,
messageAndThreadId.second,
message.getSender(), message.getMessageBody(),
message.isSecureMessage(), message.isKeyExchange());
2011-12-20 18:20:44 +00:00
}
2012-10-30 00:34:14 +00:00
return messageAndThreadId;
2011-12-20 18:20:44 +00:00
}
2012-10-30 00:34:14 +00:00
private Pair<Long, Long> storeStandardMessage(MasterSecret masterSecret, IncomingTextMessage message) {
EncryptingSmsDatabase encryptingDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
SmsDatabase plaintextDatabase = DatabaseFactory.getSmsDatabase(context);
if (masterSecret != null) {
return encryptingDatabase.insertMessageInbox(masterSecret, message);
} else if (MasterSecretUtil.hasAsymmericMasterSecret(context)) {
return encryptingDatabase.insertMessageInbox(MasterSecretUtil.getAsymmetricMasterSecret(context, null), message);
} else {
return plaintextDatabase.insertMessageInbox(message);
}
2011-12-20 18:20:44 +00:00
}
private Pair<Long, Long> storeKeyExchangeMessage(MasterSecret masterSecret,
IncomingKeyExchangeMessage message)
{
if (masterSecret != null && TextSecurePreferences.isAutoRespondKeyExchangeEnabled(context)) {
2011-12-20 18:20:44 +00:00
try {
Recipient recipient = new Recipient(null, message.getSender(), null, null);
KeyExchangeMessage keyExchangeMessage = new KeyExchangeMessage(message.getMessageBody());
2012-10-30 00:34:14 +00:00
KeyExchangeProcessor processor = new KeyExchangeProcessor(context, masterSecret, recipient);
Log.w("SmsReceiver", "Received key with fingerprint: " + keyExchangeMessage.getPublicKey().getFingerprint());
if (processor.isStale(keyExchangeMessage)) {
message.setStale(true);
} else if (processor.isTrusted(keyExchangeMessage)) {
message.setProcessed(true);
Pair<Long, Long> messageAndThreadId = storeStandardMessage(masterSecret, message);
processor.processKeyExchangeMessage(keyExchangeMessage, messageAndThreadId.second);
2012-10-30 00:34:14 +00:00
return messageAndThreadId;
2012-10-30 00:34:14 +00:00
}
2011-12-20 18:20:44 +00:00
} catch (InvalidVersionException e) {
2012-10-30 00:34:14 +00:00
Log.w("SmsReceiver", e);
2011-12-20 18:20:44 +00:00
} catch (InvalidKeyException e) {
2012-10-30 00:34:14 +00:00
Log.w("SmsReceiver", e);
2011-12-20 18:20:44 +00:00
}
}
return storeStandardMessage(masterSecret, message);
2011-12-20 18:20:44 +00:00
}
2012-10-30 00:34:14 +00:00
private Pair<Long, Long> storeMessage(MasterSecret masterSecret, IncomingTextMessage message) {
if (message.isSecureMessage()) return storeSecureMessage(masterSecret, message);
else if (message.isKeyExchange()) return storeKeyExchangeMessage(masterSecret, (IncomingKeyExchangeMessage)message);
else return storeStandardMessage(masterSecret, message);
2011-12-20 18:20:44 +00:00
}
private void handleReceiveMessage(MasterSecret masterSecret, Intent intent) {
List<IncomingTextMessage> messagesList = intent.getExtras().getParcelableArrayList("text_messages");
IncomingTextMessage message = assembleMessageFragments(messagesList);
2012-10-30 00:34:14 +00:00
2011-12-20 18:20:44 +00:00
if (message != null) {
Pair<Long, Long> messageAndThreadId = storeMessage(masterSecret, message);
MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second);
2012-10-30 00:34:14 +00:00
}
2011-12-20 18:20:44 +00:00
}
2012-10-30 00:34:14 +00:00
2011-12-20 18:20:44 +00:00
public void process(MasterSecret masterSecret, Intent intent) {
2013-02-15 03:15:40 +00:00
if (intent.getAction().equals(SendReceiveService.RECEIVE_SMS_ACTION)) {
2011-12-20 18:20:44 +00:00
handleReceiveMessage(masterSecret, intent);
2013-02-15 03:15:40 +00:00
}
2011-12-20 18:20:44 +00:00
}
}