EnumThemeColors() and EnumThemeSizes() actually do not return a single
[wine] / dlls / crypt32 / tests / cert.c
1 /*
2  * crypt32 cert functions tests
3  *
4  * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winreg.h>
26 #include <winerror.h>
27 #include <wincrypt.h>
28
29 #include "wine/test.h"
30
31 static void testCryptHashCert(void)
32 {
33     static const BYTE emptyHash[] = { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b,
34      0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07,
35      0x09 };
36     static const BYTE knownHash[] = { 0xae, 0x9d, 0xbf, 0x6d, 0xf5, 0x46, 0xee,
37      0x8b, 0xc5, 0x7a, 0x13, 0xba, 0xc2, 0xb1, 0x04, 0xf2, 0xbf, 0x52, 0xa8,
38      0xa2 };
39     static const BYTE toHash[] = "abcdefghijklmnopqrstuvwxyz0123456789.,;!?:";
40     BOOL ret;
41     BYTE hash[20];
42     DWORD hashLen = sizeof(hash);
43
44     /* NULL buffer and nonzero length crashes
45     ret = CryptHashCertificate(0, 0, 0, NULL, size, hash, &hashLen);
46        empty hash length also crashes
47     ret = CryptHashCertificate(0, 0, 0, buf, size, hash, NULL);
48      */
49     /* Test empty hash */
50     ret = CryptHashCertificate(0, 0, 0, toHash, sizeof(toHash), NULL,
51      &hashLen);
52     ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
53     ok(hashLen == sizeof(hash),
54      "Got unexpected size of hash %ld, expected %d\n", hashLen, sizeof(hash));
55     /* Test with empty buffer */
56     ret = CryptHashCertificate(0, 0, 0, NULL, 0, hash, &hashLen);
57     ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
58     ok(!memcmp(hash, emptyHash, sizeof(emptyHash)),
59      "Unexpected hash of nothing\n");
60     /* Test a known value */
61     ret = CryptHashCertificate(0, 0, 0, toHash, sizeof(toHash), hash,
62      &hashLen);
63     ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
64     ok(!memcmp(hash, knownHash, sizeof(knownHash)), "Unexpected hash\n");
65 }
66
67 static const BYTE emptyCert[] = { 0x30, 0x00 };
68 static const BYTE bigCert[] = "\x30\x7a\x02\x01\x01\x30\x02\x06\x00"
69  "\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x4a\x75\x61\x6e\x20\x4c"
70  "\x61\x6e\x67\x00\x30\x22\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30"
71  "\x30\x30\x30\x5a\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30\x30\x30"
72  "\x30\x5a\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x4a\x75\x61\x6e"
73  "\x20\x4c\x61\x6e\x67\x00\x30\x07\x30\x02\x06\x00\x03\x01\x00\xa3\x16\x30\x14"
74  "\x30\x12\x06\x03\x55\x1d\x13\x01\x01\xff\x04\x08\x30\x06\x01\x01\xff\x02\x01"
75  "\x01";
76 static const BYTE signedBigCert[] = {
77  0x30, 0x81, 0x93, 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06, 0x00, 0x30,
78  0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a,
79  0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22, 0x18, 0x0f,
80  0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
81  0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30,
82  0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06,
83  0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61,
84  0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01, 0x00, 0xa3,
85  0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
86  0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
87  0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
88  0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
89 static const BYTE serializedCert[] = { 0x20, 0x00, 0x00, 0x00,
90  0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x30, 0x7a, 0x02, 0x01, 0x01,
91  0x30, 0x02, 0x06, 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
92  0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67,
93  0x00, 0x30, 0x22, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31,
94  0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31,
95  0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15,
96  0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75,
97  0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06,
98  0x00, 0x03, 0x01, 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
99  0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02,
100  0x01, 0x01 };
101
102 static void testMemStore(void)
103 {
104     HCERTSTORE store1, store2;
105     PCCERT_CONTEXT context;
106     BOOL ret;
107
108     /* NULL provider */
109     store1 = CertOpenStore(0, 0, 0, 0, NULL);
110     ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
111      "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
112     /* weird flags */
113     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
114      CERT_STORE_DELETE_FLAG, NULL);
115     ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
116      "Expected ERROR_CALL_NOT_IMPLEMENTED, got %ld\n", GetLastError());
117
118     /* normal */
119     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
120      CERT_STORE_CREATE_NEW_FLAG, NULL);
121     ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
122     /* open existing doesn't */
123     store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
124      CERT_STORE_OPEN_EXISTING_FLAG, NULL);
125     ok(store2 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
126     ok(store1 != store2, "Expected different stores\n");
127
128     /* add a bogus (empty) cert */
129     context = NULL;
130     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
131      sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
132     /* Windows returns CRYPT_E_ASN1_EOD, but accept CRYPT_E_ASN1_CORRUPT as
133      * well (because matching errors is tough in this case)
134      */
135     ok(!ret && (GetLastError() == CRYPT_E_ASN1_EOD || GetLastError() ==
136      CRYPT_E_ASN1_CORRUPT),
137      "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
138      GetLastError());
139     /* add a signed cert (this also fails) */
140     ok(!ret && (GetLastError() == CRYPT_E_ASN1_EOD || GetLastError() ==
141      CRYPT_E_ASN1_CORRUPT),
142      "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
143      GetLastError());
144     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
145      signedBigCert, sizeof(signedBigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
146     /* add a cert to store1 */
147     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
148      sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
149     ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
150     ok(context != NULL, "Expected a valid cert context\n");
151     if (context)
152     {
153         DWORD size;
154         BYTE *buf;
155
156         ok(context->cbCertEncoded == sizeof(bigCert) - 1,
157          "Expected cert of %d bytes, got %ld\n", sizeof(bigCert) - 1,
158          context->cbCertEncoded);
159         ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert) - 1),
160          "Unexpected encoded cert in context\n");
161         ok(context->hCertStore == store1, "Unexpected store\n");
162
163         /* check serializing this element */
164         /* These crash
165         ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
166         ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
167         ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
168          */
169         /* apparently flags are ignored */
170         ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
171         ok(ret, "CertSerializeCertificateStoreElement failed: %08lx\n",
172          GetLastError());
173         buf = HeapAlloc(GetProcessHeap(), 0, size);
174         if (buf)
175         {
176             ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
177             ok(size == sizeof(serializedCert), "Expected size %d, got %ld\n",
178              sizeof(serializedCert), size);
179             ok(!memcmp(serializedCert, buf, size),
180              "Unexpected serialized cert\n");
181             HeapFree(GetProcessHeap(), 0, buf);
182         }
183
184         ret = CertFreeCertificateContext(context);
185         ok(ret, "CertFreeCertificateContext failed: %08lx\n", GetLastError());
186     }
187     /* verify the cert's in store1 */
188     context = CertEnumCertificatesInStore(store1, NULL);
189     ok(context != NULL, "Expected a valid context\n");
190     context = CertEnumCertificatesInStore(store1, context);
191     ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
192      "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
193     /* verify store2 (the "open existing" mem store) is still empty */
194     context = CertEnumCertificatesInStore(store2, NULL);
195     ok(!context, "Expected an empty store\n");
196     /* delete the cert from store1, and check it's empty */
197     context = CertEnumCertificatesInStore(store1, NULL);
198     if (context)
199     {
200         /* Deleting a bitwise copy crashes with an access to an uninitialized
201          * pointer, so a cert context has some special data out there in memory
202          * someplace
203         CERT_CONTEXT copy;
204         memcpy(&copy, context, sizeof(copy));
205         ret = CertDeleteCertificateFromStore(&copy);
206          */
207         PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
208
209         ok(copy != NULL, "CertDuplicateCertificateContext failed: %08lx\n",
210          GetLastError());
211         ret = CertDeleteCertificateFromStore(context);
212         ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
213          GetLastError());
214         /* try deleting a copy */
215         ret = CertDeleteCertificateFromStore(copy);
216         ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
217          GetLastError());
218         /* check that the store is empty */
219         context = CertEnumCertificatesInStore(store1, NULL);
220         ok(!context, "Expected an empty store\n");
221     }
222
223     /* close an empty store */
224     ret = CertCloseStore(NULL, 0);
225     ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
226     ret = CertCloseStore(store1, 0);
227     ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
228     ret = CertCloseStore(store2, 0);
229     ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
230
231     /* This seems nonsensical, but you can open a read-only mem store, only
232      * it isn't read-only
233      */
234     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
235      CERT_STORE_READONLY_FLAG, NULL);
236     ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
237     /* yep, this succeeds */
238     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
239      sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
240     ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
241     ok(context != NULL, "Expected a valid cert context\n");
242     if (context)
243     {
244         ok(context->cbCertEncoded == sizeof(bigCert) - 1,
245          "Expected cert of %d bytes, got %ld\n", sizeof(bigCert) - 1,
246          context->cbCertEncoded);
247         ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert) - 1),
248          "Unexpected encoded cert in context\n");
249         ok(context->hCertStore == store1, "Unexpected store\n");
250         ret = CertDeleteCertificateFromStore(context);
251         ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
252          GetLastError());
253     }
254     CertCloseStore(store1, 0);
255 }
256
257 static void testCertProperties(void)
258 {
259     PCCERT_CONTEXT context = CertCreateCertificateContext(X509_ASN_ENCODING,
260      bigCert, sizeof(bigCert) - 1);
261
262     ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n",
263      GetLastError());
264     if (context)
265     {
266         DWORD propID, numProps, access, size;
267         BOOL ret;
268         BYTE hash[20] = { 0 }, hashProperty[20];
269         CRYPT_DATA_BLOB blob;
270
271         /* This crashes
272         propID = CertEnumCertificateContextProperties(NULL, 0);
273          */
274
275         propID = 0;
276         numProps = 0;
277         do {
278             propID = CertEnumCertificateContextProperties(context, propID);
279             if (propID)
280                 numProps++;
281         } while (propID != 0);
282         ok(numProps == 0, "Expected 0 properties, got %ld\n", numProps);
283
284         /* Tests with a NULL cert context.  Prop ID 0 fails.. */
285         ret = CertSetCertificateContextProperty(NULL, 0, 0, NULL);
286         ok(!ret && GetLastError() ==
287          HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
288          "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
289          GetLastError());
290         /* while this just crashes.
291         ret = CertSetCertificateContextProperty(NULL,
292          CERT_KEY_PROV_HANDLE_PROP_ID, 0, NULL);
293          */
294
295         ret = CertSetCertificateContextProperty(context, 0, 0, NULL);
296         ok(!ret && GetLastError() ==
297          HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
298          "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
299          GetLastError());
300         /* Can't set the cert property directly, this crashes.
301         ret = CertSetCertificateContextProperty(context,
302          CERT_CERT_PROP_ID, 0, bigCert2);
303          */
304
305         /* This crashes.
306         ret = CertGetCertificateContextProperty(context,
307          CERT_ACCESS_STATE_PROP_ID, 0, NULL);
308          */
309         size = sizeof(access);
310         ret = CertGetCertificateContextProperty(context,
311          CERT_ACCESS_STATE_PROP_ID, &access, &size);
312         ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
313          GetLastError());
314         ok(!(access & CERT_ACCESS_STATE_WRITE_PERSIST_FLAG),
315          "Didn't expect a persisted cert\n");
316         /* Trying to set this "read only" property crashes.
317         access |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
318         ret = CertSetCertificateContextProperty(context,
319          CERT_ACCESS_STATE_PROP_ID, 0, &access);
320          */
321
322         /* Can I set the hash to an invalid hash? */
323         blob.pbData = hash;
324         blob.cbData = sizeof(hash);
325         ret = CertSetCertificateContextProperty(context, CERT_HASH_PROP_ID, 0,
326          &blob);
327         ok(ret, "CertSetCertificateContextProperty failed: %08lx\n",
328          GetLastError());
329         size = sizeof(hashProperty);
330         ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
331          hashProperty, &size);
332         ok(!memcmp(hashProperty, hash, sizeof(hash)), "Unexpected hash\n");
333         /* Delete the (bogus) hash, and get the real one */
334         ret = CertSetCertificateContextProperty(context, CERT_HASH_PROP_ID, 0,
335          NULL);
336         ok(ret, "CertSetCertificateContextProperty failed: %08lx\n",
337          GetLastError());
338         size = sizeof(hash);
339         ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert) - 1,
340          hash, &size);
341         ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
342         ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
343          hashProperty, &size);
344         ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
345          GetLastError());
346         ok(!memcmp(hash, hashProperty, sizeof(hash)), "Unexpected hash\n");
347
348         /* Now that the hash property is set, we should get one property when
349          * enumerating.
350          */
351         propID = 0;
352         numProps = 0;
353         do {
354             propID = CertEnumCertificateContextProperties(context, propID);
355             if (propID)
356                 numProps++;
357         } while (propID != 0);
358         ok(numProps == 1, "Expected 1 properties, got %ld\n", numProps);
359
360         CertFreeCertificateContext(context);
361     }
362 }
363
364 START_TEST(cert)
365 {
366     testCryptHashCert();
367
368     /* various combinations of CertOpenStore */
369     testMemStore();
370
371     testCertProperties();
372 }