Enable a debug WebRTC mode without any encryption
https://bugs.webkit.org/show_bug.cgi?id=199177
<rdar://problem/52074986>

Reviewed by Eric Carlson.

Source/JavaScriptCore:

* inspector/protocol/Page.json:

Source/ThirdParty/libwebrtc:

* Configurations/libwebrtc.iOS.exp:
* Configurations/libwebrtc.iOSsim.exp:
* Configurations/libwebrtc.mac.exp:

Source/WebCore:

For every RTCPeerConnection, first set whether to use encryption or not
based on page settings.
If encryption is disabled, log it.
Add internals API to toggle the switch from tests.
Test: webrtc/disable-encryption.html

* Modules/mediastream/RTCPeerConnection.cpp:
(WebCore::RTCPeerConnection::RTCPeerConnection):
* Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp:
(WebCore::createLibWebRTCPeerConnectionBackend):
* inspector/agents/InspectorPageAgent.cpp:
* page/Settings.yaml:
* platform/mediastream/libwebrtc/LibWebRTCProvider.cpp:
(WebCore::LibWebRTCProvider::setEnableWebRTCEncryption):
* platform/mediastream/libwebrtc/LibWebRTCProvider.h:
* testing/Internals.cpp:
(WebCore::Internals::resetToConsistentState):
(WebCore::Internals::setEnableWebRTCEncryption):
* testing/Internals.h:
* testing/Internals.idl:

Source/WebInspectorUI:

* Localizations/en.lproj/localizedStrings.js:
* UserInterface/Base/Main.js:

LayoutTests:

* webrtc/disable-encryption-expected.txt: Added.
* webrtc/disable-encryption.html: Added.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@247438 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index cbf7b88..8fcb678 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,5 +1,16 @@
 2019-07-15  Youenn Fablet  <youenn@apple.com>
 
+        Enable a debug WebRTC mode without any encryption
+        https://bugs.webkit.org/show_bug.cgi?id=199177
+        <rdar://problem/52074986>
+
+        Reviewed by Eric Carlson.
+
+        * webrtc/disable-encryption-expected.txt: Added.
+        * webrtc/disable-encryption.html: Added.
+
+2019-07-15  Youenn Fablet  <youenn@apple.com>
+
         Filter SDP c lines
         https://bugs.webkit.org/show_bug.cgi?id=199791
 
diff --git a/LayoutTests/webrtc/disable-encryption-expected.txt b/LayoutTests/webrtc/disable-encryption-expected.txt
new file mode 100644
index 0000000..ddd4b8c
--- /dev/null
+++ b/LayoutTests/webrtc/disable-encryption-expected.txt
@@ -0,0 +1,5 @@
+
+
+PASS Basic data channel exchange without encryption 
+PASS Make sure a pc with encryption and a pc without encryption cannot talk 
+
diff --git a/LayoutTests/webrtc/disable-encryption.html b/LayoutTests/webrtc/disable-encryption.html
new file mode 100644
index 0000000..6749bf9
--- /dev/null
+++ b/LayoutTests/webrtc/disable-encryption.html
@@ -0,0 +1,50 @@
+<!doctype html>
+<html>
+  <head>
+    <meta charset="utf-8">
+    <title>Testing connection with and without encryption</title>
+    <script src="../resources/testharness.js"></script>
+    <script src="../resources/testharnessreport.js"></script>
+  </head>
+  <body>
+    <video id="video" autoplay playsinline></video>
+    <script src ="routines.js"></script>
+    <script>
+promise_test(async (test) => {
+    if (window.internals)
+        internals.setEnableWebRTCEncryption(false);
+    const localStream = await navigator.mediaDevices.getUserMedia({video: true});
+
+    video.srcObject = await new Promise((resolve, reject) => {
+        createConnections((localConnection) => {
+            localConnection.addTrack(localStream.getVideoTracks()[0], localStream);
+        }, (remoteConnection) => {
+            remoteConnection.ontrack = (event) => {
+                resolve(event.streams[0]);
+            };
+        });
+        setTimeout(() => { reject("Test timed out"); }, 5000);
+    });
+
+    await video.play();
+}, "Basic data channel exchange without encryption");
+
+promise_test(async (test) => {
+    if (!window.internals)
+        return Promise.rejects("Test needs internals");
+
+    internals.setEnableWebRTCEncryption(false);
+    const pc1 = new RTCPeerConnection();
+
+    internals.setEnableWebRTCEncryption(true);
+    const pc2 = new RTCPeerConnection();
+
+    pc1.addTransceiver('audio');
+    const offer = await pc1.createOffer();
+    await pc1.setLocalDescription(offer);
+
+    return promise_rejects(test, 'InvalidAccessError', pc2.setRemoteDescription(offer));
+}, "Make sure a pc with encryption and a pc without encryption cannot talk");
+    </script>
+  </body>
+</html>
diff --git a/Source/JavaScriptCore/ChangeLog b/Source/JavaScriptCore/ChangeLog
index 0aaec2e..73d0122 100644
--- a/Source/JavaScriptCore/ChangeLog
+++ b/Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,13 @@
+2019-07-15  Youenn Fablet  <youenn@apple.com>
+
+        Enable a debug WebRTC mode without any encryption
+        https://bugs.webkit.org/show_bug.cgi?id=199177
+        <rdar://problem/52074986>
+
+        Reviewed by Eric Carlson.
+
+        * inspector/protocol/Page.json:
+
 2019-07-15  Ryan Haddad  <ryanhaddad@apple.com>
 
         Unreviewed, attempt to fix production builds after r247403.
