session-android/src/org/thoughtcrime/securesms/ReceiveKeyActivity.java

207 lines
8.6 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
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.
*
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;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
2011-12-20 18:20:44 +00:00
import org.thoughtcrime.securesms.crypto.IdentityKey;
import org.thoughtcrime.securesms.crypto.InvalidKeyException;
import org.thoughtcrime.securesms.crypto.InvalidVersionException;
import org.thoughtcrime.securesms.crypto.KeyExchangeMessage;
import org.thoughtcrime.securesms.crypto.KeyExchangeProcessor;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.MemoryCleaner;
/**
* Activity for displaying sent/received session keys.
*
2011-12-20 18:20:44 +00:00
* @author Moxie Marlinspike
*/
public class ReceiveKeyActivity extends Activity {
private TextView descriptionText;
private TextView signatureText;
2011-12-20 18:20:44 +00:00
private Button confirmButton;
private Button cancelButton;
private Button verifySessionButton;
private Button verifyIdentityButton;
2011-12-20 18:20:44 +00:00
private Recipient recipient;
private long threadId;
2011-12-20 18:20:44 +00:00
private MasterSecret masterSecret;
private KeyExchangeMessage keyExchangeMessage;
private KeyExchangeProcessor keyExchangeProcessor;
2011-12-20 18:20:44 +00:00
private boolean sent;
2011-12-20 18:20:44 +00:00
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.receive_key_activity);
2011-12-20 18:20:44 +00:00
initializeResources();
try {
initializeKey();
initializeText();
} catch (InvalidKeyException ike) {
Log.w("ReceiveKeyActivity", ike);
initializeCorruptedKeyText();
} catch (InvalidVersionException ive) {
initializeBadVersionText();
}
initializeListeners();
}
2011-12-20 18:20:44 +00:00
@Override
protected void onDestroy() {
MemoryCleaner.clean(masterSecret);
super.onDestroy();
}
2011-12-20 18:20:44 +00:00
private void initializeText() {
if (keyExchangeProcessor.hasCompletedSession()) initializeTextForExistingSession();
else initializeTextForNewSession();
2011-12-20 18:20:44 +00:00
initializeSignatureText();
}
private void initializeCorruptedKeyText() {
descriptionText.setText(R.string.ReceiveKeyActivity_error_you_have_received_a_corrupted_public_key);
2011-12-20 18:20:44 +00:00
confirmButton.setVisibility(View.GONE);
}
2011-12-20 18:20:44 +00:00
private void initializeBadVersionText() {
descriptionText.setText(R.string.ReceiveKeyActivity_error_you_have_received_a_public_key_from_an_unsupported_version_of_the_protocol);
confirmButton.setVisibility(View.GONE);
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
private void initializeSignatureText() {
if (!keyExchangeMessage.hasIdentityKey()) {
signatureText.setText(R.string.ReceiveKeyActivity_this_key_exchange_message_does_not_include_an_identity_signature);
2011-12-20 18:20:44 +00:00
return;
}
2011-12-20 18:20:44 +00:00
IdentityKey identityKey = keyExchangeMessage.getIdentityKey();
String identityName = DatabaseFactory.getIdentityDatabase(this).getNameForIdentity(masterSecret, identityKey);
2011-12-20 18:20:44 +00:00
if (identityName == null) {
signatureText.setText(R.string.ReceiveKeyActivity_this_key_exchange_message_includes_an_identity_signature_but_you_do_not_yet_trust_it);
2011-12-20 18:20:44 +00:00
} else {
signatureText.setText(String.format(getString(R.string.ReceiveKeyActivity_this_key_exchange_message_includes_an_identity_signature_which_you_trust_for_s), identityName));
2011-12-20 18:20:44 +00:00
}
}
2011-12-20 18:20:44 +00:00
private void initializeTextForExistingSession() {
if (keyExchangeProcessor.isRemoteKeyExchangeForExistingSession(keyExchangeMessage)) {
descriptionText.setText(String.format(getString(R.string.ReceiveKeyActivity_this_is_the_key_that_you_sent_to_start_your_current_encrypted_session_with_s), recipient.toShortString()));
2011-12-20 18:20:44 +00:00
this.confirmButton.setVisibility(View.GONE);
this.verifySessionButton.setVisibility(View.VISIBLE);
this.verifyIdentityButton.setVisibility(View.VISIBLE);
} else if (keyExchangeProcessor.isLocalKeyExchangeForExistingSession(keyExchangeMessage)) {
descriptionText.setText(String.format(getString(R.string.ReceiveKeyActivity_this_is_the_key_that_you_received_to_start_your_current_encrypted_session_with_s), recipient.toShortString()));
2011-12-20 18:20:44 +00:00
this.confirmButton.setVisibility(View.GONE);
this.verifySessionButton.setVisibility(View.VISIBLE);
this.verifyIdentityButton.setVisibility(View.VISIBLE);
} else {
descriptionText.setText(String.format(getString(R.string.ReceiveKeyActivity_you_have_received_a_key_exchange_message_from_s_warning_you_already_have_an_encrypted_session), recipient.toShortString()));
2011-12-20 18:20:44 +00:00
this.confirmButton.setVisibility(View.VISIBLE);
this.verifyIdentityButton.setVisibility(View.GONE);
this.verifySessionButton.setVisibility(View.GONE);
}
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
private void initializeTextForNewSession() {
if (keyExchangeProcessor.hasInitiatedSession() && !this.sent)
descriptionText.setText(String.format(getString(R.string.ReceiveKeyActivity_you_have_received_a_key_exchange_message_from_s_you_have_previously_initiated), recipient.toShortString()));
2011-12-20 18:20:44 +00:00
else if (keyExchangeProcessor.hasInitiatedSession() && this.sent)
descriptionText.setText(String.format(getString(R.string.ReceiveKeyActivity_you_have_initiated_a_key_exchange_message_with_s_but_have_not_yet_received_a_reply), recipient.toShortString()));
2011-12-20 18:20:44 +00:00
else if (!keyExchangeProcessor.hasInitiatedSession() && !this.sent)
descriptionText.setText(String.format(getString(R.string.ReceiveKeyActivity_you_have_received_a_key_exchange_message_from_s_you_have_no_existing_session), recipient.toShortString()));
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
private void initializeKey() throws InvalidKeyException, InvalidVersionException {
String messageBody = getIntent().getStringExtra("body");
this.keyExchangeMessage = new KeyExchangeMessage(messageBody);
}
2011-12-20 18:20:44 +00:00
private void initializeResources() {
this.descriptionText = (TextView) findViewById(R.id.description_text);
this.signatureText = (TextView) findViewById(R.id.signature_text);
this.confirmButton = (Button) findViewById(R.id.ok_button);
this.cancelButton = (Button) findViewById(R.id.cancel_button);
this.verifyIdentityButton = (Button) findViewById(R.id.verify_identity_button);
this.verifySessionButton = (Button) findViewById(R.id.verify_session_button);
2011-12-20 18:20:44 +00:00
this.recipient = getIntent().getParcelableExtra("recipient");
this.threadId = getIntent().getLongExtra("thread_id", -1);
this.masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret");
this.sent = getIntent().getBooleanExtra("sent", false);
this.keyExchangeProcessor = new KeyExchangeProcessor(this, masterSecret, recipient);
}
2011-12-20 18:20:44 +00:00
private void initializeListeners() {
this.confirmButton.setOnClickListener(new OkListener());
this.cancelButton.setOnClickListener(new CancelListener());
this.verifyIdentityButton.setOnClickListener(new VerifyIdentityListener());
this.verifySessionButton.setOnClickListener(new VerifySessionListener());
}
2011-12-20 18:20:44 +00:00
private class VerifyIdentityListener implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(ReceiveKeyActivity.this, VerifyIdentityActivity.class);
intent.putExtra("recipient", recipient);
intent.putExtra("master_secret", masterSecret);
startActivity(intent);
finish();
}
}
2011-12-20 18:20:44 +00:00
private class VerifySessionListener implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(ReceiveKeyActivity.this, VerifyKeysActivity.class);
intent.putExtra("recipient", recipient);
intent.putExtra("master_secret", masterSecret);
startActivity(intent);
finish();
}
}
2011-12-20 18:20:44 +00:00
private class OkListener implements View.OnClickListener {
public void onClick(View v) {
keyExchangeProcessor.processKeyExchangeMessage(keyExchangeMessage, threadId);
2011-12-20 18:20:44 +00:00
finish();
}
}
2011-12-20 18:20:44 +00:00
private class CancelListener implements View.OnClickListener {
public void onClick(View v) {
ReceiveKeyActivity.this.finish();
}
}
}