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 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 context_t
*CTL_clone(context_t
*context
, WINECRYPT_CERTSTORE
*store
, BOOL use_link
)
47 FIXME("Only links supported\n");
51 ctl
= (ctl_t
*)Context_CreateLinkContext(sizeof(CTL_CONTEXT
), context
);
55 ctl
->ctx
.hCertStore
= store
;
59 static const context_vtbl_t ctl_vtbl
= {
64 BOOL WINAPI
CertAddCTLContextToStore(HCERTSTORE hCertStore
,
65 PCCTL_CONTEXT pCtlContext
, DWORD dwAddDisposition
,
66 PCCTL_CONTEXT
* ppStoreContext
)
68 WINECRYPT_CERTSTORE
*store
= hCertStore
;
70 PCCTL_CONTEXT toAdd
= NULL
, existing
= NULL
;
72 TRACE("(%p, %p, %08x, %p)\n", hCertStore
, pCtlContext
, dwAddDisposition
,
75 if (dwAddDisposition
!= CERT_STORE_ADD_ALWAYS
)
77 existing
= CertFindCTLInStore(hCertStore
, 0, 0, CTL_FIND_EXISTING
,
81 switch (dwAddDisposition
)
83 case CERT_STORE_ADD_ALWAYS
:
84 toAdd
= CertDuplicateCTLContext(pCtlContext
);
86 case CERT_STORE_ADD_NEW
:
89 TRACE("found matching CTL, not adding\n");
90 SetLastError(CRYPT_E_EXISTS
);
94 toAdd
= CertDuplicateCTLContext(pCtlContext
);
96 case CERT_STORE_ADD_NEWER
:
99 LONG newer
= CompareFileTime(&existing
->pCtlInfo
->ThisUpdate
,
100 &pCtlContext
->pCtlInfo
->ThisUpdate
);
103 toAdd
= CertDuplicateCTLContext(pCtlContext
);
106 TRACE("existing CTL is newer, not adding\n");
107 SetLastError(CRYPT_E_EXISTS
);
112 toAdd
= CertDuplicateCTLContext(pCtlContext
);
114 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
117 LONG newer
= CompareFileTime(&existing
->pCtlInfo
->ThisUpdate
,
118 &pCtlContext
->pCtlInfo
->ThisUpdate
);
122 toAdd
= CertDuplicateCTLContext(pCtlContext
);
123 Context_CopyProperties(existing
, pCtlContext
);
127 TRACE("existing CTL is newer, not adding\n");
128 SetLastError(CRYPT_E_EXISTS
);
133 toAdd
= CertDuplicateCTLContext(pCtlContext
);
135 case CERT_STORE_ADD_REPLACE_EXISTING
:
136 toAdd
= CertDuplicateCTLContext(pCtlContext
);
138 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
139 toAdd
= CertDuplicateCTLContext(pCtlContext
);
141 Context_CopyProperties(toAdd
, existing
);
143 case CERT_STORE_ADD_USE_EXISTING
:
146 Context_CopyProperties(existing
, pCtlContext
);
148 *ppStoreContext
= CertDuplicateCTLContext(existing
);
151 toAdd
= CertDuplicateCTLContext(pCtlContext
);
154 FIXME("Unimplemented add disposition %d\n", dwAddDisposition
);
161 ret
= store
->vtbl
->ctls
.addContext(store
, (void *)toAdd
,
162 (void *)existing
, (const void **)ppStoreContext
, TRUE
);
163 else if (ppStoreContext
)
164 *ppStoreContext
= CertDuplicateCTLContext(toAdd
);
165 CertFreeCTLContext(toAdd
);
167 CertFreeCTLContext(existing
);
169 TRACE("returning %d\n", ret
);
173 BOOL WINAPI
CertAddEncodedCTLToStore(HCERTSTORE hCertStore
,
174 DWORD dwMsgAndCertEncodingType
, const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
,
175 DWORD dwAddDisposition
, PCCTL_CONTEXT
*ppCtlContext
)
177 PCCTL_CONTEXT ctl
= CertCreateCTLContext(dwMsgAndCertEncodingType
,
178 pbCtlEncoded
, cbCtlEncoded
);
181 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
,
182 dwMsgAndCertEncodingType
, pbCtlEncoded
, cbCtlEncoded
, dwAddDisposition
,
187 ret
= CertAddCTLContextToStore(hCertStore
, ctl
, dwAddDisposition
,
189 CertFreeCTLContext(ctl
);
196 PCCTL_CONTEXT WINAPI
CertEnumCTLsInStore(HCERTSTORE hCertStore
,
199 WINECRYPT_CERTSTORE
*hcs
= hCertStore
;
202 TRACE("(%p, %p)\n", hCertStore
, pPrev
);
205 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
208 ret
= (PCCTL_CONTEXT
)hcs
->vtbl
->ctls
.enumContext(hcs
, (void *)pPrev
);
212 typedef BOOL (*CtlCompareFunc
)(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
213 DWORD dwFlags
, const void *pvPara
);
215 static BOOL
compare_ctl_any(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
216 DWORD dwFlags
, const void *pvPara
)
221 static BOOL
compare_ctl_by_md5_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
222 DWORD dwFlags
, const void *pvPara
)
226 DWORD size
= sizeof(hash
);
228 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_MD5_HASH_PROP_ID
, hash
,
232 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
234 if (size
== pHash
->cbData
)
235 ret
= !memcmp(pHash
->pbData
, hash
, size
);
242 static BOOL
compare_ctl_by_sha1_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
243 DWORD dwFlags
, const void *pvPara
)
247 DWORD size
= sizeof(hash
);
249 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_SHA1_HASH_PROP_ID
, hash
,
253 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
255 if (size
== pHash
->cbData
)
256 ret
= !memcmp(pHash
->pbData
, hash
, size
);
263 static BOOL
compare_ctl_existing(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
264 DWORD dwFlags
, const void *pvPara
)
270 PCCTL_CONTEXT ctl
= pvPara
;
272 if (pCtlContext
->cbCtlContext
== ctl
->cbCtlContext
)
274 if (ctl
->cbCtlContext
)
275 ret
= !memcmp(pCtlContext
->pbCtlContext
, ctl
->pbCtlContext
,
288 PCCTL_CONTEXT WINAPI
CertFindCTLInStore(HCERTSTORE hCertStore
,
289 DWORD dwCertEncodingType
, DWORD dwFindFlags
, DWORD dwFindType
,
290 const void *pvFindPara
, PCCTL_CONTEXT pPrevCtlContext
)
293 CtlCompareFunc compare
;
295 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore
, dwCertEncodingType
,
296 dwFindFlags
, dwFindType
, pvFindPara
, pPrevCtlContext
);
301 compare
= compare_ctl_any
;
303 case CTL_FIND_SHA1_HASH
:
304 compare
= compare_ctl_by_sha1_hash
;
306 case CTL_FIND_MD5_HASH
:
307 compare
= compare_ctl_by_md5_hash
;
309 case CTL_FIND_EXISTING
:
310 compare
= compare_ctl_existing
;
313 FIXME("find type %08x unimplemented\n", dwFindType
);
319 BOOL matches
= FALSE
;
321 ret
= pPrevCtlContext
;
323 ret
= CertEnumCTLsInStore(hCertStore
, ret
);
325 matches
= compare(ret
, dwFindType
, dwFindFlags
, pvFindPara
);
326 } while (ret
!= NULL
&& !matches
);
328 SetLastError(CRYPT_E_NOT_FOUND
);
332 SetLastError(CRYPT_E_NOT_FOUND
);
338 BOOL WINAPI
CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext
)
342 TRACE("(%p)\n", pCtlContext
);
346 else if (!pCtlContext
->hCertStore
)
347 ret
= CertFreeCTLContext(pCtlContext
);
350 WINECRYPT_CERTSTORE
*hcs
= pCtlContext
->hCertStore
;
351 ctl_t
*ctl
= ctl_from_ptr(pCtlContext
);
353 if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
356 ret
= hcs
->vtbl
->ctls
.delete(hcs
, &ctl
->base
);
358 ret
= CertFreeCTLContext(pCtlContext
);
363 PCCTL_CONTEXT WINAPI
CertCreateCTLContext(DWORD dwMsgAndCertEncodingType
,
364 const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
)
366 PCTL_CONTEXT ctl
= NULL
;
369 BYTE
*content
= NULL
;
370 DWORD contentSize
= 0, size
;
371 PCTL_INFO ctlInfo
= NULL
;
373 TRACE("(%08x, %p, %d)\n", dwMsgAndCertEncodingType
, pbCtlEncoded
,
376 if (GET_CERT_ENCODING_TYPE(dwMsgAndCertEncodingType
) != X509_ASN_ENCODING
)
378 SetLastError(E_INVALIDARG
);
381 if (!pbCtlEncoded
|| !cbCtlEncoded
)
383 SetLastError(ERROR_INVALID_DATA
);
386 msg
= CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING
| X509_ASN_ENCODING
, 0, 0,
390 ret
= CryptMsgUpdate(msg
, pbCtlEncoded
, cbCtlEncoded
, TRUE
);
393 SetLastError(ERROR_INVALID_DATA
);
396 /* Check that it's really a CTL */
397 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
, &size
);
400 char *innerContent
= CryptMemAlloc(size
);
404 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0,
405 innerContent
, &size
);
408 if (strcmp(innerContent
, szOID_CTL
))
410 SetLastError(ERROR_INVALID_DATA
);
414 CryptMemFree(innerContent
);
418 SetLastError(ERROR_OUTOFMEMORY
);
424 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, NULL
, &contentSize
);
427 content
= CryptMemAlloc(contentSize
);
430 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, content
,
434 ret
= CryptDecodeObjectEx(dwMsgAndCertEncodingType
, PKCS_CTL
,
435 content
, contentSize
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
439 ctl
= Context_CreateDataContext(sizeof(CTL_CONTEXT
), &ctl_vtbl
);
442 BYTE
*data
= CryptMemAlloc(cbCtlEncoded
);
446 memcpy(data
, pbCtlEncoded
, cbCtlEncoded
);
447 ctl
->dwMsgAndCertEncodingType
=
448 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
449 ctl
->pbCtlEncoded
= data
;
450 ctl
->cbCtlEncoded
= cbCtlEncoded
;
451 ctl
->pCtlInfo
= ctlInfo
;
452 ctl
->hCertStore
= NULL
;
453 ctl
->hCryptMsg
= msg
;
454 ctl
->pbCtlContext
= content
;
455 ctl
->cbCtlContext
= contentSize
;
459 SetLastError(ERROR_OUTOFMEMORY
);
465 SetLastError(ERROR_OUTOFMEMORY
);
473 SetLastError(ERROR_OUTOFMEMORY
);
480 CertFreeCTLContext(ctl
);
483 CryptMemFree(content
);
489 PCCTL_CONTEXT WINAPI
CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext
)
491 TRACE("(%p)\n", pCtlContext
);
493 Context_AddRef(&ctl_from_ptr(pCtlContext
)->base
);
497 BOOL WINAPI
CertFreeCTLContext(PCCTL_CONTEXT pCTLContext
)
501 TRACE("(%p)\n", pCTLContext
);
504 ret
= Context_Release(&ctl_from_ptr(pCTLContext
)->base
);
508 DWORD WINAPI
CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext
,
511 ctl_t
*ctl
= ctl_from_ptr(pCTLContext
);
514 TRACE("(%p, %d)\n", pCTLContext
, dwPropId
);
516 if (ctl
->base
.properties
)
517 ret
= ContextPropertyList_EnumPropIDs(ctl
->base
.properties
, dwPropId
);
523 static BOOL
CTLContext_SetProperty(ctl_t
*ctl
, DWORD dwPropId
,
524 DWORD dwFlags
, const void *pvData
);
526 static BOOL
CTLContext_GetHashProp(ctl_t
*ctl
, DWORD dwPropId
,
527 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
530 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
534 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
536 ret
= CTLContext_SetProperty(ctl
, dwPropId
, 0, &blob
);
541 static BOOL
CTLContext_GetProperty(ctl_t
*ctl
, DWORD dwPropId
,
542 void *pvData
, DWORD
*pcbData
)
545 CRYPT_DATA_BLOB blob
;
547 TRACE("(%p, %d, %p, %p)\n", ctl
, dwPropId
, pvData
, pcbData
);
549 if (ctl
->base
.properties
)
550 ret
= ContextPropertyList_FindProperty(ctl
->base
.properties
, dwPropId
, &blob
);
556 *pcbData
= blob
.cbData
;
557 else if (*pcbData
< blob
.cbData
)
559 SetLastError(ERROR_MORE_DATA
);
560 *pcbData
= blob
.cbData
;
565 memcpy(pvData
, blob
.pbData
, blob
.cbData
);
566 *pcbData
= blob
.cbData
;
571 /* Implicit properties */
574 case CERT_SHA1_HASH_PROP_ID
:
575 ret
= CTLContext_GetHashProp(ctl
, dwPropId
, CALG_SHA1
,
576 ctl
->ctx
.pbCtlEncoded
, ctl
->ctx
.cbCtlEncoded
, pvData
, pcbData
);
578 case CERT_MD5_HASH_PROP_ID
:
579 ret
= CTLContext_GetHashProp(ctl
, dwPropId
, CALG_MD5
,
580 ctl
->ctx
.pbCtlEncoded
, ctl
->ctx
.cbCtlEncoded
, pvData
, pcbData
);
583 SetLastError(CRYPT_E_NOT_FOUND
);
586 TRACE("returning %d\n", ret
);
590 BOOL WINAPI
CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
591 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
595 TRACE("(%p, %d, %p, %p)\n", pCTLContext
, dwPropId
, pvData
, pcbData
);
600 case CERT_CERT_PROP_ID
:
601 case CERT_CRL_PROP_ID
:
602 case CERT_CTL_PROP_ID
:
603 SetLastError(E_INVALIDARG
);
606 case CERT_ACCESS_STATE_PROP_ID
:
609 *pcbData
= sizeof(DWORD
);
612 else if (*pcbData
< sizeof(DWORD
))
614 SetLastError(ERROR_MORE_DATA
);
615 *pcbData
= sizeof(DWORD
);
620 if (pCTLContext
->hCertStore
)
621 ret
= CertGetStoreProperty(pCTLContext
->hCertStore
, dwPropId
,
625 *(DWORD
*)pvData
= 0;
631 ret
= CTLContext_GetProperty(ctl_from_ptr(pCTLContext
), dwPropId
, pvData
,
637 static BOOL
CTLContext_SetProperty(ctl_t
*ctl
, DWORD dwPropId
,
638 DWORD dwFlags
, const void *pvData
)
642 TRACE("(%p, %d, %08x, %p)\n", ctl
, dwPropId
, dwFlags
, pvData
);
644 if (!ctl
->base
.properties
)
648 ContextPropertyList_RemoveProperty(ctl
->base
.properties
, dwPropId
);
655 case CERT_AUTO_ENROLL_PROP_ID
:
656 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
657 case CERT_DESCRIPTION_PROP_ID
:
658 case CERT_FRIENDLY_NAME_PROP_ID
:
659 case CERT_HASH_PROP_ID
:
660 case CERT_KEY_IDENTIFIER_PROP_ID
:
661 case CERT_MD5_HASH_PROP_ID
:
662 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
663 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
664 case CERT_PVK_FILE_PROP_ID
:
665 case CERT_SIGNATURE_HASH_PROP_ID
:
666 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
667 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
668 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
669 case CERT_ENROLLMENT_PROP_ID
:
670 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
671 case CERT_RENEWAL_PROP_ID
:
673 PCRYPT_DATA_BLOB blob
= (PCRYPT_DATA_BLOB
)pvData
;
675 ret
= ContextPropertyList_SetProperty(ctl
->base
.properties
, dwPropId
,
676 blob
->pbData
, blob
->cbData
);
679 case CERT_DATE_STAMP_PROP_ID
:
680 ret
= ContextPropertyList_SetProperty(ctl
->base
.properties
, dwPropId
,
681 pvData
, sizeof(FILETIME
));
684 FIXME("%d: stub\n", dwPropId
);
688 TRACE("returning %d\n", ret
);
692 BOOL WINAPI
CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
693 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
697 TRACE("(%p, %d, %08x, %p)\n", pCTLContext
, dwPropId
, dwFlags
, pvData
);
699 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
700 * crashes on most of these, I'll be safer.
705 case CERT_ACCESS_STATE_PROP_ID
:
706 case CERT_CERT_PROP_ID
:
707 case CERT_CRL_PROP_ID
:
708 case CERT_CTL_PROP_ID
:
709 SetLastError(E_INVALIDARG
);
712 ret
= CTLContext_SetProperty(ctl_from_ptr(pCTLContext
), dwPropId
, dwFlags
, pvData
);
713 TRACE("returning %d\n", ret
);