From b72486752f53faa99ed32b23b2d0798b0b5cb8b4 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 26 Jul 2024 08:29:18 +0200 Subject: [PATCH] PGP: retry with trim --- .../eu/faircode/email/FragmentMessages.java | 20 ++++++--- .../java/eu/faircode/email/MessageHelper.java | 42 +++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 1889a66676..70f19f668d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -9015,7 +9015,7 @@ public class FragmentMessages extends FragmentBase Intent data = new Intent(); data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); data.putExtra(BuildConfig.APPLICATION_ID, id); - onPgp(data, auto); + onPgp(data, auto, false); } } @@ -9035,7 +9035,7 @@ public class FragmentMessages extends FragmentBase break; case REQUEST_OPENPGP: if (resultCode == RESULT_OK && data != null) - onPgp(data, false); + onPgp(data, false, false); break; case REQUEST_MESSAGE_DELETE: if (resultCode == RESULT_OK && data != null) @@ -9250,16 +9250,18 @@ public class FragmentMessages extends FragmentBase }.execute(this, args, "raw:save"); } - private void onPgp(Intent data, boolean auto) { + private void onPgp(Intent data, boolean auto, boolean stripped) { Bundle args = new Bundle(); args.putParcelable("data", data); args.putBoolean("auto", auto); + args.putBoolean("stripped", stripped); new SimpleTask() { @Override protected PendingIntent onExecute(Context context, Bundle args) throws Throwable { // Get arguments boolean auto = args.getBoolean("auto"); + boolean stripped = args.getBoolean("stripped"); Intent data = args.getParcelable("data"); long id = data.getLongExtra(BuildConfig.APPLICATION_ID, -1); @@ -9348,6 +9350,9 @@ public class FragmentMessages extends FragmentBase else throw new IllegalArgumentException(context.getString(R.string.title_not_encrypted)); + if (stripped) + in = new MessageHelper.StripStream((in)); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean autocrypt = prefs.getBoolean("autocrypt", true); if (autocrypt && @@ -9542,8 +9547,13 @@ public class FragmentMessages extends FragmentBase } else if (sresult == RESULT_KEY_MISSING) args.putString("sigresult", context.getString(R.string.title_signature_key_missing)); else { - String text = context.getString(R.string.title_signature_invalid_reason, Integer.toString(sresult)); - args.putString("sigresult", text); + if (stripped) { + String text = context.getString(R.string.title_signature_invalid_reason, Integer.toString(sresult)); + args.putString("sigresult", text); + } else { + onPgp(data, auto, true); + return null; + } } break; diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index a986e1d7dd..785ddf5060 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -88,6 +88,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; +import java.io.FilterInputStream; import java.io.FilterOutputStream; import java.io.IOException; import java.io.InputStream; @@ -6130,6 +6131,47 @@ public class MessageHelper { } } + static class StripStream extends FilterInputStream { + protected StripStream(InputStream in) { + super(in); + } + + @Override + public int read() throws IOException { + int b = super.read(); + if (b == ' ') { + super.mark(1000); + while (true) { + b = super.read(); + if (b != ' ') { + if (b == '\r' || b == '\n') + return b; + else { + super.reset(); + return ' '; + } + } + } + } else + return b; + } + + @Override + public int read(byte[] b) throws IOException { + for (int i = 0; i < b.length; i++) { + b[i] = (byte) read(); + if (b[i] < 0) + return i; + } + return b.length; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + throw new UnsupportedOperationException(); + } + } + static class CanonicalizingStream extends FilterOutputStream { private OutputStream os; private int content;