-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTokenManager.pas
More file actions
237 lines (218 loc) · 6.8 KB
/
Copy pathTokenManager.pas
File metadata and controls
237 lines (218 loc) · 6.8 KB
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
unit TokenManager;
interface
uses
System.SysUtils,
System.NetEncoding,
System.IniFiles,
Windows;
type
TTokenManager = class
private type
TNCryptDescriptorHandle = Pointer;
TNCryptBuffer = record
cbBuffer: DWORD;
BufferType: DWORD;
pvBuffer: PVOID;
end;
PNCryptBuffer = ^TNCryptBuffer;
TNCryptBufferDesc = record
ulVersion: DWORD;
cBuffers: DWORD;
pBuffers: PNCryptBuffer;
end;
PNCryptBufferDesc = ^TNCryptBufferDesc;
PPBYTE = ^PBYTE;
private
hNCrypt: HMODULE;
FIniFile: TIniFile;
NCryptCreateProtectionDescriptor: function(
pszDescriptorString: LPCWSTR;
dwFlags: DWORD;
phDescriptor: PPointer
): HRESULT; stdcall;
NCryptProtectSecret: function(
hDescriptor: TNCryptDescriptorHandle;
dwFlags: DWORD;
pbData: PBYTE;
cbData: DWORD;
pMemPara: PNCryptBufferDesc;
hWnd: HWND;
ppbProtectedBlob: PPBYTE;
pcbProtectedBlob: PDWORD
): HRESULT; stdcall;
NCryptUnprotectSecret: function(
hDescriptor: TNCryptDescriptorHandle;
dwFlags: DWORD;
pbProtectedBlob: PBYTE;
cbProtectedBlob: DWORD;
pMemPara: PNCryptBufferDesc;
hWnd: HWND;
ppbData: PPBYTE;
pcbData: PDWORD
): HRESULT; stdcall;
NCryptCloseProtectionDescriptor: function(
hDescriptor: TNCryptDescriptorHandle
): HRESULT; stdcall;
public
constructor Create(const IniFileName: string);
destructor Destroy; override;
function ProtectSecret(const Secret: string): string;
function UnprotectSecret(const ProtectedBlob: string): string;
function RetrieveDecryptedToken(inSection, inName: string): string;
procedure StoreEncryptedToken(const inSection: string; const inName: string; const Token: string);
// procedure StoreExtraData(const Name, Value: string);
procedure StoreExtraSectionData(section: string; const Name, Value: string);
// function RetrieveExtraData(const name: string): string;
function RetrieveExtraSectionData(const section: string; const name: string): string;
end;
implementation
const
NCRYPT_PROTECT_SECRET_VERSION = 1;
function SecureZeroMemory(_ptr: Pointer; cnt: Longint): Pointer;
begin
FillChar(_ptr^, cnt, 0);
Result := _ptr;
end;
constructor TTokenManager.Create(const IniFileName: string);
begin
inherited Create;
FIniFile := TIniFile.Create(IniFileName);
hNCrypt := LoadLibrary('NCrypt.dll');
if hNCrypt <> 0 then
begin
@NCryptCreateProtectionDescriptor := GetProcAddress(hNCrypt, 'NCryptCreateProtectionDescriptor');
@NCryptProtectSecret := GetProcAddress(hNCrypt, 'NCryptProtectSecret');
@NCryptUnprotectSecret := GetProcAddress(hNCrypt, 'NCryptUnprotectSecret');
@NCryptCloseProtectionDescriptor := GetProcAddress(hNCrypt, 'NCryptCloseProtectionDescriptor');
if not Assigned(NCryptCreateProtectionDescriptor) or
not Assigned(NCryptProtectSecret) or
not Assigned(NCryptUnprotectSecret) or
not Assigned(NCryptCloseProtectionDescriptor) then
raise Exception.Create('Failed to load one or more NCrypt functions');
end
else
raise Exception.Create('Failed to load NCrypt.dll');
end;
destructor TTokenManager.Destroy;
begin
FIniFile.Free;
if hNCrypt <> 0 then
FreeLibrary(hNCrypt);
inherited Destroy;
end;
function TTokenManager.ProtectSecret(const Secret: string): string;
var
hDescriptor: TNCryptDescriptorHandle;
ProtectedBlob: PBYTE;
ProtectedBlobSize: DWORD;
ResultCode: HRESULT;
EncryptedBytes: TBytes;
begin
if not Assigned(NCryptCreateProtectionDescriptor) or not Assigned(NCryptProtectSecret) then
raise Exception.Create('NCrypt functions not assigned');
// Create the protection descriptor
ResultCode := NCryptCreateProtectionDescriptor('LOCAL=user', 0, @hDescriptor);
if ResultCode <> S_OK then
RaiseLastOSError(ResultCode);
try
ResultCode := NCryptProtectSecret(
hDescriptor,
0,
PBYTE(Secret),
Length(Secret) * SizeOf(Char), // Length in bytes
nil,
0,
@ProtectedBlob,
@ProtectedBlobSize
);
if ResultCode = S_OK then
begin
SetLength(EncryptedBytes, ProtectedBlobSize);
Move(ProtectedBlob^, EncryptedBytes[0], ProtectedBlobSize);
Result := TNetEncoding.Base64String.EncodeBytesToString(EncryptedBytes);
LocalFree(HLOCAL(ProtectedBlob));
end
else
RaiseLastOSError(ResultCode);
finally
// Close the protection descriptor
NCryptCloseProtectionDescriptor(hDescriptor);
end;
end;
function TTokenManager.UnprotectSecret(const ProtectedBlob: string): string;
var
hDescriptor: TNCryptDescriptorHandle;
Data: PBYTE;
DataSize: DWORD;
ResultCode: HRESULT;
EncryptedBytes: TBytes;
begin
if not Assigned(NCryptCreateProtectionDescriptor) or not Assigned(NCryptUnprotectSecret) then
raise Exception.Create('NCrypt functions not assigned');
// Create the protection descriptor
ResultCode := NCryptCreateProtectionDescriptor('LOCAL=user', 0, @hDescriptor);
if ResultCode <> S_OK then
RaiseLastOSError(ResultCode);
try
EncryptedBytes := TNetEncoding.Base64String.DecodeStringToBytes(ProtectedBlob);
ResultCode := NCryptUnprotectSecret(
hDescriptor,
0,
@EncryptedBytes[0],
Length(EncryptedBytes),
nil,
0,
@Data,
@DataSize
);
if ResultCode = S_OK then
begin
SetLength(Result, DataSize div SizeOf(Char)); // Adjust length for character size
Move(Data^, PChar(Result)^, DataSize);
SecureZeroMemory(Data, DataSize);
LocalFree(HLOCAL(Data));
end
else
RaiseLastOSError(ResultCode);
finally
// Close the protection descriptor
NCryptCloseProtectionDescriptor(hDescriptor);
end;
end;
function TTokenManager.RetrieveDecryptedToken(inSection: string; inName: string): string;
var
EncryptedToken: string;
begin
EncryptedToken := FIniFile.ReadString(inSection, inName, '');
if EncryptedToken = '' then
begin
Result := '';
Exit;
end;
Result := UnprotectSecret(EncryptedToken);
end;
//function TTokenManager.RetrieveExtraData(const name: string): string;
//begin
// Result := FIniFile.ReadString('Token', name, '');
//end;
function TTokenManager.RetrieveExtraSectionData(const section, name: string): string;
begin
Result := FIniFile.ReadString(section, name, '');
end;
procedure TTokenManager.StoreEncryptedToken(const inSection: string; const inName: string; const Token: string);
var
EncryptedToken: string;
begin
EncryptedToken := ProtectSecret(Token);
FIniFile.WriteString(inSection, inName, EncryptedToken);
end;
//
//procedure TTokenManager.StoreExtraData(const Name: string; const Value: string);
//begin
// FIniFile.WriteString('Token', Name, Value);
//end;
procedure TTokenManager.StoreExtraSectionData(section: string; const Name, Value: string);
begin
FIniFile.WriteString(section, Name, Value);
end;
end.