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 /***********************************************************************
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 static HWND crypt_hWindow;
55 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
56 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))
58 static inline PWSTR CRYPT_GetProvKeyName(PCWSTR pProvName)
60 static const WCHAR KEYSTR[] = {
61 'S','o','f','t','w','a','r','e','\\',
62 'M','i','c','r','o','s','o','f','t','\\',
63 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
64 'D','e','f','a','u','l','t','s','\\',
65 'P','r','o','v','i','d','e','r','\\',0
69 keyname = CRYPT_Alloc((strlenW(KEYSTR) + strlenW(pProvName) +1)*sizeof(WCHAR));
72 strcpyW(keyname, KEYSTR);
73 strcpyW(keyname + strlenW(KEYSTR), pProvName);
75 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
79 static inline PWSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
81 static const WCHAR MACHINESTR[] = {
82 'S','o','f','t','w','a','r','e','\\',
83 'M','i','c','r','o','s','o','f','t','\\',
84 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
85 'D','e','f','a','u','l','t','s','\\',
86 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
87 'T','y','p','e',' ','X','X','X',0
89 static const WCHAR USERSTR[] = {
90 'S','o','f','t','w','a','r','e','\\',
91 'M','i','c','r','o','s','o','f','t','\\',
92 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
93 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
98 keyname = CRYPT_Alloc( ((user ? strlenW(USERSTR) : strlenW(MACHINESTR)) +1)*sizeof(WCHAR));
101 user ? strcpyW(keyname, USERSTR) : strcpyW(keyname, MACHINESTR);
102 ptr = keyname + strlenW(keyname);
103 *(--ptr) = (dwType % 10) + '0';
104 *(--ptr) = ((dwType / 10) % 10) + '0';
105 *(--ptr) = (dwType / 100) + '0';
107 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
111 /* CRYPT_UnicodeTOANSI
112 * wstr - unicode string
113 * str - pointer to ANSI string
114 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
116 * returns TRUE if unsuccessful, FALSE otherwise.
117 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
119 static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
128 count = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
130 *str = CRYPT_Alloc(count * sizeof(CHAR));
132 count = min( count, strsize );
135 WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, count, NULL, NULL);
138 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
142 /* CRYPT_ANSITOUnicode
144 * wstr - pointer to unicode string
145 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
147 static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
156 wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
158 *wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
160 wcount = min( wcount, wstrsize/sizeof(WCHAR) );
163 MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
166 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
170 /* These next 2 functions are used by the VTableProvStruc structure */
171 static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
173 if (!lpszImage || !pData)
175 SetLastError(ERROR_INVALID_PARAMETER);
179 FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
184 static BOOL CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
188 *phWnd = crypt_hWindow;
192 #define CRYPT_GetProvFunc(name) \
193 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
194 #define CRYPT_GetProvFuncOpt(name) \
195 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
196 static PCRYPTPROV CRYPT_LoadProvider(PWSTR pImage)
199 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
201 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
202 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
203 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
204 if ( !(provider->hModule = LoadLibraryW(pImage)) )
206 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
207 FIXME("Failed to load dll %s\n", debugstr_w(pImage));
210 provider->dwMagic = MAGIC_CRYPTPROV;
211 provider->refcount = 1;
213 errorcode = NTE_PROVIDER_DLL_FAIL;
214 CRYPT_GetProvFunc(CPAcquireContext);
215 CRYPT_GetProvFunc(CPCreateHash);
216 CRYPT_GetProvFunc(CPDecrypt);
217 CRYPT_GetProvFunc(CPDeriveKey);
218 CRYPT_GetProvFunc(CPDestroyHash);
219 CRYPT_GetProvFunc(CPDestroyKey);
220 CRYPT_GetProvFuncOpt(CPDuplicateHash);
221 CRYPT_GetProvFuncOpt(CPDuplicateKey);
222 CRYPT_GetProvFunc(CPEncrypt);
223 CRYPT_GetProvFunc(CPExportKey);
224 CRYPT_GetProvFunc(CPGenKey);
225 CRYPT_GetProvFunc(CPGenRandom);
226 CRYPT_GetProvFunc(CPGetHashParam);
227 CRYPT_GetProvFunc(CPGetKeyParam);
228 CRYPT_GetProvFunc(CPGetProvParam);
229 CRYPT_GetProvFunc(CPGetUserKey);
230 CRYPT_GetProvFunc(CPHashData);
231 CRYPT_GetProvFunc(CPHashSessionKey);
232 CRYPT_GetProvFunc(CPImportKey);
233 CRYPT_GetProvFunc(CPReleaseContext);
234 CRYPT_GetProvFunc(CPSetHashParam);
235 CRYPT_GetProvFunc(CPSetKeyParam);
236 CRYPT_GetProvFunc(CPSetProvParam);
237 CRYPT_GetProvFunc(CPSignHash);
238 CRYPT_GetProvFunc(CPVerifySignature);
240 /* FIXME: Not sure what the pbContextInfo field is for.
241 * Does it need memory allocation?
243 provider->pVTable->Version = 3;
244 provider->pVTable->pFuncVerifyImage = (FARPROC)CRYPT_VerifyImage;
245 provider->pVTable->pFuncReturnhWnd = (FARPROC)CRYPT_ReturnhWnd;
246 provider->pVTable->dwProvType = 0;
247 provider->pVTable->pbContextInfo = NULL;
248 provider->pVTable->cbContextInfo = 0;
249 provider->pVTable->pszProvName = NULL;
253 SetLastError(errorcode);
256 provider->dwMagic = 0;
257 if (provider->hModule)
258 FreeLibrary(provider->hModule);
259 CRYPT_Free(provider->pVTable);
260 CRYPT_Free(provider->pFuncs);
261 CRYPT_Free(provider);
265 #undef CRYPT_GetProvFunc
266 #undef CRYPT_GetProvFuncOpt
269 /******************************************************************************
270 * CryptAcquireContextW (ADVAPI32.@)
272 * Acquire a crypto provider context handle.
275 * phProv [O] Pointer to HCRYPTPROV for the output.
276 * pszContainer [I] Key Container Name
277 * pszProvider [I] Cryptographic Service Provider Name
278 * dwProvType [I] Crypto provider type to get a handle.
279 * dwFlags [I] flags for the operation
282 * TRUE on success, FALSE on failure.
284 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
285 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
287 PCRYPTPROV pProv = NULL;
289 PWSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
290 PSTR provnameA = NULL, pszContainerA = NULL;
291 DWORD keytype, type, len;
293 static const WCHAR nameW[] = {'N','a','m','e',0};
294 static const WCHAR typeW[] = {'T','y','p','e',0};
295 static const WCHAR imagepathW[] = {'I','m','a','g','e',' ','P','a','t','h',0};
297 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, debugstr_w(pszContainer),
298 debugstr_w(pszProvider), dwProvType, dwFlags);
300 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
302 SetLastError(NTE_BAD_PROV_TYPE);
308 SetLastError(ERROR_INVALID_PARAMETER);
312 if (!pszProvider || !*pszProvider)
314 /* No CSP name specified so try the user default CSP first
315 * then try the machine default CSP
317 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
318 TRACE("No provider registered for crypto provider type %d.\n", dwProvType);
319 SetLastError(NTE_PROV_TYPE_NOT_DEF);
322 if (RegOpenKeyW(HKEY_CURRENT_USER, keyname, &key))
325 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
326 TRACE("No type registered for crypto provider type %d.\n", dwProvType);
328 SetLastError(NTE_PROV_TYPE_NOT_DEF);
331 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key)) {
332 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname));
335 SetLastError(NTE_PROV_TYPE_NOT_DEF);
340 r = RegQueryValueExW(key, nameW, NULL, &keytype, NULL, &len);
341 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
343 TRACE("error %d reading size of 'Name' from registry\n", r );
345 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
348 if(!(provname = CRYPT_Alloc(len)))
351 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
354 r = RegQueryValueExW(key, nameW, NULL, NULL, (LPBYTE)provname, &len);
355 if( r != ERROR_SUCCESS )
357 TRACE("error %d reading 'Name' from registry\n", r );
359 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
364 if ( !(provname = CRYPT_Alloc((strlenW(pszProvider) +1)*sizeof(WCHAR))) )
366 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
369 strcpyW(provname, pszProvider);
372 keyname = CRYPT_GetProvKeyName(provname);
373 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyname, &key);
375 if (r != ERROR_SUCCESS)
377 SetLastError(NTE_KEYSET_NOT_DEF);
381 r = RegQueryValueExW(key, typeW, NULL, NULL, (BYTE*)&type, &len);
382 if (r != ERROR_SUCCESS)
384 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
387 if (type != dwProvType)
389 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type, dwProvType);
390 SetLastError(NTE_PROV_TYPE_NO_MATCH);
394 r = RegQueryValueExW(key, imagepathW, NULL, &keytype, NULL, &len);
395 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
397 TRACE("error %d reading size of 'Image Path' from registry\n", r );
399 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
402 if (!(temp = CRYPT_Alloc(len)))
405 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
408 r = RegQueryValueExW(key, imagepathW, NULL, NULL, (LPBYTE)temp, &len);
409 if( r != ERROR_SUCCESS )
411 TRACE("error %d reading 'Image Path' from registry\n", r );
413 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
417 len = ExpandEnvironmentStringsW(temp, NULL, 0);
418 if ( !(imagepath = CRYPT_Alloc(len*sizeof(WCHAR))) )
420 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
423 if (!ExpandEnvironmentStringsW(temp, imagepath, len))
425 /* ExpandEnvironmentStrings will call SetLastError */
428 pProv = CRYPT_LoadProvider(imagepath);
430 /* CRYPT_LoadProvider calls SetLastError */
433 pProv->pVTable->dwProvType = dwProvType;
434 if(!CRYPT_UnicodeToANSI(provname, &provnameA, -1))
436 /* CRYPT_UnicodeToANSI calls SetLastError */
439 pProv->pVTable->pszProvName = provnameA;
440 if(!CRYPT_UnicodeToANSI(pszContainer, &pszContainerA, -1))
442 /* CRYPT_UnicodeToANSI calls SetLastError */
445 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, pszContainerA, dwFlags, pProv->pVTable))
447 /* MSDN: When this flag is set, the value returned in phProv is undefined,
448 * and thus, the CryptReleaseContext function need not be called afterwards.
449 * Therefore, we must clean up everything now.
451 if (dwFlags & CRYPT_DELETEKEYSET)
454 FreeLibrary(pProv->hModule);
455 CRYPT_Free(provnameA);
456 CRYPT_Free(pProv->pVTable);
457 CRYPT_Free(pProv->pFuncs);
460 *phProv = (HCRYPTPROV)pProv;
462 CRYPT_Free(pszContainerA);
463 CRYPT_Free(provname);
465 CRYPT_Free(imagepath);
468 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
474 FreeLibrary(pProv->hModule);
475 CRYPT_Free(pProv->pVTable);
476 CRYPT_Free(pProv->pFuncs);
479 CRYPT_Free(pszContainerA);
480 CRYPT_Free(provnameA);
481 CRYPT_Free(provname);
483 CRYPT_Free(imagepath);
487 /******************************************************************************
488 * CryptAcquireContextA (ADVAPI32.@)
490 * See CryptAcquireContextW.
492 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
493 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
495 PWSTR pProvider = NULL, pContainer = NULL;
498 TRACE("(%p, %s, %s, %d, %08x)\n", phProv, pszContainer,
499 pszProvider, dwProvType, dwFlags);
501 if ( !CRYPT_ANSIToUnicode(pszContainer, &pContainer, -1) )
503 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
506 if ( !CRYPT_ANSIToUnicode(pszProvider, &pProvider, -1) )
508 CRYPT_Free(pContainer);
509 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
513 ret = CryptAcquireContextW(phProv, pContainer, pProvider, dwProvType, dwFlags);
515 CRYPT_Free(pContainer);
516 CRYPT_Free(pProvider);
521 /******************************************************************************
522 * CryptContextAddRef (ADVAPI32.@)
524 * Increases reference count of a cryptographic service provider handle
528 * hProv [I] Handle to the CSP whose reference is being incremented.
529 * pdwReserved [IN] Reserved for future use and must be NULL.
530 * dwFlags [I] Reserved for future use and must be NULL.
536 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
538 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
540 TRACE("(0x%lx, %p, %08x)\n", hProv, pdwReserved, dwFlags);
544 SetLastError(NTE_BAD_UID);
548 if (pProv->dwMagic != MAGIC_CRYPTPROV)
550 SetLastError(ERROR_INVALID_PARAMETER);
558 /******************************************************************************
559 * CryptReleaseContext (ADVAPI32.@)
561 * Releases the handle of a CSP. Reference count is decreased.
564 * hProv [I] Handle of a CSP.
565 * dwFlags [I] Reserved for future use and must be NULL.
571 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, DWORD dwFlags)
573 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
576 TRACE("(0x%lx, %08x)\n", hProv, dwFlags);
580 SetLastError(NTE_BAD_UID);
584 if (pProv->dwMagic != MAGIC_CRYPTPROV)
586 SetLastError(ERROR_INVALID_PARAMETER);
591 if (pProv->refcount <= 0)
593 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
595 FreeLibrary(pProv->hModule);
597 CRYPT_Free(pProv->pVTable->pContextInfo);
599 CRYPT_Free(pProv->pVTable->pszProvName);
600 CRYPT_Free(pProv->pVTable);
601 CRYPT_Free(pProv->pFuncs);
607 /******************************************************************************
608 * CryptGenRandom (ADVAPI32.@)
610 * Fills a buffer with cryptographically random bytes.
613 * hProv [I] Handle of a CSP.
614 * dwLen [I] Number of bytes to generate.
615 * pbBuffer [I/O] Buffer to contain random bytes.
622 * pdBuffer must be at least dwLen bytes long.
624 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
626 PCRYPTPROV prov = (PCRYPTPROV)hProv;
628 TRACE("(0x%lx, %d, %p)\n", hProv, dwLen, pbBuffer);
632 SetLastError(ERROR_INVALID_HANDLE);
636 if (prov->dwMagic != MAGIC_CRYPTPROV)
638 SetLastError(ERROR_INVALID_PARAMETER);
642 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
645 /******************************************************************************
646 * CryptCreateHash (ADVAPI32.@)
648 * Initiates the hashing of a stream of data.
651 * hProv [I] Handle of a CSP.
652 * Algid [I] Identifies the hash algorithm to use.
653 * hKey [I] Key for the hash (if required).
654 * dwFlags [I] Reserved for future use and must be NULL.
655 * phHash [O] Address of the future handle to the new hash object.
662 * If the algorithm is a keyed hash, hKey is the key.
664 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
665 DWORD dwFlags, HCRYPTHASH *phHash)
667 PCRYPTPROV prov = (PCRYPTPROV)hProv;
668 PCRYPTKEY key = (PCRYPTKEY)hKey;
671 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
675 SetLastError(ERROR_INVALID_HANDLE);
678 if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV)
680 SetLastError(ERROR_INVALID_PARAMETER);
685 SetLastError(NTE_BAD_FLAGS);
688 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
690 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
694 hash->pProvider = prov;
696 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
697 key ? key->hPrivate : 0, 0, &hash->hPrivate))
699 *phHash = (HCRYPTHASH)hash;
709 /******************************************************************************
710 * CryptDecrypt (ADVAPI32.@)
712 * Decrypts data encrypted by CryptEncrypt.
715 * hKey [I] Handle to the decryption key.
716 * hHash [I] Handle to a hash object.
717 * Final [I] TRUE if this is the last section to be decrypted.
718 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
719 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
721 * pdwDataLen [I/O] Length of pbData before and after the call.
727 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
728 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
731 PCRYPTKEY key = (PCRYPTKEY)hKey;
732 PCRYPTHASH hash = (PCRYPTHASH)hHash;
734 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
736 if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
738 SetLastError(ERROR_INVALID_PARAMETER);
742 prov = key->pProvider;
743 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
744 Final, dwFlags, pbData, pdwDataLen);
747 /******************************************************************************
748 * CryptDeriveKey (ADVAPI32.@)
750 * Generates session keys derived from a base data value.
753 * hProv [I] Handle to a CSP.
754 * Algid [I] Identifies the symmetric encryption algorithm to use.
755 * hBaseData [I] Handle to a hash object.
756 * dwFlags [I] Type of key to generate.
757 * phKey [I/O] Address of the newly generated key.
763 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
764 DWORD dwFlags, HCRYPTKEY *phKey)
766 PCRYPTPROV prov = (PCRYPTPROV)hProv;
767 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
770 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
774 SetLastError(ERROR_INVALID_HANDLE);
777 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV)
779 SetLastError(ERROR_INVALID_PARAMETER);
782 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
784 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
788 key->pProvider = prov;
789 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
791 *phKey = (HCRYPTKEY)key;
801 /******************************************************************************
802 * CryptDestroyHash (ADVAPI32.@)
804 * Destroys the hash object referenced by hHash.
807 * hHash [I] Handle of the hash object to be destroyed.
813 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
815 PCRYPTHASH hash = (PCRYPTHASH)hHash;
819 TRACE("(0x%lx)\n", hHash);
823 SetLastError(ERROR_INVALID_HANDLE);
827 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
829 SetLastError(ERROR_INVALID_PARAMETER);
833 prov = hash->pProvider;
834 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
839 /******************************************************************************
840 * CryptDestroyKey (ADVAPI32.@)
842 * Releases the handle referenced by hKey.
845 * hKey [I] Handle of the key to be destroyed.
851 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
853 PCRYPTKEY key = (PCRYPTKEY)hKey;
857 TRACE("(0x%lx)\n", hKey);
861 SetLastError(ERROR_INVALID_HANDLE);
865 if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
867 SetLastError(ERROR_INVALID_PARAMETER);
871 prov = key->pProvider;
872 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
877 /******************************************************************************
878 * CryptDuplicateHash (ADVAPI32.@)
883 * hHash [I] Handle to the hash to be copied.
884 * pdwReserved [I] Reserved for future use and must be zero.
885 * dwFlags [I] Reserved for future use and must be zero.
886 * phHash [O] Address of the handle to receive the copy.
892 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
893 DWORD dwFlags, HCRYPTHASH *phHash)
896 PCRYPTHASH orghash, newhash;
898 TRACE("(0x%lx, %p, %08x, %p)\n", hHash, pdwReserved, dwFlags, phHash);
900 orghash = (PCRYPTHASH)hHash;
901 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
902 orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
904 SetLastError(ERROR_INVALID_PARAMETER);
908 prov = orghash->pProvider;
909 if (!prov->pFuncs->pCPDuplicateHash)
911 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
915 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
917 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
921 newhash->pProvider = prov;
922 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
924 *phHash = (HCRYPTHASH)newhash;
931 /******************************************************************************
932 * CryptDuplicateKey (ADVAPI32.@)
934 * Duplicate a key and the key's state.
937 * hKey [I] Handle of the key to copy.
938 * pdwReserved [I] Reserved for future use and must be NULL.
939 * dwFlags [I] Reserved for future use and must be zero.
940 * phKey [I] Address of the handle to the duplicated key.
946 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
949 PCRYPTKEY orgkey, newkey;
951 TRACE("(0x%lx, %p, %08x, %p)\n", hKey, pdwReserved, dwFlags, phKey);
953 orgkey = (PCRYPTKEY)hKey;
954 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
955 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
957 SetLastError(ERROR_INVALID_PARAMETER);
961 prov = orgkey->pProvider;
962 if (!prov->pFuncs->pCPDuplicateKey)
964 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
968 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
970 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
974 newkey->pProvider = prov;
975 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
977 *phKey = (HCRYPTKEY)newkey;
984 /******************************************************************************
985 * CryptEncrypt (ADVAPI32.@)
990 * hKey [I] Handle to the enryption key.
991 * hHash [I] Handle to a hash object.
992 * Final [I] TRUE if this is the last section to encrypt.
993 * dwFlags [I] Can be CRYPT_OAEP.
994 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
995 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
996 * encrypted data after call.
997 * dwBufLen [I] Length of the input pbData buffer.
1004 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1005 * required for the returned data in pdwDataLen.
1007 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1008 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1011 PCRYPTKEY key = (PCRYPTKEY)hKey;
1012 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1014 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1016 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1018 SetLastError(ERROR_INVALID_PARAMETER);
1022 prov = key->pProvider;
1023 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
1024 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
1027 /******************************************************************************
1028 * CryptEnumProvidersW (ADVAPI32.@)
1030 * Returns the next availabe CSP.
1033 * dwIndex [I] Index of the next provider to be enumerated.
1034 * pdwReserved [I] Reserved for future use and must be NULL.
1035 * dwFlags [I] Reserved for future use and must be zero.
1036 * pdwProvType [O] DWORD designating the type of the provider.
1037 * pszProvName [O] Buffer that receives data from the provider.
1038 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1039 * of bytes stored in the buffer on return.
1046 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1047 * for memory allocation purposes.
1049 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
1050 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
1053 static const WCHAR providerW[] = {
1054 'S','o','f','t','w','a','r','e','\\',
1055 'M','i','c','r','o','s','o','f','t','\\',
1056 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1057 'D','e','f','a','u','l','t','s','\\',
1058 'P','r','o','v','i','d','e','r',0
1060 static const WCHAR typeW[] = {'T','y','p','e',0};
1062 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1063 pdwProvType, pszProvName, pcbProvName);
1065 if (pdwReserved || !pcbProvName)
1067 SetLastError(ERROR_INVALID_PARAMETER);
1072 SetLastError(NTE_BAD_FLAGS);
1076 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
1078 SetLastError(NTE_FAIL);
1087 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1088 NULL, NULL, NULL, NULL, NULL, NULL);
1090 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1092 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1096 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1097 CRYPT_Free(provNameW);
1099 *pcbProvName *= sizeof(WCHAR);
1101 if (dwIndex >= numkeys)
1103 SetLastError(ERROR_NO_MORE_ITEMS);
1107 DWORD size = sizeof(DWORD);
1111 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1114 SetLastError(result);
1117 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1119 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1121 RegCloseKey(subkey);
1127 /******************************************************************************
1128 * CryptEnumProvidersA (ADVAPI32.@)
1130 * See CryptEnumProvidersW.
1132 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1133 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1137 BOOL ret; /* = FALSE; */
1139 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1140 pdwProvType, pszProvName, pcbProvName);
1142 CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &strlen);
1143 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1145 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1148 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1150 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1151 *pcbProvName = strlen / sizeof(WCHAR); /* FIXME: not correct */
1155 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1157 SetLastError(ERROR_MORE_DATA);
1164 /******************************************************************************
1165 * CryptEnumProviderTypesW (ADVAPI32.@)
1167 * Retrieves the next type of CSP supported.
1170 * dwIndex [I] Index of the next provider to be enumerated.
1171 * pdwReserved [I] Reserved for future use and must be NULL.
1172 * dwFlags [I] Reserved for future use and must be zero.
1173 * pdwProvType [O] DWORD designating the type of the provider.
1174 * pszTypeName [O] Buffer that receives data from the provider type.
1175 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1176 * of bytes stored in the buffer on return.
1183 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1184 * for memory allocation purposes.
1186 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1187 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1190 DWORD keylen, numkeys, dwType;
1193 static const WCHAR KEYSTR[] = {
1194 'S','o','f','t','w','a','r','e','\\',
1195 'M','i','c','r','o','s','o','f','t','\\',
1196 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1197 'D','e','f','a','u','l','t','s','\\',
1198 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1200 static const WCHAR typenameW[] = {'T','y','p','e','N','a','m','e',0};
1202 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved,
1203 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1205 if (pdwReserved || !pdwProvType || !pcbTypeName)
1207 SetLastError(ERROR_INVALID_PARAMETER);
1212 SetLastError(NTE_BAD_FLAGS);
1216 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, KEYSTR, &hKey))
1219 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1220 if (dwIndex >= numkeys)
1222 SetLastError(ERROR_NO_MORE_ITEMS);
1226 if ( !(keyname = CRYPT_Alloc(keylen*sizeof(WCHAR))) )
1228 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1231 if ( RegEnumKeyW(hKey, dwIndex, keyname, keylen) ) {
1232 CRYPT_Free(keyname);
1235 RegOpenKeyW(hKey, keyname, &hSubkey);
1236 ch = keyname + strlenW(keyname);
1237 /* Convert "Type 000" to 0, etc/ */
1238 *pdwProvType = *(--ch) - '0';
1239 *pdwProvType += (*(--ch) - '0') * 10;
1240 *pdwProvType += (*(--ch) - '0') * 100;
1241 CRYPT_Free(keyname);
1243 result = RegQueryValueExW(hSubkey, typenameW, NULL, &dwType, (LPBYTE)pszTypeName, pcbTypeName);
1246 SetLastError(result);
1250 RegCloseKey(hSubkey);
1255 /******************************************************************************
1256 * CryptEnumProviderTypesA (ADVAPI32.@)
1258 * See CryptEnumProviderTypesW.
1260 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1261 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1267 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1268 pdwProvType, pszTypeName, pcbTypeName);
1270 CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, NULL, &strlen);
1271 if ( pszTypeName && !(str = CRYPT_Alloc(strlen)) )
1273 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1276 ret = CryptEnumProviderTypesW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1278 CRYPT_UnicodeToANSI(str, &pszTypeName, *pcbTypeName);
1279 *pcbTypeName = strlen / sizeof(WCHAR);
1283 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1285 SetLastError(ERROR_MORE_DATA);
1292 /******************************************************************************
1293 * CryptExportKey (ADVAPI32.@)
1295 * Exports a cryptographic key from a CSP.
1298 * hKey [I] Handle to the key to export.
1299 * hExpKey [I] Handle to a cryptographic key of the end user.
1300 * dwBlobType [I] Type of BLOB to be exported.
1301 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1302 * pbData [O] Buffer to receive BLOB data.
1303 * pdwDataLen [I/O] Specifies the size of pbData.
1310 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1311 * buffer needed to hold the BLOB.
1313 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1314 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1317 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1319 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1321 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1323 SetLastError(ERROR_INVALID_PARAMETER);
1327 prov = key->pProvider;
1328 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1329 dwBlobType, dwFlags, pbData, pdwDataLen);
1332 /******************************************************************************
1333 * CryptGenKey (ADVAPI32.@)
1335 * Generates a random cryptographic session key or a pub/priv key pair.
1338 * hProv [I] Handle to a CSP.
1339 * Algid [I] Algorithm to use to make key.
1340 * dwFlags [I] Specifies type of key to make.
1341 * phKey [I] Address of the handle to which the new key is copied.
1347 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1349 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1352 TRACE("(0x%lx, %d, %08x, %p)\n", hProv, Algid, dwFlags, phKey);
1356 SetLastError(ERROR_INVALID_HANDLE);
1359 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1361 SetLastError(ERROR_INVALID_PARAMETER);
1364 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1366 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1370 key->pProvider = prov;
1372 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1374 *phKey = (HCRYPTKEY)key;
1383 /******************************************************************************
1384 * CryptGetDefaultProviderW (ADVAPI32.@)
1386 * Finds the default CSP of a certain provider type.
1389 * dwProvType [I] Provider type to look for.
1390 * pdwReserved [I] Reserved for future use and must be NULL.
1391 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1392 * pszProvName [O] Name of the default CSP.
1393 * pcbProvName [I/O] Size of pszProvName
1400 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1401 * memory allocation purposes on return.
1403 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1404 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1409 static const WCHAR nameW[] = {'N','a','m','e',0};
1411 if (pdwReserved || !pcbProvName)
1413 SetLastError(ERROR_INVALID_PARAMETER);
1416 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1418 SetLastError(NTE_BAD_FLAGS);
1421 if (dwProvType > 999)
1423 SetLastError(NTE_BAD_PROV_TYPE);
1426 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1428 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1431 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1433 CRYPT_Free(keyname);
1434 SetLastError(NTE_PROV_TYPE_NOT_DEF);
1437 CRYPT_Free(keyname);
1439 result = RegQueryValueExW(hKey, nameW, NULL, NULL, (LPBYTE)pszProvName, pcbProvName);
1442 if (result != ERROR_MORE_DATA)
1443 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1445 SetLastError(result);
1454 /******************************************************************************
1455 * CryptGetDefaultProviderA (ADVAPI32.@)
1457 * See CryptGetDefaultProviderW.
1459 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1460 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1466 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1468 CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, NULL, &strlen);
1469 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1471 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1474 ret = CryptGetDefaultProviderW(dwProvType, pdwReserved, dwFlags, str, &strlen);
1476 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1477 *pcbProvName = strlen / sizeof(WCHAR);
1481 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1483 SetLastError(ERROR_MORE_DATA);
1490 /******************************************************************************
1491 * CryptGetHashParam (ADVAPI32.@)
1493 * Retrieves data that controls the operations of a hash object.
1496 * hHash [I] Handle of the hash object to question.
1497 * dwParam [I] Query type.
1498 * pbData [O] Buffer that receives the value data.
1499 * pdwDataLen [I/O] Size of the pbData buffer.
1500 * dwFlags [I] Reserved for future use and must be zero.
1507 * If pbData is NULL, pdwDataLen will contain the length required.
1509 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1510 DWORD *pdwDataLen, DWORD dwFlags)
1513 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1515 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1517 if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1519 SetLastError(ERROR_INVALID_PARAMETER);
1523 prov = hash->pProvider;
1524 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1525 pbData, pdwDataLen, dwFlags);
1528 /******************************************************************************
1529 * CryptGetKeyParam (ADVAPI32.@)
1531 * Retrieves data that controls the operations of a key.
1534 * hKey [I] Handle to they key in question.
1535 * dwParam [I] Specifies query type.
1536 * pbData [O] Sequence of bytes to receive data.
1537 * pdwDataLen [I/O] Size of pbData.
1538 * dwFlags [I] Reserved for future use and must be zero.
1545 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1547 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1548 DWORD *pdwDataLen, DWORD dwFlags)
1551 PCRYPTKEY key = (PCRYPTKEY)hKey;
1553 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1555 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1557 SetLastError(ERROR_INVALID_PARAMETER);
1561 prov = key->pProvider;
1562 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1563 pbData, pdwDataLen, dwFlags);
1566 /******************************************************************************
1567 * CryptGetProvParam (ADVAPI32.@)
1569 * Retrieves parameters that control the operations of a CSP.
1572 * hProv [I] Handle of the CSP in question.
1573 * dwParam [I] Specifies query type.
1574 * pbData [O] Buffer to receive the data.
1575 * pdwDataLen [I/O] Size of pbData.
1576 * dwFlags [I] see MSDN Docs.
1583 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1585 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1586 DWORD *pdwDataLen, DWORD dwFlags)
1588 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1590 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1592 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1594 SetLastError(ERROR_INVALID_PARAMETER);
1598 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1601 /******************************************************************************
1602 * CryptGetUserKey (ADVAPI32.@)
1604 * Gets a handle of one of a user's two public/private key pairs.
1607 * hProv [I] Handle of a CSP.
1608 * dwKeySpec [I] Private key to use.
1609 * phUserKey [O] Pointer to the handle of the retrieved keys.
1615 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1617 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1620 TRACE("(0x%lx, %d, %p)\n", hProv, dwKeySpec, phUserKey);
1624 SetLastError(ERROR_INVALID_HANDLE);
1627 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1629 SetLastError(ERROR_INVALID_PARAMETER);
1632 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1634 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1638 key->pProvider = prov;
1640 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1642 *phUserKey = (HCRYPTKEY)key;
1652 /******************************************************************************
1653 * CryptHashData (ADVAPI32.@)
1655 * Adds data to a hash object.
1658 * hHash [I] Handle of the hash object.
1659 * pbData [I] Buffer of data to be hashed.
1660 * dwDataLen [I] Number of bytes to add.
1661 * dwFlags [I] Can be CRYPT_USERDATA
1667 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1669 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1672 TRACE("(0x%lx, %p, %d, %08x)\n", hHash, pbData, dwDataLen, dwFlags);
1676 SetLastError(ERROR_INVALID_HANDLE);
1679 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1681 SetLastError(ERROR_INVALID_PARAMETER);
1685 prov = hash->pProvider;
1686 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1689 /******************************************************************************
1690 * CryptHashSessionKey (ADVAPI32.@)
1692 * Compute the cryptographic hash of a session key object.
1695 * hHash [I] Handle to the hash object.
1696 * hKey [I] Handle to the key to be hashed.
1697 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1703 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1705 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1706 PCRYPTKEY key = (PCRYPTKEY)hKey;
1709 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash, hKey, dwFlags);
1713 SetLastError(ERROR_INVALID_HANDLE);
1717 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1719 SetLastError(ERROR_INVALID_PARAMETER);
1723 prov = hash->pProvider;
1724 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1727 /******************************************************************************
1728 * CryptImportKey (ADVAPI32.@)
1730 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1733 * hProv [I] Handle of a CSP.
1734 * pbData [I] Contains the key to be imported.
1735 * dwDataLen [I] Length of the key.
1736 * hPubKey [I] Cryptographic key that decrypts pdData
1737 * dwFlags [I] Used only with a public/private key pair.
1738 * phKey [O] Imported key.
1744 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, BYTE *pbData, DWORD dwDataLen,
1745 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1747 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1748 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1750 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1752 if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV)
1754 SetLastError(ERROR_INVALID_PARAMETER);
1758 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1760 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1764 importkey->pProvider = prov;
1765 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1766 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1768 *phKey = (HCRYPTKEY)importkey;
1772 CRYPT_Free(importkey);
1776 /******************************************************************************
1777 * CryptSignHashW (ADVAPI32.@)
1782 * hHash [I] Handle of the hash object to be signed.
1783 * dwKeySpec [I] Private key to use.
1784 * sDescription [I] Should be NULL.
1785 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1786 * pbSignature [O] Buffer of the signature data.
1787 * pdwSigLen [I/O] Size of the pbSignature buffer.
1794 * Because of security flaws sDescription should not be used and should thus be
1795 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1798 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1799 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1801 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1804 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1805 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1809 SetLastError(ERROR_INVALID_HANDLE);
1812 if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1814 SetLastError(ERROR_INVALID_PARAMETER);
1818 prov = hash->pProvider;
1819 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1820 dwFlags, pbSignature, pdwSigLen);
1823 /******************************************************************************
1824 * CryptSignHashA (ADVAPI32.@)
1826 * See CryptSignHashW.
1828 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1829 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1831 LPWSTR wsDescription;
1834 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1835 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1837 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1838 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1839 CRYPT_Free(wsDescription);
1844 /******************************************************************************
1845 * CryptSetHashParam (ADVAPI32.@)
1847 * Customizes the operations of a hash object.
1850 * hHash [I] Handle of the hash object to set parameters.
1851 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1852 * pbData [I] Value data buffer.
1853 * dwFlags [I] Reserved for future use and must be zero.
1859 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1862 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1864 TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
1866 if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1868 SetLastError(ERROR_INVALID_PARAMETER);
1872 prov = hash->pProvider;
1873 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1874 dwParam, pbData, dwFlags);
1877 /******************************************************************************
1878 * CryptSetKeyParam (ADVAPI32.@)
1880 * Customizes a session key's operations.
1883 * hKey [I] Handle to the key to set values.
1884 * dwParam [I] See MSDN Doc.
1885 * pbData [I] Buffer of values to set.
1886 * dwFlags [I] Only used when dwParam == KP_ALGID.
1892 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1895 PCRYPTKEY key = (PCRYPTKEY)hKey;
1897 TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
1899 if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1901 SetLastError(ERROR_INVALID_PARAMETER);
1905 prov = key->pProvider;
1906 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1907 dwParam, pbData, dwFlags);
1910 /******************************************************************************
1911 * CryptSetProviderA (ADVAPI32.@)
1913 * Specifies the current user's default CSP.
1916 * pszProvName [I] Name of the new default CSP.
1917 * dwProvType [I] Provider type of the CSP.
1923 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
1925 TRACE("(%s, %d)\n", pszProvName, dwProvType);
1926 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1929 /******************************************************************************
1930 * CryptSetProviderW (ADVAPI32.@)
1932 * See CryptSetProviderA.
1934 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
1936 TRACE("(%s, %d)\n", debugstr_w(pszProvName), dwProvType);
1937 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1940 /******************************************************************************
1941 * CryptSetProviderExW (ADVAPI32.@)
1943 * Specifies the default CSP.
1946 * pszProvName [I] Name of the new default CSP.
1947 * dwProvType [I] Provider type of the CSP.
1948 * pdwReserved [I] Reserved for future use and must be NULL.
1949 * dwFlags [I] See MSDN Doc.
1955 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1957 HKEY hProvKey, hTypeKey;
1959 static const WCHAR nameW[] = {'N','a','m','e',0};
1961 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
1963 if (!pszProvName || pdwReserved)
1965 SetLastError(ERROR_INVALID_PARAMETER);
1968 if (dwProvType > MAXPROVTYPES)
1970 SetLastError(NTE_BAD_PROV_TYPE);
1973 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
1974 || dwFlags == CRYPT_DELETE_DEFAULT)
1976 SetLastError(NTE_BAD_FLAGS);
1980 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
1982 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1985 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1986 keyname, &hTypeKey))
1988 CRYPT_Free(keyname);
1989 SetLastError(NTE_BAD_PROVIDER);
1992 CRYPT_Free(keyname);
1994 if (dwFlags & CRYPT_DELETE_DEFAULT)
1996 RegDeleteValueW(hTypeKey, nameW);
2000 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
2002 RegCloseKey(hTypeKey);
2003 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2006 if (RegOpenKeyW((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
2007 keyname, &hProvKey))
2009 CRYPT_Free(keyname);
2010 RegCloseKey(hTypeKey);
2011 SetLastError(NTE_BAD_PROVIDER);
2014 CRYPT_Free(keyname);
2016 if (RegSetValueExW(hTypeKey, nameW, 0, REG_SZ, (const BYTE *)pszProvName,
2017 (strlenW(pszProvName) + 1)*sizeof(WCHAR)))
2019 RegCloseKey(hTypeKey);
2020 RegCloseKey(hProvKey);
2024 RegCloseKey(hProvKey);
2026 RegCloseKey(hTypeKey);
2031 /******************************************************************************
2032 * CryptSetProviderExA (ADVAPI32.@)
2034 * See CryptSetProviderExW.
2036 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
2041 TRACE("(%s, %d, %p, %08x)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
2043 if (CRYPT_ANSIToUnicode(pszProvName, &str, -1))
2045 ret = CryptSetProviderExW(str, dwProvType, pdwReserved, dwFlags);
2051 /******************************************************************************
2052 * CryptSetProvParam (ADVAPI32.@)
2054 * Customizes the operations of a CSP.
2057 * hProv [I] Handle of a CSP.
2058 * dwParam [I] See MSDN Doc.
2059 * pbData [I] Buffer that contains a value to set as a parameter.
2060 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2066 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
2068 PCRYPTPROV prov = (PCRYPTPROV)hProv;
2070 TRACE("(0x%lx, %d, %p, %08x)\n", hProv, dwParam, pbData, dwFlags);
2074 SetLastError(ERROR_INVALID_HANDLE);
2077 if (prov->dwMagic != MAGIC_CRYPTPROV)
2079 SetLastError(ERROR_INVALID_PARAMETER);
2082 if (dwFlags & PP_USE_HARDWARE_RNG)
2084 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2085 FIXME("\tLetting the CSP decide.\n");
2087 if (dwFlags & PP_CLIENT_HWND)
2089 /* FIXME: Should verify the parameter */
2090 if (pbData /* && IsWindow((HWND)pbData) */)
2092 crypt_hWindow = (HWND)(pbData);
2095 SetLastError(ERROR_INVALID_PARAMETER);
2099 /* All other flags go to the CSP */
2100 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
2103 /******************************************************************************
2104 * CryptVerifySignatureW (ADVAPI32.@)
2106 * Verifies the signature of a hash object.
2109 * hHash [I] Handle of the hash object to verify.
2110 * pbSignature [I] Signature data to verify.
2111 * dwSigLen [I] Size of pbSignature.
2112 * hPubKey [I] Handle to the public key to authenticate signature.
2113 * sDescription [I] Should be NULL.
2114 * dwFlags [I] See MSDN doc.
2121 * Because of security flaws sDescription should not be used and should thus be
2122 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2125 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
2126 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
2128 PCRYPTHASH hash = (PCRYPTHASH)hHash;
2129 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
2132 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2133 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
2137 SetLastError(ERROR_INVALID_HANDLE);
2140 if (!pbSignature || !dwSigLen ||
2141 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
2142 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
2144 SetLastError(ERROR_INVALID_PARAMETER);
2148 prov = hash->pProvider;
2149 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
2150 key->hPrivate, sDescription, dwFlags);
2153 /******************************************************************************
2154 * CryptVerifySignatureA (ADVAPI32.@)
2156 * See CryptVerifySignatureW.
2158 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
2159 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
2161 LPWSTR wsDescription;
2164 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
2165 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
2167 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
2168 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
2169 CRYPT_Free(wsDescription);
2174 /******************************************************************************
2175 * SystemFunction030 (ADVAPI32.@)
2177 * Tests if two blocks of 16 bytes are equal
2180 * b1,b2 [I] block of 16 bytes
2183 * TRUE if blocks are the same
2184 * FALSE if blocks are different
2186 BOOL WINAPI SystemFunction030(PVOID b1, PVOID b2)
2188 return !memcmp(b1, b2, 0x10);
2191 /******************************************************************************
2192 * SystemFunction035 (ADVAPI32.@)
2195 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2198 * Stub, always return TRUE.
2200 BOOL WINAPI SystemFunction035(LPCSTR lpszDllFilePath)
2202 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath));
2206 /******************************************************************************
2207 * SystemFunction036 (ADVAPI32.@)
2209 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2212 * pbBufer [O] Pointer to memory to receive random bytes.
2213 * dwLen [I] Number of random bytes to fetch.
2220 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
2224 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
2225 * quality for cryptographic applications. /dev/random is much better,
2226 * but it blocks if the kernel has not yet collected enough entropy for
2227 * the request, which will suspend the calling thread for an indefinite
2228 * amount of time. */
2229 dev_random = open("/dev/urandom", O_RDONLY);
2230 if (dev_random != -1)
2232 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
2239 SetLastError(NTE_FAIL);
2244 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2248 /******************************************************************************
2249 * SystemFunction040 (ADVAPI32.@)
2251 * MSDN documents this function as RtlEncryptMemory.
2254 * memory [I/O] Pointer to memory to encrypt.
2255 * length [I] Length of region to encrypt in bytes.
2256 * flags [I] Control whether other processes are able to decrypt the memory.
2257 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2258 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2259 * RTL_ENCRYPT_OPTION_SAME_LOGON
2262 * Success: STATUS_SUCCESS
2263 * Failure: NTSTATUS error code
2266 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2267 * If flags are specified when encrypting, the same flag value must be given
2268 * when decrypting the memory.
2270 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
2272 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory, length, flags);
2273 return STATUS_SUCCESS;
2276 /******************************************************************************
2277 * SystemFunction041 (ADVAPI32.@)
2279 * MSDN documents this function as RtlDecryptMemory.
2282 * memory [I/O] Pointer to memory to decrypt.
2283 * length [I] Length of region to decrypt in bytes.
2284 * flags [I] Control whether other processes are able to decrypt the memory.
2285 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2286 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2287 * RTL_ENCRYPT_OPTION_SAME_LOGON
2290 * Success: STATUS_SUCCESS
2291 * Failure: NTSTATUS error code
2294 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2295 * If flags are specified when encrypting, the same flag value must be given
2296 * when decrypting the memory.
2298 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
2300 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory, length, flags);
2301 return STATUS_SUCCESS;