public void Connect()
 {
     TcpClient.Connect(ServerAddress);
     Stream = new SslStreamProvider
     {
         ServerName         = ServerName,
         Protocols          = Protocol,
         ClientCertificates = Certificates
     }.GetStream(TcpClient);
 }
        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;
            }
        }