MessageDecryptVerified -> MessageCryptoStructureDetector

This commit is contained in:
Vincent Breitmoser 2017-09-13 11:37:41 +02:00
parent 68e0a8721f
commit afc4febd5d
7 changed files with 57 additions and 49 deletions

View file

@ -26,7 +26,7 @@ import com.fsck.k9.ui.crypto.MessageCryptoAnnotations;
import static com.fsck.k9.mail.internet.MimeUtility.isSameMimeType;
public class MessageDecryptVerifier {
public class MessageCryptoStructureDetector {
private static final String MULTIPART_ENCRYPTED = "multipart/encrypted";
private static final String MULTIPART_SIGNED = "multipart/signed";
private static final String PROTOCOL_PARAMETER = "protocol";

View file

@ -3,7 +3,7 @@ package com.fsck.k9.message;
import java.util.List;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.crypto.MessageCryptoStructureDetector;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Part;
@ -14,7 +14,7 @@ public class ComposePgpEnableByDefaultDecider {
}
private boolean messageIsEncrypted(Message localMessage) {
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(localMessage);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(localMessage);
return !encryptedParts.isEmpty();
}
}

View file

@ -3,7 +3,7 @@ package com.fsck.k9.message;
import java.util.List;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.crypto.MessageCryptoStructureDetector;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Part;
@ -15,7 +15,7 @@ public class ComposePgpInlineDecider {
}
private boolean messageHasPgpInlineParts(Message localMessage) {
List<Part> inlineParts = MessageDecryptVerifier.findPgpInlineParts(localMessage);
List<Part> inlineParts = MessageCryptoStructureDetector.findPgpInlineParts(localMessage);
return !inlineParts.isEmpty();
}
}

View file

