Search in sources :

Example 1 with OpenPgpDataSource

use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.

the class PgpMessageBuilder method createOpenPgpDataSourceFromBodyPart.

@NonNull
private OpenPgpDataSource createOpenPgpDataSourceFromBodyPart(final MimeBodyPart bodyPart, final boolean writeBodyContentOnly) throws MessagingException {
    return new OpenPgpDataSource() {

        @Override
        public void writeTo(OutputStream os) throws IOException {
            try {
                if (writeBodyContentOnly) {
                    Body body = bodyPart.getBody();
                    InputStream inputStream = body.getInputStream();
                    IOUtils.copy(inputStream, os);
                } else {
                    bodyPart.writeTo(os);
                }
            } catch (MessagingException e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) InputStream(java.io.InputStream) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) IOException(java.io.IOException) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) NonNull(androidx.annotation.NonNull)

Example 2 with OpenPgpDataSource

use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.

the class PgpMessageBuilder method launchOpenPgpApiIntent.

private PendingIntent launchOpenPgpApiIntent(@NonNull Intent openPgpIntent, MimeBodyPart bodyPart, boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
    OpenPgpDataSource dataSource = createOpenPgpDataSourceFromBodyPart(bodyPart, writeBodyContentOnly);
    BinaryTempFileBody pgpResultTempBody = null;
    OutputStream outputStream = null;
    if (captureOutputPart) {
        try {
            pgpResultTempBody = new BinaryTempFileBody(capturedOutputPartIs7Bit ? MimeUtil.ENC_7BIT : MimeUtil.ENC_8BIT);
            outputStream = pgpResultTempBody.getOutputStream();
            // OpenKeychain/BouncyCastle at this point use the system newline for formatting, which is LF on android.
            // we need this to be CRLF, so we convert the data after receiving.
            outputStream = new EOLConvertingOutputStream(outputStream);
        } catch (IOException e) {
            throw new MessagingException("could not allocate temp file for storage!", e);
        }
    }
    Intent result = openPgpApi.executeApi(openPgpIntent, dataSource, outputStream);
    switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
        case OpenPgpApi.RESULT_CODE_SUCCESS:
            mimeBuildMessage(result, bodyPart, pgpResultTempBody);
            return null;
        case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
            PendingIntent returnedPendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
            if (returnedPendingIntent == null) {
                throw new MessagingException("openpgp api needs user interaction, but returned no pendingintent!");
            }
            return returnedPendingIntent;
        case OpenPgpApi.RESULT_CODE_ERROR:
            OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
            if (error == null) {
                throw new MessagingException("internal openpgp api error");
            }
            /*
                boolean isOpportunisticError = error.getErrorId() == OpenPgpError.OPPORTUNISTIC_MISSING_KEYS;
                if (isOpportunisticError) {
                    if (!cryptoStatus.isEncryptionOpportunistic()) {
                        throw new IllegalStateException(
                                "Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
                    }
                    Timber.d("Skipping encryption due to opportunistic mode");
                    return null;
                }
                */
            throw new MessagingException(error.getMessage());
    }
    throw new IllegalStateException("unreachable code segment reached");
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) MessagingException(com.fsck.k9.mail.MessagingException) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError)

Example 3 with OpenPgpDataSource

use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.

the class MessageCryptoHelper method callAsyncDecrypt.

private void callAsyncDecrypt(Intent intent) throws IOException {
    OpenPgpDataSource dataSource = getDataSourceForEncryptedOrInlineData();
    OpenPgpDataSink<MimeBodyPart> openPgpDataSink = getDataSinkForDecryptedData();
    cancelableBackgroundOperation = openPgpApi.executeApiAsync(intent, dataSource, openPgpDataSink, new IOpenPgpSinkResultCallback<MimeBodyPart>() {

        @Override
        public void onReturn(Intent result, MimeBodyPart decryptedPart) {
            cancelableBackgroundOperation = null;
            currentCryptoResult = result;
            onCryptoOperationReturned(decryptedPart);
        }

        @Override
        public void onProgress(int current, int max) {
            Timber.d("received progress status: %d / %d", current, max);
            callbackProgress(current, max);
        }
    });
}
Also used : IOpenPgpSinkResultCallback(org.openintents.openpgp.util.OpenPgpApi.IOpenPgpSinkResultCallback) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 4 with OpenPgpDataSource

use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.

the class MessageCryptoHelper method callAsyncInlineOperation.

private void callAsyncInlineOperation(Intent intent) throws IOException {
    OpenPgpDataSource dataSource = getDataSourceForEncryptedOrInlineData();
    OpenPgpDataSink<MimeBodyPart> dataSink = getDataSinkForDecryptedInlineData();
    cancelableBackgroundOperation = openPgpApi.executeApiAsync(intent, dataSource, dataSink, new IOpenPgpSinkResultCallback<MimeBodyPart>() {

        @Override
        public void onProgress(int current, int max) {
            Timber.d("received progress status: %d / %d", current, max);
            callbackProgress(current, max);
        }

        @Override
        public void onReturn(Intent result, MimeBodyPart bodyPart) {
            cancelableBackgroundOperation = null;
            currentCryptoResult = result;
            onCryptoOperationReturned(bodyPart);
        }
    });
}
Also used : IOpenPgpSinkResultCallback(org.openintents.openpgp.util.OpenPgpApi.IOpenPgpSinkResultCallback) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 5 with OpenPgpDataSource

use of org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource in project k-9 by k9mail.

the class MessageCryptoHelper method callAsyncDetachedVerify.

private void callAsyncDetachedVerify(Intent intent) throws IOException, MessagingException {
    OpenPgpDataSource dataSource = getDataSourceForSignedData(currentCryptoPart.part);
    byte[] signatureData = MessageCryptoStructureDetector.getSignatureData(currentCryptoPart.part);
    intent.putExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE, signatureData);
    openPgpApi.executeApiAsync(intent, dataSource, new IOpenPgpSinkResultCallback<Void>() {

        @Override
        public void onReturn(Intent result, Void dummy) {
            cancelableBackgroundOperation = null;
            currentCryptoResult = result;
            onCryptoOperationReturned(null);
        }

        @Override
        public void onProgress(int current, int max) {
            Timber.d("received progress status: %d / %d", current, max);
            callbackProgress(current, max);
        }
    });
}
Also used : OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Aggregations

OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)10 PendingIntent (android.app.PendingIntent)7 Intent (android.content.Intent)7 MessagingException (com.fsck.k9.mail.MessagingException)5 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)5 OutputStream (java.io.OutputStream)5 IOpenPgpSinkResultCallback (org.openintents.openpgp.util.OpenPgpApi.IOpenPgpSinkResultCallback)4 EOLConvertingOutputStream (com.fsck.k9.mail.filter.EOLConvertingOutputStream)3 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)3 IOException (java.io.IOException)3 Body (com.fsck.k9.mail.Body)2 BodyPart (com.fsck.k9.mail.BodyPart)2 Multipart (com.fsck.k9.mail.Multipart)2 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)2 TextBody (com.fsck.k9.mail.internet.TextBody)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OpenPgpError (org.openintents.openpgp.OpenPgpError)2 NonNull (androidx.annotation.NonNull)1 Part (com.fsck.k9.mail.Part)1 SizeAware (com.fsck.k9.mail.internet.SizeAware)1