2 * Copyright 2004-2006 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
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "crypt32_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
35 /* Internal version of CertGetCertificateContextProperty that gets properties
36 * directly from the context (or the context it's linked to, depending on its
37 * type.) Doesn't handle special-case properties, since they are handled by
38 * CertGetCertificateContextProperty, and are particular to the store in which
39 * the property exists (which is separate from the context.)
41 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
42 void *pvData
, DWORD
*pcbData
);
44 /* Internal version of CertSetCertificateContextProperty that sets properties
45 * directly on the context (or the context it's linked to, depending on its
46 * type.) Doesn't handle special cases, since they're handled by
47 * CertSetCertificateContextProperty anyway.
49 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
50 DWORD dwFlags
, const void *pvData
);
52 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
53 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
54 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
56 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
57 pbCertEncoded
, cbCertEncoded
);
60 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
, dwCertEncodingType
,
61 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
65 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
66 dwAddDisposition
, ppCertContext
);
67 CertFreeCertificateContext(cert
);
74 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreA(LPCSTR pszCertStoreName
,
75 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
80 TRACE("(%s, %p, %d)\n", debugstr_a(pszCertStoreName
), pbCertEncoded
,
83 store
= CertOpenSystemStoreA(0, pszCertStoreName
);
86 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
87 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
88 CertCloseStore(store
, 0);
93 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreW(LPCWSTR pszCertStoreName
,
94 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
99 TRACE("(%s, %p, %d)\n", debugstr_w(pszCertStoreName
), pbCertEncoded
,
102 store
= CertOpenSystemStoreW(0, pszCertStoreName
);
105 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
106 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
107 CertCloseStore(store
, 0);
112 static void Cert_free(context_t
*context
)
114 cert_t
*cert
= (cert_t
*)context
;
116 CryptMemFree(cert
->ctx
.pbCertEncoded
);
117 LocalFree(cert
->ctx
.pCertInfo
);
120 static const context_vtbl_t cert_vtbl
= {
124 BOOL WINAPI
CertAddCertificateLinkToStore(HCERTSTORE hCertStore
,
125 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
126 PCCERT_CONTEXT
*ppCertContext
)
129 WINECRYPT_CERTSTORE
*store
= (WINECRYPT_CERTSTORE
*)hCertStore
;
132 FIXME("(%p, %p, %08x, %p): semi-stub\n", hCertStore
, pCertContext
,
133 dwAddDisposition
, ppCertContext
);
134 if (store
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
136 if (store
->type
== StoreTypeCollection
)
138 SetLastError(E_INVALIDARG
);
141 return CertAddCertificateContextToStore(hCertStore
, pCertContext
,
142 dwAddDisposition
, ppCertContext
);
145 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
146 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
148 PCERT_CONTEXT cert
= NULL
;
150 PCERT_INFO certInfo
= NULL
;
153 TRACE("(%08x, %p, %d)\n", dwCertEncodingType
, pbCertEncoded
,
156 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
158 SetLastError(E_INVALIDARG
);
162 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
163 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
169 cert
= Context_CreateDataContext(sizeof(CERT_CONTEXT
), &cert_vtbl
);
172 data
= CryptMemAlloc(cbCertEncoded
);
175 CertFreeCertificateContext(cert
);
179 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
180 cert
->dwCertEncodingType
= dwCertEncodingType
;
181 cert
->pbCertEncoded
= data
;
182 cert
->cbCertEncoded
= cbCertEncoded
;
183 cert
->pCertInfo
= certInfo
;
184 cert
->hCertStore
= &empty_store
;
191 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(PCCERT_CONTEXT pCertContext
)
193 TRACE("(%p)\n", pCertContext
);
198 Context_AddRef(&cert_from_ptr(pCertContext
)->base
);
202 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
206 TRACE("(%p)\n", pCertContext
);
209 ret
= Context_Release(&cert_from_ptr(pCertContext
)->base
);
213 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
216 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(pCertContext
);
219 TRACE("(%p, %d)\n", pCertContext
, dwPropId
);
222 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
228 static BOOL
CertContext_GetHashProp(void *context
, DWORD dwPropId
,
229 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
232 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
236 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
238 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
243 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
250 else if (*pcbData
< cb
)
252 SetLastError(ERROR_MORE_DATA
);
258 memcpy(pvData
, pb
, cb
);
264 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
265 void *pvData
, DWORD
*pcbData
)
267 PCCERT_CONTEXT pCertContext
= context
;
268 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(context
);
270 CRYPT_DATA_BLOB blob
;
272 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
275 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
279 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
282 /* Implicit properties */
285 case CERT_SHA1_HASH_PROP_ID
:
286 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
287 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
290 case CERT_MD5_HASH_PROP_ID
:
291 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
292 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
295 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
296 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
297 pCertContext
->pCertInfo
->Subject
.pbData
,
298 pCertContext
->pCertInfo
->Subject
.cbData
,
301 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
302 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
303 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
304 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
307 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
308 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
309 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
310 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
313 case CERT_SIGNATURE_HASH_PROP_ID
:
314 ret
= CryptHashToBeSigned(0, pCertContext
->dwCertEncodingType
,
315 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
319 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
321 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
324 case CERT_KEY_IDENTIFIER_PROP_ID
:
326 PCERT_EXTENSION ext
= CertFindExtension(
327 szOID_SUBJECT_KEY_IDENTIFIER
, pCertContext
->pCertInfo
->cExtension
,
328 pCertContext
->pCertInfo
->rgExtension
);
332 CRYPT_DATA_BLOB value
;
333 DWORD size
= sizeof(value
);
335 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
336 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
337 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
341 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
343 CertContext_SetProperty(context
, dwPropId
, 0, &value
);
347 SetLastError(ERROR_INVALID_DATA
);
351 SetLastError(CRYPT_E_NOT_FOUND
);
354 TRACE("returning %d\n", ret
);
358 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
360 DWORD i
, containerLen
, provNameLen
;
361 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
363 info
->pwszContainerName
= (LPWSTR
)data
;
364 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
365 data
+= containerLen
;
367 info
->pwszProvName
= (LPWSTR
)data
;
368 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
371 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
372 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
374 for (i
= 0; i
< info
->cProvParam
; i
++)
376 info
->rgProvParam
[i
].pbData
= data
;
377 data
+= info
->rgProvParam
[i
].cbData
;
381 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
382 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
386 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
391 case CERT_CERT_PROP_ID
:
392 case CERT_CRL_PROP_ID
:
393 case CERT_CTL_PROP_ID
:
394 SetLastError(E_INVALIDARG
);
397 case CERT_ACCESS_STATE_PROP_ID
:
398 if (pCertContext
->hCertStore
)
399 ret
= CertGetStoreProperty(pCertContext
->hCertStore
, dwPropId
,
405 ret
= CertContext_CopyParam(pvData
, pcbData
, &state
, sizeof(state
));
408 case CERT_KEY_PROV_HANDLE_PROP_ID
:
410 CERT_KEY_CONTEXT keyContext
;
411 DWORD size
= sizeof(keyContext
);
413 ret
= CertContext_GetProperty((void *)pCertContext
,
414 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
416 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
417 sizeof(keyContext
.hCryptProv
));
420 case CERT_KEY_PROV_INFO_PROP_ID
:
421 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
424 CRYPT_FixKeyProvInfoPointers(pvData
);
427 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
431 TRACE("returning %d\n", ret
);
435 /* Copies key provider info from from into to, where to is assumed to be a
436 * contiguous buffer of memory large enough for from and all its associated
437 * data, but whose pointers are uninitialized.
438 * Upon return, to contains a contiguous copy of from, packed in the following
440 * - CRYPT_KEY_PROV_INFO
441 * - pwszContainerName
443 * - rgProvParam[0]...
445 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
446 const CRYPT_KEY_PROV_INFO
*from
)
449 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
451 if (from
->pwszContainerName
)
453 to
->pwszContainerName
= (LPWSTR
)nextData
;
454 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
455 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
458 to
->pwszContainerName
= NULL
;
459 if (from
->pwszProvName
)
461 to
->pwszProvName
= (LPWSTR
)nextData
;
462 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
463 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
466 to
->pwszProvName
= NULL
;
467 to
->dwProvType
= from
->dwProvType
;
468 to
->dwFlags
= from
->dwFlags
;
469 to
->cProvParam
= from
->cProvParam
;
470 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
471 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
472 to
->dwKeySpec
= from
->dwKeySpec
;
473 for (i
= 0; i
< to
->cProvParam
; i
++)
475 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
476 sizeof(CRYPT_KEY_PROV_PARAM
));
477 to
->rgProvParam
[i
].pbData
= nextData
;
478 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
479 from
->rgProvParam
[i
].cbData
);
480 nextData
+= from
->rgProvParam
[i
].cbData
;
484 static BOOL
CertContext_SetKeyProvInfoProperty(CONTEXT_PROPERTY_LIST
*properties
,
485 const CRYPT_KEY_PROV_INFO
*info
)
489 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
491 if (info
->pwszContainerName
)
492 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
495 if (info
->pwszProvName
)
496 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
499 size
+= containerSize
+ provNameSize
;
500 for (i
= 0; i
< info
->cProvParam
; i
++)
501 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
502 buf
= CryptMemAlloc(size
);
505 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
506 ret
= ContextPropertyList_SetProperty(properties
,
507 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
515 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
516 DWORD dwFlags
, const void *pvData
)
518 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(context
);
521 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
529 case CERT_AUTO_ENROLL_PROP_ID
:
530 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
531 case CERT_DESCRIPTION_PROP_ID
:
532 case CERT_FRIENDLY_NAME_PROP_ID
:
533 case CERT_HASH_PROP_ID
:
534 case CERT_KEY_IDENTIFIER_PROP_ID
:
535 case CERT_MD5_HASH_PROP_ID
:
536 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
537 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
538 case CERT_PVK_FILE_PROP_ID
:
539 case CERT_SIGNATURE_HASH_PROP_ID
:
540 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
541 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
542 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
543 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
544 case CERT_ENROLLMENT_PROP_ID
:
545 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
546 case CERT_RENEWAL_PROP_ID
:
550 const CRYPT_DATA_BLOB
*blob
= pvData
;
552 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
553 blob
->pbData
, blob
->cbData
);
557 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
562 case CERT_DATE_STAMP_PROP_ID
:
564 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
565 pvData
, sizeof(FILETIME
));
568 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
572 case CERT_KEY_CONTEXT_PROP_ID
:
576 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
578 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
580 SetLastError(E_INVALIDARG
);
584 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
585 (const BYTE
*)keyContext
, keyContext
->cbSize
);
589 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
594 case CERT_KEY_PROV_INFO_PROP_ID
:
596 ret
= CertContext_SetKeyProvInfoProperty(properties
, pvData
);
599 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
603 case CERT_KEY_PROV_HANDLE_PROP_ID
:
605 CERT_KEY_CONTEXT keyContext
;
606 DWORD size
= sizeof(keyContext
);
608 ret
= CertContext_GetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
612 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
613 CryptReleaseContext(keyContext
.hCryptProv
, 0);
615 keyContext
.cbSize
= sizeof(keyContext
);
617 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
620 keyContext
.hCryptProv
= 0;
621 keyContext
.dwKeySpec
= AT_SIGNATURE
;
623 ret
= CertContext_SetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
628 FIXME("%d: stub\n", dwPropId
);
632 TRACE("returning %d\n", ret
);
636 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
637 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
641 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
643 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
644 * crashes on most of these, I'll be safer.
649 case CERT_ACCESS_STATE_PROP_ID
:
650 case CERT_CERT_PROP_ID
:
651 case CERT_CRL_PROP_ID
:
652 case CERT_CTL_PROP_ID
:
653 SetLastError(E_INVALIDARG
);
656 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
658 TRACE("returning %d\n", ret
);
662 /* Acquires the private key using the key provider info, retrieving info from
663 * the certificate if info is NULL. The acquired provider is returned in
664 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
666 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
667 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
670 BOOL allocated
= FALSE
, ret
= TRUE
;
674 ret
= CertGetCertificateContextProperty(pCert
,
675 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
678 info
= HeapAlloc(GetProcessHeap(), 0, size
);
681 ret
= CertGetCertificateContextProperty(pCert
,
682 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
687 SetLastError(ERROR_OUTOFMEMORY
);
692 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
696 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
697 info
->pwszProvName
, info
->dwProvType
, 0);
702 for (i
= 0; i
< info
->cProvParam
; i
++)
704 CryptSetProvParam(*phCryptProv
,
705 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
706 info
->rgProvParam
[i
].dwFlags
);
708 *pdwKeySpec
= info
->dwKeySpec
;
711 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
714 HeapFree(GetProcessHeap(), 0, info
);
718 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
719 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
720 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
722 BOOL ret
= FALSE
, cache
= FALSE
;
723 PCRYPT_KEY_PROV_INFO info
= NULL
;
724 CERT_KEY_CONTEXT keyContext
;
727 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
728 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
730 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
734 ret
= CertGetCertificateContextProperty(pCert
,
735 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
738 info
= HeapAlloc(GetProcessHeap(), 0, size
);
739 ret
= CertGetCertificateContextProperty(pCert
,
740 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
742 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
745 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
750 size
= sizeof(keyContext
);
751 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
755 *phCryptProv
= keyContext
.hCryptProv
;
757 *pdwKeySpec
= keyContext
.dwKeySpec
;
758 if (pfCallerFreeProv
)
759 *pfCallerFreeProv
= !cache
;
764 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
765 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
768 *phCryptProv
= keyContext
.hCryptProv
;
770 *pdwKeySpec
= keyContext
.dwKeySpec
;
773 keyContext
.cbSize
= sizeof(keyContext
);
774 if (CertSetCertificateContextProperty(pCert
,
775 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
777 if (pfCallerFreeProv
)
778 *pfCallerFreeProv
= FALSE
;
783 if (pfCallerFreeProv
)
784 *pfCallerFreeProv
= TRUE
;
788 HeapFree(GetProcessHeap(), 0, info
);
792 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
793 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
796 BOOL matches
= FALSE
;
798 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
799 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
803 /* Need to sign something to verify the sig. What to sign? Why not
804 * the certificate itself?
806 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
807 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
808 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
810 BYTE
*certEncoded
= CryptMemAlloc(size
);
814 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
815 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
816 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
817 NULL
, certEncoded
, &size
))
819 if (size
== pCert
->cbCertEncoded
&&
820 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
823 CryptMemFree(certEncoded
);
826 CryptReleaseContext(csp
, 0);
831 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
832 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
834 CRYPT_KEY_PROV_INFO copy
;
835 WCHAR containerW
[MAX_PATH
];
836 BOOL matches
= FALSE
;
838 MultiByteToWideChar(CP_ACP
, 0, container
, -1,
839 containerW
, sizeof(containerW
) / sizeof(containerW
[0]));
840 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
841 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
845 copy
.pwszContainerName
= containerW
;
846 matches
= key_prov_info_matches_cert(pCert
, ©
);
849 keyProvInfo
->pwszContainerName
=
850 CryptMemAlloc((strlenW(containerW
) + 1) * sizeof(WCHAR
));
851 if (keyProvInfo
->pwszContainerName
)
853 strcpyW(keyProvInfo
->pwszContainerName
, containerW
);
854 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
862 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
863 * private key matches pCert's public key. Upon success, updates keyProvInfo
864 * with the matching container's info (free keyProvInfo.pwszContainerName upon
866 * Returns TRUE if found, FALSE if not.
868 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
869 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
871 HCRYPTPROV defProvider
;
872 BOOL ret
, found
= FALSE
;
873 char containerA
[MAX_PATH
];
875 assert(keyProvInfo
->pwszContainerName
== NULL
);
876 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
877 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
878 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
880 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
882 while (ret
&& !found
)
884 DWORD size
= sizeof(containerA
);
886 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
887 (BYTE
*)containerA
, &size
, enumFlags
);
889 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
890 if (enumFlags
& CRYPT_FIRST
)
892 enumFlags
&= ~CRYPT_FIRST
;
893 enumFlags
|= CRYPT_NEXT
;
896 CryptReleaseContext(defProvider
, 0);
901 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
903 BOOL found
= FALSE
, ret
= TRUE
;
904 DWORD index
= 0, cbProvName
= 0;
905 CRYPT_KEY_PROV_INFO keyProvInfo
;
907 TRACE("(%p, %08x)\n", pCert
, dwFlags
);
909 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
910 while (ret
&& !found
)
914 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
918 if (size
<= cbProvName
)
919 ret
= CryptEnumProvidersW(index
, NULL
, 0,
920 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
923 CryptMemFree(keyProvInfo
.pwszProvName
);
924 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
925 if (keyProvInfo
.pwszProvName
)
928 ret
= CryptEnumProvidersW(index
, NULL
, 0,
929 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
932 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
933 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
934 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
935 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
936 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
938 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
939 found
= find_key_prov_info_in_provider(pCert
,
944 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
945 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
946 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
948 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
949 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
950 found
= find_key_prov_info_in_provider(pCert
,
963 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
965 CryptMemFree(keyProvInfo
.pwszProvName
);
966 CryptMemFree(keyProvInfo
.pwszContainerName
);
970 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
972 BOOL matches
= FALSE
;
975 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
978 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
982 if (CertGetCertificateContextProperty(pCert
,
983 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
984 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
985 CryptMemFree(keyProvInfo
);
991 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
992 DWORD dwFlags
, void *pvReserved
)
994 BOOL matches
= FALSE
;
996 TRACE("(%p, %08x, %p)\n", pCert
, dwFlags
, pvReserved
);
998 matches
= cert_prov_info_matches_cert(pCert
);
1000 matches
= find_matching_provider(pCert
, dwFlags
);
1004 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1005 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1009 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1011 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1012 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1013 &pCertId2
->SerialNumber
);
1014 TRACE("returning %d\n", ret
);
1018 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1019 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1023 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1025 if (pCertName1
->cbData
== pCertName2
->cbData
)
1027 if (pCertName1
->cbData
)
1028 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1029 pCertName1
->cbData
);
1035 TRACE("returning %d\n", ret
);
1039 /* Returns the number of significant bytes in pInt, where a byte is
1040 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1041 * for negative numbers. pInt is assumed to be little-endian.
1043 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1045 DWORD ret
= pInt
->cbData
;
1049 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1051 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1059 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1060 PCRYPT_INTEGER_BLOB pInt2
)
1065 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1067 cb1
= CRYPT_significantBytes(pInt1
);
1068 cb2
= CRYPT_significantBytes(pInt2
);
1072 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1078 TRACE("returning %d\n", ret
);
1082 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1083 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1087 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1089 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1091 case 0: /* Seems to mean "raw binary bits" */
1092 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1093 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1095 if (pPublicKey2
->PublicKey
.cbData
)
1096 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1097 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1105 WARN("Unknown encoding type %08x\n", dwCertEncodingType
);
1107 case X509_ASN_ENCODING
:
1109 BLOBHEADER
*pblob1
, *pblob2
;
1112 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1113 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1116 pblob1
= CryptMemAlloc(length
);
1117 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1118 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1119 0, pblob1
, &length
))
1121 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1122 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1125 pblob2
= CryptMemAlloc(length
);
1126 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1127 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1128 0, pblob2
, &length
))
1130 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1131 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1132 *pk2
= (LPVOID
)(pblob2
+ 1);
1133 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1134 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1136 CryptMemFree(pblob2
);
1139 CryptMemFree(pblob1
);
1148 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1149 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1153 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
1155 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1157 SetLastError(ERROR_FILE_NOT_FOUND
);
1160 if (pPublicKey
->Algorithm
.pszObjId
&&
1161 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1163 FIXME("unimplemented for DH public keys\n");
1164 SetLastError(CRYPT_E_ASN1_BADTAG
);
1170 BOOL ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1171 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1172 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1177 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1179 len
= rsaPubKey
->bitlen
;
1186 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1187 DWORD dwFlags
, const void *pvPara
);
1189 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1190 DWORD dwFlags
, const void *pvPara
)
1194 DWORD size
= sizeof(hash
);
1196 ret
= CertGetCertificateContextProperty(pCertContext
,
1197 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1200 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1202 if (size
== pHash
->cbData
)
1203 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1210 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1211 DWORD dwFlags
, const void *pvPara
)
1215 DWORD size
= sizeof(hash
);
1217 ret
= CertGetCertificateContextProperty(pCertContext
,
1218 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1221 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1223 if (size
== pHash
->cbData
)
1224 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1231 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1232 DWORD dwFlags
, const void *pvPara
)
1234 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1237 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1238 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1240 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1241 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1246 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1247 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1249 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1252 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1253 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1257 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1258 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1260 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1263 /* Matching serial number and subject match.. */
1264 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1265 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1267 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1268 &pCertInfo
->SerialNumber
);
1271 /* failing that, if the serial number and issuer match, we match */
1272 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1273 &pCertInfo
->SerialNumber
);
1275 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1276 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1278 TRACE("returning %d\n", ret
);
1282 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1283 DWORD dwFlags
, const void *pvPara
)
1285 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1288 switch (id
->dwIdChoice
)
1290 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1291 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1292 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1294 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1295 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1297 case CERT_ID_SHA1_HASH
:
1298 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1301 case CERT_ID_KEY_IDENTIFIER
:
1305 ret
= CertGetCertificateContextProperty(pCertContext
,
1306 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1307 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1309 LPBYTE buf
= CryptMemAlloc(size
);
1313 CertGetCertificateContextProperty(pCertContext
,
1314 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1315 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1330 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1331 DWORD dwFlags
, const void *pvPara
)
1333 PCCERT_CONTEXT toCompare
= pvPara
;
1334 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1335 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1338 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1339 DWORD dwFlags
, const void *pvPara
)
1341 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1345 ret
= CertGetCertificateContextProperty(pCertContext
,
1346 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1347 if (ret
&& size
== hash
->cbData
)
1349 LPBYTE buf
= CryptMemAlloc(size
);
1353 CertGetCertificateContextProperty(pCertContext
,
1354 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1355 ret
= !memcmp(buf
, hash
->pbData
, size
);
1364 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1365 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1368 BOOL matches
= FALSE
;
1373 ret
= CertEnumCertificatesInStore(store
, ret
);
1375 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1376 } while (ret
!= NULL
&& !matches
);
1380 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1381 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1383 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1384 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1386 return CertEnumCertificatesInStore(store
, prev
);
1389 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1390 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1393 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1394 PCERT_EXTENSION ext
;
1397 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1398 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1400 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1402 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1403 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1404 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1410 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1412 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1413 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1414 sizeof(CERT_NAME_BLOB
));
1415 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1416 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1418 else if (info
->KeyId
.cbData
)
1420 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1421 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1426 found
= cert_compare_certs_in_store(store
, prev
,
1427 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1431 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1432 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1434 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1436 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1437 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1438 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1444 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1445 info
->AuthorityCertSerialNumber
.cbData
)
1447 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1450 for (i
= 0; !directoryName
&&
1451 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1452 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1453 == CERT_ALT_NAME_DIRECTORY_NAME
)
1455 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1458 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1459 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1460 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1461 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1462 &info
->AuthorityCertSerialNumber
,
1463 sizeof(CRYPT_INTEGER_BLOB
));
1467 FIXME("no supported name type in authority key id2\n");
1471 else if (info
->KeyId
.cbData
)
1473 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1474 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1479 found
= cert_compare_certs_in_store(store
, prev
,
1480 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1485 found
= cert_compare_certs_in_store(store
, prev
,
1486 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1487 dwFlags
, &subject
->pCertInfo
->Issuer
);
1491 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1492 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1494 PCERT_NAME_BLOB name
;
1498 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1499 name
= &pCertContext
->pCertInfo
->Subject
;
1501 name
= &pCertContext
->pCertInfo
->Issuer
;
1502 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1503 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1506 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1512 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1513 CERT_SIMPLE_NAME_STR
, str
, len
);
1514 for (ptr
= str
; *ptr
; ptr
++)
1515 *ptr
= tolowerW(*ptr
);
1516 if (strstrW(str
, pvPara
))
1524 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1525 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1527 PCCERT_CONTEXT found
= NULL
;
1529 TRACE("%s\n", debugstr_a(pvPara
));
1533 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1534 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1540 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1541 for (ptr
= str
; *ptr
; ptr
++)
1542 *ptr
= tolowerW(*ptr
);
1543 found
= cert_compare_certs_in_store(store
, prev
,
1544 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1549 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1553 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1554 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1556 PCCERT_CONTEXT found
= NULL
;
1558 TRACE("%s\n", debugstr_w(pvPara
));
1562 DWORD len
= strlenW(pvPara
);
1563 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1570 for (src
= pvPara
, dst
= str
; *src
; src
++, dst
++)
1571 *dst
= tolowerW(*src
);
1573 found
= cert_compare_certs_in_store(store
, prev
,
1574 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1579 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1583 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1584 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1585 PCCERT_CONTEXT pPrevCertContext
)
1588 CertFindFunc find
= NULL
;
1589 CertCompareFunc compare
= NULL
;
1591 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1592 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1594 switch (dwType
>> CERT_COMPARE_SHIFT
)
1596 case CERT_COMPARE_ANY
:
1597 find
= find_cert_any
;
1599 case CERT_COMPARE_MD5_HASH
:
1600 compare
= compare_cert_by_md5_hash
;
1602 case CERT_COMPARE_SHA1_HASH
:
1603 compare
= compare_cert_by_sha1_hash
;
1605 case CERT_COMPARE_NAME
:
1606 compare
= compare_cert_by_name
;
1608 case CERT_COMPARE_PUBLIC_KEY
:
1609 compare
= compare_cert_by_public_key
;
1611 case CERT_COMPARE_NAME_STR_A
:
1612 find
= find_cert_by_name_str_a
;
1614 case CERT_COMPARE_NAME_STR_W
:
1615 find
= find_cert_by_name_str_w
;
1617 case CERT_COMPARE_SUBJECT_CERT
:
1618 compare
= compare_cert_by_subject_cert
;
1620 case CERT_COMPARE_CERT_ID
:
1621 compare
= compare_cert_by_cert_id
;
1623 case CERT_COMPARE_ISSUER_OF
:
1624 find
= find_cert_by_issuer
;
1626 case CERT_COMPARE_EXISTING
:
1627 compare
= compare_existing_cert
;
1629 case CERT_COMPARE_SIGNATURE_HASH
:
1630 compare
= compare_cert_by_signature_hash
;
1633 FIXME("find type %08x unimplemented\n", dwType
);
1637 ret
= find(hCertStore
, dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1639 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1640 compare
, dwType
, dwFlags
, pvPara
);
1644 SetLastError(CRYPT_E_NOT_FOUND
);
1645 TRACE("returning %p\n", ret
);
1649 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1650 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1652 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1656 SetLastError(E_INVALIDARG
);
1659 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1660 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1663 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1664 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1666 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1667 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1669 if (*pdwFlags
& ~supportedFlags
)
1671 SetLastError(E_INVALIDARG
);
1674 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1677 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1680 /* FIXME: what if the CRL has expired? */
1683 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1684 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1685 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1688 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1690 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1692 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1693 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1695 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1697 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1698 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1699 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1700 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1705 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1706 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1711 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1712 pPrevIssuerContext
, *pdwFlags
);
1714 if (!pSubjectContext
)
1716 SetLastError(E_INVALIDARG
);
1720 ret
= CertFindCertificateInStore(hCertStore
,
1721 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1722 pSubjectContext
, pPrevIssuerContext
);
1725 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1728 CertFreeCertificateContext(ret
);
1732 TRACE("returning %p\n", ret
);
1736 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1741 } OLD_CERT_REVOCATION_STATUS
;
1743 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1744 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
1746 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1747 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1748 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1752 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1753 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1755 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1756 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1758 SetLastError(E_INVALIDARG
);
1763 static HCRYPTOIDFUNCSET set
= NULL
;
1767 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
1768 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
1774 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
1779 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
1783 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
1787 for (ptr
= dllList
; ret
&& *ptr
;
1788 ptr
+= lstrlenW(ptr
) + 1)
1790 CertVerifyRevocationFunc func
;
1791 HCRYPTOIDFUNCADDR hFunc
;
1793 ret
= CryptGetDefaultOIDFunctionAddress(set
,
1794 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
1797 ret
= func(dwEncodingType
, dwRevType
, cContext
,
1798 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1799 CryptFreeOIDFunctionAddress(hFunc
, 0);
1803 CryptMemFree(dllList
);
1807 SetLastError(ERROR_OUTOFMEMORY
);
1818 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
1819 CRYPT_ATTRIBUTE rgAttr
[])
1821 PCRYPT_ATTRIBUTE ret
= NULL
;
1824 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
1830 SetLastError(ERROR_INVALID_PARAMETER
);
1834 for (i
= 0; !ret
&& i
< cAttr
; i
++)
1835 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
1840 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
1841 CERT_EXTENSION rgExtensions
[])
1843 PCERT_EXTENSION ret
= NULL
;
1846 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
1852 SetLastError(ERROR_INVALID_PARAMETER
);
1856 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
1857 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
1858 rgExtensions
[i
].pszObjId
))
1859 ret
= &rgExtensions
[i
];
1863 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
1865 PCERT_RDN_ATTR ret
= NULL
;
1868 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
1872 SetLastError(ERROR_INVALID_PARAMETER
);
1876 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
1877 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
1878 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
1879 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
1880 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
1884 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
1885 const CERT_RDN_ATTR
*attr
)
1890 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
1892 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
1894 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
1896 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
1899 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
1902 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1903 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
1905 if (attr
->Value
.cbData
!=
1906 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1908 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1909 match
= !strncmpiW(nameStr
, attrStr
,
1910 attr
->Value
.cbData
/ sizeof(WCHAR
));
1912 match
= !strncmpW(nameStr
, attrStr
,
1913 attr
->Value
.cbData
/ sizeof(WCHAR
));
1914 TRACE("%s : %s => %d\n",
1915 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1916 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1922 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1923 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
1925 if (attr
->Value
.cbData
!=
1926 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1928 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1929 match
= !strncasecmp(nameStr
, attrStr
,
1930 attr
->Value
.cbData
);
1932 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
1933 TRACE("%s : %s => %d\n",
1934 debugstr_an(nameStr
, attr
->Value
.cbData
),
1935 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
1943 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
1944 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
1946 CERT_NAME_INFO
*name
;
1951 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
1954 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
1956 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
1957 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
1961 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
1962 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
1964 SetLastError(CRYPT_E_NO_MATCH
);
1970 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
1971 PCERT_INFO pCertInfo
)
1978 GetSystemTimeAsFileTime(&fileTime
);
1979 pTimeToVerify
= &fileTime
;
1981 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
1983 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
1990 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
1991 PCERT_INFO pIssuerInfo
)
1993 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
1995 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
1996 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
1999 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2000 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2001 DWORD
*pcbComputedHash
)
2004 HCRYPTHASH hHash
= 0;
2006 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2007 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2010 hCryptProv
= CRYPT_GetDefaultProvider();
2015 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2018 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2020 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2021 pcbComputedHash
, 0);
2022 CryptDestroyHash(hHash
);
2028 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2029 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2030 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2033 HCRYPTHASH hHash
= 0;
2035 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2036 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2039 hCryptProv
= CRYPT_GetDefaultProvider();
2042 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2044 SetLastError(ERROR_FILE_NOT_FOUND
);
2052 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2053 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2054 (LPBYTE
)&buf
, &size
);
2057 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2060 ret
= CryptHashData(hHash
, buf
, size
, 0);
2062 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2063 pcbComputedHash
, 0);
2064 CryptDestroyHash(hHash
);
2072 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2073 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2074 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2077 CERT_SIGNED_CONTENT_INFO
*info
;
2080 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv
, dwCertEncodingType
,
2081 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2083 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2084 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2087 PCCRYPT_OID_INFO oidInfo
;
2091 hCryptProv
= CRYPT_GetDefaultProvider();
2092 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2093 info
->SignatureAlgorithm
.pszObjId
, 0);
2096 SetLastError(NTE_BAD_ALGID
);
2101 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2104 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2105 info
->ToBeSigned
.cbData
, 0);
2107 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2108 pcbComputedHash
, 0);
2109 CryptDestroyHash(hHash
);
2117 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2118 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2119 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2120 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2123 PCCRYPT_OID_INFO info
;
2126 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
2127 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2128 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2130 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2131 pSignatureAlgorithm
->pszObjId
, 0);
2134 SetLastError(NTE_BAD_ALGID
);
2137 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2140 hCryptProv
= CRYPT_GetDefaultProvider();
2141 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2144 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2145 cbEncodedToBeSigned
, 0);
2147 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2149 CryptDestroyHash(hHash
);
2156 SetLastError(ERROR_INVALID_PARAMETER
);
2161 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2164 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2165 cbEncodedToBeSigned
, 0);
2167 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2169 CryptDestroyHash(hHash
);
2176 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2177 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2178 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2179 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2182 DWORD encodedSize
, hashSize
;
2184 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2185 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2186 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2188 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2189 NULL
, &encodedSize
);
2192 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2196 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2197 pvStructInfo
, encoded
, &encodedSize
);
2200 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2201 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2202 pvHashAuxInfo
, NULL
, &hashSize
);
2205 PBYTE hash
= CryptMemAlloc(hashSize
);
2209 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2210 dwCertEncodingType
, encoded
, encodedSize
,
2211 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2214 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2216 info
.ToBeSigned
.cbData
= encodedSize
;
2217 info
.ToBeSigned
.pbData
= encoded
;
2218 info
.SignatureAlgorithm
= *pSignatureAlgorithm
;
2219 info
.Signature
.cbData
= hashSize
;
2220 info
.Signature
.pbData
= hash
;
2221 info
.Signature
.cUnusedBits
= 0;
2222 ret
= CryptEncodeObject(dwCertEncodingType
,
2223 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2229 CryptMemFree(encoded
);
2235 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2236 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2237 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2239 CRYPT_DATA_BLOB blob
= { cbEncoded
, (BYTE
*)pbEncoded
};
2241 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2242 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, &blob
,
2243 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2246 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
,
2247 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pubKeyInfo
,
2248 const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2252 PCCRYPT_OID_INFO info
;
2253 ALG_ID pubKeyID
, hashID
;
2255 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2256 signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2257 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2259 SetLastError(NTE_BAD_ALGID
);
2262 hashID
= info
->u
.Algid
;
2263 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2264 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2267 /* Load the default provider if necessary */
2269 hCryptProv
= CRYPT_GetDefaultProvider();
2270 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2271 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2276 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2279 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2280 signedCert
->ToBeSigned
.cbData
, 0);
2282 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2283 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2284 CryptDestroyHash(hash
);
2286 CryptDestroyKey(key
);
2291 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2292 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2293 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2296 CRYPT_DATA_BLOB subjectBlob
;
2298 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
2299 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2300 dwFlags
, pvReserved
);
2302 switch (dwSubjectType
)
2304 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2306 PCRYPT_DATA_BLOB blob
= pvSubject
;
2308 subjectBlob
.pbData
= blob
->pbData
;
2309 subjectBlob
.cbData
= blob
->cbData
;
2312 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2314 PCERT_CONTEXT context
= pvSubject
;
2316 subjectBlob
.pbData
= context
->pbCertEncoded
;
2317 subjectBlob
.cbData
= context
->cbCertEncoded
;
2320 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2322 PCRL_CONTEXT context
= pvSubject
;
2324 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2325 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2329 SetLastError(E_INVALIDARG
);
2335 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2338 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2339 subjectBlob
.pbData
, subjectBlob
.cbData
,
2340 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2341 &signedCert
, &size
);
2344 switch (dwIssuerType
)
2346 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2347 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2348 dwCertEncodingType
, pvIssuer
,
2351 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2352 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2354 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2357 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2358 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2361 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2364 SetLastError(E_INVALIDARG
);
2369 FIXME("unimplemented for NULL signer\n");
2370 SetLastError(E_INVALIDARG
);
2375 SetLastError(E_INVALIDARG
);
2378 LocalFree(signedCert
);
2384 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2385 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
2387 PCERT_EXTENSION ext
;
2390 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
2393 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
2394 pCertInfo
->rgExtension
);
2397 CRYPT_BIT_BLOB usage
;
2398 DWORD size
= sizeof(usage
);
2400 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
2401 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2405 if (cbKeyUsage
< usage
.cbData
)
2409 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
2410 if (cbKeyUsage
> usage
.cbData
)
2411 memset(pbKeyUsage
+ usage
.cbData
, 0,
2412 cbKeyUsage
- usage
.cbData
);
2421 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
2422 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
2424 PCERT_ENHKEY_USAGE usage
= NULL
;
2428 if (!pCertContext
|| !pcbUsage
)
2430 SetLastError(ERROR_INVALID_PARAMETER
);
2434 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
2436 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
2440 if (CertGetCertificateContextProperty(pCertContext
,
2441 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
2443 LPBYTE buf
= CryptMemAlloc(propSize
);
2447 if (CertGetCertificateContextProperty(pCertContext
,
2448 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
2450 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2451 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
2452 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2458 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
2460 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
2461 pCertContext
->pCertInfo
->cExtension
,
2462 pCertContext
->pCertInfo
->rgExtension
);
2466 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2467 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2468 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2473 /* If a particular location is specified, this should fail. Otherwise
2474 * it should succeed with an empty usage. (This is true on Win2k and
2475 * later, which we emulate.)
2479 SetLastError(CRYPT_E_NOT_FOUND
);
2483 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
2489 *pcbUsage
= bytesNeeded
;
2490 else if (*pcbUsage
< bytesNeeded
)
2492 SetLastError(ERROR_MORE_DATA
);
2493 *pcbUsage
= bytesNeeded
;
2498 *pcbUsage
= bytesNeeded
;
2502 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
2503 sizeof(CERT_ENHKEY_USAGE
) +
2504 usage
->cUsageIdentifier
* sizeof(LPSTR
));
2506 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
2507 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
2508 sizeof(CERT_ENHKEY_USAGE
));
2509 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2511 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2512 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2513 nextOID
+= strlen(nextOID
) + 1;
2517 pUsage
->cUsageIdentifier
= 0;
2522 TRACE("returning %d\n", ret
);
2526 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
2527 PCERT_ENHKEY_USAGE pUsage
)
2531 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
2535 CRYPT_DATA_BLOB blob
= { 0, NULL
};
2537 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
2538 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
2541 ret
= CertSetCertificateContextProperty(pCertContext
,
2542 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
2543 LocalFree(blob
.pbData
);
2547 ret
= CertSetCertificateContextProperty(pCertContext
,
2548 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
2552 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2553 LPCSTR pszUsageIdentifier
)
2558 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2560 if (CertGetEnhancedKeyUsage(pCertContext
,
2561 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
2563 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
2567 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2568 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
2572 BOOL exists
= FALSE
;
2574 /* Make sure usage doesn't already exist */
2575 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
2577 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
2578 pszUsageIdentifier
))
2583 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
2584 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2590 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
2591 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
2592 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
2593 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
2594 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2596 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2597 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2598 nextOID
+= strlen(nextOID
) + 1;
2600 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2601 strcpy(nextOID
, pszUsageIdentifier
);
2602 newUsage
->cUsageIdentifier
= i
+ 1;
2603 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
2604 CryptMemFree(newUsage
);
2610 CryptMemFree(usage
);
2617 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
2618 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2622 usage
->rgpszUsageIdentifier
=
2623 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
2624 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
2625 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
2626 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
2627 usage
->cUsageIdentifier
= 1;
2628 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
2629 CryptMemFree(usage
);
2637 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2638 LPCSTR pszUsageIdentifier
)
2642 CERT_ENHKEY_USAGE usage
;
2644 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2646 size
= sizeof(usage
);
2647 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2648 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
2649 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2651 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2655 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2656 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
2659 if (pUsage
->cUsageIdentifier
)
2664 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
2666 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
2667 pszUsageIdentifier
))
2669 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
2670 pUsage
->rgpszUsageIdentifier
[i
] =
2671 pUsage
->rgpszUsageIdentifier
[i
+ 1];
2673 pUsage
->cUsageIdentifier
--;
2674 /* Remove the usage if it's empty */
2675 if (pUsage
->cUsageIdentifier
)
2676 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
2678 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
2681 CryptMemFree(pUsage
);
2688 /* it fit in an empty usage, therefore there's nothing to remove */
2700 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
2702 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
2704 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2706 if (indexIndex
+ 1 > field
->cIndexes
)
2708 if (field
->cIndexes
)
2709 field
->indexes
= CryptMemRealloc(field
->indexes
,
2710 (indexIndex
+ 1) * sizeof(DWORD
));
2712 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
2715 field
->indexes
[indexIndex
] = 0;
2716 field
->cIndexes
= indexIndex
+ 1;
2720 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
2723 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
2726 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2728 assert(field
->cIndexes
);
2729 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
2733 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
2734 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
2737 DWORD i
, cbOIDs
= 0;
2738 BOOL allUsagesValid
= TRUE
;
2739 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
2741 TRACE("(%d, %p, %d, %p, %d)\n", cCerts
, rghCerts
, *cNumOIDs
,
2744 for (i
= 0; i
< cCerts
; i
++)
2746 CERT_ENHKEY_USAGE usage
;
2747 DWORD size
= sizeof(usage
);
2749 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
2750 /* Success is deliberately ignored: it implies all usages are valid */
2751 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2753 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2755 allUsagesValid
= FALSE
;
2758 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
2761 if (!validUsages
.cUsageIdentifier
)
2765 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
2766 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
2767 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2768 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
2770 validUsages
.rgpszUsageIdentifier
=
2771 CryptMemAlloc(cbOIDs
);
2772 if (validUsages
.rgpszUsageIdentifier
)
2774 LPSTR nextOID
= (LPSTR
)
2775 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
2776 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2778 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2780 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
2781 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
2782 pUsage
->rgpszUsageIdentifier
[j
]);
2783 nextOID
+= lstrlenA(nextOID
) + 1;
2789 struct BitField validIndexes
= { 0, NULL
};
2790 DWORD j
, k
, numRemoved
= 0;
2792 /* Merge: build a bitmap of all the indexes of
2793 * validUsages.rgpszUsageIdentifier that are in pUsage.
2795 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
2797 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
2799 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
2800 validUsages
.rgpszUsageIdentifier
[k
]))
2802 CRYPT_SetBitInField(&validIndexes
, k
);
2807 /* Merge by removing from validUsages those that are
2808 * not in the bitmap.
2810 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2812 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
2814 if (j
< validUsages
.cUsageIdentifier
- 1)
2816 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
2817 &validUsages
.rgpszUsageIdentifier
[j
+
2819 (validUsages
.cUsageIdentifier
- numRemoved
2820 - j
- 1) * sizeof(LPSTR
));
2822 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
2824 validUsages
.cUsageIdentifier
--;
2828 validUsages
.cUsageIdentifier
--;
2831 CryptMemFree(validIndexes
.indexes
);
2834 CryptMemFree(pUsage
);
2846 *cNumOIDs
= validUsages
.cUsageIdentifier
;
2849 else if (*pcbOIDs
< cbOIDs
)
2852 SetLastError(ERROR_MORE_DATA
);
2857 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
2858 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2861 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
2863 rghOIDs
[i
] = nextOID
;
2864 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
2865 nextOID
+= lstrlenA(nextOID
) + 1;
2869 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
2870 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
2871 TRACE("returning %d\n", ret
);
2875 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
2876 * pInfo is NULL, from the attributes of hProv.
2878 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
2879 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
2881 CRYPT_KEY_PROV_INFO info
= { 0 };
2889 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
2892 LPSTR szContainer
= CryptMemAlloc(size
);
2896 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
2897 (BYTE
*)szContainer
, &size
, 0);
2900 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2904 info
.pwszContainerName
= CryptMemAlloc(len
*
2906 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2907 info
.pwszContainerName
, len
);
2910 CryptMemFree(szContainer
);
2913 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
2916 LPSTR szProvider
= CryptMemAlloc(size
);
2920 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
2924 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2928 info
.pwszProvName
= CryptMemAlloc(len
*
2930 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2931 info
.pwszProvName
, len
);
2934 CryptMemFree(szProvider
);
2937 size
= sizeof(info
.dwKeySpec
);
2938 /* in case no CRYPT_KEY_PROV_INFO given,
2939 * we always use AT_SIGNATURE key spec
2941 info
.dwKeySpec
= AT_SIGNATURE
;
2942 size
= sizeof(info
.dwProvType
);
2943 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
2946 info
.dwProvType
= PROV_RSA_FULL
;
2950 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
2955 CryptMemFree(info
.pwszContainerName
);
2956 CryptMemFree(info
.pwszProvName
);
2960 /* Creates a signed certificate context from the unsigned, encoded certificate
2961 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
2963 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
2964 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
2966 PCCERT_CONTEXT context
= NULL
;
2970 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2971 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
2974 LPBYTE sig
= CryptMemAlloc(sigSize
);
2976 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2977 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
2980 CERT_SIGNED_CONTENT_INFO signedInfo
;
2981 BYTE
*encodedSignedCert
= NULL
;
2982 DWORD encodedSignedCertSize
= 0;
2984 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
2985 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
2986 signedInfo
.SignatureAlgorithm
= *sigAlgo
;
2987 signedInfo
.Signature
.cbData
= sigSize
;
2988 signedInfo
.Signature
.pbData
= sig
;
2989 signedInfo
.Signature
.cUnusedBits
= 0;
2990 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
2991 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2992 &encodedSignedCert
, &encodedSignedCertSize
);
2995 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2996 encodedSignedCert
, encodedSignedCertSize
);
2997 LocalFree(encodedSignedCert
);
3005 /* Copies data from the parameters into info, where:
3006 * pSerialNumber: The serial number. Must not be NULL.
3007 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3009 * pSignatureAlgorithm: Optional.
3010 * pStartTime: The starting time of the certificate. If NULL, the current
3011 * system time is used.
3012 * pEndTime: The ending time of the certificate. If NULL, one year past the
3013 * starting time is used.
3014 * pubKey: The public key of the certificate. Must not be NULL.
3015 * pExtensions: Extensions to be included with the certificate. Optional.
3017 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3018 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3019 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3020 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3021 const CERT_EXTENSIONS
*pExtensions
)
3023 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3026 assert(pSerialNumber
);
3027 assert(pSubjectIssuerBlob
);
3030 if (pExtensions
&& pExtensions
->cExtension
)
3031 info
->dwVersion
= CERT_V3
;
3033 info
->dwVersion
= CERT_V1
;
3034 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3035 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3036 if (pSignatureAlgorithm
)
3037 info
->SignatureAlgorithm
= *pSignatureAlgorithm
;
3040 info
->SignatureAlgorithm
.pszObjId
= oid
;
3041 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3042 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3044 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3045 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3047 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3049 GetSystemTimeAsFileTime(&info
->NotBefore
);
3051 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3056 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3059 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3062 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3063 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3064 info
->SubjectPublicKeyInfo
= *pubKey
;
3067 info
->cExtension
= pExtensions
->cExtension
;
3068 info
->rgExtension
= pExtensions
->rgExtension
;
3072 info
->cExtension
= 0;
3073 info
->rgExtension
= NULL
;
3077 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3078 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3079 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3081 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3083 HCRYPTPROV hProv
= 0;
3084 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
3088 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3090 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3092 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3093 rpcrt
, "RpcStringFreeA");
3095 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3098 RPC_STATUS status
= uuidCreate(&uuid
);
3100 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3102 unsigned char *uuidStr
;
3104 status
= uuidToString(&uuid
, &uuidStr
);
3105 if (status
== RPC_S_OK
)
3107 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3108 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3114 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3116 CryptDestroyKey(key
);
3118 rpcStringFree(&uuidStr
);
3127 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3128 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3129 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3130 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3131 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3133 PCCERT_CONTEXT context
= NULL
;
3134 BOOL ret
, releaseContext
= FALSE
;
3135 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3136 DWORD pubKeySize
= 0, dwKeySpec
;
3138 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
3139 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3140 pExtensions
, pExtensions
);
3142 if(!pSubjectIssuerBlob
)
3144 SetLastError(ERROR_INVALID_PARAMETER
);
3148 dwKeySpec
= pKeyProvInfo
? pKeyProvInfo
->dwKeySpec
: AT_SIGNATURE
;
3153 hProv
= CRYPT_CreateKeyProv();
3154 releaseContext
= TRUE
;
3156 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3158 SetLastError(NTE_BAD_FLAGS
);
3164 /* acquire the context using the given information*/
3165 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3166 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3167 pKeyProvInfo
->dwFlags
);
3170 if(GetLastError() != NTE_BAD_KEYSET
)
3172 /* create the key set */
3173 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3174 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3175 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3179 /* check if the key is here */
3180 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3183 if (NTE_NO_KEY
== GetLastError())
3184 { /* generate the key */
3185 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3189 CryptReleaseContext(hProv
,0);
3190 SetLastError(NTE_BAD_KEYSET
);
3194 CryptDestroyKey(hKey
);
3195 releaseContext
= TRUE
;
3199 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3203 pubKey
= CryptMemAlloc(pubKeySize
);
3206 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3207 pubKey
, &pubKeySize
);
3210 CERT_INFO info
= { 0 };
3211 CRYPT_DER_BLOB blob
= { 0, NULL
};
3213 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3215 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3216 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3217 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3218 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3219 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3223 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3224 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3225 &info
.SignatureAlgorithm
);
3227 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3228 blob
.pbData
, blob
.cbData
);
3229 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3230 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3231 LocalFree(blob
.pbData
);
3234 CryptMemFree(pubKey
);
3238 CryptReleaseContext(hProv
, 0);
3242 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3243 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3244 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3245 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3247 FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType
,
3248 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3249 pVerifyUsageStatus
);
3250 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3254 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3255 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3256 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3258 TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType
, dwEncodingType
,
3259 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3263 FIXME("dwFlags 0x%08x not handled\n", dwFlags
);
3268 FIXME("pCreatePara not handled\n");
3272 switch (dwContextType
)
3274 case CERT_STORE_CERTIFICATE_CONTEXT
:
3275 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3276 case CERT_STORE_CRL_CONTEXT
:
3277 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3278 case CERT_STORE_CTL_CONTEXT
:
3279 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3281 WARN("unknown context type: 0x%x\n", dwContextType
);