aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-02-28 00:22:56 +0100
committerDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-02-28 00:22:56 +0100
commit37d1a5380634471b11c828cf265d4a39e818a1c1 (patch)
treebbd6d58171beb79369c9c452098286591e6a6eae /src
parentbfee69b00b2762a910f7f3f1714d18fddc99a9ad (diff)
basic pgp support.
Diffstat (limited to '')
-rw-r--r--src/de/gultsch/chat/crypto/OtrEngine.java2
-rw-r--r--src/de/gultsch/chat/crypto/PgpEngine.java148
-rw-r--r--src/de/gultsch/chat/entities/Account.java9
-rw-r--r--src/de/gultsch/chat/entities/Contact.java20
-rw-r--r--src/de/gultsch/chat/entities/Message.java6
-rw-r--r--src/de/gultsch/chat/services/XmppConnectionService.java118
-rw-r--r--src/de/gultsch/chat/ui/ConversationActivity.java19
-rw-r--r--src/de/gultsch/chat/ui/ConversationFragment.java193
-rw-r--r--src/de/gultsch/chat/ui/DialogContactDetails.java2
-rw-r--r--src/de/gultsch/chat/ui/ManageAccountActivity.java30
-rw-r--r--src/de/gultsch/chat/utils/MessageParser.java15
11 files changed, 514 insertions, 48 deletions
diff --git a/src/de/gultsch/chat/crypto/OtrEngine.java b/src/de/gultsch/chat/crypto/OtrEngine.java
index 7be9bed3..cdd71d61 100644
--- a/src/de/gultsch/chat/crypto/OtrEngine.java
+++ b/src/de/gultsch/chat/crypto/OtrEngine.java
@@ -93,8 +93,6 @@ public class OtrEngine implements OtrEngineHost {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
}
}
diff --git a/src/de/gultsch/chat/crypto/PgpEngine.java b/src/de/gultsch/chat/crypto/PgpEngine.java
new file mode 100644
index 00000000..6d94e577
--- /dev/null
+++ b/src/de/gultsch/chat/crypto/PgpEngine.java
@@ -0,0 +1,148 @@
+package de.gultsch.chat.crypto;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import org.openintents.openpgp.OpenPgpError;
+import org.openintents.openpgp.OpenPgpSignatureResult;
+import org.openintents.openpgp.util.OpenPgpApi;
+import org.openintents.openpgp.util.OpenPgpConstants;
+
+import android.app.PendingIntent;
+import android.os.Bundle;
+import android.util.Log;
+
+public class PgpEngine {
+ private OpenPgpApi api;
+
+ public PgpEngine(OpenPgpApi api) {
+ this.api = api;
+ }
+
+ public String decrypt(String message) throws UserInputRequiredException,
+ OpenPgpException {
+ InputStream is = new ByteArrayInputStream(message.getBytes());
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ Bundle result = api.decryptAndVerify(is, os);
+ switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
+ case OpenPgpConstants.RESULT_CODE_SUCCESS:
+ return os.toString();
+ case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED:
+ throw new UserInputRequiredException(
+ (PendingIntent) result
+ .getParcelable(OpenPgpConstants.RESULT_INTENT));
+ case OpenPgpConstants.RESULT_CODE_ERROR:
+ throw new OpenPgpException(
+ (OpenPgpError) result
+ .getParcelable(OpenPgpConstants.RESULT_ERRORS));
+ default:
+ return null;
+ }
+ }
+
+ public String encrypt(long keyId, String message) {
+ Bundle params = new Bundle();
+ params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);
+ long[] keyIds = { keyId };
+ params.putLongArray(OpenPgpConstants.PARAMS_KEY_IDS, keyIds);
+
+ InputStream is = new ByteArrayInputStream(message.getBytes());
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ Bundle result = api.encrypt(params, is, os);
+ StringBuilder encryptedMessageBody = new StringBuilder();
+ String[] lines = os.toString().split("\n");
+ for (int i = 3; i < lines.length - 1; ++i) {
+ encryptedMessageBody.append(lines[i].trim());
+ }
+ return encryptedMessageBody.toString();
+ }
+
+ public long fetchKeyId(String status, String signature)
+ throws OpenPgpException {
+ StringBuilder pgpSig = new StringBuilder();
+ pgpSig.append("-----BEGIN PGP SIGNED MESSAGE-----");
+ pgpSig.append('\n');
+ pgpSig.append("Hash: SHA1");
+ pgpSig.append('\n');
+ pgpSig.append('\n');
+ pgpSig.append(status);
+ pgpSig.append('\n');
+ pgpSig.append("-----BEGIN PGP SIGNATURE-----");
+ pgpSig.append('\n');
+ pgpSig.append('\n');
+ pgpSig.append(signature.replace("\n", "").trim());
+ pgpSig.append('\n');
+ pgpSig.append("-----END PGP SIGNATURE-----");
+ Bundle params = new Bundle();
+ params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);
+ InputStream is = new ByteArrayInputStream(pgpSig.toString().getBytes());
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ Bundle result = api.decryptAndVerify(params, is, os);
+ switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
+ case OpenPgpConstants.RESULT_CODE_SUCCESS:
+ OpenPgpSignatureResult sigResult = result
+ .getParcelable(OpenPgpConstants.RESULT_SIGNATURE);
+ return sigResult.getKeyId();
+ case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED:
+ break;
+ case OpenPgpConstants.RESULT_CODE_ERROR:
+ throw new OpenPgpException(
+ (OpenPgpError) result
+ .getParcelable(OpenPgpConstants.RESULT_ERRORS));
+ }
+ return 0;
+ }
+
+ public String generateSignature(String status)
+ throws UserInputRequiredException {
+ Bundle params = new Bundle();
+ params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);
+ InputStream is = new ByteArrayInputStream(status.getBytes());
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ Bundle result = api.sign(params, is, os);
+ StringBuilder signatureBuilder = new StringBuilder();
+ switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
+ case OpenPgpConstants.RESULT_CODE_SUCCESS:
+ String[] lines = os.toString().split("\n");
+ for (int i = 7; i < lines.length - 1; ++i) {
+ signatureBuilder.append(lines[i].trim());
+ }
+ break;
+ case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED:
+ UserInputRequiredException exception = new UserInputRequiredException(
+ (PendingIntent) result
+ .getParcelable(OpenPgpConstants.RESULT_INTENT));
+ throw exception;
+ case OpenPgpConstants.RESULT_CODE_ERROR:
+ break;
+ }
+ return signatureBuilder.toString();
+ }
+
+ public class UserInputRequiredException extends Exception {
+ private static final long serialVersionUID = -6913480043269132016L;
+ private PendingIntent pi;
+
+ public UserInputRequiredException(PendingIntent pi) {
+ this.pi = pi;
+ }
+
+ public PendingIntent getPendingIntent() {
+ return this.pi;
+ }
+ }
+
+ public class OpenPgpException extends Exception {
+ private static final long serialVersionUID = -7324789703473056077L;
+ private OpenPgpError error;
+
+ public OpenPgpException(OpenPgpError openPgpError) {
+ this.error = openPgpError;
+ }
+
+ public OpenPgpError getOpenPgpError() {
+ return this.error;
+ }
+ }
+}
diff --git a/src/de/gultsch/chat/entities/Account.java b/src/de/gultsch/chat/entities/Account.java
index 3ffc75f9..c8f6d063 100644
--- a/src/de/gultsch/chat/entities/Account.java
+++ b/src/de/gultsch/chat/entities/Account.java
@@ -137,8 +137,13 @@ public class Account extends AbstractEntity{
return keys;
}
- public void setKey(String keyName, String keyValue) throws JSONException {
- this.keys.put(keyName, keyValue);
+ public boolean setKey(String keyName, String keyValue) {
+ try {
+ this.keys.put(keyName, keyValue);
+ return true;
+ } catch (JSONException e) {
+ return false;
+ }
}
@Override
diff --git a/src/de/gultsch/chat/entities/Contact.java b/src/de/gultsch/chat/entities/Contact.java
index a0dbf22f..a8c9fa62 100644
--- a/src/de/gultsch/chat/entities/Contact.java
+++ b/src/de/gultsch/chat/entities/Contact.java
@@ -221,6 +221,26 @@ public class Contact extends AbstractEntity implements Serializable {
}
}
+ public void setPgpKeyId(long keyId) {
+ try {
+ this.keys.put("pgp_keyid", keyId);
+ } catch (JSONException e) {
+
+ }
+ }
+
+ public long getPgpKeyId() {
+ if (this.keys.has("pgp_keyid")) {
+ try {
+ return this.keys.getLong("pgp_keyid");
+ } catch (JSONException e) {
+ return 0;
+ }
+ } else {
+ return 0;
+ }
+ }
+
public void setSubscriptionOption(int option) {
this.subscription |= 1 << option;
}
diff --git a/src/de/gultsch/chat/entities/Message.java b/src/de/gultsch/chat/entities/Message.java
index 9b308c75..58e4ef63 100644
--- a/src/de/gultsch/chat/entities/Message.java
+++ b/src/de/gultsch/chat/entities/Message.java
@@ -17,6 +17,7 @@ public class Message extends AbstractEntity {
public static final int ENCRYPTION_NONE = 0;
public static final int ENCRYPTION_PGP = 1;
public static final int ENCRYPTION_OTR = 2;
+ public static final int ENCRYPTION_DECRYPTED = 3;
public static String CONVERSATION = "conversationUuid";
public static String COUNTERPART = "counterpart";
@@ -136,5 +137,8 @@ public class Message extends AbstractEntity {
public void setEncryption(int encryption) {
this.encryption = encryption;
}
-
+
+ public void setBody(String body) {
+ this.body = body;
+ }
}
diff --git a/src/de/gultsch/chat/services/XmppConnectionService.java b/src/de/gultsch/chat/services/XmppConnectionService.java
index ee8fd9cb..a2b85f62 100644
--- a/src/de/gultsch/chat/services/XmppConnectionService.java
+++ b/src/de/gultsch/chat/services/XmppConnectionService.java
@@ -1,5 +1,16 @@
package de.gultsch.chat.services;
+import java.io.BufferedOutputStream;
+import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -8,11 +19,21 @@ import java.util.Hashtable;
import java.util.List;
import java.util.Set;
+import org.json.JSONException;
+import org.openintents.openpgp.IOpenPgpService;
+import org.openintents.openpgp.OpenPgpSignatureResult;
+import org.openintents.openpgp.util.OpenPgpApi;
+import org.openintents.openpgp.util.OpenPgpServiceConnection;
+import org.openintents.openpgp.OpenPgpError;
+
import net.java.otr4j.OtrException;
import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus;
+import de.gultsch.chat.crypto.PgpEngine;
+import de.gultsch.chat.crypto.PgpEngine.OpenPgpException;
+import de.gultsch.chat.crypto.PgpEngine.UserInputRequiredException;
import de.gultsch.chat.entities.Account;
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation;
@@ -46,7 +67,9 @@ import android.database.DatabaseUtils;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
+import android.os.ParcelFileDescriptor;
import android.os.PowerManager;
+import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;
@@ -84,7 +107,12 @@ public class XmppConnectionService extends Service {
Message message = null;
boolean notify = false;
if ((packet.getType() == MessagePacket.TYPE_CHAT)) {
- if (packet.hasChild("body")
+ String pgpBody = MessageParser.getPgpBody(packet);
+ if (pgpBody != null) {
+ message = MessageParser.parsePgpChat(pgpBody, packet,
+ account, service);
+ notify = false;
+ } else if (packet.hasChild("body")
&& (packet.getBody().startsWith("?OTR"))) {
message = MessageParser.parseOtrChat(packet, account,
service);
@@ -164,6 +192,13 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) {
convChangedListener.onConversationListChanged();
}
+ if (account.getKeys().has("pgp_signature")) {
+ try {
+ sendPgpPresence(account, account.getKeys().getString("pgp_signature"));
+ } catch (JSONException e) {
+ //
+ }
+ }
}
}
};
@@ -195,6 +230,18 @@ public class XmppConnectionService extends Service {
} else if (show.getContent().equals("dnd")) {
contact.updatePresence(fromParts[1], Presences.DND);
}
+ Element x = packet.findChild("x");
+ if ((x != null)
+ && (x.getAttribute("xmlns").equals("jabber:x:signed"))) {
+ try {
+ Log.d(LOGTAG,"pgp signature for contact" +packet.getAttribute("from"));
+ contact.setPgpKeyId(getPgpEngine().fetchKeyId(packet.findChild("status")
+ .getContent(), x.getContent()));
+ databaseBackend.updateContact(contact);
+ } catch (OpenPgpException e) {
+ Log.d(LOGTAG,"faulty pgp. just ignore");
+ }
+ }
databaseBackend.updateContact(contact);
} else if (type.equals("unavailable")) {
if (fromParts.length != 2) {
@@ -242,6 +289,23 @@ public class XmppConnectionService extends Service {
}
};
+ private OpenPgpServiceConnection pgpServiceConnection;
+ private PgpEngine mPgpEngine = null;
+
+ public PgpEngine getPgpEngine() {
+ if (pgpServiceConnection.isBound()) {
+ if (this.mPgpEngine == null) {
+ this.mPgpEngine = new PgpEngine(new OpenPgpApi(
+ getApplicationContext(),
+ pgpServiceConnection.getService()));
+ }
+ return mPgpEngine;
+ } else {
+ return null;
+ }
+
+ }
+
private void processRosterItems(Account account, Element elements) {
for (Element item : elements.getChildren()) {
if (item.getName().equals("item")) {
@@ -307,6 +371,9 @@ public class XmppConnectionService extends Service {
getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
+ this.pgpServiceConnection = new OpenPgpServiceConnection(
+ getApplicationContext(), "org.sufficientlysecure.keychain");
+ this.pgpServiceConnection.bindToService();
}
@Override
@@ -332,7 +399,8 @@ public class XmppConnectionService extends Service {
return connection;
}
- public void sendMessage(Account account, Message message, String presence) {
+ public void sendMessage(Message message, String presence) {
+ Account account = message.getConversation().getAccount();
Conversation conv = message.getConversation();
boolean saveInDb = false;
boolean addToConversation = false;
@@ -352,6 +420,25 @@ public class XmppConnectionService extends Service {
}
saveInDb = true;
addToConversation = true;
+ } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
+ long keyId = message.getConversation().getContact()
+ .getPgpKeyId();
+ packet = new MessagePacket();
+ packet.setType(MessagePacket.TYPE_CHAT);
+ packet.setFrom(message.getConversation().getAccount()
+ .getFullJid());
+ packet.setTo(message.getCounterpart());
+ packet.setBody("This is an XEP-0027 encryted message");
+ Element x = new Element("x");
+ x.setAttribute("xmlns", "jabber:x:encrypted");
+ x.setContent(this.getPgpEngine().encrypt(keyId,
+ message.getBody()));
+ packet.addChild(x);
+ account.getXmppConnection().sendMessagePacket(packet);
+ message.setStatus(Message.STATUS_SEND);
+ message.setEncryption(Message.ENCRYPTION_DECRYPTED);
+ saveInDb = true;
+ addToConversation = true;
} else {
// don't encrypt
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
@@ -777,6 +864,10 @@ public class XmppConnectionService extends Service {
databaseBackend.updateContact(contact);
}
+ public void updateMessage(Message message) {
+ databaseBackend.updateMessage(message);
+ }
+
public void createContact(Contact contact) {
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
@@ -841,4 +932,27 @@ public class XmppConnectionService extends Service {
Log.d(LOGTAG, packet.toString());
contact.getAccount().getXmppConnection().sendPresencePacket(packet);
}
+
+ public void sendPgpPresence(Account account, String signature) {
+ PresencePacket packet = new PresencePacket();
+ packet.setAttribute("from", account.getFullJid());
+ Element status = new Element("status");
+ status.setContent("online");
+ packet.addChild(status);
+ Element x = new Element("x");
+ x.setAttribute("xmlns", "jabber:x:signed");
+ x.setContent(signature);
+ packet.addChild(x);
+ account.getXmppConnection().sendPresencePacket(packet);
+ }
+
+ public void generatePgpAnnouncement(Account account)
+ throws PgpEngine.UserInputRequiredException {
+ if (account.getStatus() == Account.STATUS_ONLINE) {
+ String signature = getPgpEngine().generateSignature("online");
+ account.setKey("pgp_signature", signature);
+ databaseBackend.updateAccount(account);
+ sendPgpPresence(account, signature);
+ }
+ }
} \ No newline at end of file
diff --git a/src/de/gultsch/chat/ui/ConversationActivity.java b/src/de/gultsch/chat/ui/ConversationActivity.java
index f9a924ec..7fbc432e 100644
--- a/src/de/gultsch/chat/ui/ConversationActivity.java
+++ b/src/de/gultsch/chat/ui/ConversationActivity.java
@@ -5,8 +5,13 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
+import org.openintents.openpgp.OpenPgpSignatureResult;
+import org.openintents.openpgp.util.OpenPgpConstants;
+
import de.gultsch.chat.R;
import de.gultsch.chat.R.id;
+import de.gultsch.chat.crypto.PgpEngine;
+import de.gultsch.chat.crypto.PgpEngine.UserInputRequiredException;
import de.gultsch.chat.entities.Account;
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation;
@@ -20,6 +25,7 @@ import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
+import android.content.IntentSender.SendIntentException;
import android.graphics.Typeface;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
@@ -45,7 +51,8 @@ public class ConversationActivity extends XmppActivity {
public static final String VIEW_CONVERSATION = "viewConversation";
public static final String CONVERSATION = "conversationUuid";
- public static final int INSERT_CONTACT = 0x9889;
+ public static final int REQUEST_SEND_MESSAGE = 0x75441;
+ public static final int REQUEST_DECRYPT_PGP = 0x76783;
protected SlidingPaneLayout spl;
@@ -354,6 +361,9 @@ public class ConversationActivity extends XmppActivity {
case Message.ENCRYPTION_PGP:
popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
break;
+ case Message.ENCRYPTION_DECRYPTED:
+ popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
+ break;
default:
popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
break;
@@ -459,11 +469,4 @@ public class ConversationActivity extends XmppActivity {
}
}
}
-
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode==INSERT_CONTACT) {
- Log.d("xmppService","contact inserted");
- this.contactInserted = true;
- }
- }
}
diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java
index b3a6ad77..099f2253 100644
--- a/src/de/gultsch/chat/ui/ConversationFragment.java
+++ b/src/de/gultsch/chat/ui/ConversationFragment.java
@@ -3,17 +3,16 @@ package de.gultsch.chat.ui;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Hashtable;
+import java.util.LinkedList;
import java.util.List;
import java.util.Set;
-import javax.crypto.spec.PSource;
-
-import net.java.otr4j.OtrException;
import net.java.otr4j.session.SessionStatus;
import de.gultsch.chat.R;
+import de.gultsch.chat.crypto.PgpEngine.OpenPgpException;
+import de.gultsch.chat.crypto.PgpEngine.UserInputRequiredException;
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.entities.Message;
@@ -23,18 +22,20 @@ import de.gultsch.chat.utils.UIHelper;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
+import android.content.IntentSender;
import android.content.SharedPreferences;
+import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.net.Uri;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
-import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
@@ -42,6 +43,7 @@ import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ImageButton;
import android.widget.ImageView;
+import android.widget.ProgressBar;
import android.widget.TextView;
public class ConversationFragment extends Fragment {
@@ -54,27 +56,45 @@ public class ConversationFragment extends Fragment {
protected Contact contact;
protected BitmapCache mBitmapCache = new BitmapCache();
+ protected String queuedPqpMessage = null;
+
private EditText chatMsg;
-
+
protected Bitmap selfBitmap;
+
+ private IntentSender askForPassphraseIntent = null;
private OnClickListener sendMsgListener = new OnClickListener() {
@Override
public void onClick(View v) {
- ConversationActivity activity = (ConversationActivity) getActivity();
- final XmppConnectionService xmppService = activity.xmppConnectionService;
if (chatMsg.getText().length() < 1)
return;
Message message = new Message(conversation, chatMsg.getText()
.toString(), conversation.nextMessageEncryption);
if (conversation.nextMessageEncryption == Message.ENCRYPTION_OTR) {
sendOtrMessage(message);
+ } else if (conversation.nextMessageEncryption == Message.ENCRYPTION_PGP) {
+ sendPgpMessage(message);
} else {
sendPlainTextMessage(message);
}
}
};
+ protected OnClickListener clickToDecryptListener = new OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ Log.d("gultsch","clicked to decrypt");
+ if (askForPassphraseIntent!=null) {
+ try {
+ getActivity().startIntentSenderForResult(askForPassphraseIntent, ConversationActivity.REQUEST_DECRYPT_PGP, null, 0, 0, 0);
+ } catch (SendIntentException e) {
+ Log.d("gultsch","couldnt fire intent");
+ }
+ }
+ }
+ };
public void updateChatMsgHint() {
if (conversation.getMode() == Conversation.MODE_MULTI) {
@@ -89,6 +109,10 @@ public class ConversationFragment extends Fragment {
break;
case Message.ENCRYPTION_PGP:
chatMsg.setHint("Send openPGP encryted messeage");
+ break;
+ case Message.ENCRYPTION_DECRYPTED:
+ chatMsg.setHint("Send openPGP encryted messeage");
+ break;
default:
break;
}
@@ -107,9 +131,9 @@ public class ConversationFragment extends Fragment {
ImageButton sendButton = (ImageButton) view
.findViewById(R.id.textSendButton);
sendButton.setOnClickListener(this.sendMsgListener);
-
- messagesView = (ListView) view.findViewById(R.id.messages_view);
+ messagesView = (ListView) view.findViewById(R.id.messages_view);
+
messageListAdapter = new ArrayAdapter<Message>(this.getActivity()
.getApplicationContext(), R.layout.message_sent,
this.messageList) {
@@ -155,23 +179,28 @@ public class ConversationFragment extends Fragment {
viewHolder.imageView = (ImageView) view
.findViewById(R.id.message_photo);
if (item.getConversation().getMode() == Conversation.MODE_SINGLE) {
- Uri uri = item.getConversation().getProfilePhotoUri();
+ Uri uri = item.getConversation()
+ .getProfilePhotoUri();
if (uri != null) {
viewHolder.imageView
.setImageBitmap(mBitmapCache.get(item
- .getConversation().getName(), uri));
+ .getConversation().getName(),
+ uri));
} else {
viewHolder.imageView
.setImageBitmap(mBitmapCache.get(item
- .getConversation().getName(), null));
+ .getConversation().getName(),
+ null));
}
}
break;
case ERROR:
- view = (View) inflater.inflate(R.layout.message_error, null);
+ view = (View) inflater.inflate(R.layout.message_error,
+ null);
viewHolder.imageView = (ImageView) view
.findViewById(R.id.message_photo);
- viewHolder.imageView.setImageBitmap(mBitmapCache.getError());
+ viewHolder.imageView.setImageBitmap(mBitmapCache
+ .getError());
break;
default:
viewHolder = null;
@@ -186,7 +215,7 @@ public class ConversationFragment extends Fragment {
viewHolder = (ViewHolder) view.getTag();
}
if (type == RECIEVED) {
- if (item.getConversation().getMode() == Conversation.MODE_MULTI) {
+ if (item.getConversation().getMode() == Conversation.MODE_MULTI) {
if (item.getCounterpart() != null) {
viewHolder.imageView.setImageBitmap(mBitmapCache
.get(item.getCounterpart(), null));
@@ -199,7 +228,15 @@ public class ConversationFragment extends Fragment {
}
String body = item.getBody();
if (body != null) {
- viewHolder.messageBody.setText(body.trim());
+ if (item.getEncryption() == Message.ENCRYPTION_PGP) {
+ viewHolder.messageBody.setText(getString(R.string.encrypted_message));
+ viewHolder.messageBody.setTextColor(0xff33B5E5);
+ viewHolder.messageBody.setOnClickListener(clickToDecryptListener);
+ } else {
+ viewHolder.messageBody.setText(body.trim());
+ viewHolder.messageBody.setTextColor(0xff000000);
+ viewHolder.messageBody.setOnClickListener(null);
+ }
}
if (item.getStatus() == Message.STATUS_UNSEND) {
viewHolder.time.setTypeface(null, Typeface.ITALIC);
@@ -236,7 +273,7 @@ public class ConversationFragment extends Fragment {
if (showPhoneSelfContactPicture) {
Uri selfiUri = PhoneHelper.getSefliUri(getActivity());
- if (selfiUri!=null) {
+ if (selfiUri != null) {
try {
self = BitmapFactory.decodeStream(getActivity()
.getContentResolver().openInputStream(selfiUri));
@@ -245,7 +282,7 @@ public class ConversationFragment extends Fragment {
}
}
}
- if (self==null){
+ if (self == null) {
self = UIHelper.getUnknownContactPicture(conversation.getAccount()
.getJid(), 200);
}
@@ -280,17 +317,38 @@ public class ConversationFragment extends Fragment {
activity.updateConversationList();
}
}
+ if (queuedPqpMessage != null) {
+ this.conversation.nextMessageEncryption = Message.ENCRYPTION_PGP;
+ Message message = new Message(conversation, queuedPqpMessage,
+ Message.ENCRYPTION_PGP);
+ sendPgpMessage(message);
+ }
}
public void updateMessages() {
ConversationActivity activity = (ConversationActivity) getActivity();
+ List<Message> encryptedMessages = new LinkedList<Message>();
+ for(Message message : this.conversation.getMessages()) {
+ if (message.getEncryption() == Message.ENCRYPTION_PGP) {
+ encryptedMessages.add(message);
+ }
+ }
+ if (encryptedMessages.size() > 0) {
+ DecryptMessage task = new DecryptMessage();
+ Message[] msgs = new Message[encryptedMessages.size()];
+ task.execute(encryptedMessages.toArray(msgs));
+ }
this.messageList.clear();
this.messageList.addAll(this.conversation.getMessages());
this.messageListAdapter.notifyDataSetChanged();
if (messageList.size() >= 1) {
int latestEncryption = this.conversation.getLatestMessage()
.getEncryption();
- conversation.nextMessageEncryption = latestEncryption;
+ if (latestEncryption== Message.ENCRYPTION_DECRYPTED) {
+ conversation.nextMessageEncryption = Message.ENCRYPTION_PGP;
+ } else {
+ conversation.nextMessageEncryption = latestEncryption;
+ }
makeFingerprintWarning(latestEncryption);
}
getActivity().invalidateOptionsMenu();
@@ -339,17 +397,47 @@ public class ConversationFragment extends Fragment {
protected void sendPlainTextMessage(Message message) {
ConversationActivity activity = (ConversationActivity) getActivity();
- activity.xmppConnectionService.sendMessage(conversation.getAccount(),
- message, null);
+ activity.xmppConnectionService.sendMessage(message, null);
chatMsg.setText("");
}
+ protected void sendPgpMessage(final Message message) {
+ ConversationActivity activity = (ConversationActivity) getActivity();
+ final XmppConnectionService xmppService = activity.xmppConnectionService;
+ Contact contact = message.getConversation().getContact();
+ if (contact.getPgpKeyId() != 0) {
+ xmppService.sendMessage(message, null);
+ chatMsg.setText("");
+ } else {
+ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+ builder.setTitle("No openPGP key found");
+ builder.setIconAttribute(android.R.attr.alertDialogIcon);
+ builder.setMessage("There is no openPGP key assoziated with this contact");
+ builder.setNegativeButton("Cancel", null);
+ builder.setPositiveButton("Send plain text",
+ new DialogInterface.OnClickListener() {
+
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ conversation.nextMessageEncryption = Message.ENCRYPTION_NONE;
+ message.setEncryption(Message.ENCRYPTION_NONE);
+ xmppService.sendMessage(message, null);
+ chatMsg.setText("");
+ }
+ });
+ builder.create().show();
+ }
+ }
+
+ public void resendPgpMessage(String msg) {
+ this.queuedPqpMessage = msg;
+ }
+
protected void sendOtrMessage(final Message message) {
ConversationActivity activity = (ConversationActivity) getActivity();
final XmppConnectionService xmppService = activity.xmppConnectionService;
if (conversation.hasValidOtrSession()) {
- activity.xmppConnectionService.sendMessage(
- conversation.getAccount(), message, null);
+ activity.xmppConnectionService.sendMessage(message, null);
chatMsg.setText("");
} else {
Hashtable<String, Integer> presences;
@@ -372,17 +460,15 @@ public class ConversationFragment extends Fragment {
int which) {
conversation.nextMessageEncryption = Message.ENCRYPTION_NONE;
message.setEncryption(Message.ENCRYPTION_NONE);
- xmppService.sendMessage(
- conversation.getAccount(), message,
- null);
+ xmppService.sendMessage(message, null);
chatMsg.setText("");
}
});
builder.setNegativeButton("Cancel", null);
builder.create().show();
} else if (presences.size() == 1) {
- xmppService.sendMessage(conversation.getAccount(), message,
- (String) presences.keySet().toArray()[0]);
+ xmppService.sendMessage(message, (String) presences.keySet()
+ .toArray()[0]);
chatMsg.setText("");
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
@@ -396,8 +482,7 @@ public class ConversationFragment extends Fragment {
@Override
public void onClick(DialogInterface dialog,
int which) {
- xmppService.sendMessage(
- conversation.getAccount(), message,
+ xmppService.sendMessage(message,
presencesArray[which]);
chatMsg.setText("");
}
@@ -438,7 +523,7 @@ public class ConversationFragment extends Fragment {
return bm;
}
}
-
+
public Bitmap getError() {
if (error == null) {
error = UIHelper.getErrorPicture(200);
@@ -446,4 +531,48 @@ public class ConversationFragment extends Fragment {
return error;
}
}
+
+ class DecryptMessage extends AsyncTask<Message, Void, Boolean> {
+
+ @Override
+ protected Boolean doInBackground(Message... params) {
+ XmppActivity activity = (XmppActivity) getActivity();
+ askForPassphraseIntent = null;
+ for(int i = 0; i < params.length; ++i) {
+ if (params[i].getEncryption() == Message.ENCRYPTION_PGP) {
+ String body = params[i].getBody();
+ String decrypted = null;
+ try {
+ if (activity==null) {
+ return false;
+ }
+ Log.d("gultsch","calling to decrypt message id #"+params[i].getUuid());
+ decrypted = activity.xmppConnectionService.getPgpEngine().decrypt(body);
+ } catch (UserInputRequiredException e) {
+ askForPassphraseIntent = e.getPendingIntent().getIntentSender();
+ return false;
+
+ } catch (OpenPgpException e) {
+ Log.d("gultsch","error decrypting pgp");
+ }
+ if (decrypted!=null) {
+ params[i].setBody(decrypted);
+ params[i].setEncryption(Message.ENCRYPTION_DECRYPTED);
+ activity.xmppConnectionService.updateMessage(params[i]);
+ }
+ if (activity!=null) {
+ activity.runOnUiThread(new Runnable() {
+
+ @Override
+ public void run() {
+ messageListAdapter.notifyDataSetChanged();
+ }
+ });
+ }
+ }
+ }
+ return true;
+ }
+
+ }
}
diff --git a/src/de/gultsch/chat/ui/DialogContactDetails.java b/src/de/gultsch/chat/ui/DialogContactDetails.java
index 8983a873..324a7aac 100644
--- a/src/de/gultsch/chat/ui/DialogContactDetails.java
+++ b/src/de/gultsch/chat/ui/DialogContactDetails.java
@@ -63,7 +63,7 @@ public class DialogContactDetails extends DialogFragment {
intent.putExtra(Intents.Insert.IM_HANDLE,contact.getJid());
intent.putExtra(Intents.Insert.IM_PROTOCOL,CommonDataKinds.Im.PROTOCOL_JABBER);
intent.putExtra("finishActivityOnSaveCompleted", true);
- getActivity().startActivityForResult(intent,ConversationActivity.INSERT_CONTACT);
+ getActivity().startActivityForResult(intent,0);
mDetailsDialog.dismiss();
}
};
diff --git a/src/de/gultsch/chat/ui/ManageAccountActivity.java b/src/de/gultsch/chat/ui/ManageAccountActivity.java
index 66a40782..bcc5306d 100644
--- a/src/de/gultsch/chat/ui/ManageAccountActivity.java
+++ b/src/de/gultsch/chat/ui/ManageAccountActivity.java
@@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.List;
import de.gultsch.chat.R;
+import de.gultsch.chat.crypto.PgpEngine;
+import de.gultsch.chat.crypto.PgpEngine.UserInputRequiredException;
import de.gultsch.chat.entities.Account;
import de.gultsch.chat.ui.EditAccount.EditAccountListener;
import android.app.Activity;
@@ -12,6 +14,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
+import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
@@ -31,6 +34,8 @@ import android.widget.TextView;
public class ManageAccountActivity extends XmppActivity implements ActionMode.Callback {
+ public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
+
protected boolean isActionMode = false;
protected ActionMode actionMode;
protected Account selectedAccountForActionMode = null;
@@ -231,6 +236,17 @@ public class ManageAccountActivity extends XmppActivity implements ActionMode.Ca
});
builder.setNegativeButton("Cancel",null);
builder.create().show();
+ } else if (item.getItemId()==R.id.announce_pgp) {
+ mode.finish();
+ try {
+ xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
+ } catch (PgpEngine.UserInputRequiredException e) {
+ try {
+ startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
+ } catch (SendIntentException e1) {
+ Log.d("gultsch","sending intent failed");
+ }
+ }
}
return true;
}
@@ -279,4 +295,18 @@ public class ManageAccountActivity extends XmppActivity implements ActionMode.Ca
}
});
}
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ super.onActivityResult(requestCode, resultCode, data);
+ if (resultCode == RESULT_OK) {
+ if (requestCode == REQUEST_ANNOUNCE_PGP) {
+ try {
+ xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
+ } catch (UserInputRequiredException e) {
+ Log.d("gultsch","already came back. ignoring");
+ }
+ }
+ }
+ }
}
diff --git a/src/de/gultsch/chat/utils/MessageParser.java b/src/de/gultsch/chat/utils/MessageParser.java
index 1dca7343..a12ef58c 100644
--- a/src/de/gultsch/chat/utils/MessageParser.java
+++ b/src/de/gultsch/chat/utils/MessageParser.java
@@ -23,6 +23,12 @@ public class MessageParser {
return new Message(conversation, packet.getFrom(), body, Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED);
}
+ public static Message parsePgpChat(String pgpBody, MessagePacket packet, Account account, XmppConnectionService service) {
+ String[] fromParts = packet.getFrom().split("/");
+ Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
+ return new Message(conversation, packet.getFrom(), pgpBody, Message.ENCRYPTION_PGP, Message.STATUS_RECIEVED);
+ }
+
public static Message parseOtrChat(MessagePacket packet, Account account, XmppConnectionService service) {
String[] fromParts = packet.getFrom().split("/");
Conversation conversation = service.findOrCreateConversation(account, fromParts[0],false);
@@ -131,4 +137,13 @@ public class MessageParser {
}
return new Message(conversation, packet.getFrom(), displayError, Message.ENCRYPTION_NONE, Message.STATUS_ERROR);
}
+
+ public static String getPgpBody(MessagePacket packet) {
+ for(Element child : packet.getChildren()) {
+ if (child.getName().equals("x")&&child.getAttribute("xmlns").equals("jabber:x:encrypted")) {
+ return child.getContent();
+ }
+ }
+ return null;
+ }
}