Пример #1
0
        public static void magix_core_application_startup(object sender, ActiveEventArgs e)
        {
            Node ip = Ip(e.Params);

            if (ShouldInspect(ip))
            {
                AppendInspectFromResource(
                    ip["inspect"],
                    "Magix.email",
                    "Magix.email.hyperlisp.inspect.hl",
                    "[magix.core.application-startup-dox].value");
                return;
            }

            CryptographyContext.Register(typeof(WindowsSecureMimeContext));
        }
Пример #2
0
        static PgpMimeTests()
        {
            Environment.SetEnvironmentVariable("GNUPGHOME", Path.GetFullPath("."));
            var dataDir = Path.Combine("..", "..", "TestData", "openpgp");

            CryptographyContext.Register(typeof(DummyOpenPgpContext));

            foreach (var name in new [] { "pubring.gpg", "pubring.gpg~", "secring.gpg", "secring.gpg~" })
            {
                if (File.Exists(name))
                {
                    File.Delete(name);
                }
            }

            using (var ctx = new DummyOpenPgpContext()) {
                using (var seckeys = File.OpenRead(Path.Combine(dataDir, "mimekit.gpg.sec")))
                    ctx.ImportSecretKeys(seckeys);

                using (var pubkeys = File.OpenRead(Path.Combine(dataDir, "mimekit.gpg.pub")))
                    ctx.Import(pubkeys);
            }
        }
Пример #3
0
 private static void _p5_core_application_start(ApplicationContext context, ActiveEventArgs e)
 {
     // Registering our Cryptography context, which is wrapping the local installation of Gnu Privacy Guard
     CryptographyContext.Register(typeof(GnuPrivacyContext));
 }
