Exemple #1
0
        /// <summary>
        /// Start up a session on the server side
        /// </summary>
        /// <param name="udpChannel">What channel are we on</param>
        /// <param name="message">What was the last message we got?</param>
        public void Accept(UDPChannel udpChannel, byte[] message)
        {
            DtlsServerProtocol serverProtocol = new DtlsServerProtocol(new SecureRandom());

            DtlsServer server = new DtlsServer(_serverKeys, _userKeys);

            server.TlsEventHandler += OnTlsEvent;
#if SUPPORT_TLS_CWT
            server.CwtTrustKeySet = CwtTrustKeySet;
#endif

            _transport.UDPChannel = udpChannel;
            _transport.Receive(message);

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


            _listening = 1;
            DtlsTransport dtlsServer = serverProtocol.Accept(server, _transport);
            _listening = 0;

            _dtlsSession              = dtlsServer;
            AuthenticationKey         = server.AuthenticationKey;
            AuthenticationCertificate = server.AuthenticationCertificate;

            new Thread(StartListen).Start();
        }
Exemple #2
0
        public void Accept(DtlsServerProtocol serverProtocol, TlsServer server)
        {
            _dtlsTransport = serverProtocol.Accept(server, _udpTransport);

            if (server is IDtlsServerWithConnectionInfo serverWithInfo)
            {
                var serverInfo = serverWithInfo.GetConnectionInfo();
                ConnectionInfo = serverInfo;
            }
        }
Exemple #3
0
 public DtlsTransport Accept()
 {
     try
     {
         DtlsTransport dtlsTransport = dtlsServerProtocol.Accept(tlsServer, DatagramTransport);
         HandshakeComplete?.Invoke(this, new HandshakeCompleteEventArgs(tlsServer.ChosenSrtpProtectionProfile, TlsRole.Server, tlsServer.SrtpKeyingMaterial));
         return(dtlsTransport);
     }
     catch (System.Exception e)
     {
         throw e;
     }
 }
Exemple #4
0
        public bool DoHandshakeAsServer()
        {
            logger.LogDebug("DTLS commencing handshake as server.");

            if (!handshaking && !handshakeComplete)
            {
                this.startTime   = DateTime.Now;
                this.handshaking = true;
                SecureRandom       secureRandom   = new SecureRandom();
                DtlsServerProtocol serverProtocol = new DtlsServerProtocol(secureRandom);
                try
                {
                    var server = (DtlsSrtpServer)connection;

                    // Perform the handshake in a non-blocking fashion
                    Transport = serverProtocol.Accept(server, this);
                    // Prepare the shared key to be used in RTP streaming
                    //server.PrepareSrtpSharedSecret();
                    // Generate encoders for DTLS traffic
                    if (server.GetSrtpPolicy() != null)
                    {
                        srtpDecoder  = GenerateRtpDecoder();
                        srtpEncoder  = GenerateRtpEncoder();
                        srtcpDecoder = GenerateRtcpDecoder();
                        srtcpEncoder = GenerateRtcpEncoder();
                    }

                    // Declare handshake as complete
                    handshakeComplete = true;
                    handshakeFailed   = false;
                    handshaking       = false;
                    // Warn listeners handshake completed
                    //UnityEngine.Debug.Log("DTLS Handshake Completed");
                    return(true);
                }
                catch (Exception excp)
                {
                    logger.LogWarning($"DTLS handshake as server failed. {excp.Message}");

                    // Declare handshake as failed
                    handshakeComplete = false;
                    handshakeFailed   = true;
                    handshaking       = false;
                    // Warn listeners handshake completed
                    //UnityEngine.Debug.Log("DTLS Handshake failed\n"+ e);
                }
            }

            return(false);
        }
