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
25 #define WIN32_NO_STATUS
29 #define CRYPT_OID_INFO_HAS_EXTRA_FIELDS
35 #include "wine/debug.h"
36 #include "crypt32_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
40 /* Internal version of CertGetCertificateContextProperty that gets properties
41 * directly from the context (or the context it's linked to, depending on its
42 * type.) Doesn't handle special-case properties, since they are handled by
43 * CertGetCertificateContextProperty, and are particular to the store in which
44 * the property exists (which is separate from the context.)
46 static BOOL
CertContext_GetProperty(cert_t
*cert
, DWORD dwPropId
,
47 void *pvData
, DWORD
*pcbData
);
49 /* Internal version of CertSetCertificateContextProperty that sets properties
50 * directly on the context (or the context it's linked to, depending on its
51 * type.) Doesn't handle special cases, since they're handled by
52 * CertSetCertificateContextProperty anyway.
54 static BOOL
CertContext_SetProperty(cert_t
*cert
, DWORD dwPropId
,
55 DWORD dwFlags
, const void *pvData
);
57 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
58 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
59 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
61 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
62 pbCertEncoded
, cbCertEncoded
);
65 TRACE("(%p, %08lx, %p, %ld, %08lx, %p)\n", hCertStore
, dwCertEncodingType
,
66 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
70 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
71 dwAddDisposition
, ppCertContext
);
72 CertFreeCertificateContext(cert
);
79 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreA(LPCSTR pszCertStoreName
,
80 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
85 TRACE("(%s, %p, %ld)\n", debugstr_a(pszCertStoreName
), pbCertEncoded
,
88 store
= CertOpenSystemStoreA(0, pszCertStoreName
);
91 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
92 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
93 CertCloseStore(store
, 0);
98 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreW(LPCWSTR pszCertStoreName
,
99 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
104 TRACE("(%s, %p, %ld)\n", debugstr_w(pszCertStoreName
), pbCertEncoded
,
107 store
= CertOpenSystemStoreW(0, pszCertStoreName
);
110 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
111 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
112 CertCloseStore(store
, 0);
117 static const context_vtbl_t cert_vtbl
;
119 static void Cert_free(context_t
*context
)
121 cert_t
*cert
= (cert_t
*)context
;
123 CryptMemFree(cert
->ctx
.pbCertEncoded
);
124 LocalFree(cert
->ctx
.pCertInfo
);
127 static context_t
*Cert_clone(context_t
*context
, WINECRYPT_CERTSTORE
*store
, BOOL use_link
)
132 cert
= (cert_t
*)Context_CreateLinkContext(sizeof(CERT_CONTEXT
), context
, store
);
136 const cert_t
*cloned
= (const cert_t
*)context
;
140 cert
= (cert_t
*)Context_CreateDataContext(sizeof(CERT_CONTEXT
), &cert_vtbl
, store
);
144 Context_CopyProperties(&cert
->ctx
, &cloned
->ctx
);
146 cert
->ctx
.dwCertEncodingType
= cloned
->ctx
.dwCertEncodingType
;
147 cert
->ctx
.pbCertEncoded
= CryptMemAlloc(cloned
->ctx
.cbCertEncoded
);
148 memcpy(cert
->ctx
.pbCertEncoded
, cloned
->ctx
.pbCertEncoded
, cloned
->ctx
.cbCertEncoded
);
149 cert
->ctx
.cbCertEncoded
= cloned
->ctx
.cbCertEncoded
;
151 /* FIXME: We don't need to decode the object here, we could just clone cert info. */
152 res
= CryptDecodeObjectEx(cert
->ctx
.dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
153 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
154 &cert
->ctx
.pCertInfo
, &size
);
156 CertFreeCertificateContext(&cert
->ctx
);
161 cert
->ctx
.hCertStore
= store
;
165 static const context_vtbl_t cert_vtbl
= {
170 static BOOL
add_cert_to_store(WINECRYPT_CERTSTORE
*store
, const CERT_CONTEXT
*cert
,
171 DWORD add_disposition
, BOOL use_link
, PCCERT_CONTEXT
*ret_context
)
173 const CERT_CONTEXT
*existing
= NULL
;
174 BOOL ret
= TRUE
, inherit_props
= FALSE
;
175 context_t
*new_context
= NULL
;
177 switch (add_disposition
)
179 case CERT_STORE_ADD_ALWAYS
:
181 case CERT_STORE_ADD_NEW
:
182 case CERT_STORE_ADD_REPLACE_EXISTING
:
183 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
184 case CERT_STORE_ADD_USE_EXISTING
:
185 case CERT_STORE_ADD_NEWER
:
186 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
189 DWORD size
= sizeof(hashToAdd
);
191 ret
= CertGetCertificateContextProperty(cert
, CERT_HASH_PROP_ID
,
195 CRYPT_HASH_BLOB blob
= { sizeof(hashToAdd
), hashToAdd
};
197 existing
= CertFindCertificateInStore(store
, cert
->dwCertEncodingType
, 0,
198 CERT_FIND_SHA1_HASH
, &blob
, NULL
);
203 FIXME("Unimplemented add disposition %ld\n", add_disposition
);
204 SetLastError(E_INVALIDARG
);
208 switch (add_disposition
)
210 case CERT_STORE_ADD_ALWAYS
:
212 case CERT_STORE_ADD_NEW
:
215 TRACE("found matching certificate, not adding\n");
216 SetLastError(CRYPT_E_EXISTS
);
220 case CERT_STORE_ADD_REPLACE_EXISTING
:
222 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
224 FIXME("CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES: semi-stub for links\n");
226 inherit_props
= TRUE
;
228 case CERT_STORE_ADD_USE_EXISTING
:
230 FIXME("CERT_STORE_ADD_USE_EXISTING: semi-stub for links\n");
233 Context_CopyProperties(existing
, cert
);
235 *ret_context
= CertDuplicateCertificateContext(existing
);
239 case CERT_STORE_ADD_NEWER
:
240 if (existing
&& CompareFileTime(&existing
->pCertInfo
->NotBefore
, &cert
->pCertInfo
->NotBefore
) >= 0)
242 TRACE("existing certificate is newer, not adding\n");
243 SetLastError(CRYPT_E_EXISTS
);
247 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
250 if (CompareFileTime(&existing
->pCertInfo
->NotBefore
, &cert
->pCertInfo
->NotBefore
) >= 0)
252 TRACE("existing certificate is newer, not adding\n");
253 SetLastError(CRYPT_E_EXISTS
);
256 inherit_props
= TRUE
;
261 /* FIXME: We have tests that this works, but what should we really do in this case? */
264 *ret_context
= CertDuplicateCertificateContext(cert
);
268 ret
= store
->vtbl
->certs
.addContext(store
, context_from_ptr(cert
), existing
? context_from_ptr(existing
) : NULL
,
269 (ret_context
|| inherit_props
) ? &new_context
: NULL
, use_link
);
274 Context_CopyProperties(context_ptr(new_context
), existing
);
277 *ret_context
= context_ptr(new_context
);
279 Context_Release(new_context
);
281 TRACE("returning %d\n", ret
);
285 BOOL WINAPI
CertAddCertificateContextToStore(HCERTSTORE hCertStore
, PCCERT_CONTEXT pCertContext
,
286 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppStoreContext
)
288 WINECRYPT_CERTSTORE
*store
= hCertStore
;
290 TRACE("(%p, %p, %08lx, %p)\n", hCertStore
, pCertContext
, dwAddDisposition
, ppStoreContext
);
292 return add_cert_to_store(store
, pCertContext
, dwAddDisposition
, FALSE
, ppStoreContext
);
295 BOOL WINAPI
CertAddCertificateLinkToStore(HCERTSTORE hCertStore
,
296 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
297 PCCERT_CONTEXT
*ppCertContext
)
300 WINECRYPT_CERTSTORE
*store
= (WINECRYPT_CERTSTORE
*)hCertStore
;
303 FIXME("(%p, %p, %08lx, %p): semi-stub\n", hCertStore
, pCertContext
,
304 dwAddDisposition
, ppCertContext
);
305 if (store
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
307 if (store
->type
== StoreTypeCollection
)
309 SetLastError(E_INVALIDARG
);
312 return add_cert_to_store(hCertStore
, pCertContext
, dwAddDisposition
, TRUE
, ppCertContext
);
315 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
316 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
321 PCERT_INFO certInfo
= NULL
;
324 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType
, pbCertEncoded
,
327 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
329 SetLastError(E_INVALIDARG
);
333 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
334 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
339 cert
= (cert_t
*)Context_CreateDataContext(sizeof(CERT_CONTEXT
), &cert_vtbl
, &empty_store
);
342 data
= CryptMemAlloc(cbCertEncoded
);
345 Context_Release(&cert
->base
);
349 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
350 cert
->ctx
.dwCertEncodingType
= dwCertEncodingType
;
351 cert
->ctx
.pbCertEncoded
= data
;
352 cert
->ctx
.cbCertEncoded
= cbCertEncoded
;
353 cert
->ctx
.pCertInfo
= certInfo
;
354 cert
->ctx
.hCertStore
= &empty_store
;
359 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(PCCERT_CONTEXT pCertContext
)
361 TRACE("(%p)\n", pCertContext
);
366 Context_AddRef(&cert_from_ptr(pCertContext
)->base
);
370 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
372 TRACE("(%p)\n", pCertContext
);
375 Context_Release(&cert_from_ptr(pCertContext
)->base
);
379 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
382 cert_t
*cert
= cert_from_ptr(pCertContext
);
385 TRACE("(%p, %ld)\n", pCertContext
, dwPropId
);
387 if (cert
->base
.properties
)
388 ret
= ContextPropertyList_EnumPropIDs(cert
->base
.properties
, dwPropId
);
394 static BOOL
CertContext_GetHashProp(cert_t
*cert
, DWORD dwPropId
,
395 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
398 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
402 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
404 ret
= CertContext_SetProperty(cert
, dwPropId
, 0, &blob
);
409 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
416 else if (*pcbData
< cb
)
418 SetLastError(ERROR_MORE_DATA
);
424 memcpy(pvData
, pb
, cb
);
430 void CRYPT_ConvertKeyContext(const struct store_CERT_KEY_CONTEXT
*src
, CERT_KEY_CONTEXT
*dst
)
432 dst
->cbSize
= sizeof(*dst
);
433 dst
->hCryptProv
= src
->hCryptProv
;
434 dst
->dwKeySpec
= src
->dwKeySpec
;
438 * Fix offsets in a continuous block of memory of CRYPT_KEY_PROV_INFO with
439 * its associated data.
441 static void fix_KeyProvInfoProperty(CRYPT_KEY_PROV_INFO
*info
)
446 data
= (BYTE
*)(info
+ 1) + sizeof(CRYPT_KEY_PROV_PARAM
) * info
->cProvParam
;
448 if (info
->pwszContainerName
)
450 info
->pwszContainerName
= (LPWSTR
)data
;
451 data
+= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
454 if (info
->pwszProvName
)
456 info
->pwszProvName
= (LPWSTR
)data
;
457 data
+= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
460 info
->rgProvParam
= info
->cProvParam
? (CRYPT_KEY_PROV_PARAM
*)(info
+ 1) : NULL
;
462 for (i
= 0; i
< info
->cProvParam
; i
++)
464 info
->rgProvParam
[i
].pbData
= info
->rgProvParam
[i
].cbData
? data
: NULL
;
465 data
+= info
->rgProvParam
[i
].cbData
;
470 * Copy to a continuous block of memory of CRYPT_KEY_PROV_INFO with
471 * its associated data.
473 static void copy_KeyProvInfoProperty(const CRYPT_KEY_PROV_INFO
*from
, CRYPT_KEY_PROV_INFO
*to
)
478 data
= (BYTE
*)(to
+ 1) + sizeof(CRYPT_KEY_PROV_PARAM
) * from
->cProvParam
;
480 if (from
->pwszContainerName
)
482 to
->pwszContainerName
= (LPWSTR
)data
;
483 lstrcpyW((LPWSTR
)data
, from
->pwszContainerName
);
484 data
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
487 to
->pwszContainerName
= NULL
;
489 if (from
->pwszProvName
)
491 to
->pwszProvName
= (LPWSTR
)data
;
492 lstrcpyW((LPWSTR
)data
, from
->pwszProvName
);
493 data
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
496 to
->pwszProvName
= NULL
;
498 to
->dwProvType
= from
->dwProvType
;
499 to
->dwFlags
= from
->dwFlags
;
500 to
->cProvParam
= from
->cProvParam
;
501 to
->rgProvParam
= from
->cProvParam
? (CRYPT_KEY_PROV_PARAM
*)(to
+ 1) : NULL
;
502 to
->dwKeySpec
= from
->dwKeySpec
;
504 for (i
= 0; i
< from
->cProvParam
; i
++)
506 to
->rgProvParam
[i
].dwParam
= from
->rgProvParam
[i
].dwParam
;
507 to
->rgProvParam
[i
].dwFlags
= from
->rgProvParam
[i
].dwFlags
;
508 to
->rgProvParam
[i
].cbData
= from
->rgProvParam
[i
].cbData
;
509 to
->rgProvParam
[i
].pbData
= from
->rgProvParam
[i
].cbData
? data
: NULL
;
510 memcpy(data
, from
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].cbData
);
511 data
+= from
->rgProvParam
[i
].cbData
;
515 static BOOL
CertContext_GetProperty(cert_t
*cert
, DWORD dwPropId
,
516 void *pvData
, DWORD
*pcbData
)
519 CRYPT_DATA_BLOB blob
;
521 TRACE("(%p, %ld, %p, %p)\n", cert
, dwPropId
, pvData
, pcbData
);
523 if (cert
->base
.properties
)
524 ret
= ContextPropertyList_FindProperty(cert
->base
.properties
, dwPropId
, &blob
);
529 CERT_KEY_CONTEXT ctx
;
530 if (dwPropId
== CERT_KEY_CONTEXT_PROP_ID
)
532 CRYPT_ConvertKeyContext((const struct store_CERT_KEY_CONTEXT
*)blob
.pbData
, &ctx
);
533 blob
.pbData
= (BYTE
*)&ctx
;
534 blob
.cbData
= ctx
.cbSize
;
536 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
540 /* Implicit properties */
543 case CERT_SHA1_HASH_PROP_ID
:
544 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_SHA1
,
545 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
548 case CERT_MD5_HASH_PROP_ID
:
549 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
550 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
553 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
554 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
555 cert
->ctx
.pCertInfo
->Subject
.pbData
,
556 cert
->ctx
.pCertInfo
->Subject
.cbData
,
559 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
560 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
561 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
562 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
565 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
566 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
567 cert
->ctx
.pCertInfo
->SerialNumber
.pbData
,
568 cert
->ctx
.pCertInfo
->SerialNumber
.cbData
,
571 case CERT_SIGNATURE_HASH_PROP_ID
:
572 ret
= CryptHashToBeSigned(0, cert
->ctx
.dwCertEncodingType
,
573 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
577 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
579 ret
= CertContext_SetProperty(cert
, dwPropId
, 0, &blob
);
582 case CERT_KEY_IDENTIFIER_PROP_ID
:
584 PCERT_EXTENSION ext
= CertFindExtension(
585 szOID_SUBJECT_KEY_IDENTIFIER
, cert
->ctx
.pCertInfo
->cExtension
,
586 cert
->ctx
.pCertInfo
->rgExtension
);
590 CRYPT_DATA_BLOB value
;
591 DWORD size
= sizeof(value
);
593 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
594 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
595 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
599 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
601 CertContext_SetProperty(cert
, dwPropId
, 0, &value
);
605 SetLastError(ERROR_INVALID_DATA
);
609 SetLastError(CRYPT_E_NOT_FOUND
);
612 TRACE("returning %d\n", ret
);
616 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
617 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
619 cert_t
*cert
= cert_from_ptr(pCertContext
);
622 TRACE("(%p, %ld, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
627 case CERT_CERT_PROP_ID
:
628 case CERT_CRL_PROP_ID
:
629 case CERT_CTL_PROP_ID
:
630 SetLastError(E_INVALIDARG
);
633 case CERT_ACCESS_STATE_PROP_ID
:
634 ret
= CertGetStoreProperty(cert
->ctx
.hCertStore
, dwPropId
, pvData
, pcbData
);
636 case CERT_KEY_PROV_HANDLE_PROP_ID
:
638 CERT_KEY_CONTEXT keyContext
;
639 DWORD size
= sizeof(keyContext
);
641 ret
= CertContext_GetProperty(cert
,
642 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
644 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
645 sizeof(keyContext
.hCryptProv
));
648 case CERT_KEY_PROV_INFO_PROP_ID
:
649 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
, pcbData
);
651 fix_KeyProvInfoProperty(pvData
);
654 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
,
658 TRACE("returning %d\n", ret
);
663 * Create a continuous block of memory for CRYPT_KEY_PROV_INFO with
664 * its associated data, and add it to the certificate properties.
666 static BOOL
CertContext_SetKeyProvInfoProperty(CONTEXT_PROPERTY_LIST
*properties
, const CRYPT_KEY_PROV_INFO
*info
)
668 CRYPT_KEY_PROV_INFO
*prop
;
669 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
;
672 if (info
->pwszContainerName
)
673 size
+= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
674 if (info
->pwszProvName
)
675 size
+= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
677 for (i
= 0; i
< info
->cProvParam
; i
++)
678 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
680 prop
= CryptMemAlloc(size
);
683 SetLastError(ERROR_OUTOFMEMORY
);
687 copy_KeyProvInfoProperty(info
, prop
);
689 ret
= ContextPropertyList_SetProperty(properties
, CERT_KEY_PROV_INFO_PROP_ID
, (const BYTE
*)prop
, size
);
695 static BOOL
CertContext_SetKeyContextProperty(CONTEXT_PROPERTY_LIST
*properties
,
696 const CERT_KEY_CONTEXT
*keyContext
)
698 struct store_CERT_KEY_CONTEXT ctx
;
700 ctx
.cbSize
= sizeof(ctx
);
701 ctx
.hCryptProv
= keyContext
->hCryptProv
;
702 ctx
.dwKeySpec
= keyContext
->dwKeySpec
;
704 return ContextPropertyList_SetProperty(properties
, CERT_KEY_CONTEXT_PROP_ID
,
705 (const BYTE
*)&ctx
, ctx
.cbSize
);
708 static BOOL
CertContext_SetProperty(cert_t
*cert
, DWORD dwPropId
,
709 DWORD dwFlags
, const void *pvData
)
713 TRACE("(%p, %ld, %08lx, %p)\n", cert
, dwPropId
, dwFlags
, pvData
);
715 if (!cert
->base
.properties
)
721 case CERT_AUTO_ENROLL_PROP_ID
:
722 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
723 case CERT_DESCRIPTION_PROP_ID
:
724 case CERT_FRIENDLY_NAME_PROP_ID
:
725 case CERT_HASH_PROP_ID
:
726 case CERT_KEY_IDENTIFIER_PROP_ID
:
727 case CERT_MD5_HASH_PROP_ID
:
728 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
729 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
730 case CERT_PVK_FILE_PROP_ID
:
731 case CERT_SIGNATURE_HASH_PROP_ID
:
732 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
733 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
734 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
735 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
736 case CERT_ENROLLMENT_PROP_ID
:
737 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
738 case CERT_OCSP_RESPONSE_PROP_ID
:
739 case CERT_RENEWAL_PROP_ID
:
743 const CRYPT_DATA_BLOB
*blob
= pvData
;
745 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
746 blob
->pbData
, blob
->cbData
);
750 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
755 case CERT_DATE_STAMP_PROP_ID
:
757 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
758 pvData
, sizeof(FILETIME
));
761 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
765 case CERT_KEY_CONTEXT_PROP_ID
:
768 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
770 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
772 SetLastError(E_INVALIDARG
);
776 ret
= CertContext_SetKeyContextProperty(cert
->base
.properties
, pvData
);
780 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
784 case CERT_KEY_PROV_INFO_PROP_ID
:
786 ret
= CertContext_SetKeyProvInfoProperty(cert
->base
.properties
, pvData
);
789 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
793 case CERT_KEY_PROV_HANDLE_PROP_ID
:
795 CERT_KEY_CONTEXT keyContext
;
796 DWORD size
= sizeof(keyContext
);
798 ret
= CertContext_GetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
802 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
803 CryptReleaseContext(keyContext
.hCryptProv
, 0);
805 keyContext
.cbSize
= sizeof(keyContext
);
807 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
810 keyContext
.hCryptProv
= 0;
811 keyContext
.dwKeySpec
= AT_SIGNATURE
;
813 ret
= CertContext_SetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
818 FIXME("%ld: stub\n", dwPropId
);
822 TRACE("returning %d\n", ret
);
826 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
827 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
831 TRACE("(%p, %ld, %08lx, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
833 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
834 * crashes on most of these, I'll be safer.
839 case CERT_ACCESS_STATE_PROP_ID
:
840 case CERT_CERT_PROP_ID
:
841 case CERT_CRL_PROP_ID
:
842 case CERT_CTL_PROP_ID
:
843 SetLastError(E_INVALIDARG
);
846 ret
= CertContext_SetProperty(cert_from_ptr(pCertContext
), dwPropId
, dwFlags
,
848 TRACE("returning %d\n", ret
);
852 /* Acquires the private key using the key provider info, retrieving info from
853 * the certificate if info is NULL. The acquired provider is returned in
854 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
856 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
, DWORD dwFlags
,
857 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
860 BOOL allocated
= FALSE
, ret
= TRUE
;
864 ret
= CertGetCertificateContextProperty(pCert
,
865 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
868 info
= CryptMemAlloc(size
);
871 ret
= CertGetCertificateContextProperty(pCert
,
872 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
877 SetLastError(ERROR_OUTOFMEMORY
);
882 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
886 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
887 info
->pwszProvName
, info
->dwProvType
, (dwFlags
& CRYPT_ACQUIRE_SILENT_FLAG
) ? CRYPT_SILENT
: 0);
892 for (i
= 0; i
< info
->cProvParam
; i
++)
894 CryptSetProvParam(*phCryptProv
,
895 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
896 info
->rgProvParam
[i
].dwFlags
);
898 *pdwKeySpec
= info
->dwKeySpec
;
901 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
908 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
909 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
910 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
912 BOOL ret
= FALSE
, cache
= FALSE
;
913 PCRYPT_KEY_PROV_INFO info
= NULL
;
914 CERT_KEY_CONTEXT keyContext
;
916 PCCERT_CONTEXT cert_in_store
= NULL
;
918 TRACE("(%p, %08lx, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
919 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
921 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
925 ret
= CertGetCertificateContextProperty(pCert
,
926 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
932 hstore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
933 CERT_SYSTEM_STORE_CURRENT_USER
, L
"My");
936 cert_in_store
= CertFindCertificateInStore(hstore
, pCert
->dwCertEncodingType
, 0,
937 CERT_FIND_EXISTING
, pCert
, NULL
);
940 ret
= CertGetCertificateContextProperty(cert_in_store
, CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
942 pCert
= cert_in_store
;
945 CertFreeCertificateContext(cert_in_store
);
946 cert_in_store
= NULL
;
950 CertCloseStore(hstore
, 0);
956 info
= CryptMemAlloc(size
);
957 ret
= CertGetCertificateContextProperty(pCert
,
958 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
960 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
963 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
968 size
= sizeof(keyContext
);
969 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
973 *phCryptProv
= keyContext
.hCryptProv
;
975 *pdwKeySpec
= keyContext
.dwKeySpec
;
976 if (pfCallerFreeProv
)
977 *pfCallerFreeProv
= FALSE
;
982 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, dwFlags
, info
,
983 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
986 *phCryptProv
= keyContext
.hCryptProv
;
988 *pdwKeySpec
= keyContext
.dwKeySpec
;
991 keyContext
.cbSize
= sizeof(keyContext
);
992 if (CertSetCertificateContextProperty(pCert
,
993 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
995 if (pfCallerFreeProv
)
996 *pfCallerFreeProv
= FALSE
;
1001 if (pfCallerFreeProv
)
1002 *pfCallerFreeProv
= TRUE
;
1008 CertFreeCertificateContext(cert_in_store
);
1009 if (ret
) SetLastError(0);
1013 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
1014 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1017 BOOL matches
= FALSE
;
1019 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
1020 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
1024 /* Need to sign something to verify the sig. What to sign? Why not
1025 * the certificate itself?
1027 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
1028 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
1029 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
1031 BYTE
*certEncoded
= CryptMemAlloc(size
);
1035 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
1036 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
1037 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
1038 NULL
, certEncoded
, &size
))
1040 if (size
== pCert
->cbCertEncoded
&&
1041 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
1044 CryptMemFree(certEncoded
);
1047 CryptReleaseContext(csp
, 0);
1052 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
1053 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1055 CRYPT_KEY_PROV_INFO copy
;
1056 WCHAR containerW
[MAX_PATH
];
1059 MultiByteToWideChar(CP_ACP
, 0, container
, -1, containerW
, ARRAY_SIZE(containerW
));
1060 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
1061 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
1064 copy
= *keyProvInfo
;
1065 copy
.pwszContainerName
= containerW
;
1066 matches
= key_prov_info_matches_cert(pCert
, ©
);
1069 keyProvInfo
->pwszContainerName
=
1070 CryptMemAlloc((lstrlenW(containerW
) + 1) * sizeof(WCHAR
));
1071 if (keyProvInfo
->pwszContainerName
)
1073 lstrcpyW(keyProvInfo
->pwszContainerName
, containerW
);
1074 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
1082 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
1083 * private key matches pCert's public key. Upon success, updates keyProvInfo
1084 * with the matching container's info (free keyProvInfo.pwszContainerName upon
1086 * Returns TRUE if found, FALSE if not.
1088 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
1089 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1091 HCRYPTPROV defProvider
;
1092 BOOL ret
, found
= FALSE
;
1093 char containerA
[MAX_PATH
];
1095 assert(keyProvInfo
->pwszContainerName
== NULL
);
1096 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
1097 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
1098 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
1100 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
1102 while (ret
&& !found
)
1104 DWORD size
= sizeof(containerA
);
1106 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
1107 (BYTE
*)containerA
, &size
, enumFlags
);
1109 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
1110 if (enumFlags
& CRYPT_FIRST
)
1112 enumFlags
&= ~CRYPT_FIRST
;
1113 enumFlags
|= CRYPT_NEXT
;
1116 CryptReleaseContext(defProvider
, 0);
1121 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
1123 BOOL found
= FALSE
, ret
= TRUE
;
1124 DWORD index
= 0, cbProvName
= 0;
1125 CRYPT_KEY_PROV_INFO keyProvInfo
;
1127 TRACE("(%p, %08lx)\n", pCert
, dwFlags
);
1129 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
1130 while (ret
&& !found
)
1134 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
1138 if (size
<= cbProvName
)
1139 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1140 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1143 CryptMemFree(keyProvInfo
.pwszProvName
);
1144 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
1145 if (keyProvInfo
.pwszProvName
)
1148 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1149 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1152 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
1153 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
1154 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
1155 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1156 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1158 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
1159 found
= find_key_prov_info_in_provider(pCert
,
1164 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
1165 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1166 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1168 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
1169 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
1170 found
= find_key_prov_info_in_provider(pCert
,
1183 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1185 CryptMemFree(keyProvInfo
.pwszProvName
);
1186 CryptMemFree(keyProvInfo
.pwszContainerName
);
1190 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
1192 BOOL matches
= FALSE
;
1195 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1198 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
1202 if (CertGetCertificateContextProperty(pCert
,
1203 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
1204 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
1205 CryptMemFree(keyProvInfo
);
1211 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
1212 DWORD dwFlags
, void *pvReserved
)
1216 TRACE("(%p, %08lx, %p)\n", pCert
, dwFlags
, pvReserved
);
1218 matches
= cert_prov_info_matches_cert(pCert
);
1220 matches
= find_matching_provider(pCert
, dwFlags
);
1224 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1225 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1229 TRACE("(%08lx, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1231 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1232 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1233 &pCertId2
->SerialNumber
);
1234 TRACE("returning %d\n", ret
);
1238 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1239 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1243 TRACE("(%08lx, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1245 if (pCertName1
->cbData
== pCertName2
->cbData
)
1247 if (pCertName1
->cbData
)
1248 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1249 pCertName1
->cbData
);
1255 TRACE("returning %d\n", ret
);
1259 /* Returns the number of significant bytes in pInt, where a byte is
1260 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1261 * for negative numbers. pInt is assumed to be little-endian.
1263 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1265 DWORD ret
= pInt
->cbData
;
1269 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1271 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1279 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1280 PCRYPT_INTEGER_BLOB pInt2
)
1285 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1287 cb1
= CRYPT_significantBytes(pInt1
);
1288 cb2
= CRYPT_significantBytes(pInt2
);
1292 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1298 TRACE("returning %d\n", ret
);
1302 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1303 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1307 TRACE("(%08lx, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1309 /* RSA public key data should start with ASN_SEQUENCE,
1310 * otherwise it's not a RSA_CSP_PUBLICKEYBLOB.
1312 if (!pPublicKey1
->PublicKey
.cbData
|| pPublicKey1
->PublicKey
.pbData
[0] != ASN_SEQUENCE
)
1313 dwCertEncodingType
= 0;
1315 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1317 case 0: /* Seems to mean "raw binary bits" */
1318 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1319 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1321 if (pPublicKey2
->PublicKey
.cbData
)
1322 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1323 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1331 WARN("Unknown encoding type %08lx\n", dwCertEncodingType
);
1333 case X509_ASN_ENCODING
:
1335 BLOBHEADER
*pblob1
, *pblob2
;
1338 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1339 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1340 CRYPT_DECODE_ALLOC_FLAG
, &pblob1
, &length
))
1342 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1343 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1344 CRYPT_DECODE_ALLOC_FLAG
, &pblob2
, &length
))
1346 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1347 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1348 *pk2
= (LPVOID
)(pblob2
+ 1);
1349 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1350 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1363 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1364 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1368 TRACE("(%08lx, %p)\n", dwCertEncodingType
, pPublicKey
);
1370 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1372 SetLastError(ERROR_FILE_NOT_FOUND
);
1375 if (pPublicKey
->Algorithm
.pszObjId
&&
1376 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1378 FIXME("unimplemented for DH public keys\n");
1379 SetLastError(CRYPT_E_ASN1_BADTAG
);
1383 PCCRYPT_OID_INFO info
;
1388 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, pPublicKey
->Algorithm
.pszObjId
, 0);
1393 TRACE("public key algid %#x (%s)\n", info
->u
.Algid
, debugstr_a(pPublicKey
->Algorithm
.pszObjId
));
1395 ret
= CryptImportPublicKeyInfo(I_CryptGetDefaultCryptProv(info
->u
.Algid
), dwCertEncodingType
, pPublicKey
, &key
);
1399 ret
= CryptGetKeyParam(key
, KP_KEYLEN
, (BYTE
*)&len
, &size
, 0);
1400 CryptDestroyKey(key
);
1403 /* fallback to RSA */
1406 ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1407 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1408 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1413 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1415 len
= rsaPubKey
->bitlen
;
1422 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1423 DWORD dwFlags
, const void *pvPara
);
1425 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1426 DWORD dwFlags
, const void *pvPara
)
1430 DWORD size
= sizeof(hash
);
1432 ret
= CertGetCertificateContextProperty(pCertContext
,
1433 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1436 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1438 if (size
== pHash
->cbData
)
1439 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1446 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1447 DWORD dwFlags
, const void *pvPara
)
1451 DWORD size
= sizeof(hash
);
1453 ret
= CertGetCertificateContextProperty(pCertContext
,
1454 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1457 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1459 if (size
== pHash
->cbData
)
1460 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1467 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1468 DWORD dwFlags
, const void *pvPara
)
1470 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1473 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1474 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1476 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1477 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1482 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1483 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1485 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1488 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1489 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1493 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1494 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1496 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1499 /* Matching serial number and subject match.. */
1500 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1501 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1503 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1504 &pCertInfo
->SerialNumber
);
1507 /* failing that, if the serial number and issuer match, we match */
1508 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1509 &pCertInfo
->SerialNumber
);
1511 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1512 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1514 TRACE("returning %d\n", ret
);
1518 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1519 DWORD dwFlags
, const void *pvPara
)
1521 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1524 switch (id
->dwIdChoice
)
1526 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1527 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1528 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1530 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1531 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1533 case CERT_ID_SHA1_HASH
:
1534 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1537 case CERT_ID_KEY_IDENTIFIER
:
1541 ret
= CertGetCertificateContextProperty(pCertContext
,
1542 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1543 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1545 LPBYTE buf
= CryptMemAlloc(size
);
1549 CertGetCertificateContextProperty(pCertContext
,
1550 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1551 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1568 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1569 DWORD dwFlags
, const void *pvPara
)
1571 PCCERT_CONTEXT toCompare
= pvPara
;
1572 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1573 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1576 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1577 DWORD dwFlags
, const void *pvPara
)
1579 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1583 ret
= CertGetCertificateContextProperty(pCertContext
,
1584 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1585 if (ret
&& size
== hash
->cbData
)
1587 LPBYTE buf
= CryptMemAlloc(size
);
1591 CertGetCertificateContextProperty(pCertContext
,
1592 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1593 ret
= !memcmp(buf
, hash
->pbData
, size
);
1604 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1605 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1608 BOOL matches
= FALSE
;
1613 ret
= CertEnumCertificatesInStore(store
, ret
);
1615 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1616 } while (ret
!= NULL
&& !matches
);
1620 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1621 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1623 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1624 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1626 return CertEnumCertificatesInStore(store
, prev
);
1629 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1630 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1633 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1634 PCERT_EXTENSION ext
;
1637 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1638 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1640 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1642 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1643 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1644 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1650 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1652 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1653 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1654 sizeof(CERT_NAME_BLOB
));
1655 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1656 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1658 else if (info
->KeyId
.cbData
)
1660 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1661 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1666 found
= cert_compare_certs_in_store(store
, prev
,
1667 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1671 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1672 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1674 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1676 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1677 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1678 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1684 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1685 info
->AuthorityCertSerialNumber
.cbData
)
1687 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1690 for (i
= 0; !directoryName
&&
1691 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1692 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1693 == CERT_ALT_NAME_DIRECTORY_NAME
)
1695 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1698 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1699 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1700 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1701 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1702 &info
->AuthorityCertSerialNumber
,
1703 sizeof(CRYPT_INTEGER_BLOB
));
1707 FIXME("no supported name type in authority key id2\n");
1711 else if (info
->KeyId
.cbData
)
1713 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1714 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1719 found
= cert_compare_certs_in_store(store
, prev
,
1720 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1725 found
= cert_compare_certs_in_store(store
, prev
,
1726 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1727 dwFlags
, &subject
->pCertInfo
->Issuer
);
1731 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1732 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1734 PCERT_NAME_BLOB name
;
1738 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1739 name
= &pCertContext
->pCertInfo
->Subject
;
1741 name
= &pCertContext
->pCertInfo
->Issuer
;
1742 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1743 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1746 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1750 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1751 CERT_SIMPLE_NAME_STR
, str
, len
);
1753 if (wcsstr(str
, pvPara
))
1761 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1762 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1764 PCCERT_CONTEXT found
= NULL
;
1766 TRACE("%s\n", debugstr_a(pvPara
));
1770 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1771 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1775 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1777 found
= cert_compare_certs_in_store(store
, prev
,
1778 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1783 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1787 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1788 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1790 PCCERT_CONTEXT found
= NULL
;
1792 TRACE("%s\n", debugstr_w(pvPara
));
1796 DWORD len
= lstrlenW(pvPara
);
1797 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1801 wcscpy( str
, pvPara
);
1803 found
= cert_compare_certs_in_store(store
, prev
,
1804 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1809 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1813 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1814 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1815 PCCERT_CONTEXT pPrevCertContext
)
1818 CertFindFunc find
= NULL
;
1819 CertCompareFunc compare
= NULL
;
1822 TRACE("(%p, %08lx, %08lx, %08lx, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1823 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1825 switch (dwType
>> CERT_COMPARE_SHIFT
)
1827 case CERT_COMPARE_ANY
:
1828 find
= find_cert_any
;
1830 case CERT_COMPARE_MD5_HASH
:
1831 compare
= compare_cert_by_md5_hash
;
1833 case CERT_COMPARE_SHA1_HASH
:
1834 compare
= compare_cert_by_sha1_hash
;
1836 case CERT_COMPARE_NAME
:
1837 compare
= compare_cert_by_name
;
1839 case CERT_COMPARE_PUBLIC_KEY
:
1840 compare
= compare_cert_by_public_key
;
1842 case CERT_COMPARE_NAME_STR_A
:
1843 find
= find_cert_by_name_str_a
;
1845 case CERT_COMPARE_NAME_STR_W
:
1846 find
= find_cert_by_name_str_w
;
1848 case CERT_COMPARE_SUBJECT_CERT
:
1849 compare
= compare_cert_by_subject_cert
;
1851 case CERT_COMPARE_KEY_IDENTIFIER
:
1852 cert_id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1853 cert_id
.u
.KeyId
= *(const CRYPT_HASH_BLOB
*)pvPara
;
1856 case CERT_COMPARE_CERT_ID
:
1857 compare
= compare_cert_by_cert_id
;
1859 case CERT_COMPARE_ISSUER_OF
:
1860 find
= find_cert_by_issuer
;
1862 case CERT_COMPARE_EXISTING
:
1863 compare
= compare_existing_cert
;
1865 case CERT_COMPARE_SIGNATURE_HASH
:
1866 compare
= compare_cert_by_signature_hash
;
1869 FIXME("find type %08lx unimplemented\n", dwType
);
1873 ret
= find(hCertStore
, dwType
, dwFlags
, pvPara
, pPrevCertContext
);
1875 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1876 compare
, dwType
, dwFlags
, pvPara
);
1880 SetLastError(CRYPT_E_NOT_FOUND
);
1881 TRACE("returning %p\n", ret
);
1885 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1886 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1888 TRACE("(%p, %08lx, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1892 SetLastError(E_INVALIDARG
);
1895 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1896 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1899 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1900 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1902 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1903 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1905 if (*pdwFlags
& ~supportedFlags
)
1907 SetLastError(E_INVALIDARG
);
1910 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1913 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1916 /* FIXME: what if the CRL has expired? */
1919 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1920 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1921 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1924 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1926 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1928 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1929 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1931 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1933 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1934 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1935 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1936 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1941 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1942 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1947 TRACE("(%p, %p, %p, %08lx)\n", hCertStore
, pSubjectContext
,
1948 pPrevIssuerContext
, *pdwFlags
);
1950 if (!pSubjectContext
)
1952 SetLastError(E_INVALIDARG
);
1956 ret
= CertFindCertificateInStore(hCertStore
,
1957 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1958 pSubjectContext
, pPrevIssuerContext
);
1961 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1964 CertFreeCertificateContext(ret
);
1967 if (CRYPT_IsCertificateSelfSigned(pSubjectContext
))
1969 CertFreeCertificateContext(ret
);
1971 SetLastError(CRYPT_E_SELF_SIGNED
);
1974 TRACE("returning %p\n", ret
);
1978 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1983 } OLD_CERT_REVOCATION_STATUS
;
1985 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1986 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
1988 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1989 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1990 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1994 TRACE("(%08lx, %ld, %ld, %p, %08lx, %p, %p)\n", dwEncodingType
, dwRevType
,
1995 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1997 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1998 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
2000 SetLastError(E_INVALIDARG
);
2005 static HCRYPTOIDFUNCSET set
= NULL
;
2009 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
2010 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
2016 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
2021 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
2025 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
2029 for (ptr
= dllList
; ret
&& *ptr
;
2030 ptr
+= lstrlenW(ptr
) + 1)
2032 CertVerifyRevocationFunc func
;
2033 HCRYPTOIDFUNCADDR hFunc
;
2035 ret
= CryptGetDefaultOIDFunctionAddress(set
,
2036 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
2039 ret
= func(dwEncodingType
, dwRevType
, cContext
,
2040 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
2041 CryptFreeOIDFunctionAddress(hFunc
, 0);
2045 CryptMemFree(dllList
);
2049 SetLastError(ERROR_OUTOFMEMORY
);
2060 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
2061 CRYPT_ATTRIBUTE rgAttr
[])
2063 PCRYPT_ATTRIBUTE ret
= NULL
;
2066 TRACE("%s %ld %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
2072 SetLastError(ERROR_INVALID_PARAMETER
);
2076 for (i
= 0; !ret
&& i
< cAttr
; i
++)
2077 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
2082 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
2083 CERT_EXTENSION rgExtensions
[])
2085 PCERT_EXTENSION ret
= NULL
;
2088 TRACE("%s %ld %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
2094 SetLastError(ERROR_INVALID_PARAMETER
);
2098 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
2099 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
2100 rgExtensions
[i
].pszObjId
))
2101 ret
= &rgExtensions
[i
];
2105 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
2107 PCERT_RDN_ATTR ret
= NULL
;
2110 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
2114 SetLastError(ERROR_INVALID_PARAMETER
);
2118 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
2119 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
2120 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
2121 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
2122 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
2126 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
2127 const CERT_RDN_ATTR
*attr
)
2132 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
2134 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
2136 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
2138 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
2141 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
2144 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2145 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
2147 if (attr
->Value
.cbData
!=
2148 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2150 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2151 match
= !wcsnicmp(nameStr
, attrStr
,
2152 attr
->Value
.cbData
/ sizeof(WCHAR
));
2154 match
= !wcsncmp(nameStr
, attrStr
,
2155 attr
->Value
.cbData
/ sizeof(WCHAR
));
2156 TRACE("%s : %s => %d\n",
2157 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2158 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2164 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2165 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
2167 if (attr
->Value
.cbData
!=
2168 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2170 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2171 match
= !_strnicmp(nameStr
, attrStr
,
2172 attr
->Value
.cbData
);
2174 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
2175 TRACE("%s : %s => %d\n",
2176 debugstr_an(nameStr
, attr
->Value
.cbData
),
2177 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
2185 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
2186 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
2188 CERT_NAME_INFO
*name
;
2193 TRACE("(%08lx, %08lx, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
2196 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
2198 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
2199 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
2203 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
2204 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
2206 SetLastError(CRYPT_E_NO_MATCH
);
2212 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
2213 PCERT_INFO pCertInfo
)
2220 GetSystemTimeAsFileTime(&fileTime
);
2221 pTimeToVerify
= &fileTime
;
2223 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
2225 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
2232 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
2233 PCERT_INFO pIssuerInfo
)
2235 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
2237 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
2238 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
2241 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2242 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2243 DWORD
*pcbComputedHash
)
2246 HCRYPTHASH hHash
= 0;
2248 TRACE("(%08Ix, %d, %08lx, %p, %ld, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2249 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2252 hCryptProv
= I_CryptGetDefaultCryptProv(Algid
);
2257 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2260 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2262 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2263 pcbComputedHash
, 0);
2264 CryptDestroyHash(hHash
);
2270 BOOL WINAPI
CryptHashCertificate2(LPCWSTR pwszCNGHashAlgid
, DWORD dwFlags
,
2271 void *pvReserved
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2272 DWORD
*pcbComputedHash
)
2274 BCRYPT_HASH_HANDLE hash
= NULL
;
2275 BCRYPT_ALG_HANDLE alg
= NULL
;
2278 DWORD hash_len_size
;
2280 TRACE("(%s, %08lx, %p, %p, %ld, %p, %p)\n", debugstr_w(pwszCNGHashAlgid
),
2281 dwFlags
, pvReserved
, pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2283 if ((status
= BCryptOpenAlgorithmProvider(&alg
, pwszCNGHashAlgid
, NULL
, 0)))
2285 if (status
== STATUS_NOT_IMPLEMENTED
)
2286 status
= STATUS_NOT_FOUND
;
2290 if ((status
= BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0)))
2293 if ((status
= BCryptGetProperty(hash
, BCRYPT_HASH_LENGTH
, (BYTE
*)&hash_len
, sizeof(hash_len
), &hash_len_size
, 0)))
2296 if (!pbComputedHash
)
2298 *pcbComputedHash
= hash_len
;
2302 if (*pcbComputedHash
< hash_len
)
2304 status
= ERROR_MORE_DATA
;
2308 *pcbComputedHash
= hash_len
;
2310 if ((status
= BCryptHashData(hash
, (BYTE
*)pbEncoded
, cbEncoded
, 0)))
2313 if ((status
= BCryptFinishHash(hash
, pbComputedHash
, hash_len
, 0)))
2317 if (hash
) BCryptDestroyHash(hash
);
2318 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2319 if (status
) SetLastError(status
);
2323 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2324 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2325 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2328 HCRYPTHASH hHash
= 0;
2330 TRACE("(%08Ix, %d, %08lx, %ld, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2331 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2334 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2337 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2339 SetLastError(ERROR_FILE_NOT_FOUND
);
2347 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2348 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2349 (LPBYTE
)&buf
, &size
);
2352 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2355 ret
= CryptHashData(hHash
, buf
, size
, 0);
2357 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2358 pcbComputedHash
, 0);
2359 CryptDestroyHash(hHash
);
2367 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2368 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2369 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2372 CERT_SIGNED_CONTENT_INFO
*info
;
2375 TRACE("(%08Ix, %08lx, %p, %ld, %p, %ld)\n", hCryptProv
, dwCertEncodingType
,
2376 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2378 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2379 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2382 PCCRYPT_OID_INFO oidInfo
;
2386 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2387 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2388 info
->SignatureAlgorithm
.pszObjId
, 0);
2391 SetLastError(NTE_BAD_ALGID
);
2396 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2399 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2400 info
->ToBeSigned
.cbData
, 0);
2402 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2403 pcbComputedHash
, 0);
2404 CryptDestroyHash(hHash
);
2412 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2413 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2414 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2415 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2418 PCCRYPT_OID_INFO info
;
2421 TRACE("(%08Ix, %ld, %ld, %p, %ld, %p, %p, %p, %p)\n", hCryptProv
,
2422 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2423 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2425 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2426 pSignatureAlgorithm
->pszObjId
, 0);
2429 SetLastError(NTE_BAD_ALGID
);
2432 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2435 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2436 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2439 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2440 cbEncodedToBeSigned
, 0);
2442 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2444 CryptDestroyHash(hHash
);
2451 SetLastError(ERROR_INVALID_PARAMETER
);
2456 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2459 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2460 cbEncodedToBeSigned
, 0);
2462 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2464 CryptDestroyHash(hHash
);
2471 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2472 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2473 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2474 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2477 DWORD encodedSize
, hashSize
;
2479 TRACE("(%08Ix, %ld, %ld, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2480 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2481 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2483 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2484 NULL
, &encodedSize
);
2487 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2491 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2492 pvStructInfo
, encoded
, &encodedSize
);
2495 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2496 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2497 pvHashAuxInfo
, NULL
, &hashSize
);
2500 PBYTE hash
= CryptMemAlloc(hashSize
);
2504 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2505 dwCertEncodingType
, encoded
, encodedSize
,
2506 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2509 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2511 info
.ToBeSigned
.cbData
= encodedSize
;
2512 info
.ToBeSigned
.pbData
= encoded
;
2513 info
.SignatureAlgorithm
= *pSignatureAlgorithm
;
2514 info
.Signature
.cbData
= hashSize
;
2515 info
.Signature
.pbData
= hash
;
2516 info
.Signature
.cUnusedBits
= 0;
2517 ret
= CryptEncodeObject(dwCertEncodingType
,
2518 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2526 CryptMemFree(encoded
);
2534 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2535 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2536 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2538 CRYPT_DATA_BLOB blob
= { cbEncoded
, (BYTE
*)pbEncoded
};
2540 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2541 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, &blob
,
2542 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2545 static BOOL
CRYPT_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2546 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2550 ALG_ID pubKeyID
, hashID
;
2552 hashID
= info
->u
.Algid
;
2553 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2554 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2557 /* Load the default provider if necessary */
2559 hCryptProv
= I_CryptGetDefaultCryptProv(hashID
);
2560 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2561 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2566 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2569 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2570 signedCert
->ToBeSigned
.cbData
, 0);
2572 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2573 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2574 CryptDestroyHash(hash
);
2576 CryptDestroyKey(key
);
2581 static BOOL
CNG_CalcHash(const WCHAR
*algorithm
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2582 BYTE
**hash_value
, DWORD
*hash_len
)
2584 BCRYPT_HASH_HANDLE hash
= NULL
;
2585 BCRYPT_ALG_HANDLE alg
= NULL
;
2589 if ((status
= BCryptOpenAlgorithmProvider(&alg
, algorithm
, NULL
, 0)))
2592 if ((status
= BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0)))
2595 if ((status
= BCryptHashData(hash
, signedCert
->ToBeSigned
.pbData
, signedCert
->ToBeSigned
.cbData
, 0)))
2598 if ((status
= BCryptGetProperty(hash
, BCRYPT_HASH_LENGTH
, (BYTE
*)hash_len
, sizeof(*hash_len
), &size
, 0)))
2601 if (!(*hash_value
= CryptMemAlloc(*hash_len
)))
2603 status
= STATUS_NO_MEMORY
;
2607 if ((status
= BCryptFinishHash(hash
, *hash_value
, *hash_len
, 0)))
2609 CryptMemFree(*hash_value
);
2614 if (hash
) BCryptDestroyHash(hash
);
2615 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2616 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2620 static BOOL
CNG_ImportECCPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2622 DWORD blob_magic
, ecckey_len
, size
;
2623 BCRYPT_ALG_HANDLE alg
= NULL
;
2624 BCRYPT_ECCKEY_BLOB
*ecckey
;
2625 const WCHAR
*sign_algo
;
2629 if (!pubKeyInfo
->PublicKey
.cbData
)
2631 SetLastError(NTE_BAD_ALGID
);
2635 if (pubKeyInfo
->PublicKey
.pbData
[0] != 0x4)
2637 FIXME("Compressed ECC curves (%02x) not yet supported\n", pubKeyInfo
->PublicKey
.pbData
[0]);
2638 SetLastError(NTE_BAD_ALGID
);
2642 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_OBJECT_IDENTIFIER
, pubKeyInfo
->Algorithm
.Parameters
.pbData
,
2643 pubKeyInfo
->Algorithm
.Parameters
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_curve
, &size
))
2646 if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P256
))
2648 sign_algo
= BCRYPT_ECDSA_P256_ALGORITHM
;
2649 blob_magic
= BCRYPT_ECDSA_PUBLIC_P256_MAGIC
;
2651 else if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P384
))
2653 sign_algo
= BCRYPT_ECDSA_P384_ALGORITHM
;
2654 blob_magic
= BCRYPT_ECDSA_PUBLIC_P384_MAGIC
;
2658 FIXME("Unsupported ecc curve type: %s\n", *ecc_curve
);
2662 LocalFree(ecc_curve
);
2666 SetLastError(NTE_BAD_ALGID
);
2670 if ((status
= BCryptOpenAlgorithmProvider(&alg
, sign_algo
, NULL
, 0)))
2673 ecckey_len
= sizeof(BCRYPT_ECCKEY_BLOB
) + pubKeyInfo
->PublicKey
.cbData
- 1;
2674 if (!(ecckey
= CryptMemAlloc(ecckey_len
)))
2676 status
= STATUS_NO_MEMORY
;
2680 ecckey
->dwMagic
= blob_magic
;
2681 ecckey
->cbKey
= (pubKeyInfo
->PublicKey
.cbData
- 1) / 2;
2682 memcpy(ecckey
+ 1, pubKeyInfo
->PublicKey
.pbData
+ 1, pubKeyInfo
->PublicKey
.cbData
- 1);
2684 status
= BCryptImportKeyPair(alg
, NULL
, BCRYPT_ECCPUBLIC_BLOB
, key
, (BYTE
*)ecckey
, ecckey_len
, 0);
2685 CryptMemFree(ecckey
);
2688 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2689 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2693 static BOOL
CNG_ImportRSAPubKey(CERT_PUBLIC_KEY_INFO
*info
, BCRYPT_KEY_HANDLE
*key
)
2695 DWORD size
, modulus_len
, i
;
2697 RSAPUBKEY
*rsapubkey
;
2698 const WCHAR
*rsa_algo
;
2699 BCRYPT_ALG_HANDLE alg
= NULL
;
2700 BCRYPT_RSAKEY_BLOB
*rsakey
;
2704 if (!info
->PublicKey
.cbData
)
2706 SetLastError(NTE_BAD_ALGID
);
2710 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, RSA_CSP_PUBLICKEYBLOB
, info
->PublicKey
.pbData
,
2711 info
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &hdr
, &size
))
2713 WARN("CryptDecodeObjectEx failed\n");
2717 if (hdr
->aiKeyAlg
== CALG_RSA_KEYX
)
2718 rsa_algo
= BCRYPT_RSA_ALGORITHM
;
2719 else if (hdr
->aiKeyAlg
== CALG_RSA_SIGN
)
2720 rsa_algo
= BCRYPT_RSA_SIGN_ALGORITHM
;
2723 FIXME("Unsupported RSA algorithm: %#x\n", hdr
->aiKeyAlg
);
2725 SetLastError(NTE_BAD_ALGID
);
2729 if ((status
= BCryptOpenAlgorithmProvider(&alg
, rsa_algo
, NULL
, 0)))
2732 rsapubkey
= (RSAPUBKEY
*)(hdr
+ 1);
2734 modulus_len
= size
- sizeof(*hdr
) - sizeof(*rsapubkey
);
2735 if (modulus_len
!= rsapubkey
->bitlen
/ 8)
2736 FIXME("RSA pubkey has wrong modulus_len %lu\n", modulus_len
);
2738 size
= sizeof(*rsakey
) + sizeof(ULONG
) + modulus_len
;
2740 if (!(rsakey
= CryptMemAlloc(size
)))
2742 status
= STATUS_NO_MEMORY
;
2746 rsakey
->Magic
= BCRYPT_RSAPUBLIC_MAGIC
;
2747 rsakey
->BitLength
= rsapubkey
->bitlen
;
2748 rsakey
->cbPublicExp
= sizeof(ULONG
);
2749 rsakey
->cbModulus
= modulus_len
;
2750 rsakey
->cbPrime1
= 0;
2751 rsakey
->cbPrime2
= 0;
2753 d
= (BYTE
*)(rsakey
+ 1);
2754 /* According to MSDN modulus and pubexp are in LE while
2755 * BCRYPT_RSAKEY_BLOB is supposed to have them in BE format */
2756 *(ULONG
*)d
= RtlUlongByteSwap(rsapubkey
->pubexp
);
2758 s
= (BYTE
*)(rsapubkey
+ 1);
2759 for (i
= 0; i
< modulus_len
; i
++)
2760 d
[i
] = s
[modulus_len
- i
- 1];
2762 status
= BCryptImportKeyPair(alg
, NULL
, BCRYPT_RSAPUBLIC_BLOB
, key
, (BYTE
*)rsakey
, size
, 0);
2763 CryptMemFree(rsakey
);
2767 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2768 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2772 BOOL
CNG_ImportPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2774 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_ECC_PUBLIC_KEY
))
2775 return CNG_ImportECCPubKey(pubKeyInfo
, key
);
2777 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_RSA_RSA
))
2778 return CNG_ImportRSAPubKey(pubKeyInfo
, key
);
2780 FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo
->Algorithm
.pszObjId
));
2781 SetLastError(NTE_BAD_ALGID
);
2785 static BOOL
CNG_PrepareSignatureECC(BYTE
*encoded_sig
, DWORD encoded_size
, BYTE
**sig_value
, DWORD
*sig_len
)
2787 CERT_ECC_SIGNATURE
*ecc_sig
;
2791 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ECC_SIGNATURE
, encoded_sig
, encoded_size
,
2792 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_sig
, &size
))
2795 if (!ecc_sig
->r
.cbData
|| !ecc_sig
->s
.cbData
)
2798 SetLastError(ERROR_INVALID_DATA
);
2802 *sig_len
= ecc_sig
->r
.cbData
+ ecc_sig
->s
.cbData
;
2803 if (!(*sig_value
= CryptMemAlloc(*sig_len
)))
2806 SetLastError(ERROR_OUTOFMEMORY
);
2810 for (i
= 0; i
< ecc_sig
->r
.cbData
; i
++)
2811 (*sig_value
)[i
] = ecc_sig
->r
.pbData
[ecc_sig
->r
.cbData
- i
- 1];
2812 for (i
= 0; i
< ecc_sig
->s
.cbData
; i
++)
2813 (*sig_value
)[ecc_sig
->r
.cbData
+ i
] = ecc_sig
->s
.pbData
[ecc_sig
->s
.cbData
- i
- 1];
2819 static BOOL
CNG_PrepareSignature(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2820 BYTE
**sig_value
, DWORD
*sig_len
)
2826 if (!signedCert
->Signature
.cbData
)
2828 SetLastError(ERROR_INVALID_DATA
);
2832 if (!(encoded_sig
= CryptMemAlloc(signedCert
->Signature
.cbData
)))
2834 SetLastError(ERROR_OUTOFMEMORY
);
2838 for (i
= 0; i
< signedCert
->Signature
.cbData
; i
++)
2839 encoded_sig
[i
] = signedCert
->Signature
.pbData
[signedCert
->Signature
.cbData
- i
- 1];
2841 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_ECC_PUBLIC_KEY
))
2842 ret
= CNG_PrepareSignatureECC(encoded_sig
, signedCert
->Signature
.cbData
, sig_value
, sig_len
);
2845 FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo
->Algorithm
.pszObjId
));
2846 SetLastError(NTE_BAD_ALGID
);
2849 CryptMemFree(encoded_sig
);
2853 static BOOL
CNG_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2854 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2856 BCRYPT_KEY_HANDLE key
= NULL
;
2857 BYTE
*hash_value
= NULL
, *sig_value
;
2858 DWORD hash_len
, sig_len
;
2862 ret
= CNG_ImportPubKey(pubKeyInfo
, &key
);
2865 ret
= CNG_CalcHash(info
->pwszCNGAlgid
, signedCert
, &hash_value
, &hash_len
);
2868 ret
= CNG_PrepareSignature(pubKeyInfo
, signedCert
, &sig_value
, &sig_len
);
2871 status
= BCryptVerifySignature(key
, NULL
, hash_value
, hash_len
, sig_value
, sig_len
, 0);
2874 FIXME("Failed to verify signature: %08lx\n", status
);
2875 SetLastError(RtlNtStatusToDosError(status
));
2878 CryptMemFree(sig_value
);
2880 CryptMemFree(hash_value
);
2882 BCryptDestroyKey(key
);
2888 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2889 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2891 CCRYPT_OID_INFO
*info
;
2893 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2894 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2896 SetLastError(NTE_BAD_ALGID
);
2900 if (info
->u
.Algid
== CALG_OID_INFO_CNG_ONLY
)
2901 return CNG_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2903 return CRYPT_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2906 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2907 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2908 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2911 CRYPT_DATA_BLOB subjectBlob
;
2913 TRACE("(%08Ix, %ld, %ld, %p, %ld, %p, %08lx, %p)\n", hCryptProv
,
2914 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2915 dwFlags
, pvReserved
);
2917 switch (dwSubjectType
)
2919 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2921 PCRYPT_DATA_BLOB blob
= pvSubject
;
2923 subjectBlob
.pbData
= blob
->pbData
;
2924 subjectBlob
.cbData
= blob
->cbData
;
2927 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2929 PCERT_CONTEXT context
= pvSubject
;
2931 subjectBlob
.pbData
= context
->pbCertEncoded
;
2932 subjectBlob
.cbData
= context
->cbCertEncoded
;
2935 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2937 PCRL_CONTEXT context
= pvSubject
;
2939 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2940 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2944 SetLastError(E_INVALIDARG
);
2950 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2953 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2954 subjectBlob
.pbData
, subjectBlob
.cbData
,
2955 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2956 &signedCert
, &size
);
2959 switch (dwIssuerType
)
2961 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2962 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2963 dwCertEncodingType
, pvIssuer
,
2966 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2967 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2969 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2972 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2973 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2976 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2979 SetLastError(E_INVALIDARG
);
2984 FIXME("unimplemented for NULL signer\n");
2985 SetLastError(E_INVALIDARG
);
2990 SetLastError(E_INVALIDARG
);
2993 LocalFree(signedCert
);
2999 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
3000 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
3002 PCERT_EXTENSION ext
;
3005 TRACE("(%08lx, %p, %p, %ld)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
3008 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
3009 pCertInfo
->rgExtension
);
3012 CRYPT_BIT_BLOB usage
;
3013 DWORD size
= sizeof(usage
);
3015 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
3016 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
3020 if (cbKeyUsage
< usage
.cbData
)
3024 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
3025 if (cbKeyUsage
> usage
.cbData
)
3026 memset(pbKeyUsage
+ usage
.cbData
, 0,
3027 cbKeyUsage
- usage
.cbData
);
3036 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
3037 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
3039 PCERT_ENHKEY_USAGE usage
= NULL
;
3043 if (!pCertContext
|| !pcbUsage
)
3045 SetLastError(ERROR_INVALID_PARAMETER
);
3049 TRACE("(%p, %08lx, %p, %ld)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
3051 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
3055 if (CertGetCertificateContextProperty(pCertContext
,
3056 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
3058 LPBYTE buf
= CryptMemAlloc(propSize
);
3062 if (CertGetCertificateContextProperty(pCertContext
,
3063 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
3065 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
3066 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
3067 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
3073 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
3075 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
3076 pCertContext
->pCertInfo
->cExtension
,
3077 pCertContext
->pCertInfo
->rgExtension
);
3081 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
3082 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
3083 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
3088 /* If a particular location is specified, this should fail. Otherwise
3089 * it should succeed with an empty usage. (This is true on Win2k and
3090 * later, which we emulate.)
3094 SetLastError(CRYPT_E_NOT_FOUND
);
3098 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
3104 *pcbUsage
= bytesNeeded
;
3105 else if (*pcbUsage
< bytesNeeded
)
3107 SetLastError(ERROR_MORE_DATA
);
3108 *pcbUsage
= bytesNeeded
;
3113 *pcbUsage
= bytesNeeded
;
3117 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
3118 sizeof(CERT_ENHKEY_USAGE
) +
3119 usage
->cUsageIdentifier
* sizeof(LPSTR
));
3121 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
3122 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
3123 sizeof(CERT_ENHKEY_USAGE
));
3124 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3126 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3127 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
3128 nextOID
+= strlen(nextOID
) + 1;
3132 pUsage
->cUsageIdentifier
= 0;
3137 TRACE("returning %d\n", ret
);
3141 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
3142 PCERT_ENHKEY_USAGE pUsage
)
3146 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
3150 CRYPT_DATA_BLOB blob
= { 0, NULL
};
3152 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
3153 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
3156 ret
= CertSetCertificateContextProperty(pCertContext
,
3157 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
3158 LocalFree(blob
.pbData
);
3162 ret
= CertSetCertificateContextProperty(pCertContext
,
3163 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
3167 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
3168 LPCSTR pszUsageIdentifier
)
3173 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
3175 if (CertGetEnhancedKeyUsage(pCertContext
,
3176 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
3178 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
3182 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3183 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
3187 BOOL exists
= FALSE
;
3189 /* Make sure usage doesn't already exist */
3190 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
3192 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
3193 pszUsageIdentifier
))
3198 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
3199 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3205 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
3206 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
3207 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
3208 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
3209 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3211 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3212 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
3213 nextOID
+= strlen(nextOID
) + 1;
3215 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3216 strcpy(nextOID
, pszUsageIdentifier
);
3217 newUsage
->cUsageIdentifier
= i
+ 1;
3218 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
3219 CryptMemFree(newUsage
);
3225 CryptMemFree(usage
);
3232 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
3233 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3237 usage
->rgpszUsageIdentifier
=
3238 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
3239 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
3240 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
3241 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
3242 usage
->cUsageIdentifier
= 1;
3243 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
3244 CryptMemFree(usage
);
3252 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
3253 LPCSTR pszUsageIdentifier
)
3257 CERT_ENHKEY_USAGE usage
;
3259 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
3261 size
= sizeof(usage
);
3262 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3263 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
3264 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3266 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3270 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3271 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
3274 if (pUsage
->cUsageIdentifier
)
3279 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
3281 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
3282 pszUsageIdentifier
))
3284 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
3285 pUsage
->rgpszUsageIdentifier
[i
] =
3286 pUsage
->rgpszUsageIdentifier
[i
+ 1];
3288 pUsage
->cUsageIdentifier
--;
3289 /* Remove the usage if it's empty */
3290 if (pUsage
->cUsageIdentifier
)
3291 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
3293 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
3296 CryptMemFree(pUsage
);
3303 /* it fit in an empty usage, therefore there's nothing to remove */
3315 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
3317 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
3319 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3321 if (indexIndex
+ 1 > field
->cIndexes
)
3323 if (field
->cIndexes
)
3324 field
->indexes
= CryptMemRealloc(field
->indexes
,
3325 (indexIndex
+ 1) * sizeof(DWORD
));
3327 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
3330 field
->indexes
[indexIndex
] = 0;
3331 field
->cIndexes
= indexIndex
+ 1;
3335 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
3338 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
3341 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3343 assert(field
->cIndexes
);
3344 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
3348 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
3349 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
3352 DWORD i
, cbOIDs
= 0;
3353 BOOL allUsagesValid
= TRUE
;
3354 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
3356 TRACE("(%ld, %p, %d, %p, %ld)\n", cCerts
, rghCerts
, *cNumOIDs
,
3359 for (i
= 0; i
< cCerts
; i
++)
3361 CERT_ENHKEY_USAGE usage
;
3362 DWORD size
= sizeof(usage
);
3364 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
3365 /* Success is deliberately ignored: it implies all usages are valid */
3366 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3368 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3370 allUsagesValid
= FALSE
;
3373 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
3376 if (!validUsages
.cUsageIdentifier
)
3380 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
3381 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
3382 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3383 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
3385 validUsages
.rgpszUsageIdentifier
=
3386 CryptMemAlloc(cbOIDs
);
3387 if (validUsages
.rgpszUsageIdentifier
)
3389 LPSTR nextOID
= (LPSTR
)
3390 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
3391 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3393 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3395 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
3396 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
3397 pUsage
->rgpszUsageIdentifier
[j
]);
3398 nextOID
+= lstrlenA(nextOID
) + 1;
3404 struct BitField validIndexes
= { 0, NULL
};
3405 DWORD j
, k
, numRemoved
= 0;
3407 /* Merge: build a bitmap of all the indexes of
3408 * validUsages.rgpszUsageIdentifier that are in pUsage.
3410 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
3412 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
3414 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
3415 validUsages
.rgpszUsageIdentifier
[k
]))
3417 CRYPT_SetBitInField(&validIndexes
, k
);
3422 /* Merge by removing from validUsages those that are
3423 * not in the bitmap.
3425 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3427 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
3429 if (j
< validUsages
.cUsageIdentifier
- 1)
3431 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
3432 &validUsages
.rgpszUsageIdentifier
[j
+
3434 (validUsages
.cUsageIdentifier
- numRemoved
3435 - j
- 1) * sizeof(LPSTR
));
3437 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
3439 validUsages
.cUsageIdentifier
--;
3443 validUsages
.cUsageIdentifier
--;
3446 CryptMemFree(validIndexes
.indexes
);
3449 CryptMemFree(pUsage
);
3461 *cNumOIDs
= validUsages
.cUsageIdentifier
;
3464 else if (*pcbOIDs
< cbOIDs
)
3467 SetLastError(ERROR_MORE_DATA
);
3472 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
3473 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3476 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
3478 rghOIDs
[i
] = nextOID
;
3479 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
3480 nextOID
+= lstrlenA(nextOID
) + 1;
3484 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
3485 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
3486 TRACE("returning %d\n", ret
);
3490 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
3491 * pInfo is NULL, from the attributes of hProv.
3493 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
3494 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
3496 CRYPT_KEY_PROV_INFO info
= { 0 };
3504 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
3507 LPSTR szContainer
= CryptMemAlloc(size
);
3511 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
3512 (BYTE
*)szContainer
, &size
, 0);
3515 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3519 info
.pwszContainerName
= CryptMemAlloc(len
*
3521 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3522 info
.pwszContainerName
, len
);
3525 CryptMemFree(szContainer
);
3528 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
3531 LPSTR szProvider
= CryptMemAlloc(size
);
3535 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
3539 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3543 info
.pwszProvName
= CryptMemAlloc(len
*
3545 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3546 info
.pwszProvName
, len
);
3549 CryptMemFree(szProvider
);
3552 /* in case no CRYPT_KEY_PROV_INFO given,
3553 * we always use AT_SIGNATURE key spec
3555 info
.dwKeySpec
= AT_SIGNATURE
;
3556 size
= sizeof(info
.dwProvType
);
3557 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
3560 info
.dwProvType
= PROV_RSA_FULL
;
3564 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
3569 CryptMemFree(info
.pwszContainerName
);
3570 CryptMemFree(info
.pwszProvName
);
3574 /* Creates a signed certificate context from the unsigned, encoded certificate
3575 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
3577 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
3578 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
3580 PCCERT_CONTEXT context
= NULL
;
3584 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3585 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
3588 LPBYTE sig
= CryptMemAlloc(sigSize
);
3590 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3591 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
3594 CERT_SIGNED_CONTENT_INFO signedInfo
;
3595 BYTE
*encodedSignedCert
= NULL
;
3596 DWORD encodedSignedCertSize
= 0;
3598 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
3599 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
3600 signedInfo
.SignatureAlgorithm
= *sigAlgo
;
3601 signedInfo
.Signature
.cbData
= sigSize
;
3602 signedInfo
.Signature
.pbData
= sig
;
3603 signedInfo
.Signature
.cUnusedBits
= 0;
3604 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
3605 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
3606 &encodedSignedCert
, &encodedSignedCertSize
);
3609 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3610 encodedSignedCert
, encodedSignedCertSize
);
3611 LocalFree(encodedSignedCert
);
3619 /* Copies data from the parameters into info, where:
3620 * pSerialNumber: The serial number. Must not be NULL.
3621 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3623 * pSignatureAlgorithm: Optional.
3624 * pStartTime: The starting time of the certificate. If NULL, the current
3625 * system time is used.
3626 * pEndTime: The ending time of the certificate. If NULL, one year past the
3627 * starting time is used.
3628 * pubKey: The public key of the certificate. Must not be NULL.
3629 * pExtensions: Extensions to be included with the certificate. Optional.
3631 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3632 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3633 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3634 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3635 const CERT_EXTENSIONS
*pExtensions
)
3637 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3640 assert(pSerialNumber
);
3641 assert(pSubjectIssuerBlob
);
3644 if (pExtensions
&& pExtensions
->cExtension
)
3645 info
->dwVersion
= CERT_V3
;
3647 info
->dwVersion
= CERT_V1
;
3648 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3649 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3650 if (pSignatureAlgorithm
)
3651 info
->SignatureAlgorithm
= *pSignatureAlgorithm
;
3654 info
->SignatureAlgorithm
.pszObjId
= oid
;
3655 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3656 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3658 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3659 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3661 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3663 GetSystemTimeAsFileTime(&info
->NotBefore
);
3665 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3670 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3673 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3676 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3677 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3678 info
->SubjectPublicKeyInfo
= *pubKey
;
3681 info
->cExtension
= pExtensions
->cExtension
;
3682 info
->rgExtension
= pExtensions
->rgExtension
;
3686 info
->cExtension
= 0;
3687 info
->rgExtension
= NULL
;
3691 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3692 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3693 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3695 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3697 HCRYPTPROV hProv
= 0;
3698 HMODULE rpcrt
= LoadLibraryW(L
"rpcrt4");
3702 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3704 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3706 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3707 rpcrt
, "RpcStringFreeA");
3709 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3712 RPC_STATUS status
= uuidCreate(&uuid
);
3714 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3716 unsigned char *uuidStr
;
3718 status
= uuidToString(&uuid
, &uuidStr
);
3719 if (status
== RPC_S_OK
)
3721 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3722 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3728 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3730 CryptDestroyKey(key
);
3732 rpcStringFree(&uuidStr
);
3741 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3742 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3743 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3744 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3745 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3747 PCCERT_CONTEXT context
= NULL
;
3748 BOOL ret
, releaseContext
= FALSE
;
3749 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3750 DWORD pubKeySize
= 0, dwKeySpec
;
3752 TRACE("(%08Ix, %p, %08lx, %p, %p, %p, %p, %p)\n", hProv
,
3753 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3754 pExtensions
, pExtensions
);
3756 if(!pSubjectIssuerBlob
)
3758 SetLastError(ERROR_INVALID_PARAMETER
);
3762 dwKeySpec
= pKeyProvInfo
? pKeyProvInfo
->dwKeySpec
: AT_SIGNATURE
;
3767 hProv
= CRYPT_CreateKeyProv();
3768 releaseContext
= TRUE
;
3770 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3772 SetLastError(NTE_BAD_FLAGS
);
3778 /* acquire the context using the given information*/
3779 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3780 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3781 pKeyProvInfo
->dwFlags
);
3784 if(GetLastError() != NTE_BAD_KEYSET
)
3786 /* create the key set */
3787 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3788 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3789 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3793 /* check if the key is here */
3794 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3797 if (NTE_NO_KEY
== GetLastError())
3798 { /* generate the key */
3799 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3803 CryptReleaseContext(hProv
,0);
3804 SetLastError(NTE_BAD_KEYSET
);
3808 CryptDestroyKey(hKey
);
3809 releaseContext
= TRUE
;
3813 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3817 pubKey
= CryptMemAlloc(pubKeySize
);
3820 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3821 pubKey
, &pubKeySize
);
3824 CERT_INFO info
= { 0 };
3825 CRYPT_DER_BLOB blob
= { 0, NULL
};
3827 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3829 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3830 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3831 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3832 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3833 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3837 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3838 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3839 &info
.SignatureAlgorithm
);
3841 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3842 blob
.pbData
, blob
.cbData
);
3843 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3844 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3845 LocalFree(blob
.pbData
);
3848 CryptMemFree(pubKey
);
3852 CryptReleaseContext(hProv
, 0);
3856 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3857 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3858 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3859 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3861 FIXME("(0x%lx, %ld, %p, %p, 0x%lx, %p, %p): stub\n", dwEncodingType
,
3862 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3863 pVerifyUsageStatus
);
3864 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3868 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3869 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3870 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3872 TRACE("(0x%lx, 0x%lx, %p, %ld, 0x%08lx, %p)\n", dwContextType
, dwEncodingType
,
3873 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3877 FIXME("dwFlags 0x%08lx not handled\n", dwFlags
);
3882 FIXME("pCreatePara not handled\n");
3886 switch (dwContextType
)
3888 case CERT_STORE_CERTIFICATE_CONTEXT
:
3889 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3890 case CERT_STORE_CRL_CONTEXT
:
3891 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3892 case CERT_STORE_CTL_CONTEXT
:
3893 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3895 WARN("unknown context type: 0x%lx\n", dwContextType
);
3900 BOOL WINAPI
CryptSetKeyIdentifierProperty(const CRYPT_HASH_BLOB
*pKeyIdentifier
, DWORD dwPropId
,
3901 DWORD dwFlags
, LPCWSTR pwszComputerName
, void *pvReserved
, const void *pvData
)
3903 FIXME("(%p, 0x%lx, 0x%lx, %s, %p, %p): stub\n", pKeyIdentifier
, dwPropId
, dwFlags
,
3904 debugstr_w(pwszComputerName
), pvReserved
, pvData
);