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