2014-10-28 18:24:00 +08:00
|
|
|
|
#region License
|
|
|
|
|
/*
|
2014-11-06 10:51:38 +08:00
|
|
|
|
* ClientSslConfiguration.cs
|
2014-10-28 18:24:00 +08:00
|
|
|
|
*
|
|
|
|
|
* The MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2014 liryna
|
2014-10-30 18:09:12 +08:00
|
|
|
|
* Copyright (c) 2014 sta.blockhead
|
2014-10-28 18:24:00 +08:00
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Authors
|
|
|
|
|
/*
|
|
|
|
|
* Authors:
|
2014-10-30 18:09:12 +08:00
|
|
|
|
* - Liryna <liryna.stark@gmail.com>
|
2014-10-28 18:24:00 +08:00
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-05-11 14:38:27 +08:00
|
|
|
|
using System;
|
2014-11-03 14:11:43 +08:00
|
|
|
|
using System.Net.Security;
|
2014-10-28 18:24:00 +08:00
|
|
|
|
using System.Security.Authentication;
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
|
2014-10-29 18:57:34 +08:00
|
|
|
|
namespace WebSocketSharp.Net
|
2014-10-28 18:24:00 +08:00
|
|
|
|
{
|
2014-10-30 18:09:12 +08:00
|
|
|
|
/// <summary>
|
2017-05-10 13:22:40 +08:00
|
|
|
|
/// Stores the parameters for the <see cref="SslStream"/> used by clients.
|
2014-10-30 18:09:12 +08:00
|
|
|
|
/// </summary>
|
2017-05-11 14:20:31 +08:00
|
|
|
|
public class ClientSslConfiguration
|
2014-10-30 18:09:12 +08:00
|
|
|
|
{
|
2014-11-04 10:01:40 +08:00
|
|
|
|
#region Private Fields
|
|
|
|
|
|
2017-05-11 14:20:31 +08:00
|
|
|
|
private bool _checkCertRevocation;
|
|
|
|
|
private LocalCertificateSelectionCallback _clientCertSelectionCallback;
|
|
|
|
|
private X509CertificateCollection _clientCerts;
|
|
|
|
|
private SslProtocols _enabledSslProtocols;
|
|
|
|
|
private RemoteCertificateValidationCallback _serverCertValidationCallback;
|
|
|
|
|
private string _targetHost;
|
2014-11-04 10:01:40 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2014-10-30 18:09:12 +08:00
|
|
|
|
#region Public Constructors
|
|
|
|
|
|
2017-05-11 14:26:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="ClientSslConfiguration"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ClientSslConfiguration ()
|
|
|
|
|
{
|
|
|
|
|
_enabledSslProtocols = SslProtocols.Default;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 18:09:12 +08:00
|
|
|
|
/// <summary>
|
2017-05-10 13:38:05 +08:00
|
|
|
|
/// Initializes a new instance of the <see cref="ClientSslConfiguration"/> class
|
|
|
|
|
/// with the specified <paramref name="targetHost"/>.
|
2014-10-30 18:09:12 +08:00
|
|
|
|
/// </summary>
|
2014-11-03 14:11:43 +08:00
|
|
|
|
/// <param name="targetHost">
|
2017-05-10 13:38:05 +08:00
|
|
|
|
/// A <see cref="string"/> that represents the name of the server that
|
|
|
|
|
/// will share a secure connection.
|
2014-10-30 18:09:12 +08:00
|
|
|
|
/// </param>
|
2014-11-06 10:51:38 +08:00
|
|
|
|
public ClientSslConfiguration (string targetHost)
|
2014-10-30 18:09:12 +08:00
|
|
|
|
{
|
2017-05-11 14:45:44 +08:00
|
|
|
|
_targetHost = targetHost;
|
|
|
|
|
_enabledSslProtocols = SslProtocols.Default;
|
2014-10-30 18:09:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 14:38:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies the parameters from the specified <paramref name="configuration"/> to
|
|
|
|
|
/// a new instance of the <see cref="ClientSslConfiguration"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="configuration">
|
|
|
|
|
/// A <see cref="ClientSslConfiguration"/> from which to copy.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <exception cref="ArgumentNullException">
|
|
|
|
|
/// <paramref name="configuration"/> is <see langword="null"/>.
|
|
|
|
|
/// </exception>
|
|
|
|
|
public ClientSslConfiguration (ClientSslConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
if (configuration == null)
|
|
|
|
|
throw new ArgumentNullException ("configuration");
|
|
|
|
|
|
|
|
|
|
_checkCertRevocation = configuration._checkCertRevocation;
|
|
|
|
|
_clientCertSelectionCallback = configuration._clientCertSelectionCallback;
|
|
|
|
|
_clientCerts = configuration._clientCerts;
|
|
|
|
|
_enabledSslProtocols = configuration._enabledSslProtocols;
|
|
|
|
|
_serverCertValidationCallback = configuration._serverCertValidationCallback;
|
|
|
|
|
_targetHost = configuration._targetHost;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 18:09:12 +08:00
|
|
|
|
#endregion
|
2014-10-28 18:24:00 +08:00
|
|
|
|
|
2014-10-30 18:09:12 +08:00
|
|
|
|
#region Public Properties
|
2014-10-28 18:24:00 +08:00
|
|
|
|
|
2017-05-11 14:20:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a value indicating whether the certificate revocation
|
|
|
|
|
/// list is checked during authentication.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// <c>true</c> if the certificate revocation list is checked during
|
|
|
|
|
/// authentication; otherwise, <c>false</c>.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The default value is <c>false</c>.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </value>
|
|
|
|
|
public bool CheckCertificateRevocation {
|
|
|
|
|
get {
|
|
|
|
|
return _checkCertRevocation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set {
|
|
|
|
|
_checkCertRevocation = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 18:09:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the collection that contains client certificates.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
|
|
|
|
/// A <see cref="X509CertificateCollection"/> that contains client certificates.
|
|
|
|
|
/// </value>
|
2014-11-05 10:06:55 +08:00
|
|
|
|
public X509CertificateCollection ClientCertificates {
|
|
|
|
|
get {
|
2017-05-10 15:34:32 +08:00
|
|
|
|
return _clientCerts;
|
2014-11-05 10:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set {
|
2017-05-10 15:34:32 +08:00
|
|
|
|
_clientCerts = value;
|
2014-11-05 10:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-05-10 14:27:57 +08:00
|
|
|
|
/// Gets or sets the callback used to select the certificate to
|
|
|
|
|
/// supply to the server.
|
2014-11-05 10:06:55 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
2017-05-10 14:27:57 +08:00
|
|
|
|
/// No certificate is supplied if the callback returns
|
|
|
|
|
/// <see langword="null"/>.
|
2014-11-05 10:06:55 +08:00
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <value>
|
2017-05-10 14:27:57 +08:00
|
|
|
|
/// <para>
|
|
|
|
|
/// A <see cref="LocalCertificateSelectionCallback"/> delegate that
|
|
|
|
|
/// invokes the method called for selecting the certificate.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The default value is a delegate that invokes a method that
|
|
|
|
|
/// only returns <see langword="null"/>.
|
|
|
|
|
/// </para>
|
2014-11-05 10:06:55 +08:00
|
|
|
|
/// </value>
|
|
|
|
|
public LocalCertificateSelectionCallback ClientCertificateSelectionCallback {
|
|
|
|
|
get {
|
2017-05-11 14:20:31 +08:00
|
|
|
|
if (_clientCertSelectionCallback == null)
|
|
|
|
|
_clientCertSelectionCallback = defaultSelectClientCertificate;
|
|
|
|
|
|
|
|
|
|
return _clientCertSelectionCallback;
|
2014-11-05 10:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set {
|
2017-05-11 14:20:31 +08:00
|
|
|
|
_clientCertSelectionCallback = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the protocols used for authentication.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The <see cref="SslProtocols"/> enum values that represent
|
|
|
|
|
/// the protocols used for authentication.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The default value is <see cref="SslProtocols.Default"/>.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </value>
|
|
|
|
|
public SslProtocols EnabledSslProtocols {
|
|
|
|
|
get {
|
|
|
|
|
return _enabledSslProtocols;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set {
|
|
|
|
|
_enabledSslProtocols = value;
|
2014-11-05 10:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-28 18:24:00 +08:00
|
|
|
|
|
2014-11-04 10:01:40 +08:00
|
|
|
|
/// <summary>
|
2017-05-10 14:33:59 +08:00
|
|
|
|
/// Gets or sets the callback used to validate the certificate
|
|
|
|
|
/// supplied by the server.
|
2014-11-04 10:01:40 +08:00
|
|
|
|
/// </summary>
|
2014-11-05 10:06:55 +08:00
|
|
|
|
/// <remarks>
|
2017-05-10 14:33:59 +08:00
|
|
|
|
/// The certificate is valid if the callback returns <c>true</c>.
|
2014-11-05 10:06:55 +08:00
|
|
|
|
/// </remarks>
|
2014-11-04 10:01:40 +08:00
|
|
|
|
/// <value>
|
2017-05-10 14:33:59 +08:00
|
|
|
|
/// <para>
|
|
|
|
|
/// A <see cref="RemoteCertificateValidationCallback"/> delegate that
|
|
|
|
|
/// invokes the method called for validating the certificate.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The default value is a delegate that invokes a method that
|
|
|
|
|
/// only returns <c>true</c>.
|
|
|
|
|
/// </para>
|
2014-11-04 10:01:40 +08:00
|
|
|
|
/// </value>
|
|
|
|
|
public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
|
|
|
|
|
get {
|
2017-05-11 14:20:31 +08:00
|
|
|
|
if (_serverCertValidationCallback == null)
|
|
|
|
|
_serverCertValidationCallback = defaultValidateServerCertificate;
|
|
|
|
|
|
|
|
|
|
return _serverCertValidationCallback;
|
2014-11-04 10:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set {
|
2017-05-11 14:20:31 +08:00
|
|
|
|
_serverCertValidationCallback = value;
|
2014-11-04 10:01:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-03 14:11:43 +08:00
|
|
|
|
/// <summary>
|
2017-05-10 15:31:01 +08:00
|
|
|
|
/// Gets or sets the target host server name.
|
2014-11-03 14:11:43 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
2017-05-12 14:53:24 +08:00
|
|
|
|
/// <para>
|
|
|
|
|
/// A <see cref="string"/> or <see langword="null"/>
|
|
|
|
|
/// if not specified.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// That string represents the name of the server that
|
|
|
|
|
/// will share a secure connection with a client.
|
|
|
|
|
/// </para>
|
2014-11-03 14:11:43 +08:00
|
|
|
|
/// </value>
|
2014-11-05 10:06:55 +08:00
|
|
|
|
public string TargetHost {
|
|
|
|
|
get {
|
2017-05-10 15:38:01 +08:00
|
|
|
|
return _targetHost;
|
2014-11-05 10:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set {
|
2017-05-10 15:38:01 +08:00
|
|
|
|
_targetHost = value;
|
2014-11-05 10:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-03 14:11:43 +08:00
|
|
|
|
|
2014-10-30 18:09:12 +08:00
|
|
|
|
#endregion
|
2017-05-11 14:20:31 +08:00
|
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
|
|
|
|
|
|
private static X509Certificate defaultSelectClientCertificate (
|
|
|
|
|
object sender,
|
|
|
|
|
string targetHost,
|
|
|
|
|
X509CertificateCollection clientCertificates,
|
|
|
|
|
X509Certificate serverCertificate,
|
|
|
|
|
string[] acceptableIssuers
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool defaultValidateServerCertificate (
|
|
|
|
|
object sender,
|
|
|
|
|
X509Certificate certificate,
|
|
|
|
|
X509Chain chain,
|
|
|
|
|
SslPolicyErrors sslPolicyErrors
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2014-10-30 18:09:12 +08:00
|
|
|
|
}
|
2014-11-08 14:57:47 +08:00
|
|
|
|
}
|