makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / advapi32 / tests / crypt.c
blobf34c99ee9bffc851b413437854cfe48277bff515
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 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 BOOL (WINAPI *pCryptEnumProviderTypesA)(DWORD, DWORD*, DWORD, DWORD*, LPSTR, DWORD*);
38 static BOOL (WINAPI *pCryptEnumProvidersA)(DWORD, DWORD*, DWORD, DWORD*, LPSTR, DWORD*);
39 static BOOL (WINAPI *pCryptGetDefaultProviderA)(DWORD, DWORD*, DWORD, LPSTR, DWORD*);
40 static BOOL (WINAPI *pCryptSetProviderExA)(LPCSTR, DWORD, DWORD*, DWORD);
41 static BOOL (WINAPI *pCryptGenRandom)(HCRYPTPROV, DWORD, BYTE*);
42 static BOOL (WINAPI *pCryptDuplicateHash)(HCRYPTHASH, DWORD*, DWORD, HCRYPTHASH*);
43 static BOOL (WINAPI *pCryptHashSessionKey)(HCRYPTHASH, HCRYPTKEY, DWORD);
44 static BOOL (WINAPI *pCryptSignHashW)(HCRYPTHASH, DWORD, LPCWSTR, DWORD, BYTE*, DWORD*);
45 static BOOL (WINAPI *pCryptVerifySignatureW)(HCRYPTHASH, BYTE*, DWORD, HCRYPTKEY, LPCWSTR, DWORD);
46 static BOOLEAN (WINAPI *pSystemFunction036)(PVOID, ULONG);
48 static void init_function_pointers(void)
50 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
52 pCryptEnumProviderTypesA = (void*)GetProcAddress(hadvapi32, "CryptEnumProviderTypesA");
53 pCryptEnumProvidersA = (void*)GetProcAddress(hadvapi32, "CryptEnumProvidersA");
54 pCryptGetDefaultProviderA = (void*)GetProcAddress(hadvapi32, "CryptGetDefaultProviderA");
55 pCryptSetProviderExA = (void*)GetProcAddress(hadvapi32, "CryptSetProviderExA");
56 pCryptGenRandom = (void*)GetProcAddress(hadvapi32, "CryptGenRandom");
57 pCryptDuplicateHash = (void*)GetProcAddress(hadvapi32, "CryptDuplicateHash");
58 pCryptHashSessionKey = (void*)GetProcAddress(hadvapi32, "CryptHashSessionKey");
59 pCryptSignHashW = (void*)GetProcAddress(hadvapi32, "CryptSignHashW");
60 pCryptVerifySignatureW = (void*)GetProcAddress(hadvapi32, "CryptVerifySignatureW");
61 pSystemFunction036 = (void*)GetProcAddress(hadvapi32, "SystemFunction036");
64 static void init_environment(void)
66 HCRYPTPROV hProv;
67 BOOL ret;
69 /* Ensure that container "wine_test_keyset" does exist */
70 if (!CryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
72 CryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_NEWKEYSET);
74 ret = CryptReleaseContext(hProv, 0);
75 ok(ret, "got %u\n", GetLastError());
77 /* Ensure that container "wine_test_keyset" does exist in default PROV_RSA_FULL type provider */
78 if (!CryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
80 CryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
82 ret = CryptReleaseContext(hProv, 0);
83 ok(ret, "got %u\n", GetLastError());
85 /* Ensure that container "wine_test_bad_keyset" does not exist. */
86 if (CryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
88 ret = CryptReleaseContext(hProv, 0);
89 ok(ret, "got %u\n", GetLastError());
91 CryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
95 static void clean_up_environment(void)
97 HCRYPTPROV hProv;
98 BOOL ret;
100 /* Remove container "wine_test_keyset" */
101 if (CryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
103 ret = CryptReleaseContext(hProv, 0);
104 ok(ret, "got %u\n", GetLastError());
106 CryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
109 /* Remove container "wine_test_keyset" from default PROV_RSA_FULL type provider */
110 if (CryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, 0))
112 ret = CryptReleaseContext(hProv, 0);
113 ok(ret, "got %u\n", GetLastError());
115 CryptAcquireContextA(&hProv, szKeySet, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
118 /* Remove container "wine_test_bad_keyset" */
119 if (CryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, 0))
121 ret = CryptReleaseContext(hProv, 0);
122 ok(ret, "got %u\n", GetLastError());
124 CryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
128 static void test_CryptReleaseContext(void)
130 BOOL ret;
131 HCRYPTPROV prov;
133 /* TODO: Add cases for ERROR_BUSY, ERROR_INVALID_HANDLE and NTE_BAD_UID */
135 /* NULL provider */
137 SetLastError(0xdeadbeef);
138 ret = CryptReleaseContext(0, 0);
139 ok(!ret, "CryptReleaseContext succeeded unexpectedly\n");
140 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got %u\n", GetLastError());
142 SetLastError(0xdeadbeef);
143 ret = CryptReleaseContext(0, ~0);
144 ok(!ret, "CryptReleaseContext succeeded unexpectedly\n");
145 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got %u\n", GetLastError());
147 /* Additional refcount */
149 ret = CryptAcquireContextA(&prov, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
150 ok(ret, "got %u\n", GetLastError());
152 ret = CryptContextAddRef(prov, NULL, 0);
153 ok(ret, "got %u\n", GetLastError());
155 ret = CryptReleaseContext(prov, 0);
156 ok(ret, "got %u\n", GetLastError());
158 /* Nonzero flags, which allow release nonetheless */
160 SetLastError(0xdeadbeef);
161 ret = CryptReleaseContext(prov, ~0);
162 ok(!ret, "CryptReleaseContext succeeded unexpectedly\n");
163 ok(GetLastError() == NTE_BAD_FLAGS, "got %u\n", GetLastError());
165 /* Obsolete provider */
167 SetLastError(0xdeadbeef);
168 ret = CryptReleaseContext(prov, 0);
169 ok(!ret, "CryptReleaseContext succeeded unexpectedly\n");
170 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got %u\n", GetLastError());
172 SetLastError(0xdeadbeef);
173 ret = CryptReleaseContext(prov, ~0);
174 ok(!ret, "CryptReleaseContext succeeded unexpectedly\n");
175 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got %u\n", GetLastError());
178 static void test_acquire_context(void)
180 BOOL result;
181 HCRYPTPROV hProv;
182 DWORD GLE;
184 /* Provoke all kinds of error conditions (which are easy to provoke).
185 * The order of the error tests seems to match Windows XP's rsaenh.dll CSP,
186 * but since this is likely to change between CSP versions, we don't check
187 * this. Please don't change the order of tests. */
188 result = CryptAcquireContextA(&hProv, NULL, NULL, 0, 0);
189 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%d\n", GetLastError());
191 result = CryptAcquireContextA(&hProv, NULL, NULL, 1000, 0);
192 ok(!result && GetLastError()==NTE_BAD_PROV_TYPE, "%d\n", GetLastError());
194 result = CryptAcquireContextA(&hProv, NULL, NULL, NON_DEF_PROV_TYPE, 0);
195 ok(!result && GetLastError()==NTE_PROV_TYPE_NOT_DEF, "%d\n", GetLastError());
197 result = CryptAcquireContextA(&hProv, szKeySet, szNonExistentProv, PROV_RSA_FULL, 0);
198 ok(!result && GetLastError()==NTE_KEYSET_NOT_DEF, "%d\n", GetLastError());
200 result = CryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, NON_DEF_PROV_TYPE, 0);
201 ok(!result && GetLastError()==NTE_PROV_TYPE_NO_MATCH, "%d\n", GetLastError());
204 if (0)
206 /* This test fails under Win2k SP4:
207 result = TRUE, GetLastError() == ERROR_INVALID_PARAMETER */
208 SetLastError(0xdeadbeef);
209 result = CryptAcquireContextA(NULL, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
210 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d/%d\n", result, GetLastError());
213 /* Last not least, try to really acquire a context. */
214 hProv = 0;
215 SetLastError(0xdeadbeef);
216 result = CryptAcquireContextA(&hProv, szKeySet, szRsaBaseProv, PROV_RSA_FULL, 0);
217 GLE = GetLastError();
218 ok(result && (GLE == ERROR_ENVVAR_NOT_FOUND ||
219 GLE == ERROR_SUCCESS ||
220 GLE == ERROR_RING2_STACK_IN_USE ||
221 GLE == NTE_FAIL ||
222 GLE == ERROR_NOT_LOGGED_ON), "%d/%d\n", result, GLE);
224 if (hProv)
226 result = CryptReleaseContext(hProv, 0);
227 ok(result, "got %u\n", GetLastError());
230 /* Try again, witch an empty ("\0") szProvider parameter */
231 hProv = 0;
232 SetLastError(0xdeadbeef);
233 result = CryptAcquireContextA(&hProv, szKeySet, "", PROV_RSA_FULL, 0);
234 GLE = GetLastError();
235 ok(result && (GLE == ERROR_ENVVAR_NOT_FOUND ||
236 GLE == ERROR_SUCCESS ||
237 GLE == ERROR_RING2_STACK_IN_USE ||
238 GLE == NTE_FAIL ||
239 GLE == ERROR_NOT_LOGGED_ON), "%d/%d\n", result, GetLastError());
241 if (hProv)
243 result = CryptReleaseContext(hProv, 0);
244 ok(result, "got %u\n", GetLastError());
248 static void test_incorrect_api_usage(void)
250 BOOL result;
251 HCRYPTPROV hProv, hProv2;
252 HCRYPTHASH hHash, hHash2;
253 HCRYPTKEY hKey, hKey2;
254 BYTE temp;
255 DWORD dwLen, dwTemp;
257 /* This is to document incorrect api usage in the
258 * "Uru - Ages beyond Myst Demo" installer as reported by Paul Vriens.
260 * The installer destroys a hash object after having released the context
261 * with which the hash was created. This is not allowed according to MSDN,
262 * since CryptReleaseContext destroys all hash and key objects belonging to
263 * the respective context. However, while wine used to crash, Windows is more
264 * robust here and returns an ERROR_INVALID_PARAMETER code.
267 result = CryptAcquireContextA(&hProv, szBadKeySet, szRsaBaseProv,
268 PROV_RSA_FULL, CRYPT_NEWKEYSET);
269 ok (result, "%08x\n", GetLastError());
270 if (!result) return;
272 result = CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash);
273 ok (result, "%d\n", GetLastError());
274 if (!result) return;
275 CryptDestroyHash(hHash);
277 result = CryptCreateHash(0, CALG_SHA, 0, 0, &hHash);
278 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
280 result = CryptGenKey(0, CALG_RC4, 0, &hKey);
281 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
283 result = CryptGenKey(hProv, CALG_RC4, 0, &hKey);
284 ok (result, "%d\n", GetLastError());
285 if (!result) return;
287 result = CryptDestroyKey(hKey);
288 ok (result, "%d\n", GetLastError());
290 result = CryptGenKey(hProv, CALG_RC4, 0, &hKey2);
291 ok (result, "%d\n", GetLastError());
292 if (!result) return;
294 result = CryptDestroyKey(hKey2);
295 ok (result, "%d\n", GetLastError());
297 dwTemp = CRYPT_MODE_ECB;
298 result = CryptSetKeyParam(hKey2, KP_MODE, (BYTE*)&dwTemp, sizeof(DWORD));
299 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
301 hProv2 = 0xdeadbeef;
302 result = CryptAcquireContextA(&hProv2, szBadKeySet, NULL, PROV_RSA_FULL,
303 CRYPT_DELETEKEYSET);
304 ok (result, "%d\n", GetLastError());
305 ok (hProv2 == 0, "%ld\n", hProv2);
306 if (!result) return;
308 result = CryptReleaseContext(hProv, 0);
309 ok(result, "got %u\n", GetLastError());
310 if (!result) return;
312 result = pCryptGenRandom(hProv, 1, &temp);
313 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
315 result = CryptContextAddRef(hProv, NULL, 0);
316 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
318 result = CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash2);
319 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
321 dwLen = 1;
322 result = CryptDecrypt(hKey, 0, TRUE, 0, &temp, &dwLen);
323 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
325 dwLen = 1;
326 result = CryptEncrypt(hKey, 0, TRUE, 0, &temp, &dwLen, 1);
327 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
329 result = CryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey2);
330 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
332 result = pCryptDuplicateHash(hHash, NULL, 0, &hHash2);
333 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
335 result = CryptDuplicateKey(hKey, NULL, 0, &hKey2);
336 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
338 dwLen = 1;
339 result = CryptExportKey(hKey, 0, 0, 0, &temp, &dwLen);
340 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
342 result = CryptGenKey(hProv, CALG_RC4, 0, &hKey2);
343 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
345 dwLen = 1;
346 result = CryptGetHashParam(hHash, 0, &temp, &dwLen, 0);
347 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
349 dwLen = 1;
350 result = CryptGetKeyParam(hKey, 0, &temp, &dwLen, 0);
351 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
353 dwLen = 1;
354 result = CryptGetProvParam(hProv, 0, &temp, &dwLen, 0);
355 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
357 result = CryptGetUserKey(hProv, 0, &hKey2);
358 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
360 result = CryptHashData(hHash, &temp, 1, 0);
361 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
363 result = pCryptHashSessionKey(hHash, hKey, 0);
364 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
366 result = CryptImportKey(hProv, &temp, 1, 0, 0, &hKey2);
367 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
369 if (pCryptSignHashW)
371 dwLen = 1;
372 result = pCryptSignHashW(hHash, 0, NULL, 0, &temp, &dwLen);
373 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
374 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%d\n", GetLastError());
376 else
377 win_skip("CryptSignHashW is not available\n");
379 result = CryptSetKeyParam(hKey, 0, &temp, 1);
380 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
382 result = CryptSetHashParam(hHash, 0, &temp, 1);
383 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
385 result = CryptSetProvParam(hProv, 0, &temp, 1);
386 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
388 if (pCryptVerifySignatureW)
390 result = pCryptVerifySignatureW(hHash, &temp, 1, hKey, NULL, 0);
391 ok (!result && (GetLastError() == ERROR_INVALID_PARAMETER ||
392 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), "%d\n", GetLastError());
394 else
395 win_skip("CryptVerifySignatureW is not available\n");
397 result = CryptDestroyHash(hHash);
398 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
400 result = CryptDestroyKey(hKey);
401 ok (!result && GetLastError() == ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
404 static const BYTE privKey[] = {
405 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32, 0x00,
406 0x02, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x79, 0x10, 0x1c, 0xd0, 0x6b, 0x10,
407 0x18, 0x30, 0x94, 0x61, 0xdc, 0x0e, 0xcb, 0x96, 0x4e, 0x21, 0x3f, 0x79, 0xcd,
408 0xa9, 0x17, 0x62, 0xbc, 0xbb, 0x61, 0x4c, 0xe0, 0x75, 0x38, 0x6c, 0xf3, 0xde,
409 0x60, 0x86, 0x03, 0x97, 0x65, 0xeb, 0x1e, 0x6b, 0xdb, 0x53, 0x85, 0xad, 0x68,
410 0x21, 0xf1, 0x5d, 0xe7, 0x1f, 0xe6, 0x53, 0xb4, 0xbb, 0x59, 0x3e, 0x14, 0x27,
411 0xb1, 0x83, 0xa7, 0x3a, 0x54, 0xe2, 0x8f, 0x65, 0x8e, 0x6a, 0x4a, 0xcf, 0x3b,
412 0x1f, 0x65, 0xff, 0xfe, 0xf1, 0x31, 0x3a, 0x37, 0x7a, 0x8b, 0xcb, 0xc6, 0xd4,
413 0x98, 0x50, 0x36, 0x67, 0xe4, 0xa1, 0xe8, 0x7e, 0x8a, 0xc5, 0x23, 0xf2, 0x77,
414 0xf5, 0x37, 0x61, 0x49, 0x72, 0x59, 0xe8, 0x3d, 0xf7, 0x60, 0xb2, 0x77, 0xca,
415 0x78, 0x54, 0x6d, 0x65, 0x9e, 0x03, 0x97, 0x1b, 0x61, 0xbd, 0x0c, 0xd8, 0x06,
416 0x63, 0xe2, 0xc5, 0x48, 0xef, 0xb3, 0xe2, 0x6e, 0x98, 0x7d, 0xbd, 0x4e, 0x72,
417 0x91, 0xdb, 0x31, 0x57, 0xe3, 0x65, 0x3a, 0x49, 0xca, 0xec, 0xd2, 0x02, 0x4e,
418 0x22, 0x7e, 0x72, 0x8e, 0xf9, 0x79, 0x84, 0x82, 0xdf, 0x7b, 0x92, 0x2d, 0xaf,
419 0xc9, 0xe4, 0x33, 0xef, 0x89, 0x5c, 0x66, 0x99, 0xd8, 0x80, 0x81, 0x47, 0x2b,
420 0xb1, 0x66, 0x02, 0x84, 0x59, 0x7b, 0xc3, 0xbe, 0x98, 0x45, 0x4a, 0x3d, 0xdd,
421 0xea, 0x2b, 0xdf, 0x4e, 0xb4, 0x24, 0x6b, 0xec, 0xe7, 0xd9, 0x0c, 0x45, 0xb8,
422 0xbe, 0xca, 0x69, 0x37, 0x92, 0x4c, 0x38, 0x6b, 0x96, 0x6d, 0xcd, 0x86, 0x67,
423 0x5c, 0xea, 0x54, 0x94, 0xa4, 0xca, 0xa4, 0x02, 0xa5, 0x21, 0x4d, 0xae, 0x40,
424 0x8f, 0x9d, 0x51, 0x83, 0xf2, 0x3f, 0x33, 0xc1, 0x72, 0xb4, 0x1d, 0x94, 0x6e,
425 0x7d, 0xe4, 0x27, 0x3f, 0xea, 0xff, 0xe5, 0x9b, 0xa7, 0x5e, 0x55, 0x8e, 0x0d,
426 0x69, 0x1c, 0x7a, 0xff, 0x81, 0x9d, 0x53, 0x52, 0x97, 0x9a, 0x76, 0x79, 0xda,
427 0x93, 0x32, 0x16, 0xec, 0x69, 0x51, 0x1a, 0x4e, 0xc3, 0xf1, 0x72, 0x80, 0x78,
428 0x5e, 0x66, 0x4a, 0x8d, 0x85, 0x2f, 0x3f, 0xb2, 0xa7 };
430 static void test_verify_sig(void)
432 BOOL ret;
433 HCRYPTPROV prov;
434 HCRYPTKEY key;
435 HCRYPTHASH hash;
436 BYTE bogus[] = { 0 };
438 if (!pCryptVerifySignatureW)
440 win_skip("CryptVerifySignatureW is not available\n");
441 return;
444 SetLastError(0xdeadbeef);
445 ret = pCryptVerifySignatureW(0, NULL, 0, 0, NULL, 0);
446 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
448 win_skip("CryptVerifySignatureW is not implemented\n");
449 return;
451 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
452 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
453 ret = CryptAcquireContextA(&prov, szKeySet, NULL, PROV_RSA_FULL,
454 CRYPT_NEWKEYSET);
455 if (!ret && GetLastError() == NTE_EXISTS)
456 ret = CryptAcquireContextA(&prov, szKeySet, NULL, PROV_RSA_FULL, 0);
457 ok(ret, "CryptAcquireContextA failed: %08x\n", GetLastError());
458 ret = CryptImportKey(prov, (LPBYTE)privKey, sizeof(privKey), 0, 0, &key);
459 ok(ret, "CryptImportKey failed: %08x\n", GetLastError());
460 ret = CryptCreateHash(prov, CALG_MD5, 0, 0, &hash);
461 ok(ret, "CryptCreateHash failed: %08x\n", GetLastError());
462 SetLastError(0xdeadbeef);
463 ret = pCryptVerifySignatureW(hash, NULL, 0, 0, NULL, 0);
464 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
465 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
466 SetLastError(0xdeadbeef);
467 ret = pCryptVerifySignatureW(0, NULL, 0, key, NULL, 0);
468 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
469 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
470 SetLastError(0xdeadbeef);
471 ret = pCryptVerifySignatureW(hash, NULL, 0, key, NULL, 0);
472 ok(!ret && (GetLastError() == NTE_BAD_SIGNATURE ||
473 GetLastError() == ERROR_INVALID_PARAMETER),
474 "Expected NTE_BAD_SIGNATURE or ERROR_INVALID_PARAMETER, got %08x\n",
475 GetLastError());
476 SetLastError(0xdeadbeef);
477 ret = pCryptVerifySignatureW(hash, NULL, sizeof(bogus), key, NULL, 0);
478 ok(!ret && (GetLastError() == NTE_BAD_SIGNATURE ||
479 GetLastError() == ERROR_INVALID_PARAMETER),
480 "Expected NTE_BAD_SIGNATURE or ERROR_INVALID_PARAMETER, got %08x\n",
481 GetLastError());
482 SetLastError(0xdeadbeef);
483 ret = pCryptVerifySignatureW(hash, bogus, 0, key, NULL, 0);
484 ok(!ret && GetLastError() == NTE_BAD_SIGNATURE,
485 "Expected NTE_BAD_SIGNATURE, got %08x\n", GetLastError());
486 SetLastError(0xdeadbeef);
487 ret = pCryptVerifySignatureW(hash, bogus, sizeof(bogus), key, NULL, 0);
488 ok(!ret &&
489 (GetLastError() == NTE_BAD_SIGNATURE ||
490 broken(GetLastError() == NTE_BAD_HASH_STATE /* older NT4 */)),
491 "Expected NTE_BAD_SIGNATURE, got %08x\n", GetLastError());
492 CryptDestroyKey(key);
493 CryptDestroyHash(hash);
495 ret = CryptReleaseContext(prov, 0);
496 ok(ret, "got %u\n", GetLastError());
499 static BOOL FindProvRegVals(DWORD dwIndex, DWORD *pdwProvType, LPSTR *pszProvName,
500 DWORD *pcbProvName, DWORD *pdwProvCount)
502 HKEY hKey;
503 HKEY subkey;
504 DWORD size = sizeof(DWORD);
506 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey))
507 return FALSE;
509 RegQueryInfoKeyA(hKey, NULL, NULL, NULL, pdwProvCount, pcbProvName,
510 NULL, NULL, NULL, NULL, NULL, NULL);
511 (*pcbProvName)++;
513 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
514 return FALSE;
516 RegEnumKeyExA(hKey, dwIndex, *pszProvName, pcbProvName, NULL, NULL, NULL, NULL);
517 (*pcbProvName)++;
519 RegOpenKeyA(hKey, *pszProvName, &subkey);
520 RegQueryValueExA(subkey, "Type", NULL, NULL, (LPBYTE)pdwProvType, &size);
522 RegCloseKey(subkey);
523 RegCloseKey(hKey);
525 return TRUE;
528 static void test_enum_providers(void)
530 /* expected results */
531 CHAR *pszProvName = NULL;
532 DWORD cbName;
533 DWORD dwType;
534 DWORD provCount;
535 DWORD dwIndex = 0;
537 /* actual results */
538 CHAR *provider = NULL;
539 DWORD providerLen;
540 DWORD type;
541 DWORD count;
542 DWORD result;
543 DWORD notNull = 5;
544 DWORD notZeroFlags = 5;
546 if(!pCryptEnumProvidersA)
548 win_skip("CryptEnumProvidersA is not available\n");
549 return;
552 if (!FindProvRegVals(dwIndex, &dwType, &pszProvName, &cbName, &provCount))
554 win_skip("Could not find providers in registry\n");
555 return;
558 /* check pdwReserved flag for NULL */
559 result = pCryptEnumProvidersA(dwIndex, &notNull, 0, &type, NULL, &providerLen);
560 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "%d\n", GetLastError());
562 /* check dwFlags == 0 */
563 result = pCryptEnumProvidersA(dwIndex, NULL, notZeroFlags, &type, NULL, &providerLen);
564 ok(!result && GetLastError()==NTE_BAD_FLAGS, "%d\n", GetLastError());
566 /* alloc provider to half the size required
567 * cbName holds the size required */
568 providerLen = cbName / 2;
569 if (!(provider = LocalAlloc(LMEM_ZEROINIT, providerLen)))
570 return;
572 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
573 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %d\n",
574 ERROR_MORE_DATA, GetLastError());
576 LocalFree(provider);
578 /* loop through the providers to get the number of providers
579 * after loop ends, count should be provCount + 1 so subtract 1
580 * to get actual number of providers */
581 count = 0;
582 while(pCryptEnumProvidersA(count++, NULL, 0, &type, NULL, &providerLen))
584 count--;
585 ok(count==provCount, "expected %i, got %i\n", (int)provCount, (int)count);
587 /* loop past the actual number of providers to get the error
588 * ERROR_NO_MORE_ITEMS */
589 for (count = 0; count < provCount + 1; count++)
590 result = pCryptEnumProvidersA(count, NULL, 0, &type, NULL, &providerLen);
591 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected %i, got %d\n",
592 ERROR_NO_MORE_ITEMS, GetLastError());
594 /* check expected versus actual values returned */
595 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, NULL, &providerLen);
596 ok(result && providerLen==cbName, "expected %i, got %i\n", (int)cbName, (int)providerLen);
597 if (!(provider = LocalAlloc(LMEM_ZEROINIT, providerLen)))
598 return;
600 providerLen = -1;
601 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
602 ok(result, "expected TRUE, got %d\n", result);
603 ok(type==dwType, "expected %d, got %d\n", dwType, type);
604 if (pszProvName)
605 ok(!strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
606 ok(cbName==providerLen, "expected %d, got %d\n", cbName, providerLen);
608 providerLen = -1000;
609 provider[0] = 0;
610 result = pCryptEnumProvidersA(dwIndex, NULL, 0, &type, provider, &providerLen);
611 ok(result, "expected TRUE, got %d\n", result);
612 ok(type==dwType, "expected %d, got %d\n", dwType, type);
613 if (pszProvName)
614 ok(!strcmp(pszProvName, provider), "expected %s, got %s\n", pszProvName, provider);
615 ok(cbName==providerLen, "expected %d, got %d\n", cbName, providerLen);
617 LocalFree(pszProvName);
618 LocalFree(provider);
621 static BOOL FindProvTypesRegVals(DWORD *pdwIndex, DWORD *pdwProvType, LPSTR *pszTypeName,
622 DWORD *pcbTypeName, DWORD *pdwTypeCount)
624 HKEY hKey;
625 HKEY hSubKey;
626 PSTR ch;
627 LPSTR szName;
628 DWORD cbName;
629 BOOL ret = FALSE;
631 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey))
632 return FALSE;
634 if (RegQueryInfoKeyA(hKey, NULL, NULL, NULL, pdwTypeCount, &cbName, NULL,
635 NULL, NULL, NULL, NULL, NULL))
636 goto cleanup;
637 cbName++;
639 if (!(szName = LocalAlloc(LMEM_ZEROINIT, cbName)))
640 goto cleanup;
642 while (!RegEnumKeyExA(hKey, *pdwIndex, szName, &cbName, NULL, NULL, NULL, NULL))
644 cbName++;
645 ch = szName + strlen(szName);
646 /* Convert "Type 000" to 0, etc/ */
647 *pdwProvType = *(--ch) - '0';
648 *pdwProvType += (*(--ch) - '0') * 10;
649 *pdwProvType += (*(--ch) - '0') * 100;
651 if (RegOpenKeyA(hKey, szName, &hSubKey))
652 break;
654 if (!RegQueryValueExA(hSubKey, "TypeName", NULL, NULL, NULL, pcbTypeName))
656 if (!(*pszTypeName = LocalAlloc(LMEM_ZEROINIT, *pcbTypeName)))
657 break;
659 if (!RegQueryValueExA(hSubKey, "TypeName", NULL, NULL, (LPBYTE)*pszTypeName, pcbTypeName))
661 ret = TRUE;
662 break;
665 LocalFree(*pszTypeName);
668 RegCloseKey(hSubKey);
670 (*pdwIndex)++;
672 RegCloseKey(hSubKey);
673 LocalFree(szName);
675 cleanup:
676 RegCloseKey(hKey);
678 return ret;
681 static void test_enum_provider_types(void)
683 /* expected values */
684 DWORD dwProvType = 0;
685 LPSTR pszTypeName = NULL;
686 DWORD cbTypeName;
687 DWORD dwTypeCount;
689 /* actual values */
690 DWORD index = 0;
691 DWORD provType;
692 LPSTR typeName = NULL;
693 DWORD typeNameSize;
694 DWORD typeCount;
695 DWORD result;
696 DWORD notNull = 5;
697 DWORD notZeroFlags = 5;
699 if(!pCryptEnumProviderTypesA)
701 win_skip("CryptEnumProviderTypesA is not available\n");
702 return;
705 if (!FindProvTypesRegVals(&index, &dwProvType, &pszTypeName, &cbTypeName, &dwTypeCount))
707 skip("Could not find provider types in registry\n");
708 return;
711 /* check pdwReserved for NULL */
712 result = pCryptEnumProviderTypesA(index, &notNull, 0, &provType, typeName, &typeNameSize);
713 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n",
714 GetLastError());
716 /* check dwFlags == zero */
717 result = pCryptEnumProviderTypesA(index, NULL, notZeroFlags, &provType, typeName, &typeNameSize);
718 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected ERROR_INVALID_PARAMETER, got %d\n",
719 GetLastError());
721 /* This test fails under Win2k SP4:
722 * result = TRUE, GetLastError() == 0xdeadbeef */
723 if (0)
725 /* alloc provider type to half the size required
726 * cbTypeName holds the size required */
727 typeNameSize = cbTypeName / 2;
728 if (!(typeName = LocalAlloc(LMEM_ZEROINIT, typeNameSize)))
729 goto cleanup;
731 SetLastError(0xdeadbeef);
732 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
733 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected 0/ERROR_MORE_DATA, got %d/%d\n",
734 result, GetLastError());
736 LocalFree(typeName);
739 /* loop through the provider types to get the number of provider types
740 * after loop ends, count should be dwTypeCount + 1 so subtract 1
741 * to get actual number of provider types */
742 typeCount = 0;
743 while(pCryptEnumProviderTypesA(typeCount++, NULL, 0, &provType, NULL, &typeNameSize))
745 typeCount--;
746 ok(typeCount==dwTypeCount, "expected %d, got %d\n", dwTypeCount, typeCount);
748 /* loop past the actual number of provider types to get the error
749 * ERROR_NO_MORE_ITEMS */
750 for (typeCount = 0; typeCount < dwTypeCount + 1; typeCount++)
751 result = pCryptEnumProviderTypesA(typeCount, NULL, 0, &provType, NULL, &typeNameSize);
752 ok(!result && GetLastError()==ERROR_NO_MORE_ITEMS, "expected ERROR_NO_MORE_ITEMS, got %d\n",
753 GetLastError());
755 /* check expected versus actual values returned */
756 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, NULL, &typeNameSize);
757 ok(result && typeNameSize==cbTypeName, "expected %d, got %d\n", cbTypeName, typeNameSize);
758 if (!(typeName = LocalAlloc(LMEM_ZEROINIT, typeNameSize)))
759 goto cleanup;
761 typeNameSize = 0xdeadbeef;
762 result = pCryptEnumProviderTypesA(index, NULL, 0, &provType, typeName, &typeNameSize);
763 ok(result, "expected TRUE, got %d\n", result);
764 ok(provType==dwProvType, "expected %d, got %d\n", dwProvType, provType);
765 if (pszTypeName)
766 ok(!strcmp(pszTypeName, typeName), "expected %s, got %s\n", pszTypeName, typeName);
767 ok(typeNameSize==cbTypeName, "expected %d, got %d\n", cbTypeName, typeNameSize);
769 LocalFree(typeName);
770 cleanup:
771 LocalFree(pszTypeName);
774 static BOOL FindDfltProvRegVals(DWORD dwProvType, DWORD dwFlags, LPSTR *pszProvName, DWORD *pcbProvName)
776 HKEY hKey;
777 PSTR keyname;
778 PSTR ptr;
779 DWORD user = dwFlags & CRYPT_USER_DEFAULT;
781 LPCSTR machinestr = "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
782 LPCSTR userstr = "Software\\Microsoft\\Cryptography\\Provider Type XXX";
784 keyname = LocalAlloc(LMEM_ZEROINIT, (user ? strlen(userstr) : strlen(machinestr)) + 1);
785 if (keyname)
787 user ? strcpy(keyname, userstr) : strcpy(keyname, machinestr);
788 ptr = keyname + strlen(keyname);
789 *(--ptr) = (dwProvType % 10) + '0';
790 *(--ptr) = ((dwProvType / 10) % 10) + '0';
791 *(--ptr) = (dwProvType / 100) + '0';
792 } else
793 return FALSE;
795 if (RegOpenKeyA((dwFlags & CRYPT_USER_DEFAULT) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE ,keyname, &hKey))
797 LocalFree(keyname);
798 return FALSE;
800 LocalFree(keyname);
802 if (RegQueryValueExA(hKey, "Name", NULL, NULL, (LPBYTE)*pszProvName, pcbProvName))
804 if (GetLastError() != ERROR_MORE_DATA)
805 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
806 return FALSE;
809 if (!(*pszProvName = LocalAlloc(LMEM_ZEROINIT, *pcbProvName)))
810 return FALSE;
812 if (RegQueryValueExA(hKey, "Name", NULL, NULL, (LPBYTE)*pszProvName, pcbProvName))
814 if (GetLastError() != ERROR_MORE_DATA)
815 SetLastError(NTE_PROV_TYPE_ENTRY_BAD);
816 return FALSE;
819 RegCloseKey(hKey);
821 return TRUE;
824 static void test_get_default_provider(void)
826 /* expected results */
827 DWORD dwProvType = PROV_RSA_FULL;
828 DWORD dwFlags = CRYPT_MACHINE_DEFAULT;
829 LPSTR pszProvName = NULL;
830 DWORD cbProvName;
832 /* actual results */
833 DWORD provType = PROV_RSA_FULL;
834 DWORD flags = CRYPT_MACHINE_DEFAULT;
835 LPSTR provName = NULL;
836 DWORD provNameSize;
837 DWORD result;
838 DWORD notNull = 5;
840 if(!pCryptGetDefaultProviderA)
842 win_skip("CryptGetDefaultProviderA is not available\n");
843 return;
846 if(!FindDfltProvRegVals(dwProvType, dwFlags, &pszProvName, &cbProvName))
848 skip("Could not find default provider in registry\n");
849 return;
852 /* check pdwReserved for NULL */
853 result = pCryptGetDefaultProviderA(provType, &notNull, flags, provName, &provNameSize);
854 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %d\n",
855 ERROR_INVALID_PARAMETER, GetLastError());
857 /* check for invalid flag */
858 flags = 0xdeadbeef;
859 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
860 ok(!result && GetLastError()==NTE_BAD_FLAGS, "expected %d, got %d\n",
861 NTE_BAD_FLAGS, GetLastError());
862 flags = CRYPT_MACHINE_DEFAULT;
864 /* check for invalid prov type */
865 provType = 0xdeadbeef;
866 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
867 ok(!result && (GetLastError() == NTE_BAD_PROV_TYPE ||
868 GetLastError() == ERROR_INVALID_PARAMETER),
869 "expected NTE_BAD_PROV_TYPE or ERROR_INVALID_PARAMETER, got %d/%d\n",
870 result, GetLastError());
871 provType = PROV_RSA_FULL;
873 SetLastError(0);
875 /* alloc provName to half the size required
876 * cbProvName holds the size required */
877 provNameSize = cbProvName / 2;
878 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
879 return;
881 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
882 ok(!result && GetLastError()==ERROR_MORE_DATA, "expected %i, got %d\n",
883 ERROR_MORE_DATA, GetLastError());
885 LocalFree(provName);
887 /* check expected versus actual values returned */
888 result = pCryptGetDefaultProviderA(provType, NULL, flags, NULL, &provNameSize);
889 ok(result && provNameSize==cbProvName, "expected %d, got %d\n", cbProvName, provNameSize);
890 provNameSize = cbProvName;
892 if (!(provName = LocalAlloc(LMEM_ZEROINIT, provNameSize)))
893 return;
895 provNameSize = 0xdeadbeef;
896 result = pCryptGetDefaultProviderA(provType, NULL, flags, provName, &provNameSize);
897 ok(result, "expected TRUE, got %d\n", result);
898 if(pszProvName)
899 ok(!strcmp(pszProvName, provName), "expected %s, got %s\n", pszProvName, provName);
900 ok(provNameSize==cbProvName, "expected %d, got %d\n", cbProvName, provNameSize);
902 LocalFree(pszProvName);
903 LocalFree(provName);
906 static void test_set_provider_ex(void)
908 DWORD result;
909 DWORD notNull = 5;
910 LPSTR curProvName = NULL;
911 DWORD curlen;
913 /* results */
914 LPSTR pszProvName = NULL;
915 DWORD cbProvName;
917 if(!pCryptGetDefaultProviderA || !pCryptSetProviderExA)
919 win_skip("CryptGetDefaultProviderA and/or CryptSetProviderExA are not available\n");
920 return;
923 /* store the current one */
924 pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &curlen);
925 if (!(curProvName = LocalAlloc(LMEM_ZEROINIT, curlen)))
926 return;
927 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, curProvName, &curlen);
928 ok(result, "%d\n", GetLastError());
930 /* check pdwReserved for NULL */
931 result = pCryptSetProviderExA(MS_DEF_PROV_A, PROV_RSA_FULL, &notNull, CRYPT_MACHINE_DEFAULT);
932 ok(!result && GetLastError()==ERROR_INVALID_PARAMETER, "expected %i, got %d\n",
933 ERROR_INVALID_PARAMETER, GetLastError());
935 /* remove the default provider and then set it to MS_DEF_PROV/PROV_RSA_FULL */
936 SetLastError(0xdeadbeef);
937 result = pCryptSetProviderExA(MS_DEF_PROV_A, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT | CRYPT_DELETE_DEFAULT);
938 if (!result)
940 ok( GetLastError() == ERROR_ACCESS_DENIED || broken(GetLastError() == ERROR_INVALID_PARAMETER),
941 "wrong error %u\n", GetLastError() );
942 skip("Not enough rights to remove the default provider\n");
943 LocalFree(curProvName);
944 return;
947 result = pCryptSetProviderExA(MS_DEF_PROV_A, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
948 ok(result, "%d\n", GetLastError());
950 /* call CryptGetDefaultProvider to see if they match */
951 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, NULL, &cbProvName);
952 ok(result, "%d\n", GetLastError());
953 if (!(pszProvName = LocalAlloc(LMEM_ZEROINIT, cbProvName)))
954 goto reset;
956 result = pCryptGetDefaultProviderA(PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, pszProvName, &cbProvName);
957 ok(result && !strcmp(MS_DEF_PROV_A, pszProvName), "expected %s, got %s\n", MS_DEF_PROV_A, pszProvName);
958 ok(result && cbProvName==(strlen(MS_DEF_PROV_A) + 1), "expected %i, got %d\n", (lstrlenA(MS_DEF_PROV_A) + 1), cbProvName);
960 LocalFree(pszProvName);
962 reset:
963 /* Set the provider back to its original */
964 result = pCryptSetProviderExA(curProvName, PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT);
965 ok(result, "%d\n", GetLastError());
966 LocalFree(curProvName);
969 static void test_machine_guid(void)
971 char originalGuid[40];
972 LONG r;
973 HKEY key;
974 DWORD size;
975 HCRYPTPROV hCryptProv;
976 BOOL restoreGuid = FALSE, ret;
978 r = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Cryptography",
979 0, KEY_ALL_ACCESS, &key);
980 if (r != ERROR_SUCCESS)
982 skip("couldn't open HKLM\\Software\\Microsoft\\Cryptography\n");
983 return;
985 /* Cache existing MachineGuid, and delete it */
986 size = sizeof(originalGuid);
987 r = RegQueryValueExA(key, "MachineGuid", NULL, NULL, (BYTE *)originalGuid,
988 &size);
989 if (r == ERROR_SUCCESS)
991 restoreGuid = TRUE;
992 r = RegDeleteValueA(key, "MachineGuid");
993 ok(!r || broken(r == ERROR_ACCESS_DENIED) /*win8*/, "RegDeleteValueA failed: %d\n", r);
994 if (r == ERROR_ACCESS_DENIED)
996 skip("broken virtualization on HKLM\\Software\\Microsoft\\Cryptography\n");
997 RegCloseKey(key);
998 return;
1001 else
1002 ok(r == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n",
1004 /* Create and release a provider */
1005 ret = CryptAcquireContextA(&hCryptProv, szKeySet, NULL, PROV_RSA_FULL, 0);
1006 ok(ret || broken(!ret && GetLastError() == NTE_KEYSET_ENTRY_BAD /* NT4 */),
1007 "CryptAcquireContextA failed: %08x\n", GetLastError());
1008 ret = CryptReleaseContext(hCryptProv, 0);
1009 ok(ret, "got %u\n", GetLastError());
1011 if (restoreGuid)
1012 RegSetValueExA(key, "MachineGuid", 0, REG_SZ, (const BYTE *)originalGuid,
1013 strlen(originalGuid)+1);
1014 RegCloseKey(key);
1017 #define key_length 16
1019 static const unsigned char key[key_length] =
1020 { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd,
1021 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 };
1023 static void test_rc2_keylen(void)
1025 struct KeyBlob
1027 BLOBHEADER header;
1028 DWORD key_size;
1029 BYTE key_data[2048];
1030 } key_blob;
1032 HCRYPTPROV provider;
1033 HCRYPTKEY hkey = 0;
1034 BOOL ret;
1036 SetLastError(0xdeadbeef);
1037 ret = CryptAcquireContextA(&provider, NULL, NULL,
1038 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
1039 ok(ret, "CryptAcquireContext error %u\n", GetLastError());
1040 if (ret)
1042 key_blob.header.bType = PLAINTEXTKEYBLOB;
1043 key_blob.header.bVersion = CUR_BLOB_VERSION;
1044 key_blob.header.reserved = 0;
1045 key_blob.header.aiKeyAlg = CALG_RC2;
1046 key_blob.key_size = sizeof(key);
1047 memcpy(key_blob.key_data, key, key_length);
1049 /* Importing a 16-byte key works with the default provider. */
1050 SetLastError(0xdeadbeef);
1051 ret = CryptImportKey(provider, (BYTE *)&key_blob, sizeof(BLOBHEADER) + sizeof(DWORD) + key_blob.key_size,
1052 0, CRYPT_IPSEC_HMAC_KEY, &hkey);
1053 /* CRYPT_IPSEC_HMAC_KEY is not supported on W2K and lower */
1054 ok(ret ||
1055 broken(!ret && GetLastError() == NTE_BAD_FLAGS),
1056 "CryptImportKey error %08x\n", GetLastError());
1057 if (ret)
1058 CryptDestroyKey(hkey);
1060 ret = CryptReleaseContext(provider, 0);
1061 ok(ret, "got %u\n", GetLastError());
1064 SetLastError(0xdeadbeef);
1065 ret = CryptAcquireContextA(&provider, NULL, MS_DEF_PROV_A,
1066 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
1067 ok(ret, "CryptAcquireContext error %08x\n", GetLastError());
1069 if (ret)
1071 /* Importing a 16-byte key doesn't work with the base provider.. */
1072 SetLastError(0xdeadbeef);
1073 ret = CryptImportKey(provider, (BYTE *)&key_blob, sizeof(BLOBHEADER) + sizeof(DWORD) + key_blob.key_size,
1074 0, 0, &hkey);
1075 ok(!ret && (GetLastError() == NTE_BAD_DATA ||
1076 GetLastError() == NTE_BAD_LEN || /* Win7 */
1077 GetLastError() == NTE_BAD_TYPE || /* W2K */
1078 GetLastError() == NTE_PERM), /* Win9x, WinMe and NT4 */
1079 "unexpected error %08x\n", GetLastError());
1080 /* but importing an 56-bit (7-byte) key does.. */
1081 key_blob.key_size = 7;
1082 SetLastError(0xdeadbeef);
1083 ret = CryptImportKey(provider, (BYTE *)&key_blob, sizeof(BLOBHEADER) + sizeof(DWORD) + key_blob.key_size,
1084 0, 0, &hkey);
1085 ok(ret ||
1086 broken(!ret && GetLastError() == NTE_BAD_TYPE) || /* W2K */
1087 broken(!ret && GetLastError() == NTE_PERM), /* Win9x, WinMe and NT4 */
1088 "CryptAcquireContext error %08x\n", GetLastError());
1089 if (ret)
1090 CryptDestroyKey(hkey);
1091 /* as does importing a 16-byte key with the base provider when
1092 * CRYPT_IPSEC_HMAC_KEY is specified.
1094 key_blob.key_size = sizeof(key);
1095 SetLastError(0xdeadbeef);
1096 ret = CryptImportKey(provider, (BYTE *)&key_blob, sizeof(BLOBHEADER) + sizeof(DWORD) + key_blob.key_size,
1097 0, CRYPT_IPSEC_HMAC_KEY, &hkey);
1098 /* CRYPT_IPSEC_HMAC_KEY is not supported on W2K and lower */
1099 ok(ret ||
1100 broken(!ret && GetLastError() == NTE_BAD_FLAGS),
1101 "CryptImportKey error %08x\n", GetLastError());
1102 if (ret)
1103 CryptDestroyKey(hkey);
1105 ret = CryptReleaseContext(provider, 0);
1106 ok(ret, "got %u\n", GetLastError());
1109 key_blob.key_size = sizeof(key);
1110 SetLastError(0xdeadbeef);
1111 ret = CryptAcquireContextA(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
1112 ok(ret, "CryptAcquireContext error %08x\n", GetLastError());
1114 if (ret)
1116 /* Importing a 16-byte key also works with the default provider when
1117 * CRYPT_IPSEC_HMAC_KEY is specified.
1119 SetLastError(0xdeadbeef);
1120 ret = CryptImportKey(provider, (BYTE *)&key_blob, sizeof(BLOBHEADER) + sizeof(DWORD) + key_blob.key_size,
1121 0, CRYPT_IPSEC_HMAC_KEY, &hkey);
1122 ok(ret ||
1123 broken(!ret && GetLastError() == NTE_BAD_FLAGS),
1124 "CryptImportKey error %08x\n", GetLastError());
1125 if (ret)
1126 CryptDestroyKey(hkey);
1128 /* There is no apparent limit to the size of the input key when
1129 * CRYPT_IPSEC_HMAC_KEY is specified.
1131 key_blob.key_size = sizeof(key_blob.key_data);
1132 SetLastError(0xdeadbeef);
1133 ret = CryptImportKey(provider, (BYTE *)&key_blob, sizeof(BLOBHEADER) + sizeof(DWORD) + key_blob.key_size,
1134 0, CRYPT_IPSEC_HMAC_KEY, &hkey);
1135 ok(ret ||
1136 broken(!ret && GetLastError() == NTE_BAD_FLAGS),
1137 "CryptImportKey error %08x\n", GetLastError());
1138 if (ret)
1139 CryptDestroyKey(hkey);
1141 ret = CryptReleaseContext(provider, 0);
1142 ok(ret, "got %u\n", GetLastError());
1146 static void test_SystemFunction036(void)
1148 BOOL ret;
1149 int test;
1151 if (!pSystemFunction036)
1153 win_skip("SystemFunction036 is not available\n");
1154 return;
1157 ret = pSystemFunction036(NULL, 0);
1158 ok(ret == TRUE, "Expected SystemFunction036 to return TRUE, got %d\n", ret);
1160 /* Test crashes on Windows. */
1161 if (0)
1163 SetLastError(0xdeadbeef);
1164 ret = pSystemFunction036(NULL, 5);
1165 trace("ret = %d, GetLastError() = %d\n", ret, GetLastError());
1168 ret = pSystemFunction036(&test, 0);
1169 ok(ret == TRUE, "Expected SystemFunction036 to return TRUE, got %d\n", ret);
1171 ret = pSystemFunction036(&test, sizeof(int));
1172 ok(ret == TRUE, "Expected SystemFunction036 to return TRUE, got %d\n", ret);
1175 static void test_container_sd(void)
1177 HCRYPTPROV prov;
1178 SECURITY_DESCRIPTOR *sd;
1179 DWORD len, err;
1180 BOOL ret;
1182 ret = CryptAcquireContextA(&prov, "winetest", "Microsoft Enhanced Cryptographic Provider v1.0",
1183 PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET);
1184 ok(ret, "got %u\n", GetLastError());
1186 len = 0;
1187 SetLastError(0xdeadbeef);
1188 ret = CryptGetProvParam(prov, PP_KEYSET_SEC_DESCR, NULL, &len, OWNER_SECURITY_INFORMATION);
1189 err = GetLastError();
1190 ok(ret, "got %u\n", err);
1191 ok(err == ERROR_INSUFFICIENT_BUFFER || broken(err == ERROR_INVALID_PARAMETER), "got %u\n", err);
1192 ok(len, "expected len > 0\n");
1194 sd = HeapAlloc(GetProcessHeap(), 0, len);
1195 ret = CryptGetProvParam(prov, PP_KEYSET_SEC_DESCR, (BYTE *)sd, &len, OWNER_SECURITY_INFORMATION);
1196 ok(ret, "got %u\n", GetLastError());
1197 HeapFree(GetProcessHeap(), 0, sd);
1199 ret = CryptReleaseContext(prov, 0);
1200 ok(ret, "got %u\n", GetLastError());
1202 prov = 0xdeadbeef;
1203 ret = CryptAcquireContextA(&prov, "winetest", "Microsoft Enhanced Cryptographic Provider v1.0",
1204 PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_DELETEKEYSET);
1205 ok(ret, "got %u\n", GetLastError());
1206 ok(prov == 0, "got %ld\n", prov);
1209 START_TEST(crypt)
1211 init_function_pointers();
1213 test_rc2_keylen();
1215 init_environment();
1216 test_CryptReleaseContext();
1217 test_acquire_context();
1218 test_incorrect_api_usage();
1219 test_verify_sig();
1220 test_machine_guid();
1221 test_container_sd();
1222 clean_up_environment();
1224 test_enum_providers();
1225 test_enum_provider_types();
1226 test_get_default_provider();
1227 test_set_provider_ex();
1228 test_SystemFunction036();