1 /* Transport Security Layer (TLS)
\r
2 * Copyright (c) 2003-2004 Carlos Guzman Alvarez
\r
4 * Permission is hereby granted, free of charge, to any person
\r
5 * obtaining a copy of this software and associated documentation
\r
6 * files (the "Software"), to deal in the Software without restriction,
\r
7 * including without limitation the rights to use, copy, modify, merge,
\r
8 * publish, distribute, sublicense, and/or sell copies of the Software,
\r
9 * and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included
\r
13 * in all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
\r
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
\r
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
\r
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
22 * DEALINGS IN THE SOFTWARE.
\r
29 namespace Mono.Security.Protocol.Tls
\r
31 internal class TlsStream : Stream
\r
35 private bool canRead;
\r
36 private bool canWrite;
\r
37 private MemoryStream buffer;
\r
47 if (this.Position < this.Length)
\r
60 #region Stream Properties
\r
62 public override bool CanWrite
\r
64 get { return canWrite; }
\r
67 public override bool CanRead
\r
69 get { return canRead; }
\r
72 public override bool CanSeek
\r
74 get { return buffer.CanSeek; }
\r
77 public override long Position
\r
79 get { return buffer.Position; }
\r
80 set { buffer.Position = value; }
\r
83 public override long Length
\r
85 get { return buffer.Length; }
\r
90 #region Constructors
\r
92 public TlsStream() : base()
\r
94 this.buffer = new MemoryStream(0);
\r
95 this.canRead = false;
\r
96 this.canWrite = true;
\r
99 public TlsStream(byte[] data) : base()
\r
101 this.buffer = new MemoryStream(data);
\r
102 this.canRead = true;
\r
103 this.canWrite = false;
\r
108 #region Specific Read Methods
\r
110 public new byte ReadByte()
\r
112 return (byte)base.ReadByte();
\r
115 public short ReadInt16()
\r
117 byte[] bytes = this.ReadBytes(2);
\r
119 return IPAddress.HostToNetworkOrder(BitConverter.ToInt16(bytes, 0));
\r
122 public int ReadInt24()
\r
124 byte[] b = this.ReadBytes(3);
\r
126 return ((b[0] & 0xff) << 16) | ((b[1] & 0xff) << 8) | (b[2] & 0xff);
\r
129 public int ReadInt32()
\r
131 byte[] bytes = this.ReadBytes(4);
\r
133 return IPAddress.HostToNetworkOrder(BitConverter.ToInt32(bytes, 0));
\r
136 public long ReadInt64()
\r
138 byte[] bytes = this.ReadBytes(8);
\r
140 return IPAddress.HostToNetworkOrder(BitConverter.ToInt64(bytes, 0));
\r
143 public byte[] ReadBytes(int count)
\r
145 byte[] bytes = new byte[count];
\r
146 this.Read(bytes, 0, count);
\r
153 #region Specific Write Methods
\r
155 public void Write(byte value)
\r
160 public void Write(short value)
\r
162 byte[] bytes = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder(value));
\r
166 public void WriteInt24(int value)
\r
168 int int24 = IPAddress.HostToNetworkOrder(value);
\r
169 byte[] content = new byte[3];
\r
171 System.Array.Copy(BitConverter.GetBytes(int24), 1, content, 0, 3);
\r
176 public void Write(int value)
\r
178 byte[] bytes = BitConverter.GetBytes((int)IPAddress.HostToNetworkOrder(value));
\r
182 public void Write(long value)
\r
184 byte[] bytes = BitConverter.GetBytes((long)IPAddress.HostToNetworkOrder(value));
\r
188 public void Write(byte[] buffer)
\r
190 Write(buffer, 0, buffer.Length);
\r
197 public void Reset()
\r
199 buffer.SetLength(0);
\r
200 buffer.Position = 0;
\r
203 public byte[] ToArray()
\r
205 return buffer.ToArray();
\r
210 #region Stream Methods
\r
212 public override void Flush()
\r
217 public override void SetLength(long length)
\r
219 buffer.SetLength(length);
\r
222 public override long Seek(long offset, System.IO.SeekOrigin loc)
\r
224 return buffer.Seek(offset, loc);
\r
227 public override int Read(byte[] buffer, int offset, int count)
\r
231 return this.buffer.Read(buffer, offset, count);
\r
233 throw new InvalidOperationException("Read operations are not allowed by this stream");
\r
236 public override void Write(byte[] buffer, int offset, int count)
\r
240 this.buffer.Write(buffer, offset, count);
\r
244 throw new InvalidOperationException("Write operations are not allowed by this stream");
\r