2 * Miscellaneous crypt32 tests
4 * Copyright 2005 Juan Lang
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
29 #include "wine/test.h"
33 static void test_findAttribute(void)
36 BYTE blobbin[] = {0x02,0x01,0x01};
37 static CHAR oid[] = "1.2.3";
38 CRYPT_ATTR_BLOB blobs[] = { { sizeof blobbin, blobbin }, };
39 CRYPT_ATTRIBUTE attr = { oid, sizeof(blobs) / sizeof(blobs[0]), blobs };
41 /* returns NULL, last error not set */
42 SetLastError(0xdeadbeef);
43 ret = CertFindAttribute(NULL, 0, NULL);
44 ok(ret == NULL, "Expected failure\n");
45 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
50 SetLastError(0xdeadbeef);
51 ret = CertFindAttribute(NULL, 1, NULL);
52 /* returns NULL, last error is ERROR_INVALID_PARAMETER
55 SetLastError(0xdeadbeef);
56 ret = CertFindAttribute(NULL, 1, &attr);
57 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
58 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
61 /* returns NULL, last error not set */
62 SetLastError(0xdeadbeef);
63 ret = CertFindAttribute("bogus", 1, &attr);
64 ok(ret == NULL, "Expected failure\n");
65 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
67 /* returns NULL, last error not set */
68 SetLastError(0xdeadbeef);
69 ret = CertFindAttribute("1.2.4", 1, &attr);
70 ok(ret == NULL, "Expected failure\n");
71 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
73 /* succeeds, last error not set */
74 SetLastError(0xdeadbeef);
75 ret = CertFindAttribute("1.2.3", 1, &attr);
76 ok(ret != NULL, "CertFindAttribute failed: %08x\n", GetLastError());
79 static void test_findExtension(void)
82 static CHAR oid[] = "1.2.3";
83 BYTE blobbin[] = {0x02,0x01,0x01};
84 CERT_EXTENSION ext = { oid, TRUE, { sizeof blobbin, blobbin } };
86 /* returns NULL, last error not set */
87 SetLastError(0xdeadbeef);
88 ret = CertFindExtension(NULL, 0, NULL);
89 ok(ret == NULL, "Expected failure\n");
90 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
95 SetLastError(0xdeadbeef);
96 ret = CertFindExtension(NULL, 1, NULL);
97 /* returns NULL, last error is ERROR_INVALID_PARAMETER
100 SetLastError(0xdeadbeef);
101 ret = CertFindExtension(NULL, 1, &ext);
102 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
103 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
106 /* returns NULL, last error not set */
107 SetLastError(0xdeadbeef);
108 ret = CertFindExtension("bogus", 1, &ext);
109 ok(ret == NULL, "Expected failure\n");
110 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
112 /* returns NULL, last error not set */
113 SetLastError(0xdeadbeef);
114 ret = CertFindExtension("1.2.4", 1, &ext);
115 ok(ret == NULL, "Expected failure\n");
116 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
118 /* succeeds, last error not set */
119 SetLastError(0xdeadbeef);
120 ret = CertFindExtension("1.2.3", 1, &ext);
121 ok(ret != NULL, "CertFindExtension failed: %08x\n", GetLastError());
124 static void test_findRDNAttr(void)
127 static CHAR oid[] = "1.2.3";
128 BYTE bin[] = { 0x16,0x09,'J','u','a','n',' ','L','a','n','g' };
129 CERT_RDN_ATTR attrs[] = {
130 { oid, CERT_RDN_IA5_STRING, { sizeof bin, bin } },
133 { sizeof(attrs) / sizeof(attrs[0]), attrs },
135 CERT_NAME_INFO nameInfo = { sizeof(rdns) / sizeof(rdns[0]), rdns };
140 SetLastError(0xdeadbeef);
141 ret = CertFindRDNAttr(NULL, NULL);
142 /* returns NULL, last error is ERROR_INVALID_PARAMETER
145 SetLastError(0xdeadbeef);
146 ret = CertFindRDNAttr(NULL, &nameInfo);
147 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
148 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
151 /* returns NULL, last error not set */
152 SetLastError(0xdeadbeef);
153 ret = CertFindRDNAttr("bogus", &nameInfo);
154 ok(ret == NULL, "Expected failure\n");
155 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
157 /* returns NULL, last error not set */
158 SetLastError(0xdeadbeef);
159 ret = CertFindRDNAttr("1.2.4", &nameInfo);
160 ok(ret == NULL, "Expected failure\n");
161 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
163 /* succeeds, last error not set */
164 SetLastError(0xdeadbeef);
165 ret = CertFindRDNAttr("1.2.3", &nameInfo);
166 ok(ret != NULL, "CertFindRDNAttr failed: %08x\n", GetLastError());
169 static void test_verifyTimeValidity(void)
173 CERT_INFO info = { 0 };
176 GetSystemTime(&sysTime);
177 SystemTimeToFileTime(&sysTime, &fileTime);
179 ret = CertVerifyTimeValidity(NULL, NULL);
180 ret = CertVerifyTimeValidity(&fileTime, NULL);
182 /* Check with 0 NotBefore and NotAfter */
183 ret = CertVerifyTimeValidity(&fileTime, &info);
184 ok(ret == 1, "Expected 1, got %d\n", ret);
185 memcpy(&info.NotAfter, &fileTime, sizeof(info.NotAfter));
186 /* Check with NotAfter equal to comparison time */
187 ret = CertVerifyTimeValidity(&fileTime, &info);
188 ok(ret == 0, "Expected 0, got %d\n", ret);
189 /* Check with NotBefore after comparison time */
190 memcpy(&info.NotBefore, &fileTime, sizeof(info.NotBefore));
191 info.NotBefore.dwLowDateTime += 5000;
192 ret = CertVerifyTimeValidity(&fileTime, &info);
193 ok(ret == -1, "Expected -1, got %d\n", ret);
196 static void test_cryptAllocate(void)
200 buf = CryptMemAlloc(0);
201 ok(buf != NULL, "CryptMemAlloc failed: %08x\n", GetLastError());
203 /* CryptMemRealloc(NULL, 0) fails pre-Vista */
204 buf = CryptMemAlloc(0);
205 buf = CryptMemRealloc(buf, 1);
206 ok(buf != NULL, "CryptMemRealloc failed: %08x\n", GetLastError());
210 typedef DWORD (WINAPI *I_CryptAllocTlsFunc)(void);
211 typedef LPVOID (WINAPI *I_CryptDetachTlsFunc)(DWORD dwTlsIndex);
212 typedef LPVOID (WINAPI *I_CryptGetTlsFunc)(DWORD dwTlsIndex);
213 typedef BOOL (WINAPI *I_CryptSetTlsFunc)(DWORD dwTlsIndex, LPVOID lpTlsValue);
214 typedef BOOL (WINAPI *I_CryptFreeTlsFunc)(DWORD dwTlsIndex, DWORD unknown);
216 static I_CryptAllocTlsFunc pI_CryptAllocTls;
217 static I_CryptDetachTlsFunc pI_CryptDetachTls;
218 static I_CryptGetTlsFunc pI_CryptGetTls;
219 static I_CryptSetTlsFunc pI_CryptSetTls;
220 static I_CryptFreeTlsFunc pI_CryptFreeTls;
222 static void test_cryptTls(void)
227 pI_CryptAllocTls = (I_CryptAllocTlsFunc)GetProcAddress(hCrypt,
229 pI_CryptDetachTls = (I_CryptDetachTlsFunc)GetProcAddress(hCrypt,
231 pI_CryptGetTls = (I_CryptGetTlsFunc)GetProcAddress(hCrypt,
233 pI_CryptSetTls = (I_CryptSetTlsFunc)GetProcAddress(hCrypt,
235 pI_CryptFreeTls = (I_CryptFreeTlsFunc)GetProcAddress(hCrypt,
238 /* One normal pass */
239 index = pI_CryptAllocTls();
240 ok(index, "I_CryptAllocTls failed: %08x\n", GetLastError());
245 ptr = pI_CryptGetTls(index);
246 ok(!ptr, "Expected NULL\n");
247 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
248 ok(ret, "I_CryptSetTls failed: %08x\n", GetLastError());
249 ptr = pI_CryptGetTls(index);
250 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
252 ret = pI_CryptFreeTls(index, 1);
254 ret = pI_CryptFreeTls(index, 0);
255 ok(ret, "I_CryptFreeTls failed: %08x\n", GetLastError());
256 ret = pI_CryptFreeTls(index, 0);
257 /* Not sure if this fails because TlsFree should fail, so leave as
260 todo_wine ok(!ret && GetLastError() == E_INVALIDARG,
261 "Expected E_INVALIDARG, got %08x\n", GetLastError());
263 /* Similar pass, check I_CryptDetachTls */
264 index = pI_CryptAllocTls();
265 ok(index, "I_CryptAllocTls failed: %08x\n", GetLastError());
270 ptr = pI_CryptGetTls(index);
271 ok(!ptr, "Expected NULL\n");
272 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
273 ok(ret, "I_CryptSetTls failed: %08x\n", GetLastError());
274 ptr = pI_CryptGetTls(index);
275 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
276 ptr = pI_CryptDetachTls(index);
277 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
278 ptr = pI_CryptGetTls(index);
279 ok(!ptr, "Expected NULL\n");
283 typedef BOOL (WINAPI *I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)
286 static void test_readTrustedPublisherDWORD(void)
288 I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc pReadDWORD;
291 (I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)GetProcAddress(
292 hCrypt, "I_CryptReadTrustedPublisherDWORDValueFromRegistry");
295 static const WCHAR safer[] = {
296 'S','o','f','t','w','a','r','e','\\',
297 'P','o','l','i','c','i','e','s','\\',
298 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
299 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
300 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',
301 '\\','S','a','f','e','r',0 };
302 static const WCHAR authenticodeFlags[] = { 'A','u','t','h','e','n',
303 't','i','c','o','d','e','F','l','a','g','s',0 };
304 BOOL ret, exists = FALSE;
305 DWORD size, readFlags = 0, returnedFlags;
309 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, safer, &key);
310 if (rc == ERROR_SUCCESS)
312 size = sizeof(readFlags);
313 rc = RegQueryValueExW(key, authenticodeFlags, NULL, NULL,
314 (LPBYTE)&readFlags, &size);
315 if (rc == ERROR_SUCCESS)
318 returnedFlags = 0xdeadbeef;
319 ret = pReadDWORD(authenticodeFlags, &returnedFlags);
320 ok(ret == exists, "Unexpected return value\n");
321 ok(readFlags == returnedFlags,
322 "Expected flags %08x, got %08x\n", readFlags, returnedFlags);
326 typedef HCRYPTPROV (WINAPI *I_CryptGetDefaultCryptProvFunc)(DWORD w);
328 static void test_getDefaultCryptProv(void)
330 I_CryptGetDefaultCryptProvFunc pI_CryptGetDefaultCryptProv;
333 pI_CryptGetDefaultCryptProv = (I_CryptGetDefaultCryptProvFunc)
334 GetProcAddress(hCrypt, "I_CryptGetDefaultCryptProv");
335 if (!pI_CryptGetDefaultCryptProv) return;
337 prov = pI_CryptGetDefaultCryptProv(0xdeadbeef);
338 ok(prov == 0 && GetLastError() == E_INVALIDARG,
339 "Expected E_INVALIDARG, got %08x\n", GetLastError());
340 prov = pI_CryptGetDefaultCryptProv(PROV_RSA_FULL);
341 ok(prov == 0 && GetLastError() == E_INVALIDARG,
342 "Expected E_INVALIDARG, got %08x\n", GetLastError());
343 prov = pI_CryptGetDefaultCryptProv(1);
344 ok(prov == 0 && GetLastError() == E_INVALIDARG,
345 "Expected E_INVALIDARG, got %08x\n", GetLastError());
346 prov = pI_CryptGetDefaultCryptProv(0);
347 ok(prov != 0, "I_CryptGetDefaultCryptProv failed: %08x\n", GetLastError());
348 CryptReleaseContext(prov, 0);
351 typedef int (WINAPI *I_CryptInstallOssGlobal)(DWORD,DWORD,DWORD);
353 static void test_CryptInstallOssGlobal(void)
356 I_CryptInstallOssGlobal pI_CryptInstallOssGlobal;
358 pI_CryptInstallOssGlobal= (I_CryptInstallOssGlobal)GetProcAddress(hCrypt,"I_CryptInstallOssGlobal");
359 /* passing in some random values to I_CryptInstallOssGlobal, it always returns 9 the first time, then 10, 11 etc.*/
362 ret = pI_CryptInstallOssGlobal(rand(),rand(),rand());
364 ret == 0, /* Vista */
365 "Expected %d or 0, got %d\n",(9+i),ret);
371 hCrypt = GetModuleHandleA("crypt32.dll");
373 test_findAttribute();
374 test_findExtension();
376 test_verifyTimeValidity();
377 test_cryptAllocate();
379 test_readTrustedPublisherDWORD();
380 test_getDefaultCryptProv();
381 test_CryptInstallOssGlobal();