/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="peerConnection">The WebRTC session to perform the DTLS handshake on.</param> /// <returns>True if the handshake completes successfully. False if not.</returns> private static async Task <bool> DoDtlsHandshake(RTCPeerConnection peerConnection, DtlsHandshake dtls) { Console.WriteLine("DoDtlsHandshake started."); int res = dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle); Console.WriteLine("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { Console.WriteLine("DTLS negotiation complete."); // TODO fix race condition!!! First RTP packet is not getting decrypted. var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); await peerConnection.Start(); return(true); } else { return(false); } }
/// <summary> /// Runs a DTLS handshake test between two threads on a loopback address. The main motivation for /// this test was that the first DTLS handshake between this application and a client browser /// was often substantially slower and occasionally failed. By doing a loopback test the idea /// is that the internal OpenSSL state is initialised. /// </summary> private void DoDtlsHandshakeLoopbackTest() { IPAddress testAddr = IPAddress.Loopback; Socket svrSock = new Socket(testAddr.AddressFamily, SocketType.Dgram, ProtocolType.Udp); svrSock.Bind(new IPEndPoint(testAddr, 9000)); int svrPort = ((IPEndPoint)svrSock.LocalEndPoint).Port; DtlsHandshake svrHandshake = new DtlsHandshake(_dtlsCertificatePath, _dtlsKeyPath); //svrHandshake.Debug = true; var svrTask = Task.Run(() => svrHandshake.DoHandshakeAsServer((ulong)svrSock.Handle)); Socket cliSock = new Socket(testAddr.AddressFamily, SocketType.Dgram, ProtocolType.Udp); cliSock.Bind(new IPEndPoint(testAddr, 0)); cliSock.Connect(testAddr, svrPort); DtlsHandshake cliHandshake = new DtlsHandshake(); //cliHandshake.Debug = true; var cliTask = Task.Run(() => cliHandshake.DoHandshakeAsClient((ulong)cliSock.Handle, (short)testAddr.AddressFamily, testAddr.GetAddressBytes(), (ushort)svrPort)); bool result = Task.WaitAll(new Task[] { svrTask, cliTask }, TEST_DTLS_HANDSHAKE_TIMEOUT); cliHandshake.Shutdown(); svrHandshake.Shutdown(); cliSock.Close(); svrSock.Close(); }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> /// <returns>True if the handshake completes successfully. False if not.</returns> private static bool DoDtlsHandshake(WebRtcSession webRtcSession) { Console.WriteLine("DoDtlsHandshake started."); var dtls = new DtlsHandshake(DTLS_CERTIFICATE_PATH, DTLS_KEY_PATH); webRtcSession.OnClose += (reason) => dtls.Shutdown(); int res = dtls.DoHandshakeAsServer((ulong)webRtcSession.RtpSession.RtpChannel.RtpSocket.Handle); Console.WriteLine("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { Console.WriteLine("DTLS negotiation complete."); // TODO fix race condition!!! First RTP packet is not getting decrypted. var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); webRtcSession.RtpSession.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); return(true); } else { return(false); } }
/// <summary> /// Runs a DTLS handshake test between two threads on a loopback address. The main motivation for /// this test was that the first DTLS handshake between this application and a client browser /// was often substantially slower and occasionally failed. By doing a loopback test the idea /// is that the internal OpenSSL state is initialised. /// </summary> private static void DoDtlsHandshakeLoopbackTest() { IPAddress testAddr = IPAddress.Loopback; //var dtlsFingerprint = DTLS_CERTIFICATE_FINGERPRINT.Substring(DTLS_CERTIFICATE_FINGERPRINT.Length + 1).Trim().Replace(":", "").Replace(" ", ""); //var dtlsFingerprintBuffer = SIPSorcery.Sys.ByteBufferInfo.ParseHexStr(dtlsFingerprint); Socket svrSock = new Socket(testAddr.AddressFamily, SocketType.Dgram, ProtocolType.Udp); svrSock.Bind(new IPEndPoint(testAddr, 9000)); int svrPort = ((IPEndPoint)svrSock.LocalEndPoint).Port; DtlsHandshake svrHandshake = new DtlsHandshake(DTLS_CERTIFICATE_PATH, DTLS_KEY_PATH); svrHandshake.Debug = true; byte[] clientFingerprint = null; var svrTask = Task.Run(() => svrHandshake.DoHandshakeAsServer((ulong)svrSock.Handle, ref clientFingerprint)); Socket cliSock = new Socket(testAddr.AddressFamily, SocketType.Dgram, ProtocolType.Udp); cliSock.Bind(new IPEndPoint(testAddr, 0)); cliSock.Connect(testAddr, svrPort); DtlsHandshake cliHandshake = new DtlsHandshake(); cliHandshake.Debug = true; byte[] serverFingerprint = null; var cliTask = Task.Run(() => cliHandshake.DoHandshakeAsClient((ulong)cliSock.Handle, (short)testAddr.AddressFamily, testAddr.GetAddressBytes(), (ushort)svrPort, ref serverFingerprint)); bool result = Task.WaitAll(new Task[] { svrTask, cliTask }, TEST_DTLS_HANDSHAKE_TIMEOUT); cliHandshake.Shutdown(); svrHandshake.Shutdown(); cliSock.Close(); svrSock.Close(); }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> /// <returns>True if the handshake completed successfully or false otherwise.</returns> private static async Task <bool> DoDtlsHandshake(RTCPeerConnection peerConnection, DtlsHandshake dtls) { logger.LogDebug("DoDtlsHandshake started."); int res = dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle); logger.LogDebug("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { logger.LogDebug("DTLS negotiation complete."); var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); await peerConnection.Start(); if (!_isSampling) { _ = Task.Run(StartMedia); } return(true); } else { return(false); } }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> /// <returns>True if the handshake completed successfully or false otherwise.</returns> private static bool DoDtlsHandshake(RTCPeerConnection peerConnection) { logger.LogDebug("DoDtlsHandshake started."); var dtls = new DtlsHandshake(DTLS_CERTIFICATE_PATH, DTLS_KEY_PATH); int res = dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle); logger.LogDebug("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { logger.LogDebug("DTLS negotiation complete."); var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); dtls.Shutdown(); return(true); } else { dtls.Shutdown(); return(false); } }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> /// <returns>True if the handshake completed successfully or false otherwise.</returns> private bool DoDtlsHandshake(WebRtcSession webRtcSession) { try { logger.LogDebug("DoDtlsHandshake started."); if (!File.Exists(_dtlsCertificatePath)) { throw new ApplicationException($"The DTLS certificate file could not be found at {_dtlsCertificatePath}."); } else if (!File.Exists(_dtlsKeyPath)) { throw new ApplicationException($"The DTLS key file could not be found at {_dtlsKeyPath}."); } var dtls = new DtlsHandshake(_dtlsCertificatePath, _dtlsKeyPath); webRtcSession.OnClose += (reason) => dtls.Shutdown(); int res = dtls.DoHandshakeAsServer((ulong)webRtcSession.RtpSession.RtpChannel.RtpSocket.Handle); logger.LogDebug("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { logger.LogDebug("DTLS negotiation complete."); var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); webRtcSession.RtpSession.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); webRtcSession.IsDtlsNegotiationComplete = true; return(true); } else { return(false); } } catch (Exception excp) { logger.LogWarning($"Exception DoDtlsHandshake. {excp}"); return(false); } }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> private static bool DoDtlsHandshake(RTCPeerConnection peerConnection) { Log.LogDebug("DoDtlsHandshake started."); if (!File.Exists(DTLS_CERTIFICATE_PATH)) { throw new ApplicationException($"The DTLS certificate file could not be found at {DTLS_CERTIFICATE_PATH}."); } else if (!File.Exists(DTLS_KEY_PATH)) { throw new ApplicationException($"The DTLS key file could not be found at {DTLS_KEY_PATH}."); } var dtls = new DtlsHandshake(DTLS_CERTIFICATE_PATH, DTLS_KEY_PATH); peerConnection.onconnectionstatechange += (state) => { if (state == RTCPeerConnectionState.closed) { dtls.Shutdown(); } }; byte[] clientFingerprint = null; int res = dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle, ref clientFingerprint); Log.LogDebug("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { // TODO: Check client fingerprint matches one supplied in the SDP. Log.LogDebug("DTLS negotiation complete."); var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); return(true); } else { return(false); } }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> private static async Task <bool> DoDtlsHandshake(RTCPeerConnection peerConnection, DtlsHandshake dtls) { logger.LogDebug("DoDtlsHandshake started."); if (!File.Exists(DTLS_CERTIFICATE_PATH)) { throw new ApplicationException($"The DTLS certificate file could not be found at {DTLS_CERTIFICATE_PATH}."); } else if (!File.Exists(DTLS_KEY_PATH)) { throw new ApplicationException($"The DTLS key file could not be found at {DTLS_KEY_PATH}."); } byte[] clientFingerprint = null; int res = dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle, ref clientFingerprint); logger.LogDebug("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { logger.LogDebug("DTLS negotiation complete."); // TODO: Check client fingerprint matches one supplied in the SDP. var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); await peerConnection.Start(); //dtls.Shutdown(); if (_sendTestPatternTimer == null) { _sendTestPatternTimer = new Timer(SendTestPattern, null, 0, TEST_PATTERN_SPACING_MILLISECONDS); } return(true); } else { return(false); } }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> private static async Task <bool> DoDtlsHandshake(RTCPeerConnection peerConnection) { logger.LogDebug("DoDtlsHandshake started."); if (!File.Exists(DTLS_CERTIFICATE_PATH)) { throw new ApplicationException($"The DTLS certificate file could not be found at {DTLS_CERTIFICATE_PATH}."); } else if (!File.Exists(DTLS_KEY_PATH)) { throw new ApplicationException($"The DTLS key file could not be found at {DTLS_KEY_PATH}."); } var dtls = new DtlsHandshake(DTLS_CERTIFICATE_PATH, DTLS_KEY_PATH); byte[] clientFingerprint = null; var dtlsResult = await Task.Run(() => dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle, ref clientFingerprint)); logger.LogDebug("DtlsContext initialisation result=" + dtlsResult); if (dtls.IsHandshakeComplete()) { logger.LogDebug("DTLS handshake succeeded."); // TODO: Check client fingerprint matches one supplied in the SDP. var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); return(true); } else { logger.LogWarning("DTLS handshake failed."); dtls.Shutdown(); return(false); } }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> private static int DoDtlsHandshake(WebRtcSession webRtcSession) { logger.LogDebug("DoDtlsHandshake started."); if (!File.Exists(DTLS_CERTIFICATE_PATH)) { throw new ApplicationException($"The DTLS certificate file could not be found at {DTLS_CERTIFICATE_PATH}."); } else if (!File.Exists(DTLS_KEY_PATH)) { throw new ApplicationException($"The DTLS key file could not be found at {DTLS_KEY_PATH}."); } var dtls = new DtlsHandshake(DTLS_CERTIFICATE_PATH, DTLS_KEY_PATH); webRtcSession.OnClose += (reason) => dtls.Shutdown(); int res = dtls.DoHandshakeAsServer((ulong)webRtcSession.RtpSession.RtpChannel.RtpSocket.Handle); logger.LogDebug("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { logger.LogDebug("DTLS negotiation complete."); var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); webRtcSession.RtpSession.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); if (!_isSampling) { Task.Run(StartMedia); } } return(res); }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> private static bool DoDtlsHandshake(RTCPeerConnection peerConnection, DtlsHandshake dtls) { Log.LogDebug("DoDtlsHandshake started."); if (!File.Exists(DTLS_CERTIFICATE_PATH)) { throw new ApplicationException($"The DTLS certificate file could not be found at {DTLS_CERTIFICATE_PATH}."); } else if (!File.Exists(DTLS_KEY_PATH)) { throw new ApplicationException($"The DTLS key file could not be found at {DTLS_KEY_PATH}."); } byte[] clientFingerprint = null; var dtlsResult = dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle, ref clientFingerprint); Log.LogDebug($"DtlsContext initialisation result {dtlsResult}."); if (dtls.IsHandshakeComplete()) { Log.LogDebug("DTLS negotiation complete."); var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); return(true); } else { Log.LogWarning("DTLS handshake failed."); dtls.Shutdown(); return(false); } }
/// <summary> /// Hands the socket handle to the DTLS context and waits for the handshake to complete. /// </summary> /// <param name="webRtcSession">The WebRTC session to perform the DTLS handshake on.</param> private static async Task <bool> DoDtlsHandshake(RTCPeerConnection peerConnection, DtlsHandshake dtls) { logger.LogDebug("DoDtlsHandshake started."); if (!File.Exists(DTLS_CERTIFICATE_PATH)) { throw new ApplicationException($"The DTLS certificate file could not be found at {DTLS_CERTIFICATE_PATH}."); } else if (!File.Exists(DTLS_KEY_PATH)) { throw new ApplicationException($"The DTLS key file could not be found at {DTLS_KEY_PATH}."); } int res = dtls.DoHandshakeAsServer((ulong)peerConnection.GetRtpChannel(SDPMediaTypesEnum.audio).RtpSocket.Handle); logger.LogDebug("DtlsContext initialisation result=" + res); if (dtls.IsHandshakeComplete()) { logger.LogDebug("DTLS negotiation complete."); var srtpSendContext = new Srtp(dtls, false); var srtpReceiveContext = new Srtp(dtls, true); peerConnection.SetSecurityContext( srtpSendContext.ProtectRTP, srtpReceiveContext.UnprotectRTP, srtpSendContext.ProtectRTCP, srtpReceiveContext.UnprotectRTCP); //dtls.Shutdown(); return(true); } else { return(false); } }