-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGssClient.cs
187 lines (158 loc) · 6.81 KB
/
GssClient.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
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using GSSAPI.Native;
using GSSAPI.Utility;
namespace GSSAPI
{
/// <summary>
/// GSS API client library
/// </summary>
public class GssClient : IDisposable
{
/// <summary>
/// Current state
/// </summary>
private GssClientState _state;
/// <summary>
/// Current token (created by last call to NextToken)
/// </summary>
public string Token => _state?.Token;
/// <summary>
/// Flag if authentication is completed
/// </summary>
public bool Completed => _state?.Completed ?? false;
/// <summary>
/// Initializes authentication client
/// </summary>
/// <param name="service">Service name</param>
/// <param name="flags">Request flags</param>
/// <param name="auth">Authentication mechanism</param>
/// <returns>Initialized client</returns>
public static GssClient Init(string service, GssContextFlags flags, GssClientAuth auth = GssClientAuth.SpNego)
{
var client = new GssClient();
client.InitState(service, flags, auth);
return client;
}
/// <summary>
/// Initializes authentication client for HTTP service
/// </summary>
/// <param name="host">Host name</param>
/// <param name="auth">Authentication mechanism</param>
/// <returns>Initialized client</returns>
public static GssClient InitHttp(string host, GssClientAuth auth = GssClientAuth.SpNego)
{
return Init("HTTP@" + host, GssContextFlags.None, auth);
}
/// <summary>
/// Initializes client, creates "state"
/// </summary>
/// <param name="service">Service name</param>
/// <param name="flags">Request flags</param>
/// <param name="auth">Authentication mechanism</param>
public void InitState(string service, GssContextFlags flags, GssClientAuth auth = GssClientAuth.SpNego)
{
if (string.IsNullOrWhiteSpace(service))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(service));
TraceLog.WriteLine($"[{GetHashCode():X8}] GssClient.InitState service:{service} flags:{flags} auth:{auth}");
var state = new GssClientState
{
Service = service,
Context = IntPtr.Zero,
ServerName = IntPtr.Zero,
Auth = auth,
Flags = flags,
Completed = false,
Token = null
};
uint minStat = 0;
uint majStat = 0;
using (var nameToken = Cvt.GetBufferFromString(service))
using (var inputNameType = Const.GetGssKrb5NtServiceName())
majStat = NativeMethods.gss_import_name(ref minStat, ref nameToken.Value, ref inputNameType.Value, ref state.ServerName);
Gss.CheckAndThrow(majStat, minStat, $"gss_import_name failed for {service}!");
TraceLog.WriteLine($"[{GetHashCode():X8}] GssClient.InitState gss_import_name majStat:{majStat:X8} minStat:{minStat:X8} serverName:{state.ServerName}");
_state = state;
}
/// <summary>
/// Processes
/// </summary>
/// <param name="challenge"></param>
/// <returns></returns>
public string NextToken(string challenge)
{
if (_state == null)
throw new ArgumentNullException(nameof(_state));
if (challenge == null)
throw new ArgumentNullException(nameof(challenge));
challenge = challenge.Trim();
TraceLog.WriteLine($"[{GetHashCode():X8}] GssClient.NextToken challenge:{challenge}");
uint minStat = 0;
uint majStat = 0;
var outputToken = Cvt.GetEmptyBuffer();
try
{
using (var mechType = Gss.GetAuthOid(_state.Auth))
using (var inputToken = Cvt.GetBufferFromBase64StringOrEmpty(challenge))
majStat = NativeMethods.gss_init_sec_context(
ref minStat,
IntPtr.Zero,
ref _state.Context,
_state.ServerName,
ref mechType.Value,
(uint)_state.Flags,
Const.GssIndefinite,
IntPtr.Zero,
ref inputToken.Value,
IntPtr.Zero,
ref outputToken,
IntPtr.Zero,
IntPtr.Zero);
Gss.CheckAndThrow(majStat, minStat, $"gss_init_sec_context failed for {_state.Service}!");
TraceLog.WriteLine($"[{GetHashCode():X8}] GssClient.NextToken gss_init_sec_context majStat:{majStat:X8} minStat:{minStat:X8} challenge:{challenge} outputToken:{outputToken.length}");
if (outputToken.length > 0)
{
var buffer = new byte[outputToken.length];
Marshal.Copy(outputToken.value, buffer, 0, (int)outputToken.length);
_state.Token = Convert.ToBase64String(buffer);
TraceLog.WriteLine($"[{GetHashCode():X8}] GssClient.NextToken gss_init_sec_context majStat:{majStat:X8} minStat:{minStat:X8} challenge:{challenge} outputToken:{outputToken.length} token:{_state.Token}");
}
if (majStat == Const.GssComplete)
_state.Completed = true;
return _state.Token;
}
finally
{
if (outputToken.value != IntPtr.Zero)
majStat = NativeMethods.gss_release_buffer(ref minStat, ref outputToken);
}
}
/// <summary>
/// Cleanups unmanaged resources
/// </summary>
public void Cleanup()
{
uint minStat = 0;
uint majStat = 0;
if (_state.Context != IntPtr.Zero)
majStat = NativeMethods.gss_delete_sec_context(ref minStat, ref _state.Context, IntPtr.Zero);
if (_state.ServerName != IntPtr.Zero)
majStat = NativeMethods.gss_release_name(ref minStat, ref _state.ServerName);
}
private void ReleaseUnmanagedResources()
{
Cleanup();
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~GssClient()
{
ReleaseUnmanagedResources();
}
}
}