ServerContext.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Collections;
  25. using System.Security.Cryptography;
  26. using System.Security.Cryptography.X509Certificates;
  27. using Mono.Security.Protocol.Tls.Handshake;
  28. using MonoX509 = Mono.Security.X509;
  29. namespace Mono.Security.Protocol.Tls
  30. {
  31. internal class ServerContext : Context
  32. {
  33. #region Fields
  34. private SslServerStream sslStream;
  35. private bool request_client_certificate;
  36. private bool clientCertificateRequired;
  37. #endregion
  38. #region Properties
  39. public SslServerStream SslStream
  40. {
  41. get { return this.sslStream; }
  42. }
  43. public bool ClientCertificateRequired
  44. {
  45. get { return this.clientCertificateRequired; }
  46. }
  47. public bool RequestClientCertificate {
  48. get { return request_client_certificate; }
  49. }
  50. #endregion
  51. #region Constructors
  52. public ServerContext(
  53. SslServerStream stream,
  54. SecurityProtocolType securityProtocolType,
  55. X509Certificate serverCertificate,
  56. bool clientCertificateRequired,
  57. bool requestClientCertificate)
  58. : base(securityProtocolType)
  59. {
  60. this.sslStream = stream;
  61. this.clientCertificateRequired = clientCertificateRequired;
  62. this.request_client_certificate = requestClientCertificate;
  63. // Convert the System.Security cert to a Mono Cert
  64. MonoX509.X509Certificate cert = new MonoX509.X509Certificate(serverCertificate.GetRawCertData());
  65. // Add server certificate to the certificate collection
  66. this.ServerSettings.Certificates = new MonoX509.X509CertificateCollection();
  67. this.ServerSettings.Certificates.Add(cert);
  68. this.ServerSettings.UpdateCertificateRSA();
  69. // Add requested certificate types
  70. this.ServerSettings.CertificateTypes = new ClientCertificateType[1];
  71. this.ServerSettings.CertificateTypes[0] = ClientCertificateType.RSA;
  72. // Add certificate authorities
  73. MonoX509.X509CertificateCollection trusted = MonoX509.X509StoreManager.TrustedRootCertificates;
  74. string[] list = new string [trusted.Count];
  75. int i = 0;
  76. foreach (MonoX509.X509Certificate root in trusted)
  77. {
  78. list [i++] = root.IssuerName;
  79. }
  80. this.ServerSettings.DistinguisedNames = list;
  81. }
  82. #endregion
  83. }
  84. }