server: Consume events triggered.
[wine/hramrach.git] / dlls / advapi32 / crypt.c
blob4235e8373cc7fd675856d605b7658c15610c6ebb
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 <time.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "crypt.h"
45 #include "winnls.h"
46 #include "winreg.h"
47 #include "rpc.h"
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50 #include "winternl.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
54 static HWND crypt_hWindow;
56 #define CRYPT_Alloc(size) (LocalAlloc(LMEM_ZEROINIT, size))
57 #define CRYPT_Free(buffer) (LocalFree(buffer))
59 static inline PWSTR CRYPT_GetProvKeyName(PCWSTR pProvName)
61 static const WCHAR KEYSTR[] = {
62 'S','o','f','t','w','a','r','e','\\',
63 'M','i','c','r','o','s','o','f','t','\\',
64 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
65 'D','e','f','a','u','l','t','s','\\',
66 'P','r','o','v','i','d','e','r','\\',0
68 PWSTR keyname;
70 keyname = CRYPT_Alloc((strlenW(KEYSTR) + strlenW(pProvName) +1)*sizeof(WCHAR));
71 if (keyname)
73 strcpyW(keyname, KEYSTR);
74 strcpyW(keyname + strlenW(KEYSTR), pProvName);
75 } else
76 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
77 return keyname;
80 static inline PWSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
82 static const WCHAR MACHINESTR[] = {
83 'S','o','f','t','w','a','r','e','\\',
84 'M','i','c','r','o','s','o','f','t','\\',
85 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
86 'D','e','f','a','u','l','t','s','\\',
87 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
88 'T','y','p','e',' ','X','X','X',0
90 static const WCHAR USERSTR[] = {
91 'S','o','f','t','w','a','r','e','\\',
92 'M','i','c','r','o','s','o','f','t','\\',
93 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
94 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
96 PWSTR keyname;
97 PWSTR ptr;
99 keyname = CRYPT_Alloc( ((user ? strlenW(USERSTR) : strlenW(MACHINESTR)) +1)*sizeof(WCHAR));
100 if (keyname)
102 user ? strcpyW(keyname, USERSTR) : strcpyW(keyname, MACHINESTR);
103 ptr = keyname + strlenW(keyname);
104 *(--ptr) = (dwType % 10) + '0';
105 *(--ptr) = ((dwType / 10) % 10) + '0';
106 *(--ptr) = (dwType / 100) + '0';
107 } else
108 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
109 return keyname;
112 /* CRYPT_UnicodeTOANSI
113 * wstr - unicode string
114 * str - pointer to ANSI string
115 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
117 * returns TRUE if unsuccessful, FALSE otherwise.
118 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
120 static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
122 int count;
124 if (!wstr)
126 *str = NULL;
127 return TRUE;
129 count = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
130 if (strsize == -1)
131 *str = CRYPT_Alloc(count * sizeof(CHAR));
132 else
133 count = min( count, strsize );
134 if (*str)
136 WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, count, NULL, NULL);
137 return TRUE;
139 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
140 return FALSE;
143 /* CRYPT_ANSITOUnicode
144 * str - ANSI string
145 * wstr - pointer to unicode string
146 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
148 static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
150 unsigned int wcount;
152 if (!str)
154 *wstr = NULL;
155 return TRUE;
157 wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
158 if (wstrsize == -1)
159 *wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
160 else
161 wcount = min( wcount, wstrsize/sizeof(WCHAR) );
162 if (*wstr)
164 MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
165 return TRUE;
167 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
168 return FALSE;
171 /* These next 2 functions are used by the VTableProvStruc structure */
172 static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
174 if (!lpszImage || !pData)
176 SetLastError(ERROR_INVALID_PARAMETER);
177 return FALSE;
180 FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
182 return TRUE;
185 static void CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
187 if (phWnd) *phWnd = crypt_hWindow;
190 #define CRYPT_GetProvFunc(name) \
191 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
192 #define CRYPT_GetProvFuncOpt(name) \
193 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
194 static PCRYPTPROV CRYPT_LoadProvider(PCWSTR pImage)
196 PCRYPTPROV provider;
197 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
199 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
200 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
201 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
202 if ( !(provider->hModule = LoadLibraryW(pImage)) )
204 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
205 FIXME("Failed to load dll %s\n", debugstr_w(pImage));
206 goto error;
208 provider->dwMagic = MAGIC_CRYPTPROV;
209 provider->refcount = 1;
211 errorcode = NTE_PROVIDER_DLL_FAIL;
212 CRYPT_GetProvFunc(CPAcquireContext);
213 CRYPT_GetProvFunc(CPCreateHash);
214 CRYPT_GetProvFunc(CPDecrypt);
215 CRYPT_GetProvFunc(CPDeriveKey);
216 CRYPT_GetProvFunc(CPDestroyHash);
217 CRYPT_GetProvFunc(CPDestroyKey);
218 CRYPT_GetProvFuncOpt(CPDuplicateHash);
219 CRYPT_GetProvFuncOpt(CPDuplicateKey);
220 CRYPT_GetProvFunc(CPEncrypt);
221 CRYPT_GetProvFunc(CPExportKey);
222 CRYPT_GetProvFunc(CPGenKey);
223 CRYPT_GetProvFunc(CPGenRandom);
224 CRYPT_GetProvFunc(CPGetHashParam);
225 CRYPT_GetProvFunc(CPGetKeyParam);
226 CRYPT_GetProvFunc(CPGetProvParam);
227 CRYPT_GetProvFunc(CPGetUserKey);
228 CRYPT_GetProvFunc(CPHashData);
229 CRYPT_GetProvFunc(CPHashSessionKey);
230 CRYPT_GetProvFunc(CPImportKey);
231 CRYPT_GetProvFunc(CPReleaseContext);
232 CRYPT_GetProvFunc(CPSetHashParam);
233 CRYPT_GetProvFunc(CPSetKeyParam);
234 CRYPT_GetProvFunc(CPSetProvParam);
235 CRYPT_GetProvFunc(CPSignHash);
236 CRYPT_GetProvFunc(CPVerifySignature);
238 /* FIXME: Not sure what the pbContextInfo field is for.
239 * Does it need memory allocation?
241 provider->pVTable->Version = 3;
242 provider->pVTable->FuncVerifyImage = CRYPT_VerifyImage;
243 provider->pVTable->FuncReturnhWnd = CRYPT_ReturnhWnd;
244 provider->pVTable->dwProvType = 0;
245 provider->pVTable->pbContextInfo = NULL;
246 provider->pVTable->cbContextInfo = 0;
247 provider->pVTable->pszProvName = NULL;
248 return provider;
250 error:
251 SetLastError(errorcode);
252 if (provider)
254 provider->dwMagic = 0;
255 if (provider->hModule)
256 FreeLibrary(provider->hModule);
257 CRYPT_Free(provider->pVTable);
258 CRYPT_Free(provider->pFuncs);
259 CRYPT_Free(provider);
261 return NULL;
263 #undef CRYPT_GetProvFunc
264 #undef CRYPT_GetProvFuncOpt
267 static void CRYPT_CreateMachineGuid(void)
269 static const WCHAR cryptographyW[] = {
270 'S','o','f','t','w','a','r','e','\\',
271 'M','i','c','r','o','s','o','f','t','\\',
272 'C','r','y','p','t','o','g','r','a','p','h','y',0 };
273 static const WCHAR machineGuidW[] = {
274 'M','a','c','h','i','n','e','G','u','i','d',0 };
275 LONG r;
276 HKEY key;
278 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, cryptographyW, 0, KEY_ALL_ACCESS,
279 &key);
280 if (!r)
282 DWORD size;
284 r = RegQueryValueExW(key, machineGuidW, NULL, NULL, NULL, &size);
285 if (r == ERROR_FILE_NOT_FOUND)
287 UUID uuid;
288 WCHAR buf[37];
289 RPC_STATUS rs;
290 static const WCHAR uuidFmt[] = {
291 '%','0','8','x','-','%','0','4','x','-',
292 '%','0','4','x','-','%','0','2','x',
293 '%','0','2','x','-','%','0','2','x',
294 '%','0','2','x','%','0','2','x',
295 '%','0','2','x','%','0','2','x',
296 '%','0','2','x',0 };
298 rs = UuidCreate(&uuid);
299 if (rs == S_OK)
301 sprintfW(buf, uuidFmt,
302 uuid.Data1, uuid.Data2, uuid.Data3,
303 uuid.Data4[0], uuid.Data4[1],
304 uuid.Data4[2], uuid.Data4[3],
305 uuid.Data4[4], uuid.Data4[5],
306 uuid.Data4[6], uuid.Data4[7] );
307 RegSetValueExW(key, machineGuidW, 0, REG_SZ,
308 (const BYTE *)buf,
309 (lstrlenW(buf)+1)*sizeof(WCHAR));
312 RegCloseKey(key);
316 /******************************************************************************
317 * CryptAcquireContextW (ADVAPI32.@)
319 * Acquire a crypto provider context handle.
321 * PARAMS
322 * phProv [O] Pointer to HCRYPTPROV for the output.
323 * pszContainer [I] Key Container Name
324 * pszProvider [I] Cryptographic Service Provider Name
325 * dwProvType [I] Crypto provider type to get a handle.
326 * dwFlags [I] flags for the operation
328 * RETURNS
329 * TRUE on success, FALSE on failure.
331 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
332 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
334 PCRYPTPROV pProv = NULL;
335 HKEY key;
336 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
337 PSTR provnameA = NULL, pszContainerA = NULL;
338 DWORD keytype, type, len;
339 ULONG r;
340 static const WCHAR nameW[] = {'N','a','m','e',0};
341 static const WCHAR typeW[] = {'T','y','p','e',0};
342 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
344 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
345 debugstr_w(pszProvider), dwProvType, dwFlags);
347 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
349 SetLastError(NTE_BAD_PROV_TYPE);
350 return FALSE;
353 if (!phProv)
355 SetLastError(ERROR_INVALID_PARAMETER);
356 return FALSE;
359 /* Make sure the MachineGuid value exists */
360 CRYPT_CreateMachineGuid();
362 if (!pszProvider || !*pszProvider)
364 /* No CSP name specified so try the user default CSP first
365 * then try the machine default CSP
367 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
368 TRACE("No provider registered for crypto provider type %d.\n", dwProvType);
369 SetLastError(NTE_PROV_TYPE_NOT_DEF);
370 return FALSE;
372 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
374 CRYPT_Free(keyname);
375 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
376 TRACE("No type registered for crypto provider type %d.\n", dwProvType);
377 RegCloseKey(key);
378 SetLastError(NTE_PROV_TYPE_NOT_DEF);
379 goto error;
381 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
382 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
383 CRYPT_Free(keyname);
384 RegCloseKey(key);
385 SetLastError(NTE_PROV_TYPE_NOT_DEF);
386 goto error;
389 CRYPT_Free(keyname);
390 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
391 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
393 TRACE("error %d reading size of 'Name' from registry\n", r );
394 RegCloseKey(key);
395 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
396 goto error;
398 if(!(provname = CRYPT_Alloc(len)))
400 RegCloseKey(key);
401 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
402 goto error;
404 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
405 if( r != ERROR_SUCCESS )
407 TRACE("error %d reading 'Name' from registry\n", r );
408 RegCloseKey(key);
409 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
410 goto error;
412 RegCloseKey(key);
413 } else {
414 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
416 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
417 goto error;
419 strcpyW(provname, pszProvider);
422 keyname = CRYPT_GetProvKeyName(provname);
423 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
424 CRYPT_Free(keyname);
425 if (r != ERROR_SUCCESS)
427 SetLastError(NTE_KEYSET_NOT_DEF);
428 goto error;
430 len = sizeof(DWORD);
431 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
432 if (r != ERROR_SUCCESS)
434 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
435 goto error;
437 if (type != dwProvType)
439 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
440 SetLastError(NTE_PROV_TYPE_NO_MATCH);
441 goto error;
444 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
445 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
447 TRACE("error %d reading size of 'Image Path' from registry\n", r );
448 RegCloseKey(key);
449 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
450 goto error;
452 if (!(temp = CRYPT_Alloc(len)))
454 RegCloseKey(key);
455 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
456 goto error;
458 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
459 if( r != ERROR_SUCCESS )
461 TRACE("error %d reading 'Image Path' from registry\n", r );
462 RegCloseKey(key);
463 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
464 goto error;
466 RegCloseKey(key);
467 len = ExpandEnvironmentStringsW(temp, NULL, 0);
468 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
470 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
471 goto error;
473 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
475 /* ExpandEnvironmentStrings will call SetLastError */
476 goto error;
478 pProv = CRYPT_LoadProvider(imagepath);
479 if (!pProv) {
480 /* CRYPT_LoadProvider calls SetLastError */
481 goto error;
483 pProv->pVTable->dwProvType = dwProvType;
484 if(!CRYPT_UnicodeToANSI(provname, &provnameA, -1))
486 /* CRYPT_UnicodeToANSI calls SetLastError */
487 goto error;
489 pProv->pVTable->pszProvName = provnameA;
490 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, -1))
492 /* CRYPT_UnicodeToANSI calls SetLastError */
493 goto error;
495 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
497 /* MSDN: When this flag is set, the value returned in phProv is undefined,
498 * and thus, the CryptReleaseContext function need not be called afterwards.
499 * Therefore, we must clean up everything now.
501 if (dwFlags & CRYPT_DELETEKEYSET)
503 pProv->dwMagic = 0;
504 FreeLibrary(pProv->hModule);
505 CRYPT_Free(provnameA);
506 CRYPT_Free(pProv->pVTable);
507 CRYPT_Free(pProv->pFuncs);
508 CRYPT_Free(pProv);
509 } else {
510 *phProv = (HCRYPTPROV)pProv;
512 CRYPT_Free(pszContainerA);
513 CRYPT_Free(provname);
514 CRYPT_Free(temp);
515 CRYPT_Free(imagepath);
516 return TRUE;
518 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
519 error:
520 if (pProv)
522 pProv->dwMagic = 0;
523 if (pProv->hModule)
524 FreeLibrary(pProv->hModule);
525 CRYPT_Free(pProv->pVTable);
526 CRYPT_Free(pProv->pFuncs);
527 CRYPT_Free(pProv);
529 CRYPT_Free(pszContainerA);
530 CRYPT_Free(provnameA);
531 CRYPT_Free(provname);
532 CRYPT_Free(temp);
533 CRYPT_Free(imagepath);
534 return FALSE;
537 /******************************************************************************
538 * CryptAcquireContextA (ADVAPI32.@)
540 * See CryptAcquireContextW.
542 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
543 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
545 PWSTR pProvider = NULL, pContainer = NULL;
546 BOOL ret = FALSE;
548 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, pszContainer,
549 pszProvider, dwProvType, dwFlags);
551 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
553 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
554 return FALSE;
556 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
558 CRYPT_Free(pContainer);
559 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
560 return FALSE;
563 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
565 CRYPT_Free(pContainer);
566 CRYPT_Free(pProvider);
568 return ret;
571 /******************************************************************************
572 * CryptContextAddRef (ADVAPI32.@)
574 * Increases reference count of a cryptographic service provider handle
575 * by one.
577 * PARAMS
578 * hProv [I] Handle to the CSP whose reference is being incremented.
579 * pdwReserved [IN] Reserved for future use and must be NULL.
580 * dwFlags [I] Reserved for future use and must be NULL.
582 * RETURNS
583 * Success: TRUE
584 * Failure: FALSE
586 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
588 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
590 TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
592 if (!pProv)
594 SetLastError(NTE_BAD_UID);
595 return FALSE;
598 if (pProv->dwMagic != MAGIC_CRYPTPROV)
600 SetLastError(ERROR_INVALID_PARAMETER);
601 return FALSE;
604 pProv->refcount++;
605 return TRUE;
608 /******************************************************************************
609 * CryptReleaseContext (ADVAPI32.@)
611 * Releases the handle of a CSP. Reference count is decreased.
613 * PARAMS
614 * hProv [I] Handle of a CSP.
615 * dwFlags [I] Reserved for future use and must be NULL.
617 * RETURNS
618 * Success: TRUE
619 * Failure: FALSE
621 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, ULONG_PTR dwFlags)
623 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
624 BOOL ret = TRUE;
626 TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
628 if (!pProv)
630 SetLastError(NTE_BAD_UID);
631 return FALSE;
634 if (pProv->dwMagic != MAGIC_CRYPTPROV)
636 SetLastError(ERROR_INVALID_PARAMETER);
637 return FALSE;
640 pProv->refcount--;
641 if (pProv->refcount <= 0)
643 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
644 pProv->dwMagic = 0;
645 FreeLibrary(pProv->hModule);
646 #if 0
647 CRYPT_Free(pProv->pVTable->pContextInfo);
648 #endif
649 CRYPT_Free(pProv->pVTable->pszProvName);
650 CRYPT_Free(pProv->pVTable);
651 CRYPT_Free(pProv->pFuncs);
652 CRYPT_Free(pProv);
654 return ret;
657 /******************************************************************************
658 * CryptGenRandom (ADVAPI32.@)
660 * Fills a buffer with cryptographically random bytes.
662 * PARAMS
663 * hProv [I] Handle of a CSP.
664 * dwLen [I] Number of bytes to generate.
665 * pbBuffer [I/O] Buffer to contain random bytes.
667 * RETURNS
668 * Success: TRUE
669 * Failure: FALSE
671 * NOTES
672 * pdBuffer must be at least dwLen bytes long.
674 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
676 PCRYPTPROV prov = (PCRYPTPROV)hProv;
678 TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
680 if (!hProv)
682 SetLastError(ERROR_INVALID_HANDLE);
683 return FALSE;
686 if (prov->dwMagic != MAGIC_CRYPTPROV)
688 SetLastError(ERROR_INVALID_PARAMETER);
689 return FALSE;
692 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
695 /******************************************************************************
696 * CryptCreateHash (ADVAPI32.@)
698 * Initiates the hashing of a stream of data.
700 * PARAMS
701 * hProv [I] Handle of a CSP.
702 * Algid [I] Identifies the hash algorithm to use.
703 * hKey [I] Key for the hash (if required).
704 * dwFlags [I] Reserved for future use and must be NULL.
705 * phHash [O] Address of the future handle to the new hash object.
707 * RETURNS
708 * Success: TRUE
709 * Failure: FALSE
711 * NOTES
712 * If the algorithm is a keyed hash, hKey is the key.
714 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
715 DWORD dwFlags, HCRYPTHASH *phHash)
717 PCRYPTPROV prov = (PCRYPTPROV)hProv;
718 PCRYPTKEY key = (PCRYPTKEY)hKey;
719 PCRYPTHASH hash;
721 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
723 if (!prov)
725 SetLastError(ERROR_INVALID_HANDLE);
726 return FALSE;
728 if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV)
730 SetLastError(ERROR_INVALID_PARAMETER);
731 return FALSE;
733 if (dwFlags)
735 SetLastError(NTE_BAD_FLAGS);
736 return FALSE;
738 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
740 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
741 return FALSE;
744 hash->pProvider = prov;
746 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
747 key ? key->hPrivate : 0, 0, &hash->hPrivate))
749 *phHash = (HCRYPTHASH)hash;
750 return TRUE;
753 /* CSP error! */
754 CRYPT_Free(hash);
755 *phHash = 0;
756 return FALSE;
759 /******************************************************************************
760 * CryptDecrypt (ADVAPI32.@)
762 * Decrypts data encrypted by CryptEncrypt.
764 * PARAMS
765 * hKey [I] Handle to the decryption key.
766 * hHash [I] Handle to a hash object.
767 * Final [I] TRUE if this is the last section to be decrypted.
768 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
769 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
770 * data on return
771 * pdwDataLen [I/O] Length of pbData before and after the call.
773 * RETURNS
774 * Success: TRUE
775 * Failure: FALSE
777 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
778 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
780 PCRYPTPROV prov;
781 PCRYPTKEY key = (PCRYPTKEY)hKey;
782 PCRYPTHASH hash = (PCRYPTHASH)hHash;
784 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
786 if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
788 SetLastError(ERROR_INVALID_PARAMETER);
789 return FALSE;
792 prov = key->pProvider;
793 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
794 Final, dwFlags, pbData, pdwDataLen);
797 /******************************************************************************
798 * CryptDeriveKey (ADVAPI32.@)
800 * Generates session keys derived from a base data value.
802 * PARAMS
803 * hProv [I] Handle to a CSP.
804 * Algid [I] Identifies the symmetric encryption algorithm to use.
805 * hBaseData [I] Handle to a hash object.
806 * dwFlags [I] Type of key to generate.
807 * phKey [I/O] Address of the newly generated key.
809 * RETURNS
810 * Success: TRUE
811 * Failure: FALSE
813 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
814 DWORD dwFlags, HCRYPTKEY *phKey)
816 PCRYPTPROV prov = (PCRYPTPROV)hProv;
817 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
818 PCRYPTKEY key;
820 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
822 if (!prov || !hash)
824 SetLastError(ERROR_INVALID_HANDLE);
825 return FALSE;
827 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV)
829 SetLastError(ERROR_INVALID_PARAMETER);
830 return FALSE;
832 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
834 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
835 return FALSE;
838 key->pProvider = prov;
839 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
841 *phKey = (HCRYPTKEY)key;
842 return TRUE;
845 /* CSP error! */
846 CRYPT_Free(key);
847 *phKey = 0;
848 return FALSE;
851 /******************************************************************************
852 * CryptDestroyHash (ADVAPI32.@)
854 * Destroys the hash object referenced by hHash.
856 * PARAMS
857 * hHash [I] Handle of the hash object to be destroyed.
859 * RETURNS
860 * Success: TRUE
861 * Failure: FALSE
863 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
865 PCRYPTHASH hash = (PCRYPTHASH)hHash;
866 PCRYPTPROV prov;
867 BOOL ret;
869 TRACE("(0x%lx)\n", hHash);
871 if (!hash)
873 SetLastError(ERROR_INVALID_HANDLE);
874 return FALSE;
877 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
879 SetLastError(ERROR_INVALID_PARAMETER);
880 return FALSE;
883 prov = hash->pProvider;
884 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
885 CRYPT_Free(hash);
886 return ret;
889 /******************************************************************************
890 * CryptDestroyKey (ADVAPI32.@)
892 * Releases the handle referenced by hKey.
894 * PARAMS
895 * hKey [I] Handle of the key to be destroyed.
897 * RETURNS
898 * Success: TRUE
899 * Failure: FALSE
901 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
903 PCRYPTKEY key = (PCRYPTKEY)hKey;
904 PCRYPTPROV prov;
905 BOOL ret;
907 TRACE("(0x%lx)\n", hKey);
909 if (!key)
911 SetLastError(ERROR_INVALID_HANDLE);
912 return FALSE;
915 if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
917 SetLastError(ERROR_INVALID_PARAMETER);
918 return FALSE;
921 prov = key->pProvider;
922 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
923 CRYPT_Free(key);
924 return ret;
927 /******************************************************************************
928 * CryptDuplicateHash (ADVAPI32.@)
930 * Duplicates a hash.
932 * PARAMS
933 * hHash [I] Handle to the hash to be copied.
934 * pdwReserved [I] Reserved for future use and must be zero.
935 * dwFlags [I] Reserved for future use and must be zero.
936 * phHash [O] Address of the handle to receive the copy.
938 * RETURNS
939 * Success: TRUE
940 * Failure: FALSE
942 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
943 DWORD dwFlags, HCRYPTHASH *phHash)
945 PCRYPTPROV prov;
946 PCRYPTHASH orghash, newhash;
948 TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
950 orghash = (PCRYPTHASH)hHash;
951 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
952 orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
954 SetLastError(ERROR_INVALID_PARAMETER);
955 return FALSE;
958 prov = orghash->pProvider;
959 if (!prov->pFuncs->pCPDuplicateHash)
961 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
962 return FALSE;
965 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
967 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
968 return FALSE;
971 newhash->pProvider = prov;
972 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
974 *phHash = (HCRYPTHASH)newhash;
975 return TRUE;
977 CRYPT_Free(newhash);
978 return FALSE;
981 /******************************************************************************
982 * CryptDuplicateKey (ADVAPI32.@)
984 * Duplicate a key and the key's state.
986 * PARAMS
987 * hKey [I] Handle of the key to copy.
988 * pdwReserved [I] Reserved for future use and must be NULL.
989 * dwFlags [I] Reserved for future use and must be zero.
990 * phKey [I] Address of the handle to the duplicated key.
992 * RETURNS
993 * Success: TRUE
994 * Failure: FALSE
996 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
998 PCRYPTPROV prov;
999 PCRYPTKEY orgkey, newkey;
1001 TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1003 orgkey = (PCRYPTKEY)hKey;
1004 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
1005 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
1007 SetLastError(ERROR_INVALID_PARAMETER);
1008 return FALSE;
1011 prov = orgkey->pProvider;
1012 if (!prov->pFuncs->pCPDuplicateKey)
1014 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1015 return FALSE;
1018 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1020 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1021 return FALSE;
1024 newkey->pProvider = prov;
1025 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
1027 *phKey = (HCRYPTKEY)newkey;
1028 return TRUE;
1030 CRYPT_Free(newkey);
1031 return FALSE;
1034 /******************************************************************************
1035 * CryptEncrypt (ADVAPI32.@)
1037 * Encrypts data.
1039 * PARAMS
1040 * hKey [I] Handle to the encryption key.
1041 * hHash [I] Handle to a hash object.
1042 * Final [I] TRUE if this is the last section to encrypt.
1043 * dwFlags [I] Can be CRYPT_OAEP.
1044 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1045 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1046 * encrypted data after call.
1047 * dwBufLen [I] Length of the input pbData buffer.
1049 * RETURNS
1050 * Success: TRUE
1051 * Failure: FALSE
1053 * NOTES
1054 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1055 * required for the returned data in pdwDataLen.
1057 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1058 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1060 PCRYPTPROV prov;
1061 PCRYPTKEY key = (PCRYPTKEY)hKey;
1062 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1064 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1066 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1068 SetLastError(ERROR_INVALID_PARAMETER);
1069 return FALSE;
1072 prov = key->pProvider;
1073 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
1074 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1077 /******************************************************************************
1078 * CryptEnumProvidersW (ADVAPI32.@)
1080 * Returns the next available CSP.
1082 * PARAMS
1083 * dwIndex [I] Index of the next provider to be enumerated.
1084 * pdwReserved [I] Reserved for future use and must be NULL.
1085 * dwFlags [I] Reserved for future use and must be zero.
1086 * pdwProvType [O] DWORD designating the type of the provider.
1087 * pszProvName [O] Buffer that receives data from the provider.
1088 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1089 * of bytes stored in the buffer on return.
1091 * RETURNS
1092 * Success: TRUE
1093 * Failure: FALSE
1095 * NOTES
1096 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1097 * for memory allocation purposes.
1099 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
1100 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1102 HKEY hKey;
1103 static const WCHAR providerW[] = {
1104 'S','o','f','t','w','a','r','e','\\',
1105 'M','i','c','r','o','s','o','f','t','\\',
1106 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1107 'D','e','f','a','u','l','t','s','\\',
1108 'P','r','o','v','i','d','e','r',0
1110 static const WCHAR typeW[] = {'T','y','p','e',0};
1112 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1113 pdwProvType, pszProvName, pcbProvName);
1115 if (pdwReserved || !pcbProvName)
1117 SetLastError(ERROR_INVALID_PARAMETER);
1118 return FALSE;
1120 if (dwFlags)
1122 SetLastError(NTE_BAD_FLAGS);
1123 return FALSE;
1126 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1128 SetLastError(NTE_FAIL);
1129 return FALSE;
1132 if (!pszProvName)
1134 DWORD numkeys;
1135 WCHAR *provNameW;
1137 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1138 NULL, NULL, NULL, NULL, NULL, NULL);
1140 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1142 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1143 return FALSE;
1146 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1147 CRYPT_Free(provNameW);
1148 (*pcbProvName)++;
1149 *pcbProvName *= sizeof(WCHAR);
1151 if (dwIndex >= numkeys)
1153 SetLastError(ERROR_NO_MORE_ITEMS);
1154 return FALSE;
1156 } else {
1157 DWORD size = sizeof(DWORD);
1158 DWORD result;
1159 HKEY subkey;
1161 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1162 if (result)
1164 SetLastError(result);
1165 return FALSE;
1167 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1168 return FALSE;
1169 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1170 return FALSE;
1171 RegCloseKey(subkey);
1173 RegCloseKey(hKey);
1174 return TRUE;
1177 /******************************************************************************
1178 * CryptEnumProvidersA (ADVAPI32.@)
1180 * See CryptEnumProvidersW.
1182 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1183 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1185 PWSTR str = NULL;
1186 DWORD bufsize;
1187 BOOL ret; /* = FALSE; */
1189 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1190 pdwProvType, pszProvName, pcbProvName);
1192 if(!CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1193 return FALSE;
1194 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1196 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1197 return FALSE;
1199 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1200 if (str)
1201 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1202 *pcbProvName = bufsize / sizeof(WCHAR); /* FIXME: not correct */
1203 if (str)
1205 CRYPT_Free(str);
1206 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1208 SetLastError(ERROR_MORE_DATA);
1209 return FALSE;
1212 return ret;
1215 /******************************************************************************
1216 * CryptEnumProviderTypesW (ADVAPI32.@)
1218 * Retrieves the next type of CSP supported.
1220 * PARAMS
1221 * dwIndex [I] Index of the next provider to be enumerated.
1222 * pdwReserved [I] Reserved for future use and must be NULL.
1223 * dwFlags [I] Reserved for future use and must be zero.
1224 * pdwProvType [O] DWORD designating the type of the provider.
1225 * pszTypeName [O] Buffer that receives data from the provider type.
1226 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1227 * of bytes stored in the buffer on return.
1229 * RETURNS
1230 * Success: TRUE
1231 * Failure: FALSE
1233 * NOTES
1234 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1235 * for memory allocation purposes.
1237 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1238 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1240 HKEY hKey, hSubkey;
1241 DWORD keylen, numkeys, dwType;
1242 PWSTR keyname, ch;
1243 DWORD result;
1244 static const WCHAR KEYSTR[] = {
1245 'S','o','f','t','w','a','r','e','\\',
1246 'M','i','c','r','o','s','o','f','t','\\',
1247 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1248 'D','e','f','a','u','l','t','s','\\',
1249 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1251 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1253 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1254 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1256 if (pdwReserved || !pdwProvType || !pcbTypeName)
1258 SetLastError(ERROR_INVALID_PARAMETER);
1259 return FALSE;
1261 if (dwFlags)
1263 SetLastError(NTE_BAD_FLAGS);
1264 return FALSE;
1267 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1268 return FALSE;
1270 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1271 if (dwIndex >= numkeys)
1273 SetLastError(ERROR_NO_MORE_ITEMS);
1274 return FALSE;
1276 keylen++;
1277 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1279 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1280 return FALSE;
1282 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1283 CRYPT_Free(keyname);
1284 return FALSE;
1286 RegOpenKeyW(hKey, keyname, &hSubkey);
1287 ch = keyname + strlenW(keyname);
1288 /* Convert "Type 000" to 0, etc/ */
1289 *pdwProvType = *(--ch) - '0';
1290 *pdwProvType += (*(--ch) - '0') * 10;
1291 *pdwProvType += (*(--ch) - '0') * 100;
1292 CRYPT_Free(keyname);
1294 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1295 if (result)
1297 SetLastError(result);
1298 return FALSE;
1301 RegCloseKey(hSubkey);
1302 RegCloseKey(hKey);
1303 return TRUE;
1306 /******************************************************************************
1307 * CryptEnumProviderTypesA (ADVAPI32.@)
1309 * See CryptEnumProviderTypesW.
1311 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1312 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1314 PWSTR str = NULL;
1315 DWORD bufsize;
1316 BOOL ret;
1318 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1319 pdwProvType, pszTypeName, pcbTypeName);
1321 if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1322 return FALSE;
1323 if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1325 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1326 return FALSE;
1328 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1329 if (str)
1330 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1331 *pcbTypeName = bufsize / sizeof(WCHAR);
1332 if (str)
1334 CRYPT_Free(str);
1335 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1337 SetLastError(ERROR_MORE_DATA);
1338 return FALSE;
1341 return ret;
1344 /******************************************************************************
1345 * CryptExportKey (ADVAPI32.@)
1347 * Exports a cryptographic key from a CSP.
1349 * PARAMS
1350 * hKey [I] Handle to the key to export.
1351 * hExpKey [I] Handle to a cryptographic key of the end user.
1352 * dwBlobType [I] Type of BLOB to be exported.
1353 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1354 * pbData [O] Buffer to receive BLOB data.
1355 * pdwDataLen [I/O] Specifies the size of pbData.
1357 * RETURNS
1358 * Success: TRUE
1359 * Failure: FALSE
1361 * NOTES
1362 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1363 * buffer needed to hold the BLOB.
1365 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1366 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1368 PCRYPTPROV prov;
1369 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1371 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1373 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1375 SetLastError(ERROR_INVALID_PARAMETER);
1376 return FALSE;
1379 prov = key->pProvider;
1380 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1381 dwBlobType, dwFlags, pbData, pdwDataLen);
1384 /******************************************************************************
1385 * CryptGenKey (ADVAPI32.@)
1387 * Generates a random cryptographic session key or a pub/priv key pair.
1389 * PARAMS
1390 * hProv [I] Handle to a CSP.
1391 * Algid [I] Algorithm to use to make key.
1392 * dwFlags [I] Specifies type of key to make.
1393 * phKey [I] Address of the handle to which the new key is copied.
1395 * RETURNS
1396 * Success: TRUE
1397 * Failure: FALSE
1399 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1401 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1402 PCRYPTKEY key;
1404 TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1406 if (!prov)
1408 SetLastError(ERROR_INVALID_HANDLE);
1409 return FALSE;
1411 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1413 SetLastError(ERROR_INVALID_PARAMETER);
1414 return FALSE;
1416 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1418 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1419 return FALSE;
1422 key->pProvider = prov;
1424 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1426 *phKey = (HCRYPTKEY)key;
1427 return TRUE;
1430 /* CSP error! */
1431 CRYPT_Free(key);
1432 return FALSE;
1435 /******************************************************************************
1436 * CryptGetDefaultProviderW (ADVAPI32.@)
1438 * Finds the default CSP of a certain provider type.
1440 * PARAMS
1441 * dwProvType [I] Provider type to look for.
1442 * pdwReserved [I] Reserved for future use and must be NULL.
1443 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1444 * pszProvName [O] Name of the default CSP.
1445 * pcbProvName [I/O] Size of pszProvName
1447 * RETURNS
1448 * Success: TRUE
1449 * Failure: FALSE
1451 * NOTES
1452 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1453 * memory allocation purposes on return.
1455 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1456 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1458 HKEY hKey;
1459 PWSTR keyname;
1460 DWORD result;
1461 static const WCHAR nameW[] = {'N','a','m','e',0};
1463 if (pdwReserved || !pcbProvName)
1465 SetLastError(ERROR_INVALID_PARAMETER);
1466 return FALSE;
1468 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1470 SetLastError(NTE_BAD_FLAGS);
1471 return FALSE;
1473 if (dwProvType > 999)
1475 SetLastError(NTE_BAD_PROV_TYPE);
1476 return FALSE;
1478 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1480 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1481 return FALSE;
1483 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1485 CRYPT_Free(keyname);
1486 SetLastError(NTE_PROV_TYPE_NOT_DEF);
1487 return FALSE;
1489 CRYPT_Free(keyname);
1491 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1492 if (result)
1494 if (result != ERROR_MORE_DATA)
1495 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1496 else
1497 SetLastError(result);
1499 return FALSE;
1502 RegCloseKey(hKey);
1503 return TRUE;
1506 /******************************************************************************
1507 * CryptGetDefaultProviderA (ADVAPI32.@)
1509 * See CryptGetDefaultProviderW.
1511 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1512 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1514 PWSTR str = NULL;
1515 DWORD bufsize;
1516 BOOL ret = FALSE;
1518 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1520 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
1521 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1523 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1524 return FALSE;
1526 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1527 if (str)
1528 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1529 *pcbProvName = bufsize / sizeof(WCHAR);
1530 if (str)
1532 CRYPT_Free(str);
1533 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1535 SetLastError(ERROR_MORE_DATA);
1536 return FALSE;
1539 return ret;
1542 /******************************************************************************
1543 * CryptGetHashParam (ADVAPI32.@)
1545 * Retrieves data that controls the operations of a hash object.
1547 * PARAMS
1548 * hHash [I] Handle of the hash object to question.
1549 * dwParam [I] Query type.
1550 * pbData [O] Buffer that receives the value data.
1551 * pdwDataLen [I/O] Size of the pbData buffer.
1552 * dwFlags [I] Reserved for future use and must be zero.
1554 * RETURNS
1555 * Success: TRUE
1556 * Failure: FALSE
1558 * NOTES
1559 * If pbData is NULL, pdwDataLen will contain the length required.
1561 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1562 DWORD *pdwDataLen, DWORD dwFlags)
1564 PCRYPTPROV prov;
1565 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1567 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1569 if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1571 SetLastError(ERROR_INVALID_PARAMETER);
1572 return FALSE;
1575 prov = hash->pProvider;
1576 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1577 pbData, pdwDataLen, dwFlags);
1580 /******************************************************************************
1581 * CryptGetKeyParam (ADVAPI32.@)
1583 * Retrieves data that controls the operations of a key.
1585 * PARAMS
1586 * hKey [I] Handle to they key in question.
1587 * dwParam [I] Specifies query type.
1588 * pbData [O] Sequence of bytes to receive data.
1589 * pdwDataLen [I/O] Size of pbData.
1590 * dwFlags [I] Reserved for future use and must be zero.
1592 * RETURNS
1593 * Success: TRUE
1594 * Failure: FALSE
1596 * NOTES
1597 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1599 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1600 DWORD *pdwDataLen, DWORD dwFlags)
1602 PCRYPTPROV prov;
1603 PCRYPTKEY key = (PCRYPTKEY)hKey;
1605 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1607 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1609 SetLastError(ERROR_INVALID_PARAMETER);
1610 return FALSE;
1613 prov = key->pProvider;
1614 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1615 pbData, pdwDataLen, dwFlags);
1618 /******************************************************************************
1619 * CryptGetProvParam (ADVAPI32.@)
1621 * Retrieves parameters that control the operations of a CSP.
1623 * PARAMS
1624 * hProv [I] Handle of the CSP in question.
1625 * dwParam [I] Specifies query type.
1626 * pbData [O] Buffer to receive the data.
1627 * pdwDataLen [I/O] Size of pbData.
1628 * dwFlags [I] see MSDN Docs.
1630 * RETURNS
1631 * Success: TRUE
1632 * Failure: FALSE
1634 * NOTES
1635 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1637 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1638 DWORD *pdwDataLen, DWORD dwFlags)
1640 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1642 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1644 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1646 SetLastError(ERROR_INVALID_PARAMETER);
1647 return FALSE;
1650 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1653 /******************************************************************************
1654 * CryptGetUserKey (ADVAPI32.@)
1656 * Gets a handle of one of a user's two public/private key pairs.
1658 * PARAMS
1659 * hProv [I] Handle of a CSP.
1660 * dwKeySpec [I] Private key to use.
1661 * phUserKey [O] Pointer to the handle of the retrieved keys.
1663 * RETURNS
1664 * Success: TRUE
1665 * Failure: FALSE
1667 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1669 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1670 PCRYPTKEY key;
1672 TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1674 if (!prov)
1676 SetLastError(ERROR_INVALID_HANDLE);
1677 return FALSE;
1679 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1681 SetLastError(ERROR_INVALID_PARAMETER);
1682 return FALSE;
1684 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1686 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1687 return FALSE;
1690 key->pProvider = prov;
1692 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1694 *phUserKey = (HCRYPTKEY)key;
1695 return TRUE;
1698 /* CSP Error */
1699 CRYPT_Free(key);
1700 *phUserKey = 0;
1701 return FALSE;
1704 /******************************************************************************
1705 * CryptHashData (ADVAPI32.@)
1707 * Adds data to a hash object.
1709 * PARAMS
1710 * hHash [I] Handle of the hash object.
1711 * pbData [I] Buffer of data to be hashed.
1712 * dwDataLen [I] Number of bytes to add.
1713 * dwFlags [I] Can be CRYPT_USERDATA
1715 * RETURNS
1716 * Success: TRUE
1717 * Failure: FALSE
1719 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1721 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1722 PCRYPTPROV prov;
1724 TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1726 if (!hash)
1728 SetLastError(ERROR_INVALID_HANDLE);
1729 return FALSE;
1731 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1733 SetLastError(ERROR_INVALID_PARAMETER);
1734 return FALSE;
1737 prov = hash->pProvider;
1738 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1741 /******************************************************************************
1742 * CryptHashSessionKey (ADVAPI32.@)
1744 * Compute the cryptographic hash of a session key object.
1746 * PARAMS
1747 * hHash [I] Handle to the hash object.
1748 * hKey [I] Handle to the key to be hashed.
1749 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1751 * RETURNS
1752 * Success: TRUE
1753 * Failure: FALSE
1755 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1757 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1758 PCRYPTKEY key = (PCRYPTKEY)hKey;
1759 PCRYPTPROV prov;
1761 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1763 if (!hash || !key)
1765 SetLastError(ERROR_INVALID_HANDLE);
1766 return FALSE;
1769 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1771 SetLastError(ERROR_INVALID_PARAMETER);
1772 return FALSE;
1775 prov = hash->pProvider;
1776 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1779 /******************************************************************************
1780 * CryptImportKey (ADVAPI32.@)
1782 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1784 * PARAMS
1785 * hProv [I] Handle of a CSP.
1786 * pbData [I] Contains the key to be imported.
1787 * dwDataLen [I] Length of the key.
1788 * hPubKey [I] Cryptographic key that decrypts pdData
1789 * dwFlags [I] Used only with a public/private key pair.
1790 * phKey [O] Imported key.
1792 * RETURNS
1793 * Success: TRUE
1794 * Failure: FALSE
1796 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1797 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1799 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1800 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1802 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1804 if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV)
1806 SetLastError(ERROR_INVALID_PARAMETER);
1807 return FALSE;
1810 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1812 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1813 return FALSE;
1816 importkey->pProvider = prov;
1817 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1818 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1820 *phKey = (HCRYPTKEY)importkey;
1821 return TRUE;
1824 CRYPT_Free(importkey);
1825 return FALSE;
1828 /******************************************************************************
1829 * CryptSignHashW (ADVAPI32.@)
1831 * Signs data.
1833 * PARAMS
1834 * hHash [I] Handle of the hash object to be signed.
1835 * dwKeySpec [I] Private key to use.
1836 * sDescription [I] Should be NULL.
1837 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1838 * pbSignature [O] Buffer of the signature data.
1839 * pdwSigLen [I/O] Size of the pbSignature buffer.
1841 * RETURNS
1842 * Success: TRUE
1843 * Failure: FALSE
1845 * NOTES
1846 * Because of security flaws sDescription should not be used and should thus be
1847 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1848 * Providers.
1850 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1851 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1853 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1854 PCRYPTPROV prov;
1856 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1857 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1859 if (!hash)
1861 SetLastError(ERROR_INVALID_HANDLE);
1862 return FALSE;
1864 if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1866 SetLastError(ERROR_INVALID_PARAMETER);
1867 return FALSE;
1870 prov = hash->pProvider;
1871 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1872 dwFlags, pbSignature, pdwSigLen);
1875 /******************************************************************************
1876 * CryptSignHashA (ADVAPI32.@)
1878 * See CryptSignHashW.
1880 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1881 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1883 LPWSTR wsDescription;
1884 BOOL result;
1886 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1887 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1889 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1890 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1891 CRYPT_Free(wsDescription);
1893 return result;
1896 /******************************************************************************
1897 * CryptSetHashParam (ADVAPI32.@)
1899 * Customizes the operations of a hash object.
1901 * PARAMS
1902 * hHash [I] Handle of the hash object to set parameters.
1903 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1904 * pbData [I] Value data buffer.
1905 * dwFlags [I] Reserved for future use and must be zero.
1907 * RETURNS
1908 * Success: TRUE
1909 * Failure: FALSE
1911 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1913 PCRYPTPROV prov;
1914 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1916 TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1918 if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1920 SetLastError(ERROR_INVALID_PARAMETER);
1921 return FALSE;
1924 prov = hash->pProvider;
1925 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1926 dwParam, pbData, dwFlags);
1929 /******************************************************************************
1930 * CryptSetKeyParam (ADVAPI32.@)
1932 * Customizes a session key's operations.
1934 * PARAMS
1935 * hKey [I] Handle to the key to set values.
1936 * dwParam [I] See MSDN Doc.
1937 * pbData [I] Buffer of values to set.
1938 * dwFlags [I] Only used when dwParam == KP_ALGID.
1940 * RETURNS
1941 * Success: TRUE
1942 * Failure: FALSE
1944 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1946 PCRYPTPROV prov;
1947 PCRYPTKEY key = (PCRYPTKEY)hKey;
1949 TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
1951 if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1953 SetLastError(ERROR_INVALID_PARAMETER);
1954 return FALSE;
1957 prov = key->pProvider;
1958 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1959 dwParam, pbData, dwFlags);
1962 /******************************************************************************
1963 * CryptSetProviderA (ADVAPI32.@)
1965 * Specifies the current user's default CSP.
1967 * PARAMS
1968 * pszProvName [I] Name of the new default CSP.
1969 * dwProvType [I] Provider type of the CSP.
1971 * RETURNS
1972 * Success: TRUE
1973 * Failure: FALSE
1975 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
1977 TRACE("(%s, %d)\n", pszProvName, dwProvType);
1978 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1981 /******************************************************************************
1982 * CryptSetProviderW (ADVAPI32.@)
1984 * See CryptSetProviderA.
1986 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
1988 TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
1989 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1992 /******************************************************************************
1993 * CryptSetProviderExW (ADVAPI32.@)
1995 * Specifies the default CSP.
1997 * PARAMS
1998 * pszProvName [I] Name of the new default CSP.
1999 * dwProvType [I] Provider type of the CSP.
2000 * pdwReserved [I] Reserved for future use and must be NULL.
2001 * dwFlags [I] See MSDN Doc.
2003 * RETURNS
2004 * Success: TRUE
2005 * Failure: FALSE
2007 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2009 HKEY hProvKey, hTypeKey;
2010 PWSTR keyname;
2011 static const WCHAR nameW[] = {'N','a','m','e',0};
2013 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2015 if (!pszProvName || pdwReserved)
2017 SetLastError(ERROR_INVALID_PARAMETER);
2018 return FALSE;
2020 if (dwProvType > MAXPROVTYPES)
2022 SetLastError(NTE_BAD_PROV_TYPE);
2023 return FALSE;
2025 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
2026 || dwFlags == CRYPT_DELETE_DEFAULT)
2028 SetLastError(NTE_BAD_FLAGS);
2029 return FALSE;
2032 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2034 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2035 return FALSE;
2037 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2038 keyname, &hTypeKey))
2040 CRYPT_Free(keyname);
2041 SetLastError(NTE_BAD_PROVIDER);
2042 return FALSE;
2044 CRYPT_Free(keyname);
2046 if (dwFlags & CRYPT_DELETE_DEFAULT)
2048 RegDeleteValueW(hTypeKey, nameW);
2050 else
2052 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
2054 RegCloseKey(hTypeKey);
2055 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2056 return FALSE;
2058 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2059 keyname, &hProvKey))
2061 CRYPT_Free(keyname);
2062 RegCloseKey(hTypeKey);
2063 SetLastError(NTE_BAD_PROVIDER);
2064 return FALSE;
2066 CRYPT_Free(keyname);
2068 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
2069 (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2071 RegCloseKey(hTypeKey);
2072 RegCloseKey(hProvKey);
2073 return FALSE;
2076 RegCloseKey(hProvKey);
2078 RegCloseKey(hTypeKey);
2080 return TRUE;
2083 /******************************************************************************
2084 * CryptSetProviderExA (ADVAPI32.@)
2086 * See CryptSetProviderExW.
2088 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2090 BOOL ret = FALSE;
2091 PWSTR str = NULL;
2093 TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2095 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2097 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2098 CRYPT_Free(str);
2100 return ret;
2103 /******************************************************************************
2104 * CryptSetProvParam (ADVAPI32.@)
2106 * Customizes the operations of a CSP.
2108 * PARAMS
2109 * hProv [I] Handle of a CSP.
2110 * dwParam [I] See MSDN Doc.
2111 * pbData [I] Buffer that contains a value to set as a parameter.
2112 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2114 * RETURNS
2115 * Success: TRUE
2116 * Failure: FALSE
2118 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
2120 PCRYPTPROV prov = (PCRYPTPROV)hProv;
2122 TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2124 if (!prov)
2126 SetLastError(ERROR_INVALID_HANDLE);
2127 return FALSE;
2129 if (prov->dwMagic != MAGIC_CRYPTPROV)
2131 SetLastError(ERROR_INVALID_PARAMETER);
2132 return FALSE;
2134 if (dwFlags & PP_USE_HARDWARE_RNG)
2136 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2137 FIXME("\tLetting the CSP decide.\n");
2139 if (dwFlags & PP_CLIENT_HWND)
2141 /* FIXME: Should verify the parameter */
2142 if (pbData /* && IsWindow((HWND)pbData) */)
2144 crypt_hWindow = (HWND)(pbData);
2145 return TRUE;
2146 } else {
2147 SetLastError(ERROR_INVALID_PARAMETER);
2148 return FALSE;
2151 /* All other flags go to the CSP */
2152 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2155 /******************************************************************************
2156 * CryptVerifySignatureW (ADVAPI32.@)
2158 * Verifies the signature of a hash object.
2160 * PARAMS
2161 * hHash [I] Handle of the hash object to verify.
2162 * pbSignature [I] Signature data to verify.
2163 * dwSigLen [I] Size of pbSignature.
2164 * hPubKey [I] Handle to the public key to authenticate signature.
2165 * sDescription [I] Should be NULL.
2166 * dwFlags [I] See MSDN doc.
2168 * RETURNS
2169 * Success: TRUE
2170 * Failure: FALSE
2172 * NOTES
2173 * Because of security flaws sDescription should not be used and should thus be
2174 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2175 * Providers.
2177 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2178 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2180 PCRYPTHASH hash = (PCRYPTHASH)hHash;
2181 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
2182 PCRYPTPROV prov;
2184 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2185 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2187 if (!hash || !key ||
2188 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
2189 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2191 SetLastError(ERROR_INVALID_PARAMETER);
2192 return FALSE;
2195 prov = hash->pProvider;
2196 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2197 key->hPrivate, sDescription, dwFlags);
2200 /******************************************************************************
2201 * CryptVerifySignatureA (ADVAPI32.@)
2203 * See CryptVerifySignatureW.
2205 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2206 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
2208 LPWSTR wsDescription;
2209 BOOL result;
2211 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2212 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
2214 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
2215 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2216 CRYPT_Free(wsDescription);
2218 return result;
2221 /******************************************************************************
2222 * SystemFunction030 (ADVAPI32.@)
2224 * Tests if two blocks of 16 bytes are equal
2226 * PARAMS
2227 * b1,b2 [I] block of 16 bytes
2229 * RETURNS
2230 * TRUE if blocks are the same
2231 * FALSE if blocks are different
2233 BOOL WINAPI SystemFunction030(LPCVOID b1, LPCVOID b2)
2235 return !memcmp(b1, b2, 0x10);
2238 /******************************************************************************
2239 * SystemFunction035 (ADVAPI32.@)
2241 * Described here:
2242 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2244 * NOTES
2245 * Stub, always return TRUE.
2247 BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
2249 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
2250 return TRUE;
2253 /******************************************************************************
2254 * SystemFunction036 (ADVAPI32.@)
2256 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2258 * PARAMS
2259 * pbBufer [O] Pointer to memory to receive random bytes.
2260 * dwLen [I] Number of random bytes to fetch.
2262 * RETURNS
2263 * Success: TRUE
2264 * Failure: FALSE
2267 BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2269 int dev_random;
2271 dev_random = open("/dev/urandom", O_RDONLY);
2272 if (dev_random != -1)
2274 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2276 close(dev_random);
2277 return TRUE;
2279 close(dev_random);
2281 else
2282 FIXME("couldn't open /dev/urandom\n");
2283 SetLastError(NTE_FAIL);
2284 return FALSE;
2288 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2289 in crypt32.dll.
2292 /******************************************************************************
2293 * SystemFunction040 (ADVAPI32.@)
2295 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2297 * PARAMS
2298 * memory [I/O] Pointer to memory to encrypt.
2299 * length [I] Length of region to encrypt in bytes.
2300 * flags [I] Control whether other processes are able to decrypt the memory.
2301 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2302 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2303 * RTL_ENCRYPT_OPTION_SAME_LOGON
2305 * RETURNS
2306 * Success: STATUS_SUCCESS
2307 * Failure: NTSTATUS error code
2309 * NOTES
2310 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2311 * If flags are specified when encrypting, the same flag value must be given
2312 * when decrypting the memory.
2314 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2316 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2317 return STATUS_SUCCESS;
2320 /******************************************************************************
2321 * SystemFunction041 (ADVAPI32.@)
2323 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2325 * PARAMS
2326 * memory [I/O] Pointer to memory to decrypt.
2327 * length [I] Length of region to decrypt in bytes.
2328 * flags [I] Control whether other processes are able to decrypt the memory.
2329 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2330 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2331 * RTL_ENCRYPT_OPTION_SAME_LOGON
2333 * RETURNS
2334 * Success: STATUS_SUCCESS
2335 * Failure: NTSTATUS error code
2337 * NOTES
2338 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2339 * If flags are specified when encrypting, the same flag value must be given
2340 * when decrypting the memory.
2342 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2344 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2345 return STATUS_SUCCESS;