Moved WebSocket.ClientCertificateSelectionCallback property to ClientSslAuthConfiguration class, and refactored
This commit is contained in:
		@@ -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
 | 
			
		||||
    /// <value>
 | 
			
		||||
    /// <c>true</c> if the certificate revocation list is checked; otherwise, <c>false</c>.
 | 
			
		||||
    /// </value>
 | 
			
		||||
    public bool CheckCertificateRevocation { get; set; }
 | 
			
		||||
    public bool CheckCertificateRevocation {
 | 
			
		||||
      get {
 | 
			
		||||
        return _checkCertRevocation;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      set {
 | 
			
		||||
        _checkCertRevocation = value;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Gets or sets the collection that contains client certificates.
 | 
			
		||||
@@ -117,7 +130,39 @@ namespace WebSocketSharp.Net
 | 
			
		||||
    /// <value>
 | 
			
		||||
    /// A <see cref="X509CertificateCollection"/> that contains client certificates.
 | 
			
		||||
    /// </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>
 | 
			
		||||
    /// 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
 | 
			
		||||
    /// authentication.
 | 
			
		||||
    /// </value>
 | 
			
		||||
    public SslProtocols EnabledSslProtocols { get; set; }
 | 
			
		||||
    public SslProtocols EnabledSslProtocols {
 | 
			
		||||
      get {
 | 
			
		||||
        return _enabledProtocols;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      set {
 | 
			
		||||
        _enabledProtocols = value;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Gets or sets the callback used to validate the certificate supplied by the server.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <remarks>
 | 
			
		||||
    /// If this callback returns <c>true</c>, the server certificate will be valid.
 | 
			
		||||
    /// </remarks>
 | 
			
		||||
    /// <value>
 | 
			
		||||
    /// A <see cref="RemoteCertificateValidationCallback"/> 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 <see cref="string"/> that represents the name of the server that shares
 | 
			
		||||
    /// a secure connection.
 | 
			
		||||
    /// </value>
 | 
			
		||||
    public string TargetHost { get; set; }
 | 
			
		||||
    public string TargetHost {
 | 
			
		||||
      get {
 | 
			
		||||
        return _host;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      set {
 | 
			
		||||
        _host = value;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #endregion
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
    /// <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>
 | 
			
		||||
    /// 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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user