예제 #1
0
 public void Run()
 {
     try
     {
         MockTlsServer     server         = new MockTlsServer();
         TlsServerProtocol serverProtocol = new TlsServerProtocol(s.GetStream(), secureRandom);
         serverProtocol.Accept(server);
         Stream log = new TeeOutputStream(serverProtocol.Stream, stdout);
         Streams.PipeAll(serverProtocol.Stream, log);
         serverProtocol.Close();
     }
     finally
     {
         try
         {
             s.Close();
         }
         catch (IOException)
         {
         }
         finally
         {
         }
     }
 }
        private static void DoTestClientServer(bool fragment)
        {
            SecureRandom secureRandom = new SecureRandom();

            TlsClientProtocol clientProtocol = new TlsClientProtocol(secureRandom);
            TlsServerProtocol serverProtocol = new TlsServerProtocol(secureRandom);

            clientProtocol.Connect(new MockTlsClient(null));
            serverProtocol.Accept(new MockTlsServer());

            // pump handshake
            bool hadDataFromServer = true;
            bool hadDataFromClient = true;

            while (hadDataFromServer || hadDataFromClient)
            {
                hadDataFromServer = PumpData(serverProtocol, clientProtocol, fragment);
                hadDataFromClient = PumpData(clientProtocol, serverProtocol, fragment);
            }

            // send data in both directions
            byte[] data = new byte[1024];
            secureRandom.NextBytes(data);
            WriteAndRead(clientProtocol, serverProtocol, data, fragment);
            WriteAndRead(serverProtocol, clientProtocol, data, fragment);

            // close the connection
            clientProtocol.Close();
            PumpData(clientProtocol, serverProtocol, fragment);
            serverProtocol.CloseInput();
            CheckClosed(serverProtocol);
            CheckClosed(clientProtocol);
        }
        private static void DoTestClientServer(bool fragment)
        {
            SecureRandom secureRandom = new SecureRandom();

            TlsClientProtocol clientProtocol = new TlsClientProtocol(secureRandom);
            TlsServerProtocol serverProtocol = new TlsServerProtocol(secureRandom);

            clientProtocol.Connect(new MockTlsClient(null));
            serverProtocol.Accept(new MockTlsServer());

            // pump handshake
            bool hadDataFromServer = true;
            bool hadDataFromClient = true;
            while (hadDataFromServer || hadDataFromClient)
            {
                hadDataFromServer = PumpData(serverProtocol, clientProtocol, fragment);
                hadDataFromClient = PumpData(clientProtocol, serverProtocol, fragment);
            }

            // send data in both directions
            byte[] data = new byte[1024];
            secureRandom.NextBytes(data);
            WriteAndRead(clientProtocol, serverProtocol, data, fragment);
            WriteAndRead(serverProtocol, clientProtocol, data, fragment);

            // close the connection
            clientProtocol.Close();
            PumpData(clientProtocol, serverProtocol, fragment);
            CheckClosed(serverProtocol);
            CheckClosed(clientProtocol);
        }
예제 #4
0
 public void Run()
 {
     try
     {
         MockTlsServer server = new MockTlsServer();
         TlsServerProtocol serverProtocol = new TlsServerProtocol(s.GetStream(), secureRandom);
         serverProtocol.Accept(server);
         Stream log = new TeeOutputStream(serverProtocol.Stream, stdout);
         Streams.PipeAll(serverProtocol.Stream, log);
         serverProtocol.Close();
     }
     finally
     {
         try
         {
             s.Close();
         }
         catch (IOException)
         {
         }
         finally
         {
         }
     }
 }
예제 #5
0
 public void Run()
 {
     try
     {
         MockPskTlsServer server = new MockPskTlsServer();
         mServerProtocol.Accept(server);
         Streams.PipeAll(mServerProtocol.Stream, mServerProtocol.Stream);
         mServerProtocol.Close();
     }
     catch (Exception)
     {
         //throw new RuntimeException(e);
     }
 }
        public async Task OnConnectionAsync(ConnectionContext context)
        {
            var inputStream  = context.Transport.Input.AsStream();
            var outputStream = context.Transport.Output.AsStream();
            var protocol     = new TlsServerProtocol(inputStream, outputStream, new Org.BouncyCastle.Security.SecureRandom());

            protocol.Accept(new BlazeTlsServer(_options));
            var sslStream = protocol.Stream;


            var memoryPool = context.Features.Get <IMemoryPoolFeature>()?.MemoryPool;

            var inputPipeOptions = new StreamPipeReaderOptions
                                   (
                pool: memoryPool,
                bufferSize: memoryPool.GetMinimumSegmentSize(),
                minimumReadSize: memoryPool.GetMinimumAllocSize(),
                leaveOpen: true
                                   );


            var outputPipeOptions = new StreamPipeWriterOptions
                                    (
                pool: memoryPool,
                leaveOpen: true
                                    );

            var sslDuplexPipe = new DuplexPipeStreamAdapter <Stream>(context.Transport, inputPipeOptions, outputPipeOptions, sslStream);

            var originalTransport = context.Transport;

            try
            {
                context.Transport = sslDuplexPipe;

                // Disposing the stream will dispose the sslDuplexPipe
                await using (sslStream)
                    await using (sslDuplexPipe)
                    {
                        await _next(context).ConfigureAwait(false);

                        // Dispose the inner stream (SslDuplexPipe) before disposing the SslStream
                        // as the duplex pipe can hit an ODE as it still may be writing.
                    }
            }
            finally
            {
                context.Transport = originalTransport;
            }
        }
