From 0e4e96ef69527516e2fa9a81e6fcfe0e510caa4f Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 2 Dec 2019 10:03:46 +0100 Subject: [PATCH] S/MIME proof of concept --- app/build.gradle | 4 +- .../eu/faircode/email/AdapterMessage.java | 3 +- .../eu/faircode/email/FragmentMessages.java | 142 +++++++++++++++--- 3 files changed, 124 insertions(+), 25 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 063682e1ea..f27a885592 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -332,6 +332,6 @@ dependencies { // https://github.com/AzureAD/microsoft-authentication-library-for-android implementation "com.microsoft.identity.client:msal:$msal_version" - // https://mvnrepository.com/artifact/org.bouncycastle/bcmail-jdk15on - //implementation "org.bouncycastle:bcmail-jdk15on:1.64" + // https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on + implementation "org.bouncycastle:bcpkix-jdk15on:1.64" } diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 8a1d33e75c..a86bc9ebab 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -1572,7 +1572,8 @@ public class AdapterMessage extends RecyclerView.Adapter attachments = db.attachment().getAttachments(id); + for (EntityAttachment attachment : attachments) + if (EntityAttachment.SMIME_MESSAGE.equals(attachment.encryption)) { + input = attachment.getFile(context); + break; + } + if (input == null) + throw new IllegalArgumentException("Encrypted message missing"); + + FileInputStream fis = new FileInputStream(input); + CMSEnvelopedData envelopedData = new CMSEnvelopedData(fis); + + Collection recipients = envelopedData.getRecipientInfos().getRecipients(); + KeyTransRecipientInformation recipientInfo = (KeyTransRecipientInformation) recipients.iterator().next(); + JceKeyTransRecipient recipient = new JceKeyTransEnvelopedRecipient(pk); + + byte[] result = recipientInfo.getContent(recipient); + File output = EntityMessage.getFile(context, id); + try (OutputStream os = new FileOutputStream(output)) { + os.write("
\r\n".getBytes());
+                    os.write(result);
+                    os.write("
\r\n".getBytes()); + } + + db.message().setMessageStored(id, new Date().getTime()); + + return true; + } + + @Override + protected void onExecuted(Bundle args, Object data) { + int type = args.getInt("type"); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + if (ex instanceof IllegalArgumentException) + Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); + else + Helper.unexpectedError(getParentFragmentManager(), ex); + } + }.execute(this, args, "decrypt:s/mime"); } private void onDelete(long id) {