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());
209 typedef DWORD (WINAPI
*I_CryptAllocTlsFunc
)(void);
210 typedef LPVOID (WINAPI
*I_CryptDetachTlsFunc
)(DWORD dwTlsIndex
);
211 typedef LPVOID (WINAPI
*I_CryptGetTlsFunc
)(DWORD dwTlsIndex
);
212 typedef BOOL (WINAPI
*I_CryptSetTlsFunc
)(DWORD dwTlsIndex
, LPVOID lpTlsValue
);
213 typedef BOOL (WINAPI
*I_CryptFreeTlsFunc
)(DWORD dwTlsIndex
, DWORD unknown
);
215 static I_CryptAllocTlsFunc pI_CryptAllocTls
;
216 static I_CryptDetachTlsFunc pI_CryptDetachTls
;
217 static I_CryptGetTlsFunc pI_CryptGetTls
;
218 static I_CryptSetTlsFunc pI_CryptSetTls
;
219 static I_CryptFreeTlsFunc pI_CryptFreeTls
;
221 static void test_cryptTls(void)
226 pI_CryptAllocTls
= (I_CryptAllocTlsFunc
)GetProcAddress(hCrypt
,
228 pI_CryptDetachTls
= (I_CryptDetachTlsFunc
)GetProcAddress(hCrypt
,
230 pI_CryptGetTls
= (I_CryptGetTlsFunc
)GetProcAddress(hCrypt
,
232 pI_CryptSetTls
= (I_CryptSetTlsFunc
)GetProcAddress(hCrypt
,
234 pI_CryptFreeTls
= (I_CryptFreeTlsFunc
)GetProcAddress(hCrypt
,
237 /* One normal pass */
238 index
= pI_CryptAllocTls();
239 ok(index
, "I_CryptAllocTls failed: %08x\n", GetLastError());
244 ptr
= pI_CryptGetTls(index
);
245 ok(!ptr
, "Expected NULL\n");
246 ret
= pI_CryptSetTls(index
, (LPVOID
)0xdeadbeef);
247 ok(ret
, "I_CryptSetTls failed: %08x\n", GetLastError());
248 ptr
= pI_CryptGetTls(index
);
249 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
251 ret = pI_CryptFreeTls(index, 1);
253 ret
= pI_CryptFreeTls(index
, 0);
254 ok(ret
, "I_CryptFreeTls failed: %08x\n", GetLastError());
255 ret
= pI_CryptFreeTls(index
, 0);
256 ok(!ret
, "I_CryptFreeTls succeeded\n");
257 ok(GetLastError() == E_INVALIDARG
,
258 "Expected E_INVALIDARG, got %08x\n", GetLastError());
260 /* Similar pass, check I_CryptDetachTls */
261 index
= pI_CryptAllocTls();
262 ok(index
, "I_CryptAllocTls failed: %08x\n", GetLastError());
267 ptr
= pI_CryptGetTls(index
);
268 ok(!ptr
, "Expected NULL\n");
269 ret
= pI_CryptSetTls(index
, (LPVOID
)0xdeadbeef);
270 ok(ret
, "I_CryptSetTls failed: %08x\n", GetLastError());
271 ptr
= pI_CryptGetTls(index
);
272 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
273 ptr
= pI_CryptDetachTls(index
);
274 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
275 ptr
= pI_CryptGetTls(index
);
276 ok(!ptr
, "Expected NULL\n");
280 typedef BOOL (WINAPI
*I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc
)
283 static void test_readTrustedPublisherDWORD(void)
285 I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc pReadDWORD
;
288 (I_CryptReadTrustedPublisherDWORDValueFromRegistryFunc
)GetProcAddress(
289 hCrypt
, "I_CryptReadTrustedPublisherDWORDValueFromRegistry");
292 static const WCHAR safer
[] = {
293 'S','o','f','t','w','a','r','e','\\',
294 'P','o','l','i','c','i','e','s','\\',
295 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
296 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
297 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',
298 '\\','S','a','f','e','r',0 };
299 static const WCHAR authenticodeFlags
[] = { 'A','u','t','h','e','n',
300 't','i','c','o','d','e','F','l','a','g','s',0 };
301 BOOL ret
, exists
= FALSE
;
302 DWORD size
, readFlags
= 0, returnedFlags
;
306 rc
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, safer
, &key
);
307 if (rc
== ERROR_SUCCESS
)
309 size
= sizeof(readFlags
);
310 rc
= RegQueryValueExW(key
, authenticodeFlags
, NULL
, NULL
,
311 (LPBYTE
)&readFlags
, &size
);
312 if (rc
== ERROR_SUCCESS
)
315 returnedFlags
= 0xdeadbeef;
316 ret
= pReadDWORD(authenticodeFlags
, &returnedFlags
);
317 ok(ret
== exists
, "Unexpected return value\n");
318 ok(readFlags
== returnedFlags
,
319 "Expected flags %08x, got %08x\n", readFlags
, returnedFlags
);
323 typedef HCRYPTPROV (WINAPI
*I_CryptGetDefaultCryptProvFunc
)(DWORD w
);
325 static void test_getDefaultCryptProv(void)
327 I_CryptGetDefaultCryptProvFunc pI_CryptGetDefaultCryptProv
;
330 pI_CryptGetDefaultCryptProv
= (I_CryptGetDefaultCryptProvFunc
)
331 GetProcAddress(hCrypt
, "I_CryptGetDefaultCryptProv");
332 if (!pI_CryptGetDefaultCryptProv
) return;
334 prov
= pI_CryptGetDefaultCryptProv(0xdeadbeef);
335 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
336 "Expected E_INVALIDARG, got %08x\n", GetLastError());
337 prov
= pI_CryptGetDefaultCryptProv(PROV_RSA_FULL
);
338 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
339 "Expected E_INVALIDARG, got %08x\n", GetLastError());
340 prov
= pI_CryptGetDefaultCryptProv(1);
341 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
342 "Expected E_INVALIDARG, got %08x\n", GetLastError());
343 prov
= pI_CryptGetDefaultCryptProv(0);
344 ok(prov
!= 0, "I_CryptGetDefaultCryptProv failed: %08x\n", GetLastError());
345 CryptReleaseContext(prov
, 0);
348 typedef int (WINAPI
*I_CryptInstallOssGlobal
)(DWORD
,DWORD
,DWORD
);
350 static void test_CryptInstallOssGlobal(void)
353 I_CryptInstallOssGlobal pI_CryptInstallOssGlobal
;
355 pI_CryptInstallOssGlobal
= (I_CryptInstallOssGlobal
)GetProcAddress(hCrypt
,"I_CryptInstallOssGlobal");
356 /* passing in some random values to I_CryptInstallOssGlobal, it always returns 9 the first time, then 10, 11 etc.*/
359 ret
= pI_CryptInstallOssGlobal(rand(),rand(),rand());
361 ret
== 0, /* Vista */
362 "Expected %d or 0, got %d\n",(9+i
),ret
);
366 static BOOL (WINAPI
*pCryptFormatObject
)(DWORD dwEncoding
, DWORD dwFormatType
,
367 DWORD dwFormatStrType
, void *pFormatStruct
, LPCSTR lpszStructType
,
368 const BYTE
*pbEncoded
, DWORD dwEncoded
, void *pbFormat
, DWORD
*pcbFormat
);
370 static const BYTE encodedInt
[] = { 0x02,0x01,0x01 };
371 static const WCHAR encodedIntStr
[] = { '0','2',' ','0','1',' ','0','1',0 };
372 static const BYTE encodedBigInt
[] = { 0x02,0x1f,0x01,0x02,0x03,0x04,0x05,0x06,
373 0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,
374 0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f };
375 static const WCHAR encodedBigIntStr
[] = { '0','2',' ','1','f',' ','0','1',' ',
376 '0','2',' ','0','3',' ','0','4',' ','0','5',' ','0','6',' ','0','7',' ','0',
377 '8',' ','0','9',' ','0','a',' ','0','b',' ','0','c',' ','0','d',' ','0','e',
378 ' ','0','f',' ','1','0',' ','1','1',' ','1','2',' ','1','3',' ','1','4',' ',
379 '1','5',' ','1','6',' ','1','7',' ','1','8',' ','1','9',' ','1','a',' ','1',
380 'b',' ','1','c',' ','1','d',' ','1','e',' ','1','f',0 };
382 static void test_format_object(void)
388 pCryptFormatObject
= (void *)GetProcAddress(hCrypt
, "CryptFormatObject");
389 if (!pCryptFormatObject
)
391 skip("No CryptFormatObject\n");
397 pCryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
399 /* When called with any but the default encoding, it fails to find a
400 * formatting function.
402 SetLastError(0xdeadbeef);
403 ret
= pCryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, &size
);
404 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
405 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
406 /* When called with the default encoding type for any undefined struct type
407 * (including none), it succeeds: the default encoding is a hex string
410 SetLastError(0xdeadbeef);
411 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
413 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
416 if (size
== 0 && GetLastError() == ERROR_FILE_NOT_FOUND
)
418 win_skip("CryptFormatObject has no default implementation\n");
421 ok(size
== sizeof(WCHAR
), "unexpected size %d\n", size
);
422 str
= HeapAlloc(GetProcessHeap(), 0, size
);
423 SetLastError(0xdeadbeef);
425 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
427 ok(!ret
&& GetLastError() == ERROR_MORE_DATA
,
428 "expected ERROR_MORE_DATA, got %d\n", GetLastError());
429 size
= sizeof(WCHAR
);
430 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
432 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
433 ok(!str
[0], "expected empty string\n");
434 HeapFree(GetProcessHeap(), 0, str
);
436 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, encodedInt
,
437 sizeof(encodedInt
), NULL
, &size
);
438 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
441 str
= HeapAlloc(GetProcessHeap(), 0, size
);
442 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
443 encodedInt
, sizeof(encodedInt
), str
, &size
);
444 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
445 ok(!lstrcmpW(str
, encodedIntStr
), "unexpected format string\n");
446 HeapFree(GetProcessHeap(), 0, str
);
448 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
449 encodedBigInt
, sizeof(encodedBigInt
), NULL
, &size
);
450 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
453 str
= HeapAlloc(GetProcessHeap(), 0, size
);
454 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
455 encodedBigInt
, sizeof(encodedBigInt
), str
, &size
);
456 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
457 ok(!lstrcmpiW(str
, encodedBigIntStr
), "unexpected format string\n");
458 HeapFree(GetProcessHeap(), 0, str
);
460 /* When called with the default encoding type for any undefined struct
461 * type but CRYPT_FORMAT_STR_NO_HEX specified, it fails to find a
462 * formatting function.
464 SetLastError(0xdeadbeef);
465 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, CRYPT_FORMAT_STR_NO_HEX
,
466 NULL
, NULL
, NULL
, 0, NULL
, &size
);
467 ok(!ret
, "CryptFormatObject succeeded\n");
468 ok(GetLastError() == ERROR_FILE_NOT_FOUND
||
469 GetLastError() == 0xdeadbeef, /* Vista, W2K8 */
470 "expected ERROR_FILE_NOT_FOUND or no change, got %d\n", GetLastError());
471 /* When called to format an AUTHORITY_KEY_ID2_INFO, it fails when no
474 SetLastError(0xdeadbeef);
475 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
,
476 szOID_AUTHORITY_KEY_IDENTIFIER2
, NULL
, 0, NULL
, &size
);
477 ok(!ret
&& GetLastError() == E_INVALIDARG
,
478 "expected E_INVALIDARG, got %d\n", GetLastError());
483 hCrypt
= GetModuleHandleA("crypt32.dll");
485 test_findAttribute();
486 test_findExtension();
488 test_verifyTimeValidity();
489 test_cryptAllocate();
491 test_readTrustedPublisherDWORD();
492 test_getDefaultCryptProv();
493 test_CryptInstallOssGlobal();
494 test_format_object();