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