2 // Novell.Directory.Ldap.Security.SecureStream.cs
5 // Boris Kirzner <borsk@mainsoft.com>
6 // Konstantin Triger <kostat@mainsoft.com>
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace Novell.Directory.Ldap.Security
\r
37 internal class SecureStream : Stream
\r
41 private readonly Stream _stream;
\r
42 private readonly Krb5Helper _helper;
\r
44 private readonly byte [] _lenBuf = new byte [4];
\r
45 private byte [] _buffer;
\r
46 private int _bufferPosition;
\r
48 #endregion // Fields
\r
50 #region Constructors
\r
52 public SecureStream(Stream stream, Krb5Helper helper): base ()
\r
58 #endregion // Constructors
\r
62 public override bool CanRead
\r
64 get { return _stream.CanRead; }
\r
67 public override bool CanSeek
\r
69 get { return _stream.CanSeek; }
\r
72 public override bool CanWrite
\r
74 get { return _stream.CanWrite; }
\r
77 public override long Length
\r
79 get { throw new NotSupportedException (); }
\r
82 public override long Position
\r
84 get { throw new NotSupportedException (); }
\r
85 set { throw new NotSupportedException (); }
\r
88 #endregion // Properties
\r
92 public override void Flush()
\r
97 public override int Read( byte [] buffer, int offset, int count)
\r
99 if (_buffer == null || _bufferPosition >= _buffer.Length) {
\r
100 int actual = Fill ();
\r
101 while (actual == 0)
\r
108 int available = _buffer.Length - _bufferPosition;
\r
109 if (count > available) {
\r
110 Array.Copy (_buffer, _bufferPosition, buffer, offset, available);
\r
111 _bufferPosition = _buffer.Length;
\r
115 Array.Copy (_buffer, _bufferPosition, buffer, offset, count);
\r
116 _bufferPosition += count;
\r
121 public override void Close() {
\r
128 int actual = ReadAll (_lenBuf, 4);
\r
133 int length = NetworkByteOrderToInt (_lenBuf, 0, 4);
\r
135 // if (length > _recvMaxBufSize)
\r
136 // throw new LdapException(length + " exceeds the negotiated receive buffer size limit: " + _recvMaxBufSize, 80, "");
\r
138 byte [] rawBuffer = new byte [length];
\r
139 actual = ReadAll (rawBuffer, length);
\r
141 if (actual != length)
\r
142 throw new LdapException("Expected to read " + length + " bytes, but get " + actual, 80, "");
\r
144 _buffer = _helper.Unwrap (rawBuffer, 0, length);
\r
145 _bufferPosition = 0;
\r
146 return _buffer.Length;
\r
149 private int ReadAll(byte [] buffer, int total)
\r
153 while (total > 0) {
\r
154 count = _stream.Read (buffer, pos, total);
\r
158 //return ((pos == 0) ? -1 : pos);
\r
166 public override long Seek(long offset, SeekOrigin loc)
\r
168 return _stream.Seek (offset, loc);
\r
171 public override void SetLength(long value)
\r
173 _stream.SetLength (value);
\r
176 public override void Write(byte [] buffer, int offset, int count)
\r
178 // FIXME: use GSSCOntext.getWrapSizeLimit to divide the buffer
\r
179 // Generate wrapped token
\r
180 byte [] wrappedToken = _helper.Wrap (buffer, offset, count);
\r
181 // Write out length
\r
182 IntToNetworkByteOrder (wrappedToken.Length, _lenBuf, 0, 4);
\r
183 _stream.Write (_lenBuf, 0, 4);
\r
184 // Write out wrapped token
\r
185 _stream.Write (wrappedToken, 0, wrappedToken.Length);
\r
188 internal static int NetworkByteOrderToInt(byte [] buf, int start, int count)
\r
191 for (int i = 0; i < count; i++) {
\r
193 answer |= ((int)buf [start + i] & 0xff);
\r
198 internal static void IntToNetworkByteOrder(int num, byte [] buf, int start, int count)
\r
200 for (int i = count-1; i >= 0; i--) {
\r
201 buf [start + i] = (byte)(num & 0xff);
\r
206 #endregion // Methods
\r