Search in sources :

Example 6 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, boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
    final MimeBodyPart bodyPart = currentProcessedMimeMessage.toBodyPart();
    String[] contentType = currentProcessedMimeMessage.getHeader(MimeHeader.HEADER_CONTENT_TYPE);
    if (contentType.length > 0) {
        bodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType[0]);
    }
    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) 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) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 7 with OpenPgpDataSource

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

the class MessageCryptoHelper method getDataSourceForEncryptedOrInlineData.

private OpenPgpDataSource getDataSourceForEncryptedOrInlineData() throws IOException {
    return new OpenPgpApi.OpenPgpDataSource() {

        @Override
        public Long getSizeForProgress() {
            Part part = currentCryptoPart.part;
            CryptoPartType cryptoPartType = currentCryptoPart.type;
            Body body;
            if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                body = encryptionPayloadPart.getBody();
            } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                body = part.getBody();
            } else {
                throw new IllegalStateException("part to stream must be encrypted or inline!");
            }
            if (body instanceof SizeAware) {
                long bodySize = ((SizeAware) body).getSize();
                if (bodySize > PROGRESS_SIZE_THRESHOLD) {
                    return bodySize;
                }
            }
            return null;
        }

        @Override
        @WorkerThread
        public void writeTo(OutputStream os) throws IOException {
            try {
                Part part = currentCryptoPart.part;
                CryptoPartType cryptoPartType = currentCryptoPart.type;
                if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                    Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                    BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                    Body encryptionPayloadBody = encryptionPayloadPart.getBody();
                    encryptionPayloadBody.writeTo(os);
                } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                    String text = MessageExtractor.getTextFromPart(part);
                    os.write(text.getBytes());
                } else {
                    throw new IllegalStateException("part to stream must be encrypted or inline!");
                }
            } catch (MessagingException e) {
                Timber.e(e, "MessagingException while writing message to crypto provider");
            }
        }
    };
}
Also used : MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) SizeAware(com.fsck.k9.mail.internet.SizeAware) MessagingException(com.fsck.k9.mail.MessagingException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body)

Aggregations

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