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 BOOL WINAPI CertAddCertificateLinkToStore(HCERTSTORE hCertStore,
113 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
114 PCCERT_CONTEXT *ppCertContext)
117 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
120 FIXME("(%p, %p, %08x, %p): semi-stub\n", hCertStore, pCertContext,
121 dwAddDisposition, ppCertContext);
122 if (store->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
124 if (store->type == StoreTypeCollection)
126 SetLastError(E_INVALIDARG);
129 return CertAddCertificateContextToStore(hCertStore, pCertContext,
130 dwAddDisposition, ppCertContext);
133 PCCERT_CONTEXT WINAPI CertCreateCertificateContext(DWORD dwCertEncodingType,
134 const BYTE *pbCertEncoded, DWORD cbCertEncoded)
136 PCERT_CONTEXT cert = NULL;
138 PCERT_INFO certInfo = NULL;
141 TRACE("(%08x, %p, %d)\n", dwCertEncodingType, pbCertEncoded,
144 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING)
146 SetLastError(E_INVALIDARG);
150 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT_TO_BE_SIGNED,
151 pbCertEncoded, cbCertEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
157 cert = Context_CreateDataContext(sizeof(CERT_CONTEXT));
160 data = CryptMemAlloc(cbCertEncoded);
163 CertFreeCertificateContext(cert);
167 memcpy(data, pbCertEncoded, cbCertEncoded);
168 cert->dwCertEncodingType = dwCertEncodingType;
169 cert->pbCertEncoded = data;
170 cert->cbCertEncoded = cbCertEncoded;
171 cert->pCertInfo = certInfo;
172 cert->hCertStore = 0;
179 PCCERT_CONTEXT WINAPI CertDuplicateCertificateContext(
180 PCCERT_CONTEXT pCertContext)
182 TRACE("(%p)\n", pCertContext);
187 Context_AddRef((void *)pCertContext, sizeof(CERT_CONTEXT));
191 static void CertDataContext_Free(void *context)
193 PCERT_CONTEXT certContext = context;
195 CryptMemFree(certContext->pbCertEncoded);
196 LocalFree(certContext->pCertInfo);
199 BOOL WINAPI CertFreeCertificateContext(PCCERT_CONTEXT pCertContext)
203 TRACE("(%p)\n", pCertContext);
206 ret = Context_Release((void *)pCertContext, sizeof(CERT_CONTEXT),
207 CertDataContext_Free);
211 DWORD WINAPI CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext,
214 PCONTEXT_PROPERTY_LIST properties = Context_GetProperties(
215 pCertContext, sizeof(CERT_CONTEXT));
218 TRACE("(%p, %d)\n", pCertContext, dwPropId);
221 ret = ContextPropertyList_EnumPropIDs(properties, dwPropId);
227 static BOOL CertContext_GetHashProp(void *context, DWORD dwPropId,
228 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
231 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
235 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
237 ret = CertContext_SetProperty(context, dwPropId, 0, &blob);
242 static BOOL CertContext_CopyParam(void *pvData, DWORD *pcbData, const void *pb,
249 else if (*pcbData < cb)
251 SetLastError(ERROR_MORE_DATA);
257 memcpy(pvData, pb, cb);
263 static BOOL CertContext_GetProperty(void *context, DWORD dwPropId,
264 void *pvData, DWORD *pcbData)
266 PCCERT_CONTEXT pCertContext = context;
267 PCONTEXT_PROPERTY_LIST properties =
268 Context_GetProperties(context, sizeof(CERT_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(PCONTEXT_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 PCONTEXT_PROPERTY_LIST properties =
519 Context_GetProperties(context, sizeof(CERT_CONTEXT));
522 TRACE("(%p, %d, %08x, %p)\n", context, dwPropId, dwFlags, pvData);
530 case CERT_AUTO_ENROLL_PROP_ID:
531 case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
532 case CERT_DESCRIPTION_PROP_ID:
533 case CERT_FRIENDLY_NAME_PROP_ID:
534 case CERT_HASH_PROP_ID:
535 case CERT_KEY_IDENTIFIER_PROP_ID:
536 case CERT_MD5_HASH_PROP_ID:
537 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
538 case CERT_PUBKEY_ALG_PARA_PROP_ID:
539 case CERT_PVK_FILE_PROP_ID:
540 case CERT_SIGNATURE_HASH_PROP_ID:
541 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
542 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
543 case CERT_EXTENDED_ERROR_INFO_PROP_ID:
544 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
545 case CERT_ENROLLMENT_PROP_ID:
546 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
547 case CERT_RENEWAL_PROP_ID:
551 const CRYPT_DATA_BLOB *blob = pvData;
553 ret = ContextPropertyList_SetProperty(properties, dwPropId,
554 blob->pbData, blob->cbData);
558 ContextPropertyList_RemoveProperty(properties, dwPropId);
563 case CERT_DATE_STAMP_PROP_ID:
565 ret = ContextPropertyList_SetProperty(properties, dwPropId,
566 pvData, sizeof(FILETIME));
569 ContextPropertyList_RemoveProperty(properties, dwPropId);
573 case CERT_KEY_CONTEXT_PROP_ID:
577 const CERT_KEY_CONTEXT *keyContext = pvData;
579 if (keyContext->cbSize != sizeof(CERT_KEY_CONTEXT))
581 SetLastError(E_INVALIDARG);
585 ret = ContextPropertyList_SetProperty(properties, dwPropId,
586 (const BYTE *)keyContext, keyContext->cbSize);
590 ContextPropertyList_RemoveProperty(properties, dwPropId);
595 case CERT_KEY_PROV_INFO_PROP_ID:
597 ret = CertContext_SetKeyProvInfoProperty(properties, pvData);
600 ContextPropertyList_RemoveProperty(properties, dwPropId);
604 case CERT_KEY_PROV_HANDLE_PROP_ID:
606 CERT_KEY_CONTEXT keyContext;
607 DWORD size = sizeof(keyContext);
609 ret = CertContext_GetProperty(context, CERT_KEY_CONTEXT_PROP_ID,
613 if (!(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
614 CryptReleaseContext(keyContext.hCryptProv, 0);
616 keyContext.cbSize = sizeof(keyContext);
618 keyContext.hCryptProv = *(const HCRYPTPROV *)pvData;
621 keyContext.hCryptProv = 0;
622 keyContext.dwKeySpec = AT_SIGNATURE;
624 ret = CertContext_SetProperty(context, CERT_KEY_CONTEXT_PROP_ID,
629 FIXME("%d: stub\n", dwPropId);
633 TRACE("returning %d\n", ret);
637 BOOL WINAPI CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
638 DWORD dwPropId, DWORD dwFlags, const void *pvData)
642 TRACE("(%p, %d, %08x, %p)\n", pCertContext, dwPropId, dwFlags, pvData);
644 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
645 * crashes on most of these, I'll be safer.
650 case CERT_ACCESS_STATE_PROP_ID:
651 case CERT_CERT_PROP_ID:
652 case CERT_CRL_PROP_ID:
653 case CERT_CTL_PROP_ID:
654 SetLastError(E_INVALIDARG);
657 ret = CertContext_SetProperty((void *)pCertContext, dwPropId, dwFlags,
659 TRACE("returning %d\n", ret);
663 /* Acquires the private key using the key provider info, retrieving info from
664 * the certificate if info is NULL. The acquired provider is returned in
665 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
667 static BOOL CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert,
668 PCRYPT_KEY_PROV_INFO info, HCRYPTPROV *phCryptProv, DWORD *pdwKeySpec)
671 BOOL allocated = FALSE, ret = TRUE;
675 ret = CertGetCertificateContextProperty(pCert,
676 CERT_KEY_PROV_INFO_PROP_ID, 0, &size);
679 info = HeapAlloc(GetProcessHeap(), 0, size);
682 ret = CertGetCertificateContextProperty(pCert,
683 CERT_KEY_PROV_INFO_PROP_ID, info, &size);
688 SetLastError(ERROR_OUTOFMEMORY);
693 SetLastError(CRYPT_E_NO_KEY_PROPERTY);
697 ret = CryptAcquireContextW(phCryptProv, info->pwszContainerName,
698 info->pwszProvName, info->dwProvType, 0);
703 for (i = 0; i < info->cProvParam; i++)
705 CryptSetProvParam(*phCryptProv,
706 info->rgProvParam[i].dwParam, info->rgProvParam[i].pbData,
707 info->rgProvParam[i].dwFlags);
709 *pdwKeySpec = info->dwKeySpec;
712 SetLastError(CRYPT_E_NO_KEY_PROPERTY);
715 HeapFree(GetProcessHeap(), 0, info);
719 BOOL WINAPI CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert,
720 DWORD dwFlags, void *pvReserved, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE *phCryptProv,
721 DWORD *pdwKeySpec, BOOL *pfCallerFreeProv)
723 BOOL ret = FALSE, cache = FALSE;
724 PCRYPT_KEY_PROV_INFO info = NULL;
725 CERT_KEY_CONTEXT keyContext;
728 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert, dwFlags, pvReserved,
729 phCryptProv, pdwKeySpec, pfCallerFreeProv);
731 if (dwFlags & CRYPT_ACQUIRE_USE_PROV_INFO_FLAG)
735 ret = CertGetCertificateContextProperty(pCert,
736 CERT_KEY_PROV_INFO_PROP_ID, 0, &size);
739 info = HeapAlloc(GetProcessHeap(), 0, size);
740 ret = CertGetCertificateContextProperty(pCert,
741 CERT_KEY_PROV_INFO_PROP_ID, info, &size);
743 cache = info->dwFlags & CERT_SET_KEY_CONTEXT_PROP_ID;
746 else if (dwFlags & CRYPT_ACQUIRE_CACHE_FLAG)
751 size = sizeof(keyContext);
752 ret = CertGetCertificateContextProperty(pCert, CERT_KEY_CONTEXT_PROP_ID,
756 *phCryptProv = keyContext.hCryptProv;
758 *pdwKeySpec = keyContext.dwKeySpec;
759 if (pfCallerFreeProv)
760 *pfCallerFreeProv = !cache;
765 ret = CRYPT_AcquirePrivateKeyFromProvInfo(pCert, info,
766 &keyContext.hCryptProv, &keyContext.dwKeySpec);
769 *phCryptProv = keyContext.hCryptProv;
771 *pdwKeySpec = keyContext.dwKeySpec;
774 keyContext.cbSize = sizeof(keyContext);
775 if (CertSetCertificateContextProperty(pCert,
776 CERT_KEY_CONTEXT_PROP_ID, 0, &keyContext))
778 if (pfCallerFreeProv)
779 *pfCallerFreeProv = FALSE;
784 if (pfCallerFreeProv)
785 *pfCallerFreeProv = TRUE;
789 HeapFree(GetProcessHeap(), 0, info);
793 static BOOL key_prov_info_matches_cert(PCCERT_CONTEXT pCert,
794 const CRYPT_KEY_PROV_INFO *keyProvInfo)
797 BOOL matches = FALSE;
799 if (CryptAcquireContextW(&csp, keyProvInfo->pwszContainerName,
800 keyProvInfo->pwszProvName, keyProvInfo->dwProvType, keyProvInfo->dwFlags))
804 /* Need to sign something to verify the sig. What to sign? Why not
805 * the certificate itself?
807 if (CryptSignAndEncodeCertificate(csp, AT_SIGNATURE,
808 pCert->dwCertEncodingType, X509_CERT_TO_BE_SIGNED, pCert->pCertInfo,
809 &pCert->pCertInfo->SignatureAlgorithm, NULL, NULL, &size))
811 BYTE *certEncoded = CryptMemAlloc(size);
815 if (CryptSignAndEncodeCertificate(csp, AT_SIGNATURE,
816 pCert->dwCertEncodingType, X509_CERT_TO_BE_SIGNED,
817 pCert->pCertInfo, &pCert->pCertInfo->SignatureAlgorithm,
818 NULL, certEncoded, &size))
820 if (size == pCert->cbCertEncoded &&
821 !memcmp(certEncoded, pCert->pbCertEncoded, size))
824 CryptMemFree(certEncoded);
827 CryptReleaseContext(csp, 0);
832 static BOOL container_matches_cert(PCCERT_CONTEXT pCert, LPCSTR container,
833 CRYPT_KEY_PROV_INFO *keyProvInfo)
835 CRYPT_KEY_PROV_INFO copy;
836 WCHAR containerW[MAX_PATH];
837 BOOL matches = FALSE;
839 MultiByteToWideChar(CP_ACP, 0, container, -1,
840 containerW, sizeof(containerW) / sizeof(containerW[0]));
841 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
842 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
845 memcpy(©, keyProvInfo, sizeof(copy));
846 copy.pwszContainerName = containerW;
847 matches = key_prov_info_matches_cert(pCert, ©);
850 keyProvInfo->pwszContainerName =
851 CryptMemAlloc((strlenW(containerW) + 1) * sizeof(WCHAR));
852 if (keyProvInfo->pwszContainerName)
854 strcpyW(keyProvInfo->pwszContainerName, containerW);
855 keyProvInfo->dwKeySpec = AT_SIGNATURE;
863 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
864 * private key matches pCert's public key. Upon success, updates keyProvInfo
865 * with the matching container's info (free keyProvInfo.pwszContainerName upon
867 * Returns TRUE if found, FALSE if not.
869 static BOOL find_key_prov_info_in_provider(PCCERT_CONTEXT pCert,
870 CRYPT_KEY_PROV_INFO *keyProvInfo)
872 HCRYPTPROV defProvider;
873 BOOL ret, found = FALSE;
874 char containerA[MAX_PATH];
876 assert(keyProvInfo->pwszContainerName == NULL);
877 if ((ret = CryptAcquireContextW(&defProvider, NULL,
878 keyProvInfo->pwszProvName, keyProvInfo->dwProvType,
879 keyProvInfo->dwFlags | CRYPT_VERIFYCONTEXT)))
881 DWORD enumFlags = keyProvInfo->dwFlags | CRYPT_FIRST;
883 while (ret && !found)
885 DWORD size = sizeof(containerA);
887 ret = CryptGetProvParam(defProvider, PP_ENUMCONTAINERS,
888 (BYTE *)containerA, &size, enumFlags);
890 found = container_matches_cert(pCert, containerA, keyProvInfo);
891 if (enumFlags & CRYPT_FIRST)
893 enumFlags &= ~CRYPT_FIRST;
894 enumFlags |= CRYPT_NEXT;
897 CryptReleaseContext(defProvider, 0);
902 static BOOL find_matching_provider(PCCERT_CONTEXT pCert, DWORD dwFlags)
904 BOOL found = FALSE, ret = TRUE;
905 DWORD index = 0, cbProvName = 0;
906 CRYPT_KEY_PROV_INFO keyProvInfo;
908 TRACE("(%p, %08x)\n", pCert, dwFlags);
910 memset(&keyProvInfo, 0, sizeof(keyProvInfo));
911 while (ret && !found)
915 ret = CryptEnumProvidersW(index, NULL, 0, &keyProvInfo.dwProvType,
919 if (size <= cbProvName)
920 ret = CryptEnumProvidersW(index, NULL, 0,
921 &keyProvInfo.dwProvType, keyProvInfo.pwszProvName, &size);
924 CryptMemFree(keyProvInfo.pwszProvName);
925 keyProvInfo.pwszProvName = CryptMemAlloc(size);
926 if (keyProvInfo.pwszProvName)
929 ret = CryptEnumProvidersW(index, NULL, 0,
930 &keyProvInfo.dwProvType, keyProvInfo.pwszProvName, &size);
933 if (dwFlags & CRYPT_FIND_SILENT_KEYSET_FLAG)
934 keyProvInfo.dwFlags |= CRYPT_SILENT;
935 if (dwFlags & CRYPT_FIND_USER_KEYSET_FLAG ||
936 !(dwFlags & (CRYPT_FIND_USER_KEYSET_FLAG |
937 CRYPT_FIND_MACHINE_KEYSET_FLAG)))
939 keyProvInfo.dwFlags |= CRYPT_USER_KEYSET;
940 found = find_key_prov_info_in_provider(pCert,
945 if (dwFlags & CRYPT_FIND_MACHINE_KEYSET_FLAG ||
946 !(dwFlags & (CRYPT_FIND_USER_KEYSET_FLAG |
947 CRYPT_FIND_MACHINE_KEYSET_FLAG)))
949 keyProvInfo.dwFlags &= ~CRYPT_USER_KEYSET;
950 keyProvInfo.dwFlags |= CRYPT_MACHINE_KEYSET;
951 found = find_key_prov_info_in_provider(pCert,
964 CertSetCertificateContextProperty(pCert, CERT_KEY_PROV_INFO_PROP_ID,
966 CryptMemFree(keyProvInfo.pwszProvName);
967 CryptMemFree(keyProvInfo.pwszContainerName);
971 static BOOL cert_prov_info_matches_cert(PCCERT_CONTEXT pCert)
973 BOOL matches = FALSE;
976 if (CertGetCertificateContextProperty(pCert, CERT_KEY_PROV_INFO_PROP_ID,
979 CRYPT_KEY_PROV_INFO *keyProvInfo = CryptMemAlloc(size);
983 if (CertGetCertificateContextProperty(pCert,
984 CERT_KEY_PROV_INFO_PROP_ID, keyProvInfo, &size))
985 matches = key_prov_info_matches_cert(pCert, keyProvInfo);
986 CryptMemFree(keyProvInfo);
992 BOOL WINAPI CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert,
993 DWORD dwFlags, void *pvReserved)
995 BOOL matches = FALSE;
997 TRACE("(%p, %08x, %p)\n", pCert, dwFlags, pvReserved);
999 matches = cert_prov_info_matches_cert(pCert);
1001 matches = find_matching_provider(pCert, dwFlags);
1005 BOOL WINAPI CertCompareCertificate(DWORD dwCertEncodingType,
1006 PCERT_INFO pCertId1, PCERT_INFO pCertId2)
1010 TRACE("(%08x, %p, %p)\n", dwCertEncodingType, pCertId1, pCertId2);
1012 ret = CertCompareCertificateName(dwCertEncodingType, &pCertId1->Issuer,
1013 &pCertId2->Issuer) && CertCompareIntegerBlob(&pCertId1->SerialNumber,
1014 &pCertId2->SerialNumber);
1015 TRACE("returning %d\n", ret);
1019 BOOL WINAPI CertCompareCertificateName(DWORD dwCertEncodingType,
1020 PCERT_NAME_BLOB pCertName1, PCERT_NAME_BLOB pCertName2)
1024 TRACE("(%08x, %p, %p)\n", dwCertEncodingType, pCertName1, pCertName2);
1026 if (pCertName1->cbData == pCertName2->cbData)
1028 if (pCertName1->cbData)
1029 ret = !memcmp(pCertName1->pbData, pCertName2->pbData,
1030 pCertName1->cbData);
1036 TRACE("returning %d\n", ret);
1040 /* Returns the number of significant bytes in pInt, where a byte is
1041 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1042 * for negative numbers. pInt is assumed to be little-endian.
1044 static DWORD CRYPT_significantBytes(const CRYPT_INTEGER_BLOB *pInt)
1046 DWORD ret = pInt->cbData;
1050 if (pInt->pbData[ret - 2] <= 0x7f && pInt->pbData[ret - 1] == 0)
1052 else if (pInt->pbData[ret - 2] >= 0x80 && pInt->pbData[ret - 1] == 0xff)
1060 BOOL WINAPI CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1,
1061 PCRYPT_INTEGER_BLOB pInt2)
1066 TRACE("(%p, %p)\n", pInt1, pInt2);
1068 cb1 = CRYPT_significantBytes(pInt1);
1069 cb2 = CRYPT_significantBytes(pInt2);
1073 ret = !memcmp(pInt1->pbData, pInt2->pbData, cb1);
1079 TRACE("returning %d\n", ret);
1083 BOOL WINAPI CertComparePublicKeyInfo(DWORD dwCertEncodingType,
1084 PCERT_PUBLIC_KEY_INFO pPublicKey1, PCERT_PUBLIC_KEY_INFO pPublicKey2)
1088 TRACE("(%08x, %p, %p)\n", dwCertEncodingType, pPublicKey1, pPublicKey2);
1090 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType))
1092 case 0: /* Seems to mean "raw binary bits" */
1093 if (pPublicKey1->PublicKey.cbData == pPublicKey2->PublicKey.cbData &&
1094 pPublicKey1->PublicKey.cUnusedBits == pPublicKey2->PublicKey.cUnusedBits)
1096 if (pPublicKey2->PublicKey.cbData)
1097 ret = !memcmp(pPublicKey1->PublicKey.pbData,
1098 pPublicKey2->PublicKey.pbData, pPublicKey1->PublicKey.cbData);
1106 WARN("Unknown encoding type %08x\n", dwCertEncodingType);
1108 case X509_ASN_ENCODING:
1110 BLOBHEADER *pblob1, *pblob2;
1113 if (CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
1114 pPublicKey1->PublicKey.pbData, pPublicKey1->PublicKey.cbData,
1117 pblob1 = CryptMemAlloc(length);
1118 if (CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
1119 pPublicKey1->PublicKey.pbData, pPublicKey1->PublicKey.cbData,
1120 0, pblob1, &length))
1122 if (CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
1123 pPublicKey2->PublicKey.pbData, pPublicKey2->PublicKey.cbData,
1126 pblob2 = CryptMemAlloc(length);
1127 if (CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
1128 pPublicKey2->PublicKey.pbData, pPublicKey2->PublicKey.cbData,
1129 0, pblob2, &length))
1131 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1132 RSAPUBKEY *pk1 = (LPVOID)(pblob1 + 1),
1133 *pk2 = (LPVOID)(pblob2 + 1);
1134 ret = (pk1->bitlen == pk2->bitlen) && (pk1->pubexp == pk2->pubexp)
1135 && !memcmp(pk1 + 1, pk2 + 1, pk1->bitlen/8);
1137 CryptMemFree(pblob2);
1140 CryptMemFree(pblob1);
1149 DWORD WINAPI CertGetPublicKeyLength(DWORD dwCertEncodingType,
1150 PCERT_PUBLIC_KEY_INFO pPublicKey)
1154 TRACE("(%08x, %p)\n", dwCertEncodingType, pPublicKey);
1156 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType) != X509_ASN_ENCODING)
1158 SetLastError(ERROR_FILE_NOT_FOUND);
1161 if (pPublicKey->Algorithm.pszObjId &&
1162 !strcmp(pPublicKey->Algorithm.pszObjId, szOID_RSA_DH))
1164 FIXME("unimplemented for DH public keys\n");
1165 SetLastError(CRYPT_E_ASN1_BADTAG);
1171 BOOL ret = CryptDecodeObjectEx(dwCertEncodingType,
1172 RSA_CSP_PUBLICKEYBLOB, pPublicKey->PublicKey.pbData,
1173 pPublicKey->PublicKey.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &buf,
1178 RSAPUBKEY *rsaPubKey = (RSAPUBKEY *)(buf + sizeof(BLOBHEADER));
1180 len = rsaPubKey->bitlen;
1187 typedef BOOL (*CertCompareFunc)(PCCERT_CONTEXT pCertContext, DWORD dwType,
1188 DWORD dwFlags, const void *pvPara);
1190 static BOOL compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
1191 DWORD dwFlags, const void *pvPara)
1195 DWORD size = sizeof(hash);
1197 ret = CertGetCertificateContextProperty(pCertContext,
1198 CERT_MD5_HASH_PROP_ID, hash, &size);
1201 const CRYPT_HASH_BLOB *pHash = pvPara;
1203 if (size == pHash->cbData)
1204 ret = !memcmp(pHash->pbData, hash, size);
1211 static BOOL compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
1212 DWORD dwFlags, const void *pvPara)
1216 DWORD size = sizeof(hash);
1218 ret = CertGetCertificateContextProperty(pCertContext,
1219 CERT_SHA1_HASH_PROP_ID, hash, &size);
1222 const CRYPT_HASH_BLOB *pHash = pvPara;
1224 if (size == pHash->cbData)
1225 ret = !memcmp(pHash->pbData, hash, size);
1232 static BOOL compare_cert_by_name(PCCERT_CONTEXT pCertContext, DWORD dwType,
1233 DWORD dwFlags, const void *pvPara)
1235 CERT_NAME_BLOB *blob = (CERT_NAME_BLOB *)pvPara, *toCompare;
1238 if (dwType & CERT_INFO_SUBJECT_FLAG)
1239 toCompare = &pCertContext->pCertInfo->Subject;
1241 toCompare = &pCertContext->pCertInfo->Issuer;
1242 ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
1247 static BOOL compare_cert_by_public_key(PCCERT_CONTEXT pCertContext,
1248 DWORD dwType, DWORD dwFlags, const void *pvPara)
1250 CERT_PUBLIC_KEY_INFO *publicKey = (CERT_PUBLIC_KEY_INFO *)pvPara;
1253 ret = CertComparePublicKeyInfo(pCertContext->dwCertEncodingType,
1254 &pCertContext->pCertInfo->SubjectPublicKeyInfo, publicKey);
1258 static BOOL compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext,
1259 DWORD dwType, DWORD dwFlags, const void *pvPara)
1261 CERT_INFO *pCertInfo = (CERT_INFO *)pvPara;
1264 /* Matching serial number and subject match.. */
1265 ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
1266 &pCertContext->pCertInfo->Subject, &pCertInfo->Issuer);
1268 ret = CertCompareIntegerBlob(&pCertContext->pCertInfo->SerialNumber,
1269 &pCertInfo->SerialNumber);
1272 /* failing that, if the serial number and issuer match, we match */
1273 ret = CertCompareIntegerBlob(&pCertContext->pCertInfo->SerialNumber,
1274 &pCertInfo->SerialNumber);
1276 ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
1277 &pCertContext->pCertInfo->Issuer, &pCertInfo->Issuer);
1279 TRACE("returning %d\n", ret);
1283 static BOOL compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext, DWORD dwType,
1284 DWORD dwFlags, const void *pvPara)
1286 CERT_ID *id = (CERT_ID *)pvPara;
1289 switch (id->dwIdChoice)
1291 case CERT_ID_ISSUER_SERIAL_NUMBER:
1292 ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
1293 &pCertContext->pCertInfo->Issuer, &id->u.IssuerSerialNumber.Issuer);
1295 ret = CertCompareIntegerBlob(&pCertContext->pCertInfo->SerialNumber,
1296 &id->u.IssuerSerialNumber.SerialNumber);
1298 case CERT_ID_SHA1_HASH:
1299 ret = compare_cert_by_sha1_hash(pCertContext, dwType, dwFlags,
1302 case CERT_ID_KEY_IDENTIFIER:
1306 ret = CertGetCertificateContextProperty(pCertContext,
1307 CERT_KEY_IDENTIFIER_PROP_ID, NULL, &size);
1308 if (ret && size == id->u.KeyId.cbData)
1310 LPBYTE buf = CryptMemAlloc(size);
1314 CertGetCertificateContextProperty(pCertContext,
1315 CERT_KEY_IDENTIFIER_PROP_ID, buf, &size);
1316 ret = !memcmp(buf, id->u.KeyId.pbData, size);
1331 static BOOL compare_existing_cert(PCCERT_CONTEXT pCertContext, DWORD dwType,
1332 DWORD dwFlags, const void *pvPara)
1334 PCCERT_CONTEXT toCompare = pvPara;
1335 return CertCompareCertificate(pCertContext->dwCertEncodingType,
1336 pCertContext->pCertInfo, toCompare->pCertInfo);
1339 static BOOL compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
1340 DWORD dwFlags, const void *pvPara)
1342 const CRYPT_HASH_BLOB *hash = pvPara;
1346 ret = CertGetCertificateContextProperty(pCertContext,
1347 CERT_SIGNATURE_HASH_PROP_ID, NULL, &size);
1348 if (ret && size == hash->cbData)
1350 LPBYTE buf = CryptMemAlloc(size);
1354 CertGetCertificateContextProperty(pCertContext,
1355 CERT_SIGNATURE_HASH_PROP_ID, buf, &size);
1356 ret = !memcmp(buf, hash->pbData, size);
1365 static inline PCCERT_CONTEXT cert_compare_certs_in_store(HCERTSTORE store,
1366 PCCERT_CONTEXT prev, CertCompareFunc compare, DWORD dwType, DWORD dwFlags,
1369 BOOL matches = FALSE;
1374 ret = CertEnumCertificatesInStore(store, ret);
1376 matches = compare(ret, dwType, dwFlags, pvPara);
1377 } while (ret != NULL && !matches);
1381 typedef PCCERT_CONTEXT (*CertFindFunc)(HCERTSTORE store, DWORD dwType,
1382 DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev);
1384 static PCCERT_CONTEXT find_cert_any(HCERTSTORE store, DWORD dwType,
1385 DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
1387 return CertEnumCertificatesInStore(store, prev);
1390 static PCCERT_CONTEXT find_cert_by_issuer(HCERTSTORE store, DWORD dwType,
1391 DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
1394 PCCERT_CONTEXT found = NULL, subject = pvPara;
1395 PCERT_EXTENSION ext;
1398 if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER,
1399 subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
1401 CERT_AUTHORITY_KEY_ID_INFO *info;
1403 ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
1404 X509_AUTHORITY_KEY_ID, ext->Value.pbData, ext->Value.cbData,
1405 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
1411 if (info->CertIssuer.cbData && info->CertSerialNumber.cbData)
1413 id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
1414 memcpy(&id.u.IssuerSerialNumber.Issuer, &info->CertIssuer,
1415 sizeof(CERT_NAME_BLOB));
1416 memcpy(&id.u.IssuerSerialNumber.SerialNumber,
1417 &info->CertSerialNumber, sizeof(CRYPT_INTEGER_BLOB));
1419 else if (info->KeyId.cbData)
1421 id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
1422 memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
1427 found = cert_compare_certs_in_store(store, prev,
1428 compare_cert_by_cert_id, dwType, dwFlags, &id);
1432 else if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2,
1433 subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
1435 CERT_AUTHORITY_KEY_ID2_INFO *info;
1437 ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
1438 X509_AUTHORITY_KEY_ID2, ext->Value.pbData, ext->Value.cbData,
1439 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
1445 if (info->AuthorityCertIssuer.cAltEntry &&
1446 info->AuthorityCertSerialNumber.cbData)
1448 PCERT_ALT_NAME_ENTRY directoryName = NULL;
1451 for (i = 0; !directoryName &&
1452 i < info->AuthorityCertIssuer.cAltEntry; i++)
1453 if (info->AuthorityCertIssuer.rgAltEntry[i].dwAltNameChoice
1454 == CERT_ALT_NAME_DIRECTORY_NAME)
1456 &info->AuthorityCertIssuer.rgAltEntry[i];
1459 id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
1460 memcpy(&id.u.IssuerSerialNumber.Issuer,
1461 &directoryName->u.DirectoryName, sizeof(CERT_NAME_BLOB));
1462 memcpy(&id.u.IssuerSerialNumber.SerialNumber,
1463 &info->AuthorityCertSerialNumber,
1464 sizeof(CRYPT_INTEGER_BLOB));
1468 FIXME("no supported name type in authority key id2\n");
1472 else if (info->KeyId.cbData)
1474 id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
1475 memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
1480 found = cert_compare_certs_in_store(store, prev,
1481 compare_cert_by_cert_id, dwType, dwFlags, &id);
1486 found = cert_compare_certs_in_store(store, prev,
1487 compare_cert_by_name, CERT_COMPARE_NAME | CERT_COMPARE_SUBJECT_CERT,
1488 dwFlags, &subject->pCertInfo->Issuer);
1492 static BOOL compare_cert_by_name_str(PCCERT_CONTEXT pCertContext,
1493 DWORD dwType, DWORD dwFlags, const void *pvPara)
1495 PCERT_NAME_BLOB name;
1499 if (dwType & CERT_INFO_SUBJECT_FLAG)
1500 name = &pCertContext->pCertInfo->Subject;
1502 name = &pCertContext->pCertInfo->Issuer;
1503 len = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1504 CERT_SIMPLE_NAME_STR, NULL, 0);
1507 LPWSTR str = CryptMemAlloc(len * sizeof(WCHAR));
1513 CertNameToStrW(pCertContext->dwCertEncodingType, name,
1514 CERT_SIMPLE_NAME_STR, str, len);
1515 for (ptr = str; *ptr; ptr++)
1516 *ptr = tolowerW(*ptr);
1517 if (strstrW(str, pvPara))
1525 static PCCERT_CONTEXT find_cert_by_name_str_a(HCERTSTORE store, DWORD dwType,
1526 DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
1528 PCCERT_CONTEXT found = NULL;
1530 TRACE("%s\n", debugstr_a(pvPara));
1534 int len = MultiByteToWideChar(CP_ACP, 0, pvPara, -1, NULL, 0);
1535 LPWSTR str = CryptMemAlloc(len * sizeof(WCHAR));
1541 MultiByteToWideChar(CP_ACP, 0, pvPara, -1, str, len);
1542 for (ptr = str; *ptr; ptr++)
1543 *ptr = tolowerW(*ptr);
1544 found = cert_compare_certs_in_store(store, prev,
1545 compare_cert_by_name_str, dwType, dwFlags, str);
1550 found = find_cert_any(store, dwType, dwFlags, NULL, prev);
1554 static PCCERT_CONTEXT find_cert_by_name_str_w(HCERTSTORE store, DWORD dwType,
1555 DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
1557 PCCERT_CONTEXT found = NULL;
1559 TRACE("%s\n", debugstr_w(pvPara));
1563 DWORD len = strlenW(pvPara);
1564 LPWSTR str = CryptMemAlloc((len + 1) * sizeof(WCHAR));
1571 for (src = pvPara, dst = str; *src; src++, dst++)
1572 *dst = tolowerW(*src);
1574 found = cert_compare_certs_in_store(store, prev,
1575 compare_cert_by_name_str, dwType, dwFlags, str);
1580 found = find_cert_any(store, dwType, dwFlags, NULL, prev);
1584 PCCERT_CONTEXT WINAPI CertFindCertificateInStore(HCERTSTORE hCertStore,
1585 DWORD dwCertEncodingType, DWORD dwFlags, DWORD dwType, const void *pvPara,
1586 PCCERT_CONTEXT pPrevCertContext)
1589 CertFindFunc find = NULL;
1590 CertCompareFunc compare = NULL;
1592 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore, dwCertEncodingType,
1593 dwFlags, dwType, pvPara, pPrevCertContext);
1595 switch (dwType >> CERT_COMPARE_SHIFT)
1597 case CERT_COMPARE_ANY:
1598 find = find_cert_any;
1600 case CERT_COMPARE_MD5_HASH:
1601 compare = compare_cert_by_md5_hash;
1603 case CERT_COMPARE_SHA1_HASH:
1604 compare = compare_cert_by_sha1_hash;
1606 case CERT_COMPARE_NAME:
1607 compare = compare_cert_by_name;
1609 case CERT_COMPARE_PUBLIC_KEY:
1610 compare = compare_cert_by_public_key;
1612 case CERT_COMPARE_NAME_STR_A:
1613 find = find_cert_by_name_str_a;
1615 case CERT_COMPARE_NAME_STR_W:
1616 find = find_cert_by_name_str_w;
1618 case CERT_COMPARE_SUBJECT_CERT:
1619 compare = compare_cert_by_subject_cert;
1621 case CERT_COMPARE_CERT_ID:
1622 compare = compare_cert_by_cert_id;
1624 case CERT_COMPARE_ISSUER_OF:
1625 find = find_cert_by_issuer;
1627 case CERT_COMPARE_EXISTING:
1628 compare = compare_existing_cert;
1630 case CERT_COMPARE_SIGNATURE_HASH:
1631 compare = compare_cert_by_signature_hash;
1634 FIXME("find type %08x unimplemented\n", dwType);
1638 ret = find(hCertStore, dwFlags, dwType, pvPara, pPrevCertContext);
1640 ret = cert_compare_certs_in_store(hCertStore, pPrevCertContext,
1641 compare, dwType, dwFlags, pvPara);
1645 SetLastError(CRYPT_E_NOT_FOUND);
1646 TRACE("returning %p\n", ret);
1650 PCCERT_CONTEXT WINAPI CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore,
1651 DWORD dwCertEncodingType, PCERT_INFO pCertId)
1653 TRACE("(%p, %08x, %p)\n", hCertStore, dwCertEncodingType, pCertId);
1657 SetLastError(E_INVALIDARG);
1660 return CertFindCertificateInStore(hCertStore, dwCertEncodingType, 0,
1661 CERT_FIND_SUBJECT_CERT, pCertId, NULL);
1664 BOOL WINAPI CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject,
1665 PCCERT_CONTEXT pIssuer, DWORD *pdwFlags)
1667 static const DWORD supportedFlags = CERT_STORE_REVOCATION_FLAG |
1668 CERT_STORE_SIGNATURE_FLAG | CERT_STORE_TIME_VALIDITY_FLAG;
1670 if (*pdwFlags & ~supportedFlags)
1672 SetLastError(E_INVALIDARG);
1675 if (*pdwFlags & CERT_STORE_REVOCATION_FLAG)
1678 PCCRL_CONTEXT crl = CertGetCRLFromStore(pSubject->hCertStore, pSubject,
1681 /* FIXME: what if the CRL has expired? */
1684 if (CertVerifyCRLRevocation(pSubject->dwCertEncodingType,
1685 pSubject->pCertInfo, 1, (PCRL_INFO *)&crl->pCrlInfo))
1686 *pdwFlags &= CERT_STORE_REVOCATION_FLAG;
1689 *pdwFlags |= CERT_STORE_NO_CRL_FLAG;
1691 if (*pdwFlags & CERT_STORE_TIME_VALIDITY_FLAG)
1693 if (0 == CertVerifyTimeValidity(NULL, pSubject->pCertInfo))
1694 *pdwFlags &= ~CERT_STORE_TIME_VALIDITY_FLAG;
1696 if (*pdwFlags & CERT_STORE_SIGNATURE_FLAG)
1698 if (CryptVerifyCertificateSignatureEx(0, pSubject->dwCertEncodingType,
1699 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT, (void *)pSubject,
1700 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT, (void *)pIssuer, 0, NULL))
1701 *pdwFlags &= ~CERT_STORE_SIGNATURE_FLAG;
1706 PCCERT_CONTEXT WINAPI CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore,
1707 PCCERT_CONTEXT pSubjectContext, PCCERT_CONTEXT pPrevIssuerContext,
1712 TRACE("(%p, %p, %p, %08x)\n", hCertStore, pSubjectContext,
1713 pPrevIssuerContext, *pdwFlags);
1715 if (!pSubjectContext)
1717 SetLastError(E_INVALIDARG);
1721 ret = CertFindCertificateInStore(hCertStore,
1722 pSubjectContext->dwCertEncodingType, 0, CERT_FIND_ISSUER_OF,
1723 pSubjectContext, pPrevIssuerContext);
1726 if (!CertVerifySubjectCertificateContext(pSubjectContext, ret,
1729 CertFreeCertificateContext(ret);
1733 TRACE("returning %p\n", ret);
1737 typedef struct _OLD_CERT_REVOCATION_STATUS {
1742 } OLD_CERT_REVOCATION_STATUS, *POLD_CERT_REVOCATION_STATUS;
1744 typedef BOOL (WINAPI *CertVerifyRevocationFunc)(DWORD, DWORD, DWORD,
1745 void **, DWORD, PCERT_REVOCATION_PARA, PCERT_REVOCATION_STATUS);
1747 BOOL WINAPI CertVerifyRevocation(DWORD dwEncodingType, DWORD dwRevType,
1748 DWORD cContext, PVOID rgpvContext[], DWORD dwFlags,
1749 PCERT_REVOCATION_PARA pRevPara, PCERT_REVOCATION_STATUS pRevStatus)
1753 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType, dwRevType,
1754 cContext, rgpvContext, dwFlags, pRevPara, pRevStatus);
1756 if (pRevStatus->cbSize != sizeof(OLD_CERT_REVOCATION_STATUS) &&
1757 pRevStatus->cbSize != sizeof(CERT_REVOCATION_STATUS))
1759 SetLastError(E_INVALIDARG);
1764 static HCRYPTOIDFUNCSET set = NULL;
1768 set = CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC, 0);
1769 ret = CryptGetDefaultOIDDllList(set, dwEncodingType, NULL, &size);
1775 SetLastError(CRYPT_E_NO_REVOCATION_DLL);
1780 LPWSTR dllList = CryptMemAlloc(size * sizeof(WCHAR)), ptr;
1784 ret = CryptGetDefaultOIDDllList(set, dwEncodingType,
1788 for (ptr = dllList; ret && *ptr;
1789 ptr += lstrlenW(ptr) + 1)
1791 CertVerifyRevocationFunc func;
1792 HCRYPTOIDFUNCADDR hFunc;
1794 ret = CryptGetDefaultOIDFunctionAddress(set,
1795 dwEncodingType, ptr, 0, (void **)&func, &hFunc);
1798 ret = func(dwEncodingType, dwRevType, cContext,
1799 rgpvContext, dwFlags, pRevPara, pRevStatus);
1800 CryptFreeOIDFunctionAddress(hFunc, 0);
1804 CryptMemFree(dllList);
1808 SetLastError(ERROR_OUTOFMEMORY);
1819 PCRYPT_ATTRIBUTE WINAPI CertFindAttribute(LPCSTR pszObjId, DWORD cAttr,
1820 CRYPT_ATTRIBUTE rgAttr[])
1822 PCRYPT_ATTRIBUTE ret = NULL;
1825 TRACE("%s %d %p\n", debugstr_a(pszObjId), cAttr, rgAttr);
1831 SetLastError(ERROR_INVALID_PARAMETER);
1835 for (i = 0; !ret && i < cAttr; i++)
1836 if (rgAttr[i].pszObjId && !strcmp(pszObjId, rgAttr[i].pszObjId))
1841 PCERT_EXTENSION WINAPI CertFindExtension(LPCSTR pszObjId, DWORD cExtensions,
1842 CERT_EXTENSION rgExtensions[])
1844 PCERT_EXTENSION ret = NULL;
1847 TRACE("%s %d %p\n", debugstr_a(pszObjId), cExtensions, rgExtensions);
1853 SetLastError(ERROR_INVALID_PARAMETER);
1857 for (i = 0; !ret && i < cExtensions; i++)
1858 if (rgExtensions[i].pszObjId && !strcmp(pszObjId,
1859 rgExtensions[i].pszObjId))
1860 ret = &rgExtensions[i];
1864 PCERT_RDN_ATTR WINAPI CertFindRDNAttr(LPCSTR pszObjId, PCERT_NAME_INFO pName)
1866 PCERT_RDN_ATTR ret = NULL;
1869 TRACE("%s %p\n", debugstr_a(pszObjId), pName);
1873 SetLastError(ERROR_INVALID_PARAMETER);
1877 for (i = 0; !ret && i < pName->cRDN; i++)
1878 for (j = 0; !ret && j < pName->rgRDN[i].cRDNAttr; j++)
1879 if (pName->rgRDN[i].rgRDNAttr[j].pszObjId && !strcmp(pszObjId,
1880 pName->rgRDN[i].rgRDNAttr[j].pszObjId))
1881 ret = &pName->rgRDN[i].rgRDNAttr[j];
1885 static BOOL find_matching_rdn_attr(DWORD dwFlags, const CERT_NAME_INFO *name,
1886 const CERT_RDN_ATTR *attr)
1891 for (i = 0; !match && i < name->cRDN; i++)
1893 for (j = 0; j < name->rgRDN[i].cRDNAttr; j++)
1895 if (!strcmp(name->rgRDN[i].rgRDNAttr[j].pszObjId,
1897 name->rgRDN[i].rgRDNAttr[j].dwValueType ==
1900 if (dwFlags & CERT_UNICODE_IS_RDN_ATTRS_FLAG)
1903 (LPCWSTR)name->rgRDN[i].rgRDNAttr[j].Value.pbData;
1904 LPCWSTR attrStr = (LPCWSTR)attr->Value.pbData;
1906 if (attr->Value.cbData !=
1907 name->rgRDN[i].rgRDNAttr[j].Value.cbData)
1909 else if (dwFlags & CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG)
1910 match = !strncmpiW(nameStr, attrStr,
1911 attr->Value.cbData / sizeof(WCHAR));
1913 match = !strncmpW(nameStr, attrStr,
1914 attr->Value.cbData / sizeof(WCHAR));
1915 TRACE("%s : %s => %d\n",
1916 debugstr_wn(nameStr, attr->Value.cbData / sizeof(WCHAR)),
1917 debugstr_wn(attrStr, attr->Value.cbData / sizeof(WCHAR)),
1923 (LPCSTR)name->rgRDN[i].rgRDNAttr[j].Value.pbData;
1924 LPCSTR attrStr = (LPCSTR)attr->Value.pbData;
1926 if (attr->Value.cbData !=
1927 name->rgRDN[i].rgRDNAttr[j].Value.cbData)
1929 else if (dwFlags & CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG)
1930 match = !strncasecmp(nameStr, attrStr,
1931 attr->Value.cbData);
1933 match = !strncmp(nameStr, attrStr, attr->Value.cbData);
1934 TRACE("%s : %s => %d\n",
1935 debugstr_an(nameStr, attr->Value.cbData),
1936 debugstr_an(attrStr, attr->Value.cbData), match);
1944 BOOL WINAPI CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType,
1945 DWORD dwFlags, PCERT_NAME_BLOB pCertName, PCERT_RDN pRDN)
1947 CERT_NAME_INFO *name;
1952 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType, dwFlags, pCertName,
1955 type = dwFlags & CERT_UNICODE_IS_RDN_ATTRS_FLAG ? X509_UNICODE_NAME :
1957 if ((ret = CryptDecodeObjectEx(dwCertEncodingType, type, pCertName->pbData,
1958 pCertName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &name, &size)))
1962 for (i = 0; ret && i < pRDN->cRDNAttr; i++)
1963 ret = find_matching_rdn_attr(dwFlags, name, &pRDN->rgRDNAttr[i]);
1965 SetLastError(CRYPT_E_NO_MATCH);
1971 LONG WINAPI CertVerifyTimeValidity(LPFILETIME pTimeToVerify,
1972 PCERT_INFO pCertInfo)
1979 GetSystemTimeAsFileTime(&fileTime);
1980 pTimeToVerify = &fileTime;
1982 if ((ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotBefore)) >= 0)
1984 ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotAfter);
1991 BOOL WINAPI CertVerifyValidityNesting(PCERT_INFO pSubjectInfo,
1992 PCERT_INFO pIssuerInfo)
1994 TRACE("(%p, %p)\n", pSubjectInfo, pIssuerInfo);
1996 return CertVerifyTimeValidity(&pSubjectInfo->NotBefore, pIssuerInfo) == 0
1997 && CertVerifyTimeValidity(&pSubjectInfo->NotAfter, pIssuerInfo) == 0;
2000 BOOL WINAPI CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv, ALG_ID Algid,
2001 DWORD dwFlags, const BYTE *pbEncoded, DWORD cbEncoded, BYTE *pbComputedHash,
2002 DWORD *pcbComputedHash)
2005 HCRYPTHASH hHash = 0;
2007 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv, Algid, dwFlags,
2008 pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash);
2011 hCryptProv = CRYPT_GetDefaultProvider();
2016 ret = CryptCreateHash(hCryptProv, Algid, 0, 0, &hHash);
2019 ret = CryptHashData(hHash, pbEncoded, cbEncoded, 0);
2021 ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
2022 pcbComputedHash, 0);
2023 CryptDestroyHash(hHash);
2029 BOOL WINAPI CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv, ALG_ID Algid,
2030 DWORD dwFlags, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo,
2031 BYTE *pbComputedHash, DWORD *pcbComputedHash)
2034 HCRYPTHASH hHash = 0;
2036 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv, Algid, dwFlags,
2037 dwCertEncodingType, pInfo, pbComputedHash, pcbComputedHash);
2040 hCryptProv = CRYPT_GetDefaultProvider();
2043 if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING)
2045 SetLastError(ERROR_FILE_NOT_FOUND);
2053 ret = CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType,
2054 X509_PUBLIC_KEY_INFO, pInfo, CRYPT_ENCODE_ALLOC_FLAG, NULL,
2055 (LPBYTE)&buf, &size);
2058 ret = CryptCreateHash(hCryptProv, Algid, 0, 0, &hHash);
2061 ret = CryptHashData(hHash, buf, size, 0);
2063 ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
2064 pcbComputedHash, 0);
2065 CryptDestroyHash(hHash);
2073 BOOL WINAPI CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv,
2074 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
2075 BYTE *pbComputedHash, DWORD *pcbComputedHash)
2078 CERT_SIGNED_CONTENT_INFO *info;
2081 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv, dwCertEncodingType,
2082 pbEncoded, cbEncoded, pbComputedHash, *pcbComputedHash);
2084 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT,
2085 pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &size);
2088 PCCRYPT_OID_INFO oidInfo;
2092 hCryptProv = CRYPT_GetDefaultProvider();
2093 oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
2094 info->SignatureAlgorithm.pszObjId, 0);
2097 SetLastError(NTE_BAD_ALGID);
2102 ret = CryptCreateHash(hCryptProv, oidInfo->u.Algid, 0, 0, &hHash);
2105 ret = CryptHashData(hHash, info->ToBeSigned.pbData,
2106 info->ToBeSigned.cbData, 0);
2108 ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
2109 pcbComputedHash, 0);
2110 CryptDestroyHash(hHash);
2118 BOOL WINAPI CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
2119 DWORD dwKeySpec, DWORD dwCertEncodingType, const BYTE *pbEncodedToBeSigned,
2120 DWORD cbEncodedToBeSigned, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
2121 const void *pvHashAuxInfo, BYTE *pbSignature, DWORD *pcbSignature)
2124 PCCRYPT_OID_INFO info;
2127 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv,
2128 dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned,
2129 pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature);
2131 info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
2132 pSignatureAlgorithm->pszObjId, 0);
2135 SetLastError(NTE_BAD_ALGID);
2138 if (info->dwGroupId == CRYPT_HASH_ALG_OID_GROUP_ID)
2141 hCryptProv = CRYPT_GetDefaultProvider();
2142 ret = CryptCreateHash(hCryptProv, info->u.Algid, 0, 0, &hHash);
2145 ret = CryptHashData(hHash, pbEncodedToBeSigned,
2146 cbEncodedToBeSigned, 0);
2148 ret = CryptGetHashParam(hHash, HP_HASHVAL, pbSignature,
2150 CryptDestroyHash(hHash);
2157 SetLastError(ERROR_INVALID_PARAMETER);
2162 ret = CryptCreateHash(hCryptProv, info->u.Algid, 0, 0, &hHash);
2165 ret = CryptHashData(hHash, pbEncodedToBeSigned,
2166 cbEncodedToBeSigned, 0);
2168 ret = CryptSignHashW(hHash, dwKeySpec, NULL, 0, pbSignature,
2170 CryptDestroyHash(hHash);
2177 BOOL WINAPI CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
2178 DWORD dwKeySpec, DWORD dwCertEncodingType, LPCSTR lpszStructType,
2179 const void *pvStructInfo, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
2180 const void *pvHashAuxInfo, BYTE *pbEncoded, DWORD *pcbEncoded)
2183 DWORD encodedSize, hashSize;
2185 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv, dwKeySpec,
2186 dwCertEncodingType, debugstr_a(lpszStructType), pvStructInfo,
2187 pSignatureAlgorithm, pvHashAuxInfo, pbEncoded, pcbEncoded);
2189 ret = CryptEncodeObject(dwCertEncodingType, lpszStructType, pvStructInfo,
2190 NULL, &encodedSize);
2193 PBYTE encoded = CryptMemAlloc(encodedSize);
2197 ret = CryptEncodeObject(dwCertEncodingType, lpszStructType,
2198 pvStructInfo, encoded, &encodedSize);
2201 ret = CryptSignCertificate(hCryptProv, dwKeySpec,
2202 dwCertEncodingType, encoded, encodedSize, pSignatureAlgorithm,
2203 pvHashAuxInfo, NULL, &hashSize);
2206 PBYTE hash = CryptMemAlloc(hashSize);
2210 ret = CryptSignCertificate(hCryptProv, dwKeySpec,
2211 dwCertEncodingType, encoded, encodedSize,
2212 pSignatureAlgorithm, pvHashAuxInfo, hash, &hashSize);
2215 CERT_SIGNED_CONTENT_INFO info = { { 0 } };
2217 info.ToBeSigned.cbData = encodedSize;
2218 info.ToBeSigned.pbData = encoded;
2219 memcpy(&info.SignatureAlgorithm,
2220 pSignatureAlgorithm,
2221 sizeof(info.SignatureAlgorithm));
2222 info.Signature.cbData = hashSize;
2223 info.Signature.pbData = hash;
2224 info.Signature.cUnusedBits = 0;
2225 ret = CryptEncodeObject(dwCertEncodingType,
2226 X509_CERT, &info, pbEncoded, pcbEncoded);
2232 CryptMemFree(encoded);
2238 BOOL WINAPI CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv,
2239 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
2240 PCERT_PUBLIC_KEY_INFO pPublicKey)
2242 return CryptVerifyCertificateSignatureEx(hCryptProv, dwCertEncodingType,
2243 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, (void *)pbEncoded,
2244 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY, pPublicKey, 0, NULL);
2247 static BOOL CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv,
2248 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pubKeyInfo,
2249 const CERT_SIGNED_CONTENT_INFO *signedCert)
2253 PCCRYPT_OID_INFO info;
2254 ALG_ID pubKeyID, hashID;
2256 info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
2257 signedCert->SignatureAlgorithm.pszObjId, 0);
2258 if (!info || info->dwGroupId != CRYPT_SIGN_ALG_OID_GROUP_ID)
2260 SetLastError(NTE_BAD_ALGID);
2263 hashID = info->u.Algid;
2264 if (info->ExtraInfo.cbData >= sizeof(ALG_ID))
2265 pubKeyID = *(ALG_ID *)info->ExtraInfo.pbData;
2268 /* Load the default provider if necessary */
2270 hCryptProv = CRYPT_GetDefaultProvider();
2271 ret = CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType,
2272 pubKeyInfo, pubKeyID, 0, NULL, &key);
2277 ret = CryptCreateHash(hCryptProv, hashID, 0, 0, &hash);
2280 ret = CryptHashData(hash, signedCert->ToBeSigned.pbData,
2281 signedCert->ToBeSigned.cbData, 0);
2283 ret = CryptVerifySignatureW(hash, signedCert->Signature.pbData,
2284 signedCert->Signature.cbData, key, NULL, 0);
2285 CryptDestroyHash(hash);
2287 CryptDestroyKey(key);
2292 BOOL WINAPI CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv,
2293 DWORD dwCertEncodingType, DWORD dwSubjectType, void *pvSubject,
2294 DWORD dwIssuerType, void *pvIssuer, DWORD dwFlags, void *pvReserved)
2297 CRYPT_DATA_BLOB subjectBlob;
2299 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv,
2300 dwCertEncodingType, dwSubjectType, pvSubject, dwIssuerType, pvIssuer,
2301 dwFlags, pvReserved);
2303 switch (dwSubjectType)
2305 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB:
2307 PCRYPT_DATA_BLOB blob = pvSubject;
2309 subjectBlob.pbData = blob->pbData;
2310 subjectBlob.cbData = blob->cbData;
2313 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT:
2315 PCERT_CONTEXT context = pvSubject;
2317 subjectBlob.pbData = context->pbCertEncoded;
2318 subjectBlob.cbData = context->cbCertEncoded;
2321 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL:
2323 PCRL_CONTEXT context = pvSubject;
2325 subjectBlob.pbData = context->pbCrlEncoded;
2326 subjectBlob.cbData = context->cbCrlEncoded;
2330 SetLastError(E_INVALIDARG);
2336 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
2339 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT,
2340 subjectBlob.pbData, subjectBlob.cbData,
2341 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
2342 &signedCert, &size);
2345 switch (dwIssuerType)
2347 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY:
2348 ret = CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv,
2349 dwCertEncodingType, pvIssuer,
2352 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT:
2353 ret = CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv,
2355 &((PCCERT_CONTEXT)pvIssuer)->pCertInfo->SubjectPublicKeyInfo,
2358 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN:
2359 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2362 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL:
2365 SetLastError(E_INVALIDARG);
2370 FIXME("unimplemented for NULL signer\n");
2371 SetLastError(E_INVALIDARG);
2376 SetLastError(E_INVALIDARG);
2379 LocalFree(signedCert);
2385 BOOL WINAPI CertGetIntendedKeyUsage(DWORD dwCertEncodingType,
2386 PCERT_INFO pCertInfo, BYTE *pbKeyUsage, DWORD cbKeyUsage)
2388 PCERT_EXTENSION ext;
2391 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType, pCertInfo, pbKeyUsage,
2394 ext = CertFindExtension(szOID_KEY_USAGE, pCertInfo->cExtension,
2395 pCertInfo->rgExtension);
2398 CRYPT_BIT_BLOB usage;
2399 DWORD size = sizeof(usage);
2401 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_BITS,
2402 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_NOCOPY_FLAG, NULL,
2406 if (cbKeyUsage < usage.cbData)
2410 memcpy(pbKeyUsage, usage.pbData, usage.cbData);
2411 if (cbKeyUsage > usage.cbData)
2412 memset(pbKeyUsage + usage.cbData, 0,
2413 cbKeyUsage - usage.cbData);
2422 BOOL WINAPI CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext, DWORD dwFlags,
2423 PCERT_ENHKEY_USAGE pUsage, DWORD *pcbUsage)
2425 PCERT_ENHKEY_USAGE usage = NULL;
2429 if (!pCertContext || !pcbUsage)
2431 SetLastError(ERROR_INVALID_PARAMETER);
2435 TRACE("(%p, %08x, %p, %d)\n", pCertContext, dwFlags, pUsage, *pcbUsage);
2437 if (!(dwFlags & CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG))
2441 if (CertGetCertificateContextProperty(pCertContext,
2442 CERT_ENHKEY_USAGE_PROP_ID, NULL, &propSize))
2444 LPBYTE buf = CryptMemAlloc(propSize);
2448 if (CertGetCertificateContextProperty(pCertContext,
2449 CERT_ENHKEY_USAGE_PROP_ID, buf, &propSize))
2451 ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
2452 X509_ENHANCED_KEY_USAGE, buf, propSize,
2453 CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
2459 if (!usage && !(dwFlags & CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG))
2461 PCERT_EXTENSION ext = CertFindExtension(szOID_ENHANCED_KEY_USAGE,
2462 pCertContext->pCertInfo->cExtension,
2463 pCertContext->pCertInfo->rgExtension);
2467 ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
2468 X509_ENHANCED_KEY_USAGE, ext->Value.pbData, ext->Value.cbData,
2469 CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
2474 /* If a particular location is specified, this should fail. Otherwise
2475 * it should succeed with an empty usage. (This is true on Win2k and
2476 * later, which we emulate.)
2480 SetLastError(CRYPT_E_NOT_FOUND);
2484 bytesNeeded = sizeof(CERT_ENHKEY_USAGE);
2490 *pcbUsage = bytesNeeded;
2491 else if (*pcbUsage < bytesNeeded)
2493 SetLastError(ERROR_MORE_DATA);
2494 *pcbUsage = bytesNeeded;
2499 *pcbUsage = bytesNeeded;
2503 LPSTR nextOID = (LPSTR)((LPBYTE)pUsage +
2504 sizeof(CERT_ENHKEY_USAGE) +
2505 usage->cUsageIdentifier * sizeof(LPSTR));
2507 pUsage->cUsageIdentifier = usage->cUsageIdentifier;
2508 pUsage->rgpszUsageIdentifier = (LPSTR *)((LPBYTE)pUsage +
2509 sizeof(CERT_ENHKEY_USAGE));
2510 for (i = 0; i < usage->cUsageIdentifier; i++)
2512 pUsage->rgpszUsageIdentifier[i] = nextOID;
2513 strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
2514 nextOID += strlen(nextOID) + 1;
2518 pUsage->cUsageIdentifier = 0;
2523 TRACE("returning %d\n", ret);
2527 BOOL WINAPI CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext,
2528 PCERT_ENHKEY_USAGE pUsage)
2532 TRACE("(%p, %p)\n", pCertContext, pUsage);
2536 CRYPT_DATA_BLOB blob = { 0, NULL };
2538 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_ENHANCED_KEY_USAGE,
2539 pUsage, CRYPT_ENCODE_ALLOC_FLAG, NULL, &blob.pbData, &blob.cbData);
2542 ret = CertSetCertificateContextProperty(pCertContext,
2543 CERT_ENHKEY_USAGE_PROP_ID, 0, &blob);
2544 LocalFree(blob.pbData);
2548 ret = CertSetCertificateContextProperty(pCertContext,
2549 CERT_ENHKEY_USAGE_PROP_ID, 0, NULL);
2553 BOOL WINAPI CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
2554 LPCSTR pszUsageIdentifier)
2559 TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
2561 if (CertGetEnhancedKeyUsage(pCertContext,
2562 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, NULL, &size))
2564 PCERT_ENHKEY_USAGE usage = CryptMemAlloc(size);
2568 ret = CertGetEnhancedKeyUsage(pCertContext,
2569 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size);
2573 BOOL exists = FALSE;
2575 /* Make sure usage doesn't already exist */
2576 for (i = 0; !exists && i < usage->cUsageIdentifier; i++)
2578 if (!strcmp(usage->rgpszUsageIdentifier[i],
2579 pszUsageIdentifier))
2584 PCERT_ENHKEY_USAGE newUsage = CryptMemAlloc(size +
2585 sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
2591 newUsage->rgpszUsageIdentifier = (LPSTR *)
2592 ((LPBYTE)newUsage + sizeof(CERT_ENHKEY_USAGE));
2593 nextOID = (LPSTR)((LPBYTE)newUsage->rgpszUsageIdentifier
2594 + (usage->cUsageIdentifier + 1) * sizeof(LPSTR));
2595 for (i = 0; i < usage->cUsageIdentifier; i++)
2597 newUsage->rgpszUsageIdentifier[i] = nextOID;
2598 strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
2599 nextOID += strlen(nextOID) + 1;
2601 newUsage->rgpszUsageIdentifier[i] = nextOID;
2602 strcpy(nextOID, pszUsageIdentifier);
2603 newUsage->cUsageIdentifier = i + 1;
2604 ret = CertSetEnhancedKeyUsage(pCertContext, newUsage);
2605 CryptMemFree(newUsage);
2611 CryptMemFree(usage);
2618 PCERT_ENHKEY_USAGE usage = CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE) +
2619 sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
2623 usage->rgpszUsageIdentifier =
2624 (LPSTR *)((LPBYTE)usage + sizeof(CERT_ENHKEY_USAGE));
2625 usage->rgpszUsageIdentifier[0] = (LPSTR)((LPBYTE)usage +
2626 sizeof(CERT_ENHKEY_USAGE) + sizeof(LPSTR));
2627 strcpy(usage->rgpszUsageIdentifier[0], pszUsageIdentifier);
2628 usage->cUsageIdentifier = 1;
2629 ret = CertSetEnhancedKeyUsage(pCertContext, usage);
2630 CryptMemFree(usage);
2638 BOOL WINAPI CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
2639 LPCSTR pszUsageIdentifier)
2643 CERT_ENHKEY_USAGE usage;
2645 TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
2647 size = sizeof(usage);
2648 ret = CertGetEnhancedKeyUsage(pCertContext,
2649 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, &usage, &size);
2650 if (!ret && GetLastError() == ERROR_MORE_DATA)
2652 PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
2656 ret = CertGetEnhancedKeyUsage(pCertContext,
2657 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, pUsage, &size);
2660 if (pUsage->cUsageIdentifier)
2665 for (i = 0; i < pUsage->cUsageIdentifier; i++)
2667 if (!strcmp(pUsage->rgpszUsageIdentifier[i],
2668 pszUsageIdentifier))
2670 if (found && i < pUsage->cUsageIdentifier - 1)
2671 pUsage->rgpszUsageIdentifier[i] =
2672 pUsage->rgpszUsageIdentifier[i + 1];
2674 pUsage->cUsageIdentifier--;
2675 /* Remove the usage if it's empty */
2676 if (pUsage->cUsageIdentifier)
2677 ret = CertSetEnhancedKeyUsage(pCertContext, pUsage);
2679 ret = CertSetEnhancedKeyUsage(pCertContext, NULL);
2682 CryptMemFree(pUsage);
2689 /* it fit in an empty usage, therefore there's nothing to remove */
2701 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
2703 static void CRYPT_SetBitInField(struct BitField *field, DWORD bit)
2705 DWORD indexIndex = bit / BITS_PER_DWORD;
2707 if (indexIndex + 1 > field->cIndexes)
2709 if (field->cIndexes)
2710 field->indexes = CryptMemRealloc(field->indexes,
2711 (indexIndex + 1) * sizeof(DWORD));
2713 field->indexes = CryptMemAlloc(sizeof(DWORD));
2716 field->indexes[indexIndex] = 0;
2717 field->cIndexes = indexIndex + 1;
2721 field->indexes[indexIndex] |= 1 << (bit % BITS_PER_DWORD);
2724 static BOOL CRYPT_IsBitInFieldSet(const struct BitField *field, DWORD bit)
2727 DWORD indexIndex = bit / BITS_PER_DWORD;
2729 assert(field->cIndexes);
2730 set = field->indexes[indexIndex] & (1 << (bit % BITS_PER_DWORD));
2734 BOOL WINAPI CertGetValidUsages(DWORD cCerts, PCCERT_CONTEXT *rghCerts,
2735 int *cNumOIDs, LPSTR *rghOIDs, DWORD *pcbOIDs)
2738 DWORD i, cbOIDs = 0;
2739 BOOL allUsagesValid = TRUE;
2740 CERT_ENHKEY_USAGE validUsages = { 0, NULL };
2742 TRACE("(%d, %p, %d, %p, %d)\n", cCerts, rghCerts, *cNumOIDs,
2745 for (i = 0; i < cCerts; i++)
2747 CERT_ENHKEY_USAGE usage;
2748 DWORD size = sizeof(usage);
2750 ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, &usage, &size);
2751 /* Success is deliberately ignored: it implies all usages are valid */
2752 if (!ret && GetLastError() == ERROR_MORE_DATA)
2754 PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
2756 allUsagesValid = FALSE;
2759 ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, pUsage, &size);
2762 if (!validUsages.cUsageIdentifier)
2766 cbOIDs = pUsage->cUsageIdentifier * sizeof(LPSTR);
2767 validUsages.cUsageIdentifier = pUsage->cUsageIdentifier;
2768 for (j = 0; j < validUsages.cUsageIdentifier; j++)
2769 cbOIDs += lstrlenA(pUsage->rgpszUsageIdentifier[j])
2771 validUsages.rgpszUsageIdentifier =
2772 CryptMemAlloc(cbOIDs);
2773 if (validUsages.rgpszUsageIdentifier)
2775 LPSTR nextOID = (LPSTR)
2776 ((LPBYTE)validUsages.rgpszUsageIdentifier +
2777 validUsages.cUsageIdentifier * sizeof(LPSTR));
2779 for (j = 0; j < validUsages.cUsageIdentifier; j++)
2781 validUsages.rgpszUsageIdentifier[j] = nextOID;
2782 lstrcpyA(validUsages.rgpszUsageIdentifier[j],
2783 pUsage->rgpszUsageIdentifier[j]);
2784 nextOID += lstrlenA(nextOID) + 1;
2790 struct BitField validIndexes = { 0, NULL };
2791 DWORD j, k, numRemoved = 0;
2793 /* Merge: build a bitmap of all the indexes of
2794 * validUsages.rgpszUsageIdentifier that are in pUsage.
2796 for (j = 0; j < pUsage->cUsageIdentifier; j++)
2798 for (k = 0; k < validUsages.cUsageIdentifier; k++)
2800 if (!strcmp(pUsage->rgpszUsageIdentifier[j],
2801 validUsages.rgpszUsageIdentifier[k]))
2803 CRYPT_SetBitInField(&validIndexes, k);
2808 /* Merge by removing from validUsages those that are
2809 * not in the bitmap.
2811 for (j = 0; j < validUsages.cUsageIdentifier; j++)
2813 if (!CRYPT_IsBitInFieldSet(&validIndexes, j))
2815 if (j < validUsages.cUsageIdentifier - 1)
2817 memmove(&validUsages.rgpszUsageIdentifier[j],
2818 &validUsages.rgpszUsageIdentifier[j +
2820 (validUsages.cUsageIdentifier - numRemoved
2821 - j - 1) * sizeof(LPSTR));
2823 validUsages.rgpszUsageIdentifier[j]) + 1 +
2825 validUsages.cUsageIdentifier--;
2829 validUsages.cUsageIdentifier--;
2832 CryptMemFree(validIndexes.indexes);
2835 CryptMemFree(pUsage);
2847 *cNumOIDs = validUsages.cUsageIdentifier;
2850 else if (*pcbOIDs < cbOIDs)
2853 SetLastError(ERROR_MORE_DATA);
2858 LPSTR nextOID = (LPSTR)((LPBYTE)rghOIDs +
2859 validUsages.cUsageIdentifier * sizeof(LPSTR));
2862 for (i = 0; i < validUsages.cUsageIdentifier; i++)
2864 rghOIDs[i] = nextOID;
2865 lstrcpyA(nextOID, validUsages.rgpszUsageIdentifier[i]);
2866 nextOID += lstrlenA(nextOID) + 1;
2870 CryptMemFree(validUsages.rgpszUsageIdentifier);
2871 TRACE("cNumOIDs: %d\n", *cNumOIDs);
2872 TRACE("returning %d\n", ret);
2876 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
2877 * pInfo is NULL, from the attributes of hProv.
2879 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context,
2880 const CRYPT_KEY_PROV_INFO *pInfo, HCRYPTPROV hProv)
2882 CRYPT_KEY_PROV_INFO info = { 0 };
2890 ret = CryptGetProvParam(hProv, PP_CONTAINER, NULL, &size, 0);
2893 LPSTR szContainer = CryptMemAlloc(size);
2897 ret = CryptGetProvParam(hProv, PP_CONTAINER,
2898 (BYTE *)szContainer, &size, 0);
2901 len = MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
2905 info.pwszContainerName = CryptMemAlloc(len *
2907 MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
2908 info.pwszContainerName, len);
2911 CryptMemFree(szContainer);
2914 ret = CryptGetProvParam(hProv, PP_NAME, NULL, &size, 0);
2917 LPSTR szProvider = CryptMemAlloc(size);
2921 ret = CryptGetProvParam(hProv, PP_NAME, (BYTE *)szProvider,
2925 len = MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
2929 info.pwszProvName = CryptMemAlloc(len *
2931 MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
2932 info.pwszProvName, len);
2935 CryptMemFree(szProvider);
2938 size = sizeof(info.dwKeySpec);
2939 /* in case no CRYPT_KEY_PROV_INFO given,
2940 * we always use AT_SIGNATURE key spec
2942 info.dwKeySpec = AT_SIGNATURE;
2943 size = sizeof(info.dwProvType);
2944 ret = CryptGetProvParam(hProv, PP_PROVTYPE, (LPBYTE)&info.dwProvType,
2947 info.dwProvType = PROV_RSA_FULL;
2951 CertSetCertificateContextProperty(context, CERT_KEY_PROV_INFO_PROP_ID,
2956 CryptMemFree(info.pwszContainerName);
2957 CryptMemFree(info.pwszProvName);
2961 /* Creates a signed certificate context from the unsigned, encoded certificate
2962 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
2964 static PCCERT_CONTEXT CRYPT_CreateSignedCert(const CRYPT_DER_BLOB *blob,
2965 HCRYPTPROV hProv, DWORD dwKeySpec, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo)
2967 PCCERT_CONTEXT context = NULL;
2971 ret = CryptSignCertificate(hProv, dwKeySpec, X509_ASN_ENCODING,
2972 blob->pbData, blob->cbData, sigAlgo, NULL, NULL, &sigSize);
2975 LPBYTE sig = CryptMemAlloc(sigSize);
2977 ret = CryptSignCertificate(hProv, dwKeySpec, X509_ASN_ENCODING,
2978 blob->pbData, blob->cbData, sigAlgo, NULL, sig, &sigSize);
2981 CERT_SIGNED_CONTENT_INFO signedInfo;
2982 BYTE *encodedSignedCert = NULL;
2983 DWORD encodedSignedCertSize = 0;
2985 signedInfo.ToBeSigned.cbData = blob->cbData;
2986 signedInfo.ToBeSigned.pbData = blob->pbData;
2987 memcpy(&signedInfo.SignatureAlgorithm, sigAlgo,
2988 sizeof(signedInfo.SignatureAlgorithm));
2989 signedInfo.Signature.cbData = sigSize;
2990 signedInfo.Signature.pbData = sig;
2991 signedInfo.Signature.cUnusedBits = 0;
2992 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT,
2993 &signedInfo, CRYPT_ENCODE_ALLOC_FLAG, NULL,
2994 &encodedSignedCert, &encodedSignedCertSize);
2997 context = CertCreateCertificateContext(X509_ASN_ENCODING,
2998 encodedSignedCert, encodedSignedCertSize);
2999 LocalFree(encodedSignedCert);
3007 /* Copies data from the parameters into info, where:
3008 * pSerialNumber: The serial number. Must not be NULL.
3009 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3011 * pSignatureAlgorithm: Optional.
3012 * pStartTime: The starting time of the certificate. If NULL, the current
3013 * system time is used.
3014 * pEndTime: The ending time of the certificate. If NULL, one year past the
3015 * starting time is used.
3016 * pubKey: The public key of the certificate. Must not be NULL.
3017 * pExtensions: Extensions to be included with the certificate. Optional.
3019 static void CRYPT_MakeCertInfo(PCERT_INFO info, const CRYPT_DATA_BLOB *pSerialNumber,
3020 const CERT_NAME_BLOB *pSubjectIssuerBlob,
3021 const CRYPT_ALGORITHM_IDENTIFIER *pSignatureAlgorithm, const SYSTEMTIME *pStartTime,
3022 const SYSTEMTIME *pEndTime, const CERT_PUBLIC_KEY_INFO *pubKey,
3023 const CERT_EXTENSIONS *pExtensions)
3025 static CHAR oid[] = szOID_RSA_SHA1RSA;
3028 assert(pSerialNumber);
3029 assert(pSubjectIssuerBlob);
3032 if (pExtensions && pExtensions->cExtension)
3033 info->dwVersion = CERT_V3;
3035 info->dwVersion = CERT_V1;
3036 info->SerialNumber.cbData = pSerialNumber->cbData;
3037 info->SerialNumber.pbData = pSerialNumber->pbData;
3038 if (pSignatureAlgorithm)
3039 memcpy(&info->SignatureAlgorithm, pSignatureAlgorithm,
3040 sizeof(info->SignatureAlgorithm));
3043 info->SignatureAlgorithm.pszObjId = oid;
3044 info->SignatureAlgorithm.Parameters.cbData = 0;
3045 info->SignatureAlgorithm.Parameters.pbData = NULL;
3047 info->Issuer.cbData = pSubjectIssuerBlob->cbData;
3048 info->Issuer.pbData = pSubjectIssuerBlob->pbData;
3050 SystemTimeToFileTime(pStartTime, &info->NotBefore);
3052 GetSystemTimeAsFileTime(&info->NotBefore);
3054 SystemTimeToFileTime(pEndTime, &info->NotAfter);
3059 if (FileTimeToSystemTime(&info->NotBefore, &endTime))
3062 SystemTimeToFileTime(&endTime, &info->NotAfter);
3065 info->Subject.cbData = pSubjectIssuerBlob->cbData;
3066 info->Subject.pbData = pSubjectIssuerBlob->pbData;
3067 memcpy(&info->SubjectPublicKeyInfo, pubKey,
3068 sizeof(info->SubjectPublicKeyInfo));
3071 info->cExtension = pExtensions->cExtension;
3072 info->rgExtension = pExtensions->rgExtension;
3076 info->cExtension = 0;
3077 info->rgExtension = NULL;
3081 typedef RPC_STATUS (RPC_ENTRY *UuidCreateFunc)(UUID *);
3082 typedef RPC_STATUS (RPC_ENTRY *UuidToStringFunc)(UUID *, unsigned char **);
3083 typedef RPC_STATUS (RPC_ENTRY *RpcStringFreeFunc)(unsigned char **);
3085 static HCRYPTPROV CRYPT_CreateKeyProv(void)
3087 HCRYPTPROV hProv = 0;
3088 HMODULE rpcrt = LoadLibraryA("rpcrt4");
3092 UuidCreateFunc uuidCreate = (UuidCreateFunc)GetProcAddress(rpcrt,
3094 UuidToStringFunc uuidToString = (UuidToStringFunc)GetProcAddress(rpcrt,
3096 RpcStringFreeFunc rpcStringFree = (RpcStringFreeFunc)GetProcAddress(
3097 rpcrt, "RpcStringFreeA");
3099 if (uuidCreate && uuidToString && rpcStringFree)
3102 RPC_STATUS status = uuidCreate(&uuid);
3104 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
3106 unsigned char *uuidStr;
3108 status = uuidToString(&uuid, &uuidStr);
3109 if (status == RPC_S_OK)
3111 BOOL ret = CryptAcquireContextA(&hProv, (LPCSTR)uuidStr,
3112 MS_DEF_PROV_A, PROV_RSA_FULL, CRYPT_NEWKEYSET);
3118 ret = CryptGenKey(hProv, AT_SIGNATURE, 0, &key);
3120 CryptDestroyKey(key);
3122 rpcStringFree(&uuidStr);
3131 PCCERT_CONTEXT WINAPI CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv,
3132 PCERT_NAME_BLOB pSubjectIssuerBlob, DWORD dwFlags,
3133 PCRYPT_KEY_PROV_INFO pKeyProvInfo,
3134 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm, PSYSTEMTIME pStartTime,
3135 PSYSTEMTIME pEndTime, PCERT_EXTENSIONS pExtensions)
3137 PCCERT_CONTEXT context = NULL;
3138 BOOL ret, releaseContext = FALSE;
3139 PCERT_PUBLIC_KEY_INFO pubKey = NULL;
3140 DWORD pubKeySize = 0,dwKeySpec = AT_SIGNATURE;
3142 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv,
3143 pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime,
3144 pExtensions, pExtensions);
3146 if(!pSubjectIssuerBlob)
3148 SetLastError(ERROR_INVALID_PARAMETER);
3156 hProv = CRYPT_CreateKeyProv();
3157 releaseContext = TRUE;
3159 else if (pKeyProvInfo->dwFlags & CERT_SET_KEY_PROV_HANDLE_PROP_ID)
3161 SetLastError(NTE_BAD_FLAGS);
3167 /* acquire the context using the given information*/
3168 ret = CryptAcquireContextW(&hProv,pKeyProvInfo->pwszContainerName,
3169 pKeyProvInfo->pwszProvName,pKeyProvInfo->dwProvType,
3170 pKeyProvInfo->dwFlags);
3173 if(GetLastError() != NTE_BAD_KEYSET)
3175 /* create the key set */
3176 ret = CryptAcquireContextW(&hProv,pKeyProvInfo->pwszContainerName,
3177 pKeyProvInfo->pwszProvName,pKeyProvInfo->dwProvType,
3178 pKeyProvInfo->dwFlags|CRYPT_NEWKEYSET);
3182 dwKeySpec = pKeyProvInfo->dwKeySpec;
3183 /* check if the key is here */
3184 ret = CryptGetUserKey(hProv,dwKeySpec,&hKey);
3187 if (NTE_NO_KEY == GetLastError())
3188 { /* generate the key */
3189 ret = CryptGenKey(hProv,dwKeySpec,0,&hKey);
3193 CryptReleaseContext(hProv,0);
3194 SetLastError(NTE_BAD_KEYSET);
3198 CryptDestroyKey(hKey);
3199 releaseContext = TRUE;
3202 else if (pKeyProvInfo)
3204 SetLastError(ERROR_INVALID_PARAMETER);
3208 CryptExportPublicKeyInfo(hProv, dwKeySpec, X509_ASN_ENCODING, NULL,
3210 pubKey = CryptMemAlloc(pubKeySize);
3213 ret = CryptExportPublicKeyInfo(hProv, dwKeySpec, X509_ASN_ENCODING,
3214 pubKey, &pubKeySize);
3217 CERT_INFO info = { 0 };
3218 CRYPT_DER_BLOB blob = { 0, NULL };
3220 CRYPT_DATA_BLOB serialBlob = { sizeof(serial), serial };
3222 CryptGenRandom(hProv, sizeof(serial), serial);
3223 CRYPT_MakeCertInfo(&info, &serialBlob, pSubjectIssuerBlob,
3224 pSignatureAlgorithm, pStartTime, pEndTime, pubKey, pExtensions);
3225 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
3226 &info, CRYPT_ENCODE_ALLOC_FLAG, NULL, &blob.pbData,
3230 if (!(dwFlags & CERT_CREATE_SELFSIGN_NO_SIGN))
3231 context = CRYPT_CreateSignedCert(&blob, hProv,dwKeySpec,
3232 &info.SignatureAlgorithm);
3234 context = CertCreateCertificateContext(X509_ASN_ENCODING,
3235 blob.pbData, blob.cbData);
3236 if (context && !(dwFlags & CERT_CREATE_SELFSIGN_NO_KEY_INFO))
3237 CertContext_SetKeyProvInfo(context, pKeyProvInfo, hProv);
3238 LocalFree(blob.pbData);
3241 CryptMemFree(pubKey);
3244 CryptReleaseContext(hProv, 0);
3248 BOOL WINAPI CertVerifyCTLUsage(DWORD dwEncodingType, DWORD dwSubjectType,
3249 void *pvSubject, PCTL_USAGE pSubjectUsage, DWORD dwFlags,
3250 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara,
3251 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus)
3253 FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType,
3254 dwSubjectType, pvSubject, pSubjectUsage, dwFlags, pVerifyUsagePara,
3255 pVerifyUsageStatus);
3256 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3260 const void * WINAPI CertCreateContext(DWORD dwContextType, DWORD dwEncodingType,
3261 const BYTE *pbEncoded, DWORD cbEncoded,
3262 DWORD dwFlags, PCERT_CREATE_CONTEXT_PARA pCreatePara)
3264 TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType, dwEncodingType,
3265 pbEncoded, cbEncoded, dwFlags, pCreatePara);
3269 FIXME("dwFlags 0x%08x not handled\n", dwFlags);
3274 FIXME("pCreatePara not handled\n");
3278 switch (dwContextType)
3280 case CERT_STORE_CERTIFICATE_CONTEXT:
3281 return CertCreateCertificateContext(dwEncodingType, pbEncoded, cbEncoded);
3282 case CERT_STORE_CRL_CONTEXT:
3283 return CertCreateCRLContext(dwEncodingType, pbEncoded, cbEncoded);
3284 case CERT_STORE_CTL_CONTEXT:
3285 return CertCreateCTLContext(dwEncodingType, pbEncoded, cbEncoded);
3287 WARN("unknown context type: 0x%x\n", dwContextType);