-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
SynSSPIAuth.pas
355 lines (299 loc) · 13.6 KB
/
SynSSPIAuth.pas
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/// low level access to Windows Authentication for the Win32/Win64 platform
// - this unit is a part of the freeware Synopse framework,
// licensed under a MPL/GPL/LGPL tri-license; version 1.18
unit SynSSPIAuth;
{
This file is part of Synopse mORMot framework.
Synopse mORMot framework. Copyright (c) Arnaud Bouchez
Synopse Informatique - https://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
The Original Code is Synopse mORMot framework.
The Initial Developer of the Original Code is Chaa.
Portions created by the Initial Developer are Copyright (c)
the Initial Developer. All Rights Reserved.
Contributor(s):
Arnaud Bouchez
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****
This unit has been contributed by Chaa.
See https://synopse.info/forum/viewtopic.php?pid=5619#p5619
}
{$I Synopse.inc} // define HASINLINE and other compatibility switches
interface
{$ifdef MSWINDOWS} // compiles as void unit for non-Windows - allow Lazarus package
uses
Windows,
SysUtils,
SynCommons,
SynSSPI;
/// Client-side authentication procedure
// - aSecContext holds information between function calls
// - aInData contains data received from server
// - aSecKerberosSPN is the optional SPN domain name, e.g.
// 'mymormotservice/myserver.mydomain.tld'
// - aOutData contains data that must be sent to server
// - if function returns True, client must send aOutData to server
// and call function again width data, returned from servsr
function ClientSSPIAuth(var aSecContext: TSecContext;
const aInData: RawByteString; const aSecKerberosSPN: RawUTF8;
out aOutData: RawByteString): Boolean;
/// Client-side authentication procedure with clear text password.
// This function must be used when application need to use different
// user credentials (not credentials of logged in user)
// - aSecContext holds information between function calls
// - aInData contains data received from server
// - aUserName is the domain and user name, in form of
// 'DomainName\UserName'
// - aPassword is the user clear text password
// - aOutData contains data that must be sent to server
// - if function returns True, client must send aOutData to server
// and call function again width data, returned from server
function ClientSSPIAuthWithPassword(var aSecContext: TSecContext;
const aInData: RawByteString; const aUserName: RawUTF8;
const aPassword: RawUTF8; out aOutData: RawByteString): Boolean;
/// Server-side authentication procedure
// - aSecContext holds information between function calls
// - aInData contains data recieved from client
// - aOutData contains data that must be sent to client
// - if function returns True, server must send aOutData to client
// and call function again width data, returned from client
function ServerSSPIAuth(var aSecContext: TSecContext;
const aInData: RawByteString; out aOutData: RawByteString): Boolean;
/// Server-side function that returns authenticated user name
// - aSecContext must be received from previous successful call to ServerSSPIAuth
// - aUserName contains authenticated user name
procedure ServerSSPIAuthUser(var aSecContext: TSecContext; out aUserName: RawUTF8);
/// Returns name of the security package that has been used with the negotiation process
// - aSecContext must be received from previos success call to ServerSSPIAuth
// or ClientSSPIAuth
function SecPackageName(var aSecContext: TSecContext): RawUTF8;
/// Force using aSecKerberosSPN for server identification.
// - aSecKerberosSPN is the Service Principal Name,
// registered in domain, e.g.
// 'mymormotservice/[email protected]'
procedure ClientForceSPN(const aSecKerberosSPN: RawUTF8);
/// Force NTLM authentication instead of Negotiate for browser authenticaton.
// Use case: SPNs not configured properly in domain
// - see for details https://synopse.info/forum/viewtopic.php?id=931&p=3
procedure ServerForceNTLM(IsNTLM: boolean);
const
/// SSPI package names. Client always use Negotiate.
// Server detect Negotiate or NTLM requests and use appropriate package
SECPKGNAMENTLM = 'NTLM';
SECPKGNAMENEGOTIATE = 'Negotiate';
var
/// HTTP header to be set for SSPI authentication 'WWW-Authenticate: NTLM' or 'WWW-Authenticate: Negotiate';
SECPKGNAMEHTTPWWWAUTHENTICATE: RawUTF8;
/// HTTP header pattern received for SSPI authentication 'AUTHORIZATION: NTLM ' or 'AUTHORIZATION: NEGOTIATE '
SECPKGNAMEHTTPAUTHORIZATION: PAnsiChar;
implementation
var
ForceSecKerberosSPN: WideString;
function ClientSSPIAuthWorker(var aSecContext: TSecContext;
const aInData: RawByteString; pszTargetName: PWideChar;
pAuthData: PSecWinntAuthIdentityW;
out aOutData: RawByteString): Boolean;
var InBuf: TSecBuffer;
InDesc: TSecBufferDesc;
InDescPtr: PSecBufferDesc;
SecPkgInfo: PSecPkgInfoW;
Expiry: LARGE_INTEGER;
LInCtxPtr: PSecHandle;
OutBuf: TSecBuffer;
OutDesc: TSecBufferDesc;
CtxReqAttr: Cardinal;
CtxAttr: Cardinal;
Status: Integer;
begin
InBuf.BufferType := SECBUFFER_TOKEN;
InBuf.cbBuffer := Length(aInData);
InBuf.pvBuffer := PByte(aInData);
if (aSecContext.CredHandle.dwLower = -1) and (aSecContext.CredHandle.dwUpper = -1) then begin
aSecContext.CreatedTick64 := GetTickCount64();
if QuerySecurityPackageInfoW(SECPKGNAMENEGOTIATE, SecPkgInfo) <> 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
try
if AcquireCredentialsHandleW(nil, SecPkgInfo^.Name, SECPKG_CRED_OUTBOUND, nil, pAuthData, nil, nil, @aSecContext.CredHandle, Expiry) <> 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
finally
FreeContextBuffer(SecPkgInfo);
end;
InDescPtr := nil;
LInCtxPtr := nil;
end
else begin
InDesc.ulVersion := SECBUFFER_VERSION;
InDesc.cBuffers := 1;
InDesc.pBuffers := @InBuf;
InDescPtr := @InDesc;
LInCtxPtr := @aSecContext.CtxHandle;
end;
CtxReqAttr := ISC_REQ_ALLOCATE_MEMORY or ASC_REQ_CONFIDENTIALITY;
if pszTargetName <> nil then
CtxReqAttr := CtxReqAttr or ISC_REQ_MUTUAL_AUTH;
OutBuf.BufferType := SECBUFFER_TOKEN;
OutBuf.cbBuffer := 0;
OutBuf.pvBuffer := nil;
OutDesc.ulVersion := SECBUFFER_VERSION;
OutDesc.cBuffers := 1;
OutDesc.pBuffers := @OutBuf;
Status := InitializeSecurityContextW(@aSecContext.CredHandle, LInCtxPtr,
pszTargetName, CtxReqAttr,
0, SECURITY_NATIVE_DREP, InDescPtr, 0, @aSecContext.CtxHandle, @OutDesc, CtxAttr, Expiry);
Result := (Status = SEC_I_CONTINUE_NEEDED) or (Status = SEC_I_COMPLETE_AND_CONTINUE);
if (Status = SEC_I_COMPLETE_NEEDED) or (Status = SEC_I_COMPLETE_AND_CONTINUE) then
Status := CompleteAuthToken(@aSecContext.CtxHandle, @OutDesc);
if Status < 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
SetString(aOutData, PAnsiChar(OutBuf.pvBuffer), OutBuf.cbBuffer);
FreeContextBuffer(OutBuf.pvBuffer);
end;
function ClientSSPIAuth(var aSecContext: TSecContext;
const aInData: RawByteString; const aSecKerberosSPN: RawUTF8;
out aOutData: RawByteString): Boolean;
var TargetName: PWideChar;
begin
if aSecKerberosSPN <> '' then
TargetName := PWideChar(UTF8ToSynUnicode(aSecKerberosSPN)) else begin
if ForceSecKerberosSPN <> '' then
TargetName := PWideChar(ForceSecKerberosSPN) else
TargetName := nil;
end;
Result := ClientSSPIAuthWorker(aSecContext, aInData, TargetName, nil, aOutData);
end;
function ClientSSPIAuthWithPassword(var aSecContext: TSecContext;
const aInData: RawByteString; const aUserName: RawUTF8;
const aPassword: RawUTF8; out aOutData: RawByteString): Boolean;
var UserPos: Integer;
Domain, User, Password: SynUnicode;
AuthIdentity: TSecWinntAuthIdentityW;
TargetName: PWideChar;
begin
UserPos := PosExChar('\', aUserName);
if UserPos=0 then begin
Domain := '';
User := UTF8ToSynUnicode(aUserName);
end else begin
Domain := UTF8ToSynUnicode(Copy(aUserName, 1, UserPos-1));
User := UTF8ToSynUnicode(Copy(aUserName, UserPos+1, MaxInt));
end;
Password := UTF8ToSynUnicode(aPassword);
AuthIdentity.Domain := PWideChar(Domain);
AuthIdentity.DomainLength := Length(Domain);
AuthIdentity.User := PWideChar(User);
AuthIdentity.UserLength := Length(User);
AuthIdentity.Password := PWideChar(Password);
AuthIdentity.PasswordLength := Length(Password);
AuthIdentity.Flags := SEC_WINNT_AUTH_IDENTITY_UNICODE;
if ForceSecKerberosSPN <> '' then
TargetName := PWideChar(ForceSecKerberosSPN) else
TargetName := nil;
Result := ClientSSPIAuthWorker(aSecContext, aInData, TargetName, @AuthIdentity, aOutData);
end;
function ServerSSPIAuth(var aSecContext: TSecContext;
const aInData: RawByteString; out aOutData: RawByteString): Boolean;
var InBuf: TSecBuffer;
InDesc: TSecBufferDesc;
SecPkgInfo: PSecPkgInfoW;
Expiry: LARGE_INTEGER;
LInCtxPtr: PSecHandle;
OutBuf: TSecBuffer;
OutDesc: TSecBufferDesc;
CtxAttr: Cardinal;
Status: Integer;
begin
InBuf.BufferType := SECBUFFER_TOKEN;
InBuf.cbBuffer := Length(aInData);
InBuf.pvBuffer := PByte(aInData);
InDesc.ulVersion := SECBUFFER_VERSION;
InDesc.cBuffers := 1;
InDesc.pBuffers := @InBuf;
if (aSecContext.CredHandle.dwLower = -1) and (aSecContext.CredHandle.dwUpper = -1) then begin
aSecContext.CreatedTick64 := GetTickCount64();
if IdemPChar(Pointer(aInData), 'NTLMSSP') then begin
if QuerySecurityPackageInfoW(SECPKGNAMENTLM, SecPkgInfo) <> 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
end else begin
if QuerySecurityPackageInfoW(SECPKGNAMENEGOTIATE, SecPkgInfo) <> 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
end;
try
if AcquireCredentialsHandleW(nil, SecPkgInfo^.Name, SECPKG_CRED_INBOUND, nil, nil, nil, nil, @aSecContext.CredHandle, Expiry) <> 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
finally
FreeContextBuffer(SecPkgInfo);
end;
LInCtxPtr := nil;
end else
LInCtxPtr := @aSecContext.CtxHandle;
OutBuf.BufferType := SECBUFFER_TOKEN;
OutBuf.cbBuffer := 0;
OutBuf.pvBuffer := nil;
OutDesc.ulVersion := SECBUFFER_VERSION;
OutDesc.cBuffers := 1;
OutDesc.pBuffers := @OutBuf;
Status := AcceptSecurityContext(@aSecContext.CredHandle, LInCtxPtr, @InDesc,
ASC_REQ_ALLOCATE_MEMORY or ASC_REQ_CONFIDENTIALITY,
SECURITY_NATIVE_DREP, @aSecContext.CtxHandle, @OutDesc, CtxAttr, Expiry);
Result := (Status = SEC_I_CONTINUE_NEEDED) or (Status = SEC_I_COMPLETE_AND_CONTINUE);
if (Status = SEC_I_COMPLETE_NEEDED) or (Status = SEC_I_COMPLETE_AND_CONTINUE) then
Status := CompleteAuthToken(@aSecContext.CtxHandle, @OutDesc);
if Status < 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
SetString(aOutData, PAnsiChar(OutBuf.pvBuffer), OutBuf.cbBuffer);
FreeContextBuffer(OutBuf.pvBuffer);
end;
procedure ServerSSPIAuthUser(var aSecContext: TSecContext; out aUserName: RawUTF8);
var Names: SecPkgContext_NamesW;
begin
if QueryContextAttributesW(@aSecContext.CtxHandle, SECPKG_ATTR_NAMES, @Names) <> 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
aUserName := RawUnicodeToUtf8(Names.sUserName, StrLenW(Names.sUserName));
FreeContextBuffer(Names.sUserName);
end;
function SecPackageName(var aSecContext: TSecContext): RawUTF8;
var NegotiationInfo: TSecPkgContext_NegotiationInfo;
begin
if QueryContextAttributesW(@aSecContext.CtxHandle, SECPKG_ATTR_NEGOTIATION_INFO, @NegotiationInfo) <> 0 then
raise ESynSSPI.CreateLastOSError(aSecContext);
Result := RawUnicodeToUtf8(NegotiationInfo.PackageInfo^.Name, StrLenW(NegotiationInfo.PackageInfo^.Name));
FreeContextBuffer(NegotiationInfo.PackageInfo);
end;
procedure ClientForceSPN(const aSecKerberosSPN: RawUTF8);
begin
ForceSecKerberosSPN := UTF8ToSynUnicode(aSecKerberosSPN);
end;
procedure ServerForceNTLM(IsNTLM: boolean);
begin
if IsNTLM then begin
SECPKGNAMEHTTPWWWAUTHENTICATE := 'WWW-Authenticate: NTLM';
SECPKGNAMEHTTPAUTHORIZATION := 'AUTHORIZATION: NTLM ';
end else begin
SECPKGNAMEHTTPWWWAUTHENTICATE := 'WWW-Authenticate: Negotiate';
SECPKGNAMEHTTPAUTHORIZATION := 'AUTHORIZATION: NEGOTIATE ';
end;
end;
initialization
ServerForceNTLM(False);
{$else}
implementation
{$endif MSWINDOWS} // compiles as void unit for non-Windows - allow Lazarus package
end.