po: Update Portuguese translation.
[wine/multimedia.git] / dlls / advapi32 / crypt.c
blob5f44728da8c07d7796bde174326ff781d75a178f
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, debugstr_a(pszContainer),
549 debugstr_a(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 || !phHash || prov->dwMagic != MAGIC_CRYPTPROV ||
724 (key && key->dwMagic != MAGIC_CRYPTKEY))
726 SetLastError(ERROR_INVALID_PARAMETER);
727 return FALSE;
729 if (dwFlags)
731 SetLastError(NTE_BAD_FLAGS);
732 return FALSE;
734 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
736 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
737 return FALSE;
740 hash->pProvider = prov;
741 hash->dwMagic = MAGIC_CRYPTHASH;
742 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
743 key ? key->hPrivate : 0, 0, &hash->hPrivate))
745 *phHash = (HCRYPTHASH)hash;
746 return TRUE;
749 /* CSP error! */
750 hash->dwMagic = 0;
751 CRYPT_Free(hash);
752 *phHash = 0;
753 return FALSE;
756 /******************************************************************************
757 * CryptDecrypt (ADVAPI32.@)
759 * Decrypts data encrypted by CryptEncrypt.
761 * PARAMS
762 * hKey [I] Handle to the decryption key.
763 * hHash [I] Handle to a hash object.
764 * Final [I] TRUE if this is the last section to be decrypted.
765 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
766 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
767 * data on return
768 * pdwDataLen [I/O] Length of pbData before and after the call.
770 * RETURNS
771 * Success: TRUE
772 * Failure: FALSE
774 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
775 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
777 PCRYPTPROV prov;
778 PCRYPTKEY key = (PCRYPTKEY)hKey;
779 PCRYPTHASH hash = (PCRYPTHASH)hHash;
781 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
783 if (!key || !pbData || !pdwDataLen ||
784 !key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
785 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
787 SetLastError(ERROR_INVALID_PARAMETER);
788 return FALSE;
791 prov = key->pProvider;
792 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
793 Final, dwFlags, pbData, pdwDataLen);
796 /******************************************************************************
797 * CryptDeriveKey (ADVAPI32.@)
799 * Generates session keys derived from a base data value.
801 * PARAMS
802 * hProv [I] Handle to a CSP.
803 * Algid [I] Identifies the symmetric encryption algorithm to use.
804 * hBaseData [I] Handle to a hash object.
805 * dwFlags [I] Type of key to generate.
806 * phKey [I/O] Address of the newly generated key.
808 * RETURNS
809 * Success: TRUE
810 * Failure: FALSE
812 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
813 DWORD dwFlags, HCRYPTKEY *phKey)
815 PCRYPTPROV prov = (PCRYPTPROV)hProv;
816 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
817 PCRYPTKEY key;
819 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
821 if (!prov || !hash)
823 SetLastError(ERROR_INVALID_HANDLE);
824 return FALSE;
826 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV || hash->dwMagic != MAGIC_CRYPTHASH)
828 SetLastError(ERROR_INVALID_PARAMETER);
829 return FALSE;
831 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
833 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
834 return FALSE;
837 key->pProvider = prov;
838 key->dwMagic = MAGIC_CRYPTKEY;
839 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
841 *phKey = (HCRYPTKEY)key;
842 return TRUE;
845 /* CSP error! */
846 key->dwMagic = 0;
847 CRYPT_Free(key);
848 *phKey = 0;
849 return FALSE;
852 /******************************************************************************
853 * CryptDestroyHash (ADVAPI32.@)
855 * Destroys the hash object referenced by hHash.
857 * PARAMS
858 * hHash [I] Handle of the hash object to be destroyed.
860 * RETURNS
861 * Success: TRUE
862 * Failure: FALSE
864 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
866 PCRYPTHASH hash = (PCRYPTHASH)hHash;
867 PCRYPTPROV prov;
868 BOOL ret;
870 TRACE("(0x%lx)\n", hHash);
872 if (!hash)
874 SetLastError(ERROR_INVALID_HANDLE);
875 return FALSE;
878 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
879 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
881 SetLastError(ERROR_INVALID_PARAMETER);
882 return FALSE;
885 prov = hash->pProvider;
886 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
887 hash->dwMagic = 0;
888 CRYPT_Free(hash);
889 return ret;
892 /******************************************************************************
893 * CryptDestroyKey (ADVAPI32.@)
895 * Releases the handle referenced by hKey.
897 * PARAMS
898 * hKey [I] Handle of the key to be destroyed.
900 * RETURNS
901 * Success: TRUE
902 * Failure: FALSE
904 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
906 PCRYPTKEY key = (PCRYPTKEY)hKey;
907 PCRYPTPROV prov;
908 BOOL ret;
910 TRACE("(0x%lx)\n", hKey);
912 if (!key)
914 SetLastError(ERROR_INVALID_HANDLE);
915 return FALSE;
918 if (!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
919 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
921 SetLastError(ERROR_INVALID_PARAMETER);
922 return FALSE;
925 prov = key->pProvider;
926 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
927 key->dwMagic = 0;
928 CRYPT_Free(key);
929 return ret;
932 /******************************************************************************
933 * CryptDuplicateHash (ADVAPI32.@)
935 * Duplicates a hash.
937 * PARAMS
938 * hHash [I] Handle to the hash to be copied.
939 * pdwReserved [I] Reserved for future use and must be zero.
940 * dwFlags [I] Reserved for future use and must be zero.
941 * phHash [O] Address of the handle to receive the copy.
943 * RETURNS
944 * Success: TRUE
945 * Failure: FALSE
947 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
948 DWORD dwFlags, HCRYPTHASH *phHash)
950 PCRYPTPROV prov;
951 PCRYPTHASH orghash, newhash;
953 TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
955 orghash = (PCRYPTHASH)hHash;
956 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
957 orghash->dwMagic != MAGIC_CRYPTHASH || orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
959 SetLastError(ERROR_INVALID_PARAMETER);
960 return FALSE;
963 prov = orghash->pProvider;
964 if (!prov->pFuncs->pCPDuplicateHash)
966 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
967 return FALSE;
970 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
972 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
973 return FALSE;
976 newhash->pProvider = prov;
977 newhash->dwMagic = MAGIC_CRYPTHASH;
978 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
980 *phHash = (HCRYPTHASH)newhash;
981 return TRUE;
983 newhash->dwMagic = 0;
984 CRYPT_Free(newhash);
985 return FALSE;
988 /******************************************************************************
989 * CryptDuplicateKey (ADVAPI32.@)
991 * Duplicate a key and the key's state.
993 * PARAMS
994 * hKey [I] Handle of the key to copy.
995 * pdwReserved [I] Reserved for future use and must be NULL.
996 * dwFlags [I] Reserved for future use and must be zero.
997 * phKey [I] Address of the handle to the duplicated key.
999 * RETURNS
1000 * Success: TRUE
1001 * Failure: FALSE
1003 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
1005 PCRYPTPROV prov;
1006 PCRYPTKEY orgkey, newkey;
1008 TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1010 orgkey = (PCRYPTKEY)hKey;
1011 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
1012 orgkey->dwMagic != MAGIC_CRYPTKEY ||
1013 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
1015 SetLastError(ERROR_INVALID_PARAMETER);
1016 return FALSE;
1019 prov = orgkey->pProvider;
1020 if (!prov->pFuncs->pCPDuplicateKey)
1022 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1023 return FALSE;
1026 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1028 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1029 return FALSE;
1032 newkey->pProvider = prov;
1033 newkey->dwMagic = MAGIC_CRYPTKEY;
1034 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
1036 *phKey = (HCRYPTKEY)newkey;
1037 return TRUE;
1039 newkey->dwMagic = 0;
1040 CRYPT_Free(newkey);
1041 return FALSE;
1044 /******************************************************************************
1045 * CryptEncrypt (ADVAPI32.@)
1047 * Encrypts data.
1049 * PARAMS
1050 * hKey [I] Handle to the encryption key.
1051 * hHash [I] Handle to a hash object.
1052 * Final [I] TRUE if this is the last section to encrypt.
1053 * dwFlags [I] Can be CRYPT_OAEP.
1054 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1055 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1056 * encrypted data after call.
1057 * dwBufLen [I] Length of the input pbData buffer.
1059 * RETURNS
1060 * Success: TRUE
1061 * Failure: FALSE
1063 * NOTES
1064 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1065 * required for the returned data in pdwDataLen.
1067 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1068 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1070 PCRYPTPROV prov;
1071 PCRYPTKEY key = (PCRYPTKEY)hKey;
1072 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1074 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1076 if (!key || !pdwDataLen || !key->pProvider ||
1077 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1079 SetLastError(ERROR_INVALID_PARAMETER);
1080 return FALSE;
1083 prov = key->pProvider;
1084 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
1085 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1088 /******************************************************************************
1089 * CryptEnumProvidersW (ADVAPI32.@)
1091 * Returns the next available CSP.
1093 * PARAMS
1094 * dwIndex [I] Index of the next provider to be enumerated.
1095 * pdwReserved [I] Reserved for future use and must be NULL.
1096 * dwFlags [I] Reserved for future use and must be zero.
1097 * pdwProvType [O] DWORD designating the type of the provider.
1098 * pszProvName [O] Buffer that receives data from the provider.
1099 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1100 * of bytes stored in the buffer on return.
1102 * RETURNS
1103 * Success: TRUE
1104 * Failure: FALSE
1106 * NOTES
1107 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1108 * for memory allocation purposes.
1110 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
1111 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1113 HKEY hKey;
1114 static const WCHAR providerW[] = {
1115 'S','o','f','t','w','a','r','e','\\',
1116 'M','i','c','r','o','s','o','f','t','\\',
1117 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1118 'D','e','f','a','u','l','t','s','\\',
1119 'P','r','o','v','i','d','e','r',0
1121 static const WCHAR typeW[] = {'T','y','p','e',0};
1122 BOOL ret;
1124 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1125 pdwProvType, pszProvName, pcbProvName);
1127 if (pdwReserved || !pcbProvName)
1129 SetLastError(ERROR_INVALID_PARAMETER);
1130 return FALSE;
1132 if (dwFlags)
1134 SetLastError(NTE_BAD_FLAGS);
1135 return FALSE;
1138 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1140 SetLastError(NTE_FAIL);
1141 return FALSE;
1144 ret = TRUE;
1145 if (!pszProvName)
1147 DWORD numkeys;
1148 WCHAR *provNameW;
1150 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1151 NULL, NULL, NULL, NULL, NULL, NULL);
1153 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1155 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1156 RegCloseKey(hKey);
1157 return FALSE;
1160 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1161 CRYPT_Free(provNameW);
1162 (*pcbProvName)++;
1163 *pcbProvName *= sizeof(WCHAR);
1165 if (dwIndex >= numkeys)
1167 SetLastError(ERROR_NO_MORE_ITEMS);
1168 ret = FALSE;
1170 } else {
1171 DWORD size = sizeof(DWORD);
1172 DWORD result;
1173 HKEY subkey;
1175 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1176 if (result)
1178 SetLastError(result);
1179 RegCloseKey(hKey);
1180 return FALSE;
1182 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1184 RegCloseKey(hKey);
1185 return FALSE;
1188 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1189 ret = FALSE;
1191 RegCloseKey(subkey);
1193 RegCloseKey(hKey);
1194 return ret;
1197 /******************************************************************************
1198 * CryptEnumProvidersA (ADVAPI32.@)
1200 * See CryptEnumProvidersW.
1202 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1203 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1205 PWSTR str = NULL;
1206 DWORD bufsize;
1207 BOOL ret;
1209 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1210 pdwProvType, pszProvName, pcbProvName);
1212 if(!CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1213 return FALSE;
1214 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1216 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1217 return FALSE;
1219 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1220 if (str)
1221 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1222 *pcbProvName = bufsize / sizeof(WCHAR); /* FIXME: not correct */
1223 if (str)
1225 CRYPT_Free(str);
1226 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1228 SetLastError(ERROR_MORE_DATA);
1229 return FALSE;
1232 return ret;
1235 /******************************************************************************
1236 * CryptEnumProviderTypesW (ADVAPI32.@)
1238 * Retrieves the next type of CSP supported.
1240 * PARAMS
1241 * dwIndex [I] Index of the next provider to be enumerated.
1242 * pdwReserved [I] Reserved for future use and must be NULL.
1243 * dwFlags [I] Reserved for future use and must be zero.
1244 * pdwProvType [O] DWORD designating the type of the provider.
1245 * pszTypeName [O] Buffer that receives data from the provider type.
1246 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1247 * of bytes stored in the buffer on return.
1249 * RETURNS
1250 * Success: TRUE
1251 * Failure: FALSE
1253 * NOTES
1254 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1255 * for memory allocation purposes.
1257 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1258 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1260 HKEY hKey, hSubkey;
1261 DWORD keylen, numkeys, dwType;
1262 PWSTR keyname, ch;
1263 DWORD result;
1264 static const WCHAR KEYSTR[] = {
1265 'S','o','f','t','w','a','r','e','\\',
1266 'M','i','c','r','o','s','o','f','t','\\',
1267 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1268 'D','e','f','a','u','l','t','s','\\',
1269 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1271 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1273 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1274 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1276 if (pdwReserved || !pdwProvType || !pcbTypeName)
1278 SetLastError(ERROR_INVALID_PARAMETER);
1279 return FALSE;
1281 if (dwFlags)
1283 SetLastError(NTE_BAD_FLAGS);
1284 return FALSE;
1287 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1288 return FALSE;
1290 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1291 if (dwIndex >= numkeys)
1293 SetLastError(ERROR_NO_MORE_ITEMS);
1294 RegCloseKey(hKey);
1295 return FALSE;
1297 keylen++;
1298 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1300 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1301 RegCloseKey(hKey);
1302 return FALSE;
1304 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1305 CRYPT_Free(keyname);
1306 RegCloseKey(hKey);
1307 return FALSE;
1309 RegOpenKeyW(hKey, keyname, &hSubkey);
1310 RegCloseKey(hKey);
1312 ch = keyname + strlenW(keyname);
1313 /* Convert "Type 000" to 0, etc/ */
1314 *pdwProvType = *(--ch) - '0';
1315 *pdwProvType += (*(--ch) - '0') * 10;
1316 *pdwProvType += (*(--ch) - '0') * 100;
1317 CRYPT_Free(keyname);
1319 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1320 if (result)
1322 SetLastError(result);
1323 RegCloseKey(hSubkey);
1324 return FALSE;
1327 RegCloseKey(hSubkey);
1328 return TRUE;
1331 /******************************************************************************
1332 * CryptEnumProviderTypesA (ADVAPI32.@)
1334 * See CryptEnumProviderTypesW.
1336 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1337 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1339 PWSTR str = NULL;
1340 DWORD bufsize;
1341 BOOL ret;
1343 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1344 pdwProvType, pszTypeName, pcbTypeName);
1346 if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1347 return FALSE;
1348 if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1350 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1351 return FALSE;
1353 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1354 if (str)
1355 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1356 *pcbTypeName = bufsize / sizeof(WCHAR);
1357 if (str)
1359 CRYPT_Free(str);
1360 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1362 SetLastError(ERROR_MORE_DATA);
1363 return FALSE;
1366 return ret;
1369 /******************************************************************************
1370 * CryptExportKey (ADVAPI32.@)
1372 * Exports a cryptographic key from a CSP.
1374 * PARAMS
1375 * hKey [I] Handle to the key to export.
1376 * hExpKey [I] Handle to a cryptographic key of the end user.
1377 * dwBlobType [I] Type of BLOB to be exported.
1378 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1379 * pbData [O] Buffer to receive BLOB data.
1380 * pdwDataLen [I/O] Specifies the size of pbData.
1382 * RETURNS
1383 * Success: TRUE
1384 * Failure: FALSE
1386 * NOTES
1387 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1388 * buffer needed to hold the BLOB.
1390 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1391 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1393 PCRYPTPROV prov;
1394 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1396 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1398 if (!key || !pdwDataLen || !key->pProvider ||
1399 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1401 SetLastError(ERROR_INVALID_PARAMETER);
1402 return FALSE;
1405 prov = key->pProvider;
1406 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1407 dwBlobType, dwFlags, pbData, pdwDataLen);
1410 /******************************************************************************
1411 * CryptGenKey (ADVAPI32.@)
1413 * Generates a random cryptographic session key or a pub/priv key pair.
1415 * PARAMS
1416 * hProv [I] Handle to a CSP.
1417 * Algid [I] Algorithm to use to make key.
1418 * dwFlags [I] Specifies type of key to make.
1419 * phKey [I] Address of the handle to which the new key is copied.
1421 * RETURNS
1422 * Success: TRUE
1423 * Failure: FALSE
1425 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1427 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1428 PCRYPTKEY key;
1430 TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1432 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1434 SetLastError(ERROR_INVALID_PARAMETER);
1435 return FALSE;
1437 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1439 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1440 return FALSE;
1443 key->pProvider = prov;
1444 key->dwMagic = MAGIC_CRYPTKEY;
1445 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1447 *phKey = (HCRYPTKEY)key;
1448 return TRUE;
1451 /* CSP error! */
1452 key->dwMagic = 0;
1453 CRYPT_Free(key);
1454 return FALSE;
1457 /******************************************************************************
1458 * CryptGetDefaultProviderW (ADVAPI32.@)
1460 * Finds the default CSP of a certain provider type.
1462 * PARAMS
1463 * dwProvType [I] Provider type to look for.
1464 * pdwReserved [I] Reserved for future use and must be NULL.
1465 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1466 * pszProvName [O] Name of the default CSP.
1467 * pcbProvName [I/O] Size of pszProvName
1469 * RETURNS
1470 * Success: TRUE
1471 * Failure: FALSE
1473 * NOTES
1474 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1475 * memory allocation purposes on return.
1477 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1478 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1480 HKEY hKey;
1481 PWSTR keyname;
1482 DWORD result;
1483 static const WCHAR nameW[] = {'N','a','m','e',0};
1485 if (pdwReserved || !pcbProvName)
1487 SetLastError(ERROR_INVALID_PARAMETER);
1488 return FALSE;
1490 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1492 SetLastError(NTE_BAD_FLAGS);
1493 return FALSE;
1495 if (dwProvType > 999)
1497 SetLastError(NTE_BAD_PROV_TYPE);
1498 return FALSE;
1500 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1502 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1503 return FALSE;
1505 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1507 CRYPT_Free(keyname);
1508 SetLastError(NTE_PROV_TYPE_NOT_DEF);
1509 return FALSE;
1511 CRYPT_Free(keyname);
1513 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1514 RegCloseKey(hKey);
1516 if (result)
1518 if (result != ERROR_MORE_DATA)
1519 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1520 else
1521 SetLastError(result);
1523 return FALSE;
1526 return TRUE;
1529 /******************************************************************************
1530 * CryptGetDefaultProviderA (ADVAPI32.@)
1532 * See CryptGetDefaultProviderW.
1534 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1535 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1537 PWSTR str = NULL;
1538 DWORD bufsize;
1539 BOOL ret;
1541 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1543 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
1544 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1546 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1547 return FALSE;
1549 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1550 if (str)
1551 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1552 *pcbProvName = bufsize / sizeof(WCHAR);
1553 if (str)
1555 CRYPT_Free(str);
1556 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1558 SetLastError(ERROR_MORE_DATA);
1559 return FALSE;
1562 return ret;
1565 /******************************************************************************
1566 * CryptGetHashParam (ADVAPI32.@)
1568 * Retrieves data that controls the operations of a hash object.
1570 * PARAMS
1571 * hHash [I] Handle of the hash object to question.
1572 * dwParam [I] Query type.
1573 * pbData [O] Buffer that receives the value data.
1574 * pdwDataLen [I/O] Size of the pbData buffer.
1575 * dwFlags [I] Reserved for future use and must be zero.
1577 * RETURNS
1578 * Success: TRUE
1579 * Failure: FALSE
1581 * NOTES
1582 * If pbData is NULL, pdwDataLen will contain the length required.
1584 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1585 DWORD *pdwDataLen, DWORD dwFlags)
1587 PCRYPTPROV prov;
1588 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1590 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1592 if (!hash || !pdwDataLen || !hash->pProvider ||
1593 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1595 SetLastError(ERROR_INVALID_PARAMETER);
1596 return FALSE;
1599 prov = hash->pProvider;
1600 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1601 pbData, pdwDataLen, dwFlags);
1604 /******************************************************************************
1605 * CryptGetKeyParam (ADVAPI32.@)
1607 * Retrieves data that controls the operations of a key.
1609 * PARAMS
1610 * hKey [I] Handle to they key in question.
1611 * dwParam [I] Specifies query type.
1612 * pbData [O] Sequence of bytes to receive data.
1613 * pdwDataLen [I/O] Size of pbData.
1614 * dwFlags [I] Reserved for future use and must be zero.
1616 * RETURNS
1617 * Success: TRUE
1618 * Failure: FALSE
1620 * NOTES
1621 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1623 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1624 DWORD *pdwDataLen, DWORD dwFlags)
1626 PCRYPTPROV prov;
1627 PCRYPTKEY key = (PCRYPTKEY)hKey;
1629 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1631 if (!key || !pdwDataLen || !key->pProvider ||
1632 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1634 SetLastError(ERROR_INVALID_PARAMETER);
1635 return FALSE;
1638 prov = key->pProvider;
1639 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1640 pbData, pdwDataLen, dwFlags);
1643 /******************************************************************************
1644 * CryptGetProvParam (ADVAPI32.@)
1646 * Retrieves parameters that control the operations of a CSP.
1648 * PARAMS
1649 * hProv [I] Handle of the CSP in question.
1650 * dwParam [I] Specifies query type.
1651 * pbData [O] Buffer to receive the data.
1652 * pdwDataLen [I/O] Size of pbData.
1653 * dwFlags [I] see MSDN Docs.
1655 * RETURNS
1656 * Success: TRUE
1657 * Failure: FALSE
1659 * NOTES
1660 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1662 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1663 DWORD *pdwDataLen, DWORD dwFlags)
1665 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1667 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1669 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1671 SetLastError(ERROR_INVALID_PARAMETER);
1672 return FALSE;
1675 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1678 /******************************************************************************
1679 * CryptGetUserKey (ADVAPI32.@)
1681 * Gets a handle of one of a user's two public/private key pairs.
1683 * PARAMS
1684 * hProv [I] Handle of a CSP.
1685 * dwKeySpec [I] Private key to use.
1686 * phUserKey [O] Pointer to the handle of the retrieved keys.
1688 * RETURNS
1689 * Success: TRUE
1690 * Failure: FALSE
1692 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1694 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1695 PCRYPTKEY key;
1697 TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1699 if (!prov)
1701 SetLastError(ERROR_INVALID_HANDLE);
1702 return FALSE;
1704 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1706 SetLastError(ERROR_INVALID_PARAMETER);
1707 return FALSE;
1709 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1711 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1712 return FALSE;
1715 key->pProvider = prov;
1716 key->dwMagic = MAGIC_CRYPTKEY;
1717 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1719 *phUserKey = (HCRYPTKEY)key;
1720 return TRUE;
1723 /* CSP Error */
1724 key->dwMagic = 0;
1725 CRYPT_Free(key);
1726 *phUserKey = 0;
1727 return FALSE;
1730 /******************************************************************************
1731 * CryptHashData (ADVAPI32.@)
1733 * Adds data to a hash object.
1735 * PARAMS
1736 * hHash [I] Handle of the hash object.
1737 * pbData [I] Buffer of data to be hashed.
1738 * dwDataLen [I] Number of bytes to add.
1739 * dwFlags [I] Can be CRYPT_USERDATA
1741 * RETURNS
1742 * Success: TRUE
1743 * Failure: FALSE
1745 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1747 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1748 PCRYPTPROV prov;
1750 TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1752 if (!hash)
1754 SetLastError(ERROR_INVALID_HANDLE);
1755 return FALSE;
1757 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1758 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1760 SetLastError(ERROR_INVALID_PARAMETER);
1761 return FALSE;
1764 prov = hash->pProvider;
1765 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1768 /******************************************************************************
1769 * CryptHashSessionKey (ADVAPI32.@)
1771 * Compute the cryptographic hash of a session key object.
1773 * PARAMS
1774 * hHash [I] Handle to the hash object.
1775 * hKey [I] Handle to the key to be hashed.
1776 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1778 * RETURNS
1779 * Success: TRUE
1780 * Failure: FALSE
1782 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1784 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1785 PCRYPTKEY key = (PCRYPTKEY)hKey;
1786 PCRYPTPROV prov;
1788 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1790 if (!hash || !key)
1792 SetLastError(ERROR_INVALID_HANDLE);
1793 return FALSE;
1796 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1797 hash->pProvider->dwMagic != MAGIC_CRYPTPROV || key->dwMagic != MAGIC_CRYPTKEY)
1799 SetLastError(ERROR_INVALID_PARAMETER);
1800 return FALSE;
1803 prov = hash->pProvider;
1804 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1807 /******************************************************************************
1808 * CryptImportKey (ADVAPI32.@)
1810 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1812 * PARAMS
1813 * hProv [I] Handle of a CSP.
1814 * pbData [I] Contains the key to be imported.
1815 * dwDataLen [I] Length of the key.
1816 * hPubKey [I] Cryptographic key that decrypts pdData
1817 * dwFlags [I] Used only with a public/private key pair.
1818 * phKey [O] Imported key.
1820 * RETURNS
1821 * Success: TRUE
1822 * Failure: FALSE
1824 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1825 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1827 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1828 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1830 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1832 if (!prov || !pbData || !dwDataLen || !phKey ||
1833 prov->dwMagic != MAGIC_CRYPTPROV ||
1834 (pubkey && pubkey->dwMagic != MAGIC_CRYPTKEY))
1836 SetLastError(ERROR_INVALID_PARAMETER);
1837 return FALSE;
1840 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1842 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1843 return FALSE;
1846 importkey->pProvider = prov;
1847 importkey->dwMagic = MAGIC_CRYPTKEY;
1848 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1849 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1851 *phKey = (HCRYPTKEY)importkey;
1852 return TRUE;
1855 importkey->dwMagic = 0;
1856 CRYPT_Free(importkey);
1857 return FALSE;
1860 /******************************************************************************
1861 * CryptSignHashW (ADVAPI32.@)
1863 * Signs data.
1865 * PARAMS
1866 * hHash [I] Handle of the hash object to be signed.
1867 * dwKeySpec [I] Private key to use.
1868 * sDescription [I] Should be NULL.
1869 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1870 * pbSignature [O] Buffer of the signature data.
1871 * pdwSigLen [I/O] Size of the pbSignature buffer.
1873 * RETURNS
1874 * Success: TRUE
1875 * Failure: FALSE
1877 * NOTES
1878 * Because of security flaws sDescription should not be used and should thus be
1879 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1880 * Providers.
1882 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1883 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1885 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1886 PCRYPTPROV prov;
1888 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1889 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1891 if (!hash)
1893 SetLastError(ERROR_INVALID_HANDLE);
1894 return FALSE;
1896 if (!pdwSigLen || !hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1897 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1899 SetLastError(ERROR_INVALID_PARAMETER);
1900 return FALSE;
1903 prov = hash->pProvider;
1904 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1905 dwFlags, pbSignature, pdwSigLen);
1908 /******************************************************************************
1909 * CryptSignHashA (ADVAPI32.@)
1911 * See CryptSignHashW.
1913 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1914 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1916 LPWSTR wsDescription;
1917 BOOL result;
1919 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1920 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1922 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1923 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1924 CRYPT_Free(wsDescription);
1926 return result;
1929 /******************************************************************************
1930 * CryptSetHashParam (ADVAPI32.@)
1932 * Customizes the operations of a hash object.
1934 * PARAMS
1935 * hHash [I] Handle of the hash object to set parameters.
1936 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1937 * pbData [I] Value data buffer.
1938 * dwFlags [I] Reserved for future use and must be zero.
1940 * RETURNS
1941 * Success: TRUE
1942 * Failure: FALSE
1944 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1946 PCRYPTPROV prov;
1947 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1949 TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1951 if (!hash || !pbData || !hash->pProvider ||
1952 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1954 SetLastError(ERROR_INVALID_PARAMETER);
1955 return FALSE;
1958 prov = hash->pProvider;
1959 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1960 dwParam, pbData, dwFlags);
1963 /******************************************************************************
1964 * CryptSetKeyParam (ADVAPI32.@)
1966 * Customizes a session key's operations.
1968 * PARAMS
1969 * hKey [I] Handle to the key to set values.
1970 * dwParam [I] See MSDN Doc.
1971 * pbData [I] Buffer of values to set.
1972 * dwFlags [I] Only used when dwParam == KP_ALGID.
1974 * RETURNS
1975 * Success: TRUE
1976 * Failure: FALSE
1978 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1980 PCRYPTPROV prov;
1981 PCRYPTKEY key = (PCRYPTKEY)hKey;
1983 TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
1985 if (!key || !pbData || !key->pProvider ||
1986 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1988 SetLastError(ERROR_INVALID_PARAMETER);
1989 return FALSE;
1992 prov = key->pProvider;
1993 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1994 dwParam, pbData, dwFlags);
1997 /******************************************************************************
1998 * CryptSetProviderA (ADVAPI32.@)
2000 * Specifies the current user's default CSP.
2002 * PARAMS
2003 * pszProvName [I] Name of the new default CSP.
2004 * dwProvType [I] Provider type of the CSP.
2006 * RETURNS
2007 * Success: TRUE
2008 * Failure: FALSE
2010 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
2012 TRACE("(%s, %d)\n", pszProvName, dwProvType);
2013 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2016 /******************************************************************************
2017 * CryptSetProviderW (ADVAPI32.@)
2019 * See CryptSetProviderA.
2021 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
2023 TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
2024 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2027 /******************************************************************************
2028 * CryptSetProviderExW (ADVAPI32.@)
2030 * Specifies the default CSP.
2032 * PARAMS
2033 * pszProvName [I] Name of the new default CSP.
2034 * dwProvType [I] Provider type of the CSP.
2035 * pdwReserved [I] Reserved for future use and must be NULL.
2036 * dwFlags [I] See MSDN Doc.
2038 * RETURNS
2039 * Success: TRUE
2040 * Failure: FALSE
2042 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2044 HKEY hProvKey, hTypeKey;
2045 PWSTR keyname;
2046 static const WCHAR nameW[] = {'N','a','m','e',0};
2048 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2050 if (!pszProvName || pdwReserved)
2052 SetLastError(ERROR_INVALID_PARAMETER);
2053 return FALSE;
2055 if (dwProvType > MAXPROVTYPES)
2057 SetLastError(NTE_BAD_PROV_TYPE);
2058 return FALSE;
2060 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
2061 || dwFlags == CRYPT_DELETE_DEFAULT)
2063 SetLastError(NTE_BAD_FLAGS);
2064 return FALSE;
2067 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2069 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2070 return FALSE;
2072 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2073 keyname, &hTypeKey))
2075 CRYPT_Free(keyname);
2076 SetLastError(NTE_BAD_PROVIDER);
2077 return FALSE;
2079 CRYPT_Free(keyname);
2081 if (dwFlags & CRYPT_DELETE_DEFAULT)
2083 RegDeleteValueW(hTypeKey, nameW);
2085 else
2087 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
2089 RegCloseKey(hTypeKey);
2090 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2091 return FALSE;
2093 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2094 keyname, &hProvKey))
2096 CRYPT_Free(keyname);
2097 RegCloseKey(hTypeKey);
2098 SetLastError(NTE_BAD_PROVIDER);
2099 return FALSE;
2101 CRYPT_Free(keyname);
2103 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
2104 (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2106 RegCloseKey(hTypeKey);
2107 RegCloseKey(hProvKey);
2108 return FALSE;
2111 RegCloseKey(hProvKey);
2113 RegCloseKey(hTypeKey);
2115 return TRUE;
2118 /******************************************************************************
2119 * CryptSetProviderExA (ADVAPI32.@)
2121 * See CryptSetProviderExW.
2123 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2125 BOOL ret = FALSE;
2126 PWSTR str = NULL;
2128 TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2130 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2132 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2133 CRYPT_Free(str);
2135 return ret;
2138 /******************************************************************************
2139 * CryptSetProvParam (ADVAPI32.@)
2141 * Customizes the operations of a CSP.
2143 * PARAMS
2144 * hProv [I] Handle of a CSP.
2145 * dwParam [I] See MSDN Doc.
2146 * pbData [I] Buffer that contains a value to set as a parameter.
2147 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2149 * RETURNS
2150 * Success: TRUE
2151 * Failure: FALSE
2153 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
2155 PCRYPTPROV prov = (PCRYPTPROV)hProv;
2157 TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2159 if (!prov)
2161 SetLastError(ERROR_INVALID_HANDLE);
2162 return FALSE;
2164 if (prov->dwMagic != MAGIC_CRYPTPROV)
2166 SetLastError(ERROR_INVALID_PARAMETER);
2167 return FALSE;
2169 if (dwFlags & PP_USE_HARDWARE_RNG)
2171 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2172 FIXME("\tLetting the CSP decide.\n");
2174 if (dwFlags & PP_CLIENT_HWND)
2176 /* FIXME: Should verify the parameter */
2177 if (pbData /* && IsWindow((HWND)pbData) */)
2179 crypt_hWindow = (HWND)(pbData);
2180 return TRUE;
2181 } else {
2182 SetLastError(ERROR_INVALID_PARAMETER);
2183 return FALSE;
2186 /* All other flags go to the CSP */
2187 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2190 /******************************************************************************
2191 * CryptVerifySignatureW (ADVAPI32.@)
2193 * Verifies the signature of a hash object.
2195 * PARAMS
2196 * hHash [I] Handle of the hash object to verify.
2197 * pbSignature [I] Signature data to verify.
2198 * dwSigLen [I] Size of pbSignature.
2199 * hPubKey [I] Handle to the public key to authenticate signature.
2200 * sDescription [I] Should be NULL.
2201 * dwFlags [I] See MSDN doc.
2203 * RETURNS
2204 * Success: TRUE
2205 * Failure: FALSE
2207 * NOTES
2208 * Because of security flaws sDescription should not be used and should thus be
2209 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2210 * Providers.
2212 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2213 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2215 PCRYPTHASH hash = (PCRYPTHASH)hHash;
2216 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
2217 PCRYPTPROV prov;
2219 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2220 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2222 if (!hash || !key || key->dwMagic != MAGIC_CRYPTKEY || hash->dwMagic != MAGIC_CRYPTHASH ||
2223 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
2224 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2226 SetLastError(ERROR_INVALID_PARAMETER);
2227 return FALSE;
2230 prov = hash->pProvider;
2231 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2232 key->hPrivate, sDescription, dwFlags);
2235 /******************************************************************************
2236 * CryptVerifySignatureA (ADVAPI32.@)
2238 * See CryptVerifySignatureW.
2240 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2241 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
2243 LPWSTR wsDescription;
2244 BOOL result;
2246 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2247 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
2249 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
2250 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2251 CRYPT_Free(wsDescription);
2253 return result;
2256 /******************************************************************************
2257 * SystemFunction030 (ADVAPI32.@)
2259 * Tests if two blocks of 16 bytes are equal
2261 * PARAMS
2262 * b1,b2 [I] block of 16 bytes
2264 * RETURNS
2265 * TRUE if blocks are the same
2266 * FALSE if blocks are different
2268 BOOL WINAPI SystemFunction030(LPCVOID b1, LPCVOID b2)
2270 return !memcmp(b1, b2, 0x10);
2273 /******************************************************************************
2274 * SystemFunction035 (ADVAPI32.@)
2276 * Described here:
2277 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2279 * NOTES
2280 * Stub, always return TRUE.
2282 BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
2284 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
2285 return TRUE;
2288 /******************************************************************************
2289 * SystemFunction036 (ADVAPI32.@)
2291 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2293 * PARAMS
2294 * pbBufer [O] Pointer to memory to receive random bytes.
2295 * dwLen [I] Number of random bytes to fetch.
2297 * RETURNS
2298 * Success: TRUE
2299 * Failure: FALSE
2302 BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2304 int dev_random;
2306 dev_random = open("/dev/urandom", O_RDONLY);
2307 if (dev_random != -1)
2309 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2311 close(dev_random);
2312 return TRUE;
2314 close(dev_random);
2316 else
2317 FIXME("couldn't open /dev/urandom\n");
2318 SetLastError(NTE_FAIL);
2319 return FALSE;
2323 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2324 in crypt32.dll.
2327 /******************************************************************************
2328 * SystemFunction040 (ADVAPI32.@)
2330 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2332 * PARAMS
2333 * memory [I/O] Pointer to memory to encrypt.
2334 * length [I] Length of region to encrypt in bytes.
2335 * flags [I] Control whether other processes are able to decrypt the memory.
2336 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2337 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2338 * RTL_ENCRYPT_OPTION_SAME_LOGON
2340 * RETURNS
2341 * Success: STATUS_SUCCESS
2342 * Failure: NTSTATUS error code
2344 * NOTES
2345 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2346 * If flags are specified when encrypting, the same flag value must be given
2347 * when decrypting the memory.
2349 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2351 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2352 return STATUS_SUCCESS;
2355 /******************************************************************************
2356 * SystemFunction041 (ADVAPI32.@)
2358 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2360 * PARAMS
2361 * memory [I/O] Pointer to memory to decrypt.
2362 * length [I] Length of region to decrypt in bytes.
2363 * flags [I] Control whether other processes are able to decrypt the memory.
2364 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2365 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2366 * RTL_ENCRYPT_OPTION_SAME_LOGON
2368 * RETURNS
2369 * Success: STATUS_SUCCESS
2370 * Failure: NTSTATUS error code
2372 * NOTES
2373 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2374 * If flags are specified when encrypting, the same flag value must be given
2375 * when decrypting the memory.
2377 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2379 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2380 return STATUS_SUCCESS;