d3d10core: Implement d3d10_device_OMGetRenderTargets().
[wine.git] / dlls / advapi32 / crypt.c
blob2c5e41111db132fcf403d2e6746a095934599884
1 /*
2 * Copyright 1999 Ian Schmidt
3 * Copyright 2001 Travis Michielsen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /***********************************************************************
22 * TODO:
23 * - Reference counting
24 * - Thread-safing
27 #include "config.h"
28 #include "wine/port.h"
30 #include <limits.h>
31 #include <time.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38 #include <fcntl.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
43 #include "ntstatus.h"
44 #define WIN32_NO_STATUS
45 #include "crypt.h"
46 #include "winnls.h"
47 #include "winreg.h"
48 #include "rpc.h"
49 #include "wine/debug.h"
50 #include "wine/unicode.h"
51 #include "winternl.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
55 static HWND crypt_hWindow;
57 #define CRYPT_Alloc(size) (LocalAlloc(LMEM_ZEROINIT, size))
58 #define CRYPT_Free(buffer) (LocalFree(buffer))
60 static inline PWSTR CRYPT_GetProvKeyName(PCWSTR pProvName)
62 static const WCHAR KEYSTR[] = {
63 'S','o','f','t','w','a','r','e','\\',
64 'M','i','c','r','o','s','o','f','t','\\',
65 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
66 'D','e','f','a','u','l','t','s','\\',
67 'P','r','o','v','i','d','e','r','\\',0
69 PWSTR keyname;
71 keyname = CRYPT_Alloc((strlenW(KEYSTR) + strlenW(pProvName) +1)*sizeof(WCHAR));
72 if (keyname)
74 strcpyW(keyname, KEYSTR);
75 strcpyW(keyname + strlenW(KEYSTR), pProvName);
76 } else
77 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
78 return keyname;
81 static inline PWSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
83 static const WCHAR MACHINESTR[] = {
84 'S','o','f','t','w','a','r','e','\\',
85 'M','i','c','r','o','s','o','f','t','\\',
86 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
87 'D','e','f','a','u','l','t','s','\\',
88 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
89 'T','y','p','e',' ','X','X','X',0
91 static const WCHAR USERSTR[] = {
92 'S','o','f','t','w','a','r','e','\\',
93 'M','i','c','r','o','s','o','f','t','\\',
94 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
95 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
97 PWSTR keyname;
98 PWSTR ptr;
100 keyname = CRYPT_Alloc( ((user ? strlenW(USERSTR) : strlenW(MACHINESTR)) +1)*sizeof(WCHAR));
101 if (keyname)
103 user ? strcpyW(keyname, USERSTR) : strcpyW(keyname, MACHINESTR);
104 ptr = keyname + strlenW(keyname);
105 *(--ptr) = (dwType % 10) + '0';
106 *(--ptr) = ((dwType / 10) % 10) + '0';
107 *(--ptr) = (dwType / 100) + '0';
108 } else
109 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
110 return keyname;
113 /* CRYPT_UnicodeToANSI
114 * wstr - unicode string
115 * str - pointer to ANSI string or pointer to null pointer if we have to do the allocation
116 * strsize - size of buffer pointed to by str
118 * returns TRUE if unsuccessful, FALSE otherwise.
119 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
121 static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
123 if (!wstr)
125 *str = NULL;
126 return TRUE;
129 if (!*str)
131 strsize = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
132 *str = CRYPT_Alloc(strsize * sizeof(CHAR));
134 else if (strsize < 0)
136 strsize = INT_MAX; /* windows will pretend that the buffer is infinitely long */
139 if (*str)
141 WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, strsize, NULL, NULL);
142 return TRUE;
145 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
146 return FALSE;
149 /* CRYPT_ANSITOUnicode
150 * str - ANSI string
151 * wstr - pointer to unicode string
152 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
154 static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
156 unsigned int wcount;
158 if (!str)
160 *wstr = NULL;
161 return TRUE;
163 wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
164 if (wstrsize == -1)
165 *wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
166 else
167 wcount = min( wcount, wstrsize/sizeof(WCHAR) );
168 if (*wstr)
170 MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
171 return TRUE;
173 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
174 return FALSE;
177 /* These next 2 functions are used by the VTableProvStruc structure */
178 static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
180 if (!lpszImage || !pData)
182 SetLastError(ERROR_INVALID_PARAMETER);
183 return FALSE;
186 FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
188 return TRUE;
191 static void CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
193 if (phWnd) *phWnd = crypt_hWindow;
196 #define CRYPT_GetProvFunc(name) \
197 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
198 #define CRYPT_GetProvFuncOpt(name) \
199 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
200 static PCRYPTPROV CRYPT_LoadProvider(PCWSTR pImage)
202 PCRYPTPROV provider;
203 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
205 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
206 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
207 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
208 if ( !(provider->hModule = LoadLibraryW(pImage)) )
210 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
211 FIXME("Failed to load dll %s\n", debugstr_w(pImage));
212 goto error;
214 provider->dwMagic = MAGIC_CRYPTPROV;
215 provider->refcount = 1;
217 errorcode = NTE_PROVIDER_DLL_FAIL;
218 CRYPT_GetProvFunc(CPAcquireContext);
219 CRYPT_GetProvFunc(CPCreateHash);
220 CRYPT_GetProvFunc(CPDecrypt);
221 CRYPT_GetProvFunc(CPDeriveKey);
222 CRYPT_GetProvFunc(CPDestroyHash);
223 CRYPT_GetProvFunc(CPDestroyKey);
224 CRYPT_GetProvFuncOpt(CPDuplicateHash);
225 CRYPT_GetProvFuncOpt(CPDuplicateKey);
226 CRYPT_GetProvFunc(CPEncrypt);
227 CRYPT_GetProvFunc(CPExportKey);
228 CRYPT_GetProvFunc(CPGenKey);
229 CRYPT_GetProvFunc(CPGenRandom);
230 CRYPT_GetProvFunc(CPGetHashParam);
231 CRYPT_GetProvFunc(CPGetKeyParam);
232 CRYPT_GetProvFunc(CPGetProvParam);
233 CRYPT_GetProvFunc(CPGetUserKey);
234 CRYPT_GetProvFunc(CPHashData);
235 CRYPT_GetProvFunc(CPHashSessionKey);
236 CRYPT_GetProvFunc(CPImportKey);
237 CRYPT_GetProvFunc(CPReleaseContext);
238 CRYPT_GetProvFunc(CPSetHashParam);
239 CRYPT_GetProvFunc(CPSetKeyParam);
240 CRYPT_GetProvFunc(CPSetProvParam);
241 CRYPT_GetProvFunc(CPSignHash);
242 CRYPT_GetProvFunc(CPVerifySignature);
244 /* FIXME: Not sure what the pbContextInfo field is for.
245 * Does it need memory allocation?
247 provider->pVTable->Version = 3;
248 provider->pVTable->FuncVerifyImage = CRYPT_VerifyImage;
249 provider->pVTable->FuncReturnhWnd = CRYPT_ReturnhWnd;
250 provider->pVTable->dwProvType = 0;
251 provider->pVTable->pbContextInfo = NULL;
252 provider->pVTable->cbContextInfo = 0;
253 provider->pVTable->pszProvName = NULL;
254 return provider;
256 error:
257 SetLastError(errorcode);
258 if (provider)
260 provider->dwMagic = 0;
261 if (provider->hModule)
262 FreeLibrary(provider->hModule);
263 CRYPT_Free(provider->pVTable);
264 CRYPT_Free(provider->pFuncs);
265 CRYPT_Free(provider);
267 return NULL;
269 #undef CRYPT_GetProvFunc
270 #undef CRYPT_GetProvFuncOpt
273 static void CRYPT_CreateMachineGuid(void)
275 static const WCHAR cryptographyW[] = {
276 'S','o','f','t','w','a','r','e','\\',
277 'M','i','c','r','o','s','o','f','t','\\',
278 'C','r','y','p','t','o','g','r','a','p','h','y',0 };
279 static const WCHAR machineGuidW[] = {
280 'M','a','c','h','i','n','e','G','u','i','d',0 };
281 LONG r;
282 HKEY key;
284 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, cryptographyW, 0, KEY_ALL_ACCESS,
285 &key);
286 if (!r)
288 DWORD size;
290 r = RegQueryValueExW(key, machineGuidW, NULL, NULL, NULL, &size);
291 if (r == ERROR_FILE_NOT_FOUND)
293 UUID uuid;
294 WCHAR buf[37];
295 RPC_STATUS rs;
296 static const WCHAR uuidFmt[] = {
297 '%','0','8','x','-','%','0','4','x','-',
298 '%','0','4','x','-','%','0','2','x',
299 '%','0','2','x','-','%','0','2','x',
300 '%','0','2','x','%','0','2','x',
301 '%','0','2','x','%','0','2','x',
302 '%','0','2','x',0 };
304 rs = UuidCreate(&uuid);
305 if (rs == S_OK)
307 sprintfW(buf, uuidFmt,
308 uuid.Data1, uuid.Data2, uuid.Data3,
309 uuid.Data4[0], uuid.Data4[1],
310 uuid.Data4[2], uuid.Data4[3],
311 uuid.Data4[4], uuid.Data4[5],
312 uuid.Data4[6], uuid.Data4[7] );
313 RegSetValueExW(key, machineGuidW, 0, REG_SZ,
314 (const BYTE *)buf,
315 (lstrlenW(buf)+1)*sizeof(WCHAR));
318 RegCloseKey(key);
322 /******************************************************************************
323 * CryptAcquireContextW (ADVAPI32.@)
325 * Acquire a crypto provider context handle.
327 * PARAMS
328 * phProv [O] Pointer to HCRYPTPROV for the output.
329 * pszContainer [I] Key Container Name
330 * pszProvider [I] Cryptographic Service Provider Name
331 * dwProvType [I] Crypto provider type to get a handle.
332 * dwFlags [I] flags for the operation
334 * RETURNS
335 * TRUE on success, FALSE on failure.
337 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
338 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
340 PCRYPTPROV pProv = NULL;
341 HKEY key;
342 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
343 PSTR provnameA = NULL, pszContainerA = NULL;
344 DWORD keytype, type, len;
345 ULONG r;
346 static const WCHAR nameW[] = {'N','a','m','e',0};
347 static const WCHAR typeW[] = {'T','y','p','e',0};
348 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
350 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
351 debugstr_w(pszProvider), dwProvType, dwFlags);
353 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
355 SetLastError(NTE_BAD_PROV_TYPE);
356 return FALSE;
359 if (!phProv)
361 SetLastError(ERROR_INVALID_PARAMETER);
362 return FALSE;
365 /* Make sure the MachineGuid value exists */
366 CRYPT_CreateMachineGuid();
368 if (!pszProvider || !*pszProvider)
370 /* No CSP name specified so try the user default CSP first
371 * then try the machine default CSP
373 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
374 TRACE("No provider registered for crypto provider type %d.\n", dwProvType);
375 SetLastError(NTE_PROV_TYPE_NOT_DEF);
376 return FALSE;
378 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
380 CRYPT_Free(keyname);
381 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
382 TRACE("No type registered for crypto provider type %d.\n", dwProvType);
383 RegCloseKey(key);
384 SetLastError(NTE_PROV_TYPE_NOT_DEF);
385 goto error;
387 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
388 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
389 CRYPT_Free(keyname);
390 RegCloseKey(key);
391 SetLastError(NTE_PROV_TYPE_NOT_DEF);
392 goto error;
395 CRYPT_Free(keyname);
396 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
397 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
399 TRACE("error %d reading size of 'Name' from registry\n", r );
400 RegCloseKey(key);
401 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
402 goto error;
404 if(!(provname = CRYPT_Alloc(len)))
406 RegCloseKey(key);
407 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
408 goto error;
410 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
411 if( r != ERROR_SUCCESS )
413 TRACE("error %d reading 'Name' from registry\n", r );
414 RegCloseKey(key);
415 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
416 goto error;
418 RegCloseKey(key);
419 } else {
420 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
422 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
423 goto error;
425 strcpyW(provname, pszProvider);
428 keyname = CRYPT_GetProvKeyName(provname);
429 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
430 CRYPT_Free(keyname);
431 if (r != ERROR_SUCCESS)
433 SetLastError(NTE_KEYSET_NOT_DEF);
434 goto error;
436 len = sizeof(DWORD);
437 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
438 if (r != ERROR_SUCCESS)
440 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
441 goto error;
443 if (type != dwProvType)
445 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
446 SetLastError(NTE_PROV_TYPE_NO_MATCH);
447 goto error;
450 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
451 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
453 TRACE("error %d reading size of 'Image Path' from registry\n", r );
454 RegCloseKey(key);
455 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
456 goto error;
458 if (!(temp = CRYPT_Alloc(len)))
460 RegCloseKey(key);
461 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
462 goto error;
464 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
465 if( r != ERROR_SUCCESS )
467 TRACE("error %d reading 'Image Path' from registry\n", r );
468 RegCloseKey(key);
469 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
470 goto error;
472 RegCloseKey(key);
473 len = ExpandEnvironmentStringsW(temp, NULL, 0);
474 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
476 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
477 goto error;
479 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
481 /* ExpandEnvironmentStrings will call SetLastError */
482 goto error;
484 pProv = CRYPT_LoadProvider(imagepath);
485 if (!pProv) {
486 /* CRYPT_LoadProvider calls SetLastError */
487 goto error;
489 pProv->pVTable->dwProvType = dwProvType;
490 if(!CRYPT_UnicodeToANSI(provname, &provnameA, 0))
492 /* CRYPT_UnicodeToANSI calls SetLastError */
493 goto error;
495 pProv->pVTable->pszProvName = provnameA;
496 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, 0))
498 /* CRYPT_UnicodeToANSI calls SetLastError */
499 goto error;
501 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
503 /* MSDN: When this flag is set, the value returned in phProv is undefined,
504 * and thus, the CryptReleaseContext function need not be called afterwards.
505 * Therefore, we must clean up everything now.
507 if (dwFlags & CRYPT_DELETEKEYSET)
509 pProv->dwMagic = 0;
510 FreeLibrary(pProv->hModule);
511 CRYPT_Free(provnameA);
512 CRYPT_Free(pProv->pVTable);
513 CRYPT_Free(pProv->pFuncs);
514 CRYPT_Free(pProv);
515 } else {
516 *phProv = (HCRYPTPROV)pProv;
518 CRYPT_Free(pszContainerA);
519 CRYPT_Free(provname);
520 CRYPT_Free(temp);
521 CRYPT_Free(imagepath);
522 return TRUE;
524 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
525 error:
526 if (pProv)
528 pProv->dwMagic = 0;
529 if (pProv->hModule)
530 FreeLibrary(pProv->hModule);
531 CRYPT_Free(pProv->pVTable);
532 CRYPT_Free(pProv->pFuncs);
533 CRYPT_Free(pProv);
535 CRYPT_Free(pszContainerA);
536 CRYPT_Free(provnameA);
537 CRYPT_Free(provname);
538 CRYPT_Free(temp);
539 CRYPT_Free(imagepath);
540 return FALSE;
543 /******************************************************************************
544 * CryptAcquireContextA (ADVAPI32.@)
546 * See CryptAcquireContextW.
548 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
549 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
551 PWSTR pProvider = NULL, pContainer = NULL;
552 BOOL ret = FALSE;
554 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_a(pszContainer),
555 debugstr_a(pszProvider), dwProvType, dwFlags);
557 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
559 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
560 return FALSE;
562 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
564 CRYPT_Free(pContainer);
565 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
566 return FALSE;
569 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
571 CRYPT_Free(pContainer);
572 CRYPT_Free(pProvider);
574 return ret;
577 /******************************************************************************
578 * CryptContextAddRef (ADVAPI32.@)
580 * Increases reference count of a cryptographic service provider handle
581 * by one.
583 * PARAMS
584 * hProv [I] Handle to the CSP whose reference is being incremented.
585 * pdwReserved [IN] Reserved for future use and must be NULL.
586 * dwFlags [I] Reserved for future use and must be NULL.
588 * RETURNS
589 * Success: TRUE
590 * Failure: FALSE
592 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
594 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
596 TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
598 if (!pProv)
600 SetLastError(NTE_BAD_UID);
601 return FALSE;
604 if (pProv->dwMagic != MAGIC_CRYPTPROV)
606 SetLastError(ERROR_INVALID_PARAMETER);
607 return FALSE;
610 pProv->refcount++;
611 return TRUE;
614 /******************************************************************************
615 * CryptReleaseContext (ADVAPI32.@)
617 * Releases the handle of a CSP. Reference count is decreased.
619 * PARAMS
620 * hProv [I] Handle of a CSP.
621 * dwFlags [I] Reserved for future use and must be NULL.
623 * RETURNS
624 * Success: TRUE
625 * Failure: FALSE
627 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, ULONG_PTR dwFlags)
629 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
630 BOOL ret = TRUE;
632 TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
634 if (!pProv)
636 SetLastError(NTE_BAD_UID);
637 return FALSE;
640 if (pProv->dwMagic != MAGIC_CRYPTPROV)
642 SetLastError(ERROR_INVALID_PARAMETER);
643 return FALSE;
646 pProv->refcount--;
647 if (pProv->refcount <= 0)
649 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
650 pProv->dwMagic = 0;
651 FreeLibrary(pProv->hModule);
652 #if 0
653 CRYPT_Free(pProv->pVTable->pContextInfo);
654 #endif
655 CRYPT_Free(pProv->pVTable->pszProvName);
656 CRYPT_Free(pProv->pVTable);
657 CRYPT_Free(pProv->pFuncs);
658 CRYPT_Free(pProv);
660 return ret;
663 /******************************************************************************
664 * CryptGenRandom (ADVAPI32.@)
666 * Fills a buffer with cryptographically random bytes.
668 * PARAMS
669 * hProv [I] Handle of a CSP.
670 * dwLen [I] Number of bytes to generate.
671 * pbBuffer [I/O] Buffer to contain random bytes.
673 * RETURNS
674 * Success: TRUE
675 * Failure: FALSE
677 * NOTES
678 * pdBuffer must be at least dwLen bytes long.
680 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
682 PCRYPTPROV prov = (PCRYPTPROV)hProv;
684 TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
686 if (!hProv)
688 SetLastError(ERROR_INVALID_HANDLE);
689 return FALSE;
692 if (prov->dwMagic != MAGIC_CRYPTPROV)
694 SetLastError(ERROR_INVALID_PARAMETER);
695 return FALSE;
698 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
701 /******************************************************************************
702 * CryptCreateHash (ADVAPI32.@)
704 * Initiates the hashing of a stream of data.
706 * PARAMS
707 * hProv [I] Handle of a CSP.
708 * Algid [I] Identifies the hash algorithm to use.
709 * hKey [I] Key for the hash (if required).
710 * dwFlags [I] Reserved for future use and must be NULL.
711 * phHash [O] Address of the future handle to the new hash object.
713 * RETURNS
714 * Success: TRUE
715 * Failure: FALSE
717 * NOTES
718 * If the algorithm is a keyed hash, hKey is the key.
720 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
721 DWORD dwFlags, HCRYPTHASH *phHash)
723 PCRYPTPROV prov = (PCRYPTPROV)hProv;
724 PCRYPTKEY key = (PCRYPTKEY)hKey;
725 PCRYPTHASH hash;
727 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
729 if (!prov || !phHash || prov->dwMagic != MAGIC_CRYPTPROV ||
730 (key && key->dwMagic != MAGIC_CRYPTKEY))
732 SetLastError(ERROR_INVALID_PARAMETER);
733 return FALSE;
735 if (dwFlags)
737 SetLastError(NTE_BAD_FLAGS);
738 return FALSE;
740 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
742 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
743 return FALSE;
746 hash->pProvider = prov;
747 hash->dwMagic = MAGIC_CRYPTHASH;
748 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
749 key ? key->hPrivate : 0, 0, &hash->hPrivate))
751 *phHash = (HCRYPTHASH)hash;
752 return TRUE;
755 /* CSP error! */
756 hash->dwMagic = 0;
757 CRYPT_Free(hash);
758 *phHash = 0;
759 return FALSE;
762 /******************************************************************************
763 * CryptDecrypt (ADVAPI32.@)
765 * Decrypts data encrypted by CryptEncrypt.
767 * PARAMS
768 * hKey [I] Handle to the decryption key.
769 * hHash [I] Handle to a hash object.
770 * Final [I] TRUE if this is the last section to be decrypted.
771 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
772 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
773 * data on return
774 * pdwDataLen [I/O] Length of pbData before and after the call.
776 * RETURNS
777 * Success: TRUE
778 * Failure: FALSE
780 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
781 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
783 PCRYPTPROV prov;
784 PCRYPTKEY key = (PCRYPTKEY)hKey;
785 PCRYPTHASH hash = (PCRYPTHASH)hHash;
787 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
789 if (!key || !pbData || !pdwDataLen ||
790 !key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
791 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
793 SetLastError(ERROR_INVALID_PARAMETER);
794 return FALSE;
797 prov = key->pProvider;
798 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
799 Final, dwFlags, pbData, pdwDataLen);
802 /******************************************************************************
803 * CryptDeriveKey (ADVAPI32.@)
805 * Generates session keys derived from a base data value.
807 * PARAMS
808 * hProv [I] Handle to a CSP.
809 * Algid [I] Identifies the symmetric encryption algorithm to use.
810 * hBaseData [I] Handle to a hash object.
811 * dwFlags [I] Type of key to generate.
812 * phKey [I/O] Address of the newly generated key.
814 * RETURNS
815 * Success: TRUE
816 * Failure: FALSE
818 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
819 DWORD dwFlags, HCRYPTKEY *phKey)
821 PCRYPTPROV prov = (PCRYPTPROV)hProv;
822 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
823 PCRYPTKEY key;
825 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
827 if (!prov || !hash)
829 SetLastError(ERROR_INVALID_HANDLE);
830 return FALSE;
832 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV || hash->dwMagic != MAGIC_CRYPTHASH)
834 SetLastError(ERROR_INVALID_PARAMETER);
835 return FALSE;
837 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
839 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
840 return FALSE;
843 key->pProvider = prov;
844 key->dwMagic = MAGIC_CRYPTKEY;
845 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
847 *phKey = (HCRYPTKEY)key;
848 return TRUE;
851 /* CSP error! */
852 key->dwMagic = 0;
853 CRYPT_Free(key);
854 *phKey = 0;
855 return FALSE;
858 /******************************************************************************
859 * CryptDestroyHash (ADVAPI32.@)
861 * Destroys the hash object referenced by hHash.
863 * PARAMS
864 * hHash [I] Handle of the hash object to be destroyed.
866 * RETURNS
867 * Success: TRUE
868 * Failure: FALSE
870 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
872 PCRYPTHASH hash = (PCRYPTHASH)hHash;
873 PCRYPTPROV prov;
874 BOOL ret;
876 TRACE("(0x%lx)\n", hHash);
878 if (!hash)
880 SetLastError(ERROR_INVALID_HANDLE);
881 return FALSE;
884 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
885 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
887 SetLastError(ERROR_INVALID_PARAMETER);
888 return FALSE;
891 prov = hash->pProvider;
892 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
893 hash->dwMagic = 0;
894 CRYPT_Free(hash);
895 return ret;
898 /******************************************************************************
899 * CryptDestroyKey (ADVAPI32.@)
901 * Releases the handle referenced by hKey.
903 * PARAMS
904 * hKey [I] Handle of the key to be destroyed.
906 * RETURNS
907 * Success: TRUE
908 * Failure: FALSE
910 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
912 PCRYPTKEY key = (PCRYPTKEY)hKey;
913 PCRYPTPROV prov;
914 BOOL ret;
916 TRACE("(0x%lx)\n", hKey);
918 if (!key)
920 SetLastError(ERROR_INVALID_HANDLE);
921 return FALSE;
924 if (!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
925 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
927 SetLastError(ERROR_INVALID_PARAMETER);
928 return FALSE;
931 prov = key->pProvider;
932 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
933 key->dwMagic = 0;
934 CRYPT_Free(key);
935 return ret;
938 /******************************************************************************
939 * CryptDuplicateHash (ADVAPI32.@)
941 * Duplicates a hash.
943 * PARAMS
944 * hHash [I] Handle to the hash to be copied.
945 * pdwReserved [I] Reserved for future use and must be zero.
946 * dwFlags [I] Reserved for future use and must be zero.
947 * phHash [O] Address of the handle to receive the copy.
949 * RETURNS
950 * Success: TRUE
951 * Failure: FALSE
953 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
954 DWORD dwFlags, HCRYPTHASH *phHash)
956 PCRYPTPROV prov;
957 PCRYPTHASH orghash, newhash;
959 TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
961 orghash = (PCRYPTHASH)hHash;
962 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
963 orghash->dwMagic != MAGIC_CRYPTHASH || orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
965 SetLastError(ERROR_INVALID_PARAMETER);
966 return FALSE;
969 prov = orghash->pProvider;
970 if (!prov->pFuncs->pCPDuplicateHash)
972 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
973 return FALSE;
976 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
978 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
979 return FALSE;
982 newhash->pProvider = prov;
983 newhash->dwMagic = MAGIC_CRYPTHASH;
984 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
986 *phHash = (HCRYPTHASH)newhash;
987 return TRUE;
989 newhash->dwMagic = 0;
990 CRYPT_Free(newhash);
991 return FALSE;
994 /******************************************************************************
995 * CryptDuplicateKey (ADVAPI32.@)
997 * Duplicate a key and the key's state.
999 * PARAMS
1000 * hKey [I] Handle of the key to copy.
1001 * pdwReserved [I] Reserved for future use and must be NULL.
1002 * dwFlags [I] Reserved for future use and must be zero.
1003 * phKey [I] Address of the handle to the duplicated key.
1005 * RETURNS
1006 * Success: TRUE
1007 * Failure: FALSE
1009 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
1011 PCRYPTPROV prov;
1012 PCRYPTKEY orgkey, newkey;
1014 TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1016 orgkey = (PCRYPTKEY)hKey;
1017 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
1018 orgkey->dwMagic != MAGIC_CRYPTKEY ||
1019 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
1021 SetLastError(ERROR_INVALID_PARAMETER);
1022 return FALSE;
1025 prov = orgkey->pProvider;
1026 if (!prov->pFuncs->pCPDuplicateKey)
1028 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1029 return FALSE;
1032 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1034 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1035 return FALSE;
1038 newkey->pProvider = prov;
1039 newkey->dwMagic = MAGIC_CRYPTKEY;
1040 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
1042 *phKey = (HCRYPTKEY)newkey;
1043 return TRUE;
1045 newkey->dwMagic = 0;
1046 CRYPT_Free(newkey);
1047 return FALSE;
1050 /******************************************************************************
1051 * CryptEncrypt (ADVAPI32.@)
1053 * Encrypts data.
1055 * PARAMS
1056 * hKey [I] Handle to the encryption key.
1057 * hHash [I] Handle to a hash object.
1058 * Final [I] TRUE if this is the last section to encrypt.
1059 * dwFlags [I] Can be CRYPT_OAEP.
1060 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1061 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1062 * encrypted data after call.
1063 * dwBufLen [I] Length of the input pbData buffer.
1065 * RETURNS
1066 * Success: TRUE
1067 * Failure: FALSE
1069 * NOTES
1070 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1071 * required for the returned data in pdwDataLen.
1073 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1074 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1076 PCRYPTPROV prov;
1077 PCRYPTKEY key = (PCRYPTKEY)hKey;
1078 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1080 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1082 if (!key || !pdwDataLen || !key->pProvider ||
1083 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1085 SetLastError(ERROR_INVALID_PARAMETER);
1086 return FALSE;
1089 prov = key->pProvider;
1090 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
1091 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1094 /******************************************************************************
1095 * CryptEnumProvidersW (ADVAPI32.@)
1097 * Returns the next available CSP.
1099 * PARAMS
1100 * dwIndex [I] Index of the next provider to be enumerated.
1101 * pdwReserved [I] Reserved for future use and must be NULL.
1102 * dwFlags [I] Reserved for future use and must be zero.
1103 * pdwProvType [O] DWORD designating the type of the provider.
1104 * pszProvName [O] Buffer that receives data from the provider.
1105 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1106 * of bytes stored in the buffer on return.
1108 * RETURNS
1109 * Success: TRUE
1110 * Failure: FALSE
1112 * NOTES
1113 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1114 * for memory allocation purposes.
1116 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
1117 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1119 HKEY hKey;
1120 static const WCHAR providerW[] = {
1121 'S','o','f','t','w','a','r','e','\\',
1122 'M','i','c','r','o','s','o','f','t','\\',
1123 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1124 'D','e','f','a','u','l','t','s','\\',
1125 'P','r','o','v','i','d','e','r',0
1127 static const WCHAR typeW[] = {'T','y','p','e',0};
1128 BOOL ret;
1130 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1131 pdwProvType, pszProvName, pcbProvName);
1133 if (pdwReserved || !pcbProvName)
1135 SetLastError(ERROR_INVALID_PARAMETER);
1136 return FALSE;
1138 if (dwFlags)
1140 SetLastError(NTE_BAD_FLAGS);
1141 return FALSE;
1144 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1146 SetLastError(NTE_FAIL);
1147 return FALSE;
1150 ret = TRUE;
1151 if (!pszProvName)
1153 DWORD numkeys;
1154 WCHAR *provNameW;
1156 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1157 NULL, NULL, NULL, NULL, NULL, NULL);
1159 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1161 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1162 RegCloseKey(hKey);
1163 return FALSE;
1166 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1167 CRYPT_Free(provNameW);
1168 (*pcbProvName)++;
1169 *pcbProvName *= sizeof(WCHAR);
1171 if (dwIndex >= numkeys)
1173 SetLastError(ERROR_NO_MORE_ITEMS);
1174 ret = FALSE;
1176 } else {
1177 DWORD size = sizeof(DWORD);
1178 DWORD result;
1179 HKEY subkey;
1181 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1182 if (result)
1184 SetLastError(result);
1185 RegCloseKey(hKey);
1186 return FALSE;
1188 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1190 RegCloseKey(hKey);
1191 return FALSE;
1194 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1195 ret = FALSE;
1197 RegCloseKey(subkey);
1199 RegCloseKey(hKey);
1200 return ret;
1203 /******************************************************************************
1204 * CryptEnumProvidersA (ADVAPI32.@)
1206 * See CryptEnumProvidersW.
1208 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1209 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1211 PWSTR str = NULL;
1212 DWORD bufsize;
1213 BOOL ret;
1215 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1216 pdwProvType, pszProvName, pcbProvName);
1218 if(!CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1219 return FALSE;
1220 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1222 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1223 return FALSE;
1225 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1226 if (str)
1227 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1228 *pcbProvName = bufsize / sizeof(WCHAR); /* FIXME: not correct */
1229 if (str)
1231 CRYPT_Free(str);
1232 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1234 SetLastError(ERROR_MORE_DATA);
1235 return FALSE;
1238 return ret;
1241 /******************************************************************************
1242 * CryptEnumProviderTypesW (ADVAPI32.@)
1244 * Retrieves the next type of CSP supported.
1246 * PARAMS
1247 * dwIndex [I] Index of the next provider to be enumerated.
1248 * pdwReserved [I] Reserved for future use and must be NULL.
1249 * dwFlags [I] Reserved for future use and must be zero.
1250 * pdwProvType [O] DWORD designating the type of the provider.
1251 * pszTypeName [O] Buffer that receives data from the provider type.
1252 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1253 * of bytes stored in the buffer on return.
1255 * RETURNS
1256 * Success: TRUE
1257 * Failure: FALSE
1259 * NOTES
1260 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1261 * for memory allocation purposes.
1263 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1264 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1266 HKEY hKey, hSubkey;
1267 DWORD keylen, numkeys, dwType;
1268 PWSTR keyname, ch;
1269 DWORD result;
1270 static const WCHAR KEYSTR[] = {
1271 'S','o','f','t','w','a','r','e','\\',
1272 'M','i','c','r','o','s','o','f','t','\\',
1273 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1274 'D','e','f','a','u','l','t','s','\\',
1275 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1277 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1279 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1280 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1282 if (pdwReserved || !pdwProvType || !pcbTypeName)
1284 SetLastError(ERROR_INVALID_PARAMETER);
1285 return FALSE;
1287 if (dwFlags)
1289 SetLastError(NTE_BAD_FLAGS);
1290 return FALSE;
1293 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1294 return FALSE;
1296 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1297 if (dwIndex >= numkeys)
1299 SetLastError(ERROR_NO_MORE_ITEMS);
1300 RegCloseKey(hKey);
1301 return FALSE;
1303 keylen++;
1304 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1306 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1307 RegCloseKey(hKey);
1308 return FALSE;
1310 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1311 CRYPT_Free(keyname);
1312 RegCloseKey(hKey);
1313 return FALSE;
1315 RegOpenKeyW(hKey, keyname, &hSubkey);
1316 RegCloseKey(hKey);
1318 ch = keyname + strlenW(keyname);
1319 /* Convert "Type 000" to 0, etc/ */
1320 *pdwProvType = *(--ch) - '0';
1321 *pdwProvType += (*(--ch) - '0') * 10;
1322 *pdwProvType += (*(--ch) - '0') * 100;
1323 CRYPT_Free(keyname);
1325 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1326 if (result)
1328 SetLastError(result);
1329 RegCloseKey(hSubkey);
1330 return FALSE;
1333 RegCloseKey(hSubkey);
1334 return TRUE;
1337 /******************************************************************************
1338 * CryptEnumProviderTypesA (ADVAPI32.@)
1340 * See CryptEnumProviderTypesW.
1342 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1343 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1345 PWSTR str = NULL;
1346 DWORD bufsize;
1347 BOOL ret;
1349 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1350 pdwProvType, pszTypeName, pcbTypeName);
1352 if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1353 return FALSE;
1354 if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1356 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1357 return FALSE;
1359 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1360 if (str)
1361 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1362 *pcbTypeName = bufsize / sizeof(WCHAR);
1363 if (str)
1365 CRYPT_Free(str);
1366 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1368 SetLastError(ERROR_MORE_DATA);
1369 return FALSE;
1372 return ret;
1375 /******************************************************************************
1376 * CryptExportKey (ADVAPI32.@)
1378 * Exports a cryptographic key from a CSP.
1380 * PARAMS
1381 * hKey [I] Handle to the key to export.
1382 * hExpKey [I] Handle to a cryptographic key of the end user.
1383 * dwBlobType [I] Type of BLOB to be exported.
1384 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1385 * pbData [O] Buffer to receive BLOB data.
1386 * pdwDataLen [I/O] Specifies the size of pbData.
1388 * RETURNS
1389 * Success: TRUE
1390 * Failure: FALSE
1392 * NOTES
1393 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1394 * buffer needed to hold the BLOB.
1396 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1397 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1399 PCRYPTPROV prov;
1400 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1402 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1404 if (!key || !pdwDataLen || !key->pProvider ||
1405 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1407 SetLastError(ERROR_INVALID_PARAMETER);
1408 return FALSE;
1411 prov = key->pProvider;
1412 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1413 dwBlobType, dwFlags, pbData, pdwDataLen);
1416 /******************************************************************************
1417 * CryptGenKey (ADVAPI32.@)
1419 * Generates a random cryptographic session key or a pub/priv key pair.
1421 * PARAMS
1422 * hProv [I] Handle to a CSP.
1423 * Algid [I] Algorithm to use to make key.
1424 * dwFlags [I] Specifies type of key to make.
1425 * phKey [I] Address of the handle to which the new key is copied.
1427 * RETURNS
1428 * Success: TRUE
1429 * Failure: FALSE
1431 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1433 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1434 PCRYPTKEY key;
1436 TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1438 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1440 SetLastError(ERROR_INVALID_PARAMETER);
1441 return FALSE;
1443 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1445 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1446 return FALSE;
1449 key->pProvider = prov;
1450 key->dwMagic = MAGIC_CRYPTKEY;
1451 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1453 *phKey = (HCRYPTKEY)key;
1454 return TRUE;
1457 /* CSP error! */
1458 key->dwMagic = 0;
1459 CRYPT_Free(key);
1460 return FALSE;
1463 /******************************************************************************
1464 * CryptGetDefaultProviderW (ADVAPI32.@)
1466 * Finds the default CSP of a certain provider type.
1468 * PARAMS
1469 * dwProvType [I] Provider type to look for.
1470 * pdwReserved [I] Reserved for future use and must be NULL.
1471 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1472 * pszProvName [O] Name of the default CSP.
1473 * pcbProvName [I/O] Size of pszProvName
1475 * RETURNS
1476 * Success: TRUE
1477 * Failure: FALSE
1479 * NOTES
1480 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1481 * memory allocation purposes on return.
1483 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1484 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1486 HKEY hKey;
1487 PWSTR keyname;
1488 DWORD result;
1489 static const WCHAR nameW[] = {'N','a','m','e',0};
1491 if (pdwReserved || !pcbProvName)
1493 SetLastError(ERROR_INVALID_PARAMETER);
1494 return FALSE;
1496 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1498 SetLastError(NTE_BAD_FLAGS);
1499 return FALSE;
1501 if (dwProvType > 999)
1503 SetLastError(NTE_BAD_PROV_TYPE);
1504 return FALSE;
1506 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1508 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1509 return FALSE;
1511 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1513 CRYPT_Free(keyname);
1514 SetLastError(NTE_PROV_TYPE_NOT_DEF);
1515 return FALSE;
1517 CRYPT_Free(keyname);
1519 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1520 RegCloseKey(hKey);
1522 if (result)
1524 if (result != ERROR_MORE_DATA)
1525 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1526 else
1527 SetLastError(result);
1529 return FALSE;
1532 return TRUE;
1535 /******************************************************************************
1536 * CryptGetDefaultProviderA (ADVAPI32.@)
1538 * See CryptGetDefaultProviderW.
1540 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1541 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1543 PWSTR str = NULL;
1544 DWORD bufsize;
1545 BOOL ret;
1547 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1549 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
1550 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1552 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1553 return FALSE;
1555 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1556 if (str)
1557 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1558 *pcbProvName = bufsize / sizeof(WCHAR);
1559 if (str)
1561 CRYPT_Free(str);
1562 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1564 SetLastError(ERROR_MORE_DATA);
1565 return FALSE;
1568 return ret;
1571 /******************************************************************************
1572 * CryptGetHashParam (ADVAPI32.@)
1574 * Retrieves data that controls the operations of a hash object.
1576 * PARAMS
1577 * hHash [I] Handle of the hash object to question.
1578 * dwParam [I] Query type.
1579 * pbData [O] Buffer that receives the value data.
1580 * pdwDataLen [I/O] Size of the pbData buffer.
1581 * dwFlags [I] Reserved for future use and must be zero.
1583 * RETURNS
1584 * Success: TRUE
1585 * Failure: FALSE
1587 * NOTES
1588 * If pbData is NULL, pdwDataLen will contain the length required.
1590 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1591 DWORD *pdwDataLen, DWORD dwFlags)
1593 PCRYPTPROV prov;
1594 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1596 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1598 if (!hash || !pdwDataLen || !hash->pProvider ||
1599 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1601 SetLastError(ERROR_INVALID_PARAMETER);
1602 return FALSE;
1605 prov = hash->pProvider;
1606 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1607 pbData, pdwDataLen, dwFlags);
1610 /******************************************************************************
1611 * CryptGetKeyParam (ADVAPI32.@)
1613 * Retrieves data that controls the operations of a key.
1615 * PARAMS
1616 * hKey [I] Handle to they key in question.
1617 * dwParam [I] Specifies query type.
1618 * pbData [O] Sequence of bytes to receive data.
1619 * pdwDataLen [I/O] Size of pbData.
1620 * dwFlags [I] Reserved for future use and must be zero.
1622 * RETURNS
1623 * Success: TRUE
1624 * Failure: FALSE
1626 * NOTES
1627 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1629 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1630 DWORD *pdwDataLen, DWORD dwFlags)
1632 PCRYPTPROV prov;
1633 PCRYPTKEY key = (PCRYPTKEY)hKey;
1635 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1637 if (!key || !pdwDataLen || !key->pProvider ||
1638 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1640 SetLastError(ERROR_INVALID_PARAMETER);
1641 return FALSE;
1644 prov = key->pProvider;
1645 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1646 pbData, pdwDataLen, dwFlags);
1649 /******************************************************************************
1650 * CryptGetProvParam (ADVAPI32.@)
1652 * Retrieves parameters that control the operations of a CSP.
1654 * PARAMS
1655 * hProv [I] Handle of the CSP in question.
1656 * dwParam [I] Specifies query type.
1657 * pbData [O] Buffer to receive the data.
1658 * pdwDataLen [I/O] Size of pbData.
1659 * dwFlags [I] see MSDN Docs.
1661 * RETURNS
1662 * Success: TRUE
1663 * Failure: FALSE
1665 * NOTES
1666 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1668 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1669 DWORD *pdwDataLen, DWORD dwFlags)
1671 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1673 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1675 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1677 SetLastError(ERROR_INVALID_PARAMETER);
1678 return FALSE;
1681 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1684 /******************************************************************************
1685 * CryptGetUserKey (ADVAPI32.@)
1687 * Gets a handle of one of a user's two public/private key pairs.
1689 * PARAMS
1690 * hProv [I] Handle of a CSP.
1691 * dwKeySpec [I] Private key to use.
1692 * phUserKey [O] Pointer to the handle of the retrieved keys.
1694 * RETURNS
1695 * Success: TRUE
1696 * Failure: FALSE
1698 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1700 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1701 PCRYPTKEY key;
1703 TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1705 if (!prov)
1707 SetLastError(ERROR_INVALID_HANDLE);
1708 return FALSE;
1710 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1712 SetLastError(ERROR_INVALID_PARAMETER);
1713 return FALSE;
1715 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1717 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1718 return FALSE;
1721 key->pProvider = prov;
1722 key->dwMagic = MAGIC_CRYPTKEY;
1723 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1725 *phUserKey = (HCRYPTKEY)key;
1726 return TRUE;
1729 /* CSP Error */
1730 key->dwMagic = 0;
1731 CRYPT_Free(key);
1732 *phUserKey = 0;
1733 return FALSE;
1736 /******************************************************************************
1737 * CryptHashData (ADVAPI32.@)
1739 * Adds data to a hash object.
1741 * PARAMS
1742 * hHash [I] Handle of the hash object.
1743 * pbData [I] Buffer of data to be hashed.
1744 * dwDataLen [I] Number of bytes to add.
1745 * dwFlags [I] Can be CRYPT_USERDATA
1747 * RETURNS
1748 * Success: TRUE
1749 * Failure: FALSE
1751 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1753 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1754 PCRYPTPROV prov;
1756 TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1758 if (!hash)
1760 SetLastError(ERROR_INVALID_HANDLE);
1761 return FALSE;
1763 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1764 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1766 SetLastError(ERROR_INVALID_PARAMETER);
1767 return FALSE;
1770 prov = hash->pProvider;
1771 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1774 /******************************************************************************
1775 * CryptHashSessionKey (ADVAPI32.@)
1777 * Compute the cryptographic hash of a session key object.
1779 * PARAMS
1780 * hHash [I] Handle to the hash object.
1781 * hKey [I] Handle to the key to be hashed.
1782 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1784 * RETURNS
1785 * Success: TRUE
1786 * Failure: FALSE
1788 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1790 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1791 PCRYPTKEY key = (PCRYPTKEY)hKey;
1792 PCRYPTPROV prov;
1794 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1796 if (!hash || !key)
1798 SetLastError(ERROR_INVALID_HANDLE);
1799 return FALSE;
1802 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1803 hash->pProvider->dwMagic != MAGIC_CRYPTPROV || key->dwMagic != MAGIC_CRYPTKEY)
1805 SetLastError(ERROR_INVALID_PARAMETER);
1806 return FALSE;
1809 prov = hash->pProvider;
1810 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1813 /******************************************************************************
1814 * CryptImportKey (ADVAPI32.@)
1816 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1818 * PARAMS
1819 * hProv [I] Handle of a CSP.
1820 * pbData [I] Contains the key to be imported.
1821 * dwDataLen [I] Length of the key.
1822 * hPubKey [I] Cryptographic key that decrypts pdData
1823 * dwFlags [I] Used only with a public/private key pair.
1824 * phKey [O] Imported key.
1826 * RETURNS
1827 * Success: TRUE
1828 * Failure: FALSE
1830 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
1831 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1833 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1834 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1836 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1838 if (!prov || !pbData || !dwDataLen || !phKey ||
1839 prov->dwMagic != MAGIC_CRYPTPROV ||
1840 (pubkey && pubkey->dwMagic != MAGIC_CRYPTKEY))
1842 SetLastError(ERROR_INVALID_PARAMETER);
1843 return FALSE;
1846 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1848 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1849 return FALSE;
1852 importkey->pProvider = prov;
1853 importkey->dwMagic = MAGIC_CRYPTKEY;
1854 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1855 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1857 *phKey = (HCRYPTKEY)importkey;
1858 return TRUE;
1861 importkey->dwMagic = 0;
1862 CRYPT_Free(importkey);
1863 return FALSE;
1866 /******************************************************************************
1867 * CryptSignHashW (ADVAPI32.@)
1869 * Signs data.
1871 * PARAMS
1872 * hHash [I] Handle of the hash object to be signed.
1873 * dwKeySpec [I] Private key to use.
1874 * sDescription [I] Should be NULL.
1875 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1876 * pbSignature [O] Buffer of the signature data.
1877 * pdwSigLen [I/O] Size of the pbSignature buffer.
1879 * RETURNS
1880 * Success: TRUE
1881 * Failure: FALSE
1883 * NOTES
1884 * Because of security flaws sDescription should not be used and should thus be
1885 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1886 * Providers.
1888 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1889 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1891 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1892 PCRYPTPROV prov;
1894 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1895 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1897 if (!hash)
1899 SetLastError(ERROR_INVALID_HANDLE);
1900 return FALSE;
1902 if (!pdwSigLen || !hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1903 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1905 SetLastError(ERROR_INVALID_PARAMETER);
1906 return FALSE;
1909 prov = hash->pProvider;
1910 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1911 dwFlags, pbSignature, pdwSigLen);
1914 /******************************************************************************
1915 * CryptSignHashA (ADVAPI32.@)
1917 * See CryptSignHashW.
1919 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1920 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1922 LPWSTR wsDescription;
1923 BOOL result;
1925 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1926 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1928 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1929 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1930 CRYPT_Free(wsDescription);
1932 return result;
1935 /******************************************************************************
1936 * CryptSetHashParam (ADVAPI32.@)
1938 * Customizes the operations of a hash object.
1940 * PARAMS
1941 * hHash [I] Handle of the hash object to set parameters.
1942 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1943 * pbData [I] Value data buffer.
1944 * dwFlags [I] Reserved for future use and must be zero.
1946 * RETURNS
1947 * Success: TRUE
1948 * Failure: FALSE
1950 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, const BYTE *pbData, DWORD dwFlags)
1952 PCRYPTPROV prov;
1953 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1955 TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1957 if (!hash || !pbData || !hash->pProvider ||
1958 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1960 SetLastError(ERROR_INVALID_PARAMETER);
1961 return FALSE;
1964 prov = hash->pProvider;
1965 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1966 dwParam, pbData, dwFlags);
1969 /******************************************************************************
1970 * CryptSetKeyParam (ADVAPI32.@)
1972 * Customizes a session key's operations.
1974 * PARAMS
1975 * hKey [I] Handle to the key to set values.
1976 * dwParam [I] See MSDN Doc.
1977 * pbData [I] Buffer of values to set.
1978 * dwFlags [I] Only used when dwParam == KP_ALGID.
1980 * RETURNS
1981 * Success: TRUE
1982 * Failure: FALSE
1984 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, const BYTE *pbData, DWORD dwFlags)
1986 PCRYPTPROV prov;
1987 PCRYPTKEY key = (PCRYPTKEY)hKey;
1989 TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
1991 if (!key || !pbData || !key->pProvider ||
1992 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1994 SetLastError(ERROR_INVALID_PARAMETER);
1995 return FALSE;
1998 prov = key->pProvider;
1999 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
2000 dwParam, pbData, dwFlags);
2003 /******************************************************************************
2004 * CryptSetProviderA (ADVAPI32.@)
2006 * Specifies the current user's default CSP.
2008 * PARAMS
2009 * pszProvName [I] Name of the new default CSP.
2010 * dwProvType [I] Provider type of the CSP.
2012 * RETURNS
2013 * Success: TRUE
2014 * Failure: FALSE
2016 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
2018 TRACE("(%s, %d)\n", pszProvName, dwProvType);
2019 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2022 /******************************************************************************
2023 * CryptSetProviderW (ADVAPI32.@)
2025 * See CryptSetProviderA.
2027 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
2029 TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
2030 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2033 /******************************************************************************
2034 * CryptSetProviderExW (ADVAPI32.@)
2036 * Specifies the default CSP.
2038 * PARAMS
2039 * pszProvName [I] Name of the new default CSP.
2040 * dwProvType [I] Provider type of the CSP.
2041 * pdwReserved [I] Reserved for future use and must be NULL.
2042 * dwFlags [I] See MSDN Doc.
2044 * RETURNS
2045 * Success: TRUE
2046 * Failure: FALSE
2048 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2050 HKEY hProvKey, hTypeKey;
2051 PWSTR keyname;
2052 static const WCHAR nameW[] = {'N','a','m','e',0};
2054 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2056 if (!pszProvName || pdwReserved)
2058 SetLastError(ERROR_INVALID_PARAMETER);
2059 return FALSE;
2061 if (dwProvType > MAXPROVTYPES)
2063 SetLastError(NTE_BAD_PROV_TYPE);
2064 return FALSE;
2066 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
2067 || dwFlags == CRYPT_DELETE_DEFAULT)
2069 SetLastError(NTE_BAD_FLAGS);
2070 return FALSE;
2073 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2075 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2076 return FALSE;
2078 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2079 keyname, &hTypeKey))
2081 CRYPT_Free(keyname);
2082 SetLastError(NTE_BAD_PROVIDER);
2083 return FALSE;
2085 CRYPT_Free(keyname);
2087 if (dwFlags & CRYPT_DELETE_DEFAULT)
2089 RegDeleteValueW(hTypeKey, nameW);
2091 else
2093 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
2095 RegCloseKey(hTypeKey);
2096 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2097 return FALSE;
2099 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2100 keyname, &hProvKey))
2102 CRYPT_Free(keyname);
2103 RegCloseKey(hTypeKey);
2104 SetLastError(NTE_BAD_PROVIDER);
2105 return FALSE;
2107 CRYPT_Free(keyname);
2109 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
2110 (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2112 RegCloseKey(hTypeKey);
2113 RegCloseKey(hProvKey);
2114 return FALSE;
2117 RegCloseKey(hProvKey);
2119 RegCloseKey(hTypeKey);
2121 return TRUE;
2124 /******************************************************************************
2125 * CryptSetProviderExA (ADVAPI32.@)
2127 * See CryptSetProviderExW.
2129 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2131 BOOL ret = FALSE;
2132 PWSTR str = NULL;
2134 TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2136 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2138 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2139 CRYPT_Free(str);
2141 return ret;
2144 /******************************************************************************
2145 * CryptSetProvParam (ADVAPI32.@)
2147 * Customizes the operations of a CSP.
2149 * PARAMS
2150 * hProv [I] Handle of a CSP.
2151 * dwParam [I] See MSDN Doc.
2152 * pbData [I] Buffer that contains a value to set as a parameter.
2153 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2155 * RETURNS
2156 * Success: TRUE
2157 * Failure: FALSE
2159 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, const BYTE *pbData, DWORD dwFlags)
2161 PCRYPTPROV prov = (PCRYPTPROV)hProv;
2163 TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2165 if (!prov)
2167 SetLastError(ERROR_INVALID_HANDLE);
2168 return FALSE;
2170 if (prov->dwMagic != MAGIC_CRYPTPROV)
2172 SetLastError(ERROR_INVALID_PARAMETER);
2173 return FALSE;
2175 if (dwParam == PP_USE_HARDWARE_RNG)
2177 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2178 FIXME("\tLetting the CSP decide.\n");
2180 if (dwFlags & PP_CLIENT_HWND)
2182 /* FIXME: Should verify the parameter */
2183 if (pbData /* && IsWindow((HWND)pbData) */)
2185 crypt_hWindow = (HWND)(pbData);
2186 return TRUE;
2187 } else {
2188 SetLastError(ERROR_INVALID_PARAMETER);
2189 return FALSE;
2192 /* All other flags go to the CSP */
2193 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2196 /******************************************************************************
2197 * CryptVerifySignatureW (ADVAPI32.@)
2199 * Verifies the signature of a hash object.
2201 * PARAMS
2202 * hHash [I] Handle of the hash object to verify.
2203 * pbSignature [I] Signature data to verify.
2204 * dwSigLen [I] Size of pbSignature.
2205 * hPubKey [I] Handle to the public key to authenticate signature.
2206 * sDescription [I] Should be NULL.
2207 * dwFlags [I] See MSDN doc.
2209 * RETURNS
2210 * Success: TRUE
2211 * Failure: FALSE
2213 * NOTES
2214 * Because of security flaws sDescription should not be used and should thus be
2215 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2216 * Providers.
2218 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, const BYTE *pbSignature, DWORD dwSigLen,
2219 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2221 PCRYPTHASH hash = (PCRYPTHASH)hHash;
2222 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
2223 PCRYPTPROV prov;
2225 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2226 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2228 if (!hash || !key || key->dwMagic != MAGIC_CRYPTKEY || hash->dwMagic != MAGIC_CRYPTHASH ||
2229 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
2230 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2232 SetLastError(ERROR_INVALID_PARAMETER);
2233 return FALSE;
2236 prov = hash->pProvider;
2237 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2238 key->hPrivate, sDescription, dwFlags);
2241 /******************************************************************************
2242 * CryptVerifySignatureA (ADVAPI32.@)
2244 * See CryptVerifySignatureW.
2246 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, const BYTE *pbSignature, DWORD dwSigLen,
2247 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
2249 LPWSTR wsDescription;
2250 BOOL result;
2252 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2253 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
2255 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
2256 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2257 CRYPT_Free(wsDescription);
2259 return result;
2262 /******************************************************************************
2263 * SystemFunction030 (ADVAPI32.@)
2265 * Tests if two blocks of 16 bytes are equal
2267 * PARAMS
2268 * b1,b2 [I] block of 16 bytes
2270 * RETURNS
2271 * TRUE if blocks are the same
2272 * FALSE if blocks are different
2274 BOOL WINAPI SystemFunction030(LPCVOID b1, LPCVOID b2)
2276 return !memcmp(b1, b2, 0x10);
2279 /******************************************************************************
2280 * SystemFunction035 (ADVAPI32.@)
2282 * Described here:
2283 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2285 * NOTES
2286 * Stub, always return TRUE.
2288 BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
2290 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
2291 return TRUE;
2294 /******************************************************************************
2295 * SystemFunction036 (ADVAPI32.@)
2297 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2299 * PARAMS
2300 * pbBufer [O] Pointer to memory to receive random bytes.
2301 * dwLen [I] Number of random bytes to fetch.
2303 * RETURNS
2304 * Success: TRUE
2305 * Failure: FALSE
2308 BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2310 int dev_random;
2312 dev_random = open("/dev/urandom", O_RDONLY);
2313 if (dev_random != -1)
2315 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2317 close(dev_random);
2318 return TRUE;
2320 close(dev_random);
2322 else
2323 FIXME("couldn't open /dev/urandom\n");
2324 SetLastError(NTE_FAIL);
2325 return FALSE;
2329 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2330 in crypt32.dll.
2333 /******************************************************************************
2334 * SystemFunction040 (ADVAPI32.@)
2336 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2338 * PARAMS
2339 * memory [I/O] Pointer to memory to encrypt.
2340 * length [I] Length of region to encrypt in bytes.
2341 * flags [I] Control whether other processes are able to decrypt the memory.
2342 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2343 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2344 * RTL_ENCRYPT_OPTION_SAME_LOGON
2346 * RETURNS
2347 * Success: STATUS_SUCCESS
2348 * Failure: NTSTATUS error code
2350 * NOTES
2351 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2352 * If flags are specified when encrypting, the same flag value must be given
2353 * when decrypting the memory.
2355 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2357 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2358 return STATUS_SUCCESS;
2361 /******************************************************************************
2362 * SystemFunction041 (ADVAPI32.@)
2364 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2366 * PARAMS
2367 * memory [I/O] Pointer to memory to decrypt.
2368 * length [I] Length of region to decrypt in bytes.
2369 * flags [I] Control whether other processes are able to decrypt the memory.
2370 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2371 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2372 * RTL_ENCRYPT_OPTION_SAME_LOGON
2374 * RETURNS
2375 * Success: STATUS_SUCCESS
2376 * Failure: NTSTATUS error code
2378 * NOTES
2379 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2380 * If flags are specified when encrypting, the same flag value must be given
2381 * when decrypting the memory.
2383 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2385 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2386 return STATUS_SUCCESS;