2015-10-15 22:21:47 +00:00
|
|
|
package eu.siacs.conversations.crypto;
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
2016-06-13 11:32:14 +00:00
|
|
|
import android.content.Intent;
|
2018-01-29 00:40:02 +00:00
|
|
|
import android.util.Log;
|
2016-06-13 11:32:14 +00:00
|
|
|
|
2018-01-29 00:40:02 +00:00
|
|
|
import org.openintents.openpgp.OpenPgpMetadata;
|
2016-06-13 11:32:14 +00:00
|
|
|
import org.openintents.openpgp.util.OpenPgpApi;
|
|
|
|
|
|
|
|
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.net.URL;
|
|
|
|
import java.util.ArrayDeque;
|
2016-06-15 10:44:29 +00:00
|
|
|
import java.util.HashSet;
|
2016-06-13 11:32:14 +00:00
|
|
|
import java.util.List;
|
2015-10-15 22:21:47 +00:00
|
|
|
|
2018-01-29 00:40:02 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2016-06-13 11:32:14 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.DownloadableFile;
|
2015-10-15 22:21:47 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
2016-06-13 11:32:14 +00:00
|
|
|
import eu.siacs.conversations.http.HttpConnectionManager;
|
2015-10-15 22:21:47 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2018-01-29 00:40:02 +00:00
|
|
|
import eu.siacs.conversations.utils.MimeUtils;
|
2015-10-15 22:21:47 +00:00
|
|
|
|
|
|
|
public class PgpDecryptionService {
|
|
|
|
|
2016-06-13 11:32:14 +00:00
|
|
|
protected final ArrayDeque<Message> messages = new ArrayDeque();
|
2017-11-22 12:36:00 +00:00
|
|
|
protected final HashSet<Message> pendingNotifications = new HashSet<>();
|
|
|
|
private final XmppConnectionService mXmppConnectionService;
|
|
|
|
private OpenPgpApi openPgpApi = null;
|
|
|
|
private Message currentMessage;
|
|
|
|
private PendingIntent pendingIntent;
|
|
|
|
private Intent userInteractionResult;
|
2015-10-15 22:21:47 +00:00
|
|
|
|
|
|
|
|
2017-11-22 12:36:00 +00:00
|
|
|
public PgpDecryptionService(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
2015-10-15 22:21:47 +00:00
|
|
|
|
2016-06-15 10:44:29 +00:00
|
|
|
public synchronized boolean decrypt(final Message message, boolean notify) {
|
2017-11-22 12:36:00 +00:00
|
|
|
messages.add(message);
|
|
|
|
if (notify && pendingIntent == null) {
|
|
|
|
pendingNotifications.add(message);
|
|
|
|
continueDecryption();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
continueDecryption();
|
|
|
|
return notify;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void decrypt(final List<Message> list) {
|
|
|
|
for (Message message : list) {
|
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
|
|
|
messages.add(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continueDecryption();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void discard(List<Message> discards) {
|
|
|
|
this.messages.removeAll(discards);
|
|
|
|
this.pendingNotifications.removeAll(discards);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void discard(Message message) {
|
|
|
|
this.messages.remove(message);
|
|
|
|
this.pendingNotifications.remove(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void giveUpCurrentDecryption() {
|
|
|
|
Message message;
|
|
|
|
synchronized (this) {
|
|
|
|
if (currentMessage != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
message = messages.peekFirst();
|
|
|
|
if (message == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
discard(message);
|
|
|
|
}
|
|
|
|
synchronized (message) {
|
|
|
|
if (message.getEncryption() == Message.ENCRYPTION_PGP) {
|
|
|
|
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 08:47:28 +00:00
|
|
|
mXmppConnectionService.updateMessage(message, false);
|
2017-11-22 12:36:00 +00:00
|
|
|
continueDecryption(true);
|
|
|
|
}
|
2017-07-23 08:53:42 +00:00
|
|
|
|
2016-06-13 11:32:14 +00:00
|
|
|
protected synchronized void decryptNext() {
|
|
|
|
if (pendingIntent == null
|
2017-11-22 12:36:00 +00:00
|
|
|
&& getOpenPgpApi() != null
|
|
|
|
&& (currentMessage = messages.poll()) != null) {
|
2016-06-13 11:32:14 +00:00
|
|
|
new Thread(new Runnable() {
|
2017-11-22 12:36:00 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
executeApi(currentMessage);
|
|
|
|
decryptNext();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void continueDecryption(boolean resetPending) {
|
|
|
|
if (resetPending) {
|
|
|
|
this.pendingIntent = null;
|
|
|
|
}
|
|
|
|
continueDecryption();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void continueDecryption(Intent userInteractionResult) {
|
|
|
|
this.pendingIntent = null;
|
|
|
|
this.userInteractionResult = userInteractionResult;
|
|
|
|
continueDecryption();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void continueDecryption() {
|
|
|
|
if (currentMessage == null) {
|
|
|
|
decryptNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized OpenPgpApi getOpenPgpApi() {
|
|
|
|
if (openPgpApi == null) {
|
|
|
|
this.openPgpApi = mXmppConnectionService.getOpenPgpApi();
|
|
|
|
}
|
|
|
|
return this.openPgpApi;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void executeApi(Message message) {
|
|
|
|
synchronized (message) {
|
|
|
|
Intent params = userInteractionResult != null ? userInteractionResult : new Intent();
|
|
|
|
params.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
|
|
|
|
if (message.getType() == Message.TYPE_TEXT) {
|
|
|
|
InputStream is = new ByteArrayInputStream(message.getBody().getBytes());
|
|
|
|
final OutputStream os = new ByteArrayOutputStream();
|
|
|
|
Intent result = getOpenPgpApi().executeApi(params, is, os);
|
|
|
|
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
|
|
|
case OpenPgpApi.RESULT_CODE_SUCCESS:
|
|
|
|
try {
|
|
|
|
os.flush();
|
|
|
|
final String body = os.toString();
|
|
|
|
if (body == null) {
|
|
|
|
throw new IOException("body was null");
|
|
|
|
}
|
|
|
|
message.setBody(body);
|
|
|
|
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
|
|
|
|
final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
|
|
|
|
if (message.trusted()
|
|
|
|
&& message.treatAsDownloadable()
|
|
|
|
&& manager.getAutoAcceptFileSize() > 0) {
|
|
|
|
manager.createNewDownloadConnection(message);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
|
|
|
|
}
|
|
|
|
mXmppConnectionService.updateMessage(message);
|
|
|
|
break;
|
|
|
|
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
|
|
|
|
synchronized (PgpDecryptionService.this) {
|
|
|
|
PendingIntent pendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
|
|
|
|
messages.addFirst(message);
|
|
|
|
currentMessage = null;
|
|
|
|
storePendingIntent(pendingIntent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OpenPgpApi.RESULT_CODE_ERROR:
|
|
|
|
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
|
|
|
|
mXmppConnectionService.updateMessage(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
|
|
|
|
try {
|
|
|
|
final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
|
|
|
|
final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
|
2018-01-29 00:40:02 +00:00
|
|
|
if (outputFile.getParentFile().mkdirs()) {
|
|
|
|
Log.d(Config.LOGTAG,"created parent directories for "+outputFile.getAbsolutePath());
|
|
|
|
}
|
2017-11-22 12:36:00 +00:00
|
|
|
outputFile.createNewFile();
|
|
|
|
InputStream is = new FileInputStream(inputFile);
|
|
|
|
OutputStream os = new FileOutputStream(outputFile);
|
|
|
|
Intent result = getOpenPgpApi().executeApi(params, is, os);
|
|
|
|
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
|
|
|
case OpenPgpApi.RESULT_CODE_SUCCESS:
|
2018-01-29 00:40:02 +00:00
|
|
|
OpenPgpMetadata openPgpMetadata = result.getParcelableExtra(OpenPgpApi.RESULT_METADATA);
|
|
|
|
String originalFilename = openPgpMetadata.getFilename();
|
|
|
|
String originalExtension = originalFilename == null ? null : MimeUtils.extractRelevantExtension(originalFilename);
|
|
|
|
if (originalExtension != null && MimeUtils.extractRelevantExtension(outputFile.getName()) == null) {
|
|
|
|
Log.d(Config.LOGTAG,"detected original filename during pgp decryption");
|
|
|
|
String mime = MimeUtils.guessMimeTypeFromExtension(originalExtension);
|
|
|
|
String path = outputFile.getName()+"."+originalExtension;
|
|
|
|
DownloadableFile fixedFile = mXmppConnectionService.getFileBackend().getFileForPath(path,mime);
|
|
|
|
if (fixedFile.getParentFile().mkdirs()) {
|
|
|
|
Log.d(Config.LOGTAG,"created parent directories for "+fixedFile.getAbsolutePath());
|
|
|
|
}
|
|
|
|
if (outputFile.renameTo(fixedFile)) {
|
|
|
|
Log.d(Config.LOGTAG, "renamed " + outputFile.getAbsolutePath() + " to " + fixedFile.getAbsolutePath());
|
|
|
|
message.setRelativeFilePath(path);
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 12:36:00 +00:00
|
|
|
URL url = message.getFileParams().url;
|
|
|
|
mXmppConnectionService.getFileBackend().updateFileParams(message, url);
|
|
|
|
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
|
|
|
|
inputFile.delete();
|
|
|
|
mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile);
|
|
|
|
mXmppConnectionService.updateMessage(message);
|
|
|
|
break;
|
|
|
|
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
|
|
|
|
synchronized (PgpDecryptionService.this) {
|
|
|
|
PendingIntent pendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
|
|
|
|
messages.addFirst(message);
|
|
|
|
currentMessage = null;
|
|
|
|
storePendingIntent(pendingIntent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OpenPgpApi.RESULT_CODE_ERROR:
|
|
|
|
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
|
|
|
|
mXmppConnectionService.updateMessage(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (final IOException e) {
|
|
|
|
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
|
|
|
|
mXmppConnectionService.updateMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyIfPending(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void notifyIfPending(Message message) {
|
|
|
|
if (pendingNotifications.remove(message)) {
|
|
|
|
mXmppConnectionService.getNotificationService().push(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void storePendingIntent(PendingIntent pendingIntent) {
|
|
|
|
this.pendingIntent = pendingIntent;
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean hasPendingIntent(Conversation conversation) {
|
|
|
|
if (pendingIntent == null) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
for (Message message : messages) {
|
|
|
|
if (message.getConversation() == conversation) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-10-15 22:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:36:00 +00:00
|
|
|
public PendingIntent getPendingIntent() {
|
|
|
|
return pendingIntent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isConnected() {
|
|
|
|
return getOpenPgpApi() != null;
|
|
|
|
}
|
2015-10-15 22:21:47 +00:00
|
|
|
}
|