From 5502e4bdda680e660675db311c22d8f217852ac0 Mon Sep 17 00:00:00 2001 From: sta Date: Tue, 4 Nov 2014 11:01:40 +0900 Subject: [PATCH] Moved WebSocket.ServerCertificateValidationCallback property to ClientSslAuthConfiguration class --- Example/Program.cs | 19 +++++----- .../Net/ClientSslAuthConfiguration.cs | 26 +++++++++++++ websocket-sharp/WebSocket.cs | 37 +------------------ 3 files changed, 37 insertions(+), 45 deletions(-) diff --git a/Example/Program.cs b/Example/Program.cs index 01e974c0..3282957c 100644 --- a/Example/Program.cs +++ b/Example/Program.cs @@ -66,15 +66,16 @@ namespace Example //ws.Compression = CompressionMethod.Deflate; /* To validate the server certificate. - ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { - ws.Log.Debug ( - String.Format ( - "Certificate:\n- Issuer: {0}\n- Subject: {1}", - certificate.Issuer, - certificate.Subject)); - - return true; // If the server certificate is valid. - }; + ws.SslConfiguration.ServerCertificateValidationCallback = + (sender, certificate, chain, sslPolicyErrors) => { + ws.Log.Debug ( + String.Format ( + "Certificate:\n- Issuer: {0}\n- Subject: {1}", + certificate.Issuer, + certificate.Subject)); + + return true; // If the server certificate is valid. + }; */ // To set the credentials for the HTTP Authentication (Basic/Digest). diff --git a/websocket-sharp/Net/ClientSslAuthConfiguration.cs b/websocket-sharp/Net/ClientSslAuthConfiguration.cs index 0da06479..c8f13d6a 100644 --- a/websocket-sharp/Net/ClientSslAuthConfiguration.cs +++ b/websocket-sharp/Net/ClientSslAuthConfiguration.cs @@ -45,6 +45,12 @@ namespace WebSocketSharp.Net /// public class ClientSslAuthConfiguration { + #region Private Fields + + private RemoteCertificateValidationCallback _serverCertValidationCallback; + + #endregion + #region Public Constructors /// @@ -122,6 +128,26 @@ namespace WebSocketSharp.Net /// public SslProtocols EnabledSslProtocols { get; set; } + /// + /// Gets or sets the callback used to validate the certificate supplied by the server. + /// + /// + /// A delegate that references the method + /// used to validate the server certificate. The default value is a function that only returns + /// true. + /// + public RemoteCertificateValidationCallback ServerCertificateValidationCallback { + get { + return _serverCertValidationCallback ?? + (_serverCertValidationCallback = + (sender, certificate, chain, sslPolicyErrors) => true); + } + + set { + _serverCertValidationCallback = value; + } + } + /// /// Gets or sets the name of the server that shares a secure connection. /// diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index eae805a5..611c19d5 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -72,8 +72,6 @@ namespace WebSocketSharp private string _base64Key; private LocalCertificateSelectionCallback _certSelectionCallback; - private RemoteCertificateValidationCallback - _certValidationCallback; private bool _client; private Action _closeContext; private CompressionMethod _compression; @@ -464,39 +462,6 @@ namespace WebSocketSharp } } - /// - /// Gets or sets the callback used to validate the certificate supplied by the server. - /// - /// - /// If the value of this property is , the validation does nothing with - /// the server certificate, and always returns valid. - /// - /// - /// A delegate that references the method - /// used to validate the server certificate. The default value is . - /// - public RemoteCertificateValidationCallback ServerCertificateValidationCallback { - get { - return _certValidationCallback; - } - - set { - lock (_forConn) { - var msg = checkIfAvailable (false, false); - if (msg != null) { - _logger.Error (msg); - error ( - "An error has occurred in setting the server certificate validation callback.", - null); - - return; - } - - _certValidationCallback = value; - } - } - } - /// /// Gets or sets the SSL configuration used to authenticate the server and /// optionally the client for secure connection. @@ -1378,7 +1343,7 @@ namespace WebSocketSharp var sslStream = new SslStream ( _stream, false, - _certValidationCallback ?? ((sender, certificate, chain, sslPolicyErrors) => true), + conf.ServerCertificateValidationCallback, _certSelectionCallback ?? ((sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => null));