Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SecureStream.cs « Novell.Directory.Ldap.Security.jvm « Novell.Directory.Ldap « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9aa4e43aecdef9b61ba797d1951f0fbf1722ef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 
// Novell.Directory.Ldap.Security.SecureStream.cs
//
// Authors:
//  Boris Kirzner <borsk@mainsoft.com>
//	Konstantin Triger <kostat@mainsoft.com>
//	
// (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;

namespace Novell.Directory.Ldap.Security
{
	internal class SecureStream : Stream
	{
		#region Fields

		private readonly Stream _stream;
		private readonly Krb5Helper _helper;

		private readonly byte [] _lenBuf = new byte [4];  
		private byte [] _buffer;
		private int _bufferPosition;

		#endregion // Fields

		#region Constructors

		public SecureStream(Stream stream, Krb5Helper helper): base () 
		{
			_stream = stream;
			_helper = helper;
		}

		#endregion // Constructors

		#region Properties

		public override bool CanRead 
		{ 
			get { return _stream.CanRead; }
		}

		public override bool CanSeek 
		{ 
			get { return _stream.CanSeek; } 
		}

		public override bool CanWrite 
		{ 
			get { return _stream.CanWrite; } 
		}

		public override long Length 
		{ 
			get { throw new NotSupportedException (); } 
		}

		public override long Position 
		{ 
			get { throw new NotSupportedException (); }
			set { throw new NotSupportedException (); }
		}

		#endregion // Properties

		#region Methods

		public override void Flush()
		{
			_stream.Flush ();
		}

		public override int Read( byte [] buffer, int offset, int count)
		{
			if (_buffer == null || _bufferPosition >= _buffer.Length) {
				int actual = Fill ();
				while (actual == 0)
					actual = Fill ();

				if (actual == -1)
					return -1;				
			}

			int available = _buffer.Length - _bufferPosition;
			if (count > available) {
				Array.Copy (_buffer, _bufferPosition, buffer, offset, available);
				_bufferPosition = _buffer.Length;
				return available;
			}
			else {
				Array.Copy (_buffer, _bufferPosition, buffer, offset, count);
				_bufferPosition += count;
				return count;
			}		
		}

		public override void Close() {
			_stream.Close();
			_helper.Dispose();
		}

		private int Fill()
		{
			int actual = ReadAll (_lenBuf, 4);
	
			if (actual != 4) 
				return -1;

			int length = NetworkByteOrderToInt (_lenBuf, 0, 4);

//			if (length > _recvMaxBufSize)
//				throw new LdapException(length + " exceeds the negotiated receive buffer size limit: " + _recvMaxBufSize, 80, "");

			byte [] rawBuffer = new byte [length];
			actual = ReadAll (rawBuffer, length);

			if (actual != length)
				throw new LdapException("Expected to read " + length + " bytes, but get " + actual, 80, "");

			_buffer = _helper.Unwrap (rawBuffer, 0, length);
			_bufferPosition = 0;
			return _buffer.Length;
		}

		private int ReadAll(byte [] buffer, int total)
		{
			int count = 0;
			int pos = 0;
			while (total > 0) {
				count = _stream.Read (buffer, pos, total);

				if (count == -1)
					break;
					//return ((pos == 0) ? -1 : pos);

				pos += count;
				total -= count;
			}
			return pos;
		}

		public override long Seek(long offset, SeekOrigin loc)
		{
			return _stream.Seek (offset, loc);
		}

		public override void SetLength(long value)
		{
			_stream.SetLength (value);
		}

		public override void Write(byte [] buffer, int offset, int count)
		{
			// FIXME: use GSSCOntext.getWrapSizeLimit to divide the buffer
			// Generate wrapped token 
			byte [] wrappedToken = _helper.Wrap (buffer, offset, count);
			// Write out length
			IntToNetworkByteOrder (wrappedToken.Length, _lenBuf, 0, 4);
			_stream.Write (_lenBuf, 0, 4);
			// Write out wrapped token
			_stream.Write (wrappedToken, 0, wrappedToken.Length);
		}

		internal static int NetworkByteOrderToInt(byte [] buf, int start, int count) 
		{
			int answer = 0;
			for (int i = 0; i < count; i++) {
				answer <<= 8;
				answer |= ((int)buf [start + i] & 0xff);
			}
			return answer;
		}

		internal static void IntToNetworkByteOrder(int num, byte [] buf, int start, int count) 
		{
			for (int i = count-1; i >= 0; i--) {
				buf [start + i] = (byte)(num & 0xff);
				num >>= 8;
			}
		}

		#endregion // Methods
	}
}