Merge branch 'GH-571_check_protocol_parameter'

This commit is contained in:
cketti 2015-12-16 23:52:18 +01:00
commit 971ae08426
2 changed files with 44 additions and 23 deletions

View file

@ -13,6 +13,7 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part; import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MessageExtractor; import com.fsck.k9.mail.internet.MessageExtractor;
import com.fsck.k9.mail.internet.MimeUtility;
import org.openintents.openpgp.util.OpenPgpUtils; import org.openintents.openpgp.util.OpenPgpUtils;
import static com.fsck.k9.mail.internet.MimeUtility.isSameMimeType; import static com.fsck.k9.mail.internet.MimeUtility.isSameMimeType;
@ -34,10 +35,9 @@ public class MessageDecryptVerifier {
while (!partsToCheck.isEmpty()) { while (!partsToCheck.isEmpty()) {
Part part = partsToCheck.pop(); Part part = partsToCheck.pop();
String mimeType = part.getMimeType();
Body body = part.getBody(); Body body = part.getBody();
if (isSameMimeType(mimeType, MULTIPART_ENCRYPTED)) { if (isPgpMimeEncryptedPart(part)) {
encryptedParts.add(part); encryptedParts.add(part);
} else if (body instanceof Multipart) { } else if (body instanceof Multipart) {
Multipart multipart = (Multipart) body; Multipart multipart = (Multipart) body;
@ -58,10 +58,9 @@ public class MessageDecryptVerifier {
while (!partsToCheck.isEmpty()) { while (!partsToCheck.isEmpty()) {
Part part = partsToCheck.pop(); Part part = partsToCheck.pop();
String mimeType = part.getMimeType();
Body body = part.getBody(); Body body = part.getBody();
if (isSameMimeType(mimeType, MULTIPART_SIGNED)) { if (isPgpMimeSignedPart(part)) {
signedParts.add(part); signedParts.add(part);
} else if (body instanceof Multipart) { } else if (body instanceof Multipart) {
Multipart multipart = (Multipart) body; Multipart multipart = (Multipart) body;
@ -105,8 +104,7 @@ public class MessageDecryptVerifier {
} }
public static byte[] getSignatureData(Part part) throws IOException, MessagingException { public static byte[] getSignatureData(Part part) throws IOException, MessagingException {
if (isPgpMimeSignedPart(part)) {
if (isSameMimeType(part.getMimeType(), MULTIPART_SIGNED)) {
Body body = part.getBody(); Body body = part.getBody();
if (body instanceof Multipart) { if (body instanceof Multipart) {
Multipart multi = (Multipart) body; Multipart multi = (Multipart) body;
@ -122,15 +120,23 @@ public class MessageDecryptVerifier {
return null; return null;
} }
public static boolean isPgpMimeSignedPart(Part part) { private static boolean isPgpMimeSignedPart(Part part) {
return isSameMimeType(part.getMimeType(), MULTIPART_SIGNED); if (!isSameMimeType(part.getMimeType(), MULTIPART_SIGNED)) {
return false;
}
String contentType = part.getContentType();
String protocol = MimeUtility.getHeaderParameter(contentType, PROTOCOL_PARAMETER);
return APPLICATION_PGP_SIGNATURE.equalsIgnoreCase(protocol);
} }
public static boolean isPgpMimeEncryptedPart(Part part) { private static boolean isPgpMimeEncryptedPart(Part part) {
//FIXME: Doesn't work right now because LocalMessage.getContentType() doesn't load headers from database if (!isSameMimeType(part.getMimeType(), MULTIPART_ENCRYPTED)) {
// String contentType = part.getContentType(); return false;
// String protocol = MimeUtility.getHeaderParameter(contentType, PROTOCOL_PARAMETER); }
// return APPLICATION_PGP_ENCRYPTED.equals(protocol);
return isSameMimeType(part.getMimeType(), MULTIPART_ENCRYPTED); String contentType = part.getContentType();
String protocol = MimeUtility.getHeaderParameter(contentType, PROTOCOL_PARAMETER);
return APPLICATION_PGP_ENCRYPTED.equalsIgnoreCase(protocol);
} }
} }

View file

@ -3,8 +3,10 @@ package com.fsck.k9.crypto;
import java.util.List; import java.util.List;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part; import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.MimeHeader;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.MimeMessageHelper; import com.fsck.k9.mail.internet.MimeMessageHelper;
import com.fsck.k9.mail.internet.MimeMultipart; import com.fsck.k9.mail.internet.MimeMultipart;
@ -27,6 +29,7 @@ public class MessageDecryptVerifierTest {
MimeMessage emptyMessage = new MimeMessage(); MimeMessage emptyMessage = new MimeMessage();
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(emptyMessage); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(emptyMessage);
assertEquals(0, encryptedParts.size()); assertEquals(0, encryptedParts.size());
} }
@ -36,17 +39,20 @@ public class MessageDecryptVerifierTest {
message.setBody(new TextBody("message text")); message.setBody(new TextBody("message text"));
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
assertEquals(0, encryptedParts.size()); assertEquals(0, encryptedParts.size());
} }
@Test @Test
public void findEncryptedPartsShouldReturnEmptyEncryptedPart() throws Exception { public void findEncryptedPartsShouldReturnEmptyEncryptedPart() throws Exception {
MimeMessage message = new MimeMessage(); MimeMessage message = new MimeMessage();
MimeMultipart mulitpartEncrypted = new MimeMultipart(); MimeMultipart multipartEncrypted = new MimeMultipart();
mulitpartEncrypted.setSubType("encrypted"); multipartEncrypted.setSubType("encrypted");
MimeMessageHelper.setBody(message, mulitpartEncrypted); MimeMessageHelper.setBody(message, multipartEncrypted);
addProtocolParameter(message);
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
assertEquals(1, encryptedParts.size()); assertEquals(1, encryptedParts.size());
assertSame(message, encryptedParts.get(0)); assertSame(message, encryptedParts.get(0));
} }
@ -58,22 +64,31 @@ public class MessageDecryptVerifierTest {
multipartMixed.setSubType("mixed"); multipartMixed.setSubType("mixed");
MimeMessageHelper.setBody(message, multipartMixed); MimeMessageHelper.setBody(message, multipartMixed);
MimeMultipart mulitpartEncryptedOne = new MimeMultipart(); MimeMultipart multipartEncryptedOne = new MimeMultipart();
mulitpartEncryptedOne.setSubType("encrypted"); multipartEncryptedOne.setSubType("encrypted");
MimeBodyPart bodyPartOne = new MimeBodyPart(mulitpartEncryptedOne); MimeBodyPart bodyPartOne = new MimeBodyPart(multipartEncryptedOne);
addProtocolParameter(bodyPartOne);
multipartMixed.addBodyPart(bodyPartOne); multipartMixed.addBodyPart(bodyPartOne);
MimeBodyPart bodyPartTwo = new MimeBodyPart(null, "text/plain"); MimeBodyPart bodyPartTwo = new MimeBodyPart(null, "text/plain");
multipartMixed.addBodyPart(bodyPartTwo); multipartMixed.addBodyPart(bodyPartTwo);
MimeMultipart mulitpartEncryptedThree = new MimeMultipart(); MimeMultipart multipartEncryptedThree = new MimeMultipart();
mulitpartEncryptedThree.setSubType("encrypted"); multipartEncryptedThree.setSubType("encrypted");
MimeBodyPart bodyPartThree = new MimeBodyPart(mulitpartEncryptedThree); MimeBodyPart bodyPartThree = new MimeBodyPart(multipartEncryptedThree);
addProtocolParameter(bodyPartThree);
multipartMixed.addBodyPart(bodyPartThree); multipartMixed.addBodyPart(bodyPartThree);
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
assertEquals(2, encryptedParts.size()); assertEquals(2, encryptedParts.size());
assertSame(bodyPartOne, encryptedParts.get(0)); assertSame(bodyPartOne, encryptedParts.get(0));
assertSame(bodyPartThree, encryptedParts.get(1)); assertSame(bodyPartThree, encryptedParts.get(1));
} }
//TODO: Find a cleaner way to do this
private void addProtocolParameter(Part part) throws MessagingException {
String contentType = part.getContentType();
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType + "; protocol=\"application/pgp-encrypted\"");
}
} }