ClientSslConfiguration.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #region License
  2. /*
  3. * ClientSslConfiguration.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 clients.
  43. /// </summary>
  44. public class ClientSslConfiguration
  45. {
  46. #region Private Fields
  47. private bool _checkCertRevocation;
  48. private LocalCertificateSelectionCallback _clientCertSelectionCallback;
  49. private X509CertificateCollection _clientCerts;
  50. private SslProtocols _enabledSslProtocols;
  51. private RemoteCertificateValidationCallback _serverCertValidationCallback;
  52. private string _targetHost;
  53. #endregion
  54. #region Public Constructors
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="ClientSslConfiguration"/>
  57. /// class with the specified target host name.
  58. /// </summary>
  59. /// <param name="targetHost">
  60. /// A <see cref="string"/> that specifies the name of the server that
  61. /// will share a secure connection with a client.
  62. /// </param>
  63. /// <exception cref="ArgumentNullException">
  64. /// <paramref name="targetHost"/> is <see langword="null"/>.
  65. /// </exception>
  66. /// <exception cref="ArgumentException">
  67. /// <paramref name="targetHost"/> is an empty string.
  68. /// </exception>
  69. public ClientSslConfiguration (string targetHost)
  70. {
  71. if (targetHost == null)
  72. throw new ArgumentNullException ("targetHost");
  73. if (targetHost.Length == 0)
  74. throw new ArgumentException ("An empty string.", "targetHost");
  75. _targetHost = targetHost;
  76. _enabledSslProtocols = SslProtocols.None;
  77. }
  78. /// <summary>
  79. /// Initializes a new instance of the <see cref="ClientSslConfiguration"/>
  80. /// class copying from the specified configuration.
  81. /// </summary>
  82. /// <param name="configuration">
  83. /// A <see cref="ClientSslConfiguration"/> from which to copy.
  84. /// </param>
  85. /// <exception cref="ArgumentNullException">
  86. /// <paramref name="configuration"/> is <see langword="null"/>.
  87. /// </exception>
  88. public ClientSslConfiguration (ClientSslConfiguration configuration)
  89. {
  90. if (configuration == null)
  91. throw new ArgumentNullException ("configuration");
  92. _checkCertRevocation = configuration._checkCertRevocation;
  93. _clientCertSelectionCallback = configuration._clientCertSelectionCallback;
  94. _clientCerts = configuration._clientCerts;
  95. _enabledSslProtocols = configuration._enabledSslProtocols;
  96. _serverCertValidationCallback = configuration._serverCertValidationCallback;
  97. _targetHost = configuration._targetHost;
  98. }
  99. #endregion
  100. #region Public Properties
  101. /// <summary>
  102. /// Gets or sets a value indicating whether the certificate revocation
  103. /// list is checked during authentication.
  104. /// </summary>
  105. /// <value>
  106. /// <para>
  107. /// <c>true</c> if the certificate revocation list is checked during
  108. /// authentication; otherwise, <c>false</c>.
  109. /// </para>
  110. /// <para>
  111. /// The default value is <c>false</c>.
  112. /// </para>
  113. /// </value>
  114. public bool CheckCertificateRevocation {
  115. get {
  116. return _checkCertRevocation;
  117. }
  118. set {
  119. _checkCertRevocation = value;
  120. }
  121. }
  122. /// <summary>
  123. /// Gets or sets the collection of the certificates from which to select
  124. /// one to supply to the server.
  125. /// </summary>
  126. /// <value>
  127. /// <para>
  128. /// A <see cref="X509CertificateCollection"/> that contains
  129. /// the certificates from which to select.
  130. /// </para>
  131. /// <para>
  132. /// <see langword="null"/> if not present.
  133. /// </para>
  134. /// <para>
  135. /// The default value is <see langword="null"/>.
  136. /// </para>
  137. /// </value>
  138. public X509CertificateCollection ClientCertificates {
  139. get {
  140. return _clientCerts;
  141. }
  142. set {
  143. _clientCerts = value;
  144. }
  145. }
  146. /// <summary>
  147. /// Gets or sets the callback used to select the certificate to supply to
  148. /// the server.
  149. /// </summary>
  150. /// <remarks>
  151. /// No certificate is supplied if the callback returns <see langword="null"/>.
  152. /// </remarks>
  153. /// <value>
  154. /// <para>
  155. /// A <see cref="LocalCertificateSelectionCallback"/> delegate.
  156. /// </para>
  157. /// <para>
  158. /// The delegate invokes the method called when a client selects
  159. /// the certificate.
  160. /// </para>
  161. /// <para>
  162. /// The default value is a delegate that invokes a method that only
  163. /// returns <see langword="null"/>.
  164. /// </para>
  165. /// </value>
  166. public LocalCertificateSelectionCallback ClientCertificateSelectionCallback {
  167. get {
  168. if (_clientCertSelectionCallback == null)
  169. _clientCertSelectionCallback = defaultSelectClientCertificate;
  170. return _clientCertSelectionCallback;
  171. }
  172. set {
  173. _clientCertSelectionCallback = value;
  174. }
  175. }
  176. /// <summary>
  177. /// Gets or sets the enabled versions of the SSL/TLS protocols.
  178. /// </summary>
  179. /// <value>
  180. /// <para>
  181. /// Any of the <see cref="SslProtocols"/> enum values.
  182. /// </para>
  183. /// <para>
  184. /// It represents the enabled versions of the SSL/TLS protocols.
  185. /// </para>
  186. /// <para>
  187. /// The default value is <see cref="SslProtocols.None"/>.
  188. /// </para>
  189. /// </value>
  190. public SslProtocols EnabledSslProtocols {
  191. get {
  192. return _enabledSslProtocols;
  193. }
  194. set {
  195. _enabledSslProtocols = value;
  196. }
  197. }
  198. /// <summary>
  199. /// Gets or sets the callback used to validate the certificate supplied by
  200. /// the server.
  201. /// </summary>
  202. /// <remarks>
  203. /// The certificate is valid if the callback returns <c>true</c>.
  204. /// </remarks>
  205. /// <value>
  206. /// <para>
  207. /// A <see cref="RemoteCertificateValidationCallback"/> delegate.
  208. /// </para>
  209. /// <para>
  210. /// The delegate invokes the method called when a client validates
  211. /// the certificate.
  212. /// </para>
  213. /// <para>
  214. /// The default value is a delegate that invokes a method that only
  215. /// returns <c>true</c>.
  216. /// </para>
  217. /// </value>
  218. public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
  219. get {
  220. if (_serverCertValidationCallback == null)
  221. _serverCertValidationCallback = defaultValidateServerCertificate;
  222. return _serverCertValidationCallback;
  223. }
  224. set {
  225. _serverCertValidationCallback = value;
  226. }
  227. }
  228. /// <summary>
  229. /// Gets or sets the target host name.
  230. /// </summary>
  231. /// <value>
  232. /// A <see cref="string"/> that represents the name of the server that
  233. /// will share a secure connection with a client.
  234. /// </value>
  235. /// <exception cref="ArgumentNullException">
  236. /// The value specified for a set operation is <see langword="null"/>.
  237. /// </exception>
  238. /// <exception cref="ArgumentException">
  239. /// The value specified for a set operation is an empty string.
  240. /// </exception>
  241. public string TargetHost {
  242. get {
  243. return _targetHost;
  244. }
  245. set {
  246. if (value == null)
  247. throw new ArgumentNullException ("value");
  248. if (value.Length == 0)
  249. throw new ArgumentException ("An empty string.", "value");
  250. _targetHost = value;
  251. }
  252. }
  253. #endregion
  254. #region Private Methods
  255. private static X509Certificate defaultSelectClientCertificate (
  256. object sender,
  257. string targetHost,
  258. X509CertificateCollection clientCertificates,
  259. X509Certificate serverCertificate,
  260. string[] acceptableIssuers
  261. )
  262. {
  263. return null;
  264. }
  265. private static bool defaultValidateServerCertificate (
  266. object sender,
  267. X509Certificate certificate,
  268. X509Chain chain,
  269. SslPolicyErrors sslPolicyErrors
  270. )
  271. {
  272. return true;
  273. }
  274. #endregion
  275. }
  276. }