2 * Unit tests for crypt functions
4 * Copyright (c) 2004 Michael Jung
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/test.h"
31 static const char szRsaBaseProv[] = MS_DEF_PROV_A;
32 static const char szNonExistentProv[] = "Wine Nonexistent Cryptographic Provider v11.2";
33 static const char szKeySet[] = "wine_test_keyset";
34 static const char szBadKeySet[] = "wine_test_bad_keyset";
35 #define NON_DEF_PROV_TYPE 999
37 static HMODULE hadvapi32;
38 static BOOL (WINAPI *pCryptAcquireContextA)(HCRYPTPROV*,LPCSTR,LPCSTR,DWORD,DWORD);
39 static BOOL (WINAPI *pCryptEnumProviderTypesA)(DWORD, DWORD*, DWORD, DWORD*, LPSTR, DWORD*);
40 static BOOL (WINAPI *pCryptEnumProvidersA)(DWORD, DWORD*, DWORD, DWORD*, LPSTR, DWORD*);
41 static BOOL (WINAPI *pCryptGetDefaultProviderA)(DWORD, DWORD*, DWORD, LPSTR, DWORD*);
42 static BOOL (WINAPI *pCryptReleaseContext)(HCRYPTPROV, DWORD);
43 static BOOL (WINAPI *pCryptSetProviderExA)(LPCSTR, DWORD, DWORD*, DWORD);
44 static BOOL (WINAPI *pCryptCreateHash)(HCRYPTPROV, ALG_ID, HCRYPTKEY, DWORD, HCRYPTHASH*);
45 static BOOL (WINAPI *pCryptDestroyHash)(HCRYPTHASH);
46 static BOOL (WINAPI *pCryptGenRandom)(HCRYPTPROV, DWORD, BYTE*);
47 static BOOL (WINAPI *pCryptContextAddRef)(HCRYPTPROV, DWORD*, DWORD dwFlags);
48 static BOOL (WINAPI *pCryptGenKey)(HCRYPTPROV, ALG_ID, DWORD, HCRYPTKEY*);
49 static BOOL (WINAPI *pCryptDestroyKey)(HCRYPTKEY);
50 static BOOL (WINAPI *pCryptDecrypt)(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, BYTE*, DWORD*);
51 static BOOL (WINAPI *pCryptDeriveKey)(HCRYPTPROV, ALG_ID, HCRYPTHASH, DWORD, HCRYPTKEY*);
52 static BOOL (WINAPI *pCryptDuplicateHash)(HCRYPTHASH, DWORD*, DWORD, HCRYPTHASH*);
53 static BOOL (WINAPI *pCryptDuplicateKey)(HCRYPTKEY, DWORD*, DWORD, HCRYPTKEY*);
54 static BOOL (WINAPI *pCryptEncrypt)(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, BYTE*, DWORD*, DWORD);
55 static BOOL (WINAPI *pCryptExportKey)(HCRYPTKEY, HCRYPTKEY, DWORD, DWORD, BYTE*, DWORD*);
56 static BOOL (WINAPI *pCryptGetHashParam)(HCRYPTHASH, DWORD, BYTE*, DWORD*, DWORD);
57 static BOOL (WINAPI *pCryptGetKeyParam)(HCRYPTKEY, DWORD, BYTE*, DWORD*, DWORD);
58 static BOOL (WINAPI *pCryptGetProvParam)(HCRYPTPROV, DWORD, BYTE*, DWORD*, DWORD);
59 static BOOL (WINAPI *pCryptGetUserKey)(HCRYPTPROV, DWORD, HCRYPTKEY*);
60 static BOOL (WINAPI *pCryptHashData)(HCRYPTHASH, BYTE*, DWORD, DWORD);
61 static BOOL (WINAPI *pCryptHashSessionKey)(HCRYPTHASH, HCRYPTKEY, DWORD);
62 static BOOL (WINAPI *pCryptImportKey)(HCRYPTPROV, BYTE*, DWORD, HCRYPTKEY, DWORD, HCRYPTKEY*);
63 static BOOL (WINAPI *pCryptSignHashW)(HCRYPTHASH, DWORD, LPCWSTR, DWORD, BYTE*, DWORD*);
64 static BOOL (WINAPI *pCryptSetHashParam)(HCRYPTKEY, DWORD, BYTE*, DWORD);
65 static BOOL (WINAPI *pCryptSetKeyParam)(HCRYPTKEY, DWORD, BYTE*, DWORD);
66 static BOOL (WINAPI *pCryptSetProvParam)(HCRYPTPROV, DWORD, BYTE*, DWORD);
67 static BOOL (WINAPI *pCryptVerifySignatureW)(HCRYPTHASH, BYTE*, DWORD, HCRYPTKEY, LPCWSTR, DWORD);
69 static void init_function_pointers(void)
71 hadvapi32 = GetModuleHandleA("advapi32.dll");
75 pCryptAcquireContextA = (void*)GetProcAddress(hadvapi32, "CryptAcquireContextA");
76 pCryptEnumProviderTypesA = (void*)GetProcAddress(hadvapi32, "CryptEnumProviderTypesA");
77 pCryptEnumProvidersA = (void*)GetProcAddress(hadvapi32, "CryptEnumProvidersA");
78 pCryptGetDefaultProviderA = (void*)GetProcAddress(hadvapi32, "CryptGetDefaultProviderA");
79 pCryptReleaseContext = (void*)GetProcAddress(hadvapi32, "CryptReleaseContext");
80 pCryptSetProviderExA = (void*)GetProcAddress(hadvapi32, "CryptSetProviderExA");
81 pCryptCreateHash = (void*)GetProcAddress(hadvapi32, "CryptCreateHash");
82 pCryptDestroyHash = (void*)GetProcAddress(hadvapi32, "CryptDestroyHash");
83 pCryptGenRandom = (void*)GetProcAddress(hadvapi32, "CryptGenRandom");
84 pCryptContextAddRef = (void*)GetProcAddress(hadvapi32, "CryptContextAddRef");
85 pCryptGenKey = (void*)GetProcAddress(hadvapi32, "CryptGenKey");
86 pCryptDestroyKey = (void*)GetProcAddress(hadvapi32, "CryptDestroyKey");
87 pCryptDecrypt = (void*)GetProcAddress(hadvapi32, "CryptDecrypt");
88 pCryptDeriveKey = (void*)GetProcAddress(hadvapi32, "CryptDeriveKey");
89 pCryptDuplicateHash = (void*)GetProcAddress(hadvapi32, "CryptDuplicateHash");
90 pCryptDuplicateKey = (void*)GetProcAddress(hadvapi32, "CryptDuplicateKey");
91 pCryptEncrypt = (void*)GetProcAddress(hadvapi32, "CryptEncrypt");
92 pCryptExportKey = (void*)GetProcAddress(hadvapi32, "CryptExportKey");
93 pCryptGetHashParam = (void*)GetProcAddress(hadvapi32, "CryptGetHashParam");
94 pCryptGetKeyParam = (void*)GetProcAddress(hadvapi32, "CryptGetKeyParam");
95 pCryptGetProvParam = (void*)GetProcAddress(hadvapi32, "CryptGetProvParam");
96 pCryptGetUserKey = (void*)GetProcAddress(hadvapi32, "CryptGetUserKey");
97 pCryptHashData = (void*)GetProcAddress(hadvapi32, "CryptHashData");
98 pCryptHashSessionKey = (void*)GetProcAddress(hadvapi32, "CryptHashSessionKey");
99 pCryptImportKey = (void*)GetProcAddress(hadvapi32, "CryptImportKey");
100 pCryptSignHashW = (void*)GetProcAddress(hadvapi32, "CryptSignHashW");
101 pCryptSetHashParam = (void*)GetProcAddress(hadvapi32, "CryptSetHashParam");
102 pCryptSetKeyParam = (void*)GetProcAddress(hadvapi32, "CryptSetKeyParam");
103 pCryptSetProvParam = (void*)GetProcAddress(hadvapi32, "CryptSetProvParam");
104 pCryptVerifySignatureW = (void*)GetProcAddress(hadvapi32, "CryptVerifySignatureW");
108 static void init_environment(void)
112 /* Ensure that container "wine_test_keyset" does exist */
113 if (!pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
115 pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
117 pCryptReleaseContext(hProv, 0);
119 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
120 if (!pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
122 pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
124 pCryptReleaseContext(hProv, 0);
126 /* Ensure that container "wine_test_bad_keyset" does not exist. */
127 if (pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
129 pCryptReleaseContext(hProv, 0);
130 pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
134 static void clean_up_environment(void)
138 /* Remove container "wine_test_keyset" */
139 if (pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
141 pCryptReleaseContext(hProv, 0);
142 pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
145 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
146 if (pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
148 pCryptReleaseContext(hProv, 0);
149 pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
153 static void test_acquire_context(void)
158 /* Provoke all kinds of error conditions (which are easy to provoke).
159 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
160 * but since this is likely to change between CSP versions, we don't check
161 * this. Please don't change the order of tests. */
162 result = pCryptAcquireContextA(&hProv, NULL, NULL, 0, 0);
163 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
165 result = pCryptAcquireContextA(&hProv, NULL, NULL, 1000, 0);
166 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
168 result = pCryptAcquireContextA(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
169 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%ld\n", GetLastError());
171 result = pCryptAcquireContextA(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
172 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%ld\n", GetLastError());
174 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
175 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%ld\n", GetLastError());
177 /* This test fails under Win2k SP4:
178 result = TRUE, GetLastError() == ERROR_INVALID_PARAMETER
179 SetLastError(0xdeadbeef);
180 result = pCryptAcquireContextA(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
181 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d/%ld\n", result, GetLastError());
184 /* Last not least, try to really acquire a context. */
186 SetLastError(0xdeadbeef);
187 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
188 ok(result && (GetLastError() == ERROR_ENVVAR_NOT_FOUND || GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_RING2_STACK_IN_USE || GetLastError() == NTE_FAIL), "%d/%ld\n", result, GetLastError());
191 pCryptReleaseContext(hProv, 0);
193 /* Try again, witch an empty ("\0") szProvider parameter */
195 SetLastError(0xdeadbeef);
196 result = pCryptAcquireContextA(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
197 ok(result && (GetLastError() == ERROR_ENVVAR_NOT_FOUND || GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_RING2_STACK_IN_USE || GetLastError() == NTE_FAIL), "%d/%ld\n", result, GetLastError());
200 pCryptReleaseContext(hProv, 0);
203 static void test_incorrect_api_usage(void)
206 HCRYPTPROV hProv, hProv2;
207 HCRYPTHASH hHash, hHash2;
208 HCRYPTKEY hKey, hKey2;
212 /* This is to document incorrect api usage in the
213 * "Uru - Ages beyond Myst Demo" installer as reported by Paul Vriens.
215 * The installer destroys a hash object after having released the context
216 * with which the hash was created. This is not allowed according to MSDN,
217 * since CryptReleaseContext destroys all hash and key objects belonging to
218 * the respective context. However, while wine used to crash, Windows is more
219 * robust here and returns an ERROR_INVALID_PARAMETER code.
222 result = pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv,
223 PROV_RSA_FULL, CRYPT_NEWKEYSET);
224 ok (result, "%08lx\n", GetLastError());
227 result = pCryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
228 ok (result, "%ld\n", GetLastError());
231 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey);
232 ok (result, "%ld\n", GetLastError());
235 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey2);
236 ok (result, "%ld\n", GetLastError());
239 result = pCryptDestroyKey(hKey2);
240 ok (result, "%ld\n", GetLastError());
242 dwTemp = CRYPT_MODE_ECB;
243 result = pCryptSetKeyParam(hKey2, KP_MODE, (BYTE*)&dwTemp, sizeof(DWORD));
244 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
246 result = pCryptAcquireContextA(&hProv2, szBadKeySet, NULL, PROV_RSA_FULL,
248 ok (result, "%ld\n", GetLastError());
251 result = pCryptReleaseContext(hProv, 0);
252 ok (result, "%ld\n", GetLastError());
255 result = pCryptReleaseContext(hProv, 0);
256 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
258 result = pCryptGenRandom(hProv, 1, &temp);
259 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
261 #ifdef CRASHES_ON_NT40
262 result = pCryptContextAddRef(hProv, NULL, 0);
263 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
266 result = pCryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash2);
267 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
270 result = pCryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, &temp, &dwLen);
271 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
274 result = pCryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, &temp, &dwLen, 1);
275 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
277 result = pCryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey2);
278 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
280 #ifdef CRASHES_ON_NT40
281 result = pCryptDuplicateHash(hHash, NULL, 0, &hHash2);
282 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
284 result = pCryptDuplicateKey(hKey, NULL, 0, &hKey2);
285 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
289 result = pCryptExportKey(hKey, (HCRYPTPROV)NULL, 0, 0, &temp, &dwLen);
290 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
292 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey2);
293 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
296 result = pCryptGetHashParam(hHash, 0, &temp, &dwLen, 0);
297 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
300 result = pCryptGetKeyParam(hKey, 0, &temp, &dwLen, 0);
301 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
304 result = pCryptGetProvParam(hProv, 0, &temp, &dwLen, 0);
305 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
307 result = pCryptGetUserKey(hProv, 0, &hKey2);
308 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
310 result = pCryptHashData(hHash, &temp, 1, 0);
311 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
313 result = pCryptHashSessionKey(hHash, hKey, 0);
314 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
316 result = pCryptImportKey(hProv, &temp, 1, (HCRYPTKEY)NULL, 0, &hKey2);
317 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
320 result = pCryptSignHashW(hHash, 0, NULL, 0, &temp, &dwLen);
321 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
322 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%ld\n", GetLastError());
324 result = pCryptSetKeyParam(hKey, 0, &temp, 1);
325 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
327 result = pCryptSetHashParam(hHash, 0, &temp, 1);
328 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
330 result = pCryptSetProvParam(hProv, 0, &temp, 1);
331 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
333 result = pCryptVerifySignatureW(hHash, &temp, 1, hKey, NULL, 0);
334 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
335 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%ld\n", GetLastError());
337 result = pCryptDestroyHash(hHash);
338 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
340 result = pCryptDestroyKey(hKey);
341 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
344 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
345 DWORD *pcbProvName, DWORD *pdwProvCount)
349 DWORD size = sizeof(DWORD);
351 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
354 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
355 NULL, NULL, NULL, NULL, NULL, NULL);
358 if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
361 RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
364 RegOpenKey(hKey, *pszProvName, &subkey);
365 RegQueryValueEx(subkey, "Type", NULL, NULL, (BYTE*)pdwProvType, &size);
373 static void test_enum_providers(void)
375 /* expected results */
376 CHAR *pszProvName = NULL;
383 CHAR *provider = NULL;
389 DWORD notZeroFlags = 5;
391 if(!pCryptEnumProvidersA)
393 trace("skipping CryptEnumProviders tests\n");
397 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
400 /* check pdwReserved flag for NULL */
401 result = pCryptEnumProvidersA(dwIndex, ¬Null, 0, &type, NULL, &providerLen);
402 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
404 /* check dwFlags == 0 */
405 result = pCryptEnumProvidersA(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
406 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%ld\n", GetLastError());
408 /* alloc provider to half the size required
409 * cbName holds the size required */
410 providerLen = cbName / 2;
411 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
414 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
415 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
416 ERROR_MORE_DATA, GetLastError());
420 /* loop through the providers to get the number of providers
421 * after loop ends, count should be provCount + 1 so subtract 1
422 * to get actual number of providers */
424 while(pCryptEnumProvidersA(count++, NULL, 0, &type, NULL, &providerLen))
427 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
429 /* loop past the actual number of providers to get the error
430 * ERROR_NO_MORE_ITEMS */
431 for (count = 0; count < provCount + 1; count++)
432 result = pCryptEnumProvidersA(count, NULL, 0, &type, NULL, &providerLen);
433 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
434 ERROR_NO_MORE_ITEMS, GetLastError());
436 /* check expected versus actual values returned */
437 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, NULL, &providerLen);
438 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
439 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
442 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
443 ok(result && type==dwType, "expected %ld, got %ld\n",
445 ok(result && !strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
446 ok(result && cbName==providerLen, "expected %ld, got %ld\n",
447 cbName, providerLen);
452 static BOOL FindProvTypesRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszTypeName,
453 DWORD *pcbTypeName, DWORD *pdwTypeCount)
459 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
462 if (RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwTypeCount, pcbTypeName, NULL,
463 NULL, NULL, NULL, NULL, NULL))
467 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
470 if (RegEnumKeyEx(hKey, dwIndex, *pszTypeName, pcbTypeName, NULL, NULL, NULL, NULL))
473 ch = *pszTypeName + strlen(*pszTypeName);
474 /* Convert "Type 000" to 0, etc/ */
475 *pdwProvType = *(--ch) - '0';
476 *pdwProvType += (*(--ch) - '0') * 10;
477 *pdwProvType += (*(--ch) - '0') * 100;
479 if (RegOpenKey(hKey, *pszTypeName, &hSubKey))
482 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, NULL, pcbTypeName))
485 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
488 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, *pszTypeName, pcbTypeName))
491 RegCloseKey(hSubKey);
497 static void test_enum_provider_types()
499 /* expected values */
501 LPSTR pszTypeName = NULL;
508 LPSTR typeName = NULL;
513 DWORD notZeroFlags = 5;
515 if(!pCryptEnumProviderTypesA)
517 trace("skipping CryptEnumProviderTypes tests\n");
521 if (!FindProvTypesRegVals(index, &dwProvType, &pszTypeName, &cbTypeName, &dwTypeCount))
523 trace("could not find provider types in registry, skipping the test\n");
527 /* check pdwReserved for NULL */
528 result = pCryptEnumProviderTypesA(index, ¬Null, 0, &provType, typeName, &typeNameSize);
529 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
530 ERROR_INVALID_PARAMETER, GetLastError());
532 /* check dwFlags == zero */
533 result = pCryptEnumProviderTypesA(index, NULL, notZeroFlags, &provType, typeName, &typeNameSize);
534 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %i, got %ld\n",
535 ERROR_INVALID_PARAMETER, GetLastError());
537 /* alloc provider type to half the size required
538 * cbTypeName holds the size required */
539 typeNameSize = cbTypeName / 2;
540 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
543 /* This test fails under Win2k SP4:
544 result = TRUE, GetLastError() == 0xdeadbeef
545 SetLastError(0xdeadbeef);
546 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
547 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected 0/ERROR_MORE_DATA, got %d/%08lx\n",
548 result, GetLastError());
553 /* loop through the provider types to get the number of provider types
554 * after loop ends, count should be dwTypeCount + 1 so subtract 1
555 * to get actual number of provider types */
557 while(pCryptEnumProviderTypesA(typeCount++, NULL, 0, &provType, NULL, &typeNameSize))
560 ok(typeCount==dwTypeCount, "expected %ld, got %ld\n", dwTypeCount, typeCount);
562 /* loop past the actual number of provider types to get the error
563 * ERROR_NO_MORE_ITEMS */
564 for (typeCount = 0; typeCount < dwTypeCount + 1; typeCount++)
565 result = pCryptEnumProviderTypesA(typeCount, NULL, 0, &provType, NULL, &typeNameSize);
566 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
567 ERROR_NO_MORE_ITEMS, GetLastError());
570 /* check expected versus actual values returned */
571 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, NULL, &typeNameSize);
572 ok(result && typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
573 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
576 typeNameSize = 0xdeadbeef;
577 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
578 ok(result, "expected TRUE, got %ld\n", result);
579 ok(provType==dwProvType, "expected %ld, got %ld\n", dwProvType, provType);
581 ok(!strcmp(pszTypeName, typeName), "expected %s, got %s\n", pszTypeName, typeName);
582 ok(typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
587 static BOOL FindDfltProvRegVals(DWORD dwProvType, DWORD dwFlags, LPSTR *pszProvName, DWORD *pcbProvName)
592 DWORD user = dwFlags & CRYPT_USER_DEFAULT;
594 LPCSTR machinestr = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
595 LPCSTR userstr = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
597 keyname = LocalAlloc(LMEM_ZEROINIT, (user ? strlen(userstr) : strlen(machinestr)) + 1);
600 user ? strcpy(keyname, userstr) : strcpy(keyname, machinestr);
601 ptr = keyname + strlen(keyname);
602 *(--ptr) = (dwProvType % 10) + '0';
603 *(--ptr) = ((dwProvType / 10) % 10) + '0';
604 *(--ptr) = (dwProvType / 100) + '0';
608 if (RegOpenKey((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
615 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
617 if (GetLastError() != ERROR_MORE_DATA)
618 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
622 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
625 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
627 if (GetLastError() != ERROR_MORE_DATA)
628 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
637 static void test_get_default_provider()
639 /* expected results */
640 DWORD dwProvType = PROV_RSA_FULL;
641 DWORD dwFlags = CRYPT_MACHINE_DEFAULT;
642 LPSTR pszProvName = NULL;
646 DWORD provType = PROV_RSA_FULL;
647 DWORD flags = CRYPT_MACHINE_DEFAULT;
648 LPSTR provName = NULL;
653 if(!pCryptGetDefaultProviderA)
655 trace("skipping CryptGetDefaultProvider tests\n");
659 FindDfltProvRegVals(dwProvType, dwFlags, &pszProvName, &cbProvName);
661 /* check pdwReserved for NULL */
662 result = pCryptGetDefaultProviderA(provType, ¬Null, flags, provName, &provNameSize);
663 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
664 ERROR_INVALID_PARAMETER, GetLastError());
666 /* check for invalid flag */
668 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
669 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %ld, got %ld\n",
670 NTE_BAD_FLAGS, GetLastError());
671 flags = CRYPT_MACHINE_DEFAULT;
673 /* check for invalid prov type */
674 provType = 0xdeadbeef;
675 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
676 ok(!result && (GetLastError() == NTE_BAD_PROV_TYPE ||
677 GetLastError() == ERROR_INVALID_PARAMETER),
678 "expected NTE_BAD_PROV_TYPE or ERROR_INVALID_PARAMETER, got %ld/%ld\n",
679 result, GetLastError());
680 provType = PROV_RSA_FULL;
684 /* alloc provName to half the size required
685 * cbProvName holds the size required */
686 provNameSize = cbProvName / 2;
687 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
690 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
691 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
692 ERROR_MORE_DATA, GetLastError());
696 /* check expected versus actual values returned */
697 result = pCryptGetDefaultProviderA(provType, NULL, flags, NULL, &provNameSize);
698 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
699 provNameSize = cbProvName;
701 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
704 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
705 ok(result && !strcmp(pszProvName, provName), "expected %s, got %s\n", pszProvName, provName);
706 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
711 static void test_set_provider_ex()
717 LPSTR pszProvName = NULL;
720 if(!pCryptGetDefaultProviderA || !pCryptSetProviderExA)
722 trace("skipping CryptSetProviderEx tests\n");
726 /* check pdwReserved for NULL */
727 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, ¬Null, CRYPT_MACHINE_DEFAULT);
728 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
729 ERROR_INVALID_PARAMETER, GetLastError());
731 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
732 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT | CRYPT_DELETE_DEFAULT);
733 ok(result, "%ld\n", GetLastError());
735 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
736 ok(result, "%ld\n", GetLastError());
738 /* call CryptGetDefaultProvider to see if they match */
739 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &cbProvName);
740 if (!(pszProvName = LocalAlloc(LMEM_ZEROINIT, cbProvName)))
743 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, pszProvName, &cbProvName);
744 ok(result && !strcmp(MS_DEF_PROV, pszProvName), "expected %s, got %s\n", MS_DEF_PROV, pszProvName);
745 ok(result && cbProvName==(strlen(MS_DEF_PROV) + 1), "expected %i, got %ld\n", (strlen(MS_DEF_PROV) + 1), cbProvName);
747 LocalFree(pszProvName);
752 init_function_pointers();
753 if(pCryptAcquireContextA && pCryptReleaseContext) {
755 test_acquire_context();
756 test_incorrect_api_usage();
757 clean_up_environment();
760 test_enum_providers();
761 test_enum_provider_types();
762 test_get_default_provider();
763 test_set_provider_ex();