none
[E2010] [TA] [C#] [N/A]: How to I get the (signed) content from an embedded clear signed message? RRS feed

  • Question

  • Short version...

    What's the equivalent to using MimeReader's EnableReadingUnparsedHeaders & GetRawContentReadStream methods for a MimePart?

    Long version...

    I'm working on a Transport Agent that needs to validate the signatures on messages (and those on embedded messages too).

    The code I have fails to validate the signature on a clear signed message embedded in another clear signed message.

    Other message constructs (such as a clear signed message embedded in a plain message) are fine as the underlying MIME structure of the message is fundamentally different, and processing goes through a different code path where it's possible to make use of the MimeReader EnableReadingUnparsedHeaders & GetRawContentReadStream methods (more on this soon).

    In the problematic case the outer clear signed message is successfully decoded to create a temporary EmailMessage object that is a plain message with the embedded clear signed message directly accessible from the EmailMessage.Attachments collection as an EmbeddedMessage. When the embedded clear signed message is subsequently processed, unlike the outer message (which had a smime.p7m attachment), this has no attachments but its RootPart is "multipart/signed" and the signature and content are separable by enumerating the MimeParts of the RootPart.

    Here's the crux of the problem - the clear content that's obtained by this code path is missing the MIME header section for the multipart/signed, whereas the clear content obtained via the other code path (that uses EnableReadingUnparsedHeaders & GetRawContentReadStream) has it, and reading the Wikipedia definition for multipart/signed (http://en.wikipedia.org/wiki/MIME) it specifically notes:

    "The whole of the body part, including mime headers, is used to create the signature part"

    The section missing is this:

    "MIME-Version: 1.0
    Content-Type: multipart/signed; protocol="application/x-pkcs7-signature";
    micalg=SHA1; boundary="------------Boundary-00=_JSI1WMUoUzc0s5Rt2uRh"

    "

    ... after that the 2 methods are byte-for-byte identical.

    I've tried many different ways to get the clear content (both enumerating the MimePart of the RootPart, and getting the whole content stream of RootPart and neither will give me the clear content without removing the MIME header.

    I noticed here: http://msdn.microsoft.com/en-us/library/microsoft.exchange.data.mime.mimereader.getrawcontentreadstream(v=exchg.140).aspx

    "When it is called immediately after the EnableReadingUnparsedHeaders method, the GetRawContentReadStream method returns a stream that contains the unparsed headers in the current part, and all unparsed nested parts inside the current part."

    Which leads me to suspect that there ought to be something similar for MimePart - but I can't see anything!

    Does anyone know how to obtain the complete clear content part of the message *with* that MIME header for this specific situation?

    Monday, October 22, 2012 4:35 PM

All replies

  • FWIW we were unable to solve the problem using the EmailMessage or MimePart interfaces. It's possible that we were doing something wrong with the MS model - but we couldn't find how to do it.

    Instead we have used the Hunny Mime parser which allowed us to get the correct clear content needed for signature validation.

    It's far from an ideal solution as we have to start out in the MS model and at certain points transition an embedded message into the Hunny Mime parser world and end up with equivalent code duplication for both forms.

    Tuesday, October 30, 2012 11:40 AM