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");
73 pCryptAcquireContextA = (void*)GetProcAddress(hadvapi32, "CryptAcquireContextA");
74 pCryptEnumProviderTypesA = (void*)GetProcAddress(hadvapi32, "CryptEnumProviderTypesA");
75 pCryptEnumProvidersA = (void*)GetProcAddress(hadvapi32, "CryptEnumProvidersA");
76 pCryptGetDefaultProviderA = (void*)GetProcAddress(hadvapi32, "CryptGetDefaultProviderA");
77 pCryptReleaseContext = (void*)GetProcAddress(hadvapi32, "CryptReleaseContext");
78 pCryptSetProviderExA = (void*)GetProcAddress(hadvapi32, "CryptSetProviderExA");
79 pCryptCreateHash = (void*)GetProcAddress(hadvapi32, "CryptCreateHash");
80 pCryptDestroyHash = (void*)GetProcAddress(hadvapi32, "CryptDestroyHash");
81 pCryptGenRandom = (void*)GetProcAddress(hadvapi32, "CryptGenRandom");
82 pCryptContextAddRef = (void*)GetProcAddress(hadvapi32, "CryptContextAddRef");
83 pCryptGenKey = (void*)GetProcAddress(hadvapi32, "CryptGenKey");
84 pCryptDestroyKey = (void*)GetProcAddress(hadvapi32, "CryptDestroyKey");
85 pCryptDecrypt = (void*)GetProcAddress(hadvapi32, "CryptDecrypt");
86 pCryptDeriveKey = (void*)GetProcAddress(hadvapi32, "CryptDeriveKey");
87 pCryptDuplicateHash = (void*)GetProcAddress(hadvapi32, "CryptDuplicateHash");
88 pCryptDuplicateKey = (void*)GetProcAddress(hadvapi32, "CryptDuplicateKey");
89 pCryptEncrypt = (void*)GetProcAddress(hadvapi32, "CryptEncrypt");
90 pCryptExportKey = (void*)GetProcAddress(hadvapi32, "CryptExportKey");
91 pCryptGetHashParam = (void*)GetProcAddress(hadvapi32, "CryptGetHashParam");
92 pCryptGetKeyParam = (void*)GetProcAddress(hadvapi32, "CryptGetKeyParam");
93 pCryptGetProvParam = (void*)GetProcAddress(hadvapi32, "CryptGetProvParam");
94 pCryptGetUserKey = (void*)GetProcAddress(hadvapi32, "CryptGetUserKey");
95 pCryptHashData = (void*)GetProcAddress(hadvapi32, "CryptHashData");
96 pCryptHashSessionKey = (void*)GetProcAddress(hadvapi32, "CryptHashSessionKey");
97 pCryptImportKey = (void*)GetProcAddress(hadvapi32, "CryptImportKey");
98 pCryptSignHashW = (void*)GetProcAddress(hadvapi32, "CryptSignHashW");
99 pCryptSetHashParam = (void*)GetProcAddress(hadvapi32, "CryptSetHashParam");
100 pCryptSetKeyParam = (void*)GetProcAddress(hadvapi32, "CryptSetKeyParam");
101 pCryptSetProvParam = (void*)GetProcAddress(hadvapi32, "CryptSetProvParam");
102 pCryptVerifySignatureW = (void*)GetProcAddress(hadvapi32, "CryptVerifySignatureW");
105 static void init_environment(void)
109 /* Ensure that container "wine_test_keyset" does exist */
110 if (!pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
112 pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
114 pCryptReleaseContext(hProv, 0);
116 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
117 if (!pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
119 pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
121 pCryptReleaseContext(hProv, 0);
123 /* Ensure that container "wine_test_bad_keyset" does not exist. */
124 if (pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
126 pCryptReleaseContext(hProv, 0);
127 pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
131 static void clean_up_environment(void)
135 /* Remove container "wine_test_keyset" */
136 if (pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
138 pCryptReleaseContext(hProv, 0);
139 pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
142 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
143 if (pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
145 pCryptReleaseContext(hProv, 0);
146 pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
150 static void test_acquire_context(void)
156 /* Provoke all kinds of error conditions (which are easy to provoke).
157 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
158 * but since this is likely to change between CSP versions, we don't check
159 * this. Please don't change the order of tests. */
160 result = pCryptAcquireContextA(&hProv, NULL, NULL, 0, 0);
161 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%d\n", GetLastError());
163 result = pCryptAcquireContextA(&hProv, NULL, NULL, 1000, 0);
164 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%d\n", GetLastError());
166 result = pCryptAcquireContextA(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
167 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%d\n", GetLastError());
169 result = pCryptAcquireContextA(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
170 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%d\n", GetLastError());
172 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
173 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%d\n", GetLastError());
175 /* This test fails under Win2k SP4:
176 result = TRUE, GetLastError() == ERROR_INVALID_PARAMETER
177 SetLastError(0xdeadbeef);
178 result = pCryptAcquireContextA(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
179 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d/%d\n", result, GetLastError());
182 /* Last not least, try to really acquire a context. */
184 SetLastError(0xdeadbeef);
185 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
186 GLE = GetLastError();
187 ok(result && (GLE == ERROR_ENVVAR_NOT_FOUND ||
188 GLE == ERROR_SUCCESS ||
189 GLE == ERROR_RING2_STACK_IN_USE ||
191 GLE == ERROR_NOT_LOGGED_ON), "%d/%d\n", result, GLE);
194 pCryptReleaseContext(hProv, 0);
196 /* Try again, witch an empty ("\0") szProvider parameter */
198 SetLastError(0xdeadbeef);
199 result = pCryptAcquireContextA(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
200 GLE = GetLastError();
201 ok(result && (GLE == ERROR_ENVVAR_NOT_FOUND ||
202 GLE == ERROR_SUCCESS ||
203 GLE == ERROR_RING2_STACK_IN_USE ||
205 GLE == ERROR_NOT_LOGGED_ON), "%d/%d\n", result, GetLastError());
208 pCryptReleaseContext(hProv, 0);
211 static void test_incorrect_api_usage(void)
214 HCRYPTPROV hProv, hProv2;
215 HCRYPTHASH hHash, hHash2;
216 HCRYPTKEY hKey, hKey2;
220 /* This is to document incorrect api usage in the
221 * "Uru - Ages beyond Myst Demo" installer as reported by Paul Vriens.
223 * The installer destroys a hash object after having released the context
224 * with which the hash was created. This is not allowed according to MSDN,
225 * since CryptReleaseContext destroys all hash and key objects belonging to
226 * the respective context. However, while wine used to crash, Windows is more
227 * robust here and returns an ERROR_INVALID_PARAMETER code.
230 result = pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv,
231 PROV_RSA_FULL, CRYPT_NEWKEYSET);
232 ok (result, "%08x\n", GetLastError());
235 result = pCryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
236 ok (result, "%d\n", GetLastError());
239 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey);
240 ok (result, "%d\n", GetLastError());
243 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey2);
244 ok (result, "%d\n", GetLastError());
247 result = pCryptDestroyKey(hKey2);
248 ok (result, "%d\n", GetLastError());
250 dwTemp = CRYPT_MODE_ECB;
251 result = pCryptSetKeyParam(hKey2, KP_MODE, (BYTE*)&dwTemp, sizeof(DWORD));
252 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
254 result = pCryptAcquireContextA(&hProv2, szBadKeySet, NULL, PROV_RSA_FULL,
256 ok (result, "%d\n", GetLastError());
259 result = pCryptReleaseContext(hProv, 0);
260 ok (result, "%d\n", GetLastError());
263 result = pCryptReleaseContext(hProv, 0);
264 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
266 result = pCryptGenRandom(hProv, 1, &temp);
267 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
269 #ifdef CRASHES_ON_NT40
270 result = pCryptContextAddRef(hProv, NULL, 0);
271 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
274 result = pCryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash2);
275 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
278 result = pCryptDecrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, &temp, &dwLen);
279 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
282 result = pCryptEncrypt(hKey, (HCRYPTHASH)NULL, TRUE, 0, &temp, &dwLen, 1);
283 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
285 result = pCryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey2);
286 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
288 #ifdef CRASHES_ON_NT40
289 result = pCryptDuplicateHash(hHash, NULL, 0, &hHash2);
290 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
292 result = pCryptDuplicateKey(hKey, NULL, 0, &hKey2);
293 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
297 result = pCryptExportKey(hKey, (HCRYPTPROV)NULL, 0, 0, &temp, &dwLen);
298 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
300 result = pCryptGenKey(hProv, CALG_RC4, 0, &hKey2);
301 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
304 result = pCryptGetHashParam(hHash, 0, &temp, &dwLen, 0);
305 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
308 result = pCryptGetKeyParam(hKey, 0, &temp, &dwLen, 0);
309 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
312 result = pCryptGetProvParam(hProv, 0, &temp, &dwLen, 0);
313 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
315 result = pCryptGetUserKey(hProv, 0, &hKey2);
316 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
318 result = pCryptHashData(hHash, &temp, 1, 0);
319 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
321 result = pCryptHashSessionKey(hHash, hKey, 0);
322 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
324 result = pCryptImportKey(hProv, &temp, 1, (HCRYPTKEY)NULL, 0, &hKey2);
325 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
328 result = pCryptSignHashW(hHash, 0, NULL, 0, &temp, &dwLen);
329 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
330 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%d\n", GetLastError());
332 result = pCryptSetKeyParam(hKey, 0, &temp, 1);
333 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
335 result = pCryptSetHashParam(hHash, 0, &temp, 1);
336 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
338 result = pCryptSetProvParam(hProv, 0, &temp, 1);
339 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
341 result = pCryptVerifySignatureW(hHash, &temp, 1, hKey, NULL, 0);
342 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
343 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%d\n", GetLastError());
345 result = pCryptDestroyHash(hHash);
346 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
348 result = pCryptDestroyKey(hKey);
349 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
352 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
353 DWORD *pcbProvName, DWORD *pdwProvCount)
357 DWORD size = sizeof(DWORD);
359 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
362 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
363 NULL, NULL, NULL, NULL, NULL, NULL);
366 if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
369 RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
372 RegOpenKey(hKey, *pszProvName, &subkey);
373 RegQueryValueEx(subkey, "Type", NULL, NULL, (LPBYTE)pdwProvType, &size);
381 static void test_enum_providers(void)
383 /* expected results */
384 CHAR *pszProvName = NULL;
391 CHAR *provider = NULL;
397 DWORD notZeroFlags = 5;
399 if(!pCryptEnumProvidersA)
401 skip("CryptEnumProvidersA is not available\n");
405 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
407 skip("Could not find providers in registry\n");
411 /* check pdwReserved flag for NULL */
412 result = pCryptEnumProvidersA(dwIndex, ¬Null, 0, &type, NULL, &providerLen);
413 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
415 /* check dwFlags == 0 */
416 result = pCryptEnumProvidersA(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
417 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%d\n", GetLastError());
419 /* alloc provider to half the size required
420 * cbName holds the size required */
421 providerLen = cbName / 2;
422 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
425 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
426 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %d\n",
427 ERROR_MORE_DATA, GetLastError());
431 /* loop through the providers to get the number of providers
432 * after loop ends, count should be provCount + 1 so subtract 1
433 * to get actual number of providers */
435 while(pCryptEnumProvidersA(count++, NULL, 0, &type, NULL, &providerLen))
438 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
440 /* loop past the actual number of providers to get the error
441 * ERROR_NO_MORE_ITEMS */
442 for (count = 0; count < provCount + 1; count++)
443 result = pCryptEnumProvidersA(count, NULL, 0, &type, NULL, &providerLen);
444 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %d\n",
445 ERROR_NO_MORE_ITEMS, GetLastError());
447 /* check expected versus actual values returned */
448 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, NULL, &providerLen);
449 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
450 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
453 providerLen = 0xdeadbeef;
454 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
455 ok(result, "expected TRUE, got %d\n", result);
456 ok(type==dwType, "expected %d, got %d\n", dwType, type);
458 ok(!strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
459 ok(cbName==providerLen, "expected %d, got %d\n", cbName, providerLen);
464 static BOOL FindProvTypesRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszTypeName,
465 DWORD *pcbTypeName, DWORD *pdwTypeCount)
471 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
474 if (RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwTypeCount, pcbTypeName, NULL,
475 NULL, NULL, NULL, NULL, NULL))
479 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
482 if (RegEnumKeyEx(hKey, dwIndex, *pszTypeName, pcbTypeName, NULL, NULL, NULL, NULL))
485 ch = *pszTypeName + strlen(*pszTypeName);
486 /* Convert "Type 000" to 0, etc/ */
487 *pdwProvType = *(--ch) - '0';
488 *pdwProvType += (*(--ch) - '0') * 10;
489 *pdwProvType += (*(--ch) - '0') * 100;
491 if (RegOpenKey(hKey, *pszTypeName, &hSubKey))
494 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, NULL, pcbTypeName))
497 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
500 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, (LPBYTE)*pszTypeName, pcbTypeName))
503 RegCloseKey(hSubKey);
509 static void test_enum_provider_types(void)
511 /* expected values */
513 LPSTR pszTypeName = NULL;
520 LPSTR typeName = NULL;
525 DWORD notZeroFlags = 5;
527 if(!pCryptEnumProviderTypesA)
529 skip("CryptEnumProviderTypesA is not available\n");
533 if (!FindProvTypesRegVals(index, &dwProvType, &pszTypeName, &cbTypeName, &dwTypeCount))
535 skip("Could not find provider types in registry\n");
539 /* check pdwReserved for NULL */
540 result = pCryptEnumProviderTypesA(index, ¬Null, 0, &provType, typeName, &typeNameSize);
541 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %d\n",
542 ERROR_INVALID_PARAMETER, GetLastError());
544 /* check dwFlags == zero */
545 result = pCryptEnumProviderTypesA(index, NULL, notZeroFlags, &provType, typeName, &typeNameSize);
546 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %i, got %d\n",
547 ERROR_INVALID_PARAMETER, GetLastError());
549 /* alloc provider type to half the size required
550 * cbTypeName holds the size required */
551 typeNameSize = cbTypeName / 2;
552 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
555 /* This test fails under Win2k SP4:
556 result = TRUE, GetLastError() == 0xdeadbeef
557 SetLastError(0xdeadbeef);
558 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
559 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected 0/ERROR_MORE_DATA, got %d/%08lx\n",
560 result, GetLastError());
565 /* loop through the provider types to get the number of provider types
566 * after loop ends, count should be dwTypeCount + 1 so subtract 1
567 * to get actual number of provider types */
569 while(pCryptEnumProviderTypesA(typeCount++, NULL, 0, &provType, NULL, &typeNameSize))
572 ok(typeCount==dwTypeCount, "expected %d, got %d\n", dwTypeCount, typeCount);
574 /* loop past the actual number of provider types to get the error
575 * ERROR_NO_MORE_ITEMS */
576 for (typeCount = 0; typeCount < dwTypeCount + 1; typeCount++)
577 result = pCryptEnumProviderTypesA(typeCount, NULL, 0, &provType, NULL, &typeNameSize);
578 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %d\n",
579 ERROR_NO_MORE_ITEMS, GetLastError());
582 /* check expected versus actual values returned */
583 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, NULL, &typeNameSize);
584 ok(result && typeNameSize==cbTypeName, "expected %d, got %d\n", cbTypeName, typeNameSize);
585 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
588 typeNameSize = 0xdeadbeef;
589 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
590 ok(result, "expected TRUE, got %d\n", result);
591 ok(provType==dwProvType, "expected %d, got %d\n", dwProvType, provType);
593 ok(!strcmp(pszTypeName, typeName), "expected %s, got %s\n", pszTypeName, typeName);
594 ok(typeNameSize==cbTypeName, "expected %d, got %d\n", cbTypeName, typeNameSize);
599 static BOOL FindDfltProvRegVals(DWORD dwProvType, DWORD dwFlags, LPSTR *pszProvName, DWORD *pcbProvName)
604 DWORD user = dwFlags & CRYPT_USER_DEFAULT;
606 LPCSTR machinestr = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
607 LPCSTR userstr = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
609 keyname = LocalAlloc(LMEM_ZEROINIT, (user ? strlen(userstr) : strlen(machinestr)) + 1);
612 user ? strcpy(keyname, userstr) : strcpy(keyname, machinestr);
613 ptr = keyname + strlen(keyname);
614 *(--ptr) = (dwProvType % 10) + '0';
615 *(--ptr) = ((dwProvType / 10) % 10) + '0';
616 *(--ptr) = (dwProvType / 100) + '0';
620 if (RegOpenKey((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
627 if (RegQueryValueEx(hKey, "Name", NULL, NULL, (LPBYTE)*pszProvName, pcbProvName))
629 if (GetLastError() != ERROR_MORE_DATA)
630 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
634 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
637 if (RegQueryValueEx(hKey, "Name", NULL, NULL, (LPBYTE)*pszProvName, pcbProvName))
639 if (GetLastError() != ERROR_MORE_DATA)
640 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
649 static void test_get_default_provider(void)
651 /* expected results */
652 DWORD dwProvType = PROV_RSA_FULL;
653 DWORD dwFlags = CRYPT_MACHINE_DEFAULT;
654 LPSTR pszProvName = NULL;
658 DWORD provType = PROV_RSA_FULL;
659 DWORD flags = CRYPT_MACHINE_DEFAULT;
660 LPSTR provName = NULL;
665 if(!pCryptGetDefaultProviderA)
667 skip("CryptGetDefaultProviderA is not available\n");
671 if(!FindDfltProvRegVals(dwProvType, dwFlags, &pszProvName, &cbProvName))
673 skip("Could not find default provider in registry\n");
677 /* check pdwReserved for NULL */
678 result = pCryptGetDefaultProviderA(provType, ¬Null, flags, provName, &provNameSize);
679 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %d\n",
680 ERROR_INVALID_PARAMETER, GetLastError());
682 /* check for invalid flag */
684 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
685 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %d, got %d\n",
686 NTE_BAD_FLAGS, GetLastError());
687 flags = CRYPT_MACHINE_DEFAULT;
689 /* check for invalid prov type */
690 provType = 0xdeadbeef;
691 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
692 ok(!result && (GetLastError() == NTE_BAD_PROV_TYPE ||
693 GetLastError() == ERROR_INVALID_PARAMETER),
694 "expected NTE_BAD_PROV_TYPE or ERROR_INVALID_PARAMETER, got %d/%d\n",
695 result, GetLastError());
696 provType = PROV_RSA_FULL;
700 /* alloc provName to half the size required
701 * cbProvName holds the size required */
702 provNameSize = cbProvName / 2;
703 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
706 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
707 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %d\n",
708 ERROR_MORE_DATA, GetLastError());
712 /* check expected versus actual values returned */
713 result = pCryptGetDefaultProviderA(provType, NULL, flags, NULL, &provNameSize);
714 ok(result && provNameSize==cbProvName, "expected %d, got %d\n", cbProvName, provNameSize);
715 provNameSize = cbProvName;
717 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
720 provNameSize = 0xdeadbeef;
721 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
722 ok(result, "expected TRUE, got %d\n", result);
724 ok(!strcmp(pszProvName, provName), "expected %s, got %s\n", pszProvName, provName);
725 ok(provNameSize==cbProvName, "expected %d, got %d\n", cbProvName, provNameSize);
730 static void test_set_provider_ex(void)
736 LPSTR pszProvName = NULL;
739 if(!pCryptGetDefaultProviderA || !pCryptSetProviderExA)
741 skip("CryptGetDefaultProviderA and/or CryptSetProviderExA are not available\n");
745 /* check pdwReserved for NULL */
746 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, ¬Null, CRYPT_MACHINE_DEFAULT);
747 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %d\n",
748 ERROR_INVALID_PARAMETER, GetLastError());
750 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
751 SetLastError(0xdeadbeef);
752 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT | CRYPT_DELETE_DEFAULT);
753 if (!result && (GetLastError() == ERROR_ACCESS_DENIED))
755 skip("Not enough rights to remove the default provider\n");
758 ok(result, "%d\n", GetLastError());
760 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
761 ok(result, "%d\n", GetLastError());
763 /* call CryptGetDefaultProvider to see if they match */
764 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &cbProvName);
765 if (!(pszProvName = LocalAlloc(LMEM_ZEROINIT, cbProvName)))
768 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, pszProvName, &cbProvName);
769 ok(result && !strcmp(MS_DEF_PROV, pszProvName), "expected %s, got %s\n", MS_DEF_PROV, pszProvName);
770 ok(result && cbProvName==(strlen(MS_DEF_PROV) + 1), "expected %i, got %d\n", (lstrlenA(MS_DEF_PROV) + 1), cbProvName);
772 LocalFree(pszProvName);
777 init_function_pointers();
778 if(pCryptAcquireContextA && pCryptReleaseContext) {
780 test_acquire_context();
781 test_incorrect_api_usage();
782 clean_up_environment();
785 test_enum_providers();
786 test_enum_provider_types();
787 test_get_default_provider();
788 test_set_provider_ex();