1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2025-01-01 04:35:57 +00:00

Decode S/MIME signed data

This commit is contained in:
M66B 2020-01-10 22:12:19 +01:00
parent 05417a1fd0
commit ad2a7a9fa4

View file

@ -4667,9 +4667,13 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (message == null) if (message == null)
return null; return null;
InputStream is = null;
X509Certificate result = null;
String alias = args.getString("alias");
if (EntityMessage.SMIME_SIGNONLY.equals(type)) { if (EntityMessage.SMIME_SIGNONLY.equals(type)) {
// Get content/signature // Get content/signature
boolean data = false; boolean sdata = false;
File content = null; File content = null;
File signature = null; File signature = null;
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id); List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
@ -4681,7 +4685,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
} else if (EntityAttachment.SMIME_SIGNED_DATA.equals(attachment.encryption)) { } else if (EntityAttachment.SMIME_SIGNED_DATA.equals(attachment.encryption)) {
if (!attachment.available) if (!attachment.available)
throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing)); throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing));
data = true; sdata = true;
signature = attachment.getFile(context); signature = attachment.getFile(context);
} else if (EntityAttachment.SMIME_CONTENT.equals(attachment.encryption)) { } else if (EntityAttachment.SMIME_CONTENT.equals(attachment.encryption)) {
if (!attachment.available) if (!attachment.available)
@ -4689,35 +4693,31 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
content = attachment.getFile(context); content = attachment.getFile(context);
} }
if (content == null && !data) if (content == null && !sdata)
throw new IllegalArgumentException("Signed content missing"); throw new IllegalArgumentException("Signed content missing");
if (signature == null) if (signature == null)
throw new IllegalArgumentException("Signature missing"); throw new IllegalArgumentException("Signature missing");
// Build signed data // Build signed data
CMSProcessable signedContent = new CMSProcessableFile(content);
FileInputStream fis = new FileInputStream(signature); FileInputStream fis = new FileInputStream(signature);
CMSSignedData signedData; CMSSignedData signedData;
if (data) { if (sdata) {
signedData = new CMSSignedData(fis); signedData = new CMSSignedData(fis);
CMSTypedData sc = signedData.getSignedContent(); CMSTypedData sc = signedData.getSignedContent();
if (sc == null) if (sc == null)
throw new IllegalArgumentException("Signed content missing"); throw new IllegalArgumentException("Signed content missing");
try (OutputStream os = new FileOutputStream(message.getFile(context))) { is = new ByteArrayInputStream((byte[]) sc.getContent());
sc.write(os); } else {
} CMSProcessable signedContent = new CMSProcessableFile(content);
db.message().setMessageEncrypt(message.id, null);
db.message().setMessageStored(message.id, new Date().getTime());
} else
signedData = new CMSSignedData(signedContent, fis); signedData = new CMSSignedData(signedContent, fis);
}
// Check signature // Check signature
Store store = signedData.getCertificates(); Store store = signedData.getCertificates();
SignerInformationStore signerInfos = signedData.getSignerInfos(); SignerInformationStore signerInfos = signedData.getSignerInfos();
for (SignerInformation signer : signerInfos.getSigners()) for (SignerInformation signer : signerInfos.getSigners()) {
for (Object match : store.getMatches(signer.getSID())) { for (Object match : store.getMatches(signer.getSID())) {
X509CertificateHolder certHolder = (X509CertificateHolder) match; X509CertificateHolder certHolder = (X509CertificateHolder) match;
X509Certificate cert = new JcaX509CertificateConverter() X509Certificate cert = new JcaX509CertificateConverter()
@ -4740,17 +4740,18 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
args.putString("sender", sender); args.putString("sender", sender);
args.putBoolean("known", known); args.putBoolean("known", known);
return cert; result = cert;
break;
} }
} catch (CMSVerifierCertificateNotValidException ex) { } catch (CMSVerifierCertificateNotValidException ex) {
Log.w(ex); Log.w(ex);
} }
} }
if (result != null)
return null; break;
}
} else { } else {
// Get alias // Check alias
String alias = args.getString("alias");
if (alias == null) if (alias == null)
throw new IllegalArgumentException("Key alias missing"); throw new IllegalArgumentException("Key alias missing");
@ -4789,7 +4790,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
Collection<RecipientInformation> recipients = envelopedData.getRecipientInfos().getRecipients(); // KeyTransRecipientInformation Collection<RecipientInformation> recipients = envelopedData.getRecipientInfos().getRecipients(); // KeyTransRecipientInformation
// Find recipient // Find recipient
InputStream is = null; is = null;
if (chain[0].getSerialNumber() != null) if (chain[0].getSerialNumber() != null)
for (RecipientInformation recipientInfo : recipients) { for (RecipientInformation recipientInfo : recipients) {
KeyTransRecipientId recipientId = (KeyTransRecipientId) recipientInfo.getRID(); KeyTransRecipientId recipientId = (KeyTransRecipientId) recipientInfo.getRID();
@ -4815,7 +4816,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
db.identity().setIdentitySignKeyAlias(message.identity, null); db.identity().setIdentitySignKeyAlias(message.identity, null);
throw new IllegalArgumentException(context.getString(R.string.title_invalid_key)); throw new IllegalArgumentException(context.getString(R.string.title_invalid_key));
} }
}
if (is != null) {
// Decode message // Decode message
Properties props = MessageHelper.getSessionProperties(); Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null); Session isession = Session.getInstance(props, null);
@ -4852,16 +4855,16 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
db.message().setMessageEncrypt(message.id, parts.getEncryption()); db.message().setMessageEncrypt(message.id, parts.getEncryption());
db.message().setMessageStored(message.id, new Date().getTime()); db.message().setMessageStored(message.id, new Date().getTime());
if (message.identity != null) if (alias != null && message.identity != null)
db.identity().setIdentitySignKeyAlias(message.identity, alias); db.identity().setIdentitySignKeyAlias(message.identity, alias);
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {
db.endTransaction(); db.endTransaction();
} }
return null;
} }
return result;
} }
@Override @Override