Fix for issue #86, added ClientCertificateValidationCallback property to ServerSslAuthConfiguration class, and refactored

This commit is contained in:
sta 2014-11-06 11:16:15 +09:00
parent 82db3b5ac6
commit e3e8fafaa6
7 changed files with 103 additions and 81 deletions

View File

@ -561,11 +561,10 @@ namespace WebSocketSharp
this TcpClient tcpClient, this TcpClient tcpClient,
string protocol, string protocol,
bool secure, bool secure,
ServerSslAuthConfiguration sslConfiguration, ServerSslAuthConfiguration sslConfig,
Logger logger) Logger logger)
{ {
return new TcpListenerWebSocketContext ( return new TcpListenerWebSocketContext (tcpClient, protocol, secure, sslConfig, logger);
tcpClient, protocol, secure, sslConfiguration, logger);
} }
internal static byte[] InternalToByteArray (this ushort value, ByteOrder order) internal static byte[] InternalToByteArray (this ushort value, ByteOrder order)

View File

@ -93,13 +93,13 @@ namespace WebSocketSharp.Net
var netStream = new NetworkStream (socket, false); var netStream = new NetworkStream (socket, false);
if (_secure) { if (_secure) {
var sslStream = new SslStream (netStream, false); var conf = listener.SslConfiguration;
var sslConfig = listener.SslConfiguration; var sslStream = new SslStream (netStream, false, conf.ClientCertificateValidationCallback);
sslStream.AuthenticateAsServer ( sslStream.AuthenticateAsServer (
sslConfig.ServerCertificate, conf.ServerCertificate,
sslConfig.ClientCertificateRequired, conf.ClientCertificateRequired,
sslConfig.EnabledSslProtocols, conf.EnabledSslProtocols,
sslConfig.CheckCertificateRevocation); conf.CheckCertificateRevocation);
_stream = sslStream; _stream = sslStream;
} }

View File

@ -307,12 +307,12 @@ namespace WebSocketSharp.Net
} }
/// <summary> /// <summary>
/// Gets or sets the SSL configuration used to authenticate the server and optionally the client /// Gets or sets the SSL configuration used to authenticate the server and
/// for secure connection. /// optionally the client for secure connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="ServerSslAuthConfiguration"/> that represents the configuration used to /// A <see cref="ServerSslAuthConfiguration"/> that represents the configuration
/// authenticate the server and optionally the client for secure connection. /// used to authenticate the server and optionally the client for secure connection.
/// </value> /// </value>
/// <exception cref="ObjectDisposedException"> /// <exception cref="ObjectDisposedException">
/// This listener has been closed. /// This listener has been closed.

View File

