From 5e9157a3d4027a7b4c0d422f8bd94dfdb050c191 Mon Sep 17 00:00:00 2001 From: sta Date: Wed, 5 Nov 2014 11:06:55 +0900 Subject: [PATCH] Moved WebSocket.ClientCertificateSelectionCallback property to ClientSslAuthConfiguration class, and refactored --- .../Net/ClientSslAuthConfiguration.cs | 80 +++++++++++++++++-- websocket-sharp/WebSocket.cs | 45 +---------- 2 files changed, 76 insertions(+), 49 deletions(-) diff --git a/websocket-sharp/Net/ClientSslAuthConfiguration.cs b/websocket-sharp/Net/ClientSslAuthConfiguration.cs index c8f13d6a..5e71bf88 100644 --- a/websocket-sharp/Net/ClientSslAuthConfiguration.cs +++ b/websocket-sharp/Net/ClientSslAuthConfiguration.cs @@ -47,6 +47,11 @@ namespace WebSocketSharp.Net { #region Private Fields + private X509CertificateCollection _certs; + private LocalCertificateSelectionCallback _certSelectionCallback; + private bool _checkCertRevocation; + private SslProtocols _enabledProtocols; + private string _host; private RemoteCertificateValidationCallback _serverCertValidationCallback; #endregion @@ -92,10 +97,10 @@ namespace WebSocketSharp.Net SslProtocols enabledSslProtocols, bool checkCertificateRevocation) { - TargetHost = targetHost; - ClientCertificates = clientCertificates; - EnabledSslProtocols = enabledSslProtocols; - CheckCertificateRevocation = checkCertificateRevocation; + _host = targetHost; + _certs = clientCertificates; + _enabledProtocols = enabledSslProtocols; + _checkCertRevocation = checkCertificateRevocation; } #endregion @@ -109,7 +114,15 @@ namespace WebSocketSharp.Net /// /// true if the certificate revocation list is checked; otherwise, false. /// - public bool CheckCertificateRevocation { get; set; } + public bool CheckCertificateRevocation { + get { + return _checkCertRevocation; + } + + set { + _checkCertRevocation = value; + } + } /// /// Gets or sets the collection that contains client certificates. @@ -117,7 +130,39 @@ namespace WebSocketSharp.Net /// /// A that contains client certificates. /// - public X509CertificateCollection ClientCertificates { get; set; } + public X509CertificateCollection ClientCertificates { + get { + return _certs; + } + + set { + _certs = value; + } + } + + /// + /// Gets or sets the callback used to select a client certificate to supply to the server. + /// + /// + /// If this callback returns , no client certificate will be supplied. + /// + /// + /// A delegate that references the method + /// used to select the client certificate. The default value is a function that only returns + /// . + /// + public LocalCertificateSelectionCallback ClientCertificateSelectionCallback { + get { + return _certSelectionCallback ?? + (_certSelectionCallback = + (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => + null); + } + + set { + _certSelectionCallback = value; + } + } /// /// Gets or sets the SSL protocols used for authentication. @@ -126,11 +171,22 @@ namespace WebSocketSharp.Net /// The enum value that represents the protocols used for /// authentication. /// - public SslProtocols EnabledSslProtocols { get; set; } + public SslProtocols EnabledSslProtocols { + get { + return _enabledProtocols; + } + + set { + _enabledProtocols = value; + } + } /// /// Gets or sets the callback used to validate the certificate supplied by the server. /// + /// + /// If this callback returns true, the server certificate will be valid. + /// /// /// A delegate that references the method /// used to validate the server certificate. The default value is a function that only returns @@ -155,7 +211,15 @@ namespace WebSocketSharp.Net /// A that represents the name of the server that shares /// a secure connection. /// - public string TargetHost { get; set; } + public string TargetHost { + get { + return _host; + } + + set { + _host = value; + } + } #endregion } diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 611c19d5..b37cfe7f 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -70,8 +70,6 @@ namespace WebSocketSharp private AuthenticationChallenge _authChallenge; private string _base64Key; - private LocalCertificateSelectionCallback - _certSelectionCallback; private bool _client; private Action _closeContext; private CompressionMethod _compression; @@ -234,40 +232,6 @@ namespace WebSocketSharp #region Public Properties - /// - /// Gets or sets the callback used to select a client certificate to supply to the server. - /// - /// - /// If the value of this property is , no client certificate will be - /// supplied. - /// - /// - /// A delegate that references the method - /// used to select the client certificate. The default value is . - /// - public LocalCertificateSelectionCallback ClientCertificateSelectionCallback - { - get { - return _certSelectionCallback; - } - - set { - lock (_forConn) { - var msg = checkIfAvailable (false, false); - if (msg != null) { - _logger.Error (msg); - error ( - "An error has occurred in setting the client certificate selection callback.", - null); - - return; - } - - _certSelectionCallback = value; - } - } - } - /// /// Gets or sets the compression method used to compress the message on the WebSocket /// connection. @@ -1335,7 +1299,8 @@ namespace WebSocketSharp if (_secure) { var conf = SslConfiguration; - if (conf.TargetHost != _uri.DnsSafeHost) + var host = conf.TargetHost; + if (host != _uri.DnsSafeHost) throw new WebSocketException ( CloseStatusCode.TlsHandshakeFailure, "An invalid host name is specified."); @@ -1344,12 +1309,10 @@ namespace WebSocketSharp _stream, false, conf.ServerCertificateValidationCallback, - _certSelectionCallback ?? - ((sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => - null)); + conf.ClientCertificateSelectionCallback); sslStream.AuthenticateAsClient ( - conf.TargetHost, + host, conf.ClientCertificates, conf.EnabledSslProtocols, conf.CheckCertificateRevocation);