crypt32: Moved context desatructor to vtbl.
[wine.git] / dlls / crypt32 / ctl.c
blobfb976b4d2c3b544da867379a3918bc7666ff7c59
1 /*
2 * Copyright 2008 Juan Lang
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.
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.
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
20 #include <assert.h>
21 #include <stdarg.h>
23 #define NONAMELESSUNION
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wincrypt.h"
27 #include "wine/debug.h"
28 #include "crypt32_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32 static void CTL_free(context_t *context)
34 ctl_t *ctl = (ctl_t*)context;
36 CryptMsgClose(ctl->ctx.hCryptMsg);
37 CryptMemFree(ctl->ctx.pbCtlEncoded);
38 CryptMemFree(ctl->ctx.pbCtlContext);
39 LocalFree(ctl->ctx.pCtlInfo);
42 static const context_vtbl_t ctl_vtbl = {
43 CTL_free
46 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
47 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
48 PCCTL_CONTEXT* ppStoreContext)
50 WINECRYPT_CERTSTORE *store = hCertStore;
51 BOOL ret = TRUE;
52 PCCTL_CONTEXT toAdd = NULL, existing = NULL;
54 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCtlContext, dwAddDisposition,
55 ppStoreContext);
57 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
59 existing = CertFindCTLInStore(hCertStore, 0, 0, CTL_FIND_EXISTING,
60 pCtlContext, NULL);
63 switch (dwAddDisposition)
65 case CERT_STORE_ADD_ALWAYS:
66 toAdd = CertDuplicateCTLContext(pCtlContext);
67 break;
68 case CERT_STORE_ADD_NEW:
69 if (existing)
71 TRACE("found matching CTL, not adding\n");
72 SetLastError(CRYPT_E_EXISTS);
73 ret = FALSE;
75 else
76 toAdd = CertDuplicateCTLContext(pCtlContext);
77 break;
78 case CERT_STORE_ADD_NEWER:
79 if (existing)
81 LONG newer = CompareFileTime(&existing->pCtlInfo->ThisUpdate,
82 &pCtlContext->pCtlInfo->ThisUpdate);
84 if (newer < 0)
85 toAdd = CertDuplicateCTLContext(pCtlContext);
86 else
88 TRACE("existing CTL is newer, not adding\n");
89 SetLastError(CRYPT_E_EXISTS);
90 ret = FALSE;
93 else
94 toAdd = CertDuplicateCTLContext(pCtlContext);
95 break;
96 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES:
97 if (existing)
99 LONG newer = CompareFileTime(&existing->pCtlInfo->ThisUpdate,
100 &pCtlContext->pCtlInfo->ThisUpdate);
102 if (newer < 0)
104 toAdd = CertDuplicateCTLContext(pCtlContext);
105 Context_CopyProperties(existing, pCtlContext);
107 else
109 TRACE("existing CTL is newer, not adding\n");
110 SetLastError(CRYPT_E_EXISTS);
111 ret = FALSE;
114 else
115 toAdd = CertDuplicateCTLContext(pCtlContext);
116 break;
117 case CERT_STORE_ADD_REPLACE_EXISTING:
118 toAdd = CertDuplicateCTLContext(pCtlContext);
119 break;
120 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
121 toAdd = CertDuplicateCTLContext(pCtlContext);
122 if (existing)
123 Context_CopyProperties(toAdd, existing);
124 break;
125 case CERT_STORE_ADD_USE_EXISTING:
126 if (existing)
128 Context_CopyProperties(existing, pCtlContext);
129 if (ppStoreContext)
130 *ppStoreContext = CertDuplicateCTLContext(existing);
132 else
133 toAdd = CertDuplicateCTLContext(pCtlContext);
134 break;
135 default:
136 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
137 ret = FALSE;
140 if (toAdd)
142 if (store)
143 ret = store->vtbl->ctls.addContext(store, (void *)toAdd,
144 (void *)existing, (const void **)ppStoreContext);
145 else if (ppStoreContext)
146 *ppStoreContext = CertDuplicateCTLContext(toAdd);
147 CertFreeCTLContext(toAdd);
149 CertFreeCTLContext(existing);
151 TRACE("returning %d\n", ret);
152 return ret;
155 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
156 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
157 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
159 PCCTL_CONTEXT ctl = CertCreateCTLContext(dwMsgAndCertEncodingType,
160 pbCtlEncoded, cbCtlEncoded);
161 BOOL ret;
163 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore,
164 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
165 ppCtlContext);
167 if (ctl)
169 ret = CertAddCTLContextToStore(hCertStore, ctl, dwAddDisposition,
170 ppCtlContext);
171 CertFreeCTLContext(ctl);
173 else
174 ret = FALSE;
175 return ret;
178 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
179 PCCTL_CONTEXT pPrev)
181 WINECRYPT_CERTSTORE *hcs = hCertStore;
182 PCCTL_CONTEXT ret;
184 TRACE("(%p, %p)\n", hCertStore, pPrev);
185 if (!hCertStore)
186 ret = NULL;
187 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
188 ret = NULL;
189 else
190 ret = (PCCTL_CONTEXT)hcs->vtbl->ctls.enumContext(hcs, (void *)pPrev);
191 return ret;
194 typedef BOOL (*CtlCompareFunc)(PCCTL_CONTEXT pCtlContext, DWORD dwType,
195 DWORD dwFlags, const void *pvPara);
197 static BOOL compare_ctl_any(PCCTL_CONTEXT pCtlContext, DWORD dwType,
198 DWORD dwFlags, const void *pvPara)
200 return TRUE;
203 static BOOL compare_ctl_by_md5_hash(PCCTL_CONTEXT pCtlContext, DWORD dwType,
204 DWORD dwFlags, const void *pvPara)
206 BOOL ret;
207 BYTE hash[16];
208 DWORD size = sizeof(hash);
210 ret = CertGetCTLContextProperty(pCtlContext, CERT_MD5_HASH_PROP_ID, hash,
211 &size);
212 if (ret)
214 const CRYPT_HASH_BLOB *pHash = pvPara;
216 if (size == pHash->cbData)
217 ret = !memcmp(pHash->pbData, hash, size);
218 else
219 ret = FALSE;
221 return ret;
224 static BOOL compare_ctl_by_sha1_hash(PCCTL_CONTEXT pCtlContext, DWORD dwType,
225 DWORD dwFlags, const void *pvPara)
227 BOOL ret;
228 BYTE hash[20];
229 DWORD size = sizeof(hash);
231 ret = CertGetCTLContextProperty(pCtlContext, CERT_SHA1_HASH_PROP_ID, hash,
232 &size);
233 if (ret)
235 const CRYPT_HASH_BLOB *pHash = pvPara;
237 if (size == pHash->cbData)
238 ret = !memcmp(pHash->pbData, hash, size);
239 else
240 ret = FALSE;
242 return ret;
245 static BOOL compare_ctl_existing(PCCTL_CONTEXT pCtlContext, DWORD dwType,
246 DWORD dwFlags, const void *pvPara)
248 BOOL ret;
250 if (pvPara)
252 PCCTL_CONTEXT ctl = pvPara;
254 if (pCtlContext->cbCtlContext == ctl->cbCtlContext)
256 if (ctl->cbCtlContext)
257 ret = !memcmp(pCtlContext->pbCtlContext, ctl->pbCtlContext,
258 ctl->cbCtlContext);
259 else
260 ret = TRUE;
262 else
263 ret = FALSE;
265 else
266 ret = FALSE;
267 return ret;
270 PCCTL_CONTEXT WINAPI CertFindCTLInStore(HCERTSTORE hCertStore,
271 DWORD dwCertEncodingType, DWORD dwFindFlags, DWORD dwFindType,
272 const void *pvFindPara, PCCTL_CONTEXT pPrevCtlContext)
274 PCCTL_CONTEXT ret;
275 CtlCompareFunc compare;
277 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore, dwCertEncodingType,
278 dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext);
280 switch (dwFindType)
282 case CTL_FIND_ANY:
283 compare = compare_ctl_any;
284 break;
285 case CTL_FIND_SHA1_HASH:
286 compare = compare_ctl_by_sha1_hash;
287 break;
288 case CTL_FIND_MD5_HASH:
289 compare = compare_ctl_by_md5_hash;
290 break;
291 case CTL_FIND_EXISTING:
292 compare = compare_ctl_existing;
293 break;
294 default:
295 FIXME("find type %08x unimplemented\n", dwFindType);
296 compare = NULL;
299 if (compare)
301 BOOL matches = FALSE;
303 ret = pPrevCtlContext;
304 do {
305 ret = CertEnumCTLsInStore(hCertStore, ret);
306 if (ret)
307 matches = compare(ret, dwFindType, dwFindFlags, pvFindPara);
308 } while (ret != NULL && !matches);
309 if (!ret)
310 SetLastError(CRYPT_E_NOT_FOUND);
312 else
314 SetLastError(CRYPT_E_NOT_FOUND);
315 ret = NULL;
317 return ret;
320 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
322 BOOL ret;
324 TRACE("(%p)\n", pCtlContext);
326 if (!pCtlContext)
327 ret = TRUE;
328 else if (!pCtlContext->hCertStore)
329 ret = CertFreeCTLContext(pCtlContext);
330 else
332 WINECRYPT_CERTSTORE *hcs = pCtlContext->hCertStore;
334 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
335 ret = FALSE;
336 else
337 ret = hcs->vtbl->ctls.deleteContext(hcs, (void *)pCtlContext);
338 if (ret)
339 ret = CertFreeCTLContext(pCtlContext);
341 return ret;
344 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwMsgAndCertEncodingType,
345 const BYTE *pbCtlEncoded, DWORD cbCtlEncoded)
347 PCTL_CONTEXT ctl = NULL;
348 HCRYPTMSG msg;
349 BOOL ret;
350 BYTE *content = NULL;
351 DWORD contentSize = 0, size;
352 PCTL_INFO ctlInfo = NULL;
354 TRACE("(%08x, %p, %d)\n", dwMsgAndCertEncodingType, pbCtlEncoded,
355 cbCtlEncoded);
357 if (GET_CERT_ENCODING_TYPE(dwMsgAndCertEncodingType) != X509_ASN_ENCODING)
359 SetLastError(E_INVALIDARG);
360 return NULL;
362 if (!pbCtlEncoded || !cbCtlEncoded)
364 SetLastError(ERROR_INVALID_DATA);
365 return NULL;
367 msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, 0,
368 0, NULL, NULL);
369 if (!msg)
370 return NULL;
371 ret = CryptMsgUpdate(msg, pbCtlEncoded, cbCtlEncoded, TRUE);
372 if (!ret)
374 SetLastError(ERROR_INVALID_DATA);
375 goto end;
377 /* Check that it's really a CTL */
378 ret = CryptMsgGetParam(msg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL, &size);
379 if (ret)
381 char *innerContent = CryptMemAlloc(size);
383 if (innerContent)
385 ret = CryptMsgGetParam(msg, CMSG_INNER_CONTENT_TYPE_PARAM, 0,
386 innerContent, &size);
387 if (ret)
389 if (strcmp(innerContent, szOID_CTL))
391 SetLastError(ERROR_INVALID_DATA);
392 ret = FALSE;
395 CryptMemFree(innerContent);
397 else
399 SetLastError(ERROR_OUTOFMEMORY);
400 ret = FALSE;
403 if (!ret)
404 goto end;
405 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, NULL, &contentSize);
406 if (!ret)
407 goto end;
408 content = CryptMemAlloc(contentSize);
409 if (content)
411 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, content,
412 &contentSize);
413 if (ret)
415 ret = CryptDecodeObjectEx(dwMsgAndCertEncodingType, PKCS_CTL,
416 content, contentSize, CRYPT_DECODE_ALLOC_FLAG, NULL,
417 &ctlInfo, &size);
418 if (ret)
420 ctl = Context_CreateDataContext(sizeof(CTL_CONTEXT), &ctl_vtbl);
421 if (ctl)
423 BYTE *data = CryptMemAlloc(cbCtlEncoded);
425 if (data)
427 memcpy(data, pbCtlEncoded, cbCtlEncoded);
428 ctl->dwMsgAndCertEncodingType =
429 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
430 ctl->pbCtlEncoded = data;
431 ctl->cbCtlEncoded = cbCtlEncoded;
432 ctl->pCtlInfo = ctlInfo;
433 ctl->hCertStore = NULL;
434 ctl->hCryptMsg = msg;
435 ctl->pbCtlContext = content;
436 ctl->cbCtlContext = contentSize;
438 else
440 SetLastError(ERROR_OUTOFMEMORY);
441 ret = FALSE;
444 else
446 SetLastError(ERROR_OUTOFMEMORY);
447 ret = FALSE;
452 else
454 SetLastError(ERROR_OUTOFMEMORY);
455 ret = FALSE;
458 end:
459 if (!ret)
461 CertFreeCTLContext(ctl);
462 ctl = NULL;
463 LocalFree(ctlInfo);
464 CryptMemFree(content);
465 CryptMsgClose(msg);
467 return ctl;
470 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
472 TRACE("(%p)\n", pCtlContext);
473 if (pCtlContext)
474 Context_AddRef(&ctl_from_ptr(pCtlContext)->base);
475 return pCtlContext;
478 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCTLContext)
480 BOOL ret = TRUE;
482 TRACE("(%p)\n", pCTLContext);
484 if (pCTLContext)
485 ret = Context_Release(&ctl_from_ptr(pCTLContext)->base);
486 return ret;
489 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
490 DWORD dwPropId)
492 CONTEXT_PROPERTY_LIST *properties = Context_GetProperties(pCTLContext);
493 DWORD ret;
495 TRACE("(%p, %d)\n", pCTLContext, dwPropId);
497 if (properties)
498 ret = ContextPropertyList_EnumPropIDs(properties, dwPropId);
499 else
500 ret = 0;
501 return ret;
504 static BOOL CTLContext_SetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
505 DWORD dwFlags, const void *pvData);
507 static BOOL CTLContext_GetHashProp(PCCTL_CONTEXT context, DWORD dwPropId,
508 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
509 DWORD *pcbData)
511 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
512 pcbData);
513 if (ret && pvData)
515 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
517 ret = CTLContext_SetProperty(context, dwPropId, 0, &blob);
519 return ret;
522 static BOOL CTLContext_GetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
523 void *pvData, DWORD *pcbData)
525 CONTEXT_PROPERTY_LIST *properties = Context_GetProperties(context);
526 BOOL ret;
527 CRYPT_DATA_BLOB blob;
529 TRACE("(%p, %d, %p, %p)\n", context, dwPropId, pvData, pcbData);
531 if (properties)
532 ret = ContextPropertyList_FindProperty(properties, dwPropId, &blob);
533 else
534 ret = FALSE;
535 if (ret)
537 if (!pvData)
538 *pcbData = blob.cbData;
539 else if (*pcbData < blob.cbData)
541 SetLastError(ERROR_MORE_DATA);
542 *pcbData = blob.cbData;
543 ret = FALSE;
545 else
547 memcpy(pvData, blob.pbData, blob.cbData);
548 *pcbData = blob.cbData;
551 else
553 /* Implicit properties */
554 switch (dwPropId)
556 case CERT_SHA1_HASH_PROP_ID:
557 ret = CTLContext_GetHashProp(context, dwPropId, CALG_SHA1,
558 context->pbCtlEncoded, context->cbCtlEncoded, pvData, pcbData);
559 break;
560 case CERT_MD5_HASH_PROP_ID:
561 ret = CTLContext_GetHashProp(context, dwPropId, CALG_MD5,
562 context->pbCtlEncoded, context->cbCtlEncoded, pvData, pcbData);
563 break;
564 default:
565 SetLastError(CRYPT_E_NOT_FOUND);
568 TRACE("returning %d\n", ret);
569 return ret;
572 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
573 DWORD dwPropId, void *pvData, DWORD *pcbData)
575 BOOL ret;
577 TRACE("(%p, %d, %p, %p)\n", pCTLContext, dwPropId, pvData, pcbData);
579 switch (dwPropId)
581 case 0:
582 case CERT_CERT_PROP_ID:
583 case CERT_CRL_PROP_ID:
584 case CERT_CTL_PROP_ID:
585 SetLastError(E_INVALIDARG);
586 ret = FALSE;
587 break;
588 case CERT_ACCESS_STATE_PROP_ID:
589 if (!pvData)
591 *pcbData = sizeof(DWORD);
592 ret = TRUE;
594 else if (*pcbData < sizeof(DWORD))
596 SetLastError(ERROR_MORE_DATA);
597 *pcbData = sizeof(DWORD);
598 ret = FALSE;
600 else
602 if (pCTLContext->hCertStore)
603 ret = CertGetStoreProperty(pCTLContext->hCertStore, dwPropId,
604 pvData, pcbData);
605 else
607 *(DWORD *)pvData = 0;
608 ret = TRUE;
611 break;
612 default:
613 ret = CTLContext_GetProperty(pCTLContext, dwPropId, pvData,
614 pcbData);
616 return ret;
619 static BOOL CTLContext_SetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
620 DWORD dwFlags, const void *pvData)
622 CONTEXT_PROPERTY_LIST *properties = Context_GetProperties(context);
623 BOOL ret;
625 TRACE("(%p, %d, %08x, %p)\n", context, dwPropId, dwFlags, pvData);
627 if (!properties)
628 ret = FALSE;
629 else if (!pvData)
631 ContextPropertyList_RemoveProperty(properties, dwPropId);
632 ret = TRUE;
634 else
636 switch (dwPropId)
638 case CERT_AUTO_ENROLL_PROP_ID:
639 case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
640 case CERT_DESCRIPTION_PROP_ID:
641 case CERT_FRIENDLY_NAME_PROP_ID:
642 case CERT_HASH_PROP_ID:
643 case CERT_KEY_IDENTIFIER_PROP_ID:
644 case CERT_MD5_HASH_PROP_ID:
645 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
646 case CERT_PUBKEY_ALG_PARA_PROP_ID:
647 case CERT_PVK_FILE_PROP_ID:
648 case CERT_SIGNATURE_HASH_PROP_ID:
649 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
650 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
651 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
652 case CERT_ENROLLMENT_PROP_ID:
653 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
654 case CERT_RENEWAL_PROP_ID:
656 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
658 ret = ContextPropertyList_SetProperty(properties, dwPropId,
659 blob->pbData, blob->cbData);
660 break;
662 case CERT_DATE_STAMP_PROP_ID:
663 ret = ContextPropertyList_SetProperty(properties, dwPropId,
664 pvData, sizeof(FILETIME));
665 break;
666 default:
667 FIXME("%d: stub\n", dwPropId);
668 ret = FALSE;
671 TRACE("returning %d\n", ret);
672 return ret;
675 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
676 DWORD dwPropId, DWORD dwFlags, const void *pvData)
678 BOOL ret;
680 TRACE("(%p, %d, %08x, %p)\n", pCTLContext, dwPropId, dwFlags, pvData);
682 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
683 * crashes on most of these, I'll be safer.
685 switch (dwPropId)
687 case 0:
688 case CERT_ACCESS_STATE_PROP_ID:
689 case CERT_CERT_PROP_ID:
690 case CERT_CRL_PROP_ID:
691 case CERT_CTL_PROP_ID:
692 SetLastError(E_INVALIDARG);
693 return FALSE;
695 ret = CTLContext_SetProperty(pCTLContext, dwPropId, dwFlags, pvData);
696 TRACE("returning %d\n", ret);
697 return ret;