Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0df44a14 authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

messageview: strip pgp markers for partially downloaded clearsigned messages before view

parent 58daf685
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -195,4 +195,13 @@ public class MessageDecryptVerifier {

        return isPgpEncrypted || isPgpSigned;
    }

    public static boolean isPartPgpInlineEncrypted(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);
    }

}
+28 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import com.fsck.k9.K9;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.BodyPart;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part;
@@ -47,6 +48,7 @@ import org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSink;
import org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import org.openintents.openpgp.util.OpenPgpServiceConnection.OnBound;
import org.openintents.openpgp.util.OpenPgpUtils;


public class MessageCryptoHelper {
@@ -159,6 +161,16 @@ public class MessageCryptoHelper {

    private void addFoundInlinePgpParts(List<Part> foundParts) {
        for (Part part : foundParts) {
            if (!currentMessage.getFlags().contains(Flag.X_DOWNLOADED_FULL)) {
                if (MessageDecryptVerifier.isPartPgpInlineEncrypted(part)) {
                    addErrorAnnotation(part, CryptoError.OPENPGP_ENCRYPTED_BUT_INCOMPLETE, NO_REPLACEMENT_PART);
                } else {
                    MimeBodyPart replacementPart = extractClearsignedTextReplacementPart(part);
                    addErrorAnnotation(part, CryptoError.OPENPGP_SIGNED_BUT_INCOMPLETE, replacementPart);
                }
                continue;
            }

            CryptoPart cryptoPart = new CryptoPart(CryptoPartType.PGP_INLINE, part);
            partsToDecryptOrVerify.add(cryptoPart);
        }
@@ -650,4 +662,20 @@ public class MessageCryptoHelper {
        }
        return replacementPart;
    }

    private static MimeBodyPart extractClearsignedTextReplacementPart(Part part) {
        try {
            String clearsignedText = MessageExtractor.getTextFromPart(part);
            String replacementText = OpenPgpUtils.extractClearsignedMessage(clearsignedText);
            if (replacementText == null) {
                Log.e(K9.LOG_TAG, "failed to extract clearsigned text for replacement part");
                return NO_REPLACEMENT_PART;
            }
            return new MimeBodyPart(new TextBody(replacementText), "text/plain");
        } catch (MessagingException e) {
            Log.e(K9.LOG_TAG, "failed to create clearsigned text replacement part", e);
            return NO_REPLACEMENT_PART;
        }
    }

}
+19 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package org.openintents.openpgp.util;


import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
@@ -32,6 +33,9 @@ public class OpenPgpUtils {
            "(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*",
            Pattern.DOTALL);

    public static final String PGP_MARKER_CLEARSIGN_BEGIN_MESSAGE = "-----BEGIN PGP SIGNED MESSAGE-----";
    public static final String PGP_MARKER_CLEARSIGN_BEGIN_SIGNATURE = "-----BEGIN PGP SIGNATURE-----";

    public static final Pattern PGP_SIGNED_MESSAGE = Pattern.compile(
            "(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*",
            Pattern.DOTALL);
@@ -75,6 +79,21 @@ public class OpenPgpUtils {
        return hexString;
    }

    public static String extractClearsignedMessage(String text) {
        if (!text.startsWith(PGP_MARKER_CLEARSIGN_BEGIN_MESSAGE)) {
            return null;
        }
        int endOfHeader = text.indexOf("\r\n\r\n") +4;
        if (endOfHeader < 0) {
            return null;
        }
        int endOfCleartext = text.indexOf(PGP_MARKER_CLEARSIGN_BEGIN_SIGNATURE);
        if (endOfCleartext < 0) {
            endOfCleartext = text.length();
        }

        return text.substring(endOfHeader, endOfCleartext);
    }

    private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");