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

Commit 4e17fdf1 authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

remove "always sign, encrypt if possible" mode of operation from PgpMessageBuilder

parent 6ae88459
Loading
Loading
Loading
Loading
+0 −15
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ public class ComposeCryptoStatus {

    private CryptoProviderState cryptoProviderState;
    private CryptoMode cryptoMode;
    private boolean cryptoSupportSignOnly;
    private boolean allKeysAvailable;
    private boolean allKeysVerified;
    private boolean hasRecipients;
@@ -100,10 +99,6 @@ public class ComposeCryptoStatus {
        return cryptoMode == CryptoMode.OPPORTUNISTIC;
    }

    public boolean isOpportunisticSignOnly() {
        return cryptoSupportSignOnly;
    }

    public boolean isSignOnly() {
        return cryptoMode == CryptoMode.SIGN_ONLY;
    }
@@ -132,7 +127,6 @@ public class ComposeCryptoStatus {
        private Long selfEncryptKeyId;
        private List<Recipient> recipients;
        private Boolean enablePgpInline;
        private Boolean cryptoSupportSignOnly;

        public ComposeCryptoStatusBuilder setCryptoProviderState(CryptoProviderState cryptoProviderState) {
            this.cryptoProviderState = cryptoProviderState;
@@ -144,11 +138,6 @@ public class ComposeCryptoStatus {
            return this;
        }

        public ComposeCryptoStatusBuilder setCryptoSupportSignOnly(boolean cryptoSupportSignOnly) {
            this.cryptoSupportSignOnly = cryptoSupportSignOnly;
            return this;
        }

        public ComposeCryptoStatusBuilder setSigningKeyId(long signingKeyId) {
            this.signingKeyId = signingKeyId;
            return this;
@@ -182,9 +171,6 @@ public class ComposeCryptoStatus {
            if (enablePgpInline == null) {
                throw new AssertionError("enablePgpInline must be set!");
            }
            if (cryptoSupportSignOnly == null) {
                throw new AssertionError("supportSignOnly must be set!");
            }

            ArrayList<String> recipientAddresses = new ArrayList<>();
            boolean allKeysAvailable = true;
@@ -205,7 +191,6 @@ public class ComposeCryptoStatus {
            ComposeCryptoStatus result = new ComposeCryptoStatus();
            result.cryptoProviderState = cryptoProviderState;
            result.cryptoMode = cryptoMode;
            result.cryptoSupportSignOnly = cryptoSupportSignOnly;
            result.recipientAddresses = recipientAddresses.toArray(new String[0]);
            result.allKeysAvailable = allKeysAvailable;
            result.allKeysVerified = allKeysVerified;
+0 −1
Original line number Diff line number Diff line
@@ -359,7 +359,6 @@ public class RecipientPresenter implements PermissionPingCallback {
            ComposeCryptoStatusBuilder builder = new ComposeCryptoStatusBuilder()
                    .setCryptoProviderState(cryptoProviderState)
                    .setCryptoMode(currentCryptoMode)
                    .setCryptoSupportSignOnly(false) // TODO get rid of this setting
                    .setEnablePgpInline(cryptoEnablePgpInline)
                    .setRecipients(getAllRecipients());

+7 −20
Original line number Diff line number Diff line
@@ -46,8 +46,6 @@ public class PgpMessageBuilder extends MessageBuilder {

    private MimeMessage currentProcessedMimeMessage;
    private ComposeCryptoStatus cryptoStatus;
    private boolean opportunisticSkipEncryption;
    private boolean opportunisticSecondPass;


    public static PgpMessageBuilder newInstance() {
@@ -104,7 +102,7 @@ public class PgpMessageBuilder extends MessageBuilder {
    private void startOrContinueBuildMessage(@Nullable Intent pgpApiIntent) {
        try {
            boolean shouldSign = cryptoStatus.isSigningEnabled();
            boolean shouldEncrypt = cryptoStatus.isEncryptionEnabled() && !opportunisticSkipEncryption;
            boolean shouldEncrypt = cryptoStatus.isEncryptionEnabled();
            boolean isPgpInlineMode = cryptoStatus.isPgpInlineModeEnabled();

            if (!shouldSign && !shouldEncrypt) {
@@ -128,13 +126,6 @@ public class PgpMessageBuilder extends MessageBuilder {
                return;
            }

            boolean shouldSignOnly = cryptoStatus.isOpportunisticSignOnly();
            if (opportunisticSkipEncryption && shouldSignOnly && !opportunisticSecondPass) {
                opportunisticSecondPass = true;
                startOrContinueBuildMessage(null);
                return;
            }

            queueMessageBuildSuccess(currentProcessedMimeMessage);
        } catch (MessagingException me) {
            queueMessageBuildException(me);
@@ -224,7 +215,11 @@ public class PgpMessageBuilder extends MessageBuilder {
                }
                boolean isOpportunisticError = error.getErrorId() == OpenPgpError.OPPORTUNISTIC_MISSING_KEYS;
                if (isOpportunisticError) {
                    skipEncryptingMessage();
                    if (!cryptoStatus.isEncryptionOpportunistic()) {
                        throw new IllegalStateException(
                                "Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
                    }
                    Log.d(K9.LOG_TAG, "Skipping encryption due to opportunistic mode");
                    return null;
                }
                throw new MessagingException(error.getMessage());
@@ -259,8 +254,7 @@ public class PgpMessageBuilder extends MessageBuilder {
            @NonNull Intent result, @NonNull MimeBodyPart bodyPart, @Nullable BinaryTempFileBody pgpResultTempBody)
            throws MessagingException {
        if (pgpResultTempBody == null) {
            boolean shouldHaveResultPart = cryptoStatus.isPgpInlineModeEnabled() ||
                    (cryptoStatus.isEncryptionEnabled() && !opportunisticSkipEncryption);
            boolean shouldHaveResultPart = cryptoStatus.isPgpInlineModeEnabled() || cryptoStatus.isEncryptionEnabled();
            if (shouldHaveResultPart) {
                throw new AssertionError("encryption or pgp/inline is enabled, but no output part!");
            }
@@ -335,13 +329,6 @@ public class PgpMessageBuilder extends MessageBuilder {
        MimeMessageHelper.setBody(currentProcessedMimeMessage, inlineBodyPart);
    }

    private void skipEncryptingMessage() throws MessagingException {
        if (!cryptoStatus.isEncryptionOpportunistic()) {
            throw new AssertionError("Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
        }
        opportunisticSkipEncryption = true;
    }

    public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) {
        this.cryptoStatus = cryptoStatus;
    }
+1 −37
Original line number Diff line number Diff line
@@ -412,7 +412,6 @@ public class PgpMessageBuilderTest {
        ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder
                .setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key")))
                .setCryptoMode(CryptoMode.OPPORTUNISTIC)
                .setCryptoSupportSignOnly(false)
                .build();
        pgpMessageBuilder.setCryptoStatus(cryptoStatus);

@@ -438,40 +437,6 @@ public class PgpMessageBuilderTest {
        Assert.assertEquals("text/plain", message.getMimeType());
    }

    @Test
    public void buildOpportunisticEncrypt__withNoKeysAndSignOnly__shouldBeSigned() throws MessagingException {
        ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder
                .setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key")))
                .setCryptoMode(CryptoMode.OPPORTUNISTIC)
                .setCryptoSupportSignOnly(true)
                .build();
        pgpMessageBuilder.setCryptoStatus(cryptoStatus);

        Intent returnIntentOpportunisticError = new Intent();
        returnIntentOpportunisticError.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
        returnIntentOpportunisticError.putExtra(OpenPgpApi.RESULT_ERROR,
                new OpenPgpError(OpenPgpError.OPPORTUNISTIC_MISSING_KEYS, "Missing keys"));

        Intent returnIntentSigned = new Intent();
        returnIntentSigned.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
        returnIntentSigned.putExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE, new byte[] { (byte) 159 });


        when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class)))
                .thenReturn(returnIntentOpportunisticError, returnIntentSigned);

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


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

        MimeMessage message = captor.getValue();
        Assert.assertEquals("multipart/signed", message.getMimeType());
    }

    @Test
    public void buildSign__withNoDetachedSignatureExtra__shouldFail() throws MessagingException {
        ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder
@@ -500,8 +465,7 @@ public class PgpMessageBuilderTest {
                .setSigningKeyId(TEST_SIGN_KEY_ID)
                .setSelfEncryptId(TEST_SELF_ENCRYPT_KEY_ID)
                .setRecipients(new ArrayList<Recipient>())
                .setCryptoProviderState(CryptoProviderState.OK)
                .setCryptoSupportSignOnly(true);
                .setCryptoProviderState(CryptoProviderState.OK);
    }

    private static PgpMessageBuilder createDefaultPgpMessageBuilder(OpenPgpApi openPgpApi) {