- Fix typo in error message
[wine/testsucceed.git] / dlls / advapi32 / tests / crypt.c
bloba4b8cd8bf7d7fdbf256035b13c8051a83d3992b7
1 /*
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wincrypt.h"
26 #include "winerror.h"
27 #include "winreg.h"
29 #include "wine/test.h"
31 static const char szRsaBaseProv[] = MS_DEF_PROV_A;
32 static const char szNonExistentProv[] = "Wine Non Existent 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 void init_environment(void)
39 HCRYPTPROV hProv;
41 /* Ensure that container "wine_test_keyset" does exist */
42 if (!CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
44 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
46 CryptReleaseContext(hProv, 0);
48 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
49 if (!CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
51 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
53 CryptReleaseContext(hProv, 0);
55 /* Ensure that container "wine_test_bad_keyset" does not exist. */
56 if (CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
58 CryptReleaseContext(hProv, 0);
59 CryptAcquireContext(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
63 static void clean_up_environment(void)
65 HCRYPTPROV hProv;
67 /* Remove container "wine_test_keyset" */
68 if (CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
70 CryptReleaseContext(hProv, 0);
71 CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
74 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
75 if (CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
77 CryptReleaseContext(hProv, 0);
78 CryptAcquireContext(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
82 static void test_acquire_context(void)
84 BOOL result;
85 HCRYPTPROV hProv;
87 /* Provoke all kinds of error conditions (which are easy to provoke).
88 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
89 * but since this is likely to change between CSP versions, we don't check
90 * this. Please don't change the order of tests. */
91 result = CryptAcquireContext(&hProv, NULL, NULL, 0, 0);
92 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
94 result = CryptAcquireContext(&hProv, NULL, NULL, 1000, 0);
95 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%ld\n", GetLastError());
97 result = CryptAcquireContext(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
98 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%ld\n", GetLastError());
100 result = CryptAcquireContext(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
101 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%ld\n", GetLastError());
103 result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
104 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%ld\n", GetLastError());
106 /* This test fails under Win2k SP4:
107 result = TRUE, GetLastError() == ERROR_INVALID_PARAMETER
108 SetLastError(0xdeadbeef);
109 result = CryptAcquireContext(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
110 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d/%ld\n", result, GetLastError());
113 /* Last not least, try to really acquire a context. */
114 hProv = 0;
115 SetLastError(0xdeadbeef);
116 result = CryptAcquireContext(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
117 ok(result && GetLastError() == ERROR_SUCCESS, "%d/%ld\n", result, GetLastError());
119 if (hProv)
120 CryptReleaseContext(hProv, 0);
122 /* Try again, witch an empty ("\0") szProvider parameter */
123 hProv = 0;
124 SetLastError(0xdeadbeef);
125 result = CryptAcquireContext(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
126 ok(result && GetLastError() == ERROR_SUCCESS, "%d/%ld\n", result, GetLastError());
128 if (hProv)
129 CryptReleaseContext(hProv, 0);
132 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
133 DWORD *pcbProvName, DWORD *pdwProvCount)
135 HKEY hKey;
136 HKEY subkey;
137 DWORD size = sizeof(DWORD);
139 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
140 return FALSE;
142 RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
143 NULL, NULL, NULL, NULL, NULL, NULL);
144 (*pcbProvName)++;
146 if (!(*pszProvName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbProvName))))
147 return FALSE;
149 RegEnumKeyEx(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
150 (*pcbProvName)++;
152 RegOpenKey(hKey, *pszProvName, &subkey);
153 RegQueryValueEx(subkey, "Type", NULL, NULL, (BYTE*)pdwProvType, &size);
155 RegCloseKey(subkey);
156 RegCloseKey(hKey);
158 return TRUE;
161 static void test_enum_providers(void)
163 /* expected results */
164 CHAR *pszProvName = NULL;
165 DWORD cbName;
166 DWORD dwType;
167 DWORD provCount;
168 DWORD dwIndex = 0;
170 /* actual results */
171 CHAR *provider = NULL;
172 DWORD providerLen;
173 DWORD type;
174 DWORD count;
175 BOOL result;
176 DWORD notNull = 5;
177 DWORD notZeroFlags = 5;
179 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
180 return;
182 /* check pdwReserved flag for NULL */
183 result = CryptEnumProviders(dwIndex, &notNull, 0, &type, NULL, &providerLen);
184 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%ld\n", GetLastError());
186 /* check dwFlags == 0 */
187 result = CryptEnumProviders(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
188 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%ld\n", GetLastError());
190 /* alloc provider to half the size required
191 * cbName holds the size required */
192 providerLen = cbName / 2;
193 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
194 return;
196 result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
197 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
198 ERROR_MORE_DATA, GetLastError());
200 LocalFree(provider);
202 /* loop through the providers to get the number of providers
203 * after loop ends, count should be provCount + 1 so subtract 1
204 * to get actual number of providers */
205 count = 0;
206 while(CryptEnumProviders(count++, NULL, 0, &type, NULL, &providerLen))
208 count--;
209 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
211 /* loop past the actual number of providers to get the error
212 * ERROR_NO_MORE_ITEMS */
213 for (count = 0; count < provCount + 1; count++)
214 result = CryptEnumProviders(count, NULL, 0, &type, NULL, &providerLen);
215 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
216 ERROR_NO_MORE_ITEMS, GetLastError());
218 /* check expected versus actual values returned */
219 result = CryptEnumProviders(dwIndex, NULL, 0, &type, NULL, &providerLen);
220 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
221 if (!(provider = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, providerLen))))
222 return;
224 result = CryptEnumProviders(dwIndex, NULL, 0, &type, provider, &providerLen);
225 ok(result && type==dwType, "expected %ld, got %ld\n",
226 dwType, type);
227 ok(result && !strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
228 ok(result && cbName==providerLen, "expected %ld, got %ld\n",
229 cbName, providerLen);
232 static BOOL FindProvTypesRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszTypeName,
233 DWORD *pcbTypeName, DWORD *pdwTypeCount)
235 HKEY hKey;
236 HKEY hSubKey;
237 PSTR ch;
239 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
240 return FALSE;
242 if (RegQueryInfoKey(hKey, NULL, NULL, NULL, pdwTypeCount, pcbTypeName, NULL,
243 NULL, NULL, NULL, NULL, NULL))
244 return FALSE;
245 (*pcbTypeName)++;
247 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
248 return FALSE;
250 if (RegEnumKeyEx(hKey, dwIndex, *pszTypeName, pcbTypeName, NULL, NULL, NULL, NULL))
251 return FALSE;
252 (*pcbTypeName)++;
253 ch = *pszTypeName + strlen(*pszTypeName);
254 /* Convert "Type 000" to 0, etc/ */
255 *pdwProvType = *(--ch) - '0';
256 *pdwProvType += (*(--ch) - '0') * 10;
257 *pdwProvType += (*(--ch) - '0') * 100;
259 if (RegOpenKey(hKey, *pszTypeName, &hSubKey))
260 return FALSE;
262 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, NULL, pcbTypeName))
263 return FALSE;
265 if (!(*pszTypeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, *pcbTypeName))))
266 return FALSE;
268 if (RegQueryValueEx(hSubKey, "TypeName", NULL, NULL, *pszTypeName, pcbTypeName))
269 return FALSE;
271 RegCloseKey(hSubKey);
272 RegCloseKey(hKey);
274 return TRUE;
277 static void test_enum_provider_types()
279 /* expected values */
280 DWORD dwProvType;
281 LPSTR pszTypeName = NULL;
282 DWORD cbTypeName;
283 DWORD dwTypeCount;
285 /* actual values */
286 DWORD index = 0;
287 DWORD provType;
288 LPSTR typeName = NULL;
289 DWORD typeNameSize;
290 DWORD typeCount;
291 DWORD result;
292 DWORD notNull = 5;
293 DWORD notZeroFlags = 5;
295 if (!FindProvTypesRegVals(index, &dwProvType, &pszTypeName, &cbTypeName, &dwTypeCount))
297 trace("could not find provider types in registry, skipping the test\n");
298 return;
301 /* check pdwReserved for NULL */
302 result = CryptEnumProviderTypes(index, &notNull, 0, &provType, typeName, &typeNameSize);
303 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
304 ERROR_INVALID_PARAMETER, GetLastError());
306 /* check dwFlags == zero */
307 result = CryptEnumProviderTypes(index, NULL, notZeroFlags, &provType, typeName, &typeNameSize);
308 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %i, got %ld\n",
309 ERROR_INVALID_PARAMETER, GetLastError());
311 /* alloc provider type to half the size required
312 * cbTypeName holds the size required */
313 typeNameSize = cbTypeName / 2;
314 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
315 return;
317 /* This test fails under Win2k SP4:
318 result = TRUE, GetLastError() == 0xdeadbeef
319 SetLastError(0xdeadbeef);
320 result = CryptEnumProviderTypes(index, NULL, 0, &provType, typeName, &typeNameSize);
321 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected 0/ERROR_MORE_DATA, got %d/%08lx\n",
322 result, GetLastError());
325 LocalFree(typeName);
327 /* loop through the provider types to get the number of provider types
328 * after loop ends, count should be dwTypeCount + 1 so subtract 1
329 * to get actual number of provider types */
330 typeCount = 0;
331 while(CryptEnumProviderTypes(typeCount++, NULL, 0, &provType, NULL, &typeNameSize))
333 typeCount--;
334 ok(typeCount==dwTypeCount, "expected %ld, got %ld\n", dwTypeCount, typeCount);
336 /* loop past the actual number of provider types to get the error
337 * ERROR_NO_MORE_ITEMS */
338 for (typeCount = 0; typeCount < dwTypeCount + 1; typeCount++)
339 result = CryptEnumProviderTypes(typeCount, NULL, 0, &provType, NULL, &typeNameSize);
340 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %ld\n",
341 ERROR_NO_MORE_ITEMS, GetLastError());
344 /* check expected versus actual values returned */
345 result = CryptEnumProviderTypes(index, NULL, 0, &provType, NULL, &typeNameSize);
346 ok(result && typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
347 if (!(typeName = ((LPSTR)LocalAlloc(LMEM_ZEROINIT, typeNameSize))))
348 return;
350 typeNameSize = 0xdeadbeef;
351 result = CryptEnumProviderTypes(index, NULL, 0, &provType, typeName, &typeNameSize);
352 ok(result, "expected TRUE, got %ld\n", result);
353 ok(provType==dwProvType, "expected %ld, got %ld\n", dwProvType, provType);
354 if (pszTypeName)
355 ok(!strcmp(pszTypeName, typeName), "expected %s, got %s\n", pszTypeName, typeName);
356 ok(typeNameSize==cbTypeName, "expected %ld, got %ld\n", cbTypeName, typeNameSize);
359 static BOOL FindDfltProvRegVals(DWORD dwProvType, DWORD dwFlags, LPSTR *pszProvName, DWORD *pcbProvName)
361 HKEY hKey;
362 PSTR keyname;
363 PSTR ptr;
364 DWORD user = dwFlags & CRYPT_USER_DEFAULT;
366 LPSTR MACHINESTR = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
367 LPSTR USERSTR = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
369 keyname = LocalAlloc(LMEM_ZEROINIT, (user ? strlen(USERSTR) : strlen(MACHINESTR)) + 1);
370 if (keyname)
372 user ? strcpy(keyname, USERSTR) : strcpy(keyname, MACHINESTR);
373 ptr = keyname + strlen(keyname);
374 *(--ptr) = (dwProvType % 10) + '0';
375 *(--ptr) = ((dwProvType / 10) % 10) + '0';
376 *(--ptr) = (dwProvType / 100) + '0';
377 } else
378 return FALSE;
380 if (RegOpenKey((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
382 LocalFree(keyname);
383 return FALSE;
385 LocalFree(keyname);
387 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
389 if (GetLastError() != ERROR_MORE_DATA)
390 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
391 return FALSE;
394 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
395 return FALSE;
397 if (RegQueryValueEx(hKey, "Name", NULL, NULL, *pszProvName, pcbProvName))
399 if (GetLastError() != ERROR_MORE_DATA)
400 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
401 return FALSE;
404 RegCloseKey(hKey);
406 return TRUE;
409 static void test_get_default_provider()
411 /* expected results */
412 DWORD dwProvType = PROV_RSA_FULL;
413 DWORD dwFlags = CRYPT_MACHINE_DEFAULT;
414 LPSTR pszProvName = NULL;
415 DWORD cbProvName;
417 /* actual results */
418 DWORD provType = PROV_RSA_FULL;
419 DWORD flags = CRYPT_MACHINE_DEFAULT;
420 LPSTR provName = NULL;
421 DWORD provNameSize;
422 DWORD result;
423 DWORD notNull = 5;
425 FindDfltProvRegVals(dwProvType, dwFlags, &pszProvName, &cbProvName);
427 /* check pdwReserved for NULL */
428 result = CryptGetDefaultProvider(provType, &notNull, flags, provName, &provNameSize);
429 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
430 ERROR_INVALID_PARAMETER, GetLastError());
432 /* check for invalid flag */
433 flags = 0xdeadbeef;
434 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
435 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %ld, got %ld\n",
436 NTE_BAD_FLAGS, GetLastError());
437 flags = CRYPT_MACHINE_DEFAULT;
439 /* check for invalid prov type */
440 provType = 0xdeadbeef;
441 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
442 ok(!result && (GetLastError() == NTE_BAD_PROV_TYPE ||
443 GetLastError() == ERROR_INVALID_PARAMETER),
444 "expected NTE_BAD_PROV_TYPE or ERROR_INVALID_PARAMETER, got %ld/%ld\n",
445 result, GetLastError());
446 provType = PROV_RSA_FULL;
448 SetLastError(0);
450 /* alloc provName to half the size required
451 * cbProvName holds the size required */
452 provNameSize = cbProvName / 2;
453 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
454 return;
456 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
457 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %ld\n",
458 ERROR_MORE_DATA, GetLastError());
460 LocalFree(provName);
462 /* check expected versus actual values returned */
463 result = CryptGetDefaultProvider(provType, NULL, flags, NULL, &provNameSize);
464 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
465 provNameSize = cbProvName;
467 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
468 return;
470 result = CryptGetDefaultProvider(provType, NULL, flags, provName, &provNameSize);
471 ok(result && !strcmp(pszProvName, provName), "expected %s, got %s\n", pszProvName, provName);
472 ok(result && provNameSize==cbProvName, "expected %ld, got %ld\n", cbProvName, provNameSize);
475 static void test_set_provider_ex()
477 DWORD result;
478 DWORD notNull = 5;
480 /* results */
481 LPSTR pszProvName = NULL;
482 DWORD cbProvName;
484 /* check pdwReserved for NULL */
485 result = CryptSetProviderEx(MS_DEF_PROV, PROV_RSA_FULL, &notNull, CRYPT_MACHINE_DEFAULT);
486 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %ld\n",
487 ERROR_INVALID_PARAMETER, GetLastError());
489 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
490 result = CryptSetProviderEx(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT | CRYPT_DELETE_DEFAULT);
491 ok(result, "%ld\n", GetLastError());
493 result = CryptSetProviderEx(MS_DEF_PROV, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
494 ok(result, "%ld\n", GetLastError());
496 /* call CryptGetDefaultProvider to see if they match */
497 result = CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &cbProvName);
498 if (!(pszProvName = LocalAlloc(LMEM_ZEROINIT, cbProvName)))
499 return;
501 result = CryptGetDefaultProvider(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, pszProvName, &cbProvName);
502 ok(result && !strcmp(MS_DEF_PROV, pszProvName), "expected %s, got %s\n", MS_DEF_PROV, pszProvName);
503 ok(result && cbProvName==(strlen(MS_DEF_PROV) + 1), "expected %i, got %ld\n", (strlen(MS_DEF_PROV) + 1), cbProvName);
506 START_TEST(crypt)
508 init_environment();
509 test_acquire_context();
510 clean_up_environment();
512 test_enum_providers();
513 test_enum_provider_types();
514 test_get_default_provider();
515 test_set_provider_ex();
516 test_set_provider_ex();