Search in sources :

Example 31 with BodyPart

use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.

the class MessageTest method testToBodyPart.

@Test
public void testToBodyPart() throws MessagingException, IOException {
    MimeMessage message;
    ByteArrayOutputStream out;
    BinaryTempFileBody.setTempDirectory(context.getCacheDir());
    mMimeBoundary = 101;
    message = nestedMessage(nestedMessage(sampleMessage()));
    out = new ByteArrayOutputStream();
    MimeBodyPart bodyPart = message.toBodyPart();
    bodyPart.writeTo(out);
    assertEquals(TO_BODY_PART_RESULT, out.toString());
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 32 with BodyPart

use of com.fsck.k9.mail.BodyPart 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 33 with BodyPart

use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.

the class PgpMimeMessageTest method testSignedMessage.

@Test
public void testSignedMessage() throws IOException, MessagingException, PGPException {
    String messageSource = "Date: Mon, 08 Dec 2014 17:44:18 +0100\r\n" + "From: cketti <cketti@googlemail.com>\r\n" + "MIME-Version: 1.0\r\n" + "To: test@example.com\r\n" + "Subject: OpenPGP signature test\r\n" + "Content-Type: multipart/signed; micalg=pgp-sha1;\r\n" + " protocol=\"application/pgp-signature\";\r\n" + " boundary=\"24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\"\r\n" + "\r\n" + "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)\r\n" + "--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\r\n" + "Content-Type: multipart/mixed;\r\n" + " boundary=\"------------030308060900040601010501\"\r\n" + "\r\n" + "This is a multi-part message in MIME format.\r\n" + "--------------030308060900040601010501\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + "\r\n" + "Message body\r\n" + "goes here\r\n" + "\r\n" + "\r\n" + "--------------030308060900040601010501\r\n" + "Content-Type: text/plain; charset=UTF-8;\r\n" + " name=\"attachment.txt\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment;\r\n" + " filename=\"attachment.txt\"\r\n" + "\r\n" + "VGV4dCBhdHRhY2htZW50Cg==\r\n" + "--------------030308060900040601010501--\r\n" + "\r\n" + "--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\r\n" + "Content-Type: application/pgp-signature; name=\"signature.asc\"\r\n" + "Content-Description: OpenPGP digital signature\r\n" + "Content-Disposition: attachment; filename=\"signature.asc\"\r\n" + "\r\n" + "-----BEGIN PGP SIGNATURE-----\r\n" + "Version: GnuPG v1\r\n" + "\r\n" + "iQIcBAEBAgAGBQJUhdVqAAoJEO4v7zp9qOKJ8DQP/1+JE8UF7UmirnN1ZO+25hFC\r\n" + "jAfFMxRWMWXN0gGB+6ySy6ah0bCwmRwHpRBsW/tNcsmOPKb2XBf9zwF06uk/lLp4\r\n" + "ZmGXxSdQ9XJrlaHk8Sitn9Gi/1L+MNWgrsrLROAZv2jfc9wqN3FOrhN9NC1QXQvO\r\n" + "+D7sMorSr3l94majoIDrzvxEnfJVfrZWNTUaulJofOJ55GBZ3UJNob1WKjrnculL\r\n" + "IwmSERmVUoFBUfe/MBqqZH0WDJq9nt//NZFHLunj6nGsrpush1dQRcbR3zzQfXkk\r\n" + "s7zDLDa8VUv6OxcefjsVN/O7EenoWWgNg6GfW6tY2+oUsLSP2OS3JXvYsylQP4hR\r\n" + "iU1V9vvsu2Ax6bVb0+uTqw3jNiqVFy3o4mBigVUqp1EFIwBYmyNbe5wj4ACs9Avj\r\n" + "9t2reFSfXobWQFUS4s71JeMefNAHHJWZI63wNTxE6LOw01YxdJiDaPWGTOyM75MK\r\n" + "yqn7r5uIfeSv8NypGJaUv4firxKbrcZKk7Wpeh/rZuUSgoPcf3I1IzXfGKKIBHjU\r\n" + "WUMhTF5SoC5kIZyeXvHrhTM8HszcS8EoG2XcmcYArwgCUlOunFwZNqLPsfdMTRL6\r\n" + "9rcioaohEtroqoJiGAToJtIz8kqCaamnP/ASBkp9qqJizRd6fqt+tE8BsmJbuPLS\r\n" + "6lBpS8j0TqmaZMYfB9u4\r\n" + "=QvET\r\n" + "-----END PGP SIGNATURE-----\r\n" + "\r\n" + "--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM--\r\n";
    BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
    InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
    MimeMessage message;
    try {
        message = MimeMessage.parseMimeMessage(messageInputStream, true);
    } finally {
        messageInputStream.close();
    }
    Multipart multipartSigned = (Multipart) message.getBody();
    BodyPart signedPart = multipartSigned.getBodyPart(0);
    ByteArrayOutputStream signedPartOutputStream = new ByteArrayOutputStream();
    signedPart.writeTo(signedPartOutputStream);
    byte[] signedData = signedPartOutputStream.toByteArray();
    Body signatureBody = multipartSigned.getBodyPart(1).getBody();
    ByteArrayOutputStream signatureBodyOutputStream = new ByteArrayOutputStream();
    signatureBody.writeTo(signatureBodyOutputStream);
    byte[] signatureData = signatureBodyOutputStream.toByteArray();
    assertTrue(verifySignature(signedData, signatureData));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) Test(org.junit.Test)

Example 34 with BodyPart

use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.

the class MimeMultipart method writeTo.

@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
    if (preamble != null) {
        out.write(preamble);
        writer.write("\r\n");
    }
    if (getBodyParts().isEmpty()) {
        writer.write("--");
        writer.write(boundary);
        writer.write("\r\n");
    } else {
        for (BodyPart bodyPart : getBodyParts()) {
            writer.write("--");
            writer.write(boundary);
            writer.write("\r\n");
            writer.flush();
            bodyPart.writeTo(out);
            writer.write("\r\n");
        }
    }
    writer.write("--");
    writer.write(boundary);
    writer.write("--\r\n");
    writer.flush();
    if (epilogue != null) {
        out.write(epilogue);
    }
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 35 with BodyPart

use of com.fsck.k9.mail.BodyPart in project k-9 by k9mail.

the class MessageCryptoHelper method getDataSourceForSignedData.

private OpenPgpDataSource getDataSourceForSignedData(final Part signedPart) throws IOException {
    return new OpenPgpDataSource() {

        @Override
        public void writeTo(OutputStream os) throws IOException {
            try {
                Multipart multipartSignedMultipart = (Multipart) signedPart.getBody();
                BodyPart signatureBodyPart = multipartSignedMultipart.getBodyPart(0);
                Timber.d("signed data type: %s", signatureBodyPart.getMimeType());
                signatureBodyPart.writeTo(os);
            } catch (MessagingException e) {
                Timber.e(e, "Exception 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) MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)

Aggregations

BodyPart (com.fsck.k9.mail.BodyPart)49 Part (com.fsck.k9.mail.Part)38 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)33 Test (org.junit.Test)32 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 Multipart (com.fsck.k9.mail.Multipart)21 Body (com.fsck.k9.mail.Body)17 Message (com.fsck.k9.mail.Message)17 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)13 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)11 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)11 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 MessagingException (com.fsck.k9.mail.MessagingException)9 OutputStream (java.io.OutputStream)8 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)7 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)7 ArrayList (java.util.ArrayList)6 Stack (java.util.Stack)6 TextBody (com.fsck.k9.mail.internet.TextBody)5