This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
SslStream.cs
247 lines (201 loc) · 6.3 KB
/
SslStream.cs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Org.BouncyCastle.Crypto.Tls;
namespace System.Net.Security
{
public class SslStream : Stream
{
class AsyncResult : IAsyncResult
{
public object AsyncState { get; set; }
public WaitHandle AsyncWaitHandle { get; set; }
public bool CompletedSynchronously { get; set; }
public bool IsCompleted { get; set; }
public AsyncCallback Callback { get; set; }
public Exception Exception { get; set; }
}
private TlsProtocolHandler m_TlsHandler;
private Stream m_InnerStream;
private Stream m_SecureStream;
public SslStream(Stream innerStream)
{
if (innerStream == null)
throw new ArgumentNullException("innerStream");
m_InnerStream = innerStream;
m_SecureStream = innerStream;
}
public IAsyncResult BeginAuthenticateAsClient(string targetHost, AsyncCallback asyncCallback, Object asyncState)
{
var waitCallback = new WaitCallback(StartAuthentication);
var result = new AsyncResult();
result.Callback = asyncCallback;
result.AsyncState = asyncState;
ThreadPool.QueueUserWorkItem(waitCallback, result);
return result;
}
void StartAuthentication(object state)
{
var result = state as AsyncResult;
m_TlsHandler = new TlsProtocolHandler(m_InnerStream);
try
{
#pragma warning disable 0612,0618
m_TlsHandler.Connect(new LegacyTlsClient(new AlwaysValidVerifyer()));
#pragma warning restore 0612,0618
m_SecureStream = m_TlsHandler.Stream;
}
catch (Exception e)
{
result.Exception = e;
}
finally
{
result.IsCompleted = true;
var callback = result.Callback;
if (callback != null)
callback(result);
}
}
public void EndAuthenticateAsClient(IAsyncResult asyncResult)
{
var result = asyncResult as AsyncResult;
if(result.Exception != null)
throw result.Exception;
}
public override bool CanRead
{
get { return m_SecureStream.CanRead; }
}
public override bool CanSeek
{
get { return m_SecureStream.CanSeek; }
}
public override bool CanWrite
{
get { return m_SecureStream.CanWrite; }
}
public override void Flush()
{
m_SecureStream.Flush();
}
public override long Length
{
get { return m_SecureStream.Length; }
}
public override long Position
{
get
{
return m_SecureStream.Position;
}
set
{
m_SecureStream.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return m_SecureStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return m_SecureStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
m_SecureStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
m_SecureStream.Write(buffer, offset, count);
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return m_SecureStream.BeginRead(buffer, offset, count, callback, state);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
var waitCallback = new WaitCallback(StartSend);
var result = new AsyncResult();
result.AsyncState = state;
result.Callback = callback;
result.CompletedSynchronously = false;
ThreadPool.QueueUserWorkItem(waitCallback, new object[] { buffer, offset, count, result });
return result;
}
private void StartSend(object state)
{
var stateArr = state as object[];
var result = stateArr[3] as AsyncResult;
try
{
m_SecureStream.Write((byte[])stateArr[0], (int)stateArr[1], (int)stateArr[2]);
m_SecureStream.Flush();
result.IsCompleted = true;
}
catch (Exception e)
{
result.Exception = e;
}
finally
{
if (result.Callback != null)
result.Callback(result);
}
}
public override int EndRead(IAsyncResult asyncResult)
{
return m_SecureStream.EndRead(asyncResult);
}
public override void EndWrite(IAsyncResult asyncResult)
{
var result = asyncResult as AsyncResult;
if (result.Exception != null)
throw result.Exception;
}
public override void Close()
{
m_SecureStream.Close();
}
public override int ReadByte()
{
return m_SecureStream.ReadByte();
}
public override void WriteByte(byte value)
{
m_SecureStream.WriteByte(value);
}
public override int ReadTimeout
{
get
{
return m_SecureStream.ReadTimeout;
}
set
{
m_SecureStream.ReadTimeout = value;
}
}
public override int WriteTimeout
{
get
{
return m_SecureStream.WriteTimeout;
}
set
{
m_SecureStream.WriteTimeout = value;
}
}
public override bool CanTimeout
{
get
{
return m_SecureStream.CanTimeout;
}
}
}
}