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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
91 for (i = 0; i < sizeof(oidToAlgID) / sizeof(oidToAlgID[0]); i++)
93 DWORD alg = CertOIDToAlgId(oidToAlgID[i].oid);
95 ok(alg == oidToAlgID[i].algID,
96 "Expected %ld, got %ld\n", oidToAlgID[i].algID, alg);
100 static void testAlgIDToOID(void)
104 for (i = 0; i < sizeof(algIDToOID) / sizeof(algIDToOID[0]); i++)
106 LPCSTR oid = CertAlgIdToOID(algIDToOID[i].algID);
108 if ((!oid || !algIDToOID[i].oid) && oid != algIDToOID[i].oid &&
110 printf("Expected %s, got %s\n", algIDToOID[i].oid, oid);
111 else if (oid && algIDToOID[i].oid && strcmp(oid, algIDToOID[i].oid))
112 printf("Expected %s, got %s\n", algIDToOID[i].oid, oid);
116 static void test_findAttribute(void)
118 PCRYPT_ATTRIBUTE ret;
119 BYTE blobbin[] = {0x02,0x01,0x01};
120 CRYPT_ATTR_BLOB blobs[] = { { sizeof blobbin, blobbin }, };
121 CRYPT_ATTRIBUTE attr = { "1.2.3", sizeof(blobs) / sizeof(blobs[0]), blobs };
123 /* returns NULL, last error not set */
124 SetLastError(0xdeadbeef);
125 ret = CertFindAttribute(NULL, 0, NULL);
126 ok(ret == NULL, "Expected failure\n");
127 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
130 SetLastError(0xdeadbeef);
131 ret = CertFindAttribute(NULL, 1, NULL);
133 /* returns NULL, last error is ERROR_INVALID_PARAMETER */
134 SetLastError(0xdeadbeef);
135 ret = CertFindAttribute(NULL, 1, &attr);
136 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
137 "Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
139 /* returns NULL, last error not set */
140 SetLastError(0xdeadbeef);
141 ret = CertFindAttribute("bogus", 1, &attr);
142 ok(ret == NULL, "Expected failure\n");
143 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
145 /* returns NULL, last error not set */
146 SetLastError(0xdeadbeef);
147 ret = CertFindAttribute("1.2.4", 1, &attr);
148 ok(ret == NULL, "Expected failure\n");
149 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
151 /* succeeds, last error not set */
152 SetLastError(0xdeadbeef);
153 ret = CertFindAttribute("1.2.3", 1, &attr);
154 ok(ret != NULL, "CertFindAttribute failed: %08lx\n", GetLastError());
157 static void test_findExtension(void)
160 BYTE blobbin[] = {0x02,0x01,0x01};
161 CERT_EXTENSION ext = { "1.2.3", TRUE, { sizeof blobbin, blobbin } };
163 /* returns NULL, last error not set */
164 SetLastError(0xdeadbeef);
165 ret = CertFindExtension(NULL, 0, NULL);
166 ok(ret == NULL, "Expected failure\n");
167 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
170 SetLastError(0xdeadbeef);
171 ret = CertFindExtension(NULL, 1, NULL);
173 /* returns NULL, last error is ERROR_INVALID_PARAMETER */
174 SetLastError(0xdeadbeef);
175 ret = CertFindExtension(NULL, 1, &ext);
176 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
177 "Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
179 /* returns NULL, last error not set */
180 SetLastError(0xdeadbeef);
181 ret = CertFindExtension("bogus", 1, &ext);
182 ok(ret == NULL, "Expected failure\n");
183 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
185 /* returns NULL, last error not set */
186 SetLastError(0xdeadbeef);
187 ret = CertFindExtension("1.2.4", 1, &ext);
188 ok(ret == NULL, "Expected failure\n");
189 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
191 /* succeeds, last error not set */
192 SetLastError(0xdeadbeef);
193 ret = CertFindExtension("1.2.3", 1, &ext);
194 ok(ret != NULL, "CertFindExtension failed: %08lx\n", GetLastError());
197 static void test_findRDNAttr(void)
200 BYTE bin[] = { 0x16,0x09,'J','u','a','n',' ','L','a','n','g' };
201 CERT_RDN_ATTR attrs[] = {
202 { "1.2.3", CERT_RDN_IA5_STRING, { sizeof bin, bin } },
205 { sizeof(attrs) / sizeof(attrs[0]), attrs },
207 CERT_NAME_INFO nameInfo = { sizeof(rdns) / sizeof(rdns[0]), rdns };
210 SetLastError(0xdeadbeef);
211 ret = CertFindRDNAttr(NULL, NULL);
213 /* returns NULL, last error is ERROR_INVALID_PARAMETER */
214 SetLastError(0xdeadbeef);
215 ret = CertFindRDNAttr(NULL, &nameInfo);
216 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
217 "Expected ERROR_INVALID_PARAMETER, got %ld (%08lx)\n", GetLastError(),
219 /* returns NULL, last error not set */
220 SetLastError(0xdeadbeef);
221 ret = CertFindRDNAttr("bogus", &nameInfo);
222 ok(ret == NULL, "Expected failure\n");
223 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
225 /* returns NULL, last error not set */
226 SetLastError(0xdeadbeef);
227 ret = CertFindRDNAttr("1.2.4", &nameInfo);
228 ok(ret == NULL, "Expected failure\n");
229 ok(GetLastError() == 0xdeadbeef, "Last error was set to %08lx\n",
231 /* succeeds, last error not set */
232 SetLastError(0xdeadbeef);
233 ret = CertFindRDNAttr("1.2.3", &nameInfo);
234 ok(ret != NULL, "CertFindRDNAttr failed: %08lx\n", GetLastError());
237 static void test_verifyTimeValidity(void)
241 CERT_INFO info = { 0 };
244 GetSystemTime(&sysTime);
245 SystemTimeToFileTime(&sysTime, &fileTime);
247 ret = CertVerifyTimeValidity(NULL, NULL);
248 ret = CertVerifyTimeValidity(&fileTime, NULL);
250 /* Check with 0 NotBefore and NotAfter */
251 ret = CertVerifyTimeValidity(&fileTime, &info);
252 ok(ret == 1, "Expected 1, got %ld\n", ret);
253 memcpy(&info.NotAfter, &fileTime, sizeof(info.NotAfter));
254 /* Check with NotAfter equal to comparison time */
255 ret = CertVerifyTimeValidity(&fileTime, &info);
256 ok(ret == 0, "Expected 0, got %ld\n", ret);
257 /* Check with NotBefore after comparison time */
258 memcpy(&info.NotBefore, &fileTime, sizeof(info.NotBefore));
259 info.NotBefore.dwLowDateTime += 5000;
260 ret = CertVerifyTimeValidity(&fileTime, &info);
261 ok(ret == -1, "Expected -1, got %ld\n", ret);
268 test_findAttribute();
269 test_findExtension();
271 test_verifyTimeValidity();