TlsClientCertificate.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Security.Cryptography.X509Certificates;
  25. using Mono.Security.Protocol.Tls;
  26. namespace Mono.Security.Protocol.Tls.Handshake.Client
  27. {
  28. internal class TlsClientCertificate : HandshakeMessage
  29. {
  30. private bool clientCertSelected;
  31. private X509Certificate clientCert;
  32. #region Constructors
  33. public TlsClientCertificate(Context context)
  34. : base(context, HandshakeType.Certificate)
  35. {
  36. }
  37. #endregion
  38. #region Properties
  39. public X509Certificate ClientCertificate {
  40. get {
  41. if (!clientCertSelected)
  42. {
  43. GetClientCertificate ();
  44. clientCertSelected = true;
  45. }
  46. return clientCert;
  47. }
  48. }
  49. #endregion
  50. #region Methods
  51. public override void Update()
  52. {
  53. base.Update();
  54. this.Reset();
  55. }
  56. #endregion
  57. #region Protected Methods
  58. private void GetClientCertificate ()
  59. {
  60. #warning "Client certificate selection is unfinished"
  61. ClientContext context = (ClientContext)this.Context;
  62. // note: the server may ask for mutual authentication
  63. // but may not require it (i.e. it can be optional).
  64. if (context.ClientSettings.Certificates != null &&
  65. context.ClientSettings.Certificates.Count > 0)
  66. {
  67. clientCert = context.SslStream.RaiseClientCertificateSelection(
  68. this.Context.ClientSettings.Certificates,
  69. new X509Certificate(this.Context.ServerSettings.Certificates[0].RawData),
  70. this.Context.ClientSettings.TargetHost,
  71. null);
  72. // Note: the application code can raise it's
  73. // own exception to stop the connection too.
  74. }
  75. // Update the selected client certificate
  76. context.ClientSettings.ClientCertificate = clientCert;
  77. }
  78. private void SendCertificates ()
  79. {
  80. TlsStream chain = new TlsStream ();
  81. X509Certificate currentCert = this.ClientCertificate;
  82. while (currentCert != null) {
  83. byte[] rawCert = currentCert.GetRawCertData ();
  84. chain.WriteInt24 (rawCert.Length);
  85. chain.Write(rawCert);
  86. currentCert = FindParentCertificate (currentCert);
  87. }
  88. this.WriteInt24 ((int)chain.Length);
  89. this.Write (chain.ToArray ());
  90. }
  91. protected override void ProcessAsSsl3()
  92. {
  93. if (this.ClientCertificate != null) {
  94. SendCertificates ();
  95. } else {
  96. // an Alert warning for NoCertificate (41)
  97. // should be sent from here - but that would
  98. // break the current message handling
  99. }
  100. }
  101. protected override void ProcessAsTls1()
  102. {
  103. if (this.ClientCertificate != null) {
  104. SendCertificates ();
  105. } else {
  106. // return message with empty certificate (see 7.4.6 in RFC2246)
  107. this.WriteInt24 (0);
  108. }
  109. }
  110. private X509Certificate FindParentCertificate (X509Certificate cert)
  111. {
  112. // This certificate is the root certificate
  113. if (cert.GetName () == cert.GetIssuerName ())
  114. return null;
  115. foreach (X509Certificate certificate in this.Context.ClientSettings.Certificates) {
  116. if (cert.GetName () == cert.GetIssuerName ())
  117. return certificate;
  118. }
  119. return null;
  120. }
  121. #endregion
  122. }
  123. }