qcap/tests: Add media tests for the SmartTee filter.
[wine/multimedia.git] / dlls / advapi32 / crypt.c
blobb2be5e3ae44d5c87e623a48e47d5af9d27fd3c84
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);
323 /******************************************************************************
324 * CloseEncryptedFileRaw (ADVAPI32.@)
326 * Close encrypted files
328 * PARAMS
329 * context [I] pointer to the context
330 * RETURNS
331 * Success: ERROR_SUCCESS
332 * Failure: NTSTATUS error code
334 void WINAPI CloseEncryptedFileRaw(PVOID context)
336 FIXME("(%p): stub\n", context);
339 /******************************************************************************
340 * CryptAcquireContextW (ADVAPI32.@)
342 * Acquire a crypto provider context handle.
344 * PARAMS
345 * phProv [O] Pointer to HCRYPTPROV for the output.
346 * pszContainer [I] Key Container Name
347 * pszProvider [I] Cryptographic Service Provider Name
348 * dwProvType [I] Crypto provider type to get a handle.
349 * dwFlags [I] flags for the operation
351 * RETURNS
352 * TRUE on success, FALSE on failure.
354 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
355 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
357 PCRYPTPROV pProv = NULL;
358 HKEY key;
359 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
360 PSTR provnameA = NULL, pszContainerA = NULL;
361 DWORD keytype, type, len;
362 ULONG r;
363 static const WCHAR nameW[] = {'N','a','m','e',0};
364 static const WCHAR typeW[] = {'T','y','p','e',0};
365 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
367 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
368 debugstr_w(pszProvider), dwProvType, dwFlags);
370 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
372 SetLastError(NTE_BAD_PROV_TYPE);
373 return FALSE;
376 if (!phProv)
378 SetLastError(ERROR_INVALID_PARAMETER);
379 return FALSE;
382 /* Make sure the MachineGuid value exists */
383 CRYPT_CreateMachineGuid();
385 if (!pszProvider || !*pszProvider)
387 /* No CSP name specified so try the user default CSP first
388 * then try the machine default CSP
390 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
391 TRACE("No provider registered for crypto provider type %d.\n", dwProvType);
392 SetLastError(NTE_PROV_TYPE_NOT_DEF);
393 return FALSE;
395 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
397 CRYPT_Free(keyname);
398 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
399 TRACE("No type registered for crypto provider type %d.\n", dwProvType);
400 RegCloseKey(key);
401 SetLastError(NTE_PROV_TYPE_NOT_DEF);
402 goto error;
404 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
405 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
406 CRYPT_Free(keyname);
407 RegCloseKey(key);
408 SetLastError(NTE_PROV_TYPE_NOT_DEF);
409 goto error;
412 CRYPT_Free(keyname);
413 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
414 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
416 TRACE("error %d reading size of 'Name' from registry\n", r );
417 RegCloseKey(key);
418 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
419 goto error;
421 if(!(provname = CRYPT_Alloc(len)))
423 RegCloseKey(key);
424 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
425 goto error;
427 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
428 if( r != ERROR_SUCCESS )
430 TRACE("error %d reading 'Name' from registry\n", r );
431 RegCloseKey(key);
432 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
433 goto error;
435 RegCloseKey(key);
436 } else {
437 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
439 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
440 goto error;
442 strcpyW(provname, pszProvider);
445 keyname = CRYPT_GetProvKeyName(provname);
446 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
447 CRYPT_Free(keyname);
448 if (r != ERROR_SUCCESS)
450 SetLastError(NTE_KEYSET_NOT_DEF);
451 goto error;
453 len = sizeof(DWORD);
454 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
455 if (r != ERROR_SUCCESS)
457 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
458 goto error;
460 if (type != dwProvType)
462 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
463 SetLastError(NTE_PROV_TYPE_NO_MATCH);
464 goto error;
467 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
468 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
470 TRACE("error %d reading size of 'Image Path' from registry\n", r );
471 RegCloseKey(key);
472 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
473 goto error;
475 if (!(temp = CRYPT_Alloc(len)))
477 RegCloseKey(key);
478 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
479 goto error;
481 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
482 if( r != ERROR_SUCCESS )
484 TRACE("error %d reading 'Image Path' from registry\n", r );
485 RegCloseKey(key);
486 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
487 goto error;
489 RegCloseKey(key);
490 len = ExpandEnvironmentStringsW(temp, NULL, 0);
491 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
493 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
494 goto error;
496 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
498 /* ExpandEnvironmentStrings will call SetLastError */
499 goto error;
501 pProv = CRYPT_LoadProvider(imagepath);
502 if (!pProv) {
503 /* CRYPT_LoadProvider calls SetLastError */
504 goto error;
506 pProv->pVTable->dwProvType = dwProvType;
507 if(!CRYPT_UnicodeToANSI(provname, &provnameA, 0))
509 /* CRYPT_UnicodeToANSI calls SetLastError */
510 goto error;
512 pProv->pVTable->pszProvName = provnameA;
513 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, 0))
515 /* CRYPT_UnicodeToANSI calls SetLastError */
516 goto error;
518 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
520 /* MSDN: When this flag is set, the value returned in phProv is undefined,
521 * and thus, the CryptReleaseContext function need not be called afterwards.
522 * Therefore, we must clean up everything now.
524 if (dwFlags & CRYPT_DELETEKEYSET)
526 pProv->dwMagic = 0;
527 FreeLibrary(pProv->hModule);
528 CRYPT_Free(provnameA);
529 CRYPT_Free(pProv->pVTable);
530 CRYPT_Free(pProv->pFuncs);
531 CRYPT_Free(pProv);
532 } else {
533 *phProv = (HCRYPTPROV)pProv;
535 CRYPT_Free(pszContainerA);
536 CRYPT_Free(provname);
537 CRYPT_Free(temp);
538 CRYPT_Free(imagepath);
539 return TRUE;
541 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
542 error:
543 if (pProv)
545 pProv->dwMagic = 0;
546 if (pProv->hModule)
547 FreeLibrary(pProv->hModule);
548 CRYPT_Free(pProv->pVTable);
549 CRYPT_Free(pProv->pFuncs);
550 CRYPT_Free(pProv);
552 CRYPT_Free(pszContainerA);
553 CRYPT_Free(provnameA);
554 CRYPT_Free(provname);
555 CRYPT_Free(temp);
556 CRYPT_Free(imagepath);
557 return FALSE;
560 /******************************************************************************
561 * CryptAcquireContextA (ADVAPI32.@)
563 * See CryptAcquireContextW.
565 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
566 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
568 PWSTR pProvider = NULL, pContainer = NULL;
569 BOOL ret = FALSE;
571 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_a(pszContainer),
572 debugstr_a(pszProvider), dwProvType, dwFlags);
574 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
576 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
577 return FALSE;
579 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
581 CRYPT_Free(pContainer);
582 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
583 return FALSE;
586 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
588 CRYPT_Free(pContainer);
589 CRYPT_Free(pProvider);
591 return ret;
594 /******************************************************************************
595 * CryptContextAddRef (ADVAPI32.@)
597 * Increases reference count of a cryptographic service provider handle
598 * by one.
600 * PARAMS
601 * hProv [I] Handle to the CSP whose reference is being incremented.
602 * pdwReserved [IN] Reserved for future use and must be NULL.
603 * dwFlags [I] Reserved for future use and must be NULL.
605 * RETURNS
606 * Success: TRUE
607 * Failure: FALSE
609 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
611 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
613 TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
615 if (!pProv)
617 SetLastError(NTE_BAD_UID);
618 return FALSE;
621 if (pProv->dwMagic != MAGIC_CRYPTPROV)
623 SetLastError(ERROR_INVALID_PARAMETER);
624 return FALSE;
627 pProv->refcount++;
628 return TRUE;
631 /******************************************************************************
632 * CryptReleaseContext (ADVAPI32.@)
634 * Releases the handle of a CSP. Reference count is decreased.
636 * PARAMS
637 * hProv [I] Handle of a CSP.
638 * dwFlags [I] Reserved for future use and must be NULL.
640 * RETURNS
641 * Success: TRUE
642 * Failure: FALSE
644 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, ULONG_PTR dwFlags)
646 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
647 BOOL ret = TRUE;
649 TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
651 if (!pProv)
653 SetLastError(NTE_BAD_UID);
654 return FALSE;
657 if (pProv->dwMagic != MAGIC_CRYPTPROV)
659 SetLastError(ERROR_INVALID_PARAMETER);
660 return FALSE;
663 pProv->refcount--;
664 if (pProv->refcount <= 0)
666 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
667 pProv->dwMagic = 0;
668 FreeLibrary(pProv->hModule);
669 #if 0
670 CRYPT_Free(pProv->pVTable->pContextInfo);
671 #endif
672 CRYPT_Free(pProv->pVTable->pszProvName);
673 CRYPT_Free(pProv->pVTable);
674 CRYPT_Free(pProv->pFuncs);
675 CRYPT_Free(pProv);
677 return ret;
680 /******************************************************************************
681 * CryptGenRandom (ADVAPI32.@)
683 * Fills a buffer with cryptographically random bytes.
685 * PARAMS
686 * hProv [I] Handle of a CSP.
687 * dwLen [I] Number of bytes to generate.
688 * pbBuffer [I/O] Buffer to contain random bytes.
690 * RETURNS
691 * Success: TRUE
692 * Failure: FALSE
694 * NOTES
695 * pdBuffer must be at least dwLen bytes long.
697 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
699 PCRYPTPROV prov = (PCRYPTPROV)hProv;
701 TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
703 if (!hProv)
705 SetLastError(ERROR_INVALID_HANDLE);
706 return FALSE;
709 if (prov->dwMagic != MAGIC_CRYPTPROV)
711 SetLastError(ERROR_INVALID_PARAMETER);
712 return FALSE;
715 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
718 /******************************************************************************
719 * CryptCreateHash (ADVAPI32.@)
721 * Initiates the hashing of a stream of data.
723 * PARAMS
724 * hProv [I] Handle of a CSP.
725 * Algid [I] Identifies the hash algorithm to use.
726 * hKey [I] Key for the hash (if required).
727 * dwFlags [I] Reserved for future use and must be NULL.
728 * phHash [O] Address of the future handle to the new hash object.
730 * RETURNS
731 * Success: TRUE
732 * Failure: FALSE
734 * NOTES
735 * If the algorithm is a keyed hash, hKey is the key.
737 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
738 DWORD dwFlags, HCRYPTHASH *phHash)
740 PCRYPTPROV prov = (PCRYPTPROV)hProv;
741 PCRYPTKEY key = (PCRYPTKEY)hKey;
742 PCRYPTHASH hash;
744 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
746 if (!prov || !phHash || prov->dwMagic != MAGIC_CRYPTPROV ||
747 (key && key->dwMagic != MAGIC_CRYPTKEY))
749 SetLastError(ERROR_INVALID_PARAMETER);
750 return FALSE;
752 if (dwFlags)
754 SetLastError(NTE_BAD_FLAGS);
755 return FALSE;
757 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
759 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
760 return FALSE;
763 hash->pProvider = prov;
764 hash->dwMagic = MAGIC_CRYPTHASH;
765 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
766 key ? key->hPrivate : 0, 0, &hash->hPrivate))
768 *phHash = (HCRYPTHASH)hash;
769 return TRUE;
772 /* CSP error! */
773 hash->dwMagic = 0;
774 CRYPT_Free(hash);
775 *phHash = 0;
776 return FALSE;
779 /******************************************************************************
780 * CryptDecrypt (ADVAPI32.@)
782 * Decrypts data encrypted by CryptEncrypt.
784 * PARAMS
785 * hKey [I] Handle to the decryption key.
786 * hHash [I] Handle to a hash object.
787 * Final [I] TRUE if this is the last section to be decrypted.
788 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
789 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
790 * data on return
791 * pdwDataLen [I/O] Length of pbData before and after the call.
793 * RETURNS
794 * Success: TRUE
795 * Failure: FALSE
797 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
798 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
800 PCRYPTPROV prov;
801 PCRYPTKEY key = (PCRYPTKEY)hKey;
802 PCRYPTHASH hash = (PCRYPTHASH)hHash;
804 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
806 if (!key || !pbData || !pdwDataLen ||
807 !key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
808 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
810 SetLastError(ERROR_INVALID_PARAMETER);
811 return FALSE;
814 prov = key->pProvider;
815 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
816 Final, dwFlags, pbData, pdwDataLen);
819 /******************************************************************************
820 * CryptDeriveKey (ADVAPI32.@)
822 * Generates session keys derived from a base data value.
824 * PARAMS
825 * hProv [I] Handle to a CSP.
826 * Algid [I] Identifies the symmetric encryption algorithm to use.
827 * hBaseData [I] Handle to a hash object.
828 * dwFlags [I] Type of key to generate.
829 * phKey [I/O] Address of the newly generated key.
831 * RETURNS
832 * Success: TRUE
833 * Failure: FALSE
835 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
836 DWORD dwFlags, HCRYPTKEY *phKey)
838 PCRYPTPROV prov = (PCRYPTPROV)hProv;
839 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
840 PCRYPTKEY key;
842 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
844 if (!prov || !hash)
846 SetLastError(ERROR_INVALID_HANDLE);
847 return FALSE;
849 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV || hash->dwMagic != MAGIC_CRYPTHASH)
851 SetLastError(ERROR_INVALID_PARAMETER);
852 return FALSE;
854 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
856 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
857 return FALSE;
860 key->pProvider = prov;
861 key->dwMagic = MAGIC_CRYPTKEY;
862 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
864 *phKey = (HCRYPTKEY)key;
865 return TRUE;
868 /* CSP error! */
869 key->dwMagic = 0;
870 CRYPT_Free(key);
871 *phKey = 0;
872 return FALSE;
875 /******************************************************************************
876 * CryptDestroyHash (ADVAPI32.@)
878 * Destroys the hash object referenced by hHash.
880 * PARAMS
881 * hHash [I] Handle of the hash object to be destroyed.
883 * RETURNS
884 * Success: TRUE
885 * Failure: FALSE
887 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
889 PCRYPTHASH hash = (PCRYPTHASH)hHash;
890 PCRYPTPROV prov;
891 BOOL ret;
893 TRACE("(0x%lx)\n", hHash);
895 if (!hash)
897 SetLastError(ERROR_INVALID_HANDLE);
898 return FALSE;
901 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
902 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
904 SetLastError(ERROR_INVALID_PARAMETER);
905 return FALSE;
908 prov = hash->pProvider;
909 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
910 hash->dwMagic = 0;
911 CRYPT_Free(hash);
912 return ret;
915 /******************************************************************************
916 * CryptDestroyKey (ADVAPI32.@)
918 * Releases the handle referenced by hKey.
920 * PARAMS
921 * hKey [I] Handle of the key to be destroyed.
923 * RETURNS
924 * Success: TRUE
925 * Failure: FALSE
927 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
929 PCRYPTKEY key = (PCRYPTKEY)hKey;
930 PCRYPTPROV prov;
931 BOOL ret;
933 TRACE("(0x%lx)\n", hKey);
935 if (!key)
937 SetLastError(ERROR_INVALID_HANDLE);
938 return FALSE;
941 if (!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
942 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
944 SetLastError(ERROR_INVALID_PARAMETER);
945 return FALSE;
948 prov = key->pProvider;
949 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
950 key->dwMagic = 0;
951 CRYPT_Free(key);
952 return ret;
955 /******************************************************************************
956 * CryptDuplicateHash (ADVAPI32.@)
958 * Duplicates a hash.
960 * PARAMS
961 * hHash [I] Handle to the hash to be copied.
962 * pdwReserved [I] Reserved for future use and must be zero.
963 * dwFlags [I] Reserved for future use and must be zero.
964 * phHash [O] Address of the handle to receive the copy.
966 * RETURNS
967 * Success: TRUE
968 * Failure: FALSE
970 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
971 DWORD dwFlags, HCRYPTHASH *phHash)
973 PCRYPTPROV prov;
974 PCRYPTHASH orghash, newhash;
976 TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
978 orghash = (PCRYPTHASH)hHash;
979 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
980 orghash->dwMagic != MAGIC_CRYPTHASH || orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
982 SetLastError(ERROR_INVALID_PARAMETER);
983 return FALSE;
986 prov = orghash->pProvider;
987 if (!prov->pFuncs->pCPDuplicateHash)
989 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
990 return FALSE;
993 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
995 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
996 return FALSE;
999 newhash->pProvider = prov;
1000 newhash->dwMagic = MAGIC_CRYPTHASH;
1001 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
1003 *phHash = (HCRYPTHASH)newhash;
1004 return TRUE;
1006 newhash->dwMagic = 0;
1007 CRYPT_Free(newhash);
1008 return FALSE;
1011 /******************************************************************************
1012 * CryptDuplicateKey (ADVAPI32.@)
1014 * Duplicate a key and the key's state.
1016 * PARAMS
1017 * hKey [I] Handle of the key to copy.
1018 * pdwReserved [I] Reserved for future use and must be NULL.
1019 * dwFlags [I] Reserved for future use and must be zero.
1020 * phKey [I] Address of the handle to the duplicated key.
1022 * RETURNS
1023 * Success: TRUE
1024 * Failure: FALSE
1026 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
1028 PCRYPTPROV prov;
1029 PCRYPTKEY orgkey, newkey;
1031 TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1033 orgkey = (PCRYPTKEY)hKey;
1034 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
1035 orgkey->dwMagic != MAGIC_CRYPTKEY ||
1036 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
1038 SetLastError(ERROR_INVALID_PARAMETER);
1039 return FALSE;
1042 prov = orgkey->pProvider;
1043 if (!prov->pFuncs->pCPDuplicateKey)
1045 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1046 return FALSE;
1049 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1051 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1052 return FALSE;
1055 newkey->pProvider = prov;
1056 newkey->dwMagic = MAGIC_CRYPTKEY;
1057 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
1059 *phKey = (HCRYPTKEY)newkey;
1060 return TRUE;
1062 newkey->dwMagic = 0;
1063 CRYPT_Free(newkey);
1064 return FALSE;
1067 /******************************************************************************
1068 * CryptEncrypt (ADVAPI32.@)
1070 * Encrypts data.
1072 * PARAMS
1073 * hKey [I] Handle to the encryption key.
1074 * hHash [I] Handle to a hash object.
1075 * Final [I] TRUE if this is the last section to encrypt.
1076 * dwFlags [I] Can be CRYPT_OAEP.
1077 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1078 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1079 * encrypted data after call.
1080 * dwBufLen [I] Length of the input pbData buffer.
1082 * RETURNS
1083 * Success: TRUE
1084 * Failure: FALSE
1086 * NOTES
1087 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1088 * required for the returned data in pdwDataLen.
1090 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1091 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1093 PCRYPTPROV prov;
1094 PCRYPTKEY key = (PCRYPTKEY)hKey;
1095 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1097 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1099 if (!key || !pdwDataLen || !key->pProvider ||
1100 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1102 SetLastError(ERROR_INVALID_PARAMETER);
1103 return FALSE;
1106 prov = key->pProvider;
1107 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
1108 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1111 /******************************************************************************
1112 * CryptEnumProvidersW (ADVAPI32.@)
1114 * Returns the next available CSP.
1116 * PARAMS
1117 * dwIndex [I] Index of the next provider to be enumerated.
1118 * pdwReserved [I] Reserved for future use and must be NULL.
1119 * dwFlags [I] Reserved for future use and must be zero.
1120 * pdwProvType [O] DWORD designating the type of the provider.
1121 * pszProvName [O] Buffer that receives data from the provider.
1122 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1123 * of bytes stored in the buffer on return.
1125 * RETURNS
1126 * Success: TRUE
1127 * Failure: FALSE
1129 * NOTES
1130 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1131 * for memory allocation purposes.
1133 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
1134 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1136 HKEY hKey;
1137 static const WCHAR providerW[] = {
1138 'S','o','f','t','w','a','r','e','\\',
1139 'M','i','c','r','o','s','o','f','t','\\',
1140 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1141 'D','e','f','a','u','l','t','s','\\',
1142 'P','r','o','v','i','d','e','r',0
1144 static const WCHAR typeW[] = {'T','y','p','e',0};
1145 BOOL ret;
1147 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1148 pdwProvType, pszProvName, pcbProvName);
1150 if (pdwReserved || !pcbProvName)
1152 SetLastError(ERROR_INVALID_PARAMETER);
1153 return FALSE;
1155 if (dwFlags)
1157 SetLastError(NTE_BAD_FLAGS);
1158 return FALSE;
1161 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1163 SetLastError(NTE_FAIL);
1164 return FALSE;
1167 ret = TRUE;
1168 if (!pszProvName)
1170 DWORD numkeys;
1171 WCHAR *provNameW;
1173 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1174 NULL, NULL, NULL, NULL, NULL, NULL);
1176 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1178 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1179 RegCloseKey(hKey);
1180 return FALSE;
1183 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1184 CRYPT_Free(provNameW);
1185 (*pcbProvName)++;
1186 *pcbProvName *= sizeof(WCHAR);
1188 if (dwIndex >= numkeys)
1190 SetLastError(ERROR_NO_MORE_ITEMS);
1191 ret = FALSE;
1193 } else {
1194 DWORD size = sizeof(DWORD);
1195 DWORD result;
1196 HKEY subkey;
1198 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1199 if (result)
1201 SetLastError(result);
1202 RegCloseKey(hKey);
1203 return FALSE;
1205 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1207 RegCloseKey(hKey);
1208 return FALSE;
1211 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1212 ret = FALSE;
1214 RegCloseKey(subkey);
1216 RegCloseKey(hKey);
1217 return ret;
1220 /******************************************************************************
1221 * CryptEnumProvidersA (ADVAPI32.@)
1223 * See CryptEnumProvidersW.
1225 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1226 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1228 PWSTR str = NULL;
1229 DWORD bufsize;
1230 BOOL ret;
1232 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1233 pdwProvType, pszProvName, pcbProvName);
1235 if(!CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1236 return FALSE;
1237 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1239 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1240 return FALSE;
1242 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1243 if (str)
1244 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1245 *pcbProvName = bufsize / sizeof(WCHAR); /* FIXME: not correct */
1246 if (str)
1248 CRYPT_Free(str);
1249 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1251 SetLastError(ERROR_MORE_DATA);
1252 return FALSE;
1255 return ret;
1258 /******************************************************************************
1259 * CryptEnumProviderTypesW (ADVAPI32.@)
1261 * Retrieves the next type of CSP supported.
1263 * PARAMS
1264 * dwIndex [I] Index of the next provider to be enumerated.
1265 * pdwReserved [I] Reserved for future use and must be NULL.
1266 * dwFlags [I] Reserved for future use and must be zero.
1267 * pdwProvType [O] DWORD designating the type of the provider.
1268 * pszTypeName [O] Buffer that receives data from the provider type.
1269 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1270 * of bytes stored in the buffer on return.
1272 * RETURNS
1273 * Success: TRUE
1274 * Failure: FALSE
1276 * NOTES
1277 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1278 * for memory allocation purposes.
1280 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1281 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1283 HKEY hKey, hSubkey;
1284 DWORD keylen, numkeys, dwType;
1285 PWSTR keyname, ch;
1286 DWORD result;
1287 static const WCHAR KEYSTR[] = {
1288 'S','o','f','t','w','a','r','e','\\',
1289 'M','i','c','r','o','s','o','f','t','\\',
1290 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1291 'D','e','f','a','u','l','t','s','\\',
1292 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1294 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1296 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1297 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1299 if (pdwReserved || !pdwProvType || !pcbTypeName)
1301 SetLastError(ERROR_INVALID_PARAMETER);
1302 return FALSE;
1304 if (dwFlags)
1306 SetLastError(NTE_BAD_FLAGS);
1307 return FALSE;
1310 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1311 return FALSE;
1313 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1314 if (dwIndex >= numkeys)
1316 SetLastError(ERROR_NO_MORE_ITEMS);
1317 RegCloseKey(hKey);
1318 return FALSE;
1320 keylen++;
1321 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1323 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1324 RegCloseKey(hKey);
1325 return FALSE;
1327 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1328 CRYPT_Free(keyname);
1329 RegCloseKey(hKey);
1330 return FALSE;
1332 RegOpenKeyW(hKey, keyname, &hSubkey);
1333 RegCloseKey(hKey);
1335 ch = keyname + strlenW(keyname);
1336 /* Convert "Type 000" to 0, etc/ */
1337 *pdwProvType = *(--ch) - '0';
1338 *pdwProvType += (*(--ch) - '0') * 10;
1339 *pdwProvType += (*(--ch) - '0') * 100;
1340 CRYPT_Free(keyname);
1342 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1343 if (result)
1345 SetLastError(result);
1346 RegCloseKey(hSubkey);
1347 return FALSE;
1350 RegCloseKey(hSubkey);
1351 return TRUE;
1354 /******************************************************************************
1355 * CryptEnumProviderTypesA (ADVAPI32.@)
1357 * See CryptEnumProviderTypesW.
1359 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1360 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1362 PWSTR str = NULL;
1363 DWORD bufsize;
1364 BOOL ret;
1366 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1367 pdwProvType, pszTypeName, pcbTypeName);
1369 if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1370 return FALSE;
1371 if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1373 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1374 return FALSE;
1376 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1377 if (str)
1378 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1379 *pcbTypeName = bufsize / sizeof(WCHAR);
1380 if (str)
1382 CRYPT_Free(str);
1383 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1385 SetLastError(ERROR_MORE_DATA);
1386 return FALSE;
1389 return ret;
1392 /******************************************************************************
1393 * CryptExportKey (ADVAPI32.@)
1395 * Exports a cryptographic key from a CSP.
1397 * PARAMS
1398 * hKey [I] Handle to the key to export.
1399 * hExpKey [I] Handle to a cryptographic key of the end user.
1400 * dwBlobType [I] Type of BLOB to be exported.
1401 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1402 * pbData [O] Buffer to receive BLOB data.
1403 * pdwDataLen [I/O] Specifies the size of pbData.
1405 * RETURNS
1406 * Success: TRUE
1407 * Failure: FALSE
1409 * NOTES
1410 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1411 * buffer needed to hold the BLOB.
1413 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1414 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1416 PCRYPTPROV prov;
1417 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1419 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1421 if (!key || !pdwDataLen || !key->pProvider ||
1422 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1424 SetLastError(ERROR_INVALID_PARAMETER);
1425 return FALSE;
1428 prov = key->pProvider;
1429 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1430 dwBlobType, dwFlags, pbData, pdwDataLen);
1433 /******************************************************************************
1434 * CryptGenKey (ADVAPI32.@)
1436 * Generates a random cryptographic session key or a pub/priv key pair.
1438 * PARAMS
1439 * hProv [I] Handle to a CSP.
1440 * Algid [I] Algorithm to use to make key.
1441 * dwFlags [I] Specifies type of key to make.
1442 * phKey [I] Address of the handle to which the new key is copied.
1444 * RETURNS
1445 * Success: TRUE
1446 * Failure: FALSE
1448 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1450 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1451 PCRYPTKEY key;
1453 TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1455 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1457 SetLastError(ERROR_INVALID_PARAMETER);
1458 return FALSE;
1460 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1462 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1463 return FALSE;
1466 key->pProvider = prov;
1467 key->dwMagic = MAGIC_CRYPTKEY;
1468 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1470 *phKey = (HCRYPTKEY)key;
1471 return TRUE;
1474 /* CSP error! */
1475 key->dwMagic = 0;
1476 CRYPT_Free(key);
1477 return FALSE;
1480 /******************************************************************************
1481 * CryptGetDefaultProviderW (ADVAPI32.@)
1483 * Finds the default CSP of a certain provider type.
1485 * PARAMS
1486 * dwProvType [I] Provider type to look for.
1487 * pdwReserved [I] Reserved for future use and must be NULL.
1488 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1489 * pszProvName [O] Name of the default CSP.
1490 * pcbProvName [I/O] Size of pszProvName
1492 * RETURNS
1493 * Success: TRUE
1494 * Failure: FALSE
1496 * NOTES
1497 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1498 * memory allocation purposes on return.
1500 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1501 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1503 HKEY hKey;
1504 PWSTR keyname;
1505 DWORD result;
1506 static const WCHAR nameW[] = {'N','a','m','e',0};
1508 if (pdwReserved || !pcbProvName)
1510 SetLastError(ERROR_INVALID_PARAMETER);
1511 return FALSE;
1513 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1515 SetLastError(NTE_BAD_FLAGS);
1516 return FALSE;
1518 if (dwProvType > 999)
1520 SetLastError(NTE_BAD_PROV_TYPE);
1521 return FALSE;
1523 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1525 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1526 return FALSE;
1528 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1530 CRYPT_Free(keyname);
1531 SetLastError(NTE_PROV_TYPE_NOT_DEF);
1532 return FALSE;
1534 CRYPT_Free(keyname);
1536 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1537 RegCloseKey(hKey);
1539 if (result)
1541 if (result != ERROR_MORE_DATA)
1542 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1543 else
1544 SetLastError(result);
1546 return FALSE;
1549 return TRUE;
1552 /******************************************************************************
1553 * CryptGetDefaultProviderA (ADVAPI32.@)
1555 * See CryptGetDefaultProviderW.
1557 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1558 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1560 PWSTR str = NULL;
1561 DWORD bufsize;
1562 BOOL ret;
1564 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1566 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
1567 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1569 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1570 return FALSE;
1572 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1573 if (str)
1574 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1575 *pcbProvName = bufsize / sizeof(WCHAR);
1576 if (str)
1578 CRYPT_Free(str);
1579 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1581 SetLastError(ERROR_MORE_DATA);
1582 return FALSE;
1585 return ret;
1588 /******************************************************************************
1589 * CryptGetHashParam (ADVAPI32.@)
1591 * Retrieves data that controls the operations of a hash object.
1593 * PARAMS
1594 * hHash [I] Handle of the hash object to question.
1595 * dwParam [I] Query type.
1596 * pbData [O] Buffer that receives the value data.
1597 * pdwDataLen [I/O] Size of the pbData buffer.
1598 * dwFlags [I] Reserved for future use and must be zero.
1600 * RETURNS
1601 * Success: TRUE
1602 * Failure: FALSE
1604 * NOTES
1605 * If pbData is NULL, pdwDataLen will contain the length required.
1607 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1608 DWORD *pdwDataLen, DWORD dwFlags)
1610 PCRYPTPROV prov;
1611 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1613 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1615 if (!hash || !pdwDataLen || !hash->pProvider ||
1616 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1618 SetLastError(ERROR_INVALID_PARAMETER);
1619 return FALSE;
1622 prov = hash->pProvider;
1623 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1624 pbData, pdwDataLen, dwFlags);
1627 /******************************************************************************
1628 * CryptGetKeyParam (ADVAPI32.@)
1630 * Retrieves data that controls the operations of a key.
1632 * PARAMS
1633 * hKey [I] Handle to they key in question.
1634 * dwParam [I] Specifies query type.
1635 * pbData [O] Sequence of bytes to receive data.
1636 * pdwDataLen [I/O] Size of pbData.
1637 * dwFlags [I] Reserved for future use and must be zero.
1639 * RETURNS
1640 * Success: TRUE
1641 * Failure: FALSE
1643 * NOTES
1644 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1646 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1647 DWORD *pdwDataLen, DWORD dwFlags)
1649 PCRYPTPROV prov;
1650 PCRYPTKEY key = (PCRYPTKEY)hKey;
1652 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1654 if (!key || !pdwDataLen || !key->pProvider ||
1655 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1657 SetLastError(ERROR_INVALID_PARAMETER);
1658 return FALSE;
1661 prov = key->pProvider;
1662 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1663 pbData, pdwDataLen, dwFlags);
1666 /******************************************************************************
1667 * CryptGetProvParam (ADVAPI32.@)
1669 * Retrieves parameters that control the operations of a CSP.
1671 * PARAMS
1672 * hProv [I] Handle of the CSP in question.
1673 * dwParam [I] Specifies query type.
1674 * pbData [O] Buffer to receive the data.
1675 * pdwDataLen [I/O] Size of pbData.
1676 * dwFlags [I] see MSDN Docs.
1678 * RETURNS
1679 * Success: TRUE
1680 * Failure: FALSE
1682 * NOTES
1683 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1685 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1686 DWORD *pdwDataLen, DWORD dwFlags)
1688 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1690 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1692 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1694 SetLastError(ERROR_INVALID_PARAMETER);
1695 return FALSE;
1698 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1701 /******************************************************************************
1702 * CryptGetUserKey (ADVAPI32.@)
1704 * Gets a handle of one of a user's two public/private key pairs.
1706 * PARAMS
1707 * hProv [I] Handle of a CSP.
1708 * dwKeySpec [I] Private key to use.
1709 * phUserKey [O] Pointer to the handle of the retrieved keys.
1711 * RETURNS
1712 * Success: TRUE
1713 * Failure: FALSE
1715 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1717 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1718 PCRYPTKEY key;
1720 TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1722 if (!prov)
1724 SetLastError(ERROR_INVALID_HANDLE);
1725 return FALSE;
1727 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1729 SetLastError(ERROR_INVALID_PARAMETER);
1730 return FALSE;
1732 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1734 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1735 return FALSE;
1738 key->pProvider = prov;
1739 key->dwMagic = MAGIC_CRYPTKEY;
1740 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1742 *phUserKey = (HCRYPTKEY)key;
1743 return TRUE;
1746 /* CSP Error */
1747 key->dwMagic = 0;
1748 CRYPT_Free(key);
1749 *phUserKey = 0;
1750 return FALSE;
1753 /******************************************************************************
1754 * CryptHashData (ADVAPI32.@)
1756 * Adds data to a hash object.
1758 * PARAMS
1759 * hHash [I] Handle of the hash object.
1760 * pbData [I] Buffer of data to be hashed.
1761 * dwDataLen [I] Number of bytes to add.
1762 * dwFlags [I] Can be CRYPT_USERDATA
1764 * RETURNS
1765 * Success: TRUE
1766 * Failure: FALSE
1768 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1770 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1771 PCRYPTPROV prov;
1773 TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1775 if (!hash)
1777 SetLastError(ERROR_INVALID_HANDLE);
1778 return FALSE;
1780 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1781 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1783 SetLastError(ERROR_INVALID_PARAMETER);
1784 return FALSE;
1787 prov = hash->pProvider;
1788 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1791 /******************************************************************************
1792 * CryptHashSessionKey (ADVAPI32.@)
1794 * Compute the cryptographic hash of a session key object.
1796 * PARAMS
1797 * hHash [I] Handle to the hash object.
1798 * hKey [I] Handle to the key to be hashed.
1799 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1801 * RETURNS
1802 * Success: TRUE
1803 * Failure: FALSE
1805 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1807 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1808 PCRYPTKEY key = (PCRYPTKEY)hKey;
1809 PCRYPTPROV prov;
1811 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1813 if (!hash || !key)
1815 SetLastError(ERROR_INVALID_HANDLE);
1816 return FALSE;
1819 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1820 hash->pProvider->dwMagic != MAGIC_CRYPTPROV || key->dwMagic != MAGIC_CRYPTKEY)
1822 SetLastError(ERROR_INVALID_PARAMETER);
1823 return FALSE;
1826 prov = hash->pProvider;
1827 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1830 /******************************************************************************
1831 * CryptImportKey (ADVAPI32.@)
1833 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1835 * PARAMS
1836 * hProv [I] Handle of a CSP.
1837 * pbData [I] Contains the key to be imported.
1838 * dwDataLen [I] Length of the key.
1839 * hPubKey [I] Cryptographic key that decrypts pdData
1840 * dwFlags [I] Used only with a public/private key pair.
1841 * phKey [O] Imported key.
1843 * RETURNS
1844 * Success: TRUE
1845 * Failure: FALSE
1847 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, const BYTE *pbData, DWORD dwDataLen,
1848 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1850 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1851 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1853 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1855 if (!prov || !pbData || !dwDataLen || !phKey ||
1856 prov->dwMagic != MAGIC_CRYPTPROV ||
1857 (pubkey && pubkey->dwMagic != MAGIC_CRYPTKEY))
1859 SetLastError(ERROR_INVALID_PARAMETER);
1860 return FALSE;
1863 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1865 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1866 return FALSE;
1869 importkey->pProvider = prov;
1870 importkey->dwMagic = MAGIC_CRYPTKEY;
1871 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1872 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1874 *phKey = (HCRYPTKEY)importkey;
1875 return TRUE;
1878 importkey->dwMagic = 0;
1879 CRYPT_Free(importkey);
1880 return FALSE;
1883 /******************************************************************************
1884 * CryptSignHashW (ADVAPI32.@)
1886 * Signs data.
1888 * PARAMS
1889 * hHash [I] Handle of the hash object to be signed.
1890 * dwKeySpec [I] Private key to use.
1891 * sDescription [I] Should be NULL.
1892 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1893 * pbSignature [O] Buffer of the signature data.
1894 * pdwSigLen [I/O] Size of the pbSignature buffer.
1896 * RETURNS
1897 * Success: TRUE
1898 * Failure: FALSE
1900 * NOTES
1901 * Because of security flaws sDescription should not be used and should thus be
1902 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1903 * Providers.
1905 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1906 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1908 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1909 PCRYPTPROV prov;
1911 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1912 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1914 if (!hash)
1916 SetLastError(ERROR_INVALID_HANDLE);
1917 return FALSE;
1919 if (!pdwSigLen || !hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1920 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1922 SetLastError(ERROR_INVALID_PARAMETER);
1923 return FALSE;
1926 prov = hash->pProvider;
1927 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1928 dwFlags, pbSignature, pdwSigLen);
1931 /******************************************************************************
1932 * CryptSignHashA (ADVAPI32.@)
1934 * See CryptSignHashW.
1936 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1937 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1939 LPWSTR wsDescription;
1940 BOOL result;
1942 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1943 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1945 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1946 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1947 CRYPT_Free(wsDescription);
1949 return result;
1952 /******************************************************************************
1953 * CryptSetHashParam (ADVAPI32.@)
1955 * Customizes the operations of a hash object.
1957 * PARAMS
1958 * hHash [I] Handle of the hash object to set parameters.
1959 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1960 * pbData [I] Value data buffer.
1961 * dwFlags [I] Reserved for future use and must be zero.
1963 * RETURNS
1964 * Success: TRUE
1965 * Failure: FALSE
1967 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, const BYTE *pbData, DWORD dwFlags)
1969 PCRYPTPROV prov;
1970 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1972 TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1974 if (!hash || !pbData || !hash->pProvider ||
1975 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1977 SetLastError(ERROR_INVALID_PARAMETER);
1978 return FALSE;
1981 prov = hash->pProvider;
1982 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1983 dwParam, pbData, dwFlags);
1986 /******************************************************************************
1987 * CryptSetKeyParam (ADVAPI32.@)
1989 * Customizes a session key's operations.
1991 * PARAMS
1992 * hKey [I] Handle to the key to set values.
1993 * dwParam [I] See MSDN Doc.
1994 * pbData [I] Buffer of values to set.
1995 * dwFlags [I] Only used when dwParam == KP_ALGID.
1997 * RETURNS
1998 * Success: TRUE
1999 * Failure: FALSE
2001 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, const BYTE *pbData, DWORD dwFlags)
2003 PCRYPTPROV prov;
2004 PCRYPTKEY key = (PCRYPTKEY)hKey;
2006 TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
2008 if (!key || !pbData || !key->pProvider ||
2009 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2011 SetLastError(ERROR_INVALID_PARAMETER);
2012 return FALSE;
2015 prov = key->pProvider;
2016 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
2017 dwParam, pbData, dwFlags);
2020 /******************************************************************************
2021 * CryptSetProviderA (ADVAPI32.@)
2023 * Specifies the current user's default CSP.
2025 * PARAMS
2026 * pszProvName [I] Name of the new default CSP.
2027 * dwProvType [I] Provider type of the CSP.
2029 * RETURNS
2030 * Success: TRUE
2031 * Failure: FALSE
2033 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
2035 TRACE("(%s, %d)\n", pszProvName, dwProvType);
2036 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2039 /******************************************************************************
2040 * CryptSetProviderW (ADVAPI32.@)
2042 * See CryptSetProviderA.
2044 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
2046 TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
2047 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2050 /******************************************************************************
2051 * CryptSetProviderExW (ADVAPI32.@)
2053 * Specifies the default CSP.
2055 * PARAMS
2056 * pszProvName [I] Name of the new default CSP.
2057 * dwProvType [I] Provider type of the CSP.
2058 * pdwReserved [I] Reserved for future use and must be NULL.
2059 * dwFlags [I] See MSDN Doc.
2061 * RETURNS
2062 * Success: TRUE
2063 * Failure: FALSE
2065 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2067 HKEY hProvKey, hTypeKey;
2068 PWSTR keyname;
2069 static const WCHAR nameW[] = {'N','a','m','e',0};
2071 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2073 if (!pszProvName || pdwReserved)
2075 SetLastError(ERROR_INVALID_PARAMETER);
2076 return FALSE;
2078 if (dwProvType > MAXPROVTYPES)
2080 SetLastError(NTE_BAD_PROV_TYPE);
2081 return FALSE;
2083 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
2084 || dwFlags == CRYPT_DELETE_DEFAULT)
2086 SetLastError(NTE_BAD_FLAGS);
2087 return FALSE;
2090 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2092 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2093 return FALSE;
2095 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2096 keyname, &hTypeKey))
2098 CRYPT_Free(keyname);
2099 SetLastError(NTE_BAD_PROVIDER);
2100 return FALSE;
2102 CRYPT_Free(keyname);
2104 if (dwFlags & CRYPT_DELETE_DEFAULT)
2106 RegDeleteValueW(hTypeKey, nameW);
2108 else
2110 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
2112 RegCloseKey(hTypeKey);
2113 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2114 return FALSE;
2116 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2117 keyname, &hProvKey))
2119 CRYPT_Free(keyname);
2120 RegCloseKey(hTypeKey);
2121 SetLastError(NTE_BAD_PROVIDER);
2122 return FALSE;
2124 CRYPT_Free(keyname);
2126 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
2127 (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2129 RegCloseKey(hTypeKey);
2130 RegCloseKey(hProvKey);
2131 return FALSE;
2134 RegCloseKey(hProvKey);
2136 RegCloseKey(hTypeKey);
2138 return TRUE;
2141 /******************************************************************************
2142 * CryptSetProviderExA (ADVAPI32.@)
2144 * See CryptSetProviderExW.
2146 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2148 BOOL ret = FALSE;
2149 PWSTR str = NULL;
2151 TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2153 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2155 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2156 CRYPT_Free(str);
2158 return ret;
2161 /******************************************************************************
2162 * CryptSetProvParam (ADVAPI32.@)
2164 * Customizes the operations of a CSP.
2166 * PARAMS
2167 * hProv [I] Handle of a CSP.
2168 * dwParam [I] See MSDN Doc.
2169 * pbData [I] Buffer that contains a value to set as a parameter.
2170 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2172 * RETURNS
2173 * Success: TRUE
2174 * Failure: FALSE
2176 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, const BYTE *pbData, DWORD dwFlags)
2178 PCRYPTPROV prov = (PCRYPTPROV)hProv;
2180 TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2182 if (!prov)
2184 SetLastError(ERROR_INVALID_HANDLE);
2185 return FALSE;
2187 if (prov->dwMagic != MAGIC_CRYPTPROV)
2189 SetLastError(ERROR_INVALID_PARAMETER);
2190 return FALSE;
2192 if (dwParam == PP_USE_HARDWARE_RNG)
2194 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2195 FIXME("\tLetting the CSP decide.\n");
2197 if (dwFlags & PP_CLIENT_HWND)
2199 /* FIXME: Should verify the parameter */
2200 if (pbData /* && IsWindow((HWND)pbData) */)
2202 crypt_hWindow = (HWND)(pbData);
2203 return TRUE;
2204 } else {
2205 SetLastError(ERROR_INVALID_PARAMETER);
2206 return FALSE;
2209 /* All other flags go to the CSP */
2210 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2213 /******************************************************************************
2214 * CryptVerifySignatureW (ADVAPI32.@)
2216 * Verifies the signature of a hash object.
2218 * PARAMS
2219 * hHash [I] Handle of the hash object to verify.
2220 * pbSignature [I] Signature data to verify.
2221 * dwSigLen [I] Size of pbSignature.
2222 * hPubKey [I] Handle to the public key to authenticate signature.
2223 * sDescription [I] Should be NULL.
2224 * dwFlags [I] See MSDN doc.
2226 * RETURNS
2227 * Success: TRUE
2228 * Failure: FALSE
2230 * NOTES
2231 * Because of security flaws sDescription should not be used and should thus be
2232 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2233 * Providers.
2235 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, const BYTE *pbSignature, DWORD dwSigLen,
2236 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2238 PCRYPTHASH hash = (PCRYPTHASH)hHash;
2239 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
2240 PCRYPTPROV prov;
2242 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2243 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2245 if (!hash || !key || key->dwMagic != MAGIC_CRYPTKEY || hash->dwMagic != MAGIC_CRYPTHASH ||
2246 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
2247 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2249 SetLastError(ERROR_INVALID_PARAMETER);
2250 return FALSE;
2253 prov = hash->pProvider;
2254 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2255 key->hPrivate, sDescription, dwFlags);
2258 /******************************************************************************
2259 * CryptVerifySignatureA (ADVAPI32.@)
2261 * See CryptVerifySignatureW.
2263 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, const BYTE *pbSignature, DWORD dwSigLen,
2264 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
2266 LPWSTR wsDescription;
2267 BOOL result;
2269 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2270 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
2272 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
2273 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2274 CRYPT_Free(wsDescription);
2276 return result;
2279 /******************************************************************************
2280 * OpenEncryptedFileRawA (ADVAPI32.@)
2282 * See OpenEncryptedFileRawW
2284 DWORD WINAPI OpenEncryptedFileRawA(LPCSTR filename, ULONG flags, PVOID *context)
2286 FIXME("(%s, %x, %p): stub\n", debugstr_a(filename), flags, context);
2287 return ERROR_CALL_NOT_IMPLEMENTED;
2290 /******************************************************************************
2291 * OpenEncryptedFileRawW (ADVAPI32.@)
2293 * Opens an EFS encrypted file for backup/restore
2295 * PARAMS
2296 * filename [I] Filename to operate on
2297 * flags [I] Operation to perform
2298 * context [I] Handle to the context (out)
2299 * RETURNS
2300 * Success: ERROR_SUCCESS
2301 * Failure: NTSTATUS error code
2303 DWORD WINAPI OpenEncryptedFileRawW(LPCWSTR filename, ULONG flags, PVOID *context)
2305 FIXME("(%s, %x, %p): stub\n", debugstr_w(filename), flags, context);
2306 return ERROR_CALL_NOT_IMPLEMENTED;
2309 /******************************************************************************
2310 * ReadEncryptedFileRaw (ADVAPI32.@)
2312 * Export encrypted files
2314 * PARAMS
2315 * export [I] pointer to the export callback function
2316 * callback [I] pointer to the application defined context
2317 * context [I] pointer to the system context
2318 * RETURNS
2319 * Success: ERROR_SUCCESS
2320 * Failure: NTSTATUS error code
2322 DWORD WINAPI ReadEncryptedFileRaw(PFE_EXPORT_FUNC export, PVOID callback, PVOID context)
2324 FIXME("(%p, %p, %p): stub\n", export, callback, context);
2325 return ERROR_CALL_NOT_IMPLEMENTED;
2328 /******************************************************************************
2329 * SystemFunction030 (ADVAPI32.@)
2331 * Tests if two blocks of 16 bytes are equal
2333 * PARAMS
2334 * b1,b2 [I] block of 16 bytes
2336 * RETURNS
2337 * TRUE if blocks are the same
2338 * FALSE if blocks are different
2340 BOOL WINAPI SystemFunction030(LPCVOID b1, LPCVOID b2)
2342 return !memcmp(b1, b2, 0x10);
2345 /******************************************************************************
2346 * SystemFunction035 (ADVAPI32.@)
2348 * Described here:
2349 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2351 * NOTES
2352 * Stub, always return TRUE.
2354 BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
2356 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
2357 return TRUE;
2360 /******************************************************************************
2361 * SystemFunction036 (ADVAPI32.@)
2363 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2365 * PARAMS
2366 * pbBufer [O] Pointer to memory to receive random bytes.
2367 * dwLen [I] Number of random bytes to fetch.
2369 * RETURNS
2370 * Success: TRUE
2371 * Failure: FALSE
2374 BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2376 int dev_random;
2378 dev_random = open("/dev/urandom", O_RDONLY);
2379 if (dev_random != -1)
2381 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2383 close(dev_random);
2384 return TRUE;
2386 close(dev_random);
2388 else
2389 FIXME("couldn't open /dev/urandom\n");
2390 SetLastError(NTE_FAIL);
2391 return FALSE;
2395 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2396 in crypt32.dll.
2399 /******************************************************************************
2400 * SystemFunction040 (ADVAPI32.@)
2402 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2404 * PARAMS
2405 * memory [I/O] Pointer to memory to encrypt.
2406 * length [I] Length of region to encrypt in bytes.
2407 * flags [I] Control whether other processes are able to decrypt the memory.
2408 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2409 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2410 * RTL_ENCRYPT_OPTION_SAME_LOGON
2412 * RETURNS
2413 * Success: STATUS_SUCCESS
2414 * Failure: NTSTATUS error code
2416 * NOTES
2417 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2418 * If flags are specified when encrypting, the same flag value must be given
2419 * when decrypting the memory.
2421 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2423 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2424 return STATUS_SUCCESS;
2427 /******************************************************************************
2428 * SystemFunction041 (ADVAPI32.@)
2430 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2432 * PARAMS
2433 * memory [I/O] Pointer to memory to decrypt.
2434 * length [I] Length of region to decrypt in bytes.
2435 * flags [I] Control whether other processes are able to decrypt the memory.
2436 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2437 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2438 * RTL_ENCRYPT_OPTION_SAME_LOGON
2440 * RETURNS
2441 * Success: STATUS_SUCCESS
2442 * Failure: NTSTATUS error code
2444 * NOTES
2445 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2446 * If flags are specified when encrypting, the same flag value must be given
2447 * when decrypting the memory.
2449 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2451 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2452 return STATUS_SUCCESS;
2455 /******************************************************************************
2456 * WriteEncryptedFileRaw (ADVAPI32.@)
2458 * Import encrypted files
2460 * PARAMS
2461 * import [I] pointer to the import callback function
2462 * callback [I] pointer to the application defined context
2463 * context [I] pointer to the system context
2464 * RETURNS
2465 * Success: ERROR_SUCCESS
2466 * Failure: NTSTATUS error code
2468 DWORD WINAPI WriteEncryptedFileRaw(PFE_IMPORT_FUNC import, PVOID callback, PVOID context)
2470 FIXME("(%p, %p, %p): stub\n", import, callback, context);
2471 return ERROR_CALL_NOT_IMPLEMENTED;