gdi32: Revert an unrelated part of 028617b90ba586bdb30723c700eea888c159ada7.
[wine/multimedia.git] / dlls / advapi32 / crypt.c
blobcae03d8f6bfbad04a5b0ab944af5bcc5194b7acc
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 BOOL CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
187 if (!phWnd)
188 return FALSE;
189 *phWnd = crypt_hWindow;
190 return TRUE;
193 #define CRYPT_GetProvFunc(name) \
194 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
195 #define CRYPT_GetProvFuncOpt(name) \
196 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
197 static PCRYPTPROV CRYPT_LoadProvider(PCWSTR pImage)
199 PCRYPTPROV provider;
200 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
202 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
203 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
204 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
205 if ( !(provider->hModule = LoadLibraryW(pImage)) )
207 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
208 FIXME("Failed to load dll %s\n", debugstr_w(pImage));
209 goto error;
211 provider->dwMagic = MAGIC_CRYPTPROV;
212 provider->refcount = 1;
214 errorcode = NTE_PROVIDER_DLL_FAIL;
215 CRYPT_GetProvFunc(CPAcquireContext);
216 CRYPT_GetProvFunc(CPCreateHash);
217 CRYPT_GetProvFunc(CPDecrypt);
218 CRYPT_GetProvFunc(CPDeriveKey);
219 CRYPT_GetProvFunc(CPDestroyHash);
220 CRYPT_GetProvFunc(CPDestroyKey);
221 CRYPT_GetProvFuncOpt(CPDuplicateHash);
222 CRYPT_GetProvFuncOpt(CPDuplicateKey);
223 CRYPT_GetProvFunc(CPEncrypt);
224 CRYPT_GetProvFunc(CPExportKey);
225 CRYPT_GetProvFunc(CPGenKey);
226 CRYPT_GetProvFunc(CPGenRandom);
227 CRYPT_GetProvFunc(CPGetHashParam);
228 CRYPT_GetProvFunc(CPGetKeyParam);
229 CRYPT_GetProvFunc(CPGetProvParam);
230 CRYPT_GetProvFunc(CPGetUserKey);
231 CRYPT_GetProvFunc(CPHashData);
232 CRYPT_GetProvFunc(CPHashSessionKey);
233 CRYPT_GetProvFunc(CPImportKey);
234 CRYPT_GetProvFunc(CPReleaseContext);
235 CRYPT_GetProvFunc(CPSetHashParam);
236 CRYPT_GetProvFunc(CPSetKeyParam);
237 CRYPT_GetProvFunc(CPSetProvParam);
238 CRYPT_GetProvFunc(CPSignHash);
239 CRYPT_GetProvFunc(CPVerifySignature);
241 /* FIXME: Not sure what the pbContextInfo field is for.
242 * Does it need memory allocation?
244 provider->pVTable->Version = 3;
245 provider->pVTable->pFuncVerifyImage = (FARPROC)CRYPT_VerifyImage;
246 provider->pVTable->pFuncReturnhWnd = (FARPROC)CRYPT_ReturnhWnd;
247 provider->pVTable->dwProvType = 0;
248 provider->pVTable->pbContextInfo = NULL;
249 provider->pVTable->cbContextInfo = 0;
250 provider->pVTable->pszProvName = NULL;
251 return provider;
253 error:
254 SetLastError(errorcode);
255 if (provider)
257 provider->dwMagic = 0;
258 if (provider->hModule)
259 FreeLibrary(provider->hModule);
260 CRYPT_Free(provider->pVTable);
261 CRYPT_Free(provider->pFuncs);
262 CRYPT_Free(provider);
264 return NULL;
266 #undef CRYPT_GetProvFunc
267 #undef CRYPT_GetProvFuncOpt
270 static void CRYPT_CreateMachineGuid(void)
272 static const WCHAR cryptographyW[] = {
273 'S','o','f','t','w','a','r','e','\\',
274 'M','i','c','r','o','s','o','f','t','\\',
275 'C','r','y','p','t','o','g','r','a','p','h','y',0 };
276 static const WCHAR machineGuidW[] = {
277 'M','a','c','h','i','n','e','G','u','i','d',0 };
278 LONG r;
279 HKEY key;
281 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, cryptographyW, 0, KEY_ALL_ACCESS,
282 &key);
283 if (!r)
285 DWORD size;
287 r = RegQueryValueExW(key, machineGuidW, NULL, NULL, NULL, &size);
288 if (r == ERROR_FILE_NOT_FOUND)
290 static const WCHAR rpcrt4[] = {
291 'r','p','c','r','t','4',0 };
292 HMODULE lib = LoadLibraryW(rpcrt4);
294 if (lib)
296 RPC_STATUS (RPC_ENTRY *pUuidCreate)(UUID *);
297 UUID uuid;
298 WCHAR buf[37];
299 RPC_STATUS rs;
300 static const WCHAR uuidFmt[] = {
301 '%','0','8','x','-','%','0','4','x','-',
302 '%','0','4','x','-','%','0','2','x',
303 '%','0','2','x','-','%','0','2','x',
304 '%','0','2','x','%','0','2','x',
305 '%','0','2','x','%','0','2','x',
306 '%','0','2','x',0 };
308 pUuidCreate = (void *)GetProcAddress(lib, "UuidCreate");
309 rs = pUuidCreate(&uuid);
310 if (rs == S_OK)
312 sprintfW(buf, uuidFmt,
313 uuid.Data1, uuid.Data2, uuid.Data3,
314 uuid.Data4[0], uuid.Data4[1],
315 uuid.Data4[2], uuid.Data4[3],
316 uuid.Data4[4], uuid.Data4[5],
317 uuid.Data4[6], uuid.Data4[7] );
318 RegSetValueExW(key, machineGuidW, 0, REG_SZ,
319 (const BYTE *)buf,
320 (lstrlenW(buf)+1)*sizeof(WCHAR));
322 FreeLibrary(lib);
325 RegCloseKey(key);
329 /******************************************************************************
330 * CryptAcquireContextW (ADVAPI32.@)
332 * Acquire a crypto provider context handle.
334 * PARAMS
335 * phProv [O] Pointer to HCRYPTPROV for the output.
336 * pszContainer [I] Key Container Name
337 * pszProvider [I] Cryptographic Service Provider Name
338 * dwProvType [I] Crypto provider type to get a handle.
339 * dwFlags [I] flags for the operation
341 * RETURNS
342 * TRUE on success, FALSE on failure.
344 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
345 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
347 PCRYPTPROV pProv = NULL;
348 HKEY key;
349 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
350 PSTR provnameA = NULL, pszContainerA = NULL;
351 DWORD keytype, type, len;
352 ULONG r;
353 static const WCHAR nameW[] = {'N','a','m','e',0};
354 static const WCHAR typeW[] = {'T','y','p','e',0};
355 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
357 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
358 debugstr_w(pszProvider), dwProvType, dwFlags);
360 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
362 SetLastError(NTE_BAD_PROV_TYPE);
363 return FALSE;
366 if (!phProv)
368 SetLastError(ERROR_INVALID_PARAMETER);
369 return FALSE;
372 /* Make sure the MachineGuid value exists */
373 CRYPT_CreateMachineGuid();
375 if (!pszProvider || !*pszProvider)
377 /* No CSP name specified so try the user default CSP first
378 * then try the machine default CSP
380 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
381 TRACE("No provider registered for crypto provider type %d.\n", dwProvType);
382 SetLastError(NTE_PROV_TYPE_NOT_DEF);
383 return FALSE;
385 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
387 CRYPT_Free(keyname);
388 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
389 TRACE("No type registered for crypto provider type %d.\n", dwProvType);
390 RegCloseKey(key);
391 SetLastError(NTE_PROV_TYPE_NOT_DEF);
392 goto error;
394 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
395 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
396 CRYPT_Free(keyname);
397 RegCloseKey(key);
398 SetLastError(NTE_PROV_TYPE_NOT_DEF);
399 goto error;
402 CRYPT_Free(keyname);
403 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
404 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
406 TRACE("error %d reading size of 'Name' from registry\n", r );
407 RegCloseKey(key);
408 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
409 goto error;
411 if(!(provname = CRYPT_Alloc(len)))
413 RegCloseKey(key);
414 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
415 goto error;
417 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
418 if( r != ERROR_SUCCESS )
420 TRACE("error %d reading 'Name' from registry\n", r );
421 RegCloseKey(key);
422 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
423 goto error;
425 RegCloseKey(key);
426 } else {
427 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
429 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
430 goto error;
432 strcpyW(provname, pszProvider);
435 keyname = CRYPT_GetProvKeyName(provname);
436 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
437 CRYPT_Free(keyname);
438 if (r != ERROR_SUCCESS)
440 SetLastError(NTE_KEYSET_NOT_DEF);
441 goto error;
443 len = sizeof(DWORD);
444 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
445 if (r != ERROR_SUCCESS)
447 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
448 goto error;
450 if (type != dwProvType)
452 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
453 SetLastError(NTE_PROV_TYPE_NO_MATCH);
454 goto error;
457 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
458 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
460 TRACE("error %d reading size of 'Image Path' from registry\n", r );
461 RegCloseKey(key);
462 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
463 goto error;
465 if (!(temp = CRYPT_Alloc(len)))
467 RegCloseKey(key);
468 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
469 goto error;
471 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
472 if( r != ERROR_SUCCESS )
474 TRACE("error %d reading 'Image Path' from registry\n", r );
475 RegCloseKey(key);
476 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
477 goto error;
479 RegCloseKey(key);
480 len = ExpandEnvironmentStringsW(temp, NULL, 0);
481 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
483 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
484 goto error;
486 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
488 /* ExpandEnvironmentStrings will call SetLastError */
489 goto error;
491 pProv = CRYPT_LoadProvider(imagepath);
492 if (!pProv) {
493 /* CRYPT_LoadProvider calls SetLastError */
494 goto error;
496 pProv->pVTable->dwProvType = dwProvType;
497 if(!CRYPT_UnicodeToANSI(provname, &provnameA, -1))
499 /* CRYPT_UnicodeToANSI calls SetLastError */
500 goto error;
502 pProv->pVTable->pszProvName = provnameA;
503 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, -1))
505 /* CRYPT_UnicodeToANSI calls SetLastError */
506 goto error;
508 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
510 /* MSDN: When this flag is set, the value returned in phProv is undefined,
511 * and thus, the CryptReleaseContext function need not be called afterwards.
512 * Therefore, we must clean up everything now.
514 if (dwFlags & CRYPT_DELETEKEYSET)
516 pProv->dwMagic = 0;
517 FreeLibrary(pProv->hModule);
518 CRYPT_Free(provnameA);
519 CRYPT_Free(pProv->pVTable);
520 CRYPT_Free(pProv->pFuncs);
521 CRYPT_Free(pProv);
522 } else {
523 *phProv = (HCRYPTPROV)pProv;
525 CRYPT_Free(pszContainerA);
526 CRYPT_Free(provname);
527 CRYPT_Free(temp);
528 CRYPT_Free(imagepath);
529 return TRUE;
531 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
532 error:
533 if (pProv)
535 pProv->dwMagic = 0;
536 if (pProv->hModule)
537 FreeLibrary(pProv->hModule);
538 CRYPT_Free(pProv->pVTable);
539 CRYPT_Free(pProv->pFuncs);
540 CRYPT_Free(pProv);
542 CRYPT_Free(pszContainerA);
543 CRYPT_Free(provnameA);
544 CRYPT_Free(provname);
545 CRYPT_Free(temp);
546 CRYPT_Free(imagepath);
547 return FALSE;
550 /******************************************************************************
551 * CryptAcquireContextA (ADVAPI32.@)
553 * See CryptAcquireContextW.
555 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
556 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
558 PWSTR pProvider = NULL, pContainer = NULL;
559 BOOL ret = FALSE;
561 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, pszContainer,
562 pszProvider, dwProvType, dwFlags);
564 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
566 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
567 return FALSE;
569 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
571 CRYPT_Free(pContainer);
572 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
573 return FALSE;
576 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
578 CRYPT_Free(pContainer);
579 CRYPT_Free(pProvider);
581 return ret;
584 /******************************************************************************
585 * CryptContextAddRef (ADVAPI32.@)
587 * Increases reference count of a cryptographic service provider handle
588 * by one.
590 * PARAMS
591 * hProv [I] Handle to the CSP whose reference is being incremented.
592 * pdwReserved [IN] Reserved for future use and must be NULL.
593 * dwFlags [I] Reserved for future use and must be NULL.
595 * RETURNS
596 * Success: TRUE
597 * Failure: FALSE
599 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
601 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
603 TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
605 if (!pProv)
607 SetLastError(NTE_BAD_UID);
608 return FALSE;
611 if (pProv->dwMagic != MAGIC_CRYPTPROV)
613 SetLastError(ERROR_INVALID_PARAMETER);
614 return FALSE;
617 pProv->refcount++;
618 return TRUE;
621 /******************************************************************************
622 * CryptReleaseContext (ADVAPI32.@)
624 * Releases the handle of a CSP. Reference count is decreased.
626 * PARAMS
627 * hProv [I] Handle of a CSP.
628 * dwFlags [I] Reserved for future use and must be NULL.
630 * RETURNS
631 * Success: TRUE
632 * Failure: FALSE
634 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, ULONG_PTR dwFlags)
636 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
637 BOOL ret = TRUE;
639 TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
641 if (!pProv)
643 SetLastError(NTE_BAD_UID);
644 return FALSE;
647 if (pProv->dwMagic != MAGIC_CRYPTPROV)
649 SetLastError(ERROR_INVALID_PARAMETER);
650 return FALSE;
653 pProv->refcount--;
654 if (pProv->refcount <= 0)
656 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
657 pProv->dwMagic = 0;
658 FreeLibrary(pProv->hModule);
659 #if 0
660 CRYPT_Free(pProv->pVTable->pContextInfo);
661 #endif
662 CRYPT_Free(pProv->pVTable->pszProvName);
663 CRYPT_Free(pProv->pVTable);
664 CRYPT_Free(pProv->pFuncs);
665 CRYPT_Free(pProv);
667 return ret;
670 /******************************************************************************
671 * CryptGenRandom (ADVAPI32.@)
673 * Fills a buffer with cryptographically random bytes.
675 * PARAMS
676 * hProv [I] Handle of a CSP.
677 * dwLen [I] Number of bytes to generate.
678 * pbBuffer [I/O] Buffer to contain random bytes.
680 * RETURNS
681 * Success: TRUE
682 * Failure: FALSE
684 * NOTES
685 * pdBuffer must be at least dwLen bytes long.
687 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
689 PCRYPTPROV prov = (PCRYPTPROV)hProv;
691 TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
693 if (!hProv)
695 SetLastError(ERROR_INVALID_HANDLE);
696 return FALSE;
699 if (prov->dwMagic != MAGIC_CRYPTPROV)
701 SetLastError(ERROR_INVALID_PARAMETER);
702 return FALSE;
705 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
708 /******************************************************************************
709 * CryptCreateHash (ADVAPI32.@)
711 * Initiates the hashing of a stream of data.
713 * PARAMS
714 * hProv [I] Handle of a CSP.
715 * Algid [I] Identifies the hash algorithm to use.
716 * hKey [I] Key for the hash (if required).
717 * dwFlags [I] Reserved for future use and must be NULL.
718 * phHash [O] Address of the future handle to the new hash object.
720 * RETURNS
721 * Success: TRUE
722 * Failure: FALSE
724 * NOTES
725 * If the algorithm is a keyed hash, hKey is the key.
727 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
728 DWORD dwFlags, HCRYPTHASH *phHash)
730 PCRYPTPROV prov = (PCRYPTPROV)hProv;
731 PCRYPTKEY key = (PCRYPTKEY)hKey;
732 PCRYPTHASH hash;
734 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
736 if (!prov)
738 SetLastError(ERROR_INVALID_HANDLE);
739 return FALSE;
741 if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV)
743 SetLastError(ERROR_INVALID_PARAMETER);
744 return FALSE;
746 if (dwFlags)
748 SetLastError(NTE_BAD_FLAGS);
749 return FALSE;
751 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
753 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
754 return FALSE;
757 hash->pProvider = prov;
759 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
760 key ? key->hPrivate : 0, 0, &hash->hPrivate))
762 *phHash = (HCRYPTHASH)hash;
763 return TRUE;
766 /* CSP error! */
767 CRYPT_Free(hash);
768 *phHash = 0;
769 return FALSE;
772 /******************************************************************************
773 * CryptDecrypt (ADVAPI32.@)
775 * Decrypts data encrypted by CryptEncrypt.
777 * PARAMS
778 * hKey [I] Handle to the decryption key.
779 * hHash [I] Handle to a hash object.
780 * Final [I] TRUE if this is the last section to be decrypted.
781 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
782 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
783 * data on return
784 * pdwDataLen [I/O] Length of pbData before and after the call.
786 * RETURNS
787 * Success: TRUE
788 * Failure: FALSE
790 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
791 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
793 PCRYPTPROV prov;
794 PCRYPTKEY key = (PCRYPTKEY)hKey;
795 PCRYPTHASH hash = (PCRYPTHASH)hHash;
797 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
799 if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
801 SetLastError(ERROR_INVALID_PARAMETER);
802 return FALSE;
805 prov = key->pProvider;
806 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
807 Final, dwFlags, pbData, pdwDataLen);
810 /******************************************************************************
811 * CryptDeriveKey (ADVAPI32.@)
813 * Generates session keys derived from a base data value.
815 * PARAMS
816 * hProv [I] Handle to a CSP.
817 * Algid [I] Identifies the symmetric encryption algorithm to use.
818 * hBaseData [I] Handle to a hash object.
819 * dwFlags [I] Type of key to generate.
820 * phKey [I/O] Address of the newly generated key.
822 * RETURNS
823 * Success: TRUE
824 * Failure: FALSE
826 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
827 DWORD dwFlags, HCRYPTKEY *phKey)
829 PCRYPTPROV prov = (PCRYPTPROV)hProv;
830 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
831 PCRYPTKEY key;
833 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
835 if (!prov || !hash)
837 SetLastError(ERROR_INVALID_HANDLE);
838 return FALSE;
840 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV)
842 SetLastError(ERROR_INVALID_PARAMETER);
843 return FALSE;
845 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
847 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
848 return FALSE;
851 key->pProvider = prov;
852 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
854 *phKey = (HCRYPTKEY)key;
855 return TRUE;
858 /* CSP error! */
859 CRYPT_Free(key);
860 *phKey = 0;
861 return FALSE;
864 /******************************************************************************
865 * CryptDestroyHash (ADVAPI32.@)
867 * Destroys the hash object referenced by hHash.
869 * PARAMS
870 * hHash [I] Handle of the hash object to be destroyed.
872 * RETURNS
873 * Success: TRUE
874 * Failure: FALSE
876 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
878 PCRYPTHASH hash = (PCRYPTHASH)hHash;
879 PCRYPTPROV prov;
880 BOOL ret;
882 TRACE("(0x%lx)\n", hHash);
884 if (!hash)
886 SetLastError(ERROR_INVALID_HANDLE);
887 return FALSE;
890 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
892 SetLastError(ERROR_INVALID_PARAMETER);
893 return FALSE;
896 prov = hash->pProvider;
897 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
898 CRYPT_Free(hash);
899 return ret;
902 /******************************************************************************
903 * CryptDestroyKey (ADVAPI32.@)
905 * Releases the handle referenced by hKey.
907 * PARAMS
908 * hKey [I] Handle of the key to be destroyed.
910 * RETURNS
911 * Success: TRUE
912 * Failure: FALSE
914 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
916 PCRYPTKEY key = (PCRYPTKEY)hKey;
917 PCRYPTPROV prov;
918 BOOL ret;
920 TRACE("(0x%lx)\n", hKey);
922 if (!key)
924 SetLastError(ERROR_INVALID_HANDLE);
925 return FALSE;
928 if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
930 SetLastError(ERROR_INVALID_PARAMETER);
931 return FALSE;
934 prov = key->pProvider;
935 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
936 CRYPT_Free(key);
937 return ret;
940 /******************************************************************************
941 * CryptDuplicateHash (ADVAPI32.@)
943 * Duplicates a hash.
945 * PARAMS
946 * hHash [I] Handle to the hash to be copied.
947 * pdwReserved [I] Reserved for future use and must be zero.
948 * dwFlags [I] Reserved for future use and must be zero.
949 * phHash [O] Address of the handle to receive the copy.
951 * RETURNS
952 * Success: TRUE
953 * Failure: FALSE
955 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
956 DWORD dwFlags, HCRYPTHASH *phHash)
958 PCRYPTPROV prov;
959 PCRYPTHASH orghash, newhash;
961 TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
963 orghash = (PCRYPTHASH)hHash;
964 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
965 orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
967 SetLastError(ERROR_INVALID_PARAMETER);
968 return FALSE;
971 prov = orghash->pProvider;
972 if (!prov->pFuncs->pCPDuplicateHash)
974 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
975 return FALSE;
978 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
980 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
981 return FALSE;
984 newhash->pProvider = prov;
985 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
987 *phHash = (HCRYPTHASH)newhash;
988 return TRUE;
990 CRYPT_Free(newhash);
991 return FALSE;
994 /******************************************************************************
995 * CryptDuplicateKey (ADVAPI32.@)
997 * Duplicate a key and the key's state.
999 * PARAMS
1000 * hKey [I] Handle of the key to copy.
1001 * pdwReserved [I] Reserved for future use and must be NULL.
1002 * dwFlags [I] Reserved for future use and must be zero.
1003 * phKey [I] Address of the handle to the duplicated key.
1005 * RETURNS
1006 * Success: TRUE
1007 * Failure: FALSE
1009 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
1011 PCRYPTPROV prov;
1012 PCRYPTKEY orgkey, newkey;
1014 TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1016 orgkey = (PCRYPTKEY)hKey;
1017 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
1018 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
1020 SetLastError(ERROR_INVALID_PARAMETER);
1021 return FALSE;
1024 prov = orgkey->pProvider;
1025 if (!prov->pFuncs->pCPDuplicateKey)
1027 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1028 return FALSE;
1031 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1033 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1034 return FALSE;
1037 newkey->pProvider = prov;
1038 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
1040 *phKey = (HCRYPTKEY)newkey;
1041 return TRUE;
1043 CRYPT_Free(newkey);
1044 return FALSE;
1047 /******************************************************************************
1048 * CryptEncrypt (ADVAPI32.@)
1050 * Encrypts data.
1052 * PARAMS
1053 * hKey [I] Handle to the encryption key.
1054 * hHash [I] Handle to a hash object.
1055 * Final [I] TRUE if this is the last section to encrypt.
1056 * dwFlags [I] Can be CRYPT_OAEP.
1057 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1058 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1059 * encrypted data after call.
1060 * dwBufLen [I] Length of the input pbData buffer.
1062 * RETURNS
1063 * Success: TRUE
1064 * Failure: FALSE
1066 * NOTES
1067 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1068 * required for the returned data in pdwDataLen.
1070 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1071 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1073 PCRYPTPROV prov;
1074 PCRYPTKEY key = (PCRYPTKEY)hKey;
1075 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1077 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1079 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1081 SetLastError(ERROR_INVALID_PARAMETER);
1082 return FALSE;
1085 prov = key->pProvider;
1086 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
1087 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1090 /******************************************************************************
1091 * CryptEnumProvidersW (ADVAPI32.@)
1093 * Returns the next available CSP.
1095 * PARAMS
1096 * dwIndex [I] Index of the next provider to be enumerated.
1097 * pdwReserved [I] Reserved for future use and must be NULL.
1098 * dwFlags [I] Reserved for future use and must be zero.
1099 * pdwProvType [O] DWORD designating the type of the provider.
1100 * pszProvName [O] Buffer that receives data from the provider.
1101 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1102 * of bytes stored in the buffer on return.
1104 * RETURNS
1105 * Success: TRUE
1106 * Failure: FALSE
1108 * NOTES
1109 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1110 * for memory allocation purposes.
1112 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
1113 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1115 HKEY hKey;
1116 static const WCHAR providerW[] = {
1117 'S','o','f','t','w','a','r','e','\\',
1118 'M','i','c','r','o','s','o','f','t','\\',
1119 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1120 'D','e','f','a','u','l','t','s','\\',
1121 'P','r','o','v','i','d','e','r',0
1123 static const WCHAR typeW[] = {'T','y','p','e',0};
1125 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1126 pdwProvType, pszProvName, pcbProvName);
1128 if (pdwReserved || !pcbProvName)
1130 SetLastError(ERROR_INVALID_PARAMETER);
1131 return FALSE;
1133 if (dwFlags)
1135 SetLastError(NTE_BAD_FLAGS);
1136 return FALSE;
1139 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1141 SetLastError(NTE_FAIL);
1142 return FALSE;
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 return FALSE;
1159 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1160 CRYPT_Free(provNameW);
1161 (*pcbProvName)++;
1162 *pcbProvName *= sizeof(WCHAR);
1164 if (dwIndex >= numkeys)
1166 SetLastError(ERROR_NO_MORE_ITEMS);
1167 return FALSE;
1169 } else {
1170 DWORD size = sizeof(DWORD);
1171 DWORD result;
1172 HKEY subkey;
1174 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1175 if (result)
1177 SetLastError(result);
1178 return FALSE;
1180 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1181 return FALSE;
1182 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1183 return FALSE;
1184 RegCloseKey(subkey);
1186 RegCloseKey(hKey);
1187 return TRUE;
1190 /******************************************************************************
1191 * CryptEnumProvidersA (ADVAPI32.@)
1193 * See CryptEnumProvidersW.
1195 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1196 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1198 PWSTR str = NULL;
1199 DWORD bufsize;
1200 BOOL ret; /* = FALSE; */
1202 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1203 pdwProvType, pszProvName, pcbProvName);
1205 if(!CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1206 return FALSE;
1207 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1209 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1210 return FALSE;
1212 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1213 if (str)
1214 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1215 *pcbProvName = bufsize / sizeof(WCHAR); /* FIXME: not correct */
1216 if (str)
1218 CRYPT_Free(str);
1219 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1221 SetLastError(ERROR_MORE_DATA);
1222 return FALSE;
1225 return ret;
1228 /******************************************************************************
1229 * CryptEnumProviderTypesW (ADVAPI32.@)
1231 * Retrieves the next type of CSP supported.
1233 * PARAMS
1234 * dwIndex [I] Index of the next provider to be enumerated.
1235 * pdwReserved [I] Reserved for future use and must be NULL.
1236 * dwFlags [I] Reserved for future use and must be zero.
1237 * pdwProvType [O] DWORD designating the type of the provider.
1238 * pszTypeName [O] Buffer that receives data from the provider type.
1239 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1240 * of bytes stored in the buffer on return.
1242 * RETURNS
1243 * Success: TRUE
1244 * Failure: FALSE
1246 * NOTES
1247 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1248 * for memory allocation purposes.
1250 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1251 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1253 HKEY hKey, hSubkey;
1254 DWORD keylen, numkeys, dwType;
1255 PWSTR keyname, ch;
1256 DWORD result;
1257 static const WCHAR KEYSTR[] = {
1258 'S','o','f','t','w','a','r','e','\\',
1259 'M','i','c','r','o','s','o','f','t','\\',
1260 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1261 'D','e','f','a','u','l','t','s','\\',
1262 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1264 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1266 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1267 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1269 if (pdwReserved || !pdwProvType || !pcbTypeName)
1271 SetLastError(ERROR_INVALID_PARAMETER);
1272 return FALSE;
1274 if (dwFlags)
1276 SetLastError(NTE_BAD_FLAGS);
1277 return FALSE;
1280 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1281 return FALSE;
1283 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1284 if (dwIndex >= numkeys)
1286 SetLastError(ERROR_NO_MORE_ITEMS);
1287 return FALSE;
1289 keylen++;
1290 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1292 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1293 return FALSE;
1295 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1296 CRYPT_Free(keyname);
1297 return FALSE;
1299 RegOpenKeyW(hKey, keyname, &hSubkey);
1300 ch = keyname + strlenW(keyname);
1301 /* Convert "Type 000" to 0, etc/ */
1302 *pdwProvType = *(--ch) - '0';
1303 *pdwProvType += (*(--ch) - '0') * 10;
1304 *pdwProvType += (*(--ch) - '0') * 100;
1305 CRYPT_Free(keyname);
1307 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1308 if (result)
1310 SetLastError(result);
1311 return FALSE;
1314 RegCloseKey(hSubkey);
1315 RegCloseKey(hKey);
1316 return TRUE;
1319 /******************************************************************************
1320 * CryptEnumProviderTypesA (ADVAPI32.@)
1322 * See CryptEnumProviderTypesW.
1324 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1325 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1327 PWSTR str = NULL;
1328 DWORD bufsize;
1329 BOOL ret;
1331 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1332 pdwProvType, pszTypeName, pcbTypeName);
1334 if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1335 return FALSE;
1336 if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1338 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1339 return FALSE;
1341 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1342 if (str)
1343 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1344 *pcbTypeName = bufsize / sizeof(WCHAR);
1345 if (str)
1347 CRYPT_Free(str);
1348 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1350 SetLastError(ERROR_MORE_DATA);
1351 return FALSE;
1354 return ret;
1357 /******************************************************************************
1358 * CryptExportKey (ADVAPI32.@)
1360 * Exports a cryptographic key from a CSP.
1362 * PARAMS
1363 * hKey [I] Handle to the key to export.
1364 * hExpKey [I] Handle to a cryptographic key of the end user.
1365 * dwBlobType [I] Type of BLOB to be exported.
1366 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1367 * pbData [O] Buffer to receive BLOB data.
1368 * pdwDataLen [I/O] Specifies the size of pbData.
1370 * RETURNS
1371 * Success: TRUE
1372 * Failure: FALSE
1374 * NOTES
1375 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1376 * buffer needed to hold the BLOB.
1378 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1379 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1381 PCRYPTPROV prov;
1382 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1384 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1386 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1388 SetLastError(ERROR_INVALID_PARAMETER);
1389 return FALSE;
1392 prov = key->pProvider;
1393 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1394 dwBlobType, dwFlags, pbData, pdwDataLen);
1397 /******************************************************************************
1398 * CryptGenKey (ADVAPI32.@)
1400 * Generates a random cryptographic session key or a pub/priv key pair.
1402 * PARAMS
1403 * hProv [I] Handle to a CSP.
1404 * Algid [I] Algorithm to use to make key.
1405 * dwFlags [I] Specifies type of key to make.
1406 * phKey [I] Address of the handle to which the new key is copied.
1408 * RETURNS
1409 * Success: TRUE
1410 * Failure: FALSE
1412 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1414 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1415 PCRYPTKEY key;
1417 TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1419 if (!prov)
1421 SetLastError(ERROR_INVALID_HANDLE);
1422 return FALSE;
1424 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1426 SetLastError(ERROR_INVALID_PARAMETER);
1427 return FALSE;
1429 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1431 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1432 return FALSE;
1435 key->pProvider = prov;
1437 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1439 *phKey = (HCRYPTKEY)key;
1440 return TRUE;
1443 /* CSP error! */
1444 CRYPT_Free(key);
1445 return FALSE;
1448 /******************************************************************************
1449 * CryptGetDefaultProviderW (ADVAPI32.@)
1451 * Finds the default CSP of a certain provider type.
1453 * PARAMS
1454 * dwProvType [I] Provider type to look for.
1455 * pdwReserved [I] Reserved for future use and must be NULL.
1456 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1457 * pszProvName [O] Name of the default CSP.
1458 * pcbProvName [I/O] Size of pszProvName
1460 * RETURNS
1461 * Success: TRUE
1462 * Failure: FALSE
1464 * NOTES
1465 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1466 * memory allocation purposes on return.
1468 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1469 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1471 HKEY hKey;
1472 PWSTR keyname;
1473 DWORD result;
1474 static const WCHAR nameW[] = {'N','a','m','e',0};
1476 if (pdwReserved || !pcbProvName)
1478 SetLastError(ERROR_INVALID_PARAMETER);
1479 return FALSE;
1481 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1483 SetLastError(NTE_BAD_FLAGS);
1484 return FALSE;
1486 if (dwProvType > 999)
1488 SetLastError(NTE_BAD_PROV_TYPE);
1489 return FALSE;
1491 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1493 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1494 return FALSE;
1496 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1498 CRYPT_Free(keyname);
1499 SetLastError(NTE_PROV_TYPE_NOT_DEF);
1500 return FALSE;
1502 CRYPT_Free(keyname);
1504 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1505 if (result)
1507 if (result != ERROR_MORE_DATA)
1508 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1509 else
1510 SetLastError(result);
1512 return FALSE;
1515 RegCloseKey(hKey);
1516 return TRUE;
1519 /******************************************************************************
1520 * CryptGetDefaultProviderA (ADVAPI32.@)
1522 * See CryptGetDefaultProviderW.
1524 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1525 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1527 PWSTR str = NULL;
1528 DWORD bufsize;
1529 BOOL ret = FALSE;
1531 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1533 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
1534 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1536 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1537 return FALSE;
1539 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1540 if (str)
1541 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1542 *pcbProvName = bufsize / sizeof(WCHAR);
1543 if (str)
1545 CRYPT_Free(str);
1546 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1548 SetLastError(ERROR_MORE_DATA);
1549 return FALSE;
1552 return ret;
1555 /******************************************************************************
1556 * CryptGetHashParam (ADVAPI32.@)
1558 * Retrieves data that controls the operations of a hash object.
1560 * PARAMS
1561 * hHash [I] Handle of the hash object to question.
1562 * dwParam [I] Query type.
1563 * pbData [O] Buffer that receives the value data.
1564 * pdwDataLen [I/O] Size of the pbData buffer.
1565 * dwFlags [I] Reserved for future use and must be zero.
1567 * RETURNS
1568 * Success: TRUE
1569 * Failure: FALSE
1571 * NOTES
1572 * If pbData is NULL, pdwDataLen will contain the length required.
1574 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1575 DWORD *pdwDataLen, DWORD dwFlags)
1577 PCRYPTPROV prov;
1578 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1580 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1582 if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1584 SetLastError(ERROR_INVALID_PARAMETER);
1585 return FALSE;
1588 prov = hash->pProvider;
1589 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1590 pbData, pdwDataLen, dwFlags);
1593 /******************************************************************************
1594 * CryptGetKeyParam (ADVAPI32.@)
1596 * Retrieves data that controls the operations of a key.
1598 * PARAMS
1599 * hKey [I] Handle to they key in question.
1600 * dwParam [I] Specifies query type.
1601 * pbData [O] Sequence of bytes to receive data.
1602 * pdwDataLen [I/O] Size of pbData.
1603 * dwFlags [I] Reserved for future use and must be zero.
1605 * RETURNS
1606 * Success: TRUE
1607 * Failure: FALSE
1609 * NOTES
1610 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1612 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1613 DWORD *pdwDataLen, DWORD dwFlags)
1615 PCRYPTPROV prov;
1616 PCRYPTKEY key = (PCRYPTKEY)hKey;
1618 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1620 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1622 SetLastError(ERROR_INVALID_PARAMETER);
1623 return FALSE;
1626 prov = key->pProvider;
1627 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1628 pbData, pdwDataLen, dwFlags);
1631 /******************************************************************************
1632 * CryptGetProvParam (ADVAPI32.@)
1634 * Retrieves parameters that control the operations of a CSP.
1636 * PARAMS
1637 * hProv [I] Handle of the CSP in question.
1638 * dwParam [I] Specifies query type.
1639 * pbData [O] Buffer to receive the data.
1640 * pdwDataLen [I/O] Size of pbData.
1641 * dwFlags [I] see MSDN Docs.
1643 * RETURNS
1644 * Success: TRUE
1645 * Failure: FALSE
1647 * NOTES
1648 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1650 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1651 DWORD *pdwDataLen, DWORD dwFlags)
1653 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1655 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1657 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1659 SetLastError(ERROR_INVALID_PARAMETER);
1660 return FALSE;
1663 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1666 /******************************************************************************
1667 * CryptGetUserKey (ADVAPI32.@)
1669 * Gets a handle of one of a user's two public/private key pairs.
1671 * PARAMS
1672 * hProv [I] Handle of a CSP.
1673 * dwKeySpec [I] Private key to use.
1674 * phUserKey [O] Pointer to the handle of the retrieved keys.
1676 * RETURNS
1677 * Success: TRUE
1678 * Failure: FALSE
1680 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1682 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1683 PCRYPTKEY key;
1685 TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1687 if (!prov)
1689 SetLastError(ERROR_INVALID_HANDLE);
1690 return FALSE;
1692 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1694 SetLastError(ERROR_INVALID_PARAMETER);
1695 return FALSE;
1697 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1699 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1700 return FALSE;
1703 key->pProvider = prov;
1705 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1707 *phUserKey = (HCRYPTKEY)key;
1708 return TRUE;
1711 /* CSP Error */
1712 CRYPT_Free(key);
1713 *phUserKey = 0;
1714 return FALSE;
1717 /******************************************************************************
1718 * CryptHashData (ADVAPI32.@)
1720 * Adds data to a hash object.
1722 * PARAMS
1723 * hHash [I] Handle of the hash object.
1724 * pbData [I] Buffer of data to be hashed.
1725 * dwDataLen [I] Number of bytes to add.
1726 * dwFlags [I] Can be CRYPT_USERDATA
1728 * RETURNS
1729 * Success: TRUE
1730 * Failure: FALSE
1732 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1734 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1735 PCRYPTPROV prov;
1737 TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1739 if (!hash)
1741 SetLastError(ERROR_INVALID_HANDLE);
1742 return FALSE;
1744 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1746 SetLastError(ERROR_INVALID_PARAMETER);
1747 return FALSE;
1750 prov = hash->pProvider;
1751 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1754 /******************************************************************************
1755 * CryptHashSessionKey (ADVAPI32.@)
1757 * Compute the cryptographic hash of a session key object.
1759 * PARAMS
1760 * hHash [I] Handle to the hash object.
1761 * hKey [I] Handle to the key to be hashed.
1762 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1764 * RETURNS
1765 * Success: TRUE
1766 * Failure: FALSE
1768 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1770 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1771 PCRYPTKEY key = (PCRYPTKEY)hKey;
1772 PCRYPTPROV prov;
1774 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1776 if (!hash || !key)
1778 SetLastError(ERROR_INVALID_HANDLE);
1779 return FALSE;
1782 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1784 SetLastError(ERROR_INVALID_PARAMETER);
1785 return FALSE;
1788 prov = hash->pProvider;
1789 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1792 /******************************************************************************
1793 * CryptImportKey (ADVAPI32.@)
1795 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1797 * PARAMS
1798 * hProv [I] Handle of a CSP.
1799 * pbData [I] Contains the key to be imported.
1800 * dwDataLen [I] Length of the key.
1801 * hPubKey [I] Cryptographic key that decrypts pdData
1802 * dwFlags [I] Used only with a public/private key pair.
1803 * phKey [O] Imported key.
1805 * RETURNS
1806 * Success: TRUE
1807 * Failure: FALSE
1809 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1810 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1812 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1813 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1815 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1817 if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV)
1819 SetLastError(ERROR_INVALID_PARAMETER);
1820 return FALSE;
1823 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1825 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1826 return FALSE;
1829 importkey->pProvider = prov;
1830 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1831 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1833 *phKey = (HCRYPTKEY)importkey;
1834 return TRUE;
1837 CRYPT_Free(importkey);
1838 return FALSE;
1841 /******************************************************************************
1842 * CryptSignHashW (ADVAPI32.@)
1844 * Signs data.
1846 * PARAMS
1847 * hHash [I] Handle of the hash object to be signed.
1848 * dwKeySpec [I] Private key to use.
1849 * sDescription [I] Should be NULL.
1850 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1851 * pbSignature [O] Buffer of the signature data.
1852 * pdwSigLen [I/O] Size of the pbSignature buffer.
1854 * RETURNS
1855 * Success: TRUE
1856 * Failure: FALSE
1858 * NOTES
1859 * Because of security flaws sDescription should not be used and should thus be
1860 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1861 * Providers.
1863 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1864 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1866 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1867 PCRYPTPROV prov;
1869 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1870 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1872 if (!hash)
1874 SetLastError(ERROR_INVALID_HANDLE);
1875 return FALSE;
1877 if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1879 SetLastError(ERROR_INVALID_PARAMETER);
1880 return FALSE;
1883 prov = hash->pProvider;
1884 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1885 dwFlags, pbSignature, pdwSigLen);
1888 /******************************************************************************
1889 * CryptSignHashA (ADVAPI32.@)
1891 * See CryptSignHashW.
1893 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1894 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1896 LPWSTR wsDescription;
1897 BOOL result;
1899 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1900 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1902 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1903 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1904 CRYPT_Free(wsDescription);
1906 return result;
1909 /******************************************************************************
1910 * CryptSetHashParam (ADVAPI32.@)
1912 * Customizes the operations of a hash object.
1914 * PARAMS
1915 * hHash [I] Handle of the hash object to set parameters.
1916 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1917 * pbData [I] Value data buffer.
1918 * dwFlags [I] Reserved for future use and must be zero.
1920 * RETURNS
1921 * Success: TRUE
1922 * Failure: FALSE
1924 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1926 PCRYPTPROV prov;
1927 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1929 TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1931 if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1933 SetLastError(ERROR_INVALID_PARAMETER);
1934 return FALSE;
1937 prov = hash->pProvider;
1938 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1939 dwParam, pbData, dwFlags);
1942 /******************************************************************************
1943 * CryptSetKeyParam (ADVAPI32.@)
1945 * Customizes a session key's operations.
1947 * PARAMS
1948 * hKey [I] Handle to the key to set values.
1949 * dwParam [I] See MSDN Doc.
1950 * pbData [I] Buffer of values to set.
1951 * dwFlags [I] Only used when dwParam == KP_ALGID.
1953 * RETURNS
1954 * Success: TRUE
1955 * Failure: FALSE
1957 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1959 PCRYPTPROV prov;
1960 PCRYPTKEY key = (PCRYPTKEY)hKey;
1962 TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
1964 if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1966 SetLastError(ERROR_INVALID_PARAMETER);
1967 return FALSE;
1970 prov = key->pProvider;
1971 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1972 dwParam, pbData, dwFlags);
1975 /******************************************************************************
1976 * CryptSetProviderA (ADVAPI32.@)
1978 * Specifies the current user's default CSP.
1980 * PARAMS
1981 * pszProvName [I] Name of the new default CSP.
1982 * dwProvType [I] Provider type of the CSP.
1984 * RETURNS
1985 * Success: TRUE
1986 * Failure: FALSE
1988 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
1990 TRACE("(%s, %d)\n", pszProvName, dwProvType);
1991 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1994 /******************************************************************************
1995 * CryptSetProviderW (ADVAPI32.@)
1997 * See CryptSetProviderA.
1999 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
2001 TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
2002 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2005 /******************************************************************************
2006 * CryptSetProviderExW (ADVAPI32.@)
2008 * Specifies the default CSP.
2010 * PARAMS
2011 * pszProvName [I] Name of the new default CSP.
2012 * dwProvType [I] Provider type of the CSP.
2013 * pdwReserved [I] Reserved for future use and must be NULL.
2014 * dwFlags [I] See MSDN Doc.
2016 * RETURNS
2017 * Success: TRUE
2018 * Failure: FALSE
2020 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2022 HKEY hProvKey, hTypeKey;
2023 PWSTR keyname;
2024 static const WCHAR nameW[] = {'N','a','m','e',0};
2026 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2028 if (!pszProvName || pdwReserved)
2030 SetLastError(ERROR_INVALID_PARAMETER);
2031 return FALSE;
2033 if (dwProvType > MAXPROVTYPES)
2035 SetLastError(NTE_BAD_PROV_TYPE);
2036 return FALSE;
2038 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
2039 || dwFlags == CRYPT_DELETE_DEFAULT)
2041 SetLastError(NTE_BAD_FLAGS);
2042 return FALSE;
2045 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2047 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2048 return FALSE;
2050 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2051 keyname, &hTypeKey))
2053 CRYPT_Free(keyname);
2054 SetLastError(NTE_BAD_PROVIDER);
2055 return FALSE;
2057 CRYPT_Free(keyname);
2059 if (dwFlags & CRYPT_DELETE_DEFAULT)
2061 RegDeleteValueW(hTypeKey, nameW);
2063 else
2065 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
2067 RegCloseKey(hTypeKey);
2068 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2069 return FALSE;
2071 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2072 keyname, &hProvKey))
2074 CRYPT_Free(keyname);
2075 RegCloseKey(hTypeKey);
2076 SetLastError(NTE_BAD_PROVIDER);
2077 return FALSE;
2079 CRYPT_Free(keyname);
2081 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
2082 (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2084 RegCloseKey(hTypeKey);
2085 RegCloseKey(hProvKey);
2086 return FALSE;
2089 RegCloseKey(hProvKey);
2091 RegCloseKey(hTypeKey);
2093 return TRUE;
2096 /******************************************************************************
2097 * CryptSetProviderExA (ADVAPI32.@)
2099 * See CryptSetProviderExW.
2101 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2103 BOOL ret = FALSE;
2104 PWSTR str = NULL;
2106 TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2108 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2110 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2111 CRYPT_Free(str);
2113 return ret;
2116 /******************************************************************************
2117 * CryptSetProvParam (ADVAPI32.@)
2119 * Customizes the operations of a CSP.
2121 * PARAMS
2122 * hProv [I] Handle of a CSP.
2123 * dwParam [I] See MSDN Doc.
2124 * pbData [I] Buffer that contains a value to set as a parameter.
2125 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2127 * RETURNS
2128 * Success: TRUE
2129 * Failure: FALSE
2131 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
2133 PCRYPTPROV prov = (PCRYPTPROV)hProv;
2135 TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2137 if (!prov)
2139 SetLastError(ERROR_INVALID_HANDLE);
2140 return FALSE;
2142 if (prov->dwMagic != MAGIC_CRYPTPROV)
2144 SetLastError(ERROR_INVALID_PARAMETER);
2145 return FALSE;
2147 if (dwFlags & PP_USE_HARDWARE_RNG)
2149 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2150 FIXME("\tLetting the CSP decide.\n");
2152 if (dwFlags & PP_CLIENT_HWND)
2154 /* FIXME: Should verify the parameter */
2155 if (pbData /* && IsWindow((HWND)pbData) */)
2157 crypt_hWindow = (HWND)(pbData);
2158 return TRUE;
2159 } else {
2160 SetLastError(ERROR_INVALID_PARAMETER);
2161 return FALSE;
2164 /* All other flags go to the CSP */
2165 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2168 /******************************************************************************
2169 * CryptVerifySignatureW (ADVAPI32.@)
2171 * Verifies the signature of a hash object.
2173 * PARAMS
2174 * hHash [I] Handle of the hash object to verify.
2175 * pbSignature [I] Signature data to verify.
2176 * dwSigLen [I] Size of pbSignature.
2177 * hPubKey [I] Handle to the public key to authenticate signature.
2178 * sDescription [I] Should be NULL.
2179 * dwFlags [I] See MSDN doc.
2181 * RETURNS
2182 * Success: TRUE
2183 * Failure: FALSE
2185 * NOTES
2186 * Because of security flaws sDescription should not be used and should thus be
2187 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2188 * Providers.
2190 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2191 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2193 PCRYPTHASH hash = (PCRYPTHASH)hHash;
2194 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
2195 PCRYPTPROV prov;
2197 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2198 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2200 if (!hash || !key ||
2201 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
2202 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2204 SetLastError(ERROR_INVALID_PARAMETER);
2205 return FALSE;
2208 prov = hash->pProvider;
2209 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2210 key->hPrivate, sDescription, dwFlags);
2213 /******************************************************************************
2214 * CryptVerifySignatureA (ADVAPI32.@)
2216 * See CryptVerifySignatureW.
2218 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2219 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
2221 LPWSTR wsDescription;
2222 BOOL result;
2224 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2225 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
2227 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
2228 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2229 CRYPT_Free(wsDescription);
2231 return result;
2234 /******************************************************************************
2235 * SystemFunction030 (ADVAPI32.@)
2237 * Tests if two blocks of 16 bytes are equal
2239 * PARAMS
2240 * b1,b2 [I] block of 16 bytes
2242 * RETURNS
2243 * TRUE if blocks are the same
2244 * FALSE if blocks are different
2246 BOOL WINAPI SystemFunction030(PVOID b1, PVOID b2)
2248 return !memcmp(b1, b2, 0x10);
2251 /******************************************************************************
2252 * SystemFunction035 (ADVAPI32.@)
2254 * Described here:
2255 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2257 * NOTES
2258 * Stub, always return TRUE.
2260 BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
2262 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
2263 return TRUE;
2266 /******************************************************************************
2267 * SystemFunction036 (ADVAPI32.@)
2269 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2271 * PARAMS
2272 * pbBufer [O] Pointer to memory to receive random bytes.
2273 * dwLen [I] Number of random bytes to fetch.
2275 * RETURNS
2276 * Success: TRUE
2277 * Failure: FALSE
2280 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2282 int dev_random;
2284 dev_random = open("/dev/urandom", O_RDONLY);
2285 if (dev_random != -1)
2287 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2289 close(dev_random);
2290 return TRUE;
2292 close(dev_random);
2294 else
2295 FIXME("couldn't open /dev/urandom\n");
2296 SetLastError(NTE_FAIL);
2297 return FALSE;
2301 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2302 in crypt32.dll.
2305 /******************************************************************************
2306 * SystemFunction040 (ADVAPI32.@)
2308 * MSDN documents this function as RtlEncryptMemory.
2310 * PARAMS
2311 * memory [I/O] Pointer to memory to encrypt.
2312 * length [I] Length of region to encrypt in bytes.
2313 * flags [I] Control whether other processes are able to decrypt the memory.
2314 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2315 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2316 * RTL_ENCRYPT_OPTION_SAME_LOGON
2318 * RETURNS
2319 * Success: STATUS_SUCCESS
2320 * Failure: NTSTATUS error code
2322 * NOTES
2323 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2324 * If flags are specified when encrypting, the same flag value must be given
2325 * when decrypting the memory.
2327 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2329 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2330 return STATUS_SUCCESS;
2333 /******************************************************************************
2334 * SystemFunction041 (ADVAPI32.@)
2336 * MSDN documents this function as RtlDecryptMemory.
2338 * PARAMS
2339 * memory [I/O] Pointer to memory to decrypt.
2340 * length [I] Length of region to decrypt in bytes.
2341 * flags [I] Control whether other processes are able to decrypt the memory.
2342 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2343 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2344 * RTL_ENCRYPT_OPTION_SAME_LOGON
2346 * RETURNS
2347 * Success: STATUS_SUCCESS
2348 * Failure: NTSTATUS error code
2350 * NOTES
2351 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2352 * If flags are specified when encrypting, the same flag value must be given
2353 * when decrypting the memory.
2355 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2357 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2358 return STATUS_SUCCESS;