[CRYPT32_WINETEST] Sync with Wine Staging 2.2. CORE-12823
[reactos.git] / rostests / winetests / crypt32 / store.c
1 /*
2 * crypt32 cert store function tests
3 *
4 * Copyright 2005-2006 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 <stdio.h>
22 //#include <stdarg.h>
23
24 #include <windef.h>
25 #include <winbase.h>
26 #include <shlobj.h>
27 #include <shlwapi.h>
28 #include <winreg.h>
29 //#include <winerror.h>
30 #include <wincrypt.h>
31
32 #include <wine/test.h>
33
34 /* The following aren't defined in wincrypt.h, as they're "reserved" */
35 #define CERT_CERT_PROP_ID 32
36 #define CERT_CRL_PROP_ID 33
37 #define CERT_CTL_PROP_ID 34
38
39 struct CertPropIDHeader
40 {
41 DWORD propID;
42 DWORD unknown1;
43 DWORD cb;
44 };
45
46 static const BYTE emptyCert[] = { 0x30, 0x00 };
47 static const BYTE bigCert[] = { 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
48 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
49 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22,
50 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30,
51 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30,
52 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30,
53 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20,
54 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01,
55 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
56 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01 };
57 static const BYTE signedBigCert[] = {
58 0x30, 0x81, 0x93, 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06, 0x00, 0x30,
59 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a,
60 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22, 0x18, 0x0f,
61 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
62 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30,
63 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06,
64 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61,
65 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01, 0x00, 0xa3,
66 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
67 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
68 0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
69 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
70 static const BYTE serializedCert[] = { 0x20, 0x00, 0x00, 0x00,
71 0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x30, 0x7a, 0x02, 0x01, 0x01,
72 0x30, 0x02, 0x06, 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
73 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67,
74 0x00, 0x30, 0x22, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31,
75 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31,
76 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15,
77 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75,
78 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06,
79 0x00, 0x03, 0x01, 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
80 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02,
81 0x01, 0x01 };
82 static const BYTE signedCRL[] = { 0x30, 0x45, 0x30, 0x2c, 0x30, 0x02, 0x06,
83 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
84 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x18, 0x0f,
85 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
86 0x30, 0x5a, 0x30, 0x02, 0x06, 0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c,
87 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
88 static const BYTE bigCert2[] = { 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
89 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
90 0x0a, 0x41, 0x6c, 0x65, 0x78, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22,
91 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30,
92 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30,
93 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30,
94 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x41, 0x6c, 0x65, 0x78, 0x20,
95 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01,
96 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
97 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01 };
98 static const BYTE signedCTLWithCTLInnerContent[] = {
99 0x30,0x82,0x01,0x0f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
100 0xa0,0x82,0x01,0x00,0x30,0x81,0xfd,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,
101 0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x30,0x06,0x09,
102 0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0xa0,0x23,0x30,0x21,0x30,0x00,
103 0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,
104 0x30,0x5a,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,
105 0x00,0x31,0x81,0xb5,0x30,0x81,0xb2,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
106 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
107 0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
108 0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0xa0,0x3b,0x30,0x18,0x06,0x09,0x2a,0x86,
109 0x48,0x86,0xf7,0x0d,0x01,0x09,0x03,0x31,0x0b,0x06,0x09,0x2b,0x06,0x01,0x04,
110 0x01,0x82,0x37,0x0a,0x01,0x30,0x1f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
111 0x01,0x09,0x04,0x31,0x12,0x04,0x10,0x54,0x71,0xbc,0xe1,0x56,0x31,0xa2,0xf9,
112 0x65,0x70,0x34,0xf8,0xe2,0xe9,0xb4,0xf4,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
113 0x40,0x2f,0x1b,0x9f,0x5a,0x4a,0x15,0x73,0xfa,0xb1,0x93,0x3d,0x09,0x52,0xdf,
114 0x6b,0x98,0x4b,0x13,0x5e,0xe7,0xbf,0x65,0xf4,0x9c,0xc2,0xb1,0x77,0x09,0xb1,
115 0x66,0x4d,0x72,0x0d,0xb1,0x1a,0x50,0x20,0xe0,0x57,0xa2,0x39,0xc7,0xcd,0x7f,
116 0x8e,0xe7,0x5f,0x76,0x2b,0xd1,0x6a,0x82,0xb3,0x30,0x25,0x61,0xf6,0x25,0x23,
117 0x57,0x6c,0x0b,0x47,0xb8 };
118
119
120 static BOOL (WINAPI *pCertAddStoreToCollection)(HCERTSTORE,HCERTSTORE,DWORD,DWORD);
121 static BOOL (WINAPI *pCertControlStore)(HCERTSTORE,DWORD,DWORD,void const*);
122 static PCCRL_CONTEXT (WINAPI *pCertEnumCRLsInStore)(HCERTSTORE,PCCRL_CONTEXT);
123 static BOOL (WINAPI *pCertEnumSystemStore)(DWORD,void*,void*,PFN_CERT_ENUM_SYSTEM_STORE);
124 static BOOL (WINAPI *pCertGetStoreProperty)(HCERTSTORE,DWORD,void*,DWORD*);
125 static void (WINAPI *pCertRemoveStoreFromCollection)(HCERTSTORE,HCERTSTORE);
126 static BOOL (WINAPI *pCertSetStoreProperty)(HCERTSTORE,DWORD,DWORD,const void*);
127 static BOOL (WINAPI *pCertAddCertificateLinkToStore)(HCERTSTORE,PCCERT_CONTEXT,DWORD,PCCERT_CONTEXT*);
128 static BOOL (WINAPI *pCertRegisterSystemStore)(const void*,DWORD,void*,void*);
129 static BOOL (WINAPI *pCertUnregisterSystemStore)(const void*,DWORD);
130
131 #define test_store_is_empty(store) _test_store_is_empty(__LINE__,store)
132 static void _test_store_is_empty(unsigned line, HCERTSTORE store)
133 {
134 const CERT_CONTEXT *cert;
135
136 cert = CertEnumCertificatesInStore(store, NULL);
137 ok_(__FILE__,line)(!cert && GetLastError() == CRYPT_E_NOT_FOUND, "store is not empty\n");
138 }
139
140 static void testMemStore(void)
141 {
142 HCERTSTORE store1, store2;
143 PCCERT_CONTEXT context;
144 BOOL ret;
145 DWORD GLE;
146
147 /* NULL provider */
148 store1 = CertOpenStore(0, 0, 0, 0, NULL);
149 ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
150 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
151 /* weird flags */
152 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
153 CERT_STORE_DELETE_FLAG, NULL);
154 ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
155 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %d\n", GetLastError());
156
157 /* normal */
158 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
159 CERT_STORE_CREATE_NEW_FLAG, NULL);
160 ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
161 /* open existing doesn't */
162 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
163 CERT_STORE_OPEN_EXISTING_FLAG, NULL);
164 ok(store2 != NULL, "CertOpenStore failed: %d\n", GetLastError());
165 ok(store1 != store2, "Expected different stores\n");
166
167 /* add a bogus (empty) cert */
168 context = NULL;
169 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
170 sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
171 /* Windows returns CRYPT_E_ASN1_EOD or OSS_DATA_ERROR, but accept
172 * CRYPT_E_ASN1_CORRUPT as well (because matching errors is tough in this
173 * case)
174 */
175 GLE = GetLastError();
176 ok(!ret && (GLE == CRYPT_E_ASN1_EOD || GLE == CRYPT_E_ASN1_CORRUPT ||
177 GLE == OSS_DATA_ERROR),
178 "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
179 GLE);
180 /* add a "signed" cert--the signature isn't a real signature, so this adds
181 * without any check of the signature's validity
182 */
183 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
184 signedBigCert, sizeof(signedBigCert), CERT_STORE_ADD_ALWAYS, &context);
185 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
186 ok(context != NULL, "Expected a valid cert context\n");
187 if (context)
188 {
189 ok(context->cbCertEncoded == sizeof(signedBigCert),
190 "Wrong cert size %d\n", context->cbCertEncoded);
191 ok(!memcmp(context->pbCertEncoded, signedBigCert,
192 sizeof(signedBigCert)), "Unexpected encoded cert in context\n");
193 /* remove it, the rest of the tests will work on an unsigned cert */
194 ret = CertDeleteCertificateFromStore(context);
195 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
196 GetLastError());
197 }
198 /* try adding a "signed" CRL as a cert */
199 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
200 signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &context);
201 GLE = GetLastError();
202 ok(!ret && (GLE == CRYPT_E_ASN1_BADTAG || GLE == CRYPT_E_ASN1_CORRUPT ||
203 GLE == OSS_DATA_ERROR),
204 "Expected CRYPT_E_ASN1_BADTAG or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
205 GLE);
206 /* add a cert to store1 */
207 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
208 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
209 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
210 ok(context != NULL, "Expected a valid cert context\n");
211 if (context)
212 {
213 DWORD size;
214 BYTE *buf;
215
216 ok(context->cbCertEncoded == sizeof(bigCert),
217 "Wrong cert size %d\n", context->cbCertEncoded);
218 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
219 "Unexpected encoded cert in context\n");
220 ok(context->hCertStore == store1, "Unexpected store\n");
221
222 /* check serializing this element */
223 /* These crash
224 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
225 ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
226 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
227 */
228 /* apparently flags are ignored */
229 ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
230 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
231 GetLastError());
232 buf = HeapAlloc(GetProcessHeap(), 0, size);
233 if (buf)
234 {
235 ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
236 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n", GetLastError());
237 ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
238 ok(!memcmp(serializedCert, buf, size),
239 "Unexpected serialized cert\n");
240 HeapFree(GetProcessHeap(), 0, buf);
241 }
242
243 ret = CertFreeCertificateContext(context);
244 ok(ret, "CertFreeCertificateContext failed: %08x\n", GetLastError());
245 }
246 /* verify the cert's in store1 */
247 context = CertEnumCertificatesInStore(store1, NULL);
248 ok(context != NULL, "Expected a valid context\n");
249 context = CertEnumCertificatesInStore(store1, context);
250 ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
251 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
252 /* verify store2 (the "open existing" mem store) is still empty */
253 context = CertEnumCertificatesInStore(store2, NULL);
254 ok(!context, "Expected an empty store\n");
255 /* delete the cert from store1, and check it's empty */
256 context = CertEnumCertificatesInStore(store1, NULL);
257 if (context)
258 {
259 /* Deleting a bitwise copy crashes with an access to an uninitialized
260 * pointer, so a cert context has some special data out there in memory
261 * someplace
262 CERT_CONTEXT copy;
263 memcpy(&copy, context, sizeof(copy));
264 ret = CertDeleteCertificateFromStore(&copy);
265 */
266 PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
267
268 ok(copy != NULL, "CertDuplicateCertificateContext failed: %08x\n",
269 GetLastError());
270 ret = CertDeleteCertificateFromStore(context);
271 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
272 GetLastError());
273 /* try deleting a copy */
274 ret = CertDeleteCertificateFromStore(copy);
275 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
276 GetLastError());
277 /* check that the store is empty */
278 context = CertEnumCertificatesInStore(store1, NULL);
279 ok(!context, "Expected an empty store\n");
280 }
281
282 /* close an empty store */
283 ret = CertCloseStore(NULL, 0);
284 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
285 ret = CertCloseStore(store1, 0);
286 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
287 ret = CertCloseStore(store2, 0);
288 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
289
290 /* This seems nonsensical, but you can open a read-only mem store, only
291 * it isn't read-only
292 */
293 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
294 CERT_STORE_READONLY_FLAG, NULL);
295 ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
296 /* yep, this succeeds */
297 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
298 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
299 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
300 ok(context != NULL, "Expected a valid cert context\n");
301 if (context)
302 {
303 ok(context->cbCertEncoded == sizeof(bigCert),
304 "Wrong cert size %d\n", context->cbCertEncoded);
305 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
306 "Unexpected encoded cert in context\n");
307 ok(context->hCertStore == store1, "Unexpected store\n");
308 ret = CertDeleteCertificateFromStore(context);
309 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
310 GetLastError());
311 }
312 CertCloseStore(store1, 0);
313 }
314
315 static void compareStore(HCERTSTORE store, LPCSTR name, const BYTE *pb,
316 DWORD cb, BOOL todo)
317 {
318 BOOL ret;
319 CRYPT_DATA_BLOB blob = { 0, NULL };
320
321 ret = CertSaveStore(store, X509_ASN_ENCODING, CERT_STORE_SAVE_AS_STORE,
322 CERT_STORE_SAVE_TO_MEMORY, &blob, 0);
323 ok(ret, "CertSaveStore failed: %08x\n", GetLastError());
324 todo_wine_if (todo)
325 ok(blob.cbData == cb, "%s: expected size %d, got %d\n", name, cb,
326 blob.cbData);
327 blob.pbData = HeapAlloc(GetProcessHeap(), 0, blob.cbData);
328 if (blob.pbData)
329 {
330 ret = CertSaveStore(store, X509_ASN_ENCODING, CERT_STORE_SAVE_AS_STORE,
331 CERT_STORE_SAVE_TO_MEMORY, &blob, 0);
332 ok(ret, "CertSaveStore failed: %08x\n", GetLastError());
333 todo_wine_if (todo)
334 ok(!memcmp(pb, blob.pbData, cb), "%s: unexpected value\n", name);
335 HeapFree(GetProcessHeap(), 0, blob.pbData);
336 }
337 }
338
339 static const BYTE serializedStoreWithCert[] = {
340 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
341 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
342 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
343 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
344 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
345 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
346 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
347 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
348 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
349 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
350 0x00,0x00,0x00,0x00,0x00,0x00 };
351
352 static const struct
353 {
354 HKEY key;
355 DWORD cert_store;
356 BOOL appdata_file;
357 WCHAR store_name[16];
358 const WCHAR *base_reg_path;
359 } reg_store_saved_certs[] = {
360 { HKEY_LOCAL_MACHINE, CERT_SYSTEM_STORE_LOCAL_MACHINE, FALSE,
361 {'R','O','O','T',0}, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH },
362 { HKEY_LOCAL_MACHINE, CERT_SYSTEM_STORE_LOCAL_MACHINE, FALSE,
363 {'M','Y',0}, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH },
364 { HKEY_LOCAL_MACHINE, CERT_SYSTEM_STORE_LOCAL_MACHINE, FALSE,
365 {'C','A',0}, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH },
366 /* Adding to HKCU\Root triggers safety warning. */
367 { HKEY_CURRENT_USER, CERT_SYSTEM_STORE_CURRENT_USER, TRUE,
368 {'M','Y',0}, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH },
369 { HKEY_CURRENT_USER, CERT_SYSTEM_STORE_CURRENT_USER, FALSE,
370 {'C','A',0}, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH }
371 };
372
373 /* Testing whether system stores are available for adding new certs
374 * and checking directly in the registry whether they are actually saved or deleted.
375 * Windows treats HKCU\My (at least) as a special case and uses AppData directory
376 * for storing certs, not registry.
377 */
378 static void testRegStoreSavedCerts(void)
379 {
380 static const WCHAR fmt[] =
381 { '%','s','\\','%','s','\\','%','s','\\','%','s',0},
382 ms_certs[] =
383 { 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m','C','e','r','t','i','f','i','c','a','t','e','s',0},
384 certs[] =
385 {'C','e','r','t','i','f','i','c','a','t','e','s',0},
386 bigCert_hash[] = {
387 '6','E','3','0','9','0','7','1','5','F','D','9','2','3',
388 '5','6','E','B','A','E','2','5','4','0','E','6','2','2',
389 'D','A','1','9','2','6','0','2','A','6','0','8',0};
390 PCCERT_CONTEXT cert1, cert2;
391 HCERTSTORE store;
392 HANDLE cert_file;
393 HRESULT pathres;
394 WCHAR key_name[MAX_PATH], appdata_path[MAX_PATH];
395 HKEY key;
396 BOOL ret;
397 DWORD res,i;
398
399 for (i = 0; i < sizeof(reg_store_saved_certs) / sizeof(reg_store_saved_certs[0]); i++)
400 {
401 DWORD err;
402
403 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
404 reg_store_saved_certs[i].cert_store, reg_store_saved_certs[i].store_name);
405
406 err = GetLastError();
407 if (!store)
408 {
409 ok (err == ERROR_ACCESS_DENIED, "Failed to create store at %d (%08x)\n", i, err);
410 skip("Insufficient privileges for the test %d\n", i);
411 continue;
412 }
413 ok (store!=NULL, "Failed to open the store at %d, %x\n", i, GetLastError());
414 cert1 = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert, sizeof(bigCert));
415 ok (cert1 != NULL, "Create cert context failed at %d, %x\n", i, GetLastError());
416 ret = CertAddCertificateContextToStore(store, cert1, CERT_STORE_ADD_REPLACE_EXISTING, NULL);
417 /* Addittional skip per Win7, it allows opening HKLM store, but disallows adding certs */
418 err = GetLastError();
419 if (!ret)
420 {
421 ok (err == ERROR_ACCESS_DENIED, "Failed to add certificate to store at %d (%08x)\n", i, err);
422 skip("Insufficient privileges for the test %d\n", i);
423 continue;
424 }
425 ok (ret, "Adding to the store failed at %d, %x\n", i, err);
426 CertFreeCertificateContext(cert1);
427 CertCloseStore(store, 0);
428
429 wsprintfW(key_name, fmt, reg_store_saved_certs[i].base_reg_path,
430 reg_store_saved_certs[i].store_name, certs, bigCert_hash);
431
432 if (!reg_store_saved_certs[i].appdata_file)
433 {
434 res = RegOpenKeyExW(reg_store_saved_certs[i].key, key_name, 0, KEY_ALL_ACCESS, &key);
435 ok (!res, "The cert hasn't been saved at %d, %x\n", i, GetLastError());
436 if (!res) RegCloseKey(key);
437 } else
438 {
439 pathres = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, appdata_path);
440 ok (pathres == S_OK,
441 "Failed to get app data path at %d (%x)\n", pathres, GetLastError());
442 if (pathres == S_OK)
443 {
444 PathAppendW(appdata_path, ms_certs);
445 PathAppendW(appdata_path, reg_store_saved_certs[i].store_name);
446 PathAppendW(appdata_path, certs);
447 PathAppendW(appdata_path, bigCert_hash);
448
449 cert_file = CreateFileW(appdata_path, GENERIC_READ, 0, NULL,
450 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
451 todo_wine ok (cert_file != INVALID_HANDLE_VALUE,
452 "Cert was not saved in AppData at %d (%x)\n", i, GetLastError());
453 CloseHandle(cert_file);
454 }
455 }
456
457 /* deleting cert from store */
458 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
459 reg_store_saved_certs[i].cert_store, reg_store_saved_certs[i].store_name);
460 ok (store!=NULL, "Failed to open the store at %d, %x\n", i, GetLastError());
461
462 cert1 = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert, sizeof(bigCert));
463 ok (cert1 != NULL, "Create cert context failed at %d, %x\n", i, GetLastError());
464
465 cert2 = CertFindCertificateInStore(store, X509_ASN_ENCODING, 0,
466 CERT_FIND_EXISTING, cert1, NULL);
467 ok (cert2 != NULL, "Failed to find cert in the store at %d, %x\n", i, GetLastError());
468
469 ret = CertDeleteCertificateFromStore(cert2);
470 ok (ret, "Failed to delete certificate from store at %d, %x\n", i, GetLastError());
471
472 CertFreeCertificateContext(cert1);
473 CertFreeCertificateContext(cert2);
474 CertCloseStore(store, 0);
475
476 res = RegOpenKeyExW(reg_store_saved_certs[i].key, key_name, 0, KEY_ALL_ACCESS, &key);
477 ok (res, "The cert's registry entry should be absent at %i, %x\n", i, GetLastError());
478 if (!res) RegCloseKey(key);
479
480 if (reg_store_saved_certs[i].appdata_file)
481 {
482 cert_file = CreateFileW(appdata_path, GENERIC_READ, 0, NULL,
483 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
484 ok (cert_file == INVALID_HANDLE_VALUE,
485 "Cert should have been absent in AppData %d\n", i);
486
487 CloseHandle(cert_file);
488 }
489 }
490 }
491
492 /**
493 * This test checks that certificate falls into correct store of a collection
494 * depending on the access flags and priorities.
495 */
496 static void testStoresInCollection(void)
497 {
498 PCCERT_CONTEXT cert1, cert2, tcert1;
499 HCERTSTORE collection, ro_store, rw_store, rw_store_2, tstore;
500 static const WCHAR WineTestRO_W[] = { 'W','i','n','e','T','e','s','t','_','R','O',0 },
501 WineTestRW_W[] = { 'W','i','n','e','T','e','s','t','_','R','W',0 },
502 WineTestRW2_W[]= { 'W','i','n','e','T','e','s','t','_','R','W','2',0 };
503 BOOL ret;
504
505 if (!pCertAddStoreToCollection)
506 {
507 win_skip("CertAddStoreToCollection() is not available\n");
508 return;
509 }
510 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
511 CERT_STORE_CREATE_NEW_FLAG, NULL);
512 ok(collection != NULL, "Failed to init collection store, last error %x\n", GetLastError());
513 /* Add read-only store to collection with very high priority*/
514 ro_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0, 0,
515 CERT_SYSTEM_STORE_CURRENT_USER, WineTestRO_W);
516 ok(ro_store != NULL, "Failed to init ro store %x\n", GetLastError());
517
518 ret = CertAddStoreToCollection(collection, ro_store, 0, 1000);
519 ok (ret, "Failed to add read-only store to collection %x\n", GetLastError());
520
521 cert1 = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert, sizeof(bigCert));
522 ok (cert1 != NULL, "Create cert context failed %x\n", GetLastError());
523 ret = CertAddCertificateContextToStore(collection, cert1, CERT_STORE_ADD_ALWAYS, NULL);
524 ok (!ret, "Added cert to collection with single read-only store %x\n", GetLastError());
525
526 /* Add read-write store to collection with the lowest priority*/
527 rw_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0, 0,
528 CERT_SYSTEM_STORE_CURRENT_USER, WineTestRW_W);
529 ok (rw_store != NULL, "Failed to open rw store %x\n", GetLastError());
530 ret = CertAddStoreToCollection(collection, rw_store, CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
531 ok (ret, "Failed to add rw store to collection %x\n", GetLastError());
532 /** Adding certificate to collection should fall into rw store,
533 * even though prioirty of the ro_store is higher */
534 ret = CertAddCertificateContextToStore(collection, cert1, CERT_STORE_ADD_REPLACE_EXISTING, NULL);
535 ok (ret, "Failed to add cert to the collection %x\n", GetLastError());
536
537 tcert1 = CertEnumCertificatesInStore(ro_store, NULL);
538 ok (!tcert1, "Read-only ro_store contains cert\n");
539
540 tcert1 = CertEnumCertificatesInStore(rw_store, NULL);
541 ok (cert1 && tcert1->cbCertEncoded == cert1->cbCertEncoded,
542 "Unexpected cert in the rw store\n");
543 CertFreeCertificateContext(tcert1);
544
545 tcert1 = CertEnumCertificatesInStore(collection, NULL);
546 ok (tcert1 && tcert1->cbCertEncoded == cert1->cbCertEncoded,
547 "Unexpected cert in the collection\n");
548 CertFreeCertificateContext(tcert1);
549
550 /** adding one more rw store with higher priority*/
551 rw_store_2 = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0, 0,
552 CERT_SYSTEM_STORE_CURRENT_USER, WineTestRW2_W);
553 ok (rw_store_2 != NULL, "Failed to init second rw store %x\n", GetLastError());
554 ret = CertAddStoreToCollection(collection, rw_store_2, CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 50);
555 ok (ret, "Failed to add rw_store_2 to collection %x\n",GetLastError());
556
557 cert2 = CertCreateCertificateContext(X509_ASN_ENCODING, signedBigCert, sizeof(signedBigCert));
558 ok (cert2 != NULL, "Failed to create cert context %x\n", GetLastError());
559 ret = CertAddCertificateContextToStore(collection, cert2, CERT_STORE_ADD_REPLACE_EXISTING, NULL);
560 ok (ret, "Failed to add cert3 to the store %x\n",GetLastError());
561
562 /** checking certificates in the stores */
563 tcert1 = CertEnumCertificatesInStore(ro_store, 0);
564 ok (tcert1 == NULL, "Read-only store not empty\n");
565
566 tcert1 = CertEnumCertificatesInStore(rw_store, NULL);
567 ok (tcert1 && tcert1->cbCertEncoded == cert1->cbCertEncoded,
568 "Unexpected cert in the rw_store\n");
569 CertFreeCertificateContext(tcert1);
570
571 tcert1 = CertEnumCertificatesInStore(rw_store_2, NULL);
572 ok (tcert1 && tcert1->cbCertEncoded == cert2->cbCertEncoded,
573 "Unexpected cert in the rw_store_2\n");
574 CertFreeCertificateContext(tcert1);
575
576 /** checking certificates in the collection */
577 tcert1 = CertEnumCertificatesInStore(collection, NULL);
578 ok (tcert1 && tcert1->cbCertEncoded == cert2->cbCertEncoded,
579 "cert2 expected in the collection got %p, %x\n",tcert1, GetLastError());
580 tcert1 = CertEnumCertificatesInStore(collection, tcert1);
581 ok (tcert1 && tcert1->cbCertEncoded == cert1->cbCertEncoded,
582 "cert1 expected in the collection got %p, %x\n",tcert1, GetLastError());
583 tcert1 = CertEnumCertificatesInStore(collection, tcert1);
584 ok (tcert1==NULL,"Unexpected cert in the collection %p %x\n",tcert1, GetLastError());
585
586 /* checking whether certs had been saved */
587 tstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
588 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, WineTestRW_W);
589 ok (tstore!=NULL, "Failed to open existing rw store\n");
590 tcert1 = CertEnumCertificatesInStore(tstore, NULL);
591 todo_wine
592 ok(tcert1 && tcert1->cbCertEncoded == cert1->cbCertEncoded, "cert1 wasn't saved\n");
593 CertFreeCertificateContext(tcert1);
594 CertCloseStore(tstore,0);
595
596 tstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
597 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, WineTestRW2_W);
598 ok (tstore!=NULL, "Failed to open existing rw2 store\n");
599 tcert1 = CertEnumCertificatesInStore(tstore, NULL);
600 todo_wine
601 ok (tcert1 && tcert1->cbCertEncoded == cert2->cbCertEncoded, "cert2 wasn't saved\n");
602 CertFreeCertificateContext(tcert1);
603 CertCloseStore(tstore,0);
604
605 CertCloseStore(collection,0);
606 CertCloseStore(ro_store,0);
607 CertCloseStore(rw_store,0);
608 CertCloseStore(rw_store_2,0);
609
610 /* reopening registry stores to check whether certs had been saved */
611 rw_store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
612 CERT_SYSTEM_STORE_CURRENT_USER, WineTestRW_W);
613 tcert1 = CertEnumCertificatesInStore(rw_store, NULL);
614 ok (tcert1 && tcert1->cbCertEncoded == cert1->cbCertEncoded,
615 "Unexpected cert in store %p\n", tcert1);
616 CertFreeCertificateContext(tcert1);
617 CertCloseStore(rw_store,0);
618
619 rw_store_2 = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
620 CERT_SYSTEM_STORE_CURRENT_USER, WineTestRW2_W);
621 tcert1 = CertEnumCertificatesInStore(rw_store_2, NULL);
622 ok (tcert1 && tcert1->cbCertEncoded == cert2->cbCertEncoded,
623 "Unexpected cert in store %p\n", tcert1);
624 CertFreeCertificateContext(tcert1);
625 CertCloseStore(rw_store_2,0);
626
627 CertFreeCertificateContext(cert1);
628 CertFreeCertificateContext(cert2);
629 CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
630 CERT_STORE_DELETE_FLAG|CERT_SYSTEM_STORE_CURRENT_USER,WineTestRO_W);
631 CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
632 CERT_STORE_DELETE_FLAG|CERT_SYSTEM_STORE_CURRENT_USER,WineTestRW_W);
633 CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,0,0,
634 CERT_STORE_DELETE_FLAG|CERT_SYSTEM_STORE_CURRENT_USER,WineTestRW2_W);
635
636 }
637
638 static void testCollectionStore(void)
639 {
640 HCERTSTORE store1, store2, collection, collection2;
641 PCCERT_CONTEXT context;
642 BOOL ret;
643 static const WCHAR szPrefix[] = { 'c','e','r',0 };
644 static const WCHAR szDot[] = { '.',0 };
645 WCHAR filename[MAX_PATH];
646 HANDLE file;
647
648 if (!pCertAddStoreToCollection)
649 {
650 win_skip("CertAddStoreToCollection() is not available\n");
651 return;
652 }
653
654 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
655 CERT_STORE_CREATE_NEW_FLAG, NULL);
656
657 /* Try adding a cert to any empty collection */
658 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
659 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
660 ok(!ret && GetLastError() == E_ACCESSDENIED,
661 "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
662
663 /* Create and add a cert to a memory store */
664 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
665 CERT_STORE_CREATE_NEW_FLAG, NULL);
666 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
667 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
668 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
669 /* Add the memory store to the collection, without allowing adding */
670 ret = pCertAddStoreToCollection(collection, store1, 0, 0);
671 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
672 /* Verify the cert is in the collection */
673 context = CertEnumCertificatesInStore(collection, NULL);
674 ok(context != NULL, "Expected a valid context\n");
675 if (context)
676 {
677 ok(context->hCertStore == collection, "Unexpected store\n");
678 CertFreeCertificateContext(context);
679 }
680 /* Check that adding to the collection isn't allowed */
681 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
682 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
683 ok(!ret && GetLastError() == E_ACCESSDENIED,
684 "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
685
686 /* Create a new memory store */
687 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
688 CERT_STORE_CREATE_NEW_FLAG, NULL);
689 /* Try adding a store to a non-collection store */
690 ret = pCertAddStoreToCollection(store1, store2,
691 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
692 ok(!ret && GetLastError() == E_INVALIDARG,
693 "Expected E_INVALIDARG, got %08x\n", GetLastError());
694 /* Try adding some bogus stores */
695 /* This crashes in Windows
696 ret = pCertAddStoreToCollection(0, store2,
697 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
698 */
699 /* This "succeeds"... */
700 ret = pCertAddStoreToCollection(collection, 0,
701 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
702 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
703 /* while this crashes.
704 ret = pCertAddStoreToCollection(collection, 1,
705 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
706 */
707
708 /* Add it to the collection, this time allowing adding */
709 ret = pCertAddStoreToCollection(collection, store2,
710 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
711 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
712 /* Check that adding to the collection is allowed */
713 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
714 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
715 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
716 /* Now check that it was actually added to store2 */
717 context = CertEnumCertificatesInStore(store2, NULL);
718 ok(context != NULL, "Expected a valid context\n");
719 if (context)
720 {
721 ok(context->hCertStore == store2, "Unexpected store\n");
722 CertFreeCertificateContext(context);
723 }
724 /* Check that the collection has both bigCert and bigCert2. bigCert comes
725 * first because store1 was added first.
726 */
727 context = CertEnumCertificatesInStore(collection, NULL);
728 ok(context != NULL, "Expected a valid context\n");
729 if (context)
730 {
731 ok(context->hCertStore == collection, "Unexpected store\n");
732 ok(context->cbCertEncoded == sizeof(bigCert),
733 "Wrong size %d\n", context->cbCertEncoded);
734 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
735 "Unexpected cert\n");
736 context = CertEnumCertificatesInStore(collection, context);
737 ok(context != NULL, "Expected a valid context\n");
738 if (context)
739 {
740 ok(context->hCertStore == collection, "Unexpected store\n");
741 ok(context->cbCertEncoded == sizeof(bigCert2),
742 "Wrong size %d\n", context->cbCertEncoded);
743 ok(!memcmp(context->pbCertEncoded, bigCert2,
744 context->cbCertEncoded), "Unexpected cert\n");
745 context = CertEnumCertificatesInStore(collection, context);
746 ok(!context, "Unexpected cert\n");
747 }
748 }
749 /* close store2, and check that the collection is unmodified */
750 CertCloseStore(store2, 0);
751 context = CertEnumCertificatesInStore(collection, NULL);
752 ok(context != NULL, "Expected a valid context\n");
753 if (context)
754 {
755 ok(context->hCertStore == collection, "Unexpected store\n");
756 ok(context->cbCertEncoded == sizeof(bigCert),
757 "Wrong size %d\n", context->cbCertEncoded);
758 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
759 "Unexpected cert\n");
760 context = CertEnumCertificatesInStore(collection, context);
761 ok(context != NULL, "Expected a valid context\n");
762 if (context)
763 {
764 ok(context->hCertStore == collection, "Unexpected store\n");
765 ok(context->cbCertEncoded == sizeof(bigCert2),
766 "Wrong size %d\n", context->cbCertEncoded);
767 ok(!memcmp(context->pbCertEncoded, bigCert2,
768 context->cbCertEncoded), "Unexpected cert\n");
769 context = CertEnumCertificatesInStore(collection, context);
770 ok(!context, "Unexpected cert\n");
771 }
772 }
773
774 /* Adding a collection to a collection is legal */
775 collection2 = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
776 CERT_STORE_CREATE_NEW_FLAG, NULL);
777 ret = pCertAddStoreToCollection(collection2, collection,
778 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
779 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
780 /* check the contents of collection2 */
781 context = CertEnumCertificatesInStore(collection2, NULL);
782 ok(context != NULL, "Expected a valid context\n");
783 if (context)
784 {
785 ok(context->hCertStore == collection2, "Unexpected store\n");
786 ok(context->cbCertEncoded == sizeof(bigCert),
787 "Wrong size %d\n", context->cbCertEncoded);
788 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
789 "Unexpected cert\n");
790 context = CertEnumCertificatesInStore(collection2, context);
791 ok(context != NULL, "Expected a valid context\n");
792 if (context)
793 {
794 ok(context->hCertStore == collection2, "Unexpected store\n");
795 ok(context->cbCertEncoded == sizeof(bigCert2),
796 "Wrong size %d\n", context->cbCertEncoded);
797 ok(!memcmp(context->pbCertEncoded, bigCert2,
798 context->cbCertEncoded), "Unexpected cert\n");
799 context = CertEnumCertificatesInStore(collection2, context);
800 ok(!context, "Unexpected cert\n");
801 }
802 }
803
804 /* I'd like to test closing the collection in the middle of enumeration,
805 * but my tests have been inconsistent. The first time calling
806 * CertEnumCertificatesInStore on a closed collection succeeded, while the
807 * second crashed. So anything appears to be fair game.
808 * I'd also like to test removing a store from a collection in the middle
809 * of an enumeration, but my tests in Windows have been inconclusive.
810 * In one scenario it worked. In another scenario, about a third of the
811 * time this leads to "random" crashes elsewhere in the code. This
812 * probably means this is not allowed.
813 */
814
815 CertCloseStore(store1, 0);
816 CertCloseStore(collection, 0);
817 CertCloseStore(collection2, 0);
818
819 /* Add the same cert to two memory stores, then put them in a collection */
820 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
821 CERT_STORE_CREATE_NEW_FLAG, NULL);
822 ok(store1 != 0, "CertOpenStore failed: %08x\n", GetLastError());
823 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
824 CERT_STORE_CREATE_NEW_FLAG, NULL);
825 ok(store2 != 0, "CertOpenStore failed: %08x\n", GetLastError());
826
827 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
828 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
829 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
830 ret = CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING,
831 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
832 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
833 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
834 CERT_STORE_CREATE_NEW_FLAG, NULL);
835 ok(collection != 0, "CertOpenStore failed: %08x\n", GetLastError());
836
837 ret = pCertAddStoreToCollection(collection, store1,
838 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
839 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
840 ret = pCertAddStoreToCollection(collection, store2,
841 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
842 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
843
844 /* Check that the collection has two copies of the same cert */
845 context = CertEnumCertificatesInStore(collection, NULL);
846 ok(context != NULL, "Expected a valid context\n");
847 if (context)
848 {
849 ok(context->hCertStore == collection, "Unexpected store\n");
850 ok(context->cbCertEncoded == sizeof(bigCert),
851 "Wrong size %d\n", context->cbCertEncoded);
852 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
853 "Unexpected cert\n");
854 context = CertEnumCertificatesInStore(collection, context);
855 ok(context != NULL, "Expected a valid context\n");
856 if (context)
857 {
858 ok(context->hCertStore == collection, "Unexpected store\n");
859 ok(context->cbCertEncoded == sizeof(bigCert),
860 "Wrong size %d\n", context->cbCertEncoded);
861 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
862 "Unexpected cert\n");
863 context = CertEnumCertificatesInStore(collection, context);
864 ok(context == NULL, "Unexpected cert\n");
865 }
866 }
867
868 /* The following would check whether I can delete an identical cert, rather
869 * than one enumerated from the store. It crashes, so that means I must
870 * only call CertDeleteCertificateFromStore with contexts enumerated from
871 * the store.
872 context = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
873 sizeof(bigCert));
874 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
875 GetLastError());
876 if (context)
877 {
878 ret = CertDeleteCertificateFromStore(collection, context);
879 printf("ret is %d, GetLastError is %08x\n", ret, GetLastError());
880 CertFreeCertificateContext(context);
881 }
882 */
883
884 /* Now check deleting from the collection. */
885 context = CertEnumCertificatesInStore(collection, NULL);
886 ok(context != NULL, "Expected a valid context\n");
887 if (context)
888 {
889 CertDeleteCertificateFromStore(context);
890 /* store1 should now be empty */
891 context = CertEnumCertificatesInStore(store1, NULL);
892 ok(!context, "Unexpected cert\n");
893 /* and there should be one certificate in the collection */
894 context = CertEnumCertificatesInStore(collection, NULL);
895 ok(context != NULL, "Expected a valid cert\n");
896 if (context)
897 {
898 ok(context->hCertStore == collection, "Unexpected store\n");
899 ok(context->cbCertEncoded == sizeof(bigCert),
900 "Wrong size %d\n", context->cbCertEncoded);
901 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
902 "Unexpected cert\n");
903 }
904 context = CertEnumCertificatesInStore(collection, context);
905 ok(context == NULL, "Unexpected cert\n");
906 }
907
908 if (!pCertRemoveStoreFromCollection)
909 {
910 win_skip("CertRemoveStoreFromCollection() is not available\n");
911 }
912 else
913 {
914 /* Finally, test removing stores from the collection. No return
915 * value, so it's a bit funny to test.
916 */
917 /* This crashes
918 * pCertRemoveStoreFromCollection(NULL, NULL);
919 */
920 /* This "succeeds," no crash, no last error set */
921 SetLastError(0xdeadbeef);
922 pCertRemoveStoreFromCollection(store2, collection);
923 ok(GetLastError() == 0xdeadbeef,
924 "Didn't expect an error to be set: %08x\n", GetLastError());
925
926 /* After removing store2, the collection should be empty */
927 SetLastError(0xdeadbeef);
928 pCertRemoveStoreFromCollection(collection, store2);
929 ok(GetLastError() == 0xdeadbeef,
930 "Didn't expect an error to be set: %08x\n", GetLastError());
931 context = CertEnumCertificatesInStore(collection, NULL);
932 ok(!context, "Unexpected cert\n");
933 }
934
935 CertCloseStore(collection, 0);
936 CertCloseStore(store2, 0);
937 CertCloseStore(store1, 0);
938
939 /* Test adding certificates to and deleting certificates from collections.
940 */
941 store1 = CertOpenSystemStoreA(0, "My");
942 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
943 CERT_STORE_CREATE_NEW_FLAG, NULL);
944
945 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
946 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
947 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
948 CertDeleteCertificateFromStore(context);
949
950 pCertAddStoreToCollection(collection, store1,
951 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
952
953 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
954 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
955 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
956 CertDeleteCertificateFromStore(context);
957
958 CertCloseStore(collection, 0);
959 CertCloseStore(store1, 0);
960
961 /* Test whether a collection store can be committed */
962 if (!pCertControlStore)
963 {
964 win_skip("CertControlStore() is not available\n");
965 return;
966 }
967 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
968 CERT_STORE_CREATE_NEW_FLAG, NULL);
969
970 SetLastError(0xdeadbeef);
971 ret = pCertControlStore(collection, 0, CERT_STORE_CTRL_COMMIT, NULL);
972 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
973
974 /* Adding a mem store that can't be committed prevents a successful commit.
975 */
976 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
977 CERT_STORE_CREATE_NEW_FLAG, NULL);
978 pCertAddStoreToCollection(collection, store1, 0, 0);
979 SetLastError(0xdeadbeef);
980 ret = pCertControlStore(collection, 0, CERT_STORE_CTRL_COMMIT, NULL);
981 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
982 "expected ERROR_CALL_NOT_IMPLEMENTED, got %d\n", GetLastError());
983 pCertRemoveStoreFromCollection(collection, store1);
984 CertCloseStore(store1, 0);
985
986 /* Test adding a cert to a collection with a file store, committing the
987 * change to the collection, and comparing the resulting file.
988 */
989 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
990 return;
991
992 DeleteFileW(filename);
993 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
994 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
995 if (file == INVALID_HANDLE_VALUE)
996 return;
997
998 store1 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
999 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1000 ok(store1 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1001 CloseHandle(file);
1002 pCertAddStoreToCollection(collection, store1,
1003 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1004 CertCloseStore(store1, 0);
1005
1006 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
1007 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1008 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1009 GetLastError());
1010 ret = pCertControlStore(collection, 0, CERT_STORE_CTRL_COMMIT, NULL);
1011 ok(ret, "CertControlStore failed: %d\n", ret);
1012 compareStore(collection, "serialized store with cert",
1013 serializedStoreWithCert, sizeof(serializedStoreWithCert), FALSE);
1014 CertCloseStore(collection, 0);
1015
1016 DeleteFileW(filename);
1017 }
1018
1019 /* Looks for the property with ID propID in the buffer buf. Returns a pointer
1020 * to its header if found, NULL if not.
1021 */
1022 static const struct CertPropIDHeader *findPropID(const BYTE *buf, DWORD size,
1023 DWORD propID)
1024 {
1025 const struct CertPropIDHeader *ret = NULL;
1026 BOOL failed = FALSE;
1027
1028 while (size && !ret && !failed)
1029 {
1030 if (size < sizeof(struct CertPropIDHeader))
1031 failed = TRUE;
1032 else
1033 {
1034 const struct CertPropIDHeader *hdr =
1035 (const struct CertPropIDHeader *)buf;
1036
1037 size -= sizeof(struct CertPropIDHeader);
1038 buf += sizeof(struct CertPropIDHeader);
1039 if (size < hdr->cb)
1040 failed = TRUE;
1041 else if (hdr->propID == propID)
1042 ret = hdr;
1043 else
1044 {
1045 buf += hdr->cb;
1046 size -= hdr->cb;
1047 }
1048 }
1049 }
1050 return ret;
1051 }
1052
1053 static void testRegStore(void)
1054 {
1055 static const char tempKey[] = "Software\\Wine\\CryptTemp";
1056 HCERTSTORE store;
1057 LONG rc;
1058 HKEY key = NULL;
1059 DWORD disp, GLE;
1060
1061 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, NULL);
1062 GLE = GetLastError();
1063 ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
1064 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
1065 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
1066 GLE = GetLastError();
1067 ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
1068 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
1069
1070 /* Opening up any old key works.. */
1071 key = HKEY_CURRENT_USER;
1072 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
1073 /* Not sure if this is a bug in DuplicateHandle, marking todo_wine for now
1074 */
1075 todo_wine ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1076 CertCloseStore(store, 0);
1077
1078 /* It looks like the remainder pretty much needs CertControlStore() */
1079 if (!pCertControlStore)
1080 {
1081 win_skip("CertControlStore() is not available\n");
1082 return;
1083 }
1084
1085 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
1086 NULL, &key, NULL);
1087 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
1088 if (key)
1089 {
1090 BOOL ret;
1091 BYTE hash[20];
1092 DWORD size, i;
1093 static const char certificates[] = "Certificates\\";
1094 char subKeyName[sizeof(certificates) + 20 * 2 + 1], *ptr;
1095 HKEY subKey;
1096 PCCERT_CONTEXT context;
1097
1098 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
1099 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1100 /* Add a certificate. It isn't persisted right away, since it's only
1101 * added to the cache..
1102 */
1103 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1104 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
1105 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1106 GetLastError());
1107 /* so flush the cache to force a commit.. */
1108 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1109 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
1110 /* and check that the expected subkey was written. */
1111 size = sizeof(hash);
1112 ret = CryptHashCertificate(0, 0, 0, bigCert2, sizeof(bigCert2),
1113 hash, &size);
1114 ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
1115 strcpy(subKeyName, certificates);
1116 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1; i < size;
1117 i++, ptr += 2)
1118 sprintf(ptr, "%02X", hash[i]);
1119 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1120 &subKey, NULL);
1121 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
1122 if (subKey)
1123 {
1124 LPBYTE buf;
1125
1126 size = 0;
1127 RegQueryValueExA(subKey, "Blob", NULL, NULL, NULL, &size);
1128 buf = HeapAlloc(GetProcessHeap(), 0, size);
1129 if (buf)
1130 {
1131 rc = RegQueryValueExA(subKey, "Blob", NULL, NULL, buf, &size);
1132 ok(!rc, "RegQueryValueExA failed: %d\n", rc);
1133 if (!rc)
1134 {
1135 const struct CertPropIDHeader *hdr;
1136
1137 /* Both the hash and the cert should be present */
1138 hdr = findPropID(buf, size, CERT_CERT_PROP_ID);
1139 ok(hdr != NULL, "Expected to find a cert property\n");
1140 if (hdr)
1141 {
1142 ok(hdr->cb == sizeof(bigCert2),
1143 "Wrong size %d of cert property\n", hdr->cb);
1144 ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), bigCert2,
1145 hdr->cb), "Unexpected cert in cert property\n");
1146 }
1147 hdr = findPropID(buf, size, CERT_HASH_PROP_ID);
1148 ok(hdr != NULL, "Expected to find a hash property\n");
1149 if (hdr)
1150 {
1151 ok(hdr->cb == sizeof(hash),
1152 "Wrong size %d of hash property\n", hdr->cb);
1153 ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), hash,
1154 hdr->cb), "Unexpected hash in cert property\n");
1155 }
1156 }
1157 HeapFree(GetProcessHeap(), 0, buf);
1158 }
1159 RegCloseKey(subKey);
1160 }
1161
1162 /* Remove the existing context */
1163 context = CertEnumCertificatesInStore(store, NULL);
1164 ok(context != NULL, "Expected a cert context\n");
1165 if (context)
1166 CertDeleteCertificateFromStore(context);
1167 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1168 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
1169
1170 /* Add a serialized cert with a bogus hash directly to the registry */
1171 memset(hash, 0, sizeof(hash));
1172 strcpy(subKeyName, certificates);
1173 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
1174 i < sizeof(hash); i++, ptr += 2)
1175 sprintf(ptr, "%02X", hash[i]);
1176 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1177 &subKey, NULL);
1178 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
1179 if (subKey)
1180 {
1181 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
1182 sizeof(bigCert)], *ptr;
1183 DWORD certCount = 0;
1184 struct CertPropIDHeader *hdr;
1185
1186 hdr = (struct CertPropIDHeader *)buf;
1187 hdr->propID = CERT_HASH_PROP_ID;
1188 hdr->unknown1 = 1;
1189 hdr->cb = sizeof(hash);
1190 ptr = buf + sizeof(*hdr);
1191 memcpy(ptr, hash, sizeof(hash));
1192 ptr += sizeof(hash);
1193 hdr = (struct CertPropIDHeader *)ptr;
1194 hdr->propID = CERT_CERT_PROP_ID;
1195 hdr->unknown1 = 1;
1196 hdr->cb = sizeof(bigCert);
1197 ptr += sizeof(*hdr);
1198 memcpy(ptr, bigCert, sizeof(bigCert));
1199
1200 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
1201 sizeof(buf));
1202 ok(!rc, "RegSetValueExA failed: %d\n", rc);
1203
1204 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
1205 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
1206
1207 /* Make sure the bogus hash cert gets loaded. */
1208 certCount = 0;
1209 context = NULL;
1210 do {
1211 context = CertEnumCertificatesInStore(store, context);
1212 if (context)
1213 certCount++;
1214 } while (context != NULL);
1215 ok(certCount == 1, "Expected 1 certificates, got %d\n", certCount);
1216
1217 RegCloseKey(subKey);
1218 }
1219
1220 /* Add another serialized cert directly to the registry, this time
1221 * under the correct key name (named with the correct hash value).
1222 */
1223 size = sizeof(hash);
1224 ret = CryptHashCertificate(0, 0, 0, bigCert2,
1225 sizeof(bigCert2), hash, &size);
1226 ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
1227 strcpy(subKeyName, certificates);
1228 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
1229 i < sizeof(hash); i++, ptr += 2)
1230 sprintf(ptr, "%02X", hash[i]);
1231 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1232 &subKey, NULL);
1233 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
1234 if (subKey)
1235 {
1236 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
1237 sizeof(bigCert2)], *ptr;
1238 DWORD certCount = 0;
1239 PCCERT_CONTEXT context;
1240 struct CertPropIDHeader *hdr;
1241
1242 /* First try with a bogus hash... */
1243 hdr = (struct CertPropIDHeader *)buf;
1244 hdr->propID = CERT_HASH_PROP_ID;
1245 hdr->unknown1 = 1;
1246 hdr->cb = sizeof(hash);
1247 ptr = buf + sizeof(*hdr);
1248 memset(ptr, 0, sizeof(hash));
1249 ptr += sizeof(hash);
1250 hdr = (struct CertPropIDHeader *)ptr;
1251 hdr->propID = CERT_CERT_PROP_ID;
1252 hdr->unknown1 = 1;
1253 hdr->cb = sizeof(bigCert2);
1254 ptr += sizeof(*hdr);
1255 memcpy(ptr, bigCert2, sizeof(bigCert2));
1256
1257 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
1258 sizeof(buf));
1259 ok(!rc, "RegSetValueExA failed: %d\n", rc);
1260
1261 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
1262 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
1263
1264 /* and make sure just one cert still gets loaded. */
1265 certCount = 0;
1266 context = NULL;
1267 do {
1268 context = CertEnumCertificatesInStore(store, context);
1269 if (context)
1270 certCount++;
1271 } while (context != NULL);
1272 ok(certCount == 1 ||
1273 broken(certCount == 2) /* NT4 */ ,
1274 "Expected 1 certificates, got %d\n", certCount);
1275
1276 /* Try again with the correct hash... */
1277 ptr = buf + sizeof(*hdr);
1278 memcpy(ptr, hash, sizeof(hash));
1279
1280 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
1281 sizeof(buf));
1282 ok(!rc, "RegSetValueExA failed: %d\n", rc);
1283
1284 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
1285 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
1286
1287 /* and make sure two certs get loaded. */
1288 certCount = 0;
1289 context = NULL;
1290 do {
1291 context = CertEnumCertificatesInStore(store, context);
1292 if (context)
1293 certCount++;
1294 } while (context != NULL);
1295 ok(certCount == 2, "Expected 2 certificates, got %d\n", certCount);
1296
1297 RegCloseKey(subKey);
1298 }
1299 CertCloseStore(store, 0);
1300 /* Is delete allowed on a reg store? */
1301 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0,
1302 CERT_STORE_DELETE_FLAG, key);
1303 ok(store == NULL, "Expected NULL return from CERT_STORE_DELETE_FLAG\n");
1304 ok(GetLastError() == 0, "CertOpenStore failed: %08x\n",
1305 GetLastError());
1306
1307 RegCloseKey(key);
1308 }
1309 /* The CertOpenStore with CERT_STORE_DELETE_FLAG above will delete the
1310 * contents of the key, but not the key itself.
1311 */
1312 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
1313 NULL, &key, &disp);
1314 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
1315 ok(disp == REG_OPENED_EXISTING_KEY,
1316 "Expected REG_OPENED_EXISTING_KEY, got %d\n", disp);
1317 if (!rc)
1318 {
1319 RegCloseKey(key);
1320 rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey);
1321 if (rc)
1322 {
1323 HMODULE shlwapi = LoadLibraryA("shlwapi");
1324
1325 /* Use shlwapi's SHDeleteKeyA to _really_ blow away the key,
1326 * otherwise subsequent tests will fail.
1327 */
1328 if (shlwapi)
1329 {
1330 DWORD (WINAPI *pSHDeleteKeyA)(HKEY, LPCSTR);
1331
1332 pSHDeleteKeyA = (void*)GetProcAddress(shlwapi, "SHDeleteKeyA");
1333 if (pSHDeleteKeyA)
1334 pSHDeleteKeyA(HKEY_CURRENT_USER, tempKey);
1335 FreeLibrary(shlwapi);
1336 }
1337 }
1338 }
1339 }
1340
1341 static const char MyA[] = { 'M','y',0,0 };
1342 static const WCHAR MyW[] = { 'M','y',0 };
1343 static const WCHAR BogusW[] = { 'B','o','g','u','s',0 };
1344 static const WCHAR BogusPathW[] = { 'S','o','f','t','w','a','r','e','\\',
1345 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m','C','e','r',
1346 't','i','f','i','c','a','t','e','s','\\','B','o','g','u','s',0 };
1347
1348 static void testSystemRegStore(void)
1349 {
1350 HCERTSTORE store, memStore;
1351
1352 /* Check with a UNICODE name */
1353 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1354 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
1355 /* Not all OSes support CERT_STORE_PROV_SYSTEM_REGISTRY, so don't continue
1356 * testing if they don't.
1357 */
1358 if (!store)
1359 return;
1360
1361 /* Check that it isn't a collection store */
1362 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1363 CERT_STORE_CREATE_NEW_FLAG, NULL);
1364 if (memStore)
1365 {
1366 if (pCertAddStoreToCollection)
1367 {
1368 BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
1369 ok(!ret && GetLastError() == E_INVALIDARG,
1370 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1371 }
1372 CertCloseStore(memStore, 0);
1373 }
1374 CertCloseStore(store, 0);
1375
1376 /* Check opening a bogus store */
1377 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1378 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
1379 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1380 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1381 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1382 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
1383 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1384 if (store)
1385 CertCloseStore(store, 0);
1386 /* Now check whether deleting is allowed */
1387 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1388 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1389 ok(!store, "CertOpenStore failed: %08x\n", GetLastError());
1390 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1391
1392 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0, 0, NULL);
1393 ok(!store && GetLastError() == E_INVALIDARG,
1394 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1395 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1396 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
1397 ok(!store && GetLastError() == E_INVALIDARG,
1398 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1399 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1400 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
1401 ok(!store && GetLastError() == E_INVALIDARG,
1402 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1403 /* The name is expected to be UNICODE, check with an ASCII name */
1404 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1405 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
1406 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1407 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1408 }
1409
1410 static void testSystemStore(void)
1411 {
1412 static const WCHAR baskslashW[] = { '\\',0 };
1413 HCERTSTORE store;
1414 WCHAR keyName[MAX_PATH];
1415 HKEY key;
1416 LONG rc;
1417
1418 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, 0, NULL);
1419 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1420 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1421 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1422 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
1423 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1424 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1425 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1426 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
1427 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1428 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1429 /* The name is expected to be UNICODE, first check with an ASCII name */
1430 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1431 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
1432 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1433 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1434 /* Create the expected key */
1435 lstrcpyW(keyName, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH);
1436 lstrcatW(keyName, baskslashW);
1437 lstrcatW(keyName, MyW);
1438 rc = RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
1439 NULL, &key, NULL);
1440 ok(!rc, "RegCreateKeyEx failed: %d\n", rc);
1441 if (!rc)
1442 RegCloseKey(key);
1443 /* Check opening with a UNICODE name, specifying the create new flag */
1444 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1445 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_CREATE_NEW_FLAG, MyW);
1446 ok(!store && GetLastError() == ERROR_FILE_EXISTS,
1447 "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
1448 /* Now check opening with a UNICODE name, this time opening existing */
1449 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1450 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
1451 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1452 if (store)
1453 {
1454 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1455 CERT_STORE_CREATE_NEW_FLAG, NULL);
1456
1457 /* Check that it's a collection store */
1458 if (memStore)
1459 {
1460 if (pCertAddStoreToCollection)
1461 {
1462 BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
1463 /* FIXME: this'll fail on NT4, but what error will it give? */
1464 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
1465 }
1466 CertCloseStore(memStore, 0);
1467 }
1468 CertCloseStore(store, 0);
1469 }
1470
1471 /* Check opening a bogus store */
1472 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1473 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
1474 ok(!store, "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1475 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1476 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
1477 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1478 if (store)
1479 CertCloseStore(store, 0);
1480 /* Now check whether deleting is allowed */
1481 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1482 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1483 ok(!store, "Didn't expect a store to be returned when deleting\n");
1484 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1485 }
1486
1487 static const BYTE serializedStoreWithCertAndCRL[] = {
1488 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1489 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1490 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1491 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1492 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1493 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1494 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1495 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1496 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1497 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x21,0x00,0x00,0x00,0x01,0x00,
1498 0x00,0x00,0x47,0x00,0x00,0x00,0x30,0x45,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,
1499 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1500 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1501 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x03,0x11,
1502 0x00,0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,
1503 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1504
1505 static void testFileStore(void)
1506 {
1507 static const WCHAR szPrefix[] = { 'c','e','r',0 };
1508 static const WCHAR szDot[] = { '.',0 };
1509 WCHAR filename[MAX_PATH];
1510 HCERTSTORE store;
1511 BOOL ret;
1512 PCCERT_CONTEXT cert;
1513 HANDLE file;
1514
1515 if (!pCertControlStore)
1516 {
1517 win_skip("CertControlStore() is not available\n");
1518 return;
1519 }
1520
1521 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, 0, NULL);
1522 ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
1523 "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1524
1525 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1526 return;
1527
1528 DeleteFileW(filename);
1529 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1530 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1531 if (file == INVALID_HANDLE_VALUE)
1532 return;
1533
1534 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, CERT_STORE_DELETE_FLAG,
1535 file);
1536 ok(!store && GetLastError() == E_INVALIDARG,
1537 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1538 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1539 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, file);
1540 ok(!store && GetLastError() == E_INVALIDARG,
1541 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1542
1543 /* A "read-only" file store.. */
1544 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1545 CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG, file);
1546 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1547 if (store)
1548 {
1549 DWORD size;
1550
1551 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1552 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1553 /* apparently allows adding certificates.. */
1554 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1555 /* but not commits.. */
1556 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1557 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1558 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1559 /* It still has certs in memory.. */
1560 cert = CertEnumCertificatesInStore(store, NULL);
1561 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1562 GetLastError());
1563 CertFreeCertificateContext(cert);
1564 /* but the file size is still 0. */
1565 size = GetFileSize(file, NULL);
1566 ok(size == 0, "Expected size 0, got %d\n", size);
1567 CertCloseStore(store, 0);
1568 }
1569
1570 /* The create new flag is allowed.. */
1571 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1572 CERT_STORE_CREATE_NEW_FLAG, file);
1573 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1574 if (store)
1575 {
1576 /* but without the commit enable flag, commits don't happen. */
1577 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1578 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1579 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1580 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1581 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1582 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1583 CertCloseStore(store, 0);
1584 }
1585 /* as is the open existing flag. */
1586 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1587 CERT_STORE_OPEN_EXISTING_FLAG, file);
1588 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1589 if (store)
1590 {
1591 /* but without the commit enable flag, commits don't happen. */
1592 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1593 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1594 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1595 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1596 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1597 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1598 CertCloseStore(store, 0);
1599 }
1600 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1601 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1602 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1603 if (store)
1604 {
1605 CloseHandle(file);
1606 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1607 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1608 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1609 GetLastError());
1610 /* with commits enabled, commit is allowed */
1611 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1612 ok(ret, "CertControlStore failed: %d\n", ret);
1613 compareStore(store, "serialized store with cert",
1614 serializedStoreWithCert, sizeof(serializedStoreWithCert), FALSE);
1615 CertCloseStore(store, 0);
1616 }
1617 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1618 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1619 if (file == INVALID_HANDLE_VALUE)
1620 return;
1621 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1622 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1623 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1624 if (store)
1625 {
1626 CloseHandle(file);
1627 ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING, signedCRL,
1628 sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1629 ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1630 compareStore(store, "serialized store with cert and CRL",
1631 serializedStoreWithCertAndCRL, sizeof(serializedStoreWithCertAndCRL),
1632 FALSE);
1633 CertCloseStore(store, 0);
1634 }
1635
1636 DeleteFileW(filename);
1637 }
1638
1639 static BOOL initFileFromData(LPCWSTR filename, const BYTE *pb, DWORD cb)
1640 {
1641 HANDLE file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1642 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1643 BOOL ret;
1644
1645 if (file != INVALID_HANDLE_VALUE)
1646 {
1647 DWORD written;
1648
1649 ret = WriteFile(file, pb, cb, &written, NULL);
1650 CloseHandle(file);
1651 }
1652 else
1653 ret = FALSE;
1654 return ret;
1655 }
1656
1657 static const BYTE base64SPC[] =
1658 "MIICJQYJKoZIhvcNAQcCoIICFjCCAhICAQExADALBgkqhkiG9w0BBwGgggH6MIIB"
1659 "9jCCAV+gAwIBAgIQnP8+EF4opr9OxH7h4uBPWTANBgkqhkiG9w0BAQQFADAUMRIw"
1660 "EAYDVQQDEwlKdWFuIExhbmcwHhcNMDgxMjEyMTcxMDE0WhcNMzkxMjMxMjM1OTU5"
1661 "WjAUMRIwEAYDVQQDEwlKdWFuIExhbmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ"
1662 "AoGBALCgNjyNvOic0FOfjxvi43HbM+D5joDkhiGSXe+gbZlf8f16k07kkObFEunz"
1663 "mdB5coscmA7gyqiWNN4ZUyr2cA3lCbnpGPA/0IblyyOcuGIFmmCzeZaVa5ZG6xZP"
1664 "K7L7o+73Qo6jXVbGhBGnMZ7Q9sAn6s2933olnStnejnqwV0NAgMBAAGjSTBHMEUG"
1665 "A1UdAQQ+MDyAEFKbKEdXYyx+CWKcV6vxM6ShFjAUMRIwEAYDVQQDEwlKdWFuIExh"
1666 "bmeCEJz/PhBeKKa/TsR+4eLgT1kwDQYJKoZIhvcNAQEEBQADgYEALpkgLgW3mEaK"
1667 "idPQ3iPJYLG0Ub1wraqEl9bd42hrhzIdcDzlQgxnm8/5cHYVxIF/C20x/HJplb1R"
1668 "G6U1ipFe/q8byWD/9JpiBKMGPi9YlUTgXHfS9d4S/QWO1h9Z7KeipBYhoslQpHXu"
1669 "y9bUr8Adqi6SzgHpCnMu53dxgxUD1r4xAA==";
1670 /* Same as base64SPC, but as a wide-char string */
1671 static const WCHAR utf16Base64SPC[] = {
1672 'M','I','I','C','J','Q','Y','J','K','o','Z','I','h','v','c','N','A',
1673 'Q','c','C','o','I','I','C','F','j','C','C','A','h','I','C','A','Q',
1674 'E','x','A','D','A','L','B','g','k','q','h','k','i','G','9','w','0',
1675 'B','B','w','G','g','g','g','H','6','M','I','I','B','9','j','C','C',
1676 'A','V','+','g','A','w','I','B','A','g','I','Q','n','P','8','+','E',
1677 'F','4','o','p','r','9','O','x','H','7','h','4','u','B','P','W','T',
1678 'A','N','B','g','k','q','h','k','i','G','9','w','0','B','A','Q','Q',
1679 'F','A','D','A','U','M','R','I','w','E','A','Y','D','V','Q','Q','D',
1680 'E','w','l','K','d','W','F','u','I','E','x','h','b','m','c','w','H',
1681 'h','c','N','M','D','g','x','M','j','E','y','M','T','c','x','M','D',
1682 'E','0','W','h','c','N','M','z','k','x','M','j','M','x','M','j','M',
1683 '1','O','T','U','5','W','j','A','U','M','R','I','w','E','A','Y','D',
1684 'V','Q','Q','D','E','w','l','K','d','W','F','u','I','E','x','h','b',
1685 'm','c','w','g','Z','8','w','D','Q','Y','J','K','o','Z','I','h','v',
1686 'c','N','A','Q','E','B','B','Q','A','D','g','Y','0','A','M','I','G',
1687 'J','A','o','G','B','A','L','C','g','N','j','y','N','v','O','i','c',
1688 '0','F','O','f','j','x','v','i','4','3','H','b','M','+','D','5','j',
1689 'o','D','k','h','i','G','S','X','e','+','g','b','Z','l','f','8','f',
1690 '1','6','k','0','7','k','k','O','b','F','E','u','n','z','m','d','B',
1691 '5','c','o','s','c','m','A','7','g','y','q','i','W','N','N','4','Z',
1692 'U','y','r','2','c','A','3','l','C','b','n','p','G','P','A','/','0',
1693 'I','b','l','y','y','O','c','u','G','I','F','m','m','C','z','e','Z',
1694 'a','V','a','5','Z','G','6','x','Z','P','K','7','L','7','o','+','7',
1695 '3','Q','o','6','j','X','V','b','G','h','B','G','n','M','Z','7','Q',
1696 '9','s','A','n','6','s','2','9','3','3','o','l','n','S','t','n','e',
1697 'j','n','q','w','V','0','N','A','g','M','B','A','A','G','j','S','T',
1698 'B','H','M','E','U','G','A','1','U','d','A','Q','Q','+','M','D','y',
1699 'A','E','F','K','b','K','E','d','X','Y','y','x','+','C','W','K','c',
1700 'V','6','v','x','M','6','S','h','F','j','A','U','M','R','I','w','E',
1701 'A','Y','D','V','Q','Q','D','E','w','l','K','d','W','F','u','I','E',
1702 'x','h','b','m','e','C','E','J','z','/','P','h','B','e','K','K','a',
1703 '/','T','s','R','+','4','e','L','g','T','1','k','w','D','Q','Y','J',
1704 'K','o','Z','I','h','v','c','N','A','Q','E','E','B','Q','A','D','g',
1705 'Y','E','A','L','p','k','g','L','g','W','3','m','E','a','K','i','d',
1706 'P','Q','3','i','P','J','Y','L','G','0','U','b','1','w','r','a','q',
1707 'E','l','9','b','d','4','2','h','r','h','z','I','d','c','D','z','l',
1708 'Q','g','x','n','m','8','/','5','c','H','Y','V','x','I','F','/','C',
1709 '2','0','x','/','H','J','p','l','b','1','R','G','6','U','1','i','p',
1710 'F','e','/','q','8','b','y','W','D','/','9','J','p','i','B','K','M',
1711 'G','P','i','9','Y','l','U','T','g','X','H','f','S','9','d','4','S',
1712 '/','Q','W','O','1','h','9','Z','7','K','e','i','p','B','Y','h','o',
1713 's','l','Q','p','H','X','u','y','9','b','U','r','8','A','d','q','i',
1714 '6','S','z','g','H','p','C','n','M','u','5','3','d','x','g','x','U',
1715 'D','1','r','4','x','A','A','=','=',0 };
1716
1717 static void testFileNameStore(void)
1718 {
1719 static const WCHAR szPrefix[] = { 'c','e','r',0 };
1720 static const WCHAR spcPrefix[] = { 's','p','c',0 };
1721 static const WCHAR szDot[] = { '.',0 };
1722 WCHAR filename[MAX_PATH];
1723 HCERTSTORE store;
1724 BOOL ret;
1725 DWORD GLE;
1726
1727 if (0)
1728 {
1729 /* Crashes on NT4 */
1730 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0, 0, NULL);
1731 GLE = GetLastError();
1732 ok(!store && (GLE == ERROR_PATH_NOT_FOUND || GLE == ERROR_INVALID_PARAMETER),
1733 "Expected ERROR_PATH_NOT_FOUND or ERROR_INVALID_PARAMETER, got %08x\n",
1734 GLE);
1735 }
1736
1737 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1738 return;
1739 DeleteFileW(filename);
1740
1741 /* The two flags are mutually exclusive */
1742 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1743 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, filename);
1744 ok(!store && GetLastError() == E_INVALIDARG,
1745 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1746
1747 /* In all of the following tests, the encoding type seems to be ignored */
1748 if (initFileFromData(filename, bigCert, sizeof(bigCert)))
1749 {
1750 PCCERT_CONTEXT cert;
1751 PCCRL_CONTEXT crl;
1752
1753 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1754 CERT_STORE_READONLY_FLAG, filename);
1755 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1756
1757 cert = CertEnumCertificatesInStore(store, NULL);
1758 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1759 GetLastError());
1760 cert = CertEnumCertificatesInStore(store, cert);
1761 ok(!cert, "Expected only one cert\n");
1762 if (pCertEnumCRLsInStore)
1763 {
1764 crl = pCertEnumCRLsInStore(store, NULL);
1765 ok(!crl, "Expected no CRLs\n");
1766 }
1767
1768 CertCloseStore(store, 0);
1769 DeleteFileW(filename);
1770 }
1771 if (initFileFromData(filename, serializedStoreWithCert,
1772 sizeof(serializedStoreWithCert)))
1773 {
1774 PCCERT_CONTEXT cert;
1775 PCCRL_CONTEXT crl;
1776
1777 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1778 CERT_STORE_READONLY_FLAG, filename);
1779 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1780
1781 cert = CertEnumCertificatesInStore(store, NULL);
1782 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1783 GetLastError());
1784 cert = CertEnumCertificatesInStore(store, cert);
1785 ok(!cert, "Expected only one cert\n");
1786 if (pCertEnumCRLsInStore)
1787 {
1788 crl = pCertEnumCRLsInStore(store, NULL);
1789 ok(!crl, "Expected no CRLs\n");
1790 }
1791
1792 CertCloseStore(store, 0);
1793 DeleteFileW(filename);
1794 }
1795 if (initFileFromData(filename, serializedStoreWithCertAndCRL,
1796 sizeof(serializedStoreWithCertAndCRL)))
1797 {
1798 PCCERT_CONTEXT cert;
1799 PCCRL_CONTEXT crl;
1800
1801 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1802 CERT_STORE_READONLY_FLAG, filename);
1803 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1804
1805 cert = CertEnumCertificatesInStore(store, NULL);
1806 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1807 GetLastError());
1808 cert = CertEnumCertificatesInStore(store, cert);
1809 ok(!cert, "Expected only one cert\n");
1810 if (pCertEnumCRLsInStore)
1811 {
1812 crl = pCertEnumCRLsInStore(store, NULL);
1813 ok(crl != NULL, "CertEnumCRLsInStore failed: %08x\n", GetLastError());
1814 crl = pCertEnumCRLsInStore(store, crl);
1815 ok(!crl, "Expected only one CRL\n");
1816 }
1817
1818 CertCloseStore(store, 0);
1819 /* Don't delete it this time, the next test uses it */
1820 }
1821 /* Now that the file exists, we can open it read-only */
1822 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1823 CERT_STORE_READONLY_FLAG, filename);
1824 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1825 CertCloseStore(store, 0);
1826 DeleteFileW(filename);
1827
1828 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1829 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_CREATE_NEW_FLAG, filename);
1830 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1831 if (store)
1832 {
1833 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1834 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1835 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1836 GetLastError());
1837 compareStore(store, "serialized store with cert",
1838 serializedStoreWithCert, sizeof(serializedStoreWithCert), FALSE);
1839 CertCloseStore(store, 0);
1840 }
1841 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1842 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, filename);
1843 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1844 if (store)
1845 {
1846 ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING,
1847 signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1848 ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1849 compareStore(store, "serialized store with cert and CRL",
1850 serializedStoreWithCertAndCRL, sizeof(serializedStoreWithCertAndCRL),
1851 FALSE);
1852 CertCloseStore(store, 0);
1853 }
1854 DeleteFileW(filename);
1855
1856 if (!GetTempFileNameW(szDot, spcPrefix, 0, filename))
1857 return;
1858 DeleteFileW(filename);
1859
1860 if (initFileFromData(filename, base64SPC, sizeof(base64SPC)))
1861 {
1862 PCCERT_CONTEXT cert;
1863 PCCRL_CONTEXT crl;
1864
1865 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1866 CERT_STORE_READONLY_FLAG, filename);
1867 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1868
1869 cert = CertEnumCertificatesInStore(store, NULL);
1870 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1871 GetLastError());
1872 cert = CertEnumCertificatesInStore(store, cert);
1873 ok(!cert, "Expected only one cert\n");
1874 if (pCertEnumCRLsInStore)
1875 {
1876 crl = pCertEnumCRLsInStore(store, NULL);
1877 ok(!crl, "Expected no CRLs\n");
1878 }
1879
1880 CertCloseStore(store, 0);
1881 DeleteFileW(filename);
1882 }
1883 if (initFileFromData(filename, (BYTE *)utf16Base64SPC,
1884 sizeof(utf16Base64SPC)))
1885 {
1886 PCCERT_CONTEXT cert;
1887 PCCRL_CONTEXT crl;
1888
1889 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1890 CERT_STORE_READONLY_FLAG, filename);
1891 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1892
1893 cert = CertEnumCertificatesInStore(store, NULL);
1894 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1895 GetLastError());
1896 cert = CertEnumCertificatesInStore(store, cert);
1897 ok(!cert, "Expected only one cert\n");
1898 if (pCertEnumCRLsInStore)
1899 {
1900 crl = pCertEnumCRLsInStore(store, NULL);
1901 ok(!crl, "Expected no CRLs\n");
1902 }
1903
1904 CertCloseStore(store, 0);
1905 DeleteFileW(filename);
1906 }
1907 }
1908
1909 static const BYTE signedContent[] = {
1910 0x30,0x81,0xb2,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
1911 0x81,0xa4,0x30,0x81,0xa1,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
1912 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
1913 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
1914 0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,
1915 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1916 0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1917 0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,
1918 0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,
1919 0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,
1920 0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,
1921 0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,
1922 0x0d };
1923 static const BYTE signedWithCertAndCrlBareContent[] = {
1924 0x30,0x82,0x01,0x4f,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,
1925 0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,
1926 0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0xa0,
1927 0x7c,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,
1928 0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
1929 0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,
1930 0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1931 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,
1932 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1933 0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,
1934 0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,
1935 0x01,0xff,0x02,0x01,0x01,0xa1,0x2e,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,0x15,
1936 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1937 0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
1938 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x31,0x77,0x30,0x75,0x02,0x01,0x01,
1939 0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,
1940 0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,
1941 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,
1942 0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,
1943 0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,
1944 0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,
1945 0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,
1946 0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
1947 static const BYTE hashContent[] = {
1948 0x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
1949 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1950 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1951 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1952 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1953 static const BYTE hashBareContent[] = {
1954 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1955 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1956 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1957 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1958
1959 static void testMessageStore(void)
1960 {
1961 HCERTSTORE store;
1962 HCRYPTMSG msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, 0, 0, 0, NULL,
1963 NULL);
1964 CRYPT_DATA_BLOB blob = { sizeof(signedWithCertAndCrlBareContent),
1965 (LPBYTE)signedWithCertAndCrlBareContent };
1966 DWORD count, size;
1967 BOOL ret;
1968
1969 /* Crashes
1970 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, NULL);
1971 */
1972 SetLastError(0xdeadbeef);
1973 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1974 ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
1975 "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
1976 CryptMsgUpdate(msg, signedContent, sizeof(signedContent), TRUE);
1977 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1978 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1979 if (store)
1980 {
1981 PCCERT_CONTEXT cert = NULL;
1982 PCCRL_CONTEXT crl = NULL;
1983
1984 count = 0;
1985 do {
1986 cert = CertEnumCertificatesInStore(store, cert);
1987 if (cert)
1988 count++;
1989 } while (cert);
1990 ok(count == 0, "Expected 0 certificates, got %d\n", count);
1991
1992 if (pCertEnumCRLsInStore)
1993 {
1994 count = 0;
1995 do {
1996 crl = pCertEnumCRLsInStore(store, crl);
1997 if (crl)
1998 count++;
1999 } while (crl);
2000 ok(count == 0, "Expected 0 CRLs, got %d\n", count);
2001 }
2002
2003 /* Can add certs to a message store */
2004 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
2005 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
2006 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
2007 GetLastError());
2008 count = 0;
2009 do {
2010 cert = CertEnumCertificatesInStore(store, cert);
2011 if (cert)
2012 count++;
2013 } while (cert);
2014 ok(count == 1, "Expected 1 certificate, got %d\n", count);
2015
2016 CertCloseStore(store, 0);
2017 }
2018 /* but the added certs weren't actually added to the message */
2019 size = sizeof(count);
2020 ret = CryptMsgGetParam(msg, CMSG_CERT_COUNT_PARAM, 0, &count, &size);
2021 ok(ret, "CryptMsgGetParam failed: %08x\n", GetLastError());
2022 ok(count == 0, "Expected 0 certificates, got %d\n", count);
2023 CryptMsgClose(msg);
2024
2025 /* Crashes
2026 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, NULL);
2027 */
2028 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
2029 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2030 if (store)
2031 {
2032 DWORD count = 0;
2033 PCCERT_CONTEXT cert = NULL;
2034 PCCRL_CONTEXT crl = NULL;
2035
2036 do {
2037 cert = CertEnumCertificatesInStore(store, cert);
2038 if (cert)
2039 count++;
2040 } while (cert);
2041 ok(count == 1, "Expected 1 certificate, got %d\n", count);
2042
2043 if (pCertEnumCRLsInStore)
2044 {
2045 count = 0;
2046 do {
2047 crl = pCertEnumCRLsInStore(store, crl);
2048 if (crl)
2049 count++;
2050 } while (crl);
2051 ok(count == 1, "Expected 1 CRL, got %d\n", count);
2052 }
2053 CertCloseStore(store, 0);
2054 }
2055 /* Encoding appears to be ignored */
2056 store = CertOpenStore(CERT_STORE_PROV_PKCS7, X509_ASN_ENCODING, 0, 0,
2057 &blob);
2058 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2059 if (store)
2060 CertCloseStore(store, 0);
2061 /* Messages other than signed messages aren't allowed */
2062 blob.cbData = sizeof(hashContent);
2063 blob.pbData = (LPBYTE)hashContent;
2064 SetLastError(0xdeadbeef);
2065 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
2066 ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
2067 "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
2068 blob.cbData = sizeof(hashBareContent);
2069 blob.pbData = (LPBYTE)hashBareContent;
2070 SetLastError(0xdeadbeef);
2071 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
2072 ok(!store &&
2073 (GetLastError() == CRYPT_E_ASN1_BADTAG ||
2074 broken(GetLastError() == OSS_DATA_ERROR)), /* NT4 */
2075 "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
2076 }
2077
2078 static void testSerializedStore(void)
2079 {
2080 HCERTSTORE store;
2081 CRYPT_DATA_BLOB blob;
2082
2083 if (0)
2084 {
2085 /* Crash */
2086 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, NULL);
2087 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0,
2088 CERT_STORE_DELETE_FLAG, NULL);
2089 }
2090 blob.cbData = sizeof(serializedStoreWithCert);
2091 blob.pbData = (BYTE *)serializedStoreWithCert;
2092 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0,
2093 CERT_STORE_DELETE_FLAG, &blob);
2094 ok(!store && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
2095 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
2096 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, &blob);
2097 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2098 if (store)
2099 {
2100 PCCERT_CONTEXT cert;
2101 PCCRL_CONTEXT crl;
2102
2103 cert = CertEnumCertificatesInStore(store, NULL);
2104 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
2105 GetLastError());
2106 cert = CertEnumCertificatesInStore(store, cert);
2107 ok(!cert, "Expected only one cert\n");
2108 if (pCertEnumCRLsInStore)
2109 {
2110 crl = pCertEnumCRLsInStore(store, NULL);
2111 ok(!crl, "Expected no CRLs\n");
2112 }
2113 CertCloseStore(store, 0);
2114 }
2115 blob.cbData = sizeof(serializedStoreWithCertAndCRL);
2116 blob.pbData = (BYTE *)serializedStoreWithCertAndCRL;
2117 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, &blob);
2118 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2119 if (store)
2120 {
2121 PCCERT_CONTEXT cert;
2122 PCCRL_CONTEXT crl;
2123
2124 cert = CertEnumCertificatesInStore(store, NULL);
2125 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
2126 GetLastError());
2127 cert = CertEnumCertificatesInStore(store, cert);
2128 ok(!cert, "Expected only one cert\n");
2129 if (pCertEnumCRLsInStore)
2130 {
2131 crl = pCertEnumCRLsInStore(store, NULL);
2132 ok(crl != NULL, "CertEnumCRLsInStore failed: %08x\n",
2133 GetLastError());
2134 crl = pCertEnumCRLsInStore(store, crl);
2135 ok(!crl, "Expected only one CRL\n");
2136 }
2137 CertCloseStore(store, 0);
2138 }
2139 }
2140
2141 static void testCertOpenSystemStore(void)
2142 {
2143 HCERTSTORE store;
2144
2145 store = CertOpenSystemStoreW(0, NULL);
2146 ok(!store && GetLastError() == E_INVALIDARG,
2147 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2148 /* This succeeds, and on WinXP at least, the Bogus key is created under
2149 * HKCU (but not under HKLM, even when run as an administrator.)
2150 */
2151 store = CertOpenSystemStoreW(0, BogusW);
2152 ok(store != 0, "CertOpenSystemStore failed: %08x\n", GetLastError());
2153 if (store)
2154 CertCloseStore(store, 0);
2155 /* Delete it so other tests succeed next time around */
2156 CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
2157 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
2158 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
2159 }
2160
2161 static const struct
2162 {
2163 DWORD cert_store;
2164 BOOL expected;
2165 BOOL todo;
2166 } reg_system_store_test_data[] = {
2167 { CERT_SYSTEM_STORE_CURRENT_USER, TRUE, 0},
2168 /* Following tests could require administrator privileges and thus could be skipped */
2169 { CERT_SYSTEM_STORE_CURRENT_SERVICE, TRUE, 1},
2170 { CERT_SYSTEM_STORE_LOCAL_MACHINE, TRUE, 0},
2171 { CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY, TRUE, 0},
2172 { CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY, TRUE, 0},
2173 { CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, TRUE, 1}
2174 };
2175
2176 static void testCertRegisterSystemStore(void)
2177 {
2178 BOOL ret, cur_flag;
2179 DWORD err = 0;
2180 HCERTSTORE hstore;
2181 static const WCHAR WineTestW[] = {'W','i','n','e','T','e','s','t',0};
2182 const CERT_CONTEXT *cert, *cert2;
2183 unsigned int i;
2184
2185 if (!pCertRegisterSystemStore || !pCertUnregisterSystemStore)
2186 {
2187 win_skip("CertRegisterSystemStore() or CertUnregisterSystemStore() is not available\n");
2188 return;
2189 }
2190
2191 for (i = 0; i < sizeof(reg_system_store_test_data) / sizeof(reg_system_store_test_data[0]); i++) {
2192 cur_flag = reg_system_store_test_data[i].cert_store;
2193 ret = pCertRegisterSystemStore(WineTestW, cur_flag, NULL, NULL);
2194 if (!ret)
2195 {
2196 err = GetLastError();
2197 if (err == ERROR_ACCESS_DENIED)
2198 {
2199 win_skip("Insufficient privileges for the flag %08x test\n", cur_flag);
2200 continue;
2201 }
2202 }
2203 todo_wine_if (reg_system_store_test_data[i].todo)
2204 ok (ret == reg_system_store_test_data[i].expected,
2205 "Store registration (dwFlags=%08x) failed, last error %x\n", cur_flag, err);
2206 if (!ret)
2207 {
2208 skip("Nothing to test without registered store at %08x\n", cur_flag);
2209 continue;
2210 }
2211
2212 hstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, CERT_STORE_OPEN_EXISTING_FLAG | cur_flag, WineTestW);
2213 ok (hstore != NULL, "Opening just registered store at %08x failed, last error %x\n", cur_flag, GetLastError());
2214
2215 cert = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert, sizeof(bigCert));
2216 ok (cert != NULL, "Failed creating cert at %08x, last error: %x\n", cur_flag, GetLastError());
2217 if (cert)
2218 {
2219 ret = CertAddCertificateContextToStore(hstore, cert, CERT_STORE_ADD_NEW, NULL);
2220 ok (ret, "Failed to add cert at %08x, last error: %x\n", cur_flag, GetLastError());
2221
2222 cert2 = CertEnumCertificatesInStore(hstore, NULL);
2223 ok (cert2 != NULL && cert2->cbCertEncoded == cert->cbCertEncoded,
2224 "Unexpected cert encoded size at %08x, last error: %x\n", cur_flag, GetLastError());
2225
2226 ret = CertDeleteCertificateFromStore(cert2);
2227 ok (ret, "Failed to delete certificate from the new store at %08x, last error: %x\n", cur_flag, GetLastError());
2228
2229 CertFreeCertificateContext(cert);
2230 }
2231
2232 ret = CertCloseStore(hstore, 0);
2233 ok (ret, "CertCloseStore failed at %08x, last error %x\n", cur_flag, GetLastError());
2234
2235 ret = pCertUnregisterSystemStore(WineTestW, cur_flag );
2236 todo_wine_if (reg_system_store_test_data[i].todo)
2237 ok( ret == reg_system_store_test_data[i].expected,
2238 "Unregistering failed at %08x, last error %d\n", cur_flag, GetLastError());
2239 }
2240
2241 }
2242
2243 struct EnumSystemStoreInfo
2244 {
2245 BOOL goOn;
2246 DWORD storeCount;
2247 };
2248
2249 static BOOL CALLBACK enumSystemStoreCB(const void *systemStore, DWORD dwFlags,
2250 PCERT_SYSTEM_STORE_INFO pStoreInfo, void *pvReserved, void *pvArg)
2251 {
2252 struct EnumSystemStoreInfo *info = pvArg;
2253
2254 info->storeCount++;
2255 return info->goOn;
2256 }
2257
2258 static void testCertEnumSystemStore(void)
2259 {
2260 BOOL ret;
2261 struct EnumSystemStoreInfo info = { FALSE, 0 };
2262
2263 if (!pCertEnumSystemStore)
2264 {
2265 win_skip("CertEnumSystemStore() is not available\n");
2266 return;
2267 }
2268
2269 SetLastError(0xdeadbeef);
2270 ret = pCertEnumSystemStore(0, NULL, NULL, NULL);
2271 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2272 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
2273 /* Crashes
2274 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, NULL,
2275 NULL);
2276 */
2277
2278 SetLastError(0xdeadbeef);
2279 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
2280 enumSystemStoreCB);
2281 /* Callback returning FALSE stops enumeration */
2282 ok(!ret, "Expected CertEnumSystemStore to stop\n");
2283 ok(info.storeCount == 0 || info.storeCount == 1,
2284 "Expected 0 or 1 stores\n");
2285
2286 info.goOn = TRUE;
2287 info.storeCount = 0;
2288 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
2289 enumSystemStoreCB);
2290 ok(ret, "CertEnumSystemStore failed: %08x\n", GetLastError());
2291 /* There should always be at least My, Root, and CA stores */
2292 ok(info.storeCount == 0 || info.storeCount >= 3,
2293 "Expected at least 3 stores\n");
2294 }
2295
2296 static void testStoreProperty(void)
2297 {
2298 HCERTSTORE store;
2299 BOOL ret;
2300 DWORD propID, size = 0, state;
2301 CRYPT_DATA_BLOB blob;
2302
2303 if (!pCertGetStoreProperty || !pCertSetStoreProperty)
2304 {
2305 win_skip("CertGet/SetStoreProperty() is not available\n");
2306 return;
2307 }
2308
2309 /* Crash
2310 ret = pCertGetStoreProperty(NULL, 0, NULL, NULL);
2311 ret = pCertGetStoreProperty(NULL, 0, NULL, &size);
2312 ret = pCertGetStoreProperty(store, 0, NULL, NULL);
2313 */
2314
2315 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2316 CERT_STORE_CREATE_NEW_FLAG, NULL);
2317 /* Check a missing prop ID */
2318 SetLastError(0xdeadbeef);
2319 ret = pCertGetStoreProperty(store, 0, NULL, &size);
2320 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
2321 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
2322 /* Contrary to MSDN, CERT_ACCESS_STATE_PROP_ID is supported for stores.. */
2323 size = sizeof(state);
2324 ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
2325 ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
2326 GetLastError());
2327 ok(!state, "Expected a non-persisted store\n");
2328 /* and CERT_STORE_LOCALIZED_NAME_PROP_ID isn't supported by default. */
2329 size = 0;
2330 ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
2331 &size);
2332 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
2333 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
2334 /* Delete an arbitrary property on a store */
2335 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
2336 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
2337 /* Set an arbitrary property on a store */
2338 blob.pbData = (LPBYTE)&state;
2339 blob.cbData = sizeof(state);
2340 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, &blob);
2341 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
2342 /* Get an arbitrary property that's been set */
2343 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
2344 ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
2345 ok(size == sizeof(state), "Unexpected data size %d\n", size);
2346 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, &propID, &size);
2347 ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
2348 ok(propID == state, "CertGetStoreProperty got the wrong value\n");
2349 /* Delete it again */
2350 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
2351 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
2352 /* And check that it's missing */
2353 SetLastError(0xdeadbeef);
2354 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
2355 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
2356 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
2357 CertCloseStore(store, 0);
2358
2359 /* Recheck on the My store.. */
2360 store = CertOpenSystemStoreW(0, MyW);
2361 size = sizeof(state);
2362 ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
2363 ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
2364 GetLastError());
2365 ok(state, "Expected a persisted store\n");
2366 SetLastError(0xdeadbeef);
2367 size = 0;
2368 ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
2369 &size);
2370 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
2371 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
2372 CertCloseStore(store, 0);
2373 }
2374
2375 static void testAddSerialized(void)
2376 {
2377 BOOL ret;
2378 HCERTSTORE store;
2379 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + 20 + sizeof(bigCert)] =
2380 { 0 };
2381 BYTE hash[20];
2382 struct CertPropIDHeader *hdr;
2383 PCCERT_CONTEXT context;
2384
2385 ret = CertAddSerializedElementToStore(0, NULL, 0, 0, 0, 0, NULL, NULL);
2386 ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
2387 "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
2388
2389 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2390 CERT_STORE_CREATE_NEW_FLAG, NULL);
2391 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
2392
2393 ret = CertAddSerializedElementToStore(