Example #1
0
        public void Connect()
        {
#if SUPPORT_TLS_CWT
            if (CwtTrustKeySet != null)
            {
                _tlsSession = new TLSClient(null, _userKey, CwtTrustKeySet);
            }
            else
            {
#endif
            if (_userKey.PrivateKey.HasKeyType((int)COSE.GeneralValuesInt.KeyType_Octet))
            {
                CBORObject kid = _userKey.PrivateKey[COSE.CoseKeyKeys.KeyIdentifier];

                BasicTlsPskIdentity pskIdentity = null;
                if (kid != null)
                {
                    pskIdentity = new BasicTlsPskIdentity(kid.GetByteString(), _userKey.PrivateKey[CoseKeyParameterKeys.Octet_k].GetByteString());
                }
                else
                {
                    pskIdentity = new BasicTlsPskIdentity(new byte[0], _userKey.PrivateKey[CoseKeyParameterKeys.Octet_k].GetByteString());
                }
                _tlsSession = new TLSClient(null, pskIdentity);
            }
            else if (_userKey.PrivateKey.HasKeyType((int)COSE.GeneralValuesInt.KeyType_EC2))
            {
                _tlsSession = new TLSClient(null, _userKey);
            }
#if SUPPORT_TLS_CWT
        }
#endif
            _tlsSession.TlsEventHandler += OnTlsEvent;

            _authKey = _userKey.PrivateKey;

            TlsClientProtocol clientProtocol = new TlsClientProtocol(new SecureRandom());

            _tcpClient = new TcpClient(_ipEndPoint.AddressFamily);

            _tcpClient.Connect(_ipEndPoint);
            _tcpStream = _tcpClient.GetStream();

            clientProtocol.Connect(_tlsSession);

            while (_tlsSession.InHandshake)
            {
                bool sleep    = true;
                int  cbToRead = clientProtocol.GetAvailableOutputBytes();
                if (cbToRead != 0)
                {
                    byte[] data   = new byte[cbToRead];
                    int    cbRead = clientProtocol.ReadOutput(data, 0, cbToRead);
                    _tcpStream.Write(data, 0, cbRead);
                    sleep = false;
                }

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

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

            _tlsClient = clientProtocol;

            //  Send over the capability block

            SendCSMSignal();

            //

            if (_toSend != null)
            {
                _queue.Enqueue(_toSend);
                _toSend = null;
            }

            BeginRead();

            WriteData();
        }
Example #2
0
 public TcpSession(IPEndPoint ipEndPoint, QueueItem toSend)
 {
     _ipEndPoint = ipEndPoint;
     _toSend     = toSend;
 }
Example #3
0
 public TLSSession(IPEndPoint ipEndPoint, QueueItem toSend, TlsKeyPair tlsKey)
 {
     _ipEndPoint = ipEndPoint;
     _toSend     = toSend;
     _userKey    = tlsKey;
 }