Exemple #1
0
        /// <summary>
        /// Parses MIME message from the specified stream.
        /// </summary>
        /// <param name="stream">Stream from where to parse MIME message. Parsing starts from current stream position.</param>
        /// <param name="headerEncoding">Header reading encoding. If not sure UTF-8 is recommended.</param>
        /// <returns>Returns parsed MIME message.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>headerEncoding</b> is null.</exception>
        public static MIME_Message ParseFromStream(Stream stream, Encoding headerEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (headerEncoding == null)
            {
                throw new ArgumentNullException("headerEncoding");
            }

            MIME_Message retVal = new MIME_Message();

            retVal.Parse(new SmartStream(stream, false), headerEncoding, new MIME_h_ContentType("text/plain"));

            return(retVal);
        }
        /// <summary>
        /// Gets signed mime content. Value null means no content.
        /// </summary>
        /// <returns>Returns signed mime content. Value null means no content.</returns>
        /// <remarks>This method is valid only if <b>Content-Type</b> parameter <b>smime-type=signed-data</b>.</remarks>
        /// <exception cref="InvalidOperationException">Is raised when <b>smime-type != signed-data</b>.</exception>
        public MIME_Message GetSignedMime()
        {
            if (!string.Equals(this.Entity.ContentType.Parameters["smime-type"], "signed-data", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new InvalidOperationException("The VerifySignature method is only valid if Content-Type parameter smime-type=signed-data.");
            }

            if (this.Data != null)
            {
                SignedCms signedCms = new SignedCms();
                signedCms.Decode(this.Data);

                return(MIME_Message.ParseFromStream(new MemoryStream(signedCms.ContentInfo.Content)));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Decrypts enveloped mime content.
        /// </summary>
        /// <param name="cert">Decrypting certificate.</param>
        /// <returns>Returns decrypted enveloped mime content.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>cert</b> is null reference.</exception>
        /// <exception cref="InvalidOperationException">Is raised when <b>smime-type != enveloped-data</b>.</exception>
        public MIME_Message GetEnvelopedMime(X509Certificate2 cert)
        {
            if (cert == null)
            {
                throw new ArgumentNullException("cert");
            }
            if (!string.Equals(this.Entity.ContentType.Parameters["smime-type"], "enveloped-data", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new InvalidOperationException("The VerifySignature method is only valid if Content-Type parameter smime-type=enveloped-data.");
            }

            EnvelopedCms envelopedCms = new EnvelopedCms();

            envelopedCms.Decode(this.Data);

            X509Certificate2Collection certificates = new X509Certificate2Collection(cert);

            envelopedCms.Decrypt(certificates);

            return(MIME_Message.ParseFromStream(new MemoryStream(envelopedCms.Encode())));
        }