2012-09-08 03:03:23 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-08 03:03:23 +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-09-08 03:03:23 +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;
|
|
|
|
|
2013-10-21 02:12:53 +00:00
|
|
|
import android.app.Activity;
|
2013-05-23 23:36:24 +00:00
|
|
|
import android.app.ProgressDialog;
|
2012-09-08 03:03:23 +00:00
|
|
|
import android.content.Intent;
|
2013-05-23 23:36:24 +00:00
|
|
|
import android.os.AsyncTask;
|
2012-09-08 03:03:23 +00:00
|
|
|
import android.os.Bundle;
|
2013-05-23 23:36:24 +00:00
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.method.LinkMovementMethod;
|
|
|
|
import android.text.style.ClickableSpan;
|
2012-09-08 03:03:23 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2013-09-14 20:33:23 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.DecryptingQueue;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.KeyExchangeProcessor;
|
2013-11-26 01:00:20 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.KeyExchangeProcessorV2;
|
|
|
|
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.recipients.Recipient;
|
2014-02-22 22:22:12 +00:00
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService;
|
2013-11-26 01:00:20 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.SmsTransportDetails;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MemoryCleaner;
|
2014-02-22 22:22:12 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-02-03 03:38:06 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.IdentityKey;
|
2013-11-26 01:00:20 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.InvalidKeyException;
|
|
|
|
import org.whispersystems.textsecure.crypto.InvalidMessageException;
|
|
|
|
import org.whispersystems.textsecure.crypto.InvalidVersionException;
|
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2013-11-01 00:23:45 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.protocol.CiphertextMessage;
|
2013-11-26 01:00:20 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.protocol.PreKeyWhisperMessage;
|
2014-02-22 22:22:12 +00:00
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
2013-09-14 20:33:23 +00:00
|
|
|
import org.whispersystems.textsecure.storage.InvalidKeyIdException;
|
2014-02-03 03:38:06 +00:00
|
|
|
import org.whispersystems.textsecure.storage.RecipientDevice;
|
2014-02-16 23:23:49 +00:00
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
2014-02-22 22:22:12 +00:00
|
|
|
import org.whispersystems.textsecure.util.InvalidNumberException;
|
2013-09-14 20:33:23 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-02-22 22:22:12 +00:00
|
|
|
import static org.whispersystems.textsecure.push.PushMessageProtos.IncomingPushMessageSignal.Type;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* Activity for displaying sent/received session keys.
|
2012-09-08 03:03:23 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
2013-10-21 02:12:53 +00:00
|
|
|
public class ReceiveKeyActivity extends Activity {
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
private TextView descriptionText;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private Button confirmButton;
|
|
|
|
private Button cancelButton;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private Recipient recipient;
|
2014-02-03 03:38:06 +00:00
|
|
|
private int recipientDeviceId;
|
2011-12-20 18:20:44 +00:00
|
|
|
private long threadId;
|
2013-05-23 23:36:24 +00:00
|
|
|
private long messageId;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private MasterSecret masterSecret;
|
2013-11-26 01:00:20 +00:00
|
|
|
private PreKeyWhisperMessage keyExchangeMessageBundle;
|
2011-12-20 18:20:44 +00:00
|
|
|
private KeyExchangeMessage keyExchangeMessage;
|
2014-02-16 23:23:49 +00:00
|
|
|
private IdentityKey identityUpdateMessage;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle state) {
|
|
|
|
super.onCreate(state);
|
|
|
|
setContentView(R.layout.receive_key_activity);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
initializeResources();
|
2013-05-23 23:36:24 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
initializeKey();
|
|
|
|
initializeText();
|
|
|
|
} catch (InvalidKeyException ike) {
|
|
|
|
Log.w("ReceiveKeyActivity", ike);
|
|
|
|
} catch (InvalidVersionException ive) {
|
2013-05-23 23:36:24 +00:00
|
|
|
Log.w("ReceiveKeyActivity", ive);
|
2013-11-26 01:00:20 +00:00
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("ReceiveKeyActivity", e);
|
2014-04-10 03:02:46 +00:00
|
|
|
} catch (LegacyMessageException e) {
|
|
|
|
Log.w("ReceiveKeyActivity", e);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
initializeListeners();
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
MemoryCleaner.clean(masterSecret);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void initializeText() {
|
2014-02-16 23:23:49 +00:00
|
|
|
if (isTrusted(keyExchangeMessage, keyExchangeMessageBundle, identityUpdateMessage)) {
|
|
|
|
initializeTrustedText();
|
|
|
|
} else {
|
|
|
|
initializeUntrustedText();
|
|
|
|
}
|
2013-05-24 00:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeTrustedText() {
|
|
|
|
descriptionText.setText(getString(R.string.ReceiveKeyActivity_the_signature_on_this_key_exchange_is_trusted_but));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeUntrustedText() {
|
|
|
|
SpannableString spannableString = new SpannableString(getString(R.string.ReceiveKeyActivity_the_signature_on_this_key_exchange_is_different) + " " +
|
2013-05-23 23:36:24 +00:00
|
|
|
getString(R.string.ReceiveKeyActivity_you_may_wish_to_verify_this_contact));
|
|
|
|
spannableString.setSpan(new ClickableSpan() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View widget) {
|
2014-02-16 23:23:49 +00:00
|
|
|
IdentityKey remoteIdentity;
|
|
|
|
|
|
|
|
if (identityUpdateMessage != null) remoteIdentity = identityUpdateMessage;
|
|
|
|
else if (keyExchangeMessageBundle != null) remoteIdentity = keyExchangeMessageBundle.getIdentityKey();
|
|
|
|
else remoteIdentity = keyExchangeMessage.getIdentityKey();
|
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
Intent intent = new Intent(ReceiveKeyActivity.this, VerifyIdentityActivity.class);
|
|
|
|
intent.putExtra("recipient", recipient);
|
|
|
|
intent.putExtra("master_secret", masterSecret);
|
2014-02-16 23:23:49 +00:00
|
|
|
intent.putExtra("remote_identity", remoteIdentity);
|
2013-05-23 23:36:24 +00:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
2013-05-24 00:17:28 +00:00
|
|
|
}, getString(R.string.ReceiveKeyActivity_the_signature_on_this_key_exchange_is_different).length() +1,
|
|
|
|
spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
2013-05-23 23:36:24 +00:00
|
|
|
|
|
|
|
descriptionText.setText(spannableString);
|
|
|
|
descriptionText.setMovementMethod(LinkMovementMethod.getInstance());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2014-02-16 23:23:49 +00:00
|
|
|
private boolean isTrusted(KeyExchangeMessage message, PreKeyWhisperMessage messageBundle, IdentityKey identityUpdateMessage) {
|
2014-02-03 03:38:06 +00:00
|
|
|
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), recipientDeviceId);
|
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
if (message != null) {
|
|
|
|
KeyExchangeProcessor processor = KeyExchangeProcessor.createFor(this, masterSecret,
|
2014-02-03 03:38:06 +00:00
|
|
|
recipientDevice, message);
|
2013-11-26 01:00:20 +00:00
|
|
|
return processor.isTrusted(message);
|
|
|
|
} else if (messageBundle != null) {
|
2014-02-03 03:38:06 +00:00
|
|
|
KeyExchangeProcessorV2 processor = new KeyExchangeProcessorV2(this, masterSecret, recipientDevice);
|
2013-11-26 01:00:20 +00:00
|
|
|
return processor.isTrusted(messageBundle);
|
2014-02-16 23:23:49 +00:00
|
|
|
} else if (identityUpdateMessage != null) {
|
|
|
|
KeyExchangeProcessorV2 processor = new KeyExchangeProcessorV2(this, masterSecret, recipientDevice);
|
|
|
|
return processor.isTrusted(identityUpdateMessage);
|
2013-11-26 01:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-14 20:33:23 +00:00
|
|
|
}
|
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
private void initializeKey()
|
2014-04-10 03:02:46 +00:00
|
|
|
throws InvalidKeyException, InvalidVersionException,
|
|
|
|
InvalidMessageException, LegacyMessageException
|
2013-11-26 01:00:20 +00:00
|
|
|
{
|
2013-09-14 20:33:23 +00:00
|
|
|
try {
|
|
|
|
String messageBody = getIntent().getStringExtra("body");
|
|
|
|
|
|
|
|
if (getIntent().getBooleanExtra("is_bundle", false)) {
|
2014-02-22 22:22:12 +00:00
|
|
|
boolean isPush = getIntent().getBooleanExtra("is_push", false);
|
|
|
|
byte[] body;
|
|
|
|
|
|
|
|
if (isPush) {
|
|
|
|
body = Base64.decode(messageBody.getBytes());
|
|
|
|
} else {
|
|
|
|
body = new SmsTransportDetails().getDecodedMessage(messageBody.getBytes());
|
|
|
|
}
|
2013-09-14 20:33:23 +00:00
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
this.keyExchangeMessageBundle = new PreKeyWhisperMessage(body);
|
2014-02-16 23:23:49 +00:00
|
|
|
} else if (getIntent().getBooleanExtra("is_identity_update", false)) {
|
|
|
|
this.identityUpdateMessage = new IdentityKey(Base64.decodeWithoutPadding(messageBody), 0);
|
2013-09-14 20:33:23 +00:00
|
|
|
} else {
|
2013-11-26 01:00:20 +00:00
|
|
|
this.keyExchangeMessage = KeyExchangeMessage.createFor(messageBody);
|
2013-09-14 20:33:23 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void initializeResources() {
|
|
|
|
this.descriptionText = (TextView) findViewById(R.id.description_text);
|
|
|
|
this.confirmButton = (Button) findViewById(R.id.ok_button);
|
|
|
|
this.cancelButton = (Button) findViewById(R.id.cancel_button);
|
|
|
|
this.recipient = getIntent().getParcelableExtra("recipient");
|
2014-02-03 03:38:06 +00:00
|
|
|
this.recipientDeviceId = getIntent().getIntExtra("recipient_device_id", -1);
|
2011-12-20 18:20:44 +00:00
|
|
|
this.threadId = getIntent().getLongExtra("thread_id", -1);
|
2013-05-23 23:36:24 +00:00
|
|
|
this.messageId = getIntent().getLongExtra("message_id", -1);
|
2013-09-14 20:33:23 +00:00
|
|
|
this.masterSecret = getIntent().getParcelableExtra("master_secret");
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void initializeListeners() {
|
|
|
|
this.confirmButton.setOnClickListener(new OkListener());
|
|
|
|
this.cancelButton.setOnClickListener(new CancelListener());
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class OkListener implements View.OnClickListener {
|
2013-02-11 01:30:51 +00:00
|
|
|
@Override
|
2011-12-20 18:20:44 +00:00
|
|
|
public void onClick(View v) {
|
2013-05-23 23:36:24 +00:00
|
|
|
new AsyncTask<Void, Void, Void> () {
|
|
|
|
private ProgressDialog dialog;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
2013-09-14 20:33:23 +00:00
|
|
|
dialog = ProgressDialog.show(ReceiveKeyActivity.this,
|
|
|
|
getString(R.string.ReceiveKeyActivity_processing),
|
|
|
|
getString(R.string.ReceiveKeyActivity_processing_key_exchange),
|
|
|
|
true);
|
2013-05-23 23:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
2013-09-14 20:33:23 +00:00
|
|
|
if (keyExchangeMessage != null) {
|
2013-11-26 01:00:20 +00:00
|
|
|
try {
|
2014-02-03 03:38:06 +00:00
|
|
|
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), recipientDeviceId);
|
|
|
|
KeyExchangeProcessor processor = KeyExchangeProcessor.createFor(ReceiveKeyActivity.this, masterSecret, recipientDevice, keyExchangeMessage);
|
2013-11-26 01:00:20 +00:00
|
|
|
processor.processKeyExchangeMessage(keyExchangeMessage, threadId);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(ReceiveKeyActivity.this)
|
|
|
|
.markAsProcessedKeyExchange(messageId);
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("ReceiveKeyActivity", e);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(ReceiveKeyActivity.this)
|
|
|
|
.markAsCorruptKeyExchange(messageId);
|
|
|
|
}
|
2013-09-14 20:33:23 +00:00
|
|
|
} else if (keyExchangeMessageBundle != null) {
|
|
|
|
try {
|
2014-02-03 03:38:06 +00:00
|
|
|
RecipientDevice recipientDevice = new RecipientDevice(recipient.getRecipientId(), recipientDeviceId);
|
2013-11-26 01:00:20 +00:00
|
|
|
KeyExchangeProcessorV2 processor = new KeyExchangeProcessorV2(ReceiveKeyActivity.this,
|
2014-02-03 03:38:06 +00:00
|
|
|
masterSecret, recipientDevice);
|
2013-11-26 01:00:20 +00:00
|
|
|
processor.processKeyExchangeMessage(keyExchangeMessageBundle);
|
|
|
|
|
2014-02-22 22:22:12 +00:00
|
|
|
CiphertextMessage bundledMessage = keyExchangeMessageBundle.getWhisperMessage();
|
2013-09-14 20:33:23 +00:00
|
|
|
|
2014-02-22 22:22:12 +00:00
|
|
|
if (getIntent().getBooleanExtra("is_push", false)) {
|
|
|
|
String source = Util.canonicalizeNumber(ReceiveKeyActivity.this, recipient.getNumber());
|
|
|
|
IncomingPushMessage incoming = new IncomingPushMessage(Type.CIPHERTEXT_VALUE, source, recipientDeviceId, bundledMessage.serialize(), System.currentTimeMillis());
|
|
|
|
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(ReceiveKeyActivity.this)
|
|
|
|
.markAsProcessedKeyExchange(messageId);
|
2013-09-14 20:33:23 +00:00
|
|
|
|
2014-02-22 22:22:12 +00:00
|
|
|
Intent intent = new Intent(ReceiveKeyActivity.this, SendReceiveService.class);
|
|
|
|
intent.setAction(SendReceiveService.RECEIVE_PUSH_ACTION);
|
|
|
|
intent.putExtra("message", incoming);
|
|
|
|
startService(intent);
|
|
|
|
} else {
|
|
|
|
SmsTransportDetails transportDetails = new SmsTransportDetails();
|
|
|
|
String messageBody = new String(transportDetails.getEncodedMessage(bundledMessage.serialize()));
|
|
|
|
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(ReceiveKeyActivity.this)
|
|
|
|
.updateBundleMessageBody(masterSecret, messageId, messageBody);
|
|
|
|
|
|
|
|
DecryptingQueue.scheduleDecryption(ReceiveKeyActivity.this, masterSecret, messageId,
|
|
|
|
threadId, recipient.getNumber(), recipientDeviceId,
|
|
|
|
messageBody, true, false, false);
|
|
|
|
}
|
2013-09-14 20:33:23 +00:00
|
|
|
} catch (InvalidKeyIdException e) {
|
|
|
|
Log.w("ReceiveKeyActivity", e);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(ReceiveKeyActivity.this)
|
|
|
|
.markAsCorruptKeyExchange(messageId);
|
2013-11-01 00:23:45 +00:00
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.w("ReceiveKeyActivity", e);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(ReceiveKeyActivity.this)
|
|
|
|
.markAsCorruptKeyExchange(messageId);
|
2014-02-22 22:22:12 +00:00
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w("ReceiveKeyActivity", e);
|
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(ReceiveKeyActivity.this)
|
|
|
|
.markAsCorruptKeyExchange(messageId);
|
2013-09-14 20:33:23 +00:00
|
|
|
}
|
2014-02-16 23:23:49 +00:00
|
|
|
} else if (identityUpdateMessage != null) {
|
|
|
|
DatabaseFactory.getIdentityDatabase(ReceiveKeyActivity.this)
|
|
|
|
.saveIdentity(masterSecret, recipient.getRecipientId(), identityUpdateMessage);
|
|
|
|
|
|
|
|
DatabaseFactory.getSmsDatabase(ReceiveKeyActivity.this).markAsProcessedKeyExchange(messageId);
|
2013-09-14 20:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-23 23:36:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
dialog.dismiss();
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}.execute();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class CancelListener implements View.OnClickListener {
|
2013-02-11 01:30:51 +00:00
|
|
|
@Override
|
2011-12-20 18:20:44 +00:00
|
|
|
public void onClick(View v) {
|
|
|
|
ReceiveKeyActivity.this.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|