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);
45 static void init_function_pointers(void)
47 hadvapi32 = GetModuleHandleA("advapi32.dll");
51 pCryptAcquireContextA = (void*)GetProcAddress(hadvapi32, "CryptAcquireContextA");
52 pCryptEnumProviderTypesA = (void*)GetProcAddress(hadvapi32, "CryptEnumProviderTypesA");
53 pCryptEnumProvidersA = (void*)GetProcAddress(hadvapi32, "CryptEnumProvidersA");
54 pCryptGetDefaultProviderA = (void*)GetProcAddress(hadvapi32, "CryptGetDefaultProviderA");
55 pCryptReleaseContext = (void*)GetProcAddress(hadvapi32, "CryptReleaseContext");
56 pCryptSetProviderExA = (void*)GetProcAddress(hadvapi32, "CryptSetProviderExA");
61 static void init_environment(void)
65 /* Ensure that container "wine_test_keyset" does exist */
66 if (!pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
68 pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
70 pCryptReleaseContext(hProv, 0);
72 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
73 if (!pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
75 pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
77 pCryptReleaseContext(hProv, 0);
79 /* Ensure that container "wine_test_bad_keyset" does not exist. */
80 if (pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
82 pCryptReleaseContext(hProv, 0);
83 pCryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
87 static void clean_up_environment(void)
91 /* Remove container "wine_test_keyset" */
92 if (pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
94 pCryptReleaseContext(hProv, 0);
95 pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
98 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
99 if (pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
101 pCryptReleaseContext(hProv, 0);
102 pCryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
106 static void test_acquire_context(void)
111 /* Provoke all kinds of error conditions (which are easy to provoke).
112 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
113 * but since this is likely to change between CSP versions, we don't check
114 * this. Please don't change the order of tests. */
115 result = pCryptAcquireContextA(&hProv, NULL, NULL, 0, 0);
116 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
118 result = pCryptAcquireContextA(&hProv, NULL, NULL, 1000, 0);
119 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
121 result = pCryptAcquireContextA(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
122 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%ld\n", GetLastError());
124 result = pCryptAcquireContextA(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
125 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%ld\n", GetLastError());
127 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
128 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%ld\n", GetLastError());
130 /* This test fails under Win2k SP4:
131 result = TRUE, GetLastError() == ERROR_INVALID_PARAMETER
132 SetLastError(0xdeadbeef);
133 result = pCryptAcquireContextA(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
134 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d/%ld\n", result, GetLastError());
137 /* Last not least, try to really acquire a context. */
139 SetLastError(0xdeadbeef);
140 result = pCryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
141 ok(result && (GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_RING2_STACK_IN_USE), "%d/%ld\n", result, GetLastError());
144 pCryptReleaseContext(hProv, 0);
146 /* Try again, witch an empty ("\0") szProvider parameter */
148 SetLastError(0xdeadbeef);
149 result = pCryptAcquireContextA(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
150 ok(result && (GetLastError() == ERROR_SUCCESS || GetLastError() == ERROR_RING2_STACK_IN_USE), "%d/%ld\n", result, GetLastError());
153 pCryptReleaseContext(hProv, 0);
156 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
157 DWORD *pcbProvName, DWORD *pdwProvCount)
161 DWORD size = sizeof(DWORD);
163 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
166 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
167 NULL, NULL, NULL, NULL, NULL, NULL);
170 if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
173 RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
176 RegOpenKey(hKey, *pszProvName, &subkey);
177 RegQueryValueEx(subkey, "Type", NULL, NULL, (BYTE*)pdwProvType, &size);
185 static void test_enum_providers(void)
187 /* expected results */
188 CHAR *pszProvName = NULL;
195 CHAR *provider = NULL;
201 DWORD notZeroFlags = 5;
203 if(!pCryptEnumProvidersA)
205 trace("skipping CryptEnumProviders tests\n");
209 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
212 /* check pdwReserved flag for NULL */
213 result = pCryptEnumProvidersA(dwIndex, ¬Null, 0, &type, NULL, &providerLen);
214 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
216 /* check dwFlags == 0 */
217 result = pCryptEnumProvidersA(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
218 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%ld\n", GetLastError());
220 /* alloc provider to half the size required
221 * cbName holds the size required */
222 providerLen = cbName / 2;
223 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
226 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
227 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
228 ERROR_MORE_DATA, GetLastError());
232 /* loop through the providers to get the number of providers
233 * after loop ends, count should be provCount + 1 so subtract 1
234 * to get actual number of providers */
236 while(pCryptEnumProvidersA(count++, NULL, 0, &type, NULL, &providerLen))
239 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
241 /* loop past the actual number of providers to get the error
242 * ERROR_NO_MORE_ITEMS */
243 for (count = 0; count < provCount + 1; count++)
244 result = pCryptEnumProvidersA(count, NULL, 0, &type, NULL, &providerLen);
245 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
246 ERROR_NO_MORE_ITEMS, GetLastError());
248 /* check expected versus actual values returned */
249 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, NULL, &providerLen);
250 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
251 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
254 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
255 ok(result && type==dwType, "expected %ld, got %ld\n",
257 ok(result && !strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
258 ok(result && cbName==providerLen, "expected %ld, got %ld\n",
259 cbName, providerLen);
264 static BOOL FindProvTypesRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszTypeName,
265 DWORD *pcbTypeName, DWORD *pdwTypeCount)
271 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
274 if (RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwTypeCount, pcbTypeName, NULL,
275 NULL, NULL, NULL, NULL, NULL))
279 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
282 if (RegEnumKeyEx(hKey, dwIndex, *pszTypeName, pcbTypeName, NULL, NULL, NULL, NULL))
285 ch = *pszTypeName + strlen(*pszTypeName);
286 /* Convert "Type 000" to 0, etc/ */
287 *pdwProvType = *(--ch) - '0';
288 *pdwProvType += (*(--ch) - '0') * 10;
289 *pdwProvType += (*(--ch) - '0') * 100;
291 if (RegOpenKey(hKey, *pszTypeName, &hSubKey))
294 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, NULL, pcbTypeName))
297 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
300 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, *pszTypeName, pcbTypeName))
303 RegCloseKey(hSubKey);
309 static void test_enum_provider_types()
311 /* expected values */
313 LPSTR pszTypeName = NULL;
320 LPSTR typeName = NULL;
325 DWORD notZeroFlags = 5;
327 if(!pCryptEnumProviderTypesA)
329 trace("skipping CryptEnumProviderTypes tests\n");
333 if (!FindProvTypesRegVals(index, &dwProvType, &pszTypeName, &cbTypeName, &dwTypeCount))
335 trace("could not find provider types in registry, skipping the test\n");
339 /* check pdwReserved for NULL */
340 result = pCryptEnumProviderTypesA(index, ¬Null, 0, &provType, typeName, &typeNameSize);
341 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
342 ERROR_INVALID_PARAMETER, GetLastError());
344 /* check dwFlags == zero */
345 result = pCryptEnumProviderTypesA(index, NULL, notZeroFlags, &provType, typeName, &typeNameSize);
346 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %i, got %ld\n",
347 ERROR_INVALID_PARAMETER, GetLastError());
349 /* alloc provider type to half the size required
350 * cbTypeName holds the size required */
351 typeNameSize = cbTypeName / 2;
352 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
355 /* This test fails under Win2k SP4:
356 result = TRUE, GetLastError() == 0xdeadbeef
357 SetLastError(0xdeadbeef);
358 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
359 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected 0/ERROR_MORE_DATA, got %d/%08lx\n",
360 result, GetLastError());
365 /* loop through the provider types to get the number of provider types
366 * after loop ends, count should be dwTypeCount + 1 so subtract 1
367 * to get actual number of provider types */
369 while(pCryptEnumProviderTypesA(typeCount++, NULL, 0, &provType, NULL, &typeNameSize))
372 ok(typeCount==dwTypeCount, "expected %ld, got %ld\n", dwTypeCount, typeCount);
374 /* loop past the actual number of provider types to get the error
375 * ERROR_NO_MORE_ITEMS */
376 for (typeCount = 0; typeCount < dwTypeCount + 1; typeCount++)
377 result = pCryptEnumProviderTypesA(typeCount, NULL, 0, &provType, NULL, &typeNameSize);
378 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
379 ERROR_NO_MORE_ITEMS, GetLastError());
382 /* check expected versus actual values returned */
383 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, NULL, &typeNameSize);
384 ok(result && typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
385 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
388 typeNameSize = 0xdeadbeef;
389 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
390 ok(result, "expected TRUE, got %ld\n", result);
391 ok(provType==dwProvType, "expected %ld, got %ld\n", dwProvType, provType);
393 ok(!strcmp(pszTypeName, typeName), "expected %s, got %s\n", pszTypeName, typeName);
394 ok(typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
399 static BOOL FindDfltProvRegVals(DWORD dwProvType, DWORD dwFlags, LPSTR *pszProvName, DWORD *pcbProvName)
404 DWORD user = dwFlags & CRYPT_USER_DEFAULT;
406 LPSTR MACHINESTR = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
407 LPSTR USERSTR = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
409 keyname = LocalAlloc(LMEM_ZEROINIT, (user ? strlen(USERSTR) : strlen(MACHINESTR)) + 1);
412 user ? strcpy(keyname, USERSTR) : strcpy(keyname, MACHINESTR);
413 ptr = keyname + strlen(keyname);
414 *(--ptr) = (dwProvType % 10) + '0';
415 *(--ptr) = ((dwProvType / 10) % 10) + '0';
416 *(--ptr) = (dwProvType / 100) + '0';
420 if (RegOpenKey((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
427 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
429 if (GetLastError() != ERROR_MORE_DATA)
430 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
434 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
437 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
439 if (GetLastError() != ERROR_MORE_DATA)
440 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
449 static void test_get_default_provider()
451 /* expected results */
452 DWORD dwProvType = PROV_RSA_FULL;
453 DWORD dwFlags = CRYPT_MACHINE_DEFAULT;
454 LPSTR pszProvName = NULL;
458 DWORD provType = PROV_RSA_FULL;
459 DWORD flags = CRYPT_MACHINE_DEFAULT;
460 LPSTR provName = NULL;
465 if(!pCryptGetDefaultProviderA)
467 trace("skipping CryptGetDefaultProvider tests\n");
471 FindDfltProvRegVals(dwProvType, dwFlags, &pszProvName, &cbProvName);
473 /* check pdwReserved for NULL */
474 result = pCryptGetDefaultProviderA(provType, ¬Null, flags, provName, &provNameSize);
475 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
476 ERROR_INVALID_PARAMETER, GetLastError());
478 /* check for invalid flag */
480 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
481 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %ld, got %ld\n",
482 NTE_BAD_FLAGS, GetLastError());
483 flags = CRYPT_MACHINE_DEFAULT;
485 /* check for invalid prov type */
486 provType = 0xdeadbeef;
487 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
488 ok(!result && (GetLastError() == NTE_BAD_PROV_TYPE ||
489 GetLastError() == ERROR_INVALID_PARAMETER),
490 "expected NTE_BAD_PROV_TYPE or ERROR_INVALID_PARAMETER, got %ld/%ld\n",
491 result, GetLastError());
492 provType = PROV_RSA_FULL;
496 /* alloc provName to half the size required
497 * cbProvName holds the size required */
498 provNameSize = cbProvName / 2;
499 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
502 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
503 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
504 ERROR_MORE_DATA, GetLastError());
508 /* check expected versus actual values returned */
509 result = pCryptGetDefaultProviderA(provType, NULL, flags, NULL, &provNameSize);
510 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
511 provNameSize = cbProvName;
513 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
516 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
517 ok(result && !strcmp(pszProvName, provName), "expected %s, got %s\n", pszProvName, provName);
518 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
523 static void test_set_provider_ex()
529 LPSTR pszProvName = NULL;
532 if(!pCryptGetDefaultProviderA || !pCryptSetProviderExA)
534 trace("skipping CryptSetProviderEx tests\n");
538 /* check pdwReserved for NULL */
539 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, ¬Null, CRYPT_MACHINE_DEFAULT);
540 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
541 ERROR_INVALID_PARAMETER, GetLastError());
543 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
544 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT | CRYPT_DELETE_DEFAULT);
545 ok(result, "%ld\n", GetLastError());
547 result = pCryptSetProviderExA(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
548 ok(result, "%ld\n", GetLastError());
550 /* call CryptGetDefaultProvider to see if they match */
551 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &cbProvName);
552 if (!(pszProvName = LocalAlloc(LMEM_ZEROINIT, cbProvName)))
555 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, pszProvName, &cbProvName);
556 ok(result && !strcmp(MS_DEF_PROV, pszProvName), "expected %s, got %s\n", MS_DEF_PROV, pszProvName);
557 ok(result && cbProvName==(strlen(MS_DEF_PROV) + 1), "expected %i, got %ld\n", (strlen(MS_DEF_PROV) + 1), cbProvName);
559 LocalFree(pszProvName);
564 init_function_pointers();
565 if(pCryptAcquireContextA && pCryptReleaseContext) {
567 test_acquire_context();
568 clean_up_environment();
571 test_enum_providers();
572 test_enum_provider_types();
573 test_get_default_provider();
574 test_set_provider_ex();