@ -34,17 +34,27 @@
*/ */
#endregion #endregion
using System.Net.Security;
using System.Security.Authentication; using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
namespace WebSocketSharp.Net namespace WebSocketSharp.Net
{ {
/// <summary> /// <summary>
/// Stores the parameters used in configuring <see cref="System.Net.Security.SslStream"/> /// Stores the parameters used to configure a <see cref="SslStream"/> instance as a server.
/// as a server.
/// </summary> /// </summary>
public class ServerSslAuthConfiguration public class ServerSslAuthConfiguration
{ {
#region Private Fields
private X509Certificate2 _cert;
private bool _checkCertRevocation;
private bool _clientCertRequired;
private RemoteCertificateValidationCallback _clientCertValidationCallback;
private SslProtocols _enabledProtocols;
#endregion
#region Public Constructors #region Public Constructors
/// <summary> /// <summary>
@ -60,50 +70,6 @@ namespace WebSocketSharp.Net
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="ServerSslAuthConfiguration"/> class with
/// the specified <paramref name="serverCertificate"/> and
/// <paramref name="clientCertificateRequired"/>.
/// </summary>
/// <param name="serverCertificate">
/// A <see cref="X509Certificate2"/> that represents the certificate used to authenticate
/// the server.
/// </param>
/// <param name="clientCertificateRequired">
/// <c>true</c> if the client must supply a certificate for authentication;
/// otherwise, <c>false</c>.
/// </param>
public ServerSslAuthConfiguration (
X509Certificate2 serverCertificate, bool clientCertificateRequired)
: this (serverCertificate, clientCertificateRequired, SslProtocols.Default, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ServerSslAuthConfiguration"/> class with
/// the specified <paramref name="serverCertificate"/>,
/// <paramref name="clientCertificateRequired"/>, and <paramref name="enabledSslProtocols"/>.
/// </summary>
/// <param name="serverCertificate">
/// A <see cref="X509Certificate2"/> that represents the certificate used to authenticate
/// the server.
/// </param>
/// <param name="clientCertificateRequired">
/// <c>true</c> if the client must supply a certificate for authentication;
/// otherwise, <c>false</c>.
/// </param>
/// <param name="enabledSslProtocols">
/// The <see cref="SslProtocols"/> enum value that represents the protocols used for
/// authentication.
/// </param>
public ServerSslAuthConfiguration (
X509Certificate2 serverCertificate,
bool clientCertificateRequired,
SslProtocols enabledSslProtocols)
: this (serverCertificate, clientCertificateRequired, enabledSslProtocols, false)
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ServerSslAuthConfiguration"/> class with /// Initializes a new instance of the <see cref="ServerSslAuthConfiguration"/> class with
/// the specified <paramref name="serverCertificate"/>, /// the specified <paramref name="serverCertificate"/>,
@ -132,10 +98,10 @@ namespace WebSocketSharp.Net
SslProtocols enabledSslProtocols, SslProtocols enabledSslProtocols,
bool checkCertificateRevocation) bool checkCertificateRevocation)
{ {
ServerCertificate = serverCertificate; _cert = serverCertificate;
ClientCertificateRequired = clientCertificateRequired; _clientCertRequired = clientCertificateRequired;
EnabledSslProtocols = enabledSslProtocols; _enabledProtocols = enabledSslProtocols;
CheckCertificateRevocation = checkCertificateRevocation; _checkCertRevocation = checkCertificateRevocation;
} }
#endregion #endregion
@ -149,7 +115,15 @@ namespace WebSocketSharp.Net
/// <value> /// <value>
/// <c>true</c> if the certificate revocation list is checked; otherwise, <c>false</c>. /// <c>true</c> if the certificate revocation list is checked; otherwise, <c>false</c>.
/// </value> /// </value>
public bool CheckCertificateRevocation { get; set; } public bool CheckCertificateRevocation {
get {
return _checkCertRevocation;
}
set {
_checkCertRevocation = value;
}
}
/// <summary> /// <summary>
/// Gets or sets a value indicating whether the client must supply a certificate for /// Gets or sets a value indicating whether the client must supply a certificate for
@ -158,7 +132,38 @@ namespace WebSocketSharp.Net
/// <value> /// <value>
/// <c>true</c> if the client must supply a certificate; otherwise, <c>false</c>. /// <c>true</c> if the client must supply a certificate; otherwise, <c>false</c>.
/// </value> /// </value>
public bool ClientCertificateRequired { get; set; } public bool ClientCertificateRequired {
get {
return _clientCertRequired;
}
set {
_clientCertRequired = value;
}
}
/// <summary>
/// Gets or sets the callback used to validate the certificate supplied by the client.
/// </summary>
/// <remarks>
/// If this callback returns <c>true</c>, the client certificate will be valid.
/// </remarks>
/// <value>
/// A <see cref="RemoteCertificateValidationCallback"/> delegate that references the method
/// used to validate the client certificate. The default value is a function that only returns
/// <c>true</c>.
/// </value>
public RemoteCertificateValidationCallback ClientCertificateValidationCallback {
get {
return _clientCertValidationCallback ??
(_clientCertValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true);
}
set {
_clientCertValidationCallback = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the SSL protocols used for authentication. /// Gets or sets the SSL protocols used for authentication.
@ -167,7 +172,15 @@ namespace WebSocketSharp.Net
/// The <see cref="SslProtocols"/> enum value that represents the protocols used for /// The <see cref="SslProtocols"/> enum value that represents the protocols used for
/// authentication. /// authentication.
/// </value> /// </value>
public SslProtocols EnabledSslProtocols { get; set; } public SslProtocols EnabledSslProtocols {
get {
return _enabledProtocols;
}
set {
_enabledProtocols = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the certificate used to authenticate the server on the secure connection. /// Gets or sets the certificate used to authenticate the server on the secure connection.
@ -176,7 +189,15 @@ namespace WebSocketSharp.Net
/// A <see cref="X509Certificate2"/> that represents the certificate used to authenticate /// A <see cref="X509Certificate2"/> that represents the certificate used to authenticate
/// the server. /// the server.
/// </value> /// </value>
public X509Certificate2 ServerCertificate { get; set; } public X509Certificate2 ServerCertificate {
get {
return _cert;
}
set {
_cert = value;
}
}
#endregion #endregion
} }

View File

@ -71,7 +71,7 @@ namespace WebSocketSharp.Net.WebSockets
TcpClient tcpClient, TcpClient tcpClient,
string protocol, string protocol,
bool secure, bool secure,
ServerSslAuthConfiguration sslConfiguration, ServerSslAuthConfiguration sslConfig,
Logger logger) Logger logger)
{ {
_tcpClient = tcpClient; _tcpClient = tcpClient;
@ -79,12 +79,14 @@ namespace WebSocketSharp.Net.WebSockets
var netStream = tcpClient.GetStream (); var netStream = tcpClient.GetStream ();
if (secure) { if (secure) {
var sslStream = new SslStream (netStream, false); var sslStream = new SslStream (
netStream, false, sslConfig.ClientCertificateValidationCallback);
sslStream.AuthenticateAsServer ( sslStream.AuthenticateAsServer (
sslConfiguration.ServerCertificate, sslConfig.ServerCertificate,
sslConfiguration.ClientCertificateRequired, sslConfig.ClientCertificateRequired,
sslConfiguration.EnabledSslProtocols, sslConfig.EnabledSslProtocols,
sslConfiguration.CheckCertificateRevocation); sslConfig.CheckCertificateRevocation);
_stream = sslStream; _stream = sslStream;
} }

View File

@ -335,12 +335,12 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets or sets the SSL configuration used to authenticate the server and optionally the client /// Gets or sets the SSL configuration used to authenticate the server and
/// for secure connection. /// optionally the client for secure connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="ServerSslAuthConfiguration"/> that represents the configuration used to /// A <see cref="ServerSslAuthConfiguration"/> that represents the configuration
/// authenticate the server and optionally the client for secure connection. /// used to authenticate the server and optionally the client for secure connection.
/// </value> /// </value>
public ServerSslAuthConfiguration SslConfiguration { public ServerSslAuthConfiguration SslConfiguration {
get { get {

View File

@ -441,12 +441,12 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Gets or sets the SSL configuration used to authenticate the server and optionally the client /// Gets or sets the SSL configuration used to authenticate the server and
/// for secure connection. /// optionally the client for secure connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="ServerSslAuthConfiguration"/> that represents the configuration used to /// A <see cref="ServerSslAuthConfiguration"/> that represents the configuration
/// authenticate the server and optionally the client for secure connection. /// used to authenticate the server and optionally the client for secure connection.
/// </value> /// </value>
public ServerSslAuthConfiguration SslConfiguration { public ServerSslAuthConfiguration SslConfiguration {
get { get {