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
23 #include "wine/test.h"
30 static const char szRsaBaseProv[] = MS_DEF_PROV_A;
31 static const char szNonExistentProv[] = "Wine Non Existent Cryptographic Provider v11.2";
32 static const char szKeySet[] = "wine_test_keyset";
33 static const char szBadKeySet[] = "wine_test_bad_keyset";
34 #define NON_DEF_PROV_TYPE 999
36 static void init_environment(void)
40 /* Ensure that container "wine_test_keyset" does exist */
41 if (!CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
43 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
45 CryptReleaseContext(hProv, 0);
47 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
48 if (!CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
50 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
52 CryptReleaseContext(hProv, 0);
54 /* Ensure that container "wine_test_bad_keyset" does not exist. */
55 if (CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
57 CryptReleaseContext(hProv, 0);
58 CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
62 static void clean_up_environment(void)
66 /* Remove container "wine_test_keyset" */
67 if (CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
69 CryptReleaseContext(hProv, 0);
70 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
73 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
74 if (CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
76 CryptReleaseContext(hProv, 0);
77 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
81 static void test_acquire_context(void)
86 /* Provoke all kinds of error conditions (which are easy to provoke).
87 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
88 * but since this is likely to change between CSP versions, we don't check
89 * this. Please don't change the order of tests. */
90 result = CryptAcquireContext(&hProv, NULL, NULL, 0, 0);
91 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
93 result = CryptAcquireContext(&hProv, NULL, NULL, 1000, 0);
94 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
96 result = CryptAcquireContext(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
97 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%ld\n", GetLastError());
99 result = CryptAcquireContext(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
100 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%ld\n", GetLastError());
102 result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
103 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%ld\n", GetLastError());
105 result = CryptAcquireContext(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
106 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
108 /* Last not least, try to really acquire a context. */
109 result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
110 ok(result, "%ld\n", GetLastError());
112 if (GetLastError() == ERROR_SUCCESS)
113 CryptReleaseContext(hProv, 0);
115 /* Try again, witch an empty ("\0") szProvider parameter */
116 result = CryptAcquireContext(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
117 ok(result, "%ld\n", GetLastError());
119 if (GetLastError() == ERROR_SUCCESS)
120 CryptReleaseContext(hProv, 0);
123 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
124 DWORD *pcbProvName, DWORD *pdwProvCount)
128 DWORD size = sizeof(DWORD);
130 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
133 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
134 NULL, NULL, NULL, NULL, NULL, NULL);
137 if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
140 RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
143 RegOpenKey(hKey, *pszProvName, &subkey);
144 RegQueryValueEx(subkey, "Type", NULL, NULL, (BYTE*)pdwProvType, &size);
152 static void test_enum_providers(void)
154 /* expected results */
155 CHAR *pszProvName = NULL;
162 CHAR *provider = NULL;
168 DWORD notZeroFlags = 5;
170 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
173 /* check pdwReserved flag for NULL */
174 result = CryptEnumProviders(dwIndex, ¬Null, 0, &type, NULL, &providerLen);
175 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
177 /* check dwFlags == 0 */
178 result = CryptEnumProviders(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
179 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%ld\n", GetLastError());
181 /* alloc provider to half the size required
182 * cbName holds the size required */
183 providerLen = cbName / 2;
184 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
187 result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
188 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
189 ERROR_MORE_DATA, GetLastError());
193 /* loop through the providers to get the number of providers
194 * after loop ends, count should be provCount + 1 so subtract 1
195 * to get actual number of providers */
197 while(CryptEnumProviders(count++, NULL, 0, &type, NULL, &providerLen))
200 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
202 /* loop past the actual number of providers to get the error
203 * ERROR_NO_MORE_ITEMS */
204 for (count = 0; count < provCount + 1; count++)
205 result = CryptEnumProviders(count, NULL, 0, &type, NULL, &providerLen);
206 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
207 ERROR_NO_MORE_ITEMS, GetLastError());
209 /* check expected versus actual values returned */
210 result = CryptEnumProviders(dwIndex, NULL, 0, &type, NULL, &providerLen);
211 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
212 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
215 result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
216 ok(result && type==dwType, "expected %ld, got %ld\n",
218 ok(result && !strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
219 ok(result && cbName==providerLen, "expected %ld, got %ld\n",
220 cbName, providerLen);
223 static BOOL FindProvTypesRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszTypeName,
224 DWORD *pcbTypeName, DWORD *pdwTypeCount)
230 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
233 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwTypeCount, pcbTypeName, NULL,
234 NULL, NULL, NULL, NULL, NULL);
237 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
240 RegEnumKeyEx(hKey, dwIndex, *pszTypeName, pcbTypeName, NULL, NULL, NULL, NULL);
242 ch = *pszTypeName + strlen(*pszTypeName);
243 /* Convert "Type 000" to 0, etc/ */
244 *pdwProvType = *(--ch) - '0';
245 *pdwProvType += (*(--ch) - '0') * 10;
246 *pdwProvType += (*(--ch) - '0') * 100;
248 RegOpenKey(hKey, *pszTypeName, &hSubKey);
249 LocalFree(*pszTypeName);
251 RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, NULL, pcbTypeName);
252 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
255 RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, *pszTypeName, pcbTypeName);
257 RegCloseKey(hSubKey);
263 static void test_enum_provider_types()
265 /* expected values */
267 LPSTR pszTypeName = NULL;
274 LPSTR typeName = NULL;
279 DWORD notZeroFlags = 5;
281 if (!FindProvTypesRegVals(index, &dwProvType, &pszTypeName, &cbTypeName, &dwTypeCount))
284 /* check pdwReserved for NULL */
285 result = CryptEnumProviderTypes(index, ¬Null, 0, &provType, typeName, &typeNameSize);
286 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
287 ERROR_INVALID_PARAMETER, GetLastError());
289 /* check dwFlags == zero */
290 result = CryptEnumProviderTypes(index, NULL, notZeroFlags, &provType, typeName, &typeNameSize);
291 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %i, got %ld\n",
292 ERROR_INVALID_PARAMETER, GetLastError());
294 /* alloc provider type to half the size required
295 * cbTypeName holds the size required */
296 typeNameSize = cbTypeName / 2;
297 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
300 result = CryptEnumProviderTypes(index, NULL, 0, &provType, typeName, &typeNameSize);
301 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
302 ERROR_MORE_DATA, GetLastError());
306 /* loop through the provider types to get the number of provider types
307 * after loop ends, count should be dwTypeCount + 1 so subtract 1
308 * to get actual number of provider types */
310 while(CryptEnumProviderTypes(typeCount++, NULL, 0, &provType, NULL, &typeNameSize))
313 ok(typeCount==dwTypeCount, "expected %ld, got %ld\n", dwTypeCount, typeCount);
315 /* loop past the actual number of provider types to get the error
316 * ERROR_NO_MORE_ITEMS */
317 for (typeCount = 0; typeCount < dwTypeCount + 1; typeCount++)
318 result = CryptEnumProviderTypes(typeCount, NULL, 0, &provType, NULL, &typeNameSize);
319 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
320 ERROR_NO_MORE_ITEMS, GetLastError());
323 /* check expected versus actual values returned */
324 result = CryptEnumProviderTypes(index, NULL, 0, &provType, NULL, &typeNameSize);
325 ok(result && typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
326 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
329 result = CryptEnumProviderTypes(index, NULL, 0, &provType, typeName, &typeNameSize);
330 ok(result && provType==dwProvType, "expected %ld, got %ld\n", dwProvType, provType);
331 ok(result && !strcmp(pszTypeName, typeName), "expected %s, got %s\n", pszTypeName, typeName);
332 ok(result && typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
335 static BOOL FindDfltProvRegVals(DWORD dwProvType, DWORD dwFlags, LPSTR *pszProvName, DWORD *pcbProvName)
340 DWORD user = dwFlags & CRYPT_USER_DEFAULT;
342 LPSTR MACHINESTR = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
343 LPSTR USERSTR = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
345 keyname = LocalAlloc(LMEM_ZEROINIT, (user ? strlen(USERSTR) : strlen(MACHINESTR)) + 1);
348 user ? strcpy(keyname, USERSTR) : strcpy(keyname, MACHINESTR);
349 ptr = keyname + strlen(keyname);
350 *(--ptr) = (dwProvType % 10) + '0';
351 *(--ptr) = ((dwProvType / 10) % 10) + '0';
352 *(--ptr) = (dwProvType / 100) + '0';
356 if (RegOpenKey((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
363 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
365 if (GetLastError() != ERROR_MORE_DATA)
366 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
370 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
373 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
375 if (GetLastError() != ERROR_MORE_DATA)
376 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
385 static void test_get_default_provider()
387 /* expected results */
388 DWORD dwProvType = PROV_RSA_FULL;
389 DWORD dwFlags = CRYPT_MACHINE_DEFAULT;
390 LPSTR pszProvName = NULL;
394 DWORD provType = PROV_RSA_FULL;
395 DWORD flags = CRYPT_MACHINE_DEFAULT;
396 LPSTR provName = NULL;
401 FindDfltProvRegVals(dwProvType, dwFlags, &pszProvName, &cbProvName);
403 /* check pdwReserved for NULL */
404 result = CryptGetDefaultProvider(provType, ¬Null, flags, provName, &provNameSize);
405 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
406 ERROR_INVALID_PARAMETER, GetLastError());
408 /* check for invalid flag */
410 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
411 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %ld, got %ld\n",
412 NTE_BAD_FLAGS, GetLastError());
413 flags = CRYPT_MACHINE_DEFAULT;
415 /* check for invalid prov type */
416 provType = 0xdeadbeef;
417 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
418 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "expected %ld, got %ld\n",
419 NTE_BAD_PROV_TYPE, GetLastError());
420 provType = PROV_RSA_FULL;
424 /* alloc provName to half the size required
425 * cbProvName holds the size required */
426 provNameSize = cbProvName / 2;
427 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
430 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
431 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
432 ERROR_MORE_DATA, GetLastError());
436 /* check expected versus actual values returned */
437 result = CryptGetDefaultProvider(provType, NULL, flags, NULL, &provNameSize);
438 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
439 provNameSize = cbProvName;
441 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
444 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
445 ok(result && !strcmp(pszProvName, provName), "expected %s, got %s\n", pszProvName, provName);
446 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
449 static void test_set_provider_ex()
455 LPSTR pszProvName = NULL;
458 /* check pdwReserved for NULL */
459 result = CryptSetProviderEx(MS_DEF_PROV, PROV_RSA_FULL, ¬Null, CRYPT_MACHINE_DEFAULT);
460 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
461 ERROR_INVALID_PARAMETER, GetLastError());
463 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
464 result = CryptSetProviderEx(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT | CRYPT_DELETE_DEFAULT);
465 ok(result, "%ld\n", GetLastError());
467 result = CryptSetProviderEx(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
468 ok(result, "%ld\n", GetLastError());
470 /* call CryptGetDefaultProvider to see if they match */
471 result = CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &cbProvName);
472 if (!(pszProvName = LocalAlloc(LMEM_ZEROINIT, cbProvName)))
475 result = CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, pszProvName, &cbProvName);
476 ok(result && !strcmp(MS_DEF_PROV, pszProvName), "expected %s, got %s\n", MS_DEF_PROV, pszProvName);
477 ok(result && cbProvName==(strlen(MS_DEF_PROV) + 1), "expected %i, got %ld\n", (strlen(MS_DEF_PROV) + 1), cbProvName);
483 test_acquire_context();
484 clean_up_environment();
486 test_enum_providers();
487 test_enum_provider_types();
488 test_get_default_provider();
489 test_set_provider_ex();
490 test_set_provider_ex();