public static Stream WrapWithTls(Stream stream)
    {
        var client            = new MyTlsClient();
        var tlsClientProtocol = new TlsClientProtocol(stream, new SecureRandom());

        tlsClientProtocol.Connect(client);
        return(tlsClientProtocol.Stream);
    }
Ejemplo n.º 2
0
        bool TryConnect(string hostname, System.Net.IPAddress ip, int port, int connectTimeout)
        {
            //EB.Debug.Log("Try connect {0}:{1}", ip, port);

            if (_client.Client.AddressFamily != ip.AddressFamily)
            {
                _client.Close();
                _client         = new System.Net.Sockets.TcpClient(ip.AddressFamily);
                _client.NoDelay = true;
            }

            var async = _client.BeginConnect(ip, port, null, null);

            if (!async.AsyncWaitHandle.WaitOne(System.TimeSpan.FromMilliseconds(connectTimeout)))
            {
                _error = NetworkFailure.TimedOut;
                return(false);
            }
            if (!async.IsCompleted)
            {
                _error = NetworkFailure.TimedOut;
                return(false);
            }
            _client.EndConnect(async);

            if (_client.Connected == false)
            {
                EB.Debug.LogError("Failed to connect to {0}:{1}", ip, port);
                _error = NetworkFailure.CannotConnectToHost;
                return(false);
            }

            _net    = _client.GetStream();
            _stream = _net;

            OnConnected();

            if (_secure)
            {
                //EB.Debug.Log("Doing ssl connect {0}:{1}", ip, port);
                try {
                    var random = new System.Random();
                    var bytes  = new byte[20];
                    random.NextBytes(bytes);

#if BCWP71
                    var secureRandom = new SecureRandom(bytes);
#else
                    var secureRandom = SecureRandom.GetInstance("SHA1PRNG", false);
#endif
                    secureRandom.SetSeed(bytes);

                    _auth      = new MyTlsAuthentication();
                    _tlsClient = new MyTlsClient(_auth);
#if BCWP71
                    _handler = new TlsProtocolHandler(_net, secureRandom);
#else
                    _handler = new TlsClientProtocol(_net, secureRandom);
#endif
                    _handler.Connect(_tlsClient);
                    _stream = _handler.Stream;
                    if (_stream == null)
                    {
                        EB.Debug.LogError("stream is null");
                        _error = NetworkFailure.SecureConnectionFailed;
                        return(false);
                    }
                }
                catch (System.Exception ex)
                {
                    EB.Debug.LogError("ssl connect failed {0}\n{1}", ex.Message, ex.StackTrace);
                    _error = NetworkFailure.SecureConnectionFailed;
                    return(false);
                }
            }

            //EB.Debug.Log("Connected to {0}:{1}", ip, port);

            LastTime = System.DateTime.Now;

            return(true);
        }