Ejemplo n.º 1
0
        public override TlsAuthentication GetAuthentication()
        {
#if SUPPORT_RPK
            if (_tlsKeyPair != null && _tlsKeyPair.CertType == CertificateType.RawPublicKey)
            {
                MyTlsAuthentication auth = new MyTlsAuthentication(mContext, _tlsKeyPair);
                auth.TlsEventHandler += MyTlsEventHandler;
                return(auth);
            }
#endif
#if SUPPORT_TLS_CWT
            if (_tlsKeyPair != null && _tlsKeyPair.CertType == CertificateType.CwtPublicKey)
            {
                MyTlsAuthentication auth = new MyTlsAuthentication(mContext, _tlsKeyPair, CwtTrustKeySet);
                auth.TlsEventHandler += MyTlsEventHandler;
                return(auth);
            }
#endif
            if (_tlsKeyPair != null && _tlsKeyPair.CertType == CertificateType.X509)
            {
                MyTlsAuthentication auth = new MyTlsAuthentication(mContext, _tlsKeyPair);
                auth.TlsEventHandler += MyTlsEventHandler;
                return(auth);
            }

            throw new CoAPException("ICE");
        }
Ejemplo n.º 2
0
        public override TlsAuthentication GetAuthentication()
        {
            MyTlsAuthentication auth = new MyTlsAuthentication(mContext, _rawPublicKey);

            auth.TlsEventHandler += MyTlsEventHandler;
            return(auth);
        }
Ejemplo n.º 3
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);
        }