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 /***********************************************************************
23 * - Reference counting
28 #include "wine/port.h"
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
43 #define WIN32_NO_STATUS
47 #include "wine/debug.h"
48 #include "wine/unicode.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
53 HWND crypt_hWindow = 0;
55 #define CRYPT_ReturnLastError(err) do {SetLastError(err); return FALSE;} while(0)
57 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
58 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))
60 static inline PWSTR CRYPT_GetProvKeyName(PCWSTR pProvName)
62 static const WCHAR KEYSTR[] = {
63 'S','o','f','t','w','a','r','e','\\',
64 'M','i','c','r','o','s','o','f','t','\\',
65 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
66 'D','e','f','a','u','l','t','s','\\',
67 'P','r','o','v','i','d','e','r','\\',0
71 keyname = CRYPT_Alloc((strlenW(KEYSTR) + strlenW(pProvName) +1)*sizeof(WCHAR));
74 strcpyW(keyname, KEYSTR);
75 strcpyW(keyname + strlenW(KEYSTR), pProvName);
77 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
81 static inline PWSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
83 static const WCHAR MACHINESTR[] = {
84 'S','o','f','t','w','a','r','e','\\',
85 'M','i','c','r','o','s','o','f','t','\\',
86 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
87 'D','e','f','a','u','l','t','s','\\',
88 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
89 'T','y','p','e',' ','X','X','X',0
91 static const WCHAR USERSTR[] = {
92 'S','o','f','t','w','a','r','e','\\',
93 'M','i','c','r','o','s','o','f','t','\\',
94 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
95 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
100 keyname = CRYPT_Alloc( ((user ? strlenW(USERSTR) : strlenW(MACHINESTR)) +1)*sizeof(WCHAR));
103 user ? strcpyW(keyname, USERSTR) : strcpyW(keyname, MACHINESTR);
104 ptr = keyname + strlenW(keyname);
105 *(--ptr) = (dwType % 10) + '0';
106 *(--ptr) = ((dwType / 10) % 10) + '0';
107 *(--ptr) = (dwType / 100) + '0';
109 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
113 /* CRYPT_UnicodeTOANSI
114 * wstr - unicode string
115 * str - pointer to ANSI string
116 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
118 * returns TRUE if unsuccessfull, FALSE otherwise.
119 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
121 static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
130 count = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
132 *str = CRYPT_Alloc(count * sizeof(CHAR));
134 count = min( count, strsize );
137 WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, count, NULL, NULL);
140 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
144 /* CRYPT_ANSITOUnicode
146 * wstr - pointer to unicode string
147 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
149 static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
158 wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
160 *wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
162 wcount = min( wcount, wstrsize/sizeof(WCHAR) );
165 MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
168 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
172 /* These next 2 functions are used by the VTableProvStruc structure */
173 static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
175 if (!lpszImage || !pData)
177 SetLastError(ERROR_INVALID_PARAMETER);
181 FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
186 static BOOL CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
190 *phWnd = crypt_hWindow;
194 #define CRYPT_GetProvFunc(name) \
195 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
196 #define CRYPT_GetProvFuncOpt(name) \
197 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
198 static PCRYPTPROV CRYPT_LoadProvider(PWSTR pImage)
201 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
203 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
204 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
205 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
206 if ( !(provider->hModule = LoadLibraryW(pImage)) )
208 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
209 FIXME("Failed to load dll %s\n", debugstr_w(pImage));
212 provider->dwMagic = MAGIC_CRYPTPROV;
213 provider->refcount = 1;
215 errorcode = NTE_PROVIDER_DLL_FAIL;
216 CRYPT_GetProvFunc(CPAcquireContext);
217 CRYPT_GetProvFunc(CPCreateHash);
218 CRYPT_GetProvFunc(CPDecrypt);
219 CRYPT_GetProvFunc(CPDeriveKey);
220 CRYPT_GetProvFunc(CPDestroyHash);
221 CRYPT_GetProvFunc(CPDestroyKey);
222 CRYPT_GetProvFuncOpt(CPDuplicateHash);
223 CRYPT_GetProvFuncOpt(CPDuplicateKey);
224 CRYPT_GetProvFunc(CPEncrypt);
225 CRYPT_GetProvFunc(CPExportKey);
226 CRYPT_GetProvFunc(CPGenKey);
227 CRYPT_GetProvFunc(CPGenRandom);
228 CRYPT_GetProvFunc(CPGetHashParam);
229 CRYPT_GetProvFunc(CPGetKeyParam);
230 CRYPT_GetProvFunc(CPGetProvParam);
231 CRYPT_GetProvFunc(CPGetUserKey);
232 CRYPT_GetProvFunc(CPHashData);
233 CRYPT_GetProvFunc(CPHashSessionKey);
234 CRYPT_GetProvFunc(CPImportKey);
235 CRYPT_GetProvFunc(CPReleaseContext);
236 CRYPT_GetProvFunc(CPSetHashParam);
237 CRYPT_GetProvFunc(CPSetKeyParam);
238 CRYPT_GetProvFunc(CPSetProvParam);
239 CRYPT_GetProvFunc(CPSignHash);
240 CRYPT_GetProvFunc(CPVerifySignature);
242 /* FIXME: Not sure what the pbContextInfo field is for.
243 * Does it need memory allocation?
245 provider->pVTable->Version = 3;
246 provider->pVTable->pFuncVerifyImage = (FARPROC)CRYPT_VerifyImage;
247 provider->pVTable->pFuncReturnhWnd = (FARPROC)CRYPT_ReturnhWnd;
248 provider->pVTable->dwProvType = 0;
249 provider->pVTable->pbContextInfo = NULL;
250 provider->pVTable->cbContextInfo = 0;
251 provider->pVTable->pszProvName = NULL;
255 SetLastError(errorcode);
258 provider->dwMagic = 0;
259 if (provider->hModule)
260 FreeLibrary(provider->hModule);
261 CRYPT_Free(provider->pVTable);
262 CRYPT_Free(provider->pFuncs);
263 CRYPT_Free(provider);
267 #undef CRYPT_GetProvFunc
268 #undef CRYPT_GetProvFuncOpt
271 /******************************************************************************
272 * CryptAcquireContextW (ADVAPI32.@)
274 * Acquire a crypto provider context handle.
277 * phProv [O] Pointer to HCRYPTPROV for the output.
278 * pszContainer [I] Key Container Name
279 * pszProvider [I] Cryptographic Service Provider Name
280 * dwProvType [I] Crypto provider type to get a handle.
281 * dwFlags [I] flags for the operation
284 * TRUE on success, FALSE on failure.
286 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
287 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
289 PCRYPTPROV pProv = NULL;
291 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
292 PSTR provnameA = NULL, pszContainerA = NULL;
293 DWORD keytype, type, len;
295 static const WCHAR nameW[] = {'N','a','m','e',0};
296 static const WCHAR typeW[] = {'T','y','p','e',0};
297 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
299 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv, debugstr_w(pszContainer),
300 debugstr_w(pszProvider), dwProvType, dwFlags);
302 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
304 SetLastError(NTE_BAD_PROV_TYPE);
310 SetLastError(ERROR_INVALID_PARAMETER);
314 if (!pszProvider || !*pszProvider)
316 /* No CSP name specified so try the user default CSP first
317 * then try the machine default CSP
319 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
320 TRACE("No provider registered for crypto provider type %ld.\n", dwProvType);
321 SetLastError(NTE_PROV_TYPE_NOT_DEF);
324 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
327 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
328 TRACE("No type registered for crypto provider type %ld.\n", dwProvType);
330 SetLastError(NTE_PROV_TYPE_NOT_DEF);
333 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
334 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
337 SetLastError(NTE_PROV_TYPE_NOT_DEF);
342 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
343 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
345 TRACE("error %ld reading size of 'Name' from registry\n", r );
347 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
350 if(!(provname = CRYPT_Alloc(len)))
353 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
356 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
357 if( r != ERROR_SUCCESS )
359 TRACE("error %ld reading 'Name' from registry\n", r );
361 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
366 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
368 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
371 strcpyW(provname, pszProvider);
374 keyname = CRYPT_GetProvKeyName(provname);
375 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
377 if (r != ERROR_SUCCESS)
379 SetLastError(NTE_KEYSET_NOT_DEF);
383 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
384 if (r != ERROR_SUCCESS)
386 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
389 if (type != dwProvType)
391 TRACE("Crypto provider has wrong type (%ld vs expected %ld).\n", type, dwProvType);
392 SetLastError(NTE_PROV_TYPE_NO_MATCH);
396 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
397 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
399 TRACE("error %ld reading size of 'Image Path' from registry\n", r );
401 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
404 if (!(temp = CRYPT_Alloc(len)))
407 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
410 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
411 if( r != ERROR_SUCCESS )
413 TRACE("error %ld reading 'Image Path' from registry\n", r );
415 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
419 len = ExpandEnvironmentStringsW(temp, NULL, 0);
420 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
422 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
425 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
427 /* ExpandEnvironmentStrings will call SetLastError */
430 pProv = CRYPT_LoadProvider(imagepath);
432 /* CRYPT_LoadProvider calls SetLastError */
435 pProv->pVTable->dwProvType = dwProvType;
436 if(!CRYPT_UnicodeToANSI(provname, &provnameA, -1))
438 /* CRYPT_UnicodeToANSI calls SetLastError */
441 pProv->pVTable->pszProvName = provnameA;
442 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, -1))
444 /* CRYPT_UnicodeToANSI calls SetLastError */
447 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
449 /* MSDN: When this flag is set, the value returned in phProv is undefined,
450 * and thus, the CryptReleaseContext function need not be called afterwards.
451 * Therefore, we must clean up everything now.
453 if (dwFlags & CRYPT_DELETEKEYSET)
456 FreeLibrary(pProv->hModule);
457 CRYPT_Free(provnameA);
458 CRYPT_Free(pProv->pVTable);
459 CRYPT_Free(pProv->pFuncs);
462 *phProv = (HCRYPTPROV)pProv;
464 CRYPT_Free(pszContainerA);
465 CRYPT_Free(provname);
467 CRYPT_Free(imagepath);
470 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
476 FreeLibrary(pProv->hModule);
478 CRYPT_Free(pProv->pVTable);
480 CRYPT_Free(pProv->pFuncs);
484 CRYPT_Free(pszContainerA);
486 CRYPT_Free(provnameA);
488 CRYPT_Free(provname);
492 CRYPT_Free(imagepath);
496 /******************************************************************************
497 * CryptAcquireContextA (ADVAPI32.@)
499 * See CryptAcquireContextW.
501 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
502 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
504 PWSTR pProvider = NULL, pContainer = NULL;
507 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv, pszContainer,
508 pszProvider, dwProvType, dwFlags);
510 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
511 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
512 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
514 CRYPT_Free(pContainer);
515 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
518 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
521 CRYPT_Free(pContainer);
523 CRYPT_Free(pProvider);
528 /******************************************************************************
529 * CryptContextAddRef (ADVAPI32.@)
531 * Increases reference count of a cryptographic service provider handle
535 * hProv [I] Handle to the CSP whose reference is being incremented.
536 * pdwReserved [IN] Reserved for future use and must be NULL.
537 * dwFlags [I] Reserved for future use and must be NULL.
543 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
545 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
547 TRACE("(0x%lx, %p, %08lx)\n", hProv, pdwReserved, dwFlags);
551 SetLastError(NTE_BAD_UID);
555 if (pProv->dwMagic != MAGIC_CRYPTPROV)
557 SetLastError(ERROR_INVALID_PARAMETER);
565 /******************************************************************************
566 * CryptReleaseContext (ADVAPI32.@)
568 * Releases the handle of a CSP. Reference count is decreased.
571 * hProv [I] Handle of a CSP.
572 * dwFlags [I] Reserved for future use and must be NULL.
578 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, DWORD dwFlags)
580 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
583 TRACE("(0x%lx, %08lx)\n", hProv, dwFlags);
587 SetLastError(NTE_BAD_UID);
591 if (pProv->dwMagic != MAGIC_CRYPTPROV)
593 SetLastError(ERROR_INVALID_PARAMETER);
598 if (pProv->refcount <= 0)
600 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
602 FreeLibrary(pProv->hModule);
604 CRYPT_Free(pProv->pVTable->pContextInfo);
606 CRYPT_Free(pProv->pVTable->pszProvName);
607 CRYPT_Free(pProv->pVTable);
608 CRYPT_Free(pProv->pFuncs);
614 /******************************************************************************
615 * CryptGenRandom (ADVAPI32.@)
617 * Fills a buffer with cryptographically random bytes.
620 * hProv [I] Handle of a CSP.
621 * dwLen [I] Number of bytes to generate.
622 * pbBuffer [I/O] Buffer to contain random bytes.
629 * pdBuffer must be at least dwLen bytes long.
631 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
633 PCRYPTPROV prov = (PCRYPTPROV)hProv;
635 TRACE("(0x%lx, %ld, %p)\n", hProv, dwLen, pbBuffer);
638 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
640 if (prov->dwMagic != MAGIC_CRYPTPROV)
641 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
643 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
646 /******************************************************************************
647 * CryptCreateHash (ADVAPI32.@)
649 * Initiates the hashing of a stream of data.
652 * hProv [I] Handle of a CSP.
653 * Algid [I] Identifies the hash algorithm to use.
654 * hKey [I] Key for the hash (if required).
655 * dwFlags [I] Reserved for future use and must be NULL.
656 * phHash [O] Address of the future handle to the new hash object.
663 * If the algorithm is a keyed hash, hKey is the key.
665 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
666 DWORD dwFlags, HCRYPTHASH *phHash)
668 PCRYPTPROV prov = (PCRYPTPROV)hProv;
669 PCRYPTKEY key = (PCRYPTKEY)hKey;
672 TRACE("(0x%lx, 0x%x, 0x%lx, %08lx, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
675 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
676 if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV)
677 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
679 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
680 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
681 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
683 hash->pProvider = prov;
685 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
686 key ? key->hPrivate : 0, 0, &hash->hPrivate))
688 *phHash = (HCRYPTHASH)hash;
698 /******************************************************************************
699 * CryptDecrypt (ADVAPI32.@)
701 * Decrypts data encrypted by CryptEncrypt.
704 * hKey [I] Handle to the decryption key.
705 * hHash [I] Handle to a hash object.
706 * Final [I] TRUE if this is the last section to be decrypted.
707 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
708 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
710 * pdwDataLen [I/O] Length of pbData before and after the call.
716 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
717 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
720 PCRYPTKEY key = (PCRYPTKEY)hKey;
721 PCRYPTHASH hash = (PCRYPTHASH)hHash;
723 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
725 if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
726 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
728 prov = key->pProvider;
729 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
730 Final, dwFlags, pbData, pdwDataLen);
733 /******************************************************************************
734 * CryptDeriveKey (ADVAPI32.@)
736 * Generates session keys derived from a base data value.
739 * hProv [I] Handle to a CSP.
740 * Algid [I] Identifies the symmetric encryption algorithm to use.
741 * hBaseData [I] Handle to a hash object.
742 * dwFlags [I] Type of key to generate.
743 * phKey [I/O] Address of the newly generated key.
749 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
750 DWORD dwFlags, HCRYPTKEY *phKey)
752 PCRYPTPROV prov = (PCRYPTPROV)hProv;
753 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
756 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08lx, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
759 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
760 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV)
761 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
762 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
763 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
765 key->pProvider = prov;
766 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
768 *phKey = (HCRYPTKEY)key;
778 /******************************************************************************
779 * CryptDestroyHash (ADVAPI32.@)
781 * Destroys the hash object referenced by hHash.
784 * hHash [I] Handle of the hash object to be destroyed.
790 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
792 PCRYPTHASH hash = (PCRYPTHASH)hHash;
796 TRACE("(0x%lx)\n", hHash);
799 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
801 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
802 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
804 prov = hash->pProvider;
805 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
810 /******************************************************************************
811 * CryptDestroyKey (ADVAPI32.@)
813 * Releases the handle referenced by hKey.
816 * hKey [I] Handle of the key to be destroyed.
822 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
824 PCRYPTKEY key = (PCRYPTKEY)hKey;
828 TRACE("(0x%lx)\n", hKey);
831 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
833 if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
834 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
836 prov = key->pProvider;
837 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
842 /******************************************************************************
843 * CryptDuplicateHash (ADVAPI32.@)
848 * hHash [I] Handle to the hash to be copied.
849 * pdwReserved [I] Reserved for future use and must be zero.
850 * dwFlags [I] Reserved for future use and must be zero.
851 * phHash [O] Address of the handle to receive the copy.
857 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
858 DWORD dwFlags, HCRYPTHASH *phHash)
861 PCRYPTHASH orghash, newhash;
863 TRACE("(0x%lx, %p, %08lx, %p)\n", hHash, pdwReserved, dwFlags, phHash);
865 orghash = (PCRYPTHASH)hHash;
866 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
867 orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
869 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
872 prov = orghash->pProvider;
873 if (!prov->pFuncs->pCPDuplicateHash)
874 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
876 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
877 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
879 newhash->pProvider = prov;
880 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
882 *phHash = (HCRYPTHASH)newhash;
889 /******************************************************************************
890 * CryptDuplicateKey (ADVAPI32.@)
892 * Duplicate a key and the key's state.
895 * hKey [I] Handle of the key to copy.
896 * pdwReserved [I] Reserved for future use and must be NULL.
897 * dwFlags [I] Reserved for future use and must be zero.
898 * phKey [I] Address of the handle to the duplicated key.
904 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
907 PCRYPTKEY orgkey, newkey;
909 TRACE("(0x%lx, %p, %08lx, %p)\n", hKey, pdwReserved, dwFlags, phKey);
911 orgkey = (PCRYPTKEY)hKey;
912 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
913 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
915 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
918 prov = orgkey->pProvider;
919 if (!prov->pFuncs->pCPDuplicateKey)
920 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
922 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
923 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
925 newkey->pProvider = prov;
926 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
928 *phKey = (HCRYPTKEY)newkey;
935 /******************************************************************************
936 * CryptEncrypt (ADVAPI32.@)
941 * hKey [I] Handle to the enryption key.
942 * hHash [I] Handle to a hash object.
943 * Final [I] TRUE if this is the last section to encrypt.
944 * dwFlags [I] Can be CRYPT_OAEP.
945 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
946 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
947 * encrypted data after call.
948 * dwBufLen [I] Length of the input pbData buffer.
955 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
956 * required for the returned data in pdwDataLen.
958 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
959 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
962 PCRYPTKEY key = (PCRYPTKEY)hKey;
963 PCRYPTHASH hash = (PCRYPTHASH)hHash;
965 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p, %ld)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
967 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
968 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
970 prov = key->pProvider;
971 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
972 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
975 /******************************************************************************
976 * CryptEnumProvidersW (ADVAPI32.@)
978 * Returns the next availabe CSP.
981 * dwIndex [I] Index of the next provider to be enumerated.
982 * pdwReserved [I] Reserved for future use and must be NULL.
983 * dwFlags [I] Reserved for future use and must be zero.
984 * pdwProvType [O] DWORD designating the type of the provider.
985 * pszProvName [O] Buffer that receives data from the provider.
986 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
987 * of bytes stored in the buffer on return.
994 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
995 * for memory allocation purposes.
997 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
998 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1001 static const WCHAR providerW[] = {
1002 'S','o','f','t','w','a','r','e','\\',
1003 'M','i','c','r','o','s','o','f','t','\\',
1004 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1005 'D','e','f','a','u','l','t','s','\\',
1006 'P','r','o','v','i','d','e','r',0
1008 static const WCHAR typeW[] = {'T','y','p','e',0};
1010 TRACE("(%ld, %p, %ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1011 pdwProvType, pszProvName, pcbProvName);
1013 if (pdwReserved || !pcbProvName) CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1014 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1016 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1017 CRYPT_ReturnLastError(NTE_FAIL);
1024 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1025 NULL, NULL, NULL, NULL, NULL, NULL);
1027 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1028 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1030 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1031 CRYPT_Free(provNameW);
1033 *pcbProvName *= sizeof(WCHAR);
1035 if (dwIndex >= numkeys)
1036 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
1038 DWORD size = sizeof(DWORD);
1042 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1044 CRYPT_ReturnLastError(result);
1045 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1047 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1049 RegCloseKey(subkey);
1055 /******************************************************************************
1056 * CryptEnumProvidersA (ADVAPI32.@)
1058 * See CryptEnumProvidersW.
1060 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1061 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1065 BOOL ret; /* = FALSE; */
1067 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1068 pdwProvType, pszProvName, pcbProvName);
1070 CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &strlen);
1071 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1072 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1073 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1075 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1076 *pcbProvName = strlen / sizeof(WCHAR); /* FIXME: not correct */
1080 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1081 CRYPT_ReturnLastError(ERROR_MORE_DATA);
1086 /******************************************************************************
1087 * CryptEnumProviderTypesW (ADVAPI32.@)
1089 * Retrieves the next type of CSP supported.
1092 * dwIndex [I] Index of the next provider to be enumerated.
1093 * pdwReserved [I] Reserved for future use and must be NULL.
1094 * dwFlags [I] Reserved for future use and must be zero.
1095 * pdwProvType [O] DWORD designating the type of the provider.
1096 * pszTypeName [O] Buffer that receives data from the provider type.
1097 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1098 * of bytes stored in the buffer on return.
1105 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1106 * for memory allocation purposes.
1108 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1109 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1112 DWORD keylen, numkeys, dwType;
1115 static const WCHAR KEYSTR[] = {
1116 'S','o','f','t','w','a','r','e','\\',
1117 'M','i','c','r','o','s','o','f','t','\\',
1118 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1119 'D','e','f','a','u','l','t','s','\\',
1120 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1122 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1124 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex, pdwReserved,
1125 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1127 if (pdwReserved || !pdwProvType || !pcbTypeName)
1128 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1129 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1131 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1134 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1135 if (dwIndex >= numkeys)
1136 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
1138 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1139 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1140 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1141 CRYPT_Free(keyname);
1144 RegOpenKeyW(hKey, keyname, &hSubkey);
1145 ch = keyname + strlenW(keyname);
1146 /* Convert "Type 000" to 0, etc/ */
1147 *pdwProvType = *(--ch) - '0';
1148 *pdwProvType += (*(--ch) - '0') * 10;
1149 *pdwProvType += (*(--ch) - '0') * 100;
1150 CRYPT_Free(keyname);
1152 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1154 CRYPT_ReturnLastError(result);
1156 RegCloseKey(hSubkey);
1161 /******************************************************************************
1162 * CryptEnumProviderTypesA (ADVAPI32.@)
1164 * See CryptEnumProviderTypesW.
1166 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1167 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1173 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1174 pdwProvType, pszTypeName, pcbTypeName);
1176 CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &strlen);
1177 if ( pszTypeName && !(str = CRYPT_Alloc(strlen)) )
1178 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1179 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1181 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1182 *pcbTypeName = strlen / sizeof(WCHAR);
1186 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1187 CRYPT_ReturnLastError(ERROR_MORE_DATA);
1192 /******************************************************************************
1193 * CryptExportKey (ADVAPI32.@)
1195 * Exports a cryptographic key from a CSP.
1198 * hKey [I] Handle to the key to export.
1199 * hExpKey [I] Handle to a cryptographic key of the end user.
1200 * dwBlobType [I] Type of BLOB to be exported.
1201 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1202 * pbData [O] Buffer to receive BLOB data.
1203 * pdwDataLen [I/O] Specifies the size of pbData.
1210 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1211 * buffer needed to hold the BLOB.
1213 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1214 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1217 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1219 TRACE("(0x%lx, 0x%lx, %ld, %08lx, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1221 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1222 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1224 prov = key->pProvider;
1225 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1226 dwBlobType, dwFlags, pbData, pdwDataLen);
1229 /******************************************************************************
1230 * CryptGenKey (ADVAPI32.@)
1232 * Generates a random cryptographic session key or a pub/priv key pair.
1235 * hProv [I] Handle to a CSP.
1236 * Algid [I] Algorithm to use to make key.
1237 * dwFlags [I] Specifies type of key to make.
1238 * phKey [I] Address of the handle to which the new key is copied.
1244 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1246 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1249 TRACE("(0x%lx, %d, %08lx, %p)\n", hProv, Algid, dwFlags, phKey);
1252 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1253 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1254 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1255 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1256 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1258 key->pProvider = prov;
1260 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1262 *phKey = (HCRYPTKEY)key;
1271 /******************************************************************************
1272 * CryptGetDefaultProviderW (ADVAPI32.@)
1274 * Finds the default CSP of a certain provider type.
1277 * dwProvType [I] Provider type to look for.
1278 * pdwReserved [I] Reserved for future use and must be NULL.
1279 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1280 * pszProvName [O] Name of the default CSP.
1281 * pcbProvName [I/O] Size of pszProvName
1288 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1289 * memory allocation purposes on return.
1291 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1292 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1297 static const WCHAR nameW[] = {'N','a','m','e',0};
1299 if (pdwReserved || !pcbProvName)
1300 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1301 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1302 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1303 if (dwProvType > 999)
1304 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1305 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1306 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1307 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1309 CRYPT_Free(keyname);
1310 CRYPT_ReturnLastError(NTE_PROV_TYPE_NOT_DEF);
1312 CRYPT_Free(keyname);
1314 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1317 if (result != ERROR_MORE_DATA)
1318 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1320 SetLastError(result);
1329 /******************************************************************************
1330 * CryptGetDefaultProviderA (ADVAPI32.@)
1332 * See CryptGetDefaultProviderW.
1334 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1335 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1341 TRACE("(%ld, %p, %08lx, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1343 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &strlen);
1344 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1345 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1346 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &strlen);
1348 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1349 *pcbProvName = strlen / sizeof(WCHAR);
1353 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1354 CRYPT_ReturnLastError(ERROR_MORE_DATA);
1359 /******************************************************************************
1360 * CryptGetHashParam (ADVAPI32.@)
1362 * Retrieves data that controls the operations of a hash object.
1365 * hHash [I] Handle of the hash object to question.
1366 * dwParam [I] Query type.
1367 * pbData [O] Buffer that receives the value data.
1368 * pdwDataLen [I/O] Size of the pbData buffer.
1369 * dwFlags [I] Reserved for future use and must be zero.
1376 * If pbData is NULL, pdwDataLen will contain the length required.
1378 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1379 DWORD *pdwDataLen, DWORD dwFlags)
1382 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1384 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1386 if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1387 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1389 prov = hash->pProvider;
1390 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1391 pbData, pdwDataLen, dwFlags);
1394 /******************************************************************************
1395 * CryptGetKeyParam (ADVAPI32.@)
1397 * Retrieves data that controls the operations of a key.
1400 * hKey [I] Handle to they key in question.
1401 * dwParam [I] Specifies query type.
1402 * pbData [O] Sequence of bytes to receive data.
1403 * pdwDataLen [I/O] Size of pbData.
1404 * dwFlags [I] Reserved for future use and must be zero.
1411 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1413 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1414 DWORD *pdwDataLen, DWORD dwFlags)
1417 PCRYPTKEY key = (PCRYPTKEY)hKey;
1419 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1421 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1422 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1424 prov = key->pProvider;
1425 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1426 pbData, pdwDataLen, dwFlags);
1429 /******************************************************************************
1430 * CryptGetProvParam (ADVAPI32.@)
1432 * Retrieves parameters that control the operations of a CSP.
1435 * hProv [I] Handle of the CSP in question.
1436 * dwParam [I] Specifies query type.
1437 * pbData [O] Buffer to receive the data.
1438 * pdwDataLen [I/O] Size of pbData.
1439 * dwFlags [I] see MSDN Docs.
1446 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1448 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1449 DWORD *pdwDataLen, DWORD dwFlags)
1451 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1453 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1455 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1456 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1458 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1461 /******************************************************************************
1462 * CryptGetUserKey (ADVAPI32.@)
1464 * Gets a handle of one of a user's two public/private key pairs.
1467 * hProv [I] Handle of a CSP.
1468 * dwKeySpec [I] Private key to use.
1469 * phUserKey [O] Pointer to the handle of the retrieved keys.
1475 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1477 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1480 TRACE("(0x%lx, %ld, %p)\n", hProv, dwKeySpec, phUserKey);
1483 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1484 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1485 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1486 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1487 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1489 key->pProvider = prov;
1491 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1493 *phUserKey = (HCRYPTKEY)key;
1503 /******************************************************************************
1504 * CryptHashData (ADVAPI32.@)
1506 * Adds data to a hash object.
1509 * hHash [I] Handle of the hash object.
1510 * pbData [I] Buffer of data to be hashed.
1511 * dwDataLen [I] Number of bytes to add.
1512 * dwFlags [I] Can be CRYPT_USERDATA
1518 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1520 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1523 TRACE("(0x%lx, %p, %ld, %08lx)\n", hHash, pbData, dwDataLen, dwFlags);
1526 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1527 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1528 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1530 prov = hash->pProvider;
1531 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1534 /******************************************************************************
1535 * CryptHashSessionKey (ADVAPI32.@)
1537 * Compute the cryptographic hash of a session key object.
1540 * hHash [I] Handle to the hash object.
1541 * hKey [I] Handle to the key to be hashed.
1542 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1548 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1550 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1551 PCRYPTKEY key = (PCRYPTKEY)hKey;
1554 TRACE("(0x%lx, 0x%lx, %08lx)\n", hHash, hKey, dwFlags);
1557 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1559 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1560 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1562 prov = hash->pProvider;
1563 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1566 /******************************************************************************
1567 * CryptImportKey (ADVAPI32.@)
1569 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1572 * hProv [I] Handle of a CSP.
1573 * pbData [I] Contains the key to be imported.
1574 * dwDataLen [I] Length of the key.
1575 * hPubKey [I] Cryptographic key that decrypts pdData
1576 * dwFlags [I] Used only with a public/private key pair.
1577 * phKey [O] Imported key.
1583 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, BYTE *pbData, DWORD dwDataLen,
1584 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1586 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1587 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1589 TRACE("(0x%lx, %p, %ld, 0x%lx, %08lx, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1591 if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV)
1592 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1594 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1595 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1597 importkey->pProvider = prov;
1598 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1599 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1601 *phKey = (HCRYPTKEY)importkey;
1605 CRYPT_Free(importkey);
1609 /******************************************************************************
1610 * CryptSignHashW (ADVAPI32.@)
1615 * hHash [I] Handle of the hash object to be signed.
1616 * dwKeySpec [I] Private key to use.
1617 * sDescription [I] Should be NULL.
1618 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1619 * pbSignature [O] Buffer of the signature data.
1620 * pdwSigLen [I/O] Size of the pbSignature buffer.
1627 * Because of security flaws sDescription should not be used and should thus be
1628 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1631 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1632 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1634 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1637 TRACE("(0x%lx, %ld, %s, %08lx, %p, %p)\n",
1638 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1641 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1642 if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1643 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1645 prov = hash->pProvider;
1646 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1647 dwFlags, pbSignature, pdwSigLen);
1650 /******************************************************************************
1651 * CryptSignHashA (ADVAPI32.@)
1653 * See CryptSignHashW.
1655 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1656 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1658 LPWSTR wsDescription;
1661 TRACE("(0x%lx, %ld, %s, %08lx, %p, %p)\n",
1662 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1664 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1665 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1666 if (wsDescription) CRYPT_Free(wsDescription);
1671 /******************************************************************************
1672 * CryptSetHashParam (ADVAPI32.@)
1674 * Customizes the operations of a hash object.
1677 * hHash [I] Handle of the hash object to set parameters.
1678 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1679 * pbData [I] Value data buffer.
1680 * dwFlags [I] Reserved for future use and must be zero.
1686 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1689 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1691 TRACE("(0x%lx, %ld, %p, %08lx)\n", hHash, dwParam, pbData, dwFlags);
1693 if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1694 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1696 prov = hash->pProvider;
1697 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1698 dwParam, pbData, dwFlags);
1701 /******************************************************************************
1702 * CryptSetKeyParam (ADVAPI32.@)
1704 * Customizes a session key's operations.
1707 * hKey [I] Handle to the key to set values.
1708 * dwParam [I] See MSDN Doc.
1709 * pbData [I] Buffer of values to set.
1710 * dwFlags [I] Only used when dwParam == KP_ALGID.
1716 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1719 PCRYPTKEY key = (PCRYPTKEY)hKey;
1721 TRACE("(0x%lx, %ld, %p, %08lx)\n", hKey, dwParam, pbData, dwFlags);
1723 if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1724 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1726 prov = key->pProvider;
1727 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1728 dwParam, pbData, dwFlags);
1731 /******************************************************************************
1732 * CryptSetProviderA (ADVAPI32.@)
1734 * Specifies the current user's default CSP.
1737 * pszProvName [I] Name of the new default CSP.
1738 * dwProvType [I] Provider type of the CSP.
1744 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
1746 TRACE("(%s, %ld)\n", pszProvName, dwProvType);
1747 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1750 /******************************************************************************
1751 * CryptSetProviderW (ADVAPI32.@)
1753 * See CryptSetProviderA.
1755 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
1757 TRACE("(%s, %ld)\n", debugstr_w(pszProvName), dwProvType);
1758 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1761 /******************************************************************************
1762 * CryptSetProviderExW (ADVAPI32.@)
1764 * Specifies the default CSP.
1767 * pszProvName [I] Name of the new default CSP.
1768 * dwProvType [I] Provider type of the CSP.
1769 * pdwReserved [I] Reserved for future use and must be NULL.
1770 * dwFlags [I] See MSDN Doc.
1776 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1778 HKEY hProvKey, hTypeKey;
1780 static const WCHAR nameW[] = {'N','a','m','e',0};
1782 TRACE("(%s, %ld, %p, %08lx)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
1784 if (!pszProvName || pdwReserved)
1785 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1786 if (dwProvType > MAXPROVTYPES)
1787 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1788 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
1789 || dwFlags == CRYPT_DELETE_DEFAULT)
1790 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1792 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
1793 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1794 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1795 keyname, &hTypeKey))
1797 CRYPT_Free(keyname);
1798 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1800 CRYPT_Free(keyname);
1802 if (dwFlags & CRYPT_DELETE_DEFAULT)
1804 RegDeleteValueW(hTypeKey, nameW);
1808 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
1810 RegCloseKey(hTypeKey);
1811 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1813 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1814 keyname, &hProvKey))
1816 CRYPT_Free(keyname);
1817 RegCloseKey(hTypeKey);
1818 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1820 CRYPT_Free(keyname);
1822 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (LPBYTE)pszProvName, (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
1824 RegCloseKey(hTypeKey);
1825 RegCloseKey(hProvKey);
1829 RegCloseKey(hProvKey);
1831 RegCloseKey(hTypeKey);
1836 /******************************************************************************
1837 * CryptSetProviderExA (ADVAPI32.@)
1839 * See CryptSetProviderExW.
1841 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1846 TRACE("(%s, %ld, %p, %08lx)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
1848 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
1850 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
1856 /******************************************************************************
1857 * CryptSetProvParam (ADVAPI32.@)
1859 * Customizes the operations of a CSP.
1862 * hProv [I] Handle of a CSP.
1863 * dwParam [I] See MSDN Doc.
1864 * pbData [I] Buffer that contains a value to set as a parameter.
1865 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
1871 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1873 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1875 TRACE("(0x%lx, %ld, %p, %08lx)\n", hProv, dwParam, pbData, dwFlags);
1878 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1879 if (prov->dwMagic != MAGIC_CRYPTPROV)
1880 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1881 if (dwFlags & PP_USE_HARDWARE_RNG)
1883 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
1884 FIXME("\tLetting the CSP decide.\n");
1886 if (dwFlags & PP_CLIENT_HWND)
1888 /* FIXME: Should verify the parameter */
1889 if (pbData /* && IsWindow((HWND)pbData) */)
1891 crypt_hWindow = (HWND)(pbData);
1894 SetLastError(ERROR_INVALID_PARAMETER);
1898 /* All other flags go to the CSP */
1899 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
1902 /******************************************************************************
1903 * CryptVerifySignatureW (ADVAPI32.@)
1905 * Verifies the signature of a hash object.
1908 * hHash [I] Handle of the hash object to verify.
1909 * pbSignature [I] Signature data to verify.
1910 * dwSigLen [I] Size of pbSignature.
1911 * hPubKey [I] Handle to the public key to authenticate signature.
1912 * sDescription [I] Should be NULL.
1913 * dwFlags [I] See MSDN doc.
1920 * Because of security flaws sDescription should not be used and should thus be
1921 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1924 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
1925 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
1927 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1928 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
1931 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08lx)\n", hHash, pbSignature,
1932 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
1935 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1936 if (!pbSignature || !dwSigLen ||
1937 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
1938 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1940 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1943 prov = hash->pProvider;
1944 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
1945 key->hPrivate, sDescription, dwFlags);
1948 /******************************************************************************
1949 * CryptVerifySignatureA (ADVAPI32.@)
1951 * See CryptVerifySignatureW.
1953 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
1954 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
1956 LPWSTR wsDescription;
1959 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08lx)\n", hHash, pbSignature,
1960 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
1962 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1963 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
1964 if (wsDescription) CRYPT_Free(wsDescription);
1969 /******************************************************************************
1970 * SystemFunction036 (ADVAPI32.@)
1972 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
1975 * pbBufer [O] Pointer to memory to receive random bytes.
1976 * dwLen [I] Number of random bytes to fetch.
1983 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
1987 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
1988 * quality for cryptographic applications. /dev/random is much better,
1989 * but it blocks if the kernel has not yet collected enough entropy for
1990 * the request, which will suspend the calling thread for an indefinite
1991 * amount of time. */
1992 dev_random = open("/dev/urandom", O_RDONLY);
1993 if (dev_random != -1)
1995 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2002 SetLastError(NTE_FAIL);
2007 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2011 /******************************************************************************
2012 * SystemFunction040 (ADVAPI32.@)
2014 * MSDN documents this function as RtlEncryptMemory.
2017 * memory [I/O] Pointer to memory to encrypt.
2018 * length [I] Length of region to encrypt in bytes.
2019 * flags [I] Control whether other processes are able to decrypt the memory.
2020 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2021 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2022 * RTL_ENCRYPT_OPTION_SAME_LOGON
2025 * Success: STATUS_SUCCESS
2026 * Failure: NTSTATUS error code
2029 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2030 * If flags are specified when encrypting, the same flag value must be given
2031 * when decrypting the memory.
2033 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2035 FIXME("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory, length, flags);
2036 return STATUS_SUCCESS;
2039 /******************************************************************************
2040 * SystemFunction041 (ADVAPI32.@)
2042 * MSDN documents this function as RtlDecryptMemory.
2045 * memory [I/O] Pointer to memory to decrypt.
2046 * length [I] Length of region to decrypt in bytes.
2047 * flags [I] Control whether other processes are able to decrypt the memory.
2048 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2049 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2050 * RTL_ENCRYPT_OPTION_SAME_LOGON
2053 * Success: STATUS_SUCCESS
2054 * Failure: NTSTATUS error code
2057 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2058 * If flags are specified when encrypting, the same flag value must be given
2059 * when decrypting the memory.
2061 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2063 FIXME("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory, length, flags);
2064 return STATUS_SUCCESS;