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
|
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Authentication;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;
namespace System.Net.Security.Tests
{
internal class UnixGssFakeNegotiateStream : NegotiateStream
{
private static Action<object> s_serverLoop = ServerLoop;
private static Action<object> s_msgLoop = MessageLoop;
private readonly UnixGssFakeStreamFramer _framer;
private SafeGssContextHandle _context;
private volatile int _dataMsgCount;
public UnixGssFakeNegotiateStream(Stream innerStream) : base(innerStream)
{
_framer = new UnixGssFakeStreamFramer(innerStream);
_dataMsgCount = 0;
}
public override Task AuthenticateAsServerAsync()
{
return Task.Factory.StartNew(s_serverLoop, this, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
}
public Task PollMessageAsync(int count)
{
_dataMsgCount = count;
return Task.Factory.StartNew(s_msgLoop, this, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
}
public static void GetDefaultKerberosCredentials(string username, string password)
{
// Fetch a Kerberos TGT which gets saved in the default cache
SafeGssCredHandle.Create(username, password, isNtlmOnly:false).Dispose();
}
private static void ServerLoop(object state)
{
UnixGssFakeNegotiateStream thisRef = (UnixGssFakeNegotiateStream)state;
var header = new byte[5];
bool handshakeDone = false;
do
{
byte[] inBuf = thisRef._framer.ReadHandshakeFrame();
byte[] outBuf = null;
try
{
handshakeDone = EstablishSecurityContext(ref thisRef._context, inBuf, out outBuf);
thisRef._framer.WriteHandshakeFrame(outBuf, 0, outBuf.Length);
}
catch (Interop.NetSecurityNative.GssApiException e)
{
thisRef._framer.WriteHandshakeFrame(e);
handshakeDone = true;
}
}
while (!handshakeDone);
}
private static void MessageLoop(object state)
{
UnixGssFakeNegotiateStream thisRef = (UnixGssFakeNegotiateStream)state;
while (thisRef._dataMsgCount > 0)
{
byte[] inBuf = thisRef._framer.ReadDataFrame();
byte[] unwrapped = UnwrapMessage(thisRef._context, inBuf);
byte[] outMsg = WrapMessage(thisRef._context, unwrapped);
thisRef._framer.WriteDataFrame(outMsg, 0, outMsg.Length);
thisRef._dataMsgCount--;
}
}
private static bool EstablishSecurityContext(
ref SafeGssContextHandle context,
byte[] buffer,
out byte[] outputBuffer)
{
outputBuffer = null;
// EstablishSecurityContext is called multiple times in a session.
// In each call, we need to pass the context handle from the previous call.
// For the first call, the context handle will be null.
if (context == null)
{
context = new SafeGssContextHandle();
}
Interop.NetSecurityNative.GssBuffer token = default(Interop.NetSecurityNative.GssBuffer);
Interop.NetSecurityNative.Status status;
try
{
Interop.NetSecurityNative.Status minorStatus;
status = Interop.NetSecurityNative.AcceptSecContext(out minorStatus,
ref context,
buffer,
(buffer == null) ? 0 : buffer.Length,
ref token);
if ((status != Interop.NetSecurityNative.Status.GSS_S_COMPLETE) && (status != Interop.NetSecurityNative.Status.GSS_S_CONTINUE_NEEDED))
{
throw new Interop.NetSecurityNative.GssApiException(status, minorStatus);
}
outputBuffer = token.ToByteArray();
}
finally
{
token.Dispose();
}
return status == Interop.NetSecurityNative.Status.GSS_S_COMPLETE;
}
private static byte[] UnwrapMessage(SafeGssContextHandle context, byte[] message)
{
Interop.NetSecurityNative.GssBuffer unwrapped = default(Interop.NetSecurityNative.GssBuffer);
Interop.NetSecurityNative.Status status;
try
{
Interop.NetSecurityNative.Status minorStatus;
status = Interop.NetSecurityNative.UnwrapBuffer(out minorStatus,
context, message, 0, message.Length, ref unwrapped);
if (status != Interop.NetSecurityNative.Status.GSS_S_COMPLETE)
{
throw new Interop.NetSecurityNative.GssApiException(status, minorStatus);
}
return unwrapped.ToByteArray();
}
finally
{
unwrapped.Dispose();
}
}
private static byte[] WrapMessage(SafeGssContextHandle context, byte[] message)
{
Interop.NetSecurityNative.GssBuffer wrapped = default(Interop.NetSecurityNative.GssBuffer);
Interop.NetSecurityNative.Status status;
try
{
Interop.NetSecurityNative.Status minorStatus;
status = Interop.NetSecurityNative.WrapBuffer(out minorStatus,
context, false, message, 0, message.Length, ref wrapped);
if (status != Interop.NetSecurityNative.Status.GSS_S_COMPLETE)
{
throw new Interop.NetSecurityNative.GssApiException(status, minorStatus);
}
return wrapped.ToByteArray();
}
finally
{
wrapped.Dispose();
}
}
}
}
|