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 const context_vtbl_t ctl_vtbl
= {
46 BOOL WINAPI
CertAddCTLContextToStore(HCERTSTORE hCertStore
,
47 PCCTL_CONTEXT pCtlContext
, DWORD dwAddDisposition
,
48 PCCTL_CONTEXT
* ppStoreContext
)
50 WINECRYPT_CERTSTORE
*store
= hCertStore
;
52 PCCTL_CONTEXT toAdd
= NULL
, existing
= NULL
;
54 TRACE("(%p, %p, %08x, %p)\n", hCertStore
, pCtlContext
, dwAddDisposition
,
57 if (dwAddDisposition
!= CERT_STORE_ADD_ALWAYS
)
59 existing
= CertFindCTLInStore(hCertStore
, 0, 0, CTL_FIND_EXISTING
,
63 switch (dwAddDisposition
)
65 case CERT_STORE_ADD_ALWAYS
:
66 toAdd
= CertDuplicateCTLContext(pCtlContext
);
68 case CERT_STORE_ADD_NEW
:
71 TRACE("found matching CTL, not adding\n");
72 SetLastError(CRYPT_E_EXISTS
);
76 toAdd
= CertDuplicateCTLContext(pCtlContext
);
78 case CERT_STORE_ADD_NEWER
:
81 LONG newer
= CompareFileTime(&existing
->pCtlInfo
->ThisUpdate
,
82 &pCtlContext
->pCtlInfo
->ThisUpdate
);
85 toAdd
= CertDuplicateCTLContext(pCtlContext
);
88 TRACE("existing CTL is newer, not adding\n");
89 SetLastError(CRYPT_E_EXISTS
);
94 toAdd
= CertDuplicateCTLContext(pCtlContext
);
96 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
99 LONG newer
= CompareFileTime(&existing
->pCtlInfo
->ThisUpdate
,
100 &pCtlContext
->pCtlInfo
->ThisUpdate
);
104 toAdd
= CertDuplicateCTLContext(pCtlContext
);
105 Context_CopyProperties(existing
, pCtlContext
);
109 TRACE("existing CTL is newer, not adding\n");
110 SetLastError(CRYPT_E_EXISTS
);
115 toAdd
= CertDuplicateCTLContext(pCtlContext
);
117 case CERT_STORE_ADD_REPLACE_EXISTING
:
118 toAdd
= CertDuplicateCTLContext(pCtlContext
);
120 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
121 toAdd
= CertDuplicateCTLContext(pCtlContext
);
123 Context_CopyProperties(toAdd
, existing
);
125 case CERT_STORE_ADD_USE_EXISTING
:
128 Context_CopyProperties(existing
, pCtlContext
);
130 *ppStoreContext
= CertDuplicateCTLContext(existing
);
133 toAdd
= CertDuplicateCTLContext(pCtlContext
);
136 FIXME("Unimplemented add disposition %d\n", dwAddDisposition
);
143 ret
= store
->vtbl
->ctls
.addContext(store
, (void *)toAdd
,
144 (void *)existing
, (const void **)ppStoreContext
);
145 else if (ppStoreContext
)
146 *ppStoreContext
= CertDuplicateCTLContext(toAdd
);
147 CertFreeCTLContext(toAdd
);
149 CertFreeCTLContext(existing
);
151 TRACE("returning %d\n", ret
);
155 BOOL WINAPI
CertAddEncodedCTLToStore(HCERTSTORE hCertStore
,
156 DWORD dwMsgAndCertEncodingType
, const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
,
157 DWORD dwAddDisposition
, PCCTL_CONTEXT
*ppCtlContext
)
159 PCCTL_CONTEXT ctl
= CertCreateCTLContext(dwMsgAndCertEncodingType
,
160 pbCtlEncoded
, cbCtlEncoded
);
163 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
,
164 dwMsgAndCertEncodingType
, pbCtlEncoded
, cbCtlEncoded
, dwAddDisposition
,
169 ret
= CertAddCTLContextToStore(hCertStore
, ctl
, dwAddDisposition
,
171 CertFreeCTLContext(ctl
);
178 PCCTL_CONTEXT WINAPI
CertEnumCTLsInStore(HCERTSTORE hCertStore
,
181 WINECRYPT_CERTSTORE
*hcs
= hCertStore
;
184 TRACE("(%p, %p)\n", hCertStore
, pPrev
);
187 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
190 ret
= (PCCTL_CONTEXT
)hcs
->vtbl
->ctls
.enumContext(hcs
, (void *)pPrev
);
194 typedef BOOL (*CtlCompareFunc
)(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
195 DWORD dwFlags
, const void *pvPara
);
197 static BOOL
compare_ctl_any(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
198 DWORD dwFlags
, const void *pvPara
)
203 static BOOL
compare_ctl_by_md5_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
204 DWORD dwFlags
, const void *pvPara
)
208 DWORD size
= sizeof(hash
);
210 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_MD5_HASH_PROP_ID
, hash
,
214 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
216 if (size
== pHash
->cbData
)
217 ret
= !memcmp(pHash
->pbData
, hash
, size
);
224 static BOOL
compare_ctl_by_sha1_hash(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
225 DWORD dwFlags
, const void *pvPara
)
229 DWORD size
= sizeof(hash
);
231 ret
= CertGetCTLContextProperty(pCtlContext
, CERT_SHA1_HASH_PROP_ID
, hash
,
235 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
237 if (size
== pHash
->cbData
)
238 ret
= !memcmp(pHash
->pbData
, hash
, size
);
245 static BOOL
compare_ctl_existing(PCCTL_CONTEXT pCtlContext
, DWORD dwType
,
246 DWORD dwFlags
, const void *pvPara
)
252 PCCTL_CONTEXT ctl
= pvPara
;
254 if (pCtlContext
->cbCtlContext
== ctl
->cbCtlContext
)
256 if (ctl
->cbCtlContext
)
257 ret
= !memcmp(pCtlContext
->pbCtlContext
, ctl
->pbCtlContext
,
270 PCCTL_CONTEXT WINAPI
CertFindCTLInStore(HCERTSTORE hCertStore
,
271 DWORD dwCertEncodingType
, DWORD dwFindFlags
, DWORD dwFindType
,
272 const void *pvFindPara
, PCCTL_CONTEXT pPrevCtlContext
)
275 CtlCompareFunc compare
;
277 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore
, dwCertEncodingType
,
278 dwFindFlags
, dwFindType
, pvFindPara
, pPrevCtlContext
);
283 compare
= compare_ctl_any
;
285 case CTL_FIND_SHA1_HASH
:
286 compare
= compare_ctl_by_sha1_hash
;
288 case CTL_FIND_MD5_HASH
:
289 compare
= compare_ctl_by_md5_hash
;
291 case CTL_FIND_EXISTING
:
292 compare
= compare_ctl_existing
;
295 FIXME("find type %08x unimplemented\n", dwFindType
);
301 BOOL matches
= FALSE
;
303 ret
= pPrevCtlContext
;
305 ret
= CertEnumCTLsInStore(hCertStore
, ret
);
307 matches
= compare(ret
, dwFindType
, dwFindFlags
, pvFindPara
);
308 } while (ret
!= NULL
&& !matches
);
310 SetLastError(CRYPT_E_NOT_FOUND
);
314 SetLastError(CRYPT_E_NOT_FOUND
);
320 BOOL WINAPI
CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext
)
324 TRACE("(%p)\n", pCtlContext
);
328 else if (!pCtlContext
->hCertStore
)
329 ret
= CertFreeCTLContext(pCtlContext
);
332 WINECRYPT_CERTSTORE
*hcs
= pCtlContext
->hCertStore
;
334 if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
337 ret
= hcs
->vtbl
->ctls
.deleteContext(hcs
, (void *)pCtlContext
);
339 ret
= CertFreeCTLContext(pCtlContext
);
344 PCCTL_CONTEXT WINAPI
CertCreateCTLContext(DWORD dwMsgAndCertEncodingType
,
345 const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
)
347 PCTL_CONTEXT ctl
= NULL
;
350 BYTE
*content
= NULL
;
351 DWORD contentSize
= 0, size
;
352 PCTL_INFO ctlInfo
= NULL
;
354 TRACE("(%08x, %p, %d)\n", dwMsgAndCertEncodingType
, pbCtlEncoded
,
357 if (GET_CERT_ENCODING_TYPE(dwMsgAndCertEncodingType
) != X509_ASN_ENCODING
)
359 SetLastError(E_INVALIDARG
);
362 if (!pbCtlEncoded
|| !cbCtlEncoded
)
364 SetLastError(ERROR_INVALID_DATA
);
367 msg
= CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING
| X509_ASN_ENCODING
, 0, 0,
371 ret
= CryptMsgUpdate(msg
, pbCtlEncoded
, cbCtlEncoded
, TRUE
);
374 SetLastError(ERROR_INVALID_DATA
);
377 /* Check that it's really a CTL */
378 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
, &size
);
381 char *innerContent
= CryptMemAlloc(size
);
385 ret
= CryptMsgGetParam(msg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0,
386 innerContent
, &size
);
389 if (strcmp(innerContent
, szOID_CTL
))
391 SetLastError(ERROR_INVALID_DATA
);
395 CryptMemFree(innerContent
);
399 SetLastError(ERROR_OUTOFMEMORY
);
405 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, NULL
, &contentSize
);
408 content
= CryptMemAlloc(contentSize
);
411 ret
= CryptMsgGetParam(msg
, CMSG_CONTENT_PARAM
, 0, content
,
415 ret
= CryptDecodeObjectEx(dwMsgAndCertEncodingType
, PKCS_CTL
,
416 content
, contentSize
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
420 ctl
= Context_CreateDataContext(sizeof(CTL_CONTEXT
), &ctl_vtbl
);
423 BYTE
*data
= CryptMemAlloc(cbCtlEncoded
);
427 memcpy(data
, pbCtlEncoded
, cbCtlEncoded
);
428 ctl
->dwMsgAndCertEncodingType
=
429 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
430 ctl
->pbCtlEncoded
= data
;
431 ctl
->cbCtlEncoded
= cbCtlEncoded
;
432 ctl
->pCtlInfo
= ctlInfo
;
433 ctl
->hCertStore
= NULL
;
434 ctl
->hCryptMsg
= msg
;
435 ctl
->pbCtlContext
= content
;
436 ctl
->cbCtlContext
= contentSize
;
440 SetLastError(ERROR_OUTOFMEMORY
);
446 SetLastError(ERROR_OUTOFMEMORY
);
454 SetLastError(ERROR_OUTOFMEMORY
);
461 CertFreeCTLContext(ctl
);
464 CryptMemFree(content
);
470 PCCTL_CONTEXT WINAPI
CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext
)
472 TRACE("(%p)\n", pCtlContext
);
474 Context_AddRef(&ctl_from_ptr(pCtlContext
)->base
);
478 BOOL WINAPI
CertFreeCTLContext(PCCTL_CONTEXT pCTLContext
)
482 TRACE("(%p)\n", pCTLContext
);
485 ret
= Context_Release(&ctl_from_ptr(pCTLContext
)->base
);
489 DWORD WINAPI
CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext
,
492 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(pCTLContext
);
495 TRACE("(%p, %d)\n", pCTLContext
, dwPropId
);
498 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
504 static BOOL
CTLContext_SetProperty(PCCTL_CONTEXT context
, DWORD dwPropId
,
505 DWORD dwFlags
, const void *pvData
);
507 static BOOL
CTLContext_GetHashProp(PCCTL_CONTEXT context
, DWORD dwPropId
,
508 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
511 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
515 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
517 ret
= CTLContext_SetProperty(context
, dwPropId
, 0, &blob
);
522 static BOOL
CTLContext_GetProperty(PCCTL_CONTEXT context
, DWORD dwPropId
,
523 void *pvData
, DWORD
*pcbData
)
525 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(context
);
527 CRYPT_DATA_BLOB blob
;
529 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
532 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
538 *pcbData
= blob
.cbData
;
539 else if (*pcbData
< blob
.cbData
)
541 SetLastError(ERROR_MORE_DATA
);
542 *pcbData
= blob
.cbData
;
547 memcpy(pvData
, blob
.pbData
, blob
.cbData
);
548 *pcbData
= blob
.cbData
;
553 /* Implicit properties */
556 case CERT_SHA1_HASH_PROP_ID
:
557 ret
= CTLContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
558 context
->pbCtlEncoded
, context
->cbCtlEncoded
, pvData
, pcbData
);
560 case CERT_MD5_HASH_PROP_ID
:
561 ret
= CTLContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
562 context
->pbCtlEncoded
, context
->cbCtlEncoded
, pvData
, pcbData
);
565 SetLastError(CRYPT_E_NOT_FOUND
);
568 TRACE("returning %d\n", ret
);
572 BOOL WINAPI
CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
573 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
577 TRACE("(%p, %d, %p, %p)\n", pCTLContext
, dwPropId
, pvData
, pcbData
);
582 case CERT_CERT_PROP_ID
:
583 case CERT_CRL_PROP_ID
:
584 case CERT_CTL_PROP_ID
:
585 SetLastError(E_INVALIDARG
);
588 case CERT_ACCESS_STATE_PROP_ID
:
591 *pcbData
= sizeof(DWORD
);
594 else if (*pcbData
< sizeof(DWORD
))
596 SetLastError(ERROR_MORE_DATA
);
597 *pcbData
= sizeof(DWORD
);
602 if (pCTLContext
->hCertStore
)
603 ret
= CertGetStoreProperty(pCTLContext
->hCertStore
, dwPropId
,
607 *(DWORD
*)pvData
= 0;
613 ret
= CTLContext_GetProperty(pCTLContext
, dwPropId
, pvData
,
619 static BOOL
CTLContext_SetProperty(PCCTL_CONTEXT context
, DWORD dwPropId
,
620 DWORD dwFlags
, const void *pvData
)
622 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(context
);
625 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
631 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
638 case CERT_AUTO_ENROLL_PROP_ID
:
639 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
640 case CERT_DESCRIPTION_PROP_ID
:
641 case CERT_FRIENDLY_NAME_PROP_ID
:
642 case CERT_HASH_PROP_ID
:
643 case CERT_KEY_IDENTIFIER_PROP_ID
:
644 case CERT_MD5_HASH_PROP_ID
:
645 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
646 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
647 case CERT_PVK_FILE_PROP_ID
:
648 case CERT_SIGNATURE_HASH_PROP_ID
:
649 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
650 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
651 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
652 case CERT_ENROLLMENT_PROP_ID
:
653 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
654 case CERT_RENEWAL_PROP_ID
:
656 PCRYPT_DATA_BLOB blob
= (PCRYPT_DATA_BLOB
)pvData
;
658 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
659 blob
->pbData
, blob
->cbData
);
662 case CERT_DATE_STAMP_PROP_ID
:
663 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
664 pvData
, sizeof(FILETIME
));
667 FIXME("%d: stub\n", dwPropId
);
671 TRACE("returning %d\n", ret
);
675 BOOL WINAPI
CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
676 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
680 TRACE("(%p, %d, %08x, %p)\n", pCTLContext
, dwPropId
, dwFlags
, pvData
);
682 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
683 * crashes on most of these, I'll be safer.
688 case CERT_ACCESS_STATE_PROP_ID
:
689 case CERT_CERT_PROP_ID
:
690 case CERT_CRL_PROP_ID
:
691 case CERT_CTL_PROP_ID
:
692 SetLastError(E_INVALIDARG
);
695 ret
= CTLContext_SetProperty(pCTLContext
, dwPropId
, dwFlags
, pvData
);
696 TRACE("returning %d\n", ret
);