+1 vote
by (400 points)

I'am writing an application which works with signed mails (S/MIME). In saved message (as ".eml") i see signature part (filename smime.p7s). I need whole this part as a file or stream (for storing in db). How to achieve it?

Applies to: Rebex Secure Mail

1 Answer

+1 vote
by (143k points)
Best answer

In Rebex Secure Mail, you can use Rebex.Net.MimeMessage object's SignedContentInfo property to access the raw signature data.

C# sample:

     //using Rebex.Mime;
        //using System.IO;

        // use the low-level MimeMessage class for this
        MimeMessage mail = new MimeMessage();

        // load the message

        // if it is a signed message...
        if (mail.Kind == MimeEntityKind.Signed)
            // ...extract the "signed content info" part
            // which contains the signature
            byte[] signature = mail.SignedContentInfo.Encode();

            // and save it
            File.WriteAllBytes("smime.p7s", signature);

The high-level Rebex.Net.MailMessage object can't be used for this - it's purpose is to shield the user from the internals :-)

Please be advised that the S/MIME standard also allows an "enveloped" signature variant where the actual content of the message (including any body parts and attachment) is a part of the signature blob. This variant is quite rare, but depending on the kind of input data you need to process, it might be necessary to take this into account when designing your database.

In Rebex Mail (the "unsecure" variant without S/MIME capabilities), the signature part is accessible as an ordinary attachment.