Release 20050930.
[wine/gsoc-2012-control.git] / dlls / advapi32 / crypt.c
blobaf17e8243757f74b5d940b80e5ec2794b7dc5ce9
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "crypt.h"
43 #include "winnls.h"
44 #include "winreg.h"
45 #include "wine/debug.h"
46 #include "wine/unicode.h"
47 #include "winternl.h"
48 #include "ntstatus.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
52 HWND crypt_hWindow = 0;
54 #define CRYPT_ReturnLastError(err) do {SetLastError(err); return FALSE;} while(0)
56 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
57 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)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 unsuccessfull, 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 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(PWSTR 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 /******************************************************************************
271 * CryptAcquireContextW (ADVAPI32.@)
272 * Acquire a crypto provider context handle.
274 * PARAMS
275 * phProv: Pointer to HCRYPTPROV for the output.
276 * pszContainer: Key Container Name
277 * pszProvider: Cryptographic Service Provider Name
278 * dwProvType: Crypto provider type to get a handle.
279 * dwFlags: flags for the operation
281 * RETURNS TRUE on success, FALSE on failure.
283 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
284 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
286 PCRYPTPROV pProv = NULL;
287 HKEY key;
288 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
289 PSTR provnameA = NULL, pszContainerA = NULL;
290 DWORD keytype, type, len;
291 ULONG r;
292 static const WCHAR nameW[] = {'N','a','m','e',0};
293 static const WCHAR typeW[] = {'T','y','p','e',0};
294 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
296 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv, debugstr_w(pszContainer),
297 debugstr_w(pszProvider), dwProvType, dwFlags);
299 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
301 SetLastError(NTE_BAD_PROV_TYPE);
302 return FALSE;
305 if (!phProv)
307 SetLastError(ERROR_INVALID_PARAMETER);
308 return FALSE;
311 if (!pszProvider || !*pszProvider)
313 /* No CSP name specified so try the user default CSP first
314 * then try the machine default CSP
316 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
317 TRACE("No provider registered for crypto provider type %ld.\n", dwProvType);
318 SetLastError(NTE_PROV_TYPE_NOT_DEF);
319 return FALSE;
321 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
323 CRYPT_Free(keyname);
324 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
325 TRACE("No type registered for crypto provider type %ld.\n", dwProvType);
326 RegCloseKey(key);
327 SetLastError(NTE_PROV_TYPE_NOT_DEF);
328 goto error;
330 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
331 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
332 CRYPT_Free(keyname);
333 RegCloseKey(key);
334 SetLastError(NTE_PROV_TYPE_NOT_DEF);
335 goto error;
338 CRYPT_Free(keyname);
339 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
340 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
342 TRACE("error %ld reading size of 'Name' from registry\n", r );
343 RegCloseKey(key);
344 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
345 goto error;
347 if(!(provname = CRYPT_Alloc(len)))
349 RegCloseKey(key);
350 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
351 goto error;
353 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
354 if( r != ERROR_SUCCESS )
356 TRACE("error %ld reading 'Name' from registry\n", r );
357 RegCloseKey(key);
358 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
359 goto error;
361 RegCloseKey(key);
362 } else {
363 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
365 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
366 goto error;
368 strcpyW(provname, pszProvider);
371 keyname = CRYPT_GetProvKeyName(provname);
372 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
373 CRYPT_Free(keyname);
374 if (r != ERROR_SUCCESS)
376 SetLastError(NTE_KEYSET_NOT_DEF);
377 goto error;
379 len = sizeof(DWORD);
380 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
381 if (r != ERROR_SUCCESS)
383 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
384 goto error;
386 if (type != dwProvType)
388 TRACE("Crypto provider has wrong type (%ld vs expected %ld).\n", type, dwProvType);
389 SetLastError(NTE_PROV_TYPE_NO_MATCH);
390 goto error;
393 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
394 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
396 TRACE("error %ld reading size of 'Image Path' from registry\n", r );
397 RegCloseKey(key);
398 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
399 goto error;
401 if (!(temp = CRYPT_Alloc(len)))
403 RegCloseKey(key);
404 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
405 goto error;
407 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
408 if( r != ERROR_SUCCESS )
410 TRACE("error %ld reading 'Image Path' from registry\n", r );
411 RegCloseKey(key);
412 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
413 goto error;
415 RegCloseKey(key);
416 len = ExpandEnvironmentStringsW(temp, NULL, 0);
417 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
419 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
420 goto error;
422 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
424 /* ExpandEnvironmentStrings will call SetLastError */
425 goto error;
427 pProv = CRYPT_LoadProvider(imagepath);
428 if (!pProv) {
429 /* CRYPT_LoadProvider calls SetLastError */
430 goto error;
432 pProv->pVTable->dwProvType = dwProvType;
433 if(!CRYPT_UnicodeToANSI(provname, &provnameA, -1))
435 /* CRYPT_UnicodeToANSI calls SetLastError */
436 goto error;
438 pProv->pVTable->pszProvName = provnameA;
439 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, -1))
441 /* CRYPT_UnicodeToANSI calls SetLastError */
442 goto error;
444 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
446 /* MSDN: When this flag is set, the value returned in phProv is undefined,
447 * and thus, the CryptReleaseContext function need not be called afterwards.
448 * Therefore, we must clean up everything now.
450 if (dwFlags & CRYPT_DELETEKEYSET)
452 pProv->dwMagic = 0;
453 FreeLibrary(pProv->hModule);
454 CRYPT_Free(provnameA);
455 CRYPT_Free(pProv->pVTable);
456 CRYPT_Free(pProv->pFuncs);
457 CRYPT_Free(pProv);
458 } else {
459 *phProv = (HCRYPTPROV)pProv;
461 CRYPT_Free(pszContainerA);
462 CRYPT_Free(provname);
463 CRYPT_Free(temp);
464 CRYPT_Free(imagepath);
465 return TRUE;
467 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
468 error:
469 if (pProv)
471 pProv->dwMagic = 0;
472 if (pProv->hModule)
473 FreeLibrary(pProv->hModule);
474 if (pProv->pVTable)
475 CRYPT_Free(pProv->pVTable);
476 if (pProv->pFuncs)
477 CRYPT_Free(pProv->pFuncs);
478 CRYPT_Free(pProv);
480 if (pszContainerA)
481 CRYPT_Free(pszContainerA);
482 if (provnameA)
483 CRYPT_Free(provnameA);
484 if (provname)
485 CRYPT_Free(provname);
486 if (temp)
487 CRYPT_Free(temp);
488 if (imagepath)
489 CRYPT_Free(imagepath);
490 return FALSE;
493 /******************************************************************************
494 * CryptAcquireContextA (ADVAPI32.@)
496 * see CryptAcquireContextW
498 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
499 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
501 PWSTR pProvider = NULL, pContainer = NULL;
502 BOOL ret = FALSE;
504 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv, pszContainer,
505 pszProvider, dwProvType, dwFlags);
507 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
508 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
509 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
511 CRYPT_Free(pContainer);
512 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
515 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
517 if (pContainer)
518 CRYPT_Free(pContainer);
519 if (pProvider)
520 CRYPT_Free(pProvider);
522 return ret;
525 /******************************************************************************
526 * CryptContextAddRef (ADVAPI32.@)
528 * Increases reference count of a cryptographic service provider handle
529 * by one.
531 * PARAMS
532 * hProv [I] Handle to the CSP whose reference is being incremented.
533 * pdwReserved [IN] Reserved for future use and must be NULL.
534 * dwFlags [I] Reserved for future use and must be NULL.
536 * RETURNS
537 * Success: TRUE
538 * Failure: FALSE
540 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
542 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
544 TRACE("(0x%lx, %p, %08lx)\n", hProv, pdwReserved, dwFlags);
546 if (!pProv)
548 SetLastError(NTE_BAD_UID);
549 return FALSE;
552 if (pProv->dwMagic != MAGIC_CRYPTPROV)
554 SetLastError(ERROR_INVALID_PARAMETER);
555 return FALSE;
558 pProv->refcount++;
559 return TRUE;
562 /******************************************************************************
563 * CryptReleaseContext (ADVAPI32.@)
565 * Releases the handle of a CSP. Reference count is decreased.
567 * PARAMS
568 * hProv [I] Handle of a CSP.
569 * dwFlags [I] Reserved for future use and must be NULL.
571 * RETURNS
572 * Success: TRUE
573 * Failure: FALSE
575 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, DWORD dwFlags)
577 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
578 BOOL ret = TRUE;
580 TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
582 if (!pProv)
584 SetLastError(NTE_BAD_UID);
585 return FALSE;
588 if (pProv->dwMagic != MAGIC_CRYPTPROV)
590 SetLastError(ERROR_INVALID_PARAMETER);
591 return FALSE;
594 pProv->refcount--;
595 if (pProv->refcount <= 0)
597 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
598 pProv->dwMagic = 0;
599 FreeLibrary(pProv->hModule);
600 #if 0
601 CRYPT_Free(pProv->pVTable->pContextInfo);
602 #endif
603 CRYPT_Free(pProv->pVTable->pszProvName);
604 CRYPT_Free(pProv->pVTable);
605 CRYPT_Free(pProv->pFuncs);
606 CRYPT_Free(pProv);
608 return ret;
611 /******************************************************************************
612 * CryptGenRandom (ADVAPI32.@)
614 * Fills a buffer with cryptographically random bytes.
616 * PARAMS
617 * hProv [I] Handle of a CSP.
618 * dwLen [I] Number of bytes to generate.
619 * pbBuffer [I/O] Buffer to contain random bytes.
621 * RETURNS
622 * Success: TRUE
623 * Failure: FALSE
625 * NOTES
626 * pdBuffer must be at least dwLen bytes long.
628 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
630 PCRYPTPROV prov = (PCRYPTPROV)hProv;
632 TRACE("(0x%lx, %ld, %p)\n", hProv, dwLen, pbBuffer);
634 if (!hProv)
635 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
637 if (prov->dwMagic != MAGIC_CRYPTPROV)
638 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
640 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
643 /******************************************************************************
644 * CryptCreateHash (ADVAPI32.@)
646 * Initiates the hashing of a stream of data.
648 * PARAMS
649 * hProv [I] Handle of a CSP.
650 * Algid [I] Identifies the hash algorithm to use.
651 * hKey [I] Key for the hash (if required).
652 * dwFlags [I] Reserved for future use and must be NULL.
653 * phHash [O] Address of the future handle to the new hash object.
655 * RETURNS
656 * Success: TRUE
657 * Failure: FALSE
659 * NOTES
660 * If the algorithm is a keyed hash, hKey is the key.
662 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
663 DWORD dwFlags, HCRYPTHASH *phHash)
665 PCRYPTPROV prov = (PCRYPTPROV)hProv;
666 PCRYPTKEY key = (PCRYPTKEY)hKey;
667 PCRYPTHASH hash;
669 TRACE("(0x%lx, 0x%x, 0x%lx, %08lx, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
671 if (!prov)
672 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
673 if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV)
674 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
675 if (dwFlags)
676 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
677 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
678 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
680 hash->pProvider = prov;
682 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
683 key ? key->hPrivate : 0, 0, &hash->hPrivate))
685 *phHash = (HCRYPTHASH)hash;
686 return TRUE;
689 /* CSP error! */
690 CRYPT_Free(hash);
691 *phHash = 0;
692 return FALSE;
695 /******************************************************************************
696 * CryptDecrypt (ADVAPI32.@)
698 * Decrypts data encrypted by CryptEncrypt.
700 * PARAMS
701 * hKey [I] Handle to the decryption key.
702 * hHash [I] Handle to a hash object.
703 * Final [I] TRUE if this is the last section to be decrypted.
704 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
705 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
706 * data on return
707 * pdwDataLen [I/O] Length of pbData before and after the call.
709 * RETURNS
710 * Success: TRUE
711 * Failure: FALSE
713 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
714 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
716 PCRYPTPROV prov;
717 PCRYPTKEY key = (PCRYPTKEY)hKey;
718 PCRYPTHASH hash = (PCRYPTHASH)hHash;
720 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
722 if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
723 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
725 prov = key->pProvider;
726 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
727 Final, dwFlags, pbData, pdwDataLen);
730 /******************************************************************************
731 * CryptDeriveKey (ADVAPI32.@)
733 * Generates session keys derived from a base data value.
735 * PARAMS
736 * hProv [I] Handle to a CSP.
737 * Algid [I] Identifies the symmetric encryption algorithm to use.
738 * hBaseData [I] Handle to a hash object.
739 * dwFlags [I] Type of key to generate.
740 * phKey [I/O] Address of the newly generated key.
742 * RETURNS
743 * Success: TRUE
744 * Failure: FALSE
746 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
747 DWORD dwFlags, HCRYPTKEY *phKey)
749 PCRYPTPROV prov = (PCRYPTPROV)hProv;
750 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
751 PCRYPTKEY key;
753 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08lx, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
755 if (!prov || !hash)
756 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
757 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV)
758 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
759 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
760 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
762 key->pProvider = prov;
763 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
765 *phKey = (HCRYPTKEY)key;
766 return TRUE;
769 /* CSP error! */
770 CRYPT_Free(key);
771 *phKey = 0;
772 return FALSE;
775 /******************************************************************************
776 * CryptDestroyHash (ADVAPI32.@)
778 * Destroys the hash object referenced by hHash.
780 * PARAMS
781 * hHash [I] Handle of the hash object to be destroyed.
783 * RETURNS
784 * Success: TRUE
785 * Failure: FALSE
787 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
789 PCRYPTHASH hash = (PCRYPTHASH)hHash;
790 PCRYPTPROV prov;
791 BOOL ret;
793 TRACE("(0x%lx)\n", hHash);
795 if (!hash)
796 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
798 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
799 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
801 prov = hash->pProvider;
802 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
803 CRYPT_Free(hash);
804 return ret;
807 /******************************************************************************
808 * CryptDestroyKey (ADVAPI32.@)
810 * Releases the handle referenced by hKey.
812 * PARAMS
813 * hKey [I] Handle of the key to be destroyed.
815 * RETURNS
816 * Success: TRUE
817 * Failure: FALSE
819 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
821 PCRYPTKEY key = (PCRYPTKEY)hKey;
822 PCRYPTPROV prov;
823 BOOL ret;
825 TRACE("(0x%lx)\n", hKey);
827 if (!key)
828 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
830 if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
831 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
833 prov = key->pProvider;
834 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
835 CRYPT_Free(key);
836 return ret;
839 /******************************************************************************
840 * CryptDuplicateHash (ADVAPI32.@)
842 * Duplicates a hash.
844 * PARAMS
845 * hHash [I] Handle to the hash to be copied.
846 * pdwReserved [I] Reserved for future use and must be zero.
847 * dwFlags [I] Reserved for future use and must be zero.
848 * phHash [O] Address of the handle to receive the copy.
850 * RETURNS
851 * Success: TRUE
852 * Failure: FALSE
854 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
855 DWORD dwFlags, HCRYPTHASH *phHash)
857 PCRYPTPROV prov;
858 PCRYPTHASH orghash, newhash;
860 TRACE("(0x%lx, %p, %08lx, %p)\n", hHash, pdwReserved, dwFlags, phHash);
862 orghash = (PCRYPTHASH)hHash;
863 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
864 orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
866 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
869 prov = orghash->pProvider;
870 if (!prov->pFuncs->pCPDuplicateHash)
871 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
873 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
874 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
876 newhash->pProvider = prov;
877 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
879 *phHash = (HCRYPTHASH)newhash;
880 return TRUE;
882 CRYPT_Free(newhash);
883 return FALSE;
886 /******************************************************************************
887 * CryptDuplicateKey (ADVAPI32.@)
889 * Duplicate a key and the key's state.
891 * PARAMS
892 * hKey [I] Handle of the key to copy.
893 * pdwReserved [I] Reserved for future use and must be NULL.
894 * dwFlags [I] Reserved for future use and must be zero.
895 * phKey [I] Address of the handle to the duplicated key.
897 * RETURNS
898 * Success: TRUE
899 * Failure: FALSE
901 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
903 PCRYPTPROV prov;
904 PCRYPTKEY orgkey, newkey;
906 TRACE("(0x%lx, %p, %08lx, %p)\n", hKey, pdwReserved, dwFlags, phKey);
908 orgkey = (PCRYPTKEY)hKey;
909 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
910 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
912 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
915 prov = orgkey->pProvider;
916 if (!prov->pFuncs->pCPDuplicateKey)
917 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
919 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
920 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
922 newkey->pProvider = prov;
923 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
925 *phKey = (HCRYPTKEY)newkey;
926 return TRUE;
928 CRYPT_Free(newkey);
929 return FALSE;
932 /******************************************************************************
933 * CryptEncrypt (ADVAPI32.@)
935 * Encrypts data.
937 * PARAMS
938 * hKey [I] Handle to the enryption key.
939 * hHash [I] Handle to a hash object.
940 * Final [I] TRUE if this is the last section to encrypt.
941 * dwFlags [I] Can be CRYPT_OAEP.
942 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
943 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
944 * encrypted data after call.
945 * dwBufLen [I] Length of the input pbData buffer.
947 * RETURNS
948 * Success: TRUE
949 * Failure: FALSE
951 * NOTES
952 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
953 * required for the returned data in pdwDataLen.
955 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
956 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
958 PCRYPTPROV prov;
959 PCRYPTKEY key = (PCRYPTKEY)hKey;
960 PCRYPTHASH hash = (PCRYPTHASH)hHash;
962 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p, %ld)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
964 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
965 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
967 prov = key->pProvider;
968 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
969 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
972 /******************************************************************************
973 * CryptEnumProvidersW (ADVAPI32.@)
975 * Returns the next availabe CSP.
977 * PARAMS
978 * dwIndex [I] Index of the next provider to be enumerated.
979 * pdwReserved [I] Reserved for future use and must be NULL.
980 * dwFlags [I] Reserved for future use and must be zero.
981 * pdwProvType [O] DWORD designating the type of the provider.
982 * pszProvName [O] Buffer that receives data from the provider.
983 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
984 * of bytes stored in the buffer on return.
986 * RETURNS
987 * Success: TRUE
988 * Failure: FALSE
990 * NOTES
991 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
992 * for memory allocation purposes.
994 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
995 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
997 HKEY hKey;
998 static const WCHAR providerW[] = {
999 'S','o','f','t','w','a','r','e','\\',
1000 'M','i','c','r','o','s','o','f','t','\\',
1001 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1002 'D','e','f','a','u','l','t','s','\\',
1003 'P','r','o','v','i','d','e','r',0
1005 static const WCHAR typeW[] = {'T','y','p','e',0};
1007 TRACE("(%ld, %p, %ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1008 pdwProvType, pszProvName, pcbProvName);
1010 if (pdwReserved || !pcbProvName) CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1011 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1013 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1014 CRYPT_ReturnLastError(NTE_FAIL);
1016 if (!pszProvName)
1018 DWORD numkeys;
1019 WCHAR *provNameW;
1021 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1022 NULL, NULL, NULL, NULL, NULL, NULL);
1024 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1025 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1027 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1028 CRYPT_Free(provNameW);
1029 (*pcbProvName)++;
1030 *pcbProvName *= sizeof(WCHAR);
1032 if (dwIndex >= numkeys)
1033 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
1034 } else {
1035 DWORD size = sizeof(DWORD);
1036 DWORD result;
1037 HKEY subkey;
1039 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1040 if (result)
1041 CRYPT_ReturnLastError(result);
1042 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1043 return FALSE;
1044 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1045 return FALSE;
1046 RegCloseKey(subkey);
1048 RegCloseKey(hKey);
1049 return TRUE;
1052 /******************************************************************************
1053 * CryptEnumProvidersA (ADVAPI32.@)
1055 * see CryptEnumProvidersW
1057 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1058 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1060 PWSTR str = NULL;
1061 DWORD strlen;
1062 BOOL ret; /* = FALSE; */
1064 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1065 pdwProvType, pszProvName, pcbProvName);
1067 CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &strlen);
1068 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1069 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1070 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1071 if (str)
1072 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1073 *pcbProvName = strlen / sizeof(WCHAR); /* FIXME: not correct */
1074 if (str)
1076 CRYPT_Free(str);
1077 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1078 CRYPT_ReturnLastError(ERROR_MORE_DATA);
1080 return ret;
1083 /******************************************************************************
1084 * CryptEnumProviderTypesW (ADVAPI32i.@)
1086 * Retrieves the next type of CSP supported.
1088 * PARAMS
1089 * dwIndex [I] Index of the next provider to be enumerated.
1090 * pdwReserved [I] Reserved for future use and must be NULL.
1091 * dwFlags [I] Reserved for future use and must be zero.
1092 * pdwProvType [O] DWORD designating the type of the provider.
1093 * pszTypeName [O] Buffer that receives data from the provider type.
1094 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1095 * of bytes stored in the buffer on return.
1097 * RETURNS
1098 * Success: TRUE
1099 * Failure: FALSE
1101 * NOTES
1102 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1103 * for memory allocation purposes.
1105 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1106 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1108 HKEY hKey, hSubkey;
1109 DWORD keylen, numkeys, dwType;
1110 PWSTR keyname, ch;
1111 DWORD result;
1112 static const WCHAR KEYSTR[] = {
1113 'S','o','f','t','w','a','r','e','\\',
1114 'M','i','c','r','o','s','o','f','t','\\',
1115 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1116 'D','e','f','a','u','l','t','s','\\',
1117 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1119 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1121 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex, pdwReserved,
1122 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1124 if (pdwReserved || !pdwProvType || !pcbTypeName)
1125 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1126 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1128 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1129 return FALSE;
1131 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1132 if (dwIndex >= numkeys)
1133 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
1134 keylen++;
1135 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1136 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1137 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1138 CRYPT_Free(keyname);
1139 return FALSE;
1141 RegOpenKeyW(hKey, keyname, &hSubkey);
1142 ch = keyname + strlenW(keyname);
1143 /* Convert "Type 000" to 0, etc/ */
1144 *pdwProvType = *(--ch) - '0';
1145 *pdwProvType += (*(--ch) - '0') * 10;
1146 *pdwProvType += (*(--ch) - '0') * 100;
1147 CRYPT_Free(keyname);
1149 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1150 if (result)
1151 CRYPT_ReturnLastError(result);
1153 RegCloseKey(hSubkey);
1154 RegCloseKey(hKey);
1155 return TRUE;
1158 /******************************************************************************
1159 * CryptEnumProviderTypesA (ADVAPI32.@)
1161 * see CryptEnumProviderTypesW
1163 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1164 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1166 PWSTR str = NULL;
1167 DWORD strlen;
1168 BOOL ret;
1170 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1171 pdwProvType, pszTypeName, pcbTypeName);
1173 CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &strlen);
1174 if ( pszTypeName && !(str = CRYPT_Alloc(strlen)) )
1175 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1176 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1177 if (str)
1178 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1179 *pcbTypeName = strlen / sizeof(WCHAR);
1180 if (str)
1182 CRYPT_Free(str);
1183 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1184 CRYPT_ReturnLastError(ERROR_MORE_DATA);
1186 return ret;
1189 /******************************************************************************
1190 * CryptExportKey (ADVAPI32.@)
1192 * Exports a cryptographic key from a CSP.
1194 * PARAMS
1195 * hKey [I] Handle to the key to export.
1196 * hExpKey [I] Handle to a cryptographic key of the end user.
1197 * dwBlobType [I] Type of BLOB to be exported.
1198 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1199 * pbData [O] Buffer to receive BLOB data.
1200 * pdwDataLen [I/O] Specifies the size of pbData.
1202 * RETURNS
1203 * Success: TRUE
1204 * Failure: FALSE
1206 * NOTES
1207 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1208 * buffer needed to hold the BLOB.
1210 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1211 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1213 PCRYPTPROV prov;
1214 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1216 TRACE("(0x%lx, 0x%lx, %ld, %08lx, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1218 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1219 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1221 prov = key->pProvider;
1222 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1223 dwBlobType, dwFlags, pbData, pdwDataLen);
1226 /******************************************************************************
1227 * CryptGenKey (ADVAPI32.@)
1229 * Generates a random cryptographic session key or a pub/priv key pair.
1231 * PARAMS
1232 * hProv [I] Handle to a CSP.
1233 * Algid [I] Algorithm to use to make key.
1234 * dwFlags [I] Specifies type of key to make.
1235 * phKey [I] Address of the handle to which the new key is copied.
1237 * RETURNS
1238 * Success: TRUE
1239 * Failure: FALSE
1241 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1243 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1244 PCRYPTKEY key;
1246 TRACE("(0x%lx, %d, %08lx, %p)\n", hProv, Algid, dwFlags, phKey);
1248 if (!prov)
1249 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1250 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1251 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1252 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1253 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1255 key->pProvider = prov;
1257 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1259 *phKey = (HCRYPTKEY)key;
1260 return TRUE;
1263 /* CSP error! */
1264 CRYPT_Free(key);
1265 return FALSE;
1268 /******************************************************************************
1269 * CryptGetDefaultProviderW (ADVAPI32.@)
1271 * Finds the default CSP of a certain provider type.
1273 * PARAMS
1274 * dwProvType [I] Provider type to look for.
1275 * pdwReserved [I] Reserved for future use and must be NULL.
1276 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1277 * pszProvName [O] Name of the default CSP.
1278 * pcbProvName [I/O] Size of pszProvName
1280 * RETURNS
1281 * Success: TRUE
1282 * Failure: FALSE
1284 * NOTES
1285 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1286 * memory allocation purposes on return.
1288 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1289 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1291 HKEY hKey;
1292 PWSTR keyname;
1293 DWORD result;
1294 static const WCHAR nameW[] = {'N','a','m','e',0};
1296 if (pdwReserved || !pcbProvName)
1297 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1298 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1299 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1300 if (dwProvType > 999)
1301 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1302 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1303 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1304 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1306 CRYPT_Free(keyname);
1307 CRYPT_ReturnLastError(NTE_PROV_TYPE_NOT_DEF);
1309 CRYPT_Free(keyname);
1311 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1312 if (result)
1314 if (result != ERROR_MORE_DATA)
1315 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1316 else
1317 SetLastError(result);
1319 return FALSE;
1322 RegCloseKey(hKey);
1323 return TRUE;
1326 /******************************************************************************
1327 * CryptGetDefaultProviderA (ADVAPI32.@)
1329 * see CryptGetDefaultProviderW
1331 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1332 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1334 PWSTR str = NULL;
1335 DWORD strlen;
1336 BOOL ret = FALSE;
1338 TRACE("(%ld, %p, %08lx, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1340 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &strlen);
1341 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1342 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1343 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &strlen);
1344 if (str)
1345 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1346 *pcbProvName = strlen / sizeof(WCHAR);
1347 if (str)
1349 CRYPT_Free(str);
1350 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1351 CRYPT_ReturnLastError(ERROR_MORE_DATA);
1353 return ret;
1356 /******************************************************************************
1357 * CryptGetHashParam (ADVAPI32.@)
1359 * Retrieves data that controls the operations of a hash object.
1361 * PARAMS
1362 * hHash [I] Handle of the hash object to question.
1363 * dwParam [I] Query type.
1364 * pbData [O] Buffer that receives the value data.
1365 * pdwDataLen [I/O] Size of the pbData buffer.
1366 * dwFlags [I] Reserved for future use and must be zero.
1368 * RETURNS
1369 * Success: TRUE
1370 * Failure: FALSE
1372 * NOTES
1373 * If pbData is NULL, pdwDataLen will contain the length required.
1375 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1376 DWORD *pdwDataLen, DWORD dwFlags)
1378 PCRYPTPROV prov;
1379 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1381 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1383 if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1384 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1386 prov = hash->pProvider;
1387 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1388 pbData, pdwDataLen, dwFlags);
1391 /******************************************************************************
1392 * CryptGetKeyParam (ADVAPI32.@)
1394 * Retrieves data that controls the operations of a key.
1396 * PARAMS
1397 * hKey [I] Handle to they key in question.
1398 * dwParam [I] Specifies query type.
1399 * pbData [O] Sequence of bytes to receive data.
1400 * pdwDataLen [I/O] Size of pbData.
1401 * dwFlags [I] Reserved for future use and must be zero.
1403 * RETURNS
1404 * Success: TRUE
1405 * Failure: FALSE
1407 * NOTES
1408 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1410 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1411 DWORD *pdwDataLen, DWORD dwFlags)
1413 PCRYPTPROV prov;
1414 PCRYPTKEY key = (PCRYPTKEY)hKey;
1416 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1418 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1419 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1421 prov = key->pProvider;
1422 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1423 pbData, pdwDataLen, dwFlags);
1426 /******************************************************************************
1427 * CryptGetProvParam (ADVAPI32.@)
1429 * Retrieves parameters that control the operations of a CSP.
1431 * PARAMS
1432 * hProv [I] Handle of the CSP in question.
1433 * dwParam [I] Specifies query type.
1434 * pbData [O] Buffer to receive the data.
1435 * pdwDataLen [I/O] Size of pbData.
1436 * dwFlags [I] see MSDN Docs.
1438 * RETURNS
1439 * Success: TRUE
1440 * Failure: FALSE
1442 * NOTES
1443 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1445 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1446 DWORD *pdwDataLen, DWORD dwFlags)
1448 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1450 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1452 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1453 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1455 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1458 /******************************************************************************
1459 * CryptGetUserKey (ADVAPI32.@)
1461 * Gets a handle of one of a user's two public/private key pairs.
1463 * PARAMS
1464 * hProv [I] Handle of a CSP.
1465 * dwKeySpec [I] Private key to use.
1466 * phUserKey [O] Pointer to the handle of the retrieved keys.
1468 * RETURNS
1469 * Success: TRUE
1470 * Failure: FALSE
1472 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1474 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1475 PCRYPTKEY key;
1477 TRACE("(0x%lx, %ld, %p)\n", hProv, dwKeySpec, phUserKey);
1479 if (!prov)
1480 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1481 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1482 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1483 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1484 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1486 key->pProvider = prov;
1488 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1490 *phUserKey = (HCRYPTKEY)key;
1491 return TRUE;
1494 /* CSP Error */
1495 CRYPT_Free(key);
1496 *phUserKey = 0;
1497 return FALSE;
1500 /******************************************************************************
1501 * CryptHashData (ADVAPI32.@)
1503 * Adds data to a hash object.
1505 * PARAMS
1506 * hHash [I] Handle of the hash object.
1507 * pbData [I] Buffer of data to be hashed.
1508 * dwDataLen [I] Number of bytes to add.
1509 * dwFlags [I] Can be CRYPT_USERDATA
1511 * RETURNS
1512 * Success: TRUE
1513 * Failure: FALSE
1515 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1517 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1518 PCRYPTPROV prov;
1520 TRACE("(0x%lx, %p, %ld, %08lx)\n", hHash, pbData, dwDataLen, dwFlags);
1522 if (!hash)
1523 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1524 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1525 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1527 prov = hash->pProvider;
1528 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1531 /******************************************************************************
1532 * CryptHashSessionKey (ADVAPI32.@)
1534 * PARAMS
1535 * hHash [I] Handle to the hash object.
1536 * hKey [I] Handle to the key to be hashed.
1537 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1539 * RETURNS
1540 * Success: TRUE
1541 * Failure: FALSE
1543 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1545 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1546 PCRYPTKEY key = (PCRYPTKEY)hKey;
1547 PCRYPTPROV prov;
1549 TRACE("(0x%lx, 0x%lx, %08lx)\n", hHash, hKey, dwFlags);
1551 if (!hash || !key)
1552 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1554 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1555 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1557 prov = hash->pProvider;
1558 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1561 /******************************************************************************
1562 * CryptImportKey (ADVAPI32.@)
1564 * PARAMS
1565 * hProv [I] Handle of a CSP.
1566 * pbData [I] Contains the key to be imported.
1567 * dwDataLen [I] Length of the key.
1568 * hPubKey [I] Cryptographic key that decrypts pdData
1569 * dwFlags [I] Used only with a public/private key pair.
1570 * phKey [O] Imported key.
1572 * RETURNS
1573 * Success: TRUE
1574 * Failure: FALSE
1576 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, BYTE *pbData, DWORD dwDataLen,
1577 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1579 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1580 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1582 TRACE("(0x%lx, %p, %ld, 0x%lx, %08lx, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1584 if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV)
1585 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1587 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1588 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1590 importkey->pProvider = prov;
1591 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1592 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1594 *phKey = (HCRYPTKEY)importkey;
1595 return TRUE;
1598 CRYPT_Free(importkey);
1599 return FALSE;
1602 /******************************************************************************
1603 * CryptSignHashW (ADVAPI32.@)
1605 * Signs data.
1607 * PARAMS
1608 * hHash [I] Handle of the hash object to be signed.
1609 * dwKeySpec [I] Private key to use.
1610 * sDescription [I] Should be NULL.
1611 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1612 * pbSignature [O] Buffer of the signature data.
1613 * pdwSigLen [I/O] Size of the pbSignature buffer.
1615 * RETURNS
1616 * Success: TRUE
1617 * Failure: FALSE
1619 * NOTES
1620 * Because of security flaws sDescription should not be used and should thus be
1621 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1622 * Providers.
1624 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1625 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1627 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1628 PCRYPTPROV prov;
1630 TRACE("(0x%lx, %ld, %s, %08lx, %p, %p)\n",
1631 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1633 if (!hash)
1634 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1635 if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1636 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1638 prov = hash->pProvider;
1639 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1640 dwFlags, pbSignature, pdwSigLen);
1643 /******************************************************************************
1644 * CryptSignHashA (ADVAPI32.@)
1646 * ASCII version of CryptSignHashW
1648 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1649 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1651 LPWSTR wsDescription;
1652 BOOL result;
1654 TRACE("(0x%lx, %ld, %s, %08lx, %p, %p)\n",
1655 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1657 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1658 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1659 if (wsDescription) CRYPT_Free(wsDescription);
1661 return result;
1664 /******************************************************************************
1665 * CryptSetHashParam (ADVAPI32.@)
1667 * Customizes the operations of a hash object.
1669 * PARAMS
1670 * hHash [I] Handle of the hash object to set parameters.
1671 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1672 * pbData [I] Value data buffer.
1673 * dwFlags [I] Reserved for future use and must be zero.
1675 * RETURNS
1676 * Success: TRUE
1677 * Failure: FALSE
1679 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1681 PCRYPTPROV prov;
1682 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1684 TRACE("(0x%lx, %ld, %p, %08lx)\n", hHash, dwParam, pbData, dwFlags);
1686 if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1687 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1689 prov = hash->pProvider;
1690 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1691 dwParam, pbData, dwFlags);
1694 /******************************************************************************
1695 * CryptSetKeyParam (ADVAPI32.@)
1697 * Customizes a session key's operations.
1699 * PARAMS
1700 * hKey [I] Handle to the key to set values.
1701 * dwParam [I] See MSDN Doc.
1702 * pbData [I] Buffer of values to set.
1703 * dwFlags [I] Only used when dwParam == KP_ALGID.
1705 * RETURNS
1706 * Success: TRUE
1707 * Failure: FALSE
1709 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1711 PCRYPTPROV prov;
1712 PCRYPTKEY key = (PCRYPTKEY)hKey;
1714 TRACE("(0x%lx, %ld, %p, %08lx)\n", hKey, dwParam, pbData, dwFlags);
1716 if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1717 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1719 prov = key->pProvider;
1720 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1721 dwParam, pbData, dwFlags);
1724 /******************************************************************************
1725 * CryptSetProviderA (ADVAPI32.@)
1727 * Specifies the current user's default CSP.
1729 * PARAMS
1730 * pszProvName [I] Name of the new default CSP.
1731 * dwProvType [I] Provider type of the CSP.
1733 * RETURNS
1734 * Success: TRUE
1735 * Failure: FALSE
1737 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
1739 TRACE("(%s, %ld)\n", pszProvName, dwProvType);
1740 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1743 /******************************************************************************
1744 * CryptSetProviderW (ADVAPI32.@)
1746 * See CryptSetProviderA
1748 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
1750 TRACE("(%s, %ld)\n", debugstr_w(pszProvName), dwProvType);
1751 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1754 /******************************************************************************
1755 * CryptSetProviderExW (ADVAPI32.@)
1757 * Specifies the default CSP.
1759 * PARAMS
1760 * pszProvName [I] Name of the new default CSP.
1761 * dwProvType [I] Provider type of the CSP.
1762 * pdwReserved [I] Reserved for future use and must be NULL.
1763 * dwFlags [I] See MSDN Doc.
1765 * RETURNS
1766 * Success: TRUE
1767 * Failure: FALSE
1769 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1771 HKEY hProvKey, hTypeKey;
1772 PWSTR keyname;
1773 static const WCHAR nameW[] = {'N','a','m','e',0};
1775 TRACE("(%s, %ld, %p, %08lx)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
1777 if (!pszProvName || pdwReserved)
1778 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1779 if (dwProvType > MAXPROVTYPES)
1780 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1781 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
1782 || dwFlags == CRYPT_DELETE_DEFAULT)
1783 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1785 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
1786 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1787 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1788 keyname, &hTypeKey))
1790 CRYPT_Free(keyname);
1791 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1793 CRYPT_Free(keyname);
1795 if (dwFlags & CRYPT_DELETE_DEFAULT)
1797 RegDeleteValueW(hTypeKey, nameW);
1799 else
1801 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
1803 RegCloseKey(hTypeKey);
1804 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1806 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1807 keyname, &hProvKey))
1809 CRYPT_Free(keyname);
1810 RegCloseKey(hTypeKey);
1811 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1813 CRYPT_Free(keyname);
1815 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (LPBYTE)pszProvName, (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
1817 RegCloseKey(hTypeKey);
1818 RegCloseKey(hProvKey);
1819 return FALSE;
1822 RegCloseKey(hProvKey);
1824 RegCloseKey(hTypeKey);
1826 return TRUE;
1829 /******************************************************************************
1830 * CryptSetProviderExA (ADVAPI32.@)
1832 * See CryptSetProviderExW
1834 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1836 BOOL ret = FALSE;
1837 PWSTR str = NULL;
1839 TRACE("(%s, %ld, %p, %08lx)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
1841 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
1843 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
1844 CRYPT_Free(str);
1846 return ret;
1849 /******************************************************************************
1850 * CryptSetProvParam (ADVAPI32.@)
1852 * Customizes the operations of a CSP.
1854 * PARAMS
1855 * hProv [I] Handle of a CSP.
1856 * dwParam [I] See MSDN Doc.
1857 * pbData [I] Buffer that contains a value to set as a parameter.
1858 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
1860 * RETURNS
1861 * Success: TRUE
1862 * Failure: FALSE
1864 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1866 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1868 TRACE("(0x%lx, %ld, %p, %08lx)\n", hProv, dwParam, pbData, dwFlags);
1870 if (!prov)
1871 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1872 if (prov->dwMagic != MAGIC_CRYPTPROV)
1873 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1874 if (dwFlags & PP_USE_HARDWARE_RNG)
1876 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
1877 FIXME("\tLetting the CSP decide.\n");
1879 if (dwFlags & PP_CLIENT_HWND)
1881 /* FIXME: Should verify the parameter */
1882 if (pbData /* && IsWindow((HWND)pbData) */)
1884 crypt_hWindow = (HWND)(pbData);
1885 return TRUE;
1886 } else {
1887 SetLastError(ERROR_INVALID_PARAMETER);
1888 return FALSE;
1891 /* All other flags go to the CSP */
1892 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
1895 /******************************************************************************
1896 * CryptVerifySignatureW (ADVAPI32.@)
1898 * Verifies the signature of a hash object.
1900 * PARAMS
1901 * hHash [I] Handle of the hash object to verify.
1902 * pbSignature [I] Signature data to verify.
1903 * dwSigLen [I] Size of pbSignature.
1904 * hPubKey [I] Handle to the public key to authenticate signature.
1905 * sDescription [I] Should be NULL.
1906 * dwFlags [I] See MSDN doc.
1908 * RETURNS
1909 * Success: TRUE
1910 * Failure: FALSE
1912 * NOTES
1913 * Because of security flaws sDescription should not be used and should thus be
1914 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1915 * Providers.
1917 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
1918 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
1920 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1921 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
1922 PCRYPTPROV prov;
1924 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08lx)\n", hHash, pbSignature,
1925 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
1927 if (!hash || !key)
1928 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1929 if (!pbSignature || !dwSigLen ||
1930 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
1931 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1933 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1936 prov = hash->pProvider;
1937 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
1938 key->hPrivate, sDescription, dwFlags);
1941 /******************************************************************************
1942 * CryptVerifySignatureA (ADVAPI32.@)
1944 * ASCII version of CryptVerifySignatureW
1946 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
1947 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
1949 LPWSTR wsDescription;
1950 BOOL result;
1952 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08lx)\n", hHash, pbSignature,
1953 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
1955 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1956 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
1957 if (wsDescription) CRYPT_Free(wsDescription);
1959 return result;
1962 /******************************************************************************
1963 * SystemFunction036 (ADVAPI32.@)
1965 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
1967 * PARAMS
1968 * pbBufer [O] Pointer to memory to receive random bytes.
1969 * dwLen [I] Number of random bytes to fetch.
1972 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
1974 int dev_random;
1976 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
1977 * quality for cryptographic applications. /dev/random is much better,
1978 * but it blocks if the kernel has not yet collected enough entropy for
1979 * the request, which will suspend the calling thread for an indefinite
1980 * amount of time. */
1981 dev_random = open("/dev/urandom", O_RDONLY);
1982 if (dev_random != -1)
1984 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
1986 close(dev_random);
1987 return TRUE;
1989 close(dev_random);
1991 SetLastError(NTE_FAIL);
1992 return FALSE;
1996 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
1997 in crypt32.dll.
2000 /******************************************************************************
2001 * SystemFunction040 (ADVAPI32.@)
2003 * MSDN documents this function as RtlEncryptMemory.
2005 * PARAMS
2006 * memory [I/O] Pointer to memory to encrypt.
2007 * length [I] Length of region to encrypt in bytes.
2008 * flags [I] Control whether other processes are able to decrypt the memory.
2009 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2010 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2011 * RTL_ENCRYPT_OPTION_SAME_LOGON
2013 * NOTES
2014 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2015 * If flags are specified when encrypting, the same flag value must be given
2016 * when decrypting the memory.
2018 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2020 FIXME("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory, length, flags);
2021 return STATUS_SUCCESS;
2024 /******************************************************************************
2025 * SystemFunction041 (ADVAPI32.@)
2027 * MSDN documents this function as RtlDecryptMemory.
2029 * PARAMS
2030 * memory [I/O] Pointer to memory to decrypt.
2031 * length [I] Length of region to decrypt in bytes.
2032 * flags [I] Control whether other processes are able to decrypt the memory.
2033 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2034 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2035 * RTL_ENCRYPT_OPTION_SAME_LOGON
2037 * NOTES
2038 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2039 * If flags are specified when encrypting, the same flag value must be given
2040 * when decrypting the memory.
2042 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2044 FIXME("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory, length, flags);
2045 return STATUS_SUCCESS;