diff --git a/Source/JavaScriptCore/inspector/protocol/Page.json b/Source/JavaScriptCore/inspector/protocol/Page.json
index 769bf4b..8c64cfc 100644
--- a/Source/JavaScriptCore/inspector/protocol/Page.json
+++ b/Source/JavaScriptCore/inspector/protocol/Page.json
@@ -15,6 +15,7 @@
                 "MockCaptureDevicesEnabled",
                 "NeedsSiteSpecificQuirks",
                 "ScriptEnabled",
+                "WebRTCEncryptionEnabled",
                 "WebSecurityEnabled"
             ]
         },
diff --git a/Source/ThirdParty/libwebrtc/ChangeLog b/Source/ThirdParty/libwebrtc/ChangeLog
index 18a01aa..03bd700 100644
--- a/Source/ThirdParty/libwebrtc/ChangeLog
+++ b/Source/ThirdParty/libwebrtc/ChangeLog
@@ -1,3 +1,15 @@
+2019-07-15  Youenn Fablet  <youenn@apple.com>
+
+        Enable a debug WebRTC mode without any encryption
+        https://bugs.webkit.org/show_bug.cgi?id=199177
+        <rdar://problem/52074986>
+
+        Reviewed by Eric Carlson.
+
+        * Configurations/libwebrtc.iOS.exp:
+        * Configurations/libwebrtc.iOSsim.exp:
+        * Configurations/libwebrtc.mac.exp:
+
 2019-06-28  Dean Jackson  <dino@apple.com>
 
         unable to build WebRTC for iOS Simulator
diff --git a/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOS.exp b/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOS.exp
index 2b06ca8..646d8d4 100644
--- a/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOS.exp
+++ b/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOS.exp
@@ -247,3 +247,4 @@
 __ZN6webrtc16RtpRtxParametersC1ERKS0_
 __ZN6webrtc16RtpRtxParametersD1Ev
 __ZN3rtc10LogMessage12SetLogOutputENS_15LoggingSeverityEPFvS1_PKcE
+__ZN6webrtc13CryptoOptions5NoGcmEv
diff --git a/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOSsim.exp b/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOSsim.exp
index 6c366cf..980c3f4 100644
--- a/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOSsim.exp
+++ b/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.iOSsim.exp
@@ -248,3 +248,4 @@
 __ZN6webrtc16RtpRtxParametersC1ERKS0_
 __ZN6webrtc16RtpRtxParametersD1Ev
 __ZN3rtc10LogMessage12SetLogOutputENS_15LoggingSeverityEPFvS1_PKcE
