websocket-sharp/websocket-sharp/Net/ServerSslConfiguration.cs
2017-05-05 14:57:35 +09:00

175 lines
5.4 KiB
C#

#region License
/*
* ServerSslConfiguration.cs
*
* The MIT License
*
* Copyright (c) 2014 liryna
* Copyright (c) 2014 sta.blockhead
*
* 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:
* - Liryna <liryna.stark@gmail.com>
*/
#endregion
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace WebSocketSharp.Net
{
/// <summary>
/// Stores the parameters used to configure the underlying <see cref="SslStream"/>
/// for servers.
/// </summary>
public class ServerSslConfiguration : SslConfiguration
{
#region Private Fields
private X509Certificate2 _serverCert;
private bool _clientCertRequired;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ServerSslConfiguration"/> class
/// with the specified <paramref name="serverCertificate"/>.
/// </summary>
/// <param name="serverCertificate">
/// A <see cref="X509Certificate2"/> that represents an X.509 certificate
/// used to authenticate the server.
/// </param>
public ServerSslConfiguration (X509Certificate2 serverCertificate)
: this (serverCertificate, false, SslProtocols.Default, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ServerSslConfiguration"/> class
/// with the specified <paramref name="serverCertificate"/>,
/// <paramref name="clientCertificateRequired"/>,
/// <paramref name="enabledSslProtocols"/>,
/// and <paramref name="checkCertificateRevocation"/>.
/// </summary>
/// <param name="serverCertificate">
/// A <see cref="X509Certificate2"/> that represents an X.509 certificate
/// used to authenticate the server.
/// </param>
/// <param name="clientCertificateRequired">
/// <c>true</c> if the client is asked for a certificate for authentication;
/// otherwise, <c>false</c>.
/// </param>
/// <param name="enabledSslProtocols">
/// The <see cref="SslProtocols"/> enum values that represent the protocols
/// used for authentication.
/// </param>
/// <param name="checkCertificateRevocation">
/// <c>true</c> if the certificate revocation list is checked during
/// authentication; otherwise, <c>false</c>.
/// </param>
public ServerSslConfiguration (
X509Certificate2 serverCertificate,
bool clientCertificateRequired,
SslProtocols enabledSslProtocols,
bool checkCertificateRevocation
)
: base (enabledSslProtocols, checkCertificateRevocation)
{
_serverCert = serverCertificate;
_clientCertRequired = clientCertificateRequired;
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets a value indicating whether the client is asked for
/// a certificate for authentication.
/// </summary>
/// <value>
/// <c>true</c> if the client is asked for a certificate for authentication;
/// otherwise, <c>false</c>.
/// </value>
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>
/// The certificate is valid if the callback returns <c>true</c>.
/// </remarks>
/// <value>
/// <para>
/// A <see cref="RemoteCertificateValidationCallback"/> delegate.
/// </para>
/// <para>
/// It invokes the method called for validating the certificate.
/// The default value is a delegate that invokes a method that
/// only returns <c>true</c>.
/// </para>
/// </value>
public RemoteCertificateValidationCallback ClientCertificateValidationCallback {
get {
return CertificateValidationCallback;
}
set {
CertificateValidationCallback = value;
}
}
/// <summary>
/// Gets or sets the certificate used to authenticate the server.
/// </summary>
/// <value>
/// A <see cref="X509Certificate2"/> that represents an X.509 certificate
/// used to authenticate the server.
/// </value>
public X509Certificate2 ServerCertificate {
get {
return _serverCert;
}
set {
_serverCert = value;
}
}
#endregion
}
}