HandshakeMessage.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Transport Security Layer (TLS)
  2. // Copyright (c) 2003-2004 Carlos Guzman Alvarez
  3. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using System;
  25. namespace Mono.Security.Protocol.Tls.Handshake
  26. {
  27. internal abstract class HandshakeMessage : TlsStream
  28. {
  29. #region Fields
  30. private Context context;
  31. private HandshakeType handshakeType;
  32. private ContentType contentType;
  33. private byte[] cache;
  34. #endregion
  35. #region Properties
  36. public Context Context
  37. {
  38. get { return this.context; }
  39. }
  40. public HandshakeType HandshakeType
  41. {
  42. get { return this.handshakeType; }
  43. }
  44. public ContentType ContentType
  45. {
  46. get { return this.contentType; }
  47. }
  48. #endregion
  49. #region Constructors
  50. public HandshakeMessage(
  51. Context context,
  52. HandshakeType handshakeType)
  53. : this(context, handshakeType, ContentType.Handshake)
  54. {
  55. }
  56. public HandshakeMessage(
  57. Context context,
  58. HandshakeType handshakeType,
  59. ContentType contentType) : base()
  60. {
  61. this.context = context;
  62. this.handshakeType = handshakeType;
  63. this.contentType = contentType;
  64. }
  65. public HandshakeMessage(
  66. Context context,
  67. HandshakeType handshakeType,
  68. byte[] data) : base(data)
  69. {
  70. this.context = context;
  71. this.handshakeType = handshakeType;
  72. }
  73. #endregion
  74. #region Abstract Methods
  75. protected abstract void ProcessAsTls1();
  76. protected abstract void ProcessAsSsl3();
  77. #endregion
  78. #region Methods
  79. public void Process()
  80. {
  81. switch (this.Context.SecurityProtocol)
  82. {
  83. case SecurityProtocolType.Tls:
  84. case SecurityProtocolType.Default:
  85. this.ProcessAsTls1();
  86. break;
  87. case SecurityProtocolType.Ssl3:
  88. this.ProcessAsSsl3();
  89. break;
  90. case SecurityProtocolType.Ssl2:
  91. default:
  92. throw new NotSupportedException("Unsupported security protocol type");
  93. }
  94. }
  95. public virtual void Update()
  96. {
  97. if (this.CanWrite)
  98. {
  99. // result may (should) be available from a previous call to EncodeMessage
  100. if (cache == null)
  101. cache = this.EncodeMessage ();
  102. this.context.HandshakeMessages.Write (cache);
  103. this.Reset();
  104. cache = null;
  105. }
  106. }
  107. public virtual byte[] EncodeMessage()
  108. {
  109. cache = null;
  110. if (CanWrite)
  111. {
  112. byte[] hs = this.ToArray ();
  113. int len = hs.Length;
  114. cache = new byte[4 + len];
  115. cache[0] = (byte) HandshakeType;
  116. // Length as an Int24 in Network Order
  117. cache[1] = (byte) (len >> 16);
  118. cache[2] = (byte) (len >> 8);
  119. cache[3] = (byte) len;
  120. Buffer.BlockCopy (hs, 0, cache, 4, len);
  121. }
  122. return cache;
  123. }
  124. static public bool Compare (byte[] buffer1, byte[] buffer2)
  125. {
  126. // in our case both null can't exist (or be valid)
  127. if ((buffer1 == null) || (buffer2 == null))
  128. return false;
  129. if (buffer1.Length != buffer2.Length)
  130. return false;
  131. for (int i = 0; i < buffer1.Length; i++) {
  132. if (buffer1[i] != buffer2[i])
  133. return false;
  134. }
  135. return true;
  136. }
  137. #endregion
  138. }
  139. }