@ -3,7 +3,7 @@ package com.fsck.k9.message.extractors;
import android.support.annotation.NonNull;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.crypto.MessageCryptoStructureDetector;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.BodyPart;
import com.fsck.k9.mail.Message;
@ -31,7 +31,7 @@ class EncryptionDetector {
private boolean containsInlinePgpEncryptedText(Message message) {
Part textPart = textPartFinder.findFirstTextPart(message);
return MessageDecryptVerifier.isPartPgpInlineEncrypted(textPart);
return MessageCryptoStructureDetector.isPartPgpInlineEncrypted(textPart);
}
private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) {

View file

@ -19,7 +19,7 @@ import android.support.annotation.WorkerThread;
import com.fsck.k9.K9;
import com.fsck.k9.autocrypt.AutocryptOperations;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.crypto.MessageCryptoStructureDetector;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.BodyPart;
@ -124,13 +124,13 @@ public class MessageCryptoHelper {
}
private void findPartsForMultipartEncryptionPass() {
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(currentMessage);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(currentMessage);
for (Part part : encryptedParts) {
if (!MessageHelper.isCompletePartAvailable(part)) {
addErrorAnnotation(part, CryptoError.OPENPGP_ENCRYPTED_BUT_INCOMPLETE, MessageHelper.createEmptyPart());
continue;
}
if (MessageDecryptVerifier.isMultipartEncryptedOpenPgpProtocol(part)) {
if (MessageCryptoStructureDetector.isMultipartEncryptedOpenPgpProtocol(part)) {
CryptoPart cryptoPart = new CryptoPart(CryptoPartType.PGP_ENCRYPTED, part);
partsToProcess.add(cryptoPart);
continue;
@ -140,14 +140,15 @@ public class MessageCryptoHelper {
}
private void findPartsForMultipartSignaturePass() {
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(currentMessage, messageAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(currentMessage, messageAnnotations);
for (Part part : signedParts) {
if (!MessageHelper.isCompletePartAvailable(part)) {
MimeBodyPart replacementPart = getMultipartSignedContentPartIfAvailable(part);
addErrorAnnotation(part, CryptoError.OPENPGP_SIGNED_BUT_INCOMPLETE, replacementPart);
continue;
}
if (MessageDecryptVerifier.isMultipartSignedOpenPgpProtocol(part)) {
if (MessageCryptoStructureDetector.isMultipartSignedOpenPgpProtocol(part)) {
CryptoPart cryptoPart = new CryptoPart(CryptoPartType.PGP_SIGNED, part);
partsToProcess.add(cryptoPart);
continue;
@ -158,10 +159,10 @@ public class MessageCryptoHelper {
}
private void findPartsForPgpInlinePass() {
List<Part> inlineParts = MessageDecryptVerifier.findPgpInlineParts(currentMessage);
List<Part> inlineParts = MessageCryptoStructureDetector.findPgpInlineParts(currentMessage);
for (Part part : inlineParts) {
if (!currentMessage.getFlags().contains(Flag.X_DOWNLOADED_FULL)) {
if (MessageDecryptVerifier.isPartPgpInlineEncrypted(part)) {
if (MessageCryptoStructureDetector.isPartPgpInlineEncrypted(part)) {
addErrorAnnotation(part, CryptoError.OPENPGP_ENCRYPTED_BUT_INCOMPLETE, NO_REPLACEMENT_PART);
} else {
MimeBodyPart replacementPart = extractClearsignedTextReplacementPart(part);
@ -386,7 +387,7 @@ public class MessageCryptoHelper {
private void callAsyncDetachedVerify(Intent intent) throws IOException, MessagingException {
OpenPgpDataSource dataSource = getDataSourceForSignedData(currentCryptoPart.part);
byte[] signatureData = MessageDecryptVerifier.getSignatureData(currentCryptoPart.part);
byte[] signatureData = MessageCryptoStructureDetector.getSignatureData(currentCryptoPart.part);
intent.putExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE, signatureData);
openPgpApi.executeApiAsync(intent, dataSource, new IOpenPgpSinkResultCallback<Void>() {

View file

@ -8,7 +8,7 @@ import java.util.List;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.crypto.MessageCryptoStructureDetector;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mailstore.CryptoResultAnnotation;
@ -21,7 +21,7 @@ public class MessageCryptoSplitter {
@Nullable
public static CryptoMessageParts split(@NonNull Message message, @Nullable MessageCryptoAnnotations annotations) {
ArrayList<Part> extraParts = new ArrayList<>();
Part primaryPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, extraParts);
Part primaryPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, extraParts);
if (primaryPart == null) {
return null;
}

View file

@ -31,7 +31,7 @@ import static org.mockito.Mockito.mock;
@SuppressWarnings("WeakerAccess")
@RunWith(K9RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class MessageDecryptVerifierTest {
public class MessageCryptoStructureDetectorTest {
MessageCryptoAnnotations messageCryptoAnnotations = mock(MessageCryptoAnnotations.class);
static final String PGP_INLINE_DATA = "" +
"-----BEGIN PGP MESSAGE-----\n" +
@ -47,7 +47,7 @@ public class MessageDecryptVerifierTest {
Message message = new MimeMessage();
MimeMessageHelper.setBody(message, new TextBody(PGP_INLINE_DATA));
Part cryptoPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertSame(message, cryptoPart);
}
@ -63,7 +63,7 @@ public class MessageDecryptVerifierTest {
)
);
Part cryptoPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertSame(pgpInlinePart, cryptoPart);
}
@ -79,7 +79,7 @@ public class MessageDecryptVerifierTest {
)
);
Part cryptoPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertSame(pgpInlinePart, cryptoPart);
}
@ -99,7 +99,7 @@ public class MessageDecryptVerifierTest {
)
);
Part cryptoPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertSame(pgpInlinePart, cryptoPart);
}
@ -111,7 +111,7 @@ public class MessageDecryptVerifierTest {
multipart("alternative", null)
);
Part cryptoPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertNull(cryptoPart);
}
@ -123,7 +123,7 @@ public class MessageDecryptVerifierTest {
multipart("mixed", null)
);
Part cryptoPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertNull(cryptoPart);
}
@ -138,7 +138,7 @@ public class MessageDecryptVerifierTest {
)
);
Part cryptoPart = MessageDecryptVerifier.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertNull(cryptoPart);
}
@ -147,7 +147,7 @@ public class MessageDecryptVerifierTest {
public void findEncryptedPartsShouldReturnEmptyListForEmptyMessage() throws Exception {
MimeMessage emptyMessage = new MimeMessage();
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(emptyMessage);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(emptyMessage);
assertEquals(0, encryptedParts.size());
}
@ -157,7 +157,7 @@ public class MessageDecryptVerifierTest {
MimeMessage message = new MimeMessage();
message.setBody(new TextBody("message text"));
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertEquals(0, encryptedParts.size());
}
@ -171,7 +171,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertEquals(1, encryptedParts.size());
assertSame(message, encryptedParts.get(0));
@ -186,7 +186,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertTrue(encryptedParts.isEmpty());
}
@ -213,7 +213,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertTrue(encryptedParts.isEmpty());
}
@ -226,7 +226,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertTrue(encryptedParts.isEmpty());
}
@ -242,7 +242,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertEquals(1, encryptedParts.size());
assertSame(getPart(message, 0), encryptedParts.get(0));
@ -264,7 +264,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertEquals(2, encryptedParts.size());
assertSame(getPart(message, 0), encryptedParts.get(0));
@ -283,7 +283,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertEquals(1, encryptedParts.size());
assertSame(getPart(message, 1), encryptedParts.get(0));
@ -301,7 +301,7 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> encryptedParts = MessageDecryptVerifier.findMultipartEncryptedParts(message);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertEquals(1, encryptedParts.size());
assertSame(getPart(message, 0), encryptedParts.get(0));
@ -316,7 +316,8 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(message, messageCryptoAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertEquals(1, signedParts.size());
assertSame(message, signedParts.get(0));
@ -331,7 +332,8 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(message, messageCryptoAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertTrue(signedParts.isEmpty());
}
@ -359,7 +361,8 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(message, messageCryptoAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertTrue(signedParts.isEmpty());
}
@ -376,7 +379,8 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(message, messageCryptoAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertEquals(1, signedParts.size());
assertSame(message, signedParts.get(0));
@ -393,7 +397,8 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(message, messageCryptoAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertEquals(1, signedParts.size());
assertSame(getPart(message, 0), signedParts.get(0));
@ -411,7 +416,8 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(message, messageCryptoAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertEquals(1, signedParts.size());
assertSame(getPart(message, 0), signedParts.get(0));
@ -429,7 +435,8 @@ public class MessageDecryptVerifierTest {
)
);
List<Part> signedParts = MessageDecryptVerifier.findMultipartSignedParts(message, messageCryptoAnnotations);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertEquals(1, signedParts.size());
assertSame(getPart(message, 1), signedParts.get(0));
@ -446,7 +453,7 @@ public class MessageDecryptVerifierTest {
MimeMessage message = new MimeMessage();
message.setBody(new TextBody(pgpInlineData));
assertTrue(MessageDecryptVerifier.isPartPgpInlineEncrypted(message));
assertTrue(MessageCryptoStructureDetector.isPartPgpInlineEncrypted(message));
}
@Test
@ -461,8 +468,8 @@ public class MessageDecryptVerifierTest {
MimeMessage message = new MimeMessage();
message.setBody(new TextBody(pgpInlineData));
assertTrue(MessageDecryptVerifier.isPartPgpInlineEncryptedOrSigned(message));
assertTrue(MessageDecryptVerifier.isPartPgpInlineEncrypted(message));
assertTrue(MessageCryptoStructureDetector.isPartPgpInlineEncryptedOrSigned(message));
assertTrue(MessageCryptoStructureDetector.isPartPgpInlineEncrypted(message));
}
@Test
@ -477,8 +484,8 @@ public class MessageDecryptVerifierTest {
MimeMessage message = new MimeMessage();
message.setBody(new TextBody(pgpInlineData));
assertFalse(MessageDecryptVerifier.isPartPgpInlineEncryptedOrSigned(message));
assertFalse(MessageDecryptVerifier.isPartPgpInlineEncrypted(message));
assertFalse(MessageCryptoStructureDetector.isPartPgpInlineEncryptedOrSigned(message));
assertFalse(MessageCryptoStructureDetector.isPartPgpInlineEncrypted(message));
}
@Test
@ -495,7 +502,7 @@ public class MessageDecryptVerifierTest {
MimeMessage message = new MimeMessage();
message.setBody(new TextBody(pgpInlineData));
assertTrue(MessageDecryptVerifier.isPartPgpInlineEncryptedOrSigned(message));
assertTrue(MessageCryptoStructureDetector.isPartPgpInlineEncryptedOrSigned(message));
}
@Test
@ -512,7 +519,7 @@ public class MessageDecryptVerifierTest {
MimeMessage message = new MimeMessage();
message.setBody(new TextBody(pgpInlineData));
assertFalse(MessageDecryptVerifier.isPartPgpInlineEncrypted(message));
assertFalse(MessageCryptoStructureDetector.isPartPgpInlineEncrypted(message));
}
static Part getPart(Part searchRootPart, int... indexes) {