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
, store
);
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
);
163 ret
= store
->vtbl
->ctls
.addContext(store
, context_from_ptr(toAdd
),
164 existing
? context_from_ptr(existing
) : NULL
, ppStoreContext
? &ret_ctx
: NULL
, TRUE
);
165 if(ret
&& ppStoreContext
)
166 *ppStoreContext
= context_ptr(ret_ctx
);
167 }else if (ppStoreContext
) {
168 *ppStoreContext
= CertDuplicateCTLContext(toAdd
);
170 CertFreeCTLContext(toAdd
);
172 CertFreeCTLContext(existing
);
174 TRACE("returning %d\n", ret
);
178 BOOL WINAPI
CertAddEncodedCTLToStore(HCERTSTORE hCertStore
,
179 DWORD dwMsgAndCertEncodingType
, const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
,
180 DWORD dwAddDisposition
, PCCTL_CONTEXT
*ppCtlContext
)
182 PCCTL_CONTEXT ctl
= CertCreateCTLContext(dwMsgAndCertEncodingType
,
183 pbCtlEncoded
, cbCtlEncoded
);
186 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
,
187 dwMsgAndCertEncodingType
, pbCtlEncoded
, cbCtlEncoded
, dwAddDisposition
,
192 ret
= CertAddCTLContextToStore(hCertStore
, ctl
, dwAddDisposition
,
194 CertFreeCTLContext(ctl
);
201 PCCTL_CONTEXT WINAPI
CertEnumCTLsInStore(HCERTSTORE hCertStore
, PCCTL_CONTEXT pPrev
)
203 ctl_t
*prev
= pPrev
? ctl_from_ptr(pPrev
) : NULL
, *ret
;
204 WINECRYPT_CERTSTORE
*hcs
= hCertStore
;
206 TRACE("(%p, %p)\n", hCertStore
, pPrev
);
209 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
212 ret
= (ctl_t
*)hcs
->vtbl
->ctls
.enumContext(hcs
, prev
? &prev
->base
: NULL
);
213 return ret
? &ret
->ctx
: NULL
;
216 typedef BOOL (*CtlCompareFunc
)(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
217 DWORD dwFlags
, const void *pvPara
);
219 static BOOL
compare_ctl_any(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
220 DWORD dwFlags
, const void *pvPara
)
225 static BOOL
compare_ctl_by_md5_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
226 DWORD dwFlags
, const void *pvPara
)
230 DWORD size
= sizeof(hash
);
232 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_MD5_HASH_PROP_ID
, hash
,
236 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
238 if (size
== pHash
->cbData
)
239 ret
= !memcmp(pHash
->pbData
, hash
, size
);
246 static BOOL
compare_ctl_by_sha1_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
247 DWORD dwFlags
, const void *pvPara
)
251 DWORD size
= sizeof(hash
);
253 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_SHA1_HASH_PROP_ID
, hash
,
257 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
259 if (size
== pHash
->cbData
)
260 ret
= !memcmp(pHash
->pbData
, hash
, size
);
267 static BOOL
compare_ctl_existing(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
268 DWORD dwFlags
, const void *pvPara
)
274 PCCTL_CONTEXT ctl
= pvPara
;
276 if (pCtlContext
->cbCtlContext
== ctl
->cbCtlContext
)
278 if (ctl
->cbCtlContext
)
279 ret
= !memcmp(pCtlContext
->pbCtlContext
, ctl
->pbCtlContext
,
292 PCCTL_CONTEXT WINAPI
CertFindCTLInStore(HCERTSTORE hCertStore
,
293 DWORD dwCertEncodingType
, DWORD dwFindFlags
, DWORD dwFindType
,
294 const void *pvFindPara
, PCCTL_CONTEXT pPrevCtlContext
)
297 CtlCompareFunc compare
;
299 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore
, dwCertEncodingType
,
300 dwFindFlags
, dwFindType
, pvFindPara
, pPrevCtlContext
);
305 compare
= compare_ctl_any
;
307 case CTL_FIND_SHA1_HASH
:
308 compare
= compare_ctl_by_sha1_hash
;
310 case CTL_FIND_MD5_HASH
:
311 compare
= compare_ctl_by_md5_hash
;
313 case CTL_FIND_EXISTING
:
314 compare
= compare_ctl_existing
;
317 FIXME("find type %08x unimplemented\n", dwFindType
);
323 BOOL matches
= FALSE
;
325 ret
= pPrevCtlContext
;
327 ret
= CertEnumCTLsInStore(hCertStore
, ret
);
329 matches
= compare(ret
, dwFindType
, dwFindFlags
, pvFindPara
);
330 } while (ret
!= NULL
&& !matches
);
332 SetLastError(CRYPT_E_NOT_FOUND
);
336 SetLastError(CRYPT_E_NOT_FOUND
);
342 BOOL WINAPI
CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext
)
344 WINECRYPT_CERTSTORE
*hcs
;
345 ctl_t
*ctl
= ctl_from_ptr(pCtlContext
);
348 TRACE("(%p)\n", pCtlContext
);
353 hcs
= pCtlContext
->hCertStore
;
355 if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
358 ret
= hcs
->vtbl
->ctls
.delete(hcs
, &ctl
->base
);
360 ret
= CertFreeCTLContext(pCtlContext
);
364 PCCTL_CONTEXT WINAPI
CertCreateCTLContext(DWORD dwMsgAndCertEncodingType
,
365 const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
)
370 BYTE
*content
= NULL
;
371 DWORD contentSize
= 0, size
;
372 PCTL_INFO ctlInfo
= NULL
;
374 TRACE("(%08x, %p, %d)\n", dwMsgAndCertEncodingType
, pbCtlEncoded
,
377 if (GET_CERT_ENCODING_TYPE(dwMsgAndCertEncodingType
) != X509_ASN_ENCODING
)
379 SetLastError(E_INVALIDARG
);
382 if (!pbCtlEncoded
|| !cbCtlEncoded
)
384 SetLastError(ERROR_INVALID_DATA
);
387 msg
= CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING
| X509_ASN_ENCODING
, 0, 0,
391 ret
= CryptMsgUpdate(msg
, pbCtlEncoded
, cbCtlEncoded
, TRUE
);
394 SetLastError(ERROR_INVALID_DATA
);
397 /* Check that it's really a CTL */
398 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
, &size
);
401 char *innerContent
= CryptMemAlloc(size
);
405 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0,
406 innerContent
, &size
);
409 if (strcmp(innerContent
, szOID_CTL
))
411 SetLastError(ERROR_INVALID_DATA
);
415 CryptMemFree(innerContent
);
419 SetLastError(ERROR_OUTOFMEMORY
);
425 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, NULL
, &contentSize
);
428 content
= CryptMemAlloc(contentSize
);
431 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, content
,
435 ret
= CryptDecodeObjectEx(dwMsgAndCertEncodingType
, PKCS_CTL
,
436 content
, contentSize
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
440 ctl
= (ctl_t
*)Context_CreateDataContext(sizeof(CTL_CONTEXT
), &ctl_vtbl
, &empty_store
);
443 BYTE
*data
= CryptMemAlloc(cbCtlEncoded
);
447 memcpy(data
, pbCtlEncoded
, cbCtlEncoded
);
448 ctl
->ctx
.dwMsgAndCertEncodingType
=
449 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
450 ctl
->ctx
.pbCtlEncoded
= data
;
451 ctl
->ctx
.cbCtlEncoded
= cbCtlEncoded
;
452 ctl
->ctx
.pCtlInfo
= ctlInfo
;
453 ctl
->ctx
.hCertStore
= &empty_store
;
454 ctl
->ctx
.hCryptMsg
= msg
;
455 ctl
->ctx
.pbCtlContext
= content
;
456 ctl
->ctx
.cbCtlContext
= contentSize
;
460 SetLastError(ERROR_OUTOFMEMORY
);
466 SetLastError(ERROR_OUTOFMEMORY
);
474 SetLastError(ERROR_OUTOFMEMORY
);
482 Context_Release(&ctl
->base
);
485 CryptMemFree(content
);
492 PCCTL_CONTEXT WINAPI
CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext
)
494 TRACE("(%p)\n", pCtlContext
);
496 Context_AddRef(&ctl_from_ptr(pCtlContext
)->base
);
500 BOOL WINAPI
CertFreeCTLContext(PCCTL_CONTEXT pCTLContext
)
502 TRACE("(%p)\n", pCTLContext
);
505 Context_Release(&ctl_from_ptr(pCTLContext
)->base
);
509 DWORD WINAPI
CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext
,
512 ctl_t
*ctl
= ctl_from_ptr(pCTLContext
);
515 TRACE("(%p, %d)\n", pCTLContext
, dwPropId
);
517 if (ctl
->base
.properties
)
518 ret
= ContextPropertyList_EnumPropIDs(ctl
->base
.properties
, dwPropId
);
524 static BOOL
CTLContext_SetProperty(ctl_t
*ctl
, DWORD dwPropId
,
525 DWORD dwFlags
, const void *pvData
);
527 static BOOL
CTLContext_GetHashProp(ctl_t
*ctl
, DWORD dwPropId
,
528 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
531 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
535 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
537 ret
= CTLContext_SetProperty(ctl
, dwPropId
, 0, &blob
);
542 static BOOL
CTLContext_GetProperty(ctl_t
*ctl
, DWORD dwPropId
,
543 void *pvData
, DWORD
*pcbData
)
546 CRYPT_DATA_BLOB blob
;
548 TRACE("(%p, %d, %p, %p)\n", ctl
, dwPropId
, pvData
, pcbData
);
550 if (ctl
->base
.properties
)
551 ret
= ContextPropertyList_FindProperty(ctl
->base
.properties
, dwPropId
, &blob
);
557 *pcbData
= blob
.cbData
;
558 else if (*pcbData
< blob
.cbData
)
560 SetLastError(ERROR_MORE_DATA
);
561 *pcbData
= blob
.cbData
;
566 memcpy(pvData
, blob
.pbData
, blob
.cbData
);
567 *pcbData
= blob
.cbData
;
572 /* Implicit properties */
575 case CERT_SHA1_HASH_PROP_ID
:
576 ret
= CTLContext_GetHashProp(ctl
, dwPropId
, CALG_SHA1
,
577 ctl
->ctx
.pbCtlEncoded
, ctl
->ctx
.cbCtlEncoded
, pvData
, pcbData
);
579 case CERT_MD5_HASH_PROP_ID
:
580 ret
= CTLContext_GetHashProp(ctl
, dwPropId
, CALG_MD5
,
581 ctl
->ctx
.pbCtlEncoded
, ctl
->ctx
.cbCtlEncoded
, pvData
, pcbData
);
584 SetLastError(CRYPT_E_NOT_FOUND
);
587 TRACE("returning %d\n", ret
);
591 BOOL WINAPI
CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
592 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
596 TRACE("(%p, %d, %p, %p)\n", pCTLContext
, dwPropId
, pvData
, pcbData
);
601 case CERT_CERT_PROP_ID
:
602 case CERT_CRL_PROP_ID
:
603 case CERT_CTL_PROP_ID
:
604 SetLastError(E_INVALIDARG
);
607 case CERT_ACCESS_STATE_PROP_ID
:
610 *pcbData
= sizeof(DWORD
);
613 else if (*pcbData
< sizeof(DWORD
))
615 SetLastError(ERROR_MORE_DATA
);
616 *pcbData
= sizeof(DWORD
);
621 ret
= CertGetStoreProperty(pCTLContext
->hCertStore
, dwPropId
, pvData
, pcbData
);
625 ret
= CTLContext_GetProperty(ctl_from_ptr(pCTLContext
), dwPropId
, pvData
,
631 static BOOL
CTLContext_SetProperty(ctl_t
*ctl
, DWORD dwPropId
,
632 DWORD dwFlags
, const void *pvData
)
636 TRACE("(%p, %d, %08x, %p)\n", ctl
, dwPropId
, dwFlags
, pvData
);
638 if (!ctl
->base
.properties
)
642 ContextPropertyList_RemoveProperty(ctl
->base
.properties
, dwPropId
);
649 case CERT_AUTO_ENROLL_PROP_ID
:
650 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
651 case CERT_DESCRIPTION_PROP_ID
:
652 case CERT_FRIENDLY_NAME_PROP_ID
:
653 case CERT_HASH_PROP_ID
:
654 case CERT_KEY_IDENTIFIER_PROP_ID
:
655 case CERT_MD5_HASH_PROP_ID
:
656 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
657 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
658 case CERT_PVK_FILE_PROP_ID
:
659 case CERT_SIGNATURE_HASH_PROP_ID
:
660 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
661 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
662 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
663 case CERT_ENROLLMENT_PROP_ID
:
664 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
665 case CERT_RENEWAL_PROP_ID
:
667 PCRYPT_DATA_BLOB blob
= (PCRYPT_DATA_BLOB
)pvData
;
669 ret
= ContextPropertyList_SetProperty(ctl
->base
.properties
, dwPropId
,
670 blob
->pbData
, blob
->cbData
);
673 case CERT_DATE_STAMP_PROP_ID
:
674 ret
= ContextPropertyList_SetProperty(ctl
->base
.properties
, dwPropId
,
675 pvData
, sizeof(FILETIME
));
678 FIXME("%d: stub\n", dwPropId
);
682 TRACE("returning %d\n", ret
);
686 BOOL WINAPI
CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
687 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
691 TRACE("(%p, %d, %08x, %p)\n", pCTLContext
, dwPropId
, dwFlags
, pvData
);
693 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
694 * crashes on most of these, I'll be safer.
699 case CERT_ACCESS_STATE_PROP_ID
:
700 case CERT_CERT_PROP_ID
:
701 case CERT_CRL_PROP_ID
:
702 case CERT_CTL_PROP_ID
:
703 SetLastError(E_INVALIDARG
);
706 ret
= CTLContext_SetProperty(ctl_from_ptr(pCTLContext
), dwPropId
, dwFlags
, pvData
);
707 TRACE("returning %d\n", ret
);