public CertificateValidationClientServer()
        {
            _serverCertificateCollection = TestConfiguration.GetServerCertificateCollection();
            _serverCertificate           = TestConfiguration.GetServerCertificate();

            _clientCertificateCollection = TestConfiguration.GetClientCertificateCollection();
            _clientCertificate           = TestConfiguration.GetClientCertificate();
        }
        public async void SslStream_SendReceiveOverNetworkStream_Ok()
        {
            X509Certificate2 serverCertificate = TestConfiguration.GetServerCertificate();
            TcpListener      listener          = new TcpListener(IPAddress.Any, 0);

            using (TcpClient client = new TcpClient())
            {
                listener.Start();

                Task             clientConnectTask  = client.ConnectAsync(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
                Task <TcpClient> listenerAcceptTask = listener.AcceptTcpClientAsync();

                await Task.WhenAll(clientConnectTask, listenerAcceptTask);

                TcpClient server = listenerAcceptTask.Result;
                using (SslStream clientStream = new SslStream(
                           client.GetStream(),
                           false,
                           new RemoteCertificateValidationCallback(ValidateServerCertificate),
                           null,
                           EncryptionPolicy.RequireEncryption))
                    using (SslStream serverStream = new SslStream(
                               server.GetStream(),
                               false,
                               null,
                               null,
                               EncryptionPolicy.RequireEncryption))
                    {
                        Task clientAuthenticationTask = clientStream.AuthenticateAsClientAsync(
                            serverCertificate.GetNameInfo(X509NameType.SimpleName, false),
                            null,
                            SslProtocols.Tls12,
                            false);

                        Task serverAuthenticationTask = serverStream.AuthenticateAsServerAsync(
                            serverCertificate,
                            false,
                            SslProtocols.Tls12,
                            false);

                        await Task.WhenAll(clientAuthenticationTask, serverAuthenticationTask);

                        byte[]     readBuffer = new byte[256];
                        Task <int> readTask   = clientStream.ReadAsync(readBuffer, 0, readBuffer.Length);

                        byte[] writeBuffer = new byte[256];
                        Task   writeTask   = clientStream.WriteAsync(writeBuffer, 0, writeBuffer.Length);

                        bool result = Task.WaitAll(
                            new Task[1] {
                            writeTask
                        },
                            TestConfiguration.PassingTestTimeoutMilliseconds);

                        Assert.True(result, "WriteAsync timed-out.");
                    }
            }
        }
Exemplo n.º 3
0
        private bool DoHandshake(SslStream clientSslStream, SslStream serverSslStream)
        {
            X509Certificate2 certificate = TestConfiguration.GetServerCertificate();

            Task[] auth = new Task[2];

            auth[0] = clientSslStream.AuthenticateAsClientAsync(certificate.GetNameInfo(X509NameType.SimpleName, false));
            auth[1] = serverSslStream.AuthenticateAsServerAsync(certificate);

            bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);

            return(finished);
        }
        public void SslStream_StreamToStream_Authentication_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var server = new SslStream(serverStream))
                        {
                            X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                            Task[]           auth        = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync(certificate.Subject);
                            auth[1] = server.AuthenticateAsServerAsync(certificate);

                            Task.WaitAll(auth);
                        }
        }
Exemplo n.º 5
0
        public void SslStream_StreamToStream_Authentication_Success()
        {
            VirtualNetwork network = new VirtualNetwork();

            using (var clientStream = new VirtualNetworkStream(network, isServer: false))
                using (var serverStream = new VirtualNetworkStream(network, isServer: true))
                    using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var server = new SslStream(serverStream))
                        {
                            X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                            Task[]           auth        = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync(certificate.GetNameInfo(X509NameType.SimpleName, false));
                            auth[1] = server.AuthenticateAsServerAsync(certificate);

                            bool finished = Task.WaitAll(auth, TestConfiguration.PassingTestTimeoutMilliseconds);
                            Assert.True(finished, "Handshake completed in the allotted time");
                        }
        }
        public void SslStream_StreamToStream_Authentication_Success()
        {
            MockNetwork network = new MockNetwork();

            using (var clientStream = new FakeNetworkStream(false, network))
                using (var serverStream = new FakeNetworkStream(true, network))
                    using (var client = new SslStream(clientStream, false, AllowAnyServerCertificate))
                        using (var server = new SslStream(serverStream))
                        {
                            X509Certificate2 certificate = TestConfiguration.GetServerCertificate();
                            Task[]           auth        = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync(certificate.Subject);
                            auth[1] = server.AuthenticateAsServerAsync(certificate);

                            bool finished = Task.WaitAll(auth, TestTimeoutSpan);
                            Assert.True(finished, "Handshake completed in the allotted time");
                        }
        }
Exemplo n.º 7
0
        public void SslStream_StreamToStream_Authentication_IncorrectServerName_Fail()
        {
            VirtualNetwork network = new VirtualNetwork();

            using (var clientStream = new VirtualNetworkStream(network, isServer: false))
                using (var serverStream = new VirtualNetworkStream(network, isServer: true))
                    using (var client = new SslStream(clientStream))
                        using (var server = new SslStream(serverStream))
                        {
                            Task[] auth = new Task[2];
                            auth[0] = client.AuthenticateAsClientAsync("incorrectServer");
                            auth[1] = server.AuthenticateAsServerAsync(TestConfiguration.GetServerCertificate());

                            Assert.Throws <AuthenticationException>(() =>
                            {
                                auth[0].GetAwaiter().GetResult();
                            });

                            auth[1].GetAwaiter().GetResult();
                        }
        }
Exemplo n.º 8
0
 public ServerAsyncAuthenticateTest()
 {
     _log = TestLogging.GetInstance();
     _serverCertificate = TestConfiguration.GetServerCertificate();
 }
Exemplo n.º 9
0
        private void OnAccept(Task <TcpClient> result)
        {
            TcpClient client = null;

            // Accept current connection
            try
            {
                client = result.Result;
            }
            catch
            {
            }

            // If we have a connection, then process it
            if (client != null)
            {
                OnClientAccepted(client);

                ClientState state;

                // Start authentication for SSL?
                if (_useSsl)
                {
                    state = new ClientState(client, _sslEncryptionPolicy);
                    _log.WriteLine("Server: starting SSL authentication.");


                    SslStream        sslStream   = null;
                    X509Certificate2 certificate = TestConfiguration.GetServerCertificate();

                    try
                    {
                        sslStream = (SslStream)state.Stream;

                        _log.WriteLine("Server: attempting to open SslStream.");
                        sslStream.AuthenticateAsServerAsync(certificate, false, _sslProtocols, false).ContinueWith(t => OnAuthenticate(t, state), TaskScheduler.Default);
                    }
                    catch (Exception ex)
                    {
                        _log.WriteLine("Server: Exception: {0}", ex);

                        state.Dispose(); // close connection to client
                    }
                }
                else
                {
                    state = new ClientState(client);

                    // Start listening for data from the client connection
                    try
                    {
                        state.Stream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
                    }
                    catch
                    {
                    }
                }
            }

            // Listen for more client connections
            try
            {
                _listener.AcceptTcpClientAsync().ContinueWith(t => OnAccept(t), TaskScheduler.Default);
            }
            catch
            {
            }
        }