From 7d21a147b1adffa29f09076e7bf0b9d76725677e Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 28 Oct 2018 14:29:45 +0000 Subject: [PATCH] Sign and encrypt --- .../java/eu/faircode/email/ActivityView.java | 12 --- .../eu/faircode/email/FragmentCompose.java | 78 +++++++++++++------ .../java/eu/faircode/email/MessageHelper.java | 23 ++++++ 3 files changed, 77 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index e8f92777..0ce0ff4f 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -61,7 +61,6 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; -import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.text.Collator; @@ -956,11 +955,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB api.executeApiAsync(data, msg, decrypted, new OpenPgpApi.IOpenPgpCallback() { @Override public void onReturn(Intent result) { - Log.i(Helper.TAG, "Pgp result=" + result); - Bundle extras = result.getExtras(); - for (String key : extras.keySet()) - Log.i(Helper.TAG, key + "=" + extras.get(key)); - try { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { case OpenPgpApi.RESULT_CODE_SUCCESS: @@ -1007,12 +1001,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); else Helper.unexpectedError(ActivityView.this, ex); - } finally { - try { - msg.close(); - } catch (IOException ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } } } }); diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index efdb6620..c2a0841e 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -495,7 +495,7 @@ public class FragmentCompose extends FragmentEx { tos[i] = ato[i].getAddress(); Intent data = new Intent(); - data.setAction(OpenPgpApi.ACTION_ENCRYPT); + data.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); data.putExtra(OpenPgpApi.EXTRA_USER_IDS, tos); data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); @@ -508,7 +508,7 @@ public class FragmentCompose extends FragmentEx { } } - private void encrypt(Intent data) throws IOException { + private void encrypt(final Intent data) throws IOException { final OpenPgpApi api = new OpenPgpApi(getContext(), pgpService.getService()); final FileInputStream msg = new FileInputStream(EntityMessage.getFile(getContext(), working)); final ByteArrayOutputStream encrypted = new ByteArrayOutputStream(); @@ -531,7 +531,20 @@ public class FragmentCompose extends FragmentEx { @Override protected Void onLoad(Context context, Bundle args) throws Throwable { long id = args.getLong("id"); - EntityAttachment attachment = new EntityAttachment(); + EntityAttachment attachment1 = new EntityAttachment(); + EntityAttachment attachment2 = new EntityAttachment(); + + Intent keyRequest = new Intent(); + keyRequest.setAction(OpenPgpApi.ACTION_DETACHED_SIGN); + keyRequest.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1)); + Intent keyData = api.executeApi(keyRequest, msg, null); + int r = keyData.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR); + if (r != OpenPgpApi.RESULT_CODE_SUCCESS) { + OpenPgpError error = keyData.getParcelableExtra(OpenPgpApi.RESULT_ERROR); + throw new IllegalArgumentException(error.getMessage()); + } + + byte[] signature = keyData.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE); DB db = DB.getInstance(context); try { @@ -539,27 +552,49 @@ public class FragmentCompose extends FragmentEx { int seq = db.attachment().getAttachmentCount(id); - attachment.message = id; - attachment.sequence = seq + 1; - attachment.name = "encrypted.asc"; - attachment.type = "application/octet-stream"; - attachment.id = db.attachment().insertAttachment(attachment); + attachment1.message = id; + attachment1.sequence = seq + 1; + attachment1.name = "encrypted.asc"; + attachment1.type = "application/octet-stream"; + attachment1.id = db.attachment().insertAttachment(attachment1); - File file = EntityAttachment.getFile(context, attachment.id); + File file1 = EntityAttachment.getFile(context, attachment1.id); - OutputStream os = null; + OutputStream os1 = null; try { - os = new BufferedOutputStream(new FileOutputStream(file)); + os1 = new BufferedOutputStream(new FileOutputStream(file1)); byte[] data = encrypted.toByteArray(); - os.write(data); + os1.write(data); + + attachment1.size = data.length; + attachment1.progress = null; + attachment1.available = true; + db.attachment().updateAttachment(attachment1); + } finally { + if (os1 != null) + os1.close(); + } + + attachment2.message = id; + attachment2.sequence = seq + 2; + attachment2.name = "signature.asc"; + attachment2.type = "application/octet-stream"; + attachment2.id = db.attachment().insertAttachment(attachment2); - attachment.size = data.length; - attachment.progress = null; - attachment.available = true; - db.attachment().updateAttachment(attachment); + File file2 = EntityAttachment.getFile(context, attachment2.id); + + OutputStream os2 = null; + try { + os2 = new BufferedOutputStream(new FileOutputStream(file2)); + os2.write(signature); + + attachment2.size = signature.length; + attachment2.progress = null; + attachment2.available = true; + db.attachment().updateAttachment(attachment2); } finally { - if (os != null) - os.close(); + if (os2 != null) + os2.close(); } db.setTransactionSuccessful(); @@ -595,12 +630,6 @@ public class FragmentCompose extends FragmentEx { Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); else Helper.unexpectedError(getContext(), ex); - } finally { - try { - msg.close(); - } catch (IOException ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } } } }); @@ -619,6 +648,7 @@ public class FragmentCompose extends FragmentEx { } else if (requestCode == ActivityCompose.REQUEST_ENCRYPT) { if (data != null) try { + data.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); encrypt(data); } catch (Throwable ex) { if (ex instanceof IllegalArgumentException) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 67e4644b..fe49ad73 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -27,9 +27,11 @@ import android.webkit.MimeTypeMap; import org.jsoup.Jsoup; +import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -197,6 +199,27 @@ public class MessageHelper { imessage.setSentDate(new Date()); + if (message.from != null && message.from.length > 0) + for (EntityAttachment attachment : attachments) + if (attachment.available && "signature.asc".equals(attachment.name)) { + InternetAddress from = (InternetAddress) message.from[0]; + File file = EntityAttachment.getFile(context, attachment.id); + BufferedReader br = null; + StringBuilder sb = new StringBuilder(); + try { + br = new BufferedReader(new FileReader(file)); + String line; + while ((line = br.readLine()) != null) + if (!line.startsWith("-----") && !line.endsWith("-----")) + sb.append(line); + } finally { + if (br != null) + br.close(); + } + + imessage.addHeader("Autocrypt", "addr=" + from.getAddress() + "; keydata=" + sb.toString()); + } + for (final EntityAttachment attachment : attachments) if (attachment.available && "encrypted.asc".equals(attachment.name)) { Multipart multipart = new MimeMultipart("encrypted; protocol=\"application/pgp-encrypted\"");