Exemple #5
0
 public DtlsClient Accept()
 {
     while (true)
     {
         if (_acceptQueue.TryDequeue(out var udpTransport))
         {
             var random        = new SecureRandom();
             var protocol      = new DtlsServerProtocol(random);
             var server        = new TlsServerImpl(ProtocolVersion.DTLSv12);
             var dtlsTransport = protocol.Accept(server, udpTransport);
             var client        = new DtlsClient(_socket, dtlsTransport);
             return(client);
         }
     }
 }
Exemple #6
0
 public void Run()
 {
     try
     {
         DtlsTransport dtlsServer = mServerProtocol.Accept(mServerImpl, mServerTransport);
         byte[]        buf        = new byte[dtlsServer.GetReceiveLimit()];
         while (!isShutdown)
         {
             int length = dtlsServer.Receive(buf, 0, buf.Length, 100);
             if (length >= 0)
             {
                 dtlsServer.Send(buf, 0, length);
             }
         }
         dtlsServer.Close();
     }
     catch (Exception e)
     {
         mCaught = e;
         mOuter.LogException(mCaught);
     }
 }
 public void Run()
 {
     try
     {
         MockDtlsServer server     = new MockDtlsServer();
         DtlsTransport  dtlsServer = mServerProtocol.Accept(server, mServerTransport);
         byte[]         buf        = new byte[dtlsServer.GetReceiveLimit()];
         while (!isShutdown)
         {
             int length = dtlsServer.Receive(buf, 0, buf.Length, 1000);
             if (length >= 0)
             {
                 dtlsServer.Send(buf, 0, length);
             }
         }
         dtlsServer.Close();
     }
     catch (Exception e)
     {
         Console.Error.WriteLine(e.StackTrace);
     }
 }
        private bool DoHandshakeAsServer(out string handshakeError)
        {
            handshakeError = null;

            logger.LogDebug("DTLS commencing handshake as server.");

            if (!_handshaking && !_handshakeComplete)
            {
                this._waitMillis  = RetransmissionMilliseconds;
                this._startTime   = System.DateTime.Now;
                this._handshaking = true;
                SecureRandom       secureRandom   = new SecureRandom();
                DtlsServerProtocol serverProtocol = new DtlsServerProtocol(secureRandom);
                try
                {
                    var server = (DtlsSrtpServer)connection;

                    // Perform the handshake in a non-blocking fashion
                    Transport = serverProtocol.Accept(server, this);
                    // Prepare the shared key to be used in RTP streaming
                    //server.PrepareSrtpSharedSecret();
                    // Generate encoders for DTLS traffic
                    if (server.GetSrtpPolicy() != null)
                    {
                        srtpDecoder  = GenerateRtpDecoder();
                        srtpEncoder  = GenerateRtpEncoder();
                        srtcpDecoder = GenerateRtcpDecoder();
                        srtcpEncoder = GenerateRtcpEncoder();
                    }
                    // Declare handshake as complete
                    _handshakeComplete = true;
                    _handshakeFailed   = false;
                    _handshaking       = false;
                    // Warn listeners handshake completed
                    //UnityEngine.Debug.Log("DTLS Handshake Completed");
                    return(true);
                }
                catch (System.Exception excp)
                {
                    if (excp.InnerException is TimeoutException)
                    {
                        logger.LogWarning(excp, $"DTLS handshake as server timed out waiting for handshake to complete.");
                        handshakeError = "timeout";
                    }
                    else
                    {
                        handshakeError = "unknown";
                        if (excp is Org.BouncyCastle.Crypto.Tls.TlsFatalAlert)
                        {
                            handshakeError = (excp as Org.BouncyCastle.Crypto.Tls.TlsFatalAlert).Message;
                        }

                        logger.LogWarning(excp, $"DTLS handshake as server failed. {excp.Message}");
                    }

                    // Declare handshake as failed
                    _handshakeComplete = false;
                    _handshakeFailed   = true;
                    _handshaking       = false;
                    // Warn listeners handshake completed
                    //UnityEngine.Debug.Log("DTLS Handshake failed\n"+ e);
                }
            }
            return(false);
        }