aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/crypto
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-05-01 22:33:49 +0200
committerDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-05-01 22:33:49 +0200
commit37c8e157d0030c478ea00fbc407c8365b400af1a (patch)
tree89c3344aac980a087632d97089037d0ee15af9f2 /src/eu/siacs/conversations/crypto
parent4b66b4b5d1c6376c964d1f252b813ba21b80a64d (diff)
reworked openpgp integration part #1
Diffstat (limited to 'src/eu/siacs/conversations/crypto')
-rw-r--r--src/eu/siacs/conversations/crypto/OnPgpEngineResult.java11
-rw-r--r--src/eu/siacs/conversations/crypto/PgpEngine.java209
2 files changed, 132 insertions, 88 deletions
diff --git a/src/eu/siacs/conversations/crypto/OnPgpEngineResult.java b/src/eu/siacs/conversations/crypto/OnPgpEngineResult.java
new file mode 100644
index 00000000..8e115839
--- /dev/null
+++ b/src/eu/siacs/conversations/crypto/OnPgpEngineResult.java
@@ -0,0 +1,11 @@
+package eu.siacs.conversations.crypto;
+
+import org.openintents.openpgp.OpenPgpError;
+
+import android.app.PendingIntent;
+
+public interface OnPgpEngineResult {
+ public void success();
+ public void error(OpenPgpError openPgpError);
+ public void userInputRequried(PendingIntent pi);
+}
diff --git a/src/eu/siacs/conversations/crypto/PgpEngine.java b/src/eu/siacs/conversations/crypto/PgpEngine.java
index 308a17d9..cfe39e4c 100644
--- a/src/eu/siacs/conversations/crypto/PgpEngine.java
+++ b/src/eu/siacs/conversations/crypto/PgpEngine.java
@@ -3,12 +3,16 @@ package eu.siacs.conversations.crypto;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.io.OutputStream;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.util.OpenPgpApi;
+import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback;
import eu.siacs.conversations.entities.Account;
+import eu.siacs.conversations.entities.Contact;
+import eu.siacs.conversations.entities.Message;
import android.app.PendingIntent;
import android.content.Intent;
@@ -21,66 +25,80 @@ public class PgpEngine {
this.api = api;
}
- public String decrypt(Account account, String message) throws UserInputRequiredException,
- OpenPgpException {
+ public void decrypt(final Message message, final OnPgpEngineResult callback) {
Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
- params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid());
- InputStream is = new ByteArrayInputStream(message.getBytes());
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- Intent result = api.executeApi(params, is, os);
- switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
- case OpenPgpApi.RESULT_CODE_SUCCESS:
- return os.toString();
- case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
- throw new UserInputRequiredException((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
- case OpenPgpApi.RESULT_CODE_ERROR:
- throw new OpenPgpException(
- (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
- default:
- return null;
- }
+ params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, message
+ .getConversation().getAccount().getJid());
+ InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
+ final OutputStream os = new ByteArrayOutputStream();
+ api.executeApiAsync(params, is, os, new IOpenPgpCallback() {
+
+ @Override
+ public void onReturn(Intent result) {
+ switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
+ OpenPgpApi.RESULT_CODE_ERROR)) {
+ case OpenPgpApi.RESULT_CODE_SUCCESS:
+ message.setBody(os.toString());
+ message.setEncryption(Message.ENCRYPTION_DECRYPTED);
+ callback.success();
+ return;
+ case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
+ callback.userInputRequried((PendingIntent) result
+ .getParcelableExtra(OpenPgpApi.RESULT_INTENT));
+ return;
+ case OpenPgpApi.RESULT_CODE_ERROR:
+ callback.error((OpenPgpError) result
+ .getParcelableExtra(OpenPgpApi.RESULT_ERROR));
+ return;
+ default:
+ return;
+ }
+ }
+ });
}
- public String encrypt(Account account, long keyId, String message) throws UserInputRequiredException, OpenPgpException {
- Log.d("xmppService","called to pgpengine::encrypt");
- long[] keys = {keyId};
+ public void encrypt(Account account, long keyId, Message message,
+ final OnPgpEngineResult callback) {
+ Log.d("xmppService", "called to pgpengine::encrypt");
+ long[] keys = { keyId };
Intent params = new Intent();
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_ACCOUNT_NAME, account.getJid());
-
- InputStream is = new ByteArrayInputStream(message.getBytes());
+
+ InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
Intent result = api.executeApi(params, is, os);
- switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
+ switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
+ OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
StringBuilder encryptedMessageBody = new StringBuilder();
String[] lines = os.toString().split("\n");
for (int i = 3; i < lines.length - 1; ++i) {
encryptedMessageBody.append(lines[i].trim());
}
- Log.d("xmppService","encrpyted message: "+encryptedMessageBody.toString());
- return encryptedMessageBody.toString();
+ message.setEncryptedBody(encryptedMessageBody.toString());
+ callback.success();
+ return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
- Log.d("xmppService","user input required");
- throw new UserInputRequiredException((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
+ callback.userInputRequried((PendingIntent) result
+ .getParcelableExtra(OpenPgpApi.RESULT_INTENT));
+ return;
case OpenPgpApi.RESULT_CODE_ERROR:
- OpenPgpError error = (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
- throw new OpenPgpException(error);
- default:
- return null;
+ callback.error((OpenPgpError) result
+ .getParcelableExtra(OpenPgpApi.RESULT_ERROR));
+ return;
}
}
- public long fetchKeyId(Account account, String status, String signature)
- throws OpenPgpException {
- if ((signature==null)||(api==null)) {
+ public long fetchKeyId(Account account, String status, String signature) {
+ if ((signature == null) || (api == null)) {
return 0;
}
- if (status==null) {
- status="";
+ if (status == null) {
+ status = "";
}
StringBuilder pgpSig = new StringBuilder();
pgpSig.append("-----BEGIN PGP SIGNED MESSAGE-----");
@@ -97,75 +115,90 @@ public class PgpEngine {
Intent params = new Intent();
params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
+ params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid());
InputStream is = new ByteArrayInputStream(pgpSig.toString().getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
Intent result = api.executeApi(params, is, os);
- switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
+ switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
+ OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
- OpenPgpSignatureResult sigResult
- = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
- if (sigResult==null) {
- return 0;
- } else {
+ OpenPgpSignatureResult sigResult = result
+ .getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
+ if (sigResult != null) {
return sigResult.getKeyId();
+ } else {
+ return 0;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
- break;
+ Log.d("xmppService","user interaction required");
+ return 0;
case OpenPgpApi.RESULT_CODE_ERROR:
- throw new OpenPgpException(
- (OpenPgpError) result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
+ Log.d("xmppService","pgp error");
+ return 0;
}
return 0;
}
- public String generateSignature(Account account, String status)
- throws UserInputRequiredException {
+ public void generateSignature(final Account account, String status,
+ final OnPgpEngineResult callback) {
Intent params = new Intent();
params.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
params.setAction(OpenPgpApi.ACTION_SIGN);
params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid());
InputStream is = new ByteArrayInputStream(status.getBytes());
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- Intent result = api.executeApi(params, is, os);
- StringBuilder signatureBuilder = new StringBuilder();
- switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
- case OpenPgpApi.RESULT_CODE_SUCCESS:
- String[] lines = os.toString().split("\n");
- for (int i = 7; i < lines.length - 1; ++i) {
- signatureBuilder.append(lines[i].trim());
+ final OutputStream os = new ByteArrayOutputStream();
+ api.executeApiAsync(params, is, os, new IOpenPgpCallback() {
+
+ @Override
+ public void onReturn(Intent result) {
+ switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
+ case OpenPgpApi.RESULT_CODE_SUCCESS:
+ StringBuilder signatureBuilder = new StringBuilder();
+ String[] lines = os.toString().split("\n");
+ for (int i = 7; i < lines.length - 1; ++i) {
+ signatureBuilder.append(lines[i].trim());
+ }
+ account.setKey("pgp_signature", signatureBuilder.toString());
+ callback.success();
+ return;
+ case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
+ callback.userInputRequried((PendingIntent) result
+ .getParcelableExtra(OpenPgpApi.RESULT_INTENT));
+ return;
+ case OpenPgpApi.RESULT_CODE_ERROR:
+ callback.error((OpenPgpError) result
+ .getParcelableExtra(OpenPgpApi.RESULT_ERROR));
+ return;
+ }
}
- break;
- case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
- throw new UserInputRequiredException((PendingIntent) result.getParcelableExtra(OpenPgpApi.RESULT_INTENT));
- case OpenPgpApi.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;
- }
+
+ public void hasKey(Account account, long keyId, final OnPgpEngineResult callback) {
+ Intent params = new Intent();
+ params.setAction(OpenPgpApi.ACTION_GET_KEY);
+ params.putExtra(OpenPgpApi.EXTRA_KEY_ID, keyId);
+ params.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, account.getJid());
+ InputStream is = new ByteArrayInputStream(new byte[0]);
+ OutputStream os = new ByteArrayOutputStream();
+ api.executeApiAsync(params, is, os, new IOpenPgpCallback() {
+
+ @Override
+ public void onReturn(Intent result) {
+ switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
+ case OpenPgpApi.RESULT_CODE_SUCCESS:
+ callback.success();
+ return;
+ case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
+ callback.userInputRequried((PendingIntent) result
+ .getParcelableExtra(OpenPgpApi.RESULT_INTENT));
+ return;
+ case OpenPgpApi.RESULT_CODE_ERROR:
+ callback.error((OpenPgpError) result
+ .getParcelableExtra(OpenPgpApi.RESULT_ERROR));
+ return;
+ }
+ }
+ });
}
}