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 void testOIDToAlgID(void)
92 /* Test with a bogus one */
93 SetLastError(0xdeadbeef);
94 alg = CertOIDToAlgId("1.2.3");
95 ok(!alg && (GetLastError() == 0xdeadbeef ||
96 GetLastError() == ERROR_RESOURCE_NAME_NOT_FOUND),
97 "Expected ERROR_RESOURCE_NAME_NOT_FOUND or no error set, got %08x\n",
100 for (i = 0; i < sizeof(oidToAlgID) / sizeof(oidToAlgID[0]); i++)
102 alg = CertOIDToAlgId(oidToAlgID[i].oid);
103 /* Not all Windows installations support all these, so make sure it's
104 * at least not the wrong one.
106 ok(alg == 0 || alg == oidToAlgID[i].algID,
107 "Expected %d, got %d\n", oidToAlgID[i].algID, alg);
111 static void testAlgIDToOID(void)
116 /* Test with a bogus one */
117 SetLastError(0xdeadbeef);
118 oid = CertAlgIdToOID(ALG_CLASS_SIGNATURE | ALG_TYPE_ANY | 80);
119 ok(!oid && GetLastError() == 0xdeadbeef,
120 "Didn't expect last error (%08x) to be set\n", GetLastError());
121 for (i = 0; i < sizeof(algIDToOID) / sizeof(algIDToOID[0]); i++)
123 oid = CertAlgIdToOID(algIDToOID[i].algID);
124 /* Allow failure, not every version of Windows supports every algo */
126 ok(!strcmp(oid, algIDToOID[i].oid),
127 "Expected %s, got %s\n", algIDToOID[i].oid, oid);
131 static void test_oidFunctionSet(void)
133 HCRYPTOIDFUNCSET set1, set2;
139 set = CryptInitOIDFunctionSet(NULL, 0);
142 /* The name doesn't mean much */
143 set1 = CryptInitOIDFunctionSet("funky", 0);
144 ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
148 ret = CryptGetDefaultOIDDllList(NULL, 0, NULL, NULL);
149 ret = CryptGetDefaultOIDDllList(NULL, 0, NULL, &size);
152 ret = CryptGetDefaultOIDDllList(set1, 0, NULL, &size);
153 ok(ret, "CryptGetDefaultOIDDllList failed: %08x\n", GetLastError());
156 buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
159 ret = CryptGetDefaultOIDDllList(set1, 0, buf, &size);
160 ok(ret, "CryptGetDefaultOIDDllList failed: %08x\n",
162 ok(!*buf, "Expected empty DLL list\n");
163 HeapFree(GetProcessHeap(), 0, buf);
168 /* MSDN says flags must be 0, but it's not checked */
169 set1 = CryptInitOIDFunctionSet("", 1);
170 ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
171 set2 = CryptInitOIDFunctionSet("", 0);
172 ok(set2 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
173 /* There isn't a free function, so there must be only one set per name to
174 * limit leaks. (I guess the sets are freed when crypt32 is unloaded.)
176 ok(set1 == set2, "Expected identical sets\n");
179 /* The empty name function set used here seems to correspond to
184 /* There's no installed function for a built-in encoding. */
185 set1 = CryptInitOIDFunctionSet("CryptDllEncodeObject", 0);
186 ok(set1 != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
190 HCRYPTOIDFUNCADDR hFuncAddr;
192 ret = CryptGetOIDFunctionAddress(set1, X509_ASN_ENCODING, X509_CERT, 0,
193 &funcAddr, &hFuncAddr);
194 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
195 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
199 typedef int (*funcY)(int);
201 static int funky(int x)
206 static void test_installOIDFunctionAddress(void)
209 CRYPT_OID_FUNC_ENTRY entry = { CRYPT_DEFAULT_OID, funky };
210 HCRYPTOIDFUNCSET set;
213 ret = CryptInstallOIDFunctionAddress(NULL, 0, NULL, 0, NULL, 0);
216 /* Installing zero functions should work */
217 SetLastError(0xdeadbeef);
218 ret = CryptInstallOIDFunctionAddress(NULL, 0, "CryptDllEncodeObject", 0,
220 ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08x\n",
223 /* The function name doesn't much matter */
224 SetLastError(0xdeadbeef);
225 ret = CryptInstallOIDFunctionAddress(NULL, 0, "OhSoFunky", 0, NULL, 0);
226 ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08x\n",
228 SetLastError(0xdeadbeef);
229 entry.pszOID = X509_CERT;
230 ret = CryptInstallOIDFunctionAddress(NULL, 0, "OhSoFunky", 1, &entry, 0);
231 ok(ret && GetLastError() == 0xdeadbeef, "Expected success, got %08x\n",
233 set = CryptInitOIDFunctionSet("OhSoFunky", 0);
234 ok(set != 0, "CryptInitOIDFunctionSet failed: %08x\n", GetLastError());
237 funcY funcAddr = NULL;
238 HCRYPTOIDFUNCADDR hFuncAddr = NULL;
241 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, 0, 0, NULL,
244 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, 0, 0,
245 (void **)&funcAddr, &hFuncAddr);
246 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
247 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
248 ret = CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, X509_CERT, 0,
249 (void **)&funcAddr, &hFuncAddr);
250 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
251 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
252 ret = CryptGetOIDFunctionAddress(set, 0, X509_CERT, 0,
253 (void **)&funcAddr, &hFuncAddr);
254 ok(ret, "CryptGetOIDFunctionAddress failed: %d\n", GetLastError());
257 int y = funcAddr(0xabadc0da);
259 ok(y == 0xabadc0da, "Unexpected return (%d) from function\n", y);
260 CryptFreeOIDFunctionAddress(hFuncAddr, 0);
265 static void test_registerOIDFunction(void)
267 static const WCHAR bogusDll[] = { 'b','o','g','u','s','.','d','l','l',0 };
270 /* oddly, this succeeds under WinXP; the function name key is merely
271 * omitted. This may be a side effect of the registry code, I don't know.
272 * I don't check it because I doubt anyone would depend on it.
273 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, NULL,
274 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
276 /* On windows XP, GetLastError is incorrectly being set with an HRESULT,
279 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo", NULL, bogusDll,
281 ok(!ret && GetLastError() == E_INVALIDARG,
282 "Expected E_INVALIDARG: %d\n", GetLastError());
283 /* This has no effect, but "succeeds" on XP */
284 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "foo",
285 "1.2.3.4.5.6.7.8.9.10", NULL, NULL);
286 ok(ret, "Expected pseudo-success, got %d\n", GetLastError());
287 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
288 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
289 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
290 ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "CryptDllEncodeObject",
291 "1.2.3.4.5.6.7.8.9.10");
292 ok(ret, "CryptUnregisterOIDFunction failed: %d\n", GetLastError());
293 ret = CryptRegisterOIDFunction(X509_ASN_ENCODING, "bogus",
294 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
295 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
296 ret = CryptUnregisterOIDFunction(X509_ASN_ENCODING, "bogus",
297 "1.2.3.4.5.6.7.8.9.10");
298 ok(ret, "CryptUnregisterOIDFunction failed: %d\n", GetLastError());
299 /* This has no effect */
300 ret = CryptRegisterOIDFunction(PKCS_7_ASN_ENCODING, "CryptDllEncodeObject",
301 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
302 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
303 /* Check with bogus encoding type: */
304 ret = CryptRegisterOIDFunction(0, "CryptDllEncodeObject",
305 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
306 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
307 /* This is written with value 3 verbatim. Thus, the encoding type isn't
308 * (for now) treated as a mask.
310 ret = CryptRegisterOIDFunction(3, "CryptDllEncodeObject",
311 "1.2.3.4.5.6.7.8.9.10", bogusDll, NULL);
312 ok(ret, "CryptRegisterOIDFunction failed: %d\n", GetLastError());
313 ret = CryptUnregisterOIDFunction(3, "CryptDllEncodeObject",
314 "1.2.3.4.5.6.7.8.9.10");
315 ok(ret, "CryptUnregisterOIDFunction failed: %d\n", GetLastError());
318 static const WCHAR bogusDll[] = { 'b','o','g','u','s','.','d','l','l',0 };
319 static const WCHAR bogus2Dll[] = { 'b','o','g','u','s','2','.','d','l','l',0 };
321 static void test_registerDefaultOIDFunction(void)
323 static const char fmt[] =
324 "Software\\Microsoft\\Cryptography\\OID\\EncodingType %d\\%s\\DEFAULT";
325 static const char func[] = "CertDllOpenStoreProv";
331 ret = CryptRegisterDefaultOIDFunction(0, NULL, 0, NULL);
332 ok(!ret && GetLastError() == E_INVALIDARG,
333 "Expected E_INVALIDARG, got %08x\n", GetLastError());
334 /* This succeeds on WinXP, although the bogus entry is unusable.
335 ret = CryptRegisterDefaultOIDFunction(0, NULL, 0, bogusDll);
337 /* Register one at index 0 */
338 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 0,
340 ok(ret, "CryptRegisterDefaultOIDFunction failed: %08x\n", GetLastError());
341 /* Reregistering should fail */
342 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 0,
344 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
345 "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
346 /* Registering the same one at index 1 should also fail */
347 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 1,
349 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
350 "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
351 /* Registering a different one at index 1 succeeds */
352 ret = CryptRegisterDefaultOIDFunction(0, "CertDllOpenStoreProv", 1,
354 ok(ret, "CryptRegisterDefaultOIDFunction failed: %08x\n", GetLastError());
355 sprintf(buf, fmt, 0, func);
356 rc = RegOpenKeyA(HKEY_LOCAL_MACHINE, buf, &key);
357 ok(rc == 0, "Expected key to exist, RegOpenKeyW failed: %ld\n", rc);
360 static const WCHAR dllW[] = { 'D','l','l',0 };
361 WCHAR dllBuf[MAX_PATH];
365 size = sizeof(dllBuf) / sizeof(dllBuf[0]);
366 rc = RegQueryValueExW(key, dllW, NULL, &type, (LPBYTE)dllBuf, &size);
368 "Expected Dll value to exist, RegQueryValueExW failed: %ld\n", rc);
369 ok(type == REG_MULTI_SZ, "Expected type REG_MULTI_SZ, got %d\n", type);
370 /* bogusDll was registered first, so that should be first */
372 ok(!lstrcmpiW(ptr, bogusDll), "Unexpected dll\n");
373 ptr += lstrlenW(ptr) + 1;
374 ok(!lstrcmpiW(ptr, bogus2Dll), "Unexpected dll\n");
377 /* Unregister both of them */
378 ret = CryptUnregisterDefaultOIDFunction(0, "CertDllOpenStoreProv",
380 ok(ret, "CryptUnregisterDefaultOIDFunction failed: %08x\n",
382 ret = CryptUnregisterDefaultOIDFunction(0, "CertDllOpenStoreProv",
384 ok(ret, "CryptUnregisterDefaultOIDFunction failed: %08x\n",
386 /* Now that they're both unregistered, unregistering should fail */
387 ret = CryptUnregisterDefaultOIDFunction(0, "CertDllOpenStoreProv",
389 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
390 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
392 /* Repeat a few tests on the normal encoding type */
393 ret = CryptRegisterDefaultOIDFunction(X509_ASN_ENCODING,
394 "CertDllOpenStoreProv", 0, bogusDll);
395 ret = CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING,
396 "CertDllOpenStoreProv", bogusDll);
397 ok(ret, "CryptUnregisterDefaultOIDFunction failed\n");
398 ret = CryptUnregisterDefaultOIDFunction(X509_ASN_ENCODING,
399 "CertDllOpenStoreProv", bogusDll);
400 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
401 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
404 static BOOL WINAPI countOidInfo(PCCRYPT_OID_INFO pInfo, void *pvArg)
410 static BOOL WINAPI noOidInfo(PCCRYPT_OID_INFO pInfo, void *pvArg)
415 static void test_enumOIDInfo(void)
421 ret = CryptEnumOIDInfo(7, 0, NULL, NULL);
424 /* Silly tests, check that more than one thing is enumerated */
425 ret = CryptEnumOIDInfo(0, 0, &count, countOidInfo);
426 ok(ret && count > 0, "Expected more than item enumerated\n");
427 ret = CryptEnumOIDInfo(0, 0, NULL, noOidInfo);
428 ok(!ret, "Expected FALSE\n");
431 static void test_findOIDInfo(void)
433 static WCHAR sha1[] = { 's','h','a','1',0 };
434 static CHAR oid_rsa_md5[] = szOID_RSA_MD5;
435 ALG_ID alg = CALG_SHA1;
436 ALG_ID algs[2] = { CALG_MD5, CALG_RSA_SIGN };
437 PCCRYPT_OID_INFO info;
439 info = CryptFindOIDInfo(0, NULL, 0);
440 ok(info == NULL, "Expected NULL\n");
441 info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, oid_rsa_md5, 0);
442 ok(info != NULL, "Expected to find szOID_RSA_MD5\n");
445 ok(!strcmp(info->pszOID, szOID_RSA_MD5), "Expected %s, got %s\n",
446 szOID_RSA_MD5, info->pszOID);
447 ok(U(*info).Algid == CALG_MD5, "Expected CALG_MD5, got %d\n",
450 info = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, sha1, 0);
451 ok(info != NULL, "Expected to find sha1\n");
454 ok(!strcmp(info->pszOID, szOID_OIWSEC_sha1), "Expected %s, got %s\n",
455 szOID_OIWSEC_sha1, info->pszOID);
456 ok(U(*info).Algid == CALG_SHA1, "Expected CALG_SHA1, got %d\n",
459 info = CryptFindOIDInfo(CRYPT_OID_INFO_ALGID_KEY, &alg, 0);
460 ok(info != NULL, "Expected to find sha1\n");
463 ok(!strcmp(info->pszOID, szOID_OIWSEC_sha1), "Expected %s, got %s\n",
464 szOID_OIWSEC_sha1, info->pszOID);
465 ok(U(*info).Algid == CALG_SHA1, "Expected CALG_SHA1, got %d\n",
468 info = CryptFindOIDInfo(CRYPT_OID_INFO_SIGN_KEY, algs, 0);
469 ok(info != NULL, "Expected to find md5RSA\n");
472 ok(!strcmp(info->pszOID, szOID_RSA_MD5RSA), "Expected %s, got %s\n",
473 szOID_RSA_MD5RSA, info->pszOID);
474 ok(U(*info).Algid == CALG_MD5, "Expected CALG_MD5, got %d\n",
485 test_oidFunctionSet();
486 test_installOIDFunctionAddress();
487 test_registerOIDFunction();
488 test_registerDefaultOIDFunction();