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
30 #include <sys/types.h>
38 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
44 HWND crypt_hWindow = 0;
46 #define CRYPT_ReturnLastError(err) {SetLastError(err); return FALSE;}
48 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
49 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))
51 static inline PSTR CRYPT_GetProvKeyName(PCSTR pProvName)
53 PCSTR KEYSTR = "Software\\Microsoft\\Cryptography\\Defaults\\Provider\\";
56 keyname = CRYPT_Alloc(strlen(KEYSTR) + strlen(pProvName) +1);
59 strcpy(keyname, KEYSTR);
60 strcpy(keyname + strlen(KEYSTR), pProvName);
62 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
66 static inline PSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
68 PCSTR MACHINESTR = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
69 PCSTR USERSTR = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
73 keyname = CRYPT_Alloc( (user ? strlen(USERSTR) : strlen(MACHINESTR)) +1);
76 user ? strcpy(keyname, USERSTR) : strcpy(keyname, MACHINESTR);
77 ptr = keyname + strlen(keyname);
78 *(--ptr) = (dwType % 10) + '0';
79 *(--ptr) = ((dwType / 10) % 10) + '0';
80 *(--ptr) = (dwType / 100) + '0';
82 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
86 /* CRYPT_UnicodeTOANSI
87 * wstr - unicode string
88 * str - pointer to ANSI string
89 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
91 * returns TRUE if unsuccessfull, FALSE otherwise.
92 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
94 static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
103 count = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
105 *str = CRYPT_Alloc(count * sizeof(CHAR));
107 count = min( count, strsize );
110 WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, count, NULL, NULL);
113 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
117 /* CRYPT_ANSITOUnicode
119 * wstr - pointer to unicode string
120 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
122 static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
131 wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
133 *wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
135 wcount = min( wcount, wstrsize/sizeof(WCHAR) );
138 MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
141 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
145 /* These next 2 functions are used by the VTableProvStruc structure */
146 static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
148 if (!lpszImage || !pData)
150 SetLastError(ERROR_INVALID_PARAMETER);
154 FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
159 static BOOL CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
163 *phWnd = crypt_hWindow;
167 #define CRYPT_GetProvFunc(name) \
168 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
169 #define CRYPT_GetProvFuncOpt(name) \
170 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
171 PCRYPTPROV CRYPT_LoadProvider(PSTR pImage)
174 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
176 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
177 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
178 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
179 if ( !(provider->hModule = LoadLibraryA(pImage)) )
181 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
182 FIXME("Failed to load dll %s\n", debugstr_a(pImage));
185 provider->dwMagic = MAGIC_CRYPTPROV;
186 provider->refcount = 1;
188 errorcode = NTE_PROVIDER_DLL_FAIL;
189 CRYPT_GetProvFunc(CPAcquireContext);
190 CRYPT_GetProvFunc(CPCreateHash);
191 CRYPT_GetProvFunc(CPDecrypt);
192 CRYPT_GetProvFunc(CPDeriveKey);
193 CRYPT_GetProvFunc(CPDestroyHash);
194 CRYPT_GetProvFunc(CPDestroyKey);
195 CRYPT_GetProvFuncOpt(CPDuplicateHash);
196 CRYPT_GetProvFuncOpt(CPDuplicateKey);
197 CRYPT_GetProvFunc(CPEncrypt);
198 CRYPT_GetProvFunc(CPExportKey);
199 CRYPT_GetProvFunc(CPGenKey);
200 CRYPT_GetProvFunc(CPGenRandom);
201 CRYPT_GetProvFunc(CPGetHashParam);
202 CRYPT_GetProvFunc(CPGetKeyParam);
203 CRYPT_GetProvFunc(CPGetProvParam);
204 CRYPT_GetProvFunc(CPGetUserKey);
205 CRYPT_GetProvFunc(CPHashData);
206 CRYPT_GetProvFunc(CPHashSessionKey);
207 CRYPT_GetProvFunc(CPImportKey);
208 CRYPT_GetProvFunc(CPReleaseContext);
209 CRYPT_GetProvFunc(CPSetHashParam);
210 CRYPT_GetProvFunc(CPSetKeyParam);
211 CRYPT_GetProvFunc(CPSetProvParam);
212 CRYPT_GetProvFunc(CPSignHash);
213 CRYPT_GetProvFunc(CPVerifySignature);
215 /* FIXME: Not sure what the pbContextInfo field is for.
216 * Does it need memory allocation?
218 provider->pVTable->Version = 3;
219 provider->pVTable->pFuncVerifyImage = (FARPROC)CRYPT_VerifyImage;
220 provider->pVTable->pFuncReturnhWnd = (FARPROC)CRYPT_ReturnhWnd;
221 provider->pVTable->dwProvType = 0;
222 provider->pVTable->pbContextInfo = NULL;
223 provider->pVTable->cbContextInfo = 0;
224 provider->pVTable->pszProvName = NULL;
228 SetLastError(errorcode);
231 provider->dwMagic = 0;
232 if (provider->hModule)
233 FreeLibrary(provider->hModule);
234 CRYPT_Free(provider->pVTable);
235 CRYPT_Free(provider->pFuncs);
236 CRYPT_Free(provider);
240 #undef CRYPT_GetProvFunc
241 #undef CRYPT_GetProvFuncOpt
244 /******************************************************************************
245 * CryptAcquireContextA (ADVAPI32.@)
246 * Acquire a crypto provider context handle.
249 * phProv: Pointer to HCRYPTPROV for the output.
250 * pszContainer: Key Container Name
251 * pszProvider: Cryptographic Service Provider Name
252 * dwProvType: Crypto provider type to get a handle.
253 * dwFlags: flags for the operation
255 * RETURNS TRUE on success, FALSE on failure.
257 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
258 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
260 PCRYPTPROV pProv = NULL;
262 PSTR imagepath = NULL, keyname = NULL, provname = NULL, temp = NULL;
263 DWORD keytype, type, len;
266 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv, pszContainer,
267 pszProvider, dwProvType, dwFlags);
269 if (dwProvType < 1 || dwProvType > MAXPROVTYPES)
271 SetLastError(NTE_BAD_PROV_TYPE);
277 SetLastError(ERROR_INVALID_PARAMETER);
281 if (!pszProvider || !*pszProvider)
283 /* No CSP name specified so try the user default CSP first
284 * then try the machine default CSP
286 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, TRUE)) ) {
287 TRACE("No provider registered for crypto provider type %ld.\n", dwProvType);
288 SetLastError(NTE_PROV_TYPE_NOT_DEF);
291 if (RegOpenKeyA(HKEY_CURRENT_USER, keyname, &key))
294 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, FALSE)) ) {
295 TRACE("No type registered for crypto provider type %ld.\n", dwProvType);
297 SetLastError(NTE_PROV_TYPE_NOT_DEF);
300 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, keyname, &key)) {
301 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_a(keyname));
304 SetLastError(NTE_PROV_TYPE_NOT_DEF);
309 r = RegQueryValueExA(key, "Name", NULL, &keytype, NULL, &len);
310 if( r != ERROR_SUCCESS || !len || keytype != REG_SZ)
312 TRACE("error %ld reading size of 'Name' from registry\n", r );
314 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
317 if(!(provname = CRYPT_Alloc(len)))
320 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
323 r = RegQueryValueExA(key, "Name", NULL, NULL, provname, &len);
324 if( r != ERROR_SUCCESS )
326 TRACE("error %ld reading 'Name' from registry\n", r );
328 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
333 if ( !(provname = CRYPT_Alloc(strlen(pszProvider) +1)) )
335 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
338 strcpy(provname, pszProvider);
341 keyname = CRYPT_GetProvKeyName(provname);
342 r = RegOpenKeyA(HKEY_LOCAL_MACHINE, keyname, &key);
344 if (r != ERROR_SUCCESS)
346 SetLastError(NTE_KEYSET_NOT_DEF);
350 r = RegQueryValueExA(key, "Type", NULL, NULL, (BYTE*)&type, &len);
351 if (r != ERROR_SUCCESS)
353 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
356 if (type != dwProvType)
358 TRACE("Crypto provider has wrong type (%ld vs expected %ld).\n", type, dwProvType);
359 SetLastError(NTE_PROV_TYPE_NO_MATCH);
363 r = RegQueryValueExA(key, "Image Path", NULL, &keytype, NULL, &len);
364 if ( r != ERROR_SUCCESS || keytype != REG_SZ)
366 TRACE("error %ld reading size of 'Image Path' from registry\n", r );
368 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
371 if (!(temp = CRYPT_Alloc(len)))
374 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
377 r = RegQueryValueExA(key, "Image Path", NULL, NULL, temp, &len);
378 if( r != ERROR_SUCCESS )
380 TRACE("error %ld reading 'Image Path' from registry\n", r );
382 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
386 len = ExpandEnvironmentStringsA(temp, NULL, 0);
387 if ( !(imagepath = CRYPT_Alloc(len)) )
389 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
392 if (!ExpandEnvironmentStringsA(temp, imagepath, len))
394 /* ExpandEnvironmentStrings will call SetLastError */
397 pProv = CRYPT_LoadProvider(imagepath);
399 /* CRYPT_LoadProvider calls SetLastError */
402 pProv->pVTable->dwProvType = dwProvType;
403 pProv->pVTable->pszProvName = provname;
404 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, (CHAR*)pszContainer, dwFlags, pProv->pVTable))
406 /* MSDN: When this flag is set, the value returned in phProv is undefined,
407 * and thus, the CryptReleaseContext function need not be called afterwards.
408 * Therefore, we must clean up everything now.
410 if (dwFlags & CRYPT_DELETEKEYSET)
413 FreeLibrary(pProv->hModule);
414 CRYPT_Free(provname);
415 CRYPT_Free(pProv->pFuncs);
418 *phProv = (HCRYPTPROV)pProv;
421 CRYPT_Free(imagepath);
424 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
430 FreeLibrary(pProv->hModule);
432 CRYPT_Free(pProv->pVTable);
434 CRYPT_Free(pProv->pFuncs);
438 CRYPT_Free(provname);
442 CRYPT_Free(imagepath);
446 /******************************************************************************
447 * CryptAcquireContextW (ADVAPI32.@)
449 * see CryptAcquireContextA
451 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
452 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
454 PSTR pProvider = NULL, pContainer = NULL;
457 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv, debugstr_w(pszContainer),
458 debugstr_w(pszProvider), dwProvType, dwFlags);
460 if ( !CRYPT_UnicodeToANSI(pszContainer, &pContainer, -1) )
461 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
462 if ( !CRYPT_UnicodeToANSI(pszProvider, &pProvider, -1) )
464 CRYPT_Free(pContainer);
465 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
468 ret = CryptAcquireContextA(phProv, pContainer, pProvider, dwProvType, dwFlags);
471 CRYPT_Free(pContainer);
473 CRYPT_Free(pProvider);
478 /******************************************************************************
479 * CryptContextAddRef (ADVAPI32.@)
481 * Increases reference count of a cryptographic service provider handle
485 * hProv [I] Handle to the CSP whose reference is being incremented.
486 * pdwReserved [IN] Reserved for future use and must be NULL.
487 * dwFlags [I] Reserved for future use and must be NULL.
493 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
495 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
497 TRACE("(0x%lx, %p, %08lx)\n", hProv, pdwReserved, dwFlags);
501 SetLastError(NTE_BAD_UID);
505 if (pProv->dwMagic != MAGIC_CRYPTPROV)
507 SetLastError(ERROR_INVALID_PARAMETER);
515 /******************************************************************************
516 * CryptReleaseContext (ADVAPI32.@)
518 * Releases the handle of a CSP. Reference count is decreased.
521 * hProv [I] Handle of a CSP.
522 * dwFlags [I] Reserved for future use and must be NULL.
528 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, DWORD dwFlags)
530 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
533 TRACE("(0x%lx, %08ld)\n", hProv, dwFlags);
537 SetLastError(NTE_BAD_UID);
541 if (pProv->dwMagic != MAGIC_CRYPTPROV)
543 SetLastError(ERROR_INVALID_PARAMETER);
548 if (pProv->refcount <= 0)
550 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
552 FreeLibrary(pProv->hModule);
554 CRYPT_Free(pProv->pVTable->pContextInfo);
556 CRYPT_Free(pProv->pVTable->pszProvName);
557 CRYPT_Free(pProv->pVTable);
558 CRYPT_Free(pProv->pFuncs);
564 /******************************************************************************
565 * CryptGenRandom (ADVAPI32.@)
567 * Fills a buffer with cryptographically random bytes.
570 * hProv [I] Handle of a CSP.
571 * dwLen [I] Number of bytes to generate.
572 * pbBuffer [I/O] Buffer to contain random bytes.
579 * pdBuffer must be at least dwLen bytes long.
581 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
583 PCRYPTPROV prov = (PCRYPTPROV)hProv;
585 TRACE("(0x%lx, %ld, %p)\n", hProv, dwLen, pbBuffer);
588 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
590 if (prov->dwMagic != MAGIC_CRYPTPROV)
591 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
593 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
596 /******************************************************************************
597 * CryptCreateHash (ADVAPI32.@)
599 * Initiates the hashing of a stream of data.
602 * hProv [I] Handle of a CSP.
603 * Algid [I] Identifies the hash algorithm to use.
604 * hKey [I] Key for the hash (if required).
605 * dwFlags [I] Reserved for future use and must be NULL.
606 * phHash [O] Address of the future handle to the new hash object.
613 * If the algorithm is a keyed hash, hKey is the key.
615 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
616 DWORD dwFlags, HCRYPTHASH *phHash)
618 PCRYPTPROV prov = (PCRYPTPROV)hProv;
619 PCRYPTKEY key = (PCRYPTKEY)hKey;
622 TRACE("(0x%lx, 0x%x, 0x%lx, %08lx, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
625 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
626 if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV)
627 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
629 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
630 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
631 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
633 hash->pProvider = prov;
635 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
636 key ? key->hPrivate : 0, 0, &hash->hPrivate))
638 *phHash = (HCRYPTHASH)hash;
648 /******************************************************************************
649 * CryptDecrypt (ADVAPI32.@)
651 * Decrypts data encrypted by CryptEncrypt.
654 * hKey [I] Handle to the decryption key.
655 * hHash [I] Handle to a hash object.
656 * Final [I] TRUE if this is the last section to be decrypted.
657 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
658 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
660 * pdwDataLen [I/O] Length of pbData before and after the call.
666 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
667 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
670 PCRYPTKEY key = (PCRYPTKEY)hKey;
671 PCRYPTHASH hash = (PCRYPTHASH)hHash;
673 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
675 if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
676 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
678 prov = key->pProvider;
679 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
680 Final, dwFlags, pbData, pdwDataLen);
683 /******************************************************************************
684 * CryptDeriveKey (ADVAPI32.@)
686 * Generates session keys derived from a base data value.
689 * hProv [I] Handle to a CSP.
690 * Algid [I] Identifies the symmetric encryption algorithm to use.
691 * hBaseData [I] Handle to a hash object.
692 * dwFlags [I] Type of key to generate.
693 * phKey [I/O] Address of the newly generated key.
699 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
700 DWORD dwFlags, HCRYPTKEY *phKey)
702 PCRYPTPROV prov = (PCRYPTPROV)hProv;
703 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
706 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08lx, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
709 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
710 if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV)
711 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
712 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
713 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
715 key->pProvider = prov;
716 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
718 *phKey = (HCRYPTKEY)key;
728 /******************************************************************************
729 * CryptDestroyHash (ADVAPI32.@)
731 * Destroys the hash object referenced by hHash.
734 * hHash [I] Handle of the hash object to be destroyed.
740 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
742 PCRYPTHASH hash = (PCRYPTHASH)hHash;
746 TRACE("(0x%lx)\n", hHash);
749 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
751 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
752 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
754 prov = hash->pProvider;
755 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
760 /******************************************************************************
761 * CryptDestroyKey (ADVAPI32.@)
763 * Releases the handle referenced by hKey.
766 * hKey [I] Handle of the key to be destroyed.
772 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
774 PCRYPTKEY key = (PCRYPTKEY)hKey;
778 TRACE("(0x%lx)\n", hKey);
781 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
783 if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
784 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
786 prov = key->pProvider;
787 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
792 /******************************************************************************
793 * CryptDuplicateHash (ADVAPI32.@)
798 * hHash [I] Handle to the hash to be copied.
799 * pdwReserved [I] Reserved for future use and must be zero.
800 * dwFlags [I] Reserved for future use and must be zero.
801 * phHash [O] Address of the handle to receive the copy.
807 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
808 DWORD dwFlags, HCRYPTHASH *phHash)
811 PCRYPTHASH orghash, newhash;
813 TRACE("(0x%lx, %p, %08ld, %p)\n", hHash, pdwReserved, dwFlags, phHash);
815 orghash = (PCRYPTHASH)hHash;
816 if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
817 orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
819 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
822 prov = orghash->pProvider;
823 if (!prov->pFuncs->pCPDuplicateHash)
824 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
826 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
827 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
829 newhash->pProvider = prov;
830 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
832 *phHash = (HCRYPTHASH)newhash;
839 /******************************************************************************
840 * CryptDuplicateKey (ADVAPI32.@)
842 * Duplicate a key and the key's state.
845 * hKey [I] Handle of the key to copy.
846 * pdwReserved [I] Reserved for future use and must be NULL.
847 * dwFlags [I] Reserved for future use and must be zero.
848 * phKey [I] Address of the handle to the duplicated key.
854 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
857 PCRYPTKEY orgkey, newkey;
859 TRACE("(0x%lx, %p, %08ld, %p)\n", hKey, pdwReserved, dwFlags, phKey);
861 orgkey = (PCRYPTKEY)hKey;
862 if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
863 orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
865 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
868 prov = orgkey->pProvider;
869 if (!prov->pFuncs->pCPDuplicateKey)
870 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
872 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
873 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
875 newkey->pProvider = prov;
876 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
878 *phKey = (HCRYPTKEY)newkey;
885 /******************************************************************************
886 * CryptEncrypt (ADVAPI32.@)
891 * hKey [I] Handle to the enryption key.
892 * hHash [I] Handle to a hash object.
893 * Final [I] TRUE if this is the last section to encrypt.
894 * dwFlags [I] Can be CRYPT_OAEP.
895 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
896 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
897 * encrypted data after call.
898 * dwBufLen [I] Length of the input pbData buffer.
905 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
906 * required for the returned data in pdwDataLen.
908 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
909 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
912 PCRYPTKEY key = (PCRYPTKEY)hKey;
913 PCRYPTHASH hash = (PCRYPTHASH)hHash;
915 TRACE("(0x%lx, 0x%lx, %d, %08ld, %p, %p, %ld)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
917 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
918 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
920 prov = key->pProvider;
921 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
922 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
925 /******************************************************************************
926 * CryptEnumProvidersW (ADVAPI32.@)
928 * Returns the next availabe CSP.
931 * dwIndex [I] Index of the next provider to be enumerated.
932 * pdwReserved [I] Reserved for future use and must be NULL.
933 * dwFlags [I] Reserved for future use and must be zero.
934 * pdwProvType [O] DWORD designating the type of the provider.
935 * pszProvName [O] Buffer that receives data from the provider.
936 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
937 * of bytes stored in the buffer on return.
944 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
945 * for memory allocation purposes.
947 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
948 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
952 static const WCHAR providerW[] = {
953 'S','o','f','t','w','a','r','e','\\',
954 'M','i','c','r','o','s','o','f','t','\\',
955 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
956 'D','e','f','a','u','l','t','s','\\',
957 'P','r','o','v','i','d','e','r',0
960 static const WCHAR typeW[] = {'T','y','p','e',0};
962 TRACE("(%ld, %p, %ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
963 pdwProvType, pszProvName, pcbProvName);
965 if (pdwReserved || !pcbProvName) CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
966 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
968 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
969 CRYPT_ReturnLastError(NTE_FAIL);
976 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
977 NULL, NULL, NULL, NULL, NULL, NULL);
979 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
980 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
982 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
984 *pcbProvName *= sizeof(WCHAR);
986 CRYPT_Free(provNameW);
988 if (dwIndex >= numkeys)
989 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
991 DWORD size = sizeof(DWORD);
995 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
997 CRYPT_ReturnLastError(result);
998 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1000 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1002 RegCloseKey(subkey);
1008 /******************************************************************************
1009 * CryptEnumProvidersA (ADVAPI32.@)
1011 * see CryptEnumProvidersW
1013 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1014 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1018 BOOL ret; /* = FALSE; */
1020 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1021 pdwProvType, pszProvName, pcbProvName);
1023 strlen = *pcbProvName * sizeof(WCHAR);
1024 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1025 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1026 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1029 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1032 *pcbProvName = strlen / sizeof(WCHAR); /* FIXME: not correct */
1036 /******************************************************************************
1037 * CryptEnumProviderTypesA (ADVAPI32i.@)
1039 * Retrieves the next type of CSP supported.
1042 * dwIndex [I] Index of the next provider to be enumerated.
1043 * pdwReserved [I] Reserved for future use and must be NULL.
1044 * dwFlags [I] Reserved for future use and must be zero.
1045 * pdwProvType [O] DWORD designating the type of the provider.
1046 * pszTypeName [O] Buffer that receives data from the provider type.
1047 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1048 * of bytes stored in the buffer on return.
1055 * If pszTypeName is NULL, CryptEnumProviderTypesA sets the size of the name
1056 * for memory allocation purposes.
1058 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1059 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1062 DWORD keylen, numkeys, dwType;
1066 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex, pdwReserved,
1067 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1069 if (pdwReserved || !pdwProvType || !pcbTypeName)
1070 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1071 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1073 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
1076 RegQueryInfoKeyA(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1077 if (dwIndex >= numkeys)
1078 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
1080 if ( !(keyname = CRYPT_Alloc(keylen)) )
1081 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1082 if ( RegEnumKeyA(hKey, dwIndex, keyname, keylen) ) {
1083 CRYPT_Free(keyname);
1086 RegOpenKeyA(hKey, keyname, &hSubkey);
1087 ch = keyname + strlen(keyname);
1088 /* Convert "Type 000" to 0, etc/ */
1089 *pdwProvType = *(--ch) - '0';
1090 *pdwProvType += (*(--ch) - '0') * 10;
1091 *pdwProvType += (*(--ch) - '0') * 100;
1092 CRYPT_Free(keyname);
1094 result = RegQueryValueExA(hSubkey, "TypeName", NULL, &dwType, pszTypeName, pcbTypeName);
1096 CRYPT_ReturnLastError(result);
1098 RegCloseKey(hSubkey);
1103 /******************************************************************************
1104 * CryptEnumProviderTypesW (ADVAPI32.@)
1106 * see CryptEnumProviderTypesA
1108 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1109 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1115 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1116 pdwProvType, pszTypeName, pcbTypeName);
1117 strlen = *pcbTypeName / sizeof(WCHAR);
1118 if ( pszTypeName && !(str = CRYPT_Alloc(strlen)) )
1119 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1120 ret = CryptEnumProviderTypesA(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1123 CRYPT_ANSIToUnicode(str, &pszTypeName, *pcbTypeName);
1126 *pcbTypeName = strlen * sizeof(WCHAR);
1130 /******************************************************************************
1131 * CryptExportKey (ADVAPI32.@)
1133 * Exports a cryptographic key from a CSP.
1136 * hKey [I] Handle to the key to export.
1137 * hExpKey [I] Handle to a cryptographic key of the end user.
1138 * dwBlobType [I] Type of BLOB to be exported.
1139 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1140 * pbData [O] Buffer to receive BLOB data.
1141 * pdwDataLen [I/O] Specifies the size of pbData.
1148 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1149 * buffer needed to hold the BLOB.
1151 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1152 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1155 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1157 TRACE("(0x%lx, 0x%lx, %ld, %08ld, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1159 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1160 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1162 prov = key->pProvider;
1163 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1164 dwBlobType, dwFlags, pbData, pdwDataLen);
1167 /******************************************************************************
1168 * CryptGenKey (ADVAPI32.@)
1170 * Generates a random cryptographic session key or a pub/priv key pair.
1173 * hProv [I] Handle to a CSP.
1174 * Algid [I] Algorithm to use to make key.
1175 * dwFlags [I] Specifies type of key to make.
1176 * phKey [I] Address of the handle to which the new key is copied.
1182 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1184 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1187 TRACE("(0x%lx, %d, %08ld, %p)\n", hProv, Algid, dwFlags, phKey);
1190 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1191 if (!phKey || !prov || prov->dwMagic != MAGIC_CRYPTPROV)
1192 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1193 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1194 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1196 key->pProvider = prov;
1198 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1200 *phKey = (HCRYPTKEY)key;
1209 /******************************************************************************
1210 * CryptGetDefaultProviderA (ADVAPI32.@)
1212 * Finds the default CSP of a certain provider type.
1215 * dwProvType [I] Provider type to look for.
1216 * pdwReserved [I] Reserved for future use and must be NULL.
1217 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1218 * pszProvName [O] Name of the default CSP.
1219 * pcbProvName [I/O] Size of pszProvName
1226 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1227 * memory allocation purposes on return.
1229 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1230 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1236 if (pdwReserved || !pcbProvName)
1237 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1238 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1239 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1240 if (dwProvType > 999)
1241 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1242 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1243 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1244 if (RegOpenKeyA((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1246 CRYPT_Free(keyname);
1247 CRYPT_ReturnLastError(NTE_PROV_TYPE_NOT_DEF);
1249 CRYPT_Free(keyname);
1251 result = RegQueryValueExA(hKey, "Name", NULL, NULL, pszProvName, pcbProvName);
1254 if (result != ERROR_MORE_DATA)
1255 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1257 SetLastError(result);
1266 /******************************************************************************
1267 * CryptGetDefaultProviderW (ADVAPI32.@)
1269 * see CryptGetDefaultProviderA
1271 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1272 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1278 TRACE("(%ld, %p, %08ld, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1280 strlen = *pcbProvName / sizeof(WCHAR);
1281 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1282 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1283 ret = CryptGetDefaultProviderA(dwProvType, pdwReserved, dwFlags, str, &strlen);
1286 CRYPT_ANSIToUnicode(str, &pszProvName, *pcbProvName);
1289 *pcbProvName = strlen * sizeof(WCHAR);
1293 /******************************************************************************
1294 * CryptGetHashParam (ADVAPI32.@)
1296 * Retrieves data that controls the operations of a hash object.
1299 * hHash [I] Handle of the hash object to question.
1300 * dwParam [I] Query type.
1301 * pbData [O] Buffer that receives the value data.
1302 * pdwDataLen [I/O] Size of the pbData buffer.
1303 * dwFlags [I] Reserved for future use and must be zero.
1310 * If pbData is NULL, pdwDataLen will contain the length required.
1312 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1313 DWORD *pdwDataLen, DWORD dwFlags)
1316 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1318 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1320 if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1321 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1323 prov = hash->pProvider;
1324 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1325 pbData, pdwDataLen, dwFlags);
1328 /******************************************************************************
1329 * CryptGetKeyParam (ADVAPI32.@)
1331 * Retrieves data that controls the operations of a key.
1334 * hKey [I] Handle to they key in question.
1335 * dwParam [I] Specifies query type.
1336 * pbData [O] Sequence of bytes to receive data.
1337 * pdwDataLen [I/O] Size of pbData.
1338 * dwFlags [I] Reserved for future use and must be zero.
1345 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1347 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1348 DWORD *pdwDataLen, DWORD dwFlags)
1351 PCRYPTKEY key = (PCRYPTKEY)hKey;
1353 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1355 if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1356 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1358 prov = key->pProvider;
1359 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1360 pbData, pdwDataLen, dwFlags);
1363 /******************************************************************************
1364 * CryptGetProvParam (ADVAPI32.@)
1366 * Retrieves parameters that control the operations of a CSP.
1369 * hProv [I] Handle of the CSP in question.
1370 * dwParam [I] Specifies query type.
1371 * pbData [O] Buffer to receive the data.
1372 * pdwDataLen [I/O] Size of pbData.
1373 * dwFlags [I] see MSDN Docs.
1380 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1382 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1383 DWORD *pdwDataLen, DWORD dwFlags)
1385 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1387 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1389 if (!prov || prov->dwMagic != MAGIC_CRYPTPROV)
1390 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1392 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1395 /******************************************************************************
1396 * CryptGetUserKey (ADVAPI32.@)
1398 * Gets a handle of one of a user's two public/private key pairs.
1401 * hProv [I] Handle of a CSP.
1402 * dwKeySpec [I] Private key to use.
1403 * phUserKey [O] Pointer to the handle of the retrieved keys.
1409 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1411 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1414 TRACE("(0x%lx, %ld, %p)\n", hProv, dwKeySpec, phUserKey);
1417 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1418 if (!phUserKey || prov->dwMagic != MAGIC_CRYPTPROV)
1419 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1420 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1421 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1423 key->pProvider = prov;
1425 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1427 *phUserKey = (HCRYPTKEY)key;
1437 /******************************************************************************
1438 * CryptHashData (ADVAPI32.@)
1440 * Adds data to a hash object.
1443 * hHash [I] Handle of the hash object.
1444 * pbData [I] Buffer of data to be hashed.
1445 * dwDataLen [I] Number of bytes to add.
1446 * dwFlags [I] Can be CRYPT_USERDATA
1452 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1454 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1457 TRACE("(0x%lx, %p, %ld, %08ld)\n", hHash, pbData, dwDataLen, dwFlags);
1460 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1461 if (!pbData || !dwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1462 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1464 prov = hash->pProvider;
1465 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1468 /******************************************************************************
1469 * CryptHashSessionKey (ADVAPI32.@)
1472 * hHash [I] Handle to the hash object.
1473 * hKey [I] Handle to the key to be hashed.
1474 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1480 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1482 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1483 PCRYPTKEY key = (PCRYPTKEY)hKey;
1486 TRACE("(0x%lx, 0x%lx, %08ld)\n", hHash, hKey, dwFlags);
1489 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1491 if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1492 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1494 prov = hash->pProvider;
1495 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1498 /******************************************************************************
1499 * CryptImportKey (ADVAPI32.@)
1502 * hProv [I] Handle of a CSP.
1503 * pbData [I] Contains the key to be imported.
1504 * dwDataLen [I] Length of the key.
1505 * hPubKey [I] Cryptographic key that decrypts pdData
1506 * dwFlags [I] Used only with a public/private key pair.
1507 * phKey [O] Imported key.
1513 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, BYTE *pbData, DWORD dwDataLen,
1514 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1516 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1517 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1519 TRACE("(0x%lx, %p, %ld, 0x%lx, %08ld, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1521 if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV)
1522 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1524 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1525 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1527 importkey->pProvider = prov;
1528 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1529 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1531 *phKey = (HCRYPTKEY)importkey;
1535 CRYPT_Free(importkey);
1539 /******************************************************************************
1540 * CryptSignHashW (ADVAPI32.@)
1545 * hHash [I] Handle of the hash object to be signed.
1546 * dwKeySpec [I] Private key to use.
1547 * sDescription [I] Should be NULL.
1548 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1549 * pbSignature [O] Buffer of the signature data.
1550 * pdwSigLen [I/O] Size of the pbSignature buffer.
1557 * Because of security flaws sDescription should not be used and should thus be
1558 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1561 BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription,
1562 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1564 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1567 TRACE("(0x%lx, %ld, %s, %08ld, %p, %p)\n",
1568 hHash, dwKeySpec, debugstr_w(sDescription), dwFlags, pbSignature, pdwSigLen);
1571 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1572 if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1573 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1575 prov = hash->pProvider;
1576 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, sDescription,
1577 dwFlags, pbSignature, pdwSigLen);
1580 /******************************************************************************
1581 * CryptSignHashA (ADVAPI32.@)
1583 * ASCII version of CryptSignHashW
1585 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1586 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1588 LPWSTR wsDescription;
1591 TRACE("(0x%lx, %ld, %s, %08ld, %p, %p)\n",
1592 hHash, dwKeySpec, debugstr_a(sDescription), dwFlags, pbSignature, pdwSigLen);
1594 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1595 result = CryptSignHashW(hHash, dwKeySpec, wsDescription, dwFlags, pbSignature, pdwSigLen);
1596 if (wsDescription) CRYPT_Free(wsDescription);
1601 /******************************************************************************
1602 * CryptSetHashParam (ADVAPI32.@)
1604 * Customizes the operations of a hash object.
1607 * hHash [I] Handle of the hash object to set parameters.
1608 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1609 * pbData [I] Value data buffer.
1610 * dwFlags [I] Reserved for future use and must be zero.
1616 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1619 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1621 TRACE("(0x%lx, %ld, %p, %08ld)\n", hHash, dwParam, pbData, dwFlags);
1623 if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
1624 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1626 prov = hash->pProvider;
1627 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1628 dwParam, pbData, dwFlags);
1631 /******************************************************************************
1632 * CryptSetKeyParam (ADVAPI32.@)
1634 * Customizes a session key's operations.
1637 * hKey [I] Handle to the key to set values.
1638 * dwParam [I] See MSDN Doc.
1639 * pbData [I] Buffer of values to set.
1640 * dwFlags [I] Only used when dwParam == KP_ALGID.
1646 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1649 PCRYPTKEY key = (PCRYPTKEY)hKey;
1651 TRACE("(0x%lx, %ld, %p, %08ld)\n", hKey, dwParam, pbData, dwFlags);
1653 if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1654 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1656 prov = key->pProvider;
1657 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1658 dwParam, pbData, dwFlags);
1661 /******************************************************************************
1662 * CryptSetProviderA (ADVAPI32.@)
1664 * Specifies the current user's default CSP.
1667 * pszProvName [I] Name of the new default CSP.
1668 * dwProvType [I] Provider type of the CSP.
1674 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
1676 TRACE("(%s, %ld)\n", pszProvName, dwProvType);
1677 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1680 /******************************************************************************
1681 * CryptSetProviderW (ADVAPI32.@)
1683 * See CryptSetProviderA
1685 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
1687 TRACE("(%s, %ld)\n", debugstr_w(pszProvName), dwProvType);
1688 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1691 /******************************************************************************
1692 * CryptSetProviderExA (ADVAPI32.@)
1694 * Specifies the default CSP.
1697 * pszProvName [I] Name of the new default CSP.
1698 * dwProvType [I] Provider type of the CSP.
1699 * pdwReserved [I] Reserved for future use and must be NULL.
1700 * dwFlags [I] See MSDN Doc.
1706 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1708 HKEY hProvKey, hTypeKey;
1711 TRACE("(%s, %ld, %p, %08ld)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
1713 if (!pszProvName || pdwReserved)
1714 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1715 if (dwProvType > MAXPROVTYPES)
1716 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1717 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
1718 || dwFlags == CRYPT_DELETE_DEFAULT)
1719 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1721 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
1722 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1723 if (RegOpenKeyA((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1724 keyname, &hTypeKey))
1726 CRYPT_Free(keyname);
1727 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1729 CRYPT_Free(keyname);
1731 if (dwFlags & CRYPT_DELETE_DEFAULT)
1733 RegDeleteValueA(hTypeKey, "Name");
1737 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
1739 RegCloseKey(hTypeKey);
1740 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1742 if (RegOpenKeyA((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1743 keyname, &hProvKey))
1745 CRYPT_Free(keyname);
1746 RegCloseKey(hTypeKey);
1747 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1749 CRYPT_Free(keyname);
1751 if (RegSetValueExA(hTypeKey, "Name", 0, REG_SZ, pszProvName, strlen(pszProvName) + 1))
1753 RegCloseKey(hTypeKey);
1754 RegCloseKey(hProvKey);
1758 RegCloseKey(hProvKey);
1760 RegCloseKey(hTypeKey);
1765 /******************************************************************************
1766 * CryptSetProviderExW (ADVAPI32.@)
1768 * See CryptSetProviderExA
1770 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1775 TRACE("(%s, %ld, %p, %08ld)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
1777 if (CRYPT_UnicodeToANSI(pszProvName, &str, -1))
1779 ret = CryptSetProviderExA(str, dwProvType, pdwReserved, dwFlags);
1785 /******************************************************************************
1786 * CryptSetProvParam (ADVAPI32.@)
1788 * Customizes the operations of a CSP.
1791 * hProv [I] Handle of a CSP.
1792 * dwParam [I] See MSDN Doc.
1793 * pbData [I] Buffer that contains a value to set as a parameter.
1794 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
1800 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1802 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1804 TRACE("(0x%lx, %ld, %p, %08ld)\n", hProv, dwParam, pbData, dwFlags);
1807 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1808 if (prov->dwMagic != MAGIC_CRYPTPROV)
1809 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1810 if (dwFlags & PP_USE_HARDWARE_RNG)
1812 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
1813 FIXME("\tLetting the CSP decide.\n");
1815 if (dwFlags & PP_CLIENT_HWND)
1817 /* FIXME: Should verify the parameter */
1818 if (pbData /* && IsWindow((HWND)pbData) */)
1820 crypt_hWindow = (HWND)(pbData);
1823 SetLastError(ERROR_INVALID_PARAMETER);
1827 /* All other flags go to the CSP */
1828 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
1831 /******************************************************************************
1832 * CryptVerifySignatureW (ADVAPI32.@)
1834 * Verifies the signature of a hash object.
1837 * hHash [I] Handle of the hash object to verify.
1838 * pbSignature [I] Signature data to verify.
1839 * dwSigLen [I] Size of pbSignature.
1840 * hPubKey [I] Handle to the public key to authenticate signature.
1841 * sDescription [I] Should be NULL.
1842 * dwFlags [I] See MSDN doc.
1849 * Because of security flaws sDescription should not be used and should thus be
1850 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1853 BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
1854 HCRYPTKEY hPubKey, LPCWSTR sDescription, DWORD dwFlags)
1856 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1857 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
1860 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08ld)\n", hHash, pbSignature,
1861 dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
1864 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1865 if (!pbSignature || !dwSigLen ||
1866 !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
1867 !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
1869 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1872 prov = hash->pProvider;
1873 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
1874 key->hPrivate, sDescription, dwFlags);
1877 /******************************************************************************
1878 * CryptVerifySignatureA (ADVAPI32.@)
1880 * ASCII version of CryptVerifySignatureW
1882 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
1883 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
1885 LPWSTR wsDescription;
1888 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08ld)\n", hHash, pbSignature,
1889 dwSigLen, hPubKey, debugstr_a(sDescription), dwFlags);
1891 CRYPT_ANSIToUnicode(sDescription, &wsDescription, -1);
1892 result = CryptVerifySignatureW(hHash, pbSignature, dwSigLen, hPubKey, wsDescription, dwFlags);
1893 if (wsDescription) CRYPT_Free(wsDescription);
1898 /******************************************************************************
1899 * SystemFunction036 (ADVAPI32.@)
1901 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
1904 * pbBufer [O] Pointer to memory to receive random bytes.
1905 * dwLen [I] Number of random bytes to fetch.
1908 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen)
1912 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
1913 * quality for cryptographic applications. /dev/random is much better,
1914 * but it blocks if the kernel has not yet collected enough entropy for
1915 * the request, which will suspend the calling thread for an indefinite
1916 * amount of time. */
1917 dev_random = open("/dev/urandom", O_RDONLY);
1918 if (dev_random != -1)
1920 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
1927 SetLastError(NTE_FAIL);
1932 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
1936 /******************************************************************************
1937 * SystemFunction040 (ADVAPI32.@)
1939 * MSDN documents this function as RtlEncryptMemory.
1942 * memory [I/O] Pointer to memory to encrypt.
1943 * length [I] Length of region to encrypt in bytes.
1944 * flags [I] Control whether other processes are able to decrypt the memory.
1945 * RTL_ENCRYPT_OPTION_SAME_PROCESS
1946 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
1947 * RTL_ENCRYPT_OPTION_SAME_LOGON
1950 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
1951 * If flags are specified when encrypting, the same flag value must be given
1952 * when decrypting the memory.
1954 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags)
1956 FIXME("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory, length, flags);
1957 return STATUS_SUCCESS;
1960 /******************************************************************************
1961 * SystemFunction041 (ADVAPI32.@)
1963 * MSDN documents this function as RtlDecryptMemory.
1966 * memory [I/O] Pointer to memory to decrypt.
1967 * length [I] Length of region to decrypt in bytes.
1968 * flags [I] Control whether other processes are able to decrypt the memory.
1969 * RTL_ENCRYPT_OPTION_SAME_PROCESS
1970 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
1971 * RTL_ENCRYPT_OPTION_SAME_LOGON
1974 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
1975 * If flags are specified when encrypting, the same flag value must be given
1976 * when decrypting the memory.
1978 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags)
1980 FIXME("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory, length, flags);
1981 return STATUS_SUCCESS;