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
k9mail/src
main/java/com/fsck/k9/crypto
test/java/com/fsck/k9/crypto

View file

@ -13,6 +13,7 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MessageExtractor;
import com.fsck.k9.mail.internet.MimeUtility;
import org.openintents.openpgp.util.OpenPgpUtils;
import static com.fsck.k9.mail.internet.MimeUtility.isSameMimeType;
@ -34,10 +35,9 @@ public class MessageDecryptVerifier {
while (!partsToCheck.isEmpty()) {
Part part = partsToCheck.pop();
String mimeType = part.getMimeType();
Body body = part.getBody();
if (isSameMimeType(mimeType, MULTIPART_ENCRYPTED)) {
if (isPgpMimeEncryptedPart(part)) {
encryptedParts.add(part);
} else if (body instanceof Multipart) {
Multipart multipart = (Multipart) body;
@ -58,10 +58,9 @@ public class MessageDecryptVerifier {
while (!partsToCheck.isEmpty()) {
Part part = partsToCheck.pop();
String mimeType = part.getMimeType();
Body body = part.getBody();
if (isSameMimeType(mimeType, MULTIPART_SIGNED)) {
if (isPgpMimeSignedPart(part)) {
signedParts.add(part);
} else if (body instanceof Multipart) {
Multipart multipart = (Multipart) body;
@ -105,8 +104,7 @@ public class MessageDecryptVerifier {
}
public static byte[] getSignatureData(Part part) throws IOException, MessagingException {
if (isSameMimeType(part.getMimeType(), MULTIPART_SIGNED)) {
if (isPgpMimeSignedPart(part)) {
Body body = part.getBody();
if (body instanceof Multipart) {
Multipart multi = (Multipart) body;
@ -122,15 +120,23 @@ public class MessageDecryptVerifier {
return null;
}
public static boolean isPgpMimeSignedPart(Part part) {
return isSameMimeType(part.getMimeType(), MULTIPART_SIGNED);
private static boolean isPgpMimeSignedPart(Part part) {
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) {
//FIXME: Doesn't work right now because LocalMessage.getContentType() doesn't load headers from database
// String contentType = part.getContentType();
// String protocol = MimeUtility.getHeaderParameter(contentType, PROTOCOL_PARAMETER);
// return APPLICATION_PGP_ENCRYPTED.equals(protocol);
return isSameMimeType(part.getMimeType(), MULTIPART_ENCRYPTED);
private static boolean isPgpMimeEncryptedPart(Part part) {
if (!isSameMimeType(part.getMimeType(), MULTIPART_ENCRYPTED)) {
return false;
}
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 com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part;
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.MimeMessageHelper;
import com.fsck.k9.mail.internet.MimeMultipart;
@ -27,6 +29,7 @@ public class MessageDecryptVerifierTest {
MimeMessage emptyMessage = new MimeMessage();
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(emptyMessage);
assertEquals(0, encryptedParts.size());
}
@ -36,17 +39,20 @@ public class MessageDecryptVerifierTest {
message.setBody(new TextBody("message text"));
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
assertEquals(0, encryptedParts.size());
}
@Test
public void findEncryptedPartsShouldReturnEmptyEncryptedPart() throws Exception {
MimeMessage message = new MimeMessage();
MimeMultipart mulitpartEncrypted = new MimeMultipart();
mulitpartEncrypted.setSubType("encrypted");
MimeMessageHelper.setBody(message, mulitpartEncrypted);
MimeMultipart multipartEncrypted = new MimeMultipart();
multipartEncrypted.setSubType("encrypted");
MimeMessageHelper.setBody(message, multipartEncrypted);
addProtocolParameter(message);
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
assertEquals(1, encryptedParts.size());
assertSame(message, encryptedParts.get(0));
}
@ -58,22 +64,31 @@ public class MessageDecryptVerifierTest {
multipartMixed.setSubType("mixed");
MimeMessageHelper.setBody(message, multipartMixed);
MimeMultipart mulitpartEncryptedOne = new MimeMultipart();
mulitpartEncryptedOne.setSubType("encrypted");
MimeBodyPart bodyPartOne = new MimeBodyPart(mulitpartEncryptedOne);
MimeMultipart multipartEncryptedOne = new MimeMultipart();
multipartEncryptedOne.setSubType("encrypted");
MimeBodyPart bodyPartOne = new MimeBodyPart(multipartEncryptedOne);
addProtocolParameter(bodyPartOne);
multipartMixed.addBodyPart(bodyPartOne);
MimeBodyPart bodyPartTwo = new MimeBodyPart(null, "text/plain");
multipartMixed.addBodyPart(bodyPartTwo);
MimeMultipart mulitpartEncryptedThree = new MimeMultipart();
mulitpartEncryptedThree.setSubType("encrypted");
MimeBodyPart bodyPartThree = new MimeBodyPart(mulitpartEncryptedThree);
MimeMultipart multipartEncryptedThree = new MimeMultipart();
multipartEncryptedThree.setSubType("encrypted");
MimeBodyPart bodyPartThree = new MimeBodyPart(multipartEncryptedThree);
addProtocolParameter(bodyPartThree);
multipartMixed.addBodyPart(bodyPartThree);
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
assertEquals(2, encryptedParts.size());
assertSame(bodyPartOne, encryptedParts.get(0));
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\"");
}
}