TlsClientSettings.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Transport Security Layer (TLS)
  2. // Copyright (c) 2003-2004 Carlos Guzman Alvarez
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Text;
  25. using System.Security.Cryptography.X509Certificates;
  26. using Mono.Security.Cryptography;
  27. using X509 = Mono.Security.X509;
  28. namespace Mono.Security.Protocol.Tls
  29. {
  30. internal sealed class TlsClientSettings
  31. {
  32. #region Fields
  33. private string targetHost;
  34. private X509CertificateCollection certificates;
  35. //private SecurityCompressionType compressionMethod;
  36. private X509Certificate clientCertificate;
  37. private RSAManaged certificateRSA;
  38. #endregion
  39. #region Properties
  40. public string TargetHost
  41. {
  42. get { return this.targetHost; }
  43. set { this.targetHost = value; }
  44. }
  45. public X509CertificateCollection Certificates
  46. {
  47. get { return this.certificates; }
  48. set { this.certificates = value; }
  49. }
  50. public X509Certificate ClientCertificate
  51. {
  52. get { return this.clientCertificate; }
  53. set
  54. {
  55. this.clientCertificate = value;
  56. this.UpdateCertificateRSA();
  57. }
  58. }
  59. public RSAManaged CertificateRSA
  60. {
  61. get { return this.certificateRSA; }
  62. }
  63. /*
  64. public SecurityCompressionType CompressionMethod
  65. {
  66. get { return this.compressionMethod; }
  67. set
  68. {
  69. if (value != SecurityCompressionType.None)
  70. {
  71. throw new NotSupportedException("Specified compression method is not supported");
  72. }
  73. this.compressionMethod = value;
  74. }
  75. }
  76. */
  77. #endregion
  78. #region Constructors
  79. public TlsClientSettings()
  80. {
  81. // this.compressionMethod = SecurityCompressionType.None;
  82. this.certificates = new X509CertificateCollection();
  83. this.targetHost = String.Empty;
  84. }
  85. #endregion
  86. #region Methods
  87. public void UpdateCertificateRSA()
  88. {
  89. if (this.clientCertificate == null)
  90. {
  91. this.certificateRSA = null;
  92. }
  93. else
  94. {
  95. X509.X509Certificate cert = new X509.X509Certificate(this.clientCertificate.GetRawCertData());
  96. this.certificateRSA = new RSAManaged(
  97. cert.RSA.KeySize);
  98. this.certificateRSA.ImportParameters(
  99. cert.RSA.ExportParameters(false));
  100. }
  101. }
  102. #endregion
  103. }
  104. }