This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathkeytar_win.cc
272 lines (232 loc) · 7.06 KB
/
keytar_win.cc
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
#include "keytar.h"
#define UNICODE
#include <windows.h>
#include <wincred.h>
#include "credentials.h"
namespace keytar {
LPWSTR utf8ToWideChar(std::string utf8) {
int wide_char_length = MultiByteToWideChar(CP_UTF8,
0,
utf8.c_str(),
-1,
NULL,
0);
if (wide_char_length == 0) {
return NULL;
}
LPWSTR result = new WCHAR[wide_char_length];
if (MultiByteToWideChar(CP_UTF8,
0,
utf8.c_str(),
-1,
result,
wide_char_length) == 0) {
delete[] result;
return NULL;
}
return result;
}
std::string wideCharToAnsi(LPWSTR wide_char) {
if (wide_char == NULL) {
return std::string();
}
int ansi_length = WideCharToMultiByte(CP_ACP,
0,
wide_char,
-1,
NULL,
0,
NULL,
NULL);
if (ansi_length == 0) {
return std::string();
}
char* buffer = new char[ansi_length];
if (WideCharToMultiByte(CP_ACP,
0,
wide_char,
-1,
buffer,
ansi_length,
NULL,
NULL) == 0) {
delete[] buffer;
return std::string();
}
std::string result = std::string(buffer);
delete[] buffer;
return result;
}
std::string wideCharToUtf8(LPWSTR wide_char) {
if (wide_char == NULL) {
return std::string();
}
int utf8_length = WideCharToMultiByte(CP_UTF8,
0,
wide_char,
-1,
NULL,
0,
NULL,
NULL);
if (utf8_length == 0) {
return std::string();
}
char* buffer = new char[utf8_length];
if (WideCharToMultiByte(CP_UTF8,
0,
wide_char,
-1,
buffer,
utf8_length,
NULL,
NULL) == 0) {
delete[] buffer;
return std::string();
}
std::string result = std::string(buffer);
delete[] buffer;
return result;
}
std::string getErrorMessage(DWORD errorCode) {
LPWSTR errBuffer;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, errorCode, 0, (LPWSTR) &errBuffer, 0, NULL);
std::string errMsg = wideCharToAnsi(errBuffer);
LocalFree(errBuffer);
return errMsg;
}
KEYTAR_OP_RESULT SetPassword(const std::string& service,
const std::string& account,
const std::string& password,
std::string* errStr) {
LPWSTR target_name = utf8ToWideChar(service + '/' + account);
if (target_name == NULL) {
return FAIL_ERROR;
}
LPWSTR user_name = utf8ToWideChar(account);
if (user_name == NULL) {
return FAIL_ERROR;
}
CREDENTIAL cred = { 0 };
cred.Type = CRED_TYPE_GENERIC;
cred.TargetName = target_name;
cred.UserName = user_name;
cred.CredentialBlobSize = password.size();
cred.CredentialBlob = (LPBYTE)(password.data());
cred.Persist = CRED_PERSIST_ENTERPRISE;
bool result = ::CredWrite(&cred, 0);
delete[] target_name;
if (!result) {
*errStr = getErrorMessage(::GetLastError());
return FAIL_ERROR;
} else {
return SUCCESS;
}
}
KEYTAR_OP_RESULT GetPassword(const std::string& service,
const std::string& account,
std::string* password,
std::string* errStr) {
LPWSTR target_name = utf8ToWideChar(service + '/' + account);
if (target_name == NULL) {
return FAIL_ERROR;
}
CREDENTIAL* cred;
bool result = ::CredRead(target_name, CRED_TYPE_GENERIC, 0, &cred);
delete[] target_name;
if (!result) {
DWORD code = ::GetLastError();
if (code == ERROR_NOT_FOUND) {
return FAIL_NONFATAL;
} else {
*errStr = getErrorMessage(code);
return FAIL_ERROR;
}
}
*password = std::string(reinterpret_cast<char*>(cred->CredentialBlob),
cred->CredentialBlobSize);
::CredFree(cred);
return SUCCESS;
}
KEYTAR_OP_RESULT DeletePassword(const std::string& service,
const std::string& account,
std::string* errStr) {
LPWSTR target_name = utf8ToWideChar(service + '/' + account);
if (target_name == NULL) {
return FAIL_ERROR;
}
bool result = ::CredDelete(target_name, CRED_TYPE_GENERIC, 0);
delete[] target_name;
if (!result) {
DWORD code = ::GetLastError();
if (code == ERROR_NOT_FOUND) {
return FAIL_NONFATAL;
} else {
*errStr = getErrorMessage(code);
return FAIL_ERROR;
}
}
return SUCCESS;
}
KEYTAR_OP_RESULT FindPassword(const std::string& service,
std::string* password,
std::string* errStr) {
LPWSTR filter = utf8ToWideChar(service + "*");
if (filter == NULL) {
return FAIL_ERROR;
}
DWORD count;
CREDENTIAL** creds;
bool result = ::CredEnumerate(filter, 0, &count, &creds);
delete[] filter;
if (!result) {
DWORD code = ::GetLastError();
if (code == ERROR_NOT_FOUND) {
return FAIL_NONFATAL;
} else {
*errStr = getErrorMessage(code);
return FAIL_ERROR;
}
}
*password = std::string(reinterpret_cast<char*>(creds[0]->CredentialBlob),
creds[0]->CredentialBlobSize);
::CredFree(creds);
return SUCCESS;
}
KEYTAR_OP_RESULT FindCredentials(const std::string& service,
std::vector<Credentials>* credentials,
std::string* errStr) {
LPWSTR filter = utf8ToWideChar(service + "*");
if (filter == NULL) {
*errStr = "Error generating credential filter";
return FAIL_ERROR;
}
DWORD count;
CREDENTIAL **creds;
bool result = ::CredEnumerate(filter, 0, &count, &creds);
if (!result) {
DWORD code = ::GetLastError();
if (code == ERROR_NOT_FOUND) {
return FAIL_NONFATAL;
} else {
*errStr = getErrorMessage(code);
return FAIL_ERROR;
}
}
for (unsigned int i = 0; i < count; ++i) {
CREDENTIAL* cred = creds[i];
if (cred->UserName == NULL || cred->CredentialBlobSize == NULL) {
continue;
}
std::string login = wideCharToUtf8(cred->UserName);
std::string password(
reinterpret_cast<char*>(
cred->CredentialBlob),
cred->CredentialBlobSize);
credentials->push_back(Credentials(login, password));
}
CredFree(creds);
return SUCCESS;
}
} // namespace keytar