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 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
145 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
151 cert
= Context_CreateDataContext(sizeof(CERT_CONTEXT
));
154 data
= CryptMemAlloc(cbCertEncoded
);
161 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
162 cert
->dwCertEncodingType
= dwCertEncodingType
;
163 cert
->pbCertEncoded
= data
;
164 cert
->cbCertEncoded
= cbCertEncoded
;
165 cert
->pCertInfo
= certInfo
;
166 cert
->hCertStore
= 0;
173 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(
174 PCCERT_CONTEXT pCertContext
)
176 TRACE("(%p)\n", pCertContext
);
181 Context_AddRef((void *)pCertContext
, sizeof(CERT_CONTEXT
));
185 static void CertDataContext_Free(void *context
)
187 PCERT_CONTEXT certContext
= context
;
189 CryptMemFree(certContext
->pbCertEncoded
);
190 LocalFree(certContext
->pCertInfo
);
193 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
197 TRACE("(%p)\n", pCertContext
);
200 ret
= Context_Release((void *)pCertContext
, sizeof(CERT_CONTEXT
),
201 CertDataContext_Free
);
205 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
208 PCONTEXT_PROPERTY_LIST properties
= Context_GetProperties(
209 pCertContext
, sizeof(CERT_CONTEXT
));
212 TRACE("(%p, %d)\n", pCertContext
, dwPropId
);
215 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
221 static BOOL
CertContext_GetHashProp(void *context
, DWORD dwPropId
,
222 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
225 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
229 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
231 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
236 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
243 else if (*pcbData
< cb
)
245 SetLastError(ERROR_MORE_DATA
);
251 memcpy(pvData
, pb
, cb
);
257 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
258 void *pvData
, DWORD
*pcbData
)
260 PCCERT_CONTEXT pCertContext
= context
;
261 PCONTEXT_PROPERTY_LIST properties
=
262 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
264 CRYPT_DATA_BLOB blob
;
266 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
269 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
273 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
276 /* Implicit properties */
279 case CERT_SHA1_HASH_PROP_ID
:
280 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
281 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
284 case CERT_MD5_HASH_PROP_ID
:
285 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
286 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
289 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
290 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
291 pCertContext
->pCertInfo
->Subject
.pbData
,
292 pCertContext
->pCertInfo
->Subject
.cbData
,
295 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
296 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
297 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
298 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
301 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
302 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
303 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
304 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
307 case CERT_SIGNATURE_HASH_PROP_ID
:
308 ret
= CryptHashToBeSigned(0, pCertContext
->dwCertEncodingType
,
309 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
313 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
315 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
318 case CERT_KEY_IDENTIFIER_PROP_ID
:
320 PCERT_EXTENSION ext
= CertFindExtension(
321 szOID_SUBJECT_KEY_IDENTIFIER
, pCertContext
->pCertInfo
->cExtension
,
322 pCertContext
->pCertInfo
->rgExtension
);
326 CRYPT_DATA_BLOB value
;
327 DWORD size
= sizeof(value
);
329 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
330 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
331 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
335 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
337 CertContext_SetProperty(context
, dwPropId
, 0, &value
);
341 SetLastError(ERROR_INVALID_DATA
);
345 SetLastError(CRYPT_E_NOT_FOUND
);
348 TRACE("returning %d\n", ret
);
352 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
354 DWORD i
, containerLen
, provNameLen
;
355 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
357 info
->pwszContainerName
= (LPWSTR
)data
;
358 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
359 data
+= containerLen
;
361 info
->pwszProvName
= (LPWSTR
)data
;
362 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
365 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
366 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
368 for (i
= 0; i
< info
->cProvParam
; i
++)
370 info
->rgProvParam
[i
].pbData
= data
;
371 data
+= info
->rgProvParam
[i
].cbData
;
375 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
376 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
380 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
385 case CERT_CERT_PROP_ID
:
386 case CERT_CRL_PROP_ID
:
387 case CERT_CTL_PROP_ID
:
388 SetLastError(E_INVALIDARG
);
391 case CERT_ACCESS_STATE_PROP_ID
:
392 if (pCertContext
->hCertStore
)
393 ret
= CertGetStoreProperty(pCertContext
->hCertStore
, dwPropId
,
399 ret
= CertContext_CopyParam(pvData
, pcbData
, &state
, sizeof(state
));
402 case CERT_KEY_PROV_HANDLE_PROP_ID
:
404 CERT_KEY_CONTEXT keyContext
;
405 DWORD size
= sizeof(keyContext
);
407 ret
= CertContext_GetProperty((void *)pCertContext
,
408 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
410 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
411 sizeof(keyContext
.hCryptProv
));
414 case CERT_KEY_PROV_INFO_PROP_ID
:
415 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
418 CRYPT_FixKeyProvInfoPointers(pvData
);
421 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
425 TRACE("returning %d\n", ret
);
429 /* Copies key provider info from from into to, where to is assumed to be a
430 * contiguous buffer of memory large enough for from and all its associated
431 * data, but whose pointers are uninitialized.
432 * Upon return, to contains a contiguous copy of from, packed in the following
434 * - CRYPT_KEY_PROV_INFO
435 * - pwszContainerName
437 * - rgProvParam[0]...
439 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
440 const CRYPT_KEY_PROV_INFO
*from
)
443 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
445 if (from
->pwszContainerName
)
447 to
->pwszContainerName
= (LPWSTR
)nextData
;
448 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
449 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
452 to
->pwszContainerName
= NULL
;
453 if (from
->pwszProvName
)
455 to
->pwszProvName
= (LPWSTR
)nextData
;
456 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
457 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
460 to
->pwszProvName
= NULL
;
461 to
->dwProvType
= from
->dwProvType
;
462 to
->dwFlags
= from
->dwFlags
;
463 to
->cProvParam
= from
->cProvParam
;
464 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
465 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
466 to
->dwKeySpec
= from
->dwKeySpec
;
467 for (i
= 0; i
< to
->cProvParam
; i
++)
469 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
470 sizeof(CRYPT_KEY_PROV_PARAM
));
471 to
->rgProvParam
[i
].pbData
= nextData
;
472 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
473 from
->rgProvParam
[i
].cbData
);
474 nextData
+= from
->rgProvParam
[i
].cbData
;
478 static BOOL
CertContext_SetKeyProvInfoProperty(PCONTEXT_PROPERTY_LIST properties
,
479 const CRYPT_KEY_PROV_INFO
*info
)
483 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
485 if (info
->pwszContainerName
)
486 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
489 if (info
->pwszProvName
)
490 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
493 size
+= containerSize
+ provNameSize
;
494 for (i
= 0; i
< info
->cProvParam
; i
++)
495 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
496 buf
= CryptMemAlloc(size
);
499 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
500 ret
= ContextPropertyList_SetProperty(properties
,
501 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
509 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
510 DWORD dwFlags
, const void *pvData
)
512 PCONTEXT_PROPERTY_LIST properties
=
513 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
516 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
524 case CERT_AUTO_ENROLL_PROP_ID
:
525 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
526 case CERT_DESCRIPTION_PROP_ID
:
527 case CERT_FRIENDLY_NAME_PROP_ID
:
528 case CERT_HASH_PROP_ID
:
529 case CERT_KEY_IDENTIFIER_PROP_ID
:
530 case CERT_MD5_HASH_PROP_ID
:
531 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
532 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
533 case CERT_PVK_FILE_PROP_ID
:
534 case CERT_SIGNATURE_HASH_PROP_ID
:
535 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
536 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
537 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
538 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
539 case CERT_ENROLLMENT_PROP_ID
:
540 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
541 case CERT_RENEWAL_PROP_ID
:
545 const CRYPT_DATA_BLOB
*blob
= pvData
;
547 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
548 blob
->pbData
, blob
->cbData
);
552 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
557 case CERT_DATE_STAMP_PROP_ID
:
559 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
560 pvData
, sizeof(FILETIME
));
563 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
567 case CERT_KEY_CONTEXT_PROP_ID
:
571 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
573 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
575 SetLastError(E_INVALIDARG
);
579 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
580 (const BYTE
*)keyContext
, keyContext
->cbSize
);
584 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
589 case CERT_KEY_PROV_INFO_PROP_ID
:
591 ret
= CertContext_SetKeyProvInfoProperty(properties
, pvData
);
594 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
598 case CERT_KEY_PROV_HANDLE_PROP_ID
:
600 CERT_KEY_CONTEXT keyContext
;
601 DWORD size
= sizeof(keyContext
);
603 ret
= CertContext_GetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
607 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
608 CryptReleaseContext(keyContext
.hCryptProv
, 0);
610 keyContext
.cbSize
= sizeof(keyContext
);
612 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
615 keyContext
.hCryptProv
= 0;
616 keyContext
.dwKeySpec
= AT_SIGNATURE
;
618 ret
= CertContext_SetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
623 FIXME("%d: stub\n", dwPropId
);
627 TRACE("returning %d\n", ret
);
631 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
632 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
636 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
638 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
639 * crashes on most of these, I'll be safer.
644 case CERT_ACCESS_STATE_PROP_ID
:
645 case CERT_CERT_PROP_ID
:
646 case CERT_CRL_PROP_ID
:
647 case CERT_CTL_PROP_ID
:
648 SetLastError(E_INVALIDARG
);
651 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
653 TRACE("returning %d\n", ret
);
657 /* Acquires the private key using the key provider info, retrieving info from
658 * the certificate if info is NULL. The acquired provider is returned in
659 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
661 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
662 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
665 BOOL allocated
= FALSE
, ret
= TRUE
;
669 ret
= CertGetCertificateContextProperty(pCert
,
670 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
673 info
= HeapAlloc(GetProcessHeap(), 0, size
);
676 ret
= CertGetCertificateContextProperty(pCert
,
677 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
682 SetLastError(ERROR_OUTOFMEMORY
);
687 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
691 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
692 info
->pwszProvName
, info
->dwProvType
, 0);
697 for (i
= 0; i
< info
->cProvParam
; i
++)
699 CryptSetProvParam(*phCryptProv
,
700 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
701 info
->rgProvParam
[i
].dwFlags
);
703 *pdwKeySpec
= info
->dwKeySpec
;
706 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
709 HeapFree(GetProcessHeap(), 0, info
);
713 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
714 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
715 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
717 BOOL ret
= FALSE
, cache
= FALSE
;
718 PCRYPT_KEY_PROV_INFO info
= NULL
;
719 CERT_KEY_CONTEXT keyContext
;
722 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
723 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
725 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
729 ret
= CertGetCertificateContextProperty(pCert
,
730 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
733 info
= HeapAlloc(GetProcessHeap(), 0, size
);
734 ret
= CertGetCertificateContextProperty(pCert
,
735 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
737 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
740 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
745 size
= sizeof(keyContext
);
746 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
750 *phCryptProv
= keyContext
.hCryptProv
;
752 *pdwKeySpec
= keyContext
.dwKeySpec
;
753 if (pfCallerFreeProv
)
754 *pfCallerFreeProv
= !cache
;
759 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
760 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
763 *phCryptProv
= keyContext
.hCryptProv
;
765 *pdwKeySpec
= keyContext
.dwKeySpec
;
768 keyContext
.cbSize
= sizeof(keyContext
);
769 if (CertSetCertificateContextProperty(pCert
,
770 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
772 if (pfCallerFreeProv
)
773 *pfCallerFreeProv
= FALSE
;
778 if (pfCallerFreeProv
)
779 *pfCallerFreeProv
= TRUE
;
783 HeapFree(GetProcessHeap(), 0, info
);
787 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
788 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
791 BOOL matches
= FALSE
;
793 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
794 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
798 /* Need to sign something to verify the sig. What to sign? Why not
799 * the certificate itself?
801 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
802 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
803 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
805 BYTE
*certEncoded
= CryptMemAlloc(size
);
809 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
810 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
811 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
812 NULL
, certEncoded
, &size
))
814 if (size
== pCert
->cbCertEncoded
&&
815 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
818 CryptMemFree(certEncoded
);
821 CryptReleaseContext(csp
, 0);
826 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
827 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
829 CRYPT_KEY_PROV_INFO copy
;
830 WCHAR containerW
[MAX_PATH
];
831 BOOL matches
= FALSE
;
833 MultiByteToWideChar(CP_ACP
, 0, container
, -1,
834 containerW
, sizeof(containerW
) / sizeof(containerW
[0]));
835 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
836 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
839 memcpy(©
, keyProvInfo
, sizeof(copy
));
840 copy
.pwszContainerName
= containerW
;
841 matches
= key_prov_info_matches_cert(pCert
, ©
);
844 keyProvInfo
->pwszContainerName
=
845 CryptMemAlloc((strlenW(containerW
) + 1) * sizeof(WCHAR
));
846 if (keyProvInfo
->pwszContainerName
)
848 strcpyW(keyProvInfo
->pwszContainerName
, containerW
);
849 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
857 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
858 * private key matches pCert's public key. Upon success, updates keyProvInfo
859 * with the matching container's info (free keyProvInfo.pwszContainerName upon
861 * Returns TRUE if found, FALSE if not.
863 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
864 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
866 HCRYPTPROV defProvider
;
867 BOOL ret
, found
= FALSE
;
868 char containerA
[MAX_PATH
];
870 assert(keyProvInfo
->pwszContainerName
== NULL
);
871 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
872 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
873 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
875 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
877 while (ret
&& !found
)
879 DWORD size
= sizeof(containerA
);
881 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
882 (BYTE
*)containerA
, &size
, enumFlags
);
884 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
885 if (enumFlags
& CRYPT_FIRST
)
887 enumFlags
&= ~CRYPT_FIRST
;
888 enumFlags
|= CRYPT_NEXT
;
891 CryptReleaseContext(defProvider
, 0);
896 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
898 BOOL found
= FALSE
, ret
= TRUE
;
899 DWORD index
= 0, cbProvName
= 0;
900 CRYPT_KEY_PROV_INFO keyProvInfo
;
902 TRACE("(%p, %08x)\n", pCert
, dwFlags
);
904 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
905 while (ret
&& !found
)
909 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
913 if (size
<= cbProvName
)
914 ret
= CryptEnumProvidersW(index
, NULL
, 0,
915 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
918 CryptMemFree(keyProvInfo
.pwszProvName
);
919 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
920 if (keyProvInfo
.pwszProvName
)
923 ret
= CryptEnumProvidersW(index
, NULL
, 0,
924 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
927 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
928 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
929 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
930 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
931 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
933 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
934 found
= find_key_prov_info_in_provider(pCert
,
939 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
940 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
941 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
943 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
944 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
945 found
= find_key_prov_info_in_provider(pCert
,
958 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
960 CryptMemFree(keyProvInfo
.pwszProvName
);
961 CryptMemFree(keyProvInfo
.pwszContainerName
);
965 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
967 BOOL matches
= FALSE
;
970 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
973 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
977 if (CertGetCertificateContextProperty(pCert
,
978 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
979 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
980 CryptMemFree(keyProvInfo
);
986 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
987 DWORD dwFlags
, void *pvReserved
)
989 BOOL matches
= FALSE
;
991 TRACE("(%p, %08x, %p)\n", pCert
, dwFlags
, pvReserved
);
993 matches
= cert_prov_info_matches_cert(pCert
);
995 matches
= find_matching_provider(pCert
, dwFlags
);
999 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1000 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1004 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1006 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1007 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1008 &pCertId2
->SerialNumber
);
1009 TRACE("returning %d\n", ret
);
1013 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1014 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1018 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1020 if (pCertName1
->cbData
== pCertName2
->cbData
)
1022 if (pCertName1
->cbData
)
1023 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1024 pCertName1
->cbData
);
1030 TRACE("returning %d\n", ret
);
1034 /* Returns the number of significant bytes in pInt, where a byte is
1035 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1036 * for negative numbers. pInt is assumed to be little-endian.
1038 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1040 DWORD ret
= pInt
->cbData
;
1044 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1046 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1054 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1055 PCRYPT_INTEGER_BLOB pInt2
)
1060 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1062 cb1
= CRYPT_significantBytes(pInt1
);
1063 cb2
= CRYPT_significantBytes(pInt2
);
1067 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1073 TRACE("returning %d\n", ret
);
1077 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1078 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1082 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1084 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1086 case 0: /* Seems to mean "raw binary bits" */
1087 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1088 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1090 if (pPublicKey2
->PublicKey
.cbData
)
1091 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1092 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1100 WARN("Unknown encoding type %08x\n", dwCertEncodingType
);
1102 case X509_ASN_ENCODING
:
1104 BLOBHEADER
*pblob1
, *pblob2
;
1107 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1108 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1111 pblob1
= CryptMemAlloc(length
);
1112 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1113 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1114 0, pblob1
, &length
))
1116 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1117 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1120 pblob2
= CryptMemAlloc(length
);
1121 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1122 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1123 0, pblob2
, &length
))
1125 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1126 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1127 *pk2
= (LPVOID
)(pblob2
+ 1);
1128 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1129 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1131 CryptMemFree(pblob2
);
1134 CryptMemFree(pblob1
);
1143 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1144 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1148 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
1150 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1152 SetLastError(ERROR_FILE_NOT_FOUND
);
1155 if (pPublicKey
->Algorithm
.pszObjId
&&
1156 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1158 FIXME("unimplemented for DH public keys\n");
1159 SetLastError(CRYPT_E_ASN1_BADTAG
);
1165 BOOL ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1166 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1167 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1172 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1174 len
= rsaPubKey
->bitlen
;
1181 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1182 DWORD dwFlags
, const void *pvPara
);
1184 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1185 DWORD dwFlags
, const void *pvPara
)
1189 DWORD size
= sizeof(hash
);
1191 ret
= CertGetCertificateContextProperty(pCertContext
,
1192 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1195 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1197 if (size
== pHash
->cbData
)
1198 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1205 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1206 DWORD dwFlags
, const void *pvPara
)
1210 DWORD size
= sizeof(hash
);
1212 ret
= CertGetCertificateContextProperty(pCertContext
,
1213 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1216 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1218 if (size
== pHash
->cbData
)
1219 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1226 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1227 DWORD dwFlags
, const void *pvPara
)
1229 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1232 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1233 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1235 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1236 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1241 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1242 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1244 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1247 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1248 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1252 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1253 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1255 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1258 /* Matching serial number and subject match.. */
1259 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1260 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1262 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1263 &pCertInfo
->SerialNumber
);
1266 /* failing that, if the serial number and issuer match, we match */
1267 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1268 &pCertInfo
->SerialNumber
);
1270 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1271 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1273 TRACE("returning %d\n", ret
);
1277 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1278 DWORD dwFlags
, const void *pvPara
)
1280 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1283 switch (id
->dwIdChoice
)
1285 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1286 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1287 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1289 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1290 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1292 case CERT_ID_SHA1_HASH
:
1293 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1296 case CERT_ID_KEY_IDENTIFIER
:
1300 ret
= CertGetCertificateContextProperty(pCertContext
,
1301 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1302 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1304 LPBYTE buf
= CryptMemAlloc(size
);
1308 CertGetCertificateContextProperty(pCertContext
,
1309 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1310 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1325 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1326 DWORD dwFlags
, const void *pvPara
)
1328 PCCERT_CONTEXT toCompare
= pvPara
;
1329 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1330 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1333 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1334 DWORD dwFlags
, const void *pvPara
)
1336 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1340 ret
= CertGetCertificateContextProperty(pCertContext
,
1341 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1342 if (ret
&& size
== hash
->cbData
)
1344 LPBYTE buf
= CryptMemAlloc(size
);
1348 CertGetCertificateContextProperty(pCertContext
,
1349 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1350 ret
= !memcmp(buf
, hash
->pbData
, size
);
1359 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1360 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1363 BOOL matches
= FALSE
;
1368 ret
= CertEnumCertificatesInStore(store
, ret
);
1370 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1371 } while (ret
!= NULL
&& !matches
);
1375 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1376 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1378 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1379 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1381 return CertEnumCertificatesInStore(store
, prev
);
1384 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1385 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1388 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1389 PCERT_EXTENSION ext
;
1392 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1393 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1395 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1397 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1398 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1399 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1405 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1407 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1408 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1409 sizeof(CERT_NAME_BLOB
));
1410 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1411 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1413 else if (info
->KeyId
.cbData
)
1415 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1416 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1421 found
= cert_compare_certs_in_store(store
, prev
,
1422 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1426 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1427 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1429 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1431 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1432 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1433 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1439 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1440 info
->AuthorityCertSerialNumber
.cbData
)
1442 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1445 for (i
= 0; !directoryName
&&
1446 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1447 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1448 == CERT_ALT_NAME_DIRECTORY_NAME
)
1450 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1453 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1454 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1455 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1456 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1457 &info
->AuthorityCertSerialNumber
,
1458 sizeof(CRYPT_INTEGER_BLOB
));
1462 FIXME("no supported name type in authority key id2\n");
1466 else if (info
->KeyId
.cbData
)
1468 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1469 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1474 found
= cert_compare_certs_in_store(store
, prev
,
1475 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1480 found
= cert_compare_certs_in_store(store
, prev
,
1481 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1482 dwFlags
, &subject
->pCertInfo
->Issuer
);
1486 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1487 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1489 PCERT_NAME_BLOB name
;
1493 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1494 name
= &pCertContext
->pCertInfo
->Subject
;
1496 name
= &pCertContext
->pCertInfo
->Issuer
;
1497 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1498 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1501 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1507 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1508 CERT_SIMPLE_NAME_STR
, str
, len
);
1509 for (ptr
= str
; *ptr
; ptr
++)
1510 *ptr
= tolowerW(*ptr
);
1511 if (strstrW(str
, pvPara
))
1519 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1520 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1522 PCCERT_CONTEXT found
= NULL
;
1524 TRACE("%s\n", debugstr_a(pvPara
));
1528 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1529 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1535 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1536 for (ptr
= str
; *ptr
; ptr
++)
1537 *ptr
= tolowerW(*ptr
);
1538 found
= cert_compare_certs_in_store(store
, prev
,
1539 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1544 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1548 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1549 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1551 PCCERT_CONTEXT found
= NULL
;
1553 TRACE("%s\n", debugstr_w(pvPara
));
1557 DWORD len
= strlenW(pvPara
);
1558 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1565 for (src
= pvPara
, dst
= str
; *src
; src
++, dst
++)
1566 *dst
= tolowerW(*src
);
1568 found
= cert_compare_certs_in_store(store
, prev
,
1569 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1574 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1578 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1579 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1580 PCCERT_CONTEXT pPrevCertContext
)
1583 CertFindFunc find
= NULL
;
1584 CertCompareFunc compare
= NULL
;
1586 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1587 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1589 switch (dwType
>> CERT_COMPARE_SHIFT
)
1591 case CERT_COMPARE_ANY
:
1592 find
= find_cert_any
;
1594 case CERT_COMPARE_MD5_HASH
:
1595 compare
= compare_cert_by_md5_hash
;
1597 case CERT_COMPARE_SHA1_HASH
:
1598 compare
= compare_cert_by_sha1_hash
;
1600 case CERT_COMPARE_NAME
:
1601 compare
= compare_cert_by_name
;
1603 case CERT_COMPARE_PUBLIC_KEY
:
1604 compare
= compare_cert_by_public_key
;
1606 case CERT_COMPARE_NAME_STR_A
:
1607 find
= find_cert_by_name_str_a
;
1609 case CERT_COMPARE_NAME_STR_W
:
1610 find
= find_cert_by_name_str_w
;
1612 case CERT_COMPARE_SUBJECT_CERT
:
1613 compare
= compare_cert_by_subject_cert
;
1615 case CERT_COMPARE_CERT_ID
:
1616 compare
= compare_cert_by_cert_id
;
1618 case CERT_COMPARE_ISSUER_OF
:
1619 find
= find_cert_by_issuer
;
1621 case CERT_COMPARE_EXISTING
:
1622 compare
= compare_existing_cert
;
1624 case CERT_COMPARE_SIGNATURE_HASH
:
1625 compare
= compare_cert_by_signature_hash
;
1628 FIXME("find type %08x unimplemented\n", dwType
);
1632 ret
= find(hCertStore
, dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1634 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1635 compare
, dwType
, dwFlags
, pvPara
);
1639 SetLastError(CRYPT_E_NOT_FOUND
);
1640 TRACE("returning %p\n", ret
);
1644 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1645 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1647 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1651 SetLastError(E_INVALIDARG
);
1654 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1655 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1658 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1659 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1661 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1662 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1664 if (*pdwFlags
& ~supportedFlags
)
1666 SetLastError(E_INVALIDARG
);
1669 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1672 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1675 /* FIXME: what if the CRL has expired? */
1678 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1679 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1680 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1683 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1685 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1687 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1688 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1690 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1692 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1693 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1694 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1695 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1700 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1701 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1706 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1707 pPrevIssuerContext
, *pdwFlags
);
1709 if (!pSubjectContext
)
1711 SetLastError(E_INVALIDARG
);
1715 ret
= CertFindCertificateInStore(hCertStore
,
1716 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1717 pSubjectContext
, pPrevIssuerContext
);
1720 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1723 CertFreeCertificateContext(ret
);
1727 TRACE("returning %p\n", ret
);
1731 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1736 } OLD_CERT_REVOCATION_STATUS
, *POLD_CERT_REVOCATION_STATUS
;
1738 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1739 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
1741 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1742 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1743 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1747 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1748 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1750 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1751 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1753 SetLastError(E_INVALIDARG
);
1758 static HCRYPTOIDFUNCSET set
= NULL
;
1762 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
1763 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
1769 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
1774 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
1778 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
1782 for (ptr
= dllList
; ret
&& *ptr
;
1783 ptr
+= lstrlenW(ptr
) + 1)
1785 CertVerifyRevocationFunc func
;
1786 HCRYPTOIDFUNCADDR hFunc
;
1788 ret
= CryptGetDefaultOIDFunctionAddress(set
,
1789 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
1792 ret
= func(dwEncodingType
, dwRevType
, cContext
,
1793 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1794 CryptFreeOIDFunctionAddress(hFunc
, 0);
1798 CryptMemFree(dllList
);
1802 SetLastError(ERROR_OUTOFMEMORY
);
1813 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
1814 CRYPT_ATTRIBUTE rgAttr
[])
1816 PCRYPT_ATTRIBUTE ret
= NULL
;
1819 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
1825 SetLastError(ERROR_INVALID_PARAMETER
);
1829 for (i
= 0; !ret
&& i
< cAttr
; i
++)
1830 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
1835 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
1836 CERT_EXTENSION rgExtensions
[])
1838 PCERT_EXTENSION ret
= NULL
;
1841 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
1847 SetLastError(ERROR_INVALID_PARAMETER
);
1851 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
1852 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
1853 rgExtensions
[i
].pszObjId
))
1854 ret
= &rgExtensions
[i
];
1858 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
1860 PCERT_RDN_ATTR ret
= NULL
;
1863 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
1867 SetLastError(ERROR_INVALID_PARAMETER
);
1871 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
1872 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
1873 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
1874 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
1875 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
1879 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
1880 const CERT_RDN_ATTR
*attr
)
1885 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
1887 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
1889 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
1891 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
1894 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
1897 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1898 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
1900 if (attr
->Value
.cbData
!=
1901 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1903 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1904 match
= !strncmpiW(nameStr
, attrStr
,
1905 attr
->Value
.cbData
/ sizeof(WCHAR
));
1907 match
= !strncmpW(nameStr
, attrStr
,
1908 attr
->Value
.cbData
/ sizeof(WCHAR
));
1909 TRACE("%s : %s => %d\n",
1910 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1911 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1917 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1918 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
1920 if (attr
->Value
.cbData
!=
1921 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1923 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1924 match
= !strncasecmp(nameStr
, attrStr
,
1925 attr
->Value
.cbData
);
1927 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
1928 TRACE("%s : %s => %d\n",
1929 debugstr_an(nameStr
, attr
->Value
.cbData
),
1930 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
1938 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
1939 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
1941 CERT_NAME_INFO
*name
;
1946 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
1949 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
1951 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
1952 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
1956 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
1957 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
1959 SetLastError(CRYPT_E_NO_MATCH
);
1965 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
1966 PCERT_INFO pCertInfo
)
1973 GetSystemTimeAsFileTime(&fileTime
);
1974 pTimeToVerify
= &fileTime
;
1976 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
1978 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
1985 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
1986 PCERT_INFO pIssuerInfo
)
1988 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
1990 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
1991 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
1994 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
1995 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
1996 DWORD
*pcbComputedHash
)
1999 HCRYPTHASH hHash
= 0;
2001 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2002 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2005 hCryptProv
= CRYPT_GetDefaultProvider();
2010 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2013 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2015 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2016 pcbComputedHash
, 0);
2017 CryptDestroyHash(hHash
);
2023 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2024 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2025 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2028 HCRYPTHASH hHash
= 0;
2030 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2031 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2034 hCryptProv
= CRYPT_GetDefaultProvider();
2037 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2039 SetLastError(ERROR_FILE_NOT_FOUND
);
2047 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2048 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2049 (LPBYTE
)&buf
, &size
);
2052 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2055 ret
= CryptHashData(hHash
, buf
, size
, 0);
2057 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2058 pcbComputedHash
, 0);
2059 CryptDestroyHash(hHash
);
2067 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2068 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2069 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2072 CERT_SIGNED_CONTENT_INFO
*info
;
2075 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv
, dwCertEncodingType
,
2076 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2078 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2079 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2082 PCCRYPT_OID_INFO oidInfo
;
2086 hCryptProv
= CRYPT_GetDefaultProvider();
2087 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2088 info
->SignatureAlgorithm
.pszObjId
, 0);
2091 SetLastError(NTE_BAD_ALGID
);
2096 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2099 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2100 info
->ToBeSigned
.cbData
, 0);
2102 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2103 pcbComputedHash
, 0);
2104 CryptDestroyHash(hHash
);
2112 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2113 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2114 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2115 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2118 PCCRYPT_OID_INFO info
;
2121 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
2122 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2123 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2125 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2126 pSignatureAlgorithm
->pszObjId
, 0);
2129 SetLastError(NTE_BAD_ALGID
);
2132 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2135 hCryptProv
= CRYPT_GetDefaultProvider();
2136 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2139 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2140 cbEncodedToBeSigned
, 0);
2142 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2144 CryptDestroyHash(hHash
);
2151 SetLastError(ERROR_INVALID_PARAMETER
);
2156 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2159 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2160 cbEncodedToBeSigned
, 0);
2162 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2164 CryptDestroyHash(hHash
);
2171 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2172 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2173 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2174 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2177 DWORD encodedSize
, hashSize
;
2179 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2180 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2181 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2183 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2184 NULL
, &encodedSize
);
2187 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2191 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2192 pvStructInfo
, encoded
, &encodedSize
);
2195 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2196 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2197 pvHashAuxInfo
, NULL
, &hashSize
);
2200 PBYTE hash
= CryptMemAlloc(hashSize
);
2204 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2205 dwCertEncodingType
, encoded
, encodedSize
,
2206 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2209 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2211 info
.ToBeSigned
.cbData
= encodedSize
;
2212 info
.ToBeSigned
.pbData
= encoded
;
2213 memcpy(&info
.SignatureAlgorithm
,
2214 pSignatureAlgorithm
,
2215 sizeof(info
.SignatureAlgorithm
));
2216 info
.Signature
.cbData
= hashSize
;
2217 info
.Signature
.pbData
= hash
;
2218 info
.Signature
.cUnusedBits
= 0;
2219 ret
= CryptEncodeObject(dwCertEncodingType
,
2220 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2226 CryptMemFree(encoded
);
2232 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2233 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2234 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2236 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2237 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, (void *)pbEncoded
,
2238 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2241 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
,
2242 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pubKeyInfo
,
2243 const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2247 PCCRYPT_OID_INFO info
;
2248 ALG_ID pubKeyID
, hashID
;
2250 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2251 signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2252 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2254 SetLastError(NTE_BAD_ALGID
);
2257 hashID
= info
->u
.Algid
;
2258 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2259 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2262 /* Load the default provider if necessary */
2264 hCryptProv
= CRYPT_GetDefaultProvider();
2265 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2266 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2271 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2274 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2275 signedCert
->ToBeSigned
.cbData
, 0);
2277 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2278 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2279 CryptDestroyHash(hash
);
2281 CryptDestroyKey(key
);
2286 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2287 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2288 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2291 CRYPT_DATA_BLOB subjectBlob
;
2293 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
2294 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2295 dwFlags
, pvReserved
);
2297 switch (dwSubjectType
)
2299 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2301 PCRYPT_DATA_BLOB blob
= pvSubject
;
2303 subjectBlob
.pbData
= blob
->pbData
;
2304 subjectBlob
.cbData
= blob
->cbData
;
2307 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2309 PCERT_CONTEXT context
= pvSubject
;
2311 subjectBlob
.pbData
= context
->pbCertEncoded
;
2312 subjectBlob
.cbData
= context
->cbCertEncoded
;
2315 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2317 PCRL_CONTEXT context
= pvSubject
;
2319 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2320 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2324 SetLastError(E_INVALIDARG
);
2330 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2333 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2334 subjectBlob
.pbData
, subjectBlob
.cbData
,
2335 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2336 &signedCert
, &size
);
2339 switch (dwIssuerType
)
2341 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2342 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2343 dwCertEncodingType
, pvIssuer
,
2346 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2347 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2349 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2352 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2353 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2356 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2359 SetLastError(E_INVALIDARG
);
2364 FIXME("unimplemented for NULL signer\n");
2365 SetLastError(E_INVALIDARG
);
2370 SetLastError(E_INVALIDARG
);
2373 LocalFree(signedCert
);
2379 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2380 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
2382 PCERT_EXTENSION ext
;
2385 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
2388 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
2389 pCertInfo
->rgExtension
);
2392 CRYPT_BIT_BLOB usage
;
2393 DWORD size
= sizeof(usage
);
2395 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
2396 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2400 if (cbKeyUsage
< usage
.cbData
)
2404 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
2405 if (cbKeyUsage
> usage
.cbData
)
2406 memset(pbKeyUsage
+ usage
.cbData
, 0,
2407 cbKeyUsage
- usage
.cbData
);
2416 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
2417 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
2419 PCERT_ENHKEY_USAGE usage
= NULL
;
2423 if (!pCertContext
|| !pcbUsage
)
2425 SetLastError(ERROR_INVALID_PARAMETER
);
2429 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
2431 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
2435 if (CertGetCertificateContextProperty(pCertContext
,
2436 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
2438 LPBYTE buf
= CryptMemAlloc(propSize
);
2442 if (CertGetCertificateContextProperty(pCertContext
,
2443 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
2445 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2446 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
2447 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2453 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
2455 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
2456 pCertContext
->pCertInfo
->cExtension
,
2457 pCertContext
->pCertInfo
->rgExtension
);
2461 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2462 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2463 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2468 /* If a particular location is specified, this should fail. Otherwise
2469 * it should succeed with an empty usage. (This is true on Win2k and
2470 * later, which we emulate.)
2474 SetLastError(CRYPT_E_NOT_FOUND
);
2478 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
2484 *pcbUsage
= bytesNeeded
;
2485 else if (*pcbUsage
< bytesNeeded
)
2487 SetLastError(ERROR_MORE_DATA
);
2488 *pcbUsage
= bytesNeeded
;
2493 *pcbUsage
= bytesNeeded
;
2497 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
2498 sizeof(CERT_ENHKEY_USAGE
) +
2499 usage
->cUsageIdentifier
* sizeof(LPSTR
));
2501 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
2502 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
2503 sizeof(CERT_ENHKEY_USAGE
));
2504 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2506 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2507 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2508 nextOID
+= strlen(nextOID
) + 1;
2512 pUsage
->cUsageIdentifier
= 0;
2517 TRACE("returning %d\n", ret
);
2521 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
2522 PCERT_ENHKEY_USAGE pUsage
)
2526 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
2530 CRYPT_DATA_BLOB blob
= { 0, NULL
};
2532 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
2533 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
2536 ret
= CertSetCertificateContextProperty(pCertContext
,
2537 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
2538 LocalFree(blob
.pbData
);
2542 ret
= CertSetCertificateContextProperty(pCertContext
,
2543 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
2547 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2548 LPCSTR pszUsageIdentifier
)
2553 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2555 if (CertGetEnhancedKeyUsage(pCertContext
,
2556 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
2558 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
2562 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2563 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
2567 BOOL exists
= FALSE
;
2569 /* Make sure usage doesn't already exist */
2570 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
2572 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
2573 pszUsageIdentifier
))
2578 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
2579 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2585 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
2586 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
2587 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
2588 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
2589 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2591 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2592 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2593 nextOID
+= strlen(nextOID
) + 1;
2595 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2596 strcpy(nextOID
, pszUsageIdentifier
);
2597 newUsage
->cUsageIdentifier
= i
+ 1;
2598 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
2599 CryptMemFree(newUsage
);
2605 CryptMemFree(usage
);
2612 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
2613 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2617 usage
->rgpszUsageIdentifier
=
2618 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
2619 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
2620 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
2621 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
2622 usage
->cUsageIdentifier
= 1;
2623 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
2624 CryptMemFree(usage
);
2632 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2633 LPCSTR pszUsageIdentifier
)
2637 CERT_ENHKEY_USAGE usage
;
2639 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2641 size
= sizeof(usage
);
2642 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2643 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
2644 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2646 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2650 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2651 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
2654 if (pUsage
->cUsageIdentifier
)
2659 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
2661 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
2662 pszUsageIdentifier
))
2664 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
2665 pUsage
->rgpszUsageIdentifier
[i
] =
2666 pUsage
->rgpszUsageIdentifier
[i
+ 1];
2668 pUsage
->cUsageIdentifier
--;
2669 /* Remove the usage if it's empty */
2670 if (pUsage
->cUsageIdentifier
)
2671 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
2673 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
2676 CryptMemFree(pUsage
);
2683 /* it fit in an empty usage, therefore there's nothing to remove */
2695 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
2697 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
2699 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2701 if (indexIndex
+ 1 > field
->cIndexes
)
2703 if (field
->cIndexes
)
2704 field
->indexes
= CryptMemRealloc(field
->indexes
,
2705 (indexIndex
+ 1) * sizeof(DWORD
));
2707 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
2710 field
->indexes
[indexIndex
] = 0;
2711 field
->cIndexes
= indexIndex
+ 1;
2715 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
2718 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
2721 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2723 assert(field
->cIndexes
);
2724 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
2728 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
2729 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
2732 DWORD i
, cbOIDs
= 0;
2733 BOOL allUsagesValid
= TRUE
;
2734 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
2736 TRACE("(%d, %p, %d, %p, %d)\n", cCerts
, rghCerts
, *cNumOIDs
,
2739 for (i
= 0; i
< cCerts
; i
++)
2741 CERT_ENHKEY_USAGE usage
;
2742 DWORD size
= sizeof(usage
);
2744 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
2745 /* Success is deliberately ignored: it implies all usages are valid */
2746 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2748 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2750 allUsagesValid
= FALSE
;
2753 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
2756 if (!validUsages
.cUsageIdentifier
)
2760 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
2761 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
2762 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2763 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
2765 validUsages
.rgpszUsageIdentifier
=
2766 CryptMemAlloc(cbOIDs
);
2767 if (validUsages
.rgpszUsageIdentifier
)
2769 LPSTR nextOID
= (LPSTR
)
2770 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
2771 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2773 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2775 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
2776 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
2777 pUsage
->rgpszUsageIdentifier
[j
]);
2778 nextOID
+= lstrlenA(nextOID
) + 1;
2784 struct BitField validIndexes
= { 0, NULL
};
2785 DWORD j
, k
, numRemoved
= 0;
2787 /* Merge: build a bitmap of all the indexes of
2788 * validUsages.rgpszUsageIdentifier that are in pUsage.
2790 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
2792 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
2794 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
2795 validUsages
.rgpszUsageIdentifier
[k
]))
2797 CRYPT_SetBitInField(&validIndexes
, k
);
2802 /* Merge by removing from validUsages those that are
2803 * not in the bitmap.
2805 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2807 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
2809 if (j
< validUsages
.cUsageIdentifier
- 1)
2811 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
2812 &validUsages
.rgpszUsageIdentifier
[j
+
2814 (validUsages
.cUsageIdentifier
- numRemoved
2815 - j
- 1) * sizeof(LPSTR
));
2817 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
2819 validUsages
.cUsageIdentifier
--;
2823 validUsages
.cUsageIdentifier
--;
2826 CryptMemFree(validIndexes
.indexes
);
2829 CryptMemFree(pUsage
);
2841 *cNumOIDs
= validUsages
.cUsageIdentifier
;
2844 else if (*pcbOIDs
< cbOIDs
)
2847 SetLastError(ERROR_MORE_DATA
);
2852 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
2853 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2856 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
2858 rghOIDs
[i
] = nextOID
;
2859 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
2860 nextOID
+= lstrlenA(nextOID
) + 1;
2864 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
2865 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
2866 TRACE("returning %d\n", ret
);
2870 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
2871 * pInfo is NULL, from the attributes of hProv.
2873 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
2874 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
2876 CRYPT_KEY_PROV_INFO info
= { 0 };
2884 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
2887 LPSTR szContainer
= CryptMemAlloc(size
);
2891 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
2892 (BYTE
*)szContainer
, &size
, 0);
2895 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2899 info
.pwszContainerName
= CryptMemAlloc(len
*
2901 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2902 info
.pwszContainerName
, len
);
2905 CryptMemFree(szContainer
);
2908 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
2911 LPSTR szProvider
= CryptMemAlloc(size
);
2915 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
2919 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2923 info
.pwszProvName
= CryptMemAlloc(len
*
2925 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2926 info
.pwszProvName
, len
);
2929 CryptMemFree(szProvider
);
2932 size
= sizeof(info
.dwKeySpec
);
2933 /* in case no CRYPT_KEY_PROV_INFO given,
2934 * we always use AT_SIGNATURE key spec
2936 info
.dwKeySpec
= AT_SIGNATURE
;
2937 size
= sizeof(info
.dwProvType
);
2938 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
2941 info
.dwProvType
= PROV_RSA_FULL
;
2945 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
2950 CryptMemFree(info
.pwszContainerName
);
2951 CryptMemFree(info
.pwszProvName
);
2955 /* Creates a signed certificate context from the unsigned, encoded certificate
2956 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
2958 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
2959 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
2961 PCCERT_CONTEXT context
= NULL
;
2965 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2966 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
2969 LPBYTE sig
= CryptMemAlloc(sigSize
);
2971 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2972 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
2975 CERT_SIGNED_CONTENT_INFO signedInfo
;
2976 BYTE
*encodedSignedCert
= NULL
;
2977 DWORD encodedSignedCertSize
= 0;
2979 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
2980 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
2981 memcpy(&signedInfo
.SignatureAlgorithm
, sigAlgo
,
2982 sizeof(signedInfo
.SignatureAlgorithm
));
2983 signedInfo
.Signature
.cbData
= sigSize
;
2984 signedInfo
.Signature
.pbData
= sig
;
2985 signedInfo
.Signature
.cUnusedBits
= 0;
2986 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
2987 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2988 &encodedSignedCert
, &encodedSignedCertSize
);
2991 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2992 encodedSignedCert
, encodedSignedCertSize
);
2993 LocalFree(encodedSignedCert
);
3001 /* Copies data from the parameters into info, where:
3002 * pSerialNumber: The serial number. Must not be NULL.
3003 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3005 * pSignatureAlgorithm: Optional.
3006 * pStartTime: The starting time of the certificate. If NULL, the current
3007 * system time is used.
3008 * pEndTime: The ending time of the certificate. If NULL, one year past the
3009 * starting time is used.
3010 * pubKey: The public key of the certificate. Must not be NULL.
3011 * pExtensions: Extensions to be included with the certificate. Optional.
3013 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3014 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3015 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3016 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3017 const CERT_EXTENSIONS
*pExtensions
)
3019 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3022 assert(pSerialNumber
);
3023 assert(pSubjectIssuerBlob
);
3026 if (pExtensions
&& pExtensions
->cExtension
)
3027 info
->dwVersion
= CERT_V3
;
3029 info
->dwVersion
= CERT_V1
;
3030 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3031 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3032 if (pSignatureAlgorithm
)
3033 memcpy(&info
->SignatureAlgorithm
, pSignatureAlgorithm
,
3034 sizeof(info
->SignatureAlgorithm
));
3037 info
->SignatureAlgorithm
.pszObjId
= oid
;
3038 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3039 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3041 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3042 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3044 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3046 GetSystemTimeAsFileTime(&info
->NotBefore
);
3048 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3053 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3056 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3059 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3060 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3061 memcpy(&info
->SubjectPublicKeyInfo
, pubKey
,
3062 sizeof(info
->SubjectPublicKeyInfo
));
3065 info
->cExtension
= pExtensions
->cExtension
;
3066 info
->rgExtension
= pExtensions
->rgExtension
;
3070 info
->cExtension
= 0;
3071 info
->rgExtension
= NULL
;
3075 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3076 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3077 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3079 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3081 HCRYPTPROV hProv
= 0;
3082 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
3086 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3088 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3090 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3091 rpcrt
, "RpcStringFreeA");
3093 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3096 RPC_STATUS status
= uuidCreate(&uuid
);
3098 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3100 unsigned char *uuidStr
;
3102 status
= uuidToString(&uuid
, &uuidStr
);
3103 if (status
== RPC_S_OK
)
3105 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3106 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3112 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3114 CryptDestroyKey(key
);
3116 rpcStringFree(&uuidStr
);
3125 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3126 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3127 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3128 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3129 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3131 PCCERT_CONTEXT context
= NULL
;
3132 BOOL ret
, releaseContext
= FALSE
;
3133 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3134 DWORD pubKeySize
= 0,dwKeySpec
= AT_SIGNATURE
;
3136 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
3137 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3138 pExtensions
, pExtensions
);
3140 if(!pSubjectIssuerBlob
)
3142 SetLastError(ERROR_INVALID_PARAMETER
);
3150 hProv
= CRYPT_CreateKeyProv();
3151 releaseContext
= TRUE
;
3153 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3155 SetLastError(NTE_BAD_FLAGS
);
3161 /* acquire the context using the given information*/
3162 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3163 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3164 pKeyProvInfo
->dwFlags
);
3167 if(GetLastError() != NTE_BAD_KEYSET
)
3169 /* create the key set */
3170 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3171 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3172 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3176 dwKeySpec
= pKeyProvInfo
->dwKeySpec
;
3177 /* check if the key is here */
3178 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3181 if (NTE_NO_KEY
== GetLastError())
3182 { /* generate the key */
3183 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3187 CryptReleaseContext(hProv
,0);
3188 SetLastError(NTE_BAD_KEYSET
);
3192 CryptDestroyKey(hKey
);
3193 releaseContext
= TRUE
;
3196 else if (pKeyProvInfo
)
3198 SetLastError(ERROR_INVALID_PARAMETER
);
3202 CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3204 pubKey
= CryptMemAlloc(pubKeySize
);
3207 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3208 pubKey
, &pubKeySize
);
3211 CERT_INFO info
= { 0 };
3212 CRYPT_DER_BLOB blob
= { 0, NULL
};
3214 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3216 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3217 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3218 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3219 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3220 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3224 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3225 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3226 &info
.SignatureAlgorithm
);
3228 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3229 blob
.pbData
, blob
.cbData
);
3230 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3231 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3232 LocalFree(blob
.pbData
);
3235 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
);