WatsonTcpServerSslConfiguration.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. namespace WatsonTcp
  2. {
  3. using System;
  4. using System.Net.Security;
  5. using System.Security.Cryptography.X509Certificates;
  6. /// <summary>
  7. /// Stores the parameters for the <see cref="SslStream"/> used by servers.
  8. /// </summary>
  9. public class WatsonTcpServerSslConfiguration
  10. {
  11. #region Public-Members
  12. /// <summary>
  13. /// Gets or sets a value indicating whether the client is asked for
  14. /// a certificate for authentication.
  15. /// </summary>
  16. public bool ClientCertificateRequired
  17. {
  18. get
  19. {
  20. return _ClientCertRequired;
  21. }
  22. set
  23. {
  24. _ClientCertRequired = value;
  25. }
  26. }
  27. /// <summary>
  28. /// Gets or sets a <see cref="RemoteCertificateValidationCallback"/> delegate responsible
  29. /// for validating the certificate supplied by the remote party.
  30. /// </summary>
  31. /// <remarks>
  32. /// The default delegate returns true for all certificates
  33. /// </remarks>
  34. public RemoteCertificateValidationCallback ClientCertificateValidationCallback
  35. {
  36. get
  37. {
  38. if (_ClientCertValidationCallback == null)
  39. _ClientCertValidationCallback = DefaultValidateClientCertificate;
  40. return _ClientCertValidationCallback;
  41. }
  42. set
  43. {
  44. _ClientCertValidationCallback = value;
  45. }
  46. }
  47. #endregion
  48. #region Private-Members
  49. private bool _ClientCertRequired = true;
  50. private RemoteCertificateValidationCallback _ClientCertValidationCallback;
  51. #endregion
  52. #region Constructors-and-Factories
  53. /// <summary>
  54. /// Initializes a new instance of <see cref="WatsonTcpServerSslConfiguration"/>.
  55. /// </summary>
  56. public WatsonTcpServerSslConfiguration()
  57. {
  58. }
  59. /// <summary>
  60. /// Initializes a new instance of the <see cref="WatsonTcpServerSslConfiguration"/>
  61. /// class that stores the parameters copied from another configuration.
  62. /// </summary>
  63. /// <param name="configuration">
  64. /// A <see cref="WatsonTcpServerSslConfiguration"/> from which to copy.
  65. /// </param>
  66. /// <exception cref="ArgumentNullException"/>
  67. public WatsonTcpServerSslConfiguration(WatsonTcpServerSslConfiguration configuration)
  68. {
  69. if (configuration == null)
  70. throw new ArgumentNullException("Can not copy from null server SSL configuration");
  71. _ClientCertRequired = configuration._ClientCertRequired;
  72. _ClientCertValidationCallback = configuration._ClientCertValidationCallback;
  73. }
  74. #endregion
  75. #region Public-Methods
  76. #endregion
  77. #region Private-Methods
  78. private static bool DefaultValidateClientCertificate(
  79. object sender,
  80. X509Certificate certificate,
  81. X509Chain chain,
  82. SslPolicyErrors sslPolicyErrors
  83. )
  84. {
  85. return true;
  86. }
  87. #endregion
  88. }
  89. }