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
20 #define WIN32_NO_STATUS
22 #define COM_NO_WINDOWS_H
27 #define NONAMELESSUNION
33 #include <wine/debug.h>
34 #include <wine/unicode.h>
35 #include "crypt32_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
39 /* Internal version of CertGetCertificateContextProperty that gets properties
40 * directly from the context (or the context it's linked to, depending on its
41 * type.) Doesn't handle special-case properties, since they are handled by
42 * CertGetCertificateContextProperty, and are particular to the store in which
43 * the property exists (which is separate from the context.)
45 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
46 void *pvData
, DWORD
*pcbData
);
48 /* Internal version of CertSetCertificateContextProperty that sets properties
49 * directly on the context (or the context it's linked to, depending on its
50 * type.) Doesn't handle special cases, since they're handled by
51 * CertSetCertificateContextProperty anyway.
53 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
54 DWORD dwFlags
, const void *pvData
);
56 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
57 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
58 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
60 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
61 pbCertEncoded
, cbCertEncoded
);
64 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
, dwCertEncodingType
,
65 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
69 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
70 dwAddDisposition
, ppCertContext
);
71 CertFreeCertificateContext(cert
);
78 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreA(LPCSTR pszCertStoreName
,
79 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
84 TRACE("(%s, %p, %d)\n", debugstr_a(pszCertStoreName
), pbCertEncoded
,
87 store
= CertOpenSystemStoreA(0, pszCertStoreName
);
90 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
91 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
92 CertCloseStore(store
, 0);
97 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreW(LPCWSTR pszCertStoreName
,
98 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
103 TRACE("(%s, %p, %d)\n", debugstr_w(pszCertStoreName
), pbCertEncoded
,
106 store
= CertOpenSystemStoreW(0, pszCertStoreName
);
109 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
110 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
111 CertCloseStore(store
, 0);
116 BOOL WINAPI
CertAddCertificateLinkToStore(HCERTSTORE hCertStore
,
117 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
118 PCCERT_CONTEXT
*ppCertContext
)
121 PWINECRYPT_CERTSTORE store
= (PWINECRYPT_CERTSTORE
)hCertStore
;
124 FIXME("(%p, %p, %08x, %p): semi-stub\n", hCertStore
, pCertContext
,
125 dwAddDisposition
, ppCertContext
);
126 if (store
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
128 if (store
->type
== StoreTypeCollection
)
130 SetLastError(E_INVALIDARG
);
133 return CertAddCertificateContextToStore(hCertStore
, pCertContext
,
134 dwAddDisposition
, ppCertContext
);
137 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
138 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
140 PCERT_CONTEXT cert
= NULL
;
142 PCERT_INFO certInfo
= NULL
;
145 TRACE("(%08x, %p, %d)\n", dwCertEncodingType
, pbCertEncoded
,
148 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
150 SetLastError(E_INVALIDARG
);
154 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
155 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
161 cert
= Context_CreateDataContext(sizeof(CERT_CONTEXT
));
164 data
= CryptMemAlloc(cbCertEncoded
);
167 CertFreeCertificateContext(cert
);
171 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
172 cert
->dwCertEncodingType
= dwCertEncodingType
;
173 cert
->pbCertEncoded
= data
;
174 cert
->cbCertEncoded
= cbCertEncoded
;
175 cert
->pCertInfo
= certInfo
;
176 cert
->hCertStore
= 0;
183 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(
184 PCCERT_CONTEXT pCertContext
)
186 TRACE("(%p)\n", pCertContext
);
191 Context_AddRef((void *)pCertContext
, sizeof(CERT_CONTEXT
));
195 static void CertDataContext_Free(void *context
)
197 PCERT_CONTEXT certContext
= context
;
199 CryptMemFree(certContext
->pbCertEncoded
);
200 LocalFree(certContext
->pCertInfo
);
203 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
207 TRACE("(%p)\n", pCertContext
);
210 ret
= Context_Release((void *)pCertContext
, sizeof(CERT_CONTEXT
),
211 CertDataContext_Free
);
215 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
218 PCONTEXT_PROPERTY_LIST properties
= Context_GetProperties(
219 pCertContext
, sizeof(CERT_CONTEXT
));
222 TRACE("(%p, %d)\n", pCertContext
, dwPropId
);
225 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
231 static BOOL
CertContext_GetHashProp(void *context
, DWORD dwPropId
,
232 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
235 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
239 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
241 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
246 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
253 else if (*pcbData
< cb
)
255 SetLastError(ERROR_MORE_DATA
);
261 memcpy(pvData
, pb
, cb
);
267 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
268 void *pvData
, DWORD
*pcbData
)
270 PCCERT_CONTEXT pCertContext
= context
;
271 PCONTEXT_PROPERTY_LIST properties
=
272 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
274 CRYPT_DATA_BLOB blob
;
276 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
279 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
283 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
286 /* Implicit properties */
289 case CERT_SHA1_HASH_PROP_ID
:
290 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
291 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
294 case CERT_MD5_HASH_PROP_ID
:
295 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
296 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
299 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
300 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
301 pCertContext
->pCertInfo
->Subject
.pbData
,
302 pCertContext
->pCertInfo
->Subject
.cbData
,
305 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
306 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
307 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
308 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
311 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
312 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
313 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
314 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
317 case CERT_SIGNATURE_HASH_PROP_ID
:
318 ret
= CryptHashToBeSigned(0, pCertContext
->dwCertEncodingType
,
319 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
323 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
325 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
328 case CERT_KEY_IDENTIFIER_PROP_ID
:
330 PCERT_EXTENSION ext
= CertFindExtension(
331 szOID_SUBJECT_KEY_IDENTIFIER
, pCertContext
->pCertInfo
->cExtension
,
332 pCertContext
->pCertInfo
->rgExtension
);
336 CRYPT_DATA_BLOB value
;
337 DWORD size
= sizeof(value
);
339 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
340 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
341 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
345 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
347 CertContext_SetProperty(context
, dwPropId
, 0, &value
);
351 SetLastError(ERROR_INVALID_DATA
);
355 SetLastError(CRYPT_E_NOT_FOUND
);
358 TRACE("returning %d\n", ret
);
362 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
364 DWORD i
, containerLen
, provNameLen
;
365 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
367 info
->pwszContainerName
= (LPWSTR
)data
;
368 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
369 data
+= containerLen
;
371 info
->pwszProvName
= (LPWSTR
)data
;
372 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
375 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
376 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
378 for (i
= 0; i
< info
->cProvParam
; i
++)
380 info
->rgProvParam
[i
].pbData
= data
;
381 data
+= info
->rgProvParam
[i
].cbData
;
385 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
386 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
390 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
395 case CERT_CERT_PROP_ID
:
396 case CERT_CRL_PROP_ID
:
397 case CERT_CTL_PROP_ID
:
398 SetLastError(E_INVALIDARG
);
401 case CERT_ACCESS_STATE_PROP_ID
:
402 if (pCertContext
->hCertStore
)
403 ret
= CertGetStoreProperty(pCertContext
->hCertStore
, dwPropId
,
409 ret
= CertContext_CopyParam(pvData
, pcbData
, &state
, sizeof(state
));
412 case CERT_KEY_PROV_HANDLE_PROP_ID
:
414 CERT_KEY_CONTEXT keyContext
;
415 DWORD size
= sizeof(keyContext
);
417 ret
= CertContext_GetProperty((void *)pCertContext
,
418 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
420 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
421 sizeof(keyContext
.hCryptProv
));
424 case CERT_KEY_PROV_INFO_PROP_ID
:
425 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
428 CRYPT_FixKeyProvInfoPointers(pvData
);
431 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
435 TRACE("returning %d\n", ret
);
439 /* Copies key provider info from from into to, where to is assumed to be a
440 * contiguous buffer of memory large enough for from and all its associated
441 * data, but whose pointers are uninitialized.
442 * Upon return, to contains a contiguous copy of from, packed in the following
444 * - CRYPT_KEY_PROV_INFO
445 * - pwszContainerName
447 * - rgProvParam[0]...
449 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
450 const CRYPT_KEY_PROV_INFO
*from
)
453 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
455 if (from
->pwszContainerName
)
457 to
->pwszContainerName
= (LPWSTR
)nextData
;
458 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
459 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
462 to
->pwszContainerName
= NULL
;
463 if (from
->pwszProvName
)
465 to
->pwszProvName
= (LPWSTR
)nextData
;
466 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
467 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
470 to
->pwszProvName
= NULL
;
471 to
->dwProvType
= from
->dwProvType
;
472 to
->dwFlags
= from
->dwFlags
;
473 to
->cProvParam
= from
->cProvParam
;
474 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
475 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
476 to
->dwKeySpec
= from
->dwKeySpec
;
477 for (i
= 0; i
< to
->cProvParam
; i
++)
479 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
480 sizeof(CRYPT_KEY_PROV_PARAM
));
481 to
->rgProvParam
[i
].pbData
= nextData
;
482 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
483 from
->rgProvParam
[i
].cbData
);
484 nextData
+= from
->rgProvParam
[i
].cbData
;
488 static BOOL
CertContext_SetKeyProvInfoProperty(PCONTEXT_PROPERTY_LIST properties
,
489 const CRYPT_KEY_PROV_INFO
*info
)
493 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
495 if (info
->pwszContainerName
)
496 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
499 if (info
->pwszProvName
)
500 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
503 size
+= containerSize
+ provNameSize
;
504 for (i
= 0; i
< info
->cProvParam
; i
++)
505 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
506 buf
= CryptMemAlloc(size
);
509 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
510 ret
= ContextPropertyList_SetProperty(properties
,
511 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
519 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
520 DWORD dwFlags
, const void *pvData
)
522 PCONTEXT_PROPERTY_LIST properties
=
523 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
526 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
534 case CERT_AUTO_ENROLL_PROP_ID
:
535 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
536 case CERT_DESCRIPTION_PROP_ID
:
537 case CERT_FRIENDLY_NAME_PROP_ID
:
538 case CERT_HASH_PROP_ID
:
539 case CERT_KEY_IDENTIFIER_PROP_ID
:
540 case CERT_MD5_HASH_PROP_ID
:
541 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
542 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
543 case CERT_PVK_FILE_PROP_ID
:
544 case CERT_SIGNATURE_HASH_PROP_ID
:
545 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
546 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
547 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
548 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
549 case CERT_ENROLLMENT_PROP_ID
:
550 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
551 case CERT_RENEWAL_PROP_ID
:
555 const CRYPT_DATA_BLOB
*blob
= pvData
;
557 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
558 blob
->pbData
, blob
->cbData
);
562 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
567 case CERT_DATE_STAMP_PROP_ID
:
569 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
570 pvData
, sizeof(FILETIME
));
573 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
577 case CERT_KEY_CONTEXT_PROP_ID
:
581 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
583 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
585 SetLastError(E_INVALIDARG
);
589 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
590 (const BYTE
*)keyContext
, keyContext
->cbSize
);
594 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
599 case CERT_KEY_PROV_INFO_PROP_ID
:
601 ret
= CertContext_SetKeyProvInfoProperty(properties
, pvData
);
604 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
608 case CERT_KEY_PROV_HANDLE_PROP_ID
:
610 CERT_KEY_CONTEXT keyContext
;
611 DWORD size
= sizeof(keyContext
);
613 ret
= CertContext_GetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
617 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
618 CryptReleaseContext(keyContext
.hCryptProv
, 0);
620 keyContext
.cbSize
= sizeof(keyContext
);
622 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
625 keyContext
.hCryptProv
= 0;
626 keyContext
.dwKeySpec
= AT_SIGNATURE
;
628 ret
= CertContext_SetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
633 FIXME("%d: stub\n", dwPropId
);
637 TRACE("returning %d\n", ret
);
641 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
642 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
646 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
648 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
649 * crashes on most of these, I'll be safer.
654 case CERT_ACCESS_STATE_PROP_ID
:
655 case CERT_CERT_PROP_ID
:
656 case CERT_CRL_PROP_ID
:
657 case CERT_CTL_PROP_ID
:
658 SetLastError(E_INVALIDARG
);
661 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
663 TRACE("returning %d\n", ret
);
667 /* Acquires the private key using the key provider info, retrieving info from
668 * the certificate if info is NULL. The acquired provider is returned in
669 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
671 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
672 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
675 BOOL allocated
= FALSE
, ret
= TRUE
;
679 ret
= CertGetCertificateContextProperty(pCert
,
680 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
683 info
= HeapAlloc(GetProcessHeap(), 0, size
);
686 ret
= CertGetCertificateContextProperty(pCert
,
687 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
692 SetLastError(ERROR_OUTOFMEMORY
);
697 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
701 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
702 info
->pwszProvName
, info
->dwProvType
, 0);
707 for (i
= 0; i
< info
->cProvParam
; i
++)
709 CryptSetProvParam(*phCryptProv
,
710 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
711 info
->rgProvParam
[i
].dwFlags
);
713 *pdwKeySpec
= info
->dwKeySpec
;
716 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
719 HeapFree(GetProcessHeap(), 0, info
);
723 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
724 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
725 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
727 BOOL ret
= FALSE
, cache
= FALSE
;
728 PCRYPT_KEY_PROV_INFO info
= NULL
;
729 CERT_KEY_CONTEXT keyContext
;
732 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
733 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
735 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
739 ret
= CertGetCertificateContextProperty(pCert
,
740 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
743 info
= HeapAlloc(GetProcessHeap(), 0, size
);
744 ret
= CertGetCertificateContextProperty(pCert
,
745 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
747 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
750 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
755 size
= sizeof(keyContext
);
756 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
760 *phCryptProv
= keyContext
.hCryptProv
;
762 *pdwKeySpec
= keyContext
.dwKeySpec
;
763 if (pfCallerFreeProv
)
764 *pfCallerFreeProv
= !cache
;
769 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
770 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
773 *phCryptProv
= keyContext
.hCryptProv
;
775 *pdwKeySpec
= keyContext
.dwKeySpec
;
778 keyContext
.cbSize
= sizeof(keyContext
);
779 if (CertSetCertificateContextProperty(pCert
,
780 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
782 if (pfCallerFreeProv
)
783 *pfCallerFreeProv
= FALSE
;
788 if (pfCallerFreeProv
)
789 *pfCallerFreeProv
= TRUE
;
793 HeapFree(GetProcessHeap(), 0, info
);
797 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
798 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
801 BOOL matches
= FALSE
;
803 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
804 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
808 /* Need to sign something to verify the sig. What to sign? Why not
809 * the certificate itself?
811 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
812 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
813 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
815 BYTE
*certEncoded
= CryptMemAlloc(size
);
819 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
820 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
821 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
822 NULL
, certEncoded
, &size
))
824 if (size
== pCert
->cbCertEncoded
&&
825 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
828 CryptMemFree(certEncoded
);
831 CryptReleaseContext(csp
, 0);
836 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
837 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
839 CRYPT_KEY_PROV_INFO copy
;
840 WCHAR containerW
[MAX_PATH
];
841 BOOL matches
= FALSE
;
843 MultiByteToWideChar(CP_ACP
, 0, container
, -1,
844 containerW
, sizeof(containerW
) / sizeof(containerW
[0]));
845 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
846 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
849 memcpy(©
, keyProvInfo
, sizeof(copy
));
850 copy
.pwszContainerName
= containerW
;
851 matches
= key_prov_info_matches_cert(pCert
, ©
);
854 keyProvInfo
->pwszContainerName
=
855 CryptMemAlloc((strlenW(containerW
) + 1) * sizeof(WCHAR
));
856 if (keyProvInfo
->pwszContainerName
)
858 strcpyW(keyProvInfo
->pwszContainerName
, containerW
);
859 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
867 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
868 * private key matches pCert's public key. Upon success, updates keyProvInfo
869 * with the matching container's info (free keyProvInfo.pwszContainerName upon
871 * Returns TRUE if found, FALSE if not.
873 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
874 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
876 HCRYPTPROV defProvider
;
877 BOOL ret
, found
= FALSE
;
878 char containerA
[MAX_PATH
];
880 assert(keyProvInfo
->pwszContainerName
== NULL
);
881 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
882 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
883 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
885 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
887 while (ret
&& !found
)
889 DWORD size
= sizeof(containerA
);
891 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
892 (BYTE
*)containerA
, &size
, enumFlags
);
894 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
895 if (enumFlags
& CRYPT_FIRST
)
897 enumFlags
&= ~CRYPT_FIRST
;
898 enumFlags
|= CRYPT_NEXT
;
901 CryptReleaseContext(defProvider
, 0);
906 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
908 BOOL found
= FALSE
, ret
= TRUE
;
909 DWORD index
= 0, cbProvName
= 0;
910 CRYPT_KEY_PROV_INFO keyProvInfo
;
912 TRACE("(%p, %08x)\n", pCert
, dwFlags
);
914 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
915 while (ret
&& !found
)
919 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
923 if (size
<= cbProvName
)
924 ret
= CryptEnumProvidersW(index
, NULL
, 0,
925 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
928 CryptMemFree(keyProvInfo
.pwszProvName
);
929 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
930 if (keyProvInfo
.pwszProvName
)
933 ret
= CryptEnumProvidersW(index
, NULL
, 0,
934 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
937 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
938 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
939 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
940 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
941 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
943 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
944 found
= find_key_prov_info_in_provider(pCert
,
949 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
950 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
951 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
953 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
954 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
955 found
= find_key_prov_info_in_provider(pCert
,
968 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
970 CryptMemFree(keyProvInfo
.pwszProvName
);
971 CryptMemFree(keyProvInfo
.pwszContainerName
);
975 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
977 BOOL matches
= FALSE
;
980 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
983 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
987 if (CertGetCertificateContextProperty(pCert
,
988 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
989 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
990 CryptMemFree(keyProvInfo
);
996 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
997 DWORD dwFlags
, void *pvReserved
)
999 BOOL matches
= FALSE
;
1001 TRACE("(%p, %08x, %p)\n", pCert
, dwFlags
, pvReserved
);
1003 matches
= cert_prov_info_matches_cert(pCert
);
1005 matches
= find_matching_provider(pCert
, dwFlags
);
1009 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1010 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1014 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1016 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1017 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1018 &pCertId2
->SerialNumber
);
1019 TRACE("returning %d\n", ret
);
1023 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1024 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1028 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1030 if (pCertName1
->cbData
== pCertName2
->cbData
)
1032 if (pCertName1
->cbData
)
1033 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1034 pCertName1
->cbData
);
1040 TRACE("returning %d\n", ret
);
1044 /* Returns the number of significant bytes in pInt, where a byte is
1045 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1046 * for negative numbers. pInt is assumed to be little-endian.
1048 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1050 DWORD ret
= pInt
->cbData
;
1054 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1056 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1064 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1065 PCRYPT_INTEGER_BLOB pInt2
)
1070 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1072 cb1
= CRYPT_significantBytes(pInt1
);
1073 cb2
= CRYPT_significantBytes(pInt2
);
1077 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1083 TRACE("returning %d\n", ret
);
1087 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1088 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1092 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1094 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1096 case 0: /* Seems to mean "raw binary bits" */
1097 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1098 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1100 if (pPublicKey2
->PublicKey
.cbData
)
1101 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1102 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1110 WARN("Unknown encoding type %08x\n", dwCertEncodingType
);
1112 case X509_ASN_ENCODING
:
1114 BLOBHEADER
*pblob1
, *pblob2
;
1117 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1118 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1121 pblob1
= CryptMemAlloc(length
);
1122 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1123 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1124 0, pblob1
, &length
))
1126 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1127 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1130 pblob2
= CryptMemAlloc(length
);
1131 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1132 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1133 0, pblob2
, &length
))
1135 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1136 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1137 *pk2
= (LPVOID
)(pblob2
+ 1);
1138 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1139 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1141 CryptMemFree(pblob2
);
1144 CryptMemFree(pblob1
);
1153 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1154 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1158 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
1160 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1162 SetLastError(ERROR_FILE_NOT_FOUND
);
1165 if (pPublicKey
->Algorithm
.pszObjId
&&
1166 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1168 FIXME("unimplemented for DH public keys\n");
1169 SetLastError(CRYPT_E_ASN1_BADTAG
);
1175 BOOL ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1176 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1177 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1182 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1184 len
= rsaPubKey
->bitlen
;
1191 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1192 DWORD dwFlags
, const void *pvPara
);
1194 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1195 DWORD dwFlags
, const void *pvPara
)
1199 DWORD size
= sizeof(hash
);
1201 ret
= CertGetCertificateContextProperty(pCertContext
,
1202 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1205 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1207 if (size
== pHash
->cbData
)
1208 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1215 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1216 DWORD dwFlags
, const void *pvPara
)
1220 DWORD size
= sizeof(hash
);
1222 ret
= CertGetCertificateContextProperty(pCertContext
,
1223 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1226 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1228 if (size
== pHash
->cbData
)
1229 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1236 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1237 DWORD dwFlags
, const void *pvPara
)
1239 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1242 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1243 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1245 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1246 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1251 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1252 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1254 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1257 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1258 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1262 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1263 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1265 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1268 /* Matching serial number and subject match.. */
1269 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1270 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1272 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1273 &pCertInfo
->SerialNumber
);
1276 /* failing that, if the serial number and issuer match, we match */
1277 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1278 &pCertInfo
->SerialNumber
);
1280 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1281 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1283 TRACE("returning %d\n", ret
);
1287 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1288 DWORD dwFlags
, const void *pvPara
)
1290 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1293 switch (id
->dwIdChoice
)
1295 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1296 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1297 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1299 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1300 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1302 case CERT_ID_SHA1_HASH
:
1303 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1306 case CERT_ID_KEY_IDENTIFIER
:
1310 ret
= CertGetCertificateContextProperty(pCertContext
,
1311 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1312 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1314 LPBYTE buf
= CryptMemAlloc(size
);
1318 CertGetCertificateContextProperty(pCertContext
,
1319 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1320 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1335 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1336 DWORD dwFlags
, const void *pvPara
)
1338 PCCERT_CONTEXT toCompare
= pvPara
;
1339 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1340 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1343 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1344 DWORD dwFlags
, const void *pvPara
)
1346 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1350 ret
= CertGetCertificateContextProperty(pCertContext
,
1351 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1352 if (ret
&& size
== hash
->cbData
)
1354 LPBYTE buf
= CryptMemAlloc(size
);
1358 CertGetCertificateContextProperty(pCertContext
,
1359 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1360 ret
= !memcmp(buf
, hash
->pbData
, size
);
1369 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1370 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1373 BOOL matches
= FALSE
;
1378 ret
= CertEnumCertificatesInStore(store
, ret
);
1380 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1381 } while (ret
!= NULL
&& !matches
);
1385 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1386 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1388 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1389 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1391 return CertEnumCertificatesInStore(store
, prev
);
1394 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1395 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1398 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1399 PCERT_EXTENSION ext
;
1402 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1403 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1405 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1407 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1408 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1409 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1415 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1417 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1418 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1419 sizeof(CERT_NAME_BLOB
));
1420 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1421 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1423 else if (info
->KeyId
.cbData
)
1425 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1426 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1431 found
= cert_compare_certs_in_store(store
, prev
,
1432 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1436 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1437 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1439 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1441 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1442 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1443 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1449 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1450 info
->AuthorityCertSerialNumber
.cbData
)
1452 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1455 for (i
= 0; !directoryName
&&
1456 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1457 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1458 == CERT_ALT_NAME_DIRECTORY_NAME
)
1460 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1463 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1464 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1465 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1466 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1467 &info
->AuthorityCertSerialNumber
,
1468 sizeof(CRYPT_INTEGER_BLOB
));
1472 FIXME("no supported name type in authority key id2\n");
1476 else if (info
->KeyId
.cbData
)
1478 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1479 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1484 found
= cert_compare_certs_in_store(store
, prev
,
1485 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1490 found
= cert_compare_certs_in_store(store
, prev
,
1491 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1492 dwFlags
, &subject
->pCertInfo
->Issuer
);
1496 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1497 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1499 PCERT_NAME_BLOB name
;
1503 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1504 name
= &pCertContext
->pCertInfo
->Subject
;
1506 name
= &pCertContext
->pCertInfo
->Issuer
;
1507 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1508 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1511 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1517 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1518 CERT_SIMPLE_NAME_STR
, str
, len
);
1519 for (ptr
= str
; *ptr
; ptr
++)
1520 *ptr
= tolowerW(*ptr
);
1521 if (strstrW(str
, pvPara
))
1529 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1530 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1532 PCCERT_CONTEXT found
= NULL
;
1534 TRACE("%s\n", debugstr_a(pvPara
));
1538 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1539 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1545 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1546 for (ptr
= str
; *ptr
; ptr
++)
1547 *ptr
= tolowerW(*ptr
);
1548 found
= cert_compare_certs_in_store(store
, prev
,
1549 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1554 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1558 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1559 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1561 PCCERT_CONTEXT found
= NULL
;
1563 TRACE("%s\n", debugstr_w(pvPara
));
1567 DWORD len
= strlenW(pvPara
);
1568 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1575 for (src
= pvPara
, dst
= str
; *src
; src
++, dst
++)
1576 *dst
= tolowerW(*src
);
1578 found
= cert_compare_certs_in_store(store
, prev
,
1579 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1584 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1588 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1589 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1590 PCCERT_CONTEXT pPrevCertContext
)
1593 CertFindFunc find
= NULL
;
1594 CertCompareFunc compare
= NULL
;
1596 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1597 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1599 switch (dwType
>> CERT_COMPARE_SHIFT
)
1601 case CERT_COMPARE_ANY
:
1602 find
= find_cert_any
;
1604 case CERT_COMPARE_MD5_HASH
:
1605 compare
= compare_cert_by_md5_hash
;
1607 case CERT_COMPARE_SHA1_HASH
:
1608 compare
= compare_cert_by_sha1_hash
;
1610 case CERT_COMPARE_NAME
:
1611 compare
= compare_cert_by_name
;
1613 case CERT_COMPARE_PUBLIC_KEY
:
1614 compare
= compare_cert_by_public_key
;
1616 case CERT_COMPARE_NAME_STR_A
:
1617 find
= find_cert_by_name_str_a
;
1619 case CERT_COMPARE_NAME_STR_W
:
1620 find
= find_cert_by_name_str_w
;
1622 case CERT_COMPARE_SUBJECT_CERT
:
1623 compare
= compare_cert_by_subject_cert
;
1625 case CERT_COMPARE_CERT_ID
:
1626 compare
= compare_cert_by_cert_id
;
1628 case CERT_COMPARE_ISSUER_OF
:
1629 find
= find_cert_by_issuer
;
1631 case CERT_COMPARE_EXISTING
:
1632 compare
= compare_existing_cert
;
1634 case CERT_COMPARE_SIGNATURE_HASH
:
1635 compare
= compare_cert_by_signature_hash
;
1638 FIXME("find type %08x unimplemented\n", dwType
);
1642 ret
= find(hCertStore
, dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1644 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1645 compare
, dwType
, dwFlags
, pvPara
);
1649 SetLastError(CRYPT_E_NOT_FOUND
);
1650 TRACE("returning %p\n", ret
);
1654 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1655 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1657 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1661 SetLastError(E_INVALIDARG
);
1664 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1665 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1668 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1669 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1671 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1672 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1674 if (*pdwFlags
& ~supportedFlags
)
1676 SetLastError(E_INVALIDARG
);
1679 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1682 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1685 /* FIXME: what if the CRL has expired? */
1688 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1689 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1690 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1693 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1695 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1697 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1698 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1700 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1702 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1703 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1704 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1705 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1710 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1711 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1716 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1717 pPrevIssuerContext
, *pdwFlags
);
1719 if (!pSubjectContext
)
1721 SetLastError(E_INVALIDARG
);
1725 ret
= CertFindCertificateInStore(hCertStore
,
1726 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1727 pSubjectContext
, pPrevIssuerContext
);
1730 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1733 CertFreeCertificateContext(ret
);
1737 TRACE("returning %p\n", ret
);
1741 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1746 } OLD_CERT_REVOCATION_STATUS
, *POLD_CERT_REVOCATION_STATUS
;
1748 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1749 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
1751 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1752 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1753 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1757 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1758 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1760 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1761 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1763 SetLastError(E_INVALIDARG
);
1768 static HCRYPTOIDFUNCSET set
= NULL
;
1772 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
1773 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
1779 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
1784 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
1788 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
1792 for (ptr
= dllList
; ret
&& *ptr
;
1793 ptr
+= lstrlenW(ptr
) + 1)
1795 CertVerifyRevocationFunc func
;
1796 HCRYPTOIDFUNCADDR hFunc
;
1798 ret
= CryptGetDefaultOIDFunctionAddress(set
,
1799 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
1802 ret
= func(dwEncodingType
, dwRevType
, cContext
,
1803 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1804 CryptFreeOIDFunctionAddress(hFunc
, 0);
1808 CryptMemFree(dllList
);
1812 SetLastError(ERROR_OUTOFMEMORY
);
1823 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
1824 CRYPT_ATTRIBUTE rgAttr
[])
1826 PCRYPT_ATTRIBUTE ret
= NULL
;
1829 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
1835 SetLastError(ERROR_INVALID_PARAMETER
);
1839 for (i
= 0; !ret
&& i
< cAttr
; i
++)
1840 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
1845 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
1846 CERT_EXTENSION rgExtensions
[])
1848 PCERT_EXTENSION ret
= NULL
;
1851 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
1857 SetLastError(ERROR_INVALID_PARAMETER
);
1861 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
1862 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
1863 rgExtensions
[i
].pszObjId
))
1864 ret
= &rgExtensions
[i
];
1868 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
1870 PCERT_RDN_ATTR ret
= NULL
;
1873 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
1877 SetLastError(ERROR_INVALID_PARAMETER
);
1881 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
1882 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
1883 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
1884 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
1885 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
1889 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
1890 const CERT_RDN_ATTR
*attr
)
1895 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
1897 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
1899 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
1901 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
1904 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
1907 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1908 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
1910 if (attr
->Value
.cbData
!=
1911 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1913 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1914 match
= !strncmpiW(nameStr
, attrStr
,
1915 attr
->Value
.cbData
/ sizeof(WCHAR
));
1917 match
= !strncmpW(nameStr
, attrStr
,
1918 attr
->Value
.cbData
/ sizeof(WCHAR
));
1919 TRACE("%s : %s => %d\n",
1920 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1921 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1927 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1928 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
1930 if (attr
->Value
.cbData
!=
1931 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1933 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1934 match
= !strncasecmp(nameStr
, attrStr
,
1935 attr
->Value
.cbData
);
1937 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
1938 TRACE("%s : %s => %d\n",
1939 debugstr_an(nameStr
, attr
->Value
.cbData
),
1940 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
1948 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
1949 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
1951 CERT_NAME_INFO
*name
;
1956 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
1959 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
1961 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
1962 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
1966 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
1967 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
1969 SetLastError(CRYPT_E_NO_MATCH
);
1975 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
1976 PCERT_INFO pCertInfo
)
1983 GetSystemTimeAsFileTime(&fileTime
);
1984 pTimeToVerify
= &fileTime
;
1986 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
1988 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
1995 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
1996 PCERT_INFO pIssuerInfo
)
1998 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
2000 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
2001 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
2004 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2005 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2006 DWORD
*pcbComputedHash
)
2009 HCRYPTHASH hHash
= 0;
2011 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2012 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2015 hCryptProv
= CRYPT_GetDefaultProvider();
2020 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2023 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2025 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2026 pcbComputedHash
, 0);
2027 CryptDestroyHash(hHash
);
2033 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2034 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2035 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2038 HCRYPTHASH hHash
= 0;
2040 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2041 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2044 hCryptProv
= CRYPT_GetDefaultProvider();
2047 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2049 SetLastError(ERROR_FILE_NOT_FOUND
);
2057 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2058 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2059 (LPBYTE
)&buf
, &size
);
2062 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2065 ret
= CryptHashData(hHash
, buf
, size
, 0);
2067 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2068 pcbComputedHash
, 0);
2069 CryptDestroyHash(hHash
);
2077 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2078 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2079 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2082 CERT_SIGNED_CONTENT_INFO
*info
;
2085 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv
, dwCertEncodingType
,
2086 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2088 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2089 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2092 PCCRYPT_OID_INFO oidInfo
;
2096 hCryptProv
= CRYPT_GetDefaultProvider();
2097 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2098 info
->SignatureAlgorithm
.pszObjId
, 0);
2101 SetLastError(NTE_BAD_ALGID
);
2106 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2109 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2110 info
->ToBeSigned
.cbData
, 0);
2112 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2113 pcbComputedHash
, 0);
2114 CryptDestroyHash(hHash
);
2122 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2123 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2124 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2125 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2128 PCCRYPT_OID_INFO info
;
2131 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
2132 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2133 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2135 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2136 pSignatureAlgorithm
->pszObjId
, 0);
2139 SetLastError(NTE_BAD_ALGID
);
2142 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2145 hCryptProv
= CRYPT_GetDefaultProvider();
2146 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2149 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2150 cbEncodedToBeSigned
, 0);
2152 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2154 CryptDestroyHash(hHash
);
2161 SetLastError(ERROR_INVALID_PARAMETER
);
2166 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2169 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2170 cbEncodedToBeSigned
, 0);
2172 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2174 CryptDestroyHash(hHash
);
2181 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2182 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2183 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2184 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2187 DWORD encodedSize
, hashSize
;
2189 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2190 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2191 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2193 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2194 NULL
, &encodedSize
);
2197 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2201 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2202 pvStructInfo
, encoded
, &encodedSize
);
2205 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2206 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2207 pvHashAuxInfo
, NULL
, &hashSize
);
2210 PBYTE hash
= CryptMemAlloc(hashSize
);
2214 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2215 dwCertEncodingType
, encoded
, encodedSize
,
2216 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2219 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2221 info
.ToBeSigned
.cbData
= encodedSize
;
2222 info
.ToBeSigned
.pbData
= encoded
;
2223 memcpy(&info
.SignatureAlgorithm
,
2224 pSignatureAlgorithm
,
2225 sizeof(info
.SignatureAlgorithm
));
2226 info
.Signature
.cbData
= hashSize
;
2227 info
.Signature
.pbData
= hash
;
2228 info
.Signature
.cUnusedBits
= 0;
2229 ret
= CryptEncodeObject(dwCertEncodingType
,
2230 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2236 CryptMemFree(encoded
);
2242 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2243 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2244 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2246 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2247 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, (void *)pbEncoded
,
2248 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2251 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
,
2252 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pubKeyInfo
,
2253 const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2257 PCCRYPT_OID_INFO info
;
2258 ALG_ID pubKeyID
, hashID
;
2260 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2261 signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2262 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2264 SetLastError(NTE_BAD_ALGID
);
2267 hashID
= info
->u
.Algid
;
2268 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2269 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2272 /* Load the default provider if necessary */
2274 hCryptProv
= CRYPT_GetDefaultProvider();
2275 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2276 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2281 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2284 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2285 signedCert
->ToBeSigned
.cbData
, 0);
2287 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2288 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2289 CryptDestroyHash(hash
);
2291 CryptDestroyKey(key
);
2296 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2297 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2298 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2301 CRYPT_DATA_BLOB subjectBlob
;
2303 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
2304 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2305 dwFlags
, pvReserved
);
2307 switch (dwSubjectType
)
2309 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2311 PCRYPT_DATA_BLOB blob
= pvSubject
;
2313 subjectBlob
.pbData
= blob
->pbData
;
2314 subjectBlob
.cbData
= blob
->cbData
;
2317 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2319 PCERT_CONTEXT context
= pvSubject
;
2321 subjectBlob
.pbData
= context
->pbCertEncoded
;
2322 subjectBlob
.cbData
= context
->cbCertEncoded
;
2325 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2327 PCRL_CONTEXT context
= pvSubject
;
2329 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2330 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2334 SetLastError(E_INVALIDARG
);
2340 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2343 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2344 subjectBlob
.pbData
, subjectBlob
.cbData
,
2345 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2346 &signedCert
, &size
);
2349 switch (dwIssuerType
)
2351 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2352 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2353 dwCertEncodingType
, pvIssuer
,
2356 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2357 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2359 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2362 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2363 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2366 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2369 SetLastError(E_INVALIDARG
);
2374 FIXME("unimplemented for NULL signer\n");
2375 SetLastError(E_INVALIDARG
);
2380 SetLastError(E_INVALIDARG
);
2383 LocalFree(signedCert
);
2389 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2390 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
2392 PCERT_EXTENSION ext
;
2395 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
2398 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
2399 pCertInfo
->rgExtension
);
2402 CRYPT_BIT_BLOB usage
;
2403 DWORD size
= sizeof(usage
);
2405 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
2406 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2410 if (cbKeyUsage
< usage
.cbData
)
2414 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
2415 if (cbKeyUsage
> usage
.cbData
)
2416 memset(pbKeyUsage
+ usage
.cbData
, 0,
2417 cbKeyUsage
- usage
.cbData
);
2426 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
2427 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
2429 PCERT_ENHKEY_USAGE usage
= NULL
;
2433 if (!pCertContext
|| !pcbUsage
)
2435 SetLastError(ERROR_INVALID_PARAMETER
);
2439 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
2441 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
2445 if (CertGetCertificateContextProperty(pCertContext
,
2446 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
2448 LPBYTE buf
= CryptMemAlloc(propSize
);
2452 if (CertGetCertificateContextProperty(pCertContext
,
2453 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
2455 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2456 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
2457 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2463 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
2465 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
2466 pCertContext
->pCertInfo
->cExtension
,
2467 pCertContext
->pCertInfo
->rgExtension
);
2471 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2472 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2473 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2478 /* If a particular location is specified, this should fail. Otherwise
2479 * it should succeed with an empty usage. (This is true on Win2k and
2480 * later, which we emulate.)
2484 SetLastError(CRYPT_E_NOT_FOUND
);
2488 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
2494 *pcbUsage
= bytesNeeded
;
2495 else if (*pcbUsage
< bytesNeeded
)
2497 SetLastError(ERROR_MORE_DATA
);
2498 *pcbUsage
= bytesNeeded
;
2503 *pcbUsage
= bytesNeeded
;
2507 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
2508 sizeof(CERT_ENHKEY_USAGE
) +
2509 usage
->cUsageIdentifier
* sizeof(LPSTR
));
2511 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
2512 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
2513 sizeof(CERT_ENHKEY_USAGE
));
2514 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2516 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2517 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2518 nextOID
+= strlen(nextOID
) + 1;
2522 pUsage
->cUsageIdentifier
= 0;
2527 TRACE("returning %d\n", ret
);
2531 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
2532 PCERT_ENHKEY_USAGE pUsage
)
2536 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
2540 CRYPT_DATA_BLOB blob
= { 0, NULL
};
2542 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
2543 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
2546 ret
= CertSetCertificateContextProperty(pCertContext
,
2547 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
2548 LocalFree(blob
.pbData
);
2552 ret
= CertSetCertificateContextProperty(pCertContext
,
2553 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
2557 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2558 LPCSTR pszUsageIdentifier
)
2563 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2565 if (CertGetEnhancedKeyUsage(pCertContext
,
2566 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
2568 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
2572 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2573 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
2577 BOOL exists
= FALSE
;
2579 /* Make sure usage doesn't already exist */
2580 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
2582 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
2583 pszUsageIdentifier
))
2588 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
2589 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2595 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
2596 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
2597 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
2598 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
2599 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2601 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2602 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2603 nextOID
+= strlen(nextOID
) + 1;
2605 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2606 strcpy(nextOID
, pszUsageIdentifier
);
2607 newUsage
->cUsageIdentifier
= i
+ 1;
2608 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
2609 CryptMemFree(newUsage
);
2615 CryptMemFree(usage
);
2622 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
2623 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2627 usage
->rgpszUsageIdentifier
=
2628 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
2629 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
2630 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
2631 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
2632 usage
->cUsageIdentifier
= 1;
2633 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
2634 CryptMemFree(usage
);
2642 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2643 LPCSTR pszUsageIdentifier
)
2647 CERT_ENHKEY_USAGE usage
;
2649 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2651 size
= sizeof(usage
);
2652 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2653 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
2654 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2656 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2660 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2661 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
2664 if (pUsage
->cUsageIdentifier
)
2669 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
2671 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
2672 pszUsageIdentifier
))
2674 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
2675 pUsage
->rgpszUsageIdentifier
[i
] =
2676 pUsage
->rgpszUsageIdentifier
[i
+ 1];
2678 pUsage
->cUsageIdentifier
--;
2679 /* Remove the usage if it's empty */
2680 if (pUsage
->cUsageIdentifier
)
2681 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
2683 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
2686 CryptMemFree(pUsage
);
2693 /* it fit in an empty usage, therefore there's nothing to remove */
2705 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
2707 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
2709 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2711 if (indexIndex
+ 1 > field
->cIndexes
)
2713 if (field
->cIndexes
)
2714 field
->indexes
= CryptMemRealloc(field
->indexes
,
2715 (indexIndex
+ 1) * sizeof(DWORD
));
2717 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
2720 field
->indexes
[indexIndex
] = 0;
2721 field
->cIndexes
= indexIndex
+ 1;
2725 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
2728 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
2731 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2733 assert(field
->cIndexes
);
2734 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
2738 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
2739 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
2742 DWORD i
, cbOIDs
= 0;
2743 BOOL allUsagesValid
= TRUE
;
2744 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
2746 TRACE("(%d, %p, %d, %p, %d)\n", cCerts
, rghCerts
, *cNumOIDs
,
2749 for (i
= 0; i
< cCerts
; i
++)
2751 CERT_ENHKEY_USAGE usage
;
2752 DWORD size
= sizeof(usage
);
2754 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
2755 /* Success is deliberately ignored: it implies all usages are valid */
2756 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2758 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2760 allUsagesValid
= FALSE
;
2763 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
2766 if (!validUsages
.cUsageIdentifier
)
2770 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
2771 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
2772 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2773 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
2775 validUsages
.rgpszUsageIdentifier
=
2776 CryptMemAlloc(cbOIDs
);
2777 if (validUsages
.rgpszUsageIdentifier
)
2779 LPSTR nextOID
= (LPSTR
)
2780 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
2781 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2783 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2785 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
2786 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
2787 pUsage
->rgpszUsageIdentifier
[j
]);
2788 nextOID
+= lstrlenA(nextOID
) + 1;
2794 struct BitField validIndexes
= { 0, NULL
};
2795 DWORD j
, k
, numRemoved
= 0;
2797 /* Merge: build a bitmap of all the indexes of
2798 * validUsages.rgpszUsageIdentifier that are in pUsage.
2800 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
2802 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
2804 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
2805 validUsages
.rgpszUsageIdentifier
[k
]))
2807 CRYPT_SetBitInField(&validIndexes
, k
);
2812 /* Merge by removing from validUsages those that are
2813 * not in the bitmap.
2815 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2817 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
2819 if (j
< validUsages
.cUsageIdentifier
- 1)
2821 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
2822 &validUsages
.rgpszUsageIdentifier
[j
+
2824 (validUsages
.cUsageIdentifier
- numRemoved
2825 - j
- 1) * sizeof(LPSTR
));
2827 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
2829 validUsages
.cUsageIdentifier
--;
2833 validUsages
.cUsageIdentifier
--;
2836 CryptMemFree(validIndexes
.indexes
);
2839 CryptMemFree(pUsage
);
2851 *cNumOIDs
= validUsages
.cUsageIdentifier
;
2854 else if (*pcbOIDs
< cbOIDs
)
2857 SetLastError(ERROR_MORE_DATA
);
2862 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
2863 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2866 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
2868 rghOIDs
[i
] = nextOID
;
2869 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
2870 nextOID
+= lstrlenA(nextOID
) + 1;
2874 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
2875 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
2876 TRACE("returning %d\n", ret
);
2880 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
2881 * pInfo is NULL, from the attributes of hProv.
2883 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
2884 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
2886 CRYPT_KEY_PROV_INFO info
= { 0 };
2894 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
2897 LPSTR szContainer
= CryptMemAlloc(size
);
2901 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
2902 (BYTE
*)szContainer
, &size
, 0);
2905 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2909 info
.pwszContainerName
= CryptMemAlloc(len
*
2911 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2912 info
.pwszContainerName
, len
);
2915 CryptMemFree(szContainer
);
2918 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
2921 LPSTR szProvider
= CryptMemAlloc(size
);
2925 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
2929 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2933 info
.pwszProvName
= CryptMemAlloc(len
*
2935 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2936 info
.pwszProvName
, len
);
2939 CryptMemFree(szProvider
);
2942 size
= sizeof(info
.dwKeySpec
);
2943 /* in case no CRYPT_KEY_PROV_INFO given,
2944 * we always use AT_SIGNATURE key spec
2946 info
.dwKeySpec
= AT_SIGNATURE
;
2947 size
= sizeof(info
.dwProvType
);
2948 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
2951 info
.dwProvType
= PROV_RSA_FULL
;
2955 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
2960 CryptMemFree(info
.pwszContainerName
);
2961 CryptMemFree(info
.pwszProvName
);
2965 /* Creates a signed certificate context from the unsigned, encoded certificate
2966 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
2968 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
2969 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
2971 PCCERT_CONTEXT context
= NULL
;
2975 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2976 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
2979 LPBYTE sig
= CryptMemAlloc(sigSize
);
2981 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2982 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
2985 CERT_SIGNED_CONTENT_INFO signedInfo
;
2986 BYTE
*encodedSignedCert
= NULL
;
2987 DWORD encodedSignedCertSize
= 0;
2989 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
2990 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
2991 memcpy(&signedInfo
.SignatureAlgorithm
, sigAlgo
,
2992 sizeof(signedInfo
.SignatureAlgorithm
));
2993 signedInfo
.Signature
.cbData
= sigSize
;
2994 signedInfo
.Signature
.pbData
= sig
;
2995 signedInfo
.Signature
.cUnusedBits
= 0;
2996 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
2997 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2998 &encodedSignedCert
, &encodedSignedCertSize
);
3001 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3002 encodedSignedCert
, encodedSignedCertSize
);
3003 LocalFree(encodedSignedCert
);
3011 /* Copies data from the parameters into info, where:
3012 * pSerialNumber: The serial number. Must not be NULL.
3013 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3015 * pSignatureAlgorithm: Optional.
3016 * pStartTime: The starting time of the certificate. If NULL, the current
3017 * system time is used.
3018 * pEndTime: The ending time of the certificate. If NULL, one year past the
3019 * starting time is used.
3020 * pubKey: The public key of the certificate. Must not be NULL.
3021 * pExtensions: Extensions to be included with the certificate. Optional.
3023 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3024 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3025 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3026 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3027 const CERT_EXTENSIONS
*pExtensions
)
3029 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3032 assert(pSerialNumber
);
3033 assert(pSubjectIssuerBlob
);
3036 if (pExtensions
&& pExtensions
->cExtension
)
3037 info
->dwVersion
= CERT_V3
;
3039 info
->dwVersion
= CERT_V1
;
3040 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3041 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3042 if (pSignatureAlgorithm
)
3043 memcpy(&info
->SignatureAlgorithm
, pSignatureAlgorithm
,
3044 sizeof(info
->SignatureAlgorithm
));
3047 info
->SignatureAlgorithm
.pszObjId
= oid
;
3048 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3049 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3051 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3052 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3054 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3056 GetSystemTimeAsFileTime(&info
->NotBefore
);
3058 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3063 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3066 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3069 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3070 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3071 memcpy(&info
->SubjectPublicKeyInfo
, pubKey
,
3072 sizeof(info
->SubjectPublicKeyInfo
));
3075 info
->cExtension
= pExtensions
->cExtension
;
3076 info
->rgExtension
= pExtensions
->rgExtension
;
3080 info
->cExtension
= 0;
3081 info
->rgExtension
= NULL
;
3085 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3086 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3087 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3089 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3091 HCRYPTPROV hProv
= 0;
3092 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
3096 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3098 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3100 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3101 rpcrt
, "RpcStringFreeA");
3103 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3106 RPC_STATUS status
= uuidCreate(&uuid
);
3108 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3110 unsigned char *uuidStr
;
3112 status
= uuidToString(&uuid
, &uuidStr
);
3113 if (status
== RPC_S_OK
)
3115 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3116 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3122 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3124 CryptDestroyKey(key
);
3126 rpcStringFree(&uuidStr
);
3135 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3136 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3137 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3138 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3139 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3141 PCCERT_CONTEXT context
= NULL
;
3142 BOOL ret
, releaseContext
= FALSE
;
3143 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3144 DWORD pubKeySize
= 0,dwKeySpec
= AT_SIGNATURE
;
3146 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
3147 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3148 pExtensions
, pExtensions
);
3150 if(!pSubjectIssuerBlob
)
3152 SetLastError(ERROR_INVALID_PARAMETER
);
3160 hProv
= CRYPT_CreateKeyProv();
3161 releaseContext
= TRUE
;
3163 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3165 SetLastError(NTE_BAD_FLAGS
);
3171 /* acquire the context using the given information*/
3172 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3173 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3174 pKeyProvInfo
->dwFlags
);
3177 if(GetLastError() != NTE_BAD_KEYSET
)
3179 /* create the key set */
3180 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3181 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3182 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3186 dwKeySpec
= pKeyProvInfo
->dwKeySpec
;
3187 /* check if the key is here */
3188 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3191 if (NTE_NO_KEY
== GetLastError())
3192 { /* generate the key */
3193 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3197 CryptReleaseContext(hProv
,0);
3198 SetLastError(NTE_BAD_KEYSET
);
3202 CryptDestroyKey(hKey
);
3203 releaseContext
= TRUE
;
3206 else if (pKeyProvInfo
)
3208 SetLastError(ERROR_INVALID_PARAMETER
);
3212 CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3214 pubKey
= CryptMemAlloc(pubKeySize
);
3217 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3218 pubKey
, &pubKeySize
);
3221 CERT_INFO info
= { 0 };
3222 CRYPT_DER_BLOB blob
= { 0, NULL
};
3224 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3226 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3227 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3228 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3229 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3230 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3234 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3235 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3236 &info
.SignatureAlgorithm
);
3238 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3239 blob
.pbData
, blob
.cbData
);
3240 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3241 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3242 LocalFree(blob
.pbData
);
3245 CryptMemFree(pubKey
);
3248 CryptReleaseContext(hProv
, 0);
3252 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3253 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3254 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3255 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3257 FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType
,
3258 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3259 pVerifyUsageStatus
);
3260 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3264 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3265 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3266 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3268 TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType
, dwEncodingType
,
3269 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3273 FIXME("dwFlags 0x%08x not handled\n", dwFlags
);
3278 FIXME("pCreatePara not handled\n");
3282 switch (dwContextType
)
3284 case CERT_STORE_CERTIFICATE_CONTEXT
:
3285 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3286 case CERT_STORE_CRL_CONTEXT
:
3287 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3288 case CERT_STORE_CTL_CONTEXT
:
3289 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3291 WARN("unknown context type: 0x%x\n", dwContextType
);