crypt32: Get rid of no longer used contextSize argument in Context_Release.
[wine.git] / dlls / crypt32 / ctl.c
blob6daa53a3dca6b66d2296a4e7f3b40eec1a46cf5a
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 #define CtlContext_CopyProperties(to, from) \
33 Context_CopyProperties((to), (from), sizeof(CTL_CONTEXT))
35 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
36 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
37 PCCTL_CONTEXT* ppStoreContext)
39 WINECRYPT_CERTSTORE *store = hCertStore;
40 BOOL ret = TRUE;
41 PCCTL_CONTEXT toAdd = NULL, existing = NULL;
43 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCtlContext, dwAddDisposition,
44 ppStoreContext);
46 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
48 existing = CertFindCTLInStore(hCertStore, 0, 0, CTL_FIND_EXISTING,
49 pCtlContext, NULL);
52 switch (dwAddDisposition)
54 case CERT_STORE_ADD_ALWAYS:
55 toAdd = CertDuplicateCTLContext(pCtlContext);
56 break;
57 case CERT_STORE_ADD_NEW:
58 if (existing)
60 TRACE("found matching CTL, not adding\n");
61 SetLastError(CRYPT_E_EXISTS);
62 ret = FALSE;
64 else
65 toAdd = CertDuplicateCTLContext(pCtlContext);
66 break;
67 case CERT_STORE_ADD_NEWER:
68 if (existing)
70 LONG newer = CompareFileTime(&existing->pCtlInfo->ThisUpdate,
71 &pCtlContext->pCtlInfo->ThisUpdate);
73 if (newer < 0)
74 toAdd = CertDuplicateCTLContext(pCtlContext);
75 else
77 TRACE("existing CTL is newer, not adding\n");
78 SetLastError(CRYPT_E_EXISTS);
79 ret = FALSE;
82 else
83 toAdd = CertDuplicateCTLContext(pCtlContext);
84 break;
85 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES:
86 if (existing)
88 LONG newer = CompareFileTime(&existing->pCtlInfo->ThisUpdate,
89 &pCtlContext->pCtlInfo->ThisUpdate);
91 if (newer < 0)
93 toAdd = CertDuplicateCTLContext(pCtlContext);
94 CtlContext_CopyProperties(existing, pCtlContext);
96 else
98 TRACE("existing CTL is newer, not adding\n");
99 SetLastError(CRYPT_E_EXISTS);
100 ret = FALSE;
103 else
104 toAdd = CertDuplicateCTLContext(pCtlContext);
105 break;
106 case CERT_STORE_ADD_REPLACE_EXISTING:
107 toAdd = CertDuplicateCTLContext(pCtlContext);
108 break;
109 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
110 toAdd = CertDuplicateCTLContext(pCtlContext);
111 if (existing)
112 CtlContext_CopyProperties(toAdd, existing);
113 break;
114 case CERT_STORE_ADD_USE_EXISTING:
115 if (existing)
117 CtlContext_CopyProperties(existing, pCtlContext);
118 if (ppStoreContext)
119 *ppStoreContext = CertDuplicateCTLContext(existing);
121 else
122 toAdd = CertDuplicateCTLContext(pCtlContext);
123 break;
124 default:
125 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
126 ret = FALSE;
129 if (toAdd)
131 if (store)
132 ret = store->ctls.addContext(store, (void *)toAdd,
133 (void *)existing, (const void **)ppStoreContext);
134 else if (ppStoreContext)
135 *ppStoreContext = CertDuplicateCTLContext(toAdd);
136 CertFreeCTLContext(toAdd);
138 CertFreeCTLContext(existing);
140 TRACE("returning %d\n", ret);
141 return ret;
144 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
145 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
146 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
148 PCCTL_CONTEXT ctl = CertCreateCTLContext(dwMsgAndCertEncodingType,
149 pbCtlEncoded, cbCtlEncoded);
150 BOOL ret;
152 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore,
153 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
154 ppCtlContext);
156 if (ctl)
158 ret = CertAddCTLContextToStore(hCertStore, ctl, dwAddDisposition,
159 ppCtlContext);
160 CertFreeCTLContext(ctl);
162 else
163 ret = FALSE;
164 return ret;
167 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
168 PCCTL_CONTEXT pPrev)
170 WINECRYPT_CERTSTORE *hcs = hCertStore;
171 PCCTL_CONTEXT ret;
173 TRACE("(%p, %p)\n", hCertStore, pPrev);
174 if (!hCertStore)
175 ret = NULL;
176 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
177 ret = NULL;
178 else
179 ret = (PCCTL_CONTEXT)hcs->ctls.enumContext(hcs, (void *)pPrev);
180 return ret;
183 typedef BOOL (*CtlCompareFunc)(PCCTL_CONTEXT pCtlContext, DWORD dwType,
184 DWORD dwFlags, const void *pvPara);
186 static BOOL compare_ctl_any(PCCTL_CONTEXT pCtlContext, DWORD dwType,
187 DWORD dwFlags, const void *pvPara)
189 return TRUE;
192 static BOOL compare_ctl_by_md5_hash(PCCTL_CONTEXT pCtlContext, DWORD dwType,
193 DWORD dwFlags, const void *pvPara)
195 BOOL ret;
196 BYTE hash[16];
197 DWORD size = sizeof(hash);
199 ret = CertGetCTLContextProperty(pCtlContext, CERT_MD5_HASH_PROP_ID, hash,
200 &size);
201 if (ret)
203 const CRYPT_HASH_BLOB *pHash = pvPara;
205 if (size == pHash->cbData)
206 ret = !memcmp(pHash->pbData, hash, size);
207 else
208 ret = FALSE;
210 return ret;
213 static BOOL compare_ctl_by_sha1_hash(PCCTL_CONTEXT pCtlContext, DWORD dwType,
214 DWORD dwFlags, const void *pvPara)
216 BOOL ret;
217 BYTE hash[20];
218 DWORD size = sizeof(hash);
220 ret = CertGetCTLContextProperty(pCtlContext, CERT_SHA1_HASH_PROP_ID, hash,
221 &size);
222 if (ret)
224 const CRYPT_HASH_BLOB *pHash = pvPara;
226 if (size == pHash->cbData)
227 ret = !memcmp(pHash->pbData, hash, size);
228 else
229 ret = FALSE;
231 return ret;
234 static BOOL compare_ctl_existing(PCCTL_CONTEXT pCtlContext, DWORD dwType,
235 DWORD dwFlags, const void *pvPara)
237 BOOL ret;
239 if (pvPara)
241 PCCTL_CONTEXT ctl = pvPara;
243 if (pCtlContext->cbCtlContext == ctl->cbCtlContext)
245 if (ctl->cbCtlContext)
246 ret = !memcmp(pCtlContext->pbCtlContext, ctl->pbCtlContext,
247 ctl->cbCtlContext);
248 else
249 ret = TRUE;
251 else
252 ret = FALSE;
254 else
255 ret = FALSE;
256 return ret;
259 PCCTL_CONTEXT WINAPI CertFindCTLInStore(HCERTSTORE hCertStore,
260 DWORD dwCertEncodingType, DWORD dwFindFlags, DWORD dwFindType,
261 const void *pvFindPara, PCCTL_CONTEXT pPrevCtlContext)
263 PCCTL_CONTEXT ret;
264 CtlCompareFunc compare;
266 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore, dwCertEncodingType,
267 dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext);
269 switch (dwFindType)
271 case CTL_FIND_ANY:
272 compare = compare_ctl_any;
273 break;
274 case CTL_FIND_SHA1_HASH:
275 compare = compare_ctl_by_sha1_hash;
276 break;
277 case CTL_FIND_MD5_HASH:
278 compare = compare_ctl_by_md5_hash;
279 break;
280 case CTL_FIND_EXISTING:
281 compare = compare_ctl_existing;
282 break;
283 default:
284 FIXME("find type %08x unimplemented\n", dwFindType);
285 compare = NULL;
288 if (compare)
290 BOOL matches = FALSE;
292 ret = pPrevCtlContext;
293 do {
294 ret = CertEnumCTLsInStore(hCertStore, ret);
295 if (ret)
296 matches = compare(ret, dwFindType, dwFindFlags, pvFindPara);
297 } while (ret != NULL && !matches);
298 if (!ret)
299 SetLastError(CRYPT_E_NOT_FOUND);
301 else
303 SetLastError(CRYPT_E_NOT_FOUND);
304 ret = NULL;
306 return ret;
309 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
311 BOOL ret;
313 TRACE("(%p)\n", pCtlContext);
315 if (!pCtlContext)
316 ret = TRUE;
317 else if (!pCtlContext->hCertStore)
318 ret = CertFreeCTLContext(pCtlContext);
319 else
321 WINECRYPT_CERTSTORE *hcs = pCtlContext->hCertStore;
323 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
324 ret = FALSE;
325 else
326 ret = hcs->ctls.deleteContext(hcs, (void *)pCtlContext);
327 if (ret)
328 ret = CertFreeCTLContext(pCtlContext);
330 return ret;
333 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwMsgAndCertEncodingType,
334 const BYTE *pbCtlEncoded, DWORD cbCtlEncoded)
336 PCTL_CONTEXT ctl = NULL;
337 HCRYPTMSG msg;
338 BOOL ret;
339 BYTE *content = NULL;
340 DWORD contentSize = 0, size;
341 PCTL_INFO ctlInfo = NULL;
343 TRACE("(%08x, %p, %d)\n", dwMsgAndCertEncodingType, pbCtlEncoded,
344 cbCtlEncoded);
346 if (GET_CERT_ENCODING_TYPE(dwMsgAndCertEncodingType) != X509_ASN_ENCODING)
348 SetLastError(E_INVALIDARG);
349 return NULL;
351 if (!pbCtlEncoded || !cbCtlEncoded)
353 SetLastError(ERROR_INVALID_DATA);
354 return NULL;
356 msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, 0,
357 0, NULL, NULL);
358 if (!msg)
359 return NULL;
360 ret = CryptMsgUpdate(msg, pbCtlEncoded, cbCtlEncoded, TRUE);
361 if (!ret)
363 SetLastError(ERROR_INVALID_DATA);
364 goto end;
366 /* Check that it's really a CTL */
367 ret = CryptMsgGetParam(msg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL, &size);
368 if (ret)
370 char *innerContent = CryptMemAlloc(size);
372 if (innerContent)
374 ret = CryptMsgGetParam(msg, CMSG_INNER_CONTENT_TYPE_PARAM, 0,
375 innerContent, &size);
376 if (ret)
378 if (strcmp(innerContent, szOID_CTL))
380 SetLastError(ERROR_INVALID_DATA);
381 ret = FALSE;
384 CryptMemFree(innerContent);
386 else
388 SetLastError(ERROR_OUTOFMEMORY);
389 ret = FALSE;
392 if (!ret)
393 goto end;
394 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, NULL, &contentSize);
395 if (!ret)
396 goto end;
397 content = CryptMemAlloc(contentSize);
398 if (content)
400 ret = CryptMsgGetParam(msg, CMSG_CONTENT_PARAM, 0, content,
401 &contentSize);
402 if (ret)
404 ret = CryptDecodeObjectEx(dwMsgAndCertEncodingType, PKCS_CTL,
405 content, contentSize, CRYPT_DECODE_ALLOC_FLAG, NULL,
406 &ctlInfo, &size);
407 if (ret)
409 ctl = Context_CreateDataContext(sizeof(CTL_CONTEXT));
410 if (ctl)
412 BYTE *data = CryptMemAlloc(cbCtlEncoded);
414 if (data)
416 memcpy(data, pbCtlEncoded, cbCtlEncoded);
417 ctl->dwMsgAndCertEncodingType =
418 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
419 ctl->pbCtlEncoded = data;
420 ctl->cbCtlEncoded = cbCtlEncoded;
421 ctl->pCtlInfo = ctlInfo;
422 ctl->hCertStore = NULL;
423 ctl->hCryptMsg = msg;
424 ctl->pbCtlContext = content;
425 ctl->cbCtlContext = contentSize;
427 else
429 SetLastError(ERROR_OUTOFMEMORY);
430 ret = FALSE;
433 else
435 SetLastError(ERROR_OUTOFMEMORY);
436 ret = FALSE;
441 else
443 SetLastError(ERROR_OUTOFMEMORY);
444 ret = FALSE;
447 end:
448 if (!ret)
450 CertFreeCTLContext(ctl);
451 ctl = NULL;
452 LocalFree(ctlInfo);
453 CryptMemFree(content);
454 CryptMsgClose(msg);
456 return ctl;
459 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
461 TRACE("(%p)\n", pCtlContext);
462 if (pCtlContext)
463 Context_AddRef((void *)pCtlContext);
464 return pCtlContext;
467 static void CTLDataContext_Free(void *context)
469 PCTL_CONTEXT ctlContext = context;
471 CryptMsgClose(ctlContext->hCryptMsg);
472 CryptMemFree(ctlContext->pbCtlEncoded);
473 CryptMemFree(ctlContext->pbCtlContext);
474 LocalFree(ctlContext->pCtlInfo);
477 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCTLContext)
479 BOOL ret = TRUE;
481 TRACE("(%p)\n", pCTLContext);
483 if (pCTLContext)
484 ret = Context_Release((void *)pCTLContext, CTLDataContext_Free);
485 return ret;
488 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
489 DWORD dwPropId)
491 CONTEXT_PROPERTY_LIST *properties = Context_GetProperties(pCTLContext);
492 DWORD ret;
494 TRACE("(%p, %d)\n", pCTLContext, dwPropId);
496 if (properties)
497 ret = ContextPropertyList_EnumPropIDs(properties, dwPropId);
498 else
499 ret = 0;
500 return ret;
503 static BOOL CTLContext_SetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
504 DWORD dwFlags, const void *pvData);
506 static BOOL CTLContext_GetHashProp(PCCTL_CONTEXT context, DWORD dwPropId,
507 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
508 DWORD *pcbData)
510 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
511 pcbData);
512 if (ret && pvData)
514 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
516 ret = CTLContext_SetProperty(context, dwPropId, 0, &blob);
518 return ret;
521 static BOOL CTLContext_GetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
522 void *pvData, DWORD *pcbData)
524 CONTEXT_PROPERTY_LIST *properties = Context_GetProperties(context);
525 BOOL ret;
526 CRYPT_DATA_BLOB blob;
528 TRACE("(%p, %d, %p, %p)\n", context, dwPropId, pvData, pcbData);
530 if (properties)
531 ret = ContextPropertyList_FindProperty(properties, dwPropId, &blob);
532 else
533 ret = FALSE;
534 if (ret)
536 if (!pvData)
537 *pcbData = blob.cbData;
538 else if (*pcbData < blob.cbData)
540 SetLastError(ERROR_MORE_DATA);
541 *pcbData = blob.cbData;
542 ret = FALSE;
544 else
546 memcpy(pvData, blob.pbData, blob.cbData);
547 *pcbData = blob.cbData;
550 else
552 /* Implicit properties */
553 switch (dwPropId)
555 case CERT_SHA1_HASH_PROP_ID:
556 ret = CTLContext_GetHashProp(context, dwPropId, CALG_SHA1,
557 context->pbCtlEncoded, context->cbCtlEncoded, pvData, pcbData);
558 break;
559 case CERT_MD5_HASH_PROP_ID:
560 ret = CTLContext_GetHashProp(context, dwPropId, CALG_MD5,
561 context->pbCtlEncoded, context->cbCtlEncoded, pvData, pcbData);
562 break;
563 default:
564 SetLastError(CRYPT_E_NOT_FOUND);
567 TRACE("returning %d\n", ret);
568 return ret;
571 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
572 DWORD dwPropId, void *pvData, DWORD *pcbData)
574 BOOL ret;
576 TRACE("(%p, %d, %p, %p)\n", pCTLContext, dwPropId, pvData, pcbData);
578 switch (dwPropId)
580 case 0:
581 case CERT_CERT_PROP_ID:
582 case CERT_CRL_PROP_ID:
583 case CERT_CTL_PROP_ID:
584 SetLastError(E_INVALIDARG);
585 ret = FALSE;
586 break;
587 case CERT_ACCESS_STATE_PROP_ID:
588 if (!pvData)
590 *pcbData = sizeof(DWORD);
591 ret = TRUE;
593 else if (*pcbData < sizeof(DWORD))
595 SetLastError(ERROR_MORE_DATA);
596 *pcbData = sizeof(DWORD);
597 ret = FALSE;
599 else
601 if (pCTLContext->hCertStore)
602 ret = CertGetStoreProperty(pCTLContext->hCertStore, dwPropId,
603 pvData, pcbData);
604 else
606 *(DWORD *)pvData = 0;
607 ret = TRUE;
610 break;
611 default:
612 ret = CTLContext_GetProperty(pCTLContext, dwPropId, pvData,
613 pcbData);
615 return ret;
618 static BOOL CTLContext_SetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
619 DWORD dwFlags, const void *pvData)
621 CONTEXT_PROPERTY_LIST *properties = Context_GetProperties(context);
622 BOOL ret;
624 TRACE("(%p, %d, %08x, %p)\n", context, dwPropId, dwFlags, pvData);
626 if (!properties)
627 ret = FALSE;
628 else if (!pvData)
630 ContextPropertyList_RemoveProperty(properties, dwPropId);
631 ret = TRUE;
633 else
635 switch (dwPropId)
637 case CERT_AUTO_ENROLL_PROP_ID:
638 case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
639 case CERT_DESCRIPTION_PROP_ID:
640 case CERT_FRIENDLY_NAME_PROP_ID:
641 case CERT_HASH_PROP_ID:
642 case CERT_KEY_IDENTIFIER_PROP_ID:
643 case CERT_MD5_HASH_PROP_ID:
644 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
645 case CERT_PUBKEY_ALG_PARA_PROP_ID:
646 case CERT_PVK_FILE_PROP_ID:
647 case CERT_SIGNATURE_HASH_PROP_ID:
648 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
649 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
650 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
651 case CERT_ENROLLMENT_PROP_ID:
652 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
653 case CERT_RENEWAL_PROP_ID:
655 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
657 ret = ContextPropertyList_SetProperty(properties, dwPropId,
658 blob->pbData, blob->cbData);
659 break;
661 case CERT_DATE_STAMP_PROP_ID:
662 ret = ContextPropertyList_SetProperty(properties, dwPropId,
663 pvData, sizeof(FILETIME));
664 break;
665 default:
666 FIXME("%d: stub\n", dwPropId);
667 ret = FALSE;
670 TRACE("returning %d\n", ret);
671 return ret;
674 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
675 DWORD dwPropId, DWORD dwFlags, const void *pvData)
677 BOOL ret;
679 TRACE("(%p, %d, %08x, %p)\n", pCTLContext, dwPropId, dwFlags, pvData);
681 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
682 * crashes on most of these, I'll be safer.
684 switch (dwPropId)
686 case 0:
687 case CERT_ACCESS_STATE_PROP_ID:
688 case CERT_CERT_PROP_ID:
689 case CERT_CRL_PROP_ID:
690 case CERT_CTL_PROP_ID:
691 SetLastError(E_INVALIDARG);
692 return FALSE;
694 ret = CTLContext_SetProperty(pCTLContext, dwPropId, dwFlags, pvData);
695 TRACE("returning %d\n", ret);
696 return ret;