wldap32: Fix Dutch translation.
[wine/multimedia.git] / dlls / crypt32 / ctl.c
blob40895fa1bdb2e65018a851813977cb60d9e4343f
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 PWINECRYPT_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 PWINECRYPT_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 CryptMemFree(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, sizeof(CTL_CONTEXT));
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, sizeof(CTL_CONTEXT),
485 CTLDataContext_Free);
486 return ret;
489 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
490 DWORD dwPropId)
492 PCONTEXT_PROPERTY_LIST properties = Context_GetProperties(
493 pCTLContext, sizeof(CTL_CONTEXT));
494 DWORD ret;
496 TRACE("(%p, %d)\n", pCTLContext, dwPropId);
498 if (properties)
499 ret = ContextPropertyList_EnumPropIDs(properties, dwPropId);
500 else
501 ret = 0;
502 return ret;
505 static BOOL CTLContext_SetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
506 DWORD dwFlags, const void *pvData);
508 static BOOL CTLContext_GetHashProp(PCCTL_CONTEXT context, DWORD dwPropId,
509 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
510 DWORD *pcbData)
512 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
513 pcbData);
514 if (ret && pvData)
516 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
518 ret = CTLContext_SetProperty(context, dwPropId, 0, &blob);
520 return ret;
523 static BOOL CTLContext_GetProperty(PCCTL_CONTEXT context, DWORD dwPropId,
524 void *pvData, DWORD *pcbData)
526 PCONTEXT_PROPERTY_LIST properties =
527 Context_GetProperties(context, sizeof(CTL_CONTEXT));
528 BOOL ret;
529 CRYPT_DATA_BLOB blob;
531 TRACE("(%p, %d, %p, %p)\n", context, dwPropId, pvData, pcbData);
533 if (properties)
534 ret = ContextPropertyList_FindProperty(properties, dwPropId, &blob);
535 else
536 ret = FALSE;
537 if (ret)
539 if (!pvData)
540 *pcbData = blob.cbData;
541 else if (*pcbData < blob.cbData)
543 SetLastError(ERROR_MORE_DATA);
544 *pcbData = blob.cbData;
545 ret = FALSE;
547 else
549 memcpy(pvData, blob.pbData, blob.cbData);
550 *pcbData = blob.cbData;
553 else
555 /* Implicit properties */
556 switch (dwPropId)
558 case CERT_SHA1_HASH_PROP_ID:
559 ret = CTLContext_GetHashProp(context, dwPropId, CALG_SHA1,
560 context->pbCtlEncoded, context->cbCtlEncoded, pvData, pcbData);
561 break;
562 case CERT_MD5_HASH_PROP_ID:
563 ret = CTLContext_GetHashProp(context, dwPropId, CALG_MD5,
564 context->pbCtlEncoded, context->cbCtlEncoded, pvData, pcbData);
565 break;
566 default:
567 SetLastError(CRYPT_E_NOT_FOUND);
570 TRACE("returning %d\n", ret);
571 return ret;
574 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
575 DWORD dwPropId, void *pvData, DWORD *pcbData)
577 BOOL ret;
579 TRACE("(%p, %d, %p, %p)\n", pCTLContext, dwPropId, pvData, pcbData);
581 switch (dwPropId)
583 case 0:
584 case CERT_CERT_PROP_ID:
585 case CERT_CRL_PROP_ID:
586 case CERT_CTL_PROP_ID:
587 SetLastError(E_INVALIDARG);
588 ret = FALSE;
589 break;
590 case CERT_ACCESS_STATE_PROP_ID:
591 if (!pvData)
593 *pcbData = sizeof(DWORD);
594 ret = TRUE;
596 else if (*pcbData < sizeof(DWORD))
598 SetLastError(ERROR_MORE_DATA);
599 *pcbData = sizeof(DWORD);
600 ret = FALSE;
602 else
604 if (pCTLContext->hCertStore)
605 ret = CertGetStoreProperty(pCTLContext->hCertStore, dwPropId,
606 pvData, pcbData);
607 else
608 *(DWORD *)pvData = 0;
609 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 PCONTEXT_PROPERTY_LIST properties =
623 Context_GetProperties(context, sizeof(CTL_CONTEXT));
624 BOOL ret;
626 TRACE("(%p, %d, %08x, %p)\n", context, dwPropId, dwFlags, pvData);
628 if (!properties)
629 ret = FALSE;
630 else if (!pvData)
632 ContextPropertyList_RemoveProperty(properties, dwPropId);
633 ret = TRUE;
635 else
637 switch (dwPropId)
639 case CERT_AUTO_ENROLL_PROP_ID:
640 case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
641 case CERT_DESCRIPTION_PROP_ID:
642 case CERT_FRIENDLY_NAME_PROP_ID:
643 case CERT_HASH_PROP_ID:
644 case CERT_KEY_IDENTIFIER_PROP_ID:
645 case CERT_MD5_HASH_PROP_ID:
646 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
647 case CERT_PUBKEY_ALG_PARA_PROP_ID:
648 case CERT_PVK_FILE_PROP_ID:
649 case CERT_SIGNATURE_HASH_PROP_ID:
650 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
651 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
652 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
653 case CERT_ENROLLMENT_PROP_ID:
654 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
655 case CERT_RENEWAL_PROP_ID:
657 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
659 ret = ContextPropertyList_SetProperty(properties, dwPropId,
660 blob->pbData, blob->cbData);
661 break;
663 case CERT_DATE_STAMP_PROP_ID:
664 ret = ContextPropertyList_SetProperty(properties, dwPropId,
665 pvData, sizeof(FILETIME));
666 break;
667 default:
668 FIXME("%d: stub\n", dwPropId);
669 ret = FALSE;
672 TRACE("returning %d\n", ret);
673 return ret;
676 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
677 DWORD dwPropId, DWORD dwFlags, const void *pvData)
679 BOOL ret;
681 TRACE("(%p, %d, %08x, %p)\n", pCTLContext, dwPropId, dwFlags, pvData);
683 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
684 * crashes on most of these, I'll be safer.
686 switch (dwPropId)
688 case 0:
689 case CERT_ACCESS_STATE_PROP_ID:
690 case CERT_CERT_PROP_ID:
691 case CERT_CRL_PROP_ID:
692 case CERT_CTL_PROP_ID:
693 SetLastError(E_INVALIDARG);
694 return FALSE;
696 ret = CTLContext_SetProperty(pCTLContext, dwPropId, dwFlags, pvData);
697 TRACE("returning %d\n", ret);
698 return ret;