forked from synopse/mORMot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynSSPIAuth.pas
622 lines (526 loc) · 22.7 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/// 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) 2015 Arnaud Bouchez
Synopse Informatique - http://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) 2015
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 http://synopse.info/forum/viewtopic.php?pid=5619#p5619
Thanks A LOT for this great contribution to the framework!
Version 1.18
- initial release, with code submitted by Chaa
- code refactored to conform MSDN sample code, see
http://msdn.microsoft.com/en-us/library/windows/desktop/aa379449.aspx
- SECURITY_NETWORK_DREP changed to SECURITY_NATIVE_DREP to conform
browser authentication requirements
- SecPkgName moved to unit interface section, to be used in
browser authentication code
- added support for Kerberos authentication method, to use Kerberos set
SecKerberosSPN variable on client-side to the value of SPN for service,
see documentation for details
- added SecPackageName() to see active Kerberos or NTLM authentication scheme
- added SecEncrypt() and SecDecrypt() functions
- added ClientSSPIAuthWithPassword for authentication procedure
with clear text password
}
{$I Synopse.inc} // define HASINLINE and other compatibility switches
interface
uses
Windows, SysUtils, SynCommons;
type
/// Windows Authentication context handle
TSecHandle = record
dwLower: PtrInt;
dwUpper: PtrInt;
end;
PSecHandle = ^TSecHandle;
/// Windows Authentication context
TSecContext = record
ID: Int64;
CredHandle: TSecHandle;
CtxHandle: TSecHandle;
CreatedTick64: Int64;
end;
PSecContext = ^TSecContext;
/// dynamic array of Windows Authentication contexts
// - used to hold information between calls to ServerSSPIAuth
TSecContextDynArray = array of TSecContext;
/// Sets aSecHandle fields to empty state for a given connection ID
procedure InvalidateSecContext(var aSecContext: TSecContext; aConnectionID: Int64);
/// 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 servsr
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 previos success 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;
/// Encrypts a message
// - aSecContext must be received from previos success call to ServerSSPIAuth
// or ClientSSPIAuth
// - aPlain contains data that must be encrypted
// - returns encrypted message
function SecEncrypt(var aSecContext: TSecContext; const aPlain: RawByteString): RawByteString;
/// Decrypts a message
// - aSecContext must be received from previos success call to ServerSSPIAuth
// or ClientSSPIAuth
// - aEncrypted contains data that must be decrypted
// - returns decrypted message
function SecDecrypt(var aSecContext: TSecContext; const aEncrypted: RawByteString): RawByteString;
/// Free aSecContext on client or server side
procedure FreeSecContext(var aSecContext: TSecContext);
const
/// identification name used for SSPI authentication
SECPKGNAMEINTERNAL = 'Negotiate';
/// HTTP header to be set for SSPI authentication
SECPKGNAMEHTTPWWWAUTHENTICATE = 'WWW-Authenticate: '+SECPKGNAMEINTERNAL;
/// HTTP header pattern received for SSPI authentication
SECPKGNAMEHTTPAUTHORIZATION = 'AUTHORIZATION: NEGOTIATE ';
implementation
type
TSecBuffer = record
cbBuffer: Cardinal; // Size of the buffer, in bytes
BufferType: Cardinal; // Type of the buffer
pvBuffer: Pointer; // Pointer to the buffer
end;
PSecBuffer = ^TSecBuffer;
TSecBufferDesc = record
ulVersion: Cardinal; // Version number
cBuffers: Cardinal; // Number of buffers
pBuffers: PSecBuffer; // Pointer to array of buffers
end;
PSecBufferDesc = ^TSecBufferDesc;
TSecPkgInfoW = record
fCapabilities: Cardinal; // Capability bitmask
wVersion: Word; // Version of driver
wRPCID: Word; // ID for RPC Runtime
cbMaxToken: Cardinal; // Size of authentication token (max)
Name: PWideChar; // Text name
Comment: PWideChar; // Comment
end;
PSecPkgInfoW = ^TSecPkgInfoW;
SecPkgContext_NegotiationInfo = record
PackageInfo: PSecPkgInfoW;
NegotiationState: Cardinal;
end;
SecPkgContext_Sizes = record
cbMaxToken: Cardinal;
cbMaxSignature: Cardinal;
cbBlockSize: Cardinal;
cbSecurityTrailer: Cardinal;
end;
TSecWinntAuthIdentityW = record
User: PWideChar;
UserLength: Cardinal;
Domain: PWideChar;
DomainLength: Cardinal;
Password: PWideChar;
PasswordLength: Cardinal;
Flags: Cardinal
end;
PSecWinntAuthIdentityW = ^TSecWinntAuthIdentityW;
const
SECBUFFER_VERSION = 0;
SECBUFFER_DATA = 1;
SECBUFFER_TOKEN = 2;
SECPKG_CRED_INBOUND = $00000001;
SECPKG_CRED_OUTBOUND = $00000002;
SECPKG_ATTR_SIZES = 0;
SECPKG_ATTR_NEGOTIATION_INFO = 12;
SECURITY_NETWORK_DREP = 0;
SECURITY_NATIVE_DREP = $10;
ISC_REQ_MUTUAL_AUTH = $00000002;
ISC_REQ_CONFIDENTIALITY = $00000010;
ISC_REQ_ALLOCATE_MEMORY = $00000100;
ASC_REQ_CONFIDENTIALITY = $00000010;
ASC_REQ_ALLOCATE_MEMORY = $00000100;
SEC_I_CONTINUE_NEEDED = $00090312;
SEC_I_COMPLETE_NEEDED = $00090313;
SEC_I_COMPLETE_AND_CONTINUE = $00090314;
SEC_WINNT_AUTH_IDENTITY_UNICODE = $02;
secur32 = 'secur32.dll';
function QuerySecurityPackageInfoW(pszPackageName: PWideChar;
var ppPackageInfo: PSecPkgInfoW): Integer; stdcall;
external secur32 name 'QuerySecurityPackageInfoW';
function AcquireCredentialsHandleW(pszPrincipal, pszPackage: PWideChar;
fCredentialUse: Cardinal; pvLogonId: Pointer; pAuthData: PSecWinntAuthIdentityW;
pGetKeyFn: Pointer; pvGetKeyArgument: Pointer; phCredential: PSecHandle;
var ptsExpiry: LARGE_INTEGER): Integer; stdcall;
external secur32 name 'AcquireCredentialsHandleW';
function InitializeSecurityContextW(phCredential: PSecHandle; phContext: PSecHandle;
pszTargetName: PWideChar; fContextReq, Reserved1, TargetDataRep: Cardinal;
pInput: PSecBufferDesc; Reserved2: Cardinal; phNewContext: PSecHandle;
pOutput: PSecBufferDesc; var pfContextAttr: Cardinal; var ptsExpiry: LARGE_INTEGER): Integer; stdcall;
external secur32 name 'InitializeSecurityContextW';
function AcceptSecurityContext(phCredential: PSecHandle; phContext: PSecHandle;
pInput: PSecBufferDesc; fContextReq, TargetDataRep: Cardinal;
phNewContext: PSecHandle; pOutput: PSecBufferDesc; var pfContextAttr: Cardinal;
var ptsExpiry: LARGE_INTEGER): Integer; stdcall;
external secur32 name 'AcceptSecurityContext';
function CompleteAuthToken(phContext: PSecHandle; pToken: PSecBufferDesc): Integer; stdcall;
external secur32 name 'CompleteAuthToken';
function QueryContextAttributesW(phContext: PSecHandle; ulAttribute: Cardinal;
pBuffer: Pointer): Integer; stdcall;
external secur32 name 'QueryContextAttributesW';
function QuerySecurityContextToken(phContext: PSecHandle; var Token: THandle): Integer; stdcall;
external secur32 name 'QuerySecurityContextToken';
function EncryptMessage(phContext: PSecHandle; fQOP: Cardinal;
pToken: PSecBufferDesc; MessageSeqNo: Cardinal): Integer; stdcall;
external secur32 name 'EncryptMessage';
function DecryptMessage(phContext: PSecHandle; pToken: PSecBufferDesc;
MessageSeqNo: Cardinal; var fQOP: Cardinal): Integer; stdcall;
external secur32 name 'DecryptMessage';
function FreeContextBuffer(pvContextBuffer: Pointer): Integer; stdcall;
external secur32 name 'FreeContextBuffer';
function DeleteSecurityContext(phContext: PSecHandle): Integer; stdcall;
external secur32 name 'DeleteSecurityContext';
function FreeCredentialsHandle(phCredential: PSecHandle): Integer; stdcall;
external secur32 name 'FreeCredentialsHandle';
procedure InvalidateSecContext(var aSecContext: TSecContext; aConnectionID: Int64);
begin
aSecContext.ID := aConnectionID;
aSecContext.CredHandle.dwLower := -1;
aSecContext.CredHandle.dwUpper := -1;
aSecContext.CtxHandle.dwLower := -1;
aSecContext.CtxHandle.dwUpper := -1;
aSecContext.CreatedTick64 := 0;
end;
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(SECPKGNAMEINTERNAL, SecPkgInfo) <> 0 then
RaiseLastOSError;
try
if AcquireCredentialsHandleW(nil, SecPkgInfo^.Name, SECPKG_CRED_OUTBOUND, nil, pAuthData, nil, nil, @aSecContext.CredHandle, Expiry) <> 0 then
RaiseLastOSError;
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
RaiseLastOSError;
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
TargetName := nil;
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;
begin
UserPos := PosEx('\', 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;
Result := ClientSSPIAuthWorker(aSecContext, aInData, nil, @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 QuerySecurityPackageInfoW(SECPKGNAMEINTERNAL, SecPkgInfo) <> 0 then
RaiseLastOSError;
try
if AcquireCredentialsHandleW(nil, SecPkgInfo^.Name, SECPKG_CRED_INBOUND, nil, nil, nil, nil, @aSecContext.CredHandle, Expiry) <> 0 then
RaiseLastOSError;
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
RaiseLastOSError;
SetString(aOutData, PAnsiChar(OutBuf.pvBuffer), OutBuf.cbBuffer);
FreeContextBuffer(OutBuf.pvBuffer);
end;
procedure ServerSSPIAuthUser(var aSecContext: TSecContext; out aUserName: RawUTF8);
var UserToken: THandle;
UserInfo: PSIDAndAttributes;
Size: Cardinal;
NameBuf: array[byte] of Char;
NameLen: Cardinal;
DomainBuf: array[byte] of Char;
DomainLen: Cardinal;
NameType: {$ifdef FPC}SID_NAME_USE{$else}Cardinal{$endif};
begin
aUserName := '';
if QuerySecurityContextToken(@aSecContext.CtxHandle, UserToken) <> 0 then
RaiseLastOSError;
try
Size := 0;
GetTokenInformation(UserToken, TokenUser, nil, 0, Size);
UserInfo := AllocMem(Size);
try
if not GetTokenInformation(Cardinal(UserToken), Windows.TokenUser, UserInfo, Size, Size) then
RaiseLastOSError;
FillChar(NameBuf[0], SizeOf(NameBuf), 0);
NameLen := 256;
FillChar(DomainBuf[0], SizeOf(DomainBuf), 0);
DomainLen := 256;
if not LookupAccountSid(nil, UserInfo^.Sid, NameBuf, NameLen, DomainBuf, DomainLen, NameType) then
RaiseLastOSError;
if NameType = SidTypeUser then
aUserName := FormatUTF8('%\%', [DomainBuf, NameBuf]);
finally
FreeMem(UserInfo);
end;
finally
CloseHandle(UserToken);
end;
end;
function SecPackageName(var aSecContext: TSecContext): RawUTF8;
var NegotiationInfo: SecPkgContext_NegotiationInfo;
begin
if QueryContextAttributesW(@aSecContext.CtxHandle, SECPKG_ATTR_NEGOTIATION_INFO, @NegotiationInfo) <> 0 then
RaiseLastOSError;
Result := RawUnicodeToUtf8(NegotiationInfo.PackageInfo^.Name, StrLenW(NegotiationInfo.PackageInfo^.Name));
end;
function SecEncrypt(var aSecContext: TSecContext; const aPlain: RawByteString): RawByteString;
var Sizes: SecPkgContext_Sizes;
SrcLen, EncLen: Cardinal;
EncBuffer: RawByteString;
InBuf: array[0..1] of TSecBuffer;
InDesc: TSecBufferDesc;
Status: Integer;
BufPtr: PByte;
begin
// Sizes.cbSecurityTrailer is size of the trailer (signature + padding) block
if QueryContextAttributesW(@aSecContext.CtxHandle, SECPKG_ATTR_SIZES, @Sizes) <> 0 then
RaiseLastOSError;
SrcLen := Length(aPlain);
EncLen := SizeOf(Cardinal) + Sizes.cbSecurityTrailer + SrcLen;
SetLength(Result, EncLen);
InBuf[0].BufferType := SECBUFFER_TOKEN;
InBuf[0].cbBuffer := Sizes.cbSecurityTrailer;
InBuf[0].pvBuffer := pointer(PtrInt(Result) + SizeOf(Cardinal));
// Encoding done in-place, so we copy the data
EncBuffer := aPlain;
InBuf[1].BufferType := SECBUFFER_DATA;
InBuf[1].cbBuffer := SrcLen;
InBuf[1].pvBuffer := UniqueRawUTF8(RawUTF8(EncBuffer));
InDesc.ulVersion := SECBUFFER_VERSION;
InDesc.cBuffers := 2;
InDesc.pBuffers := @InBuf;
Status := EncryptMessage(@aSecContext.CtxHandle, 0, @InDesc, 0);
if Status < 0 then
RaiseLastOSError;
// Encrypted data buffer structure:
//
// 4 bytes SigLen bytes Remaining bytes
// +--------+----------------+-----------------+
// | SigLen | Trailer | Data |
// +--------+----------------+-----------------+
BufPtr := PByte(Result);
PCardinal(BufPtr)^ := InBuf[0].cbBuffer;
Inc(BufPtr, SizeOf(Cardinal));
Inc(BufPtr, InBuf[0].cbBuffer);
Move(PByte(InBuf[1].pvBuffer)^, BufPtr^, SrcLen);
SetLength(Result, SizeOf(Cardinal) + InBuf[0].cbBuffer + SrcLen);
end;
function SecDecrypt(var aSecContext: TSecContext; const aEncrypted: RawByteString): RawByteString;
var EncLen, SigLen, SrcLen: Cardinal;
BufPtr: PByte;
InBuf: array [0..1] of TSecBuffer;
InDesc: TSecBufferDesc;
Status: Integer;
QOP: Cardinal;
begin
EncLen := Length(aEncrypted);
BufPtr := PByte(aEncrypted);
if EncLen < SizeOf(Cardinal) then begin
SetLastError(ERROR_INVALID_PARAMETER);
RaiseLastOSError;
end;
SigLen := PCardinal(BufPtr)^;
Inc(BufPtr, SizeOf(Cardinal));
if EncLen < (SizeOf(Cardinal) + SigLen) then begin
SetLastError(ERROR_INVALID_PARAMETER);
RaiseLastOSError;
end;
SrcLen := EncLen - SizeOf(Cardinal) - SigLen;
SetLength(Result, SrcLen);
InBuf[0].BufferType := SECBUFFER_TOKEN;
InBuf[0].cbBuffer := SigLen;
InBuf[0].pvBuffer := BufPtr;
Inc(BufPtr, SigLen);
Move(BufPtr^, PByte(Result)^, SrcLen);
InBuf[1].BufferType := SECBUFFER_DATA;
InBuf[1].cbBuffer := SrcLen;
InBuf[1].pvBuffer := PByte(Result);
InDesc.ulVersion := SECBUFFER_VERSION;
InDesc.cBuffers := 2;
InDesc.pBuffers := @InBuf;
Status := DecryptMessage(@aSecContext.CtxHandle, @InDesc, 0, QOP);
if Status < 0 then
RaiseLastOSError;
end;
procedure FreeSecContext(var aSecContext: TSecContext);
begin
if (aSecContext.CtxHandle.dwLower <> -1) or (aSecContext.CtxHandle.dwUpper <> -1) then begin
DeleteSecurityContext(@aSecContext.CtxHandle);
aSecContext.CtxHandle.dwLower := -1;
aSecContext.CtxHandle.dwUpper := -1;
end;
if (aSecContext.CredHandle.dwLower <> -1) or (aSecContext.CredHandle.dwUpper <> -1) then begin
FreeCredentialsHandle(@aSecContext.CredHandle);
aSecContext.CredHandle.dwLower := -1;
aSecContext.CredHandle.dwUpper := -1;
end;
end;
end.