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 %08lx\n",
48 SetLastError(0xdeadbeef);
49 ret = CertFindAttribute(NULL, 1, NULL);
51 /* returns NULL, last error is ERROR_INVALID_PARAMETER */
52 SetLastError(0xdeadbeef);
53 ret = CertFindAttribute(NULL, 1, &attr);
54 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
55 "Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
57 /* returns NULL, last error not set */
58 SetLastError(0xdeadbeef);
59 ret = CertFindAttribute("bogus", 1, &attr);
60 ok(ret == NULL, "Expected failure\n");
61 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
63 /* returns NULL, last error not set */
64 SetLastError(0xdeadbeef);
65 ret = CertFindAttribute("1.2.4", 1, &attr);
66 ok(ret == NULL, "Expected failure\n");
67 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
69 /* succeeds, last error not set */
70 SetLastError(0xdeadbeef);
71 ret = CertFindAttribute("1.2.3", 1, &attr);
72 ok(ret != NULL, "CertFindAttribute failed: %08lx\n", GetLastError());
75 static void test_findExtension(void)
78 static CHAR oid[] = "1.2.3";
79 BYTE blobbin[] = {0x02,0x01,0x01};
80 CERT_EXTENSION ext = { oid, TRUE, { sizeof blobbin, blobbin } };
82 /* returns NULL, last error not set */
83 SetLastError(0xdeadbeef);
84 ret = CertFindExtension(NULL, 0, NULL);
85 ok(ret == NULL, "Expected failure\n");
86 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
89 SetLastError(0xdeadbeef);
90 ret = CertFindExtension(NULL, 1, NULL);
92 /* returns NULL, last error is ERROR_INVALID_PARAMETER */
93 SetLastError(0xdeadbeef);
94 ret = CertFindExtension(NULL, 1, &ext);
95 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
96 "Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
98 /* returns NULL, last error not set */
99 SetLastError(0xdeadbeef);
100 ret = CertFindExtension("bogus", 1, &ext);
101 ok(ret == NULL, "Expected failure\n");
102 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
104 /* returns NULL, last error not set */
105 SetLastError(0xdeadbeef);
106 ret = CertFindExtension("1.2.4", 1, &ext);
107 ok(ret == NULL, "Expected failure\n");
108 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
110 /* succeeds, last error not set */
111 SetLastError(0xdeadbeef);
112 ret = CertFindExtension("1.2.3", 1, &ext);
113 ok(ret != NULL, "CertFindExtension failed: %08lx\n", GetLastError());
116 static void test_findRDNAttr(void)
119 static CHAR oid[] = "1.2.3";
120 BYTE bin[] = { 0x16,0x09,'J','u','a','n',' ','L','a','n','g' };
121 CERT_RDN_ATTR attrs[] = {
122 { oid, CERT_RDN_IA5_STRING, { sizeof bin, bin } },
125 { sizeof(attrs) / sizeof(attrs[0]), attrs },
127 CERT_NAME_INFO nameInfo = { sizeof(rdns) / sizeof(rdns[0]), rdns };
130 SetLastError(0xdeadbeef);
131 ret = CertFindRDNAttr(NULL, NULL);
133 /* returns NULL, last error is ERROR_INVALID_PARAMETER */
134 SetLastError(0xdeadbeef);
135 ret = CertFindRDNAttr(NULL, &nameInfo);
136 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
137 "Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
139 /* returns NULL, last error not set */
140 SetLastError(0xdeadbeef);
141 ret = CertFindRDNAttr("bogus", &nameInfo);
142 ok(ret == NULL, "Expected failure\n");
143 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
145 /* returns NULL, last error not set */
146 SetLastError(0xdeadbeef);
147 ret = CertFindRDNAttr("1.2.4", &nameInfo);
148 ok(ret == NULL, "Expected failure\n");
149 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
151 /* succeeds, last error not set */
152 SetLastError(0xdeadbeef);
153 ret = CertFindRDNAttr("1.2.3", &nameInfo);
154 ok(ret != NULL, "CertFindRDNAttr failed: %08lx\n", GetLastError());
157 static void test_verifyTimeValidity(void)
161 CERT_INFO info = { 0 };
164 GetSystemTime(&sysTime);
165 SystemTimeToFileTime(&sysTime, &fileTime);
167 ret = CertVerifyTimeValidity(NULL, NULL);
168 ret = CertVerifyTimeValidity(&fileTime, NULL);
170 /* Check with 0 NotBefore and NotAfter */
171 ret = CertVerifyTimeValidity(&fileTime, &info);
172 ok(ret == 1, "Expected 1, got %ld\n", ret);
173 memcpy(&info.NotAfter, &fileTime, sizeof(info.NotAfter));
174 /* Check with NotAfter equal to comparison time */
175 ret = CertVerifyTimeValidity(&fileTime, &info);
176 ok(ret == 0, "Expected 0, got %ld\n", ret);
177 /* Check with NotBefore after comparison time */
178 memcpy(&info.NotBefore, &fileTime, sizeof(info.NotBefore));
179 info.NotBefore.dwLowDateTime += 5000;
180 ret = CertVerifyTimeValidity(&fileTime, &info);
181 ok(ret == -1, "Expected -1, got %ld\n", ret);
184 static void test_cryptAllocate(void)
188 buf = CryptMemAlloc(0);
189 ok(buf != NULL, "CryptMemAlloc failed: %08lx\n", GetLastError());
191 buf = CryptMemRealloc(NULL, 0);
192 ok(!buf, "Expected NULL\n");
193 buf = CryptMemAlloc(0);
194 buf = CryptMemRealloc(buf, 1);
195 ok(buf != NULL, "CryptMemRealloc failed: %08lx\n", GetLastError());
199 typedef DWORD (WINAPI *I_CryptAllocTlsFunc)(void);
200 typedef LPVOID (WINAPI *I_CryptDetachTlsFunc)(DWORD dwTlsIndex);
201 typedef LPVOID (WINAPI *I_CryptGetTlsFunc)(DWORD dwTlsIndex);
202 typedef BOOL (WINAPI *I_CryptSetTlsFunc)(DWORD dwTlsIndex, LPVOID lpTlsValue);
203 typedef BOOL (WINAPI *I_CryptFreeTlsFunc)(DWORD dwTlsIndex, DWORD unknown);
205 static I_CryptAllocTlsFunc pI_CryptAllocTls;
206 static I_CryptDetachTlsFunc pI_CryptDetachTls;
207 static I_CryptGetTlsFunc pI_CryptGetTls;
208 static I_CryptSetTlsFunc pI_CryptSetTls;
209 static I_CryptFreeTlsFunc pI_CryptFreeTls;
211 static void test_cryptTls(void)
218 pI_CryptAllocTls = (I_CryptAllocTlsFunc)GetProcAddress(hCrypt,
220 pI_CryptDetachTls = (I_CryptDetachTlsFunc)GetProcAddress(hCrypt,
222 pI_CryptGetTls = (I_CryptGetTlsFunc)GetProcAddress(hCrypt,
224 pI_CryptSetTls = (I_CryptSetTlsFunc)GetProcAddress(hCrypt,
226 pI_CryptFreeTls = (I_CryptFreeTlsFunc)GetProcAddress(hCrypt,
229 /* One normal pass */
230 index = pI_CryptAllocTls();
231 ok(index, "I_CryptAllocTls failed: %08lx\n", GetLastError());
236 ptr = pI_CryptGetTls(index);
237 ok(!ptr, "Expected NULL\n");
238 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
239 ok(ret, "I_CryptSetTls failed: %08lx\n", GetLastError());
240 ptr = pI_CryptGetTls(index);
241 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
243 ret = pI_CryptFreeTls(index, 1);
245 ret = pI_CryptFreeTls(index, 0);
246 ok(ret, "I_CryptFreeTls failed: %08lx\n", GetLastError());
247 ret = pI_CryptFreeTls(index, 0);
248 /* Not sure if this fails because TlsFree should fail, so leave as
251 todo_wine ok(!ret && GetLastError() == E_INVALIDARG,
252 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
254 /* Similar pass, check I_CryptDetachTls */
255 index = pI_CryptAllocTls();
256 ok(index, "I_CryptAllocTls failed: %08lx\n", GetLastError());
261 ptr = pI_CryptGetTls(index);
262 ok(!ptr, "Expected NULL\n");
263 ret = pI_CryptSetTls(index, (LPVOID)0xdeadbeef);
264 ok(ret, "I_CryptSetTls failed: %08lx\n", GetLastError());
265 ptr = pI_CryptGetTls(index);
266 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
267 ptr = pI_CryptDetachTls(index);
268 ok(ptr == (LPVOID)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr);
269 ptr = pI_CryptGetTls(index);
270 ok(!ptr, "Expected NULL\n");
274 typedef BOOL (WINAPI *I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)
277 static void test_readTrustedPublisherDWORD(void)
279 I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc pReadDWORD;
284 (I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc)GetProcAddress(
285 hCrypt, "I_CryptReadTrustedPublisherDWORDValueFromRegistry");
288 static const WCHAR safer[] = {
289 'S','o','f','t','w','a','r','e','\\',
290 'P','o','l','i','c','i','e','s','\\',
291 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
292 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
293 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',
294 '\\','S','a','f','e','r',0 };
295 static const WCHAR authenticodeFlags[] = { 'A','u','t','h','e','n',
296 't','i','c','o','d','e','F','l','a','g','s',0 };
297 BOOL ret, exists = FALSE;
298 DWORD size, readFlags = 0, returnedFlags;
302 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, safer, &key);
303 if (rc == ERROR_SUCCESS)
305 size = sizeof(readFlags);
306 rc = RegQueryValueExW(key, authenticodeFlags, NULL, NULL,
307 (LPBYTE)&readFlags, &size);
308 if (rc == ERROR_SUCCESS)
311 returnedFlags = 0xdeadbeef;
312 ret = pReadDWORD(authenticodeFlags, &returnedFlags);
313 ok(ret == exists, "Unexpected return value\n");
314 ok(readFlags == returnedFlags,
315 "Expected flags %08lx, got %08lx\n", readFlags, returnedFlags);
319 typedef HCRYPTPROV (WINAPI *I_CryptGetDefaultCryptProvFunc)(DWORD w);
321 static void test_getDefaultCryptProv(void)
323 I_CryptGetDefaultCryptProvFunc pI_CryptGetDefaultCryptProv;
328 pI_CryptGetDefaultCryptProv = (I_CryptGetDefaultCryptProvFunc)
329 GetProcAddress(hCrypt, "I_CryptGetDefaultCryptProv");
330 if (!pI_CryptGetDefaultCryptProv) return;
332 prov = pI_CryptGetDefaultCryptProv(0xdeadbeef);
333 ok(prov == 0 && GetLastError() == E_INVALIDARG,
334 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
335 prov = pI_CryptGetDefaultCryptProv(PROV_RSA_FULL);
336 ok(prov == 0 && GetLastError() == E_INVALIDARG,
337 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
338 prov = pI_CryptGetDefaultCryptProv(1);
339 ok(prov == 0 && GetLastError() == E_INVALIDARG,
340 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
341 prov = pI_CryptGetDefaultCryptProv(0);
342 ok(prov != 0, "I_CryptGetDefaultCryptProv failed: %08lx\n", GetLastError());
343 CryptReleaseContext(prov, 0);
348 hCrypt = LoadLibraryA("crypt32.dll");
349 test_findAttribute();
350 test_findExtension();
352 test_verifyTimeValidity();
353 test_cryptAllocate();
355 test_readTrustedPublisherDWORD();
356 test_getDefaultCryptProv();