PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/websocket-sharp/Net/SslConfiguration.cs

https://gitlab.com/OriumVR/websocket-sharp
C# | 172 lines | 65 code | 20 blank | 87 comment | 0 complexity | 076c4cb9142d8724845b04c490697cb8 MD5 | raw file
  1. #region License
  2. /*
  3. * SslConfiguration.cs
  4. *
  5. * This code is derived from ClientSslConfiguration.cs.
  6. *
  7. * The MIT License
  8. *
  9. * Copyright (c) 2014 liryna
  10. * Copyright (c) 2014 sta.blockhead
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. #endregion
  31. #region Authors
  32. /*
  33. * Authors:
  34. * - Liryna <liryna.stark@gmail.com>
  35. */
  36. #endregion
  37. using System.Net.Security;
  38. using System.Security.Authentication;
  39. namespace WebSocketSharp.Net
  40. {
  41. /// <summary>
  42. /// Stores the parameters used to configure a <see cref="SslStream"/> instance.
  43. /// </summary>
  44. /// <remarks>
  45. /// The SslConfiguration class is an abstract class.
  46. /// </remarks>
  47. public abstract class SslConfiguration
  48. {
  49. #region Private Fields
  50. private LocalCertificateSelectionCallback _certSelectionCallback;
  51. private RemoteCertificateValidationCallback _certValidationCallback;
  52. private bool _checkCertRevocation;
  53. private SslProtocols _enabledProtocols;
  54. #endregion
  55. #region Protected Constructors
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="SslConfiguration"/> class with
  58. /// the specified <paramref name="enabledSslProtocols"/> and
  59. /// <paramref name="checkCertificateRevocation"/>.
  60. /// </summary>
  61. /// <param name="enabledSslProtocols">
  62. /// The <see cref="SslProtocols"/> enum value that represents the protocols used for
  63. /// authentication.
  64. /// </param>
  65. /// <param name="checkCertificateRevocation">
  66. /// <c>true</c> if the certificate revocation list is checked during authentication;
  67. /// otherwise, <c>false</c>.
  68. /// </param>
  69. protected SslConfiguration (SslProtocols enabledSslProtocols, bool checkCertificateRevocation)
  70. {
  71. _enabledProtocols = enabledSslProtocols;
  72. _checkCertRevocation = checkCertificateRevocation;
  73. }
  74. #endregion
  75. #region Protected Properties
  76. /// <summary>
  77. /// Gets or sets the callback used to select a certificate to supply to the remote party.
  78. /// </summary>
  79. /// <remarks>
  80. /// If this callback returns <see langword="null"/>, no certificate will be supplied.
  81. /// </remarks>
  82. /// <value>
  83. /// A <see cref="LocalCertificateSelectionCallback"/> delegate that references the method
  84. /// used to select a certificate. The default value is a function that only returns
  85. /// <see langword="null"/>.
  86. /// </value>
  87. protected LocalCertificateSelectionCallback CertificateSelectionCallback {
  88. get {
  89. return _certSelectionCallback ??
  90. (_certSelectionCallback =
  91. (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) =>
  92. null);
  93. }
  94. set {
  95. _certSelectionCallback = value;
  96. }
  97. }
  98. /// <summary>
  99. /// Gets or sets the callback used to validate the certificate supplied by the remote party.
  100. /// </summary>
  101. /// <remarks>
  102. /// If this callback returns <c>true</c>, the certificate will be valid.
  103. /// </remarks>
  104. /// <value>
  105. /// A <see cref="RemoteCertificateValidationCallback"/> delegate that references the method
  106. /// used to validate the certificate. The default value is a function that only returns
  107. /// <c>true</c>.
  108. /// </value>
  109. protected RemoteCertificateValidationCallback CertificateValidationCallback {
  110. get {
  111. return _certValidationCallback ??
  112. (_certValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true);
  113. }
  114. set {
  115. _certValidationCallback = value;
  116. }
  117. }
  118. #endregion
  119. #region Public Properties
  120. /// <summary>
  121. /// Gets or sets a value indicating whether the certificate revocation list is checked
  122. /// during authentication.
  123. /// </summary>
  124. /// <value>
  125. /// <c>true</c> if the certificate revocation list is checked; otherwise, <c>false</c>.
  126. /// </value>
  127. public bool CheckCertificateRevocation {
  128. get {
  129. return _checkCertRevocation;
  130. }
  131. set {
  132. _checkCertRevocation = value;
  133. }
  134. }
  135. /// <summary>
  136. /// Gets or sets the SSL protocols used for authentication.
  137. /// </summary>
  138. /// <value>
  139. /// The <see cref="SslProtocols"/> enum value that represents the protocols used for
  140. /// authentication.
  141. /// </value>
  142. public SslProtocols EnabledSslProtocols {
  143. get {
  144. return _enabledProtocols;
  145. }
  146. set {
  147. _enabledProtocols = value;
  148. }
  149. }
  150. #endregion
  151. }
  152. }