blob: aa94f39d10ccaad8d7e28ac58f33c4f9b19de40f [file] [log] [blame]
Juan Langc4f2bcf2006-05-25 09:01:03 -07001/*
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
28WINE_DEFAULT_DEBUG_CHANNEL(crypt);
29
30PCCRL_CONTEXT WINAPI CertCreateCRLContext(DWORD dwCertEncodingType,
31 const BYTE* pbCrlEncoded, DWORD cbCrlEncoded)
32{
33 PCRL_CONTEXT crl = NULL;
34 BOOL ret;
Juan Langc4f2bcf2006-05-25 09:01:03 -070035 PCRL_INFO crlInfo = NULL;
36 DWORD size = 0;
37
Juan Langf3a1f2b2006-10-03 21:58:09 -070038 TRACE("(%08x, %p, %d)\n", dwCertEncodingType, pbCrlEncoded,
Juan Langc4f2bcf2006-05-25 09:01:03 -070039 cbCrlEncoded);
40
41 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING)
42 {
43 SetLastError(E_INVALIDARG);
44 return NULL;
45 }
Juan Lang13e006a2006-06-19 14:11:37 -070046 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT_CRL_TO_BE_SIGNED,
47 pbCrlEncoded, cbCrlEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
48 (BYTE *)&crlInfo, &size);
Juan Langc4f2bcf2006-05-25 09:01:03 -070049 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
71end:
72 return (PCCRL_CONTEXT)crl;
73}
74
75BOOL 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
Juan Langf3a1f2b2006-10-03 21:58:09 -070083 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore, dwCertEncodingType,
Juan Langc4f2bcf2006-05-25 09:01:03 -070084 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
97typedef BOOL (*CrlCompareFunc)(PCCRL_CONTEXT pCrlContext, DWORD dwType,
98 DWORD dwFlags, const void *pvPara);
99
100static BOOL compare_crl_any(PCCRL_CONTEXT pCrlContext, DWORD dwType,
101 DWORD dwFlags, const void *pvPara)
102{
103 return TRUE;
104}
105
106static 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
123static 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
140PCCRL_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
Juan Langf3a1f2b2006-10-03 21:58:09 -0700147 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore, dwCertEncodingType,
Juan Langc4f2bcf2006-05-25 09:01:03 -0700148 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:
Juan Langf3a1f2b2006-10-03 21:58:09 -0700162 FIXME("find type %08x unimplemented\n", dwFindType);
Juan Langc4f2bcf2006-05-25 09:01:03 -0700163 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
Juan Lang77ea5832006-06-21 20:50:11 -0700187PCCRL_CONTEXT WINAPI CertGetCRLFromStore(HCERTSTORE hCertStore,
188 PCCERT_CONTEXT pIssuerContext, PCCRL_CONTEXT pPrevCrlContext, DWORD *pdwFlags)
189{
190 static const DWORD supportedFlags = CERT_STORE_SIGNATURE_FLAG |
191 CERT_STORE_TIME_VALIDITY_FLAG | CERT_STORE_BASE_CRL_FLAG |
192 CERT_STORE_DELTA_CRL_FLAG;
193 PCCRL_CONTEXT ret;
194
Juan Langf3a1f2b2006-10-03 21:58:09 -0700195 TRACE("(%p, %p, %p, %08x)\n", hCertStore, pIssuerContext, pPrevCrlContext,
Juan Lang77ea5832006-06-21 20:50:11 -0700196 *pdwFlags);
197
198 if (*pdwFlags & ~supportedFlags)
199 {
200 SetLastError(E_INVALIDARG);
201 return NULL;
202 }
203 if (pIssuerContext)
204 ret = CertFindCRLInStore(hCertStore, pIssuerContext->dwCertEncodingType,
205 0, CRL_FIND_ISSUED_BY, pIssuerContext, pPrevCrlContext);
206 else
207 ret = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_ANY, NULL,
208 pPrevCrlContext);
209 if (ret)
210 {
211 if (*pdwFlags & CERT_STORE_TIME_VALIDITY_FLAG)
212 {
213 if (0 == CertVerifyCRLTimeValidity(NULL, ret->pCrlInfo))
214 *pdwFlags &= ~CERT_STORE_TIME_VALIDITY_FLAG;
215 }
216 if (*pdwFlags & CERT_STORE_SIGNATURE_FLAG)
217 {
218 if (CryptVerifyCertificateSignatureEx(0, ret->dwCertEncodingType,
219 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL, (void *)ret,
220 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT, (void *)pIssuerContext, 0,
221 NULL))
222 *pdwFlags &= ~CERT_STORE_SIGNATURE_FLAG;
223 }
224 }
225 return ret;
226}
227
Juan Langc4f2bcf2006-05-25 09:01:03 -0700228PCCRL_CONTEXT WINAPI CertDuplicateCRLContext(PCCRL_CONTEXT pCrlContext)
229{
230 TRACE("(%p)\n", pCrlContext);
231 Context_AddRef((void *)pCrlContext, sizeof(CRL_CONTEXT));
232 return pCrlContext;
233}
234
235static void CrlDataContext_Free(void *context)
236{
237 PCRL_CONTEXT crlContext = (PCRL_CONTEXT)context;
238
239 CryptMemFree(crlContext->pbCrlEncoded);
240 LocalFree(crlContext->pCrlInfo);
241}
242
243BOOL WINAPI CertFreeCRLContext( PCCRL_CONTEXT pCrlContext)
244{
245 TRACE("(%p)\n", pCrlContext);
246
247 if (pCrlContext)
248 Context_Release((void *)pCrlContext, sizeof(CRL_CONTEXT),
249 CrlDataContext_Free);
250 return TRUE;
251}
252
253DWORD WINAPI CertEnumCRLContextProperties(PCCRL_CONTEXT pCRLContext,
254 DWORD dwPropId)
255{
256 PCONTEXT_PROPERTY_LIST properties = Context_GetProperties(
257 (void *)pCRLContext, sizeof(CRL_CONTEXT));
258 DWORD ret;
259
Juan Langf3a1f2b2006-10-03 21:58:09 -0700260 TRACE("(%p, %d)\n", pCRLContext, dwPropId);
Juan Langc4f2bcf2006-05-25 09:01:03 -0700261
262 if (properties)
263 ret = ContextPropertyList_EnumPropIDs(properties, dwPropId);
264 else
265 ret = 0;
266 return ret;
267}
268
269static BOOL WINAPI CRLContext_SetProperty(void *context, DWORD dwPropId,
270 DWORD dwFlags, const void *pvData);
271
272static BOOL CRLContext_GetHashProp(void *context, DWORD dwPropId,
273 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
274 DWORD *pcbData)
275{
276 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
277 pcbData);
278 if (ret)
279 {
280 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
281
282 ret = CRLContext_SetProperty(context, dwPropId, 0, &blob);
283 }
284 return ret;
285}
286
287static BOOL WINAPI CRLContext_GetProperty(void *context, DWORD dwPropId,
288 void *pvData, DWORD *pcbData)
289{
290 PCCRL_CONTEXT pCRLContext = (PCCRL_CONTEXT)context;
291 PCONTEXT_PROPERTY_LIST properties =
292 Context_GetProperties(context, sizeof(CRL_CONTEXT));
293 BOOL ret;
294 CRYPT_DATA_BLOB blob;
295
Juan Langf3a1f2b2006-10-03 21:58:09 -0700296 TRACE("(%p, %d, %p, %p)\n", context, dwPropId, pvData, pcbData);
Juan Langc4f2bcf2006-05-25 09:01:03 -0700297
298 if (properties)
299 ret = ContextPropertyList_FindProperty(properties, dwPropId, &blob);
300 else
301 ret = FALSE;
302 if (ret)
303 {
304 if (!pvData)
Juan Langc4f2bcf2006-05-25 09:01:03 -0700305 *pcbData = blob.cbData;
Juan Langc4f2bcf2006-05-25 09:01:03 -0700306 else if (*pcbData < blob.cbData)
307 {
308 SetLastError(ERROR_MORE_DATA);
309 *pcbData = blob.cbData;
Juan Lang0170a412007-05-14 18:04:51 -0700310 ret = FALSE;
Juan Langc4f2bcf2006-05-25 09:01:03 -0700311 }
312 else
313 {
314 memcpy(pvData, blob.pbData, blob.cbData);
315 *pcbData = blob.cbData;
Juan Langc4f2bcf2006-05-25 09:01:03 -0700316 }
317 }
318 else
319 {
320 /* Implicit properties */
321 switch (dwPropId)
322 {
323 case CERT_SHA1_HASH_PROP_ID:
324 ret = CRLContext_GetHashProp(context, dwPropId, CALG_SHA1,
325 pCRLContext->pbCrlEncoded, pCRLContext->cbCrlEncoded, pvData,
326 pcbData);
327 break;
328 case CERT_MD5_HASH_PROP_ID:
329 ret = CRLContext_GetHashProp(context, dwPropId, CALG_MD5,
330 pCRLContext->pbCrlEncoded, pCRLContext->cbCrlEncoded, pvData,
331 pcbData);
332 break;
333 default:
334 SetLastError(CRYPT_E_NOT_FOUND);
335 }
336 }
337 TRACE("returning %d\n", ret);
338 return ret;
339}
340
341BOOL WINAPI CertGetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
342 DWORD dwPropId, void *pvData, DWORD *pcbData)
343{
344 BOOL ret;
345
Juan Langf3a1f2b2006-10-03 21:58:09 -0700346 TRACE("(%p, %d, %p, %p)\n", pCRLContext, dwPropId, pvData, pcbData);
Juan Langc4f2bcf2006-05-25 09:01:03 -0700347
348 switch (dwPropId)
349 {
350 case 0:
351 case CERT_CERT_PROP_ID:
352 case CERT_CRL_PROP_ID:
353 case CERT_CTL_PROP_ID:
354 SetLastError(E_INVALIDARG);
355 ret = FALSE;
356 break;
357 case CERT_ACCESS_STATE_PROP_ID:
358 if (!pvData)
359 {
360 *pcbData = sizeof(DWORD);
361 ret = TRUE;
362 }
363 else if (*pcbData < sizeof(DWORD))
364 {
365 SetLastError(ERROR_MORE_DATA);
366 *pcbData = sizeof(DWORD);
367 ret = FALSE;
368 }
369 else
370 {
371 *(DWORD *)pvData =
372 CertStore_GetAccessState(pCRLContext->hCertStore);
373 ret = TRUE;
374 }
375 break;
376 default:
377 ret = CRLContext_GetProperty((void *)pCRLContext, dwPropId, pvData,
378 pcbData);
379 }
380 return ret;
381}
382
383static BOOL WINAPI CRLContext_SetProperty(void *context, DWORD dwPropId,
384 DWORD dwFlags, const void *pvData)
385{
386 PCONTEXT_PROPERTY_LIST properties =
387 Context_GetProperties(context, sizeof(CERT_CONTEXT));
388 BOOL ret;
389
Juan Langf3a1f2b2006-10-03 21:58:09 -0700390 TRACE("(%p, %d, %08x, %p)\n", context, dwPropId, dwFlags, pvData);
Juan Langc4f2bcf2006-05-25 09:01:03 -0700391
392 if (!properties)
393 ret = FALSE;
394 else if (!pvData)
395 {
396 ContextPropertyList_RemoveProperty(properties, dwPropId);
397 ret = TRUE;
398 }
399 else
400 {
401 switch (dwPropId)
402 {
403 case CERT_AUTO_ENROLL_PROP_ID:
404 case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
405 case CERT_DESCRIPTION_PROP_ID:
406 case CERT_FRIENDLY_NAME_PROP_ID:
407 case CERT_HASH_PROP_ID:
408 case CERT_KEY_IDENTIFIER_PROP_ID:
409 case CERT_MD5_HASH_PROP_ID:
410 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
411 case CERT_PUBKEY_ALG_PARA_PROP_ID:
412 case CERT_PVK_FILE_PROP_ID:
413 case CERT_SIGNATURE_HASH_PROP_ID:
414 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
415 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
416 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
417 case CERT_ENROLLMENT_PROP_ID:
418 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
419 case CERT_RENEWAL_PROP_ID:
420 {
421 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
422
423 ret = ContextPropertyList_SetProperty(properties, dwPropId,
424 blob->pbData, blob->cbData);
425 break;
426 }
427 case CERT_DATE_STAMP_PROP_ID:
428 ret = ContextPropertyList_SetProperty(properties, dwPropId,
Andrew Talbot58f9b602006-10-24 21:54:07 +0100429 (const BYTE *)pvData, sizeof(FILETIME));
Juan Langc4f2bcf2006-05-25 09:01:03 -0700430 break;
431 default:
Juan Langf3a1f2b2006-10-03 21:58:09 -0700432 FIXME("%d: stub\n", dwPropId);
Juan Langc4f2bcf2006-05-25 09:01:03 -0700433 ret = FALSE;
434 }
435 }
436 TRACE("returning %d\n", ret);
437 return ret;
438}
439
440BOOL WINAPI CertSetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
441 DWORD dwPropId, DWORD dwFlags, const void *pvData)
442{
443 BOOL ret;
444
Juan Langf3a1f2b2006-10-03 21:58:09 -0700445 TRACE("(%p, %d, %08x, %p)\n", pCRLContext, dwPropId, dwFlags, pvData);
Juan Langc4f2bcf2006-05-25 09:01:03 -0700446
447 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
448 * crashes on most of these, I'll be safer.
449 */
450 switch (dwPropId)
451 {
452 case 0:
453 case CERT_ACCESS_STATE_PROP_ID:
454 case CERT_CERT_PROP_ID:
455 case CERT_CRL_PROP_ID:
456 case CERT_CTL_PROP_ID:
457 SetLastError(E_INVALIDARG);
458 return FALSE;
459 }
460 ret = CRLContext_SetProperty((void *)pCRLContext, dwPropId, dwFlags,
461 pvData);
462 TRACE("returning %d\n", ret);
463 return ret;
464}
Juan Langb29c2332006-05-26 09:48:13 -0700465
Juan Lange8992af2006-06-20 16:06:27 -0700466BOOL WINAPI CertIsValidCRLForCertificate(PCCERT_CONTEXT pCert,
467 PCCRL_CONTEXT pCrl, DWORD dwFlags, void *pvReserved)
468{
Juan Langf3a1f2b2006-10-03 21:58:09 -0700469 TRACE("(%p, %p, %08x, %p)\n", pCert, pCrl, dwFlags, pvReserved);
Juan Lange8992af2006-06-20 16:06:27 -0700470 return TRUE;
471}
472
Andrew Talbote04f6be2007-04-09 20:28:22 +0100473static PCRL_ENTRY CRYPT_FindCertificateInCRL(PCERT_INFO cert, const CRL_INFO *crl)
Juan Lange8992af2006-06-20 16:06:27 -0700474{
475 DWORD i;
476 PCRL_ENTRY entry = NULL;
477
Juan Lange8992af2006-06-20 16:06:27 -0700478 for (i = 0; !entry && i < crl->cCRLEntry; i++)
479 if (CertCompareIntegerBlob(&crl->rgCRLEntry[i].SerialNumber,
480 &cert->SerialNumber))
481 entry = &crl->rgCRLEntry[i];
482 return entry;
483}
484
485BOOL WINAPI CertFindCertificateInCRL(PCCERT_CONTEXT pCert,
486 PCCRL_CONTEXT pCrlContext, DWORD dwFlags, void *pvReserved,
487 PCRL_ENTRY *ppCrlEntry)
488{
Juan Langf3a1f2b2006-10-03 21:58:09 -0700489 TRACE("(%p, %p, %08x, %p, %p)\n", pCert, pCrlContext, dwFlags, pvReserved,
Juan Lange8992af2006-06-20 16:06:27 -0700490 ppCrlEntry);
491
492 *ppCrlEntry = CRYPT_FindCertificateInCRL(pCert->pCertInfo,
493 pCrlContext->pCrlInfo);
494 return TRUE;
495}
496
497BOOL WINAPI CertVerifyCRLRevocation(DWORD dwCertEncodingType,
498 PCERT_INFO pCertId, DWORD cCrlInfo, PCRL_INFO rgpCrlInfo[])
499{
500 DWORD i;
501 PCRL_ENTRY entry = NULL;
502
Juan Langf3a1f2b2006-10-03 21:58:09 -0700503 TRACE("(%08x, %p, %d, %p)\n", dwCertEncodingType, pCertId, cCrlInfo,
Juan Lange8992af2006-06-20 16:06:27 -0700504 rgpCrlInfo);
505
506 for (i = 0; !entry && i < cCrlInfo; i++)
507 entry = CRYPT_FindCertificateInCRL(pCertId, rgpCrlInfo[i]);
508 return entry == NULL;
509}
510
Juan Langb29c2332006-05-26 09:48:13 -0700511LONG WINAPI CertVerifyCRLTimeValidity(LPFILETIME pTimeToVerify,
512 PCRL_INFO pCrlInfo)
513{
514 FILETIME fileTime;
515 LONG ret;
516
517 if (!pTimeToVerify)
518 {
519 SYSTEMTIME sysTime;
520
521 GetSystemTime(&sysTime);
522 SystemTimeToFileTime(&sysTime, &fileTime);
523 pTimeToVerify = &fileTime;
524 }
525 if ((ret = CompareFileTime(pTimeToVerify, &pCrlInfo->ThisUpdate)) >= 0)
526 {
527 ret = CompareFileTime(pTimeToVerify, &pCrlInfo->NextUpdate);
528 if (ret < 0)
529 ret = 0;
530 }
531 return ret;
532}