示例#1
0
 /// <summary>
 /// Stop the current session
 /// </summary>
 public void Stop()
 {
     if (_dtlsSession != null) {
         _dtlsSession.Close();
         EventHandler<SessionEventArgs> h = SessionEvent;
         if (h != null) {
             SessionEventArgs thisEvent = new SessionEventArgs(SessionEventArgs.SessionEvent.Closed, this);
             h(this, thisEvent);
         }
         _dtlsSession = null;
     }
     _client = null;
 }
示例#2
0
        /// <summary>
        /// Create the DTLS connection over a specific UDP channel.
        /// We already know what address we are going to use
        /// </summary>
        /// <param name="udpChannel">UDP channel to use</param>
        public void Connect(UDPChannel udpChannel)
        {
#if SUPPORT_TLS_CWT
            if (CwtTrustKeySet != null)
            {
                _client = new DtlsClient(null, _userKey, CwtTrustKeySet);
            }
            else
            {
#endif
            if (_userKey.PrivateKey.HasKeyType((int)GeneralValuesInt.KeyType_Octet))
            {
                CBORObject kid = _userKey.PrivateKey[CoseKeyKeys.KeyIdentifier];

                BasicTlsPskIdentity pskIdentity;
                pskIdentity = new BasicTlsPskIdentity(kid != null ? kid.GetByteString() : new byte[0],
                                                      _userKey.PrivateKey[CoseKeyParameterKeys.Octet_k].GetByteString());
                _client = new DtlsClient(null, pskIdentity);
            }
            else if (_userKey.PrivateKey.HasKeyType((int)GeneralValuesInt.KeyType_EC2))
            {
                _client = new DtlsClient(null, _userKey);
            }
#if SUPPORT_TLS_CWT
        }
#endif

            _client.TlsEventHandler += OnTlsEvent;

            DtlsClientProtocol clientProtocol = new DtlsClientProtocol(new SecureRandom());

            _transport.UDPChannel = udpChannel;
            AuthenticationKey     = _userKey.PrivateKey;

            _listening = 1;
            DtlsTransport dtlsClient = clientProtocol.Connect(_client, _transport);
            _listening   = 0;
            _dtlsSession = dtlsClient;

            //  We are now in the world of a connected system -
            //  We need to do the receive calls

            new Thread(StartListen).Start();
        }