123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- #region License
- /*
- * ClientSslConfiguration.cs
- *
- * The MIT License
- *
- * Copyright (c) 2014 liryna
- * Copyright (c) 2014-2023 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;
- using System.Net.Security;
- using System.Security.Authentication;
- using System.Security.Cryptography.X509Certificates;
- namespace WebSocketSharp.Net
- {
- /// <summary>
- /// Stores the parameters for the <see cref="SslStream"/> used by clients.
- /// </summary>
- public class ClientSslConfiguration
- {
- #region Private Fields
- private bool _checkCertRevocation;
- private LocalCertificateSelectionCallback _clientCertSelectionCallback;
- private X509CertificateCollection _clientCerts;
- private SslProtocols _enabledSslProtocols;
- private RemoteCertificateValidationCallback _serverCertValidationCallback;
- private string _targetHost;
- #endregion
- #region Public Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="ClientSslConfiguration"/>
- /// class with the specified target host name.
- /// </summary>
- /// <param name="targetHost">
- /// A <see cref="string"/> that specifies the name of the server that
- /// will share a secure connection with a client.
- /// </param>
- /// <exception cref="ArgumentNullException">
- /// <paramref name="targetHost"/> is <see langword="null"/>.
- /// </exception>
- /// <exception cref="ArgumentException">
- /// <paramref name="targetHost"/> is an empty string.
- /// </exception>
- public ClientSslConfiguration (string targetHost)
- {
- if (targetHost == null)
- throw new ArgumentNullException ("targetHost");
- if (targetHost.Length == 0)
- throw new ArgumentException ("An empty string.", "targetHost");
- _targetHost = targetHost;
- _enabledSslProtocols = SslProtocols.None;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="ClientSslConfiguration"/>
- /// class copying from the specified configuration.
- /// </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;
- }
- #endregion
- #region Public Properties
- /// <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;
- }
- }
- /// <summary>
- /// Gets or sets the collection of the certificates from which to select
- /// one to supply to the server.
- /// </summary>
- /// <value>
- /// <para>
- /// A <see cref="X509CertificateCollection"/> that contains
- /// the certificates from which to select.
- /// </para>
- /// <para>
- /// <see langword="null"/> if not present.
- /// </para>
- /// <para>
- /// The default value is <see langword="null"/>.
- /// </para>
- /// </value>
- public X509CertificateCollection ClientCertificates {
- get {
- return _clientCerts;
- }
- set {
- _clientCerts = value;
- }
- }
- /// <summary>
- /// Gets or sets the callback used to select the certificate to supply to
- /// the server.
- /// </summary>
- /// <remarks>
- /// No certificate is supplied if the callback returns <see langword="null"/>.
- /// </remarks>
- /// <value>
- /// <para>
- /// A <see cref="LocalCertificateSelectionCallback"/> delegate.
- /// </para>
- /// <para>
- /// The delegate invokes the method called when a client selects
- /// the certificate.
- /// </para>
- /// <para>
- /// The default value is a delegate that invokes a method that only
- /// returns <see langword="null"/>.
- /// </para>
- /// </value>
- public LocalCertificateSelectionCallback ClientCertificateSelectionCallback {
- get {
- if (_clientCertSelectionCallback == null)
- _clientCertSelectionCallback = defaultSelectClientCertificate;
- return _clientCertSelectionCallback;
- }
- set {
- _clientCertSelectionCallback = value;
- }
- }
- /// <summary>
- /// Gets or sets the enabled versions of the SSL/TLS protocols.
- /// </summary>
- /// <value>
- /// <para>
- /// Any of the <see cref="SslProtocols"/> enum values.
- /// </para>
- /// <para>
- /// It represents the enabled versions of the SSL/TLS protocols.
- /// </para>
- /// <para>
- /// The default value is <see cref="SslProtocols.None"/>.
- /// </para>
- /// </value>
- public SslProtocols EnabledSslProtocols {
- get {
- return _enabledSslProtocols;
- }
- set {
- _enabledSslProtocols = value;
- }
- }
- /// <summary>
- /// Gets or sets the callback used to validate the certificate supplied by
- /// the server.
- /// </summary>
- /// <remarks>
- /// The certificate is valid if the callback returns <c>true</c>.
- /// </remarks>
- /// <value>
- /// <para>
- /// A <see cref="RemoteCertificateValidationCallback"/> delegate.
- /// </para>
- /// <para>
- /// The delegate invokes the method called when a client validates
- /// the certificate.
- /// </para>
- /// <para>
- /// The default value is a delegate that invokes a method that only
- /// returns <c>true</c>.
- /// </para>
- /// </value>
- public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
- get {
- if (_serverCertValidationCallback == null)
- _serverCertValidationCallback = defaultValidateServerCertificate;
- return _serverCertValidationCallback;
- }
- set {
- _serverCertValidationCallback = value;
- }
- }
- /// <summary>
- /// Gets or sets the target host name.
- /// </summary>
- /// <value>
- /// A <see cref="string"/> that represents the name of the server that
- /// will share a secure connection with a client.
- /// </value>
- /// <exception cref="ArgumentNullException">
- /// The value specified for a set operation is <see langword="null"/>.
- /// </exception>
- /// <exception cref="ArgumentException">
- /// The value specified for a set operation is an empty string.
- /// </exception>
- public string TargetHost {
- get {
- return _targetHost;
- }
- set {
- if (value == null)
- throw new ArgumentNullException ("value");
- if (value.Length == 0)
- throw new ArgumentException ("An empty string.", "value");
- _targetHost = value;
- }
- }
- #endregion
- #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
- }
- }
|