inetcpl: German translation for the content property sheet.
[wine.git] / dlls / advapi32 / crypt.c
bloba6f8bc163329310b19dfb3b65969a9ff4217f80f
1 /*
2 * Copyright 1999 Ian Schmidt
3 * Copyright 2001 Travis Michielsen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /***********************************************************************
22 * TODO:
23 * - Reference counting
24 * - Thread-safing
27 #include "config.h"
28 #include "wine/port.h"
30 #include <time.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "crypt.h"
45 #include "winnls.h"
46 #include "winreg.h"
47 #include "rpc.h"
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50 #include "winternl.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
54 static HWND crypt_hWindow;
56 #define CRYPT_Alloc(size) (LocalAlloc(LMEM_ZEROINIT, size))
57 #define CRYPT_Free(buffer) (LocalFree(buffer))
59 static inline PWSTR CRYPT_GetProvKeyName(PCWSTR pProvName)
61 static const WCHAR KEYSTR[] = {
62 'S','o','f','t','w','a','r','e','\\',
63 'M','i','c','r','o','s','o','f','t','\\',
64 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
65 'D','e','f','a','u','l','t','s','\\',
66 'P','r','o','v','i','d','e','r','\\',0
68 PWSTR keyname;
70 keyname = CRYPT_Alloc((strlenW(KEYSTR) + strlenW(pProvName) +1)*sizeof(WCHAR));
71 if (keyname)
73 strcpyW(keyname, KEYSTR);
74 strcpyW(keyname + strlenW(KEYSTR), pProvName);
75 } else
76 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
77 return keyname;
80 static inline PWSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
82 static const WCHAR MACHINESTR[] = {
83 'S','o','f','t','w','a','r','e','\\',
84 'M','i','c','r','o','s','o','f','t','\\',
85 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
86 'D','e','f','a','u','l','t','s','\\',
87 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
88 'T','y','p','e',' ','X','X','X',0
90 static const WCHAR USERSTR[] = {
91 'S','o','f','t','w','a','r','e','\\',
92 'M','i','c','r','o','s','o','f','t','\\',
93 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
94 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
96 PWSTR keyname;
97 PWSTR ptr;
99 keyname = CRYPT_Alloc( ((user ? strlenW(USERSTR) : strlenW(MACHINESTR)) +1)*sizeof(WCHAR));
100 if (keyname)
102 user ? strcpyW(keyname, USERSTR) : strcpyW(keyname, MACHINESTR);
103 ptr = keyname + strlenW(keyname);
104 *(--ptr) = (dwType % 10) + '0';
105 *(--ptr) = ((dwType / 10) % 10) + '0';
106 *(--ptr) = (dwType / 100) + '0';
107 } else
108 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
109 return keyname;
112 /* CRYPT_UnicodeTOANSI
113 * wstr - unicode string
114 * str - pointer to ANSI string
115 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
117 * returns TRUE if unsuccessful, FALSE otherwise.
118 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
120 static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
122 int count;
124 if (!wstr)
126 *str = NULL;
127 return TRUE;
129 count = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
130 if (strsize == -1)
131 *str = CRYPT_Alloc(count * sizeof(CHAR));
132 else
133 count = min( count, strsize );
134 if (*str)
136 WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, count, NULL, NULL);
137 return TRUE;
139 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
140 return FALSE;
143 /* CRYPT_ANSITOUnicode
144 * str - ANSI string
145 * wstr - pointer to unicode string
146 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
148 static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
150 unsigned int wcount;
152 if (!str)
154 *wstr = NULL;
155 return TRUE;
157 wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
158 if (wstrsize == -1)
159 *wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
160 else
161 wcount = min( wcount, wstrsize/sizeof(WCHAR) );
162 if (*wstr)
164 MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
165 return TRUE;
167 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
168 return FALSE;
171 /* These next 2 functions are used by the VTableProvStruc structure */
172 static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
174 if (!lpszImage || !pData)
176 SetLastError(ERROR_INVALID_PARAMETER);
177 return FALSE;
180 FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
182 return TRUE;
185 static void CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
187 if (phWnd) *phWnd = crypt_hWindow;
190 #define CRYPT_GetProvFunc(name) \
191 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
192 #define CRYPT_GetProvFuncOpt(name) \
193 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
194 static PCRYPTPROV CRYPT_LoadProvider(PCWSTR pImage)
196 PCRYPTPROV provider;
197 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
199 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
200 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
201 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
202 if ( !(provider->hModule = LoadLibraryW(pImage)) )
204 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
205 FIXME("Failed to load dll %s\n", debugstr_w(pImage));
206 goto error;
208 provider->dwMagic = MAGIC_CRYPTPROV;
209 provider->refcount = 1;
211 errorcode = NTE_PROVIDER_DLL_FAIL;
212 CRYPT_GetProvFunc(CPAcquireContext);
213 CRYPT_GetProvFunc(CPCreateHash);
214 CRYPT_GetProvFunc(CPDecrypt);
215 CRYPT_GetProvFunc(CPDeriveKey);
216 CRYPT_GetProvFunc(CPDestroyHash);
217 CRYPT_GetProvFunc(CPDestroyKey);
218 CRYPT_GetProvFuncOpt(CPDuplicateHash);
219 CRYPT_GetProvFuncOpt(CPDuplicateKey);
220 CRYPT_GetProvFunc(CPEncrypt);
221 CRYPT_GetProvFunc(CPExportKey);
222 CRYPT_GetProvFunc(CPGenKey);
223 CRYPT_GetProvFunc(CPGenRandom);
224 CRYPT_GetProvFunc(CPGetHashParam);
225 CRYPT_GetProvFunc(CPGetKeyParam);
226 CRYPT_GetProvFunc(CPGetProvParam);
227 CRYPT_GetProvFunc(CPGetUserKey);
228 CRYPT_GetProvFunc(CPHashData);
229 CRYPT_GetProvFunc(CPHashSessionKey);
230 CRYPT_GetProvFunc(CPImportKey);
231 CRYPT_GetProvFunc(CPReleaseContext);
232 CRYPT_GetProvFunc(CPSetHashParam);
233 CRYPT_GetProvFunc(CPSetKeyParam);
234 CRYPT_GetProvFunc(CPSetProvParam);
235 CRYPT_GetProvFunc(CPSignHash);
236 CRYPT_GetProvFunc(CPVerifySignature);
238 /* FIXME: Not sure what the pbContextInfo field is for.
239 * Does it need memory allocation?
241 provider->pVTable->Version = 3;
242 provider->pVTable->FuncVerifyImage = CRYPT_VerifyImage;
243 provider->pVTable->FuncReturnhWnd = CRYPT_ReturnhWnd;
244 provider->pVTable->dwProvType = 0;
245 provider->pVTable->pbContextInfo = NULL;
246 provider->pVTable->cbContextInfo = 0;
247 provider->pVTable->pszProvName = NULL;
248 return provider;
250 error:
251 SetLastError(errorcode);
252 if (provider)
254 provider->dwMagic = 0;
255 if (provider->hModule)
256 FreeLibrary(provider->hModule);
257 CRYPT_Free(provider->pVTable);
258 CRYPT_Free(provider->pFuncs);
259 CRYPT_Free(provider);
261 return NULL;
263 #undef CRYPT_GetProvFunc
264 #undef CRYPT_GetProvFuncOpt
267 static void CRYPT_CreateMachineGuid(void)
269 static const WCHAR cryptographyW[] = {
270 'S','o','f','t','w','a','r','e','\\',
271 'M','i','c','r','o','s','o','f','t','\\',
272 'C','r','y','p','t','o','g','r','a','p','h','y',0 };
273 static const WCHAR machineGuidW[] = {
274 'M','a','c','h','i','n','e','G','u','i','d',0 };
275 LONG r;
276 HKEY key;
278 r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, cryptographyW, 0, KEY_ALL_ACCESS,
279 &key);
280 if (!r)
282 DWORD size;
284 r = RegQueryValueExW(key, machineGuidW, NULL, NULL, NULL, &size);
285 if (r == ERROR_FILE_NOT_FOUND)
287 UUID uuid;
288 WCHAR buf[37];
289 RPC_STATUS rs;
290 static const WCHAR uuidFmt[] = {
291 '%','0','8','x','-','%','0','4','x','-',
292 '%','0','4','x','-','%','0','2','x',
293 '%','0','2','x','-','%','0','2','x',
294 '%','0','2','x','%','0','2','x',
295 '%','0','2','x','%','0','2','x',
296 '%','0','2','x',0 };
298 rs = UuidCreate(&uuid);
299 if (rs == S_OK)
301 sprintfW(buf, uuidFmt,
302 uuid.Data1, uuid.Data2, uuid.Data3,
303 uuid.Data4[0], uuid.Data4[1],
304 uuid.Data4[2], uuid.Data4[3],
305 uuid.Data4[4], uuid.Data4[5],
306 uuid.Data4[6], uuid.Data4[7] );
307 RegSetValueExW(key, machineGuidW, 0, REG_SZ,
308 (const BYTE *)buf,
309 (lstrlenW(buf)+1)*sizeof(WCHAR));
312 RegCloseKey(key);
316 /******************************************************************************
317 * CryptAcquireContextW (ADVAPI32.@)
319 * Acquire a crypto provider context handle.
321 * PARAMS
322 * phProv [O] Pointer to HCRYPTPROV for the output.
323 * pszContainer [I] Key Container Name
324 * pszProvider [I] Cryptographic Service Provider Name
325 * dwProvType [I] Crypto provider type to get a handle.
326 * dwFlags [I] flags for the operation
328 * RETURNS
329 * TRUE on success, FALSE on failure.
331 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
332 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
334 PCRYPTPROV pProv = NULL;
335 HKEY key;
336 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
337 PSTR provnameA = NULL, pszContainerA = NULL;
338 DWORD keytype, type, len;
339 ULONG r;
340 static const WCHAR nameW[] = {'N','a','m','e',0};
341 static const WCHAR typeW[] = {'T','y','p','e',0};
342 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
344 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
345 debugstr_w(pszProvider), dwProvType, dwFlags);
347 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
349 SetLastError(NTE_BAD_PROV_TYPE);
350 return FALSE;
353 if (!phProv)
355 SetLastError(ERROR_INVALID_PARAMETER);
356 return FALSE;
359 /* Make sure the MachineGuid value exists */
360 CRYPT_CreateMachineGuid();
362 if (!pszProvider || !*pszProvider)
364 /* No CSP name specified so try the user default CSP first
365 * then try the machine default CSP
367 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
368 TRACE("No provider registered for crypto provider type %d.\n", dwProvType);
369 SetLastError(NTE_PROV_TYPE_NOT_DEF);
370 return FALSE;
372 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
374 CRYPT_Free(keyname);
375 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
376 TRACE("No type registered for crypto provider type %d.\n", dwProvType);
377 RegCloseKey(key);
378 SetLastError(NTE_PROV_TYPE_NOT_DEF);
379 goto error;
381 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
382 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
383 CRYPT_Free(keyname);
384 RegCloseKey(key);
385 SetLastError(NTE_PROV_TYPE_NOT_DEF);
386 goto error;
389 CRYPT_Free(keyname);
390 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
391 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
393 TRACE("error %d reading size of 'Name' from registry\n", r );
394 RegCloseKey(key);
395 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
396 goto error;
398 if(!(provname = CRYPT_Alloc(len)))
400 RegCloseKey(key);
401 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
402 goto error;
404 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
405 if( r != ERROR_SUCCESS )
407 TRACE("error %d reading 'Name' from registry\n", r );
408 RegCloseKey(key);
409 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
410 goto error;
412 RegCloseKey(key);
413 } else {
414 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
416 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
417 goto error;
419 strcpyW(provname, pszProvider);
422 keyname = CRYPT_GetProvKeyName(provname);
423 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
424 CRYPT_Free(keyname);
425 if (r != ERROR_SUCCESS)
427 SetLastError(NTE_KEYSET_NOT_DEF);
428 goto error;
430 len = sizeof(DWORD);
431 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
432 if (r != ERROR_SUCCESS)
434 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
435 goto error;
437 if (type != dwProvType)
439 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
440 SetLastError(NTE_PROV_TYPE_NO_MATCH);
441 goto error;
444 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
445 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
447 TRACE("error %d reading size of 'Image Path' from registry\n", r );
448 RegCloseKey(key);
449 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
450 goto error;
452 if (!(temp = CRYPT_Alloc(len)))
454 RegCloseKey(key);
455 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
456 goto error;
458 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
459 if( r != ERROR_SUCCESS )
461 TRACE("error %d reading 'Image Path' from registry\n", r );
462 RegCloseKey(key);
463 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
464 goto error;
466 RegCloseKey(key);
467 len = ExpandEnvironmentStringsW(temp, NULL, 0);
468 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
470 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
471 goto error;
473 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
475 /* ExpandEnvironmentStrings will call SetLastError */
476 goto error;
478 pProv = CRYPT_LoadProvider(imagepath);
479 if (!pProv) {
480 /* CRYPT_LoadProvider calls SetLastError */
481 goto error;
483 pProv->pVTable->dwProvType = dwProvType;
484 if(!CRYPT_UnicodeToANSI(provname, &provnameA, -1))
486 /* CRYPT_UnicodeToANSI calls SetLastError */
487 goto error;
489 pProv->pVTable->pszProvName = provnameA;
490 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, -1))
492 /* CRYPT_UnicodeToANSI calls SetLastError */
493 goto error;
495 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
497 /* MSDN: When this flag is set, the value returned in phProv is undefined,
498 * and thus, the CryptReleaseContext function need not be called afterwards.
499 * Therefore, we must clean up everything now.
501 if (dwFlags & CRYPT_DELETEKEYSET)
503 pProv->dwMagic = 0;
504 FreeLibrary(pProv->hModule);
505 CRYPT_Free(provnameA);
506 CRYPT_Free(pProv->pVTable);
507 CRYPT_Free(pProv->pFuncs);
508 CRYPT_Free(pProv);
509 } else {
510 *phProv = (HCRYPTPROV)pProv;
512 CRYPT_Free(pszContainerA);
513 CRYPT_Free(provname);
514 CRYPT_Free(temp);
515 CRYPT_Free(imagepath);
516 return TRUE;
518 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
519 error:
520 if (pProv)
522 pProv->dwMagic = 0;
523 if (pProv->hModule)
524 FreeLibrary(pProv->hModule);
525 CRYPT_Free(pProv->pVTable);
526 CRYPT_Free(pProv->pFuncs);
527 CRYPT_Free(pProv);
529 CRYPT_Free(pszContainerA);
530 CRYPT_Free(provnameA);
531 CRYPT_Free(provname);
532 CRYPT_Free(temp);
533 CRYPT_Free(imagepath);
534 return FALSE;
537 /******************************************************************************
538 * CryptAcquireContextA (ADVAPI32.@)
540 * See CryptAcquireContextW.
542 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
543 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
545 PWSTR pProvider = NULL, pContainer = NULL;
546 BOOL ret = FALSE;
548 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, pszContainer,
549 pszProvider, dwProvType, dwFlags);
551 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
553 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
554 return FALSE;
556 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
558 CRYPT_Free(pContainer);
559 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
560 return FALSE;
563 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
565 CRYPT_Free(pContainer);
566 CRYPT_Free(pProvider);
568 return ret;
571 /******************************************************************************
572 * CryptContextAddRef (ADVAPI32.@)
574 * Increases reference count of a cryptographic service provider handle
575 * by one.
577 * PARAMS
578 * hProv [I] Handle to the CSP whose reference is being incremented.
579 * pdwReserved [IN] Reserved for future use and must be NULL.
580 * dwFlags [I] Reserved for future use and must be NULL.
582 * RETURNS
583 * Success: TRUE
584 * Failure: FALSE
586 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
588 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
590 TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
592 if (!pProv)
594 SetLastError(NTE_BAD_UID);
595 return FALSE;
598 if (pProv->dwMagic != MAGIC_CRYPTPROV)
600 SetLastError(ERROR_INVALID_PARAMETER);
601 return FALSE;
604 pProv->refcount++;
605 return TRUE;
608 /******************************************************************************
609 * CryptReleaseContext (ADVAPI32.@)
611 * Releases the handle of a CSP. Reference count is decreased.
613 * PARAMS
614 * hProv [I] Handle of a CSP.
615 * dwFlags [I] Reserved for future use and must be NULL.
617 * RETURNS
618 * Success: TRUE
619 * Failure: FALSE
621 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, ULONG_PTR dwFlags)
623 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
624 BOOL ret = TRUE;
626 TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
628 if (!pProv)
630 SetLastError(NTE_BAD_UID);
631 return FALSE;
634 if (pProv->dwMagic != MAGIC_CRYPTPROV)
636 SetLastError(ERROR_INVALID_PARAMETER);
637 return FALSE;
640 pProv->refcount--;
641 if (pProv->refcount <= 0)
643 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
644 pProv->dwMagic = 0;
645 FreeLibrary(pProv->hModule);
646 #if 0
647 CRYPT_Free(pProv->pVTable->pContextInfo);
648 #endif
649 CRYPT_Free(pProv->pVTable->pszProvName);
650 CRYPT_Free(pProv->pVTable);
651 CRYPT_Free(pProv->pFuncs);
652 CRYPT_Free(pProv);
654 return ret;
657 /******************************************************************************
658 * CryptGenRandom (ADVAPI32.@)
660 * Fills a buffer with cryptographically random bytes.
662 * PARAMS
663 * hProv [I] Handle of a CSP.
664 * dwLen [I] Number of bytes to generate.
665 * pbBuffer [I/O] Buffer to contain random bytes.
667 * RETURNS
668 * Success: TRUE
669 * Failure: FALSE
671 * NOTES
672 * pdBuffer must be at least dwLen bytes long.
674 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
676 PCRYPTPROV prov = (PCRYPTPROV)hProv;
678 TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
680 if (!hProv)
682 SetLastError(ERROR_INVALID_HANDLE);
683 return FALSE;
686 if (prov->dwMagic != MAGIC_CRYPTPROV)
688 SetLastError(ERROR_INVALID_PARAMETER);
689 return FALSE;
692 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
695 /******************************************************************************
696 * CryptCreateHash (ADVAPI32.@)
698 * Initiates the hashing of a stream of data.
700 * PARAMS
701 * hProv [I] Handle of a CSP.
702 * Algid [I] Identifies the hash algorithm to use.
703 * hKey [I] Key for the hash (if required).
704 * dwFlags [I] Reserved for future use and must be NULL.
705 * phHash [O] Address of the future handle to the new hash object.
707 * RETURNS
708 * Success: TRUE
709 * Failure: FALSE
711 * NOTES
712 * If the algorithm is a keyed hash, hKey is the key.
714 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
715 DWORD dwFlags, HCRYPTHASH *phHash)
717 PCRYPTPROV prov = (PCRYPTPROV)hProv;
718 PCRYPTKEY key = (PCRYPTKEY)hKey;
719 PCRYPTHASH hash;
721 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
723 if (!prov)
725 SetLastError(ERROR_INVALID_HANDLE);
726 return FALSE;
728 if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV ||
729 (key && key->dwMagic != MAGIC_CRYPTKEY))
731 SetLastError(ERROR_INVALID_PARAMETER);
732 return FALSE;
734 if (dwFlags)
736 SetLastError(NTE_BAD_FLAGS);
737 return FALSE;
739 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
741 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
742 return FALSE;
745 hash->pProvider = prov;
746 hash->dwMagic = MAGIC_CRYPTHASH;
747 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
748 key ? key->hPrivate : 0, 0, &hash->hPrivate))
750 *phHash = (HCRYPTHASH)hash;
751 return TRUE;
754 /* CSP error! */
755 hash->dwMagic = 0;
756 CRYPT_Free(hash);
757 *phHash = 0;
758 return FALSE;
761 /******************************************************************************
762 * CryptDecrypt (ADVAPI32.@)
764 * Decrypts data encrypted by CryptEncrypt.
766 * PARAMS
767 * hKey [I] Handle to the decryption key.
768 * hHash [I] Handle to a hash object.
769 * Final [I] TRUE if this is the last section to be decrypted.
770 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
771 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
772 * data on return
773 * pdwDataLen [I/O] Length of pbData before and after the call.
775 * RETURNS
776 * Success: TRUE
777 * Failure: FALSE
779 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
780 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
782 PCRYPTPROV prov;
783 PCRYPTKEY key = (PCRYPTKEY)hKey;
784 PCRYPTHASH hash = (PCRYPTHASH)hHash;
786 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
788 if (!key || !pbData || !pdwDataLen ||
789 !key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
790 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
792 SetLastError(ERROR_INVALID_PARAMETER);
793 return FALSE;
796 prov = key->pProvider;
797 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
798 Final, dwFlags, pbData, pdwDataLen);
801 /******************************************************************************
802 * CryptDeriveKey (ADVAPI32.@)
804 * Generates session keys derived from a base data value.
806 * PARAMS
807 * hProv [I] Handle to a CSP.
808 * Algid [I] Identifies the symmetric encryption algorithm to use.
809 * hBaseData [I] Handle to a hash object.
810 * dwFlags [I] Type of key to generate.
811 * phKey [I/O] Address of the newly generated key.
813 * RETURNS
814 * Success: TRUE
815 * Failure: FALSE
817 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
818 DWORD dwFlags, HCRYPTKEY *phKey)
820 PCRYPTPROV prov = (PCRYPTPROV)hProv;
821 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
822 PCRYPTKEY key;
824 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
826 if (!prov || !hash)
828 SetLastError(ERROR_INVALID_HANDLE);
829 return FALSE;
831 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV || hash->dwMagic != MAGIC_CRYPTHASH)
833 SetLastError(ERROR_INVALID_PARAMETER);
834 return FALSE;
836 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
838 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
839 return FALSE;
842 key->pProvider = prov;
843 key->dwMagic = MAGIC_CRYPTKEY;
844 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
846 *phKey = (HCRYPTKEY)key;
847 return TRUE;
850 /* CSP error! */
851 key->dwMagic = 0;
852 CRYPT_Free(key);
853 *phKey = 0;
854 return FALSE;
857 /******************************************************************************
858 * CryptDestroyHash (ADVAPI32.@)
860 * Destroys the hash object referenced by hHash.
862 * PARAMS
863 * hHash [I] Handle of the hash object to be destroyed.
865 * RETURNS
866 * Success: TRUE
867 * Failure: FALSE
869 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
871 PCRYPTHASH hash = (PCRYPTHASH)hHash;
872 PCRYPTPROV prov;
873 BOOL ret;
875 TRACE("(0x%lx)\n", hHash);
877 if (!hash)
879 SetLastError(ERROR_INVALID_HANDLE);
880 return FALSE;
883 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
884 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
886 SetLastError(ERROR_INVALID_PARAMETER);
887 return FALSE;
890 prov = hash->pProvider;
891 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
892 hash->dwMagic = 0;
893 CRYPT_Free(hash);
894 return ret;
897 /******************************************************************************
898 * CryptDestroyKey (ADVAPI32.@)
900 * Releases the handle referenced by hKey.
902 * PARAMS
903 * hKey [I] Handle of the key to be destroyed.
905 * RETURNS
906 * Success: TRUE
907 * Failure: FALSE
909 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
911 PCRYPTKEY key = (PCRYPTKEY)hKey;
912 PCRYPTPROV prov;
913 BOOL ret;
915 TRACE("(0x%lx)\n", hKey);
917 if (!key)
919 SetLastError(ERROR_INVALID_HANDLE);
920 return FALSE;
923 if (!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
924 key->pProvider->dwMagic != MAGIC_CRYPTPROV)
926 SetLastError(ERROR_INVALID_PARAMETER);
927 return FALSE;
930 prov = key->pProvider;
931 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
932 key->dwMagic = 0;
933 CRYPT_Free(key);
934 return ret;
937 /******************************************************************************
938 * CryptDuplicateHash (ADVAPI32.@)
940 * Duplicates a hash.
942 * PARAMS
943 * hHash [I] Handle to the hash to be copied.
944 * pdwReserved [I] Reserved for future use and must be zero.
945 * dwFlags [I] Reserved for future use and must be zero.
946 * phHash [O] Address of the handle to receive the copy.
948 * RETURNS
949 * Success: TRUE
950 * Failure: FALSE
952 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
953 DWORD dwFlags, HCRYPTHASH *phHash)
955 PCRYPTPROV prov;
956 PCRYPTHASH orghash, newhash;
958 TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
960 orghash = (PCRYPTHASH)hHash;
961 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
962 orghash->dwMagic != MAGIC_CRYPTHASH || orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
964 SetLastError(ERROR_INVALID_PARAMETER);
965 return FALSE;
968 prov = orghash->pProvider;
969 if (!prov->pFuncs->pCPDuplicateHash)
971 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
972 return FALSE;
975 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
977 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
978 return FALSE;
981 newhash->pProvider = prov;
982 newhash->dwMagic = MAGIC_CRYPTHASH;
983 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
985 *phHash = (HCRYPTHASH)newhash;
986 return TRUE;
988 newhash->dwMagic = 0;
989 CRYPT_Free(newhash);
990 return FALSE;
993 /******************************************************************************
994 * CryptDuplicateKey (ADVAPI32.@)
996 * Duplicate a key and the key's state.
998 * PARAMS
999 * hKey [I] Handle of the key to copy.
1000 * pdwReserved [I] Reserved for future use and must be NULL.
1001 * dwFlags [I] Reserved for future use and must be zero.
1002 * phKey [I] Address of the handle to the duplicated key.
1004 * RETURNS
1005 * Success: TRUE
1006 * Failure: FALSE
1008 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
1010 PCRYPTPROV prov;
1011 PCRYPTKEY orgkey, newkey;
1013 TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
1015 orgkey = (PCRYPTKEY)hKey;
1016 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
1017 orgkey->dwMagic != MAGIC_CRYPTKEY ||
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 newkey->dwMagic = MAGIC_CRYPTKEY;
1039 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
1041 *phKey = (HCRYPTKEY)newkey;
1042 return TRUE;
1044 newkey->dwMagic = 0;
1045 CRYPT_Free(newkey);
1046 return FALSE;
1049 /******************************************************************************
1050 * CryptEncrypt (ADVAPI32.@)
1052 * Encrypts data.
1054 * PARAMS
1055 * hKey [I] Handle to the encryption key.
1056 * hHash [I] Handle to a hash object.
1057 * Final [I] TRUE if this is the last section to encrypt.
1058 * dwFlags [I] Can be CRYPT_OAEP.
1059 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1060 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1061 * encrypted data after call.
1062 * dwBufLen [I] Length of the input pbData buffer.
1064 * RETURNS
1065 * Success: TRUE
1066 * Failure: FALSE
1068 * NOTES
1069 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1070 * required for the returned data in pdwDataLen.
1072 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1073 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1075 PCRYPTPROV prov;
1076 PCRYPTKEY key = (PCRYPTKEY)hKey;
1077 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1079 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1081 if (!key || !pdwDataLen || !key->pProvider ||
1082 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1084 SetLastError(ERROR_INVALID_PARAMETER);
1085 return FALSE;
1088 prov = key->pProvider;
1089 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
1090 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1093 /******************************************************************************
1094 * CryptEnumProvidersW (ADVAPI32.@)
1096 * Returns the next available CSP.
1098 * PARAMS
1099 * dwIndex [I] Index of the next provider to be enumerated.
1100 * pdwReserved [I] Reserved for future use and must be NULL.
1101 * dwFlags [I] Reserved for future use and must be zero.
1102 * pdwProvType [O] DWORD designating the type of the provider.
1103 * pszProvName [O] Buffer that receives data from the provider.
1104 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1105 * of bytes stored in the buffer on return.
1107 * RETURNS
1108 * Success: TRUE
1109 * Failure: FALSE
1111 * NOTES
1112 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1113 * for memory allocation purposes.
1115 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
1116 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1118 HKEY hKey;
1119 static const WCHAR providerW[] = {
1120 'S','o','f','t','w','a','r','e','\\',
1121 'M','i','c','r','o','s','o','f','t','\\',
1122 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1123 'D','e','f','a','u','l','t','s','\\',
1124 'P','r','o','v','i','d','e','r',0
1126 static const WCHAR typeW[] = {'T','y','p','e',0};
1128 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1129 pdwProvType, pszProvName, pcbProvName);
1131 if (pdwReserved || !pcbProvName)
1133 SetLastError(ERROR_INVALID_PARAMETER);
1134 return FALSE;
1136 if (dwFlags)
1138 SetLastError(NTE_BAD_FLAGS);
1139 return FALSE;
1142 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1144 SetLastError(NTE_FAIL);
1145 return FALSE;
1148 if (!pszProvName)
1150 DWORD numkeys;
1151 WCHAR *provNameW;
1153 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1154 NULL, NULL, NULL, NULL, NULL, NULL);
1156 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1158 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1159 return FALSE;
1162 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1163 CRYPT_Free(provNameW);
1164 (*pcbProvName)++;
1165 *pcbProvName *= sizeof(WCHAR);
1167 if (dwIndex >= numkeys)
1169 SetLastError(ERROR_NO_MORE_ITEMS);
1170 return FALSE;
1172 } else {
1173 DWORD size = sizeof(DWORD);
1174 DWORD result;
1175 HKEY subkey;
1177 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1178 if (result)
1180 SetLastError(result);
1181 return FALSE;
1183 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1184 return FALSE;
1185 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1186 return FALSE;
1187 RegCloseKey(subkey);
1189 RegCloseKey(hKey);
1190 return TRUE;
1193 /******************************************************************************
1194 * CryptEnumProvidersA (ADVAPI32.@)
1196 * See CryptEnumProvidersW.
1198 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1199 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1201 PWSTR str = NULL;
1202 DWORD bufsize;
1203 BOOL ret; /* = FALSE; */
1205 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1206 pdwProvType, pszProvName, pcbProvName);
1208 if(!CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1209 return FALSE;
1210 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1212 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1213 return FALSE;
1215 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1216 if (str)
1217 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1218 *pcbProvName = bufsize / sizeof(WCHAR); /* FIXME: not correct */
1219 if (str)
1221 CRYPT_Free(str);
1222 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1224 SetLastError(ERROR_MORE_DATA);
1225 return FALSE;
1228 return ret;
1231 /******************************************************************************
1232 * CryptEnumProviderTypesW (ADVAPI32.@)
1234 * Retrieves the next type of CSP supported.
1236 * PARAMS
1237 * dwIndex [I] Index of the next provider to be enumerated.
1238 * pdwReserved [I] Reserved for future use and must be NULL.
1239 * dwFlags [I] Reserved for future use and must be zero.
1240 * pdwProvType [O] DWORD designating the type of the provider.
1241 * pszTypeName [O] Buffer that receives data from the provider type.
1242 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1243 * of bytes stored in the buffer on return.
1245 * RETURNS
1246 * Success: TRUE
1247 * Failure: FALSE
1249 * NOTES
1250 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1251 * for memory allocation purposes.
1253 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1254 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1256 HKEY hKey, hSubkey;
1257 DWORD keylen, numkeys, dwType;
1258 PWSTR keyname, ch;
1259 DWORD result;
1260 static const WCHAR KEYSTR[] = {
1261 'S','o','f','t','w','a','r','e','\\',
1262 'M','i','c','r','o','s','o','f','t','\\',
1263 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1264 'D','e','f','a','u','l','t','s','\\',
1265 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1267 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1269 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1270 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1272 if (pdwReserved || !pdwProvType || !pcbTypeName)
1274 SetLastError(ERROR_INVALID_PARAMETER);
1275 return FALSE;
1277 if (dwFlags)
1279 SetLastError(NTE_BAD_FLAGS);
1280 return FALSE;
1283 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1284 return FALSE;
1286 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1287 if (dwIndex >= numkeys)
1289 SetLastError(ERROR_NO_MORE_ITEMS);
1290 return FALSE;
1292 keylen++;
1293 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1295 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1296 return FALSE;
1298 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1299 CRYPT_Free(keyname);
1300 return FALSE;
1302 RegOpenKeyW(hKey, keyname, &hSubkey);
1303 ch = keyname + strlenW(keyname);
1304 /* Convert "Type 000" to 0, etc/ */
1305 *pdwProvType = *(--ch) - '0';
1306 *pdwProvType += (*(--ch) - '0') * 10;
1307 *pdwProvType += (*(--ch) - '0') * 100;
1308 CRYPT_Free(keyname);
1310 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1311 if (result)
1313 SetLastError(result);
1314 return FALSE;
1317 RegCloseKey(hSubkey);
1318 RegCloseKey(hKey);
1319 return TRUE;
1322 /******************************************************************************
1323 * CryptEnumProviderTypesA (ADVAPI32.@)
1325 * See CryptEnumProviderTypesW.
1327 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1328 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1330 PWSTR str = NULL;
1331 DWORD bufsize;
1332 BOOL ret;
1334 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1335 pdwProvType, pszTypeName, pcbTypeName);
1337 if(!CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &bufsize))
1338 return FALSE;
1339 if ( pszTypeName && !(str = CRYPT_Alloc(bufsize)) )
1341 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1342 return FALSE;
1344 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &bufsize);
1345 if (str)
1346 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1347 *pcbTypeName = bufsize / sizeof(WCHAR);
1348 if (str)
1350 CRYPT_Free(str);
1351 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1353 SetLastError(ERROR_MORE_DATA);
1354 return FALSE;
1357 return ret;
1360 /******************************************************************************
1361 * CryptExportKey (ADVAPI32.@)
1363 * Exports a cryptographic key from a CSP.
1365 * PARAMS
1366 * hKey [I] Handle to the key to export.
1367 * hExpKey [I] Handle to a cryptographic key of the end user.
1368 * dwBlobType [I] Type of BLOB to be exported.
1369 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1370 * pbData [O] Buffer to receive BLOB data.
1371 * pdwDataLen [I/O] Specifies the size of pbData.
1373 * RETURNS
1374 * Success: TRUE
1375 * Failure: FALSE
1377 * NOTES
1378 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1379 * buffer needed to hold the BLOB.
1381 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1382 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1384 PCRYPTPROV prov;
1385 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1387 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1389 if (!key || !pdwDataLen || !key->pProvider ||
1390 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1392 SetLastError(ERROR_INVALID_PARAMETER);
1393 return FALSE;
1396 prov = key->pProvider;
1397 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1398 dwBlobType, dwFlags, pbData, pdwDataLen);
1401 /******************************************************************************
1402 * CryptGenKey (ADVAPI32.@)
1404 * Generates a random cryptographic session key or a pub/priv key pair.
1406 * PARAMS
1407 * hProv [I] Handle to a CSP.
1408 * Algid [I] Algorithm to use to make key.
1409 * dwFlags [I] Specifies type of key to make.
1410 * phKey [I] Address of the handle to which the new key is copied.
1412 * RETURNS
1413 * Success: TRUE
1414 * Failure: FALSE
1416 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1418 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1419 PCRYPTKEY key;
1421 TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1423 if (!prov)
1425 SetLastError(ERROR_INVALID_HANDLE);
1426 return FALSE;
1428 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1430 SetLastError(ERROR_INVALID_PARAMETER);
1431 return FALSE;
1433 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1435 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1436 return FALSE;
1439 key->pProvider = prov;
1440 key->dwMagic = MAGIC_CRYPTKEY;
1441 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1443 *phKey = (HCRYPTKEY)key;
1444 return TRUE;
1447 /* CSP error! */
1448 key->dwMagic = 0;
1449 CRYPT_Free(key);
1450 return FALSE;
1453 /******************************************************************************
1454 * CryptGetDefaultProviderW (ADVAPI32.@)
1456 * Finds the default CSP of a certain provider type.
1458 * PARAMS
1459 * dwProvType [I] Provider type to look for.
1460 * pdwReserved [I] Reserved for future use and must be NULL.
1461 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1462 * pszProvName [O] Name of the default CSP.
1463 * pcbProvName [I/O] Size of pszProvName
1465 * RETURNS
1466 * Success: TRUE
1467 * Failure: FALSE
1469 * NOTES
1470 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1471 * memory allocation purposes on return.
1473 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1474 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1476 HKEY hKey;
1477 PWSTR keyname;
1478 DWORD result;
1479 static const WCHAR nameW[] = {'N','a','m','e',0};
1481 if (pdwReserved || !pcbProvName)
1483 SetLastError(ERROR_INVALID_PARAMETER);
1484 return FALSE;
1486 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1488 SetLastError(NTE_BAD_FLAGS);
1489 return FALSE;
1491 if (dwProvType > 999)
1493 SetLastError(NTE_BAD_PROV_TYPE);
1494 return FALSE;
1496 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1498 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1499 return FALSE;
1501 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1503 CRYPT_Free(keyname);
1504 SetLastError(NTE_PROV_TYPE_NOT_DEF);
1505 return FALSE;
1507 CRYPT_Free(keyname);
1509 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1510 if (result)
1512 if (result != ERROR_MORE_DATA)
1513 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1514 else
1515 SetLastError(result);
1517 return FALSE;
1520 RegCloseKey(hKey);
1521 return TRUE;
1524 /******************************************************************************
1525 * CryptGetDefaultProviderA (ADVAPI32.@)
1527 * See CryptGetDefaultProviderW.
1529 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1530 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1532 PWSTR str = NULL;
1533 DWORD bufsize;
1534 BOOL ret = FALSE;
1536 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1538 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &bufsize);
1539 if ( pszProvName && !(str = CRYPT_Alloc(bufsize)) )
1541 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1542 return FALSE;
1544 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &bufsize);
1545 if (str)
1546 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1547 *pcbProvName = bufsize / sizeof(WCHAR);
1548 if (str)
1550 CRYPT_Free(str);
1551 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1553 SetLastError(ERROR_MORE_DATA);
1554 return FALSE;
1557 return ret;
1560 /******************************************************************************
1561 * CryptGetHashParam (ADVAPI32.@)
1563 * Retrieves data that controls the operations of a hash object.
1565 * PARAMS
1566 * hHash [I] Handle of the hash object to question.
1567 * dwParam [I] Query type.
1568 * pbData [O] Buffer that receives the value data.
1569 * pdwDataLen [I/O] Size of the pbData buffer.
1570 * dwFlags [I] Reserved for future use and must be zero.
1572 * RETURNS
1573 * Success: TRUE
1574 * Failure: FALSE
1576 * NOTES
1577 * If pbData is NULL, pdwDataLen will contain the length required.
1579 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1580 DWORD *pdwDataLen, DWORD dwFlags)
1582 PCRYPTPROV prov;
1583 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1585 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1587 if (!hash || !pdwDataLen || !hash->pProvider ||
1588 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1590 SetLastError(ERROR_INVALID_PARAMETER);
1591 return FALSE;
1594 prov = hash->pProvider;
1595 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1596 pbData, pdwDataLen, dwFlags);
1599 /******************************************************************************
1600 * CryptGetKeyParam (ADVAPI32.@)
1602 * Retrieves data that controls the operations of a key.
1604 * PARAMS
1605 * hKey [I] Handle to they key in question.
1606 * dwParam [I] Specifies query type.
1607 * pbData [O] Sequence of bytes to receive data.
1608 * pdwDataLen [I/O] Size of pbData.
1609 * dwFlags [I] Reserved for future use and must be zero.
1611 * RETURNS
1612 * Success: TRUE
1613 * Failure: FALSE
1615 * NOTES
1616 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1618 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1619 DWORD *pdwDataLen, DWORD dwFlags)
1621 PCRYPTPROV prov;
1622 PCRYPTKEY key = (PCRYPTKEY)hKey;
1624 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1626 if (!key || !pdwDataLen || !key->pProvider ||
1627 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1629 SetLastError(ERROR_INVALID_PARAMETER);
1630 return FALSE;
1633 prov = key->pProvider;
1634 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1635 pbData, pdwDataLen, dwFlags);
1638 /******************************************************************************
1639 * CryptGetProvParam (ADVAPI32.@)
1641 * Retrieves parameters that control the operations of a CSP.
1643 * PARAMS
1644 * hProv [I] Handle of the CSP in question.
1645 * dwParam [I] Specifies query type.
1646 * pbData [O] Buffer to receive the data.
1647 * pdwDataLen [I/O] Size of pbData.
1648 * dwFlags [I] see MSDN Docs.
1650 * RETURNS
1651 * Success: TRUE
1652 * Failure: FALSE
1654 * NOTES
1655 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1657 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1658 DWORD *pdwDataLen, DWORD dwFlags)
1660 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1662 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1664 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1666 SetLastError(ERROR_INVALID_PARAMETER);
1667 return FALSE;
1670 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1673 /******************************************************************************
1674 * CryptGetUserKey (ADVAPI32.@)
1676 * Gets a handle of one of a user's two public/private key pairs.
1678 * PARAMS
1679 * hProv [I] Handle of a CSP.
1680 * dwKeySpec [I] Private key to use.
1681 * phUserKey [O] Pointer to the handle of the retrieved keys.
1683 * RETURNS
1684 * Success: TRUE
1685 * Failure: FALSE
1687 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1689 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1690 PCRYPTKEY key;
1692 TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1694 if (!prov)
1696 SetLastError(ERROR_INVALID_HANDLE);
1697 return FALSE;
1699 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1701 SetLastError(ERROR_INVALID_PARAMETER);
1702 return FALSE;
1704 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1706 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1707 return FALSE;
1710 key->pProvider = prov;
1711 key->dwMagic = MAGIC_CRYPTKEY;
1712 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1714 *phUserKey = (HCRYPTKEY)key;
1715 return TRUE;
1718 /* CSP Error */
1719 key->dwMagic = 0;
1720 CRYPT_Free(key);
1721 *phUserKey = 0;
1722 return FALSE;
1725 /******************************************************************************
1726 * CryptHashData (ADVAPI32.@)
1728 * Adds data to a hash object.
1730 * PARAMS
1731 * hHash [I] Handle of the hash object.
1732 * pbData [I] Buffer of data to be hashed.
1733 * dwDataLen [I] Number of bytes to add.
1734 * dwFlags [I] Can be CRYPT_USERDATA
1736 * RETURNS
1737 * Success: TRUE
1738 * Failure: FALSE
1740 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1742 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1743 PCRYPTPROV prov;
1745 TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1747 if (!hash)
1749 SetLastError(ERROR_INVALID_HANDLE);
1750 return FALSE;
1752 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1753 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1755 SetLastError(ERROR_INVALID_PARAMETER);
1756 return FALSE;
1759 prov = hash->pProvider;
1760 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1763 /******************************************************************************
1764 * CryptHashSessionKey (ADVAPI32.@)
1766 * Compute the cryptographic hash of a session key object.
1768 * PARAMS
1769 * hHash [I] Handle to the hash object.
1770 * hKey [I] Handle to the key to be hashed.
1771 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1773 * RETURNS
1774 * Success: TRUE
1775 * Failure: FALSE
1777 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1779 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1780 PCRYPTKEY key = (PCRYPTKEY)hKey;
1781 PCRYPTPROV prov;
1783 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1785 if (!hash || !key)
1787 SetLastError(ERROR_INVALID_HANDLE);
1788 return FALSE;
1791 if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1792 hash->pProvider->dwMagic != MAGIC_CRYPTPROV || key->dwMagic != MAGIC_CRYPTKEY)
1794 SetLastError(ERROR_INVALID_PARAMETER);
1795 return FALSE;
1798 prov = hash->pProvider;
1799 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1802 /******************************************************************************
1803 * CryptImportKey (ADVAPI32.@)
1805 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1807 * PARAMS
1808 * hProv [I] Handle of a CSP.
1809 * pbData [I] Contains the key to be imported.
1810 * dwDataLen [I] Length of the key.
1811 * hPubKey [I] Cryptographic key that decrypts pdData
1812 * dwFlags [I] Used only with a public/private key pair.
1813 * phKey [O] Imported key.
1815 * RETURNS
1816 * Success: TRUE
1817 * Failure: FALSE
1819 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1820 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1822 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1823 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1825 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1827 if (!prov || !pbData || !dwDataLen || !phKey ||
1828 prov->dwMagic != MAGIC_CRYPTPROV ||
1829 (pubkey && pubkey->dwMagic != MAGIC_CRYPTKEY))
1831 SetLastError(ERROR_INVALID_PARAMETER);
1832 return FALSE;
1835 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1837 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1838 return FALSE;
1841 importkey->pProvider = prov;
1842 importkey->dwMagic = MAGIC_CRYPTKEY;
1843 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1844 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1846 *phKey = (HCRYPTKEY)importkey;
1847 return TRUE;
1850 importkey->dwMagic = 0;
1851 CRYPT_Free(importkey);
1852 return FALSE;
1855 /******************************************************************************
1856 * CryptSignHashW (ADVAPI32.@)
1858 * Signs data.
1860 * PARAMS
1861 * hHash [I] Handle of the hash object to be signed.
1862 * dwKeySpec [I] Private key to use.
1863 * sDescription [I] Should be NULL.
1864 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1865 * pbSignature [O] Buffer of the signature data.
1866 * pdwSigLen [I/O] Size of the pbSignature buffer.
1868 * RETURNS
1869 * Success: TRUE
1870 * Failure: FALSE
1872 * NOTES
1873 * Because of security flaws sDescription should not be used and should thus be
1874 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1875 * Providers.
1877 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1878 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1880 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1881 PCRYPTPROV prov;
1883 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1884 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1886 if (!hash)
1888 SetLastError(ERROR_INVALID_HANDLE);
1889 return FALSE;
1891 if (!pdwSigLen || !hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
1892 hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1894 SetLastError(ERROR_INVALID_PARAMETER);
1895 return FALSE;
1898 prov = hash->pProvider;
1899 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1900 dwFlags, pbSignature, pdwSigLen);
1903 /******************************************************************************
1904 * CryptSignHashA (ADVAPI32.@)
1906 * See CryptSignHashW.
1908 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1909 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1911 LPWSTR wsDescription;
1912 BOOL result;
1914 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1915 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1917 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1918 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1919 CRYPT_Free(wsDescription);
1921 return result;
1924 /******************************************************************************
1925 * CryptSetHashParam (ADVAPI32.@)
1927 * Customizes the operations of a hash object.
1929 * PARAMS
1930 * hHash [I] Handle of the hash object to set parameters.
1931 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1932 * pbData [I] Value data buffer.
1933 * dwFlags [I] Reserved for future use and must be zero.
1935 * RETURNS
1936 * Success: TRUE
1937 * Failure: FALSE
1939 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1941 PCRYPTPROV prov;
1942 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1944 TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1946 if (!hash || !pbData || !hash->pProvider ||
1947 hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1949 SetLastError(ERROR_INVALID_PARAMETER);
1950 return FALSE;
1953 prov = hash->pProvider;
1954 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1955 dwParam, pbData, dwFlags);
1958 /******************************************************************************
1959 * CryptSetKeyParam (ADVAPI32.@)
1961 * Customizes a session key's operations.
1963 * PARAMS
1964 * hKey [I] Handle to the key to set values.
1965 * dwParam [I] See MSDN Doc.
1966 * pbData [I] Buffer of values to set.
1967 * dwFlags [I] Only used when dwParam == KP_ALGID.
1969 * RETURNS
1970 * Success: TRUE
1971 * Failure: FALSE
1973 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
1975 PCRYPTPROV prov;
1976 PCRYPTKEY key = (PCRYPTKEY)hKey;
1978 TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
1980 if (!key || !pbData || !key->pProvider ||
1981 key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1983 SetLastError(ERROR_INVALID_PARAMETER);
1984 return FALSE;
1987 prov = key->pProvider;
1988 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1989 dwParam, pbData, dwFlags);
1992 /******************************************************************************
1993 * CryptSetProviderA (ADVAPI32.@)
1995 * Specifies the current user's default CSP.
1997 * PARAMS
1998 * pszProvName [I] Name of the new default CSP.
1999 * dwProvType [I] Provider type of the CSP.
2001 * RETURNS
2002 * Success: TRUE
2003 * Failure: FALSE
2005 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
2007 TRACE("(%s, %d)\n", pszProvName, dwProvType);
2008 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2011 /******************************************************************************
2012 * CryptSetProviderW (ADVAPI32.@)
2014 * See CryptSetProviderA.
2016 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
2018 TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
2019 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
2022 /******************************************************************************
2023 * CryptSetProviderExW (ADVAPI32.@)
2025 * Specifies the default CSP.
2027 * PARAMS
2028 * pszProvName [I] Name of the new default CSP.
2029 * dwProvType [I] Provider type of the CSP.
2030 * pdwReserved [I] Reserved for future use and must be NULL.
2031 * dwFlags [I] See MSDN Doc.
2033 * RETURNS
2034 * Success: TRUE
2035 * Failure: FALSE
2037 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2039 HKEY hProvKey, hTypeKey;
2040 PWSTR keyname;
2041 static const WCHAR nameW[] = {'N','a','m','e',0};
2043 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
2045 if (!pszProvName || pdwReserved)
2047 SetLastError(ERROR_INVALID_PARAMETER);
2048 return FALSE;
2050 if (dwProvType > MAXPROVTYPES)
2052 SetLastError(NTE_BAD_PROV_TYPE);
2053 return FALSE;
2055 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
2056 || dwFlags == CRYPT_DELETE_DEFAULT)
2058 SetLastError(NTE_BAD_FLAGS);
2059 return FALSE;
2062 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
2064 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2065 return FALSE;
2067 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2068 keyname, &hTypeKey))
2070 CRYPT_Free(keyname);
2071 SetLastError(NTE_BAD_PROVIDER);
2072 return FALSE;
2074 CRYPT_Free(keyname);
2076 if (dwFlags & CRYPT_DELETE_DEFAULT)
2078 RegDeleteValueW(hTypeKey, nameW);
2080 else
2082 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
2084 RegCloseKey(hTypeKey);
2085 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2086 return FALSE;
2088 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2089 keyname, &hProvKey))
2091 CRYPT_Free(keyname);
2092 RegCloseKey(hTypeKey);
2093 SetLastError(NTE_BAD_PROVIDER);
2094 return FALSE;
2096 CRYPT_Free(keyname);
2098 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
2099 (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2101 RegCloseKey(hTypeKey);
2102 RegCloseKey(hProvKey);
2103 return FALSE;
2106 RegCloseKey(hProvKey);
2108 RegCloseKey(hTypeKey);
2110 return TRUE;
2113 /******************************************************************************
2114 * CryptSetProviderExA (ADVAPI32.@)
2116 * See CryptSetProviderExW.
2118 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2120 BOOL ret = FALSE;
2121 PWSTR str = NULL;
2123 TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2125 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2127 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2128 CRYPT_Free(str);
2130 return ret;
2133 /******************************************************************************
2134 * CryptSetProvParam (ADVAPI32.@)
2136 * Customizes the operations of a CSP.
2138 * PARAMS
2139 * hProv [I] Handle of a CSP.
2140 * dwParam [I] See MSDN Doc.
2141 * pbData [I] Buffer that contains a value to set as a parameter.
2142 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2144 * RETURNS
2145 * Success: TRUE
2146 * Failure: FALSE
2148 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, CONST BYTE *pbData, DWORD dwFlags)
2150 PCRYPTPROV prov = (PCRYPTPROV)hProv;
2152 TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2154 if (!prov)
2156 SetLastError(ERROR_INVALID_HANDLE);
2157 return FALSE;
2159 if (prov->dwMagic != MAGIC_CRYPTPROV)
2161 SetLastError(ERROR_INVALID_PARAMETER);
2162 return FALSE;
2164 if (dwFlags & PP_USE_HARDWARE_RNG)
2166 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2167 FIXME("\tLetting the CSP decide.\n");
2169 if (dwFlags & PP_CLIENT_HWND)
2171 /* FIXME: Should verify the parameter */
2172 if (pbData /* && IsWindow((HWND)pbData) */)
2174 crypt_hWindow = (HWND)(pbData);
2175 return TRUE;
2176 } else {
2177 SetLastError(ERROR_INVALID_PARAMETER);
2178 return FALSE;
2181 /* All other flags go to the CSP */
2182 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2185 /******************************************************************************
2186 * CryptVerifySignatureW (ADVAPI32.@)
2188 * Verifies the signature of a hash object.
2190 * PARAMS
2191 * hHash [I] Handle of the hash object to verify.
2192 * pbSignature [I] Signature data to verify.
2193 * dwSigLen [I] Size of pbSignature.
2194 * hPubKey [I] Handle to the public key to authenticate signature.
2195 * sDescription [I] Should be NULL.
2196 * dwFlags [I] See MSDN doc.
2198 * RETURNS
2199 * Success: TRUE
2200 * Failure: FALSE
2202 * NOTES
2203 * Because of security flaws sDescription should not be used and should thus be
2204 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2205 * Providers.
2207 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2208 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2210 PCRYPTHASH hash = (PCRYPTHASH)hHash;
2211 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
2212 PCRYPTPROV prov;
2214 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2215 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2217 if (!hash || !key || key->dwMagic != MAGIC_CRYPTKEY || hash->dwMagic != MAGIC_CRYPTHASH ||
2218 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
2219 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2221 SetLastError(ERROR_INVALID_PARAMETER);
2222 return FALSE;
2225 prov = hash->pProvider;
2226 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2227 key->hPrivate, sDescription, dwFlags);
2230 /******************************************************************************
2231 * CryptVerifySignatureA (ADVAPI32.@)
2233 * See CryptVerifySignatureW.
2235 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, CONST BYTE *pbSignature, DWORD dwSigLen,
2236 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
2238 LPWSTR wsDescription;
2239 BOOL result;
2241 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2242 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
2244 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
2245 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2246 CRYPT_Free(wsDescription);
2248 return result;
2251 /******************************************************************************
2252 * SystemFunction030 (ADVAPI32.@)
2254 * Tests if two blocks of 16 bytes are equal
2256 * PARAMS
2257 * b1,b2 [I] block of 16 bytes
2259 * RETURNS
2260 * TRUE if blocks are the same
2261 * FALSE if blocks are different
2263 BOOL WINAPI SystemFunction030(LPCVOID b1, LPCVOID b2)
2265 return !memcmp(b1, b2, 0x10);
2268 /******************************************************************************
2269 * SystemFunction035 (ADVAPI32.@)
2271 * Described here:
2272 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2274 * NOTES
2275 * Stub, always return TRUE.
2277 BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
2279 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
2280 return TRUE;
2283 /******************************************************************************
2284 * SystemFunction036 (ADVAPI32.@)
2286 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2288 * PARAMS
2289 * pbBufer [O] Pointer to memory to receive random bytes.
2290 * dwLen [I] Number of random bytes to fetch.
2292 * RETURNS
2293 * Success: TRUE
2294 * Failure: FALSE
2297 BOOLEAN WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2299 int dev_random;
2301 dev_random = open("/dev/urandom", O_RDONLY);
2302 if (dev_random != -1)
2304 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2306 close(dev_random);
2307 return TRUE;
2309 close(dev_random);
2311 else
2312 FIXME("couldn't open /dev/urandom\n");
2313 SetLastError(NTE_FAIL);
2314 return FALSE;
2318 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2319 in crypt32.dll.
2322 /******************************************************************************
2323 * SystemFunction040 (ADVAPI32.@)
2325 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2327 * PARAMS
2328 * memory [I/O] Pointer to memory to encrypt.
2329 * length [I] Length of region to encrypt in bytes.
2330 * flags [I] Control whether other processes are able to decrypt the memory.
2331 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2332 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2333 * RTL_ENCRYPT_OPTION_SAME_LOGON
2335 * RETURNS
2336 * Success: STATUS_SUCCESS
2337 * Failure: NTSTATUS error code
2339 * NOTES
2340 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2341 * If flags are specified when encrypting, the same flag value must be given
2342 * when decrypting the memory.
2344 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2346 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2347 return STATUS_SUCCESS;
2350 /******************************************************************************
2351 * SystemFunction041 (ADVAPI32.@)
2353 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2355 * PARAMS
2356 * memory [I/O] Pointer to memory to decrypt.
2357 * length [I] Length of region to decrypt in bytes.
2358 * flags [I] Control whether other processes are able to decrypt the memory.
2359 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2360 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2361 * RTL_ENCRYPT_OPTION_SAME_LOGON
2363 * RETURNS
2364 * Success: STATUS_SUCCESS
2365 * Failure: NTSTATUS error code
2367 * NOTES
2368 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2369 * If flags are specified when encrypting, the same flag value must be given
2370 * when decrypting the memory.
2372 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2374 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2375 return STATUS_SUCCESS;