public static void Main(string[] args)
        {
            string hostname = "localhost";
            int    port     = 5556;

            long time1 = DateTime.UtcNow.Ticks;

            /*
             * Note: This is the default PSK identity for 'openssl s_server' testing, the server must be
             * started with "-psk 6161616161" to make the keys match, and possibly the "-psk_hint"
             * option should be present.
             */
            //string psk_identity = "Client_identity";
            //byte[] psk = new byte[]{ 0x61, 0x61, 0x61, 0x61, 0x61 };

            // These correspond to the configuration of MockPskTlsServer
            string psk_identity = "client";

            byte[] psk = Strings.ToUtf8ByteArray("TLS_TEST_PSK");

            BasicTlsPskIdentity pskIdentity = new BasicTlsPskIdentity(psk_identity, psk);

            MockPskTlsClient  client   = new MockPskTlsClient(null, pskIdentity);
            TlsClientProtocol protocol = OpenTlsConnection(hostname, port, client);

            protocol.Close();

            long time2 = DateTime.UtcNow.Ticks;

            Console.WriteLine("Elapsed 1: " + (time2 - time1) / TimeSpan.TicksPerMillisecond + "ms");

            client   = new MockPskTlsClient(client.GetSessionToResume(), pskIdentity);
            protocol = OpenTlsConnection(hostname, port, client);

            long time3 = DateTime.UtcNow.Ticks;

            Console.WriteLine("Elapsed 2: " + (time3 - time2) / TimeSpan.TicksPerMillisecond + "ms");

            byte[] req = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\n\r\n");

            Stream tlsStream = protocol.Stream;

            tlsStream.Write(req, 0, req.Length);
            tlsStream.Flush();

            StreamReader reader = new StreamReader(tlsStream);

            String line;

            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(">>> " + line);
            }

            protocol.Close();
        }
        public static void Main(string[] args)
        {
            string hostname = "localhost";
            int port = 5556;

            long time1 = DateTime.UtcNow.Ticks;

            /*
             * Note: This is the default PSK identity for 'openssl s_server' testing, the server must be
             * started with "-psk 6161616161" to make the keys match, and possibly the "-psk_hint"
             * option should be present.
             */
            string psk_identity = "Client_identity";
            byte[] psk = new byte[]{ 0x61, 0x61, 0x61, 0x61, 0x61 };

            BasicTlsPskIdentity pskIdentity = new BasicTlsPskIdentity(psk_identity, psk);

            MockPskTlsClient client = new MockPskTlsClient(null, pskIdentity);
            TlsClientProtocol protocol = OpenTlsConnection(hostname, port, client);
            protocol.Close();

            long time2 = DateTime.UtcNow.Ticks;
            Console.WriteLine("Elapsed 1: " + (time2 - time1)/TimeSpan.TicksPerMillisecond + "ms");

            client = new MockPskTlsClient(client.GetSessionToResume(), pskIdentity);
            protocol = OpenTlsConnection(hostname, port, client);

            long time3 = DateTime.UtcNow.Ticks;
            Console.WriteLine("Elapsed 2: " + (time3 - time2)/TimeSpan.TicksPerMillisecond + "ms");

            byte[] req = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\n\r\n");

            Stream tlsStream = protocol.Stream;
            tlsStream.Write(req, 0, req.Length);
            tlsStream.Flush();

            StreamReader reader = new StreamReader(tlsStream);

            String line;
            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(">>> " + line);
            }

            protocol.Close();
        }
Example #3
0
        public void TestClientServer()
        {
            SecureRandom secureRandom = new SecureRandom();

            PipedStream clientPipe = new PipedStream();
            PipedStream serverPipe = new PipedStream(clientPipe);

            TlsClientProtocol clientProtocol = new TlsClientProtocol(clientPipe, secureRandom);
            TlsServerProtocol serverProtocol = new TlsServerProtocol(serverPipe, secureRandom);

            Server server = new Server(serverProtocol);

            Thread serverThread = new Thread(new ThreadStart(server.Run));

            serverThread.Start();

            MockPskTlsClient client = new MockPskTlsClient(null);

            clientProtocol.Connect(client);

            // NOTE: Because we write-all before we read-any, this length can't be more than the pipe capacity
            int length = 1000;

            byte[] data = new byte[length];
            secureRandom.NextBytes(data);

            Stream output = clientProtocol.Stream;

            output.Write(data, 0, data.Length);

            byte[] echo  = new byte[data.Length];
            int    count = Streams.ReadFully(clientProtocol.Stream, echo);

            Assert.AreEqual(count, data.Length);
            Assert.IsTrue(Arrays.AreEqual(data, echo));

            output.Close();

            serverThread.Join();
        }
        public void TestClientServer()
        {
            SecureRandom secureRandom = new SecureRandom();

            PipedStream clientPipe = new PipedStream();
            PipedStream serverPipe = new PipedStream(clientPipe);

            TlsClientProtocol clientProtocol = new TlsClientProtocol(clientPipe, secureRandom);
            TlsServerProtocol serverProtocol = new TlsServerProtocol(serverPipe, secureRandom);

            Server server = new Server(serverProtocol);

            Thread serverThread = new Thread(new ThreadStart(server.Run));
            serverThread.Start();

            MockPskTlsClient client = new MockPskTlsClient(null);
            clientProtocol.Connect(client);

            // NOTE: Because we write-all before we read-any, this length can't be more than the pipe capacity
            int length = 1000;

            byte[] data = new byte[length];
            secureRandom.NextBytes(data);

            Stream output = clientProtocol.Stream;
            output.Write(data, 0, data.Length);

            byte[] echo = new byte[data.Length];
            int count = Streams.ReadFully(clientProtocol.Stream, echo);

            Assert.AreEqual(count, data.Length);
            Assert.IsTrue(Arrays.AreEqual(data, echo));

            output.Close();

            serverThread.Join();
        }