0

I am writing an application that must be able to read signed and encrypted emails and parse through their contents. I am able to get everything working fine for emails that are only encrypted, but do not know what to do when I get an email that is also signed. Once I decrypt this email instead of having an anticipated 4 parts in a Multipart object, I have only 1 part in a MimePart object with the file name smime.p7m. I do not know how to break this file up or verify the signature. I have found the documentation on verifying a signature (http://www.mimekit.net/docs/html/Working-With-SMime.htm#Verify), but I don't see how this does anything. Obviously there is something that I am just not understanding at this point.

Below is a sample of the code that I am using. Note that this will be refactored after I get everything figured out, but this code is thus far working fine for all emails that I have tested so far that are not signed (may or may not be encrypted).

public void decryptAndSendEmails()
{
    List<EmailMessage> emails = getEmails();
    foreach (var email in emails)
    {
        var decryptedEmailMessage = new EmailMessage(service);
        MimeMessage message;

        using (var stream = new MemoryStream(email.MimeContent.Content, false))
        {
            message = MimeMessage.Load(stream);
        }

        var pkcs7 = message.BodyParts.OfType<ApplicationPkcs7Mime>().FirstOrDefault();

        if (pkcs7 != null)
        {
            //If the SecureMimeType has not been set as it should, set it to EnvelopedData
            if (pkcs7.SecureMimeType == SecureMimeType.Unknown)
            {
                var content = new MemoryStream();
                pkcs7.Content.DecodeTo(content);
                content.Position = 0;

                pkcs7 = new ApplicationPkcs7Mime(SecureMimeType.EnvelopedData, content);
            }

            using (var ctx = new TemporarySecureMimeContext())
            {
                using (var stream = File.OpenRead(ConfigurationManager.AppSettings["certLocation"]))
                {
                    ctx.Import(stream, ConfigurationManager.AppSettings["certPassword"]);
                }

                var decrypted = pkcs7.Decrypt(ctx);
                var decryptedParts = new List<MimePart>();
                if (decrypted is Multipart)
                {
                    decryptedParts = breakMultiPart((Multipart)decrypted);
                }
                else if (decrypted is MimePart)
                {
                    decryptedParts.Add((MimePart)decrypted);
                }
                else
                {
                    throw new InvalidOperationException("Unknown Mime part found");
                }

                var textParts = decryptedParts.Where(r => r is TextPart);
                var htmlParts = textParts.Where(x => ((TextPart)x).IsHtml);
                var textBodyParts = textParts.Where(x => !((TextPart)x).IsHtml);
                var attachmentParts = decryptedParts.Where(r => !(r is TextPart));

                if (htmlParts.Any())
                {
                    if (htmlParts.Count() > 1)
                    {
                        throw new InvalidOperationException("multiple html body parts.");
                    }
                    var htmlPart = (TextPart)htmlParts.First();
                    decryptedEmailMessage.Body = new MessageBody(BodyType.HTML, htmlPart.Text);
                }
                else
                {
                    //Text body
                    if (textBodyParts.Count() > 1)
                    {
                        throw new InvalidOperationException("multiple text body parts.");
                    }
                    var textPart = (TextPart)textBodyParts.First();
                    decryptedEmailMessage.Body = new MessageBody(BodyType.Text, textPart.Text);
                }

                foreach (var part in attachmentParts)
                {
                    var content = new MemoryStream();
                    part.Content.DecodeTo(content);
                    content.Position = 0;

                        decryptedEmailMessage.Attachments.AddFileAttachment(part.FileName, content);

                    if (!part.IsAttachment)
                    {
                        decryptedEmailMessage.Attachments.First(r => r.Name == part.FileName).IsInline = true;
                        decryptedEmailMessage.Attachments.First(r => r.Name == part.FileName).ContentId = part.ContentId;
                    }
                }
            }
            ////do stuff with decrypted Email
        }

        else
        {
            //The email is not encrypted
            decryptedEmailMessage = email;
            //do stuff with decrypted Email
        }
    }
}
dmoore1181
  • 1,793
  • 1
  • 25
  • 57
  • 1
    FWIW, MimeKit 2.1.2 (released a few days ago) fixes the whole SecureMimeType.Unknown thing, so that work-around is not needed anymore. That said, what is the value of `decrypted.ContentType.MimeType`? Does `decrypted.GetType()` return `MimeKit.Cryptography.ApplicationPkcs7Mime`? I would imagine that the decrypted part is another `application/pkcs7-mime` part that you should call `Verify()` on, but that's just a guess. – jstedfast Jan 04 '19 at 13:45

1 Answers1

0

I have finally figured this out using a combination of the comment from @jstedfast and the information I found in Unable to decrypt p7m using MimeKit. The following is the resulting code to fix this issue:

public void decryptAndSendEmails()
{
List<EmailMessage> emails = getEmails();

foreach (var email in emails)
{
    var decryptedEmailMessage = new EmailMessage(service);
    MimeMessage message;

    using (var stream = new MemoryStream(email.MimeContent.Content, false))
    {
        message = MimeMessage.Load(stream);
    }

    var pkcs7 = message.BodyParts.OfType<ApplicationPkcs7Mime>().FirstOrDefault();

    if (pkcs7 != null)
    {
        using (var ctx = new TemporarySecureMimeContext())
        {
            using (var stream = File.OpenRead(ConfigurationManager.AppSettings["certLocation"]))
            {
                ctx.Import(stream, ConfigurationManager.AppSettings["certPassword"]);
            }


            var decrypted = pkcs7.Decrypt(ctx);

            if (decrypted != null && decrypted is MimePart && ((MimePart)decrypted).FileName == "smime.p7m")
            {
                //We need to verify the signature
                var signedDecryptedEntity = decrypted as ApplicationPkcs7Mime;
                signedDecryptedEntity.Verify(ctx, out decrypted); //the real decrypted data
            }

            var decryptedParts = new List<MimePart>();
            if (decrypted is Multipart)
            {
                decryptedParts = breakMultiPart((Multipart)decrypted);
            }
            else if (decrypted is MimePart)
            {
                decryptedParts.Add((MimePart)decrypted);
            }
            else
            {
                throw new InvalidOperationException("Unknown Mime part found");
            }

            var textParts = decryptedParts.Where(r => r is TextPart);
            var htmlParts = textParts.Where(x => ((TextPart)x).IsHtml);
            var textBodyParts = textParts.Where(x => !((TextPart)x).IsHtml);
            var attachmentParts = decryptedParts.Where(r => !(r is TextPart));

            if (htmlParts.Any())
            {
                if (htmlParts.Count() > 1)
                {
                    throw new InvalidOperationException("multiple html body parts.");
                }
                var htmlPart = (TextPart)htmlParts.First();
                decryptedEmailMessage.Body = new MessageBody(BodyType.HTML, htmlPart.Text);
            }
            else
            {
                //Text body
                if (textBodyParts.Count() > 1)
                {
                    throw new InvalidOperationException("multiple text body parts.");
                }
                var textPart = (TextPart)textBodyParts.First();
                decryptedEmailMessage.Body = new MessageBody(BodyType.Text, textPart.Text);
            }

            foreach (var part in attachmentParts)
            {
                var content = new MemoryStream();
                part.Content.DecodeTo(content);
                content.Position = 0;

                decryptedEmailMessage.Attachments.AddFileAttachment(part.FileName, content);

                if (!part.IsAttachment)
                {
                    decryptedEmailMessage.Attachments.First(r => r.Name == part.FileName).IsInline = true;
                    decryptedEmailMessage.Attachments.First(r => r.Name == part.FileName).ContentId = part.ContentId;
                }
            }
        }
        //Do Something with email (decryptedEmailMessage)
    }

    else
    {
        //The email is not encrypted
        decryptedEmailMessage = email;
        //Do Something with email (decryptedEmailMessage)
    }

}

}

dmoore1181
  • 1,793
  • 1
  • 25
  • 57