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

Commit ef3cda97 authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

remove support for clearsigned messages in PgpMessageBuilder

parent 0378f1a8
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -39,8 +39,8 @@ import org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource;


public class PgpMessageBuilder extends MessageBuilder {
    private static final int REQUEST_USER_INTERACTION = 1;

    public static final int REQUEST_USER_INTERACTION = 1;

    private OpenPgpApi openPgpApi;

@@ -115,12 +115,16 @@ public class PgpMessageBuilder extends MessageBuilder {
                throw new MessagingException("Attachments are not supported in PGP/INLINE format!");
            }

            if (isPgpInlineMode && shouldSign && !shouldEncrypt) {
                throw new UnsupportedOperationException("Clearsigning is not supported!");
            }

            if (pgpApiIntent == null) {
                pgpApiIntent = buildOpenPgpApiIntent(shouldSign, shouldEncrypt, isPgpInlineMode);
            }

            PendingIntent returnedPendingIntent = launchOpenPgpApiIntent(
                    pgpApiIntent, shouldEncrypt || isPgpInlineMode, shouldEncrypt || !isPgpInlineMode, isPgpInlineMode);
                    pgpApiIntent, shouldEncrypt, isPgpInlineMode);
            if (returnedPendingIntent != null) {
                queueMessageBuildPendingIntent(returnedPendingIntent, REQUEST_USER_INTERACTION);
                return;
@@ -170,7 +174,7 @@ public class PgpMessageBuilder extends MessageBuilder {
    }

    private PendingIntent launchOpenPgpApiIntent(@NonNull Intent openPgpIntent,
            boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
            boolean captureOutputPart, boolean writeBodyContentOnly) throws MessagingException {
        final MimeBodyPart bodyPart = currentProcessedMimeMessage.toBodyPart();
        String[] contentType = currentProcessedMimeMessage.getHeader(MimeHeader.HEADER_CONTENT_TYPE);
        if (contentType.length > 0) {
@@ -183,8 +187,7 @@ public class PgpMessageBuilder extends MessageBuilder {
        OutputStream outputStream = null;
        if (captureOutputPart) {
            try {
                pgpResultTempBody = new BinaryTempFileBody(
                        capturedOutputPartIs7Bit ? MimeUtil.ENC_7BIT : MimeUtil.ENC_8BIT);
                pgpResultTempBody = new BinaryTempFileBody(MimeUtil.ENC_7BIT);
                outputStream = pgpResultTempBody.getOutputStream();
                // OpenKeychain/BouncyCastle at this point use the system newline for formatting, which is LF on android.
                // we need this to be CRLF, so we convert the data after receiving.
+2 −22
Original line number Diff line number Diff line
@@ -343,8 +343,8 @@ public class PgpMessageBuilderTest {
        Assert.assertEquals(MimeUtil.ENC_7BIT, ((BinaryTempFileBody) message.getBody()).getEncoding());
    }

    @Test
    public void buildSign__withInlineEnabled__shouldSucceed() throws MessagingException {
    @Test(expected = RuntimeException.class) // this is a wrapped UnsupportedOperationException from AsyncTask
    public void buildSign__withInlineEnabled__shouldFail() throws MessagingException {
        ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder
                .setCryptoMode(CryptoMode.SIGN_ONLY)
                .setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key")))
@@ -352,28 +352,8 @@ public class PgpMessageBuilderTest {
                .build();
        pgpMessageBuilder.setCryptoStatus(cryptoStatus);

        ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class);

        Intent returnIntent = new Intent();
        returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);

        when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class)))
                .thenReturn(returnIntent);

        Callback mockCallback = mock(Callback.class);
        pgpMessageBuilder.buildAsync(mockCallback);

        Intent expectedApiIntent = new Intent(OpenPgpApi.ACTION_SIGN);
        expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_SIGN_KEY_ID);
        expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
        assertIntentEqualsActionAndExtras(expectedApiIntent, capturedApiIntent.getValue());

        ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class);
        verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false));
        verifyNoMoreInteractions(mockCallback);

        MimeMessage message = captor.getValue();
        Assert.assertEquals("message must be text/plain", "text/plain", message.getMimeType());
    }

    @Test