2 * Unit test suite for crypt32.dll's OID support functions.
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
28 #include "wine/test.h"
36 static const struct OIDToAlgID oidToAlgID[] = {
37 { szOID_RSA_RSA, CALG_RSA_KEYX },
38 { szOID_RSA_MD2RSA, CALG_MD2 },
39 { szOID_RSA_MD4RSA, CALG_MD4 },
40 { szOID_RSA_MD5RSA, CALG_MD5 },
41 { szOID_RSA_SHA1RSA, CALG_SHA },
42 { szOID_RSA_DH, CALG_DH_SF },
43 { szOID_RSA_SMIMEalgESDH, CALG_DH_EPHEM },
44 { szOID_RSA_SMIMEalgCMS3DESwrap, CALG_3DES },
45 { szOID_RSA_SMIMEalgCMSRC2wrap, CALG_RC2 },
46 { szOID_RSA_MD2, CALG_MD2 },
47 { szOID_RSA_MD4, CALG_MD4 },
48 { szOID_RSA_MD5, CALG_MD5 },
49 { szOID_RSA_RC2CBC, CALG_RC2 },
50 { szOID_RSA_RC4, CALG_RC4 },
51 { szOID_RSA_DES_EDE3_CBC, CALG_3DES },
52 { szOID_ANSI_X942_DH, CALG_DH_SF },
53 { szOID_X957_DSA, CALG_DSS_SIGN },
54 { szOID_X957_SHA1DSA, CALG_SHA },
55 { szOID_OIWSEC_md4RSA, CALG_MD4 },
56 { szOID_OIWSEC_md5RSA, CALG_MD5 },
57 { szOID_OIWSEC_md4RSA2, CALG_MD4 },
58 { szOID_OIWSEC_desCBC, CALG_DES },
59 { szOID_OIWSEC_dsa, CALG_DSS_SIGN },
60 { szOID_OIWSEC_shaDSA, CALG_SHA },
61 { szOID_OIWSEC_shaRSA, CALG_SHA },
62 { szOID_OIWSEC_sha, CALG_SHA },
63 { szOID_OIWSEC_rsaXchg, CALG_RSA_KEYX },
64 { szOID_OIWSEC_sha1, CALG_SHA },
65 { szOID_OIWSEC_dsaSHA1, CALG_SHA },
66 { szOID_OIWSEC_sha1RSASign, CALG_SHA },
67 { szOID_OIWDIR_md2RSA, CALG_MD2 },
68 { szOID_INFOSEC_mosaicUpdatedSig, CALG_SHA },
69 { szOID_INFOSEC_mosaicKMandUpdSig, CALG_DSS_SIGN },
72 static const struct OIDToAlgID algIDToOID[] = {
73 { szOID_RSA_RSA, CALG_RSA_KEYX },
74 { szOID_RSA_SMIMEalgESDH, CALG_DH_EPHEM },
75 { szOID_RSA_MD2, CALG_MD2 },
76 { szOID_RSA_MD4, CALG_MD4 },
77 { szOID_RSA_MD5, CALG_MD5 },
78 { szOID_RSA_RC2CBC, CALG_RC2 },
79 { szOID_RSA_RC4, CALG_RC4 },
80 { szOID_RSA_DES_EDE3_CBC, CALG_3DES },
81 { szOID_ANSI_X942_DH, CALG_DH_SF },
82 { szOID_X957_DSA, CALG_DSS_SIGN },
83 { szOID_OIWSEC_desCBC, CALG_DES },
84 { szOID_OIWSEC_sha1, CALG_SHA },
87 static const WCHAR bogusDll[] = { 'b','o','g','u','s','.','d','l','l',0 };
88 static const WCHAR bogus2Dll[] = { 'b','o','g','u','s','2','.','d','l','l',0 };
90 static void testOIDToAlgID(void)
95 /* Test with a bogus one */
96 SetLastError(0xdeadbeef);
97 alg = CertOIDToAlgId("1.2.3");
98 ok(!alg && (GetLastError() == 0xdeadbeef ||
99 GetLastError() == ERROR_RESOURCE_NAME_NOT_FOUND),
100 "Expected ERROR_RESOURCE_NAME_NOT_FOUND or no error set, got %08x\n",
103 for (i = 0; i < sizeof(oidToAlgID) / sizeof(oidToAlgID[0]); i++)
105 alg = CertOIDToAlgId(oidToAlgID[i].oid);
106 /* Not all Windows installations support all these, so make sure it's
107 * at least not the wrong one.
109 ok(alg == 0 || alg == oidToAlgID[i].algID,
110 "Expected %d, got %d\n", oidToAlgID[i].algID, alg);
114 static void testAlgIDToOID(void)
119 /* Test with a bogus one */
120 SetLastError(0xdeadbeef);
121 oid = CertAlgIdToOID(ALG_CLASS_SIGNATURE | ALG_TYPE_ANY | 80);
122 ok(!oid && GetLastError() == 0xdeadbeef,
123 "Didn't expect last error (%08x) to be set\n", GetLastError());
124 for (i = 0; i < sizeof(algIDToOID) / sizeof(algIDToOID[0]); i++)
126 oid = CertAlgIdToOID(algIDToOID[i].algID);
127 /* Allow failure, not every version of Windows supports every algo */
129 ok(!strcmp(oid, algIDToOID[i].oid),
130 "Expected %s, got %s\n", algIDToOID[i].oid, oid);
134 static void test_oidFunctionSet(void)
136 HCRYPTOIDFUNCSET set1, set2;
142 set = CryptInitOIDFunctionSet(NULL, 0);
145 /* The name doesn't mean much */
146 set1 = CryptInitOIDFunctionSet("funky", 0);
147 ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
151 ret = CryptGetDefaultOIDDllList(NULL, 0, NULL, NULL);
152 ret = CryptGetDefaultOIDDllList(NULL, 0, NULL, &size);
155 ret = CryptGetDefaultOIDDllList(set1, 0, NULL, &size);
156 ok(ret, "CryptGetDefaultOIDDllList failed: %08x\n", GetLastError());
159 buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
162 ret = CryptGetDefaultOIDDllList(set1, 0, buf, &size);
163 ok(ret, "CryptGetDefaultOIDDllList failed: %08x\n",
165 ok(!*buf, "Expected empty DLL list\n");
166 HeapFree(GetProcessHeap(), 0, buf);
171 /* MSDN says flags must be 0, but it's not checked */
172 set1 = CryptInitOIDFunctionSet("", 1);
173 ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
174 set2 = CryptInitOIDFunctionSet("", 0);
175 ok(set2 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
176 /* There isn't a free function, so there must be only one set per name to
177 * limit leaks. (I guess the sets are freed when crypt32 is unloaded.)
179 ok(set1 == set2, "Expected identical sets\n");
182 /* The empty name function set used here seems to correspond to
187 /* There's no installed function for a built-in encoding. */
188 set1 = CryptInitOIDFunctionSet("CryptDllEncodeObject", 0);
189 ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
193 HCRYPTOIDFUNCADDR hFuncAddr;
195 ret = CryptGetOIDFunctionAddress(set1, X509_ASN_ENCODING, X509_CERT, 0,
196 &funcAddr, &hFuncAddr);
197 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
198 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
202 typedef int (*funcY)(int);
204 static int funky(int x)
209 static void test_installOIDFunctionAddress(void)
212 CRYPT_OID_FUNC_ENTRY entry = { CRYPT_DEFAULT_OID, funky };
213 HCRYPTOIDFUNCSET set;
216 ret = CryptInstallOIDFunctionAddress(NULL, 0, NULL, 0, NULL, 0);
219 /* Installing zero functions should work */
220 SetLastError(0xdeadbeef);
221 ret = CryptInstallOIDFunctionAddress(NULL, 0, "CryptDllEncodeObject", 0,
223 ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08x\n",
226 /* The function name doesn't much matter */
227 SetLastError(0xdeadbeef);
228 ret = CryptInstallOIDFunctionAddress(NULL, 0, "OhSoFunky", 0, NULL, 0);
229 ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08x\n",
231 SetLastError(0xdeadbeef);
232 entry.pszOID = X509_CERT;
233 ret = CryptInstallOIDFunctionAddress(NULL, 0, "OhSoFunky", 1, &entry, 0);
234 ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08x\n",
236 set = CryptInitOIDFunctionSet("OhSoFunky", 0);
237 ok(set != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
240 funcY funcAddr = NULL;
241 HCRYPTOIDFUNCADDR hFuncAddr = NULL;
244 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, 0, 0, NULL,
247 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, 0, 0,
248 (void **)&funcAddr, &hFuncAddr);
249 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
250 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
251 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, X509_CERT, 0,
252 (void **)&funcAddr, &hFuncAddr);
253 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
254 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
255 ret = CryptGetOIDFunctionAddress(set, 0, X509_CERT, 0,
256 (void **)&funcAddr, &hFuncAddr);
257 ok(ret, "CryptGetOIDFunctionAddress failed: %d\n", GetLastError());
260 int y = funcAddr(0xabadc0da);
262 ok(y == 0xabadc0da, "Unexpected return (%d) from function\n", y);
263 CryptFreeOIDFunctionAddress(hFuncAddr, 0);
268 static void test_registerOIDFunction(void)
272 /* oddly, this succeeds under WinXP; the function name key is merely
273 * omitted. This may be a side effect of the registry code, I don't know.
274 * I don't check it because I doubt anyone would depend on it.
275 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, NULL,
276 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
278 /* On windows XP, GetLastError is incorrectly being set with an HRESULT,
281 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo", NULL, bogusDll,
283 ok(!ret && GetLastError() == E_INVALIDARG,
284 "Expected E_INVALIDARG: %d\n", GetLastError());
285 /* This has no effect, but "succeeds" on XP */
286 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo",
287 "1.2.3.4.5.6.7.8.9.10", NULL, NULL);
288 ok(ret, "Expected pseudo-success, got %d\n", GetLastError());
289 SetLastError(0xdeadbeef);
290 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
291 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
292 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
294 skip("Need admin rights\n");
297 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
298 ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
299 "1.2.3.4.5.6.7.8.9.10");
300 ok(ret, "CryptUnregisterOIDFunction failed: %d\n", GetLastError());
301 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "bogus",
302 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
303 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
304 ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "bogus",
305 "1.2.3.4.5.6.7.8.9.10");
306 ok(ret, "CryptUnregisterOIDFunction failed: %d\n", GetLastError());
307 /* This has no effect */
308 ret = CryptRegisterOIDFunction(PKCS_7_ASN_ENCODING, "CryptDllEncodeObject",
309 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
310 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
311 /* Check with bogus encoding type: */
312 ret = CryptRegisterOIDFunction(0, "CryptDllEncodeObject",
313 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
314 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
315 /* This is written with value 3 verbatim. Thus, the encoding type isn't
316 * (for now) treated as a mask.
318 ret = CryptRegisterOIDFunction(3, "CryptDllEncodeObject",
319 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
320 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
321 ret = CryptUnregisterOIDFunction(3, "CryptDllEncodeObject",
322 "1.2.3.4.5.6.7.8.9.10");
323 ok(ret, "CryptUnregisterOIDFunction failed: %d\n", GetLastError());
326 static void test_registerDefaultOIDFunction(void)
328 static const char fmt[] =
329 "Software\\Microsoft\\Cryptography\\OID\\EncodingType %d\\%s\\DEFAULT";
330 static const char func[] = "CertDllOpenStoreProv";
336 ret = CryptRegisterDefaultOIDFunction(0, NULL, 0, NULL);
337 ok(!ret && GetLastError() == E_INVALIDARG,
338 "Expected E_INVALIDARG, got %08x\n", GetLastError());
339 /* This succeeds on WinXP, although the bogus entry is unusable.
340 ret = CryptRegisterDefaultOIDFunction(0, NULL, 0, bogusDll);
342 /* Register one at index 0 */
343 SetLastError(0xdeadbeef);
344 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 0,
346 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
348 skip("Need admin rights\n");
351 ok(ret, "CryptRegisterDefaultOIDFunction failed: %08x\n", GetLastError());
352 /* Reregistering should fail */
353 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 0,
355 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
356 "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
357 /* Registering the same one at index 1 should also fail */
358 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 1,
360 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
361 "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
362 /* Registering a different one at index 1 succeeds */
363 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 1,
365 ok(ret, "CryptRegisterDefaultOIDFunction failed: %08x\n", GetLastError());
366 sprintf(buf, fmt, 0, func);
367 rc = RegOpenKeyA(HKEY_LOCAL_MACHINE, buf, &key);
368 ok(rc == 0, "Expected key to exist, RegOpenKeyA failed: %ld\n", rc);
371 static const CHAR dllA[] = "Dll";
372 static const CHAR bogusDll_A[] = "bogus.dll";
373 static const CHAR bogus2Dll_A[] = "bogus2.dll";
374 CHAR dllBuf[MAX_PATH];
378 size = sizeof(dllBuf) / sizeof(dllBuf[0]);
379 rc = RegQueryValueExA(key, dllA, NULL, &type, (LPBYTE)dllBuf, &size);
381 "Expected Dll value to exist, RegQueryValueExA failed: %ld\n", rc);
382 ok(type == REG_MULTI_SZ, "Expected type REG_MULTI_SZ, got %d\n", type);
383 /* bogusDll was registered first, so that should be first */
385 ok(!lstrcmpiA(ptr, bogusDll_A), "Unexpected dll\n");
386 ptr += lstrlenA(ptr) + 1;
387 ok(!lstrcmpiA(ptr, bogus2Dll_A), "Unexpected dll\n");
390 /* Unregister both of them */
391 ret = CryptUnregisterDefaultOIDFunction(0, "CertDllOpenStoreProv",
393 ok(ret, "CryptUnregisterDefaultOIDFunction failed: %08x\n",
395 ret = CryptUnregisterDefaultOIDFunction(0, "CertDllOpenStoreProv",
397 ok(ret, "CryptUnregisterDefaultOIDFunction failed: %08x\n",
399 /* Now that they're both unregistered, unregistering should fail */
400 ret = CryptUnregisterDefaultOIDFunction(0, "CertDllOpenStoreProv",
402 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
403 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
405 /* Repeat a few tests on the normal encoding type */
406 ret = CryptRegisterDefaultOIDFunction(X509_ASN_ENCODING,
407 "CertDllOpenStoreProv", 0, bogusDll);
408 ret = CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING,
409 "CertDllOpenStoreProv", bogusDll);
410 ok(ret, "CryptUnregisterDefaultOIDFunction failed\n");
411 ret = CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING,
412 "CertDllOpenStoreProv", bogusDll);
413 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
414 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
417 static BOOL WINAPI countOidInfo(PCCRYPT_OID_INFO pInfo, void *pvArg)
423 static BOOL WINAPI noOidInfo(PCCRYPT_OID_INFO pInfo, void *pvArg)
428 static void test_enumOIDInfo(void)
434 ret = CryptEnumOIDInfo(7, 0, NULL, NULL);
437 /* Silly tests, check that more than one thing is enumerated */
438 ret = CryptEnumOIDInfo(0, 0, &count, countOidInfo);
439 ok(ret && count > 0, "Expected more than item enumerated\n");
440 ret = CryptEnumOIDInfo(0, 0, NULL, noOidInfo);
441 ok(!ret, "Expected FALSE\n");
444 static void test_findOIDInfo(void)
446 static WCHAR sha1[] = { 's','h','a','1',0 };
447 static CHAR oid_rsa_md5[] = szOID_RSA_MD5;
448 ALG_ID alg = CALG_SHA1;
449 ALG_ID algs[2] = { CALG_MD5, CALG_RSA_SIGN };
450 PCCRYPT_OID_INFO info;
452 info = CryptFindOIDInfo(0, NULL, 0);
453 ok(info == NULL, "Expected NULL\n");
454 info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, oid_rsa_md5, 0);
455 ok(info != NULL, "Expected to find szOID_RSA_MD5\n");
458 ok(!strcmp(info->pszOID, szOID_RSA_MD5), "Expected %s, got %s\n",
459 szOID_RSA_MD5, info->pszOID);
460 ok(U(*info).Algid == CALG_MD5, "Expected CALG_MD5, got %d\n",
463 info = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, sha1, 0);
464 ok(info != NULL, "Expected to find sha1\n");
467 ok(!strcmp(info->pszOID, szOID_OIWSEC_sha1), "Expected %s, got %s\n",
468 szOID_OIWSEC_sha1, info->pszOID);
469 ok(U(*info).Algid == CALG_SHA1, "Expected CALG_SHA1, got %d\n",
472 info = CryptFindOIDInfo(CRYPT_OID_INFO_ALGID_KEY, &alg, 0);
473 ok(info != NULL, "Expected to find sha1\n");
476 ok(!strcmp(info->pszOID, szOID_OIWSEC_sha1), "Expected %s, got %s\n",
477 szOID_OIWSEC_sha1, info->pszOID);
478 ok(U(*info).Algid == CALG_SHA1, "Expected CALG_SHA1, got %d\n",
481 info = CryptFindOIDInfo(CRYPT_OID_INFO_SIGN_KEY, algs, 0);
482 ok(info != NULL, "Expected to find md5RSA\n");
485 ok(!strcmp(info->pszOID, szOID_RSA_MD5RSA), "Expected %s, got %s\n",
486 szOID_RSA_MD5RSA, info->pszOID);
487 ok(U(*info).Algid == CALG_MD5, "Expected CALG_MD5, got %d\n",
498 test_oidFunctionSet();
499 test_installOIDFunctionAddress();
500 test_registerOIDFunction();
501 test_registerDefaultOIDFunction();