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, %08x, %p, %d, %08x, %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, %d)\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, %d)\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 %d\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, %08x, %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, %08x, %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("(%08x, %p, %d)\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, %d)\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
;
437 static BOOL
CertContext_GetProperty(cert_t
*cert
, DWORD dwPropId
,
438 void *pvData
, DWORD
*pcbData
)
441 CRYPT_DATA_BLOB blob
;
443 TRACE("(%p, %d, %p, %p)\n", cert
, dwPropId
, pvData
, pcbData
);
445 if (cert
->base
.properties
)
446 ret
= ContextPropertyList_FindProperty(cert
->base
.properties
, dwPropId
, &blob
);
451 CERT_KEY_CONTEXT ctx
;
452 if (dwPropId
== CERT_KEY_CONTEXT_PROP_ID
)
454 CRYPT_ConvertKeyContext((const struct store_CERT_KEY_CONTEXT
*)blob
.pbData
, &ctx
);
455 blob
.pbData
= (BYTE
*)&ctx
;
456 blob
.cbData
= ctx
.cbSize
;
458 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
462 /* Implicit properties */
465 case CERT_SHA1_HASH_PROP_ID
:
466 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_SHA1
,
467 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
470 case CERT_MD5_HASH_PROP_ID
:
471 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
472 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
475 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
476 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
477 cert
->ctx
.pCertInfo
->Subject
.pbData
,
478 cert
->ctx
.pCertInfo
->Subject
.cbData
,
481 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
482 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
483 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
484 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
487 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
488 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
489 cert
->ctx
.pCertInfo
->SerialNumber
.pbData
,
490 cert
->ctx
.pCertInfo
->SerialNumber
.cbData
,
493 case CERT_SIGNATURE_HASH_PROP_ID
:
494 ret
= CryptHashToBeSigned(0, cert
->ctx
.dwCertEncodingType
,
495 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
499 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
501 ret
= CertContext_SetProperty(cert
, dwPropId
, 0, &blob
);
504 case CERT_KEY_IDENTIFIER_PROP_ID
:
506 PCERT_EXTENSION ext
= CertFindExtension(
507 szOID_SUBJECT_KEY_IDENTIFIER
, cert
->ctx
.pCertInfo
->cExtension
,
508 cert
->ctx
.pCertInfo
->rgExtension
);
512 CRYPT_DATA_BLOB value
;
513 DWORD size
= sizeof(value
);
515 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
516 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
517 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
521 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
523 CertContext_SetProperty(cert
, dwPropId
, 0, &value
);
527 SetLastError(ERROR_INVALID_DATA
);
531 SetLastError(CRYPT_E_NOT_FOUND
);
534 TRACE("returning %d\n", ret
);
538 /* 64-bit compatible layout, so that 64-bit crypt32 is able to read
539 * the structure saved by 32-bit crypt32.
543 ULONG64 pwszContainerName
;
544 ULONG64 pwszProvName
;
550 } store_CRYPT_KEY_PROV_INFO
;
558 } store_CRYPT_KEY_PROV_PARAM
;
560 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO buf
)
562 CRYPT_KEY_PROV_INFO info
;
563 store_CRYPT_KEY_PROV_INFO
*store
= (store_CRYPT_KEY_PROV_INFO
*)buf
;
564 BYTE
*p
= (BYTE
*)(store
+ 1);
566 if (store
->pwszContainerName
)
568 info
.pwszContainerName
= (LPWSTR
)((BYTE
*)store
+ store
->pwszContainerName
);
569 p
+= (lstrlenW(info
.pwszContainerName
) + 1) * sizeof(WCHAR
);
572 info
.pwszContainerName
= NULL
;
574 if (store
->pwszProvName
)
576 info
.pwszProvName
= (LPWSTR
)((BYTE
*)store
+ store
->pwszProvName
);
577 p
+= (lstrlenW(info
.pwszProvName
) + 1) * sizeof(WCHAR
);
580 info
.pwszProvName
= NULL
;
582 info
.dwProvType
= store
->dwProvType
;
583 info
.dwFlags
= store
->dwFlags
;
584 info
.dwKeySpec
= store
->dwKeySpec
;
585 info
.cProvParam
= store
->cProvParam
;
591 info
.rgProvParam
= (CRYPT_KEY_PROV_PARAM
*)p
;
593 for (i
= 0; i
< store
->cProvParam
; i
++)
595 CRYPT_KEY_PROV_PARAM param
;
596 store_CRYPT_KEY_PROV_PARAM
*store_param
;
598 store_param
= (store_CRYPT_KEY_PROV_PARAM
*)p
;
599 p
+= sizeof(*store_param
);
601 param
.dwParam
= store_param
[i
].dwParam
;
602 param
.dwFlags
= store_param
[i
].dwFlags
;
603 param
.cbData
= store_param
[i
].cbData
;
604 param
.pbData
= param
.cbData
? p
: NULL
;
605 p
+= store_param
[i
].cbData
;
607 memcpy(&info
.rgProvParam
[i
], ¶m
, sizeof(param
));
611 info
.rgProvParam
= NULL
;
613 TRACE("%s,%s,%u,%08x,%u,%p,%u\n", debugstr_w(info
.pwszContainerName
), debugstr_w(info
.pwszProvName
),
614 info
.dwProvType
, info
.dwFlags
, info
.cProvParam
, info
.rgProvParam
, info
.dwKeySpec
);
619 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
620 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
622 cert_t
*cert
= cert_from_ptr(pCertContext
);
625 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
630 case CERT_CERT_PROP_ID
:
631 case CERT_CRL_PROP_ID
:
632 case CERT_CTL_PROP_ID
:
633 SetLastError(E_INVALIDARG
);
636 case CERT_ACCESS_STATE_PROP_ID
:
637 ret
= CertGetStoreProperty(cert
->ctx
.hCertStore
, dwPropId
, pvData
, pcbData
);
639 case CERT_KEY_PROV_HANDLE_PROP_ID
:
641 CERT_KEY_CONTEXT keyContext
;
642 DWORD size
= sizeof(keyContext
);
644 ret
= CertContext_GetProperty(cert
,
645 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
647 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
648 sizeof(keyContext
.hCryptProv
));
651 case CERT_KEY_PROV_INFO_PROP_ID
:
652 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
,
655 CRYPT_FixKeyProvInfoPointers(pvData
);
658 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
,
662 TRACE("returning %d\n", ret
);
666 /* Copies key provider info from from into to, where to is assumed to be a
667 * contiguous buffer of memory large enough for from and all its associated
668 * data, but whose pointers are uninitialized.
669 * Upon return, to contains a contiguous copy of from, packed in the following
671 * - store_CRYPT_KEY_PROV_INFO
672 * - pwszContainerName
674 * - store_CRYPT_KEY_PROV_PARAM[0]
675 * - store_CRYPT_KEY_PROV_PARAM[0].data
678 static void CRYPT_CopyKeyProvInfo(store_CRYPT_KEY_PROV_INFO
*to
, const CRYPT_KEY_PROV_INFO
*from
)
682 store_CRYPT_KEY_PROV_PARAM
*param
;
684 p
= (BYTE
*)(to
+ 1);
686 if (from
->pwszContainerName
)
688 to
->pwszContainerName
= p
- (BYTE
*)to
;
689 lstrcpyW((LPWSTR
)p
, from
->pwszContainerName
);
690 p
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
693 to
->pwszContainerName
= 0;
695 if (from
->pwszProvName
)
697 to
->pwszProvName
= p
- (BYTE
*)to
;
698 lstrcpyW((LPWSTR
)p
, from
->pwszProvName
);
699 p
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
702 to
->pwszProvName
= 0;
704 to
->dwProvType
= from
->dwProvType
;
705 to
->dwFlags
= from
->dwFlags
;
706 to
->cProvParam
= from
->cProvParam
;
708 to
->dwKeySpec
= from
->dwKeySpec
;
710 for (i
= 0; i
< to
->cProvParam
; i
++)
712 param
= (store_CRYPT_KEY_PROV_PARAM
*)p
;
715 param
->dwParam
= from
->rgProvParam
[i
].dwParam
;
717 param
->cbData
= from
->rgProvParam
[i
].cbData
;
718 param
->dwFlags
= from
->rgProvParam
[i
].dwFlags
;
719 memcpy(p
, from
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].cbData
);
720 p
+= from
->rgProvParam
[i
].cbData
;
724 static BOOL
CertContext_SetKeyProvInfoProperty(CONTEXT_PROPERTY_LIST
*properties
,
725 const CRYPT_KEY_PROV_INFO
*info
)
728 DWORD size
= sizeof(store_CRYPT_KEY_PROV_INFO
), i
;
731 if (info
->pwszContainerName
)
732 size
+= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
733 if (info
->pwszProvName
)
734 size
+= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
736 for (i
= 0; i
< info
->cProvParam
; i
++)
737 size
+= sizeof(store_CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
739 buf
= CryptMemAlloc(size
);
742 CRYPT_CopyKeyProvInfo((store_CRYPT_KEY_PROV_INFO
*)buf
, info
);
743 ret
= ContextPropertyList_SetProperty(properties
,
744 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
752 static BOOL
CertContext_SetKeyContextProperty(CONTEXT_PROPERTY_LIST
*properties
,
753 const CERT_KEY_CONTEXT
*keyContext
)
755 struct store_CERT_KEY_CONTEXT ctx
;
757 ctx
.cbSize
= sizeof(ctx
);
758 ctx
.hCryptProv
= keyContext
->hCryptProv
;
759 ctx
.dwKeySpec
= keyContext
->dwKeySpec
;
761 return ContextPropertyList_SetProperty(properties
, CERT_KEY_CONTEXT_PROP_ID
,
762 (const BYTE
*)&ctx
, ctx
.cbSize
);
765 static BOOL
CertContext_SetProperty(cert_t
*cert
, DWORD dwPropId
,
766 DWORD dwFlags
, const void *pvData
)
770 TRACE("(%p, %d, %08x, %p)\n", cert
, dwPropId
, dwFlags
, pvData
);
772 if (!cert
->base
.properties
)
778 case CERT_AUTO_ENROLL_PROP_ID
:
779 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
780 case CERT_DESCRIPTION_PROP_ID
:
781 case CERT_FRIENDLY_NAME_PROP_ID
:
782 case CERT_HASH_PROP_ID
:
783 case CERT_KEY_IDENTIFIER_PROP_ID
:
784 case CERT_MD5_HASH_PROP_ID
:
785 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
786 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
787 case CERT_PVK_FILE_PROP_ID
:
788 case CERT_SIGNATURE_HASH_PROP_ID
:
789 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
790 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
791 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
792 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
793 case CERT_ENROLLMENT_PROP_ID
:
794 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
795 case CERT_OCSP_RESPONSE_PROP_ID
:
796 case CERT_RENEWAL_PROP_ID
:
800 const CRYPT_DATA_BLOB
*blob
= pvData
;
802 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
803 blob
->pbData
, blob
->cbData
);
807 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
812 case CERT_DATE_STAMP_PROP_ID
:
814 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
815 pvData
, sizeof(FILETIME
));
818 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
822 case CERT_KEY_CONTEXT_PROP_ID
:
825 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
827 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
829 SetLastError(E_INVALIDARG
);
833 ret
= CertContext_SetKeyContextProperty(cert
->base
.properties
, pvData
);
837 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
841 case CERT_KEY_PROV_INFO_PROP_ID
:
843 ret
= CertContext_SetKeyProvInfoProperty(cert
->base
.properties
, pvData
);
846 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
850 case CERT_KEY_PROV_HANDLE_PROP_ID
:
852 CERT_KEY_CONTEXT keyContext
;
853 DWORD size
= sizeof(keyContext
);
855 ret
= CertContext_GetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
859 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
860 CryptReleaseContext(keyContext
.hCryptProv
, 0);
862 keyContext
.cbSize
= sizeof(keyContext
);
864 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
867 keyContext
.hCryptProv
= 0;
868 keyContext
.dwKeySpec
= AT_SIGNATURE
;
870 ret
= CertContext_SetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
875 FIXME("%d: stub\n", dwPropId
);
879 TRACE("returning %d\n", ret
);
883 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
884 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
888 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
890 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
891 * crashes on most of these, I'll be safer.
896 case CERT_ACCESS_STATE_PROP_ID
:
897 case CERT_CERT_PROP_ID
:
898 case CERT_CRL_PROP_ID
:
899 case CERT_CTL_PROP_ID
:
900 SetLastError(E_INVALIDARG
);
903 ret
= CertContext_SetProperty(cert_from_ptr(pCertContext
), dwPropId
, dwFlags
,
905 TRACE("returning %d\n", ret
);
909 /* Acquires the private key using the key provider info, retrieving info from
910 * the certificate if info is NULL. The acquired provider is returned in
911 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
913 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
, DWORD dwFlags
,
914 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
917 BOOL allocated
= FALSE
, ret
= TRUE
;
921 ret
= CertGetCertificateContextProperty(pCert
,
922 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
925 info
= HeapAlloc(GetProcessHeap(), 0, size
);
928 ret
= CertGetCertificateContextProperty(pCert
,
929 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
934 SetLastError(ERROR_OUTOFMEMORY
);
939 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
943 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
944 info
->pwszProvName
, info
->dwProvType
, (dwFlags
& CRYPT_ACQUIRE_SILENT_FLAG
) ? CRYPT_SILENT
: 0);
949 for (i
= 0; i
< info
->cProvParam
; i
++)
951 CryptSetProvParam(*phCryptProv
,
952 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
953 info
->rgProvParam
[i
].dwFlags
);
955 *pdwKeySpec
= info
->dwKeySpec
;
958 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
961 HeapFree(GetProcessHeap(), 0, info
);
965 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
966 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
967 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
969 BOOL ret
= FALSE
, cache
= FALSE
;
970 PCRYPT_KEY_PROV_INFO info
= NULL
;
971 CERT_KEY_CONTEXT keyContext
;
973 PCCERT_CONTEXT cert_in_store
= NULL
;
975 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
976 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
978 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
982 ret
= CertGetCertificateContextProperty(pCert
,
983 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
989 hstore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
990 CERT_SYSTEM_STORE_CURRENT_USER
, L
"My");
993 cert_in_store
= CertFindCertificateInStore(hstore
, pCert
->dwCertEncodingType
, 0,
994 CERT_FIND_EXISTING
, pCert
, NULL
);
997 ret
= CertGetCertificateContextProperty(cert_in_store
, CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
999 pCert
= cert_in_store
;
1002 CertFreeCertificateContext(cert_in_store
);
1003 cert_in_store
= NULL
;
1007 CertCloseStore(hstore
, 0);
1013 info
= HeapAlloc(GetProcessHeap(), 0, size
);
1014 ret
= CertGetCertificateContextProperty(pCert
,
1015 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
1017 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
1020 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
1025 size
= sizeof(keyContext
);
1026 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
1027 &keyContext
, &size
);
1030 *phCryptProv
= keyContext
.hCryptProv
;
1032 *pdwKeySpec
= keyContext
.dwKeySpec
;
1033 if (pfCallerFreeProv
)
1034 *pfCallerFreeProv
= FALSE
;
1039 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, dwFlags
, info
,
1040 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
1043 *phCryptProv
= keyContext
.hCryptProv
;
1045 *pdwKeySpec
= keyContext
.dwKeySpec
;
1048 keyContext
.cbSize
= sizeof(keyContext
);
1049 if (CertSetCertificateContextProperty(pCert
,
1050 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
1052 if (pfCallerFreeProv
)
1053 *pfCallerFreeProv
= FALSE
;
1058 if (pfCallerFreeProv
)
1059 *pfCallerFreeProv
= TRUE
;
1063 HeapFree(GetProcessHeap(), 0, info
);
1065 CertFreeCertificateContext(cert_in_store
);
1069 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
1070 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1073 BOOL matches
= FALSE
;
1075 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
1076 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
1080 /* Need to sign something to verify the sig. What to sign? Why not
1081 * the certificate itself?
1083 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
1084 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
1085 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
1087 BYTE
*certEncoded
= CryptMemAlloc(size
);
1091 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
1092 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
1093 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
1094 NULL
, certEncoded
, &size
))
1096 if (size
== pCert
->cbCertEncoded
&&
1097 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
1100 CryptMemFree(certEncoded
);
1103 CryptReleaseContext(csp
, 0);
1108 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
1109 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1111 CRYPT_KEY_PROV_INFO copy
;
1112 WCHAR containerW
[MAX_PATH
];
1115 MultiByteToWideChar(CP_ACP
, 0, container
, -1, containerW
, ARRAY_SIZE(containerW
));
1116 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
1117 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
1120 copy
= *keyProvInfo
;
1121 copy
.pwszContainerName
= containerW
;
1122 matches
= key_prov_info_matches_cert(pCert
, ©
);
1125 keyProvInfo
->pwszContainerName
=
1126 CryptMemAlloc((lstrlenW(containerW
) + 1) * sizeof(WCHAR
));
1127 if (keyProvInfo
->pwszContainerName
)
1129 lstrcpyW(keyProvInfo
->pwszContainerName
, containerW
);
1130 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
1138 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
1139 * private key matches pCert's public key. Upon success, updates keyProvInfo
1140 * with the matching container's info (free keyProvInfo.pwszContainerName upon
1142 * Returns TRUE if found, FALSE if not.
1144 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
1145 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1147 HCRYPTPROV defProvider
;
1148 BOOL ret
, found
= FALSE
;
1149 char containerA
[MAX_PATH
];
1151 assert(keyProvInfo
->pwszContainerName
== NULL
);
1152 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
1153 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
1154 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
1156 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
1158 while (ret
&& !found
)
1160 DWORD size
= sizeof(containerA
);
1162 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
1163 (BYTE
*)containerA
, &size
, enumFlags
);
1165 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
1166 if (enumFlags
& CRYPT_FIRST
)
1168 enumFlags
&= ~CRYPT_FIRST
;
1169 enumFlags
|= CRYPT_NEXT
;
1172 CryptReleaseContext(defProvider
, 0);
1177 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
1179 BOOL found
= FALSE
, ret
= TRUE
;
1180 DWORD index
= 0, cbProvName
= 0;
1181 CRYPT_KEY_PROV_INFO keyProvInfo
;
1183 TRACE("(%p, %08x)\n", pCert
, dwFlags
);
1185 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
1186 while (ret
&& !found
)
1190 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
1194 if (size
<= cbProvName
)
1195 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1196 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1199 CryptMemFree(keyProvInfo
.pwszProvName
);
1200 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
1201 if (keyProvInfo
.pwszProvName
)
1204 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1205 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1208 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
1209 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
1210 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
1211 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1212 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1214 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
1215 found
= find_key_prov_info_in_provider(pCert
,
1220 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
1221 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1222 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1224 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
1225 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
1226 found
= find_key_prov_info_in_provider(pCert
,
1239 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1241 CryptMemFree(keyProvInfo
.pwszProvName
);
1242 CryptMemFree(keyProvInfo
.pwszContainerName
);
1246 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
1248 BOOL matches
= FALSE
;
1251 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1254 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
1258 if (CertGetCertificateContextProperty(pCert
,
1259 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
1260 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
1261 CryptMemFree(keyProvInfo
);
1267 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
1268 DWORD dwFlags
, void *pvReserved
)
1272 TRACE("(%p, %08x, %p)\n", pCert
, dwFlags
, pvReserved
);
1274 matches
= cert_prov_info_matches_cert(pCert
);
1276 matches
= find_matching_provider(pCert
, dwFlags
);
1280 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1281 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1285 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1287 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1288 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1289 &pCertId2
->SerialNumber
);
1290 TRACE("returning %d\n", ret
);
1294 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1295 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1299 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1301 if (pCertName1
->cbData
== pCertName2
->cbData
)
1303 if (pCertName1
->cbData
)
1304 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1305 pCertName1
->cbData
);
1311 TRACE("returning %d\n", ret
);
1315 /* Returns the number of significant bytes in pInt, where a byte is
1316 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1317 * for negative numbers. pInt is assumed to be little-endian.
1319 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1321 DWORD ret
= pInt
->cbData
;
1325 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1327 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1335 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1336 PCRYPT_INTEGER_BLOB pInt2
)
1341 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1343 cb1
= CRYPT_significantBytes(pInt1
);
1344 cb2
= CRYPT_significantBytes(pInt2
);
1348 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1354 TRACE("returning %d\n", ret
);
1358 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1359 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1363 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1365 /* RSA public key data should start with ASN_SEQUENCE,
1366 * otherwise it's not a RSA_CSP_PUBLICKEYBLOB.
1368 if (!pPublicKey1
->PublicKey
.cbData
|| pPublicKey1
->PublicKey
.pbData
[0] != ASN_SEQUENCE
)
1369 dwCertEncodingType
= 0;
1371 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1373 case 0: /* Seems to mean "raw binary bits" */
1374 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1375 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1377 if (pPublicKey2
->PublicKey
.cbData
)
1378 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1379 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1387 WARN("Unknown encoding type %08x\n", dwCertEncodingType
);
1389 case X509_ASN_ENCODING
:
1391 BLOBHEADER
*pblob1
, *pblob2
;
1394 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1395 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1396 CRYPT_DECODE_ALLOC_FLAG
, &pblob1
, &length
))
1398 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1399 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1400 CRYPT_DECODE_ALLOC_FLAG
, &pblob2
, &length
))
1402 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1403 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1404 *pk2
= (LPVOID
)(pblob2
+ 1);
1405 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1406 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1419 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1420 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1424 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
1426 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1428 SetLastError(ERROR_FILE_NOT_FOUND
);
1431 if (pPublicKey
->Algorithm
.pszObjId
&&
1432 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1434 FIXME("unimplemented for DH public keys\n");
1435 SetLastError(CRYPT_E_ASN1_BADTAG
);
1439 PCCRYPT_OID_INFO info
;
1444 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, pPublicKey
->Algorithm
.pszObjId
, 0);
1449 TRACE("public key algid %#x (%s)\n", info
->u
.Algid
, debugstr_a(pPublicKey
->Algorithm
.pszObjId
));
1451 ret
= CryptImportPublicKeyInfo(I_CryptGetDefaultCryptProv(info
->u
.Algid
), dwCertEncodingType
, pPublicKey
, &key
);
1455 ret
= CryptGetKeyParam(key
, KP_KEYLEN
, (BYTE
*)&len
, &size
, 0);
1456 CryptDestroyKey(key
);
1459 /* fallback to RSA */
1462 ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1463 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1464 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1469 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1471 len
= rsaPubKey
->bitlen
;
1478 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1479 DWORD dwFlags
, const void *pvPara
);
1481 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1482 DWORD dwFlags
, const void *pvPara
)
1486 DWORD size
= sizeof(hash
);
1488 ret
= CertGetCertificateContextProperty(pCertContext
,
1489 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1492 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1494 if (size
== pHash
->cbData
)
1495 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1502 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1503 DWORD dwFlags
, const void *pvPara
)
1507 DWORD size
= sizeof(hash
);
1509 ret
= CertGetCertificateContextProperty(pCertContext
,
1510 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1513 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1515 if (size
== pHash
->cbData
)
1516 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1523 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1524 DWORD dwFlags
, const void *pvPara
)
1526 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1529 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1530 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1532 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1533 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1538 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1539 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1541 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1544 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1545 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1549 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1550 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1552 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1555 /* Matching serial number and subject match.. */
1556 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1557 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1559 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1560 &pCertInfo
->SerialNumber
);
1563 /* failing that, if the serial number and issuer match, we match */
1564 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1565 &pCertInfo
->SerialNumber
);
1567 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1568 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1570 TRACE("returning %d\n", ret
);
1574 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1575 DWORD dwFlags
, const void *pvPara
)
1577 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1580 switch (id
->dwIdChoice
)
1582 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1583 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1584 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1586 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1587 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1589 case CERT_ID_SHA1_HASH
:
1590 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1593 case CERT_ID_KEY_IDENTIFIER
:
1597 ret
= CertGetCertificateContextProperty(pCertContext
,
1598 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1599 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1601 LPBYTE buf
= CryptMemAlloc(size
);
1605 CertGetCertificateContextProperty(pCertContext
,
1606 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1607 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1624 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1625 DWORD dwFlags
, const void *pvPara
)
1627 PCCERT_CONTEXT toCompare
= pvPara
;
1628 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1629 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1632 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1633 DWORD dwFlags
, const void *pvPara
)
1635 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1639 ret
= CertGetCertificateContextProperty(pCertContext
,
1640 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1641 if (ret
&& size
== hash
->cbData
)
1643 LPBYTE buf
= CryptMemAlloc(size
);
1647 CertGetCertificateContextProperty(pCertContext
,
1648 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1649 ret
= !memcmp(buf
, hash
->pbData
, size
);
1660 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1661 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1664 BOOL matches
= FALSE
;
1669 ret
= CertEnumCertificatesInStore(store
, ret
);
1671 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1672 } while (ret
!= NULL
&& !matches
);
1676 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1677 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1679 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1680 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1682 return CertEnumCertificatesInStore(store
, prev
);
1685 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1686 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1689 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1690 PCERT_EXTENSION ext
;
1693 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1694 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1696 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1698 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1699 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1700 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1706 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1708 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1709 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1710 sizeof(CERT_NAME_BLOB
));
1711 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1712 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1714 else if (info
->KeyId
.cbData
)
1716 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1717 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1722 found
= cert_compare_certs_in_store(store
, prev
,
1723 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1727 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1728 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1730 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1732 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1733 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1734 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1740 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1741 info
->AuthorityCertSerialNumber
.cbData
)
1743 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1746 for (i
= 0; !directoryName
&&
1747 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1748 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1749 == CERT_ALT_NAME_DIRECTORY_NAME
)
1751 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1754 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1755 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1756 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1757 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1758 &info
->AuthorityCertSerialNumber
,
1759 sizeof(CRYPT_INTEGER_BLOB
));
1763 FIXME("no supported name type in authority key id2\n");
1767 else if (info
->KeyId
.cbData
)
1769 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1770 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1775 found
= cert_compare_certs_in_store(store
, prev
,
1776 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1781 found
= cert_compare_certs_in_store(store
, prev
,
1782 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1783 dwFlags
, &subject
->pCertInfo
->Issuer
);
1787 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1788 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1790 PCERT_NAME_BLOB name
;
1794 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1795 name
= &pCertContext
->pCertInfo
->Subject
;
1797 name
= &pCertContext
->pCertInfo
->Issuer
;
1798 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1799 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1802 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1806 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1807 CERT_SIMPLE_NAME_STR
, str
, len
);
1809 if (wcsstr(str
, pvPara
))
1817 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1818 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1820 PCCERT_CONTEXT found
= NULL
;
1822 TRACE("%s\n", debugstr_a(pvPara
));
1826 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1827 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1831 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1833 found
= cert_compare_certs_in_store(store
, prev
,
1834 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1839 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1843 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1844 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1846 PCCERT_CONTEXT found
= NULL
;
1848 TRACE("%s\n", debugstr_w(pvPara
));
1852 DWORD len
= lstrlenW(pvPara
);
1853 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1857 wcscpy( str
, pvPara
);
1859 found
= cert_compare_certs_in_store(store
, prev
,
1860 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1865 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1869 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1870 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1871 PCCERT_CONTEXT pPrevCertContext
)
1874 CertFindFunc find
= NULL
;
1875 CertCompareFunc compare
= NULL
;
1878 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1879 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1881 switch (dwType
>> CERT_COMPARE_SHIFT
)
1883 case CERT_COMPARE_ANY
:
1884 find
= find_cert_any
;
1886 case CERT_COMPARE_MD5_HASH
:
1887 compare
= compare_cert_by_md5_hash
;
1889 case CERT_COMPARE_SHA1_HASH
:
1890 compare
= compare_cert_by_sha1_hash
;
1892 case CERT_COMPARE_NAME
:
1893 compare
= compare_cert_by_name
;
1895 case CERT_COMPARE_PUBLIC_KEY
:
1896 compare
= compare_cert_by_public_key
;
1898 case CERT_COMPARE_NAME_STR_A
:
1899 find
= find_cert_by_name_str_a
;
1901 case CERT_COMPARE_NAME_STR_W
:
1902 find
= find_cert_by_name_str_w
;
1904 case CERT_COMPARE_SUBJECT_CERT
:
1905 compare
= compare_cert_by_subject_cert
;
1907 case CERT_COMPARE_KEY_IDENTIFIER
:
1908 cert_id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1909 cert_id
.u
.KeyId
= *(const CRYPT_HASH_BLOB
*)pvPara
;
1912 case CERT_COMPARE_CERT_ID
:
1913 compare
= compare_cert_by_cert_id
;
1915 case CERT_COMPARE_ISSUER_OF
:
1916 find
= find_cert_by_issuer
;
1918 case CERT_COMPARE_EXISTING
:
1919 compare
= compare_existing_cert
;
1921 case CERT_COMPARE_SIGNATURE_HASH
:
1922 compare
= compare_cert_by_signature_hash
;
1925 FIXME("find type %08x unimplemented\n", dwType
);
1929 ret
= find(hCertStore
, dwType
, dwFlags
, pvPara
, pPrevCertContext
);
1931 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1932 compare
, dwType
, dwFlags
, pvPara
);
1936 SetLastError(CRYPT_E_NOT_FOUND
);
1937 TRACE("returning %p\n", ret
);
1941 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1942 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1944 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1948 SetLastError(E_INVALIDARG
);
1951 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1952 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1955 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1956 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1958 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1959 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1961 if (*pdwFlags
& ~supportedFlags
)
1963 SetLastError(E_INVALIDARG
);
1966 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1969 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1972 /* FIXME: what if the CRL has expired? */
1975 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1976 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1977 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1980 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1982 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1984 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1985 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1987 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1989 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1990 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1991 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1992 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1997 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1998 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
2003 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
2004 pPrevIssuerContext
, *pdwFlags
);
2006 if (!pSubjectContext
)
2008 SetLastError(E_INVALIDARG
);
2012 ret
= CertFindCertificateInStore(hCertStore
,
2013 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
2014 pSubjectContext
, pPrevIssuerContext
);
2017 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
2020 CertFreeCertificateContext(ret
);
2023 if (CRYPT_IsCertificateSelfSigned(pSubjectContext
))
2025 CertFreeCertificateContext(ret
);
2027 SetLastError(CRYPT_E_SELF_SIGNED
);
2030 TRACE("returning %p\n", ret
);
2034 typedef struct _OLD_CERT_REVOCATION_STATUS
{
2039 } OLD_CERT_REVOCATION_STATUS
;
2041 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
2042 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
2044 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
2045 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
2046 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
2050 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
2051 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
2053 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
2054 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
2056 SetLastError(E_INVALIDARG
);
2061 static HCRYPTOIDFUNCSET set
= NULL
;
2065 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
2066 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
2072 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
2077 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
2081 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
2085 for (ptr
= dllList
; ret
&& *ptr
;
2086 ptr
+= lstrlenW(ptr
) + 1)
2088 CertVerifyRevocationFunc func
;
2089 HCRYPTOIDFUNCADDR hFunc
;
2091 ret
= CryptGetDefaultOIDFunctionAddress(set
,
2092 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
2095 ret
= func(dwEncodingType
, dwRevType
, cContext
,
2096 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
2097 CryptFreeOIDFunctionAddress(hFunc
, 0);
2101 CryptMemFree(dllList
);
2105 SetLastError(ERROR_OUTOFMEMORY
);
2116 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
2117 CRYPT_ATTRIBUTE rgAttr
[])
2119 PCRYPT_ATTRIBUTE ret
= NULL
;
2122 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
2128 SetLastError(ERROR_INVALID_PARAMETER
);
2132 for (i
= 0; !ret
&& i
< cAttr
; i
++)
2133 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
2138 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
2139 CERT_EXTENSION rgExtensions
[])
2141 PCERT_EXTENSION ret
= NULL
;
2144 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
2150 SetLastError(ERROR_INVALID_PARAMETER
);
2154 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
2155 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
2156 rgExtensions
[i
].pszObjId
))
2157 ret
= &rgExtensions
[i
];
2161 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
2163 PCERT_RDN_ATTR ret
= NULL
;
2166 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
2170 SetLastError(ERROR_INVALID_PARAMETER
);
2174 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
2175 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
2176 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
2177 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
2178 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
2182 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
2183 const CERT_RDN_ATTR
*attr
)
2188 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
2190 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
2192 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
2194 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
2197 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
2200 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2201 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
2203 if (attr
->Value
.cbData
!=
2204 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2206 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2207 match
= !wcsnicmp(nameStr
, attrStr
,
2208 attr
->Value
.cbData
/ sizeof(WCHAR
));
2210 match
= !wcsncmp(nameStr
, attrStr
,
2211 attr
->Value
.cbData
/ sizeof(WCHAR
));
2212 TRACE("%s : %s => %d\n",
2213 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2214 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2220 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2221 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
2223 if (attr
->Value
.cbData
!=
2224 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2226 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2227 match
= !_strnicmp(nameStr
, attrStr
,
2228 attr
->Value
.cbData
);
2230 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
2231 TRACE("%s : %s => %d\n",
2232 debugstr_an(nameStr
, attr
->Value
.cbData
),
2233 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
2241 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
2242 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
2244 CERT_NAME_INFO
*name
;
2249 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
2252 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
2254 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
2255 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
2259 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
2260 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
2262 SetLastError(CRYPT_E_NO_MATCH
);
2268 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
2269 PCERT_INFO pCertInfo
)
2276 GetSystemTimeAsFileTime(&fileTime
);
2277 pTimeToVerify
= &fileTime
;
2279 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
2281 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
2288 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
2289 PCERT_INFO pIssuerInfo
)
2291 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
2293 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
2294 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
2297 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2298 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2299 DWORD
*pcbComputedHash
)
2302 HCRYPTHASH hHash
= 0;
2304 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2305 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2308 hCryptProv
= I_CryptGetDefaultCryptProv(Algid
);
2313 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2316 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2318 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2319 pcbComputedHash
, 0);
2320 CryptDestroyHash(hHash
);
2326 BOOL WINAPI
CryptHashCertificate2(LPCWSTR pwszCNGHashAlgid
, DWORD dwFlags
,
2327 void *pvReserved
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2328 DWORD
*pcbComputedHash
)
2330 BCRYPT_HASH_HANDLE hash
= NULL
;
2331 BCRYPT_ALG_HANDLE alg
= NULL
;
2334 DWORD hash_len_size
;
2336 TRACE("(%s, %08x, %p, %p, %d, %p, %p)\n", debugstr_w(pwszCNGHashAlgid
),
2337 dwFlags
, pvReserved
, pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2339 if ((status
= BCryptOpenAlgorithmProvider(&alg
, pwszCNGHashAlgid
, NULL
, 0)))
2341 if (status
== STATUS_NOT_IMPLEMENTED
)
2342 status
= STATUS_NOT_FOUND
;
2346 if ((status
= BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0)))
2349 if ((status
= BCryptGetProperty(hash
, BCRYPT_HASH_LENGTH
, (BYTE
*)&hash_len
, sizeof(hash_len
), &hash_len_size
, 0)))
2352 if (!pbComputedHash
)
2354 *pcbComputedHash
= hash_len
;
2358 if (*pcbComputedHash
< hash_len
)
2360 status
= ERROR_MORE_DATA
;
2364 *pcbComputedHash
= hash_len
;
2366 if ((status
= BCryptHashData(hash
, (BYTE
*)pbEncoded
, cbEncoded
, 0)))
2369 if ((status
= BCryptFinishHash(hash
, pbComputedHash
, hash_len
, 0)))
2373 if (hash
) BCryptDestroyHash(hash
);
2374 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2375 if (status
) SetLastError(status
);
2379 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2380 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2381 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2384 HCRYPTHASH hHash
= 0;
2386 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2387 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2390 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2393 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2395 SetLastError(ERROR_FILE_NOT_FOUND
);
2403 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2404 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2405 (LPBYTE
)&buf
, &size
);
2408 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2411 ret
= CryptHashData(hHash
, buf
, size
, 0);
2413 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2414 pcbComputedHash
, 0);
2415 CryptDestroyHash(hHash
);
2423 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2424 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2425 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2428 CERT_SIGNED_CONTENT_INFO
*info
;
2431 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv
, dwCertEncodingType
,
2432 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2434 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2435 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2438 PCCRYPT_OID_INFO oidInfo
;
2442 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2443 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2444 info
->SignatureAlgorithm
.pszObjId
, 0);
2447 SetLastError(NTE_BAD_ALGID
);
2452 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2455 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2456 info
->ToBeSigned
.cbData
, 0);
2458 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2459 pcbComputedHash
, 0);
2460 CryptDestroyHash(hHash
);
2468 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2469 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2470 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2471 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2474 PCCRYPT_OID_INFO info
;
2477 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
2478 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2479 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2481 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2482 pSignatureAlgorithm
->pszObjId
, 0);
2485 SetLastError(NTE_BAD_ALGID
);
2488 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2491 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2492 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2495 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2496 cbEncodedToBeSigned
, 0);
2498 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2500 CryptDestroyHash(hHash
);
2507 SetLastError(ERROR_INVALID_PARAMETER
);
2512 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2515 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2516 cbEncodedToBeSigned
, 0);
2518 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2520 CryptDestroyHash(hHash
);
2527 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2528 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2529 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2530 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2533 DWORD encodedSize
, hashSize
;
2535 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2536 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2537 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2539 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2540 NULL
, &encodedSize
);
2543 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2547 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2548 pvStructInfo
, encoded
, &encodedSize
);
2551 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2552 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2553 pvHashAuxInfo
, NULL
, &hashSize
);
2556 PBYTE hash
= CryptMemAlloc(hashSize
);
2560 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2561 dwCertEncodingType
, encoded
, encodedSize
,
2562 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2565 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2567 info
.ToBeSigned
.cbData
= encodedSize
;
2568 info
.ToBeSigned
.pbData
= encoded
;
2569 info
.SignatureAlgorithm
= *pSignatureAlgorithm
;
2570 info
.Signature
.cbData
= hashSize
;
2571 info
.Signature
.pbData
= hash
;
2572 info
.Signature
.cUnusedBits
= 0;
2573 ret
= CryptEncodeObject(dwCertEncodingType
,
2574 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2582 CryptMemFree(encoded
);
2590 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2591 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2592 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2594 CRYPT_DATA_BLOB blob
= { cbEncoded
, (BYTE
*)pbEncoded
};
2596 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2597 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, &blob
,
2598 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2601 static BOOL
CRYPT_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2602 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2606 ALG_ID pubKeyID
, hashID
;
2608 hashID
= info
->u
.Algid
;
2609 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2610 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2613 /* Load the default provider if necessary */
2615 hCryptProv
= I_CryptGetDefaultCryptProv(hashID
);
2616 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2617 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2622 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2625 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2626 signedCert
->ToBeSigned
.cbData
, 0);
2628 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2629 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2630 CryptDestroyHash(hash
);
2632 CryptDestroyKey(key
);
2637 static BOOL
CNG_CalcHash(const WCHAR
*algorithm
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2638 BYTE
**hash_value
, DWORD
*hash_len
)
2640 BCRYPT_HASH_HANDLE hash
= NULL
;
2641 BCRYPT_ALG_HANDLE alg
= NULL
;
2645 if ((status
= BCryptOpenAlgorithmProvider(&alg
, algorithm
, NULL
, 0)))
2648 if ((status
= BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0)))
2651 if ((status
= BCryptHashData(hash
, signedCert
->ToBeSigned
.pbData
, signedCert
->ToBeSigned
.cbData
, 0)))
2654 if ((status
= BCryptGetProperty(hash
, BCRYPT_HASH_LENGTH
, (BYTE
*)hash_len
, sizeof(*hash_len
), &size
, 0)))
2657 if (!(*hash_value
= CryptMemAlloc(*hash_len
)))
2659 status
= STATUS_NO_MEMORY
;
2663 if ((status
= BCryptFinishHash(hash
, *hash_value
, *hash_len
, 0)))
2665 CryptMemFree(*hash_value
);
2670 if (hash
) BCryptDestroyHash(hash
);
2671 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2672 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2676 static BOOL
CNG_ImportECCPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2678 DWORD blob_magic
, ecckey_len
, size
;
2679 BCRYPT_ALG_HANDLE alg
= NULL
;
2680 BCRYPT_ECCKEY_BLOB
*ecckey
;
2681 const WCHAR
*sign_algo
;
2685 if (!pubKeyInfo
->PublicKey
.cbData
)
2687 SetLastError(NTE_BAD_ALGID
);
2691 if (pubKeyInfo
->PublicKey
.pbData
[0] != 0x4)
2693 FIXME("Compressed ECC curves (%02x) not yet supported\n", pubKeyInfo
->PublicKey
.pbData
[0]);
2694 SetLastError(NTE_BAD_ALGID
);
2698 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_OBJECT_IDENTIFIER
, pubKeyInfo
->Algorithm
.Parameters
.pbData
,
2699 pubKeyInfo
->Algorithm
.Parameters
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_curve
, &size
))
2702 if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P256
))
2704 sign_algo
= BCRYPT_ECDSA_P256_ALGORITHM
;
2705 blob_magic
= BCRYPT_ECDSA_PUBLIC_P256_MAGIC
;
2707 else if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P384
))
2709 sign_algo
= BCRYPT_ECDSA_P384_ALGORITHM
;
2710 blob_magic
= BCRYPT_ECDSA_PUBLIC_P384_MAGIC
;
2714 FIXME("Unsupported ecc curve type: %s\n", *ecc_curve
);
2718 LocalFree(ecc_curve
);
2722 SetLastError(NTE_BAD_ALGID
);
2726 if ((status
= BCryptOpenAlgorithmProvider(&alg
, sign_algo
, NULL
, 0)))
2729 ecckey_len
= sizeof(BCRYPT_ECCKEY_BLOB
) + pubKeyInfo
->PublicKey
.cbData
- 1;
2730 if (!(ecckey
= CryptMemAlloc(ecckey_len
)))
2732 status
= STATUS_NO_MEMORY
;
2736 ecckey
->dwMagic
= blob_magic
;
2737 ecckey
->cbKey
= (pubKeyInfo
->PublicKey
.cbData
- 1) / 2;
2738 memcpy(ecckey
+ 1, pubKeyInfo
->PublicKey
.pbData
+ 1, pubKeyInfo
->PublicKey
.cbData
- 1);
2740 status
= BCryptImportKeyPair(alg
, NULL
, BCRYPT_ECCPUBLIC_BLOB
, key
, (BYTE
*)ecckey
, ecckey_len
, 0);
2741 CryptMemFree(ecckey
);
2744 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2745 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2749 static BOOL
CNG_ImportPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2751 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_ECC_PUBLIC_KEY
))
2752 return CNG_ImportECCPubKey(pubKeyInfo
, key
);
2754 FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo
->Algorithm
.pszObjId
));
2755 SetLastError(NTE_BAD_ALGID
);
2759 static BOOL
CNG_PrepareSignatureECC(BYTE
*encoded_sig
, DWORD encoded_size
, BYTE
**sig_value
, DWORD
*sig_len
)
2761 CERT_ECC_SIGNATURE
*ecc_sig
;
2765 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ECC_SIGNATURE
, encoded_sig
, encoded_size
,
2766 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_sig
, &size
))
2769 if (!ecc_sig
->r
.cbData
|| !ecc_sig
->s
.cbData
)
2772 SetLastError(ERROR_INVALID_DATA
);
2776 *sig_len
= ecc_sig
->r
.cbData
+ ecc_sig
->s
.cbData
;
2777 if (!(*sig_value
= CryptMemAlloc(*sig_len
)))
2780 SetLastError(ERROR_OUTOFMEMORY
);
2784 for (i
= 0; i
< ecc_sig
->r
.cbData
; i
++)
2785 (*sig_value
)[i
] = ecc_sig
->r
.pbData
[ecc_sig
->r
.cbData
- i
- 1];
2786 for (i
= 0; i
< ecc_sig
->s
.cbData
; i
++)
2787 (*sig_value
)[ecc_sig
->r
.cbData
+ i
] = ecc_sig
->s
.pbData
[ecc_sig
->s
.cbData
- i
- 1];
2793 static BOOL
CNG_PrepareSignature(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2794 BYTE
**sig_value
, DWORD
*sig_len
)
2800 if (!signedCert
->Signature
.cbData
)
2802 SetLastError(ERROR_INVALID_DATA
);
2806 if (!(encoded_sig
= CryptMemAlloc(signedCert
->Signature
.cbData
)))
2808 SetLastError(ERROR_OUTOFMEMORY
);
2812 for (i
= 0; i
< signedCert
->Signature
.cbData
; i
++)
2813 encoded_sig
[i
] = signedCert
->Signature
.pbData
[signedCert
->Signature
.cbData
- i
- 1];
2815 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_ECC_PUBLIC_KEY
))
2816 ret
= CNG_PrepareSignatureECC(encoded_sig
, signedCert
->Signature
.cbData
, sig_value
, sig_len
);
2819 FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo
->Algorithm
.pszObjId
));
2820 SetLastError(NTE_BAD_ALGID
);
2823 CryptMemFree(encoded_sig
);
2827 static BOOL
CNG_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2828 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2830 BCRYPT_KEY_HANDLE key
= NULL
;
2831 BYTE
*hash_value
= NULL
, *sig_value
;
2832 DWORD hash_len
, sig_len
;
2836 ret
= CNG_ImportPubKey(pubKeyInfo
, &key
);
2839 ret
= CNG_CalcHash(info
->pwszCNGAlgid
, signedCert
, &hash_value
, &hash_len
);
2842 ret
= CNG_PrepareSignature(pubKeyInfo
, signedCert
, &sig_value
, &sig_len
);
2845 status
= BCryptVerifySignature(key
, NULL
, hash_value
, hash_len
, sig_value
, sig_len
, 0);
2848 FIXME("Failed to verify signature: %08x\n", status
);
2849 SetLastError(RtlNtStatusToDosError(status
));
2852 CryptMemFree(sig_value
);
2854 CryptMemFree(hash_value
);
2856 BCryptDestroyKey(key
);
2862 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2863 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2865 CCRYPT_OID_INFO
*info
;
2867 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2868 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2870 SetLastError(NTE_BAD_ALGID
);
2874 if (info
->u
.Algid
== CALG_OID_INFO_CNG_ONLY
)
2875 return CNG_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2877 return CRYPT_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2880 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2881 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2882 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2885 CRYPT_DATA_BLOB subjectBlob
;
2887 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
2888 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2889 dwFlags
, pvReserved
);
2891 switch (dwSubjectType
)
2893 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2895 PCRYPT_DATA_BLOB blob
= pvSubject
;
2897 subjectBlob
.pbData
= blob
->pbData
;
2898 subjectBlob
.cbData
= blob
->cbData
;
2901 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2903 PCERT_CONTEXT context
= pvSubject
;
2905 subjectBlob
.pbData
= context
->pbCertEncoded
;
2906 subjectBlob
.cbData
= context
->cbCertEncoded
;
2909 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2911 PCRL_CONTEXT context
= pvSubject
;
2913 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2914 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2918 SetLastError(E_INVALIDARG
);
2924 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2927 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2928 subjectBlob
.pbData
, subjectBlob
.cbData
,
2929 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2930 &signedCert
, &size
);
2933 switch (dwIssuerType
)
2935 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2936 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2937 dwCertEncodingType
, pvIssuer
,
2940 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2941 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2943 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2946 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2947 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2950 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2953 SetLastError(E_INVALIDARG
);
2958 FIXME("unimplemented for NULL signer\n");
2959 SetLastError(E_INVALIDARG
);
2964 SetLastError(E_INVALIDARG
);
2967 LocalFree(signedCert
);
2973 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2974 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
2976 PCERT_EXTENSION ext
;
2979 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
2982 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
2983 pCertInfo
->rgExtension
);
2986 CRYPT_BIT_BLOB usage
;
2987 DWORD size
= sizeof(usage
);
2989 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
2990 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2994 if (cbKeyUsage
< usage
.cbData
)
2998 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
2999 if (cbKeyUsage
> usage
.cbData
)
3000 memset(pbKeyUsage
+ usage
.cbData
, 0,
3001 cbKeyUsage
- usage
.cbData
);
3010 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
3011 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
3013 PCERT_ENHKEY_USAGE usage
= NULL
;
3017 if (!pCertContext
|| !pcbUsage
)
3019 SetLastError(ERROR_INVALID_PARAMETER
);
3023 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
3025 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
3029 if (CertGetCertificateContextProperty(pCertContext
,
3030 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
3032 LPBYTE buf
= CryptMemAlloc(propSize
);
3036 if (CertGetCertificateContextProperty(pCertContext
,
3037 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
3039 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
3040 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
3041 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
3047 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
3049 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
3050 pCertContext
->pCertInfo
->cExtension
,
3051 pCertContext
->pCertInfo
->rgExtension
);
3055 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
3056 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
3057 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
3062 /* If a particular location is specified, this should fail. Otherwise
3063 * it should succeed with an empty usage. (This is true on Win2k and
3064 * later, which we emulate.)
3068 SetLastError(CRYPT_E_NOT_FOUND
);
3072 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
3078 *pcbUsage
= bytesNeeded
;
3079 else if (*pcbUsage
< bytesNeeded
)
3081 SetLastError(ERROR_MORE_DATA
);
3082 *pcbUsage
= bytesNeeded
;
3087 *pcbUsage
= bytesNeeded
;
3091 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
3092 sizeof(CERT_ENHKEY_USAGE
) +
3093 usage
->cUsageIdentifier
* sizeof(LPSTR
));
3095 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
3096 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
3097 sizeof(CERT_ENHKEY_USAGE
));
3098 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3100 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3101 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
3102 nextOID
+= strlen(nextOID
) + 1;
3106 pUsage
->cUsageIdentifier
= 0;
3111 TRACE("returning %d\n", ret
);
3115 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
3116 PCERT_ENHKEY_USAGE pUsage
)
3120 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
3124 CRYPT_DATA_BLOB blob
= { 0, NULL
};
3126 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
3127 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
3130 ret
= CertSetCertificateContextProperty(pCertContext
,
3131 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
3132 LocalFree(blob
.pbData
);
3136 ret
= CertSetCertificateContextProperty(pCertContext
,
3137 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
3141 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
3142 LPCSTR pszUsageIdentifier
)
3147 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
3149 if (CertGetEnhancedKeyUsage(pCertContext
,
3150 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
3152 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
3156 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3157 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
3161 BOOL exists
= FALSE
;
3163 /* Make sure usage doesn't already exist */
3164 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
3166 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
3167 pszUsageIdentifier
))
3172 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
3173 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3179 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
3180 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
3181 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
3182 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
3183 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3185 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3186 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
3187 nextOID
+= strlen(nextOID
) + 1;
3189 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3190 strcpy(nextOID
, pszUsageIdentifier
);
3191 newUsage
->cUsageIdentifier
= i
+ 1;
3192 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
3193 CryptMemFree(newUsage
);
3199 CryptMemFree(usage
);
3206 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
3207 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3211 usage
->rgpszUsageIdentifier
=
3212 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
3213 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
3214 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
3215 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
3216 usage
->cUsageIdentifier
= 1;
3217 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
3218 CryptMemFree(usage
);
3226 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
3227 LPCSTR pszUsageIdentifier
)
3231 CERT_ENHKEY_USAGE usage
;
3233 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
3235 size
= sizeof(usage
);
3236 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3237 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
3238 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3240 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3244 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3245 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
3248 if (pUsage
->cUsageIdentifier
)
3253 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
3255 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
3256 pszUsageIdentifier
))
3258 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
3259 pUsage
->rgpszUsageIdentifier
[i
] =
3260 pUsage
->rgpszUsageIdentifier
[i
+ 1];
3262 pUsage
->cUsageIdentifier
--;
3263 /* Remove the usage if it's empty */
3264 if (pUsage
->cUsageIdentifier
)
3265 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
3267 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
3270 CryptMemFree(pUsage
);
3277 /* it fit in an empty usage, therefore there's nothing to remove */
3289 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
3291 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
3293 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3295 if (indexIndex
+ 1 > field
->cIndexes
)
3297 if (field
->cIndexes
)
3298 field
->indexes
= CryptMemRealloc(field
->indexes
,
3299 (indexIndex
+ 1) * sizeof(DWORD
));
3301 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
3304 field
->indexes
[indexIndex
] = 0;
3305 field
->cIndexes
= indexIndex
+ 1;
3309 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
3312 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
3315 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3317 assert(field
->cIndexes
);
3318 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
3322 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
3323 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
3326 DWORD i
, cbOIDs
= 0;
3327 BOOL allUsagesValid
= TRUE
;
3328 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
3330 TRACE("(%d, %p, %d, %p, %d)\n", cCerts
, rghCerts
, *cNumOIDs
,
3333 for (i
= 0; i
< cCerts
; i
++)
3335 CERT_ENHKEY_USAGE usage
;
3336 DWORD size
= sizeof(usage
);
3338 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
3339 /* Success is deliberately ignored: it implies all usages are valid */
3340 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3342 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3344 allUsagesValid
= FALSE
;
3347 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
3350 if (!validUsages
.cUsageIdentifier
)
3354 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
3355 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
3356 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3357 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
3359 validUsages
.rgpszUsageIdentifier
=
3360 CryptMemAlloc(cbOIDs
);
3361 if (validUsages
.rgpszUsageIdentifier
)
3363 LPSTR nextOID
= (LPSTR
)
3364 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
3365 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3367 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3369 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
3370 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
3371 pUsage
->rgpszUsageIdentifier
[j
]);
3372 nextOID
+= lstrlenA(nextOID
) + 1;
3378 struct BitField validIndexes
= { 0, NULL
};
3379 DWORD j
, k
, numRemoved
= 0;
3381 /* Merge: build a bitmap of all the indexes of
3382 * validUsages.rgpszUsageIdentifier that are in pUsage.
3384 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
3386 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
3388 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
3389 validUsages
.rgpszUsageIdentifier
[k
]))
3391 CRYPT_SetBitInField(&validIndexes
, k
);
3396 /* Merge by removing from validUsages those that are
3397 * not in the bitmap.
3399 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3401 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
3403 if (j
< validUsages
.cUsageIdentifier
- 1)
3405 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
3406 &validUsages
.rgpszUsageIdentifier
[j
+
3408 (validUsages
.cUsageIdentifier
- numRemoved
3409 - j
- 1) * sizeof(LPSTR
));
3411 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
3413 validUsages
.cUsageIdentifier
--;
3417 validUsages
.cUsageIdentifier
--;
3420 CryptMemFree(validIndexes
.indexes
);
3423 CryptMemFree(pUsage
);
3435 *cNumOIDs
= validUsages
.cUsageIdentifier
;
3438 else if (*pcbOIDs
< cbOIDs
)
3441 SetLastError(ERROR_MORE_DATA
);
3446 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
3447 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3450 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
3452 rghOIDs
[i
] = nextOID
;
3453 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
3454 nextOID
+= lstrlenA(nextOID
) + 1;
3458 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
3459 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
3460 TRACE("returning %d\n", ret
);
3464 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
3465 * pInfo is NULL, from the attributes of hProv.
3467 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
3468 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
3470 CRYPT_KEY_PROV_INFO info
= { 0 };
3478 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
3481 LPSTR szContainer
= CryptMemAlloc(size
);
3485 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
3486 (BYTE
*)szContainer
, &size
, 0);
3489 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3493 info
.pwszContainerName
= CryptMemAlloc(len
*
3495 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3496 info
.pwszContainerName
, len
);
3499 CryptMemFree(szContainer
);
3502 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
3505 LPSTR szProvider
= CryptMemAlloc(size
);
3509 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
3513 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3517 info
.pwszProvName
= CryptMemAlloc(len
*
3519 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3520 info
.pwszProvName
, len
);
3523 CryptMemFree(szProvider
);
3526 /* in case no CRYPT_KEY_PROV_INFO given,
3527 * we always use AT_SIGNATURE key spec
3529 info
.dwKeySpec
= AT_SIGNATURE
;
3530 size
= sizeof(info
.dwProvType
);
3531 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
3534 info
.dwProvType
= PROV_RSA_FULL
;
3538 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
3543 CryptMemFree(info
.pwszContainerName
);
3544 CryptMemFree(info
.pwszProvName
);
3548 /* Creates a signed certificate context from the unsigned, encoded certificate
3549 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
3551 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
3552 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
3554 PCCERT_CONTEXT context
= NULL
;
3558 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3559 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
3562 LPBYTE sig
= CryptMemAlloc(sigSize
);
3564 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3565 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
3568 CERT_SIGNED_CONTENT_INFO signedInfo
;
3569 BYTE
*encodedSignedCert
= NULL
;
3570 DWORD encodedSignedCertSize
= 0;
3572 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
3573 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
3574 signedInfo
.SignatureAlgorithm
= *sigAlgo
;
3575 signedInfo
.Signature
.cbData
= sigSize
;
3576 signedInfo
.Signature
.pbData
= sig
;
3577 signedInfo
.Signature
.cUnusedBits
= 0;
3578 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
3579 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
3580 &encodedSignedCert
, &encodedSignedCertSize
);
3583 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3584 encodedSignedCert
, encodedSignedCertSize
);
3585 LocalFree(encodedSignedCert
);
3593 /* Copies data from the parameters into info, where:
3594 * pSerialNumber: The serial number. Must not be NULL.
3595 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3597 * pSignatureAlgorithm: Optional.
3598 * pStartTime: The starting time of the certificate. If NULL, the current
3599 * system time is used.
3600 * pEndTime: The ending time of the certificate. If NULL, one year past the
3601 * starting time is used.
3602 * pubKey: The public key of the certificate. Must not be NULL.
3603 * pExtensions: Extensions to be included with the certificate. Optional.
3605 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3606 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3607 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3608 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3609 const CERT_EXTENSIONS
*pExtensions
)
3611 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3614 assert(pSerialNumber
);
3615 assert(pSubjectIssuerBlob
);
3618 if (pExtensions
&& pExtensions
->cExtension
)
3619 info
->dwVersion
= CERT_V3
;
3621 info
->dwVersion
= CERT_V1
;
3622 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3623 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3624 if (pSignatureAlgorithm
)
3625 info
->SignatureAlgorithm
= *pSignatureAlgorithm
;
3628 info
->SignatureAlgorithm
.pszObjId
= oid
;
3629 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3630 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3632 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3633 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3635 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3637 GetSystemTimeAsFileTime(&info
->NotBefore
);
3639 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3644 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3647 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3650 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3651 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3652 info
->SubjectPublicKeyInfo
= *pubKey
;
3655 info
->cExtension
= pExtensions
->cExtension
;
3656 info
->rgExtension
= pExtensions
->rgExtension
;
3660 info
->cExtension
= 0;
3661 info
->rgExtension
= NULL
;
3665 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3666 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3667 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3669 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3671 HCRYPTPROV hProv
= 0;
3672 HMODULE rpcrt
= LoadLibraryW(L
"rpcrt4");
3676 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3678 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3680 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3681 rpcrt
, "RpcStringFreeA");
3683 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3686 RPC_STATUS status
= uuidCreate(&uuid
);
3688 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3690 unsigned char *uuidStr
;
3692 status
= uuidToString(&uuid
, &uuidStr
);
3693 if (status
== RPC_S_OK
)
3695 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3696 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3702 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3704 CryptDestroyKey(key
);
3706 rpcStringFree(&uuidStr
);
3715 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3716 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3717 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3718 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3719 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3721 PCCERT_CONTEXT context
= NULL
;
3722 BOOL ret
, releaseContext
= FALSE
;
3723 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3724 DWORD pubKeySize
= 0, dwKeySpec
;
3726 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
3727 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3728 pExtensions
, pExtensions
);
3730 if(!pSubjectIssuerBlob
)
3732 SetLastError(ERROR_INVALID_PARAMETER
);
3736 dwKeySpec
= pKeyProvInfo
? pKeyProvInfo
->dwKeySpec
: AT_SIGNATURE
;
3741 hProv
= CRYPT_CreateKeyProv();
3742 releaseContext
= TRUE
;
3744 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3746 SetLastError(NTE_BAD_FLAGS
);
3752 /* acquire the context using the given information*/
3753 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3754 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3755 pKeyProvInfo
->dwFlags
);
3758 if(GetLastError() != NTE_BAD_KEYSET
)
3760 /* create the key set */
3761 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3762 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3763 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3767 /* check if the key is here */
3768 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3771 if (NTE_NO_KEY
== GetLastError())
3772 { /* generate the key */
3773 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3777 CryptReleaseContext(hProv
,0);
3778 SetLastError(NTE_BAD_KEYSET
);
3782 CryptDestroyKey(hKey
);
3783 releaseContext
= TRUE
;
3787 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3791 pubKey
= CryptMemAlloc(pubKeySize
);
3794 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3795 pubKey
, &pubKeySize
);
3798 CERT_INFO info
= { 0 };
3799 CRYPT_DER_BLOB blob
= { 0, NULL
};
3801 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3803 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3804 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3805 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3806 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3807 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3811 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3812 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3813 &info
.SignatureAlgorithm
);
3815 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3816 blob
.pbData
, blob
.cbData
);
3817 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3818 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3819 LocalFree(blob
.pbData
);
3822 CryptMemFree(pubKey
);
3826 CryptReleaseContext(hProv
, 0);
3830 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3831 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3832 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3833 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3835 FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType
,
3836 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3837 pVerifyUsageStatus
);
3838 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3842 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3843 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3844 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3846 TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType
, dwEncodingType
,
3847 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3851 FIXME("dwFlags 0x%08x not handled\n", dwFlags
);
3856 FIXME("pCreatePara not handled\n");
3860 switch (dwContextType
)
3862 case CERT_STORE_CERTIFICATE_CONTEXT
:
3863 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3864 case CERT_STORE_CRL_CONTEXT
:
3865 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3866 case CERT_STORE_CTL_CONTEXT
:
3867 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3869 WARN("unknown context type: 0x%x\n", dwContextType
);
3874 BOOL WINAPI
CryptSetKeyIdentifierProperty(const CRYPT_HASH_BLOB
*pKeyIdentifier
, DWORD dwPropId
,
3875 DWORD dwFlags
, LPCWSTR pwszComputerName
, void *pvReserved
, const void *pvData
)
3877 FIXME("(%p, 0x%x, 0x%x, %s, %p, %p): stub\n", pKeyIdentifier
, dwPropId
, dwFlags
,
3878 debugstr_w(pwszComputerName
), pvReserved
, pvData
);