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
25 * - Signature checking
32 #include "wine/unicode.h"
41 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
47 HWND crypt_hWindow = 0;
49 #define CRYPT_ReturnLastError(err) {SetLastError(err); return FALSE;}
51 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
52 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))
54 static inline PSTR CRYPT_GetProvKeyName(PCSTR pProvName)
56 PCSTR KEYSTR = "Software\\Microsoft\\Cryptography\\Defaults\\Provider\\";
59 keyname = CRYPT_Alloc(strlen(KEYSTR) + strlen(pProvName) +1);
62 strcpy(keyname, KEYSTR);
63 strcpy(keyname + strlen(KEYSTR), pProvName);
65 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
69 static inline PSTR CRYPT_GetTypeKeyName(DWORD dwType, BOOL user)
71 PCSTR MACHINESTR = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
72 PCSTR USERSTR = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
76 keyname = CRYPT_Alloc( (user ? strlen(USERSTR) : strlen(MACHINESTR)) +1);
79 user ? strcpy(keyname, USERSTR) : strcpy(keyname, MACHINESTR);
80 ptr = keyname + strlen(keyname);
81 *(--ptr) = (dwType % 10) + '0';
82 *(--ptr) = ((dwType / 10) % 10) + '0';
83 *(--ptr) = (dwType / 100) + '0';
85 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
89 /* CRYPT_UnicodeTOANSI
90 * wstr - unicode string
91 * str - pointer to ANSI string
92 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
94 * returns TRUE if unsuccessfull, FALSE otherwise.
95 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
97 static inline BOOL CRYPT_UnicodeToANSI(LPCWSTR wstr, LPSTR* str, int strsize)
106 count = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
107 count = count < strsize ? count : strsize;
109 *str = CRYPT_Alloc(count * sizeof(CHAR));
112 WideCharToMultiByte(CP_ACP, 0, wstr, -1, *str, count, NULL, NULL);
115 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
119 /* CRYPT_ANSITOUnicode
121 * wstr - pointer to unicode string
122 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
124 static inline BOOL CRYPT_ANSIToUnicode(LPCSTR str, LPWSTR* wstr, int wstrsize)
133 wcount = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
134 wcount = wcount < wstrsize/sizeof(WCHAR) ? wcount : wstrsize/sizeof(WCHAR);
136 *wstr = CRYPT_Alloc(wcount * sizeof(WCHAR));
139 MultiByteToWideChar(CP_ACP, 0, str, -1, *wstr, wcount);
142 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
146 /* These next 2 functions are used by the VTableProvStruc structure */
147 static BOOL CALLBACK CRYPT_VerifyImage(LPCSTR lpszImage, BYTE* pData)
149 if (!lpszImage || !pData)
151 SetLastError(ERROR_INVALID_PARAMETER);
155 FIXME("(%s, %p): not verifying image\n", lpszImage, pData);
160 static BOOL CALLBACK CRYPT_ReturnhWnd(HWND *phWnd)
164 *phWnd = crypt_hWindow;
168 #define CRYPT_GetProvFunc(name) \
169 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
170 #define CRYPT_GetProvFuncOpt(name) \
171 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
172 PCRYPTPROV CRYPT_LoadProvider(PSTR pImage)
175 DWORD errorcode = ERROR_NOT_ENOUGH_MEMORY;
177 if ( !(provider = CRYPT_Alloc(sizeof(CRYPTPROV))) ) goto error;
178 if ( !(provider->pFuncs = CRYPT_Alloc(sizeof(PROVFUNCS))) ) goto error;
179 if ( !(provider->pVTable = CRYPT_Alloc(sizeof(VTableProvStruc))) ) goto error;
180 if ( !(provider->hModule = LoadLibraryA(pImage)) )
182 errorcode = (GetLastError() == ERROR_FILE_NOT_FOUND) ? NTE_PROV_DLL_NOT_FOUND : NTE_PROVIDER_DLL_FAIL;
183 FIXME("Failed to load dll %s\n", debugstr_a(pImage));
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 if (provider->hModule)
232 FreeLibrary(provider->hModule);
233 CRYPT_Free(provider->pVTable);
234 CRYPT_Free(provider->pFuncs);
235 CRYPT_Free(provider);
239 #undef CRYPT_GetProvFunc
240 #undef CRYPT_GetProvFuncOpt
243 /******************************************************************************
244 * CryptAcquireContextA (ADVAPI32.@)
245 * Acquire a crypto provider context handle.
248 * phProv: Pointer to HCRYPTPROV for the output.
249 * pszContainer: Key Container Name
250 * pszProvider: Cryptographic Service Provider Name
251 * dwProvType: Crypto provider type to get a handle.
252 * dwFlags: flags for the operation
254 * RETURNS TRUE on success, FALSE on failure.
256 BOOL WINAPI CryptAcquireContextA (HCRYPTPROV *phProv, LPCSTR pszContainer,
257 LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
259 PCRYPTPROV pProv = NULL;
261 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 r = RegQueryValueExA(key, "Signature", NULL, &keytype, NULL, &len);
387 if ( r == ERROR_SUCCESS && keytype == REG_BINARY )
389 if (!(signature = CRYPT_Alloc(len)))
392 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
395 r = RegQueryValueExA(key, "Signature", NULL, NULL, signature, &len);
396 if ( r != ERROR_SUCCESS )
398 TRACE("error %ld reading 'Signature'\n", r );
400 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
406 r = RegQueryValueExA(key, "SigInFile", NULL, &keytype, NULL, &len);
407 if (r != ERROR_SUCCESS)
409 TRACE("error %ld reading size of 'SigInFile'\n", r );
411 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
416 /* FIXME: The presence of the SigInFile value indicates the
417 * provider's signature is in its resources, so need to read it.
418 * But since CRYPT_VerifyImage is stubbed, provide any old thing
421 if (!(signature = CRYPT_Alloc(1)))
424 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
430 len = ExpandEnvironmentStringsA(temp, NULL, 0);
431 if ( !(imagepath = CRYPT_Alloc(len)) )
433 CRYPT_Free(signature);
434 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
437 if (!ExpandEnvironmentStringsA(temp, imagepath, len))
439 CRYPT_Free(signature);
440 /* ExpandEnvironmentStrings will call SetLastError */
444 if (!CRYPT_VerifyImage(imagepath, signature))
446 CRYPT_Free(signature);
447 SetLastError(NTE_SIGNATURE_FILE_BAD);
450 pProv = CRYPT_LoadProvider(imagepath);
451 CRYPT_Free(signature);
453 /* CRYPT_LoadProvider calls SetLastError */
456 pProv->pVTable->dwProvType = dwProvType;
457 pProv->pVTable->pszProvName = provname;
458 if (pProv->pFuncs->pCPAcquireContext(&pProv->hPrivate, (CHAR*)pszContainer, dwFlags, pProv->pVTable))
460 /* MSDN: When this flag is set, the value returned in phProv is undefined,
461 * and thus, the CryptReleaseContext function need not be called afterwards.
462 * Therefore, we must clean up everything now.
464 if (dwFlags & CRYPT_DELETEKEYSET)
466 FreeLibrary(pProv->hModule);
467 CRYPT_Free(provname);
468 CRYPT_Free(pProv->pFuncs);
471 *phProv = (HCRYPTPROV)pProv;
474 CRYPT_Free(imagepath);
477 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
482 FreeLibrary(pProv->hModule);
484 CRYPT_Free(pProv->pVTable);
486 CRYPT_Free(pProv->pFuncs);
490 CRYPT_Free(provname);
494 CRYPT_Free(imagepath);
498 /******************************************************************************
499 * CryptAcquireContextW (ADVAPI32.@)
501 * see CryptAcquireContextA
503 BOOL WINAPI CryptAcquireContextW (HCRYPTPROV *phProv, LPCWSTR pszContainer,
504 LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
506 PSTR pProvider = NULL, pContainer = NULL;
509 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv, debugstr_w(pszContainer),
510 debugstr_w(pszProvider), dwProvType, dwFlags);
512 if ( !CRYPT_UnicodeToANSI(pszContainer, &pContainer, -1) )
513 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
514 if ( !CRYPT_UnicodeToANSI(pszProvider, &pProvider, -1) )
516 CRYPT_Free(pContainer);
517 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
520 ret = CryptAcquireContextA(phProv, pContainer, pProvider, dwProvType, dwFlags);
523 CRYPT_Free(pContainer);
525 CRYPT_Free(pProvider);
530 /******************************************************************************
531 * CryptContextAddRef (ADVAPI32.@)
533 * Increases reference count of a cryptographic service provider handle
537 * hProv [I] Handle to the CSP whose reference is being incremented.
538 * pdwReserved [IN] Reserved for future use and must be NULL.
539 * dwFlags [I] Reserved for future use and must be NULL.
545 BOOL WINAPI CryptContextAddRef (HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags)
547 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
549 TRACE("(0x%lx, %p, %08lx)\n", hProv, pdwReserved, dwFlags);
553 SetLastError(NTE_BAD_UID);
561 /******************************************************************************
562 * CryptReleaseContext (ADVAPI32.@)
564 * Releases the handle of a CSP. Reference count is decreased.
567 * hProv [I] Handle of a CSP.
568 * dwFlags [I] Reserved for future use and must be NULL.
574 BOOL WINAPI CryptReleaseContext (HCRYPTPROV hProv, DWORD dwFlags)
576 PCRYPTPROV pProv = (PCRYPTPROV)hProv;
579 TRACE("(0x%lx, %08ld)\n", hProv, dwFlags);
583 SetLastError(NTE_BAD_UID);
588 if (pProv->refcount <= 0)
590 ret = pProv->pFuncs->pCPReleaseContext(pProv->hPrivate, dwFlags);
591 FreeLibrary(pProv->hModule);
593 CRYPT_Free(pProv->pVTable->pContextInfo);
595 CRYPT_Free(pProv->pVTable->pszProvName);
596 CRYPT_Free(pProv->pVTable);
597 CRYPT_Free(pProv->pFuncs);
603 /******************************************************************************
604 * CryptGenRandom (ADVAPI32.@)
606 * Fills a buffer with cryptographically random bytes.
609 * hProv [I] Handle of a CSP.
610 * dwLen [I] Number of bytes to generate.
611 * pbBuffer [I/O] Buffer to contain random bytes.
618 * pdBuffer must be at least dwLen bytes long.
620 BOOL WINAPI CryptGenRandom (HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
622 PCRYPTPROV prov = (PCRYPTPROV)hProv;
624 TRACE("(0x%lx, %ld, %p)\n", hProv, dwLen, pbBuffer);
627 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
629 return prov->pFuncs->pCPGenRandom(prov->hPrivate, dwLen, pbBuffer);
632 /******************************************************************************
633 * CryptCreateHash (ADVAPI32.@)
635 * Initiates the hashing of a stream of data.
638 * hProv [I] Handle of a CSP.
639 * Algid [I] Identifies the hash algorithm to use.
640 * hKey [I] Key for the hash (if required).
641 * dwFlags [I] Reserved for future use and must be NULL.
642 * phHash [O] Address of the future handle to the new hash object.
649 * If the algorithm is a keyed hash, hKey is the key.
651 BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
652 DWORD dwFlags, HCRYPTHASH *phHash)
654 PCRYPTPROV prov = (PCRYPTPROV)hProv;
655 PCRYPTKEY key = (PCRYPTKEY)hKey;
658 TRACE("(0x%lx, 0x%x, 0x%lx, %08lx, %p)\n", hProv, Algid, hKey, dwFlags, phHash);
661 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
663 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
665 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
666 if ( !(hash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
667 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
669 hash->pProvider = prov;
671 if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
672 key ? key->hPrivate : 0, 0, &hash->hPrivate))
674 *phHash = (HCRYPTHASH)hash;
684 /******************************************************************************
685 * CryptDecrypt (ADVAPI32.@)
687 * Decrypts data encrypted by CryptEncrypt.
690 * hKey [I] Handle to the decryption key.
691 * hHash [I] Handle to a hash object.
692 * Final [I] TRUE if this is the last section to be decrypted.
693 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
694 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
696 * pdwDataLen [I/O] Length of pbData before and after the call.
702 BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
703 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
706 PCRYPTKEY key = (PCRYPTKEY)hKey;
707 PCRYPTHASH hash = (PCRYPTHASH)hHash;
709 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
711 if (!key || !pbData || !pdwDataLen)
712 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
714 prov = key->pProvider;
715 return prov->pFuncs->pCPDecrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
716 Final, dwFlags, pbData, pdwDataLen);
719 /******************************************************************************
720 * CryptDeriveKey (ADVAPI32.@)
722 * Generates session keys derived from a base data value.
725 * hProv [I] Handle to a CSP.
726 * Algid [I] Identifies the symmetric encryption algorithm to use.
727 * hBaseData [I] Handle to a hash object.
728 * dwFlags [I] Type of key to generate.
729 * phKey [I/O] Address of the newly generated key.
735 BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
736 DWORD dwFlags, HCRYPTKEY *phKey)
738 PCRYPTPROV prov = (PCRYPTPROV)hProv;
739 PCRYPTHASH hash = (PCRYPTHASH)hBaseData;
742 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08lx, %p)\n", hProv, Algid, hBaseData, dwFlags, phKey);
745 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
747 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
748 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
749 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
751 key->pProvider = prov;
752 if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
754 *phKey = (HCRYPTKEY)key;
764 /******************************************************************************
765 * CryptDestroyHash (ADVAPI32.@)
767 * Destroys the hash object referenced by hHash.
770 * hHash [I] Handle of the hash object to be destroyed.
776 BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
778 PCRYPTHASH hash = (PCRYPTHASH)hHash;
782 TRACE("(0x%lx)\n", hHash);
785 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
787 prov = hash->pProvider;
788 ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
793 /******************************************************************************
794 * CryptDestroyKey (ADVAPI32.@)
796 * Releases the handle referenced by hKey.
799 * hKey [I] Handle of the key to be destroyed.
805 BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
807 PCRYPTKEY key = (PCRYPTKEY)hKey;
811 TRACE("(0x%lx)\n", hKey);
814 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
816 prov = key->pProvider;
817 ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
822 /******************************************************************************
823 * CryptDuplicateHash (ADVAPI32.@)
828 * hHash [I] Handle to the hash to be copied.
829 * pdwReserved [I] Reserved for future use and must be zero.
830 * dwFlags [I] Reserved for future use and must be zero.
831 * phHash [O] Address of the handle to receive the copy.
837 BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
838 DWORD dwFlags, HCRYPTHASH *phHash)
841 PCRYPTHASH orghash, newhash;
843 TRACE("(0x%lx, %p, %08ld, %p)\n", hHash, pdwReserved, dwFlags, phHash);
845 orghash = (PCRYPTHASH)hHash;
846 if (!orghash || pdwReserved || !phHash)
847 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
849 prov = orghash->pProvider;
850 if (!prov->pFuncs->pCPDuplicateHash)
851 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
853 if ( !(newhash = CRYPT_Alloc(sizeof(CRYPTHASH))) )
854 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
856 newhash->pProvider = prov;
857 if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
859 *phHash = (HCRYPTHASH)newhash;
866 /******************************************************************************
867 * CryptDuplicateKey (ADVAPI32.@)
869 * Duplicate a key and the key's state.
872 * hKey [I] Handle of the key to copy.
873 * pdwReserved [I] Reserved for future use and must be NULL.
874 * dwFlags [I] Reserved for future use and must be zero.
875 * phKey [I] Address of the handle to the duplicated key.
881 BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags, HCRYPTKEY *phKey)
884 PCRYPTKEY orgkey, newkey;
886 TRACE("(0x%lx, %p, %08ld, %p)\n", hKey, pdwReserved, dwFlags, phKey);
888 orgkey = (PCRYPTKEY)hKey;
889 if (!orgkey || pdwReserved || !phKey)
890 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
892 prov = orgkey->pProvider;
893 if (!prov->pFuncs->pCPDuplicateKey)
894 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED);
896 if ( !(newkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
897 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
899 newkey->pProvider = prov;
900 if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
902 *phKey = (HCRYPTKEY)newkey;
909 /******************************************************************************
910 * CryptEncrypt (ADVAPI32.@)
915 * hKey [I] Handle to the enryption key.
916 * hHash [I] Handle to a hash object.
917 * Final [I] TRUE if this is the last section to encrypt.
918 * dwFlags [I] Can be CRYPT_OAEP.
919 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
920 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
921 * encrypted data after call.
922 * dwBufLen [I] Length of the input pbData buffer.
929 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
930 * required for the returned data in pdwDataLen.
932 BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
933 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
936 PCRYPTKEY key = (PCRYPTKEY)hKey;
937 PCRYPTHASH hash = (PCRYPTHASH)hHash;
939 TRACE("(0x%lx, 0x%lx, %d, %08ld, %p, %p, %ld)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
941 if (!key || !pdwDataLen)
942 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
944 prov = key->pProvider;
945 return prov->pFuncs->pCPEncrypt(prov->hPrivate, key->hPrivate, hash ? hash->hPrivate : 0,
946 Final, dwFlags, pbData, pdwDataLen, dwBufLen);
949 /******************************************************************************
950 * CryptEnumProvidersW (ADVAPI32.@)
952 * Returns the next availabe CSP.
955 * dwIndex [I] Index of the next provider to be enumerated.
956 * pdwReserved [I] Reserved for future use and must be NULL.
957 * dwFlags [I] Reserved for future use and must be zero.
958 * pdwProvType [O] DWORD designating the type of the provider.
959 * pszProvName [O] Buffer that receives data from the provider.
960 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
961 * of bytes stored in the buffer on return.
968 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
969 * for memory allocation purposes.
971 BOOL WINAPI CryptEnumProvidersW (DWORD dwIndex, DWORD *pdwReserved,
972 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszProvName, DWORD *pcbProvName)
976 static const WCHAR providerW[] = {
977 'S','o','f','t','w','a','r','e','\\',
978 'M','i','c','r','o','s','o','f','t','\\',
979 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
980 'D','e','f','a','u','l','t','s','\\',
981 'P','r','o','v','i','d','e','r',0
984 static const WCHAR typeW[] = {'T','y','p','e',0};
986 TRACE("(%ld, %p, %ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
987 pdwProvType, pszProvName, pcbProvName);
989 if (pdwReserved || !pcbProvName) CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
990 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
992 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, providerW, &hKey))
993 CRYPT_ReturnLastError(NTE_FAIL);
1000 RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &numkeys, pcbProvName,
1001 NULL, NULL, NULL, NULL, NULL, NULL);
1003 if (!(provNameW = CRYPT_Alloc(*pcbProvName * sizeof(WCHAR))))
1004 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1006 RegEnumKeyExW(hKey, dwIndex, provNameW, pcbProvName, NULL, NULL, NULL, NULL);
1008 *pcbProvName *= sizeof(WCHAR);
1010 CRYPT_Free(provNameW);
1012 if (dwIndex >= numkeys)
1013 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
1015 DWORD size = sizeof(DWORD);
1019 result = RegEnumKeyW(hKey, dwIndex, pszProvName, *pcbProvName / sizeof(WCHAR));
1021 CRYPT_ReturnLastError(result);
1022 if (RegOpenKeyW(hKey, pszProvName, &subkey))
1024 if (RegQueryValueExW(subkey, typeW, NULL, NULL, (BYTE*)pdwProvType, &size))
1026 RegCloseKey(subkey);
1032 /******************************************************************************
1033 * CryptEnumProvidersA (ADVAPI32.@)
1035 * see CryptEnumProvidersW
1037 BOOL WINAPI CryptEnumProvidersA (DWORD dwIndex, DWORD *pdwReserved,
1038 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszProvName, DWORD *pcbProvName)
1042 BOOL ret; /* = FALSE; */
1044 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1045 pdwProvType, pszProvName, pcbProvName);
1047 strlen = *pcbProvName * sizeof(WCHAR);
1048 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1049 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1050 ret = CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1053 CRYPT_UnicodeToANSI(str, &pszProvName, *pcbProvName);
1056 *pcbProvName = strlen / sizeof(WCHAR); /* FIXME: not correct */
1060 /******************************************************************************
1061 * CryptEnumProviderTypesA (ADVAPI32i.@)
1063 * Retrieves the next type of CSP supported.
1066 * dwIndex [I] Index of the next provider to be enumerated.
1067 * pdwReserved [I] Reserved for future use and must be NULL.
1068 * dwFlags [I] Reserved for future use and must be zero.
1069 * pdwProvType [O] DWORD designating the type of the provider.
1070 * pszTypeName [O] Buffer that receives data from the provider type.
1071 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1072 * of bytes stored in the buffer on return.
1079 * If pszTypeName is NULL, CryptEnumProviderTypesA sets the size of the name
1080 * for memory allocation purposes.
1082 BOOL WINAPI CryptEnumProviderTypesA (DWORD dwIndex, DWORD *pdwReserved,
1083 DWORD dwFlags, DWORD *pdwProvType, LPSTR pszTypeName, DWORD *pcbTypeName)
1086 DWORD keylen, numkeys, dwType;
1090 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex, pdwReserved,
1091 dwFlags, pdwProvType, pszTypeName, pcbTypeName);
1093 if (pdwReserved || !pdwProvType || !pcbTypeName)
1094 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1095 if (dwFlags) CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1097 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
1100 RegQueryInfoKeyA(hKey, NULL, NULL, NULL, &numkeys, &keylen, NULL, NULL, NULL, NULL, NULL, NULL);
1101 if (dwIndex >= numkeys)
1102 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS);
1104 if ( !(keyname = CRYPT_Alloc(keylen)) )
1105 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1106 if ( RegEnumKeyA(hKey, dwIndex, keyname, keylen) ) {
1107 CRYPT_Free(keyname);
1110 RegOpenKeyA(hKey, keyname, &hSubkey);
1111 ch = keyname + strlen(keyname);
1112 /* Convert "Type 000" to 0, etc/ */
1113 *pdwProvType = *(--ch) - '0';
1114 *pdwProvType += (*(--ch) - '0') * 10;
1115 *pdwProvType += (*(--ch) - '0') * 100;
1116 CRYPT_Free(keyname);
1118 result = RegQueryValueExA(hSubkey, "TypeName", NULL, &dwType, pszTypeName, pcbTypeName);
1120 CRYPT_ReturnLastError(result);
1122 RegCloseKey(hSubkey);
1127 /******************************************************************************
1128 * CryptEnumProviderTypesW (ADVAPI32.@)
1130 * see CryptEnumProviderTypesA
1132 BOOL WINAPI CryptEnumProviderTypesW (DWORD dwIndex, DWORD *pdwReserved,
1133 DWORD dwFlags, DWORD *pdwProvType, LPWSTR pszTypeName, DWORD *pcbTypeName)
1139 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex, pdwReserved, dwFlags,
1140 pdwProvType, pszTypeName, pcbTypeName);
1141 strlen = *pcbTypeName / sizeof(WCHAR);
1142 if ( pszTypeName && !(str = CRYPT_Alloc(strlen)) )
1143 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1144 ret = CryptEnumProviderTypesA(dwIndex, pdwReserved, dwFlags, pdwProvType, str, &strlen);
1147 CRYPT_ANSIToUnicode(str, &pszTypeName, *pcbTypeName);
1150 *pcbTypeName = strlen * sizeof(WCHAR);
1154 /******************************************************************************
1155 * CryptExportKey (ADVAPI32.@)
1157 * Exports a cryptographic key from a CSP.
1160 * hKey [I] Handle to the key to export.
1161 * hExpKey [I] Handle to a cryptographic key of the end user.
1162 * dwBlobType [I] Type of BLOB to be exported.
1163 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1164 * pbData [O] Buffer to receive BLOB data.
1165 * pdwDataLen [I/O] Specifies the size of pbData.
1172 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1173 * buffer needed to hold the BLOB.
1175 BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
1176 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1179 PCRYPTKEY key = (PCRYPTKEY)hKey, expkey = (PCRYPTKEY)hExpKey;
1181 TRACE("(0x%lx, 0x%lx, %ld, %08ld, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1183 if (!key || !pdwDataLen)
1184 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1186 prov = key->pProvider;
1187 return prov->pFuncs->pCPExportKey(prov->hPrivate, key->hPrivate, expkey ? expkey->hPrivate : 0,
1188 dwBlobType, dwFlags, pbData, pdwDataLen);
1191 /******************************************************************************
1192 * CryptGenKey (ADVAPI32.@)
1194 * Generates a random cryptographic session key or a pub/priv key pair.
1197 * hProv [I] Handle to a CSP.
1198 * Algid [I] Algorithm to use to make key.
1199 * dwFlags [I] Specifies type of key to make.
1200 * phKey [I] Address of the handle to which the new key is copied.
1206 BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1208 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1211 TRACE("(0x%lx, %d, %08ld, %p)\n", hProv, Algid, dwFlags, phKey);
1214 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1216 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1217 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1218 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1220 key->pProvider = prov;
1222 if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
1224 *phKey = (HCRYPTKEY)key;
1233 /******************************************************************************
1234 * CryptGetDefaultProviderA (ADVAPI32.@)
1236 * Finds the default CSP of a certain provider type.
1239 * dwProvType [I] Provider type to look for.
1240 * pdwReserved [I] Reserved for future use and must be NULL.
1241 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1242 * pszProvName [O] Name of the default CSP.
1243 * pcbProvName [I/O] Size of pszProvName
1250 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1251 * memory allocation purposes on return.
1253 BOOL WINAPI CryptGetDefaultProviderA (DWORD dwProvType, DWORD *pdwReserved,
1254 DWORD dwFlags, LPSTR pszProvName, DWORD *pcbProvName)
1260 if (pdwReserved || !pcbProvName)
1261 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1262 if (dwFlags & ~(CRYPT_USER_DEFAULT | CRYPT_MACHINE_DEFAULT))
1263 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1264 if (dwProvType > 999)
1265 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1266 if ( !(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)) )
1267 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1268 if (RegOpenKeyA((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
1270 CRYPT_Free(keyname);
1271 CRYPT_ReturnLastError(NTE_PROV_TYPE_NOT_DEF);
1273 CRYPT_Free(keyname);
1275 result = RegQueryValueExA(hKey, "Name", NULL, NULL, pszProvName, pcbProvName);
1278 if (result != ERROR_MORE_DATA)
1279 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
1281 SetLastError(result);
1290 /******************************************************************************
1291 * CryptGetDefaultProviderW (ADVAPI32.@)
1293 * see CryptGetDefaultProviderA
1295 BOOL WINAPI CryptGetDefaultProviderW (DWORD dwProvType, DWORD *pdwReserved,
1296 DWORD dwFlags, LPWSTR pszProvName, DWORD *pcbProvName)
1302 TRACE("(%ld, %p, %08ld, %p, %p)\n", dwProvType, pdwReserved, dwFlags, pszProvName, pcbProvName);
1304 strlen = *pcbProvName / sizeof(WCHAR);
1305 if ( pszProvName && !(str = CRYPT_Alloc(strlen)) )
1306 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1307 ret = CryptGetDefaultProviderA(dwProvType, pdwReserved, dwFlags, str, &strlen);
1310 CRYPT_ANSIToUnicode(str, &pszProvName, *pcbProvName);
1313 *pcbProvName = strlen * sizeof(WCHAR);
1317 /******************************************************************************
1318 * CryptGetHashParam (ADVAPI32.@)
1320 * Retrieves data that controls the operations of a hash object.
1323 * hHash [I] Handle of the hash object to question.
1324 * dwParam [I] Query type.
1325 * pbData [O] Buffer that receives the value data.
1326 * pdwDataLen [I/O] Size of the pbData buffer.
1327 * dwFlags [I] Reserved for future use and must be zero.
1334 * If pbData is NULL, pdwDataLen will contain the length required.
1336 BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1337 DWORD *pdwDataLen, DWORD dwFlags)
1340 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1342 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
1344 if (!hash || !pdwDataLen)
1345 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1347 prov = hash->pProvider;
1348 return prov->pFuncs->pCPGetHashParam(prov->hPrivate, hash->hPrivate, dwParam,
1349 pbData, pdwDataLen, dwFlags);
1352 /******************************************************************************
1353 * CryptGetKeyParam (ADVAPI32.@)
1355 * Retrieves data that controls the operations of a key.
1358 * hKey [I] Handle to they key in question.
1359 * dwParam [I] Specifies query type.
1360 * pbData [O] Sequence of bytes to receive data.
1361 * pdwDataLen [I/O] Size of pbData.
1362 * dwFlags [I] Reserved for future use and must be zero.
1369 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1371 BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1372 DWORD *pdwDataLen, DWORD dwFlags)
1375 PCRYPTKEY key = (PCRYPTKEY)hKey;
1377 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
1379 if (!key || !pdwDataLen)
1380 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1382 prov = key->pProvider;
1383 return prov->pFuncs->pCPGetKeyParam(prov->hPrivate, key->hPrivate, dwParam,
1384 pbData, pdwDataLen, dwFlags);
1387 /******************************************************************************
1388 * CryptGetProvParam (ADVAPI32.@)
1390 * Retrieves parameters that control the operations of a CSP.
1393 * hProv [I] Handle of the CSP in question.
1394 * dwParam [I] Specifies query type.
1395 * pbData [O] Buffer to receive the data.
1396 * pdwDataLen [I/O] Size of pbData.
1397 * dwFlags [I] see MSDN Docs.
1404 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1406 BOOL WINAPI CryptGetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
1407 DWORD *pdwDataLen, DWORD dwFlags)
1409 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1411 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hProv, dwParam, pbData, pdwDataLen, dwFlags);
1413 return prov->pFuncs->pCPGetProvParam(prov->hPrivate, dwParam, pbData, pdwDataLen, dwFlags);
1416 /******************************************************************************
1417 * CryptGetUserKey (ADVAPI32.@)
1419 * Gets a handle of one of a user's two public/private key pairs.
1422 * hProv [I] Handle of a CSP.
1423 * dwKeySpec [I] Private key to use.
1424 * phUserKey [O] Pointer to the handle of the retrieved keys.
1430 BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
1432 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1435 TRACE("(0x%lx, %ld, %p)\n", hProv, dwKeySpec, phUserKey);
1438 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1440 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1441 if ( !(key = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1442 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1444 key->pProvider = prov;
1446 if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
1448 *phUserKey = (HCRYPTKEY)key;
1458 /******************************************************************************
1459 * CryptHashData (ADVAPI32.@)
1461 * Adds data to a hash object.
1464 * hHash [I] Handle of the hash object.
1465 * pbData [I] Buffer of data to be hashed.
1466 * dwDataLen [I] Number of bytes to add.
1467 * dwFlags [I] Can be CRYPT_USERDATA
1473 BOOL WINAPI CryptHashData (HCRYPTHASH hHash, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
1475 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1478 TRACE("(0x%lx, %p, %ld, %08ld)\n", hHash, pbData, dwDataLen, dwFlags);
1481 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1482 if (!pbData || !dwDataLen)
1483 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1485 prov = hash->pProvider;
1486 return prov->pFuncs->pCPHashData(prov->hPrivate, hash->hPrivate, pbData, dwDataLen, dwFlags);
1489 /******************************************************************************
1490 * CryptHashSessionKey (ADVAPI32.@)
1493 * hHash [I] Handle to the hash object.
1494 * hKey [I] Handle to the key to be hashed.
1495 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1501 BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags)
1503 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1504 PCRYPTKEY key = (PCRYPTKEY)hKey;
1507 TRACE("(0x%lx, 0x%lx, %08ld)\n", hHash, hKey, dwFlags);
1510 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1512 prov = hash->pProvider;
1513 return prov->pFuncs->pCPHashSessionKey(prov->hPrivate, hash->hPrivate, key->hPrivate, dwFlags);
1516 /******************************************************************************
1517 * CryptImportKey (ADVAPI32.@)
1520 * hProv [I] Handle of a CSP.
1521 * pbData [I] Contains the key to be imported.
1522 * dwDataLen [I] Length of the key.
1523 * hPubKey [I] Cryptographic key that decrypts pdData
1524 * dwFlags [I] Used only with a public/private key pair.
1525 * phKey [O] Imported key.
1531 BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, BYTE *pbData, DWORD dwDataLen,
1532 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1534 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1535 PCRYPTKEY pubkey = (PCRYPTKEY)hPubKey, importkey;
1537 TRACE("(0x%lx, %p, %ld, 0x%lx, %08ld, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1539 if (!prov || !pbData || !dwDataLen || !phKey)
1540 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1542 if ( !(importkey = CRYPT_Alloc(sizeof(CRYPTKEY))) )
1543 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1545 importkey->pProvider = prov;
1546 if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
1547 pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
1549 *phKey = (HCRYPTKEY)importkey;
1553 CRYPT_Free(importkey);
1557 /******************************************************************************
1560 * Note: Since the sDesciption (string) is supposed to be NULL and
1561 * is only retained for compatibility no string conversions are required
1562 * and only one implementation is required for both ANSI and Unicode.
1563 * We still need to export both:
1565 * CryptSignHashA (ADVAPI32.@)
1566 * CryptSignHashW (ADVAPI32.@)
1571 * hHash [I] Handle of the hash object to be signed.
1572 * dwKeySpec [I] Private key to use.
1573 * sDescription [I] Must be NULL.
1574 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1575 * pbSignature [O] Buffer of the signature data.
1576 * pdwSigLen [I/O] Size of the pbSignature buffer.
1582 BOOL WINAPI CryptSignHashA (HCRYPTHASH hHash, DWORD dwKeySpec, LPCSTR sDescription,
1583 DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
1585 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1588 TRACE("(0x%lx, %ld, %08ld, %p, %p)\n", hHash, dwKeySpec, dwFlags, pbSignature, pdwSigLen);
1590 WARN("The sDescription parameter is not supported (and no longer used). Ignoring.\n");
1593 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1595 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1597 prov = hash->pProvider;
1598 return prov->pFuncs->pCPSignHash(prov->hPrivate, hash->hPrivate, dwKeySpec, NULL,
1599 dwFlags, pbSignature, pdwSigLen);
1602 /******************************************************************************
1603 * CryptSetHashParam (ADVAPI32.@)
1605 * Customizes the operations of a hash object.
1608 * hHash [I] Handle of the hash object to set parameters.
1609 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1610 * pbData [I] Value data buffer.
1611 * dwFlags [I] Reserved for future use and must be zero.
1617 BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1620 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1622 TRACE("(0x%lx, %ld, %p, %08ld)\n", hHash, dwParam, pbData, dwFlags);
1624 if (!hash || !pbData)
1625 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1627 prov = hash->pProvider;
1628 return prov->pFuncs->pCPSetHashParam(prov->hPrivate, hash->hPrivate,
1629 dwParam, pbData, dwFlags);
1632 /******************************************************************************
1633 * CryptSetKeyParam (ADVAPI32.@)
1635 * Customizes a session key's operations.
1638 * hKey [I] Handle to the key to set values.
1639 * dwParam [I] See MSDN Doc.
1640 * pbData [I] Buffer of values to set.
1641 * dwFlags [I] Only used when dwParam == KP_ALGID.
1647 BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1650 PCRYPTKEY key = (PCRYPTKEY)hKey;
1652 TRACE("(0x%lx, %ld, %p, %08ld)\n", hKey, dwParam, pbData, dwFlags);
1654 if (!key || !pbData)
1655 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1657 prov = key->pProvider;
1658 return prov->pFuncs->pCPSetKeyParam(prov->hPrivate, key->hPrivate,
1659 dwParam, pbData, dwFlags);
1662 /******************************************************************************
1663 * CryptSetProviderA (ADVAPI32.@)
1665 * Specifies the current user's default CSP.
1668 * pszProvName [I] Name of the new default CSP.
1669 * dwProvType [I] Provider type of the CSP.
1675 BOOL WINAPI CryptSetProviderA (LPCSTR pszProvName, DWORD dwProvType)
1677 TRACE("(%s, %ld)\n", pszProvName, dwProvType);
1678 return CryptSetProviderExA(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1681 /******************************************************************************
1682 * CryptSetProviderW (ADVAPI32.@)
1684 * See CryptSetProviderA
1686 BOOL WINAPI CryptSetProviderW (LPCWSTR pszProvName, DWORD dwProvType)
1688 TRACE("(%s, %ld)\n", debugstr_w(pszProvName), dwProvType);
1689 return CryptSetProviderExW(pszProvName, dwProvType, NULL, CRYPT_USER_DEFAULT);
1692 /******************************************************************************
1693 * CryptSetProviderExA (ADVAPI32.@)
1695 * Specifies the default CSP.
1698 * pszProvName [I] Name of the new default CSP.
1699 * dwProvType [I] Provider type of the CSP.
1700 * pdwReserved [I] Reserved for future use and must be NULL.
1701 * dwFlags [I] See MSDN Doc.
1707 BOOL WINAPI CryptSetProviderExA (LPCSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1709 HKEY hProvKey, hTypeKey;
1712 TRACE("(%s, %ld, %p, %08ld)\n", pszProvName, dwProvType, pdwReserved, dwFlags);
1714 if (!pszProvName || pdwReserved)
1715 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1716 if (dwProvType > MAXPROVTYPES)
1717 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE);
1718 if (dwFlags & ~(CRYPT_MACHINE_DEFAULT | CRYPT_USER_DEFAULT | CRYPT_DELETE_DEFAULT)
1719 || dwFlags == CRYPT_DELETE_DEFAULT)
1720 CRYPT_ReturnLastError(NTE_BAD_FLAGS);
1722 if (!(keyname = CRYPT_GetTypeKeyName(dwProvType, dwFlags & CRYPT_USER_DEFAULT)))
1723 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1724 if (RegOpenKeyA((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1725 keyname, &hTypeKey))
1727 CRYPT_Free(keyname);
1728 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1730 CRYPT_Free(keyname);
1732 if (dwFlags & CRYPT_DELETE_DEFAULT)
1734 RegDeleteValueA(hTypeKey, "Name");
1738 if (!(keyname = CRYPT_GetProvKeyName(pszProvName)))
1740 RegCloseKey(hTypeKey);
1741 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY);
1743 if (RegOpenKeyA((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
1744 keyname, &hProvKey))
1746 CRYPT_Free(keyname);
1747 RegCloseKey(hTypeKey);
1748 CRYPT_ReturnLastError(NTE_BAD_PROVIDER);
1750 CRYPT_Free(keyname);
1752 if (RegSetValueExA(hTypeKey, "Name", 0, REG_SZ, pszProvName, strlen(pszProvName) + 1))
1754 RegCloseKey(hTypeKey);
1755 RegCloseKey(hProvKey);
1759 RegCloseKey(hProvKey);
1761 RegCloseKey(hTypeKey);
1766 /******************************************************************************
1767 * CryptSetProviderExW (ADVAPI32.@)
1769 * See CryptSetProviderExA
1771 BOOL WINAPI CryptSetProviderExW (LPCWSTR pszProvName, DWORD dwProvType, DWORD *pdwReserved, DWORD dwFlags)
1776 TRACE("(%s, %ld, %p, %08ld)\n", debugstr_w(pszProvName), dwProvType, pdwReserved, dwFlags);
1778 if (CRYPT_UnicodeToANSI(pszProvName, &str, -1))
1780 ret = CryptSetProviderExA(str, dwProvType, pdwReserved, dwFlags);
1786 /******************************************************************************
1787 * CryptSetProvParam (ADVAPI32.@)
1789 * Customizes the operations of a CSP.
1792 * hProv [I] Handle of a CSP.
1793 * dwParam [I] See MSDN Doc.
1794 * pbData [I] Buffer that contains a value to set as a parameter.
1795 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
1801 BOOL WINAPI CryptSetProvParam (HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
1803 PCRYPTPROV prov = (PCRYPTPROV)hProv;
1805 TRACE("(0x%lx, %ld, %p, %08ld)\n", hProv, dwParam, pbData, dwFlags);
1808 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1809 if (dwFlags & PP_USE_HARDWARE_RNG)
1811 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
1812 FIXME("\tLetting the CSP decide.\n");
1814 if (dwFlags & PP_CLIENT_HWND)
1816 /* FIXME: Should verify the parameter */
1817 if (pbData /* && IsWindow((HWND)pbData) */)
1819 crypt_hWindow = (HWND)(pbData);
1822 SetLastError(ERROR_INVALID_PARAMETER);
1826 /* All other flags go to the CSP */
1827 return prov->pFuncs->pCPSetProvParam(prov->hPrivate, dwParam, pbData, dwFlags);
1830 /******************************************************************************
1831 * CryptVerifySignatureA
1833 * Note: Since the sDesciption (string) is supposed to be NULL and
1834 * is only retained for compatibility no string conversions are required
1835 * and only one implementation is required for both ANSI and Unicode.
1836 * We still need to export both:
1838 * CryptVerifySignatureA (ADVAPI32.@)
1839 * CryptVerifySignatureW (ADVAPI32.@)
1841 * Verifies the signature of a hash object.
1844 * hHash [I] Handle of the hash object to verify.
1845 * pbSignature [I] Signature data to verify.
1846 * dwSigLen [I] Size of pbSignature.
1847 * hPubKey [I] Handle to the public key to authenticate signature.
1848 * sDescription [I] Must be set to NULL.
1849 * dwFlags [I] See MSDN doc.
1855 BOOL WINAPI CryptVerifySignatureA (HCRYPTHASH hHash, BYTE *pbSignature, DWORD dwSigLen,
1856 HCRYPTKEY hPubKey, LPCSTR sDescription, DWORD dwFlags)
1858 PCRYPTHASH hash = (PCRYPTHASH)hHash;
1859 PCRYPTKEY key = (PCRYPTKEY)hPubKey;
1862 TRACE("(0x%lx, %p, %ld, 0x%lx, %08ld)\n", hHash, pbSignature,
1863 dwSigLen, hPubKey, dwFlags);
1865 WARN("The sDescription parameter is not supported (and no longer used). Ignoring.\n");
1868 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE);
1869 if (!pbSignature || !dwSigLen)
1870 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER);
1872 prov = hash->pProvider;
1873 return prov->pFuncs->pCPVerifySignature(prov->hPrivate, hash->hPrivate, pbSignature, dwSigLen,
1874 key->hPrivate, NULL, dwFlags);
1879 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
1883 /******************************************************************************
1884 * SystemFunction040 (ADVAPI32.@)
1887 * memory : pointer to memory to encrypt
1888 * length : length of region to encrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE
1889 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON
1890 * control whether other processes are able to decrypt the memory. The same value must be given
1891 * when decrypting the memory.
1893 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags) /* RtlEncryptMemory */
1895 FIXME("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory, length, flags);
1896 return STATUS_SUCCESS;
1899 /******************************************************************************
1900 * SystemFunction041 (ADVAPI32.@)
1903 * memory : pointer to memory to decrypt
1904 * length : length of region to decrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE
1905 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON
1906 * control whether other processes are able to decrypt the memory. The same value must be given
1907 * when encrypting the memory.
1909 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags) /* RtlDecryptMemory */
1911 FIXME("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory, length, flags);
1912 return STATUS_SUCCESS;