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
);
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");
75 pCryptAcquireContextA
= (void*)GetProcAddress(hadvapi32
, "CryptAcquireContextA");
76 pCryptEnumProviderTypesA
= (void*)GetProcAddress(hadvapi32
, "CryptEnumProviderTypesA");
77 pCryptEnumProvidersA
= (void*)GetProcAddress(hadvapi32
, "CryptEnumProvidersA");
78 pCryptGetDefaultProviderA
= (void*)GetProcAddress(hadvapi32
, "CryptGetDefaultProviderA");
79 pCryptReleaseContext
= (void*)GetProcAddress(hadvapi32
, "CryptReleaseContext");
80 pCryptSetProviderExA
= (void*)GetProcAddress(hadvapi32
, "CryptSetProviderExA");
81 pCryptCreateHash
= (void*)GetProcAddress(hadvapi32
, "CryptCreateHash");
82 pCryptDestroyHash
= (void*)GetProcAddress(hadvapi32
, "CryptDestroyHash");
83 pCryptGenRandom
= (void*)GetProcAddress(hadvapi32
, "CryptGenRandom");
84 pCryptContextAddRef
= (void*)GetProcAddress(hadvapi32
, "CryptContextAddRef");
85 pCryptGenKey
= (void*)GetProcAddress(hadvapi32
, "CryptGenKey");
86 pCryptDestroyKey
= (void*)GetProcAddress(hadvapi32
, "CryptDestroyKey");
87 pCryptDecrypt
= (void*)GetProcAddress(hadvapi32
, "CryptDecrypt");
88 pCryptDeriveKey
= (void*)GetProcAddress(hadvapi32
, "CryptDeriveKey");
89 pCryptDuplicateHash
= (void*)GetProcAddress(hadvapi32
, "CryptDuplicateHash");
90 pCryptDuplicateKey
= (void*)GetProcAddress(hadvapi32
, "CryptDuplicateKey");
91 pCryptEncrypt
= (void*)GetProcAddress(hadvapi32
, "CryptEncrypt");
92 pCryptExportKey
= (void*)GetProcAddress(hadvapi32
, "CryptExportKey");
93 pCryptGetHashParam
= (void*)GetProcAddress(hadvapi32
, "CryptGetHashParam");
94 pCryptGetKeyParam
= (void*)GetProcAddress(hadvapi32
, "CryptGetKeyParam");
95 pCryptGetProvParam
= (void*)GetProcAddress(hadvapi32
, "CryptGetProvParam");
96 pCryptGetUserKey
= (void*)GetProcAddress(hadvapi32
, "CryptGetUserKey");
97 pCryptHashData
= (void*)GetProcAddress(hadvapi32
, "CryptHashData");
98 pCryptHashSessionKey
= (void*)GetProcAddress(hadvapi32
, "CryptHashSessionKey");
99 pCryptImportKey
= (void*)GetProcAddress(hadvapi32
, "CryptImportKey");
100 pCryptSignHashW
= (void*)GetProcAddress(hadvapi32
, "CryptSignHashW");
101 pCryptSetHashParam
= (void*)GetProcAddress(hadvapi32
, "CryptSetHashParam");
102 pCryptSetKeyParam
= (void*)GetProcAddress(hadvapi32
, "CryptSetKeyParam");
103 pCryptSetProvParam
= (void*)GetProcAddress(hadvapi32
, "CryptSetProvParam");
104 pCryptVerifySignatureW
= (void*)GetProcAddress(hadvapi32
, "CryptVerifySignatureW");
108 static void init_environment(void)
112 /* Ensure that container "wine_test_keyset" does exist */
113 if (!pCryptAcquireContextA(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0))
115 pCryptAcquireContextA(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
117 pCryptReleaseContext(hProv
, 0);
119 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
120 if (!pCryptAcquireContextA(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, 0))
122 pCryptAcquireContextA(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
124 pCryptReleaseContext(hProv
, 0);
126 /* Ensure that container "wine_test_bad_keyset" does not exist. */
127 if (pCryptAcquireContextA(&hProv
, szBadKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0))
129 pCryptReleaseContext(hProv
, 0);
130 pCryptAcquireContextA(&hProv
, szBadKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, CRYPT_DELETEKEYSET
);
134 static void clean_up_environment(void)
138 /* Remove container "wine_test_keyset" */
139 if (pCryptAcquireContextA(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0))
141 pCryptReleaseContext(hProv
, 0);
142 pCryptAcquireContextA(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, CRYPT_DELETEKEYSET
);
145 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
146 if (pCryptAcquireContextA(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, 0))
148 pCryptReleaseContext(hProv
, 0);
149 pCryptAcquireContextA(&hProv
, szKeySet
, NULL
, PROV_RSA_FULL
, CRYPT_DELETEKEYSET
);
153 static void test_acquire_context(void)
158 /* Provoke all kinds of error conditions (which are easy to provoke).
159 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
160 * but since this is likely to change between CSP versions, we don't check
161 * this. Please don't change the order of tests. */
162 result
= pCryptAcquireContextA(&hProv
, NULL
, NULL
, 0, 0);
163 ok(!result
&& GetLastError()==NTE_BAD_PROV_TYPE
, "%ld\n", GetLastError());
165 result
= pCryptAcquireContextA(&hProv
, NULL
, NULL
, 1000, 0);
166 ok(!result
&& GetLastError()==NTE_BAD_PROV_TYPE
, "%ld\n", GetLastError());
168 result
= pCryptAcquireContextA(&hProv
, NULL
, NULL
, NON_DEF_PROV_TYPE
, 0);
169 ok(!result
&& GetLastError()==NTE_PROV_TYPE_NOT_DEF
, "%ld\n", GetLastError());
171 result
= pCryptAcquireContextA(&hProv
, szKeySet
, szNonExistentProv
, PROV_RSA_FULL
, 0);
172 ok(!result
&& GetLastError()==NTE_KEYSET_NOT_DEF
, "%ld\n", GetLastError());
174 result
= pCryptAcquireContextA(&hProv
, szKeySet
, szRsaBaseProv
, NON_DEF_PROV_TYPE
, 0);
175 ok(!result
&& GetLastError()==NTE_PROV_TYPE_NO_MATCH
, "%ld\n", GetLastError());
177 /* This test fails under Win2k SP4:
178 result = TRUE, GetLastError() == ERROR_INVALID_PARAMETER
179 SetLastError(0xdeadbeef);
180 result = pCryptAcquireContextA(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
181 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d/%ld\n", result, GetLastError());
184 /* Last not least, try to really acquire a context. */
186 SetLastError(0xdeadbeef);
187 result
= pCryptAcquireContextA(&hProv
, szKeySet
, szRsaBaseProv
, PROV_RSA_FULL
, 0);
188 ok(result
&& (GetLastError() == ERROR_ENVVAR_NOT_FOUND
|| GetLastError() == ERROR_SUCCESS
|| GetLastError() == ERROR_RING2_STACK_IN_USE
|| GetLastError() == NTE_FAIL
), "%d/%ld\n", result
, GetLastError());
191 pCryptReleaseContext(hProv
, 0);
193 /* Try again, witch an empty ("\0") szProvider parameter */
195 SetLastError(0xdeadbeef);
196 result
= pCryptAcquireContextA(&hProv
, szKeySet
, "", PROV_RSA_FULL
, 0);
197 ok(result
&& (GetLastError() == ERROR_ENVVAR_NOT_FOUND
|| GetLastError() == ERROR_SUCCESS
|| GetLastError() == ERROR_RING2_STACK_IN_USE
|| GetLastError() == NTE_FAIL
), "%d/%ld\n", result
, GetLastError());
200 pCryptReleaseContext(hProv
, 0);
203 static void test_incorrect_api_usage(void)
206 HCRYPTPROV hProv
, hProv2
;
207 HCRYPTHASH hHash
, hHash2
;
208 HCRYPTKEY hKey
, hKey2
;
212 /* This is to document incorrect api usage in the
213 * "Uru - Ages beyond Myst Demo" installer as reported by Paul Vriens.
215 * The installer destroys a hash object after having released the context
216 * with which the hash was created. This is not allowed according to MSDN,
217 * since CryptReleaseContext destroys all hash and key objects belonging to
218 * the respective context. However, while wine used to crash, Windows is more
219 * robust here and returns an ERROR_INVALID_PARAMETER code.
222 result
= pCryptAcquireContextA(&hProv
, szBadKeySet
, szRsaBaseProv
,
223 PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
224 ok (result
, "%08lx\n", GetLastError());
227 result
= pCryptCreateHash(hProv
, CALG_SHA
, 0, 0, &hHash
);
228 ok (result
, "%ld\n", GetLastError());
231 result
= pCryptGenKey(hProv
, CALG_RC4
, 0, &hKey
);
232 ok (result
, "%ld\n", GetLastError());
235 result
= pCryptGenKey(hProv
, CALG_RC4
, 0, &hKey2
);
236 ok (result
, "%ld\n", GetLastError());
239 result
= pCryptDestroyKey(hKey2
);
240 ok (result
, "%ld\n", GetLastError());
242 dwTemp
= CRYPT_MODE_ECB
;
243 result
= pCryptSetKeyParam(hKey2
, KP_MODE
, (BYTE
*)&dwTemp
, sizeof(DWORD
));
244 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
246 result
= pCryptAcquireContextA(&hProv2
, szBadKeySet
, NULL
, PROV_RSA_FULL
,
248 ok (result
, "%ld\n", GetLastError());
251 result
= pCryptReleaseContext(hProv
, 0);
252 ok (result
, "%ld\n", GetLastError());
255 result
= pCryptReleaseContext(hProv
, 0);
256 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
258 result
= pCryptGenRandom(hProv
, 1, &temp
);
259 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
261 #ifdef CRASHES_ON_NT40
262 result
= pCryptContextAddRef(hProv
, NULL
, 0);
263 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
266 result
= pCryptCreateHash(hProv
, CALG_SHA
, 0, 0, &hHash2
);
267 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
270 result
= pCryptDecrypt(hKey
, (HCRYPTHASH
)NULL
, TRUE
, 0, &temp
, &dwLen
);
271 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
274 result
= pCryptEncrypt(hKey
, (HCRYPTHASH
)NULL
, TRUE
, 0, &temp
, &dwLen
, 1);
275 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
277 result
= pCryptDeriveKey(hProv
, CALG_RC4
, hHash
, 0, &hKey2
);
278 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
280 #ifdef CRASHES_ON_NT40
281 result
= pCryptDuplicateHash(hHash
, NULL
, 0, &hHash2
);
282 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
284 result
= pCryptDuplicateKey(hKey
, NULL
, 0, &hKey2
);
285 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
289 result
= pCryptExportKey(hKey
, (HCRYPTPROV
)NULL
, 0, 0, &temp
, &dwLen
);
290 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
292 result
= pCryptGenKey(hProv
, CALG_RC4
, 0, &hKey2
);
293 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
296 result
= pCryptGetHashParam(hHash
, 0, &temp
, &dwLen
, 0);
297 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
300 result
= pCryptGetKeyParam(hKey
, 0, &temp
, &dwLen
, 0);
301 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
304 result
= pCryptGetProvParam(hProv
, 0, &temp
, &dwLen
, 0);
305 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
307 result
= pCryptGetUserKey(hProv
, 0, &hKey2
);
308 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
310 result
= pCryptHashData(hHash
, &temp
, 1, 0);
311 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
313 result
= pCryptHashSessionKey(hHash
, hKey
, 0);
314 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
316 result
= pCryptImportKey(hProv
, &temp
, 1, (HCRYPTKEY
)NULL
, 0, &hKey2
);
317 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
320 result
= pCryptSignHashW(hHash
, 0, NULL
, 0, &temp
, &dwLen
);
321 ok (!result
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
322 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
), "%ld\n", GetLastError());
324 result
= pCryptSetKeyParam(hKey
, 0, &temp
, 1);
325 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
327 result
= pCryptSetHashParam(hHash
, 0, &temp
, 1);
328 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
330 result
= pCryptSetProvParam(hProv
, 0, &temp
, 1);
331 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
333 result
= pCryptVerifySignatureW(hHash
, &temp
, 1, hKey
, NULL
, 0);
334 ok (!result
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
335 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
), "%ld\n", GetLastError());
337 result
= pCryptDestroyHash(hHash
);
338 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
340 result
= pCryptDestroyKey(hKey
);
341 ok (!result
&& GetLastError() == ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
344 static BOOL
FindProvRegVals(DWORD dwIndex
, DWORD
*pdwProvType
, LPSTR
*pszProvName
,
345 DWORD
*pcbProvName
, DWORD
*pdwProvCount
)
349 DWORD size
= sizeof(DWORD
);
351 if (RegOpenKey(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey
))
354 RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, pdwProvCount
, pcbProvName
,
355 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
358 if (!(*pszProvName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, *pcbProvName
))))
361 RegEnumKeyEx(hKey
, dwIndex
, *pszProvName
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
364 RegOpenKey(hKey
, *pszProvName
, &subkey
);
365 RegQueryValueEx(subkey
, "Type", NULL
, NULL
, (LPBYTE
)pdwProvType
, &size
);
373 static void test_enum_providers(void)
375 /* expected results */
376 CHAR
*pszProvName
= NULL
;
383 CHAR
*provider
= NULL
;
389 DWORD notZeroFlags
= 5;
391 if(!pCryptEnumProvidersA
)
393 trace("skipping CryptEnumProviders tests\n");
397 if (!FindProvRegVals(dwIndex
, &dwType
, &pszProvName
, &cbName
, &provCount
))
399 trace("could not find providers in registry, skipping the test\n");
403 /* check pdwReserved flag for NULL */
404 result
= pCryptEnumProvidersA(dwIndex
, ¬Null
, 0, &type
, NULL
, &providerLen
);
405 ok(!result
&& GetLastError()==ERROR_INVALID_PARAMETER
, "%ld\n", GetLastError());
407 /* check dwFlags == 0 */
408 result
= pCryptEnumProvidersA(dwIndex
, NULL
, notZeroFlags
, &type
, NULL
, &providerLen
);
409 ok(!result
&& GetLastError()==NTE_BAD_FLAGS
, "%ld\n", GetLastError());
411 /* alloc provider to half the size required
412 * cbName holds the size required */
413 providerLen
= cbName
/ 2;
414 if (!(provider
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, providerLen
))))
417 result
= pCryptEnumProvidersA(dwIndex
, NULL
, 0, &type
, provider
, &providerLen
);
418 ok(!result
&& GetLastError()==ERROR_MORE_DATA
, "expected %i, got %ld\n",
419 ERROR_MORE_DATA
, GetLastError());
423 /* loop through the providers to get the number of providers
424 * after loop ends, count should be provCount + 1 so subtract 1
425 * to get actual number of providers */
427 while(pCryptEnumProvidersA(count
++, NULL
, 0, &type
, NULL
, &providerLen
))
430 ok(count
==provCount
, "expected %i, got %i\n", (int)provCount
, (int)count
);
432 /* loop past the actual number of providers to get the error
433 * ERROR_NO_MORE_ITEMS */
434 for (count
= 0; count
< provCount
+ 1; count
++)
435 result
= pCryptEnumProvidersA(count
, NULL
, 0, &type
, NULL
, &providerLen
);
436 ok(!result
&& GetLastError()==ERROR_NO_MORE_ITEMS
, "expected %i, got %ld\n",
437 ERROR_NO_MORE_ITEMS
, GetLastError());
439 /* check expected versus actual values returned */
440 result
= pCryptEnumProvidersA(dwIndex
, NULL
, 0, &type
, NULL
, &providerLen
);
441 ok(result
&& providerLen
==cbName
, "expected %i, got %i\n", (int)cbName
, (int)providerLen
);
442 if (!(provider
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, providerLen
))))
445 providerLen
= 0xdeadbeef;
446 result
= pCryptEnumProvidersA(dwIndex
, NULL
, 0, &type
, provider
, &providerLen
);
447 ok(result
, "expected TRUE, got %ld\n", result
);
448 ok(type
==dwType
, "expected %ld, got %ld\n", dwType
, type
);
450 ok(!strcmp(pszProvName
, provider
), "expected %s, got %s\n", pszProvName
, provider
);
451 ok(cbName
==providerLen
, "expected %ld, got %ld\n", cbName
, providerLen
);
456 static BOOL
FindProvTypesRegVals(DWORD dwIndex
, DWORD
*pdwProvType
, LPSTR
*pszTypeName
,
457 DWORD
*pcbTypeName
, DWORD
*pdwTypeCount
)
463 if (RegOpenKey(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey
))
466 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, pdwTypeCount
, pcbTypeName
, NULL
,
467 NULL
, NULL
, NULL
, NULL
, NULL
))
471 if (!(*pszTypeName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, *pcbTypeName
))))
474 if (RegEnumKeyEx(hKey
, dwIndex
, *pszTypeName
, pcbTypeName
, NULL
, NULL
, NULL
, NULL
))
477 ch
= *pszTypeName
+ strlen(*pszTypeName
);
478 /* Convert "Type 000" to 0, etc/ */
479 *pdwProvType
= *(--ch
) - '0';
480 *pdwProvType
+= (*(--ch
) - '0') * 10;
481 *pdwProvType
+= (*(--ch
) - '0') * 100;
483 if (RegOpenKey(hKey
, *pszTypeName
, &hSubKey
))
486 if (RegQueryValueEx(hSubKey
, "TypeName", NULL
, NULL
, NULL
, pcbTypeName
))
489 if (!(*pszTypeName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, *pcbTypeName
))))
492 if (RegQueryValueEx(hSubKey
, "TypeName", NULL
, NULL
, (LPBYTE
)*pszTypeName
, pcbTypeName
))
495 RegCloseKey(hSubKey
);
501 static void test_enum_provider_types(void)
503 /* expected values */
505 LPSTR pszTypeName
= NULL
;
512 LPSTR typeName
= NULL
;
517 DWORD notZeroFlags
= 5;
519 if(!pCryptEnumProviderTypesA
)
521 trace("skipping CryptEnumProviderTypes tests\n");
525 if (!FindProvTypesRegVals(index
, &dwProvType
, &pszTypeName
, &cbTypeName
, &dwTypeCount
))
527 trace("could not find provider types in registry, skipping the test\n");
531 /* check pdwReserved for NULL */
532 result
= pCryptEnumProviderTypesA(index
, ¬Null
, 0, &provType
, typeName
, &typeNameSize
);
533 ok(!result
&& GetLastError()==ERROR_INVALID_PARAMETER
, "expected %i, got %ld\n",
534 ERROR_INVALID_PARAMETER
, GetLastError());
536 /* check dwFlags == zero */
537 result
= pCryptEnumProviderTypesA(index
, NULL
, notZeroFlags
, &provType
, typeName
, &typeNameSize
);
538 ok(!result
&& GetLastError()==NTE_BAD_FLAGS
, "expected %i, got %ld\n",
539 ERROR_INVALID_PARAMETER
, GetLastError());
541 /* alloc provider type to half the size required
542 * cbTypeName holds the size required */
543 typeNameSize
= cbTypeName
/ 2;
544 if (!(typeName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, typeNameSize
))))
547 /* This test fails under Win2k SP4:
548 result = TRUE, GetLastError() == 0xdeadbeef
549 SetLastError(0xdeadbeef);
550 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
551 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected 0/ERROR_MORE_DATA, got %d/%08lx\n",
552 result, GetLastError());
557 /* loop through the provider types to get the number of provider types
558 * after loop ends, count should be dwTypeCount + 1 so subtract 1
559 * to get actual number of provider types */
561 while(pCryptEnumProviderTypesA(typeCount
++, NULL
, 0, &provType
, NULL
, &typeNameSize
))
564 ok(typeCount
==dwTypeCount
, "expected %ld, got %ld\n", dwTypeCount
, typeCount
);
566 /* loop past the actual number of provider types to get the error
567 * ERROR_NO_MORE_ITEMS */
568 for (typeCount
= 0; typeCount
< dwTypeCount
+ 1; typeCount
++)
569 result
= pCryptEnumProviderTypesA(typeCount
, NULL
, 0, &provType
, NULL
, &typeNameSize
);
570 ok(!result
&& GetLastError()==ERROR_NO_MORE_ITEMS
, "expected %i, got %ld\n",
571 ERROR_NO_MORE_ITEMS
, GetLastError());
574 /* check expected versus actual values returned */
575 result
= pCryptEnumProviderTypesA(index
, NULL
, 0, &provType
, NULL
, &typeNameSize
);
576 ok(result
&& typeNameSize
==cbTypeName
, "expected %ld, got %ld\n", cbTypeName
, typeNameSize
);
577 if (!(typeName
= ((LPSTR
)LocalAlloc(LMEM_ZEROINIT
, typeNameSize
))))
580 typeNameSize
= 0xdeadbeef;
581 result
= pCryptEnumProviderTypesA(index
, NULL
, 0, &provType
, typeName
, &typeNameSize
);
582 ok(result
, "expected TRUE, got %ld\n", result
);
583 ok(provType
==dwProvType
, "expected %ld, got %ld\n", dwProvType
, provType
);
585 ok(!strcmp(pszTypeName
, typeName
), "expected %s, got %s\n", pszTypeName
, typeName
);
586 ok(typeNameSize
==cbTypeName
, "expected %ld, got %ld\n", cbTypeName
, typeNameSize
);
591 static BOOL
FindDfltProvRegVals(DWORD dwProvType
, DWORD dwFlags
, LPSTR
*pszProvName
, DWORD
*pcbProvName
)
596 DWORD user
= dwFlags
& CRYPT_USER_DEFAULT
;
598 LPCSTR machinestr
= "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
599 LPCSTR userstr
= "Software\\Microsoft\\Cryptography\\Provider Type XXX";
601 keyname
= LocalAlloc(LMEM_ZEROINIT
, (user
? strlen(userstr
) : strlen(machinestr
)) + 1);
604 user
? strcpy(keyname
, userstr
) : strcpy(keyname
, machinestr
);
605 ptr
= keyname
+ strlen(keyname
);
606 *(--ptr
) = (dwProvType
% 10) + '0';
607 *(--ptr
) = ((dwProvType
/ 10) % 10) + '0';
608 *(--ptr
) = (dwProvType
/ 100) + '0';
612 if (RegOpenKey((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
619 if (RegQueryValueEx(hKey
, "Name", NULL
, NULL
, (LPBYTE
)*pszProvName
, pcbProvName
))
621 if (GetLastError() != ERROR_MORE_DATA
)
622 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
626 if (!(*pszProvName
= LocalAlloc(LMEM_ZEROINIT
, *pcbProvName
)))
629 if (RegQueryValueEx(hKey
, "Name", NULL
, NULL
, (LPBYTE
)*pszProvName
, pcbProvName
))
631 if (GetLastError() != ERROR_MORE_DATA
)
632 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
641 static void test_get_default_provider(void)
643 /* expected results */
644 DWORD dwProvType
= PROV_RSA_FULL
;
645 DWORD dwFlags
= CRYPT_MACHINE_DEFAULT
;
646 LPSTR pszProvName
= NULL
;
650 DWORD provType
= PROV_RSA_FULL
;
651 DWORD flags
= CRYPT_MACHINE_DEFAULT
;
652 LPSTR provName
= NULL
;
657 if(!pCryptGetDefaultProviderA
)
659 trace("skipping CryptGetDefaultProvider tests\n");
663 if(!FindDfltProvRegVals(dwProvType
, dwFlags
, &pszProvName
, &cbProvName
))
665 trace("could not find default provider in registry, skipping the test\n");
669 /* check pdwReserved for NULL */
670 result
= pCryptGetDefaultProviderA(provType
, ¬Null
, flags
, provName
, &provNameSize
);
671 ok(!result
&& GetLastError()==ERROR_INVALID_PARAMETER
, "expected %i, got %ld\n",
672 ERROR_INVALID_PARAMETER
, GetLastError());
674 /* check for invalid flag */
676 result
= pCryptGetDefaultProviderA(provType
, NULL
, flags
, provName
, &provNameSize
);
677 ok(!result
&& GetLastError()==NTE_BAD_FLAGS
, "expected %ld, got %ld\n",
678 NTE_BAD_FLAGS
, GetLastError());
679 flags
= CRYPT_MACHINE_DEFAULT
;
681 /* check for invalid prov type */
682 provType
= 0xdeadbeef;
683 result
= pCryptGetDefaultProviderA(provType
, NULL
, flags
, provName
, &provNameSize
);
684 ok(!result
&& (GetLastError() == NTE_BAD_PROV_TYPE
||
685 GetLastError() == ERROR_INVALID_PARAMETER
),
686 "expected NTE_BAD_PROV_TYPE or ERROR_INVALID_PARAMETER, got %ld/%ld\n",
687 result
, GetLastError());
688 provType
= PROV_RSA_FULL
;
692 /* alloc provName to half the size required
693 * cbProvName holds the size required */
694 provNameSize
= cbProvName
/ 2;
695 if (!(provName
= LocalAlloc(LMEM_ZEROINIT
, provNameSize
)))
698 result
= pCryptGetDefaultProviderA(provType
, NULL
, flags
, provName
, &provNameSize
);
699 ok(!result
&& GetLastError()==ERROR_MORE_DATA
, "expected %i, got %ld\n",
700 ERROR_MORE_DATA
, GetLastError());
704 /* check expected versus actual values returned */
705 result
= pCryptGetDefaultProviderA(provType
, NULL
, flags
, NULL
, &provNameSize
);
706 ok(result
&& provNameSize
==cbProvName
, "expected %ld, got %ld\n", cbProvName
, provNameSize
);
707 provNameSize
= cbProvName
;
709 if (!(provName
= LocalAlloc(LMEM_ZEROINIT
, provNameSize
)))
712 provNameSize
= 0xdeadbeef;
713 result
= pCryptGetDefaultProviderA(provType
, NULL
, flags
, provName
, &provNameSize
);
714 ok(result
, "expected TRUE, got %ld\n", result
);
716 ok(!strcmp(pszProvName
, provName
), "expected %s, got %s\n", pszProvName
, provName
);
717 ok(provNameSize
==cbProvName
, "expected %ld, got %ld\n", cbProvName
, provNameSize
);
722 static void test_set_provider_ex(void)
728 LPSTR pszProvName
= NULL
;
731 if(!pCryptGetDefaultProviderA
|| !pCryptSetProviderExA
)
733 trace("skipping CryptSetProviderEx tests\n");
737 /* check pdwReserved for NULL */
738 result
= pCryptSetProviderExA(MS_DEF_PROV
, PROV_RSA_FULL
, ¬Null
, CRYPT_MACHINE_DEFAULT
);
739 ok(!result
&& GetLastError()==ERROR_INVALID_PARAMETER
, "expected %i, got %ld\n",
740 ERROR_INVALID_PARAMETER
, GetLastError());
742 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
743 result
= pCryptSetProviderExA(MS_DEF_PROV
, PROV_RSA_FULL
, NULL
, CRYPT_MACHINE_DEFAULT
| CRYPT_DELETE_DEFAULT
);
744 ok(result
, "%ld\n", GetLastError());
746 result
= pCryptSetProviderExA(MS_DEF_PROV
, PROV_RSA_FULL
, NULL
, CRYPT_MACHINE_DEFAULT
);
747 ok(result
, "%ld\n", GetLastError());
749 /* call CryptGetDefaultProvider to see if they match */
750 result
= pCryptGetDefaultProviderA(PROV_RSA_FULL
, NULL
, CRYPT_MACHINE_DEFAULT
, NULL
, &cbProvName
);
751 if (!(pszProvName
= LocalAlloc(LMEM_ZEROINIT
, cbProvName
)))
754 result
= pCryptGetDefaultProviderA(PROV_RSA_FULL
, NULL
, CRYPT_MACHINE_DEFAULT
, pszProvName
, &cbProvName
);
755 ok(result
&& !strcmp(MS_DEF_PROV
, pszProvName
), "expected %s, got %s\n", MS_DEF_PROV
, pszProvName
);
756 ok(result
&& cbProvName
==(strlen(MS_DEF_PROV
) + 1), "expected %i, got %ld\n", (strlen(MS_DEF_PROV
) + 1), cbProvName
);
758 LocalFree(pszProvName
);
763 init_function_pointers();
764 if(pCryptAcquireContextA
&& pCryptReleaseContext
) {
766 test_acquire_context();
767 test_incorrect_api_usage();
768 clean_up_environment();
771 test_enum_providers();
772 test_enum_provider_types();
773 test_get_default_provider();
774 test_set_provider_ex();