Disregard protocol parameter of multipart/encrypted and /signed parts with missing bodies

This commit is contained in:
Vincent Breitmoser 2017-09-13 13:54:54 +02:00
parent 2902657e5b
commit 96bca146c0
2 changed files with 44 additions and 9 deletions

View file

@ -227,8 +227,10 @@ public class MessageCryptoStructureDetector {
}
String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER);
BodyPart signatureBodyPart = mimeMultipart.getBodyPart(1);
return isSameMimeType(protocolParameter, signatureBodyPart.getMimeType());
boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(0).getBody() == null;
boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(1).getMimeType());
return dataUnavailable || protocolMatches;
}
private static boolean isPartMultipartEncrypted(Part part) {
@ -244,8 +246,10 @@ public class MessageCryptoStructureDetector {
}
String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER);
BodyPart signatureBodyPart = mimeMultipart.getBodyPart(0);
return isSameMimeType(protocolParameter, signatureBodyPart.getMimeType());
boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(1).getBody() == null;
boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(0).getMimeType());
return dataUnavailable || protocolMatches;
}
public static boolean isMultipartEncryptedOpenPgpProtocol(Part part) {

View file

@ -182,7 +182,7 @@ public class MessageCryptoStructureDetectorTest {
Message message = messageFromBody(
multipart("encrypted", "application/not-pgp-encrypted",
bodypart("application/pgp-encrypted"),
bodypart("application/octet-stream")
bodypart("application/octet-stream", "content")
)
);
@ -192,7 +192,7 @@ public class MessageCryptoStructureDetectorTest {
}
@Test
public void findEncrypted__withEmptyProtocol__shouldReturnEmpty() throws Exception {
public void findEncrypted__withBadProtocolAndNoBody__shouldReturnRoot() throws Exception {
Message message = messageFromBody(
multipart("encrypted", null,
bodypart("application/pgp-encrypted"),
@ -202,6 +202,21 @@ public class MessageCryptoStructureDetectorTest {
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertEquals(1, encryptedParts.size());
assertSame(message, encryptedParts.get(0));
}
@Test
public void findEncrypted__withEmptyProtocol__shouldReturnEmpty() throws Exception {
Message message = messageFromBody(
multipart("encrypted", null,
bodypart("application/pgp-encrypted"),
bodypart("application/octet-stream", "content")
)
);
List<Part> encryptedParts = MessageCryptoStructureDetector.findMultipartEncryptedParts(message);
assertTrue(encryptedParts.isEmpty());
}
@ -324,10 +339,26 @@ public class MessageCryptoStructureDetectorTest {
}
@Test
public void findSigned__withBadProtocol__shouldReturnRoot() throws Exception {
public void findSigned__withNoProtocolAndNoBody__shouldReturnRoot() throws Exception {
Message message = messageFromBody(
multipart("signed", null,
bodypart("text/plain"),
bodypart("application/pgp-signature")
)
);
List<Part> signedParts = MessageCryptoStructureDetector
.findMultipartSignedParts(message, messageCryptoAnnotations);
assertEquals(1, signedParts.size());
assertSame(message, signedParts.get(0));
}
@Test
public void findSigned__withBadProtocol__shouldReturnNothing() throws Exception {
Message message = messageFromBody(
multipart("signed", "application/not-pgp-signature",
bodypart("text/plain"),
bodypart("text/plain", "content"),
bodypart("application/pgp-signature")
)
);
@ -342,7 +373,7 @@ public class MessageCryptoStructureDetectorTest {
public void findSigned__withEmptyProtocol__shouldReturnRoot() throws Exception {
Message message = messageFromBody(
multipart("signed", null,
bodypart("text/plain"),
bodypart("text/plain", "content"),
bodypart("application/pgp-signature")
)
);