aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/siacs/conversations/crypto')
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java162
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/PgpEngine.java19
-rw-r--r--src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java2
3 files changed, 182 insertions, 1 deletions
diff --git a/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java
new file mode 100644
index 000000000..ed67dc65f
--- /dev/null
+++ b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java
@@ -0,0 +1,162 @@
+package eu.siacs.conversations.crypto;
+
+import android.app.PendingIntent;
+
+import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.services.XmppConnectionService;
+import eu.siacs.conversations.ui.UiCallback;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class PgpDecryptionService {
+
+ private final XmppConnectionService xmppConnectionService;
+ private final ConcurrentHashMap<String, List<Message>> messages = new ConcurrentHashMap<>();
+ private final ConcurrentHashMap<String, Boolean> decryptingMessages = new ConcurrentHashMap<>();
+ private Boolean keychainLocked = false;
+ private final Object keychainLockedLock = new Object();
+
+ public PgpDecryptionService(XmppConnectionService xmppConnectionService) {
+ this.xmppConnectionService = xmppConnectionService;
+ }
+
+ public void add(Message message) {
+ if (isRunning()) {
+ decryptDirectly(message);
+ } else {
+ store(message);
+ }
+ }
+
+ public void addAll(List<Message> messagesList) {
+ if (!messagesList.isEmpty()) {
+ String conversationUuid = messagesList.get(0).getConversation().getUuid();
+ if (!messages.containsKey(conversationUuid)) {
+ List<Message> list = Collections.synchronizedList(new LinkedList<Message>());
+ messages.put(conversationUuid, list);
+ }
+ synchronized (messages.get(conversationUuid)) {
+ messages.get(conversationUuid).addAll(messagesList);
+ }
+ decryptAllMessages();
+ }
+ }
+
+ public void onKeychainUnlocked() {
+ synchronized (keychainLockedLock) {
+ keychainLocked = false;
+ }
+ decryptAllMessages();
+ }
+
+ public void onKeychainLocked() {
+ synchronized (keychainLockedLock) {
+ keychainLocked = true;
+ }
+ xmppConnectionService.updateConversationUi();
+ }
+
+ public void onOpenPgpServiceBound() {
+ decryptAllMessages();
+ }
+
+ public boolean isRunning() {
+ synchronized (keychainLockedLock) {
+ return !keychainLocked;
+ }
+ }
+
+ private void store(Message message) {
+ if (messages.containsKey(message.getConversation().getUuid())) {
+ messages.get(message.getConversation().getUuid()).add(message);
+ } else {
+ List<Message> messageList = Collections.synchronizedList(new LinkedList<Message>());
+ messageList.add(message);
+ messages.put(message.getConversation().getUuid(), messageList);
+ }
+ }
+
+ private void decryptAllMessages() {
+ for (String uuid : messages.keySet()) {
+ decryptMessages(uuid);
+ }
+ }
+
+ private void decryptMessages(final String uuid) {
+ synchronized (decryptingMessages) {
+ Boolean decrypting = decryptingMessages.get(uuid);
+ if ((decrypting != null && !decrypting) || decrypting == null) {
+ decryptingMessages.put(uuid, true);
+ decryptMessage(uuid);
+ }
+ }
+ }
+
+ private void decryptMessage(final String uuid) {
+ Message message = null;
+ synchronized (messages.get(uuid)) {
+ while (!messages.get(uuid).isEmpty()) {
+ if (messages.get(uuid).get(0).getEncryption() == Message.ENCRYPTION_PGP) {
+ if (isRunning()) {
+ message = messages.get(uuid).remove(0);
+ }
+ break;
+ } else {
+ messages.get(uuid).remove(0);
+ }
+ }
+ if (message != null && xmppConnectionService.getPgpEngine() != null) {
+ xmppConnectionService.getPgpEngine().decrypt(message, new UiCallback<Message>() {
+
+ @Override
+ public void userInputRequried(PendingIntent pi, Message message) {
+ messages.get(uuid).add(0, message);
+ decryptingMessages.put(uuid, false);
+ }
+
+ @Override
+ public void success(Message message) {
+ xmppConnectionService.updateConversationUi();
+ decryptMessage(uuid);
+ }
+
+ @Override
+ public void error(int error, Message message) {
+ message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
+ xmppConnectionService.updateConversationUi();
+ decryptMessage(uuid);
+ }
+ });
+ } else {
+ decryptingMessages.put(uuid, false);
+ }
+ }
+ }
+
+ private void decryptDirectly(final Message message) {
+ if (message.getEncryption() == Message.ENCRYPTION_PGP && xmppConnectionService.getPgpEngine() != null) {
+ xmppConnectionService.getPgpEngine().decrypt(message, new UiCallback<Message>() {
+
+ @Override
+ public void userInputRequried(PendingIntent pi, Message message) {
+ store(message);
+ }
+
+ @Override
+ public void success(Message message) {
+ xmppConnectionService.updateConversationUi();
+ xmppConnectionService.getNotificationService().updateNotification(false);
+ }
+
+ @Override
+ public void error(int error, Message message) {
+ message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
+ xmppConnectionService.updateConversationUi();
+ }
+ });
+ }
+ }
+}
diff --git a/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java b/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java
index 8f8122f0e..257d0f7e8 100644
--- a/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java
+++ b/src/main/java/eu/siacs/conversations/crypto/PgpEngine.java
@@ -50,6 +50,7 @@ public class PgpEngine {
@Override
public void onReturn(Intent result) {
+ notifyPgpDecryptionService(message.getContact().getAccount(), OpenPgpApi.ACTION_DECRYPT_VERIFY, result);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
@@ -64,6 +65,7 @@ public class PgpEngine {
&& manager.getAutoAcceptFileSize() > 0) {
manager.createNewDownloadConnection(message);
}
+ mXmppConnectionService.updateMessage(message);
callback.success(message);
}
} catch (IOException e) {
@@ -158,6 +160,7 @@ public class PgpEngine {
@Override
public void onReturn(Intent result) {
+ notifyPgpDecryptionService(message.getContact().getAccount(), OpenPgpApi.ACTION_ENCRYPT, result);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
@@ -203,6 +206,7 @@ public class PgpEngine {
@Override
public void onReturn(Intent result) {
+ notifyPgpDecryptionService(message.getContact().getAccount(), OpenPgpApi.ACTION_ENCRYPT, result);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
@@ -252,6 +256,7 @@ public class PgpEngine {
InputStream is = new ByteArrayInputStream(pgpSig.toString().getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
Intent result = api.executeApi(params, is, os);
+ notifyPgpDecryptionService(account, OpenPgpApi.ACTION_DECRYPT_VERIFY, result);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE,
OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
@@ -282,6 +287,7 @@ public class PgpEngine {
@Override
public void onReturn(Intent result) {
+ notifyPgpDecryptionService(account, OpenPgpApi.ACTION_SIGN, result);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
StringBuilder signatureBuilder = new StringBuilder();
@@ -368,4 +374,17 @@ public class PgpEngine {
return (PendingIntent) result
.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
}
+
+ private void notifyPgpDecryptionService(Account account, String action, final Intent result) {
+ switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, 0)) {
+ case OpenPgpApi.RESULT_CODE_SUCCESS:
+ if (OpenPgpApi.ACTION_SIGN.equals(action)) {
+ account.getPgpDecryptionService().onKeychainUnlocked();
+ }
+ break;
+ case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
+ account.getPgpDecryptionService().onKeychainLocked();
+ break;
+ }
+ }
}
diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java
index ab3aefac0..4a895bb81 100644
--- a/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java
+++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java
@@ -323,7 +323,6 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED,
XmppAxolotlSession.Trust.UNTRUSTED);
this.deviceIds.put(jid, deviceIds);
- findDevicesWithoutSession(jid);
mXmppConnectionService.keyStatusUpdated(null);
}
@@ -515,6 +514,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
if (changed) {
if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
+ mXmppConnectionService.publishDisplayName(account);
publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
} else {
publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);