Пример #4
0
        public void TestArgumentExceptions()
        {
            Assert.Throws <ArgumentNullException> (() => CryptographyContext.Create(null));
            Assert.Throws <ArgumentNullException> (() => CryptographyContext.Register((Type)null));
            Assert.Throws <ArgumentNullException> (() => CryptographyContext.Register((Func <OpenPgpContext>)null));
            Assert.Throws <ArgumentNullException> (() => CryptographyContext.Register((Func <SecureMimeContext>)null));

            using (var ctx = new DummyOpenPgpContext()) {
                var mailboxes      = new [] { new MailboxAddress("MimeKit UnitTests", "*****@*****.**") };
                var emptyMailboxes = new MailboxAddress[0];
                var pubkeys        = ctx.GetPublicKeys(mailboxes);
                var key            = ctx.GetSigningKey(mailboxes[0]);
                var emptyPubkeys   = new PgpPublicKey[0];
                var stream         = new MemoryStream();

                Assert.Throws <ArgumentException> (() => ctx.KeyServer = new Uri("relative/uri", UriKind.Relative));

                Assert.Throws <ArgumentNullException> (() => ctx.GetDigestAlgorithm(null));
                Assert.Throws <ArgumentOutOfRangeException> (() => ctx.GetDigestAlgorithmName(DigestAlgorithm.DoubleSha));
                Assert.Throws <NotSupportedException> (() => OpenPgpContext.GetHashAlgorithm(DigestAlgorithm.DoubleSha));
                Assert.Throws <NotSupportedException> (() => OpenPgpContext.GetHashAlgorithm(DigestAlgorithm.Tiger192));
                Assert.Throws <NotSupportedException> (() => OpenPgpContext.GetHashAlgorithm(DigestAlgorithm.Haval5160));
                Assert.Throws <NotSupportedException> (() => OpenPgpContext.GetHashAlgorithm(DigestAlgorithm.MD4));
                Assert.Throws <ArgumentOutOfRangeException> (() => OpenPgpContext.GetDigestAlgorithm((Org.BouncyCastle.Bcpg.HashAlgorithmTag) 1024));

                Assert.Throws <ArgumentNullException> (() => new ApplicationPgpEncrypted((MimeEntityConstructorArgs)null));
                Assert.Throws <ArgumentNullException> (() => new ApplicationPgpSignature((MimeEntityConstructorArgs)null));
                Assert.Throws <ArgumentNullException> (() => new ApplicationPgpSignature((Stream)null));

                // Accept
                Assert.Throws <ArgumentNullException> (() => new ApplicationPgpEncrypted().Accept(null));
                Assert.Throws <ArgumentNullException> (() => new ApplicationPgpSignature(stream).Accept(null));

                // Decrypt
                Assert.Throws <ArgumentNullException> (() => ctx.Decrypt(null), "Decrypt");

                // Encrypt
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt(EncryptionAlgorithm.Cast5, (MailboxAddress[])null, stream), "Encrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt(EncryptionAlgorithm.Cast5, (PgpPublicKey[])null, stream), "Encrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt((MailboxAddress[])null, stream), "Encrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt((PgpPublicKey[])null, stream), "Encrypt");
                Assert.Throws <ArgumentException> (() => ctx.Encrypt(EncryptionAlgorithm.Cast5, emptyMailboxes, stream), "Encrypt");
                Assert.Throws <ArgumentException> (() => ctx.Encrypt(EncryptionAlgorithm.Cast5, emptyPubkeys, stream), "Encrypt");
                Assert.Throws <ArgumentException> (() => ctx.Encrypt(emptyMailboxes, stream), "Encrypt");
                Assert.Throws <ArgumentException> (() => ctx.Encrypt(emptyPubkeys, stream), "Encrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt(EncryptionAlgorithm.Cast5, mailboxes, null), "Encrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt(EncryptionAlgorithm.Cast5, pubkeys, null), "Encrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt(mailboxes, null), "Encrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.Encrypt(pubkeys, null), "Encrypt");

                // Export
                Assert.Throws <ArgumentNullException> (() => ctx.Export((PgpPublicKeyRingBundle)null), "Export");
                Assert.Throws <ArgumentNullException> (() => ctx.Export((MailboxAddress[])null), "Export");
                Assert.Throws <ArgumentNullException> (() => ctx.Export((PgpPublicKey[])null), "Export");

                // GetDecryptedStream
                Assert.Throws <ArgumentNullException> (() => ctx.GetDecryptedStream(null), "GetDecryptedStream");

                // GetDigestAlgorithmName
                Assert.Throws <ArgumentOutOfRangeException> (() => ctx.GetDigestAlgorithmName(DigestAlgorithm.None), "GetDigestAlgorithmName");

                // Import
                Assert.Throws <ArgumentNullException> (() => ctx.Import((Stream)null), "Import");
                Assert.Throws <ArgumentNullException> (() => ctx.Import((PgpPublicKeyRing)null), "Import");
                Assert.Throws <ArgumentNullException> (() => ctx.Import((PgpPublicKeyRingBundle)null), "Import");
                Assert.Throws <ArgumentNullException> (() => ctx.Import((PgpSecretKeyRing)null), "Import");
                Assert.Throws <ArgumentNullException> (() => ctx.Import((PgpSecretKeyRingBundle)null), "Import");

                // ImportSecretKeys
                Assert.Throws <ArgumentNullException> (() => ctx.ImportSecretKeys(null), "ImportSecretKeys");

                // Sign
                Assert.Throws <ArgumentNullException> (() => ctx.Sign((MailboxAddress)null, DigestAlgorithm.Sha1, stream), "Sign");
                Assert.Throws <ArgumentNullException> (() => ctx.Sign((PgpSecretKey)null, DigestAlgorithm.Sha1, stream), "Sign");
                Assert.Throws <ArgumentNullException> (() => ctx.Sign(mailboxes[0], DigestAlgorithm.Sha1, null), "Sign");
                Assert.Throws <ArgumentNullException> (() => ctx.Sign(key, DigestAlgorithm.Sha1, null), "Sign");

                // SignAndEncrypt
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt((MailboxAddress)null, DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, mailboxes, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt((PgpSecretKey)null, DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, pubkeys, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(mailboxes[0], DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, (MailboxAddress[])null, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(key, DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, (PgpPublicKey[])null, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentException> (() => ctx.SignAndEncrypt(mailboxes[0], DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, emptyMailboxes, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentException> (() => ctx.SignAndEncrypt(key, DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, emptyPubkeys, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(mailboxes[0], DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, mailboxes, null), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(key, DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, pubkeys, null), "SignAndEncrypt");

                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt((MailboxAddress)null, DigestAlgorithm.Sha1, mailboxes, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt((PgpSecretKey)null, DigestAlgorithm.Sha1, pubkeys, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(mailboxes[0], DigestAlgorithm.Sha1, (MailboxAddress[])null, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(key, DigestAlgorithm.Sha1, (PgpPublicKey[])null, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentException> (() => ctx.SignAndEncrypt(mailboxes[0], DigestAlgorithm.Sha1, emptyMailboxes, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentException> (() => ctx.SignAndEncrypt(key, DigestAlgorithm.Sha1, emptyPubkeys, stream), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(mailboxes[0], DigestAlgorithm.Sha1, mailboxes, null), "SignAndEncrypt");
                Assert.Throws <ArgumentNullException> (() => ctx.SignAndEncrypt(key, DigestAlgorithm.Sha1, pubkeys, null), "SignAndEncrypt");

                // Supports
                Assert.Throws <ArgumentNullException> (() => ctx.Supports(null), "Supports");

                // Verify
                Assert.Throws <ArgumentNullException> (() => ctx.Verify(null, stream), "Verify");
                Assert.Throws <ArgumentNullException> (() => ctx.Verify(stream, null), "Verify");
            }
        }
Пример #5
0
        protected SecureMimeTestsBase()
        {
            using (var ctx = CreateContext()) {
                var    dataDir = Path.Combine("..", "..", "TestData", "smime");
                string path;

                CryptographyContext.Register(ctx.GetType());

                if (ctx is WindowsSecureMimeContext)
                {
                    var windows = (WindowsSecureMimeContext)ctx;
                    var parser  = new X509CertificateParser();

                    using (var stream = File.OpenRead(Path.Combine(dataDir, "certificate-authority.crt"))) {
                        foreach (X509Certificate certificate in parser.ReadCertificates(stream))
                        {
                            windows.Import(StoreName.AuthRoot, certificate);
                        }
                    }

                    using (var stream = File.OpenRead(Path.Combine(dataDir, "intermediate.crt"))) {
                        foreach (X509Certificate certificate in parser.ReadCertificates(stream))
                        {
                            windows.Import(StoreName.CertificateAuthority, certificate);
                        }
                    }

                    using (var stream = File.OpenRead(Path.Combine(dataDir, "StartComCertificationAuthority.crt"))) {
                        foreach (X509Certificate certificate in parser.ReadCertificates(stream))
                        {
                            windows.Import(StoreName.AuthRoot, certificate);
                        }
                    }

                    using (var stream = File.OpenRead(Path.Combine(dataDir, "StartComClass1PrimaryIntermediateClientCA.crt"))) {
                        foreach (X509Certificate certificate in parser.ReadCertificates(stream))
                        {
                            windows.Import(StoreName.CertificateAuthority, certificate);
                        }
                    }
                }
                else
                {
                    foreach (var filename in CertificateAuthorities)
                    {
                        path = Path.Combine(dataDir, filename);
                        using (var stream = File.OpenRead(path)) {
                            if (ctx is DefaultSecureMimeContext)
                            {
                                ((DefaultSecureMimeContext)ctx).Import(stream, true);
                            }
                            else
                            {
                                var parser = new X509CertificateParser();
                                foreach (X509Certificate certificate in parser.ReadCertificates(stream))
                                {
                                    ctx.Import(certificate);
                                }
                            }
                        }
                    }
                }

                path = Path.Combine(dataDir, "smime.p12");

                using (var file = File.OpenRead(path))
                    ctx.Import(file, "no.secret");
            }
        }
Пример #6
0
        public static List <IEmailMessage> GetIncomingEmails(
            string address,
            bool loadHtmlBody = false,
            CancellationToken cancellationToken = default)
        {
            Contract.Requires(!String.IsNullOrEmpty(address));

            var settings = GetConnectionSettings(address);

            List <IEmailMessage> GetEmails()
            {
                var service = new EWS.ExchangeService(settings.Version)
                {
                    Url = new Uri(settings.Url)
                };

                if (settings.UseDefaultCredentials)
                {
                    service.UseDefaultCredentials = true;
                }
                else
                {
                    service.Credentials = new NetworkCredential(settings.UserName, settings.Password, settings.Domain);
                }
                var mailbox       = new EWS.Mailbox(address);
                var inboxFolderId = new EWS.FolderId(EWS.WellKnownFolderName.Inbox, mailbox);
                var propertySet   = new EWS.PropertySet(EWS.BasePropertySet.FirstClassProperties)
                {
                    RequestedBodyType = EWS.BodyType.Text
                };

                if (loadHtmlBody)
                {
                    propertySet.Add(new EWS.ExtendedPropertyDefinition(ExtendedProperty_HtmlBody, EWS.MapiPropertyType.Binary));
                }
                propertySet.Add(new EWS.ExtendedPropertyDefinition(ExtendedProperty_ConversationId, EWS.MapiPropertyType.Binary));
                var view = new EWS.ItemView(100)
                {
                    OrderBy     = { { EWS.ItemSchema.DateTimeReceived, EWS.SortDirection.Ascending } },
                    PropertySet = propertySet
                };

                var emails = (from item in service.FindItems(inboxFolderId, view)
                              let email = item as EWS.EmailMessage
                                          where email != null
                                          select email).ToList();

                if (emails.Count > 0)
                {
                    propertySet.Add(EWS.ItemSchema.MimeContent);
                    service.LoadPropertiesForItems(emails, propertySet);
                }
                return(emails.ConvertAll(email => {
                    if (email.ItemClass == "IPM.Note.SMIME")
                    {
                        using (var memoryStream = new MemoryStream(email.MimeContent.Content))
                        {
                            memoryStream.Position = 0L;
                            var pkcs7Mime = (ApplicationPkcs7Mime)MimeEntity.Load(memoryStream);
                            CryptographyContext.Register(typeof(WindowsSecureMimeContext));
                            pkcs7Mime.Verify(out var mimeEntity);
                            var multipart = (Multipart)mimeEntity;
                            var body = multipart.OfType <TextPart>().FirstOrDefault()?.Text;
                            var message = multipart.OfType <TnefPart>().FirstOrDefault()?.ConvertToMessage();
                            return new EmailMessage(
                                email,
                                body,
                                message?.Attachments
                                .OfType <MimePart>()
                                .Select(mp => {
                                memoryStream.SetLength(0L);
                                using (var contentStream = mp.Content.Open())
                                {
                                    contentStream.CopyTo(memoryStream);
                                }
                                return new FileAttachment(
                                    mp.FileName,
                                    memoryStream.ToArray());
                            })
                                .ToArray());
                        }
                    }
                    else
                    {
                        return (IEmailMessage) new EmailMessage(email);
                    }
                }));
            }

            var retries = 0;

            while (true)
            {
                try
                {
                    return(GetEmails());
                }
                catch when(retries < settings.Retries)
                {
                    //await Task.Delay(settings.RetryDelay, cancellationToken);
                    retries++;
                }
            }
        }