+__ZN6webrtc13CryptoOptions5NoGcmEv
diff --git a/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.mac.exp b/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.mac.exp
index 6c366cf..980c3f4 100644
--- a/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.mac.exp
+++ b/Source/ThirdParty/libwebrtc/Configurations/libwebrtc.mac.exp
@@ -248,3 +248,4 @@
 __ZN6webrtc16RtpRtxParametersC1ERKS0_
 __ZN6webrtc16RtpRtxParametersD1Ev
 __ZN3rtc10LogMessage12SetLogOutputENS_15LoggingSeverityEPFvS1_PKcE
+__ZN6webrtc13CryptoOptions5NoGcmEv
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index eb1a3f7..3852c86 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,32 @@
+2019-07-15  Youenn Fablet  <youenn@apple.com>
+
+        Enable a debug WebRTC mode without any encryption
+        https://bugs.webkit.org/show_bug.cgi?id=199177
+        <rdar://problem/52074986>
+
+        Reviewed by Eric Carlson.
+
+        For every RTCPeerConnection, first set whether to use encryption or not
+        based on page settings.
+        If encryption is disabled, log it.
+        Add internals API to toggle the switch from tests.
+        Test: webrtc/disable-encryption.html
+
+        * Modules/mediastream/RTCPeerConnection.cpp:
+        (WebCore::RTCPeerConnection::RTCPeerConnection):
+        * Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp:
+        (WebCore::createLibWebRTCPeerConnectionBackend):
+        * inspector/agents/InspectorPageAgent.cpp:
+        * page/Settings.yaml:
+        * platform/mediastream/libwebrtc/LibWebRTCProvider.cpp:
+        (WebCore::LibWebRTCProvider::setEnableWebRTCEncryption):
+        * platform/mediastream/libwebrtc/LibWebRTCProvider.h:
+        * testing/Internals.cpp:
+        (WebCore::Internals::resetToConsistentState):
+        (WebCore::Internals::setEnableWebRTCEncryption):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
 2019-07-15  Sihui Liu  <sihui_liu@apple.com>
 
         window.openDatabase is not writable
diff --git a/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp b/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp
index 8713fdd..e439186 100644
--- a/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp
+++ b/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp
@@ -52,6 +52,7 @@
 #include "RTCIceCandidate.h"
 #include "RTCPeerConnectionIceEvent.h"
 #include "RTCSessionDescription.h"
+#include "Settings.h"
 #include <wtf/CryptographicallyRandomNumber.h>
 #include <wtf/IsoMallocInlines.h>
 #include <wtf/MainThread.h>
@@ -89,6 +90,13 @@
     , m_backend(PeerConnectionBackend::create(*this))
 {
     ALWAYS_LOG(LOGIDENTIFIER);
+
+#if !RELEASE_LOG_DISABLED
+    auto* page = downcast<Document>(context).page();
+    if (page && !page->settings().webRTCEncryptionEnabled())
+        ALWAYS_LOG(LOGIDENTIFIER, "encryption is disabled");
+#endif
+
     if (!m_backend)
         m_connectionState = RTCPeerConnectionState::Closed;
 }
diff --git a/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp b/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp
index 851fdde..207f15e 100644
--- a/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp
+++ b/Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCPeerConnectionBackend.cpp
@@ -46,6 +46,7 @@
 #include "RealtimeOutgoingAudioSource.h"
 #include "RealtimeOutgoingVideoSource.h"
 #include "RuntimeEnabledFeatures.h"
+#include "Settings.h"
 
 namespace WebCore {
 
@@ -58,6 +59,8 @@
     if (!page)
         return nullptr;
 
+    page->libWebRTCProvider().setEnableWebRTCEncryption(page->settings().webRTCEncryptionEnabled());
+
     return std::make_unique<LibWebRTCPeerConnectionBackend>(peerConnection, page->libWebRTCProvider());
 }
 
