2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.AsymmetricMasterCipher;
|
|
|
|
import org.thoughtcrime.securesms.crypto.AsymmetricMasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingEncryptedMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidMessageException;
|
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureGroup;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class SmsDecryptJob extends MasterSecretJob {
|
|
|
|
|
|
|
|
private static final String TAG = SmsDecryptJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private final long messageId;
|
|
|
|
|
|
|
|
public SmsDecryptJob(Context context, long messageId) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-03-11 14:23:45 -07:00
|
|
|
public void onAdded() {}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2014-11-11 19:57:53 -08:00
|
|
|
public void onRun(MasterSecret masterSecret) throws NoSuchMessageException {
|
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
try {
|
2014-11-08 11:35:58 -08:00
|
|
|
SmsMessageRecord record = database.getMessage(masterSecret, messageId);
|
|
|
|
IncomingTextMessage message = createIncomingTextMessage(masterSecret, record);
|
|
|
|
long messageId = record.getId();
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
if (message.isSecureMessage()) {
|
|
|
|
database.markAsLegacyVersion(messageId);
|
|
|
|
} else {
|
|
|
|
database.updateMessageBody(masterSecret, messageId, message.getMessageBody());
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret);
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsDecryptFailed(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
@Override
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
2014-11-11 19:57:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getAsymmetricDecryptedBody(MasterSecret masterSecret, String body)
|
|
|
|
throws InvalidMessageException
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
AsymmetricMasterSecret asymmetricMasterSecret = MasterSecretUtil.getAsymmetricMasterSecret(context, masterSecret);
|
|
|
|
AsymmetricMasterCipher asymmetricMasterCipher = new AsymmetricMasterCipher(asymmetricMasterSecret);
|
|
|
|
|
|
|
|
return asymmetricMasterCipher.decryptBody(body);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new InvalidMessageException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IncomingTextMessage createIncomingTextMessage(MasterSecret masterSecret, SmsMessageRecord record)
|
|
|
|
throws InvalidMessageException
|
|
|
|
{
|
|
|
|
IncomingTextMessage message = new IncomingTextMessage(record.getRecipients().getPrimaryRecipient().getNumber(),
|
|
|
|
record.getRecipientDeviceId(),
|
|
|
|
record.getDateSent(),
|
2015-03-11 14:23:45 -07:00
|
|
|
record.getBody().getBody(),
|
2014-11-03 15:16:04 -08:00
|
|
|
Optional.<TextSecureGroup>absent());
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
if (record.isAsymmetricEncryption()) {
|
|
|
|
String plaintextBody = getAsymmetricDecryptedBody(masterSecret, record.getBody().getBody());
|
|
|
|
return new IncomingTextMessage(message, plaintextBody);
|
|
|
|
} else {
|
2014-11-03 15:16:04 -08:00
|
|
|
return new IncomingEncryptedMessage(message, message.getMessageBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|