ServerSslConfiguration.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #region License
  2. /*
  3. * ServerSslConfiguration.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2014 liryna
  8. * Copyright (c) 2014-2023 sta.blockhead
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #endregion
  29. #region Authors
  30. /*
  31. * Authors:
  32. * - Liryna <liryna.stark@gmail.com>
  33. */
  34. #endregion
  35. using System;
  36. using System.Net.Security;
  37. using System.Security.Authentication;
  38. using System.Security.Cryptography.X509Certificates;
  39. namespace WebSocketSharp.Net
  40. {
  41. /// <summary>
  42. /// Stores the parameters for the <see cref="SslStream"/> used by servers.
  43. /// </summary>
  44. public class ServerSslConfiguration
  45. {
  46. #region Private Fields
  47. private bool _checkCertRevocation;
  48. private bool _clientCertRequired;
  49. private RemoteCertificateValidationCallback _clientCertValidationCallback;
  50. private SslProtocols _enabledSslProtocols;
  51. private X509Certificate2 _serverCert;
  52. #endregion
  53. #region Public Constructors
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="ServerSslConfiguration"/>
  56. /// class.
  57. /// </summary>
  58. public ServerSslConfiguration ()
  59. {
  60. _enabledSslProtocols = SslProtocols.None;
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="ServerSslConfiguration"/>
  64. /// class copying from the specified configuration.
  65. /// </summary>
  66. /// <param name="configuration">
  67. /// A <see cref="ServerSslConfiguration"/> from which to copy.
  68. /// </param>
  69. /// <exception cref="ArgumentNullException">
  70. /// <paramref name="configuration"/> is <see langword="null"/>.
  71. /// </exception>
  72. public ServerSslConfiguration (ServerSslConfiguration configuration)
  73. {
  74. if (configuration == null)
  75. throw new ArgumentNullException ("configuration");
  76. _checkCertRevocation = configuration._checkCertRevocation;
  77. _clientCertRequired = configuration._clientCertRequired;
  78. _clientCertValidationCallback = configuration._clientCertValidationCallback;
  79. _enabledSslProtocols = configuration._enabledSslProtocols;
  80. _serverCert = configuration._serverCert;
  81. }
  82. #endregion
  83. #region Public Properties
  84. /// <summary>
  85. /// Gets or sets a value indicating whether the certificate revocation
  86. /// list is checked during authentication.
  87. /// </summary>
  88. /// <value>
  89. /// <para>
  90. /// <c>true</c> if the certificate revocation list is checked during
  91. /// authentication; otherwise, <c>false</c>.
  92. /// </para>
  93. /// <para>
  94. /// The default value is <c>false</c>.
  95. /// </para>
  96. /// </value>
  97. public bool CheckCertificateRevocation {
  98. get {
  99. return _checkCertRevocation;
  100. }
  101. set {
  102. _checkCertRevocation = value;
  103. }
  104. }
  105. /// <summary>
  106. /// Gets or sets a value indicating whether the client is asked for
  107. /// a certificate for authentication.
  108. /// </summary>
  109. /// <value>
  110. /// <para>
  111. /// <c>true</c> if the client is asked for a certificate for
  112. /// authentication; otherwise, <c>false</c>.
  113. /// </para>
  114. /// <para>
  115. /// The default value is <c>false</c>.
  116. /// </para>
  117. /// </value>
  118. public bool ClientCertificateRequired {
  119. get {
  120. return _clientCertRequired;
  121. }
  122. set {
  123. _clientCertRequired = value;
  124. }
  125. }
  126. /// <summary>
  127. /// Gets or sets the callback used to validate the certificate supplied by
  128. /// the client.
  129. /// </summary>
  130. /// <remarks>
  131. /// The certificate is valid if the callback returns <c>true</c>.
  132. /// </remarks>
  133. /// <value>
  134. /// <para>
  135. /// A <see cref="RemoteCertificateValidationCallback"/> delegate.
  136. /// </para>
  137. /// <para>
  138. /// The delegate invokes the method called when the server validates
  139. /// the certificate.
  140. /// </para>
  141. /// <para>
  142. /// The default value is a delegate that invokes a method that only
  143. /// returns <c>true</c>.
  144. /// </para>
  145. /// </value>
  146. public RemoteCertificateValidationCallback ClientCertificateValidationCallback {
  147. get {
  148. if (_clientCertValidationCallback == null)
  149. _clientCertValidationCallback = defaultValidateClientCertificate;
  150. return _clientCertValidationCallback;
  151. }
  152. set {
  153. _clientCertValidationCallback = value;
  154. }
  155. }
  156. /// <summary>
  157. /// Gets or sets the enabled versions of the SSL/TLS protocols.
  158. /// </summary>
  159. /// <value>
  160. /// <para>
  161. /// Any of the <see cref="SslProtocols"/> enum values.
  162. /// </para>
  163. /// <para>
  164. /// It represents the enabled versions of the SSL/TLS protocols.
  165. /// </para>
  166. /// <para>
  167. /// The default value is <see cref="SslProtocols.None"/>.
  168. /// </para>
  169. /// </value>
  170. public SslProtocols EnabledSslProtocols {
  171. get {
  172. return _enabledSslProtocols;
  173. }
  174. set {
  175. _enabledSslProtocols = value;
  176. }
  177. }
  178. /// <summary>
  179. /// Gets or sets the certificate used to authenticate the server.
  180. /// </summary>
  181. /// <value>
  182. /// <para>
  183. /// A <see cref="X509Certificate2"/> that represents an X.509 certificate.
  184. /// </para>
  185. /// <para>
  186. /// <see langword="null"/> if not present.
  187. /// </para>
  188. /// <para>
  189. /// The default value is <see langword="null"/>.
  190. /// </para>
  191. /// </value>
  192. public X509Certificate2 ServerCertificate {
  193. get {
  194. return _serverCert;
  195. }
  196. set {
  197. _serverCert = value;
  198. }
  199. }
  200. #endregion
  201. #region Private Methods
  202. private static bool defaultValidateClientCertificate (
  203. object sender,
  204. X509Certificate certificate,
  205. X509Chain chain,
  206. SslPolicyErrors sslPolicyErrors
  207. )
  208. {
  209. return true;
  210. }
  211. #endregion
  212. }
  213. }