gdi32: SetWinMetaFileBits: Use the whole device surface if the METAFILEPICT parameter...
[wine] / dlls / crypt32 / crl.c
1 /*
2  * Copyright 2006 Juan Lang
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include <assert.h>
21 #include <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wincrypt.h"
25 #include "wine/debug.h"
26 #include "crypt32_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
29
30 PCCRL_CONTEXT WINAPI CertCreateCRLContext(DWORD dwCertEncodingType,
31  const BYTE* pbCrlEncoded, DWORD cbCrlEncoded)
32 {
33     PCRL_CONTEXT crl = NULL;
34     BOOL ret;
35     PCRL_INFO crlInfo = NULL;
36     DWORD size = 0;
37
38     TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType, pbCrlEncoded,
39      cbCrlEncoded);
40
41     if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING)
42     {
43         SetLastError(E_INVALIDARG);
44         return NULL;
45     }
46     ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT_CRL_TO_BE_SIGNED,
47      pbCrlEncoded, cbCrlEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
48      (BYTE *)&crlInfo, &size);
49     if (ret)
50     {
51         BYTE *data = NULL;
52
53         crl = (PCRL_CONTEXT)Context_CreateDataContext(sizeof(CRL_CONTEXT));
54         if (!crl)
55             goto end;
56         data = CryptMemAlloc(cbCrlEncoded);
57         if (!data)
58         {
59             CryptMemFree(crl);
60             crl = NULL;
61             goto end;
62         }
63         memcpy(data, pbCrlEncoded, cbCrlEncoded);
64         crl->dwCertEncodingType = dwCertEncodingType;
65         crl->pbCrlEncoded       = data;
66         crl->cbCrlEncoded       = cbCrlEncoded;
67         crl->pCrlInfo           = crlInfo;
68         crl->hCertStore         = 0;
69     }
70
71 end:
72     return (PCCRL_CONTEXT)crl;
73 }
74
75 BOOL WINAPI CertAddEncodedCRLToStore(HCERTSTORE hCertStore,
76  DWORD dwCertEncodingType, const BYTE *pbCrlEncoded, DWORD cbCrlEncoded,
77  DWORD dwAddDisposition, PCCRL_CONTEXT *ppCrlContext)
78 {
79     PCCRL_CONTEXT crl = CertCreateCRLContext(dwCertEncodingType,
80      pbCrlEncoded, cbCrlEncoded);
81     BOOL ret;
82
83     TRACE("(%p, %08lx, %p, %ld, %08lx, %p)\n", hCertStore, dwCertEncodingType,
84      pbCrlEncoded, cbCrlEncoded, dwAddDisposition, ppCrlContext);
85
86     if (crl)
87     {
88         ret = CertAddCRLContextToStore(hCertStore, crl, dwAddDisposition,
89          ppCrlContext);
90         CertFreeCRLContext(crl);
91     }
92     else
93         ret = FALSE;
94     return ret;
95 }
96
97 typedef BOOL (*CrlCompareFunc)(PCCRL_CONTEXT pCrlContext, DWORD dwType,
98  DWORD dwFlags, const void *pvPara);
99
100 static BOOL compare_crl_any(PCCRL_CONTEXT pCrlContext, DWORD dwType,
101  DWORD dwFlags, const void *pvPara)
102 {
103     return TRUE;
104 }
105
106 static BOOL compare_crl_issued_by(PCCRL_CONTEXT pCrlContext, DWORD dwType,
107  DWORD dwFlags, const void *pvPara)
108 {
109     BOOL ret;
110
111     if (pvPara)
112     {
113         PCCERT_CONTEXT issuer = (PCCERT_CONTEXT)pvPara;
114
115         ret = CertCompareCertificateName(issuer->dwCertEncodingType,
116          &issuer->pCertInfo->Issuer, &pCrlContext->pCrlInfo->Issuer);
117     }
118     else
119         ret = TRUE;
120     return ret;
121 }
122
123 static BOOL compare_crl_existing(PCCRL_CONTEXT pCrlContext, DWORD dwType,
124  DWORD dwFlags, const void *pvPara)
125 {
126     BOOL ret;
127
128     if (pvPara)
129     {
130         PCCRL_CONTEXT crl = (PCCRL_CONTEXT)pvPara;
131
132         ret = CertCompareCertificateName(pCrlContext->dwCertEncodingType,
133          &pCrlContext->pCrlInfo->Issuer, &crl->pCrlInfo->Issuer);
134     }
135     else
136         ret = TRUE;
137     return ret;
138 }
139
140 PCCRL_CONTEXT WINAPI CertFindCRLInStore(HCERTSTORE hCertStore,
141  DWORD dwCertEncodingType, DWORD dwFindFlags, DWORD dwFindType,
142  const void *pvFindPara, PCCRL_CONTEXT pPrevCrlContext)
143 {
144     PCCRL_CONTEXT ret;
145     CrlCompareFunc compare;
146
147     TRACE("(%p, %ld, %ld, %ld, %p, %p)\n", hCertStore, dwCertEncodingType,
148          dwFindFlags, dwFindType, pvFindPara, pPrevCrlContext);
149
150     switch (dwFindType)
151     {
152     case CRL_FIND_ANY:
153         compare = compare_crl_any;
154         break;
155     case CRL_FIND_ISSUED_BY:
156         compare = compare_crl_issued_by;
157         break;
158     case CRL_FIND_EXISTING:
159         compare = compare_crl_existing;
160         break;
161     default:
162         FIXME("find type %08lx unimplemented\n", dwFindType);
163         compare = NULL;
164     }
165
166     if (compare)
167     {
168         BOOL matches = FALSE;
169
170         ret = pPrevCrlContext;
171         do {
172             ret = CertEnumCRLsInStore(hCertStore, ret);
173             if (ret)
174                 matches = compare(ret, dwFindType, dwFindFlags, pvFindPara);
175         } while (ret != NULL && !matches);
176         if (!ret)
177             SetLastError(CRYPT_E_NOT_FOUND);
178     }
179     else
180     {
181         SetLastError(CRYPT_E_NOT_FOUND);
182         ret = NULL;
183     }
184     return ret;
185 }
186
187 PCCRL_CONTEXT WINAPI CertDuplicateCRLContext(PCCRL_CONTEXT pCrlContext)
188 {
189     TRACE("(%p)\n", pCrlContext);
190     Context_AddRef((void *)pCrlContext, sizeof(CRL_CONTEXT));
191     return pCrlContext;
192 }
193
194 static void CrlDataContext_Free(void *context)
195 {
196     PCRL_CONTEXT crlContext = (PCRL_CONTEXT)context;
197
198     CryptMemFree(crlContext->pbCrlEncoded);
199     LocalFree(crlContext->pCrlInfo);
200 }
201
202 BOOL WINAPI CertFreeCRLContext( PCCRL_CONTEXT pCrlContext)
203 {
204     TRACE("(%p)\n", pCrlContext);
205
206     if (pCrlContext)
207         Context_Release((void *)pCrlContext, sizeof(CRL_CONTEXT),
208          CrlDataContext_Free);
209     return TRUE;
210 }
211
212 DWORD WINAPI CertEnumCRLContextProperties(PCCRL_CONTEXT pCRLContext,
213  DWORD dwPropId)
214 {
215     PCONTEXT_PROPERTY_LIST properties = Context_GetProperties(
216      (void *)pCRLContext, sizeof(CRL_CONTEXT));
217     DWORD ret;
218
219     TRACE("(%p, %ld)\n", pCRLContext, dwPropId);
220
221     if (properties)
222         ret = ContextPropertyList_EnumPropIDs(properties, dwPropId);
223     else
224         ret = 0;
225     return ret;
226 }
227
228 static BOOL WINAPI CRLContext_SetProperty(void *context, DWORD dwPropId,
229  DWORD dwFlags, const void *pvData);
230
231 static BOOL CRLContext_GetHashProp(void *context, DWORD dwPropId,
232  ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
233  DWORD *pcbData)
234 {
235     BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
236      pcbData);
237     if (ret)
238     {
239         CRYPT_DATA_BLOB blob = { *pcbData, pvData };
240
241         ret = CRLContext_SetProperty(context, dwPropId, 0, &blob);
242     }
243     return ret;
244 }
245
246 static BOOL WINAPI CRLContext_GetProperty(void *context, DWORD dwPropId,
247  void *pvData, DWORD *pcbData)
248 {
249     PCCRL_CONTEXT pCRLContext = (PCCRL_CONTEXT)context;
250     PCONTEXT_PROPERTY_LIST properties =
251      Context_GetProperties(context, sizeof(CRL_CONTEXT));
252     BOOL ret;
253     CRYPT_DATA_BLOB blob;
254
255     TRACE("(%p, %ld, %p, %p)\n", context, dwPropId, pvData, pcbData);
256
257     if (properties)
258         ret = ContextPropertyList_FindProperty(properties, dwPropId, &blob);
259     else
260         ret = FALSE;
261     if (ret)
262     {
263         if (!pvData)
264         {
265             *pcbData = blob.cbData;
266             ret = TRUE;
267         }
268         else if (*pcbData < blob.cbData)
269         {
270             SetLastError(ERROR_MORE_DATA);
271             *pcbData = blob.cbData;
272         }
273         else
274         {
275             memcpy(pvData, blob.pbData, blob.cbData);
276             *pcbData = blob.cbData;
277             ret = TRUE;
278         }
279     }
280     else
281     {
282         /* Implicit properties */
283         switch (dwPropId)
284         {
285         case CERT_SHA1_HASH_PROP_ID:
286             ret = CRLContext_GetHashProp(context, dwPropId, CALG_SHA1,
287              pCRLContext->pbCrlEncoded, pCRLContext->cbCrlEncoded, pvData,
288              pcbData);
289             break;
290         case CERT_MD5_HASH_PROP_ID:
291             ret = CRLContext_GetHashProp(context, dwPropId, CALG_MD5,
292              pCRLContext->pbCrlEncoded, pCRLContext->cbCrlEncoded, pvData,
293              pcbData);
294             break;
295         default:
296             SetLastError(CRYPT_E_NOT_FOUND);
297         }
298     }
299     TRACE("returning %d\n", ret);
300     return ret;
301 }
302
303 BOOL WINAPI CertGetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
304  DWORD dwPropId, void *pvData, DWORD *pcbData)
305 {
306     BOOL ret;
307
308     TRACE("(%p, %ld, %p, %p)\n", pCRLContext, dwPropId, pvData, pcbData);
309
310     switch (dwPropId)
311     {
312     case 0:
313     case CERT_CERT_PROP_ID:
314     case CERT_CRL_PROP_ID:
315     case CERT_CTL_PROP_ID:
316         SetLastError(E_INVALIDARG);
317         ret = FALSE;
318         break;
319     case CERT_ACCESS_STATE_PROP_ID:
320         if (!pvData)
321         {
322             *pcbData = sizeof(DWORD);
323             ret = TRUE;
324         }
325         else if (*pcbData < sizeof(DWORD))
326         {
327             SetLastError(ERROR_MORE_DATA);
328             *pcbData = sizeof(DWORD);
329             ret = FALSE;
330         }
331         else
332         {
333             *(DWORD *)pvData =
334              CertStore_GetAccessState(pCRLContext->hCertStore);
335             ret = TRUE;
336         }
337         break;
338     default:
339         ret = CRLContext_GetProperty((void *)pCRLContext, dwPropId, pvData,
340          pcbData);
341     }
342     return ret;
343 }
344
345 static BOOL WINAPI CRLContext_SetProperty(void *context, DWORD dwPropId,
346  DWORD dwFlags, const void *pvData)
347 {
348     PCONTEXT_PROPERTY_LIST properties =
349      Context_GetProperties(context, sizeof(CERT_CONTEXT));
350     BOOL ret;
351
352     TRACE("(%p, %ld, %08lx, %p)\n", context, dwPropId, dwFlags, pvData);
353
354     if (!properties)
355         ret = FALSE;
356     else if (!pvData)
357     {
358         ContextPropertyList_RemoveProperty(properties, dwPropId);
359         ret = TRUE;
360     }
361     else
362     {
363         switch (dwPropId)
364         {
365         case CERT_AUTO_ENROLL_PROP_ID:
366         case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
367         case CERT_DESCRIPTION_PROP_ID:
368         case CERT_FRIENDLY_NAME_PROP_ID:
369         case CERT_HASH_PROP_ID:
370         case CERT_KEY_IDENTIFIER_PROP_ID:
371         case CERT_MD5_HASH_PROP_ID:
372         case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
373         case CERT_PUBKEY_ALG_PARA_PROP_ID:
374         case CERT_PVK_FILE_PROP_ID:
375         case CERT_SIGNATURE_HASH_PROP_ID:
376         case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
377         case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
378         case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
379         case CERT_ENROLLMENT_PROP_ID:
380         case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
381         case CERT_RENEWAL_PROP_ID:
382         {
383             PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
384
385             ret = ContextPropertyList_SetProperty(properties, dwPropId,
386              blob->pbData, blob->cbData);
387             break;
388         }
389         case CERT_DATE_STAMP_PROP_ID:
390             ret = ContextPropertyList_SetProperty(properties, dwPropId,
391              (LPBYTE)pvData, sizeof(FILETIME));
392             break;
393         default:
394             FIXME("%ld: stub\n", dwPropId);
395             ret = FALSE;
396         }
397     }
398     TRACE("returning %d\n", ret);
399     return ret;
400 }
401
402 BOOL WINAPI CertSetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
403  DWORD dwPropId, DWORD dwFlags, const void *pvData)
404 {
405     BOOL ret;
406
407     TRACE("(%p, %ld, %08lx, %p)\n", pCRLContext, dwPropId, dwFlags, pvData);
408
409     /* Handle special cases for "read-only"/invalid prop IDs.  Windows just
410      * crashes on most of these, I'll be safer.
411      */
412     switch (dwPropId)
413     {
414     case 0:
415     case CERT_ACCESS_STATE_PROP_ID:
416     case CERT_CERT_PROP_ID:
417     case CERT_CRL_PROP_ID:
418     case CERT_CTL_PROP_ID:
419         SetLastError(E_INVALIDARG);
420         return FALSE;
421     }
422     ret = CRLContext_SetProperty((void *)pCRLContext, dwPropId, dwFlags,
423      pvData);
424     TRACE("returning %d\n", ret);
425     return ret;
426 }
427
428 BOOL WINAPI CertIsValidCRLForCertificate(PCCERT_CONTEXT pCert,
429  PCCRL_CONTEXT pCrl, DWORD dwFlags, void *pvReserved)
430 {
431     TRACE("(%p, %p, %08lx, %p)\n", pCert, pCrl, dwFlags, pvReserved);
432     return TRUE;
433 }
434
435 static PCRL_ENTRY CRYPT_FindCertificateInCRL(PCERT_INFO cert, PCRL_INFO crl)
436 {
437     DWORD i;
438     PCRL_ENTRY entry = NULL;
439
440     /* FIXME: do I need to compare the issuers of the cert and CRL? */
441     for (i = 0; !entry && i < crl->cCRLEntry; i++)
442         if (CertCompareIntegerBlob(&crl->rgCRLEntry[i].SerialNumber,
443          &cert->SerialNumber))
444             entry = &crl->rgCRLEntry[i];
445     return entry;
446 }
447
448 BOOL WINAPI CertFindCertificateInCRL(PCCERT_CONTEXT pCert,
449  PCCRL_CONTEXT pCrlContext, DWORD dwFlags, void *pvReserved,
450  PCRL_ENTRY *ppCrlEntry)
451 {
452     TRACE("(%p, %p, %08lx, %p, %p)\n", pCert, pCrlContext, dwFlags, pvReserved,
453      ppCrlEntry);
454
455     *ppCrlEntry = CRYPT_FindCertificateInCRL(pCert->pCertInfo,
456      pCrlContext->pCrlInfo);
457     return TRUE;
458 }
459
460 BOOL WINAPI CertVerifyCRLRevocation(DWORD dwCertEncodingType,
461  PCERT_INFO pCertId, DWORD cCrlInfo, PCRL_INFO rgpCrlInfo[])
462 {
463     DWORD i;
464     PCRL_ENTRY entry = NULL;
465
466     TRACE("(%08lx, %p, %ld, %p)\n", dwCertEncodingType, pCertId, cCrlInfo,
467      rgpCrlInfo);
468
469     for (i = 0; !entry && i < cCrlInfo; i++)
470         entry = CRYPT_FindCertificateInCRL(pCertId, rgpCrlInfo[i]);
471     return entry == NULL;
472 }
473
474 LONG WINAPI CertVerifyCRLTimeValidity(LPFILETIME pTimeToVerify,
475  PCRL_INFO pCrlInfo)
476 {
477     FILETIME fileTime;
478     LONG ret;
479
480     if (!pTimeToVerify)
481     {
482         SYSTEMTIME sysTime;
483
484         GetSystemTime(&sysTime);
485         SystemTimeToFileTime(&sysTime, &fileTime);
486         pTimeToVerify = &fileTime;
487     }
488     if ((ret = CompareFileTime(pTimeToVerify, &pCrlInfo->ThisUpdate)) >= 0)
489     {
490         ret = CompareFileTime(pTimeToVerify, &pCrlInfo->NextUpdate);
491         if (ret < 0)
492             ret = 0;
493     }
494     return ret;
495 }