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
23 #define NONAMELESSUNION
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
;
41 PCCTL_CONTEXT toAdd
= NULL
, existing
= NULL
;
43 TRACE("(%p, %p, %08x, %p)\n", hCertStore
, pCtlContext
, dwAddDisposition
,
46 if (dwAddDisposition
!= CERT_STORE_ADD_ALWAYS
)
48 existing
= CertFindCTLInStore(hCertStore
, 0, 0, CTL_FIND_EXISTING
,
52 switch (dwAddDisposition
)
54 case CERT_STORE_ADD_ALWAYS
:
55 toAdd
= CertDuplicateCTLContext(pCtlContext
);
57 case CERT_STORE_ADD_NEW
:
60 TRACE("found matching CTL, not adding\n");
61 SetLastError(CRYPT_E_EXISTS
);
65 toAdd
= CertDuplicateCTLContext(pCtlContext
);
67 case CERT_STORE_ADD_NEWER
:
70 LONG newer
= CompareFileTime(&existing
->pCtlInfo
->ThisUpdate
,
71 &pCtlContext
->pCtlInfo
->ThisUpdate
);
74 toAdd
= CertDuplicateCTLContext(pCtlContext
);
77 TRACE("existing CTL is newer, not adding\n");
78 SetLastError(CRYPT_E_EXISTS
);
83 toAdd
= CertDuplicateCTLContext(pCtlContext
);
85 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
88 LONG newer
= CompareFileTime(&existing
->pCtlInfo
->ThisUpdate
,
89 &pCtlContext
->pCtlInfo
->ThisUpdate
);
93 toAdd
= CertDuplicateCTLContext(pCtlContext
);
94 CtlContext_CopyProperties(existing
, pCtlContext
);
98 TRACE("existing CTL is newer, not adding\n");
99 SetLastError(CRYPT_E_EXISTS
);
104 toAdd
= CertDuplicateCTLContext(pCtlContext
);
106 case CERT_STORE_ADD_REPLACE_EXISTING
:
107 toAdd
= CertDuplicateCTLContext(pCtlContext
);
109 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
110 toAdd
= CertDuplicateCTLContext(pCtlContext
);
112 CtlContext_CopyProperties(toAdd
, existing
);
114 case CERT_STORE_ADD_USE_EXISTING
:
117 CtlContext_CopyProperties(existing
, pCtlContext
);
119 *ppStoreContext
= CertDuplicateCTLContext(existing
);
122 toAdd
= CertDuplicateCTLContext(pCtlContext
);
125 FIXME("Unimplemented add disposition %d\n", dwAddDisposition
);
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
);
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
);
152 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
,
153 dwMsgAndCertEncodingType
, pbCtlEncoded
, cbCtlEncoded
, dwAddDisposition
,
158 ret
= CertAddCTLContextToStore(hCertStore
, ctl
, dwAddDisposition
,
160 CertFreeCTLContext(ctl
);
167 PCCTL_CONTEXT WINAPI
CertEnumCTLsInStore(HCERTSTORE hCertStore
,
170 WINECRYPT_CERTSTORE
*hcs
= hCertStore
;
173 TRACE("(%p, %p)\n", hCertStore
, pPrev
);
176 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
179 ret
= (PCCTL_CONTEXT
)hcs
->ctls
.enumContext(hcs
, (void *)pPrev
);
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
)
192 static BOOL
compare_ctl_by_md5_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
193 DWORD dwFlags
, const void *pvPara
)
197 DWORD size
= sizeof(hash
);
199 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_MD5_HASH_PROP_ID
, hash
,
203 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
205 if (size
== pHash
->cbData
)
206 ret
= !memcmp(pHash
->pbData
, hash
, size
);
213 static BOOL
compare_ctl_by_sha1_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
214 DWORD dwFlags
, const void *pvPara
)
218 DWORD size
= sizeof(hash
);
220 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_SHA1_HASH_PROP_ID
, hash
,
224 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
226 if (size
== pHash
->cbData
)
227 ret
= !memcmp(pHash
->pbData
, hash
, size
);
234 static BOOL
compare_ctl_existing(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
235 DWORD dwFlags
, const void *pvPara
)
241 PCCTL_CONTEXT ctl
= pvPara
;
243 if (pCtlContext
->cbCtlContext
== ctl
->cbCtlContext
)
245 if (ctl
->cbCtlContext
)
246 ret
= !memcmp(pCtlContext
->pbCtlContext
, ctl
->pbCtlContext
,
259 PCCTL_CONTEXT WINAPI
CertFindCTLInStore(HCERTSTORE hCertStore
,
260 DWORD dwCertEncodingType
, DWORD dwFindFlags
, DWORD dwFindType
,
261 const void *pvFindPara
, PCCTL_CONTEXT pPrevCtlContext
)
264 CtlCompareFunc compare
;
266 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore
, dwCertEncodingType
,
267 dwFindFlags
, dwFindType
, pvFindPara
, pPrevCtlContext
);
272 compare
= compare_ctl_any
;
274 case CTL_FIND_SHA1_HASH
:
275 compare
= compare_ctl_by_sha1_hash
;
277 case CTL_FIND_MD5_HASH
:
278 compare
= compare_ctl_by_md5_hash
;
280 case CTL_FIND_EXISTING
:
281 compare
= compare_ctl_existing
;
284 FIXME("find type %08x unimplemented\n", dwFindType
);
290 BOOL matches
= FALSE
;
292 ret
= pPrevCtlContext
;
294 ret
= CertEnumCTLsInStore(hCertStore
, ret
);
296 matches
= compare(ret
, dwFindType
, dwFindFlags
, pvFindPara
);
297 } while (ret
!= NULL
&& !matches
);
299 SetLastError(CRYPT_E_NOT_FOUND
);
303 SetLastError(CRYPT_E_NOT_FOUND
);
309 BOOL WINAPI
CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext
)
313 TRACE("(%p)\n", pCtlContext
);
317 else if (!pCtlContext
->hCertStore
)
318 ret
= CertFreeCTLContext(pCtlContext
);
321 PWINECRYPT_CERTSTORE hcs
= pCtlContext
->hCertStore
;
323 if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
326 ret
= hcs
->ctls
.deleteContext(hcs
, (void *)pCtlContext
);
328 ret
= CertFreeCTLContext(pCtlContext
);
333 PCCTL_CONTEXT WINAPI
CertCreateCTLContext(DWORD dwMsgAndCertEncodingType
,
334 const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
)
336 PCTL_CONTEXT ctl
= NULL
;
339 BYTE
*content
= NULL
;
340 DWORD contentSize
= 0, size
;
341 PCTL_INFO ctlInfo
= NULL
;
343 TRACE("(%08x, %p, %d)\n", dwMsgAndCertEncodingType
, pbCtlEncoded
,
346 if (GET_CERT_ENCODING_TYPE(dwMsgAndCertEncodingType
) != X509_ASN_ENCODING
)
348 SetLastError(E_INVALIDARG
);
351 if (!pbCtlEncoded
|| !cbCtlEncoded
)
353 SetLastError(ERROR_INVALID_DATA
);
356 msg
= CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING
| X509_ASN_ENCODING
, 0, 0,
360 ret
= CryptMsgUpdate(msg
, pbCtlEncoded
, cbCtlEncoded
, TRUE
);
363 SetLastError(ERROR_INVALID_DATA
);
366 /* Check that it's really a CTL */
367 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
, &size
);
370 char *innerContent
= CryptMemAlloc(size
);
374 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0,
375 innerContent
, &size
);
378 if (strcmp(innerContent
, szOID_CTL
))
380 SetLastError(ERROR_INVALID_DATA
);
384 CryptMemFree(innerContent
);
388 SetLastError(ERROR_OUTOFMEMORY
);
394 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, NULL
, &contentSize
);
397 content
= CryptMemAlloc(contentSize
);
400 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, content
,
404 ret
= CryptDecodeObjectEx(dwMsgAndCertEncodingType
, PKCS_CTL
,
405 content
, contentSize
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
409 ctl
= Context_CreateDataContext(sizeof(CTL_CONTEXT
));
412 BYTE
*data
= CryptMemAlloc(cbCtlEncoded
);
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
;
429 SetLastError(ERROR_OUTOFMEMORY
);
435 SetLastError(ERROR_OUTOFMEMORY
);
443 SetLastError(ERROR_OUTOFMEMORY
);
450 CertFreeCTLContext(ctl
);
453 CryptMemFree(content
);
459 PCCTL_CONTEXT WINAPI
CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext
)
461 TRACE("(%p)\n", pCtlContext
);
463 Context_AddRef((void *)pCtlContext
, sizeof(CTL_CONTEXT
));
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
)
481 TRACE("(%p)\n", pCTLContext
);
484 ret
= Context_Release((void *)pCTLContext
, sizeof(CTL_CONTEXT
),
485 CTLDataContext_Free
);
489 DWORD WINAPI
CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext
,
492 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(
493 pCTLContext
, sizeof(CTL_CONTEXT
));
496 TRACE("(%p, %d)\n", pCTLContext
, dwPropId
);
499 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
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
,
512 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
516 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
518 ret
= CTLContext_SetProperty(context
, dwPropId
, 0, &blob
);
523 static BOOL
CTLContext_GetProperty(PCCTL_CONTEXT context
, DWORD dwPropId
,
524 void *pvData
, DWORD
*pcbData
)
526 CONTEXT_PROPERTY_LIST
*properties
=
527 Context_GetProperties(context
, sizeof(CTL_CONTEXT
));
529 CRYPT_DATA_BLOB blob
;
531 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
534 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
540 *pcbData
= blob
.cbData
;
541 else if (*pcbData
< blob
.cbData
)
543 SetLastError(ERROR_MORE_DATA
);
544 *pcbData
= blob
.cbData
;
549 memcpy(pvData
, blob
.pbData
, blob
.cbData
);
550 *pcbData
= blob
.cbData
;
555 /* Implicit properties */
558 case CERT_SHA1_HASH_PROP_ID
:
559 ret
= CTLContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
560 context
->pbCtlEncoded
, context
->cbCtlEncoded
, pvData
, pcbData
);
562 case CERT_MD5_HASH_PROP_ID
:
563 ret
= CTLContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
564 context
->pbCtlEncoded
, context
->cbCtlEncoded
, pvData
, pcbData
);
567 SetLastError(CRYPT_E_NOT_FOUND
);
570 TRACE("returning %d\n", ret
);
574 BOOL WINAPI
CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
575 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
579 TRACE("(%p, %d, %p, %p)\n", pCTLContext
, dwPropId
, pvData
, pcbData
);
584 case CERT_CERT_PROP_ID
:
585 case CERT_CRL_PROP_ID
:
586 case CERT_CTL_PROP_ID
:
587 SetLastError(E_INVALIDARG
);
590 case CERT_ACCESS_STATE_PROP_ID
:
593 *pcbData
= sizeof(DWORD
);
596 else if (*pcbData
< sizeof(DWORD
))
598 SetLastError(ERROR_MORE_DATA
);
599 *pcbData
= sizeof(DWORD
);
604 if (pCTLContext
->hCertStore
)
605 ret
= CertGetStoreProperty(pCTLContext
->hCertStore
, dwPropId
,
609 *(DWORD
*)pvData
= 0;
615 ret
= CTLContext_GetProperty(pCTLContext
, dwPropId
, pvData
,
621 static BOOL
CTLContext_SetProperty(PCCTL_CONTEXT context
, DWORD dwPropId
,
622 DWORD dwFlags
, const void *pvData
)
624 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(context
, sizeof(CTL_CONTEXT
));
627 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
633 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
640 case CERT_AUTO_ENROLL_PROP_ID
:
641 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
642 case CERT_DESCRIPTION_PROP_ID
:
643 case CERT_FRIENDLY_NAME_PROP_ID
:
644 case CERT_HASH_PROP_ID
:
645 case CERT_KEY_IDENTIFIER_PROP_ID
:
646 case CERT_MD5_HASH_PROP_ID
:
647 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
648 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
649 case CERT_PVK_FILE_PROP_ID
:
650 case CERT_SIGNATURE_HASH_PROP_ID
:
651 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
652 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
653 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
654 case CERT_ENROLLMENT_PROP_ID
:
655 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
656 case CERT_RENEWAL_PROP_ID
:
658 PCRYPT_DATA_BLOB blob
= (PCRYPT_DATA_BLOB
)pvData
;
660 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
661 blob
->pbData
, blob
->cbData
);
664 case CERT_DATE_STAMP_PROP_ID
:
665 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
666 pvData
, sizeof(FILETIME
));
669 FIXME("%d: stub\n", dwPropId
);
673 TRACE("returning %d\n", ret
);
677 BOOL WINAPI
CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
678 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
682 TRACE("(%p, %d, %08x, %p)\n", pCTLContext
, dwPropId
, dwFlags
, pvData
);
684 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
685 * crashes on most of these, I'll be safer.
690 case CERT_ACCESS_STATE_PROP_ID
:
691 case CERT_CERT_PROP_ID
:
692 case CERT_CRL_PROP_ID
:
693 case CERT_CTL_PROP_ID
:
694 SetLastError(E_INVALIDARG
);
697 ret
= CTLContext_SetProperty(pCTLContext
, dwPropId
, dwFlags
, pvData
);
698 TRACE("returning %d\n", ret
);