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 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)
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)
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)
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. */
115 SetLastError(0xdeadbeef);
116 result
= CryptAcquireContext(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0);
117 ok(result
&& GetLastError() == ERROR_SUCCESS
, "%d/%ld\n", result
, GetLastError());
120 CryptReleaseContext(hProv
, 0);
122 /* Try again, witch an empty ("\0") szProvider parameter */
124 SetLastError(0xdeadbeef);
125 result
= CryptAcquireContext(&hProv
, szKeySet
, "", PROV_RSA_FULL
, 0);
126 ok(result
&& GetLastError() == ERROR_SUCCESS
, "%d/%ld\n", result
, GetLastError());
129 CryptReleaseContext(hProv
, 0);
132 static BOOL
FindProvRegVals(DWORD dwIndex
, DWORD
*pdwProvType
, LPSTR
*pszProvName
,
133 DWORD
*pcbProvName
, DWORD
*pdwProvCount
)
137 DWORD size
= sizeof(DWORD
);
139 if (RegOpenKey(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey
))
142 RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, pdwProvCount
, pcbProvName
,
143 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
146 if (!(*pszProvName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, *pcbProvName
))))
149 RegEnumKeyEx(hKey
, dwIndex
, *pszProvName
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
152 RegOpenKey(hKey
, *pszProvName
, &subkey
);
153 RegQueryValueEx(subkey
, "Type", NULL
, NULL
, (BYTE
*)pdwProvType
, &size
);
161 static void test_enum_providers(void)
163 /* expected results */
164 CHAR
*pszProvName
= NULL
;
171 CHAR
*provider
= NULL
;
177 DWORD notZeroFlags
= 5;
179 if (!FindProvRegVals(dwIndex
, &dwType
, &pszProvName
, &cbName
, &provCount
))
182 /* check pdwReserved flag for NULL */
183 result
= CryptEnumProviders(dwIndex
, ¬Null
, 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
))))
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());
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 */
206 while(CryptEnumProviders(count
++, NULL
, 0, &type
, NULL
, &providerLen
))
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
))))
224 result
= CryptEnumProviders(dwIndex
, NULL
, 0, &type
, provider
, &providerLen
);
225 ok(result
&& type
==dwType
, "expected %ld, got %ld\n",
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
)
239 if (RegOpenKey(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey
))
242 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, pdwTypeCount
, pcbTypeName
, NULL
,
243 NULL
, NULL
, NULL
, NULL
, NULL
))
247 if (!(*pszTypeName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, *pcbTypeName
))))
250 if (RegEnumKeyEx(hKey
, dwIndex
, *pszTypeName
, pcbTypeName
, NULL
, NULL
, NULL
, NULL
))
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
))
262 if (RegQueryValueEx(hSubKey
, "TypeName", NULL
, NULL
, NULL
, pcbTypeName
))
265 if (!(*pszTypeName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, *pcbTypeName
))))
268 if (RegQueryValueEx(hSubKey
, "TypeName", NULL
, NULL
, *pszTypeName
, pcbTypeName
))
271 RegCloseKey(hSubKey
);
277 static void test_enum_provider_types()
279 /* expected values */
281 LPSTR pszTypeName
= NULL
;
288 LPSTR typeName
= NULL
;
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");
301 /* check pdwReserved for NULL */
302 result
= CryptEnumProviderTypes(index
, ¬Null
, 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
))))
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());
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 */
331 while(CryptEnumProviderTypes(typeCount
++, NULL
, 0, &provType
, NULL
, &typeNameSize
))
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
))))
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
);
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
)
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);
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';
380 if (RegOpenKey((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
387 if (RegQueryValueEx(hKey
, "Name", NULL
, NULL
, *pszProvName
, pcbProvName
))
389 if (GetLastError() != ERROR_MORE_DATA
)
390 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
394 if (!(*pszProvName
= LocalAlloc(LMEM_ZEROINIT
, *pcbProvName
)))
397 if (RegQueryValueEx(hKey
, "Name", NULL
, NULL
, *pszProvName
, pcbProvName
))
399 if (GetLastError() != ERROR_MORE_DATA
)
400 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
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
;
418 DWORD provType
= PROV_RSA_FULL
;
419 DWORD flags
= CRYPT_MACHINE_DEFAULT
;
420 LPSTR provName
= NULL
;
425 FindDfltProvRegVals(dwProvType
, dwFlags
, &pszProvName
, &cbProvName
);
427 /* check pdwReserved for NULL */
428 result
= CryptGetDefaultProvider(provType
, ¬Null
, 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 */
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
;
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
)))
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());
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
)))
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()
481 LPSTR pszProvName
= NULL
;
484 /* check pdwReserved for NULL */
485 result
= CryptSetProviderEx(MS_DEF_PROV
, PROV_RSA_FULL
, ¬Null
, 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
)))
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
);
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();