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