Search in sources :

Example 16 with MessageCryptoAnnotations

use of com.fsck.k9.mailstore.MessageCryptoAnnotations in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method extractMessage_withCryptoAnnotation_andReplacementPart.

@Test
public void extractMessage_withCryptoAnnotation_andReplacementPart() throws Exception {
    Message message = messageFromBody(multipart("signed", "protocol=\"application/pgp-signature\"", bodypart("text/plain", "text"), bodypart("application/pgp-signature")));
    MimeBodyPart replacementPart = bodypart("text/plain", "replacement text");
    CryptoResultAnnotation annotation = CryptoResultAnnotation.createOpenPgpResultAnnotation(null, null, null, null, replacementPart, false);
    MessageCryptoAnnotations messageCryptoAnnotations = createAnnotations(message, annotation);
    MessageViewInfo messageViewInfo = messageViewInfoExtractor.extractMessageForView(message, messageCryptoAnnotations, false);
    assertEquals("<pre dir=\"auto\" class=\"k9mail\">replacement text</pre>", messageViewInfo.text);
    assertSame(annotation, messageViewInfo.cryptoResultAnnotation);
    assertSame(message, messageViewInfo.message);
    assertSame(replacementPart, messageViewInfo.rootPart);
    assertTrue(messageViewInfo.attachments.isEmpty());
    assertTrue(messageViewInfo.extraAttachments.isEmpty());
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 17 with MessageCryptoAnnotations

use of com.fsck.k9.mailstore.MessageCryptoAnnotations in project k-9 by k9mail.

the class MessageDecryptVerifier method findSignedParts.

public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) {
    List<Part> signedParts = new ArrayList<>();
    Stack<Part> partsToCheck = new Stack<>();
    partsToCheck.push(startPart);
    while (!partsToCheck.isEmpty()) {
        Part part = partsToCheck.pop();
        if (messageCryptoAnnotations.has(part)) {
            CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part);
            MimeBodyPart replacementData = resultAnnotation.getReplacementData();
            if (replacementData != null) {
                part = replacementData;
            }
        }
        Body body = part.getBody();
        if (isPartMultipartSigned(part)) {
            signedParts.add(part);
            continue;
        }
        if (body instanceof Multipart) {
            Multipart multipart = (Multipart) body;
            for (int i = multipart.getCount() - 1; i >= 0; i--) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                partsToCheck.push(bodyPart);
            }
        }
    }
    return signedParts;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) CryptoResultAnnotation(com.fsck.k9.mailstore.CryptoResultAnnotation) ArrayList(java.util.ArrayList) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack)

Example 18 with MessageCryptoAnnotations

use of com.fsck.k9.mailstore.MessageCryptoAnnotations in project k-9 by k9mail.

the class MessageDecryptVerifierTest method findEncrypted__withMultipartMixedSubSignedAndText__shouldReturnSigned.

@Test
public void findEncrypted__withMultipartMixedSubSignedAndText__shouldReturnSigned() throws Exception {
    Message message = messageFromBody(multipart("mixed", multipart("signed", bodypart("text/plain"), bodypart("application/pgp-signature")), bodypart("text/plain")));
    List<Part> signedParts = MessageDecryptVerifier.findSignedParts(message, messageCryptoAnnotations);
    assertEquals(1, signedParts.size());
    assertSame(getPart(message, 0), signedParts.get(0));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 19 with MessageCryptoAnnotations

use of com.fsck.k9.mailstore.MessageCryptoAnnotations in project k-9 by k9mail.

the class MessageViewInfoExtractor method extractMessageForView.

@WorkerThread
public MessageViewInfo extractMessageForView(Message message, @Nullable MessageCryptoAnnotations cryptoAnnotations, boolean openPgpProviderConfigured) throws MessagingException {
    ArrayList<Part> extraParts = new ArrayList<>();
    Part cryptoContentPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, extraParts);
    if (cryptoContentPart == null) {
        if (cryptoAnnotations != null && !cryptoAnnotations.isEmpty()) {
            Timber.e("Got crypto message cryptoContentAnnotations but no crypto root part!");
        }
        MessageViewInfo messageViewInfo = extractSimpleMessageForView(message, message);
        return messageViewInfo.withSubject(message.getSubject(), false);
    }
    boolean isOpenPgpEncrypted = (MessageCryptoStructureDetector.isPartMultipartEncrypted(cryptoContentPart) && MessageCryptoStructureDetector.isMultipartEncryptedOpenPgpProtocol(cryptoContentPart)) || MessageCryptoStructureDetector.isPartPgpInlineEncrypted(cryptoContentPart);
    if (!openPgpProviderConfigured && isOpenPgpEncrypted) {
        CryptoResultAnnotation noProviderAnnotation = CryptoResultAnnotation.createErrorAnnotation(CryptoError.OPENPGP_ENCRYPTED_NO_PROVIDER, null);
        return MessageViewInfo.createWithErrorState(message, false).withCryptoData(noProviderAnnotation, null, null);
    }
    MessageViewInfo messageViewInfo = getMessageContent(message, cryptoAnnotations, extraParts, cryptoContentPart);
    messageViewInfo = extractSubject(messageViewInfo);
    return messageViewInfo;
}
Also used : Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) WorkerThread(androidx.annotation.WorkerThread)

Example 20 with MessageCryptoAnnotations

use of com.fsck.k9.mailstore.MessageCryptoAnnotations in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method extractMessage_withCryptoAnnotation_andExtraText.

@Test
public void extractMessage_withCryptoAnnotation_andExtraText() throws Exception {
    MimeBodyPart signedPart = multipart("signed", "protocol=\"application/pgp-signature\"", bodypart("text/plain", "text"), bodypart("application/pgp-signature"));
    BodyPart extraText = bodypart("text/plain", "extra text");
    Message message = messageFromBody(multipart("mixed", signedPart, extraText));
    CryptoResultAnnotation annotation = CryptoResultAnnotation.createOpenPgpResultAnnotation(null, null, null, null, null, false);
    MessageCryptoAnnotations messageCryptoAnnotations = createAnnotations(signedPart, annotation);
    MessageViewInfo messageViewInfo = messageViewInfoExtractor.extractMessageForView(message, messageCryptoAnnotations, false);
    assertEquals("<pre dir=\"auto\" class=\"k9mail\">text</pre>", messageViewInfo.text);
    assertSame(annotation, messageViewInfo.cryptoResultAnnotation);
    assertEquals("extra text", messageViewInfo.extraText);
    assertTrue(messageViewInfo.attachments.isEmpty());
    assertTrue(messageViewInfo.extraAttachments.isEmpty());
}
Also used : MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Aggregations

MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 Test (org.junit.Test)22 Message (com.fsck.k9.mail.Message)20 Part (com.fsck.k9.mail.Part)19 BodyPart (com.fsck.k9.mail.BodyPart)18 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)12 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)6 MessageCryptoAnnotations (com.fsck.k9.mailstore.MessageCryptoAnnotations)5 ArrayList (java.util.ArrayList)5 CryptoResultAnnotation (com.fsck.k9.mailstore.CryptoResultAnnotation)4 RobolectricTest (com.fsck.k9.RobolectricTest)2 Body (com.fsck.k9.mail.Body)2 Multipart (com.fsck.k9.mail.Multipart)2 Stack (java.util.Stack)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Nullable (android.support.annotation.Nullable)1 WorkerThread (android.support.annotation.WorkerThread)1 WorkerThread (androidx.annotation.WorkerThread)1 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)1