From c511f9d7ac597f502732f16e768ebc036606fa07 Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 3 Nov 2014 15:11:43 +0900 Subject: [PATCH] Fix for pull request #85, added TargetHost property to ClientSslAuthConfiguration class, and refactored --- .../Net/ClientSslAuthConfiguration.cs | 50 +++++++++---------- websocket-sharp/WebSocket.cs | 50 +++++++++++-------- 2 files changed, 54 insertions(+), 46 deletions(-) diff --git a/websocket-sharp/Net/ClientSslAuthConfiguration.cs b/websocket-sharp/Net/ClientSslAuthConfiguration.cs index 9a974047..0da06479 100644 --- a/websocket-sharp/Net/ClientSslAuthConfiguration.cs +++ b/websocket-sharp/Net/ClientSslAuthConfiguration.cs @@ -34,14 +34,14 @@ */ #endregion +using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; namespace WebSocketSharp.Net { /// - /// Stores the parameters used in configuring - /// as a client. + /// Stores the parameters used to configure a instance as a client. /// public class ClientSslAuthConfiguration { @@ -49,39 +49,26 @@ namespace WebSocketSharp.Net /// /// Initializes a new instance of the class with - /// the specified . + /// the specified . /// - /// - /// A that contains client certificates. + /// + /// A that represents the name of the server that shares + /// a secure connection. /// - public ClientSslAuthConfiguration (X509CertificateCollection clientCertificates) - : this (clientCertificates, SslProtocols.Default, false) + public ClientSslAuthConfiguration (string targetHost) + : this (targetHost, null, SslProtocols.Default, false) { } /// /// Initializes a new instance of the class with - /// the specified and - /// . + /// the specified , , + /// , and . /// - /// - /// A that contains client certificates. + /// + /// A that represents the name of the server that shares + /// a secure connection. /// - /// - /// The enum value that represents the protocols used for - /// authentication. - /// - public ClientSslAuthConfiguration ( - X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols) - : this (clientCertificates, enabledSslProtocols, false) - { - } - - /// - /// Initializes a new instance of the class with - /// the specified , , - /// and . - /// /// /// A that contains client certificates. /// @@ -94,10 +81,12 @@ namespace WebSocketSharp.Net /// otherwise, false. /// public ClientSslAuthConfiguration ( + string targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, bool checkCertificateRevocation) { + TargetHost = targetHost; ClientCertificates = clientCertificates; EnabledSslProtocols = enabledSslProtocols; CheckCertificateRevocation = checkCertificateRevocation; @@ -133,6 +122,15 @@ namespace WebSocketSharp.Net /// public SslProtocols EnabledSslProtocols { get; set; } + /// + /// Gets or sets the name of the server that shares a secure connection. + /// + /// + /// A that represents the name of the server that shares + /// a secure connection. + /// + public string TargetHost { get; set; } + #endregion } } \ No newline at end of file diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index d11398ca..eae805a5 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -498,16 +498,19 @@ namespace WebSocketSharp } /// - /// Gets or sets the SSL configuration used to authenticate the server and optionally the client - /// on the secure connection. + /// Gets or sets the SSL configuration used to authenticate the server and + /// optionally the client for secure connection. /// /// - /// A that represents the SSL configuration used to - /// authenticate the server and optionally the client. + /// A that represents the configuration + /// used to authenticate the server and optionally the client for secure connection, + /// or if the is used as server. /// public ClientSslAuthConfiguration SslConfiguration { get { - return _sslConfig; + return _client + ? (_sslConfig ?? (_sslConfig = new ClientSslAuthConfiguration (_uri.DnsSafeHost))) + : null; } set { @@ -1366,24 +1369,31 @@ namespace WebSocketSharp } if (_secure) { - var sslStream = new SslStream ( - _stream, - false, - _certValidationCallback ?? ((sender, certificate, chain, sslPolicyErrors) => true), - _certSelectionCallback ?? - ((sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => - null)); + var conf = SslConfiguration; + if (conf.TargetHost != _uri.DnsSafeHost) + throw new WebSocketException ( + CloseStatusCode.TlsHandshakeFailure, "An invalid host name is specified."); + + try { + var sslStream = new SslStream ( + _stream, + false, + _certValidationCallback ?? ((sender, certificate, chain, sslPolicyErrors) => true), + _certSelectionCallback ?? + ((sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => + null)); - if (_sslConfig == null) - sslStream.AuthenticateAsClient (_uri.DnsSafeHost); - else sslStream.AuthenticateAsClient ( - _uri.DnsSafeHost, - _sslConfig.ClientCertificates, - _sslConfig.EnabledSslProtocols, - _sslConfig.CheckCertificateRevocation); + conf.TargetHost, + conf.ClientCertificates, + conf.EnabledSslProtocols, + conf.CheckCertificateRevocation); - _stream = sslStream; + _stream = sslStream; + } + catch (Exception ex) { + throw new WebSocketException (CloseStatusCode.TlsHandshakeFailure, ex); + } } }