2 * Credential Management APIs
4 * Copyright 2007 Robert Shearman for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 # include <Security/SecKeychain.h>
32 # include <Security/SecKeychainItem.h>
33 # include <Security/SecKeychainSearch.h>
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(cred
);
43 /* the size of the ARC4 key used to encrypt the password data */
46 static const WCHAR wszCredentialManagerKey
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
47 'C','r','e','d','e','n','t','i','a','l',' ','M','a','n','a','g','e','r',0};
48 static const WCHAR wszEncryptionKeyValue
[] = {'E','n','c','r','y','p','t','i','o','n','K','e','y',0};
50 static const WCHAR wszFlagsValue
[] = {'F','l','a','g','s',0};
51 static const WCHAR wszTypeValue
[] = {'T','y','p','e',0};
52 static const WCHAR wszCommentValue
[] = {'C','o','m','m','e','n','t',0};
53 static const WCHAR wszLastWrittenValue
[] = {'L','a','s','t','W','r','i','t','t','e','n',0};
54 static const WCHAR wszPersistValue
[] = {'P','e','r','s','i','s','t',0};
55 static const WCHAR wszTargetAliasValue
[] = {'T','a','r','g','e','t','A','l','i','a','s',0};
56 static const WCHAR wszUserNameValue
[] = {'U','s','e','r','N','a','m','e',0};
57 static const WCHAR wszPasswordValue
[] = {'P','a','s','s','w','o','r','d',0};
59 static DWORD
read_credential_blob(HKEY hkey
, const BYTE key_data
[KEY_SIZE
],
60 LPBYTE credential_blob
,
61 DWORD
*credential_blob_size
)
66 *credential_blob_size
= 0;
67 ret
= RegQueryValueExW(hkey
, wszPasswordValue
, 0, &type
, NULL
, credential_blob_size
);
68 if (ret
!= ERROR_SUCCESS
)
70 else if (type
!= REG_BINARY
)
71 return ERROR_REGISTRY_CORRUPT
;
77 ret
= RegQueryValueExW(hkey
, wszPasswordValue
, 0, &type
, credential_blob
,
78 credential_blob_size
);
79 if (ret
!= ERROR_SUCCESS
)
81 else if (type
!= REG_BINARY
)
82 return ERROR_REGISTRY_CORRUPT
;
84 key
.Length
= key
.MaximumLength
= KEY_SIZE
;
85 key
.Buffer
= (unsigned char *)key_data
;
87 data
.Length
= data
.MaximumLength
= *credential_blob_size
;
88 data
.Buffer
= credential_blob
;
89 SystemFunction032(&data
, &key
);
94 static DWORD
registry_read_credential(HKEY hkey
, PCREDENTIALW credential
,
95 const BYTE key_data
[KEY_SIZE
],
96 char *buffer
, DWORD
*len
)
102 ret
= RegQueryValueExW(hkey
, NULL
, 0, &type
, NULL
, &count
);
103 if (ret
!= ERROR_SUCCESS
)
105 else if (type
!= REG_SZ
)
106 return ERROR_REGISTRY_CORRUPT
;
110 credential
->TargetName
= (LPWSTR
)buffer
;
111 ret
= RegQueryValueExW(hkey
, NULL
, 0, &type
, (LPVOID
)credential
->TargetName
,
113 if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
) return ret
;
117 ret
= RegQueryValueExW(hkey
, wszCommentValue
, 0, &type
, NULL
, &count
);
118 if (ret
!= ERROR_FILE_NOT_FOUND
&& ret
!= ERROR_SUCCESS
)
120 else if (type
!= REG_SZ
)
121 return ERROR_REGISTRY_CORRUPT
;
125 credential
->Comment
= (LPWSTR
)buffer
;
126 ret
= RegQueryValueExW(hkey
, wszCommentValue
, 0, &type
, (LPVOID
)credential
->Comment
,
128 if (ret
== ERROR_FILE_NOT_FOUND
)
129 credential
->Comment
= NULL
;
130 else if (ret
!= ERROR_SUCCESS
)
132 else if (type
!= REG_SZ
)
133 return ERROR_REGISTRY_CORRUPT
;
138 ret
= RegQueryValueExW(hkey
, wszTargetAliasValue
, 0, &type
, NULL
, &count
);
139 if (ret
!= ERROR_FILE_NOT_FOUND
&& ret
!= ERROR_SUCCESS
)
141 else if (type
!= REG_SZ
)
142 return ERROR_REGISTRY_CORRUPT
;
146 credential
->TargetAlias
= (LPWSTR
)buffer
;
147 ret
= RegQueryValueExW(hkey
, wszTargetAliasValue
, 0, &type
, (LPVOID
)credential
->TargetAlias
,
149 if (ret
== ERROR_FILE_NOT_FOUND
)
150 credential
->TargetAlias
= NULL
;
151 else if (ret
!= ERROR_SUCCESS
)
153 else if (type
!= REG_SZ
)
154 return ERROR_REGISTRY_CORRUPT
;
159 ret
= RegQueryValueExW(hkey
, wszUserNameValue
, 0, &type
, NULL
, &count
);
160 if (ret
!= ERROR_FILE_NOT_FOUND
&& ret
!= ERROR_SUCCESS
)
162 else if (type
!= REG_SZ
)
163 return ERROR_REGISTRY_CORRUPT
;
167 credential
->UserName
= (LPWSTR
)buffer
;
168 ret
= RegQueryValueExW(hkey
, wszUserNameValue
, 0, &type
, (LPVOID
)credential
->UserName
,
170 if (ret
== ERROR_FILE_NOT_FOUND
)
171 credential
->UserName
= NULL
;
172 else if (ret
!= ERROR_SUCCESS
)
174 else if (type
!= REG_SZ
)
175 return ERROR_REGISTRY_CORRUPT
;
180 ret
= read_credential_blob(hkey
, key_data
, NULL
, &count
);
181 if (ret
!= ERROR_FILE_NOT_FOUND
&& ret
!= ERROR_SUCCESS
)
186 credential
->CredentialBlob
= (LPBYTE
)buffer
;
187 ret
= read_credential_blob(hkey
, key_data
, credential
->CredentialBlob
, &count
);
188 if (ret
== ERROR_FILE_NOT_FOUND
)
189 credential
->CredentialBlob
= NULL
;
190 else if (ret
!= ERROR_SUCCESS
)
192 credential
->CredentialBlobSize
= count
;
196 /* FIXME: Attributes */
199 credential
->AttributeCount
= 0;
200 credential
->Attributes
= NULL
;
203 if (!credential
) return ERROR_SUCCESS
;
205 count
= sizeof(credential
->Flags
);
206 ret
= RegQueryValueExW(hkey
, wszFlagsValue
, NULL
, &type
, (LPVOID
)&credential
->Flags
,
208 if (ret
!= ERROR_SUCCESS
)
210 else if (type
!= REG_DWORD
)
211 return ERROR_REGISTRY_CORRUPT
;
212 count
= sizeof(credential
->Type
);
213 ret
= RegQueryValueExW(hkey
, wszTypeValue
, NULL
, &type
, (LPVOID
)&credential
->Type
,
215 if (ret
!= ERROR_SUCCESS
)
217 else if (type
!= REG_DWORD
)
218 return ERROR_REGISTRY_CORRUPT
;
220 count
= sizeof(credential
->LastWritten
);
221 ret
= RegQueryValueExW(hkey
, wszLastWrittenValue
, NULL
, &type
, (LPVOID
)&credential
->LastWritten
,
223 if (ret
!= ERROR_SUCCESS
)
225 else if (type
!= REG_BINARY
)
226 return ERROR_REGISTRY_CORRUPT
;
227 count
= sizeof(credential
->Persist
);
228 ret
= RegQueryValueExW(hkey
, wszPersistValue
, NULL
, &type
, (LPVOID
)&credential
->Persist
,
230 if (ret
== ERROR_SUCCESS
&& type
!= REG_DWORD
)
231 return ERROR_REGISTRY_CORRUPT
;
236 static DWORD
mac_read_credential_from_item(SecKeychainItemRef item
, BOOL require_password
,
237 PCREDENTIALW credential
, char *buffer
,
242 UInt32 cred_blob_len
;
244 LPWSTR domain
= NULL
;
246 BOOL user_name_present
= FALSE
;
247 SecKeychainAttributeInfo info
;
248 SecKeychainAttributeList
*attr_list
;
249 UInt32 info_tags
[] = { kSecServerItemAttr
, kSecSecurityDomainItemAttr
, kSecAccountItemAttr
,
250 kSecCommentItemAttr
, kSecCreationDateItemAttr
};
251 info
.count
= sizeof(info_tags
)/sizeof(info_tags
[0]);
252 info
.tag
= info_tags
;
254 status
= SecKeychainItemCopyAttributesAndData(item
, &info
, NULL
, &attr_list
, &cred_blob_len
, &cred_blob
);
255 if (status
== errSecAuthFailed
&& !require_password
)
259 status
= SecKeychainItemCopyAttributesAndData(item
, &info
, NULL
, &attr_list
, &cred_blob_len
, NULL
);
263 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status
);
264 return ERROR_NOT_FOUND
;
267 for (i
= 0; i
< attr_list
->count
; i
++)
268 if (attr_list
->attr
[i
].tag
== kSecAccountItemAttr
&& attr_list
->attr
[i
].data
)
270 user_name_present
= TRUE
;
273 if (!user_name_present
)
275 WARN("no kSecAccountItemAttr for item\n");
276 return ERROR_NOT_FOUND
;
281 credential
->Flags
= 0;
282 credential
->Type
= CRED_TYPE_DOMAIN_PASSWORD
;
283 credential
->TargetName
= NULL
;
284 credential
->Comment
= NULL
;
285 memset(&credential
->LastWritten
, 0, sizeof(credential
->LastWritten
));
286 credential
->CredentialBlobSize
= 0;
287 credential
->CredentialBlob
= NULL
;
288 credential
->Persist
= CRED_PERSIST_LOCAL_MACHINE
;
289 credential
->AttributeCount
= 0;
290 credential
->Attributes
= NULL
;
291 credential
->TargetAlias
= NULL
;
292 credential
->UserName
= NULL
;
294 for (i
= 0; i
< attr_list
->count
; i
++)
296 switch (attr_list
->attr
[i
].tag
)
298 case kSecServerItemAttr
:
299 TRACE("kSecServerItemAttr: %.*s\n", (int)attr_list
->attr
[i
].length
,
300 (char *)attr_list
->attr
[i
].data
);
301 if (!attr_list
->attr
[i
].data
) continue;
305 credential
->TargetName
= (LPWSTR
)buffer
;
306 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
307 attr_list
->attr
[i
].length
, (LPWSTR
)buffer
, 0xffff);
308 credential
->TargetName
[str_len
] = '\0';
309 buffer
+= (str_len
+ 1) * sizeof(WCHAR
);
310 *len
+= (str_len
+ 1) * sizeof(WCHAR
);
315 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
316 attr_list
->attr
[i
].length
, NULL
, 0);
317 *len
+= (str_len
+ 1) * sizeof(WCHAR
);
320 case kSecAccountItemAttr
:
323 TRACE("kSecAccountItemAttr: %.*s\n", (int)attr_list
->attr
[i
].length
,
324 (char *)attr_list
->attr
[i
].data
);
325 if (!attr_list
->attr
[i
].data
) continue;
326 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
327 attr_list
->attr
[i
].length
, NULL
, 0);
328 user
= HeapAlloc(GetProcessHeap(), 0, (str_len
+ 1) * sizeof(WCHAR
));
329 MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
330 attr_list
->attr
[i
].length
, user
, str_len
);
331 user
[str_len
] = '\0';
334 case kSecCommentItemAttr
:
335 TRACE("kSecCommentItemAttr: %.*s\n", (int)attr_list
->attr
[i
].length
,
336 (char *)attr_list
->attr
[i
].data
);
337 if (!attr_list
->attr
[i
].data
) continue;
341 credential
->Comment
= (LPWSTR
)buffer
;
342 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
343 attr_list
->attr
[i
].length
, (LPWSTR
)buffer
, 0xffff);
344 credential
->Comment
[str_len
] = '\0';
345 buffer
+= (str_len
+ 1) * sizeof(WCHAR
);
346 *len
+= (str_len
+ 1) * sizeof(WCHAR
);
351 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
352 attr_list
->attr
[i
].length
, NULL
, 0);
353 *len
+= (str_len
+ 1) * sizeof(WCHAR
);
356 case kSecSecurityDomainItemAttr
:
359 TRACE("kSecSecurityDomainItemAttr: %.*s\n", (int)attr_list
->attr
[i
].length
,
360 (char *)attr_list
->attr
[i
].data
);
361 if (!attr_list
->attr
[i
].data
) continue;
362 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
363 attr_list
->attr
[i
].length
, NULL
, 0);
364 domain
= HeapAlloc(GetProcessHeap(), 0, (str_len
+ 1) * sizeof(WCHAR
));
365 MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[i
].data
,
366 attr_list
->attr
[i
].length
, domain
, str_len
);
367 domain
[str_len
] = '\0';
370 case kSecCreationDateItemAttr
:
371 TRACE("kSecCreationDateItemAttr: %.*s\n", (int)attr_list
->attr
[i
].length
,
372 (char *)attr_list
->attr
[i
].data
);
375 LARGE_INTEGER win_time
;
378 memset(&tm
, 0, sizeof(tm
));
379 strptime(attr_list
->attr
[i
].data
, "%Y%m%d%H%M%SZ", &tm
);
381 RtlSecondsSince1970ToTime(time
, &win_time
);
382 credential
->LastWritten
.dwLowDateTime
= win_time
.u
.LowPart
;
383 credential
->LastWritten
.dwHighDateTime
= win_time
.u
.HighPart
;
393 credential
->UserName
= (LPWSTR
)buffer
;
396 str_len
= strlenW(domain
);
397 *len
+= (str_len
+ 1) * sizeof(WCHAR
);
400 memcpy(credential
->UserName
, domain
, str_len
* sizeof(WCHAR
));
401 /* FIXME: figure out when to use an '@' */
402 credential
->UserName
[str_len
] = '\\';
403 buffer
+= (str_len
+ 1) * sizeof(WCHAR
);
406 str_len
= strlenW(user
);
407 *len
+= (str_len
+ 1) * sizeof(WCHAR
);
410 memcpy(buffer
, user
, (str_len
+ 1) * sizeof(WCHAR
));
411 buffer
+= (str_len
+ 1) * sizeof(WCHAR
);
412 TRACE("UserName = %s\n", debugstr_w(credential
->UserName
));
415 HeapFree(GetProcessHeap(), 0, user
);
416 HeapFree(GetProcessHeap(), 0, domain
);
423 credential
->CredentialBlob
= (BYTE
*)buffer
;
424 str_len
= MultiByteToWideChar(CP_UTF8
, 0, cred_blob
, cred_blob_len
,
425 (LPWSTR
)buffer
, 0xffff);
426 credential
->CredentialBlobSize
= str_len
* sizeof(WCHAR
);
427 buffer
+= str_len
* sizeof(WCHAR
);
428 *len
+= str_len
* sizeof(WCHAR
);
433 str_len
= MultiByteToWideChar(CP_UTF8
, 0, cred_blob
, cred_blob_len
,
435 *len
+= str_len
* sizeof(WCHAR
);
438 SecKeychainItemFreeAttributesAndData(attr_list
, cred_blob
);
439 return ERROR_SUCCESS
;
443 static DWORD
write_credential_blob(HKEY hkey
, LPCWSTR target_name
, DWORD type
,
444 const BYTE key_data
[KEY_SIZE
],
445 const BYTE
*credential_blob
, DWORD credential_blob_size
)
447 LPBYTE encrypted_credential_blob
;
452 key
.Length
= key
.MaximumLength
= KEY_SIZE
;
453 key
.Buffer
= (unsigned char *)key_data
;
455 encrypted_credential_blob
= HeapAlloc(GetProcessHeap(), 0, credential_blob_size
);
456 if (!encrypted_credential_blob
) return ERROR_OUTOFMEMORY
;
458 memcpy(encrypted_credential_blob
, credential_blob
, credential_blob_size
);
459 data
.Length
= data
.MaximumLength
= credential_blob_size
;
460 data
.Buffer
= encrypted_credential_blob
;
461 SystemFunction032(&data
, &key
);
463 ret
= RegSetValueExW(hkey
, wszPasswordValue
, 0, REG_BINARY
, encrypted_credential_blob
, credential_blob_size
);
464 HeapFree(GetProcessHeap(), 0, encrypted_credential_blob
);
469 static DWORD
registry_write_credential(HKEY hkey
, const CREDENTIALW
*credential
,
470 const BYTE key_data
[KEY_SIZE
], BOOL preserve_blob
)
473 FILETIME LastWritten
;
475 GetSystemTimeAsFileTime(&LastWritten
);
477 ret
= RegSetValueExW(hkey
, wszFlagsValue
, 0, REG_DWORD
, (LPVOID
)&credential
->Flags
,
478 sizeof(credential
->Flags
));
479 if (ret
!= ERROR_SUCCESS
) return ret
;
480 ret
= RegSetValueExW(hkey
, wszTypeValue
, 0, REG_DWORD
, (LPVOID
)&credential
->Type
,
481 sizeof(credential
->Type
));
482 if (ret
!= ERROR_SUCCESS
) return ret
;
483 ret
= RegSetValueExW(hkey
, NULL
, 0, REG_SZ
, (LPVOID
)credential
->TargetName
,
484 sizeof(WCHAR
)*(strlenW(credential
->TargetName
)+1));
485 if (ret
!= ERROR_SUCCESS
) return ret
;
486 if (credential
->Comment
)
488 ret
= RegSetValueExW(hkey
, wszCommentValue
, 0, REG_SZ
, (LPVOID
)credential
->Comment
,
489 sizeof(WCHAR
)*(strlenW(credential
->Comment
)+1));
490 if (ret
!= ERROR_SUCCESS
) return ret
;
492 ret
= RegSetValueExW(hkey
, wszLastWrittenValue
, 0, REG_BINARY
, (LPVOID
)&LastWritten
,
493 sizeof(LastWritten
));
494 if (ret
!= ERROR_SUCCESS
) return ret
;
495 ret
= RegSetValueExW(hkey
, wszPersistValue
, 0, REG_DWORD
, (LPVOID
)&credential
->Persist
,
496 sizeof(credential
->Persist
));
497 if (ret
!= ERROR_SUCCESS
) return ret
;
498 /* FIXME: Attributes */
499 if (credential
->TargetAlias
)
501 ret
= RegSetValueExW(hkey
, wszTargetAliasValue
, 0, REG_SZ
, (LPVOID
)credential
->TargetAlias
,
502 sizeof(WCHAR
)*(strlenW(credential
->TargetAlias
)+1));
503 if (ret
!= ERROR_SUCCESS
) return ret
;
505 if (credential
->UserName
)
507 ret
= RegSetValueExW(hkey
, wszUserNameValue
, 0, REG_SZ
, (LPVOID
)credential
->UserName
,
508 sizeof(WCHAR
)*(strlenW(credential
->UserName
)+1));
509 if (ret
!= ERROR_SUCCESS
) return ret
;
513 ret
= write_credential_blob(hkey
, credential
->TargetName
, credential
->Type
,
514 key_data
, credential
->CredentialBlob
,
515 credential
->CredentialBlobSize
);
521 static DWORD
mac_write_credential(const CREDENTIALW
*credential
, BOOL preserve_blob
)
524 SecKeychainItemRef keychain_item
;
530 UInt32 domainlen
= 0;
534 SecKeychainAttribute attrs
[1];
535 SecKeychainAttributeList attr_list
;
537 if (credential
->Flags
)
538 FIXME("Flags 0x%x not written\n", credential
->Flags
);
539 if (credential
->Type
!= CRED_TYPE_DOMAIN_PASSWORD
)
540 FIXME("credential type of %d not supported\n", credential
->Type
);
541 if (credential
->Persist
!= CRED_PERSIST_LOCAL_MACHINE
)
542 FIXME("persist value of %d not supported\n", credential
->Persist
);
543 if (credential
->AttributeCount
)
544 FIXME("custom attributes not supported\n");
546 p
= strchrW(credential
->UserName
, '\\');
549 domainlen
= WideCharToMultiByte(CP_UTF8
, 0, credential
->UserName
,
550 p
- credential
->UserName
, NULL
, 0, NULL
, NULL
);
551 domain
= HeapAlloc(GetProcessHeap(), 0, (domainlen
+ 1) * sizeof(*domain
));
552 WideCharToMultiByte(CP_UTF8
, 0, credential
->UserName
, p
- credential
->UserName
,
553 domain
, domainlen
, NULL
, NULL
);
554 domain
[domainlen
] = '\0';
558 p
= credential
->UserName
;
559 userlen
= WideCharToMultiByte(CP_UTF8
, 0, p
, -1, NULL
, 0, NULL
, NULL
);
560 username
= HeapAlloc(GetProcessHeap(), 0, userlen
* sizeof(*username
));
561 WideCharToMultiByte(CP_UTF8
, 0, p
, -1, username
, userlen
, NULL
, NULL
);
563 serverlen
= WideCharToMultiByte(CP_UTF8
, 0, credential
->TargetName
, -1, NULL
, 0, NULL
, NULL
);
564 servername
= HeapAlloc(GetProcessHeap(), 0, serverlen
* sizeof(*servername
));
565 WideCharToMultiByte(CP_UTF8
, 0, credential
->TargetName
, -1, servername
, serverlen
, NULL
, NULL
);
566 pwlen
= WideCharToMultiByte(CP_UTF8
, 0, (LPCWSTR
)credential
->CredentialBlob
,
567 credential
->CredentialBlobSize
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
568 password
= HeapAlloc(GetProcessHeap(), 0, pwlen
* sizeof(*domain
));
569 WideCharToMultiByte(CP_UTF8
, 0, (LPCWSTR
)credential
->CredentialBlob
,
570 credential
->CredentialBlobSize
/ sizeof(WCHAR
), password
, pwlen
, NULL
, NULL
);
572 TRACE("adding server %s, domain %s, username %s using Keychain\n", servername
, domain
, username
);
573 status
= SecKeychainAddInternetPassword(NULL
, strlen(servername
), servername
,
574 strlen(domain
), domain
, strlen(username
),
575 username
, 0, NULL
, 0,
577 kSecAuthenticationTypeDefault
,
578 strlen(password
), password
, &keychain_item
);
580 ERR("SecKeychainAddInternetPassword returned %ld\n", status
);
581 if (status
== errSecDuplicateItem
)
583 SecKeychainItemRef keychain_item
;
585 status
= SecKeychainFindInternetPassword(NULL
, strlen(servername
), servername
,
586 strlen(domain
), domain
,
587 strlen(username
), username
,
588 0, NULL
/* any path */, 0,
589 0 /* any protocol */,
590 0 /* any authentication type */,
591 0, NULL
, &keychain_item
);
593 ERR("SecKeychainFindInternetPassword returned %ld\n", status
);
595 HeapFree(GetProcessHeap(), 0, domain
);
596 HeapFree(GetProcessHeap(), 0, username
);
597 HeapFree(GetProcessHeap(), 0, servername
);
600 HeapFree(GetProcessHeap(), 0, password
);
601 return ERROR_GEN_FAILURE
;
603 if (credential
->Comment
)
606 attr_list
.attr
= attrs
;
607 attrs
[0].tag
= kSecCommentItemAttr
;
608 attrs
[0].length
= WideCharToMultiByte(CP_UTF8
, 0, credential
->Comment
, -1, NULL
, 0, NULL
, NULL
);
609 if (attrs
[0].length
) attrs
[0].length
--;
610 attrs
[0].data
= HeapAlloc(GetProcessHeap(), 0, attrs
[0].length
);
611 WideCharToMultiByte(CP_UTF8
, 0, credential
->Comment
, -1, attrs
[0].data
, attrs
[0].length
, NULL
, NULL
);
616 attr_list
.attr
= NULL
;
618 status
= SecKeychainItemModifyAttributesAndData(keychain_item
, &attr_list
,
619 preserve_blob
? 0 : strlen(password
),
620 preserve_blob
? NULL
: password
);
621 if (credential
->Comment
)
622 HeapFree(GetProcessHeap(), 0, attrs
[0].data
);
623 HeapFree(GetProcessHeap(), 0, password
);
624 /* FIXME: set TargetAlias attribute */
625 CFRelease(keychain_item
);
626 return ERROR_SUCCESS
;
630 static DWORD
open_cred_mgr_key(HKEY
*hkey
, BOOL open_for_write
)
632 return RegCreateKeyExW(HKEY_CURRENT_USER
, wszCredentialManagerKey
, 0,
633 NULL
, REG_OPTION_NON_VOLATILE
,
634 KEY_READ
| (open_for_write
? KEY_WRITE
: 0), NULL
, hkey
, NULL
);
637 static DWORD
get_cred_mgr_encryption_key(HKEY hkeyMgr
, BYTE key_data
[KEY_SIZE
])
639 static const BYTE my_key_data
[KEY_SIZE
] = { 0 };
647 memcpy(key_data
, my_key_data
, KEY_SIZE
);
650 ret
= RegQueryValueExW(hkeyMgr
, wszEncryptionKeyValue
, NULL
, &type
, key_data
,
652 if (ret
== ERROR_SUCCESS
)
654 if (type
!= REG_BINARY
)
655 return ERROR_REGISTRY_CORRUPT
;
657 return ERROR_SUCCESS
;
659 if (ret
!= ERROR_FILE_NOT_FOUND
)
662 GetSystemTimeAsFileTime(&ft
);
663 seed
= ft
.dwLowDateTime
;
664 value
= RtlUniform(&seed
);
665 *(DWORD
*)key_data
= value
;
666 seed
= ft
.dwHighDateTime
;
667 value
= RtlUniform(&seed
);
668 *(DWORD
*)(key_data
+ 4) = value
;
670 ret
= RegSetValueExW(hkeyMgr
, wszEncryptionKeyValue
, 0, REG_BINARY
,
672 if (ret
== ERROR_ACCESS_DENIED
)
674 ret
= open_cred_mgr_key(&hkeyMgr
, TRUE
);
675 if (ret
== ERROR_SUCCESS
)
677 ret
= RegSetValueExW(hkeyMgr
, wszEncryptionKeyValue
, 0, REG_BINARY
,
679 RegCloseKey(hkeyMgr
);
685 static LPWSTR
get_key_name_for_target(LPCWSTR target_name
, DWORD type
)
687 static const WCHAR wszGenericPrefix
[] = {'G','e','n','e','r','i','c',':',' ',0};
688 static const WCHAR wszDomPasswdPrefix
[] = {'D','o','m','P','a','s','s','w','d',':',' ',0};
690 LPCWSTR prefix
= NULL
;
693 len
= strlenW(target_name
);
694 if (type
== CRED_TYPE_GENERIC
)
696 prefix
= wszGenericPrefix
;
697 len
+= sizeof(wszGenericPrefix
)/sizeof(wszGenericPrefix
[0]);
701 prefix
= wszDomPasswdPrefix
;
702 len
+= sizeof(wszDomPasswdPrefix
)/sizeof(wszDomPasswdPrefix
[0]);
705 key_name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
706 if (!key_name
) return NULL
;
708 strcpyW(key_name
, prefix
);
709 strcatW(key_name
, target_name
);
711 for (p
= key_name
; *p
; p
++)
712 if (*p
== '\\') *p
= '_';
717 static BOOL
credential_matches_filter(HKEY hkeyCred
, LPCWSTR filter
)
725 if (!filter
) return TRUE
;
727 ret
= RegQueryValueExW(hkeyCred
, NULL
, 0, &type
, NULL
, &count
);
728 if (ret
!= ERROR_SUCCESS
)
730 else if (type
!= REG_SZ
)
733 target_name
= HeapAlloc(GetProcessHeap(), 0, count
);
736 ret
= RegQueryValueExW(hkeyCred
, NULL
, 0, &type
, (LPVOID
)target_name
, &count
);
737 if (ret
!= ERROR_SUCCESS
|| type
!= REG_SZ
)
739 HeapFree(GetProcessHeap(), 0, target_name
);
743 TRACE("comparing filter %s to target name %s\n", debugstr_w(filter
),
744 debugstr_w(target_name
));
746 p
= strchrW(filter
, '*');
747 ret
= CompareStringW(GetThreadLocale(), 0, filter
,
748 (p
&& !p
[1] ? p
- filter
: -1), target_name
,
749 (p
&& !p
[1] ? p
- filter
: -1)) == CSTR_EQUAL
;
751 HeapFree(GetProcessHeap(), 0, target_name
);
755 static DWORD
registry_enumerate_credentials(HKEY hkeyMgr
, LPCWSTR filter
,
757 DWORD target_name_len
, BYTE key_data
[KEY_SIZE
],
758 PCREDENTIALW
*credentials
, char **buffer
,
759 DWORD
*len
, DWORD
*count
)
766 ret
= RegEnumKeyW(hkeyMgr
, i
, target_name
, target_name_len
+1);
767 if (ret
== ERROR_NO_MORE_ITEMS
)
772 else if (ret
!= ERROR_SUCCESS
)
777 TRACE("target_name = %s\n", debugstr_w(target_name
));
778 ret
= RegOpenKeyExW(hkeyMgr
, target_name
, 0, KEY_QUERY_VALUE
, &hkeyCred
);
779 if (ret
!= ERROR_SUCCESS
)
784 if (!credential_matches_filter(hkeyCred
, filter
))
786 RegCloseKey(hkeyCred
);
791 *len
= sizeof(CREDENTIALW
);
792 credentials
[*count
] = (PCREDENTIALW
)*buffer
;
795 *len
+= sizeof(CREDENTIALW
);
796 ret
= registry_read_credential(hkeyCred
, buffer
? credentials
[*count
] : NULL
,
797 key_data
, buffer
? *buffer
+ sizeof(CREDENTIALW
) : NULL
,
799 RegCloseKey(hkeyCred
);
800 if (ret
!= ERROR_SUCCESS
) break;
801 if (buffer
) *buffer
+= *len
;
808 static DWORD
mac_enumerate_credentials(LPCWSTR filter
, PCREDENTIALW
*credentials
,
809 char *buffer
, DWORD
*len
, DWORD
*count
)
811 SecKeychainSearchRef search
;
812 SecKeychainItemRef item
;
814 Boolean saved_user_interaction_allowed
;
817 SecKeychainGetUserInteractionAllowed(&saved_user_interaction_allowed
);
818 SecKeychainSetUserInteractionAllowed(false);
820 status
= SecKeychainSearchCreateFromAttributes(NULL
, kSecInternetPasswordItemClass
, NULL
, &search
);
823 while (SecKeychainSearchCopyNext(search
, &item
) == noErr
)
825 SecKeychainAttributeInfo info
;
826 SecKeychainAttributeList
*attr_list
;
827 UInt32 info_tags
[] = { kSecServerItemAttr
};
828 info
.count
= sizeof(info_tags
)/sizeof(info_tags
[0]);
829 info
.tag
= info_tags
;
831 status
= SecKeychainItemCopyAttributesAndData(item
, &info
, NULL
, &attr_list
, NULL
, NULL
);
834 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status
);
839 *len
= sizeof(CREDENTIALW
);
840 credentials
[*count
] = (PCREDENTIALW
)buffer
;
843 *len
+= sizeof(CREDENTIALW
);
844 if (attr_list
->count
!= 1 || attr_list
->attr
[0].tag
!= kSecServerItemAttr
) continue;
845 TRACE("server item: %.*s\n", (int)attr_list
->attr
[0].length
, (char *)attr_list
->attr
[0].data
);
846 /* FIXME: filter based on attr_list->attr[0].data */
847 SecKeychainItemFreeAttributesAndData(attr_list
, NULL
);
848 ret
= mac_read_credential_from_item(item
, FALSE
,
849 buffer
? credentials
[*count
] : NULL
,
850 buffer
? buffer
+ sizeof(CREDENTIALW
) : NULL
,
853 if (ret
== ERROR_SUCCESS
)
856 if (buffer
) buffer
+= *len
;
862 ERR("SecKeychainSearchCreateFromAttributes returned status %ld\n", status
);
863 SecKeychainSetUserInteractionAllowed(saved_user_interaction_allowed
);
864 return ERROR_SUCCESS
;
867 static DWORD
mac_delete_credential(LPCWSTR TargetName
)
870 SecKeychainSearchRef search
;
871 status
= SecKeychainSearchCreateFromAttributes(NULL
, kSecInternetPasswordItemClass
, NULL
, &search
);
874 SecKeychainItemRef item
;
875 while (SecKeychainSearchCopyNext(search
, &item
) == noErr
)
877 SecKeychainAttributeInfo info
;
878 SecKeychainAttributeList
*attr_list
;
879 UInt32 info_tags
[] = { kSecServerItemAttr
};
882 info
.count
= sizeof(info_tags
)/sizeof(info_tags
[0]);
883 info
.tag
= info_tags
;
885 status
= SecKeychainItemCopyAttributesAndData(item
, &info
, NULL
, &attr_list
, NULL
, NULL
);
888 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status
);
891 if (attr_list
->count
!= 1 || attr_list
->attr
[0].tag
!= kSecServerItemAttr
)
896 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[0].data
, attr_list
->attr
[0].length
, NULL
, 0);
897 target_name
= HeapAlloc(GetProcessHeap(), 0, (str_len
+ 1) * sizeof(WCHAR
));
898 MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[0].data
, attr_list
->attr
[0].length
, target_name
, str_len
);
900 target_name
[str_len
] = '\0';
901 if (strcmpiW(TargetName
, target_name
))
904 HeapFree(GetProcessHeap(), 0, target_name
);
907 HeapFree(GetProcessHeap(), 0, target_name
);
908 SecKeychainItemFreeAttributesAndData(attr_list
, NULL
);
909 SecKeychainItemDelete(item
);
913 return ERROR_SUCCESS
;
917 return ERROR_NOT_FOUND
;
921 static void convert_PCREDENTIALW_to_PCREDENTIALA(const CREDENTIALW
*CredentialW
, PCREDENTIALA CredentialA
, DWORD
*len
)
923 char *buffer
= (char *)CredentialA
+ sizeof(CREDENTIALA
);
926 *len
+= sizeof(CREDENTIALA
);
929 if (CredentialW
->TargetName
) *len
+= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->TargetName
, -1, NULL
, 0, NULL
, NULL
);
930 if (CredentialW
->Comment
) *len
+= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->Comment
, -1, NULL
, 0, NULL
, NULL
);
931 *len
+= CredentialW
->CredentialBlobSize
;
932 if (CredentialW
->TargetAlias
) *len
+= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->TargetAlias
, -1, NULL
, 0, NULL
, NULL
);
933 if (CredentialW
->UserName
) *len
+= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->UserName
, -1, NULL
, 0, NULL
, NULL
);
938 CredentialA
->Flags
= CredentialW
->Flags
;
939 CredentialA
->Type
= CredentialW
->Type
;
940 if (CredentialW
->TargetName
)
942 CredentialA
->TargetName
= buffer
;
943 string_len
= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->TargetName
, -1, CredentialA
->TargetName
, -1, NULL
, NULL
);
944 buffer
+= string_len
;
948 CredentialA
->TargetName
= NULL
;
949 if (CredentialW
->Comment
)
951 CredentialA
->Comment
= buffer
;
952 string_len
= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->Comment
, -1, CredentialA
->Comment
, -1, NULL
, NULL
);
953 buffer
+= string_len
;
957 CredentialA
->Comment
= NULL
;
958 CredentialA
->LastWritten
= CredentialW
->LastWritten
;
959 CredentialA
->CredentialBlobSize
= CredentialW
->CredentialBlobSize
;
960 if (CredentialW
->CredentialBlobSize
)
962 CredentialA
->CredentialBlob
=(LPBYTE
)buffer
;
963 memcpy(CredentialA
->CredentialBlob
, CredentialW
->CredentialBlob
,
964 CredentialW
->CredentialBlobSize
);
965 buffer
+= CredentialW
->CredentialBlobSize
;
966 *len
+= CredentialW
->CredentialBlobSize
;
969 CredentialA
->CredentialBlob
= NULL
;
970 CredentialA
->Persist
= CredentialW
->Persist
;
971 CredentialA
->AttributeCount
= 0;
972 CredentialA
->Attributes
= NULL
; /* FIXME */
973 if (CredentialW
->TargetAlias
)
975 CredentialA
->TargetAlias
= buffer
;
976 string_len
= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->TargetAlias
, -1, CredentialA
->TargetAlias
, -1, NULL
, NULL
);
977 buffer
+= string_len
;
981 CredentialA
->TargetAlias
= NULL
;
982 if (CredentialW
->UserName
)
984 CredentialA
->UserName
= buffer
;
985 string_len
= WideCharToMultiByte(CP_ACP
, 0, CredentialW
->UserName
, -1, CredentialA
->UserName
, -1, NULL
, NULL
);
986 buffer
+= string_len
;
990 CredentialA
->UserName
= NULL
;
993 static void convert_PCREDENTIALA_to_PCREDENTIALW(const CREDENTIALA
*CredentialA
, PCREDENTIALW CredentialW
, DWORD
*len
)
995 char *buffer
= (char *)CredentialW
+ sizeof(CREDENTIALW
);
998 *len
+= sizeof(CREDENTIALW
);
1001 if (CredentialA
->TargetName
) *len
+= sizeof(WCHAR
) * MultiByteToWideChar(CP_ACP
, 0, CredentialA
->TargetName
, -1, NULL
, 0);
1002 if (CredentialA
->Comment
) *len
+= sizeof(WCHAR
) * MultiByteToWideChar(CP_ACP
, 0, CredentialA
->Comment
, -1, NULL
, 0);
1003 *len
+= CredentialA
->CredentialBlobSize
;
1004 if (CredentialA
->TargetAlias
) *len
+= sizeof(WCHAR
) * MultiByteToWideChar(CP_ACP
, 0, CredentialA
->TargetAlias
, -1, NULL
, 0);
1005 if (CredentialA
->UserName
) *len
+= sizeof(WCHAR
) * MultiByteToWideChar(CP_ACP
, 0, CredentialA
->UserName
, -1, NULL
, 0);
1010 CredentialW
->Flags
= CredentialA
->Flags
;
1011 CredentialW
->Type
= CredentialA
->Type
;
1012 if (CredentialA
->TargetName
)
1014 CredentialW
->TargetName
= (LPWSTR
)buffer
;
1015 string_len
= MultiByteToWideChar(CP_ACP
, 0, CredentialA
->TargetName
, -1, CredentialW
->TargetName
, -1);
1016 buffer
+= sizeof(WCHAR
) * string_len
;
1017 *len
+= sizeof(WCHAR
) * string_len
;
1020 CredentialW
->TargetName
= NULL
;
1021 if (CredentialA
->Comment
)
1023 CredentialW
->Comment
= (LPWSTR
)buffer
;
1024 string_len
= MultiByteToWideChar(CP_ACP
, 0, CredentialA
->Comment
, -1, CredentialW
->Comment
, -1);
1025 buffer
+= sizeof(WCHAR
) * string_len
;
1026 *len
+= sizeof(WCHAR
) * string_len
;
1029 CredentialW
->Comment
= NULL
;
1030 CredentialW
->LastWritten
= CredentialA
->LastWritten
;
1031 CredentialW
->CredentialBlobSize
= CredentialA
->CredentialBlobSize
;
1032 if (CredentialA
->CredentialBlobSize
)
1034 CredentialW
->CredentialBlob
=(LPBYTE
)buffer
;
1035 memcpy(CredentialW
->CredentialBlob
, CredentialA
->CredentialBlob
,
1036 CredentialA
->CredentialBlobSize
);
1037 buffer
+= CredentialA
->CredentialBlobSize
;
1038 *len
+= CredentialA
->CredentialBlobSize
;
1041 CredentialW
->CredentialBlob
= NULL
;
1042 CredentialW
->Persist
= CredentialA
->Persist
;
1043 CredentialW
->AttributeCount
= 0;
1044 CredentialW
->Attributes
= NULL
; /* FIXME */
1045 if (CredentialA
->TargetAlias
)
1047 CredentialW
->TargetAlias
= (LPWSTR
)buffer
;
1048 string_len
= MultiByteToWideChar(CP_ACP
, 0, CredentialA
->TargetAlias
, -1, CredentialW
->TargetAlias
, -1);
1049 buffer
+= sizeof(WCHAR
) * string_len
;
1050 *len
+= sizeof(WCHAR
) * string_len
;
1053 CredentialW
->TargetAlias
= NULL
;
1054 if (CredentialA
->UserName
)
1056 CredentialW
->UserName
= (LPWSTR
)buffer
;
1057 string_len
= MultiByteToWideChar(CP_ACP
, 0, CredentialA
->UserName
, -1, CredentialW
->UserName
, -1);
1058 buffer
+= sizeof(WCHAR
) * string_len
;
1059 *len
+= sizeof(WCHAR
) * string_len
;
1062 CredentialW
->UserName
= NULL
;
1065 /******************************************************************************
1066 * CredDeleteA [ADVAPI32.@]
1068 BOOL WINAPI
CredDeleteA(LPCSTR TargetName
, DWORD Type
, DWORD Flags
)
1074 TRACE("(%s, %d, 0x%x)\n", debugstr_a(TargetName
), Type
, Flags
);
1078 SetLastError(ERROR_INVALID_PARAMETER
);
1082 len
= MultiByteToWideChar(CP_ACP
, 0, TargetName
, -1, NULL
, 0);
1083 TargetNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1086 SetLastError(ERROR_OUTOFMEMORY
);
1089 MultiByteToWideChar(CP_ACP
, 0, TargetName
, -1, TargetNameW
, len
);
1091 ret
= CredDeleteW(TargetNameW
, Type
, Flags
);
1093 HeapFree(GetProcessHeap(), 0, TargetNameW
);
1098 /******************************************************************************
1099 * CredDeleteW [ADVAPI32.@]
1101 BOOL WINAPI
CredDeleteW(LPCWSTR TargetName
, DWORD Type
, DWORD Flags
)
1107 TRACE("(%s, %d, 0x%x)\n", debugstr_w(TargetName
), Type
, Flags
);
1111 SetLastError(ERROR_INVALID_PARAMETER
);
1115 if (Type
!= CRED_TYPE_GENERIC
&& Type
!= CRED_TYPE_DOMAIN_PASSWORD
)
1117 FIXME("unhandled type %d\n", Type
);
1118 SetLastError(ERROR_INVALID_PARAMETER
);
1124 FIXME("unhandled flags 0x%x\n", Flags
);
1125 SetLastError(ERROR_INVALID_FLAGS
);
1130 if (Type
== CRED_TYPE_DOMAIN_PASSWORD
)
1132 ret
= mac_delete_credential(TargetName
);
1133 if (ret
== ERROR_SUCCESS
)
1138 ret
= open_cred_mgr_key(&hkeyMgr
, TRUE
);
1139 if (ret
!= ERROR_SUCCESS
)
1141 WARN("couldn't open/create manager key, error %d\n", ret
);
1142 SetLastError(ERROR_NO_SUCH_LOGON_SESSION
);
1146 key_name
= get_key_name_for_target(TargetName
, Type
);
1147 ret
= RegDeleteKeyW(hkeyMgr
, key_name
);
1148 HeapFree(GetProcessHeap(), 0, key_name
);
1149 RegCloseKey(hkeyMgr
);
1150 if (ret
!= ERROR_SUCCESS
)
1152 SetLastError(ERROR_NOT_FOUND
);
1159 /******************************************************************************
1160 * CredEnumerateA [ADVAPI32.@]
1162 BOOL WINAPI
CredEnumerateA(LPCSTR Filter
, DWORD Flags
, DWORD
*Count
,
1163 PCREDENTIALA
**Credentials
)
1166 PCREDENTIALW
*CredentialsW
;
1171 TRACE("(%s, 0x%x, %p, %p)\n", debugstr_a(Filter
), Flags
, Count
, Credentials
);
1175 len
= MultiByteToWideChar(CP_ACP
, 0, Filter
, -1, NULL
, 0);
1176 FilterW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1179 SetLastError(ERROR_OUTOFMEMORY
);
1182 MultiByteToWideChar(CP_ACP
, 0, Filter
, -1, FilterW
, len
);
1187 if (!CredEnumerateW(FilterW
, Flags
, Count
, &CredentialsW
))
1189 HeapFree(GetProcessHeap(), 0, FilterW
);
1192 HeapFree(GetProcessHeap(), 0, FilterW
);
1194 len
= *Count
* sizeof(PCREDENTIALA
);
1195 for (i
= 0; i
< *Count
; i
++)
1196 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW
[i
], NULL
, &len
);
1198 *Credentials
= HeapAlloc(GetProcessHeap(), 0, len
);
1201 CredFree(CredentialsW
);
1202 SetLastError(ERROR_OUTOFMEMORY
);
1206 buffer
= (char *)&(*Credentials
)[*Count
];
1207 for (i
= 0; i
< *Count
; i
++)
1210 (*Credentials
)[i
] = (PCREDENTIALA
)buffer
;
1211 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW
[i
], (*Credentials
)[i
], &len
);
1215 CredFree(CredentialsW
);
1220 /******************************************************************************
1221 * CredEnumerateW [ADVAPI32.@]
1223 BOOL WINAPI
CredEnumerateW(LPCWSTR Filter
, DWORD Flags
, DWORD
*Count
,
1224 PCREDENTIALW
**Credentials
)
1229 DWORD target_name_len
;
1232 BYTE key_data
[KEY_SIZE
];
1234 TRACE("(%s, 0x%x, %p, %p)\n", debugstr_w(Filter
), Flags
, Count
, Credentials
);
1238 SetLastError(ERROR_INVALID_FLAGS
);
1242 ret
= open_cred_mgr_key(&hkeyMgr
, FALSE
);
1243 if (ret
!= ERROR_SUCCESS
)
1245 WARN("couldn't open/create manager key, error %d\n", ret
);
1246 SetLastError(ERROR_NO_SUCH_LOGON_SESSION
);
1250 ret
= get_cred_mgr_encryption_key(hkeyMgr
, key_data
);
1251 if (ret
!= ERROR_SUCCESS
)
1253 RegCloseKey(hkeyMgr
);
1258 ret
= RegQueryInfoKeyW(hkeyMgr
, NULL
, NULL
, NULL
, NULL
, &target_name_len
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1259 if (ret
!= ERROR_SUCCESS
)
1261 RegCloseKey(hkeyMgr
);
1266 target_name
= HeapAlloc(GetProcessHeap(), 0, (target_name_len
+1)*sizeof(WCHAR
));
1269 RegCloseKey(hkeyMgr
);
1270 SetLastError(ERROR_OUTOFMEMORY
);
1276 ret
= registry_enumerate_credentials(hkeyMgr
, Filter
, target_name
, target_name_len
,
1277 key_data
, NULL
, NULL
, &len
, Count
);
1279 if (ret
== ERROR_SUCCESS
)
1280 ret
= mac_enumerate_credentials(Filter
, NULL
, NULL
, &len
, Count
);
1282 if (ret
== ERROR_SUCCESS
&& *Count
== 0)
1283 ret
= ERROR_NOT_FOUND
;
1284 if (ret
!= ERROR_SUCCESS
)
1286 HeapFree(GetProcessHeap(), 0, target_name
);
1287 RegCloseKey(hkeyMgr
);
1291 len
+= *Count
* sizeof(PCREDENTIALW
);
1293 if (ret
== ERROR_SUCCESS
)
1295 buffer
= HeapAlloc(GetProcessHeap(), 0, len
);
1296 *Credentials
= (PCREDENTIALW
*)buffer
;
1299 buffer
+= *Count
* sizeof(PCREDENTIALW
);
1301 ret
= registry_enumerate_credentials(hkeyMgr
, Filter
, target_name
,
1302 target_name_len
, key_data
,
1303 *Credentials
, &buffer
, &len
,
1306 if (ret
== ERROR_SUCCESS
)
1307 ret
= mac_enumerate_credentials(Filter
, *Credentials
,
1308 buffer
, &len
, Count
);
1312 ret
= ERROR_OUTOFMEMORY
;
1315 HeapFree(GetProcessHeap(), 0, target_name
);
1316 RegCloseKey(hkeyMgr
);
1318 if (ret
!= ERROR_SUCCESS
)
1326 /******************************************************************************
1327 * CredFree [ADVAPI32.@]
1329 VOID WINAPI
CredFree(PVOID Buffer
)
1331 HeapFree(GetProcessHeap(), 0, Buffer
);
1334 /******************************************************************************
1335 * CredReadA [ADVAPI32.@]
1337 BOOL WINAPI
CredReadA(LPCSTR TargetName
, DWORD Type
, DWORD Flags
, PCREDENTIALA
*Credential
)
1340 PCREDENTIALW CredentialW
;
1343 TRACE("(%s, %d, 0x%x, %p)\n", debugstr_a(TargetName
), Type
, Flags
, Credential
);
1347 SetLastError(ERROR_INVALID_PARAMETER
);
1351 len
= MultiByteToWideChar(CP_ACP
, 0, TargetName
, -1, NULL
, 0);
1352 TargetNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1355 SetLastError(ERROR_OUTOFMEMORY
);
1358 MultiByteToWideChar(CP_ACP
, 0, TargetName
, -1, TargetNameW
, len
);
1360 if (!CredReadW(TargetNameW
, Type
, Flags
, &CredentialW
))
1362 HeapFree(GetProcessHeap(), 0, TargetNameW
);
1365 HeapFree(GetProcessHeap(), 0, TargetNameW
);
1368 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialW
, NULL
, &len
);
1369 *Credential
= HeapAlloc(GetProcessHeap(), 0, len
);
1372 SetLastError(ERROR_OUTOFMEMORY
);
1376 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialW
, *Credential
, &len
);
1378 CredFree(CredentialW
);
1383 /******************************************************************************
1384 * CredReadW [ADVAPI32.@]
1386 BOOL WINAPI
CredReadW(LPCWSTR TargetName
, DWORD Type
, DWORD Flags
, PCREDENTIALW
*Credential
)
1393 BYTE key_data
[KEY_SIZE
];
1395 TRACE("(%s, %d, 0x%x, %p)\n", debugstr_w(TargetName
), Type
, Flags
, Credential
);
1399 SetLastError(ERROR_INVALID_PARAMETER
);
1403 if (Type
!= CRED_TYPE_GENERIC
&& Type
!= CRED_TYPE_DOMAIN_PASSWORD
)
1405 FIXME("unhandled type %d\n", Type
);
1406 SetLastError(ERROR_INVALID_PARAMETER
);
1412 FIXME("unhandled flags 0x%x\n", Flags
);
1413 SetLastError(ERROR_INVALID_FLAGS
);
1418 if (Type
== CRED_TYPE_DOMAIN_PASSWORD
)
1421 SecKeychainSearchRef search
;
1422 status
= SecKeychainSearchCreateFromAttributes(NULL
, kSecInternetPasswordItemClass
, NULL
, &search
);
1423 if (status
== noErr
)
1425 SecKeychainItemRef item
;
1426 while (SecKeychainSearchCopyNext(search
, &item
) == noErr
)
1428 SecKeychainAttributeInfo info
;
1429 SecKeychainAttributeList
*attr_list
;
1430 UInt32 info_tags
[] = { kSecServerItemAttr
};
1433 info
.count
= sizeof(info_tags
)/sizeof(info_tags
[0]);
1434 info
.tag
= info_tags
;
1436 status
= SecKeychainItemCopyAttributesAndData(item
, &info
, NULL
, &attr_list
, NULL
, NULL
);
1437 len
= sizeof(**Credential
);
1438 if (status
!= noErr
)
1440 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status
);
1443 if (attr_list
->count
!= 1 || attr_list
->attr
[0].tag
!= kSecServerItemAttr
)
1448 str_len
= MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[0].data
, attr_list
->attr
[0].length
, NULL
, 0);
1449 target_name
= HeapAlloc(GetProcessHeap(), 0, (str_len
+ 1) * sizeof(WCHAR
));
1450 MultiByteToWideChar(CP_UTF8
, 0, attr_list
->attr
[0].data
, attr_list
->attr
[0].length
, target_name
, str_len
);
1452 target_name
[str_len
] = '\0';
1453 if (strcmpiW(TargetName
, target_name
))
1456 HeapFree(GetProcessHeap(), 0, target_name
);
1459 HeapFree(GetProcessHeap(), 0, target_name
);
1460 SecKeychainItemFreeAttributesAndData(attr_list
, NULL
);
1461 ret
= mac_read_credential_from_item(item
, TRUE
, NULL
, NULL
, &len
);
1462 if (ret
== ERROR_SUCCESS
)
1464 *Credential
= HeapAlloc(GetProcessHeap(), 0, len
);
1467 len
= sizeof(**Credential
);
1468 ret
= mac_read_credential_from_item(item
, TRUE
, *Credential
,
1469 (char *)(*Credential
+ 1), &len
);
1472 ret
= ERROR_OUTOFMEMORY
;
1475 if (ret
!= ERROR_SUCCESS
)
1489 ret
= open_cred_mgr_key(&hkeyMgr
, FALSE
);
1490 if (ret
!= ERROR_SUCCESS
)
1492 WARN("couldn't open/create manager key, error %d\n", ret
);
1493 SetLastError(ERROR_NO_SUCH_LOGON_SESSION
);
1497 ret
= get_cred_mgr_encryption_key(hkeyMgr
, key_data
);
1498 if (ret
!= ERROR_SUCCESS
)
1500 RegCloseKey(hkeyMgr
);
1505 key_name
= get_key_name_for_target(TargetName
, Type
);
1506 ret
= RegOpenKeyExW(hkeyMgr
, key_name
, 0, KEY_QUERY_VALUE
, &hkeyCred
);
1507 HeapFree(GetProcessHeap(), 0, key_name
);
1508 if (ret
!= ERROR_SUCCESS
)
1510 TRACE("credentials for target name %s not found\n", debugstr_w(TargetName
));
1511 SetLastError(ERROR_NOT_FOUND
);
1515 len
= sizeof(**Credential
);
1516 ret
= registry_read_credential(hkeyCred
, NULL
, key_data
, NULL
, &len
);
1517 if (ret
== ERROR_SUCCESS
)
1519 *Credential
= HeapAlloc(GetProcessHeap(), 0, len
);
1522 len
= sizeof(**Credential
);
1523 ret
= registry_read_credential(hkeyCred
, *Credential
, key_data
,
1524 (char *)(*Credential
+ 1), &len
);
1527 ret
= ERROR_OUTOFMEMORY
;
1530 RegCloseKey(hkeyCred
);
1531 RegCloseKey(hkeyMgr
);
1533 if (ret
!= ERROR_SUCCESS
)
1541 /******************************************************************************
1542 * CredReadDomainCredentialsA [ADVAPI32.@]
1544 BOOL WINAPI
CredReadDomainCredentialsA(PCREDENTIAL_TARGET_INFORMATIONA TargetInformation
,
1545 DWORD Flags
, DWORD
*Size
, PCREDENTIALA
**Credentials
)
1547 PCREDENTIAL_TARGET_INFORMATIONW TargetInformationW
;
1549 WCHAR
*buffer
, *end
;
1551 PCREDENTIALW
* CredentialsW
;
1553 TRACE("(%p, 0x%x, %p, %p)\n", TargetInformation
, Flags
, Size
, Credentials
);
1555 /* follow Windows behavior - do not test for NULL, initialize early */
1557 *Credentials
= NULL
;
1559 if (!TargetInformation
)
1561 SetLastError(ERROR_INVALID_PARAMETER
);
1565 len
= sizeof(*TargetInformationW
);
1566 if (TargetInformation
->TargetName
)
1567 len
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->TargetName
, -1, NULL
, 0) * sizeof(WCHAR
);
1568 if (TargetInformation
->NetbiosServerName
)
1569 len
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->NetbiosServerName
, -1, NULL
, 0) * sizeof(WCHAR
);
1570 if (TargetInformation
->DnsServerName
)
1571 len
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->DnsServerName
, -1, NULL
, 0) * sizeof(WCHAR
);
1572 if (TargetInformation
->NetbiosDomainName
)
1573 len
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->NetbiosDomainName
, -1, NULL
, 0) * sizeof(WCHAR
);
1574 if (TargetInformation
->DnsDomainName
)
1575 len
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->DnsDomainName
, -1, NULL
, 0) * sizeof(WCHAR
);
1576 if (TargetInformation
->DnsTreeName
)
1577 len
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->DnsTreeName
, -1, NULL
, 0) * sizeof(WCHAR
);
1578 if (TargetInformation
->PackageName
)
1579 len
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->PackageName
, -1, NULL
, 0) * sizeof(WCHAR
);
1581 TargetInformationW
= HeapAlloc(GetProcessHeap(), 0, len
);
1582 if (!TargetInformationW
)
1584 SetLastError(ERROR_OUTOFMEMORY
);
1587 buffer
= (WCHAR
*)(TargetInformationW
+ 1);
1588 end
= (WCHAR
*)((char *)TargetInformationW
+ len
);
1590 if (TargetInformation
->TargetName
)
1592 TargetInformationW
->TargetName
= buffer
;
1593 buffer
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->TargetName
, -1,
1594 TargetInformationW
->TargetName
, end
- buffer
);
1596 TargetInformationW
->TargetName
= NULL
;
1598 if (TargetInformation
->NetbiosServerName
)
1600 TargetInformationW
->NetbiosServerName
= buffer
;
1601 buffer
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->NetbiosServerName
, -1,
1602 TargetInformationW
->NetbiosServerName
, end
- buffer
);
1604 TargetInformationW
->NetbiosServerName
= NULL
;
1606 if (TargetInformation
->DnsServerName
)
1608 TargetInformationW
->DnsServerName
= buffer
;
1609 buffer
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->DnsServerName
, -1,
1610 TargetInformationW
->DnsServerName
, end
- buffer
);
1612 TargetInformationW
->DnsServerName
= NULL
;
1614 if (TargetInformation
->NetbiosDomainName
)
1616 TargetInformationW
->NetbiosDomainName
= buffer
;
1617 buffer
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->NetbiosDomainName
, -1,
1618 TargetInformationW
->NetbiosDomainName
, end
- buffer
);
1620 TargetInformationW
->NetbiosDomainName
= NULL
;
1622 if (TargetInformation
->DnsDomainName
)
1624 TargetInformationW
->DnsDomainName
= buffer
;
1625 buffer
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->DnsDomainName
, -1,
1626 TargetInformationW
->DnsDomainName
, end
- buffer
);
1628 TargetInformationW
->DnsDomainName
= NULL
;
1630 if (TargetInformation
->DnsTreeName
)
1632 TargetInformationW
->DnsTreeName
= buffer
;
1633 buffer
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->DnsTreeName
, -1,
1634 TargetInformationW
->DnsTreeName
, end
- buffer
);
1636 TargetInformationW
->DnsTreeName
= NULL
;
1638 if (TargetInformation
->PackageName
)
1640 TargetInformationW
->PackageName
= buffer
;
1641 buffer
+= MultiByteToWideChar(CP_ACP
, 0, TargetInformation
->PackageName
, -1,
1642 TargetInformationW
->PackageName
, end
- buffer
);
1644 TargetInformationW
->PackageName
= NULL
;
1646 TargetInformationW
->Flags
= TargetInformation
->Flags
;
1647 TargetInformationW
->CredTypeCount
= TargetInformation
->CredTypeCount
;
1648 TargetInformationW
->CredTypes
= TargetInformation
->CredTypes
;
1650 ret
= CredReadDomainCredentialsW(TargetInformationW
, Flags
, Size
, &CredentialsW
);
1652 HeapFree(GetProcessHeap(), 0, TargetInformationW
);
1658 len
= *Size
* sizeof(PCREDENTIALA
);
1659 for (i
= 0; i
< *Size
; i
++)
1660 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW
[i
], NULL
, &len
);
1662 *Credentials
= HeapAlloc(GetProcessHeap(), 0, len
);
1665 CredFree(CredentialsW
);
1666 SetLastError(ERROR_OUTOFMEMORY
);
1670 buf
= (char *)&(*Credentials
)[*Size
];
1671 for (i
= 0; i
< *Size
; i
++)
1674 (*Credentials
)[i
] = (PCREDENTIALA
)buf
;
1675 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW
[i
], (*Credentials
)[i
], &len
);
1679 CredFree(CredentialsW
);
1684 /******************************************************************************
1685 * CredReadDomainCredentialsW [ADVAPI32.@]
1687 BOOL WINAPI
CredReadDomainCredentialsW(PCREDENTIAL_TARGET_INFORMATIONW TargetInformation
, DWORD Flags
,
1688 DWORD
*Size
, PCREDENTIALW
**Credentials
)
1690 FIXME("(%p, 0x%x, %p, %p) stub\n", TargetInformation
, Flags
, Size
, Credentials
);
1692 /* follow Windows behavior - do not test for NULL, initialize early */
1694 *Credentials
= NULL
;
1695 if (!TargetInformation
)
1697 SetLastError(ERROR_INVALID_PARAMETER
);
1701 SetLastError(ERROR_NOT_FOUND
);
1705 /******************************************************************************
1706 * CredWriteA [ADVAPI32.@]
1708 BOOL WINAPI
CredWriteA(PCREDENTIALA Credential
, DWORD Flags
)
1712 PCREDENTIALW CredentialW
;
1714 TRACE("(%p, 0x%x)\n", Credential
, Flags
);
1716 if (!Credential
|| !Credential
->TargetName
)
1718 SetLastError(ERROR_INVALID_PARAMETER
);
1723 convert_PCREDENTIALA_to_PCREDENTIALW(Credential
, NULL
, &len
);
1724 CredentialW
= HeapAlloc(GetProcessHeap(), 0, len
);
1727 SetLastError(ERROR_OUTOFMEMORY
);
1731 convert_PCREDENTIALA_to_PCREDENTIALW(Credential
, CredentialW
, &len
);
1733 ret
= CredWriteW(CredentialW
, Flags
);
1735 HeapFree(GetProcessHeap(), 0, CredentialW
);
1740 /******************************************************************************
1741 * CredWriteW [ADVAPI32.@]
1743 BOOL WINAPI
CredWriteW(PCREDENTIALW Credential
, DWORD Flags
)
1749 BYTE key_data
[KEY_SIZE
];
1751 TRACE("(%p, 0x%x)\n", Credential
, Flags
);
1753 if (!Credential
|| !Credential
->TargetName
)
1755 SetLastError(ERROR_INVALID_PARAMETER
);
1759 if (Flags
& ~CRED_PRESERVE_CREDENTIAL_BLOB
)
1761 FIXME("unhandled flags 0x%x\n", Flags
);
1762 SetLastError(ERROR_INVALID_FLAGS
);
1766 if (Credential
->Type
!= CRED_TYPE_GENERIC
&& Credential
->Type
!= CRED_TYPE_DOMAIN_PASSWORD
)
1768 FIXME("unhandled type %d\n", Credential
->Type
);
1769 SetLastError(ERROR_INVALID_PARAMETER
);
1773 TRACE("Credential->TargetName = %s\n", debugstr_w(Credential
->TargetName
));
1774 TRACE("Credential->UserName = %s\n", debugstr_w(Credential
->UserName
));
1776 if (Credential
->Type
== CRED_TYPE_DOMAIN_PASSWORD
)
1778 if (!Credential
->UserName
||
1779 (!strchrW(Credential
->UserName
, '\\') && !strchrW(Credential
->UserName
, '@')))
1781 ERR("bad username %s\n", debugstr_w(Credential
->UserName
));
1782 SetLastError(ERROR_BAD_USERNAME
);
1788 if (!Credential
->AttributeCount
&&
1789 Credential
->Type
== CRED_TYPE_DOMAIN_PASSWORD
&&
1790 (Credential
->Persist
== CRED_PERSIST_LOCAL_MACHINE
|| Credential
->Persist
== CRED_PERSIST_ENTERPRISE
))
1792 ret
= mac_write_credential(Credential
, Flags
& CRED_PRESERVE_CREDENTIAL_BLOB
);
1793 if (ret
!= ERROR_SUCCESS
)
1802 ret
= open_cred_mgr_key(&hkeyMgr
, FALSE
);
1803 if (ret
!= ERROR_SUCCESS
)
1805 WARN("couldn't open/create manager key, error %d\n", ret
);
1806 SetLastError(ERROR_NO_SUCH_LOGON_SESSION
);
1810 ret
= get_cred_mgr_encryption_key(hkeyMgr
, key_data
);
1811 if (ret
!= ERROR_SUCCESS
)
1813 RegCloseKey(hkeyMgr
);
1818 key_name
= get_key_name_for_target(Credential
->TargetName
, Credential
->Type
);
1819 ret
= RegCreateKeyExW(hkeyMgr
, key_name
, 0, NULL
,
1820 Credential
->Persist
== CRED_PERSIST_SESSION
? REG_OPTION_VOLATILE
: REG_OPTION_NON_VOLATILE
,
1821 KEY_READ
|KEY_WRITE
, NULL
, &hkeyCred
, NULL
);
1822 HeapFree(GetProcessHeap(), 0, key_name
);
1823 if (ret
!= ERROR_SUCCESS
)
1825 TRACE("credentials for target name %s not found\n",
1826 debugstr_w(Credential
->TargetName
));
1827 SetLastError(ERROR_NOT_FOUND
);
1831 ret
= registry_write_credential(hkeyCred
, Credential
, key_data
,
1832 Flags
& CRED_PRESERVE_CREDENTIAL_BLOB
);
1834 RegCloseKey(hkeyCred
);
1835 RegCloseKey(hkeyMgr
);
1837 if (ret
!= ERROR_SUCCESS
)
1845 /******************************************************************************
1846 * CredGetSessionTypes [ADVAPI32.@]
1848 WINADVAPI BOOL WINAPI
CredGetSessionTypes(DWORD persistCount
, LPDWORD persists
)
1850 TRACE("(%u, %p)\n", persistCount
, persists
);
1852 memset(persists
, CRED_PERSIST_NONE
, persistCount
*sizeof(*persists
));
1853 if (CRED_TYPE_GENERIC
< persistCount
)
1855 persists
[CRED_TYPE_GENERIC
] = CRED_PERSIST_ENTERPRISE
;
1857 if (CRED_TYPE_DOMAIN_PASSWORD
< persistCount
)
1859 persists
[CRED_TYPE_DOMAIN_PASSWORD
] = CRED_PERSIST_ENTERPRISE
;