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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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)
159 /* Provoke all kinds of error conditions (which are easy to provoke).
160 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
161 * but since this is likely to change between CSP versions, we don't check
162 * this. Please don't change the order of tests. */
163 result = pCryptAcquireContextA(&hProv, NULL, NULL, 0, 0);
164 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
166 result = pCryptAcquireContextA(&hProv, NULL, NULL, 1000, 0);
167 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
169 result = pCryptAcquireContextA(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
170 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%ld\n", GetLastError());
172 result = pCryptAcquireContextA(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
173 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%ld\n", GetLastError());
175 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
176 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%ld\n", GetLastError());
178 /* This test fails under Win2k SP4:
179 result = TRUE, GetLastError() == ERROR_INVALID_PARAMETER
180 SetLastError(0xdeadbeef);
181 result = pCryptAcquireContextA(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
182 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d/%ld\n", result, GetLastError());
185 /* Last not least, try to really acquire a context. */
187 SetLastError(0xdeadbeef);
188 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
189 GLE = GetLastError();
190 ok(result && (GLE == ERROR_ENVVAR_NOT_FOUND ||
191 GLE == ERROR_SUCCESS ||
192 GLE == ERROR_RING2_STACK_IN_USE ||
194 GLE == ERROR_NOT_LOGGED_ON), "%d/%ld\n", result, GLE);
197 pCryptReleaseContext(hProv, 0);
199 /* Try again, witch an empty ("\0") szProvider parameter */
201 SetLastError(0xdeadbeef);
202 result = pCryptAcquireContextA(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
203 GLE = GetLastError();
204 ok(result && (GLE == ERROR_ENVVAR_NOT_FOUND ||
205 GLE == ERROR_SUCCESS ||
206 GLE == ERROR_RING2_STACK_IN_USE ||
208 GLE == ERROR_NOT_LOGGED_ON), "%d/%ld\n", result, GetLastError());
211 pCryptReleaseContext(hProv, 0);
214 static void test_incorrect_api_usage(void)
217 HCRYPTPROV hProv, hProv2;
218 HCRYPTHASH hHash, hHash2;
219 HCRYPTKEY hKey, hKey2;
223 /* This is to document incorrect api usage in the
224 * "Uru - Ages beyond Myst Demo" installer as reported by Paul Vriens.
226 * The installer destroys a hash object after having released the context
227 * with which the hash was created. This is not allowed according to MSDN,
228 * since CryptReleaseContext destroys all hash and key objects belonging to
229 * the respective context. However, while wine used to crash, Windows is more
230 * robust here and returns an ERROR_INVALID_PARAMETER code.
233 result = pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv,
234 PROV_RSA_FULL, CRYPT_NEWKEYSET);
235 ok (result, "%08lx\n", GetLastError());
238 result = pCryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
239 ok (result, "%ld\n", GetLastError());
242 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey);
243 ok (result, "%ld\n", GetLastError());
246 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey2);
247 ok (result, "%ld\n", GetLastError());
250 result = pCryptDestroyKey(hKey2);
251 ok (result, "%ld\n", GetLastError());
253 dwTemp = CRYPT_MODE_ECB;
254 result = pCryptSetKeyParam(hKey2, KP_MODE, (BYTE*)&dwTemp, sizeof(DWORD));
255 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
257 result = pCryptAcquireContextA(&hProv2, szBadKeySet, NULL, PROV_RSA_FULL,
259 ok (result, "%ld\n", GetLastError());
262 result = pCryptReleaseContext(hProv, 0);
263 ok (result, "%ld\n", GetLastError());
266 result = pCryptReleaseContext(hProv, 0);
267 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
269 result = pCryptGenRandom(hProv, 1, &temp);
270 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
272 #ifdef CRASHES_ON_NT40
273 result = pCryptContextAddRef(hProv, NULL, 0);
274 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
277 result = pCryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash2);
278 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
281 result = pCryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, &temp, &dwLen);
282 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
285 result = pCryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, &temp, &dwLen, 1);
286 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
288 result = pCryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey2);
289 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
291 #ifdef CRASHES_ON_NT40
292 result = pCryptDuplicateHash(hHash, NULL, 0, &hHash2);
293 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
295 result = pCryptDuplicateKey(hKey, NULL, 0, &hKey2);
296 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
300 result = pCryptExportKey(hKey, (HCRYPTPROV)NULL, 0, 0, &temp, &dwLen);
301 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
303 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey2);
304 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
307 result = pCryptGetHashParam(hHash, 0, &temp, &dwLen, 0);
308 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
311 result = pCryptGetKeyParam(hKey, 0, &temp, &dwLen, 0);
312 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
315 result = pCryptGetProvParam(hProv, 0, &temp, &dwLen, 0);
316 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
318 result = pCryptGetUserKey(hProv, 0, &hKey2);
319 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
321 result = pCryptHashData(hHash, &temp, 1, 0);
322 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
324 result = pCryptHashSessionKey(hHash, hKey, 0);
325 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
327 result = pCryptImportKey(hProv, &temp, 1, (HCRYPTKEY)NULL, 0, &hKey2);
328 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
331 result = pCryptSignHashW(hHash, 0, NULL, 0, &temp, &dwLen);
332 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
333 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%ld\n", GetLastError());
335 result = pCryptSetKeyParam(hKey, 0, &temp, 1);
336 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
338 result = pCryptSetHashParam(hHash, 0, &temp, 1);
339 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
341 result = pCryptSetProvParam(hProv, 0, &temp, 1);
342 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
344 result = pCryptVerifySignatureW(hHash, &temp, 1, hKey, NULL, 0);
345 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
346 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%ld\n", GetLastError());
348 result = pCryptDestroyHash(hHash);
349 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
351 result = pCryptDestroyKey(hKey);
352 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
355 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
356 DWORD *pcbProvName, DWORD *pdwProvCount)
360 DWORD size = sizeof(DWORD);
362 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
365 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
366 NULL, NULL, NULL, NULL, NULL, NULL);
369 if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
372 RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
375 RegOpenKey(hKey, *pszProvName, &subkey);
376 RegQueryValueEx(subkey, "Type", NULL, NULL, (LPBYTE)pdwProvType, &size);
384 static void test_enum_providers(void)
386 /* expected results */
387 CHAR *pszProvName = NULL;
394 CHAR *provider = NULL;
400 DWORD notZeroFlags = 5;
402 if(!pCryptEnumProvidersA)
404 trace("skipping CryptEnumProviders tests\n");
408 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
410 trace("could not find providers in registry, skipping the test\n");
414 /* check pdwReserved flag for NULL */
415 result = pCryptEnumProvidersA(dwIndex, ¬Null, 0, &type, NULL, &providerLen);
416 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
418 /* check dwFlags == 0 */
419 result = pCryptEnumProvidersA(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
420 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%ld\n", GetLastError());
422 /* alloc provider to half the size required
423 * cbName holds the size required */
424 providerLen = cbName / 2;
425 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
428 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
429 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
430 ERROR_MORE_DATA, GetLastError());
434 /* loop through the providers to get the number of providers
435 * after loop ends, count should be provCount + 1 so subtract 1
436 * to get actual number of providers */
438 while(pCryptEnumProvidersA(count++, NULL, 0, &type, NULL, &providerLen))
441 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
443 /* loop past the actual number of providers to get the error
444 * ERROR_NO_MORE_ITEMS */
445 for (count = 0; count < provCount + 1; count++)
446 result = pCryptEnumProvidersA(count, NULL, 0, &type, NULL, &providerLen);
447 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
448 ERROR_NO_MORE_ITEMS, GetLastError());
450 /* check expected versus actual values returned */
451 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, NULL, &providerLen);
452 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
453 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
456 providerLen = 0xdeadbeef;
457 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
458 ok(result, "expected TRUE, got %ld\n", result);
459 ok(type==dwType, "expected %ld, got %ld\n", dwType, type);
461 ok(!strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
462 ok(cbName==providerLen, "expected %ld, got %ld\n", cbName, providerLen);
467 static BOOL FindProvTypesRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszTypeName,
468 DWORD *pcbTypeName, DWORD *pdwTypeCount)
474 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
477 if (RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwTypeCount, pcbTypeName, NULL,
478 NULL, NULL, NULL, NULL, NULL))
482 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
485 if (RegEnumKeyEx(hKey, dwIndex, *pszTypeName, pcbTypeName, NULL, NULL, NULL, NULL))
488 ch = *pszTypeName + strlen(*pszTypeName);
489 /* Convert "Type 000" to 0, etc/ */
490 *pdwProvType = *(--ch) - '0';
491 *pdwProvType += (*(--ch) - '0') * 10;
492 *pdwProvType += (*(--ch) - '0') * 100;
494 if (RegOpenKey(hKey, *pszTypeName, &hSubKey))
497 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, NULL, pcbTypeName))
500 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
503 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, (LPBYTE)*pszTypeName, pcbTypeName))
506 RegCloseKey(hSubKey);
512 static void test_enum_provider_types(void)
514 /* expected values */
516 LPSTR pszTypeName = NULL;
523 LPSTR typeName = NULL;
528 DWORD notZeroFlags = 5;
530 if(!pCryptEnumProviderTypesA)
532 trace("skipping CryptEnumProviderTypes tests\n");
536 if (!FindProvTypesRegVals(index, &dwProvType, &pszTypeName, &cbTypeName, &dwTypeCount))
538 trace("could not find provider types in registry, skipping the test\n");
542 /* check pdwReserved for NULL */
543 result = pCryptEnumProviderTypesA(index, ¬Null, 0, &provType, typeName, &typeNameSize);
544 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
545 ERROR_INVALID_PARAMETER, GetLastError());
547 /* check dwFlags == zero */
548 result = pCryptEnumProviderTypesA(index, NULL, notZeroFlags, &provType, typeName, &typeNameSize);
549 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %i, got %ld\n",
550 ERROR_INVALID_PARAMETER, GetLastError());
552 /* alloc provider type to half the size required
553 * cbTypeName holds the size required */
554 typeNameSize = cbTypeName / 2;
555 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
558 /* This test fails under Win2k SP4:
559 result = TRUE, GetLastError() == 0xdeadbeef
560 SetLastError(0xdeadbeef);
561 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
562 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected 0/ERROR_MORE_DATA, got %d/%08lx\n",
563 result, GetLastError());
568 /* loop through the provider types to get the number of provider types
569 * after loop ends, count should be dwTypeCount + 1 so subtract 1
570 * to get actual number of provider types */
572 while(pCryptEnumProviderTypesA(typeCount++, NULL, 0, &provType, NULL, &typeNameSize))
575 ok(typeCount==dwTypeCount, "expected %ld, got %ld\n", dwTypeCount, typeCount);
577 /* loop past the actual number of provider types to get the error
578 * ERROR_NO_MORE_ITEMS */
579 for (typeCount = 0; typeCount < dwTypeCount + 1; typeCount++)
580 result = pCryptEnumProviderTypesA(typeCount, NULL, 0, &provType, NULL, &typeNameSize);
581 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
582 ERROR_NO_MORE_ITEMS, GetLastError());
585 /* check expected versus actual values returned */
586 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, NULL, &typeNameSize);
587 ok(result && typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
588 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
591 typeNameSize = 0xdeadbeef;
592 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
593 ok(result, "expected TRUE, got %ld\n", result);
594 ok(provType==dwProvType, "expected %ld, got %ld\n", dwProvType, provType);
596 ok(!strcmp(pszTypeName, typeName), "expected %s, got %s\n", pszTypeName, typeName);
597 ok(typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
602 static BOOL FindDfltProvRegVals(DWORD dwProvType, DWORD dwFlags, LPSTR *pszProvName, DWORD *pcbProvName)
607 DWORD user = dwFlags & CRYPT_USER_DEFAULT;
609 LPCSTR machinestr = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
610 LPCSTR userstr = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
612 keyname = LocalAlloc(LMEM_ZEROINIT, (user ? strlen(userstr) : strlen(machinestr)) + 1);
615 user ? strcpy(keyname, userstr) : strcpy(keyname, machinestr);
616 ptr = keyname + strlen(keyname);
617 *(--ptr) = (dwProvType % 10) + '0';
618 *(--ptr) = ((dwProvType / 10) % 10) + '0';
619 *(--ptr) = (dwProvType / 100) + '0';
623 if (RegOpenKey((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
630 if (RegQueryValueEx(hKey, "Name", NULL, NULL, (LPBYTE)*pszProvName, pcbProvName))
632 if (GetLastError() != ERROR_MORE_DATA)
633 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
637 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
640 if (RegQueryValueEx(hKey, "Name", NULL, NULL, (LPBYTE)*pszProvName, pcbProvName))
642 if (GetLastError() != ERROR_MORE_DATA)
643 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
652 static void test_get_default_provider(void)
654 /* expected results */
655 DWORD dwProvType = PROV_RSA_FULL;
656 DWORD dwFlags = CRYPT_MACHINE_DEFAULT;
657 LPSTR pszProvName = NULL;
661 DWORD provType = PROV_RSA_FULL;
662 DWORD flags = CRYPT_MACHINE_DEFAULT;
663 LPSTR provName = NULL;
668 if(!pCryptGetDefaultProviderA)
670 trace("skipping CryptGetDefaultProvider tests\n");
674 if(!FindDfltProvRegVals(dwProvType, dwFlags, &pszProvName, &cbProvName))
676 trace("could not find default provider in registry, skipping the test\n");
680 /* check pdwReserved for NULL */
681 result = pCryptGetDefaultProviderA(provType, ¬Null, flags, provName, &provNameSize);
682 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
683 ERROR_INVALID_PARAMETER, GetLastError());
685 /* check for invalid flag */
687 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
688 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %ld, got %ld\n",
689 NTE_BAD_FLAGS, GetLastError());
690 flags = CRYPT_MACHINE_DEFAULT;
692 /* check for invalid prov type */
693 provType = 0xdeadbeef;
694 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
695 ok(!result && (GetLastError() == NTE_BAD_PROV_TYPE ||
696 GetLastError() == ERROR_INVALID_PARAMETER),
697 "expected NTE_BAD_PROV_TYPE or ERROR_INVALID_PARAMETER, got %ld/%ld\n",
698 result, GetLastError());
699 provType = PROV_RSA_FULL;
703 /* alloc provName to half the size required
704 * cbProvName holds the size required */
705 provNameSize = cbProvName / 2;
706 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
709 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
710 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
711 ERROR_MORE_DATA, GetLastError());
715 /* check expected versus actual values returned */
716 result = pCryptGetDefaultProviderA(provType, NULL, flags, NULL, &provNameSize);
717 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
718 provNameSize = cbProvName;
720 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
723 provNameSize = 0xdeadbeef;
724 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
725 ok(result, "expected TRUE, got %ld\n", result);
727 ok(!strcmp(pszProvName, provName), "expected %s, got %s\n", pszProvName, provName);
728 ok(provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
733 static void test_set_provider_ex(void)
739 LPSTR pszProvName = NULL;
742 if(!pCryptGetDefaultProviderA || !pCryptSetProviderExA)
744 trace("skipping CryptSetProviderEx tests\n");
748 /* check pdwReserved for NULL */
749 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, ¬Null, CRYPT_MACHINE_DEFAULT);
750 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
751 ERROR_INVALID_PARAMETER, GetLastError());
753 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
754 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT | CRYPT_DELETE_DEFAULT);
755 ok(result, "%ld\n", GetLastError());
757 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
758 ok(result, "%ld\n", GetLastError());
760 /* call CryptGetDefaultProvider to see if they match */
761 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &cbProvName);
762 if (!(pszProvName = LocalAlloc(LMEM_ZEROINIT, cbProvName)))
765 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, pszProvName, &cbProvName);
766 ok(result && !strcmp(MS_DEF_PROV, pszProvName), "expected %s, got %s\n", MS_DEF_PROV, pszProvName);
767 ok(result && cbProvName==(strlen(MS_DEF_PROV) + 1), "expected %i, got %ld\n", (strlen(MS_DEF_PROV) + 1), cbProvName);
769 LocalFree(pszProvName);
774 init_function_pointers();
775 if(pCryptAcquireContextA && pCryptReleaseContext) {
777 test_acquire_context();
778 test_incorrect_api_usage();
779 clean_up_environment();
782 test_enum_providers();
783 test_enum_provider_types();
784 test_get_default_provider();
785 test_set_provider_ex();