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"
31 static HMODULE hCrypt
;
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 CertFindAttribute(NULL
, 1, NULL
);
51 /* returns NULL, last error is ERROR_INVALID_PARAMETER
54 SetLastError(0xdeadbeef);
55 ret
= CertFindAttribute(NULL
, 1, &attr
);
56 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
57 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
60 /* returns NULL, last error not set */
61 SetLastError(0xdeadbeef);
62 ret
= CertFindAttribute("bogus", 1, &attr
);
63 ok(ret
== NULL
, "Expected failure\n");
64 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
66 /* returns NULL, last error not set */
67 SetLastError(0xdeadbeef);
68 ret
= CertFindAttribute("1.2.4", 1, &attr
);
69 ok(ret
== NULL
, "Expected failure\n");
70 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
72 /* succeeds, last error not set */
73 SetLastError(0xdeadbeef);
74 ret
= CertFindAttribute("1.2.3", 1, &attr
);
75 ok(ret
!= NULL
, "CertFindAttribute failed: %08x\n", GetLastError());
78 static void test_findExtension(void)
81 static CHAR oid
[] = "1.2.3";
82 BYTE blobbin
[] = {0x02,0x01,0x01};
83 CERT_EXTENSION ext
= { oid
, TRUE
, { sizeof blobbin
, blobbin
} };
85 /* returns NULL, last error not set */
86 SetLastError(0xdeadbeef);
87 ret
= CertFindExtension(NULL
, 0, NULL
);
88 ok(ret
== NULL
, "Expected failure\n");
89 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
94 SetLastError(0xdeadbeef);
95 CertFindExtension(NULL
, 1, NULL
);
96 /* returns NULL, last error is ERROR_INVALID_PARAMETER
99 SetLastError(0xdeadbeef);
100 ret
= CertFindExtension(NULL
, 1, &ext
);
101 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
102 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
105 /* returns NULL, last error not set */
106 SetLastError(0xdeadbeef);
107 ret
= CertFindExtension("bogus", 1, &ext
);
108 ok(ret
== NULL
, "Expected failure\n");
109 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
111 /* returns NULL, last error not set */
112 SetLastError(0xdeadbeef);
113 ret
= CertFindExtension("1.2.4", 1, &ext
);
114 ok(ret
== NULL
, "Expected failure\n");
115 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
117 /* succeeds, last error not set */
118 SetLastError(0xdeadbeef);
119 ret
= CertFindExtension("1.2.3", 1, &ext
);
120 ok(ret
!= NULL
, "CertFindExtension failed: %08x\n", GetLastError());
123 static void test_findRDNAttr(void)
126 static CHAR oid
[] = "1.2.3";
127 BYTE bin
[] = { 0x16,0x09,'J','u','a','n',' ','L','a','n','g' };
128 CERT_RDN_ATTR attrs
[] = {
129 { oid
, CERT_RDN_IA5_STRING
, { sizeof bin
, bin
} },
132 { sizeof(attrs
) / sizeof(attrs
[0]), attrs
},
134 CERT_NAME_INFO nameInfo
= { sizeof(rdns
) / sizeof(rdns
[0]), rdns
};
139 SetLastError(0xdeadbeef);
140 CertFindRDNAttr(NULL
, NULL
);
141 /* returns NULL, last error is ERROR_INVALID_PARAMETER
144 SetLastError(0xdeadbeef);
145 ret
= CertFindRDNAttr(NULL
, &nameInfo
);
146 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
147 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
150 /* returns NULL, last error not set */
151 SetLastError(0xdeadbeef);
152 ret
= CertFindRDNAttr("bogus", &nameInfo
);
153 ok(ret
== NULL
, "Expected failure\n");
154 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
156 /* returns NULL, last error not set */
157 SetLastError(0xdeadbeef);
158 ret
= CertFindRDNAttr("1.2.4", &nameInfo
);
159 ok(ret
== NULL
, "Expected failure\n");
160 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
162 /* succeeds, last error not set */
163 SetLastError(0xdeadbeef);
164 ret
= CertFindRDNAttr("1.2.3", &nameInfo
);
165 ok(ret
!= NULL
, "CertFindRDNAttr failed: %08x\n", GetLastError());
168 static void test_verifyTimeValidity(void)
172 CERT_INFO info
= { 0 };
175 GetSystemTime(&sysTime
);
176 SystemTimeToFileTime(&sysTime
, &fileTime
);
178 ret = CertVerifyTimeValidity(NULL, NULL);
179 ret = CertVerifyTimeValidity(&fileTime, NULL);
181 /* Check with 0 NotBefore and NotAfter */
182 ret
= CertVerifyTimeValidity(&fileTime
, &info
);
183 ok(ret
== 1, "Expected 1, got %d\n", ret
);
184 memcpy(&info
.NotAfter
, &fileTime
, sizeof(info
.NotAfter
));
185 /* Check with NotAfter equal to comparison time */
186 ret
= CertVerifyTimeValidity(&fileTime
, &info
);
187 ok(ret
== 0, "Expected 0, got %d\n", ret
);
188 /* Check with NotBefore after comparison time */
189 memcpy(&info
.NotBefore
, &fileTime
, sizeof(info
.NotBefore
));
190 info
.NotBefore
.dwLowDateTime
+= 5000;
191 ret
= CertVerifyTimeValidity(&fileTime
, &info
);
192 ok(ret
== -1, "Expected -1, got %d\n", ret
);
195 static void test_cryptAllocate(void)
199 buf
= CryptMemAlloc(0);
200 ok(buf
!= NULL
, "CryptMemAlloc failed: %08x\n", GetLastError());
202 /* CryptMemRealloc(NULL, 0) fails pre-Vista */
203 buf
= CryptMemAlloc(0);
204 buf
= CryptMemRealloc(buf
, 1);
205 ok(buf
!= NULL
, "CryptMemRealloc failed: %08x\n", GetLastError());
210 static void test_cryptTls(void)
212 DWORD (WINAPI
*pI_CryptAllocTls
)(void);
213 LPVOID (WINAPI
*pI_CryptDetachTls
)(DWORD dwTlsIndex
);
214 LPVOID (WINAPI
*pI_CryptGetTls
)(DWORD dwTlsIndex
);
215 BOOL (WINAPI
*pI_CryptSetTls
)(DWORD dwTlsIndex
, LPVOID lpTlsValue
);
216 BOOL (WINAPI
*pI_CryptFreeTls
)(DWORD dwTlsIndex
, DWORD unknown
);
220 pI_CryptAllocTls
= (void *)GetProcAddress(hCrypt
, "I_CryptAllocTls");
221 pI_CryptDetachTls
= (void *)GetProcAddress(hCrypt
, "I_CryptDetachTls");
222 pI_CryptGetTls
= (void *)GetProcAddress(hCrypt
, "I_CryptGetTls");
223 pI_CryptSetTls
= (void *)GetProcAddress(hCrypt
, "I_CryptSetTls");
224 pI_CryptFreeTls
= (void *)GetProcAddress(hCrypt
, "I_CryptFreeTls");
226 /* One normal pass */
227 index
= pI_CryptAllocTls();
228 ok(index
, "I_CryptAllocTls failed: %08x\n", GetLastError());
233 ptr
= pI_CryptGetTls(index
);
234 ok(!ptr
, "Expected NULL\n");
235 ret
= pI_CryptSetTls(index
, (LPVOID
)0xdeadbeef);
236 ok(ret
, "I_CryptSetTls failed: %08x\n", GetLastError());
237 ptr
= pI_CryptGetTls(index
);
238 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
240 ret = pI_CryptFreeTls(index, 1);
242 ret
= pI_CryptFreeTls(index
, 0);
243 ok(ret
, "I_CryptFreeTls failed: %08x\n", GetLastError());
244 ret
= pI_CryptFreeTls(index
, 0);
245 ok(!ret
, "I_CryptFreeTls succeeded\n");
246 ok(GetLastError() == E_INVALIDARG
,
247 "Expected E_INVALIDARG, got %08x\n", GetLastError());
249 /* Similar pass, check I_CryptDetachTls */
250 index
= pI_CryptAllocTls();
251 ok(index
, "I_CryptAllocTls failed: %08x\n", GetLastError());
256 ptr
= pI_CryptGetTls(index
);
257 ok(!ptr
, "Expected NULL\n");
258 ret
= pI_CryptSetTls(index
, (LPVOID
)0xdeadbeef);
259 ok(ret
, "I_CryptSetTls failed: %08x\n", GetLastError());
260 ptr
= pI_CryptGetTls(index
);
261 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
262 ptr
= pI_CryptDetachTls(index
);
263 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
264 ptr
= pI_CryptGetTls(index
);
265 ok(!ptr
, "Expected NULL\n");
269 static void test_readTrustedPublisherDWORD(void)
272 BOOL (WINAPI
*pReadDWORD
)(LPCWSTR
, DWORD
*);
274 pReadDWORD
= (void *)GetProcAddress(hCrypt
, "I_CryptReadTrustedPublisherDWORDValueFromRegistry");
277 static const WCHAR safer
[] = {
278 'S','o','f','t','w','a','r','e','\\',
279 'P','o','l','i','c','i','e','s','\\',
280 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
281 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
282 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',
283 '\\','S','a','f','e','r',0 };
284 static const WCHAR authenticodeFlags
[] = { 'A','u','t','h','e','n',
285 't','i','c','o','d','e','F','l','a','g','s',0 };
286 BOOL ret
, exists
= FALSE
;
287 DWORD size
, readFlags
= 0, returnedFlags
;
291 rc
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, safer
, &key
);
292 if (rc
== ERROR_SUCCESS
)
294 size
= sizeof(readFlags
);
295 rc
= RegQueryValueExW(key
, authenticodeFlags
, NULL
, NULL
,
296 (LPBYTE
)&readFlags
, &size
);
297 if (rc
== ERROR_SUCCESS
)
300 returnedFlags
= 0xdeadbeef;
301 ret
= pReadDWORD(authenticodeFlags
, &returnedFlags
);
302 ok(ret
== exists
, "Unexpected return value\n");
303 ok(readFlags
== returnedFlags
,
304 "Expected flags %08x, got %08x\n", readFlags
, returnedFlags
);
308 static void test_getDefaultCryptProv(void)
310 HCRYPTPROV (WINAPI
*pI_CryptGetDefaultCryptProv
)(DWORD w
);
313 pI_CryptGetDefaultCryptProv
= (void *)GetProcAddress(hCrypt
, "I_CryptGetDefaultCryptProv");
314 if (!pI_CryptGetDefaultCryptProv
) return;
316 prov
= pI_CryptGetDefaultCryptProv(0xdeadbeef);
317 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
318 "Expected E_INVALIDARG, got %08x\n", GetLastError());
319 prov
= pI_CryptGetDefaultCryptProv(PROV_RSA_FULL
);
320 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
321 "Expected E_INVALIDARG, got %08x\n", GetLastError());
322 prov
= pI_CryptGetDefaultCryptProv(1);
323 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
324 "Expected E_INVALIDARG, got %08x\n", GetLastError());
325 prov
= pI_CryptGetDefaultCryptProv(0);
326 ok(prov
!= 0, "I_CryptGetDefaultCryptProv failed: %08x\n", GetLastError());
327 CryptReleaseContext(prov
, 0);
330 static void test_CryptInstallOssGlobal(void)
332 int (WINAPI
*pI_CryptInstallOssGlobal
)(DWORD
,DWORD
,DWORD
);
335 pI_CryptInstallOssGlobal
= (void *)GetProcAddress(hCrypt
,"I_CryptInstallOssGlobal");
336 /* passing in some random values to I_CryptInstallOssGlobal, it always returns 9 the first time, then 10, 11 etc.*/
339 ret
= pI_CryptInstallOssGlobal(rand(),rand(),rand());
341 ret
== 0, /* Vista */
342 "Expected %d or 0, got %d\n",(9+i
),ret
);
346 static const BYTE encodedInt
[] = { 0x02,0x01,0x01 };
347 static const WCHAR encodedIntStr
[] = { '0','2',' ','0','1',' ','0','1',0 };
348 static const BYTE encodedBigInt
[] = { 0x02,0x1f,0x01,0x02,0x03,0x04,0x05,0x06,
349 0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,
350 0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f };
351 static const WCHAR encodedBigIntStr
[] = { '0','2',' ','1','f',' ','0','1',' ',
352 '0','2',' ','0','3',' ','0','4',' ','0','5',' ','0','6',' ','0','7',' ','0',
353 '8',' ','0','9',' ','0','a',' ','0','b',' ','0','c',' ','0','d',' ','0','e',
354 ' ','0','f',' ','1','0',' ','1','1',' ','1','2',' ','1','3',' ','1','4',' ',
355 '1','5',' ','1','6',' ','1','7',' ','1','8',' ','1','9',' ','1','a',' ','1',
356 'b',' ','1','c',' ','1','d',' ','1','e',' ','1','f',0 };
358 static void test_format_object(void)
360 BOOL (WINAPI
*pCryptFormatObject
)(DWORD dwEncoding
, DWORD dwFormatType
,
361 DWORD dwFormatStrType
, void *pFormatStruct
, LPCSTR lpszStructType
,
362 const BYTE
*pbEncoded
, DWORD dwEncoded
, void *pbFormat
,
368 pCryptFormatObject
= (void *)GetProcAddress(hCrypt
, "CryptFormatObject");
369 if (!pCryptFormatObject
)
371 skip("No CryptFormatObject\n");
377 pCryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
379 /* When called with any but the default encoding, it fails to find a
380 * formatting function.
382 SetLastError(0xdeadbeef);
383 ret
= pCryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, &size
);
384 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
385 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
386 /* When called with the default encoding type for any undefined struct type
387 * (including none), it succeeds: the default encoding is a hex string
390 SetLastError(0xdeadbeef);
391 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
393 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
396 if (size
== 0 && GetLastError() == ERROR_FILE_NOT_FOUND
)
398 win_skip("CryptFormatObject has no default implementation\n");
401 ok(size
== sizeof(WCHAR
), "unexpected size %d\n", size
);
402 str
= HeapAlloc(GetProcessHeap(), 0, size
);
403 SetLastError(0xdeadbeef);
405 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
407 ok(!ret
&& GetLastError() == ERROR_MORE_DATA
,
408 "expected ERROR_MORE_DATA, got %d\n", GetLastError());
409 size
= sizeof(WCHAR
);
410 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
412 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
413 ok(!str
[0], "expected empty string\n");
414 HeapFree(GetProcessHeap(), 0, str
);
416 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, encodedInt
,
417 sizeof(encodedInt
), NULL
, &size
);
418 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
421 str
= HeapAlloc(GetProcessHeap(), 0, size
);
422 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
423 encodedInt
, sizeof(encodedInt
), str
, &size
);
424 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
425 ok(!lstrcmpW(str
, encodedIntStr
), "unexpected format string\n");
426 HeapFree(GetProcessHeap(), 0, str
);
428 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
429 encodedBigInt
, sizeof(encodedBigInt
), NULL
, &size
);
430 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
433 str
= HeapAlloc(GetProcessHeap(), 0, size
);
434 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
435 encodedBigInt
, sizeof(encodedBigInt
), str
, &size
);
436 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
437 ok(!lstrcmpiW(str
, encodedBigIntStr
), "unexpected format string\n");
438 HeapFree(GetProcessHeap(), 0, str
);
440 /* When called with the default encoding type for any undefined struct
441 * type but CRYPT_FORMAT_STR_NO_HEX specified, it fails to find a
442 * formatting function.
444 SetLastError(0xdeadbeef);
445 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, CRYPT_FORMAT_STR_NO_HEX
,
446 NULL
, NULL
, NULL
, 0, NULL
, &size
);
447 ok(!ret
, "CryptFormatObject succeeded\n");
448 ok(GetLastError() == ERROR_FILE_NOT_FOUND
||
449 GetLastError() == 0xdeadbeef, /* Vista, W2K8 */
450 "expected ERROR_FILE_NOT_FOUND or no change, got %d\n", GetLastError());
451 /* When called to format an AUTHORITY_KEY_ID2_INFO, it fails when no
454 SetLastError(0xdeadbeef);
455 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
,
456 szOID_AUTHORITY_KEY_IDENTIFIER2
, NULL
, 0, NULL
, &size
);
457 ok(!ret
&& GetLastError() == E_INVALIDARG
,
458 "expected E_INVALIDARG, got %d\n", GetLastError());
463 hCrypt
= GetModuleHandleA("crypt32.dll");
465 test_findAttribute();
466 test_findExtension();
468 test_verifyTimeValidity();
469 test_cryptAllocate();
471 test_readTrustedPublisherDWORD();
472 test_getDefaultCryptProv();
473 test_CryptInstallOssGlobal();
474 test_format_object();