diff --git a/Source/WebCore/inspector/agents/InspectorPageAgent.cpp b/Source/WebCore/inspector/agents/InspectorPageAgent.cpp
index c6762e6..a0f4ad8 100644
--- a/Source/WebCore/inspector/agents/InspectorPageAgent.cpp
+++ b/Source/WebCore/inspector/agents/InspectorPageAgent.cpp
@@ -93,6 +93,7 @@
     macro(MockCaptureDevicesEnabled) \
     macro(NeedsSiteSpecificQuirks) \
     macro(ScriptEnabled) \
+    macro(WebRTCEncryptionEnabled) \
     macro(WebSecurityEnabled)
 
 static bool decodeBuffer(const char* buffer, unsigned size, const String& textEncodingName, String* result)
diff --git a/Source/WebCore/page/Settings.yaml b/Source/WebCore/page/Settings.yaml
index 8a74147..e097408 100644
--- a/Source/WebCore/page/Settings.yaml
+++ b/Source/WebCore/page/Settings.yaml
@@ -850,6 +850,10 @@
 allowViewportShrinkToFitContent:
   initial: true
 
+webRTCEncryptionEnabled:
+  initial: true
+  inspectorOverride: true
+
 # Deprecated
 
 iceCandidateFilteringEnabled:
diff --git a/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.cpp b/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.cpp
index ffe50e6..3aa90cf 100644
--- a/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.cpp
+++ b/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.cpp
@@ -297,6 +297,17 @@
     return createPeerConnection(observer, *factoryAndThreads.networkManager, *factoryAndThreads.packetSocketFactory, WTFMove(configuration), nullptr);
 }
 