예제 #7
0
 public static TlsServerProtocol ConnectPskTlsServer(TlsPskIdentityManager pskManager, Stream stream)
 {
     try
     {
         PskTlsServer      server   = new PskTlsServer2(pskManager);
         TlsServerProtocol protocol = new TlsServerProtocol(stream, new SecureRandom());
         protocol.Accept(server);
         return(protocol);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Exception in TLS protocol connnection '{0}'", ex.Message);
         throw ex;
     }
 }
예제 #8
0
파일: TLSSession.cs 프로젝트: jimsch/TCP
        /// <summary>
        /// Start up a session on the server side
        /// </summary>
        public void Accept()
        {
            TlsServerProtocol serverProtocol = new TlsServerProtocol(new SecureRandom());

            TlsServer server = new TlsServer(_signingKeys, _clientKeys);

            //  Make sure we do not startup a listing thread as the correct call is always made
            //  byt the DTLS accept protocol.

            _stm = _client.GetStream();
            serverProtocol.Accept(server);

            bool sleep = true;

            while (!server.HandshakeComplete)
            {
                sleep = true;

                if (_stm.DataAvailable)
                {
                    byte[] data   = new byte[1024];
                    int    cbRead = _stm.Read(data, 0, data.Length);
                    Array.Resize(ref data, cbRead);
                    serverProtocol.OfferInput(data);
                    sleep = false;
                }

                int cbToRead = serverProtocol.GetAvailableOutputBytes();
                if (cbToRead != 0)
                {
                    byte[] data   = new byte[cbToRead];
                    int    cbRead = serverProtocol.ReadOutput(data, 0, cbToRead);
                    _stm.Write(data, 0, cbRead);
                    sleep = false;
                }


                if (sleep)
                {
                    Thread.Sleep(100);
                }
            }

            _tlsServer = serverProtocol;
            _authKey   = server.AuthenticationKey;

            _stm.BeginRead(_buffer, 0, _buffer.Length, ReadCallback, this);
        }
예제 #9
0
            public void Run()
            {
                try
                {
                    mServerProtocol.Accept(mServerImpl);
                    Streams.PipeAll(mServerProtocol.Stream, mServerProtocol.Stream);
                    mServerProtocol.Close();
                }
                catch (Exception e)
                {
                    mCaught = e;
                    mOuter.LogException(mCaught);
                }

                WaitExit();
            }
예제 #10
0
        public static TlsServerProtocol ConnectPskTlsServerNonBlocking(Dictionary <string, byte[]> psks)
        {
            try
            {
                TlsPskIdentityManager pskTlsManager = new PskIdentityManager(psks);
                PskTlsServer2         server        = new PskTlsServer2(pskTlsManager);
                TlsServerProtocol     protocol      = new TlsServerProtocol(new SecureRandom());
                protocol.Accept(server);

                return(protocol);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in TLS protocol connnection '{0}'", ex.Message);
                throw ex;
            }
        }
예제 #11
0
 public static TlsServerProtocol ConnectPskTlsServer(this TcpClient client, TlsPskIdentityManager pskManager, Stream stream)
 {
     try
     {
         PskTlsServer      server   = new PskTlsServer2(pskManager);
         TlsServerProtocol protocol = new TlsServerProtocol(stream, new SecureRandom());
         protocol.Accept(server);
         return(protocol);
     }
     catch (AggregateException ae)
     {
         string msg = String.Format("AggregateException in TLS protocol connnection '{0}'", ae.Flatten().InnerException.Message);
         Console.WriteLine(msg);
         throw new Exception(msg, ae.Flatten().InnerException);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Exception in TLS protocol connnection '{0}'", ex.Message);
         throw ex;
     }
 }
예제 #12
0
        private IDataAdapter ConnectServerBC(IDataAdapter adapter, Logger logger, PropertyBag properties)
        {
            X509Certificate cert = null;

            // If server certificate not specified try and auto generate one
            if (!_config.SpecifyServerCert)
            {
                if (_remoteCert != null)
                {
                    cert = _remoteCert;
                }
                else
                {
                    cert = CertManager.GetCertificate("CN=localhost");
                }
            }
            else if (_config.ServerCertificate != null)
            {
                cert = _config.ServerCertificate.Certificate;
            }
            else
            {
                // Ideally shouldn't get here, but not necessarily consistent :)
                cert = CertManager.GetCertificate("CN=localhost");
            }

            DataAdapterToStream stm = new DataAdapterToStream(adapter);

            TlsServerProtocol server = new TlsServerProtocol(stm, stm, new SecureRandom());


            bool setReadTimeout = false;
            int  oldTimeout     = -1;

            try
            {
                oldTimeout      = stm.ReadTimeout;
                stm.ReadTimeout = _config.Timeout;
                setReadTimeout  = true;
            }
            catch (InvalidOperationException)
            {
            }

            X509Certificate[] certs;

            // If we have a remote chain then duplicate all certificates
            if (_remoteChain.Count > 0)
            {
                certs = _remoteChain.ToArray();
            }
            else
            {
                certs = new X509Certificate[] { cert };
            }

            // Accept with our CA key, doesn't really matter what it is but no point generating each time
            server.Accept(new CustomTlsServer(certs, CertManager.GetRootCert().PrivateKey, logger));

            if (setReadTimeout)
            {
                stm.ReadTimeout = oldTimeout;
            }

            // Return re-adapted layer
            return(new StreamDataAdapter(server.Stream, adapter.Description));
        }