Loading k9mail/src/main/java/com/fsck/k9/crypto/MessageDecryptVerifier.java +16 −12 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Stack; import android.support.annotation.Nullable; import android.text.TextUtils; import com.fsck.k9.mail.Body; Loading Loading @@ -128,7 +129,7 @@ public class MessageDecryptVerifier { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartPgpInline(part)) { if (isPartPgpInlineEncryptedOrSigned(part)) { inlineParts.add(part); continue; } Loading Loading @@ -162,17 +163,8 @@ public class MessageDecryptVerifier { return null; } private static boolean isPartPgpInline(Part part) { if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK); return !TextUtils.isEmpty(text) && (text.startsWith(PGP_INLINE_START_MARKER) || text.startsWith(PGP_INLINE_SIGNED_START_MARKER)); } private static boolean isPartEncryptedOrSigned(Part part) { return isPartMultipartEncrypted(part) || isPartMultipartSigned(part) || isPartPgpInline(part); return isPartMultipartEncrypted(part) || isPartMultipartSigned(part) || isPartPgpInlineEncryptedOrSigned(part); } private static boolean isPartMultipartSigned(Part part) { Loading @@ -196,7 +188,19 @@ public class MessageDecryptVerifier { return isPgpEncrypted || isPgpSigned; } public static boolean isPartPgpInlineEncrypted(Part part) { private static boolean isPartPgpInlineEncryptedOrSigned(Part part) { if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK); return !TextUtils.isEmpty(text) && (text.startsWith(PGP_INLINE_START_MARKER) || text.startsWith(PGP_INLINE_SIGNED_START_MARKER)); } public static boolean isPartPgpInlineEncrypted(@Nullable Part part) { if (part == null) { return false; } if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } Loading k9mail/src/main/java/com/fsck/k9/message/extractors/EncryptionDetector.java +2 −21 Original line number Diff line number Diff line package com.fsck.k9.message.extractors; import java.util.regex.Pattern; import android.support.annotation.NonNull; import com.fsck.k9.crypto.MessageDecryptVerifier; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.BodyPart; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; import com.fsck.k9.mail.internet.MessageExtractor; import static com.fsck.k9.mail.internet.MimeUtility.isSameMimeType; class EncryptionDetector { private static final Pattern PGP_MESSAGE_PATTERN = Pattern.compile( "(^|\\r\\n)-----BEGIN PGP MESSAGE-----\\r\\n.*?\\r\\n-----END PGP MESSAGE-----(\\r\\n|$)", Pattern.DOTALL); private final TextPartFinder textPartFinder; Loading @@ -37,20 +31,7 @@ class EncryptionDetector { private boolean containsInlinePgpEncryptedText(Message message) { Part textPart = textPartFinder.findFirstTextPart(message); if (!isUsableTextPart(textPart)) { return false; } String text = MessageExtractor.getTextFromPart(textPart); if (text == null) { return false; } return PGP_MESSAGE_PATTERN.matcher(text).find(); } private boolean isUsableTextPart(Part textPart) { return textPart != null && textPart.getBody() != null; return MessageDecryptVerifier.isPartPgpInlineEncrypted(textPart); } private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) { Loading k9mail/src/test/java/com/fsck/k9/message/extractors/EncryptionDetectorTest.java +7 −3 Original line number Diff line number Diff line Loading @@ -4,6 +4,9 @@ package com.fsck.k9.message.extractors; import com.fsck.k9.mail.Message; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import static com.fsck.k9.message.MessageCreationHelper.createMessage; import static com.fsck.k9.message.MessageCreationHelper.createMultipartMessage; Loading @@ -14,7 +17,8 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) @Config(manifest = "src/main/AndroidManifest.xml", sdk = 21) public class EncryptionDetectorTest { private static final String CRLF = "\r\n"; Loading Loading @@ -82,7 +86,7 @@ public class EncryptionDetectorTest { } @Test public void isEncrypted_withPlainTextAndInlinePgp_shouldReturnTrue() throws Exception { public void isEncrypted_withPlainTextAndPreambleWithInlinePgp_shouldReturnFalse() throws Exception { Message message = createTextMessage("text/plain", "" + "preamble" + CRLF + "-----BEGIN PGP MESSAGE-----" + CRLF + Loading @@ -93,7 +97,7 @@ public class EncryptionDetectorTest { boolean encrypted = encryptionDetector.isEncrypted(message); assertTrue(encrypted); assertFalse(encrypted); } @Test Loading Loading
k9mail/src/main/java/com/fsck/k9/crypto/MessageDecryptVerifier.java +16 −12 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Stack; import android.support.annotation.Nullable; import android.text.TextUtils; import com.fsck.k9.mail.Body; Loading Loading @@ -128,7 +129,7 @@ public class MessageDecryptVerifier { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartPgpInline(part)) { if (isPartPgpInlineEncryptedOrSigned(part)) { inlineParts.add(part); continue; } Loading Loading @@ -162,17 +163,8 @@ public class MessageDecryptVerifier { return null; } private static boolean isPartPgpInline(Part part) { if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK); return !TextUtils.isEmpty(text) && (text.startsWith(PGP_INLINE_START_MARKER) || text.startsWith(PGP_INLINE_SIGNED_START_MARKER)); } private static boolean isPartEncryptedOrSigned(Part part) { return isPartMultipartEncrypted(part) || isPartMultipartSigned(part) || isPartPgpInline(part); return isPartMultipartEncrypted(part) || isPartMultipartSigned(part) || isPartPgpInlineEncryptedOrSigned(part); } private static boolean isPartMultipartSigned(Part part) { Loading @@ -196,7 +188,19 @@ public class MessageDecryptVerifier { return isPgpEncrypted || isPgpSigned; } public static boolean isPartPgpInlineEncrypted(Part part) { private static boolean isPartPgpInlineEncryptedOrSigned(Part part) { if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK); return !TextUtils.isEmpty(text) && (text.startsWith(PGP_INLINE_START_MARKER) || text.startsWith(PGP_INLINE_SIGNED_START_MARKER)); } public static boolean isPartPgpInlineEncrypted(@Nullable Part part) { if (part == null) { return false; } if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) { return false; } Loading
k9mail/src/main/java/com/fsck/k9/message/extractors/EncryptionDetector.java +2 −21 Original line number Diff line number Diff line package com.fsck.k9.message.extractors; import java.util.regex.Pattern; import android.support.annotation.NonNull; import com.fsck.k9.crypto.MessageDecryptVerifier; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.BodyPart; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; import com.fsck.k9.mail.internet.MessageExtractor; import static com.fsck.k9.mail.internet.MimeUtility.isSameMimeType; class EncryptionDetector { private static final Pattern PGP_MESSAGE_PATTERN = Pattern.compile( "(^|\\r\\n)-----BEGIN PGP MESSAGE-----\\r\\n.*?\\r\\n-----END PGP MESSAGE-----(\\r\\n|$)", Pattern.DOTALL); private final TextPartFinder textPartFinder; Loading @@ -37,20 +31,7 @@ class EncryptionDetector { private boolean containsInlinePgpEncryptedText(Message message) { Part textPart = textPartFinder.findFirstTextPart(message); if (!isUsableTextPart(textPart)) { return false; } String text = MessageExtractor.getTextFromPart(textPart); if (text == null) { return false; } return PGP_MESSAGE_PATTERN.matcher(text).find(); } private boolean isUsableTextPart(Part textPart) { return textPart != null && textPart.getBody() != null; return MessageDecryptVerifier.isPartPgpInlineEncrypted(textPart); } private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) { Loading
k9mail/src/test/java/com/fsck/k9/message/extractors/EncryptionDetectorTest.java +7 −3 Original line number Diff line number Diff line Loading @@ -4,6 +4,9 @@ package com.fsck.k9.message.extractors; import com.fsck.k9.mail.Message; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import static com.fsck.k9.message.MessageCreationHelper.createMessage; import static com.fsck.k9.message.MessageCreationHelper.createMultipartMessage; Loading @@ -14,7 +17,8 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) @Config(manifest = "src/main/AndroidManifest.xml", sdk = 21) public class EncryptionDetectorTest { private static final String CRLF = "\r\n"; Loading Loading @@ -82,7 +86,7 @@ public class EncryptionDetectorTest { } @Test public void isEncrypted_withPlainTextAndInlinePgp_shouldReturnTrue() throws Exception { public void isEncrypted_withPlainTextAndPreambleWithInlinePgp_shouldReturnFalse() throws Exception { Message message = createTextMessage("text/plain", "" + "preamble" + CRLF + "-----BEGIN PGP MESSAGE-----" + CRLF + Loading @@ -93,7 +97,7 @@ public class EncryptionDetectorTest { boolean encrypted = encryptionDetector.isEncrypted(message); assertTrue(encrypted); assertFalse(encrypted); } @Test Loading