+void LibWebRTCProvider::setEnableWebRTCEncryption(bool enableWebRTCEncryption)
+{
+    auto* factory = this->factory();
+    if (!factory)
+        return;
+
+    webrtc::PeerConnectionFactoryInterface::Options options;
+    options.disable_encryption = !enableWebRTCEncryption;
+    m_factory->SetOptions(options);
+}
+
 rtc::scoped_refptr<webrtc::PeerConnectionInterface> LibWebRTCProvider::createPeerConnection(webrtc::PeerConnectionObserver& observer, rtc::NetworkManager& networkManager, rtc::PacketSocketFactory& packetSocketFactory, webrtc::PeerConnectionInterface::RTCConfiguration&& configuration, std::unique_ptr<webrtc::AsyncResolverFactory>&& asyncResolveFactory)
 {
     auto& factoryAndThreads = getStaticFactoryAndThreads(m_useNetworkThreadWithSocketServer);
diff --git a/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.h b/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.h
index 1a18431..248572f 100644
--- a/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.h
+++ b/Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.h
@@ -116,6 +116,7 @@
     void clearFactory() { m_factory = nullptr; }
 
     void setEnableLogging(bool);
+    void setEnableWebRTCEncryption(bool);
 
 protected:
     LibWebRTCProvider() = default;
diff --git a/Source/WebCore/testing/Internals.cpp b/Source/WebCore/testing/Internals.cpp
index e147d59..e5047c2 100644
--- a/Source/WebCore/testing/Internals.cpp
+++ b/Source/WebCore/testing/Internals.cpp
@@ -527,6 +527,7 @@
     WebCore::useRealRTCPeerConnectionFactory(rtcProvider);
     rtcProvider.disableNonLocalhostConnections();
     RuntimeEnabledFeatures::sharedFeatures().setWebRTCVP8CodecEnabled(true);
+    page.settings().setWebRTCEncryptionEnabled(true);
 #endif
 
     page.settings().setStorageAccessAPIEnabled(false);
@@ -1506,6 +1507,14 @@
 {
     connection.applyRotationForOutgoingVideoSources();
 }
+
+void Internals::setEnableWebRTCEncryption(bool value)
+{
+#if USE(LIBWEBRTC)
+    if (auto* page = contextDocument()->page())
+        page->settings().setWebRTCEncryptionEnabled(value);
+#endif
+}
 #endif
 
 #if ENABLE(MEDIA_STREAM)
diff --git a/Source/WebCore/testing/Internals.h b/Source/WebCore/testing/Internals.h
index 91a7e00..b7f2580 100644
--- a/Source/WebCore/testing/Internals.h
+++ b/Source/WebCore/testing/Internals.h
@@ -535,6 +535,7 @@
     void stopPeerConnection(RTCPeerConnection&);
     void clearPeerConnectionFactory();
     void applyRotationForOutgoingVideoSources(RTCPeerConnection&);
+    void setEnableWebRTCEncryption(bool);
 #endif
 
     String getImageSourceURL(Element&);
diff --git a/Source/WebCore/testing/Internals.idl b/Source/WebCore/testing/Internals.idl
index dc9752e..0eb589f 100644
--- a/Source/WebCore/testing/Internals.idl
+++ b/Source/WebCore/testing/Internals.idl
@@ -607,6 +607,7 @@
     [Conditional=WEB_RTC] void setEnumeratingAllNetworkInterfacesEnabled(boolean enabled);
     [Conditional=WEB_RTC] void stopPeerConnection(RTCPeerConnection connection);
     [Conditional=WEB_RTC] void clearPeerConnectionFactory();
+    [Conditional=WEB_RTC] void setEnableWebRTCEncryption(boolean enabled);
 
     [Conditional=VIDEO] void simulateSystemSleep();
     [Conditional=VIDEO] void simulateSystemWake();
diff --git a/Source/WebInspectorUI/ChangeLog b/Source/WebInspectorUI/ChangeLog
index bb26c4b..95a71e1 100644
--- a/Source/WebInspectorUI/ChangeLog
+++ b/Source/WebInspectorUI/ChangeLog
@@ -1,3 +1,14 @@
+2019-07-15  Youenn Fablet  <youenn@apple.com>
+
+        Enable a debug WebRTC mode without any encryption
+        https://bugs.webkit.org/show_bug.cgi?id=199177
+        <rdar://problem/52074986>
+
+        Reviewed by Eric Carlson.
+
+        * Localizations/en.lproj/localizedStrings.js:
+        * UserInterface/Base/Main.js:
+
 2019-07-15  Greg Doolittle <gr3g@apple.com>
 
         Web Inspector: AXI: Audit: Typos in Accessibility audits
diff --git a/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js b/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js
index 2e350d9..4958dfb 100644
--- a/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js
+++ b/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js
@@ -335,6 +335,7 @@
 localizedStrings["Disable Breakpoint"] = "Disable Breakpoint";
 localizedStrings["Disable Breakpoints"] = "Disable Breakpoints";
 localizedStrings["Disable Descendant Breakpoints"] = "Disable Descendant Breakpoints";
+localizedStrings["Disable Encryption"] = "Disable Encryption";
 localizedStrings["Disable Event Listener"] = "Disable Event Listener";
 localizedStrings["Disable ICE Candidate Restrictions"] = "Disable ICE Candidate Restrictions";
 localizedStrings["Disable Program"] = "Disable Program";
diff --git a/Source/WebInspectorUI/UserInterface/Base/Main.js b/Source/WebInspectorUI/UserInterface/Base/Main.js
index f244434..52db86d 100644
--- a/Source/WebInspectorUI/UserInterface/Base/Main.js
+++ b/Source/WebInspectorUI/UserInterface/Base/Main.js
@@ -2258,6 +2258,7 @@
                     {name: WI.UIString("Allow Media Capture on Insecure Sites"), setting: PageAgent.Setting.MediaCaptureRequiresSecureConnection, value: false},
                     {name: WI.UIString("Disable ICE Candidate Restrictions"), setting: PageAgent.Setting.ICECandidateFilteringEnabled, value: false},
                     {name: WI.UIString("Use Mock Capture Devices"), setting: PageAgent.Setting.MockCaptureDevicesEnabled, value: true},
+                    {name: WI.UIString("Disable Encryption"), setting: PageAgent.Setting.WebRTCEncryptionEnabled, value: false},
                 ],
             ],
         },