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
, ARRAY_SIZE(blobs
), 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
} },
131 CERT_RDN rdns
[] = { { ARRAY_SIZE(attrs
), attrs
} };
132 CERT_NAME_INFO nameInfo
= { ARRAY_SIZE(rdns
), rdns
};
137 SetLastError(0xdeadbeef);
138 CertFindRDNAttr(NULL
, NULL
);
139 /* returns NULL, last error is ERROR_INVALID_PARAMETER
142 SetLastError(0xdeadbeef);
143 ret
= CertFindRDNAttr(NULL
, &nameInfo
);
144 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
145 "Expected ERROR_INVALID_PARAMETER, got %d (%08x)\n", GetLastError(),
148 /* returns NULL, last error not set */
149 SetLastError(0xdeadbeef);
150 ret
= CertFindRDNAttr("bogus", &nameInfo
);
151 ok(ret
== NULL
, "Expected failure\n");
152 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
154 /* returns NULL, last error not set */
155 SetLastError(0xdeadbeef);
156 ret
= CertFindRDNAttr("1.2.4", &nameInfo
);
157 ok(ret
== NULL
, "Expected failure\n");
158 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08x\n",
160 /* succeeds, last error not set */
161 SetLastError(0xdeadbeef);
162 ret
= CertFindRDNAttr("1.2.3", &nameInfo
);
163 ok(ret
!= NULL
, "CertFindRDNAttr failed: %08x\n", GetLastError());
166 static void test_verifyTimeValidity(void)
170 CERT_INFO info
= { 0 };
173 GetSystemTime(&sysTime
);
174 SystemTimeToFileTime(&sysTime
, &fileTime
);
176 ret = CertVerifyTimeValidity(NULL, NULL);
177 ret = CertVerifyTimeValidity(&fileTime, NULL);
179 /* Check with 0 NotBefore and NotAfter */
180 ret
= CertVerifyTimeValidity(&fileTime
, &info
);
181 ok(ret
== 1, "Expected 1, got %d\n", ret
);
182 info
.NotAfter
= fileTime
;
183 /* Check with NotAfter equal to comparison time */
184 ret
= CertVerifyTimeValidity(&fileTime
, &info
);
185 ok(ret
== 0, "Expected 0, got %d\n", ret
);
186 /* Check with NotBefore after comparison time */
187 info
.NotBefore
= fileTime
;
188 info
.NotBefore
.dwLowDateTime
+= 5000;
189 ret
= CertVerifyTimeValidity(&fileTime
, &info
);
190 ok(ret
== -1, "Expected -1, got %d\n", ret
);
193 static void test_cryptAllocate(void)
197 buf
= CryptMemAlloc(0);
198 ok(buf
!= NULL
, "CryptMemAlloc failed: %08x\n", GetLastError());
200 /* CryptMemRealloc(NULL, 0) fails pre-Vista */
201 buf
= CryptMemAlloc(0);
202 buf
= CryptMemRealloc(buf
, 1);
203 ok(buf
!= NULL
, "CryptMemRealloc failed: %08x\n", GetLastError());
208 static void test_cryptTls(void)
210 DWORD (WINAPI
*pI_CryptAllocTls
)(void);
211 LPVOID (WINAPI
*pI_CryptDetachTls
)(DWORD dwTlsIndex
);
212 LPVOID (WINAPI
*pI_CryptGetTls
)(DWORD dwTlsIndex
);
213 BOOL (WINAPI
*pI_CryptSetTls
)(DWORD dwTlsIndex
, LPVOID lpTlsValue
);
214 BOOL (WINAPI
*pI_CryptFreeTls
)(DWORD dwTlsIndex
, DWORD unknown
);
218 pI_CryptAllocTls
= (void *)GetProcAddress(hCrypt
, "I_CryptAllocTls");
219 pI_CryptDetachTls
= (void *)GetProcAddress(hCrypt
, "I_CryptDetachTls");
220 pI_CryptGetTls
= (void *)GetProcAddress(hCrypt
, "I_CryptGetTls");
221 pI_CryptSetTls
= (void *)GetProcAddress(hCrypt
, "I_CryptSetTls");
222 pI_CryptFreeTls
= (void *)GetProcAddress(hCrypt
, "I_CryptFreeTls");
224 /* One normal pass */
225 index
= pI_CryptAllocTls();
226 ok(index
, "I_CryptAllocTls failed: %08x\n", GetLastError());
231 ptr
= pI_CryptGetTls(index
);
232 ok(!ptr
, "Expected NULL\n");
233 ret
= pI_CryptSetTls(index
, (LPVOID
)0xdeadbeef);
234 ok(ret
, "I_CryptSetTls failed: %08x\n", GetLastError());
235 ptr
= pI_CryptGetTls(index
);
236 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
238 ret = pI_CryptFreeTls(index, 1);
240 ret
= pI_CryptFreeTls(index
, 0);
241 ok(ret
, "I_CryptFreeTls failed: %08x\n", GetLastError());
242 ret
= pI_CryptFreeTls(index
, 0);
243 ok(!ret
, "I_CryptFreeTls succeeded\n");
244 ok(GetLastError() == E_INVALIDARG
,
245 "Expected E_INVALIDARG, got %08x\n", GetLastError());
247 /* Similar pass, check I_CryptDetachTls */
248 index
= pI_CryptAllocTls();
249 ok(index
, "I_CryptAllocTls failed: %08x\n", GetLastError());
254 ptr
= pI_CryptGetTls(index
);
255 ok(!ptr
, "Expected NULL\n");
256 ret
= pI_CryptSetTls(index
, (LPVOID
)0xdeadbeef);
257 ok(ret
, "I_CryptSetTls failed: %08x\n", GetLastError());
258 ptr
= pI_CryptGetTls(index
);
259 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
260 ptr
= pI_CryptDetachTls(index
);
261 ok(ptr
== (LPVOID
)0xdeadbeef, "Expected 0xdeadbeef, got %p\n", ptr
);
262 ptr
= pI_CryptGetTls(index
);
263 ok(!ptr
, "Expected NULL\n");
267 static void test_readTrustedPublisherDWORD(void)
270 BOOL (WINAPI
*pReadDWORD
)(LPCWSTR
, DWORD
*);
272 pReadDWORD
= (void *)GetProcAddress(hCrypt
, "I_CryptReadTrustedPublisherDWORDValueFromRegistry");
275 static const WCHAR safer
[] = {
276 'S','o','f','t','w','a','r','e','\\',
277 'P','o','l','i','c','i','e','s','\\',
278 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
279 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
280 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',
281 '\\','S','a','f','e','r',0 };
282 static const WCHAR authenticodeFlags
[] = { 'A','u','t','h','e','n',
283 't','i','c','o','d','e','F','l','a','g','s',0 };
284 BOOL ret
, exists
= FALSE
;
285 DWORD size
, readFlags
= 0, returnedFlags
;
289 rc
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, safer
, &key
);
290 if (rc
== ERROR_SUCCESS
)
292 size
= sizeof(readFlags
);
293 rc
= RegQueryValueExW(key
, authenticodeFlags
, NULL
, NULL
,
294 (LPBYTE
)&readFlags
, &size
);
295 if (rc
== ERROR_SUCCESS
)
298 returnedFlags
= 0xdeadbeef;
299 ret
= pReadDWORD(authenticodeFlags
, &returnedFlags
);
300 ok(ret
== exists
, "Unexpected return value\n");
301 ok(readFlags
== returnedFlags
,
302 "Expected flags %08x, got %08x\n", readFlags
, returnedFlags
);
306 static void test_getDefaultCryptProv(void)
308 #define ALG(id) id, #id
316 { ALG(CALG_MD2
), TRUE
},
317 { ALG(CALG_MD4
), TRUE
},
318 { ALG(CALG_MD5
), TRUE
},
319 { ALG(CALG_SHA
), TRUE
},
320 { ALG(CALG_RSA_SIGN
) },
321 { ALG(CALG_DSS_SIGN
) },
322 { ALG(CALG_NO_SIGN
) },
323 { ALG(CALG_ECDSA
), TRUE
},
324 { ALG(CALG_ECDH
), TRUE
},
325 { ALG(CALG_RSA_KEYX
) },
326 { ALG(CALG_RSA_KEYX
) },
329 HCRYPTPROV (WINAPI
*pI_CryptGetDefaultCryptProv
)(DWORD w
);
335 pI_CryptGetDefaultCryptProv
= (void *)GetProcAddress(hCrypt
, "I_CryptGetDefaultCryptProv");
336 if (!pI_CryptGetDefaultCryptProv
) return;
338 prov
= pI_CryptGetDefaultCryptProv(0xdeadbeef);
339 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
340 "Expected E_INVALIDARG, got %08x\n", GetLastError());
341 prov
= pI_CryptGetDefaultCryptProv(PROV_RSA_FULL
);
342 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
343 "Expected E_INVALIDARG, got %08x\n", GetLastError());
344 prov
= pI_CryptGetDefaultCryptProv(1);
345 ok(prov
== 0 && GetLastError() == E_INVALIDARG
,
346 "Expected E_INVALIDARG, got %08x\n", GetLastError());
347 prov
= pI_CryptGetDefaultCryptProv(0);
348 ok(prov
!= 0, "I_CryptGetDefaultCryptProv failed: %08x\n", GetLastError());
349 CryptReleaseContext(prov
, 0);
351 for (i
= 0; i
< ARRAY_SIZE(test_prov
); i
++)
353 if (winetest_debug
> 1)
354 trace("%u: algid %#x (%s): class %u, type %u, sid %u\n", i
, test_prov
[i
].algid
, test_prov
[i
].name
,
355 GET_ALG_CLASS(test_prov
[i
].algid
) >> 13, GET_ALG_TYPE(test_prov
[i
].algid
) >> 9, GET_ALG_SID(test_prov
[i
].algid
));
357 prov
= pI_CryptGetDefaultCryptProv(test_prov
[i
].algid
);
360 todo_wine_if(test_prov
[i
].algid
== CALG_DSS_SIGN
|| test_prov
[i
].algid
== CALG_NO_SIGN
)
361 ok(test_prov
[i
].optional
, "%u: I_CryptGetDefaultCryptProv(%#x) failed\n", i
, test_prov
[i
].algid
);
365 ret
= CryptGetProvParam(prov
, PP_NAME
, NULL
, &size
, 0);
366 if (ret
) /* some provders don't support PP_NAME */
368 name
= CryptMemAlloc(size
);
369 ret
= CryptGetProvParam(prov
, PP_NAME
, (BYTE
*)name
, &size
, 0);
370 ok(ret
, "%u: CryptGetProvParam failed %#x\n", i
, GetLastError());
371 if (winetest_debug
> 1)
372 trace("%u: algid %#x, name %s\n", i
, test_prov
[i
].algid
, name
);
376 CryptReleaseContext(prov
, 0);
380 static void test_CryptInstallOssGlobal(void)
382 int (WINAPI
*pI_CryptInstallOssGlobal
)(DWORD
,DWORD
,DWORD
);
385 pI_CryptInstallOssGlobal
= (void *)GetProcAddress(hCrypt
,"I_CryptInstallOssGlobal");
386 /* passing in some random values to I_CryptInstallOssGlobal, it always returns 9 the first time, then 10, 11 etc.*/
389 ret
= pI_CryptInstallOssGlobal(rand(),rand(),rand());
391 ret
== 0, /* Vista */
392 "Expected %d or 0, got %d\n",(9+i
),ret
);
396 static void test_format_object(void)
398 static const BYTE encodedInt
[] = {0x02,0x01,0x01};
399 static const BYTE encodedBigInt
[] = {0x02,0x1f,0x01,0x02,0x03,0x04,0x05,
400 0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,
401 0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f};
402 static const WCHAR encodedBigIntStr
[] = L
"02 1f 01 02 03 04 05 06 07 08 09 "
403 "0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f";
412 CryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, NULL
);
414 /* When called with any but the default encoding, it fails to find a
415 * formatting function.
417 SetLastError(0xdeadbeef);
418 ret
= CryptFormatObject(0, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, &size
);
419 ok(!ret
, "expected failure\n");
420 ok(GetLastError() == ERROR_FILE_NOT_FOUND
, "wrong error %u\n", GetLastError());
422 /* When called with the default encoding type for any undefined struct type
423 * (including none), it succeeds: the default encoding is a hex string
426 SetLastError(0xdeadbeef);
427 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0, NULL
, &size
);
428 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
429 ok(size
== sizeof(WCHAR
) || broken(!size
) /* Win10 1709+ */, "wrong size %d\n", size
);
431 SetLastError(0xdeadbeef);
433 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0, str
, &size
);
434 ok(!ret
, "expected failure\n");
435 ok(GetLastError() == ERROR_MORE_DATA
, "wrong error %u\n", GetLastError());
437 size
= sizeof(WCHAR
);
438 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, NULL
, 0, str
, &size
);
439 ok(ret
, "CryptFormatObject failed, error %u\n", GetLastError());
440 ok(!str
[0], "expected empty string\n");
442 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, encodedInt
,
443 sizeof(encodedInt
), NULL
, &size
);
444 ok(ret
, "CryptFormatObject failed, error %u\n", GetLastError());
445 ok(size
>= sizeof(L
"02 01 01"), "wrong size %u\n", size
);
447 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, encodedInt
,
448 sizeof(encodedInt
), str
, &size
);
449 ok(ret
, "CryptFormatObject failed, error %u\n", GetLastError());
450 ok(!wcscmp(str
, L
"02 01 01"), "wrong string %s\n", wine_dbgstr_w(str
));
452 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
, encodedBigInt
,
453 sizeof(encodedBigInt
), NULL
, &size
);
454 ok(ret
, "CryptFormatObject failed, error %u\n", GetLastError());
455 ok(size
>= sizeof(encodedBigIntStr
), "wrong size %u\n", size
);
457 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
, NULL
,
458 encodedBigInt
, sizeof(encodedBigInt
), str
, &size
);
459 ok(ret
, "CryptFormatObject failed: %d\n", GetLastError());
460 ok(!wcsicmp(str
, encodedBigIntStr
), "wrong string %s\n", wine_dbgstr_w(str
));
462 /* When called with the default encoding type for any undefined struct
463 * type but CRYPT_FORMAT_STR_NO_HEX specified, it fails to find a
464 * formatting function.
466 SetLastError(0xdeadbeef);
467 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, CRYPT_FORMAT_STR_NO_HEX
,
468 NULL
, NULL
, NULL
, 0, NULL
, &size
);
469 ok(!ret
, "CryptFormatObject succeeded\n");
470 ok(GetLastError() == ERROR_FILE_NOT_FOUND
471 || GetLastError() == 0xdeadbeef, /* Vista, W2K8 */
472 "wrong error %u\n", GetLastError());
474 /* When called to format an AUTHORITY_KEY_ID2_INFO, it fails when no
477 SetLastError(0xdeadbeef);
478 ret
= CryptFormatObject(X509_ASN_ENCODING
, 0, 0, NULL
,
479 szOID_AUTHORITY_KEY_IDENTIFIER2
, NULL
, 0, NULL
, &size
);
480 ok(!ret
, "expected failure\n");
481 ok(GetLastError() == E_INVALIDARG
, "wrong error %u\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();