be compatible with openkeychain api v3

This commit is contained in:
Daniel Gultsch 2014-04-03 17:39:57 +02:00
parent a53716af82
commit 2506ef82df
7 changed files with 61 additions and 23 deletions

@ -1 +1 @@
Subproject commit 921674b3f1585dde76d13f181abcfc6d49bd737d Subproject commit 114bda4d3fde90d5c824884ce6b3f8ada846b435

@ -1 +1 @@
Subproject commit 68668fd444b973502b91b7cbe0cf7dbdd8a8b5c4 Subproject commit 098823bd525bdbf215060dba1ed248af853bbfac

View file

@ -8,8 +8,11 @@ import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
import eu.siacs.conversations.entities.Account;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
public class PgpEngine { public class PgpEngine {
private OpenPgpApi api; private OpenPgpApi api;
@ -18,14 +21,15 @@ public class PgpEngine {
this.api = api; this.api = api;
} }
public String decrypt(String message) throws UserInputRequiredException, public String decrypt(Account account, String message) throws UserInputRequiredException,
OpenPgpException { OpenPgpException {
Intent params = new Intent(); Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid());
InputStream is = new ByteArrayInputStream(message.getBytes()); InputStream is = new ByteArrayInputStream(message.getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
Intent result = api.executeApi(params, is, os); Intent result = api.executeApi(params, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
return os.toString(); return os.toString();
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
@ -38,25 +42,39 @@ public class PgpEngine {
} }
} }
public String encrypt(long keyId, String message) { public String encrypt(Account account, long keyId, String message) throws UserInputRequiredException, OpenPgpException {
Log.d("xmppService","called to pgpengine::encrypt");
long[] keys = {keyId}; long[] keys = {keyId};
Intent params = new Intent(); Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_ENCRYPT); params.setAction(OpenPgpApi.ACTION_ENCRYPT);
params.putExtra(OpenPgpApi.EXTRA_KEY_IDS,keys); params.putExtra(OpenPgpApi.EXTRA_KEY_IDS,keys);
params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid());
InputStream is = new ByteArrayInputStream(message.getBytes()); InputStream is = new ByteArrayInputStream(message.getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
Intent result = api.executeApi(params, is, os); Intent result = api.executeApi(params, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
StringBuilder encryptedMessageBody = new StringBuilder(); StringBuilder encryptedMessageBody = new StringBuilder();
String[] lines = os.toString().split("\n"); String[] lines = os.toString().split("\n");
for (int i = 3; i < lines.length - 1; ++i) { for (int i = 3; i < lines.length - 1; ++i) {
encryptedMessageBody.append(lines[i].trim()); encryptedMessageBody.append(lines[i].trim());
} }
Log.d("xmppService","encrpyted message: "+encryptedMessageBody.toString());
return encryptedMessageBody.toString(); return encryptedMessageBody.toString();
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
Log.d("xmppService","user input required");
throw new UserInputRequiredException((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new OpenPgpException(error);
default:
return null;
}
} }
public long fetchKeyId(String status, String signature) public long fetchKeyId(Account account, String status, String signature)
throws OpenPgpException { throws OpenPgpException {
if ((signature==null)||(api==null)) { if ((signature==null)||(api==null)) {
return 0; return 0;
@ -82,7 +100,7 @@ public class PgpEngine {
InputStream is = new ByteArrayInputStream(pgpSig.toString().getBytes()); InputStream is = new ByteArrayInputStream(pgpSig.toString().getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
Intent result = api.executeApi(params, is, os); Intent result = api.executeApi(params, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
OpenPgpSignatureResult sigResult OpenPgpSignatureResult sigResult
= result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);

View file

@ -29,6 +29,7 @@ public class Message extends AbstractEntity {
protected String conversationUuid; protected String conversationUuid;
protected String counterpart; protected String counterpart;
protected String body; protected String body;
protected String encryptedBody;
protected long timeSent; protected long timeSent;
protected int encryption; protected int encryption;
protected int status; protected int status;
@ -141,4 +142,12 @@ public class Message extends AbstractEntity {
public void setBody(String body) { public void setBody(String body) {
this.body = body; this.body = body;
} }
public String getEncryptedBody() {
return this.encryptedBody;
}
public void setEncryptedBody(String body) {
this.encryptedBody = body;
}
} }

View file

@ -288,7 +288,7 @@ public class XmppConnectionService extends Service {
} else { } else {
msg = ""; msg = "";
} }
contact.setPgpKeyId(pgp.fetchKeyId(msg, x contact.setPgpKeyId(pgp.fetchKeyId(account,msg, x
.getContent())); .getContent()));
} catch (OpenPgpException e) { } catch (OpenPgpException e) {
Log.d(LOGTAG, "faulty pgp. just ignore"); Log.d(LOGTAG, "faulty pgp. just ignore");
@ -657,8 +657,7 @@ public class XmppConnectionService extends Service {
.getFullJid()); .getFullJid());
packet.setTo(message.getCounterpart()); packet.setTo(message.getCounterpart());
packet.setBody("This is an XEP-0027 encryted message"); packet.setBody("This is an XEP-0027 encryted message");
packet.addChild("x","jabber:x:encrypted").setContent(this.getPgpEngine().encrypt(keyId, packet.addChild("x", "jabber:x:encrypted").setContent(message.getEncryptedBody());
message.getBody()));
account.getXmppConnection().sendMessagePacket(packet); account.getXmppConnection().sendMessagePacket(packet);
message.setStatus(Message.STATUS_SEND); message.setStatus(Message.STATUS_SEND);
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);

View file

@ -12,6 +12,7 @@ import net.java.otr4j.session.SessionStatus;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine.OpenPgpException; import eu.siacs.conversations.crypto.PgpEngine.OpenPgpException;
import eu.siacs.conversations.crypto.PgpEngine.UserInputRequiredException; import eu.siacs.conversations.crypto.PgpEngine.UserInputRequiredException;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
@ -481,10 +482,24 @@ public class ConversationFragment extends Fragment {
ConversationActivity activity = (ConversationActivity) getActivity(); ConversationActivity activity = (ConversationActivity) getActivity();
final XmppConnectionService xmppService = activity.xmppConnectionService; final XmppConnectionService xmppService = activity.xmppConnectionService;
Contact contact = message.getConversation().getContact(); Contact contact = message.getConversation().getContact();
Account account = message.getConversation().getAccount();
if (activity.hasPgp()) { if (activity.hasPgp()) {
if (contact.getPgpKeyId() != 0) { if (contact.getPgpKeyId() != 0) {
try {
message.setEncryptedBody(xmppService.getPgpEngine().encrypt(account, contact.getPgpKeyId(), message.getBody()));
xmppService.sendMessage(message, null); xmppService.sendMessage(message, null);
chatMsg.setText(""); chatMsg.setText("");
} catch (UserInputRequiredException e) {
try {
getActivity().startIntentSenderForResult(e.getPendingIntent().getIntentSender(),
ConversationActivity.REQUEST_SEND_MESSAGE, null, 0,
0, 0);
} catch (SendIntentException e1) {
Log.d("xmppService","failed to start intent to send message");
}
} catch (OpenPgpException e) {
Log.d("xmppService","error encrypting with pgp: "+e.getOpenPgpError().getMessage());
}
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder( AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity()); getActivity());
@ -616,7 +631,7 @@ public class ConversationFragment extends Fragment {
} }
try { try {
decrypted = activity.xmppConnectionService decrypted = activity.xmppConnectionService
.getPgpEngine().decrypt(body); .getPgpEngine().decrypt(conversation.getAccount(),body);
} catch (UserInputRequiredException e) { } catch (UserInputRequiredException e) {
askForPassphraseIntent = e.getPendingIntent() askForPassphraseIntent = e.getPendingIntent()
.getIntentSender(); .getIntentSender();

View file

@ -1,6 +1,5 @@
package eu.siacs.conversations.utils; package eu.siacs.conversations.utils;
import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -80,11 +79,9 @@ public class ExceptionHelper {
}); });
builder.create().show(); builder.create().show();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block return;
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block return;
e.printStackTrace();
} }
} }