public void TestSslClientAuthenticationException()
        {
            var location = new IPEndPoint(IPAddress.Loopback, 5055);

            server = new SslServer(location)
            {
                ServerCertificate  = SslServer.LoadCertificate(serverCert),
                AuthenticateClient = true
            };
            server.Start();

            SslClient client =
                new SslClient(location)
            {
                ServerName   = "MyServerName",
                Protocol     = SslProtocols.Default,
                Certificates = null
            };

            try
            {
                client.Connect();

                string echo = client.Echo("Hello World");
                Assert.AreEqual(echo, "Hello World");
            }
            finally
            {
                client.Close();
                server.Stop();
                client = null;
            }
        }
        public void TestSslServerAuthentication()
        {
            var location = new IPEndPoint(IPAddress.Loopback, 5055);

            Console.WriteLine(Directory.GetCurrentDirectory());
            server = new SslServer(location)
            {
                ServerCertificate =
                    SslServer.LoadCertificate(serverCert)
            };
            server.Start();

            SslClient client =
                new SslClient(location)
            {
                ServerName = "MyServerName",
                Protocol   = SslProtocols.Default
            };

            try
            {
                client.Connect();

                string echo = client.Echo("Hello World");
                Assert.AreEqual(echo, "Hello World");
            }
            finally
            {
                client.Close();
                server.Stop();
                client = null;
            }
        }
        public void TestSslClientConfiguration5()
        {
            var location = new IPEndPoint(IPAddress.Loopback, 5055);

            server = new SslServer(location)
            {
                ServerCertificate =
                    SslServer.LoadCertificate(
                        serverCert),
                AuthenticateClient = true
            };
            server.Start();
            TcpClient client = new TcpClient();

            try
            {
                IXmlDocument xmlDoc = XmlHelper.LoadXml("./Net/Ssl/Configs/config5.xml");

                IStreamProvider streamProvider = StreamProviderFactory.CreateProvider(xmlDoc);

                client.Connect(location);
                Stream stream = streamProvider.GetStream(client);

                string echo = SslClient.Echo(stream, "Hello World");
                Assert.AreEqual(echo, "Hello World");
            }
            finally
            {
                client.Close();
                server.Stop();
                client = null;
            }
        }
Example #4
0
        public void TestSslClientAuthentication()
        {
            var location = new IPEndPoint(IPAddress.Loopback, 5055);

            server = new SslServer(location)
            {
                ServerCertificate =
                    SslServer.LoadCertificate(
                        serverCert),
                AuthenticateClient = true
            };
            server.Start();

            try
            {
                SslClient client =
                    new SslClient(location)
                {
                    ServerName = "MyServerName",
                    Protocol   = SslProtocols.Default
                };
                client.AppendCertificate(new X509Certificate(clientCert, clientCertPassword));
                client.Connect();

                string echo = client.Echo("Hello World");
                Assert.AreEqual(echo, "Hello World");
            }
            finally
            {
                server.Stop();
            }
        }
        public void TestSslConfigurationWithSelector()
        {
            IXmlDocument xmlDoc = XmlHelper.LoadXml("./Net/Ssl/Configs/config6.xml");

            Assert.NotNull(xmlDoc);

            IStreamProvider streamProvider = StreamProviderFactory.CreateProvider(xmlDoc);

            Assert.NotNull(streamProvider);
            Assert.IsTrue(streamProvider is SslStreamProvider);

            SslStreamProvider sslStreamProvider = (streamProvider as SslStreamProvider);

            Assert.IsTrue(sslStreamProvider.LocalCertificateSelector is LocalCertificateSelectionCallback);

            var location = new IPEndPoint(IPAddress.Loopback, 5055);

            server = new SslServer(location)
            {
                ServerCertificate  = SslServer.LoadCertificate(serverCert),
                AuthenticateClient = true
            };
            server.Start();

            TcpClient client = new TcpClient();

            try
            {
                client.Connect(location);
                Stream stream = streamProvider.GetStream(client);

                string echo = SslClient.Echo(stream, "Hello World");
                Assert.Fail("Expected Exception, but got none");
            }
            catch (Exception e)
            {
                Console.WriteLine("SslTests.TestSslConfigurationWithSelector(), exception: " + e.ToString());
                Assert.IsTrue(e is AuthenticationException);
                Assert.NotNull(e.Message);
                Assert.IsTrue(e.Message.Contains("RemoteCertificateNameMismatch"));
            }
            finally
            {
                client.Close();
                server.Stop();
                client = null;
            }
        }
        public void TestSslRemoteCertValidation()
        {
            IXmlDocument xmlDoc = XmlHelper.LoadXml("./Net/Ssl/Configs/config7.xml");

            Assert.NotNull(xmlDoc);

            IStreamProvider streamProvider = StreamProviderFactory.CreateProvider(xmlDoc);

            Assert.NotNull(streamProvider);
            Assert.IsTrue(streamProvider is SslStreamProvider);

            SslStreamProvider sslStreamProvider = (streamProvider as SslStreamProvider);

            Assert.IsTrue(sslStreamProvider.RemoteCertificateValidator is RemoteCertificateValidationCallback);

            var location = new IPEndPoint(IPAddress.Loopback, 5055);

            server = new SslServer(location)
            {
                ServerCertificate  = SslServer.LoadCertificate(serverCert),
                AuthenticateClient = true
            };
            server.Start();

            TcpClient client = new TcpClient();

            try
            {
                client.Connect(location);
                Stream stream = streamProvider.GetStream(client);
                Assert.NotNull(stream);
                Assert.AreEqual(sslStreamProvider.ServerName, "MyServerName");

                string echo = SslClient.Echo(stream, "Hello World");
                Assert.AreEqual(echo, "Hello World");
            }
            finally
            {
                client.Close();
                server.Stop();
                client = null;
            }
        }