TlsStream.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. using System.IO;
  26. namespace Mono.Security.Protocol.Tls
  27. {
  28. internal class TlsStream : Stream
  29. {
  30. #region Fields
  31. private bool canRead;
  32. private bool canWrite;
  33. private MemoryStream buffer;
  34. private byte[] temp;
  35. private const int temp_size = 4;
  36. #endregion
  37. #region Properties
  38. public bool EOF
  39. {
  40. get
  41. {
  42. if (this.Position < this.Length)
  43. {
  44. return false;
  45. }
  46. else
  47. {
  48. return true;
  49. }
  50. }
  51. }
  52. #endregion
  53. #region Stream Properties
  54. public override bool CanWrite
  55. {
  56. get { return this.canWrite; }
  57. }
  58. public override bool CanRead
  59. {
  60. get { return this.canRead; }
  61. }
  62. public override bool CanSeek
  63. {
  64. get { return this.buffer.CanSeek; }
  65. }
  66. public override long Position
  67. {
  68. get { return this.buffer.Position; }
  69. set { this.buffer.Position = value; }
  70. }
  71. public override long Length
  72. {
  73. get { return this.buffer.Length; }
  74. }
  75. #endregion
  76. #region Constructors
  77. public TlsStream() : base()
  78. {
  79. this.buffer = new MemoryStream(0);
  80. this.canRead = false;
  81. this.canWrite = true;
  82. }
  83. public TlsStream(byte[] data) : base()
  84. {
  85. if (data != null)
  86. this.buffer = new MemoryStream(data);
  87. else
  88. this.buffer = new MemoryStream ();
  89. this.canRead = true;
  90. this.canWrite = false;
  91. }
  92. #endregion
  93. #region Specific Read Methods
  94. // hack for reducing memory allocations
  95. // returned value is valid only for the length asked *and*
  96. // cannot be directly returned outside the class
  97. // note: Mono's Stream.ReadByte does a 1 byte array allocation
  98. private byte[] ReadSmallValue (int length)
  99. {
  100. if (length > temp_size)
  101. throw new ArgumentException ("8 bytes maximum");
  102. if (temp == null)
  103. temp = new byte[temp_size];
  104. if (this.Read (temp, 0, length) != length)
  105. throw new TlsException (String.Format ("buffer underrun"));
  106. return temp;
  107. }
  108. public new byte ReadByte()
  109. {
  110. byte[] result = ReadSmallValue (1);
  111. return result [0];
  112. }
  113. public short ReadInt16()
  114. {
  115. byte[] result = ReadSmallValue (2);
  116. return (short) (result[0] << 8 | result[1]);
  117. }
  118. public int ReadInt24()
  119. {
  120. byte[] result = ReadSmallValue (3);
  121. return ((result[0] << 16) | (result[1] << 8) | result[2]);
  122. }
  123. public int ReadInt32()
  124. {
  125. byte[] result = ReadSmallValue (4);
  126. return ((result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3]);
  127. }
  128. public byte[] ReadBytes(int count)
  129. {
  130. byte[] bytes = new byte[count];
  131. if (this.Read(bytes, 0, count) != count)
  132. throw new TlsException ("buffer underrun");
  133. return bytes;
  134. }
  135. #endregion
  136. #region Specific Write Methods
  137. // note: Mono's Stream.WriteByte does a 1 byte array allocation
  138. public void Write(byte value)
  139. {
  140. if (temp == null)
  141. temp = new byte[temp_size];
  142. temp[0] = value;
  143. this.Write (temp, 0, 1);
  144. }
  145. public void Write(short value)
  146. {
  147. if (temp == null)
  148. temp = new byte[temp_size];
  149. temp[0] = ((byte)(value >> 8));
  150. temp[1] = ((byte)value);
  151. this.Write (temp, 0, 2);
  152. }
  153. public void WriteInt24(int value)
  154. {
  155. if (temp == null)
  156. temp = new byte[temp_size];
  157. temp[0] = ((byte)(value >> 16));
  158. temp[1] = ((byte)(value >> 8));
  159. temp[2] = ((byte)value);
  160. this.Write (temp, 0, 3);
  161. }
  162. public void Write(int value)
  163. {
  164. if (temp == null)
  165. temp = new byte[temp_size];
  166. temp[0] = ((byte)(value >> 24));
  167. temp[1] = ((byte)(value >> 16));
  168. temp[2] = ((byte)(value >> 8));
  169. temp[3] = ((byte)value);
  170. this.Write (temp, 0, 4);
  171. }
  172. public void Write(ulong value)
  173. {
  174. Write ((int)(value >> 32));
  175. Write ((int)value);
  176. }
  177. public void Write(byte[] buffer)
  178. {
  179. this.Write(buffer, 0, buffer.Length);
  180. }
  181. #endregion
  182. #region Methods
  183. public void Reset()
  184. {
  185. this.buffer.SetLength(0);
  186. this.buffer.Position = 0;
  187. }
  188. public byte[] ToArray()
  189. {
  190. return this.buffer.ToArray();
  191. }
  192. #endregion
  193. #region Stream Methods
  194. public override void Flush()
  195. {
  196. this.buffer.Flush();
  197. }
  198. public override void SetLength(long length)
  199. {
  200. this.buffer.SetLength(length);
  201. }
  202. public override long Seek(long offset, System.IO.SeekOrigin loc)
  203. {
  204. return this.buffer.Seek(offset, loc);
  205. }
  206. public override int Read(byte[] buffer, int offset, int count)
  207. {
  208. if (this.canRead)
  209. {
  210. return this.buffer.Read(buffer, offset, count);
  211. }
  212. throw new InvalidOperationException("Read operations are not allowed by this stream");
  213. }
  214. public override void Write(byte[] buffer, int offset, int count)
  215. {
  216. if (this.canWrite)
  217. {
  218. this.buffer.Write(buffer, offset, count);
  219. }
  220. else
  221. {
  222. throw new InvalidOperationException("Write operations are not allowed by this stream");
  223. }
  224. }
  225. #endregion
  226. }
  227. }