Beispiel #1
0
        public void TestFiles(string fileName)
        {
            string filePath = Path.Combine("Mail\\TestFiles", fileName);
            string mailtext = File.ReadAllText(filePath);

            Message message = null;

            Assert.DoesNotThrow(() => message = Message.Load(mailtext));

            if (SMIMEStandard.IsContentMultipartSignature(message.ParsedContentType))
            {
                SignedEntity signedEntity = null;

                Assert.DoesNotThrow(() => signedEntity = SignedEntity.Load(message));
                message.Headers = message.Headers.SelectNonMimeHeaders();
                message.UpdateBody(signedEntity.Content); // this will merge in content + content specific mime headers
            }

            Message extracted = null;

            Assert.DoesNotThrow(() => extracted = WrappedMessage.ExtractInner(message));

            Header to = null;

            Assert.DoesNotThrow(() => to = extracted.To);

            MailAddressCollection addresses = null;

            Assert.DoesNotThrow(() => addresses = MailParser.ParseAddressCollection(to));
            Assert.True(addresses.Count > 0);

            Assert.DoesNotThrow(() => MailParser.ParseMailAddress(extracted.From));
        }
Beispiel #2
0
        /// <summary>
        /// Decrypt (optionally) the given message and try to extract signatures
        /// </summary>
        bool DecryptSignatures(IncomingMessage message, X509Certificate2 certificate, out SignedCms signatures, out MimeEntity payload)
        {
            MimeEntity decryptedEntity = null;

            signatures = null;
            payload    = null;

            if (certificate != null)
            {
                decryptedEntity = m_cryptographer.DecryptEntity(message.GetEncryptedBytes(m_cryptographer), certificate);
            }
            else
            {
                decryptedEntity = message.Message;
            }
            if (decryptedEntity == null)
            {
                return(false);
            }

            if (SMIMEStandard.IsContentEnvelopedSignature(decryptedEntity.ParsedContentType))
            {
                signatures = m_cryptographer.DeserializeEnvelopedSignature(decryptedEntity);
                payload    = MimeSerializer.Default.Deserialize <MimeEntity>(signatures.ContentInfo.Content);
            }
            else if (SMIMEStandard.IsContentMultipartSignature(decryptedEntity.ParsedContentType))
            {
                SignedEntity signedEntity = SignedEntity.Load(decryptedEntity);
                signatures = m_cryptographer.DeserializeDetachedSignature(signedEntity);
                payload    = signedEntity.Content;
            }
            else
            {
                throw new AgentException(AgentError.UnsignedMessage);
            }

            return(true);
        }