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 wszTargetNameValue[] = {'T','a','r','g','e','t','N','a','m','e',0};
53 static const WCHAR wszCommentValue[] = {'C','o','m','m','e','n','t',0};
54 static const WCHAR wszLastWrittenValue[] = {'L','a','s','t','W','r','i','t','t','e','n',0};
55 static const WCHAR wszPersistValue[] = {'P','e','r','s','i','s','t',0};
56 static const WCHAR wszTargetAliasValue[] = {'T','a','r','g','e','t','A','l','i','a','s',0};
57 static const WCHAR wszUserNameValue[] = {'U','s','e','r','N','a','m','e',0};
58 static const WCHAR wszPasswordValue[] = {'P','a','s','s','w','o','r','d',0};
60 static DWORD read_credential_blob(HKEY hkey, const BYTE key_data[KEY_SIZE],
61 LPBYTE credential_blob,
62 DWORD *credential_blob_size)
67 *credential_blob_size = 0;
68 ret = RegQueryValueExW(hkey, wszPasswordValue, 0, &type, NULL, credential_blob_size);
69 if (ret != ERROR_SUCCESS)
71 else if (type != REG_BINARY)
72 return ERROR_REGISTRY_CORRUPT;
78 ret = RegQueryValueExW(hkey, wszPasswordValue, 0, &type, (LPVOID)credential_blob,
79 credential_blob_size);
80 if (ret != ERROR_SUCCESS)
82 else if (type != REG_BINARY)
83 return ERROR_REGISTRY_CORRUPT;
85 key.Length = key.MaximumLength = sizeof(key_data);
86 key.Buffer = (unsigned char *)key_data;
88 data.Length = data.MaximumLength = *credential_blob_size;
89 data.Buffer = credential_blob;
90 SystemFunction032(&data, &key);
95 static DWORD registry_read_credential(HKEY hkey, PCREDENTIALW credential,
96 const BYTE key_data[KEY_SIZE],
97 char *buffer, DWORD *len)
103 ret = RegQueryValueExW(hkey, NULL, 0, &type, NULL, &count);
104 if (ret != ERROR_SUCCESS)
106 else if (type != REG_SZ)
107 return ERROR_REGISTRY_CORRUPT;
111 credential->TargetName = (LPWSTR)buffer;
112 ret = RegQueryValueExW(hkey, NULL, 0, &type, (LPVOID)credential->TargetName,
114 if (ret != ERROR_SUCCESS || type != REG_SZ) return ret;
118 ret = RegQueryValueExW(hkey, wszCommentValue, 0, &type, NULL, &count);
119 if (ret != ERROR_FILE_NOT_FOUND && ret != ERROR_SUCCESS)
121 else if (type != REG_SZ)
122 return ERROR_REGISTRY_CORRUPT;
126 credential->Comment = (LPWSTR)buffer;
127 ret = RegQueryValueExW(hkey, wszCommentValue, 0, &type, (LPVOID)credential->Comment,
129 if (ret == ERROR_FILE_NOT_FOUND)
130 credential->Comment = NULL;
131 else if (ret != ERROR_SUCCESS)
133 else if (type != REG_SZ)
134 return ERROR_REGISTRY_CORRUPT;
139 ret = RegQueryValueExW(hkey, wszTargetAliasValue, 0, &type, NULL, &count);
140 if (ret != ERROR_FILE_NOT_FOUND && ret != ERROR_SUCCESS)
142 else if (type != REG_SZ)
143 return ERROR_REGISTRY_CORRUPT;
147 credential->TargetAlias = (LPWSTR)buffer;
148 ret = RegQueryValueExW(hkey, wszTargetAliasValue, 0, &type, (LPVOID)credential->TargetAlias,
150 if (ret == ERROR_FILE_NOT_FOUND)
151 credential->TargetAlias = NULL;
152 else if (ret != ERROR_SUCCESS)
154 else if (type != REG_SZ)
155 return ERROR_REGISTRY_CORRUPT;
160 ret = RegQueryValueExW(hkey, wszUserNameValue, 0, &type, NULL, &count);
161 if (ret != ERROR_FILE_NOT_FOUND && ret != ERROR_SUCCESS)
163 else if (type != REG_SZ)
164 return ERROR_REGISTRY_CORRUPT;
168 credential->UserName = (LPWSTR)buffer;
169 ret = RegQueryValueExW(hkey, wszUserNameValue, 0, &type, (LPVOID)credential->UserName,
171 if (ret == ERROR_FILE_NOT_FOUND)
173 credential->UserName = NULL;
176 else if (ret != ERROR_SUCCESS)
178 else if (type != REG_SZ)
179 return ERROR_REGISTRY_CORRUPT;
184 ret = read_credential_blob(hkey, key_data, NULL, &count);
185 if (ret != ERROR_FILE_NOT_FOUND && ret != ERROR_SUCCESS)
190 credential->CredentialBlob = (LPBYTE)buffer;
191 ret = read_credential_blob(hkey, key_data, credential->CredentialBlob, &count);
192 if (ret == ERROR_FILE_NOT_FOUND)
194 credential->CredentialBlob = NULL;
197 else if (ret != ERROR_SUCCESS)
199 credential->CredentialBlobSize = count;
203 /* FIXME: Attributes */
206 credential->AttributeCount = 0;
207 credential->Attributes = NULL;
210 if (!credential) return ERROR_SUCCESS;
212 count = sizeof(credential->Flags);
213 ret = RegQueryValueExW(hkey, wszFlagsValue, NULL, &type, (LPVOID)&credential->Flags,
215 if (ret != ERROR_SUCCESS)
217 else if (type != REG_DWORD)
218 return ERROR_REGISTRY_CORRUPT;
219 count = sizeof(credential->Type);
220 ret = RegQueryValueExW(hkey, wszTypeValue, NULL, &type, (LPVOID)&credential->Type,
222 if (ret != ERROR_SUCCESS)
224 else if (type != REG_DWORD)
225 return ERROR_REGISTRY_CORRUPT;
227 count = sizeof(credential->LastWritten);
228 ret = RegQueryValueExW(hkey, wszLastWrittenValue, NULL, &type, (LPVOID)&credential->LastWritten,
230 if (ret != ERROR_SUCCESS)
232 else if (type != REG_BINARY)
233 return ERROR_REGISTRY_CORRUPT;
234 count = sizeof(credential->Persist);
235 ret = RegQueryValueExW(hkey, wszPersistValue, NULL, &type, (LPVOID)&credential->Persist,
237 if (ret == ERROR_SUCCESS && type != REG_DWORD)
238 return ERROR_REGISTRY_CORRUPT;
243 static DWORD mac_read_credential_from_item(SecKeychainItemRef item, BOOL require_password,
244 PCREDENTIALW credential, char *buffer,
249 UInt32 cred_blob_len;
251 LPWSTR domain = NULL;
253 SecKeychainAttributeInfo info;
254 SecKeychainAttributeList *attr_list;
255 UInt32 info_tags[] = { kSecServerItemAttr, kSecSecurityDomainItemAttr, kSecAccountItemAttr,
256 kSecCommentItemAttr, kSecCreationDateItemAttr };
257 info.count = sizeof(info_tags)/sizeof(info_tags[0]);
258 info.tag = info_tags;
260 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, &cred_blob_len, &cred_blob);
261 if (status == errSecAuthFailed && !require_password)
265 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, &cred_blob_len, NULL);
269 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status);
270 return ERROR_NOT_FOUND;
274 credential->Flags = 0;
275 credential->Type = CRED_TYPE_DOMAIN_PASSWORD;
276 credential->TargetName = NULL;
277 credential->Comment = NULL;
278 memset(&credential->LastWritten, 0, sizeof(credential->LastWritten));
279 credential->CredentialBlobSize = 0;
280 credential->CredentialBlob = NULL;
281 credential->Persist = CRED_PERSIST_LOCAL_MACHINE;
282 credential->AttributeCount = 0;
283 credential->Attributes = NULL;
284 credential->TargetAlias = NULL;
285 credential->UserName = NULL;
287 for (i = 0; i < attr_list->count; i++)
289 switch (attr_list->attr[i].tag)
291 case kSecServerItemAttr:
292 TRACE("kSecServerItemAttr: %.*s\n", (int)attr_list->attr[i].length,
293 (char *)attr_list->attr[i].data);
294 if (!attr_list->attr[i].data) continue;
298 credential->TargetName = (LPWSTR)buffer;
299 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
300 attr_list->attr[i].length, (LPWSTR)buffer, 0xffff);
301 credential->TargetName[str_len] = '\0';
302 buffer += (str_len + 1) * sizeof(WCHAR);
303 *len += (str_len + 1) * sizeof(WCHAR);
308 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
309 attr_list->attr[i].length, NULL, 0);
310 *len += (str_len + 1) * sizeof(WCHAR);
313 case kSecAccountItemAttr:
316 TRACE("kSecAccountItemAttr: %.*s\n", (int)attr_list->attr[i].length,
317 (char *)attr_list->attr[i].data);
318 if (!attr_list->attr[i].data) continue;
319 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
320 attr_list->attr[i].length, NULL, 0);
321 user = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
322 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
323 attr_list->attr[i].length, user, str_len);
324 user[str_len] = '\0';
327 case kSecCommentItemAttr:
328 TRACE("kSecCommentItemAttr: %.*s\n", (int)attr_list->attr[i].length,
329 (char *)attr_list->attr[i].data);
330 if (!attr_list->attr[i].data) continue;
334 credential->Comment = (LPWSTR)buffer;
335 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
336 attr_list->attr[i].length, (LPWSTR)buffer, 0xffff);
337 credential->Comment[str_len] = '\0';
338 buffer += (str_len + 1) * sizeof(WCHAR);
339 *len += (str_len + 1) * sizeof(WCHAR);
344 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
345 attr_list->attr[i].length, NULL, 0);
346 *len += (str_len + 1) * sizeof(WCHAR);
349 case kSecSecurityDomainItemAttr:
352 TRACE("kSecSecurityDomainItemAttr: %.*s\n", (int)attr_list->attr[i].length,
353 (char *)attr_list->attr[i].data);
354 if (!attr_list->attr[i].data) continue;
355 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
356 attr_list->attr[i].length, NULL, 0);
357 domain = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
358 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
359 attr_list->attr[i].length, domain, str_len);
360 domain[str_len] = '\0';
363 case kSecCreationDateItemAttr:
364 TRACE("kSecCreationDateItemAttr: %.*s\n", (int)attr_list->attr[i].length,
365 (char *)attr_list->attr[i].data);
368 LARGE_INTEGER win_time;
371 memset(&tm, 0, sizeof(tm));
372 strptime(attr_list->attr[i].data, "%Y%m%d%H%M%SZ", &tm);
374 RtlSecondsSince1970ToTime(time, &win_time);
375 credential->LastWritten.dwLowDateTime = win_time.u.LowPart;
376 credential->LastWritten.dwHighDateTime = win_time.u.HighPart;
386 credential->UserName = (LPWSTR)buffer;
389 str_len = strlenW(domain);
390 *len += (str_len + 1) * sizeof(WCHAR);
393 memcpy(credential->UserName, domain, str_len * sizeof(WCHAR));
394 /* FIXME: figure out when to use an '@' */
395 credential->UserName[str_len] = '\\';
396 buffer += (str_len + 1) * sizeof(WCHAR);
399 str_len = strlenW(user);
400 *len += (str_len + 1) * sizeof(WCHAR);
403 memcpy(buffer, user, (str_len + 1) * sizeof(WCHAR));
404 buffer += (str_len + 1) * sizeof(WCHAR);
405 TRACE("UserName = %s\n", debugstr_w(credential->UserName));
408 HeapFree(GetProcessHeap(), 0, user);
409 HeapFree(GetProcessHeap(), 0, domain);
416 credential->CredentialBlob = (BYTE *)buffer;
417 str_len = MultiByteToWideChar(CP_UTF8, 0, cred_blob, cred_blob_len,
418 (LPWSTR)buffer, 0xffff);
419 credential->CredentialBlobSize = str_len * sizeof(WCHAR);
420 buffer += str_len * sizeof(WCHAR);
421 *len += str_len * sizeof(WCHAR);
426 str_len = MultiByteToWideChar(CP_UTF8, 0, cred_blob, cred_blob_len,
428 *len += str_len * sizeof(WCHAR);
431 SecKeychainItemFreeAttributesAndData(attr_list, cred_blob);
432 return ERROR_SUCCESS;
436 static DWORD write_credential_blob(HKEY hkey, LPCWSTR target_name, DWORD type,
437 const BYTE key_data[KEY_SIZE],
438 const BYTE *credential_blob, DWORD credential_blob_size)
440 LPBYTE encrypted_credential_blob;
445 key.Length = key.MaximumLength = sizeof(key_data);
446 key.Buffer = (unsigned char *)key_data;
448 encrypted_credential_blob = HeapAlloc(GetProcessHeap(), 0, credential_blob_size);
449 if (!encrypted_credential_blob) return ERROR_OUTOFMEMORY;
451 memcpy(encrypted_credential_blob, credential_blob, credential_blob_size);
452 data.Length = data.MaximumLength = credential_blob_size;
453 data.Buffer = encrypted_credential_blob;
454 SystemFunction032(&data, &key);
456 ret = RegSetValueExW(hkey, wszPasswordValue, 0, REG_BINARY, (LPVOID)encrypted_credential_blob, credential_blob_size);
457 HeapFree(GetProcessHeap(), 0, encrypted_credential_blob);
462 static DWORD registry_write_credential(HKEY hkey, const CREDENTIALW *credential,
463 const BYTE key_data[KEY_SIZE], BOOL preserve_blob)
466 FILETIME LastWritten;
468 GetSystemTimeAsFileTime(&LastWritten);
470 ret = RegSetValueExW(hkey, wszFlagsValue, 0, REG_DWORD, (LPVOID)&credential->Flags,
471 sizeof(credential->Flags));
472 if (ret != ERROR_SUCCESS) return ret;
473 ret = RegSetValueExW(hkey, wszTypeValue, 0, REG_DWORD, (LPVOID)&credential->Type,
474 sizeof(credential->Type));
475 if (ret != ERROR_SUCCESS) return ret;
476 ret = RegSetValueExW(hkey, NULL, 0, REG_SZ, (LPVOID)credential->TargetName,
477 sizeof(WCHAR)*(strlenW(credential->TargetName)+1));
478 if (ret != ERROR_SUCCESS) return ret;
479 if (credential->Comment)
481 ret = RegSetValueExW(hkey, wszCommentValue, 0, REG_SZ, (LPVOID)credential->Comment,
482 sizeof(WCHAR)*(strlenW(credential->Comment)+1));
483 if (ret != ERROR_SUCCESS) return ret;
485 ret = RegSetValueExW(hkey, wszLastWrittenValue, 0, REG_BINARY, (LPVOID)&LastWritten,
486 sizeof(LastWritten));
487 if (ret != ERROR_SUCCESS) return ret;
488 ret = RegSetValueExW(hkey, wszPersistValue, 0, REG_DWORD, (LPVOID)&credential->Persist,
489 sizeof(credential->Persist));
490 if (ret != ERROR_SUCCESS) return ret;
491 /* FIXME: Attributes */
492 if (credential->TargetAlias)
494 ret = RegSetValueExW(hkey, wszTargetAliasValue, 0, REG_SZ, (LPVOID)credential->TargetAlias,
495 sizeof(WCHAR)*(strlenW(credential->TargetAlias)+1));
496 if (ret != ERROR_SUCCESS) return ret;
498 if (credential->UserName)
500 ret = RegSetValueExW(hkey, wszUserNameValue, 0, REG_SZ, (LPVOID)credential->UserName,
501 sizeof(WCHAR)*(strlenW(credential->UserName)+1));
502 if (ret != ERROR_SUCCESS) return ret;
506 ret = write_credential_blob(hkey, credential->TargetName, credential->Type,
507 key_data, credential->CredentialBlob,
508 credential->CredentialBlobSize);
514 static DWORD mac_write_credential(const CREDENTIALW *credential, BOOL preserve_blob)
517 SecKeychainItemRef keychain_item;
523 UInt32 domainlen = 0;
527 SecKeychainAttribute attrs[1];
528 SecKeychainAttributeList attr_list;
530 if (credential->Flags)
531 FIXME("Flags 0x%x not written\n", credential->Flags);
532 if (credential->Type != CRED_TYPE_DOMAIN_PASSWORD)
533 FIXME("credential type of %d not supported\n", credential->Type);
534 if (credential->Persist != CRED_PERSIST_LOCAL_MACHINE)
535 FIXME("persist value of %d not supported\n", credential->Persist);
536 if (credential->AttributeCount)
537 FIXME("custom attributes not supported\n");
539 p = strchrW(credential->UserName, '\\');
542 domainlen = WideCharToMultiByte(CP_UTF8, 0, credential->UserName,
543 p - credential->UserName, NULL, 0, NULL, NULL);
544 domain = HeapAlloc(GetProcessHeap(), 0, (domainlen + 1) * sizeof(*domain));
545 WideCharToMultiByte(CP_UTF8, 0, credential->UserName, p - credential->UserName,
546 domain, domainlen, NULL, NULL);
547 domain[domainlen] = '\0';
551 p = credential->UserName;
552 userlen = WideCharToMultiByte(CP_UTF8, 0, p, -1, NULL, 0, NULL, NULL);
553 username = HeapAlloc(GetProcessHeap(), 0, userlen * sizeof(*username));
554 WideCharToMultiByte(CP_UTF8, 0, p, -1, username, userlen, NULL, NULL);
556 serverlen = WideCharToMultiByte(CP_UTF8, 0, credential->TargetName, -1, NULL, 0, NULL, NULL);
557 servername = HeapAlloc(GetProcessHeap(), 0, serverlen * sizeof(*servername));
558 WideCharToMultiByte(CP_UTF8, 0, credential->TargetName, -1, servername, serverlen, NULL, NULL);
559 pwlen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)credential->CredentialBlob,
560 credential->CredentialBlobSize / sizeof(WCHAR), NULL, 0, NULL, NULL);
561 password = HeapAlloc(GetProcessHeap(), 0, pwlen * sizeof(*domain));
562 WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)credential->CredentialBlob,
563 credential->CredentialBlobSize / sizeof(WCHAR), password, pwlen, NULL, NULL);
565 TRACE("adding server %s, domain %s, username %s using Keychain\n", servername, domain, username);
566 status = SecKeychainAddInternetPassword(NULL, strlen(servername), servername,
567 strlen(domain), domain, strlen(username),
568 username, 0, NULL, 0,
570 kSecAuthenticationTypeDefault,
571 strlen(password), password, &keychain_item);
573 ERR("SecKeychainAddInternetPassword returned %ld\n", status);
574 if (status == errSecDuplicateItem)
576 SecKeychainItemRef keychain_item;
578 status = SecKeychainFindInternetPassword(NULL, strlen(servername), servername,
579 strlen(domain), domain,
580 strlen(username), username,
581 0, NULL /* any path */, 0,
582 0 /* any protocol */,
583 0 /* any authentication type */,
584 0, NULL, &keychain_item);
586 ERR("SecKeychainFindInternetPassword returned %ld\n", status);
588 HeapFree(GetProcessHeap(), 0, domain);
589 HeapFree(GetProcessHeap(), 0, username);
590 HeapFree(GetProcessHeap(), 0, servername);
593 HeapFree(GetProcessHeap(), 0, password);
594 return ERROR_GEN_FAILURE;
596 if (credential->Comment)
599 attr_list.attr = attrs;
600 attrs[0].tag = kSecCommentItemAttr;
601 attrs[0].length = WideCharToMultiByte(CP_UTF8, 0, credential->Comment, -1, NULL, 0, NULL, NULL);
602 if (attrs[0].length) attrs[0].length--;
603 attrs[0].data = HeapAlloc(GetProcessHeap(), 0, attrs[0].length);
604 WideCharToMultiByte(CP_UTF8, 0, credential->Comment, -1, attrs[0].data, attrs[0].length, NULL, NULL);
609 attr_list.attr = NULL;
611 status = SecKeychainItemModifyAttributesAndData(keychain_item, &attr_list,
612 preserve_blob ? 0 : strlen(password),
613 preserve_blob ? NULL : password);
614 if (credential->Comment)
615 HeapFree(GetProcessHeap(), 0, attrs[0].data);
616 HeapFree(GetProcessHeap(), 0, password);
617 /* FIXME: set TargetAlias attribute */
618 CFRelease(keychain_item);
619 return ERROR_SUCCESS;
623 static DWORD open_cred_mgr_key(HKEY *hkey, BOOL open_for_write)
625 return RegCreateKeyExW(HKEY_CURRENT_USER, wszCredentialManagerKey, 0,
626 NULL, REG_OPTION_NON_VOLATILE,
627 KEY_READ | (open_for_write ? KEY_WRITE : 0), NULL, hkey, NULL);
630 static DWORD get_cred_mgr_encryption_key(HKEY hkeyMgr, BYTE key_data[KEY_SIZE])
632 static const BYTE my_key_data[KEY_SIZE] = { 0 };
640 memcpy(key_data, my_key_data, KEY_SIZE);
643 ret = RegQueryValueExW(hkeyMgr, wszEncryptionKeyValue, NULL, &type, (LPVOID)key_data,
645 if (ret == ERROR_SUCCESS)
647 if (type != REG_BINARY)
648 return ERROR_REGISTRY_CORRUPT;
650 return ERROR_SUCCESS;
652 if (ret != ERROR_FILE_NOT_FOUND)
655 GetSystemTimeAsFileTime(&ft);
656 seed = ft.dwLowDateTime;
657 value = RtlUniform(&seed);
658 *(DWORD *)key_data = value;
659 seed = ft.dwHighDateTime;
660 value = RtlUniform(&seed);
661 *(DWORD *)(key_data + 4) = value;
663 ret = RegSetValueExW(hkeyMgr, wszEncryptionKeyValue, 0, REG_BINARY,
664 (LPVOID)key_data, KEY_SIZE);
665 if (ret == ERROR_ACCESS_DENIED)
667 ret = open_cred_mgr_key(&hkeyMgr, TRUE);
668 if (ret == ERROR_SUCCESS)
670 ret = RegSetValueExW(hkeyMgr, wszEncryptionKeyValue, 0, REG_BINARY,
671 (LPVOID)key_data, KEY_SIZE);
672 RegCloseKey(hkeyMgr);
678 static LPWSTR get_key_name_for_target(LPCWSTR target_name, DWORD type)
680 static const WCHAR wszGenericPrefix[] = {'G','e','n','e','r','i','c',':',' ',0};
681 static const WCHAR wszDomPasswdPrefix[] = {'D','o','m','P','a','s','s','w','d',':',' ',0};
683 LPCWSTR prefix = NULL;
686 len = strlenW(target_name);
687 if (type == CRED_TYPE_GENERIC)
689 prefix = wszGenericPrefix;
690 len += sizeof(wszGenericPrefix)/sizeof(wszGenericPrefix[0]);
694 prefix = wszDomPasswdPrefix;
695 len += sizeof(wszDomPasswdPrefix)/sizeof(wszDomPasswdPrefix[0]);
698 key_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
699 if (!key_name) return NULL;
701 strcpyW(key_name, prefix);
702 strcatW(key_name, target_name);
704 for (p = key_name; *p; p++)
705 if (*p == '\\') *p = '_';
710 static BOOL credential_matches_filter(HKEY hkeyCred, LPCWSTR filter)
718 if (!filter) return TRUE;
720 ret = RegQueryValueExW(hkeyCred, NULL, 0, &type, NULL, &count);
721 if (ret != ERROR_SUCCESS)
723 else if (type != REG_SZ)
726 target_name = HeapAlloc(GetProcessHeap(), 0, count);
729 ret = RegQueryValueExW(hkeyCred, NULL, 0, &type, (LPVOID)target_name, &count);
730 if (ret != ERROR_SUCCESS || type != REG_SZ)
732 HeapFree(GetProcessHeap(), 0, target_name);
736 TRACE("comparing filter %s to target name %s\n", debugstr_w(filter),
737 debugstr_w(target_name));
739 p = strchrW(filter, '*');
740 ret = CompareStringW(GetThreadLocale(), 0, filter,
741 (p && !p[1] ? p - filter : -1), target_name,
742 (p && !p[1] ? p - filter : -1)) == CSTR_EQUAL;
744 HeapFree(GetProcessHeap(), 0, target_name);
748 static DWORD registry_enumerate_credentials(HKEY hkeyMgr, LPCWSTR filter,
750 DWORD target_name_len, BYTE key_data[KEY_SIZE],
751 PCREDENTIALW *credentials, char **buffer,
752 DWORD *len, DWORD *count)
759 ret = RegEnumKeyW(hkeyMgr, i, target_name, target_name_len+1);
760 if (ret == ERROR_NO_MORE_ITEMS)
765 else if (ret != ERROR_SUCCESS)
770 TRACE("target_name = %s\n", debugstr_w(target_name));
771 ret = RegOpenKeyExW(hkeyMgr, target_name, 0, KEY_QUERY_VALUE, &hkeyCred);
772 if (ret != ERROR_SUCCESS)
777 if (!credential_matches_filter(hkeyCred, filter))
779 RegCloseKey(hkeyCred);
784 *len = sizeof(CREDENTIALW);
785 credentials[*count] = (PCREDENTIALW)*buffer;
788 *len += sizeof(CREDENTIALW);
789 ret = registry_read_credential(hkeyCred, buffer ? credentials[*count] : NULL,
790 key_data, buffer ? *buffer + sizeof(CREDENTIALW) : NULL,
792 RegCloseKey(hkeyCred);
793 if (ret != ERROR_SUCCESS) break;
794 if (buffer) *buffer += *len;
801 static DWORD mac_enumerate_credentials(LPCWSTR filter, PCREDENTIALW *credentials,
802 char *buffer, DWORD *len, DWORD *count)
804 SecKeychainSearchRef search;
805 SecKeychainItemRef item;
807 Boolean saved_user_interaction_allowed;
810 SecKeychainGetUserInteractionAllowed(&saved_user_interaction_allowed);
811 SecKeychainSetUserInteractionAllowed(false);
813 status = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, NULL, &search);
816 while (SecKeychainSearchCopyNext(search, &item) == noErr)
818 SecKeychainAttributeInfo info;
819 SecKeychainAttributeList *attr_list;
820 UInt32 info_tags[] = { kSecServerItemAttr };
821 info.count = sizeof(info_tags)/sizeof(info_tags[0]);
822 info.tag = info_tags;
824 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, NULL, NULL);
827 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status);
832 *len = sizeof(CREDENTIALW);
833 credentials[*count] = (PCREDENTIALW)buffer;
836 *len += sizeof(CREDENTIALW);
837 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServerItemAttr) continue;
838 TRACE("server item: %.*s\n", (int)attr_list->attr[0].length, (char *)attr_list->attr[0].data);
839 /* FIXME: filter based on attr_list->attr[0].data */
840 SecKeychainItemFreeAttributesAndData(attr_list, NULL);
841 ret = mac_read_credential_from_item(item, FALSE,
842 buffer ? credentials[*count] : NULL,
843 buffer ? buffer + sizeof(CREDENTIALW) : NULL,
846 if (ret == ERROR_SUCCESS)
849 if (buffer) buffer += *len;
855 ERR("SecKeychainSearchCreateFromAttributes returned status %ld\n", status);
856 SecKeychainSetUserInteractionAllowed(saved_user_interaction_allowed);
857 return ERROR_SUCCESS;
860 static DWORD mac_delete_credential(LPCWSTR TargetName)
863 SecKeychainSearchRef search;
864 status = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, NULL, &search);
867 SecKeychainItemRef item;
868 while (SecKeychainSearchCopyNext(search, &item) == noErr)
870 SecKeychainAttributeInfo info;
871 SecKeychainAttributeList *attr_list;
872 UInt32 info_tags[] = { kSecServerItemAttr };
875 info.count = sizeof(info_tags)/sizeof(info_tags[0]);
876 info.tag = info_tags;
878 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, NULL, NULL);
881 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status);
884 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServerItemAttr)
889 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, NULL, 0);
890 target_name = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
891 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, target_name, str_len);
893 target_name[str_len] = '\0';
894 if (strcmpiW(TargetName, target_name))
897 HeapFree(GetProcessHeap(), 0, target_name);
900 HeapFree(GetProcessHeap(), 0, target_name);
901 SecKeychainItemFreeAttributesAndData(attr_list, NULL);
902 SecKeychainItemDelete(item);
906 return ERROR_SUCCESS;
910 return ERROR_NOT_FOUND;
914 static void convert_PCREDENTIALW_to_PCREDENTIALA(const CREDENTIALW *CredentialW, PCREDENTIALA CredentialA, DWORD *len)
916 char *buffer = (char *)CredentialA + sizeof(CREDENTIALA);
919 *len += sizeof(CREDENTIALA);
922 if (CredentialW->TargetName) *len += WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetName, -1, NULL, 0, NULL, NULL);
923 if (CredentialW->Comment) *len += WideCharToMultiByte(CP_ACP, 0, CredentialW->Comment, -1, NULL, 0, NULL, NULL);
924 *len += CredentialW->CredentialBlobSize;
925 if (CredentialW->TargetAlias) *len += WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetAlias, -1, NULL, 0, NULL, NULL);
926 if (CredentialW->UserName) *len += WideCharToMultiByte(CP_ACP, 0, CredentialW->UserName, -1, NULL, 0, NULL, NULL);
931 CredentialA->Flags = CredentialW->Flags;
932 CredentialA->Type = CredentialW->Type;
933 if (CredentialW->TargetName)
935 CredentialA->TargetName = buffer;
936 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetName, -1, CredentialA->TargetName, -1, NULL, NULL);
937 buffer += string_len;
941 CredentialA->TargetName = NULL;
942 if (CredentialW->Comment)
944 CredentialA->Comment = buffer;
945 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->Comment, -1, CredentialA->Comment, -1, NULL, NULL);
946 buffer += string_len;
950 CredentialA->Comment = NULL;
951 CredentialA->LastWritten = CredentialW->LastWritten;
952 CredentialA->CredentialBlobSize = CredentialW->CredentialBlobSize;
953 if (CredentialW->CredentialBlobSize)
955 CredentialA->CredentialBlob =(LPBYTE)buffer;
956 memcpy(CredentialA->CredentialBlob, CredentialW->CredentialBlob,
957 CredentialW->CredentialBlobSize);
958 buffer += CredentialW->CredentialBlobSize;
959 *len += CredentialW->CredentialBlobSize;
962 CredentialA->CredentialBlob = NULL;
963 CredentialA->Persist = CredentialW->Persist;
964 CredentialA->AttributeCount = 0;
965 CredentialA->Attributes = NULL; /* FIXME */
966 if (CredentialW->TargetAlias)
968 CredentialA->TargetAlias = buffer;
969 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetAlias, -1, CredentialA->TargetAlias, -1, NULL, NULL);
970 buffer += string_len;
974 CredentialA->TargetAlias = NULL;
975 if (CredentialW->UserName)
977 CredentialA->UserName = buffer;
978 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->UserName, -1, CredentialA->UserName, -1, NULL, NULL);
979 buffer += string_len;
983 CredentialA->UserName = NULL;
986 static void convert_PCREDENTIALA_to_PCREDENTIALW(const CREDENTIALA *CredentialA, PCREDENTIALW CredentialW, DWORD *len)
988 char *buffer = (char *)CredentialW + sizeof(CREDENTIALW);
991 *len += sizeof(CREDENTIALW);
994 if (CredentialA->TargetName) *len += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetName, -1, NULL, 0);
995 if (CredentialA->Comment) *len += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->Comment, -1, NULL, 0);
996 *len += CredentialA->CredentialBlobSize;
997 if (CredentialA->TargetAlias) *len += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetAlias, -1, NULL, 0);
998 if (CredentialA->UserName) *len += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->UserName, -1, NULL, 0);
1003 CredentialW->Flags = CredentialA->Flags;
1004 CredentialW->Type = CredentialA->Type;
1005 if (CredentialA->TargetName)
1007 CredentialW->TargetName = (LPWSTR)buffer;
1008 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetName, -1, CredentialW->TargetName, -1);
1009 buffer += sizeof(WCHAR) * string_len;
1010 *len += sizeof(WCHAR) * string_len;
1013 CredentialW->TargetName = NULL;
1014 if (CredentialA->Comment)
1016 CredentialW->Comment = (LPWSTR)buffer;
1017 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->Comment, -1, CredentialW->Comment, -1);
1018 buffer += sizeof(WCHAR) * string_len;
1019 *len += sizeof(WCHAR) * string_len;
1022 CredentialW->Comment = NULL;
1023 CredentialW->LastWritten = CredentialA->LastWritten;
1024 CredentialW->CredentialBlobSize = CredentialA->CredentialBlobSize;
1025 if (CredentialA->CredentialBlobSize)
1027 CredentialW->CredentialBlob =(LPBYTE)buffer;
1028 memcpy(CredentialW->CredentialBlob, CredentialA->CredentialBlob,
1029 CredentialA->CredentialBlobSize);
1030 buffer += CredentialA->CredentialBlobSize;
1031 *len += CredentialA->CredentialBlobSize;
1034 CredentialW->CredentialBlob = NULL;
1035 CredentialW->Persist = CredentialA->Persist;
1036 CredentialW->AttributeCount = 0;
1037 CredentialW->Attributes = NULL; /* FIXME */
1038 if (CredentialA->TargetAlias)
1040 CredentialW->TargetAlias = (LPWSTR)buffer;
1041 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetAlias, -1, CredentialW->TargetAlias, -1);
1042 buffer += sizeof(WCHAR) * string_len;
1043 *len += sizeof(WCHAR) * string_len;
1046 CredentialW->TargetAlias = NULL;
1047 if (CredentialA->UserName)
1049 CredentialW->UserName = (LPWSTR)buffer;
1050 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->UserName, -1, CredentialW->UserName, -1);
1051 buffer += sizeof(WCHAR) * string_len;
1052 *len += sizeof(WCHAR) * string_len;
1055 CredentialW->UserName = NULL;
1058 /******************************************************************************
1059 * CredDeleteA [ADVAPI32.@]
1061 BOOL WINAPI CredDeleteA(LPCSTR TargetName, DWORD Type, DWORD Flags)
1067 TRACE("(%s, %d, 0x%x)\n", debugstr_a(TargetName), Type, Flags);
1071 SetLastError(ERROR_INVALID_PARAMETER);
1075 len = MultiByteToWideChar(CP_ACP, 0, TargetName, -1, NULL, 0);
1076 TargetNameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1079 SetLastError(ERROR_OUTOFMEMORY);
1082 MultiByteToWideChar(CP_ACP, 0, TargetName, -1, TargetNameW, len);
1084 ret = CredDeleteW(TargetNameW, Type, Flags);
1086 HeapFree(GetProcessHeap(), 0, TargetNameW);
1091 /******************************************************************************
1092 * CredDeleteW [ADVAPI32.@]
1094 BOOL WINAPI CredDeleteW(LPCWSTR TargetName, DWORD Type, DWORD Flags)
1100 TRACE("(%s, %d, 0x%x)\n", debugstr_w(TargetName), Type, Flags);
1104 SetLastError(ERROR_INVALID_PARAMETER);
1108 if (Type != CRED_TYPE_GENERIC && Type != CRED_TYPE_DOMAIN_PASSWORD)
1110 FIXME("unhandled type %d\n", Type);
1111 SetLastError(ERROR_INVALID_PARAMETER);
1117 FIXME("unhandled flags 0x%x\n", Flags);
1118 SetLastError(ERROR_INVALID_FLAGS);
1123 if (Type == CRED_TYPE_DOMAIN_PASSWORD)
1125 ret = mac_delete_credential(TargetName);
1126 if (ret == ERROR_SUCCESS)
1131 ret = open_cred_mgr_key(&hkeyMgr, TRUE);
1132 if (ret != ERROR_SUCCESS)
1134 WARN("couldn't open/create manager key, error %d\n", ret);
1135 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1139 key_name = get_key_name_for_target(TargetName, Type);
1140 ret = RegDeleteKeyW(hkeyMgr, key_name);
1141 HeapFree(GetProcessHeap(), 0, key_name);
1142 RegCloseKey(hkeyMgr);
1143 if (ret != ERROR_SUCCESS)
1145 SetLastError(ERROR_NOT_FOUND);
1152 /******************************************************************************
1153 * CredEnumerateA [ADVAPI32.@]
1155 BOOL WINAPI CredEnumerateA(LPCSTR Filter, DWORD Flags, DWORD *Count,
1156 PCREDENTIALA **Credentials)
1159 PCREDENTIALW *CredentialsW;
1164 TRACE("(%s, 0x%x, %p, %p)\n", debugstr_a(Filter), Flags, Count, Credentials);
1168 len = MultiByteToWideChar(CP_ACP, 0, Filter, -1, NULL, 0);
1169 FilterW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1172 SetLastError(ERROR_OUTOFMEMORY);
1175 MultiByteToWideChar(CP_ACP, 0, Filter, -1, FilterW, len);
1180 if (!CredEnumerateW(FilterW, Flags, Count, &CredentialsW))
1182 HeapFree(GetProcessHeap(), 0, FilterW);
1185 HeapFree(GetProcessHeap(), 0, FilterW);
1187 len = *Count * sizeof(PCREDENTIALA);
1188 for (i = 0; i < *Count; i++)
1189 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW[i], NULL, &len);
1191 *Credentials = HeapAlloc(GetProcessHeap(), 0, len);
1194 CredFree(CredentialsW);
1195 SetLastError(ERROR_OUTOFMEMORY);
1199 buffer = (char *)&(*Credentials)[*Count];
1200 for (i = 0; i < *Count; i++)
1203 (*Credentials)[i] = (PCREDENTIALA)buffer;
1204 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW[i], (*Credentials)[i], &len);
1208 CredFree(CredentialsW);
1213 /******************************************************************************
1214 * CredEnumerateW [ADVAPI32.@]
1216 BOOL WINAPI CredEnumerateW(LPCWSTR Filter, DWORD Flags, DWORD *Count,
1217 PCREDENTIALW **Credentials)
1222 DWORD target_name_len;
1225 BYTE key_data[KEY_SIZE];
1227 TRACE("(%s, 0x%x, %p, %p)\n", debugstr_w(Filter), Flags, Count, Credentials);
1231 SetLastError(ERROR_INVALID_FLAGS);
1235 ret = open_cred_mgr_key(&hkeyMgr, FALSE);
1236 if (ret != ERROR_SUCCESS)
1238 WARN("couldn't open/create manager key, error %d\n", ret);
1239 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1243 ret = get_cred_mgr_encryption_key(hkeyMgr, key_data);
1244 if (ret != ERROR_SUCCESS)
1246 RegCloseKey(hkeyMgr);
1251 ret = RegQueryInfoKeyW(hkeyMgr, NULL, NULL, NULL, NULL, &target_name_len, NULL, NULL, NULL, NULL, NULL, NULL);
1252 if (ret != ERROR_SUCCESS)
1254 RegCloseKey(hkeyMgr);
1259 target_name = HeapAlloc(GetProcessHeap(), 0, (target_name_len+1)*sizeof(WCHAR));
1262 RegCloseKey(hkeyMgr);
1263 SetLastError(ERROR_OUTOFMEMORY);
1269 ret = registry_enumerate_credentials(hkeyMgr, Filter, target_name, target_name_len,
1270 key_data, NULL, NULL, &len, Count);
1272 if (ret == ERROR_SUCCESS)
1273 ret = mac_enumerate_credentials(Filter, NULL, NULL, &len, Count);
1275 if (ret == ERROR_SUCCESS && *Count == 0)
1276 ret = ERROR_NOT_FOUND;
1277 if (ret != ERROR_SUCCESS)
1279 HeapFree(GetProcessHeap(), 0, target_name);
1280 RegCloseKey(hkeyMgr);
1284 len += *Count * sizeof(PCREDENTIALW);
1286 if (ret == ERROR_SUCCESS)
1288 buffer = HeapAlloc(GetProcessHeap(), 0, len);
1289 *Credentials = (PCREDENTIALW *)buffer;
1292 buffer += *Count * sizeof(PCREDENTIALW);
1294 ret = registry_enumerate_credentials(hkeyMgr, Filter, target_name,
1295 target_name_len, key_data,
1296 *Credentials, &buffer, &len,
1299 if (ret == ERROR_SUCCESS)
1300 ret = mac_enumerate_credentials(Filter, *Credentials,
1301 buffer, &len, Count);
1305 ret = ERROR_OUTOFMEMORY;
1308 HeapFree(GetProcessHeap(), 0, target_name);
1309 RegCloseKey(hkeyMgr);
1311 if (ret != ERROR_SUCCESS)
1319 /******************************************************************************
1320 * CredFree [ADVAPI32.@]
1322 VOID WINAPI CredFree(PVOID Buffer)
1324 HeapFree(GetProcessHeap(), 0, Buffer);
1327 /******************************************************************************
1328 * CredReadA [ADVAPI32.@]
1330 BOOL WINAPI CredReadA(LPCSTR TargetName, DWORD Type, DWORD Flags, PCREDENTIALA *Credential)
1333 PCREDENTIALW CredentialW;
1336 TRACE("(%s, %d, 0x%x, %p)\n", debugstr_a(TargetName), Type, Flags, Credential);
1340 SetLastError(ERROR_INVALID_PARAMETER);
1344 len = MultiByteToWideChar(CP_ACP, 0, TargetName, -1, NULL, 0);
1345 TargetNameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1348 SetLastError(ERROR_OUTOFMEMORY);
1351 MultiByteToWideChar(CP_ACP, 0, TargetName, -1, TargetNameW, len);
1353 if (!CredReadW(TargetNameW, Type, Flags, &CredentialW))
1355 HeapFree(GetProcessHeap(), 0, TargetNameW);
1358 HeapFree(GetProcessHeap(), 0, TargetNameW);
1361 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialW, NULL, &len);
1362 *Credential = HeapAlloc(GetProcessHeap(), 0, len);
1365 SetLastError(ERROR_OUTOFMEMORY);
1369 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialW, *Credential, &len);
1371 CredFree(CredentialW);
1376 /******************************************************************************
1377 * CredReadW [ADVAPI32.@]
1379 BOOL WINAPI CredReadW(LPCWSTR TargetName, DWORD Type, DWORD Flags, PCREDENTIALW *Credential)
1386 BYTE key_data[KEY_SIZE];
1388 TRACE("(%s, %d, 0x%x, %p)\n", debugstr_w(TargetName), Type, Flags, Credential);
1392 SetLastError(ERROR_INVALID_PARAMETER);
1396 if (Type != CRED_TYPE_GENERIC && Type != CRED_TYPE_DOMAIN_PASSWORD)
1398 FIXME("unhandled type %d\n", Type);
1399 SetLastError(ERROR_INVALID_PARAMETER);
1405 FIXME("unhandled flags 0x%x\n", Flags);
1406 SetLastError(ERROR_INVALID_FLAGS);
1411 if (Type == CRED_TYPE_DOMAIN_PASSWORD)
1414 SecKeychainSearchRef search;
1415 status = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, NULL, &search);
1416 if (status == noErr)
1418 SecKeychainItemRef item;
1419 while (SecKeychainSearchCopyNext(search, &item) == noErr)
1421 SecKeychainAttributeInfo info;
1422 SecKeychainAttributeList *attr_list;
1423 UInt32 info_tags[] = { kSecServerItemAttr };
1426 info.count = sizeof(info_tags)/sizeof(info_tags[0]);
1427 info.tag = info_tags;
1429 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, NULL, NULL);
1430 len = sizeof(**Credential);
1431 if (status != noErr)
1433 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status);
1436 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServerItemAttr)
1441 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, NULL, 0);
1442 target_name = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
1443 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, target_name, str_len);
1445 target_name[str_len] = '\0';
1446 if (strcmpiW(TargetName, target_name))
1449 HeapFree(GetProcessHeap(), 0, target_name);
1452 HeapFree(GetProcessHeap(), 0, target_name);
1453 SecKeychainItemFreeAttributesAndData(attr_list, NULL);
1454 ret = mac_read_credential_from_item(item, TRUE, NULL, NULL, &len);
1455 if (ret == ERROR_SUCCESS)
1457 *Credential = HeapAlloc(GetProcessHeap(), 0, len);
1460 len = sizeof(**Credential);
1461 ret = mac_read_credential_from_item(item, TRUE, *Credential,
1462 (char *)(*Credential + 1), &len);
1465 ret = ERROR_OUTOFMEMORY;
1468 if (ret != ERROR_SUCCESS)
1482 ret = open_cred_mgr_key(&hkeyMgr, FALSE);
1483 if (ret != ERROR_SUCCESS)
1485 WARN("couldn't open/create manager key, error %d\n", ret);
1486 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1490 ret = get_cred_mgr_encryption_key(hkeyMgr, key_data);
1491 if (ret != ERROR_SUCCESS)
1493 RegCloseKey(hkeyMgr);
1498 key_name = get_key_name_for_target(TargetName, Type);
1499 ret = RegOpenKeyExW(hkeyMgr, key_name, 0, KEY_QUERY_VALUE, &hkeyCred);
1500 HeapFree(GetProcessHeap(), 0, key_name);
1501 if (ret != ERROR_SUCCESS)
1503 TRACE("credentials for target name %s not found\n", debugstr_w(TargetName));
1504 SetLastError(ERROR_NOT_FOUND);
1508 len = sizeof(**Credential);
1509 ret = registry_read_credential(hkeyCred, NULL, key_data, NULL, &len);
1510 if (ret == ERROR_SUCCESS)
1512 *Credential = HeapAlloc(GetProcessHeap(), 0, len);
1515 len = sizeof(**Credential);
1516 ret = registry_read_credential(hkeyCred, *Credential, key_data,
1517 (char *)(*Credential + 1), &len);
1520 ret = ERROR_OUTOFMEMORY;
1523 RegCloseKey(hkeyCred);
1524 RegCloseKey(hkeyMgr);
1526 if (ret != ERROR_SUCCESS)
1534 /******************************************************************************
1535 * CredWriteA [ADVAPI32.@]
1537 BOOL WINAPI CredWriteA(PCREDENTIALA Credential, DWORD Flags)
1541 PCREDENTIALW CredentialW;
1543 TRACE("(%p, 0x%x)\n", Credential, Flags);
1545 if (!Credential || !Credential->TargetName)
1547 SetLastError(ERROR_INVALID_PARAMETER);
1552 convert_PCREDENTIALA_to_PCREDENTIALW(Credential, NULL, &len);
1553 CredentialW = HeapAlloc(GetProcessHeap(), 0, len);
1556 SetLastError(ERROR_OUTOFMEMORY);
1560 convert_PCREDENTIALA_to_PCREDENTIALW(Credential, CredentialW, &len);
1562 ret = CredWriteW(CredentialW, Flags);
1564 HeapFree(GetProcessHeap(), 0, CredentialW);
1569 /******************************************************************************
1570 * CredWriteW [ADVAPI32.@]
1572 BOOL WINAPI CredWriteW(PCREDENTIALW Credential, DWORD Flags)
1578 BYTE key_data[KEY_SIZE];
1580 TRACE("(%p, 0x%x)\n", Credential, Flags);
1582 if (!Credential || !Credential->TargetName)
1584 SetLastError(ERROR_INVALID_PARAMETER);
1588 if (Flags & ~CRED_PRESERVE_CREDENTIAL_BLOB)
1590 FIXME("unhandled flags 0x%x\n", Flags);
1591 SetLastError(ERROR_INVALID_FLAGS);
1595 if (Credential->Type != CRED_TYPE_GENERIC && Credential->Type != CRED_TYPE_DOMAIN_PASSWORD)
1597 FIXME("unhandled type %d\n", Credential->Type);
1598 SetLastError(ERROR_INVALID_PARAMETER);
1602 TRACE("Credential->TargetName = %s\n", debugstr_w(Credential->TargetName));
1603 TRACE("Credential->UserName = %s\n", debugstr_w(Credential->UserName));
1605 if (Credential->Type == CRED_TYPE_DOMAIN_PASSWORD)
1607 if (!Credential->UserName ||
1608 (!strchrW(Credential->UserName, '\\') && !strchrW(Credential->UserName, '@')))
1610 ERR("bad username %s\n", debugstr_w(Credential->UserName));
1611 SetLastError(ERROR_BAD_USERNAME);
1617 if (!Credential->AttributeCount &&
1618 Credential->Type == CRED_TYPE_DOMAIN_PASSWORD &&
1619 (Credential->Persist == CRED_PERSIST_LOCAL_MACHINE || Credential->Persist == CRED_PERSIST_ENTERPRISE))
1621 ret = mac_write_credential(Credential, Flags & CRED_PRESERVE_CREDENTIAL_BLOB);
1622 if (ret != ERROR_SUCCESS)
1631 ret = open_cred_mgr_key(&hkeyMgr, FALSE);
1632 if (ret != ERROR_SUCCESS)
1634 WARN("couldn't open/create manager key, error %d\n", ret);
1635 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1639 ret = get_cred_mgr_encryption_key(hkeyMgr, key_data);
1640 if (ret != ERROR_SUCCESS)
1642 RegCloseKey(hkeyMgr);
1647 key_name = get_key_name_for_target(Credential->TargetName, Credential->Type);
1648 ret = RegCreateKeyExW(hkeyMgr, key_name, 0, NULL,
1649 Credential->Persist == CRED_PERSIST_SESSION ? REG_OPTION_VOLATILE : REG_OPTION_NON_VOLATILE,
1650 KEY_READ|KEY_WRITE, NULL, &hkeyCred, NULL);
1651 HeapFree(GetProcessHeap(), 0, key_name);
1652 if (ret != ERROR_SUCCESS)
1654 TRACE("credentials for target name %s not found\n",
1655 debugstr_w(Credential->TargetName));
1656 SetLastError(ERROR_NOT_FOUND);
1660 ret = registry_write_credential(hkeyCred, Credential, key_data,
1661 Flags & CRED_PRESERVE_CREDENTIAL_BLOB);
1663 RegCloseKey(hkeyCred);
1664 RegCloseKey(hkeyMgr);
1666 if (ret != ERROR_SUCCESS)