Support S/MIME signed data (untested)

This commit is contained in:
M66B 2020-01-10 19:06:54 +01:00
parent 7f92f5692b
commit f22ec04ef8
3 changed files with 30 additions and 3 deletions

View File

@ -62,6 +62,7 @@ public class EntityAttachment {
static final Integer PGP_CONTENT = 4;
static final Integer SMIME_MESSAGE = 5;
static final Integer SMIME_SIGNATURE = 6;
static final Integer SMIME_SIGNED_DATA = 7;
static final Integer SMIME_CONTENT = 8;
// https://developer.android.com/guide/topics/media/media-formats#image-formats

View File

@ -119,6 +119,7 @@ import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSProcessableFile;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSTypedData;
import org.bouncycastle.cms.CMSVerifierCertificateNotValidException;
import org.bouncycastle.cms.KeyTransRecipientId;
import org.bouncycastle.cms.RecipientInformation;
@ -4668,6 +4669,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (EntityMessage.SMIME_SIGNONLY.equals(type)) {
// Get content/signature
boolean data = false;
File content = null;
File signature = null;
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
@ -4676,13 +4678,18 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (!attachment.available)
throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing));
signature = attachment.getFile(context);
} else if (EntityAttachment.SMIME_SIGNED_DATA.equals(attachment.encryption)) {
if (!attachment.available)
throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing));
data = true;
signature = attachment.getFile(context);
} else if (EntityAttachment.SMIME_CONTENT.equals(attachment.encryption)) {
if (!attachment.available)
throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing));
content = attachment.getFile(context);
}
if (content == null)
if (content == null && !data)
throw new IllegalArgumentException("Signed content missing");
if (signature == null)
throw new IllegalArgumentException("Signature missing");
@ -4690,7 +4697,22 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
// Build signed data
CMSProcessable signedContent = new CMSProcessableFile(content);
FileInputStream fis = new FileInputStream(signature);
CMSSignedData signedData = new CMSSignedData(signedContent, fis);
CMSSignedData signedData;
if (data) {
signedData = new CMSSignedData(fis);
CMSTypedData sc = signedData.getSignedContent();
if (sc == null)
throw new IllegalArgumentException("Signed content missing");
try (OutputStream os = new FileOutputStream(message.getFile(context))) {
sc.write(os);
}
db.message().setMessageEncrypt(message.id, null);
db.message().setMessageStored(message.id, new Date().getTime());
} else
signedData = new CMSSignedData(signedContent, fis);
// Check signature
Store store = signedData.getCertificates();

View File

@ -1230,7 +1230,8 @@ public class MessageHelper {
return EntityMessage.PGP_SIGNONLY;
else if (EntityAttachment.PGP_MESSAGE.equals(apart.attachment.encryption))
return EntityMessage.PGP_SIGNENCRYPT;
else if (EntityAttachment.SMIME_SIGNATURE.equals(apart.attachment.encryption))
else if (EntityAttachment.SMIME_SIGNATURE.equals(apart.attachment.encryption) ||
EntityAttachment.SMIME_SIGNED_DATA.equals(apart.attachment.encryption))
return EntityMessage.SMIME_SIGNONLY;
else if (EntityAttachment.SMIME_MESSAGE.equals(apart.attachment.encryption))
return EntityMessage.SMIME_SIGNENCRYPT;
@ -1476,6 +1477,9 @@ public class MessageHelper {
if ("enveloped-data".equals(smimeType)) {
getMessageParts(imessage, parts, EntityAttachment.SMIME_MESSAGE);
return parts;
} else if ("signed-data".equals(smimeType)) {
getMessageParts(imessage, parts, EntityAttachment.SMIME_SIGNED_DATA);
return parts;
}
}
} catch (ParseException ex) {