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