forked from eghuro/qtkeychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keychain_win.cpp
271 lines (239 loc) · 8.92 KB
/
keychain_win.cpp
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
/******************************************************************************
* Copyright (C) 2011-2015 Frank Osterfeld <[email protected]> *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution *
* details, check the accompanying file 'COPYING'. *
*****************************************************************************/
#include "keychain_p.h"
#include "plaintextstore_p.h"
#include <windows.h>
#include <wincrypt.h>
#include <memory>
using namespace QKeychain;
#if defined(USE_CREDENTIAL_STORE)
#include <wincred.h>
void ReadPasswordJobPrivate::scheduledStart() {
LPCWSTR name = (LPCWSTR)key.utf16();
PCREDENTIALW cred;
if (!CredReadW(name, CRED_TYPE_GENERIC, 0, &cred)) {
Error error;
QString msg;
switch(GetLastError()) {
case ERROR_NO_SUCH_LOGON_SESSION:
error = NoSuchLogonSession;
msg = tr("No such logon session");
break;
case ERROR_INVALID_PARAMETER:
error = InvalidParameter;
msg = tr("Invalid parameter");
break;
case ERROR_INVALID_FLAGS:
error = InvalidFlags;
msg = tr("Invalid flags");
break;
case ERROR_BAD_USERNAME:
error = BadUserame;
msg = tr("Bad username");
break;
case ERROR_NOT_FOUND:
error = EntryNotFound;
msg = tr("Password entry not found");
break;
case SCARD_E_NO_READERS_AVAILABLE:
error = NoReadersAvailable;
msg = tr("Smart card reader is not available");
break;
case SCARD_E_NO_SMARTCARD:
case SCARD_W_REMOVED_CARD:
error = NoSmartcard;
msg = tr("Smart card not present");
break;
case SCARD_W_WRONG_CHV:
error = BadSmartcardPin;
msg = tr("Bad pin");
break;
default:
error = OtherError;
msg = tr("Could not decrypt data");
break;
}
q->emitFinishedWithError( error, msg );
return;
}
data.first = QByteArray((char*)cred->CredentialBlob, cred->CredentialBlobSize);
for (DWORD i=0; i < cred->AttributeCount; i++) {
const CREDENTIAL_ATTRIBUTEW &cred_attr = cred->Attributes[i];
const QString attr_name{ QString::fromUtf16((const ushort *)cred_attr.Keyword) };
data.second[attr_name] = QByteArray((char *) cred_attr.Value, cred_attr.ValueSize);
}
CredFree(cred);
q->emitFinished();
}
void WritePasswordJobPrivate::scheduledStart() {
CREDENTIALW cred;
char *pwd = data.first.data();
LPWSTR name = (LPWSTR)key.utf16();
const Attributes &attrs = data.second;
memset(&cred, 0, sizeof(cred));
cred.Comment = const_cast<wchar_t*>(L"QtKeychain");
cred.Type = CRED_TYPE_GENERIC;
cred.TargetName = name;
cred.CredentialBlobSize = data.first.size();
cred.CredentialBlob = (LPBYTE)pwd;
cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
if (attrs.size() > 0) {
cred.AttributeCount = attrs.size();
cred.Attributes = new CREDENTIAL_ATTRIBUTEW[cred.AttributeCount];
QKeychain::AttributesIterator iter(attrs);
DWORD i = 0;
while (iter.hasNext()) {
iter.next();
cred.Attributes[i].Keyword = (LPWSTR) iter.key().utf16();
cred.Attributes[i].ValueSize = iter.value().length();
cred.Attributes[i].Value = (LPBYTE) iter.value().data();
i++;
}
}
if (CredWriteW(&cred, 0)) {
q->emitFinished();
delete[] cred.Attributes;
return;
}
delete[] cred.Attributes;
DWORD err = GetLastError();
// Detect size-exceeded errors and provide nicer messages.
// Unfortunately these error codes aren't documented.
// Found empirically on Win10 1803 build 17134.523.
if (err == RPC_X_BAD_STUB_DATA) {
const size_t maxBlob = CRED_MAX_CREDENTIAL_BLOB_SIZE;
if (cred.CredentialBlobSize > maxBlob) {
q->emitFinishedWithError(
OtherError,
tr("Credential size exceeds maximum size of %1").arg(maxBlob));
return;
}
}
if (err == RPC_S_INVALID_BOUND) {
const size_t maxTargetName = CRED_MAX_GENERIC_TARGET_NAME_LENGTH;
if (key.size() > maxTargetName) {
q->emitFinishedWithError(
OtherError,
tr("Credential key exceeds maximum size of %1").arg(maxTargetName));
return;
}
}
q->emitFinishedWithError( OtherError, tr("Writing credentials failed: Win32 error code %1").arg(err) );
}
void DeletePasswordJobPrivate::scheduledStart() {
LPCWSTR name = (LPCWSTR)key.utf16();
if (!CredDeleteW(name, CRED_TYPE_GENERIC, 0)) {
Error error;
QString msg;
switch(GetLastError()) {
case ERROR_NO_SUCH_LOGON_SESSION:
error = NoSuchLogonSession;
msg = tr("No such logon session");
break;
case ERROR_INVALID_PARAMETER:
error = InvalidParameter;
msg = tr("Invalid parameter");
break;
case ERROR_INVALID_FLAGS:
error = InvalidFlags;
msg = tr("Invalid flags");
break;
case ERROR_BAD_USERNAME:
error = BadUserame;
msg = tr("Bad username");
break;
case ERROR_NOT_FOUND:
error = EntryNotFound;
msg = tr("Password entry not found");
break;
case SCARD_E_NO_READERS_AVAILABLE:
error = NoReadersAvailable;
msg = tr("Smart card reader is not available");
break;
case SCARD_E_NO_SMARTCARD:
case SCARD_W_REMOVED_CARD:
error = NoSmartcard;
msg = tr("Smart card not present");
break;
case SCARD_W_WRONG_CHV:
error = BadSmartcardPin;
msg = tr("Bad pin");
break;
default:
error = OtherError;
msg = tr("Could not decrypt data");
break;
}
q->emitFinishedWithError( error, msg );
} else {
q->emitFinished();
}
}
#else
void ReadPasswordJobPrivate::scheduledStart() {
PlainTextStore plainTextStore( q->service(), q->settings() );
QByteArray encrypted = plainTextStore.readData( key );
if ( plainTextStore.error() != NoError ) {
q->emitFinishedWithError( plainTextStore.error(), plainTextStore.errorString() );
return;
}
DATA_BLOB blob_in, blob_out;
blob_in.pbData = reinterpret_cast<BYTE*>( encrypted.data() );
blob_in.cbData = encrypted.size();
const BOOL ret = CryptUnprotectData( &blob_in,
NULL,
NULL,
NULL,
NULL,
0,
&blob_out );
if ( !ret ) {
q->emitFinishedWithError( OtherError, tr("Could not decrypt data") );
return;
}
data = QByteArray( reinterpret_cast<char*>( blob_out.pbData ), blob_out.cbData );
SecureZeroMemory( blob_out.pbData, blob_out.cbData );
LocalFree( blob_out.pbData );
q->emitFinished();
}
void WritePasswordJobPrivate::scheduledStart() {
DATA_BLOB blob_in, blob_out;
blob_in.pbData = reinterpret_cast<BYTE*>( data.data() );
blob_in.cbData = data.size();
const BOOL res = CryptProtectData( &blob_in,
L"QKeychain-encrypted data",
NULL,
NULL,
NULL,
0,
&blob_out );
if ( !res ) {
q->emitFinishedWithError( OtherError, tr("Encryption failed") ); //TODO more details available?
return;
}
const QByteArray encrypted( reinterpret_cast<char*>( blob_out.pbData ), blob_out.cbData );
LocalFree( blob_out.pbData );
PlainTextStore plainTextStore( q->service(), q->settings() );
plainTextStore.write( key, encrypted, Binary );
if ( plainTextStore.error() != NoError ) {
q->emitFinishedWithError( plainTextStore.error(), plainTextStore.errorString() );
return;
}
q->emitFinished();
}
void DeletePasswordJobPrivate::scheduledStart() {
PlainTextStore plainTextStore( q->service(), q->settings() );
plainTextStore.remove( key );
if ( plainTextStore.error() != NoError ) {
q->emitFinishedWithError( plainTextStore.error(), plainTextStore.errorString() );
} else {
q->emitFinished();
}
}
#endif