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
24 #define WIN32_NO_STATUS
28 #define CRYPT_OID_INFO_HAS_EXTRA_FIELDS
34 #include "wine/debug.h"
35 #include "crypt32_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
39 /* Internal version of CertGetCertificateContextProperty that gets properties
40 * directly from the context (or the context it's linked to, depending on its
41 * type.) Doesn't handle special-case properties, since they are handled by
42 * CertGetCertificateContextProperty, and are particular to the store in which
43 * the property exists (which is separate from the context.)
45 static BOOL
CertContext_GetProperty(cert_t
*cert
, DWORD dwPropId
,
46 void *pvData
, DWORD
*pcbData
);
48 /* Internal version of CertSetCertificateContextProperty that sets properties
49 * directly on the context (or the context it's linked to, depending on its
50 * type.) Doesn't handle special cases, since they're handled by
51 * CertSetCertificateContextProperty anyway.
53 static BOOL
CertContext_SetProperty(cert_t
*cert
, DWORD dwPropId
,
54 DWORD dwFlags
, const void *pvData
);
56 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
57 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
58 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
60 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
61 pbCertEncoded
, cbCertEncoded
);
64 TRACE("(%p, %08lx, %p, %ld, %08lx, %p)\n", hCertStore
, dwCertEncodingType
,
65 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
69 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
70 dwAddDisposition
, ppCertContext
);
71 CertFreeCertificateContext(cert
);
78 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreA(LPCSTR pszCertStoreName
,
79 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
84 TRACE("(%s, %p, %ld)\n", debugstr_a(pszCertStoreName
), pbCertEncoded
,
87 store
= CertOpenSystemStoreA(0, pszCertStoreName
);
90 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
91 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
92 CertCloseStore(store
, 0);
97 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreW(LPCWSTR pszCertStoreName
,
98 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
103 TRACE("(%s, %p, %ld)\n", debugstr_w(pszCertStoreName
), pbCertEncoded
,
106 store
= CertOpenSystemStoreW(0, pszCertStoreName
);
109 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
110 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
111 CertCloseStore(store
, 0);
116 static const context_vtbl_t cert_vtbl
;
118 static void Cert_free(context_t
*context
)
120 cert_t
*cert
= (cert_t
*)context
;
122 CryptMemFree(cert
->ctx
.pbCertEncoded
);
123 LocalFree(cert
->ctx
.pCertInfo
);
126 static context_t
*Cert_clone(context_t
*context
, WINECRYPT_CERTSTORE
*store
, BOOL use_link
)
131 cert
= (cert_t
*)Context_CreateLinkContext(sizeof(CERT_CONTEXT
), context
, store
);
135 const cert_t
*cloned
= (const cert_t
*)context
;
139 cert
= (cert_t
*)Context_CreateDataContext(sizeof(CERT_CONTEXT
), &cert_vtbl
, store
);
143 Context_CopyProperties(&cert
->ctx
, &cloned
->ctx
);
145 cert
->ctx
.dwCertEncodingType
= cloned
->ctx
.dwCertEncodingType
;
146 cert
->ctx
.pbCertEncoded
= CryptMemAlloc(cloned
->ctx
.cbCertEncoded
);
147 memcpy(cert
->ctx
.pbCertEncoded
, cloned
->ctx
.pbCertEncoded
, cloned
->ctx
.cbCertEncoded
);
148 cert
->ctx
.cbCertEncoded
= cloned
->ctx
.cbCertEncoded
;
150 /* FIXME: We don't need to decode the object here, we could just clone cert info. */
151 res
= CryptDecodeObjectEx(cert
->ctx
.dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
152 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
153 &cert
->ctx
.pCertInfo
, &size
);
155 CertFreeCertificateContext(&cert
->ctx
);
160 cert
->ctx
.hCertStore
= store
;
164 static const context_vtbl_t cert_vtbl
= {
169 static BOOL
add_cert_to_store(WINECRYPT_CERTSTORE
*store
, const CERT_CONTEXT
*cert
,
170 DWORD add_disposition
, BOOL use_link
, PCCERT_CONTEXT
*ret_context
)
172 const CERT_CONTEXT
*existing
= NULL
;
173 BOOL ret
= TRUE
, inherit_props
= FALSE
;
174 context_t
*new_context
= NULL
;
176 switch (add_disposition
)
178 case CERT_STORE_ADD_ALWAYS
:
180 case CERT_STORE_ADD_NEW
:
181 case CERT_STORE_ADD_REPLACE_EXISTING
:
182 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
183 case CERT_STORE_ADD_USE_EXISTING
:
184 case CERT_STORE_ADD_NEWER
:
185 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
188 DWORD size
= sizeof(hashToAdd
);
190 ret
= CertGetCertificateContextProperty(cert
, CERT_HASH_PROP_ID
,
194 CRYPT_HASH_BLOB blob
= { sizeof(hashToAdd
), hashToAdd
};
196 existing
= CertFindCertificateInStore(store
, cert
->dwCertEncodingType
, 0,
197 CERT_FIND_SHA1_HASH
, &blob
, NULL
);
202 FIXME("Unimplemented add disposition %ld\n", add_disposition
);
203 SetLastError(E_INVALIDARG
);
207 switch (add_disposition
)
209 case CERT_STORE_ADD_ALWAYS
:
211 case CERT_STORE_ADD_NEW
:
214 TRACE("found matching certificate, not adding\n");
215 SetLastError(CRYPT_E_EXISTS
);
219 case CERT_STORE_ADD_REPLACE_EXISTING
:
221 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
223 FIXME("CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES: semi-stub for links\n");
225 inherit_props
= TRUE
;
227 case CERT_STORE_ADD_USE_EXISTING
:
229 FIXME("CERT_STORE_ADD_USE_EXISTING: semi-stub for links\n");
232 Context_CopyProperties(existing
, cert
);
234 *ret_context
= CertDuplicateCertificateContext(existing
);
238 case CERT_STORE_ADD_NEWER
:
239 if (existing
&& CompareFileTime(&existing
->pCertInfo
->NotBefore
, &cert
->pCertInfo
->NotBefore
) >= 0)
241 TRACE("existing certificate is newer, not adding\n");
242 SetLastError(CRYPT_E_EXISTS
);
246 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
249 if (CompareFileTime(&existing
->pCertInfo
->NotBefore
, &cert
->pCertInfo
->NotBefore
) >= 0)
251 TRACE("existing certificate is newer, not adding\n");
252 SetLastError(CRYPT_E_EXISTS
);
255 inherit_props
= TRUE
;
260 /* FIXME: We have tests that this works, but what should we really do in this case? */
263 *ret_context
= CertDuplicateCertificateContext(cert
);
267 ret
= store
->vtbl
->certs
.addContext(store
, context_from_ptr(cert
), existing
? context_from_ptr(existing
) : NULL
,
268 (ret_context
|| inherit_props
) ? &new_context
: NULL
, use_link
);
273 Context_CopyProperties(context_ptr(new_context
), existing
);
276 *ret_context
= context_ptr(new_context
);
278 Context_Release(new_context
);
280 TRACE("returning %d\n", ret
);
284 BOOL WINAPI
CertAddCertificateContextToStore(HCERTSTORE hCertStore
, PCCERT_CONTEXT pCertContext
,
285 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppStoreContext
)
287 WINECRYPT_CERTSTORE
*store
= hCertStore
;
289 TRACE("(%p, %p, %08lx, %p)\n", hCertStore
, pCertContext
, dwAddDisposition
, ppStoreContext
);
291 return add_cert_to_store(store
, pCertContext
, dwAddDisposition
, FALSE
, ppStoreContext
);
294 BOOL WINAPI
CertAddCertificateLinkToStore(HCERTSTORE hCertStore
,
295 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
296 PCCERT_CONTEXT
*ppCertContext
)
299 WINECRYPT_CERTSTORE
*store
= (WINECRYPT_CERTSTORE
*)hCertStore
;
302 FIXME("(%p, %p, %08lx, %p): semi-stub\n", hCertStore
, pCertContext
,
303 dwAddDisposition
, ppCertContext
);
304 if (store
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
306 if (store
->type
== StoreTypeCollection
)
308 SetLastError(E_INVALIDARG
);
311 return add_cert_to_store(hCertStore
, pCertContext
, dwAddDisposition
, TRUE
, ppCertContext
);
314 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
315 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
320 PCERT_INFO certInfo
= NULL
;
323 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType
, pbCertEncoded
,
326 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
328 SetLastError(E_INVALIDARG
);
332 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
333 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
338 cert
= (cert_t
*)Context_CreateDataContext(sizeof(CERT_CONTEXT
), &cert_vtbl
, &empty_store
);
341 data
= CryptMemAlloc(cbCertEncoded
);
344 Context_Release(&cert
->base
);
348 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
349 cert
->ctx
.dwCertEncodingType
= dwCertEncodingType
;
350 cert
->ctx
.pbCertEncoded
= data
;
351 cert
->ctx
.cbCertEncoded
= cbCertEncoded
;
352 cert
->ctx
.pCertInfo
= certInfo
;
353 cert
->ctx
.hCertStore
= &empty_store
;
358 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(PCCERT_CONTEXT pCertContext
)
360 TRACE("(%p)\n", pCertContext
);
365 Context_AddRef(&cert_from_ptr(pCertContext
)->base
);
369 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
371 TRACE("(%p)\n", pCertContext
);
374 Context_Release(&cert_from_ptr(pCertContext
)->base
);
378 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
381 cert_t
*cert
= cert_from_ptr(pCertContext
);
384 TRACE("(%p, %ld)\n", pCertContext
, dwPropId
);
386 if (cert
->base
.properties
)
387 ret
= ContextPropertyList_EnumPropIDs(cert
->base
.properties
, dwPropId
);
393 static BOOL
CertContext_GetHashProp(cert_t
*cert
, DWORD dwPropId
,
394 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
397 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
401 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
403 ret
= CertContext_SetProperty(cert
, dwPropId
, 0, &blob
);
408 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
415 else if (*pcbData
< cb
)
417 SetLastError(ERROR_MORE_DATA
);
423 memcpy(pvData
, pb
, cb
);
429 void CRYPT_ConvertKeyContext(const struct store_CERT_KEY_CONTEXT
*src
, CERT_KEY_CONTEXT
*dst
)
431 dst
->cbSize
= sizeof(*dst
);
432 dst
->hCryptProv
= src
->hCryptProv
;
433 dst
->dwKeySpec
= src
->dwKeySpec
;
437 * Fix offsets in a continuous block of memory of CRYPT_KEY_PROV_INFO with
438 * its associated data.
440 static void fix_KeyProvInfoProperty(CRYPT_KEY_PROV_INFO
*info
)
445 data
= (BYTE
*)(info
+ 1) + sizeof(CRYPT_KEY_PROV_PARAM
) * info
->cProvParam
;
447 if (info
->pwszContainerName
)
449 info
->pwszContainerName
= (LPWSTR
)data
;
450 data
+= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
453 if (info
->pwszProvName
)
455 info
->pwszProvName
= (LPWSTR
)data
;
456 data
+= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
459 info
->rgProvParam
= info
->cProvParam
? (CRYPT_KEY_PROV_PARAM
*)(info
+ 1) : NULL
;
461 for (i
= 0; i
< info
->cProvParam
; i
++)
463 info
->rgProvParam
[i
].pbData
= info
->rgProvParam
[i
].cbData
? data
: NULL
;
464 data
+= info
->rgProvParam
[i
].cbData
;
469 * Copy to a continuous block of memory of CRYPT_KEY_PROV_INFO with
470 * its associated data.
472 static void copy_KeyProvInfoProperty(const CRYPT_KEY_PROV_INFO
*from
, CRYPT_KEY_PROV_INFO
*to
)
477 data
= (BYTE
*)(to
+ 1) + sizeof(CRYPT_KEY_PROV_PARAM
) * from
->cProvParam
;
479 if (from
->pwszContainerName
)
481 to
->pwszContainerName
= (LPWSTR
)data
;
482 lstrcpyW((LPWSTR
)data
, from
->pwszContainerName
);
483 data
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
486 to
->pwszContainerName
= NULL
;
488 if (from
->pwszProvName
)
490 to
->pwszProvName
= (LPWSTR
)data
;
491 lstrcpyW((LPWSTR
)data
, from
->pwszProvName
);
492 data
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
495 to
->pwszProvName
= NULL
;
497 to
->dwProvType
= from
->dwProvType
;
498 to
->dwFlags
= from
->dwFlags
;
499 to
->cProvParam
= from
->cProvParam
;
500 to
->rgProvParam
= from
->cProvParam
? (CRYPT_KEY_PROV_PARAM
*)(to
+ 1) : NULL
;
501 to
->dwKeySpec
= from
->dwKeySpec
;
503 for (i
= 0; i
< from
->cProvParam
; i
++)
505 to
->rgProvParam
[i
].dwParam
= from
->rgProvParam
[i
].dwParam
;
506 to
->rgProvParam
[i
].dwFlags
= from
->rgProvParam
[i
].dwFlags
;
507 to
->rgProvParam
[i
].cbData
= from
->rgProvParam
[i
].cbData
;
508 to
->rgProvParam
[i
].pbData
= from
->rgProvParam
[i
].cbData
? data
: NULL
;
509 memcpy(data
, from
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].cbData
);
510 data
+= from
->rgProvParam
[i
].cbData
;
514 static BOOL
CertContext_GetProperty(cert_t
*cert
, DWORD dwPropId
,
515 void *pvData
, DWORD
*pcbData
)
518 CRYPT_DATA_BLOB blob
;
520 TRACE("(%p, %ld, %p, %p)\n", cert
, dwPropId
, pvData
, pcbData
);
522 if (cert
->base
.properties
)
523 ret
= ContextPropertyList_FindProperty(cert
->base
.properties
, dwPropId
, &blob
);
528 CERT_KEY_CONTEXT ctx
;
529 if (dwPropId
== CERT_KEY_CONTEXT_PROP_ID
)
531 CRYPT_ConvertKeyContext((const struct store_CERT_KEY_CONTEXT
*)blob
.pbData
, &ctx
);
532 blob
.pbData
= (BYTE
*)&ctx
;
533 blob
.cbData
= ctx
.cbSize
;
535 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
539 /* Implicit properties */
542 case CERT_SHA1_HASH_PROP_ID
:
543 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_SHA1
,
544 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
547 case CERT_MD5_HASH_PROP_ID
:
548 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
549 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
552 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
553 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
554 cert
->ctx
.pCertInfo
->Subject
.pbData
,
555 cert
->ctx
.pCertInfo
->Subject
.cbData
,
558 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
559 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
560 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
561 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
564 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
565 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
566 cert
->ctx
.pCertInfo
->SerialNumber
.pbData
,
567 cert
->ctx
.pCertInfo
->SerialNumber
.cbData
,
570 case CERT_SIGNATURE_HASH_PROP_ID
:
571 ret
= CryptHashToBeSigned(0, cert
->ctx
.dwCertEncodingType
,
572 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
576 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
578 ret
= CertContext_SetProperty(cert
, dwPropId
, 0, &blob
);
581 case CERT_KEY_IDENTIFIER_PROP_ID
:
583 PCERT_EXTENSION ext
= CertFindExtension(
584 szOID_SUBJECT_KEY_IDENTIFIER
, cert
->ctx
.pCertInfo
->cExtension
,
585 cert
->ctx
.pCertInfo
->rgExtension
);
589 CRYPT_DATA_BLOB value
;
590 DWORD size
= sizeof(value
);
592 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
593 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
594 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
598 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
600 CertContext_SetProperty(cert
, dwPropId
, 0, &value
);
604 SetLastError(ERROR_INVALID_DATA
);
608 SetLastError(CRYPT_E_NOT_FOUND
);
611 TRACE("returning %d\n", ret
);
615 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
616 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
618 cert_t
*cert
= cert_from_ptr(pCertContext
);
621 TRACE("(%p, %ld, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
626 case CERT_CERT_PROP_ID
:
627 case CERT_CRL_PROP_ID
:
628 case CERT_CTL_PROP_ID
:
629 SetLastError(E_INVALIDARG
);
632 case CERT_ACCESS_STATE_PROP_ID
:
633 ret
= CertGetStoreProperty(cert
->ctx
.hCertStore
, dwPropId
, pvData
, pcbData
);
635 case CERT_KEY_PROV_HANDLE_PROP_ID
:
637 CERT_KEY_CONTEXT keyContext
;
638 DWORD size
= sizeof(keyContext
);
640 ret
= CertContext_GetProperty(cert
,
641 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
643 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
644 sizeof(keyContext
.hCryptProv
));
647 case CERT_KEY_PROV_INFO_PROP_ID
:
648 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
, pcbData
);
650 fix_KeyProvInfoProperty(pvData
);
653 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
,
657 TRACE("returning %d\n", ret
);
662 * Create a continuous block of memory for CRYPT_KEY_PROV_INFO with
663 * its associated data, and add it to the certificate properties.
665 static BOOL
CertContext_SetKeyProvInfoProperty(CONTEXT_PROPERTY_LIST
*properties
, const CRYPT_KEY_PROV_INFO
*info
)
667 CRYPT_KEY_PROV_INFO
*prop
;
668 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
;
671 if (info
->pwszContainerName
)
672 size
+= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
673 if (info
->pwszProvName
)
674 size
+= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
676 for (i
= 0; i
< info
->cProvParam
; i
++)
677 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
679 prop
= CryptMemAlloc(size
);
682 SetLastError(ERROR_OUTOFMEMORY
);
686 copy_KeyProvInfoProperty(info
, prop
);
688 ret
= ContextPropertyList_SetProperty(properties
, CERT_KEY_PROV_INFO_PROP_ID
, (const BYTE
*)prop
, size
);
694 static BOOL
CertContext_SetKeyContextProperty(CONTEXT_PROPERTY_LIST
*properties
,
695 const CERT_KEY_CONTEXT
*keyContext
)
697 struct store_CERT_KEY_CONTEXT ctx
;
699 ctx
.cbSize
= sizeof(ctx
);
700 ctx
.hCryptProv
= keyContext
->hCryptProv
;
701 ctx
.dwKeySpec
= keyContext
->dwKeySpec
;
703 return ContextPropertyList_SetProperty(properties
, CERT_KEY_CONTEXT_PROP_ID
,
704 (const BYTE
*)&ctx
, ctx
.cbSize
);
707 static BOOL
CertContext_SetProperty(cert_t
*cert
, DWORD dwPropId
,
708 DWORD dwFlags
, const void *pvData
)
712 TRACE("(%p, %ld, %08lx, %p)\n", cert
, dwPropId
, dwFlags
, pvData
);
714 if (!cert
->base
.properties
)
716 else if (dwPropId
>= CERT_FIRST_USER_PROP_ID
&& dwPropId
<= CERT_LAST_USER_PROP_ID
)
720 const CRYPT_DATA_BLOB
*blob
= pvData
;
721 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
, blob
->pbData
, blob
->cbData
);
725 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
733 case CERT_AUTO_ENROLL_PROP_ID
:
734 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
735 case CERT_DESCRIPTION_PROP_ID
:
736 case CERT_FRIENDLY_NAME_PROP_ID
:
737 case CERT_HASH_PROP_ID
:
738 case CERT_KEY_IDENTIFIER_PROP_ID
:
739 case CERT_MD5_HASH_PROP_ID
:
740 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
741 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
742 case CERT_PVK_FILE_PROP_ID
:
743 case CERT_SIGNATURE_HASH_PROP_ID
:
744 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
745 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
746 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
747 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
748 case CERT_ENROLLMENT_PROP_ID
:
749 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
750 case CERT_OCSP_RESPONSE_PROP_ID
:
751 case CERT_RENEWAL_PROP_ID
:
755 const CRYPT_DATA_BLOB
*blob
= pvData
;
757 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
758 blob
->pbData
, blob
->cbData
);
762 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
767 case CERT_DATE_STAMP_PROP_ID
:
769 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
770 pvData
, sizeof(FILETIME
));
773 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
777 case CERT_KEY_CONTEXT_PROP_ID
:
780 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
782 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
784 SetLastError(E_INVALIDARG
);
788 ret
= CertContext_SetKeyContextProperty(cert
->base
.properties
, pvData
);
792 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
796 case CERT_KEY_PROV_INFO_PROP_ID
:
798 ret
= CertContext_SetKeyProvInfoProperty(cert
->base
.properties
, pvData
);
801 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
805 case CERT_KEY_PROV_HANDLE_PROP_ID
:
807 CERT_KEY_CONTEXT keyContext
;
808 DWORD size
= sizeof(keyContext
);
810 ret
= CertContext_GetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
814 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
815 CryptReleaseContext(keyContext
.hCryptProv
, 0);
817 keyContext
.cbSize
= sizeof(keyContext
);
819 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
822 keyContext
.hCryptProv
= 0;
823 keyContext
.dwKeySpec
= AT_SIGNATURE
;
825 ret
= CertContext_SetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
830 FIXME("%ld: stub\n", dwPropId
);
834 TRACE("returning %d\n", ret
);
838 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
839 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
843 TRACE("(%p, %ld, %08lx, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
845 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
846 * crashes on most of these, I'll be safer.
851 case CERT_ACCESS_STATE_PROP_ID
:
852 case CERT_CERT_PROP_ID
:
853 case CERT_CRL_PROP_ID
:
854 case CERT_CTL_PROP_ID
:
855 SetLastError(E_INVALIDARG
);
858 ret
= CertContext_SetProperty(cert_from_ptr(pCertContext
), dwPropId
, dwFlags
,
860 TRACE("returning %d\n", ret
);
864 /* Acquires the private key using the key provider info, retrieving info from
865 * the certificate if info is NULL. The acquired provider is returned in
866 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
868 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
, DWORD dwFlags
,
869 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
872 BOOL allocated
= FALSE
, ret
= TRUE
;
876 ret
= CertGetCertificateContextProperty(pCert
,
877 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
880 info
= CryptMemAlloc(size
);
883 ret
= CertGetCertificateContextProperty(pCert
,
884 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
889 SetLastError(ERROR_OUTOFMEMORY
);
894 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
898 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
899 info
->pwszProvName
, info
->dwProvType
, (dwFlags
& CRYPT_ACQUIRE_SILENT_FLAG
) ? CRYPT_SILENT
: 0);
904 for (i
= 0; i
< info
->cProvParam
; i
++)
906 CryptSetProvParam(*phCryptProv
,
907 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
908 info
->rgProvParam
[i
].dwFlags
);
910 *pdwKeySpec
= info
->dwKeySpec
;
913 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
920 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
921 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
922 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
924 BOOL ret
= FALSE
, cache
= FALSE
;
925 PCRYPT_KEY_PROV_INFO info
= NULL
;
926 CERT_KEY_CONTEXT keyContext
;
928 PCCERT_CONTEXT cert_in_store
= NULL
;
930 TRACE("(%p, %08lx, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
931 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
933 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
937 ret
= CertGetCertificateContextProperty(pCert
,
938 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
944 hstore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
945 CERT_SYSTEM_STORE_CURRENT_USER
, L
"My");
948 cert_in_store
= CertFindCertificateInStore(hstore
, pCert
->dwCertEncodingType
, 0,
949 CERT_FIND_EXISTING
, pCert
, NULL
);
952 ret
= CertGetCertificateContextProperty(cert_in_store
, CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
954 pCert
= cert_in_store
;
957 CertFreeCertificateContext(cert_in_store
);
958 cert_in_store
= NULL
;
962 CertCloseStore(hstore
, 0);
968 info
= CryptMemAlloc(size
);
969 ret
= CertGetCertificateContextProperty(pCert
,
970 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
972 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
975 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
980 size
= sizeof(keyContext
);
981 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
985 *phCryptProv
= keyContext
.hCryptProv
;
987 *pdwKeySpec
= keyContext
.dwKeySpec
;
988 if (pfCallerFreeProv
)
989 *pfCallerFreeProv
= FALSE
;
994 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, dwFlags
, info
,
995 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
998 *phCryptProv
= keyContext
.hCryptProv
;
1000 *pdwKeySpec
= keyContext
.dwKeySpec
;
1003 keyContext
.cbSize
= sizeof(keyContext
);
1004 if (CertSetCertificateContextProperty(pCert
,
1005 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
1007 if (pfCallerFreeProv
)
1008 *pfCallerFreeProv
= FALSE
;
1013 if (pfCallerFreeProv
)
1014 *pfCallerFreeProv
= TRUE
;
1020 CertFreeCertificateContext(cert_in_store
);
1021 if (ret
) SetLastError(0);
1025 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
1026 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1029 BOOL matches
= FALSE
;
1031 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
1032 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
1036 /* Need to sign something to verify the sig. What to sign? Why not
1037 * the certificate itself?
1039 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
1040 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
1041 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
1043 BYTE
*certEncoded
= CryptMemAlloc(size
);
1047 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
1048 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
1049 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
1050 NULL
, certEncoded
, &size
))
1052 if (size
== pCert
->cbCertEncoded
&&
1053 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
1056 CryptMemFree(certEncoded
);
1059 CryptReleaseContext(csp
, 0);
1064 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
1065 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1067 CRYPT_KEY_PROV_INFO copy
;
1068 WCHAR containerW
[MAX_PATH
];
1071 MultiByteToWideChar(CP_ACP
, 0, container
, -1, containerW
, ARRAY_SIZE(containerW
));
1072 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
1073 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
1076 copy
= *keyProvInfo
;
1077 copy
.pwszContainerName
= containerW
;
1078 matches
= key_prov_info_matches_cert(pCert
, ©
);
1081 keyProvInfo
->pwszContainerName
=
1082 CryptMemAlloc((lstrlenW(containerW
) + 1) * sizeof(WCHAR
));
1083 if (keyProvInfo
->pwszContainerName
)
1085 lstrcpyW(keyProvInfo
->pwszContainerName
, containerW
);
1086 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
1094 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
1095 * private key matches pCert's public key. Upon success, updates keyProvInfo
1096 * with the matching container's info (free keyProvInfo.pwszContainerName upon
1098 * Returns TRUE if found, FALSE if not.
1100 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
1101 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1103 HCRYPTPROV defProvider
;
1104 BOOL ret
, found
= FALSE
;
1105 char containerA
[MAX_PATH
];
1107 assert(keyProvInfo
->pwszContainerName
== NULL
);
1108 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
1109 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
1110 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
1112 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
1114 while (ret
&& !found
)
1116 DWORD size
= sizeof(containerA
);
1118 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
1119 (BYTE
*)containerA
, &size
, enumFlags
);
1121 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
1122 if (enumFlags
& CRYPT_FIRST
)
1124 enumFlags
&= ~CRYPT_FIRST
;
1125 enumFlags
|= CRYPT_NEXT
;
1128 CryptReleaseContext(defProvider
, 0);
1133 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
1135 BOOL found
= FALSE
, ret
= TRUE
;
1136 DWORD index
= 0, cbProvName
= 0;
1137 CRYPT_KEY_PROV_INFO keyProvInfo
;
1139 TRACE("(%p, %08lx)\n", pCert
, dwFlags
);
1141 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
1142 while (ret
&& !found
)
1146 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
1150 if (size
<= cbProvName
)
1151 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1152 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1155 CryptMemFree(keyProvInfo
.pwszProvName
);
1156 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
1157 if (keyProvInfo
.pwszProvName
)
1160 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1161 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1164 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
1165 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
1166 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
1167 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1168 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1170 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
1171 found
= find_key_prov_info_in_provider(pCert
,
1176 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
1177 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1178 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1180 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
1181 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
1182 found
= find_key_prov_info_in_provider(pCert
,
1195 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1197 CryptMemFree(keyProvInfo
.pwszProvName
);
1198 CryptMemFree(keyProvInfo
.pwszContainerName
);
1202 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
1204 BOOL matches
= FALSE
;
1207 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1210 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
1214 if (CertGetCertificateContextProperty(pCert
,
1215 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
1216 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
1217 CryptMemFree(keyProvInfo
);
1223 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
1224 DWORD dwFlags
, void *pvReserved
)
1228 TRACE("(%p, %08lx, %p)\n", pCert
, dwFlags
, pvReserved
);
1230 matches
= cert_prov_info_matches_cert(pCert
);
1232 matches
= find_matching_provider(pCert
, dwFlags
);
1236 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1237 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1241 TRACE("(%08lx, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1243 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1244 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1245 &pCertId2
->SerialNumber
);
1246 TRACE("returning %d\n", ret
);
1250 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1251 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1255 TRACE("(%08lx, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1257 if (pCertName1
->cbData
== pCertName2
->cbData
)
1259 if (pCertName1
->cbData
)
1260 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1261 pCertName1
->cbData
);
1267 TRACE("returning %d\n", ret
);
1271 /* Returns the number of significant bytes in pInt, where a byte is
1272 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1273 * for negative numbers. pInt is assumed to be little-endian.
1275 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1277 DWORD ret
= pInt
->cbData
;
1281 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1283 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1291 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1292 PCRYPT_INTEGER_BLOB pInt2
)
1297 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1299 cb1
= CRYPT_significantBytes(pInt1
);
1300 cb2
= CRYPT_significantBytes(pInt2
);
1304 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1310 TRACE("returning %d\n", ret
);
1314 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1315 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1319 TRACE("(%08lx, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1321 /* RSA public key data should start with ASN_SEQUENCE,
1322 * otherwise it's not a RSA_CSP_PUBLICKEYBLOB.
1324 if (!pPublicKey1
->PublicKey
.cbData
|| pPublicKey1
->PublicKey
.pbData
[0] != ASN_SEQUENCE
)
1325 dwCertEncodingType
= 0;
1327 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1329 case 0: /* Seems to mean "raw binary bits" */
1330 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1331 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1333 if (pPublicKey2
->PublicKey
.cbData
)
1334 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1335 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1343 WARN("Unknown encoding type %08lx\n", dwCertEncodingType
);
1345 case X509_ASN_ENCODING
:
1347 BLOBHEADER
*pblob1
, *pblob2
;
1350 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1351 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1352 CRYPT_DECODE_ALLOC_FLAG
, &pblob1
, &length
))
1354 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1355 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1356 CRYPT_DECODE_ALLOC_FLAG
, &pblob2
, &length
))
1358 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1359 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1360 *pk2
= (LPVOID
)(pblob2
+ 1);
1361 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1362 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1375 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1376 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1380 TRACE("(%08lx, %p)\n", dwCertEncodingType
, pPublicKey
);
1382 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1384 SetLastError(ERROR_FILE_NOT_FOUND
);
1387 if (pPublicKey
->Algorithm
.pszObjId
&&
1388 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1390 FIXME("unimplemented for DH public keys\n");
1391 SetLastError(CRYPT_E_ASN1_BADTAG
);
1395 PCCRYPT_OID_INFO info
;
1400 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, pPublicKey
->Algorithm
.pszObjId
, 0);
1405 TRACE("public key algid %#x (%s)\n", info
->Algid
, debugstr_a(pPublicKey
->Algorithm
.pszObjId
));
1407 ret
= CryptImportPublicKeyInfo(I_CryptGetDefaultCryptProv(info
->Algid
), dwCertEncodingType
, pPublicKey
, &key
);
1411 ret
= CryptGetKeyParam(key
, KP_KEYLEN
, (BYTE
*)&len
, &size
, 0);
1412 CryptDestroyKey(key
);
1415 /* fallback to RSA */
1418 ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1419 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1420 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1425 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1427 len
= rsaPubKey
->bitlen
;
1434 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1435 DWORD dwFlags
, const void *pvPara
);
1437 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1438 DWORD dwFlags
, const void *pvPara
)
1442 DWORD size
= sizeof(hash
);
1444 ret
= CertGetCertificateContextProperty(pCertContext
,
1445 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1448 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1450 if (size
== pHash
->cbData
)
1451 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1458 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1459 DWORD dwFlags
, const void *pvPara
)
1463 DWORD size
= sizeof(hash
);
1465 ret
= CertGetCertificateContextProperty(pCertContext
,
1466 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1469 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1471 if (size
== pHash
->cbData
)
1472 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1479 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1480 DWORD dwFlags
, const void *pvPara
)
1482 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1485 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1486 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1488 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1489 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1494 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1495 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1497 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1500 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1501 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1505 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1506 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1508 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1511 /* Matching serial number and subject match.. */
1512 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1513 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1515 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1516 &pCertInfo
->SerialNumber
);
1519 /* failing that, if the serial number and issuer match, we match */
1520 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1521 &pCertInfo
->SerialNumber
);
1523 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1524 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1526 TRACE("returning %d\n", ret
);
1530 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1531 DWORD dwFlags
, const void *pvPara
)
1533 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1536 switch (id
->dwIdChoice
)
1538 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1539 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1540 &pCertContext
->pCertInfo
->Issuer
, &id
->IssuerSerialNumber
.Issuer
);
1542 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1543 &id
->IssuerSerialNumber
.SerialNumber
);
1545 case CERT_ID_SHA1_HASH
:
1546 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1549 case CERT_ID_KEY_IDENTIFIER
:
1553 ret
= CertGetCertificateContextProperty(pCertContext
,
1554 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1555 if (ret
&& size
== id
->KeyId
.cbData
)
1557 LPBYTE buf
= CryptMemAlloc(size
);
1561 CertGetCertificateContextProperty(pCertContext
,
1562 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1563 ret
= !memcmp(buf
, id
->KeyId
.pbData
, size
);
1580 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1581 DWORD dwFlags
, const void *pvPara
)
1583 PCCERT_CONTEXT toCompare
= pvPara
;
1584 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1585 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1588 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1589 DWORD dwFlags
, const void *pvPara
)
1591 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1595 ret
= CertGetCertificateContextProperty(pCertContext
,
1596 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1597 if (ret
&& size
== hash
->cbData
)
1599 LPBYTE buf
= CryptMemAlloc(size
);
1603 CertGetCertificateContextProperty(pCertContext
,
1604 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1605 ret
= !memcmp(buf
, hash
->pbData
, size
);
1616 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1617 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1620 BOOL matches
= FALSE
;
1625 ret
= CertEnumCertificatesInStore(store
, ret
);
1627 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1628 } while (ret
!= NULL
&& !matches
);
1632 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1633 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1635 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1636 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1638 return CertEnumCertificatesInStore(store
, prev
);
1641 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1642 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1645 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1646 PCERT_EXTENSION ext
;
1649 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1650 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1652 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1654 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1655 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1656 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1662 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1664 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1665 memcpy(&id
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1666 sizeof(CERT_NAME_BLOB
));
1667 memcpy(&id
.IssuerSerialNumber
.SerialNumber
,
1668 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1670 else if (info
->KeyId
.cbData
)
1672 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1673 memcpy(&id
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1678 found
= cert_compare_certs_in_store(store
, prev
,
1679 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1683 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1684 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1686 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1688 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1689 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1690 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1696 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1697 info
->AuthorityCertSerialNumber
.cbData
)
1699 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1702 for (i
= 0; !directoryName
&&
1703 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1704 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1705 == CERT_ALT_NAME_DIRECTORY_NAME
)
1707 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1710 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1711 memcpy(&id
.IssuerSerialNumber
.Issuer
,
1712 &directoryName
->DirectoryName
, sizeof(CERT_NAME_BLOB
));
1713 memcpy(&id
.IssuerSerialNumber
.SerialNumber
,
1714 &info
->AuthorityCertSerialNumber
,
1715 sizeof(CRYPT_INTEGER_BLOB
));
1719 FIXME("no supported name type in authority key id2\n");
1723 else if (info
->KeyId
.cbData
)
1725 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1726 memcpy(&id
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1731 found
= cert_compare_certs_in_store(store
, prev
,
1732 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1737 found
= cert_compare_certs_in_store(store
, prev
,
1738 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1739 dwFlags
, &subject
->pCertInfo
->Issuer
);
1743 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1744 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1746 PCERT_NAME_BLOB name
;
1750 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1751 name
= &pCertContext
->pCertInfo
->Subject
;
1753 name
= &pCertContext
->pCertInfo
->Issuer
;
1754 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1755 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1758 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1762 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1763 CERT_SIMPLE_NAME_STR
, str
, len
);
1765 if (wcsstr(str
, pvPara
))
1773 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1774 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1776 PCCERT_CONTEXT found
= NULL
;
1778 TRACE("%s\n", debugstr_a(pvPara
));
1782 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1783 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1787 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1789 found
= cert_compare_certs_in_store(store
, prev
,
1790 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1795 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1799 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1800 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1802 PCCERT_CONTEXT found
= NULL
;
1804 TRACE("%s\n", debugstr_w(pvPara
));
1808 DWORD len
= lstrlenW(pvPara
);
1809 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1813 wcscpy( str
, pvPara
);
1815 found
= cert_compare_certs_in_store(store
, prev
,
1816 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1821 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1825 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1826 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1827 PCCERT_CONTEXT pPrevCertContext
)
1830 CertFindFunc find
= NULL
;
1831 CertCompareFunc compare
= NULL
;
1834 TRACE("(%p, %08lx, %08lx, %08lx, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1835 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1837 switch (dwType
>> CERT_COMPARE_SHIFT
)
1839 case CERT_COMPARE_ANY
:
1840 find
= find_cert_any
;
1842 case CERT_COMPARE_MD5_HASH
:
1843 compare
= compare_cert_by_md5_hash
;
1845 case CERT_COMPARE_SHA1_HASH
:
1846 compare
= compare_cert_by_sha1_hash
;
1848 case CERT_COMPARE_NAME
:
1849 compare
= compare_cert_by_name
;
1851 case CERT_COMPARE_PUBLIC_KEY
:
1852 compare
= compare_cert_by_public_key
;
1854 case CERT_COMPARE_NAME_STR_A
:
1855 find
= find_cert_by_name_str_a
;
1857 case CERT_COMPARE_NAME_STR_W
:
1858 find
= find_cert_by_name_str_w
;
1860 case CERT_COMPARE_SUBJECT_CERT
:
1861 compare
= compare_cert_by_subject_cert
;
1863 case CERT_COMPARE_KEY_IDENTIFIER
:
1864 cert_id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1865 cert_id
.KeyId
= *(const CRYPT_HASH_BLOB
*)pvPara
;
1868 case CERT_COMPARE_CERT_ID
:
1869 compare
= compare_cert_by_cert_id
;
1871 case CERT_COMPARE_ISSUER_OF
:
1872 find
= find_cert_by_issuer
;
1874 case CERT_COMPARE_EXISTING
:
1875 compare
= compare_existing_cert
;
1877 case CERT_COMPARE_SIGNATURE_HASH
:
1878 compare
= compare_cert_by_signature_hash
;
1881 FIXME("find type %08lx unimplemented\n", dwType
);
1885 ret
= find(hCertStore
, dwType
, dwFlags
, pvPara
, pPrevCertContext
);
1887 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1888 compare
, dwType
, dwFlags
, pvPara
);
1892 SetLastError(CRYPT_E_NOT_FOUND
);
1893 TRACE("returning %p\n", ret
);
1897 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1898 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1900 TRACE("(%p, %08lx, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1904 SetLastError(E_INVALIDARG
);
1907 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1908 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1911 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1912 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1914 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1915 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1917 if (*pdwFlags
& ~supportedFlags
)
1919 SetLastError(E_INVALIDARG
);
1922 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1925 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1928 /* FIXME: what if the CRL has expired? */
1931 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1932 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1933 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1936 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1938 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1940 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1941 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1943 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1945 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1946 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1947 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1948 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1953 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1954 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1959 TRACE("(%p, %p, %p, %08lx)\n", hCertStore
, pSubjectContext
,
1960 pPrevIssuerContext
, *pdwFlags
);
1962 if (!pSubjectContext
)
1964 SetLastError(E_INVALIDARG
);
1968 ret
= CertFindCertificateInStore(hCertStore
,
1969 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1970 pSubjectContext
, pPrevIssuerContext
);
1973 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1976 CertFreeCertificateContext(ret
);
1979 if (CRYPT_IsCertificateSelfSigned(pSubjectContext
))
1981 CertFreeCertificateContext(ret
);
1983 SetLastError(CRYPT_E_SELF_SIGNED
);
1986 TRACE("returning %p\n", ret
);
1990 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1995 } OLD_CERT_REVOCATION_STATUS
;
1997 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1998 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
2000 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
2001 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
2002 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
2006 TRACE("(%08lx, %ld, %ld, %p, %08lx, %p, %p)\n", dwEncodingType
, dwRevType
,
2007 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
2009 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
2010 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
2012 SetLastError(E_INVALIDARG
);
2017 static HCRYPTOIDFUNCSET set
= NULL
;
2021 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
2022 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
2028 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
2033 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
2037 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
2041 for (ptr
= dllList
; ret
&& *ptr
;
2042 ptr
+= lstrlenW(ptr
) + 1)
2044 CertVerifyRevocationFunc func
;
2045 HCRYPTOIDFUNCADDR hFunc
;
2047 ret
= CryptGetDefaultOIDFunctionAddress(set
,
2048 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
2051 ret
= func(dwEncodingType
, dwRevType
, cContext
,
2052 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
2053 CryptFreeOIDFunctionAddress(hFunc
, 0);
2057 CryptMemFree(dllList
);
2061 SetLastError(ERROR_OUTOFMEMORY
);
2072 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
2073 CRYPT_ATTRIBUTE rgAttr
[])
2075 PCRYPT_ATTRIBUTE ret
= NULL
;
2078 TRACE("%s %ld %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
2084 SetLastError(ERROR_INVALID_PARAMETER
);
2088 for (i
= 0; !ret
&& i
< cAttr
; i
++)
2089 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
2094 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
2095 CERT_EXTENSION rgExtensions
[])
2097 PCERT_EXTENSION ret
= NULL
;
2100 TRACE("%s %ld %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
2106 SetLastError(ERROR_INVALID_PARAMETER
);
2110 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
2111 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
2112 rgExtensions
[i
].pszObjId
))
2113 ret
= &rgExtensions
[i
];
2117 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
2119 PCERT_RDN_ATTR ret
= NULL
;
2122 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
2126 SetLastError(ERROR_INVALID_PARAMETER
);
2130 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
2131 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
2132 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
2133 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
2134 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
2138 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
2139 const CERT_RDN_ATTR
*attr
)
2144 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
2146 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
2148 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
2150 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
2153 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
2156 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2157 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
2159 if (attr
->Value
.cbData
!=
2160 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2162 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2163 match
= !wcsnicmp(nameStr
, attrStr
,
2164 attr
->Value
.cbData
/ sizeof(WCHAR
));
2166 match
= !wcsncmp(nameStr
, attrStr
,
2167 attr
->Value
.cbData
/ sizeof(WCHAR
));
2168 TRACE("%s : %s => %d\n",
2169 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2170 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2176 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2177 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
2179 if (attr
->Value
.cbData
!=
2180 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2182 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2183 match
= !_strnicmp(nameStr
, attrStr
,
2184 attr
->Value
.cbData
);
2186 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
2187 TRACE("%s : %s => %d\n",
2188 debugstr_an(nameStr
, attr
->Value
.cbData
),
2189 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
2197 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
2198 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
2200 CERT_NAME_INFO
*name
;
2205 TRACE("(%08lx, %08lx, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
2208 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
2210 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
2211 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
2215 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
2216 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
2218 SetLastError(CRYPT_E_NO_MATCH
);
2224 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
2225 PCERT_INFO pCertInfo
)
2232 GetSystemTimeAsFileTime(&fileTime
);
2233 pTimeToVerify
= &fileTime
;
2235 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
2237 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
2244 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
2245 PCERT_INFO pIssuerInfo
)
2247 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
2249 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
2250 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
2253 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2254 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2255 DWORD
*pcbComputedHash
)
2258 HCRYPTHASH hHash
= 0;
2260 TRACE("(%08Ix, %d, %08lx, %p, %ld, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2261 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2264 hCryptProv
= I_CryptGetDefaultCryptProv(Algid
);
2269 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2272 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2274 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2275 pcbComputedHash
, 0);
2276 CryptDestroyHash(hHash
);
2282 BOOL WINAPI
CryptHashCertificate2(LPCWSTR pwszCNGHashAlgid
, DWORD dwFlags
,
2283 void *pvReserved
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2284 DWORD
*pcbComputedHash
)
2286 BCRYPT_HASH_HANDLE hash
= NULL
;
2287 BCRYPT_ALG_HANDLE alg
= NULL
;
2290 DWORD hash_len_size
;
2292 TRACE("(%s, %08lx, %p, %p, %ld, %p, %p)\n", debugstr_w(pwszCNGHashAlgid
),
2293 dwFlags
, pvReserved
, pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2295 if ((status
= BCryptOpenAlgorithmProvider(&alg
, pwszCNGHashAlgid
, NULL
, 0)))
2297 if (status
== STATUS_NOT_IMPLEMENTED
)
2298 status
= STATUS_NOT_FOUND
;
2302 if ((status
= BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0)))
2305 if ((status
= BCryptGetProperty(hash
, BCRYPT_HASH_LENGTH
, (BYTE
*)&hash_len
, sizeof(hash_len
), &hash_len_size
, 0)))
2308 if (!pbComputedHash
)
2310 *pcbComputedHash
= hash_len
;
2314 if (*pcbComputedHash
< hash_len
)
2316 status
= ERROR_MORE_DATA
;
2320 *pcbComputedHash
= hash_len
;
2322 if ((status
= BCryptHashData(hash
, (BYTE
*)pbEncoded
, cbEncoded
, 0)))
2325 if ((status
= BCryptFinishHash(hash
, pbComputedHash
, hash_len
, 0)))
2329 if (hash
) BCryptDestroyHash(hash
);
2330 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2331 if (status
) SetLastError(status
);
2335 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2336 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2337 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2340 HCRYPTHASH hHash
= 0;
2342 TRACE("(%08Ix, %d, %08lx, %ld, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2343 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2346 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2349 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2351 SetLastError(ERROR_FILE_NOT_FOUND
);
2359 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2360 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2361 (LPBYTE
)&buf
, &size
);
2364 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2367 ret
= CryptHashData(hHash
, buf
, size
, 0);
2369 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2370 pcbComputedHash
, 0);
2371 CryptDestroyHash(hHash
);
2379 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2380 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2381 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2384 CERT_SIGNED_CONTENT_INFO
*info
;
2387 TRACE("(%08Ix, %08lx, %p, %ld, %p, %ld)\n", hCryptProv
, dwCertEncodingType
,
2388 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2390 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2391 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2394 PCCRYPT_OID_INFO oidInfo
;
2398 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2399 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2400 info
->SignatureAlgorithm
.pszObjId
, 0);
2403 SetLastError(NTE_BAD_ALGID
);
2408 ret
= CryptCreateHash(hCryptProv
, oidInfo
->Algid
, 0, 0, &hHash
);
2411 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2412 info
->ToBeSigned
.cbData
, 0);
2414 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2415 pcbComputedHash
, 0);
2416 CryptDestroyHash(hHash
);
2424 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2425 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2426 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2427 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2430 PCCRYPT_OID_INFO info
;
2433 TRACE("(%08Ix, %ld, %ld, %p, %ld, %p, %p, %p, %p)\n", hCryptProv
,
2434 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2435 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2437 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2438 pSignatureAlgorithm
->pszObjId
, 0);
2441 SetLastError(NTE_BAD_ALGID
);
2444 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2447 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2448 ret
= CryptCreateHash(hCryptProv
, info
->Algid
, 0, 0, &hHash
);
2451 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2452 cbEncodedToBeSigned
, 0);
2454 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2456 CryptDestroyHash(hHash
);
2463 SetLastError(ERROR_INVALID_PARAMETER
);
2468 ret
= CryptCreateHash(hCryptProv
, info
->Algid
, 0, 0, &hHash
);
2471 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2472 cbEncodedToBeSigned
, 0);
2474 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2476 CryptDestroyHash(hHash
);
2483 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2484 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2485 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2486 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2489 DWORD encodedSize
, hashSize
;
2491 TRACE("(%08Ix, %ld, %ld, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2492 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2493 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2495 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2496 NULL
, &encodedSize
);
2499 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2503 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2504 pvStructInfo
, encoded
, &encodedSize
);
2507 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2508 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2509 pvHashAuxInfo
, NULL
, &hashSize
);
2512 PBYTE hash
= CryptMemAlloc(hashSize
);
2516 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2517 dwCertEncodingType
, encoded
, encodedSize
,
2518 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2521 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2523 info
.ToBeSigned
.cbData
= encodedSize
;
2524 info
.ToBeSigned
.pbData
= encoded
;
2525 info
.SignatureAlgorithm
= *pSignatureAlgorithm
;
2526 info
.Signature
.cbData
= hashSize
;
2527 info
.Signature
.pbData
= hash
;
2528 info
.Signature
.cUnusedBits
= 0;
2529 ret
= CryptEncodeObject(dwCertEncodingType
,
2530 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2538 CryptMemFree(encoded
);
2546 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2547 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2548 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2550 CRYPT_DATA_BLOB blob
= { cbEncoded
, (BYTE
*)pbEncoded
};
2552 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2553 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, &blob
,
2554 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2557 static BOOL
CRYPT_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2558 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2562 ALG_ID pubKeyID
, hashID
;
2564 hashID
= info
->Algid
;
2565 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2566 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2569 /* Load the default provider if necessary */
2571 hCryptProv
= I_CryptGetDefaultCryptProv(hashID
);
2572 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2573 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2578 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2581 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2582 signedCert
->ToBeSigned
.cbData
, 0);
2584 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2585 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2586 CryptDestroyHash(hash
);
2588 CryptDestroyKey(key
);
2593 static BOOL
CNG_CalcHash(const WCHAR
*algorithm
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2594 BYTE
**hash_value
, DWORD
*hash_len
)
2596 BCRYPT_HASH_HANDLE hash
= NULL
;
2597 BCRYPT_ALG_HANDLE alg
= NULL
;
2601 if ((status
= BCryptOpenAlgorithmProvider(&alg
, algorithm
, NULL
, 0)))
2604 if ((status
= BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0)))
2607 if ((status
= BCryptHashData(hash
, signedCert
->ToBeSigned
.pbData
, signedCert
->ToBeSigned
.cbData
, 0)))
2610 if ((status
= BCryptGetProperty(hash
, BCRYPT_HASH_LENGTH
, (BYTE
*)hash_len
, sizeof(*hash_len
), &size
, 0)))
2613 if (!(*hash_value
= CryptMemAlloc(*hash_len
)))
2615 status
= STATUS_NO_MEMORY
;
2619 if ((status
= BCryptFinishHash(hash
, *hash_value
, *hash_len
, 0)))
2621 CryptMemFree(*hash_value
);
2626 if (hash
) BCryptDestroyHash(hash
);
2627 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2628 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2632 static BOOL
CNG_ImportECCPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2634 DWORD blob_magic
, ecckey_len
, size
;
2635 BCRYPT_ALG_HANDLE alg_handle
;
2636 BCRYPT_ECCKEY_BLOB
*ecckey
;
2640 if (!pubKeyInfo
->PublicKey
.cbData
)
2642 SetLastError(NTE_BAD_ALGID
);
2646 if (pubKeyInfo
->PublicKey
.pbData
[0] != 0x4)
2648 FIXME("Compressed ECC curves (%02x) not yet supported\n", pubKeyInfo
->PublicKey
.pbData
[0]);
2649 SetLastError(NTE_BAD_ALGID
);
2653 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_OBJECT_IDENTIFIER
, pubKeyInfo
->Algorithm
.Parameters
.pbData
,
2654 pubKeyInfo
->Algorithm
.Parameters
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_curve
, &size
))
2657 if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P256
))
2659 alg_handle
= BCRYPT_ECDSA_P256_ALG_HANDLE
;
2660 blob_magic
= BCRYPT_ECDSA_PUBLIC_P256_MAGIC
;
2662 else if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P384
))
2664 alg_handle
= BCRYPT_ECDSA_P384_ALG_HANDLE
;
2665 blob_magic
= BCRYPT_ECDSA_PUBLIC_P384_MAGIC
;
2669 FIXME("Unsupported ecc curve type: %s\n", *ecc_curve
);
2673 LocalFree(ecc_curve
);
2677 SetLastError(NTE_BAD_ALGID
);
2681 ecckey_len
= sizeof(BCRYPT_ECCKEY_BLOB
) + pubKeyInfo
->PublicKey
.cbData
- 1;
2682 if (!(ecckey
= CryptMemAlloc(ecckey_len
)))
2683 return STATUS_NO_MEMORY
;
2685 ecckey
->dwMagic
= blob_magic
;
2686 ecckey
->cbKey
= (pubKeyInfo
->PublicKey
.cbData
- 1) / 2;
2687 memcpy(ecckey
+ 1, pubKeyInfo
->PublicKey
.pbData
+ 1, pubKeyInfo
->PublicKey
.cbData
- 1);
2689 status
= BCryptImportKeyPair(alg_handle
, NULL
, BCRYPT_ECCPUBLIC_BLOB
, key
, (BYTE
*)ecckey
, ecckey_len
, 0);
2690 CryptMemFree(ecckey
);
2692 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2696 static BOOL
CNG_ImportRSAPubKey(CERT_PUBLIC_KEY_INFO
*info
, BCRYPT_KEY_HANDLE
*key
)
2698 DWORD size
, modulus_len
, i
;
2700 RSAPUBKEY
*rsapubkey
;
2701 BCRYPT_ALG_HANDLE alg_handle
;
2702 BCRYPT_RSAKEY_BLOB
*rsakey
;
2706 if (!info
->PublicKey
.cbData
)
2708 SetLastError(NTE_BAD_ALGID
);
2712 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, RSA_CSP_PUBLICKEYBLOB
, info
->PublicKey
.pbData
,
2713 info
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &hdr
, &size
))
2715 WARN("CryptDecodeObjectEx failed\n");
2719 if (hdr
->aiKeyAlg
== CALG_RSA_KEYX
)
2720 alg_handle
= BCRYPT_RSA_ALG_HANDLE
;
2721 else if (hdr
->aiKeyAlg
== CALG_RSA_SIGN
)
2722 alg_handle
= BCRYPT_RSA_SIGN_ALG_HANDLE
;
2725 FIXME("Unsupported RSA algorithm: %#x\n", hdr
->aiKeyAlg
);
2727 SetLastError(NTE_BAD_ALGID
);
2731 rsapubkey
= (RSAPUBKEY
*)(hdr
+ 1);
2733 modulus_len
= size
- sizeof(*hdr
) - sizeof(*rsapubkey
);
2734 if (modulus_len
!= rsapubkey
->bitlen
/ 8)
2735 FIXME("RSA pubkey has wrong modulus_len %lu\n", modulus_len
);
2737 size
= sizeof(*rsakey
) + sizeof(ULONG
) + modulus_len
;
2738 if (!(rsakey
= CryptMemAlloc(size
)))
2739 return STATUS_NO_MEMORY
;
2741 rsakey
->Magic
= BCRYPT_RSAPUBLIC_MAGIC
;
2742 rsakey
->BitLength
= rsapubkey
->bitlen
;
2743 rsakey
->cbPublicExp
= sizeof(ULONG
);
2744 rsakey
->cbModulus
= modulus_len
;
2745 rsakey
->cbPrime1
= 0;
2746 rsakey
->cbPrime2
= 0;
2748 d
= (BYTE
*)(rsakey
+ 1);
2749 /* According to MSDN modulus and pubexp are in LE while
2750 * BCRYPT_RSAKEY_BLOB is supposed to have them in BE format */
2751 *(ULONG
*)d
= RtlUlongByteSwap(rsapubkey
->pubexp
);
2753 s
= (BYTE
*)(rsapubkey
+ 1);
2754 for (i
= 0; i
< modulus_len
; i
++)
2755 d
[i
] = s
[modulus_len
- i
- 1];
2757 status
= BCryptImportKeyPair(alg_handle
, NULL
, BCRYPT_RSAPUBLIC_BLOB
, key
, (BYTE
*)rsakey
, size
, 0);
2758 CryptMemFree(rsakey
);
2761 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2765 BOOL
CNG_ImportPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2767 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_ECC_PUBLIC_KEY
))
2768 return CNG_ImportECCPubKey(pubKeyInfo
, key
);
2770 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_RSA_RSA
))
2771 return CNG_ImportRSAPubKey(pubKeyInfo
, key
);
2773 FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo
->Algorithm
.pszObjId
));
2774 SetLastError(NTE_BAD_ALGID
);
2778 static BOOL
CNG_PrepareSignatureECC(BYTE
*encoded_sig
, DWORD encoded_size
, BYTE
**sig_value
, DWORD
*sig_len
)
2780 CERT_ECC_SIGNATURE
*ecc_sig
;
2784 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ECC_SIGNATURE
, encoded_sig
, encoded_size
,
2785 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_sig
, &size
))
2788 if (!ecc_sig
->r
.cbData
|| !ecc_sig
->s
.cbData
)
2791 SetLastError(ERROR_INVALID_DATA
);
2795 *sig_len
= ecc_sig
->r
.cbData
+ ecc_sig
->s
.cbData
;
2796 if (!(*sig_value
= CryptMemAlloc(*sig_len
)))
2799 SetLastError(ERROR_OUTOFMEMORY
);
2803 for (i
= 0; i
< ecc_sig
->r
.cbData
; i
++)
2804 (*sig_value
)[i
] = ecc_sig
->r
.pbData
[ecc_sig
->r
.cbData
- i
- 1];
2805 for (i
= 0; i
< ecc_sig
->s
.cbData
; i
++)
2806 (*sig_value
)[ecc_sig
->r
.cbData
+ i
] = ecc_sig
->s
.pbData
[ecc_sig
->s
.cbData
- i
- 1];
2812 BOOL
cng_prepare_signature(const char *alg_oid
, BYTE
*encoded_sig
, DWORD encoded_sig_len
,
2813 BYTE
**sig_value
, DWORD
*sig_len
)
2815 if (!strcmp(alg_oid
, szOID_ECC_PUBLIC_KEY
))
2816 return CNG_PrepareSignatureECC(encoded_sig
, encoded_sig_len
, sig_value
, sig_len
);
2818 FIXME("Unsupported public key type: %s\n", debugstr_a(alg_oid
));
2819 SetLastError(NTE_BAD_ALGID
);
2823 static BOOL
CNG_PrepareCertSignature(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2824 BYTE
**sig_value
, DWORD
*sig_len
)
2830 if (!signedCert
->Signature
.cbData
)
2832 SetLastError(ERROR_INVALID_DATA
);
2836 if (!(encoded_sig
= CryptMemAlloc(signedCert
->Signature
.cbData
)))
2838 SetLastError(ERROR_OUTOFMEMORY
);
2842 for (i
= 0; i
< signedCert
->Signature
.cbData
; i
++)
2843 encoded_sig
[i
] = signedCert
->Signature
.pbData
[signedCert
->Signature
.cbData
- i
- 1];
2845 ret
= cng_prepare_signature(pubKeyInfo
->Algorithm
.pszObjId
, encoded_sig
, signedCert
->Signature
.cbData
,
2846 sig_value
, sig_len
);
2847 CryptMemFree(encoded_sig
);
2851 static BOOL
CNG_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2852 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2854 BCRYPT_KEY_HANDLE key
= NULL
;
2855 BYTE
*hash_value
= NULL
, *sig_value
;
2856 DWORD hash_len
, sig_len
;
2860 ret
= CNG_ImportPubKey(pubKeyInfo
, &key
);
2863 ret
= CNG_CalcHash(info
->pwszCNGAlgid
, signedCert
, &hash_value
, &hash_len
);
2866 ret
= CNG_PrepareCertSignature(pubKeyInfo
, signedCert
, &sig_value
, &sig_len
);
2869 status
= BCryptVerifySignature(key
, NULL
, hash_value
, hash_len
, sig_value
, sig_len
, 0);
2872 FIXME("Failed to verify signature: %08lx\n", status
);
2873 SetLastError(RtlNtStatusToDosError(status
));
2876 CryptMemFree(sig_value
);
2878 CryptMemFree(hash_value
);
2880 BCryptDestroyKey(key
);
2886 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2887 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2889 CCRYPT_OID_INFO
*info
;
2891 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2892 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2894 SetLastError(NTE_BAD_ALGID
);
2898 if (info
->Algid
== CALG_OID_INFO_CNG_ONLY
)
2899 return CNG_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2901 return CRYPT_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2904 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2905 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2906 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2909 CRYPT_DATA_BLOB subjectBlob
;
2911 TRACE("(%08Ix, %ld, %ld, %p, %ld, %p, %08lx, %p)\n", hCryptProv
,
2912 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2913 dwFlags
, pvReserved
);
2915 switch (dwSubjectType
)
2917 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2919 PCRYPT_DATA_BLOB blob
= pvSubject
;
2921 subjectBlob
.pbData
= blob
->pbData
;
2922 subjectBlob
.cbData
= blob
->cbData
;
2925 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2927 PCERT_CONTEXT context
= pvSubject
;
2929 subjectBlob
.pbData
= context
->pbCertEncoded
;
2930 subjectBlob
.cbData
= context
->cbCertEncoded
;
2933 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2935 PCRL_CONTEXT context
= pvSubject
;
2937 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2938 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2942 SetLastError(E_INVALIDARG
);
2948 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2951 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2952 subjectBlob
.pbData
, subjectBlob
.cbData
,
2953 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2954 &signedCert
, &size
);
2957 switch (dwIssuerType
)
2959 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2960 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2961 dwCertEncodingType
, pvIssuer
,
2964 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2965 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2967 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2970 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2971 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2974 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2977 SetLastError(E_INVALIDARG
);
2982 FIXME("unimplemented for NULL signer\n");
2983 SetLastError(E_INVALIDARG
);
2988 SetLastError(E_INVALIDARG
);
2991 LocalFree(signedCert
);
2997 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2998 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
3000 PCERT_EXTENSION ext
;
3003 TRACE("(%08lx, %p, %p, %ld)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
3006 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
3007 pCertInfo
->rgExtension
);
3010 CRYPT_BIT_BLOB usage
;
3011 DWORD size
= sizeof(usage
);
3013 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
3014 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
3018 if (cbKeyUsage
< usage
.cbData
)
3022 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
3023 if (cbKeyUsage
> usage
.cbData
)
3024 memset(pbKeyUsage
+ usage
.cbData
, 0,
3025 cbKeyUsage
- usage
.cbData
);
3034 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
3035 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
3037 PCERT_ENHKEY_USAGE usage
= NULL
;
3041 if (!pCertContext
|| !pcbUsage
)
3043 SetLastError(ERROR_INVALID_PARAMETER
);
3047 TRACE("(%p, %08lx, %p, %ld)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
3049 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
3053 if (CertGetCertificateContextProperty(pCertContext
,
3054 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
3056 LPBYTE buf
= CryptMemAlloc(propSize
);
3060 if (CertGetCertificateContextProperty(pCertContext
,
3061 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
3063 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
3064 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
3065 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
3071 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
3073 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
3074 pCertContext
->pCertInfo
->cExtension
,
3075 pCertContext
->pCertInfo
->rgExtension
);
3079 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
3080 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
3081 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
3086 /* If a particular location is specified, this should fail. Otherwise
3087 * it should succeed with an empty usage. (This is true on Win2k and
3088 * later, which we emulate.)
3092 SetLastError(CRYPT_E_NOT_FOUND
);
3096 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
3102 *pcbUsage
= bytesNeeded
;
3103 else if (*pcbUsage
< bytesNeeded
)
3105 SetLastError(ERROR_MORE_DATA
);
3106 *pcbUsage
= bytesNeeded
;
3111 *pcbUsage
= bytesNeeded
;
3115 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
3116 sizeof(CERT_ENHKEY_USAGE
) +
3117 usage
->cUsageIdentifier
* sizeof(LPSTR
));
3119 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
3120 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
3121 sizeof(CERT_ENHKEY_USAGE
));
3122 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3124 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3125 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
3126 nextOID
+= strlen(nextOID
) + 1;
3130 pUsage
->cUsageIdentifier
= 0;
3135 TRACE("returning %d\n", ret
);
3139 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
3140 PCERT_ENHKEY_USAGE pUsage
)
3144 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
3148 CRYPT_DATA_BLOB blob
= { 0, NULL
};
3150 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
3151 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
3154 ret
= CertSetCertificateContextProperty(pCertContext
,
3155 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
3156 LocalFree(blob
.pbData
);
3160 ret
= CertSetCertificateContextProperty(pCertContext
,
3161 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
3165 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
3166 LPCSTR pszUsageIdentifier
)
3171 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
3173 if (CertGetEnhancedKeyUsage(pCertContext
,
3174 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
3176 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
3180 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3181 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
3185 BOOL exists
= FALSE
;
3187 /* Make sure usage doesn't already exist */
3188 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
3190 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
3191 pszUsageIdentifier
))
3196 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
3197 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3203 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
3204 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
3205 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
3206 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
3207 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3209 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3210 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
3211 nextOID
+= strlen(nextOID
) + 1;
3213 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3214 strcpy(nextOID
, pszUsageIdentifier
);
3215 newUsage
->cUsageIdentifier
= i
+ 1;
3216 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
3217 CryptMemFree(newUsage
);
3223 CryptMemFree(usage
);
3230 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
3231 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3235 usage
->rgpszUsageIdentifier
=
3236 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
3237 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
3238 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
3239 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
3240 usage
->cUsageIdentifier
= 1;
3241 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
3242 CryptMemFree(usage
);
3250 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
3251 LPCSTR pszUsageIdentifier
)
3255 CERT_ENHKEY_USAGE usage
;
3257 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
3259 size
= sizeof(usage
);
3260 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3261 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
3262 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3264 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3268 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3269 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
3272 if (pUsage
->cUsageIdentifier
)
3277 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
3279 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
3280 pszUsageIdentifier
))
3282 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
3283 pUsage
->rgpszUsageIdentifier
[i
] =
3284 pUsage
->rgpszUsageIdentifier
[i
+ 1];
3286 pUsage
->cUsageIdentifier
--;
3287 /* Remove the usage if it's empty */
3288 if (pUsage
->cUsageIdentifier
)
3289 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
3291 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
3294 CryptMemFree(pUsage
);
3301 /* it fit in an empty usage, therefore there's nothing to remove */
3313 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
3315 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
3317 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3319 if (indexIndex
+ 1 > field
->cIndexes
)
3321 if (field
->cIndexes
)
3322 field
->indexes
= CryptMemRealloc(field
->indexes
,
3323 (indexIndex
+ 1) * sizeof(DWORD
));
3325 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
3328 field
->indexes
[indexIndex
] = 0;
3329 field
->cIndexes
= indexIndex
+ 1;
3333 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
3336 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
3339 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3341 assert(field
->cIndexes
);
3342 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
3346 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
3347 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
3350 DWORD i
, cbOIDs
= 0;
3351 BOOL allUsagesValid
= TRUE
;
3352 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
3354 TRACE("(%ld, %p, %d, %p, %ld)\n", cCerts
, rghCerts
, *cNumOIDs
,
3357 for (i
= 0; i
< cCerts
; i
++)
3359 CERT_ENHKEY_USAGE usage
;
3360 DWORD size
= sizeof(usage
);
3362 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
3363 /* Success is deliberately ignored: it implies all usages are valid */
3364 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3366 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3368 allUsagesValid
= FALSE
;
3371 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
3374 if (!validUsages
.cUsageIdentifier
)
3378 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
3379 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
3380 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3381 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
3383 validUsages
.rgpszUsageIdentifier
=
3384 CryptMemAlloc(cbOIDs
);
3385 if (validUsages
.rgpszUsageIdentifier
)
3387 LPSTR nextOID
= (LPSTR
)
3388 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
3389 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3391 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3393 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
3394 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
3395 pUsage
->rgpszUsageIdentifier
[j
]);
3396 nextOID
+= lstrlenA(nextOID
) + 1;
3402 struct BitField validIndexes
= { 0, NULL
};
3403 DWORD j
, k
, numRemoved
= 0;
3405 /* Merge: build a bitmap of all the indexes of
3406 * validUsages.rgpszUsageIdentifier that are in pUsage.
3408 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
3410 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
3412 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
3413 validUsages
.rgpszUsageIdentifier
[k
]))
3415 CRYPT_SetBitInField(&validIndexes
, k
);
3420 /* Merge by removing from validUsages those that are
3421 * not in the bitmap.
3423 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3425 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
3427 if (j
< validUsages
.cUsageIdentifier
- 1)
3429 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
3430 &validUsages
.rgpszUsageIdentifier
[j
+
3432 (validUsages
.cUsageIdentifier
- numRemoved
3433 - j
- 1) * sizeof(LPSTR
));
3435 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
3437 validUsages
.cUsageIdentifier
--;
3441 validUsages
.cUsageIdentifier
--;
3444 CryptMemFree(validIndexes
.indexes
);
3447 CryptMemFree(pUsage
);
3459 *cNumOIDs
= validUsages
.cUsageIdentifier
;
3462 else if (*pcbOIDs
< cbOIDs
)
3465 SetLastError(ERROR_MORE_DATA
);
3470 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
3471 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3474 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
3476 rghOIDs
[i
] = nextOID
;
3477 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
3478 nextOID
+= lstrlenA(nextOID
) + 1;
3482 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
3483 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
3484 TRACE("returning %d\n", ret
);
3488 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
3489 * pInfo is NULL, from the attributes of hProv.
3491 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
3492 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
3494 CRYPT_KEY_PROV_INFO info
= { 0 };
3502 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
3505 LPSTR szContainer
= CryptMemAlloc(size
);
3509 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
3510 (BYTE
*)szContainer
, &size
, 0);
3513 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3517 info
.pwszContainerName
= CryptMemAlloc(len
*
3519 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3520 info
.pwszContainerName
, len
);
3523 CryptMemFree(szContainer
);
3526 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
3529 LPSTR szProvider
= CryptMemAlloc(size
);
3533 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
3537 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3541 info
.pwszProvName
= CryptMemAlloc(len
*
3543 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3544 info
.pwszProvName
, len
);
3547 CryptMemFree(szProvider
);
3550 /* in case no CRYPT_KEY_PROV_INFO given,
3551 * we always use AT_SIGNATURE key spec
3553 info
.dwKeySpec
= AT_SIGNATURE
;
3554 size
= sizeof(info
.dwProvType
);
3555 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
3558 info
.dwProvType
= PROV_RSA_FULL
;
3562 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
3567 CryptMemFree(info
.pwszContainerName
);
3568 CryptMemFree(info
.pwszProvName
);
3572 /* Creates a signed certificate context from the unsigned, encoded certificate
3573 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
3575 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
3576 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
3578 PCCERT_CONTEXT context
= NULL
;
3582 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3583 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
3586 LPBYTE sig
= CryptMemAlloc(sigSize
);
3588 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3589 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
3592 CERT_SIGNED_CONTENT_INFO signedInfo
;
3593 BYTE
*encodedSignedCert
= NULL
;
3594 DWORD encodedSignedCertSize
= 0;
3596 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
3597 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
3598 signedInfo
.SignatureAlgorithm
= *sigAlgo
;
3599 signedInfo
.Signature
.cbData
= sigSize
;
3600 signedInfo
.Signature
.pbData
= sig
;
3601 signedInfo
.Signature
.cUnusedBits
= 0;
3602 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
3603 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
3604 &encodedSignedCert
, &encodedSignedCertSize
);
3607 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3608 encodedSignedCert
, encodedSignedCertSize
);
3609 LocalFree(encodedSignedCert
);
3617 /* Copies data from the parameters into info, where:
3618 * pSerialNumber: The serial number. Must not be NULL.
3619 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3621 * pSignatureAlgorithm: Optional.
3622 * pStartTime: The starting time of the certificate. If NULL, the current
3623 * system time is used.
3624 * pEndTime: The ending time of the certificate. If NULL, one year past the
3625 * starting time is used.
3626 * pubKey: The public key of the certificate. Must not be NULL.
3627 * pExtensions: Extensions to be included with the certificate. Optional.
3629 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3630 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3631 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3632 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3633 const CERT_EXTENSIONS
*pExtensions
)
3635 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3638 assert(pSerialNumber
);
3639 assert(pSubjectIssuerBlob
);
3642 if (pExtensions
&& pExtensions
->cExtension
)
3643 info
->dwVersion
= CERT_V3
;
3645 info
->dwVersion
= CERT_V1
;
3646 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3647 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3648 if (pSignatureAlgorithm
)
3649 info
->SignatureAlgorithm
= *pSignatureAlgorithm
;
3652 info
->SignatureAlgorithm
.pszObjId
= oid
;
3653 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3654 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3656 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3657 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3659 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3661 GetSystemTimeAsFileTime(&info
->NotBefore
);
3663 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3668 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3671 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3674 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3675 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3676 info
->SubjectPublicKeyInfo
= *pubKey
;
3679 info
->cExtension
= pExtensions
->cExtension
;
3680 info
->rgExtension
= pExtensions
->rgExtension
;
3684 info
->cExtension
= 0;
3685 info
->rgExtension
= NULL
;
3689 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3690 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3691 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3693 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3695 HCRYPTPROV hProv
= 0;
3696 HMODULE rpcrt
= LoadLibraryW(L
"rpcrt4");
3700 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3702 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3704 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3705 rpcrt
, "RpcStringFreeA");
3707 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3710 RPC_STATUS status
= uuidCreate(&uuid
);
3712 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3714 unsigned char *uuidStr
;
3716 status
= uuidToString(&uuid
, &uuidStr
);
3717 if (status
== RPC_S_OK
)
3719 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3720 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3726 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3728 CryptDestroyKey(key
);
3730 rpcStringFree(&uuidStr
);
3739 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3740 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3741 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3742 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3743 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3745 PCCERT_CONTEXT context
= NULL
;
3746 BOOL ret
, releaseContext
= FALSE
;
3747 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3748 DWORD pubKeySize
= 0, dwKeySpec
;
3750 TRACE("(%08Ix, %p, %08lx, %p, %p, %p, %p, %p)\n", hProv
,
3751 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3752 pExtensions
, pExtensions
);
3754 if(!pSubjectIssuerBlob
)
3756 SetLastError(ERROR_INVALID_PARAMETER
);
3760 dwKeySpec
= pKeyProvInfo
? pKeyProvInfo
->dwKeySpec
: AT_SIGNATURE
;
3765 hProv
= CRYPT_CreateKeyProv();
3766 releaseContext
= TRUE
;
3768 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3770 SetLastError(NTE_BAD_FLAGS
);
3776 /* acquire the context using the given information*/
3777 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3778 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3779 pKeyProvInfo
->dwFlags
);
3782 if(GetLastError() != NTE_BAD_KEYSET
)
3784 /* create the key set */
3785 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3786 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3787 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3791 /* check if the key is here */
3792 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3795 if (NTE_NO_KEY
== GetLastError())
3796 { /* generate the key */
3797 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3801 CryptReleaseContext(hProv
,0);
3802 SetLastError(NTE_BAD_KEYSET
);
3806 CryptDestroyKey(hKey
);
3807 releaseContext
= TRUE
;
3811 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3815 pubKey
= CryptMemAlloc(pubKeySize
);
3818 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3819 pubKey
, &pubKeySize
);
3822 CERT_INFO info
= { 0 };
3823 CRYPT_DER_BLOB blob
= { 0, NULL
};
3825 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3827 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3828 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3829 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3830 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3831 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3835 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3836 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3837 &info
.SignatureAlgorithm
);
3839 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3840 blob
.pbData
, blob
.cbData
);
3841 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3842 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3843 LocalFree(blob
.pbData
);
3846 CryptMemFree(pubKey
);
3850 CryptReleaseContext(hProv
, 0);
3854 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3855 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3856 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3857 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3859 FIXME("(0x%lx, %ld, %p, %p, 0x%lx, %p, %p): stub\n", dwEncodingType
,
3860 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3861 pVerifyUsageStatus
);
3862 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3866 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3867 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3868 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3870 TRACE("(0x%lx, 0x%lx, %p, %ld, 0x%08lx, %p)\n", dwContextType
, dwEncodingType
,
3871 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3875 FIXME("dwFlags 0x%08lx not handled\n", dwFlags
);
3880 FIXME("pCreatePara not handled\n");
3884 switch (dwContextType
)
3886 case CERT_STORE_CERTIFICATE_CONTEXT
:
3887 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3888 case CERT_STORE_CRL_CONTEXT
:
3889 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3890 case CERT_STORE_CTL_CONTEXT
:
3891 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3893 WARN("unknown context type: 0x%lx\n", dwContextType
);
3898 BOOL WINAPI
CryptSetKeyIdentifierProperty(const CRYPT_HASH_BLOB
*pKeyIdentifier
, DWORD dwPropId
,
3899 DWORD dwFlags
, LPCWSTR pwszComputerName
, void *pvReserved
, const void *pvData
)
3901 FIXME("(%p, 0x%lx, 0x%lx, %s, %p, %p): stub\n", pKeyIdentifier
, dwPropId
, dwFlags
,
3902 debugstr_w(pwszComputerName
), pvReserved
, pvData
);