Signal-Android/src/org/thoughtcrime/securesms/crypto/KeyExchangeInitiator.java

70 lines
3 KiB
Java
Raw Normal View History

/**
2011-12-20 10:20:44 -08:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 10:20:44 -08: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 10:20:44 -08: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.crypto;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
2011-12-20 10:20:44 -08:00
import org.thoughtcrime.securesms.R;
2011-12-20 10:20:44 -08:00
import org.thoughtcrime.securesms.database.LocalKeyRecord;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.sms.MessageSender;
import java.util.LinkedList;
2011-12-20 10:20:44 -08:00
public class KeyExchangeInitiator {
public static void initiate(final Context context, final MasterSecret masterSecret, final Recipient recipient, boolean promptOnExisting) {
if (promptOnExisting && hasInitiatedSession(context, masterSecret, recipient)) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(R.string.KeyExchangeInitiator_initiate_despite_existing_request_question);
dialog.setMessage(R.string.KeyExchangeInitiator_youve_already_sent_a_session_initiation_request_to_this_recipient_are_you_sure);
2011-12-20 10:20:44 -08:00
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.setCancelable(true);
dialog.setPositiveButton(R.string.KeyExchangeInitiator_send, new DialogInterface.OnClickListener() {
2011-12-20 10:20:44 -08:00
public void onClick(DialogInterface dialog, int which) {
initiateKeyExchange(context, masterSecret, recipient);
}
});
dialog.setNegativeButton(android.R.string.cancel, null);
2011-12-20 10:20:44 -08:00
dialog.show();
} else {
initiateKeyExchange(context, masterSecret, recipient);
}
}
2011-12-20 10:20:44 -08:00
private static void initiateKeyExchange(Context context, MasterSecret masterSecret, Recipient recipient) {
LocalKeyRecord record = KeyUtil.initializeRecordFor(recipient, context, masterSecret);
KeyExchangeMessage message = new KeyExchangeMessage(context, masterSecret, 1, record, 0);
2011-12-20 10:20:44 -08:00
Log.w("SendKeyActivity", "Sending public key: " + record.getCurrentKeyPair().getPublicKey().getFingerprint());
LinkedList<Recipient> list = new LinkedList<Recipient>();
list.add(recipient);
MessageSender.send(context, masterSecret, new Recipients(list), -1, message.serialize(), true);
2011-12-20 10:20:44 -08:00
}
private static boolean hasInitiatedSession(Context context, MasterSecret masterSecret, Recipient recipient) {
return
2011-12-20 10:20:44 -08:00
LocalKeyRecord.hasRecord(context, recipient) &&
new LocalKeyRecord(context, masterSecret, recipient).getCurrentKeyPair() != null;
}
}