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
);
369 static BOOL (WINAPI
*pCryptFormatObject
)(DWORD dwEncoding
, DWORD dwFormatType
,
370 DWORD dwFormatStrType
, void *pFormatStruct
, LPCSTR lpszStructType
,
371 const BYTE
*pbEncoded
, DWORD dwEncoded
, void *pbFormat
, DWORD
*pcbFormat
);
373 static const BYTE encodedInt
[] = { 0x02,0x01,0x01 };
374 static const WCHAR encodedIntStr
[] = { '0','2',' ','0','1',' ','0','1',0 };
375 static const BYTE encodedBigInt
[] = { 0x02,0x1f,0x01,0x02,0x03,0x04,0x05,0x06,
376 0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,
377 0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f };
378 static const WCHAR encodedBigIntStr
[] = { '0','2',' ','1','f',' ','0','1',' ',
379 '0','2',' ','0','3',' ','0','4',' ','0','5',' ','0','6',' ','0','7',' ','0',
380 '8',' ','0','9',' ','0','a',' ','0','b',' ','0','c',' ','0','d',' ','0','e',
381 ' ','0','f',' ','1','0',' ','1','1',' ','1','2',' ','1','3',' ','1','4',' ',
382 '1','5',' ','1','6',' ','1','7',' ','1','8',' ','1','9',' ','1','a',' ','1',
383 'b',' ','1','c',' ','1','d',' ','1','e',' ','1','f',0 };
385 static void test_format_object(void)
391 pCryptFormatObject
= (void *)GetProcAddress(hCrypt
, "CryptFormatObject");
392 if (!pCryptFormatObject
)
394 skip("No CryptFormatObject\n");
400 ret
= pCryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
402 /* When called with any but the default encoding, it fails to find a
403 * formatting function.
405 SetLastError(0xdeadbeef);
406 ret
= pCryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, &size
);
407 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
408 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
409 /* When called with the default encoding type for any undefined struct type
410 * (including none), it succeeds: the default encoding is a hex string
413 SetLastError(0xdeadbeef);
414 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
416 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
419 if (size
== 0 && GetLastError() == ERROR_FILE_NOT_FOUND
)
421 win_skip("CryptFormatObject has no default implementation\n");
424 ok(size
== sizeof(WCHAR
), "unexpected size %d\n", size
);
425 str
= HeapAlloc(GetProcessHeap(), 0, size
);
426 SetLastError(0xdeadbeef);
428 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
430 ok(!ret
&& GetLastError() == ERROR_MORE_DATA
,
431 "expected ERROR_MORE_DATA, got %d\n", GetLastError());
432 size
= sizeof(WCHAR
);
433 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0,
435 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
436 ok(!str
[0], "expected empty string\n");
437 HeapFree(GetProcessHeap(), 0, str
);
439 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, encodedInt
,
440 sizeof(encodedInt
), NULL
, &size
);
441 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
444 str
= HeapAlloc(GetProcessHeap(), 0, size
);
445 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
446 encodedInt
, sizeof(encodedInt
), str
, &size
);
447 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
448 ok(!lstrcmpW(str
, encodedIntStr
), "unexpected format string\n");
449 HeapFree(GetProcessHeap(), 0, str
);
451 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
452 encodedBigInt
, sizeof(encodedBigInt
), NULL
, &size
);
453 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
456 str
= HeapAlloc(GetProcessHeap(), 0, size
);
457 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
458 encodedBigInt
, sizeof(encodedBigInt
), str
, &size
);
459 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
460 ok(!lstrcmpiW(str
, encodedBigIntStr
), "unexpected format string\n");
461 HeapFree(GetProcessHeap(), 0, str
);
463 /* When called with the default encoding type for any undefined struct
464 * type but CRYPT_FORMAT_STR_NO_HEX specified, it fails to find a
465 * formatting function.
467 SetLastError(0xdeadbeef);
468 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, CRYPT_FORMAT_STR_NO_HEX
,
469 NULL
, NULL
, NULL
, 0, NULL
, &size
);
470 ok(!ret
, "CryptFormatObject succeeded\n");
471 ok(GetLastError() == ERROR_FILE_NOT_FOUND
||
472 GetLastError() == 0xdeadbeef, /* Vista, W2K8 */
473 "expected ERROR_FILE_NOT_FOUND or no change, got %d\n", GetLastError());
474 /* When called to format an AUTHORITY_KEY_ID2_INFO, it fails when no
477 SetLastError(0xdeadbeef);
478 ret
= pCryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
,
479 szOID_AUTHORITY_KEY_IDENTIFIER2
, NULL
, 0, NULL
, &size
);
480 ok(!ret
&& GetLastError() == E_INVALIDARG
,
481 "expected E_INVALIDARG, got %d\n", GetLastError());
486 hCrypt
= GetModuleHandleA("crypt32.dll");
488 test_findAttribute();
489 test_findExtension();
491 test_verifyTimeValidity();
492 test_cryptAllocate();
494 test_readTrustedPublisherDWORD();
495 test_getDefaultCryptProv();
496 test_CryptInstallOssGlobal();
497 test_format_object();