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
26 # include <Security/SecKeychain.h>
27 # include <Security/SecKeychainItem.h>
28 # include <Security/SecKeychainSearch.h>
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(cred);
44 /* the size of the ARC4 key used to encrypt the password data */
47 static const WCHAR wszCredentialManagerKey[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
48 'C','r','e','d','e','n','t','i','a','l',' ','M','a','n','a','g','e','r',0};
49 static const WCHAR wszEncryptionKeyValue[] = {'E','n','c','r','y','p','t','i','o','n','K','e','y',0};
51 static const WCHAR wszFlagsValue[] = {'F','l','a','g','s',0};
52 static const WCHAR wszTypeValue[] = {'T','y','p','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, 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 = KEY_SIZE;
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)
172 credential->UserName = NULL;
173 else if (ret != ERROR_SUCCESS)
175 else if (type != REG_SZ)
176 return ERROR_REGISTRY_CORRUPT;
181 ret = read_credential_blob(hkey, key_data, NULL, &count);
182 if (ret != ERROR_FILE_NOT_FOUND && ret != ERROR_SUCCESS)
187 credential->CredentialBlob = (LPBYTE)buffer;
188 ret = read_credential_blob(hkey, key_data, credential->CredentialBlob, &count);
189 if (ret == ERROR_FILE_NOT_FOUND)
190 credential->CredentialBlob = NULL;
191 else if (ret != ERROR_SUCCESS)
193 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 SecKeychainItemFreeAttributesAndData(attr_list, cred_blob);
277 return ERROR_NOT_FOUND;
282 credential->Flags = 0;
283 credential->Type = CRED_TYPE_DOMAIN_PASSWORD;
284 credential->TargetName = NULL;
285 credential->Comment = NULL;
286 memset(&credential->LastWritten, 0, sizeof(credential->LastWritten));
287 credential->CredentialBlobSize = 0;
288 credential->CredentialBlob = NULL;
289 credential->Persist = CRED_PERSIST_LOCAL_MACHINE;
290 credential->AttributeCount = 0;
291 credential->Attributes = NULL;
292 credential->TargetAlias = NULL;
293 credential->UserName = NULL;
295 for (i = 0; i < attr_list->count; i++)
297 switch (attr_list->attr[i].tag)
299 case kSecServerItemAttr:
300 TRACE("kSecServerItemAttr: %.*s\n", (int)attr_list->attr[i].length,
301 (char *)attr_list->attr[i].data);
302 if (!attr_list->attr[i].data) continue;
306 credential->TargetName = (LPWSTR)buffer;
307 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
308 attr_list->attr[i].length, (LPWSTR)buffer, 0xffff);
309 credential->TargetName[str_len] = '\0';
310 buffer += (str_len + 1) * sizeof(WCHAR);
311 *len += (str_len + 1) * sizeof(WCHAR);
316 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
317 attr_list->attr[i].length, NULL, 0);
318 *len += (str_len + 1) * sizeof(WCHAR);
321 case kSecAccountItemAttr:
324 TRACE("kSecAccountItemAttr: %.*s\n", (int)attr_list->attr[i].length,
325 (char *)attr_list->attr[i].data);
326 if (!attr_list->attr[i].data) continue;
327 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
328 attr_list->attr[i].length, NULL, 0);
329 user = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
330 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
331 attr_list->attr[i].length, user, str_len);
332 user[str_len] = '\0';
335 case kSecCommentItemAttr:
336 TRACE("kSecCommentItemAttr: %.*s\n", (int)attr_list->attr[i].length,
337 (char *)attr_list->attr[i].data);
338 if (!attr_list->attr[i].data) continue;
342 credential->Comment = (LPWSTR)buffer;
343 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
344 attr_list->attr[i].length, (LPWSTR)buffer, 0xffff);
345 credential->Comment[str_len] = '\0';
346 buffer += (str_len + 1) * sizeof(WCHAR);
347 *len += (str_len + 1) * sizeof(WCHAR);
352 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
353 attr_list->attr[i].length, NULL, 0);
354 *len += (str_len + 1) * sizeof(WCHAR);
357 case kSecSecurityDomainItemAttr:
360 TRACE("kSecSecurityDomainItemAttr: %.*s\n", (int)attr_list->attr[i].length,
361 (char *)attr_list->attr[i].data);
362 if (!attr_list->attr[i].data) continue;
363 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
364 attr_list->attr[i].length, NULL, 0);
365 domain = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
366 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[i].data,
367 attr_list->attr[i].length, domain, str_len);
368 domain[str_len] = '\0';
371 case kSecCreationDateItemAttr:
372 TRACE("kSecCreationDateItemAttr: %.*s\n", (int)attr_list->attr[i].length,
373 (char *)attr_list->attr[i].data);
376 LARGE_INTEGER win_time;
379 memset(&tm, 0, sizeof(tm));
380 strptime(attr_list->attr[i].data, "%Y%m%d%H%M%SZ", &tm);
382 RtlSecondsSince1970ToTime(time, &win_time);
383 credential->LastWritten.dwLowDateTime = win_time.u.LowPart;
384 credential->LastWritten.dwHighDateTime = win_time.u.HighPart;
394 credential->UserName = (LPWSTR)buffer;
397 str_len = strlenW(domain);
398 *len += (str_len + 1) * sizeof(WCHAR);
401 memcpy(credential->UserName, domain, str_len * sizeof(WCHAR));
402 /* FIXME: figure out when to use an '@' */
403 credential->UserName[str_len] = '\\';
404 buffer += (str_len + 1) * sizeof(WCHAR);
407 str_len = strlenW(user);
408 *len += (str_len + 1) * sizeof(WCHAR);
411 memcpy(buffer, user, (str_len + 1) * sizeof(WCHAR));
412 buffer += (str_len + 1) * sizeof(WCHAR);
413 TRACE("UserName = %s\n", debugstr_w(credential->UserName));
416 HeapFree(GetProcessHeap(), 0, user);
417 HeapFree(GetProcessHeap(), 0, domain);
424 credential->CredentialBlob = (BYTE *)buffer;
425 str_len = MultiByteToWideChar(CP_UTF8, 0, cred_blob, cred_blob_len,
426 (LPWSTR)buffer, 0xffff);
427 credential->CredentialBlobSize = 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, (const BYTE*)&credential->Flags,
478 sizeof(credential->Flags));
479 if (ret != ERROR_SUCCESS) return ret;
480 ret = RegSetValueExW(hkey, wszTypeValue, 0, REG_DWORD, (const BYTE*)&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, (const BYTE*)&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);
627 return ERROR_GEN_FAILURE;
628 return ERROR_SUCCESS;
632 static DWORD open_cred_mgr_key(HKEY *hkey, BOOL open_for_write)
634 return RegCreateKeyExW(HKEY_CURRENT_USER, wszCredentialManagerKey, 0,
635 NULL, REG_OPTION_NON_VOLATILE,
636 KEY_READ | (open_for_write ? KEY_WRITE : 0), NULL, hkey, NULL);
639 static DWORD get_cred_mgr_encryption_key(HKEY hkeyMgr, BYTE key_data[KEY_SIZE])
641 static const BYTE my_key_data[KEY_SIZE] = { 0 };
649 memcpy(key_data, my_key_data, KEY_SIZE);
652 ret = RegQueryValueExW(hkeyMgr, wszEncryptionKeyValue, NULL, &type, key_data,
654 if (ret == ERROR_SUCCESS)
656 if (type != REG_BINARY)
657 return ERROR_REGISTRY_CORRUPT;
659 return ERROR_SUCCESS;
661 if (ret != ERROR_FILE_NOT_FOUND)
664 GetSystemTimeAsFileTime(&ft);
665 seed = ft.dwLowDateTime;
666 value = RtlUniform(&seed);
667 *(DWORD *)key_data = value;
668 seed = ft.dwHighDateTime;
669 value = RtlUniform(&seed);
670 *(DWORD *)(key_data + 4) = value;
672 ret = RegSetValueExW(hkeyMgr, wszEncryptionKeyValue, 0, REG_BINARY,
674 if (ret == ERROR_ACCESS_DENIED)
676 ret = open_cred_mgr_key(&hkeyMgr, TRUE);
677 if (ret == ERROR_SUCCESS)
679 ret = RegSetValueExW(hkeyMgr, wszEncryptionKeyValue, 0, REG_BINARY,
681 RegCloseKey(hkeyMgr);
687 static LPWSTR get_key_name_for_target(LPCWSTR target_name, DWORD type)
689 static const WCHAR wszGenericPrefix[] = {'G','e','n','e','r','i','c',':',' ',0};
690 static const WCHAR wszDomPasswdPrefix[] = {'D','o','m','P','a','s','s','w','d',':',' ',0};
692 LPCWSTR prefix = NULL;
695 len = strlenW(target_name);
696 if (type == CRED_TYPE_GENERIC)
698 prefix = wszGenericPrefix;
699 len += sizeof(wszGenericPrefix)/sizeof(wszGenericPrefix[0]);
703 prefix = wszDomPasswdPrefix;
704 len += sizeof(wszDomPasswdPrefix)/sizeof(wszDomPasswdPrefix[0]);
707 key_name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
708 if (!key_name) return NULL;
710 strcpyW(key_name, prefix);
711 strcatW(key_name, target_name);
713 for (p = key_name; *p; p++)
714 if (*p == '\\') *p = '_';
719 static BOOL credential_matches_filter(HKEY hkeyCred, LPCWSTR filter)
727 if (!filter) return TRUE;
729 ret = RegQueryValueExW(hkeyCred, NULL, 0, &type, NULL, &count);
730 if (ret != ERROR_SUCCESS)
732 else if (type != REG_SZ)
735 target_name = HeapAlloc(GetProcessHeap(), 0, count);
738 ret = RegQueryValueExW(hkeyCred, NULL, 0, &type, (LPVOID)target_name, &count);
739 if (ret != ERROR_SUCCESS || type != REG_SZ)
741 HeapFree(GetProcessHeap(), 0, target_name);
745 TRACE("comparing filter %s to target name %s\n", debugstr_w(filter),
746 debugstr_w(target_name));
748 p = strchrW(filter, '*');
749 ret = CompareStringW(GetThreadLocale(), 0, filter,
750 (p && !p[1] ? p - filter : -1), target_name,
751 (p && !p[1] ? p - filter : -1)) == CSTR_EQUAL;
753 HeapFree(GetProcessHeap(), 0, target_name);
757 static DWORD registry_enumerate_credentials(HKEY hkeyMgr, LPCWSTR filter,
759 DWORD target_name_len, const BYTE key_data[KEY_SIZE],
760 PCREDENTIALW *credentials, char **buffer,
761 DWORD *len, DWORD *count)
768 ret = RegEnumKeyW(hkeyMgr, i, target_name, target_name_len+1);
769 if (ret == ERROR_NO_MORE_ITEMS)
774 else if (ret != ERROR_SUCCESS)
776 TRACE("target_name = %s\n", debugstr_w(target_name));
777 ret = RegOpenKeyExW(hkeyMgr, target_name, 0, KEY_QUERY_VALUE, &hkeyCred);
778 if (ret != ERROR_SUCCESS)
780 if (!credential_matches_filter(hkeyCred, filter))
782 RegCloseKey(hkeyCred);
787 *len = sizeof(CREDENTIALW);
788 credentials[*count] = (PCREDENTIALW)*buffer;
791 *len += sizeof(CREDENTIALW);
792 ret = registry_read_credential(hkeyCred, buffer ? credentials[*count] : NULL,
793 key_data, buffer ? *buffer + sizeof(CREDENTIALW) : NULL,
795 RegCloseKey(hkeyCred);
796 if (ret != ERROR_SUCCESS) break;
797 if (buffer) *buffer += *len;
804 static DWORD mac_enumerate_credentials(LPCWSTR filter, PCREDENTIALW *credentials,
805 char *buffer, DWORD *len, DWORD *count)
807 SecKeychainSearchRef search;
808 SecKeychainItemRef item;
810 Boolean saved_user_interaction_allowed;
813 SecKeychainGetUserInteractionAllowed(&saved_user_interaction_allowed);
814 SecKeychainSetUserInteractionAllowed(false);
816 status = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, NULL, &search);
819 while (SecKeychainSearchCopyNext(search, &item) == noErr)
821 SecKeychainAttributeInfo info;
822 SecKeychainAttributeList *attr_list;
823 UInt32 info_tags[] = { kSecServerItemAttr };
824 info.count = sizeof(info_tags)/sizeof(info_tags[0]);
825 info.tag = info_tags;
827 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, NULL, NULL);
830 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status);
835 *len = sizeof(CREDENTIALW);
836 credentials[*count] = (PCREDENTIALW)buffer;
839 *len += sizeof(CREDENTIALW);
840 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServerItemAttr) continue;
841 TRACE("server item: %.*s\n", (int)attr_list->attr[0].length, (char *)attr_list->attr[0].data);
842 /* FIXME: filter based on attr_list->attr[0].data */
843 SecKeychainItemFreeAttributesAndData(attr_list, NULL);
844 ret = mac_read_credential_from_item(item, FALSE,
845 buffer ? credentials[*count] : NULL,
846 buffer ? buffer + sizeof(CREDENTIALW) : NULL,
849 if (ret == ERROR_SUCCESS)
852 if (buffer) buffer += *len;
858 ERR("SecKeychainSearchCreateFromAttributes returned status %ld\n", status);
859 SecKeychainSetUserInteractionAllowed(saved_user_interaction_allowed);
860 return ERROR_SUCCESS;
863 static DWORD mac_delete_credential(LPCWSTR TargetName)
866 SecKeychainSearchRef search;
867 status = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, NULL, &search);
870 SecKeychainItemRef item;
871 while (SecKeychainSearchCopyNext(search, &item) == noErr)
873 SecKeychainAttributeInfo info;
874 SecKeychainAttributeList *attr_list;
875 UInt32 info_tags[] = { kSecServerItemAttr };
878 info.count = sizeof(info_tags)/sizeof(info_tags[0]);
879 info.tag = info_tags;
881 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, NULL, NULL);
884 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status);
887 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServerItemAttr)
892 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, NULL, 0);
893 target_name = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
894 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, target_name, str_len);
896 target_name[str_len] = '\0';
897 if (strcmpiW(TargetName, target_name))
900 HeapFree(GetProcessHeap(), 0, target_name);
903 HeapFree(GetProcessHeap(), 0, target_name);
904 SecKeychainItemFreeAttributesAndData(attr_list, NULL);
905 SecKeychainItemDelete(item);
909 return ERROR_SUCCESS;
913 return ERROR_NOT_FOUND;
917 /******************************************************************************
918 * convert_PCREDENTIALW_to_PCREDENTIALA [internal]
920 * convert a Credential struct from UNICODE to ANSI and return the needed size in Bytes
924 static INT convert_PCREDENTIALW_to_PCREDENTIALA(const CREDENTIALW *CredentialW, PCREDENTIALA CredentialA, DWORD len)
928 INT needed = sizeof(CREDENTIALA);
932 if (CredentialW->TargetName)
933 needed += WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetName, -1, NULL, 0, NULL, NULL);
934 if (CredentialW->Comment)
935 needed += WideCharToMultiByte(CP_ACP, 0, CredentialW->Comment, -1, NULL, 0, NULL, NULL);
936 needed += CredentialW->CredentialBlobSize;
937 if (CredentialW->TargetAlias)
938 needed += WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetAlias, -1, NULL, 0, NULL, NULL);
939 if (CredentialW->UserName)
940 needed += WideCharToMultiByte(CP_ACP, 0, CredentialW->UserName, -1, NULL, 0, NULL, NULL);
946 buffer = (char *)CredentialA + sizeof(CREDENTIALA);
947 len -= sizeof(CREDENTIALA);
948 CredentialA->Flags = CredentialW->Flags;
949 CredentialA->Type = CredentialW->Type;
951 if (CredentialW->TargetName)
953 CredentialA->TargetName = buffer;
954 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetName, -1, buffer, len, NULL, NULL);
955 buffer += string_len;
956 needed += string_len;
960 CredentialA->TargetName = NULL;
961 if (CredentialW->Comment)
963 CredentialA->Comment = buffer;
964 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->Comment, -1, buffer, len, NULL, NULL);
965 buffer += string_len;
966 needed += string_len;
970 CredentialA->Comment = NULL;
971 CredentialA->LastWritten = CredentialW->LastWritten;
972 CredentialA->CredentialBlobSize = CredentialW->CredentialBlobSize;
973 if (CredentialW->CredentialBlobSize && (CredentialW->CredentialBlobSize <= len))
975 CredentialA->CredentialBlob =(LPBYTE)buffer;
976 memcpy(CredentialA->CredentialBlob, CredentialW->CredentialBlob,
977 CredentialW->CredentialBlobSize);
978 buffer += CredentialW->CredentialBlobSize;
979 needed += CredentialW->CredentialBlobSize;
980 len -= CredentialW->CredentialBlobSize;
983 CredentialA->CredentialBlob = NULL;
984 CredentialA->Persist = CredentialW->Persist;
985 CredentialA->AttributeCount = 0;
986 CredentialA->Attributes = NULL; /* FIXME */
987 if (CredentialW->TargetAlias)
989 CredentialA->TargetAlias = buffer;
990 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->TargetAlias, -1, buffer, len, NULL, NULL);
991 buffer += string_len;
992 needed += string_len;
996 CredentialA->TargetAlias = NULL;
997 if (CredentialW->UserName)
999 CredentialA->UserName = buffer;
1000 string_len = WideCharToMultiByte(CP_ACP, 0, CredentialW->UserName, -1, buffer, len, NULL, NULL);
1001 needed += string_len;
1004 CredentialA->UserName = NULL;
1009 /******************************************************************************
1010 * convert_PCREDENTIALA_to_PCREDENTIALW [internal]
1012 * convert a Credential struct from ANSI to UNICODE and return the needed size in Bytes
1015 static INT convert_PCREDENTIALA_to_PCREDENTIALW(const CREDENTIALA *CredentialA, PCREDENTIALW CredentialW, INT len)
1019 INT needed = sizeof(CREDENTIALW);
1023 if (CredentialA->TargetName)
1024 needed += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetName, -1, NULL, 0);
1025 if (CredentialA->Comment)
1026 needed += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->Comment, -1, NULL, 0);
1027 needed += CredentialA->CredentialBlobSize;
1028 if (CredentialA->TargetAlias)
1029 needed += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetAlias, -1, NULL, 0);
1030 if (CredentialA->UserName)
1031 needed += sizeof(WCHAR) * MultiByteToWideChar(CP_ACP, 0, CredentialA->UserName, -1, NULL, 0);
1036 buffer = (char *)CredentialW + sizeof(CREDENTIALW);
1037 len -= sizeof(CREDENTIALW);
1038 CredentialW->Flags = CredentialA->Flags;
1039 CredentialW->Type = CredentialA->Type;
1040 if (CredentialA->TargetName)
1042 CredentialW->TargetName = (LPWSTR)buffer;
1043 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetName, -1, CredentialW->TargetName, len / sizeof(WCHAR));
1044 buffer += sizeof(WCHAR) * string_len;
1045 needed += sizeof(WCHAR) * string_len;
1046 len -= sizeof(WCHAR) * string_len;
1049 CredentialW->TargetName = NULL;
1050 if (CredentialA->Comment)
1052 CredentialW->Comment = (LPWSTR)buffer;
1053 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->Comment, -1, CredentialW->Comment, len / sizeof(WCHAR));
1054 buffer += sizeof(WCHAR) * string_len;
1055 needed += sizeof(WCHAR) * string_len;
1056 len -= sizeof(WCHAR) * string_len;
1059 CredentialW->Comment = NULL;
1060 CredentialW->LastWritten = CredentialA->LastWritten;
1061 CredentialW->CredentialBlobSize = CredentialA->CredentialBlobSize;
1062 if (CredentialA->CredentialBlobSize)
1064 CredentialW->CredentialBlob =(LPBYTE)buffer;
1065 memcpy(CredentialW->CredentialBlob, CredentialA->CredentialBlob,
1066 CredentialA->CredentialBlobSize);
1067 buffer += CredentialA->CredentialBlobSize;
1068 needed += CredentialA->CredentialBlobSize;
1069 len -= CredentialA->CredentialBlobSize;
1072 CredentialW->CredentialBlob = NULL;
1073 CredentialW->Persist = CredentialA->Persist;
1074 CredentialW->AttributeCount = 0;
1075 CredentialW->Attributes = NULL; /* FIXME */
1076 if (CredentialA->TargetAlias)
1078 CredentialW->TargetAlias = (LPWSTR)buffer;
1079 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->TargetAlias, -1, CredentialW->TargetAlias, len / sizeof(WCHAR));
1080 buffer += sizeof(WCHAR) * string_len;
1081 needed += sizeof(WCHAR) * string_len;
1082 len -= sizeof(WCHAR) * string_len;
1085 CredentialW->TargetAlias = NULL;
1086 if (CredentialA->UserName)
1088 CredentialW->UserName = (LPWSTR)buffer;
1089 string_len = MultiByteToWideChar(CP_ACP, 0, CredentialA->UserName, -1, CredentialW->UserName, len / sizeof(WCHAR));
1090 needed += sizeof(WCHAR) * string_len;
1093 CredentialW->UserName = NULL;
1098 /******************************************************************************
1099 * CredDeleteA [ADVAPI32.@]
1101 BOOL WINAPI CredDeleteA(LPCSTR TargetName, DWORD Type, DWORD Flags)
1107 TRACE("(%s, %d, 0x%x)\n", debugstr_a(TargetName), Type, Flags);
1111 SetLastError(ERROR_INVALID_PARAMETER);
1115 len = MultiByteToWideChar(CP_ACP, 0, TargetName, -1, NULL, 0);
1116 TargetNameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1119 SetLastError(ERROR_OUTOFMEMORY);
1122 MultiByteToWideChar(CP_ACP, 0, TargetName, -1, TargetNameW, len);
1124 ret = CredDeleteW(TargetNameW, Type, Flags);
1126 HeapFree(GetProcessHeap(), 0, TargetNameW);
1131 /******************************************************************************
1132 * CredDeleteW [ADVAPI32.@]
1134 BOOL WINAPI CredDeleteW(LPCWSTR TargetName, DWORD Type, DWORD Flags)
1140 TRACE("(%s, %d, 0x%x)\n", debugstr_w(TargetName), Type, Flags);
1144 SetLastError(ERROR_INVALID_PARAMETER);
1148 if (Type != CRED_TYPE_GENERIC && Type != CRED_TYPE_DOMAIN_PASSWORD)
1150 FIXME("unhandled type %d\n", Type);
1151 SetLastError(ERROR_INVALID_PARAMETER);
1157 FIXME("unhandled flags 0x%x\n", Flags);
1158 SetLastError(ERROR_INVALID_FLAGS);
1163 if (Type == CRED_TYPE_DOMAIN_PASSWORD)
1165 ret = mac_delete_credential(TargetName);
1166 if (ret == ERROR_SUCCESS)
1171 ret = open_cred_mgr_key(&hkeyMgr, TRUE);
1172 if (ret != ERROR_SUCCESS)
1174 WARN("couldn't open/create manager key, error %d\n", ret);
1175 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1179 key_name = get_key_name_for_target(TargetName, Type);
1180 ret = RegDeleteKeyW(hkeyMgr, key_name);
1181 HeapFree(GetProcessHeap(), 0, key_name);
1182 RegCloseKey(hkeyMgr);
1183 if (ret != ERROR_SUCCESS)
1185 SetLastError(ERROR_NOT_FOUND);
1192 /******************************************************************************
1193 * CredEnumerateA [ADVAPI32.@]
1195 BOOL WINAPI CredEnumerateA(LPCSTR Filter, DWORD Flags, DWORD *Count,
1196 PCREDENTIALA **Credentials)
1199 PCREDENTIALW *CredentialsW;
1205 TRACE("(%s, 0x%x, %p, %p)\n", debugstr_a(Filter), Flags, Count, Credentials);
1209 len = MultiByteToWideChar(CP_ACP, 0, Filter, -1, NULL, 0);
1210 FilterW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1213 SetLastError(ERROR_OUTOFMEMORY);
1216 MultiByteToWideChar(CP_ACP, 0, Filter, -1, FilterW, len);
1221 if (!CredEnumerateW(FilterW, Flags, Count, &CredentialsW))
1223 HeapFree(GetProcessHeap(), 0, FilterW);
1226 HeapFree(GetProcessHeap(), 0, FilterW);
1228 len = *Count * sizeof(PCREDENTIALA);
1229 for (i = 0; i < *Count; i++)
1230 len += convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW[i], NULL, 0);
1232 *Credentials = HeapAlloc(GetProcessHeap(), 0, len);
1235 CredFree(CredentialsW);
1236 SetLastError(ERROR_OUTOFMEMORY);
1240 buffer = (char *)&(*Credentials)[*Count];
1241 len -= *Count * sizeof(PCREDENTIALA);
1242 for (i = 0; i < *Count; i++)
1244 (*Credentials)[i] = (PCREDENTIALA)buffer;
1245 needed = convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW[i], (*Credentials)[i], len);
1250 CredFree(CredentialsW);
1255 /******************************************************************************
1256 * CredEnumerateW [ADVAPI32.@]
1258 BOOL WINAPI CredEnumerateW(LPCWSTR Filter, DWORD Flags, DWORD *Count,
1259 PCREDENTIALW **Credentials)
1264 DWORD target_name_len;
1267 BYTE key_data[KEY_SIZE];
1269 TRACE("(%s, 0x%x, %p, %p)\n", debugstr_w(Filter), Flags, Count, Credentials);
1273 SetLastError(ERROR_INVALID_FLAGS);
1277 ret = open_cred_mgr_key(&hkeyMgr, FALSE);
1278 if (ret != ERROR_SUCCESS)
1280 WARN("couldn't open/create manager key, error %d\n", ret);
1281 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1285 ret = get_cred_mgr_encryption_key(hkeyMgr, key_data);
1286 if (ret != ERROR_SUCCESS)
1288 RegCloseKey(hkeyMgr);
1293 ret = RegQueryInfoKeyW(hkeyMgr, NULL, NULL, NULL, NULL, &target_name_len, NULL, NULL, NULL, NULL, NULL, NULL);
1294 if (ret != ERROR_SUCCESS)
1296 RegCloseKey(hkeyMgr);
1301 target_name = HeapAlloc(GetProcessHeap(), 0, (target_name_len+1)*sizeof(WCHAR));
1304 RegCloseKey(hkeyMgr);
1305 SetLastError(ERROR_OUTOFMEMORY);
1311 ret = registry_enumerate_credentials(hkeyMgr, Filter, target_name, target_name_len,
1312 key_data, NULL, NULL, &len, Count);
1314 if (ret == ERROR_SUCCESS)
1315 ret = mac_enumerate_credentials(Filter, NULL, NULL, &len, Count);
1317 if (ret == ERROR_SUCCESS && *Count == 0)
1318 ret = ERROR_NOT_FOUND;
1319 if (ret != ERROR_SUCCESS)
1321 HeapFree(GetProcessHeap(), 0, target_name);
1322 RegCloseKey(hkeyMgr);
1326 len += *Count * sizeof(PCREDENTIALW);
1328 if (ret == ERROR_SUCCESS)
1330 buffer = HeapAlloc(GetProcessHeap(), 0, len);
1331 *Credentials = (PCREDENTIALW *)buffer;
1334 buffer += *Count * sizeof(PCREDENTIALW);
1336 ret = registry_enumerate_credentials(hkeyMgr, Filter, target_name,
1337 target_name_len, key_data,
1338 *Credentials, &buffer, &len,
1341 if (ret == ERROR_SUCCESS)
1342 ret = mac_enumerate_credentials(Filter, *Credentials,
1343 buffer, &len, Count);
1347 ret = ERROR_OUTOFMEMORY;
1350 HeapFree(GetProcessHeap(), 0, target_name);
1351 RegCloseKey(hkeyMgr);
1353 if (ret != ERROR_SUCCESS)
1361 /******************************************************************************
1362 * CredFree [ADVAPI32.@]
1364 VOID WINAPI CredFree(PVOID Buffer)
1366 HeapFree(GetProcessHeap(), 0, Buffer);
1369 /******************************************************************************
1370 * CredReadA [ADVAPI32.@]
1372 BOOL WINAPI CredReadA(LPCSTR TargetName, DWORD Type, DWORD Flags, PCREDENTIALA *Credential)
1375 PCREDENTIALW CredentialW;
1378 TRACE("(%s, %d, 0x%x, %p)\n", debugstr_a(TargetName), Type, Flags, Credential);
1382 SetLastError(ERROR_INVALID_PARAMETER);
1386 len = MultiByteToWideChar(CP_ACP, 0, TargetName, -1, NULL, 0);
1387 TargetNameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1390 SetLastError(ERROR_OUTOFMEMORY);
1393 MultiByteToWideChar(CP_ACP, 0, TargetName, -1, TargetNameW, len);
1395 if (!CredReadW(TargetNameW, Type, Flags, &CredentialW))
1397 HeapFree(GetProcessHeap(), 0, TargetNameW);
1400 HeapFree(GetProcessHeap(), 0, TargetNameW);
1402 len = convert_PCREDENTIALW_to_PCREDENTIALA(CredentialW, NULL, 0);
1403 *Credential = HeapAlloc(GetProcessHeap(), 0, len);
1406 SetLastError(ERROR_OUTOFMEMORY);
1409 convert_PCREDENTIALW_to_PCREDENTIALA(CredentialW, *Credential, len);
1411 CredFree(CredentialW);
1416 /******************************************************************************
1417 * CredReadW [ADVAPI32.@]
1419 BOOL WINAPI CredReadW(LPCWSTR TargetName, DWORD Type, DWORD Flags, PCREDENTIALW *Credential)
1426 BYTE key_data[KEY_SIZE];
1428 TRACE("(%s, %d, 0x%x, %p)\n", debugstr_w(TargetName), Type, Flags, Credential);
1432 SetLastError(ERROR_INVALID_PARAMETER);
1436 if (Type != CRED_TYPE_GENERIC && Type != CRED_TYPE_DOMAIN_PASSWORD)
1438 FIXME("unhandled type %d\n", Type);
1439 SetLastError(ERROR_INVALID_PARAMETER);
1445 FIXME("unhandled flags 0x%x\n", Flags);
1446 SetLastError(ERROR_INVALID_FLAGS);
1451 if (Type == CRED_TYPE_DOMAIN_PASSWORD)
1454 SecKeychainSearchRef search;
1455 status = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, NULL, &search);
1456 if (status == noErr)
1458 SecKeychainItemRef item;
1459 while (SecKeychainSearchCopyNext(search, &item) == noErr)
1461 SecKeychainAttributeInfo info;
1462 SecKeychainAttributeList *attr_list;
1463 UInt32 info_tags[] = { kSecServerItemAttr };
1466 info.count = sizeof(info_tags)/sizeof(info_tags[0]);
1467 info.tag = info_tags;
1469 status = SecKeychainItemCopyAttributesAndData(item, &info, NULL, &attr_list, NULL, NULL);
1470 len = sizeof(**Credential);
1471 if (status != noErr)
1473 WARN("SecKeychainItemCopyAttributesAndData returned status %ld\n", status);
1476 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServerItemAttr)
1481 str_len = MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, NULL, 0);
1482 target_name = HeapAlloc(GetProcessHeap(), 0, (str_len + 1) * sizeof(WCHAR));
1483 MultiByteToWideChar(CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, target_name, str_len);
1485 target_name[str_len] = '\0';
1486 if (strcmpiW(TargetName, target_name))
1489 HeapFree(GetProcessHeap(), 0, target_name);
1492 HeapFree(GetProcessHeap(), 0, target_name);
1493 SecKeychainItemFreeAttributesAndData(attr_list, NULL);
1494 ret = mac_read_credential_from_item(item, TRUE, NULL, NULL, &len);
1495 if (ret == ERROR_SUCCESS)
1497 *Credential = HeapAlloc(GetProcessHeap(), 0, len);
1500 len = sizeof(**Credential);
1501 ret = mac_read_credential_from_item(item, TRUE, *Credential,
1502 (char *)(*Credential + 1), &len);
1505 ret = ERROR_OUTOFMEMORY;
1508 if (ret != ERROR_SUCCESS)
1522 ret = open_cred_mgr_key(&hkeyMgr, FALSE);
1523 if (ret != ERROR_SUCCESS)
1525 WARN("couldn't open/create manager key, error %d\n", ret);
1526 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1530 ret = get_cred_mgr_encryption_key(hkeyMgr, key_data);
1531 if (ret != ERROR_SUCCESS)
1533 RegCloseKey(hkeyMgr);
1538 key_name = get_key_name_for_target(TargetName, Type);
1539 ret = RegOpenKeyExW(hkeyMgr, key_name, 0, KEY_QUERY_VALUE, &hkeyCred);
1540 HeapFree(GetProcessHeap(), 0, key_name);
1541 if (ret != ERROR_SUCCESS)
1543 TRACE("credentials for target name %s not found\n", debugstr_w(TargetName));
1544 SetLastError(ERROR_NOT_FOUND);
1548 len = sizeof(**Credential);
1549 ret = registry_read_credential(hkeyCred, NULL, key_data, NULL, &len);
1550 if (ret == ERROR_SUCCESS)
1552 *Credential = HeapAlloc(GetProcessHeap(), 0, len);
1555 len = sizeof(**Credential);
1556 ret = registry_read_credential(hkeyCred, *Credential, key_data,
1557 (char *)(*Credential + 1), &len);
1560 ret = ERROR_OUTOFMEMORY;
1563 RegCloseKey(hkeyCred);
1564 RegCloseKey(hkeyMgr);
1566 if (ret != ERROR_SUCCESS)
1574 /******************************************************************************
1575 * CredReadDomainCredentialsA [ADVAPI32.@]
1577 BOOL WINAPI CredReadDomainCredentialsA(PCREDENTIAL_TARGET_INFORMATIONA TargetInformation,
1578 DWORD Flags, DWORD *Size, PCREDENTIALA **Credentials)
1580 PCREDENTIAL_TARGET_INFORMATIONW TargetInformationW;
1583 WCHAR *buffer, *end;
1585 PCREDENTIALW* CredentialsW;
1587 TRACE("(%p, 0x%x, %p, %p)\n", TargetInformation, Flags, Size, Credentials);
1589 /* follow Windows behavior - do not test for NULL, initialize early */
1591 *Credentials = NULL;
1593 if (!TargetInformation)
1595 SetLastError(ERROR_INVALID_PARAMETER);
1599 len = sizeof(*TargetInformationW);
1600 if (TargetInformation->TargetName)
1601 len += MultiByteToWideChar(CP_ACP, 0, TargetInformation->TargetName, -1, NULL, 0) * sizeof(WCHAR);
1602 if (TargetInformation->NetbiosServerName)
1603 len += MultiByteToWideChar(CP_ACP, 0, TargetInformation->NetbiosServerName, -1, NULL, 0) * sizeof(WCHAR);
1604 if (TargetInformation->DnsServerName)
1605 len += MultiByteToWideChar(CP_ACP, 0, TargetInformation->DnsServerName, -1, NULL, 0) * sizeof(WCHAR);
1606 if (TargetInformation->NetbiosDomainName)
1607 len += MultiByteToWideChar(CP_ACP, 0, TargetInformation->NetbiosDomainName, -1, NULL, 0) * sizeof(WCHAR);
1608 if (TargetInformation->DnsDomainName)
1609 len += MultiByteToWideChar(CP_ACP, 0, TargetInformation->DnsDomainName, -1, NULL, 0) * sizeof(WCHAR);
1610 if (TargetInformation->DnsTreeName)
1611 len += MultiByteToWideChar(CP_ACP, 0, TargetInformation->DnsTreeName, -1, NULL, 0) * sizeof(WCHAR);
1612 if (TargetInformation->PackageName)
1613 len += MultiByteToWideChar(CP_ACP, 0, TargetInformation->PackageName, -1, NULL, 0) * sizeof(WCHAR);
1615 TargetInformationW = HeapAlloc(GetProcessHeap(), 0, len);
1616 if (!TargetInformationW)
1618 SetLastError(ERROR_OUTOFMEMORY);
1621 buffer = (WCHAR*)(TargetInformationW + 1);
1622 end = (WCHAR *)((char *)TargetInformationW + len);
1624 if (TargetInformation->TargetName)
1626 TargetInformationW->TargetName = buffer;
1627 buffer += MultiByteToWideChar(CP_ACP, 0, TargetInformation->TargetName, -1,
1628 TargetInformationW->TargetName, end - buffer);
1630 TargetInformationW->TargetName = NULL;
1632 if (TargetInformation->NetbiosServerName)
1634 TargetInformationW->NetbiosServerName = buffer;
1635 buffer += MultiByteToWideChar(CP_ACP, 0, TargetInformation->NetbiosServerName, -1,
1636 TargetInformationW->NetbiosServerName, end - buffer);
1638 TargetInformationW->NetbiosServerName = NULL;
1640 if (TargetInformation->DnsServerName)
1642 TargetInformationW->DnsServerName = buffer;
1643 buffer += MultiByteToWideChar(CP_ACP, 0, TargetInformation->DnsServerName, -1,
1644 TargetInformationW->DnsServerName, end - buffer);
1646 TargetInformationW->DnsServerName = NULL;
1648 if (TargetInformation->NetbiosDomainName)
1650 TargetInformationW->NetbiosDomainName = buffer;
1651 buffer += MultiByteToWideChar(CP_ACP, 0, TargetInformation->NetbiosDomainName, -1,
1652 TargetInformationW->NetbiosDomainName, end - buffer);
1654 TargetInformationW->NetbiosDomainName = NULL;
1656 if (TargetInformation->DnsDomainName)
1658 TargetInformationW->DnsDomainName = buffer;
1659 buffer += MultiByteToWideChar(CP_ACP, 0, TargetInformation->DnsDomainName, -1,
1660 TargetInformationW->DnsDomainName, end - buffer);
1662 TargetInformationW->DnsDomainName = NULL;
1664 if (TargetInformation->DnsTreeName)
1666 TargetInformationW->DnsTreeName = buffer;
1667 buffer += MultiByteToWideChar(CP_ACP, 0, TargetInformation->DnsTreeName, -1,
1668 TargetInformationW->DnsTreeName, end - buffer);
1670 TargetInformationW->DnsTreeName = NULL;
1672 if (TargetInformation->PackageName)
1674 TargetInformationW->PackageName = buffer;
1675 MultiByteToWideChar(CP_ACP, 0, TargetInformation->PackageName, -1,
1676 TargetInformationW->PackageName, end - buffer);
1678 TargetInformationW->PackageName = NULL;
1680 TargetInformationW->Flags = TargetInformation->Flags;
1681 TargetInformationW->CredTypeCount = TargetInformation->CredTypeCount;
1682 TargetInformationW->CredTypes = TargetInformation->CredTypes;
1684 ret = CredReadDomainCredentialsW(TargetInformationW, Flags, Size, &CredentialsW);
1686 HeapFree(GetProcessHeap(), 0, TargetInformationW);
1693 len = *Size * sizeof(PCREDENTIALA);
1694 for (i = 0; i < *Size; i++)
1695 len += convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW[i], NULL, 0);
1697 *Credentials = HeapAlloc(GetProcessHeap(), 0, len);
1700 CredFree(CredentialsW);
1701 SetLastError(ERROR_OUTOFMEMORY);
1705 buf = (char *)&(*Credentials)[*Size];
1706 len -= *Size * sizeof(PCREDENTIALA);
1707 for (i = 0; i < *Size; i++)
1709 (*Credentials)[i] = (PCREDENTIALA)buf;
1710 needed = convert_PCREDENTIALW_to_PCREDENTIALA(CredentialsW[i], (*Credentials)[i], len);
1715 CredFree(CredentialsW);
1720 /******************************************************************************
1721 * CredReadDomainCredentialsW [ADVAPI32.@]
1723 BOOL WINAPI CredReadDomainCredentialsW(PCREDENTIAL_TARGET_INFORMATIONW TargetInformation, DWORD Flags,
1724 DWORD *Size, PCREDENTIALW **Credentials)
1726 FIXME("(%p, 0x%x, %p, %p) stub\n", TargetInformation, Flags, Size, Credentials);
1728 /* follow Windows behavior - do not test for NULL, initialize early */
1730 *Credentials = NULL;
1731 if (!TargetInformation)
1733 SetLastError(ERROR_INVALID_PARAMETER);
1737 SetLastError(ERROR_NOT_FOUND);
1741 /******************************************************************************
1742 * CredWriteA [ADVAPI32.@]
1744 BOOL WINAPI CredWriteA(PCREDENTIALA Credential, DWORD Flags)
1748 PCREDENTIALW CredentialW;
1750 TRACE("(%p, 0x%x)\n", Credential, Flags);
1752 if (!Credential || !Credential->TargetName)
1754 SetLastError(ERROR_INVALID_PARAMETER);
1758 len = convert_PCREDENTIALA_to_PCREDENTIALW(Credential, NULL, 0);
1759 CredentialW = HeapAlloc(GetProcessHeap(), 0, len);
1762 SetLastError(ERROR_OUTOFMEMORY);
1766 convert_PCREDENTIALA_to_PCREDENTIALW(Credential, CredentialW, len);
1768 ret = CredWriteW(CredentialW, Flags);
1770 HeapFree(GetProcessHeap(), 0, CredentialW);
1775 /******************************************************************************
1776 * CredWriteW [ADVAPI32.@]
1778 BOOL WINAPI CredWriteW(PCREDENTIALW Credential, DWORD Flags)
1784 BYTE key_data[KEY_SIZE];
1786 TRACE("(%p, 0x%x)\n", Credential, Flags);
1788 if (!Credential || !Credential->TargetName)
1790 SetLastError(ERROR_INVALID_PARAMETER);
1794 if (Flags & ~CRED_PRESERVE_CREDENTIAL_BLOB)
1796 FIXME("unhandled flags 0x%x\n", Flags);
1797 SetLastError(ERROR_INVALID_FLAGS);
1801 if (Credential->Type != CRED_TYPE_GENERIC && Credential->Type != CRED_TYPE_DOMAIN_PASSWORD)
1803 FIXME("unhandled type %d\n", Credential->Type);
1804 SetLastError(ERROR_INVALID_PARAMETER);
1808 TRACE("Credential->Flags = 0x%08x\n", Credential->Flags);
1809 TRACE("Credential->Type = %u\n", Credential->Type);
1810 TRACE("Credential->TargetName = %s\n", debugstr_w(Credential->TargetName));
1811 TRACE("Credential->Comment = %s\n", debugstr_w(Credential->Comment));
1812 TRACE("Credential->Persist = %u\n", Credential->Persist);
1813 TRACE("Credential->TargetAlias = %s\n", debugstr_w(Credential->TargetAlias));
1814 TRACE("Credential->UserName = %s\n", debugstr_w(Credential->UserName));
1816 if (Credential->Type == CRED_TYPE_DOMAIN_PASSWORD)
1818 if (!Credential->UserName ||
1819 (Credential->Persist == CRED_PERSIST_ENTERPRISE &&
1820 (!strchrW(Credential->UserName, '\\') && !strchrW(Credential->UserName, '@'))))
1822 ERR("bad username %s\n", debugstr_w(Credential->UserName));
1823 SetLastError(ERROR_BAD_USERNAME);
1829 if (!Credential->AttributeCount &&
1830 Credential->Type == CRED_TYPE_DOMAIN_PASSWORD &&
1831 (Credential->Persist == CRED_PERSIST_LOCAL_MACHINE || Credential->Persist == CRED_PERSIST_ENTERPRISE))
1833 ret = mac_write_credential(Credential, Flags & CRED_PRESERVE_CREDENTIAL_BLOB);
1834 if (ret != ERROR_SUCCESS)
1843 ret = open_cred_mgr_key(&hkeyMgr, FALSE);
1844 if (ret != ERROR_SUCCESS)
1846 WARN("couldn't open/create manager key, error %d\n", ret);
1847 SetLastError(ERROR_NO_SUCH_LOGON_SESSION);
1851 ret = get_cred_mgr_encryption_key(hkeyMgr, key_data);
1852 if (ret != ERROR_SUCCESS)
1854 RegCloseKey(hkeyMgr);
1859 key_name = get_key_name_for_target(Credential->TargetName, Credential->Type);
1860 ret = RegCreateKeyExW(hkeyMgr, key_name, 0, NULL,
1861 Credential->Persist == CRED_PERSIST_SESSION ? REG_OPTION_VOLATILE : REG_OPTION_NON_VOLATILE,
1862 KEY_READ|KEY_WRITE, NULL, &hkeyCred, NULL);
1863 HeapFree(GetProcessHeap(), 0, key_name);
1864 if (ret != ERROR_SUCCESS)
1866 TRACE("credentials for target name %s not found\n",
1867 debugstr_w(Credential->TargetName));
1868 SetLastError(ERROR_NOT_FOUND);
1872 ret = registry_write_credential(hkeyCred, Credential, key_data,
1873 Flags & CRED_PRESERVE_CREDENTIAL_BLOB);
1875 RegCloseKey(hkeyCred);
1876 RegCloseKey(hkeyMgr);
1878 if (ret != ERROR_SUCCESS)
1886 /******************************************************************************
1887 * CredGetSessionTypes [ADVAPI32.@]
1889 WINADVAPI BOOL WINAPI CredGetSessionTypes(DWORD persistCount, LPDWORD persists)
1891 TRACE("(%u, %p)\n", persistCount, persists);
1893 memset(persists, CRED_PERSIST_NONE, persistCount*sizeof(*persists));
1894 if (CRED_TYPE_GENERIC < persistCount)
1896 persists[CRED_TYPE_GENERIC] = CRED_PERSIST_ENTERPRISE;
1898 if (CRED_TYPE_DOMAIN_PASSWORD < persistCount)
1900 persists[CRED_TYPE_DOMAIN_PASSWORD] = CRED_PERSIST_ENTERPRISE;
1906 /******************************************************************************
1907 * CredMarshalCredentialA [ADVAPI32.@]
1909 BOOL WINAPI CredMarshalCredentialA( CRED_MARSHAL_TYPE type, PVOID cred, LPSTR *out )
1914 TRACE("%u, %p, %p\n", type, cred, out);
1916 if ((ret = CredMarshalCredentialW( type, cred, &outW )))
1918 int len = WideCharToMultiByte( CP_ACP, 0, outW, -1, NULL, 0, NULL, NULL );
1919 if (!(*out = HeapAlloc( GetProcessHeap(), 0, len )))
1921 HeapFree( GetProcessHeap(), 0, outW );
1924 WideCharToMultiByte( CP_ACP, 0, outW, -1, *out, len, NULL, NULL );
1925 HeapFree( GetProcessHeap(), 0, outW );
1930 static UINT cred_encode( const char *bin, unsigned int len, WCHAR *cred )
1932 static char enc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#-";
1937 cred[n++] = enc[bin[0] & 0x3f];
1938 x = (bin[0] & 0xc0) >> 6;
1944 cred[n++] = enc[((bin[1] & 0xf) << 2) | x];
1945 x = (bin[1] & 0xf0) >> 4;
1951 cred[n++] = enc[((bin[2] & 0x3) << 4) | x];
1952 cred[n++] = enc[(bin[2] & 0xfc) >> 2];
1959 /******************************************************************************
1960 * CredMarshalCredentialW [ADVAPI32.@]
1962 BOOL WINAPI CredMarshalCredentialW( CRED_MARSHAL_TYPE type, PVOID cred, LPWSTR *out )
1964 CERT_CREDENTIAL_INFO *cert = cred;
1965 USERNAME_TARGET_CREDENTIAL_INFO *target = cred;
1969 TRACE("%u, %p, %p\n", type, cred, out);
1971 if (!cred || (type == CertCredential && cert->cbSize < sizeof(*cert)) ||
1972 (type != CertCredential && type != UsernameTargetCredential && type != BinaryBlobCredential) ||
1973 (type == UsernameTargetCredential && (!target->UserName || !target->UserName[0])))
1975 SetLastError( ERROR_INVALID_PARAMETER );
1980 case CertCredential:
1982 char hash[CERT_HASH_LENGTH + 2];
1984 memcpy( hash, cert->rgbHashOfCert, sizeof(cert->rgbHashOfCert) );
1985 memset( hash + sizeof(cert->rgbHashOfCert), 0, sizeof(hash) - sizeof(cert->rgbHashOfCert) );
1987 size = sizeof(hash) * 4 / 3;
1988 if (!(p = HeapAlloc( GetProcessHeap(), 0, (size + 4) * sizeof(WCHAR) ))) return FALSE;
1992 len = cred_encode( (const char *)hash, sizeof(hash), p + 3 );
1996 case UsernameTargetCredential:
1998 len = strlenW( target->UserName );
1999 size = (sizeof(DWORD) + len * sizeof(WCHAR) + 2) * 4 / 3;
2000 if (!(p = HeapAlloc( GetProcessHeap(), 0, (size + 4) * sizeof(WCHAR) ))) return FALSE;
2004 size = len * sizeof(WCHAR);
2005 len = cred_encode( (const char *)&size, sizeof(DWORD), p + 3 );
2006 len += cred_encode( (const char *)target->UserName, size, p + 3 + len );
2010 case BinaryBlobCredential:
2011 FIXME("BinaryBlobCredential not implemented\n");
2020 /******************************************************************************
2021 * CredUnmarshalCredentialA [ADVAPI32.@]
2023 BOOL WINAPI CredUnmarshalCredentialA( LPCSTR cred, PCRED_MARSHAL_TYPE type, PVOID *out )
2026 WCHAR *credW = NULL;
2028 TRACE("%s, %p, %p\n", debugstr_a(cred), type, out);
2032 int len = MultiByteToWideChar( CP_ACP, 0, cred, -1, NULL, 0 );
2033 if (!(credW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
2034 MultiByteToWideChar( CP_ACP, 0, cred, -1, credW, len );
2036 ret = CredUnmarshalCredentialW( credW, type, out );
2037 HeapFree( GetProcessHeap(), 0, credW );
2041 static inline char char_decode( WCHAR c )
2043 if (c >= 'A' && c <= 'Z') return c - 'A';
2044 if (c >= 'a' && c <= 'z') return c - 'a' + 26;
2045 if (c >= '0' && c <= '9') return c - '0' + 52;
2046 if (c == '#') return 62;
2047 if (c == '-') return 63;
2051 static BOOL cred_decode( const WCHAR *cred, unsigned int len, char *buf )
2054 char c0, c1, c2, c3;
2055 const WCHAR *p = cred;
2059 if ((c0 = char_decode( p[0] )) > 63) return FALSE;
2060 if ((c1 = char_decode( p[1] )) > 63) return FALSE;
2061 if ((c2 = char_decode( p[2] )) > 63) return FALSE;
2062 if ((c3 = char_decode( p[3] )) > 63) return FALSE;
2064 buf[i + 0] = (c1 << 6) | c0;
2065 buf[i + 1] = (c2 << 4) | (c1 >> 2);
2066 buf[i + 2] = (c3 << 2) | (c2 >> 4);
2073 if ((c0 = char_decode( p[0] )) > 63) return FALSE;
2074 if ((c1 = char_decode( p[1] )) > 63) return FALSE;
2075 if ((c2 = char_decode( p[2] )) > 63) return FALSE;
2077 buf[i + 0] = (c1 << 6) | c0;
2078 buf[i + 1] = (c2 << 4) | (c1 >> 2);
2079 buf[i + 2] = c2 >> 4;
2083 if ((c0 = char_decode( p[0] )) > 63) return FALSE;
2084 if ((c1 = char_decode( p[1] )) > 63) return FALSE;
2086 buf[i + 0] = (c1 << 6) | c0;
2087 buf[i + 1] = c1 >> 2;
2092 if ((c0 = char_decode( p[0] )) > 63) return FALSE;
2101 /******************************************************************************
2102 * CredUnmarshalCredentialW [ADVAPI32.@]
2104 BOOL WINAPI CredUnmarshalCredentialW( LPCWSTR cred, PCRED_MARSHAL_TYPE type, PVOID *out )
2106 unsigned int len, buflen;
2108 TRACE("%s, %p, %p\n", debugstr_w(cred), type, out);
2110 if (!cred || cred[0] != '@' || cred[1] != '@' || !cred[2] || !cred[3])
2112 SetLastError( ERROR_INVALID_PARAMETER );
2115 len = strlenW( cred + 3 );
2116 switch (cred[2] - 'A')
2118 case CertCredential:
2120 char hash[CERT_HASH_LENGTH + 2];
2121 CERT_CREDENTIAL_INFO *cert;
2123 if (len != 27 || !cred_decode( cred + 3, len, hash ))
2125 SetLastError( ERROR_INVALID_PARAMETER );
2128 if (!(cert = HeapAlloc( GetProcessHeap(), 0, sizeof(*cert) ))) return FALSE;
2129 memcpy( cert->rgbHashOfCert, hash, sizeof(cert->rgbHashOfCert) );
2130 cert->cbSize = sizeof(*cert);
2131 *type = CertCredential;
2135 case UsernameTargetCredential:
2137 USERNAME_TARGET_CREDENTIAL_INFO *target;
2140 if (len < 9 || !cred_decode( cred + 3, 6, (char *)&size ) ||
2141 !size || size % sizeof(WCHAR) || size > INT_MAX)
2143 SetLastError( ERROR_INVALID_PARAMETER );
2146 buflen = sizeof(*target) + size + sizeof(WCHAR);
2147 if (!(target = HeapAlloc( GetProcessHeap(), 0, buflen ))) return FALSE;
2148 if (!cred_decode( cred + 9, len - 6, (char *)(target + 1) ))
2150 HeapFree( GetProcessHeap(), 0, target );
2153 target->UserName = (WCHAR *)(target + 1);
2154 target->UserName[size / sizeof(WCHAR)] = 0;
2155 *type = UsernameTargetCredential;
2159 case BinaryBlobCredential:
2160 FIXME("BinaryBlobCredential not implemented\n");
2163 WARN("unhandled type %u\n", cred[2] - 'A');
2169 /******************************************************************************
2170 * CredIsMarshaledCredentialW [ADVAPI32.@]
2172 * Check, if the name parameter is a marshaled credential, hash or binary blob
2175 * name the name to check
2178 * TRUE: the name parameter is a marshaled credential, hash or binary blob
2179 * FALSE: the name is a plain username
2181 BOOL WINAPI CredIsMarshaledCredentialW(LPCWSTR name)
2183 TRACE("(%s)\n", debugstr_w(name));
2185 if (name && name[0] == '@' && name[1] == '@' && name[2] > 'A' && name[3])
2187 char hash[CERT_HASH_LENGTH + 2];
2188 int len = strlenW(name + 3 );
2191 if ((name[2] - 'A') == CertCredential && (len == 27) && cred_decode(name + 3, len, hash))
2194 if (((name[2] - 'A') == UsernameTargetCredential) &&
2195 (len >= 9) && cred_decode(name + 3, 6, (char *)&size) && size)
2198 if ((name[2] - 'A') == BinaryBlobCredential)
2199 FIXME("BinaryBlobCredential not checked\n");
2201 if ((name[2] - 'A') > BinaryBlobCredential)
2202 TRACE("unknown type: %d\n", (name[2] - 'A'));
2205 SetLastError(ERROR_INVALID_PARAMETER);
2209 /******************************************************************************
2210 * CredIsMarshaledCredentialA [ADVAPI32.@]
2212 * See CredIsMarshaledCredentialW
2215 BOOL WINAPI CredIsMarshaledCredentialA(LPCSTR name)
2217 LPWSTR nameW = NULL;
2221 TRACE("(%s)\n", debugstr_a(name));
2225 len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
2226 nameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2227 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, len);
2230 res = CredIsMarshaledCredentialW(nameW);
2231 HeapFree(GetProcessHeap(), 0, nameW);