Search in sources :

Example 1 with OpenPgpMetadata

use of org.openintents.openpgp.OpenPgpMetadata in project Conversations by siacs.

the class PgpDecryptionService method executeApi.

private void executeApi(Message message) {
    boolean skipNotificationPush = false;
    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.isFileOrImage()) {
            try {
                final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false);
                final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
                if (outputFile.getParentFile().mkdirs()) {
                    Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath());
                }
                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:
                        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());
                            }
                            synchronized (mXmppConnectionService.FILENAMES_TO_IGNORE_DELETION) {
                                mXmppConnectionService.FILENAMES_TO_IGNORE_DELETION.add(outputFile.getAbsolutePath());
                            }
                            if (outputFile.renameTo(fixedFile)) {
                                Log.d(Config.LOGTAG, "renamed " + outputFile.getAbsolutePath() + " to " + fixedFile.getAbsolutePath());
                                message.setRelativeFilePath(path);
                            }
                        }
                        final String url = message.getFileParams().url;
                        mXmppConnectionService.getFileBackend().updateFileParams(message, url);
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                        mXmppConnectionService.updateMessage(message);
                        if (!inputFile.delete()) {
                            Log.w(Config.LOGTAG, "unable to delete pgp encrypted source file " + inputFile.getAbsolutePath());
                        }
                        skipNotificationPush = true;
                        mXmppConnectionService.getFileBackend().updateMediaScanner(outputFile, () -> notifyIfPending(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);
            }
        }
    }
    if (!skipNotificationPush) {
        notifyIfPending(message);
    }
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) HttpConnectionManager(eu.siacs.conversations.http.HttpConnectionManager) PendingIntent(android.app.PendingIntent) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile) OpenPgpMetadata(org.openintents.openpgp.OpenPgpMetadata)

Example 2 with OpenPgpMetadata

use of org.openintents.openpgp.OpenPgpMetadata in project Pix-Art-Messenger by kriztan.

the class PgpDecryptionService method executeApi.

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);
                if (outputFile.getParentFile().mkdirs()) {
                    Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath());
                }
                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:
                        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);
                            }
                        }
                        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);
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) HttpConnectionManager(de.pixart.messenger.http.HttpConnectionManager) PendingIntent(android.app.PendingIntent) DownloadableFile(de.pixart.messenger.entities.DownloadableFile) OpenPgpMetadata(org.openintents.openpgp.OpenPgpMetadata)

Aggregations

PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 OpenPgpMetadata (org.openintents.openpgp.OpenPgpMetadata)2 DownloadableFile (de.pixart.messenger.entities.DownloadableFile)1 HttpConnectionManager (de.pixart.messenger.http.HttpConnectionManager)1 DownloadableFile (eu.siacs.conversations.entities.DownloadableFile)1 HttpConnectionManager (eu.siacs.conversations.http.HttpConnectionManager)1 URL (java.net.URL)1