diff --git a/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.cs b/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.cs index a80079730a7..7dfcff2fd8b 100644 --- a/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.cs +++ b/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.cs @@ -32,6 +32,7 @@ namespace System.Net.WebSockets public System.Net.ICredentials Credentials { get { throw null; } set { } } public System.TimeSpan KeepAliveInterval { get { throw null; } set { } } public System.Net.IWebProxy Proxy { get { throw null; } set { } } + public System.Net.Security.RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get { throw null; } set { } } public bool UseDefaultCredentials { get { throw null; } set { } } public void AddSubProtocol(string subProtocol) { } public void SetBuffer(int receiveBufferSize, int sendBufferSize) { } diff --git a/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.csproj b/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.csproj index b4619853bb9..18a1d23354c 100644 --- a/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.csproj +++ b/src/libraries/System.Net.WebSockets.Client/ref/System.Net.WebSockets.Client.csproj @@ -13,6 +13,7 @@ + diff --git a/src/libraries/System.Net.WebSockets.Client/src/ILLinkTrim.xml b/src/libraries/System.Net.WebSockets.Client/src/ILLinkTrim.xml deleted file mode 100644 index ac97af69999..00000000000 --- a/src/libraries/System.Net.WebSockets.Client/src/ILLinkTrim.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx b/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx index cc93c4738e2..dc7a2067a1f 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx +++ b/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx @@ -183,4 +183,7 @@ Client certificate was not found in the personal (\"MY\") certificate store. In UWP, client certificates are only supported if they have been added to that certificate store. + + ClientWebSocketOptions.RemoteCertificateValidationCallback is not supported on this platform. + diff --git a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/ClientWebSocketOptions.cs b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/ClientWebSocketOptions.cs index 77f3bbd1a1f..3fdbfb0408c 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/ClientWebSocketOptions.cs +++ b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/ClientWebSocketOptions.cs @@ -109,7 +109,7 @@ namespace System.Net.WebSockets } } - internal RemoteCertificateValidationCallback RemoteCertificateValidationCallback // TODO #12038: Expose publicly. + public RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get => _remoteCertificateValidationCallback; set diff --git a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.WinRT.cs b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.WinRT.cs index 6a69057405e..38a9de071a3 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.WinRT.cs +++ b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.WinRT.cs @@ -39,6 +39,11 @@ namespace System.Net.WebSockets public async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken, ClientWebSocketOptions options) { + if (options.RemoteCertificateValidationCallback != null) + { + throw new PlatformNotSupportedException(SR.net_WebSockets_RemoteValidationCallbackNotSupported); + } + try { await _webSocket.ConnectAsync(uri, cancellationToken, options).ConfigureAwait(false); diff --git a/src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketOptionsTests.cs b/src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketOptionsTests.cs index a2913b229ff..c94f5548320 100644 --- a/src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketOptionsTests.cs +++ b/src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketOptionsTests.cs @@ -142,42 +142,5 @@ namespace System.Net.WebSockets.Client.Tests AssertExtensions.Throws("value", () => cws.Options.KeepAliveInterval = TimeSpan.MinValue); } - - [OuterLoop("Connects to remote service")] - [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Lacks RemoteCertificateValidationCallback to enable loopback testing")] - [ConditionalFact(nameof(WebSocketsSupported), nameof(ClientCertificatesSupported))] - public async Task ClientCertificates_ValidCertificate_ServerReceivesCertificateAndConnectAsyncSucceeds() - { - if (PlatformDetection.IsWindows7) - { - return; // [ActiveIssue(27846)] - } - - using (X509Certificate2 clientCert = Test.Common.Configuration.Certificates.GetClientCertificate()) - { - await LoopbackServer.CreateClientAndServerAsync(async uri => - { - using (var clientSocket = new ClientWebSocket()) - using (var cts = new CancellationTokenSource(TimeOutMilliseconds)) - { - clientSocket.Options.ClientCertificates.Add(clientCert); - clientSocket.Options.GetType().GetProperty("RemoteCertificateValidationCallback", BindingFlags.NonPublic | BindingFlags.Instance) - .SetValue(clientSocket.Options, new RemoteCertificateValidationCallback(delegate { return true; })); // TODO: #12038: Simplify once property is public. - await clientSocket.ConnectAsync(uri, cts.Token); - } - }, server => server.AcceptConnectionAsync(async connection => - { - // Validate that the client certificate received by the server matches the one configured on - // the client-side socket. - SslStream sslStream = Assert.IsType(connection.Stream); - Assert.NotNull(sslStream.RemoteCertificate); - Assert.Equal(clientCert, new X509Certificate2(sslStream.RemoteCertificate)); - - // Complete the WebSocket upgrade over the secure channel. After this is done, the client-side - // ConnectAsync should complete. - Assert.True(await LoopbackHelper.WebSocketHandshakeAsync(connection)); - }), new LoopbackServer.Options { UseSsl = true, WebSocketEndpoint = true }); - } - } } } diff --git a/src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketOptionsTests.netcoreapp.cs b/src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketOptionsTests.netcoreapp.cs new file mode 100644 index 00000000000..52dea16d791 --- /dev/null +++ b/src/libraries/System.Net.WebSockets.Client/tests/ClientWebSocketOptionsTests.netcoreapp.cs @@ -0,0 +1,108 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Net.Security; +using System.Net.Test.Common; +using System.Reflection; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; + +using Xunit; +using Xunit.Abstractions; + +namespace System.Net.WebSockets.Client.Tests +{ + public partial class ClientWebSocketOptionsTests : ClientWebSocketTestBase + { + [ConditionalFact(nameof(WebSocketsSupported), nameof(ClientCertificatesSupported))] + public void RemoteCertificateValidationCallback_Roundtrips() + { + using (var cws = new ClientWebSocket()) + { + Assert.Null(cws.Options.RemoteCertificateValidationCallback); + + RemoteCertificateValidationCallback callback = delegate { return true; }; + cws.Options.RemoteCertificateValidationCallback = callback; + Assert.Same(callback, cws.Options.RemoteCertificateValidationCallback); + + cws.Options.RemoteCertificateValidationCallback = null; + Assert.Null(cws.Options.RemoteCertificateValidationCallback); + } + } + + [OuterLoop("Connects to remote service")] + [ConditionalTheory(nameof(WebSocketsSupported), nameof(ClientCertificatesSupported))] + [InlineData(false)] + [InlineData(true)] + public async Task RemoteCertificateValidationCallback_PassedRemoteCertificateInfo(bool secure) + { + if (PlatformDetection.IsWindows7) + { + return; // [ActiveIssue(27846)] + } + + bool callbackInvoked = false; + + await LoopbackServer.CreateClientAndServerAsync(async uri => + { + using (var cws = new ClientWebSocket()) + using (var cts = new CancellationTokenSource(TimeOutMilliseconds)) + { + cws.Options.RemoteCertificateValidationCallback = (source, cert, chain, errors) => + { + Assert.NotNull(source); + Assert.NotNull(cert); + Assert.NotNull(chain); + Assert.NotEqual(SslPolicyErrors.None, errors); + callbackInvoked = true; + return true; + }; + await cws.ConnectAsync(uri, cts.Token); + } + }, server => server.AcceptConnectionAsync(async connection => + { + Assert.True(await LoopbackHelper.WebSocketHandshakeAsync(connection)); + }), + new LoopbackServer.Options { UseSsl = secure, WebSocketEndpoint = true }); + + Assert.Equal(secure, callbackInvoked); + } + + [OuterLoop("Connects to remote service")] + [ConditionalFact(nameof(WebSocketsSupported), nameof(ClientCertificatesSupported))] + public async Task ClientCertificates_ValidCertificate_ServerReceivesCertificateAndConnectAsyncSucceeds() + { + if (PlatformDetection.IsWindows7) + { + return; // [ActiveIssue(27846)] + } + + using (X509Certificate2 clientCert = Test.Common.Configuration.Certificates.GetClientCertificate()) + { + await LoopbackServer.CreateClientAndServerAsync(async uri => + { + using (var clientSocket = new ClientWebSocket()) + using (var cts = new CancellationTokenSource(TimeOutMilliseconds)) + { + clientSocket.Options.ClientCertificates.Add(clientCert); + clientSocket.Options.RemoteCertificateValidationCallback = delegate { return true; }; + await clientSocket.ConnectAsync(uri, cts.Token); + } + }, server => server.AcceptConnectionAsync(async connection => + { + // Validate that the client certificate received by the server matches the one configured on + // the client-side socket. + SslStream sslStream = Assert.IsType(connection.Stream); + Assert.NotNull(sslStream.RemoteCertificate); + Assert.Equal(clientCert, new X509Certificate2(sslStream.RemoteCertificate)); + + // Complete the WebSocket upgrade over the secure channel. After this is done, the client-side + // ConnectAsync should complete. + Assert.True(await LoopbackHelper.WebSocketHandshakeAsync(connection)); + }), new LoopbackServer.Options { UseSsl = true, WebSocketEndpoint = true }); + } + } + } +} diff --git a/src/libraries/System.Net.WebSockets.Client/tests/System.Net.WebSockets.Client.Tests.csproj b/src/libraries/System.Net.WebSockets.Client/tests/System.Net.WebSockets.Client.Tests.csproj index 8f8f4d2a34f..8c059d798ae 100644 --- a/src/libraries/System.Net.WebSockets.Client/tests/System.Net.WebSockets.Client.Tests.csproj +++ b/src/libraries/System.Net.WebSockets.Client/tests/System.Net.WebSockets.Client.Tests.csproj @@ -45,6 +45,7 @@ +