[CRYPT32_WINETEST] Add a PCH.
[reactos.git] / modules / rostests / winetests / crypt32 / ctl.c
1 /*
2 * crypt32 CTL functions tests
3 *
4 * Copyright 2008 Juan Lang
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "precomp.h"
22
23 static const BYTE emptyCTL[] = {
24 0x30,0x17,0x30,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,
25 0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00 };
26 static const BYTE ctlWithOneEntry[] = {
27 0x30,0x2a,0x30,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,
28 0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x30,0x11,0x30,0x0f,0x04,
29 0x01,0x01,0x31,0x0a,0x30,0x08,0x06,0x02,0x2a,0x03,0x31,0x02,0x30,0x00 };
30 static const BYTE signedCTL[] = {
31 0x30,0x81,0xc7,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
32 0x81,0xb9,0x30,0x81,0xb6,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
33 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x28,0x06,0x09,0x2a,0x86,
34 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x1b,0x04,0x19,0x30,0x17,0x30,0x00,
35 0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,
36 0x30,0x5a,0x30,0x02,0x06,0x00,0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,
37 0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
38 0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,
39 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,
40 0x00,0x04,0x40,0xca,0xd8,0x32,0xd1,0xbd,0x97,0x61,0x54,0xd6,0x80,0xcf,0x0d,
41 0xbd,0xa2,0x42,0xc7,0xca,0x37,0x91,0x7d,0x9d,0xac,0x8c,0xdf,0x05,0x8a,0x39,
42 0xc6,0x07,0xc1,0x37,0xe6,0xb9,0xd1,0x0d,0x26,0xec,0xa5,0xb0,0x8a,0x51,0x26,
43 0x2b,0x4f,0x73,0x44,0x86,0x83,0x5e,0x2b,0x6e,0xcc,0xf8,0x1b,0x85,0x53,0xe9,
44 0x7a,0x80,0x8f,0x6b,0x42,0x19,0x93 };
45 static const BYTE signedCTLWithSubjectAlgorithm[] = {
46 0x30,0x81,0xd1,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
47 0x81,0xc3,0x30,0x81,0xc0,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
48 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x32,0x06,0x09,0x2a,0x86,
49 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x25,0x04,0x23,0x30,0x21,0x30,0x00,
50 0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,
51 0x30,0x5a,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,
52 0x00,0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,
53 0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
54 0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,
55 0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0xca,0x7a,
56 0xfa,0xbf,0x44,0x36,0xfd,0xb2,0x84,0x70,0x0f,0xf5,0x66,0xcb,0x68,0xdb,0x45,
57 0x74,0xa1,0xed,0xe3,0x30,0x4a,0x11,0xd2,0x7b,0xf5,0xa9,0x68,0x8d,0x36,0xe8,
58 0x79,0xff,0xa2,0xf2,0x4c,0x8a,0xa9,0x65,0x03,0xf8,0x77,0xa5,0x01,0xd3,0x46,
59 0x8a,0xcc,0x93,0x36,0x30,0xe1,0xa4,0x47,0x70,0x3d,0xb3,0x97,0xfc,0x6d,0x24,
60 0xe9,0xf9 };
61 static const BYTE signedCTLWithCTLInnerContent[] = {
62 0x30,0x82,0x01,0x0f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
63 0xa0,0x82,0x01,0x00,0x30,0x81,0xfd,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,
64 0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x30,0x06,0x09,
65 0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0xa0,0x23,0x30,0x21,0x30,0x00,
66 0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,
67 0x30,0x5a,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,
68 0x00,0x31,0x81,0xb5,0x30,0x81,0xb2,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
69 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
70 0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
71 0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0xa0,0x3b,0x30,0x18,0x06,0x09,0x2a,0x86,
72 0x48,0x86,0xf7,0x0d,0x01,0x09,0x03,0x31,0x0b,0x06,0x09,0x2b,0x06,0x01,0x04,
73 0x01,0x82,0x37,0x0a,0x01,0x30,0x1f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
74 0x01,0x09,0x04,0x31,0x12,0x04,0x10,0x54,0x71,0xbc,0xe1,0x56,0x31,0xa2,0xf9,
75 0x65,0x70,0x34,0xf8,0xe2,0xe9,0xb4,0xf4,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
76 0x40,0x2f,0x1b,0x9f,0x5a,0x4a,0x15,0x73,0xfa,0xb1,0x93,0x3d,0x09,0x52,0xdf,
77 0x6b,0x98,0x4b,0x13,0x5e,0xe7,0xbf,0x65,0xf4,0x9c,0xc2,0xb1,0x77,0x09,0xb1,
78 0x66,0x4d,0x72,0x0d,0xb1,0x1a,0x50,0x20,0xe0,0x57,0xa2,0x39,0xc7,0xcd,0x7f,
79 0x8e,0xe7,0x5f,0x76,0x2b,0xd1,0x6a,0x82,0xb3,0x30,0x25,0x61,0xf6,0x25,0x23,
80 0x57,0x6c,0x0b,0x47,0xb8 };
81 static const BYTE signedCTLWithCTLInnerContentAndBadSig[] = {
82 0x30,0x82,0x01,0x0f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
83 0xa0,0x82,0x01,0x00,0x30,0x81,0xfd,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,
84 0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x30,0x06,0x09,
85 0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0xa0,0x23,0x30,0x21,0x30,0x00,
86 0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,
87 0x30,0x5a,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,
88 0x00,0x31,0x81,0xb5,0x30,0x81,0xb2,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
89 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
90 0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
91 0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0xa0,0x3b,0x30,0x18,0x06,0x09,0x2a,0x86,
92 0x48,0x86,0xf7,0x0d,0x01,0x09,0x03,0x31,0x0b,0x06,0x09,0x2b,0x06,0x01,0x04,
93 0x01,0x82,0x37,0x0a,0x01,0x30,0x1f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
94 0x01,0x09,0x04,0x31,0x12,0x04,0x10,0x54,0x71,0xbc,0xe1,0x56,0x31,0xa2,0xf9,
95 0x65,0x70,0x34,0xf8,0xe2,0xe9,0xb4,0xf4,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
96 0x40,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
97 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
98 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
99 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
100 0xff,0xff,0xff,0xff,0xff };
101
102 static void testCreateCTL(void)
103 {
104 PCCTL_CONTEXT ctl;
105
106 SetLastError(0xdeadbeef);
107 ctl = CertCreateCTLContext(0, NULL, 0);
108 ok(!ctl && GetLastError() == E_INVALIDARG,
109 "expected E_INVALIDARG, got %08x\n", GetLastError());
110 SetLastError(0xdeadbeef);
111 ctl = CertCreateCTLContext(X509_ASN_ENCODING, NULL, 0);
112 ok(!ctl &&
113 (GetLastError() == ERROR_INVALID_DATA ||
114 GetLastError() == OSS_MORE_INPUT), /* win9x */
115 "expected ERROR_INVALID_DATA, got %d (0x%08x)\n", GetLastError(),
116 GetLastError());
117 /* An empty CTL can't be created.. */
118 SetLastError(0xdeadbeef);
119 ctl = CertCreateCTLContext(X509_ASN_ENCODING, emptyCTL, sizeof(emptyCTL));
120 ok(!ctl &&
121 (GetLastError() == ERROR_INVALID_DATA ||
122 GetLastError() == OSS_DATA_ERROR), /* win9x */
123 "expected ERROR_INVALID_DATA, got %d (0x%08x)\n", GetLastError(),
124 GetLastError());
125 /* Nor can any of these "signed" CTLs whose inner content OID isn't
126 * szOID_CTL.
127 */
128 SetLastError(0xdeadbeef);
129 ctl = CertCreateCTLContext(X509_ASN_ENCODING, signedCTL, sizeof(signedCTL));
130 ok(!ctl &&
131 (GetLastError() == ERROR_INVALID_DATA ||
132 GetLastError() == CRYPT_E_UNEXPECTED_MSG_TYPE /* win9x */ ||
133 GetLastError() == ERROR_SUCCESS /* some win98 */),
134 "expected ERROR_INVALID_DATA, CRYPT_E_UNEXPECTED_MSG_TYPE, or ERROR_SUCCESS, got %d (0x%08x)\n", GetLastError(),
135 GetLastError());
136 SetLastError(0xdeadbeef);
137 ctl = CertCreateCTLContext(X509_ASN_ENCODING, ctlWithOneEntry,
138 sizeof(ctlWithOneEntry));
139 ok(!ctl &&
140 (GetLastError() == ERROR_INVALID_DATA ||
141 GetLastError() == CRYPT_E_UNEXPECTED_MSG_TYPE /* win9x */ ||
142 GetLastError() == OSS_DATA_ERROR /* some win98 */ ||
143 GetLastError() == ERROR_SUCCESS /* some win98 */),
144 "expected ERROR_INVALID_DATA, CRYPT_E_UNEXPECTED_MSG_TYPE, OSS_DATA_ERROR, or ERROR_SUCCESS, got %d (0x%08x)\n", GetLastError(),
145 GetLastError());
146 SetLastError(0xdeadbeef);
147 ctl = CertCreateCTLContext(X509_ASN_ENCODING,
148 signedCTLWithSubjectAlgorithm, sizeof(signedCTLWithSubjectAlgorithm));
149 ok(!ctl &&
150 (GetLastError() == ERROR_INVALID_DATA ||
151 GetLastError() == CRYPT_E_UNEXPECTED_MSG_TYPE /* win9x */ ||
152 GetLastError() == ERROR_SUCCESS /* some win98 */),
153 "expected ERROR_INVALID_DATA, got %d (0x%08x)\n", GetLastError(),
154 GetLastError());
155 /* This signed CTL with the appropriate inner content type can be decoded.
156 */
157 ctl = CertCreateCTLContext(X509_ASN_ENCODING,
158 signedCTLWithCTLInnerContent, sizeof(signedCTLWithCTLInnerContent));
159 ok(ctl != NULL, "CertCreateCTLContext failed: %08x\n", GetLastError());
160 if (ctl)
161 {
162 /* Even though the CTL was decoded with X509_ASN_ENCODING, the
163 * message encoding type is included in the CTL's encoding type.
164 */
165 ok(ctl->dwMsgAndCertEncodingType ==
166 (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING),
167 "expected X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, got %08x\n",
168 ctl->dwMsgAndCertEncodingType);
169 CertFreeCTLContext(ctl);
170 }
171 /* This CTL with a bad signature can also be decoded, so the sig isn't
172 * checked when loading the CTL.
173 */
174 ctl = CertCreateCTLContext(X509_ASN_ENCODING,
175 signedCTLWithCTLInnerContentAndBadSig,
176 sizeof(signedCTLWithCTLInnerContentAndBadSig));
177 ok(ctl != NULL, "CertCreateCTLContext failed: %08x\n", GetLastError());
178 if (ctl)
179 CertFreeCTLContext(ctl);
180 }
181
182 static void testDupCTL(void)
183 {
184 PCCTL_CONTEXT context, dupContext;
185 BOOL res;
186
187 context = CertDuplicateCTLContext(NULL);
188 ok(context == NULL, "expected NULL\n");
189 context = CertCreateCTLContext(X509_ASN_ENCODING,
190 signedCTLWithCTLInnerContent, sizeof(signedCTLWithCTLInnerContent));
191 dupContext = CertDuplicateCTLContext(context);
192 ok(dupContext != NULL, "expected a context\n");
193 ok(dupContext == context, "expected identical context addresses\n");
194
195 res = CertFreeCTLContext(dupContext);
196 ok(res, "CertFreeCTLContext failed\n");
197
198 res = CertFreeCTLContext(context);
199 ok(res, "CertFreeCTLContext failed\n");
200
201 res = CertFreeCTLContext(NULL);
202 ok(res, "CertFreeCTLContext failed\n");
203 }
204
205 static void checkHash(const BYTE *data, DWORD dataLen, ALG_ID algID,
206 PCCTL_CONTEXT context, DWORD propID)
207 {
208 BYTE hash[20] = { 0 }, hashProperty[20];
209 BOOL ret;
210 DWORD size;
211
212 memset(hash, 0, sizeof(hash));
213 memset(hashProperty, 0, sizeof(hashProperty));
214 size = sizeof(hash);
215 ret = CryptHashCertificate(0, algID, 0, data, dataLen, hash, &size);
216 ok(ret, "CryptHashCertificate failed: %08x\n", GetLastError());
217 ret = CertGetCTLContextProperty(context, propID, hashProperty, &size);
218 ok(ret, "CertGetCTLContextProperty failed: %08x\n", GetLastError());
219 if (ret)
220 ok(!memcmp(hash, hashProperty, size),
221 "Unexpected hash for property %d\n", propID);
222 }
223
224 static void testCTLProperties(void)
225 {
226 PCCTL_CONTEXT ctl;
227 BOOL ret;
228 DWORD propID, numProps, access, size;
229
230 ctl = CertCreateCTLContext(X509_ASN_ENCODING,
231 signedCTLWithCTLInnerContent, sizeof(signedCTLWithCTLInnerContent));
232 if (!ctl)
233 {
234 skip("CertCreateCTLContext failed: %08x\n", GetLastError());
235 return;
236 }
237
238 /* No properties as yet */
239 propID = 0;
240 numProps = 0;
241 do {
242 propID = CertEnumCTLContextProperties(ctl, propID);
243 if (propID)
244 numProps++;
245 } while (propID != 0);
246 ok(numProps == 0, "Expected 0 properties, got %d\n", numProps);
247
248 /* An implicit property */
249 ret = CertGetCTLContextProperty(ctl, CERT_ACCESS_STATE_PROP_ID, NULL,
250 &size);
251 ok(ret || broken(GetLastError() == CRYPT_E_NOT_FOUND /* some win98 */),
252 "CertGetCTLContextProperty failed: %08x\n", GetLastError());
253 ret = CertGetCTLContextProperty(ctl, CERT_ACCESS_STATE_PROP_ID, &access,
254 &size);
255 ok(ret || broken(GetLastError() == CRYPT_E_NOT_FOUND /* some win98 */),
256 "CertGetCTLContextProperty failed: %08x\n", GetLastError());
257 if (ret)
258 ok(!(access & CERT_ACCESS_STATE_WRITE_PERSIST_FLAG),
259 "Didn't expect a persisted cert\n");
260
261 checkHash(signedCTLWithCTLInnerContent,
262 sizeof(signedCTLWithCTLInnerContent), CALG_SHA1, ctl, CERT_HASH_PROP_ID);
263
264 /* Now that the hash property is set, we should get one property when
265 * enumerating.
266 */
267 propID = 0;
268 numProps = 0;
269 do {
270 propID = CertEnumCTLContextProperties(ctl, propID);
271 if (propID)
272 numProps++;
273 } while (propID != 0);
274 ok(numProps == 1, "Expected 1 properties, got %d\n", numProps);
275
276 checkHash(signedCTLWithCTLInnerContent,
277 sizeof(signedCTLWithCTLInnerContent), CALG_MD5, ctl,
278 CERT_MD5_HASH_PROP_ID);
279
280 CertFreeCTLContext(ctl);
281 }
282
283 static const BYTE signedCTLWithUsage[] = {
284 0x30,0x82,0x01,0x0f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
285 0xa0,0x82,0x01,0x00,0x30,0x81,0xfd,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,
286 0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x30,0x06,0x09,
287 0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0xa0,0x23,0x30,0x21,0x30,0x0a,
288 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x18,0x0f,0x31,0x36,0x30,
289 0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,
290 0x00,0x31,0x81,0xb5,0x30,0x81,0xb2,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
291 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
292 0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
293 0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0xa0,0x3b,0x30,0x18,0x06,0x09,0x2a,0x86,
294 0x48,0x86,0xf7,0x0d,0x01,0x09,0x03,0x31,0x0b,0x06,0x09,0x2b,0x06,0x01,0x04,
295 0x01,0x82,0x37,0x0a,0x01,0x30,0x1f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
296 0x01,0x09,0x04,0x31,0x12,0x04,0x10,0xc4,0x3e,0x7e,0xc8,0xf9,0x85,0xf3,0x27,
297 0x7d,0xc0,0x38,0xb2,0x7f,0xc7,0x76,0x85,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
298 0x40,0x90,0x33,0x1b,0xb4,0x88,0x35,0xe6,0xf7,0x7f,0x93,0x05,0xc9,0x1a,0x0e,
299 0x8f,0x21,0xc0,0xaa,0xb3,0xab,0x3e,0x4a,0xa6,0x63,0x74,0xfd,0xef,0x11,0xbd,
300 0x67,0x3a,0x1b,0x07,0x4b,0x88,0x59,0x31,0xd5,0x08,0xf9,0x09,0x2f,0x0b,0x85,
301 0x62,0x5a,0x67,0x3b,0x62,0x7e,0x81,0x31,0xea,0xa4,0x36,0x5f,0x9a,0x92,0xb6,
302 0x66,0xa5,0x00,0x60,0x96 };
303 static const BYTE signedCTLWithListID1[] = {
304 0x30,0x82,0x01,0x07,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
305 0xa0,0x81,0xf9,0x30,0x81,0xf6,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,
306 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x29,0x06,0x09,0x2b,
307 0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0xa0,0x1c,0x30,0x1a,0x30,0x00,0x04,
308 0x01,0x01,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,
309 0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x31,0x81,0xb5,0x30,0x81,0xb2,0x02,
310 0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,
311 0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,
312 0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0xa0,
313 0x3b,0x30,0x18,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x03,0x31,
314 0x0b,0x06,0x09,0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0x30,0x1f,0x06,
315 0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x04,0x31,0x12,0x04,0x10,0xbc,
316 0x13,0xde,0x17,0xc7,0x7c,0x67,0x5b,0xf9,0xa9,0x7c,0xb0,0xd9,0xf2,0x84,0xd5,
317 0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
318 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
319 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
320 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
321 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff };
322 static const BYTE signedCTLWithListID2[] = {
323 0x30,0x82,0x01,0x07,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
324 0xa0,0x81,0xf9,0x30,0x81,0xf6,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,
325 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x29,0x06,0x09,0x2b,
326 0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0xa0,0x1c,0x30,0x1a,0x30,0x00,0x04,
327 0x01,0x02,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,
328 0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x31,0x81,0xb5,0x30,0x81,0xb2,0x02,
329 0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,
330 0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,
331 0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0xa0,
332 0x3b,0x30,0x18,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x03,0x31,
333 0x0b,0x06,0x09,0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0x30,0x1f,0x06,
334 0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x09,0x04,0x31,0x12,0x04,0x10,0x60,
335 0x6b,0x10,0xb6,0xb8,0x74,0xb8,0xf3,0x79,0xd5,0x42,0x15,0x4a,0x60,0x93,0x1f,
336 0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
337 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
338 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
339 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
340 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff };
341
342 static void testAddCTLToStore(void)
343 {
344 HCERTSTORE store;
345 BOOL ret;
346 DWORD numCTLs, expectedCTLs;
347 PCCTL_CONTEXT ctl;
348
349 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
350 CERT_STORE_CREATE_NEW_FLAG, NULL);
351 /* Add two CTLs */
352 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
353 signedCTLWithCTLInnerContent, sizeof(signedCTLWithCTLInnerContent),
354 CERT_STORE_ADD_ALWAYS, NULL);
355 ok(ret, "CertAddEncodedCTLToStore failed: %08x\n", GetLastError());
356 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
357 signedCTLWithCTLInnerContentAndBadSig,
358 sizeof(signedCTLWithCTLInnerContentAndBadSig), CERT_STORE_ADD_ALWAYS,
359 NULL);
360 ok(ret, "CertAddEncodedCTLToStore failed: %08x\n", GetLastError());
361 /* Check that two exist */
362 numCTLs = 0;
363 ctl = NULL;
364 do {
365 ctl = CertEnumCTLsInStore(store, ctl);
366 if (ctl)
367 numCTLs++;
368 } while (ctl);
369 ok(numCTLs == 2, "expected 2 CTLs, got %d\n", numCTLs);
370 CertCloseStore(store, 0);
371
372 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
373 CERT_STORE_CREATE_NEW_FLAG, NULL);
374 /* Add the two CTLs again. They're identical except for the signature.. */
375 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
376 signedCTLWithCTLInnerContent, sizeof(signedCTLWithCTLInnerContent),
377 CERT_STORE_ADD_NEW, NULL);
378 ok(ret, "CertAddEncodedCTLToStore failed: %08x\n", GetLastError());
379 /* so adding the second CTL fails. */
380 SetLastError(0xdeadbeef);
381 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
382 signedCTLWithCTLInnerContentAndBadSig,
383 sizeof(signedCTLWithCTLInnerContentAndBadSig), CERT_STORE_ADD_NEW,
384 NULL);
385 ok(!ret && (GetLastError() == CRYPT_E_EXISTS || GetLastError() == OSS_DATA_ERROR),
386 "expected CRYPT_E_EXISTS or OSS_DATA_ERROR, got %d %08x\n", ret, GetLastError());
387 CertCloseStore(store, 0);
388
389 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
390 CERT_STORE_CREATE_NEW_FLAG, NULL);
391 /* Add two CTLs. These two have different usages, so they're considered
392 * different.
393 */
394 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
395 signedCTLWithCTLInnerContent, sizeof(signedCTLWithCTLInnerContent),
396 CERT_STORE_ADD_NEW, NULL);
397 ok(ret, "CertAddEncodedCTLToStore failed: %08x\n", GetLastError());
398 expectedCTLs = 1;
399 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
400 signedCTLWithUsage, sizeof(signedCTLWithUsage), CERT_STORE_ADD_NEW,
401 NULL);
402 ok(ret || broken(GetLastError() == OSS_DATA_ERROR /* some win98 */),
403 "CertAddEncodedCTLToStore failed: %08x\n", GetLastError());
404 if (ret)
405 expectedCTLs++;
406 /* Check that two exist */
407 numCTLs = 0;
408 ctl = NULL;
409 do {
410 ctl = CertEnumCTLsInStore(store, ctl);
411 if (ctl)
412 numCTLs++;
413 } while (ctl);
414 ok(numCTLs == expectedCTLs, "expected %d CTLs, got %d\n", expectedCTLs,
415 numCTLs);
416 CertCloseStore(store, 0);
417
418 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
419 CERT_STORE_CREATE_NEW_FLAG, NULL);
420 /* Add two CTLs. Now they have the same (empty) usages and different list
421 * IDs, so they're different.
422 */
423 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
424 signedCTLWithListID1, sizeof(signedCTLWithListID1), CERT_STORE_ADD_NEW,
425 NULL);
426 if (!ret)
427 {
428 skip("adding a CTL with an empty usage not supported\n");
429 return;
430 }
431 ok(ret, "CertAddEncodedCTLToStore failed: %08x\n", GetLastError());
432 ret = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING,
433 signedCTLWithListID2, sizeof(signedCTLWithListID2), CERT_STORE_ADD_NEW,
434 NULL);
435 ok(ret, "CertAddEncodedCTLToStore failed: %08x\n", GetLastError());
436 /* Check that two exist */
437 numCTLs = 0;
438 ctl = NULL;
439 do {
440 ctl = CertEnumCTLsInStore(store, ctl);
441 if (ctl)
442 numCTLs++;
443 } while (ctl);
444 ok(numCTLs == 2, "expected 2 CTLs, got %d\n", numCTLs);
445 CertCloseStore(store, 0);
446 }
447
448 START_TEST(ctl)
449 {
450 testCreateCTL();
451 testDupCTL();
452 testCTLProperties();
453 testAddCTLToStore();
454 }