Exemple #1
0
        bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            bool valid;

            sslValidationInfo?.Dispose();
            sslValidationInfo = null;

            if (ServerCertificateValidationCallback != null)
            {
                valid = ServerCertificateValidationCallback(ProxyHost, certificate, chain, sslPolicyErrors);
#if !NETSTANDARD1_3 && !NETSTANDARD1_6
            }
            else if (ServicePointManager.ServerCertificateValidationCallback != null)
            {
                valid = ServicePointManager.ServerCertificateValidationCallback(ProxyHost, certificate, chain, sslPolicyErrors);
#endif
            }
            else
            {
                valid = sslPolicyErrors == SslPolicyErrors.None;
            }

            if (!valid)
            {
                // Note: The SslHandshakeException.Create() method will nullify this once it's done using it.
                sslValidationInfo = new SslCertificateValidationInfo(sender, certificate, chain, sslPolicyErrors);
            }

            return(valid);
        }
        public void TestSerialization()
        {
            var expected = new SslHandshakeException("Bad boys, bad boys. Whatcha gonna do?", new IOException("I/O Error."));
            SslCertificateValidationInfo info = null;

            Assert.AreEqual(HelpLink, expected.HelpLink, "Unexpected HelpLink.");

            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SslHandshakeException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.Message, ex.Message, "Unexpected Message.");
                Assert.AreEqual(expected.HelpLink, ex.HelpLink, "Unexpected HelpLink.");
            }

            expected = new SslHandshakeException("Bad boys, bad boys. Whatcha gonna do?");

            Assert.AreEqual(HelpLink, expected.HelpLink, "Unexpected HelpLink.");

            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SslHandshakeException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.Message, ex.Message, "Unexpected Message.");
                Assert.AreEqual(expected.HelpLink, ex.HelpLink, "Unexpected HelpLink.");
            }

            expected = new SslHandshakeException();

            Assert.AreEqual(HelpLink, expected.HelpLink, "Unexpected HelpLink.");

            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SslHandshakeException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.Message, ex.Message, "Unexpected Message.");
                Assert.AreEqual(expected.HelpLink, ex.HelpLink, "Unexpected HelpLink.");
            }

            expected = SslHandshakeException.Create(ref info, new AggregateException("Aggregate errors.", new IOException(), new IOException()), false, "IMAP", "localhost", 993, 993, 143);

            Assert.AreEqual(HelpLink, expected.HelpLink, "Unexpected HelpLink.");

            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SslHandshakeException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.Message, ex.Message, "Unexpected Message.");
                Assert.AreEqual(expected.HelpLink, ex.HelpLink, "Unexpected HelpLink.");
            }

            expected = SslHandshakeException.Create(ref info, new AggregateException("Aggregate errors.", new IOException(), new IOException()), true, "IMAP", "localhost", 143, 993, 143);

            Assert.AreEqual(HelpLink, expected.HelpLink, "Unexpected HelpLink.");

            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SslHandshakeException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.Message, ex.Message, "Unexpected Message.");
                Assert.AreEqual(expected.HelpLink, ex.HelpLink, "Unexpected HelpLink.");
            }

            expected = SslHandshakeException.Create(ref info, new AggregateException("Aggregate errors.", new IOException()), false, "IMAP", "localhost", 993, 993, 143);

            Assert.AreEqual(HelpLink, expected.HelpLink, "Unexpected HelpLink.");

            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SslHandshakeException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.Message, ex.Message, "Unexpected Message.");
                Assert.AreEqual(expected.HelpLink, ex.HelpLink, "Unexpected HelpLink.");
            }

            expected = SslHandshakeException.Create(ref info, new AggregateException("Aggregate errors.", new IOException()), true, "IMAP", "localhost", 143, 993, 143);

            Assert.AreEqual(HelpLink, expected.HelpLink, "Unexpected HelpLink.");

            using (var stream = new MemoryStream()) {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, expected);
                stream.Position = 0;

                var ex = (SslHandshakeException)formatter.Deserialize(stream);
                Assert.AreEqual(expected.Message, ex.Message, "Unexpected Message.");
                Assert.AreEqual(expected.HelpLink, ex.HelpLink, "Unexpected HelpLink.");
            }
        }