Moved WebSocket.ClientCertificateSelectionCallback property to ClientSslAuthConfiguration class, and refactored

This commit is contained in:
sta 2014-11-05 11:06:55 +09:00
parent 5502e4bdda
commit 5e9157a3d4
2 changed files with 76 additions and 49 deletions

View File

@ -47,6 +47,11 @@ namespace WebSocketSharp.Net
{ {
#region Private Fields #region Private Fields
private X509CertificateCollection _certs;
private LocalCertificateSelectionCallback _certSelectionCallback;
private bool _checkCertRevocation;
private SslProtocols _enabledProtocols;
private string _host;
private RemoteCertificateValidationCallback _serverCertValidationCallback; private RemoteCertificateValidationCallback _serverCertValidationCallback;
#endregion #endregion
@ -92,10 +97,10 @@ namespace WebSocketSharp.Net
SslProtocols enabledSslProtocols, SslProtocols enabledSslProtocols,
bool checkCertificateRevocation) bool checkCertificateRevocation)
{ {
TargetHost = targetHost; _host = targetHost;
ClientCertificates = clientCertificates; _certs = clientCertificates;
EnabledSslProtocols = enabledSslProtocols; _enabledProtocols = enabledSslProtocols;
CheckCertificateRevocation = checkCertificateRevocation; _checkCertRevocation = checkCertificateRevocation;
} }
#endregion #endregion
@ -109,7 +114,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 the collection that contains client certificates. /// Gets or sets the collection that contains client certificates.
@ -117,7 +130,39 @@ namespace WebSocketSharp.Net
/// <value> /// <value>
/// A <see cref="X509CertificateCollection"/> that contains client certificates. /// A <see cref="X509CertificateCollection"/> that contains client certificates.
/// </value> /// </value>
public X509CertificateCollection ClientCertificates { get; set; } public X509CertificateCollection ClientCertificates {
get {
return _certs;
}
set {
_certs = value;
}
}
/// <summary>
/// Gets or sets the callback used to select a client certificate to supply to the server.
/// </summary>
/// <remarks>
/// If this callback returns <see langword="null"/>, no client certificate will be supplied.
/// </remarks>
/// <value>
/// A <see cref="LocalCertificateSelectionCallback"/> delegate that references the method
/// used to select the client certificate. The default value is a function that only returns
/// <see langword="null"/>.
/// </value>
public LocalCertificateSelectionCallback ClientCertificateSelectionCallback {
get {
return _certSelectionCallback ??
(_certSelectionCallback =
(sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) =>
null);
}
set {
_certSelectionCallback = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the SSL protocols used for authentication. /// Gets or sets the SSL protocols used for authentication.
@ -126,11 +171,22 @@ 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 callback used to validate the certificate supplied by the server. /// Gets or sets the callback used to validate the certificate supplied by the server.
/// </summary> /// </summary>
/// <remarks>
/// If this callback returns <c>true</c>, the server certificate will be valid.
/// </remarks>
/// <value> /// <value>
/// A <see cref="RemoteCertificateValidationCallback"/> delegate that references the method /// A <see cref="RemoteCertificateValidationCallback"/> delegate that references the method
/// used to validate the server certificate. The default value is a function that only returns /// used to validate the server certificate. The default value is a function that only returns
@ -155,7 +211,15 @@ namespace WebSocketSharp.Net
/// A <see cref="string"/> that represents the name of the server that shares /// A <see cref="string"/> that represents the name of the server that shares
/// a secure connection. /// a secure connection.
/// </value> /// </value>
public string TargetHost { get; set; } public string TargetHost {
get {
return _host;
}
set {
_host = value;
}
}
#endregion #endregion
} }

View File

@ -70,8 +70,6 @@ namespace WebSocketSharp
private AuthenticationChallenge _authChallenge; private AuthenticationChallenge _authChallenge;
private string _base64Key; private string _base64Key;
private LocalCertificateSelectionCallback
_certSelectionCallback;
private bool _client; private bool _client;
private Action _closeContext; private Action _closeContext;
private CompressionMethod _compression; private CompressionMethod _compression;
@ -234,40 +232,6 @@ namespace WebSocketSharp
#region Public Properties #region Public Properties
/// <summary>
/// Gets or sets the callback used to select a client certificate to supply to the server.
/// </summary>
/// <remarks>
/// If the value of this property is <see langword="null"/>, no client certificate will be
/// supplied.
/// </remarks>
/// <value>
/// A <see cref="LocalCertificateSelectionCallback"/> delegate that references the method
/// used to select the client certificate. The default value is <see langword="null"/>.
/// </value>
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;
}
}
}
/// <summary> /// <summary>
/// Gets or sets the compression method used to compress the message on the WebSocket /// Gets or sets the compression method used to compress the message on the WebSocket
/// connection. /// connection.
@ -1335,7 +1299,8 @@ namespace WebSocketSharp
if (_secure) { if (_secure) {
var conf = SslConfiguration; var conf = SslConfiguration;
if (conf.TargetHost != _uri.DnsSafeHost) var host = conf.TargetHost;
if (host != _uri.DnsSafeHost)
throw new WebSocketException ( throw new WebSocketException (
CloseStatusCode.TlsHandshakeFailure, "An invalid host name is specified."); CloseStatusCode.TlsHandshakeFailure, "An invalid host name is specified.");
@ -1344,12 +1309,10 @@ namespace WebSocketSharp
_stream, _stream,
false, false,
conf.ServerCertificateValidationCallback, conf.ServerCertificateValidationCallback,
_certSelectionCallback ?? conf.ClientCertificateSelectionCallback);
((sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) =>
null));
sslStream.AuthenticateAsClient ( sslStream.AuthenticateAsClient (
conf.TargetHost, host,
conf.ClientCertificates, conf.ClientCertificates,
conf.EnabledSslProtocols, conf.EnabledSslProtocols,
conf.CheckCertificateRevocation); conf.CheckCertificateRevocation);