2 * Copyright 2004-2006 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
25 #define WIN32_NO_STATUS
28 #include "wine/winternl.h"
29 #define CRYPT_OID_INFO_HAS_EXTRA_FIELDS
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
37 #include "crypt32_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
41 /* Internal version of CertGetCertificateContextProperty that gets properties
42 * directly from the context (or the context it's linked to, depending on its
43 * type.) Doesn't handle special-case properties, since they are handled by
44 * CertGetCertificateContextProperty, and are particular to the store in which
45 * the property exists (which is separate from the context.)
47 static BOOL
CertContext_GetProperty(cert_t
*cert
, DWORD dwPropId
,
48 void *pvData
, DWORD
*pcbData
);
50 /* Internal version of CertSetCertificateContextProperty that sets properties
51 * directly on the context (or the context it's linked to, depending on its
52 * type.) Doesn't handle special cases, since they're handled by
53 * CertSetCertificateContextProperty anyway.
55 static BOOL
CertContext_SetProperty(cert_t
*cert
, DWORD dwPropId
,
56 DWORD dwFlags
, const void *pvData
);
58 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
59 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
60 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
62 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
63 pbCertEncoded
, cbCertEncoded
);
66 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
, dwCertEncodingType
,
67 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
71 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
72 dwAddDisposition
, ppCertContext
);
73 CertFreeCertificateContext(cert
);
80 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreA(LPCSTR pszCertStoreName
,
81 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
86 TRACE("(%s, %p, %d)\n", debugstr_a(pszCertStoreName
), pbCertEncoded
,
89 store
= CertOpenSystemStoreA(0, pszCertStoreName
);
92 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
93 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
94 CertCloseStore(store
, 0);
99 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreW(LPCWSTR pszCertStoreName
,
100 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
105 TRACE("(%s, %p, %d)\n", debugstr_w(pszCertStoreName
), pbCertEncoded
,
108 store
= CertOpenSystemStoreW(0, pszCertStoreName
);
111 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
112 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
113 CertCloseStore(store
, 0);
118 static const context_vtbl_t cert_vtbl
;
120 static void Cert_free(context_t
*context
)
122 cert_t
*cert
= (cert_t
*)context
;
124 CryptMemFree(cert
->ctx
.pbCertEncoded
);
125 LocalFree(cert
->ctx
.pCertInfo
);
128 static context_t
*Cert_clone(context_t
*context
, WINECRYPT_CERTSTORE
*store
, BOOL use_link
)
133 cert
= (cert_t
*)Context_CreateLinkContext(sizeof(CERT_CONTEXT
), context
, store
);
137 const cert_t
*cloned
= (const cert_t
*)context
;
141 cert
= (cert_t
*)Context_CreateDataContext(sizeof(CERT_CONTEXT
), &cert_vtbl
, store
);
145 Context_CopyProperties(&cert
->ctx
, &cloned
->ctx
);
147 cert
->ctx
.dwCertEncodingType
= cloned
->ctx
.dwCertEncodingType
;
148 cert
->ctx
.pbCertEncoded
= CryptMemAlloc(cloned
->ctx
.cbCertEncoded
);
149 memcpy(cert
->ctx
.pbCertEncoded
, cloned
->ctx
.pbCertEncoded
, cloned
->ctx
.cbCertEncoded
);
150 cert
->ctx
.cbCertEncoded
= cloned
->ctx
.cbCertEncoded
;
152 /* FIXME: We don't need to decode the object here, we could just clone cert info. */
153 res
= CryptDecodeObjectEx(cert
->ctx
.dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
154 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
155 &cert
->ctx
.pCertInfo
, &size
);
157 CertFreeCertificateContext(&cert
->ctx
);
162 cert
->ctx
.hCertStore
= store
;
166 static const context_vtbl_t cert_vtbl
= {
171 static BOOL
add_cert_to_store(WINECRYPT_CERTSTORE
*store
, const CERT_CONTEXT
*cert
,
172 DWORD add_disposition
, BOOL use_link
, PCCERT_CONTEXT
*ret_context
)
174 const CERT_CONTEXT
*existing
= NULL
;
175 BOOL ret
= TRUE
, inherit_props
= FALSE
;
176 context_t
*new_context
= NULL
;
178 switch (add_disposition
)
180 case CERT_STORE_ADD_ALWAYS
:
182 case CERT_STORE_ADD_NEW
:
183 case CERT_STORE_ADD_REPLACE_EXISTING
:
184 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
185 case CERT_STORE_ADD_USE_EXISTING
:
186 case CERT_STORE_ADD_NEWER
:
187 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
190 DWORD size
= sizeof(hashToAdd
);
192 ret
= CertGetCertificateContextProperty(cert
, CERT_HASH_PROP_ID
,
196 CRYPT_HASH_BLOB blob
= { sizeof(hashToAdd
), hashToAdd
};
198 existing
= CertFindCertificateInStore(store
, cert
->dwCertEncodingType
, 0,
199 CERT_FIND_SHA1_HASH
, &blob
, NULL
);
204 FIXME("Unimplemented add disposition %d\n", add_disposition
);
205 SetLastError(E_INVALIDARG
);
209 switch (add_disposition
)
211 case CERT_STORE_ADD_ALWAYS
:
213 case CERT_STORE_ADD_NEW
:
216 TRACE("found matching certificate, not adding\n");
217 SetLastError(CRYPT_E_EXISTS
);
221 case CERT_STORE_ADD_REPLACE_EXISTING
:
223 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
225 FIXME("CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES: semi-stub for links\n");
227 inherit_props
= TRUE
;
229 case CERT_STORE_ADD_USE_EXISTING
:
231 FIXME("CERT_STORE_ADD_USE_EXISTING: semi-stub for links\n");
234 Context_CopyProperties(existing
, cert
);
236 *ret_context
= CertDuplicateCertificateContext(existing
);
240 case CERT_STORE_ADD_NEWER
:
241 if (existing
&& CompareFileTime(&existing
->pCertInfo
->NotBefore
, &cert
->pCertInfo
->NotBefore
) >= 0)
243 TRACE("existing certificate is newer, not adding\n");
244 SetLastError(CRYPT_E_EXISTS
);
248 case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES
:
251 if (CompareFileTime(&existing
->pCertInfo
->NotBefore
, &cert
->pCertInfo
->NotBefore
) >= 0)
253 TRACE("existing certificate is newer, not adding\n");
254 SetLastError(CRYPT_E_EXISTS
);
257 inherit_props
= TRUE
;
262 /* FIXME: We have tests that this works, but what should we really do in this case? */
265 *ret_context
= CertDuplicateCertificateContext(cert
);
269 ret
= store
->vtbl
->certs
.addContext(store
, context_from_ptr(cert
), existing
? context_from_ptr(existing
) : NULL
,
270 (ret_context
|| inherit_props
) ? &new_context
: NULL
, use_link
);
275 Context_CopyProperties(context_ptr(new_context
), existing
);
278 *ret_context
= context_ptr(new_context
);
280 Context_Release(new_context
);
282 TRACE("returning %d\n", ret
);
286 BOOL WINAPI
CertAddCertificateContextToStore(HCERTSTORE hCertStore
, PCCERT_CONTEXT pCertContext
,
287 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppStoreContext
)
289 WINECRYPT_CERTSTORE
*store
= hCertStore
;
291 TRACE("(%p, %p, %08x, %p)\n", hCertStore
, pCertContext
, dwAddDisposition
, ppStoreContext
);
293 return add_cert_to_store(store
, pCertContext
, dwAddDisposition
, FALSE
, ppStoreContext
);
296 BOOL WINAPI
CertAddCertificateLinkToStore(HCERTSTORE hCertStore
,
297 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
298 PCCERT_CONTEXT
*ppCertContext
)
301 WINECRYPT_CERTSTORE
*store
= (WINECRYPT_CERTSTORE
*)hCertStore
;
304 FIXME("(%p, %p, %08x, %p): semi-stub\n", hCertStore
, pCertContext
,
305 dwAddDisposition
, ppCertContext
);
306 if (store
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
308 if (store
->type
== StoreTypeCollection
)
310 SetLastError(E_INVALIDARG
);
313 return add_cert_to_store(hCertStore
, pCertContext
, dwAddDisposition
, TRUE
, ppCertContext
);
316 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
317 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
322 PCERT_INFO certInfo
= NULL
;
325 TRACE("(%08x, %p, %d)\n", dwCertEncodingType
, pbCertEncoded
,
328 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
330 SetLastError(E_INVALIDARG
);
334 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
335 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
340 cert
= (cert_t
*)Context_CreateDataContext(sizeof(CERT_CONTEXT
), &cert_vtbl
, &empty_store
);
343 data
= CryptMemAlloc(cbCertEncoded
);
346 Context_Release(&cert
->base
);
350 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
351 cert
->ctx
.dwCertEncodingType
= dwCertEncodingType
;
352 cert
->ctx
.pbCertEncoded
= data
;
353 cert
->ctx
.cbCertEncoded
= cbCertEncoded
;
354 cert
->ctx
.pCertInfo
= certInfo
;
355 cert
->ctx
.hCertStore
= &empty_store
;
360 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(PCCERT_CONTEXT pCertContext
)
362 TRACE("(%p)\n", pCertContext
);
367 Context_AddRef(&cert_from_ptr(pCertContext
)->base
);
371 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
373 TRACE("(%p)\n", pCertContext
);
376 Context_Release(&cert_from_ptr(pCertContext
)->base
);
380 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
383 cert_t
*cert
= cert_from_ptr(pCertContext
);
386 TRACE("(%p, %d)\n", pCertContext
, dwPropId
);
388 if (cert
->base
.properties
)
389 ret
= ContextPropertyList_EnumPropIDs(cert
->base
.properties
, dwPropId
);
395 static BOOL
CertContext_GetHashProp(cert_t
*cert
, DWORD dwPropId
,
396 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
399 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
403 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
405 ret
= CertContext_SetProperty(cert
, dwPropId
, 0, &blob
);
410 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
417 else if (*pcbData
< cb
)
419 SetLastError(ERROR_MORE_DATA
);
425 memcpy(pvData
, pb
, cb
);
431 static BOOL
CertContext_GetProperty(cert_t
*cert
, DWORD dwPropId
,
432 void *pvData
, DWORD
*pcbData
)
435 CRYPT_DATA_BLOB blob
;
437 TRACE("(%p, %d, %p, %p)\n", cert
, dwPropId
, pvData
, pcbData
);
439 if (cert
->base
.properties
)
440 ret
= ContextPropertyList_FindProperty(cert
->base
.properties
, dwPropId
, &blob
);
444 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
447 /* Implicit properties */
450 case CERT_SHA1_HASH_PROP_ID
:
451 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_SHA1
,
452 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
455 case CERT_MD5_HASH_PROP_ID
:
456 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
457 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
460 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
461 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
462 cert
->ctx
.pCertInfo
->Subject
.pbData
,
463 cert
->ctx
.pCertInfo
->Subject
.cbData
,
466 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
467 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
468 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
469 cert
->ctx
.pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
472 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
473 ret
= CertContext_GetHashProp(cert
, dwPropId
, CALG_MD5
,
474 cert
->ctx
.pCertInfo
->SerialNumber
.pbData
,
475 cert
->ctx
.pCertInfo
->SerialNumber
.cbData
,
478 case CERT_SIGNATURE_HASH_PROP_ID
:
479 ret
= CryptHashToBeSigned(0, cert
->ctx
.dwCertEncodingType
,
480 cert
->ctx
.pbCertEncoded
, cert
->ctx
.cbCertEncoded
, pvData
,
484 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
486 ret
= CertContext_SetProperty(cert
, dwPropId
, 0, &blob
);
489 case CERT_KEY_IDENTIFIER_PROP_ID
:
491 PCERT_EXTENSION ext
= CertFindExtension(
492 szOID_SUBJECT_KEY_IDENTIFIER
, cert
->ctx
.pCertInfo
->cExtension
,
493 cert
->ctx
.pCertInfo
->rgExtension
);
497 CRYPT_DATA_BLOB value
;
498 DWORD size
= sizeof(value
);
500 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
501 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
502 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
506 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
508 CertContext_SetProperty(cert
, dwPropId
, 0, &value
);
512 SetLastError(ERROR_INVALID_DATA
);
516 SetLastError(CRYPT_E_NOT_FOUND
);
519 TRACE("returning %d\n", ret
);
523 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
525 DWORD i
, containerLen
, provNameLen
;
526 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
528 info
->pwszContainerName
= (LPWSTR
)data
;
529 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
530 data
+= containerLen
;
532 info
->pwszProvName
= (LPWSTR
)data
;
533 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
536 if (info
->cProvParam
)
538 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
539 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
541 for (i
= 0; i
< info
->cProvParam
; i
++)
543 info
->rgProvParam
[i
].pbData
= data
;
544 data
+= info
->rgProvParam
[i
].cbData
;
548 info
->rgProvParam
= NULL
;
551 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
552 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
554 cert_t
*cert
= cert_from_ptr(pCertContext
);
557 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
562 case CERT_CERT_PROP_ID
:
563 case CERT_CRL_PROP_ID
:
564 case CERT_CTL_PROP_ID
:
565 SetLastError(E_INVALIDARG
);
568 case CERT_ACCESS_STATE_PROP_ID
:
569 ret
= CertGetStoreProperty(cert
->ctx
.hCertStore
, dwPropId
, pvData
, pcbData
);
571 case CERT_KEY_PROV_HANDLE_PROP_ID
:
573 CERT_KEY_CONTEXT keyContext
;
574 DWORD size
= sizeof(keyContext
);
576 ret
= CertContext_GetProperty(cert
,
577 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
579 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
580 sizeof(keyContext
.hCryptProv
));
583 case CERT_KEY_PROV_INFO_PROP_ID
:
584 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
,
587 CRYPT_FixKeyProvInfoPointers(pvData
);
590 ret
= CertContext_GetProperty(cert
, dwPropId
, pvData
,
594 TRACE("returning %d\n", ret
);
598 /* Copies key provider info from from into to, where to is assumed to be a
599 * contiguous buffer of memory large enough for from and all its associated
600 * data, but whose pointers are uninitialized.
601 * Upon return, to contains a contiguous copy of from, packed in the following
603 * - CRYPT_KEY_PROV_INFO
604 * - pwszContainerName
606 * - rgProvParam[0]...
608 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
609 const CRYPT_KEY_PROV_INFO
*from
)
612 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
614 if (from
->pwszContainerName
)
616 to
->pwszContainerName
= (LPWSTR
)nextData
;
617 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
618 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
621 to
->pwszContainerName
= NULL
;
622 if (from
->pwszProvName
)
624 to
->pwszProvName
= (LPWSTR
)nextData
;
625 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
626 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
629 to
->pwszProvName
= NULL
;
630 to
->dwProvType
= from
->dwProvType
;
631 to
->dwFlags
= from
->dwFlags
;
632 to
->cProvParam
= from
->cProvParam
;
633 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
634 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
635 to
->dwKeySpec
= from
->dwKeySpec
;
636 for (i
= 0; i
< to
->cProvParam
; i
++)
638 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
639 sizeof(CRYPT_KEY_PROV_PARAM
));
640 to
->rgProvParam
[i
].pbData
= nextData
;
641 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
642 from
->rgProvParam
[i
].cbData
);
643 nextData
+= from
->rgProvParam
[i
].cbData
;
647 static BOOL
CertContext_SetKeyProvInfoProperty(CONTEXT_PROPERTY_LIST
*properties
,
648 const CRYPT_KEY_PROV_INFO
*info
)
652 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
654 if (info
->pwszContainerName
)
655 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
658 if (info
->pwszProvName
)
659 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
662 size
+= containerSize
+ provNameSize
;
663 for (i
= 0; i
< info
->cProvParam
; i
++)
664 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
665 buf
= CryptMemAlloc(size
);
668 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
669 ret
= ContextPropertyList_SetProperty(properties
,
670 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
678 static BOOL
CertContext_SetProperty(cert_t
*cert
, DWORD dwPropId
,
679 DWORD dwFlags
, const void *pvData
)
683 TRACE("(%p, %d, %08x, %p)\n", cert
, dwPropId
, dwFlags
, pvData
);
685 if (!cert
->base
.properties
)
691 case CERT_AUTO_ENROLL_PROP_ID
:
692 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
693 case CERT_DESCRIPTION_PROP_ID
:
694 case CERT_FRIENDLY_NAME_PROP_ID
:
695 case CERT_HASH_PROP_ID
:
696 case CERT_KEY_IDENTIFIER_PROP_ID
:
697 case CERT_MD5_HASH_PROP_ID
:
698 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
699 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
700 case CERT_PVK_FILE_PROP_ID
:
701 case CERT_SIGNATURE_HASH_PROP_ID
:
702 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
703 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
704 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
705 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
706 case CERT_ENROLLMENT_PROP_ID
:
707 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
708 case CERT_OCSP_RESPONSE_PROP_ID
:
709 case CERT_RENEWAL_PROP_ID
:
713 const CRYPT_DATA_BLOB
*blob
= pvData
;
715 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
716 blob
->pbData
, blob
->cbData
);
720 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
725 case CERT_DATE_STAMP_PROP_ID
:
727 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
728 pvData
, sizeof(FILETIME
));
731 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
735 case CERT_KEY_CONTEXT_PROP_ID
:
739 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
741 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
743 SetLastError(E_INVALIDARG
);
747 ret
= ContextPropertyList_SetProperty(cert
->base
.properties
, dwPropId
,
748 (const BYTE
*)keyContext
, keyContext
->cbSize
);
752 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
757 case CERT_KEY_PROV_INFO_PROP_ID
:
759 ret
= CertContext_SetKeyProvInfoProperty(cert
->base
.properties
, pvData
);
762 ContextPropertyList_RemoveProperty(cert
->base
.properties
, dwPropId
);
766 case CERT_KEY_PROV_HANDLE_PROP_ID
:
768 CERT_KEY_CONTEXT keyContext
;
769 DWORD size
= sizeof(keyContext
);
771 ret
= CertContext_GetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
775 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
776 CryptReleaseContext(keyContext
.hCryptProv
, 0);
778 keyContext
.cbSize
= sizeof(keyContext
);
780 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
783 keyContext
.hCryptProv
= 0;
784 keyContext
.dwKeySpec
= AT_SIGNATURE
;
786 ret
= CertContext_SetProperty(cert
, CERT_KEY_CONTEXT_PROP_ID
,
791 FIXME("%d: stub\n", dwPropId
);
795 TRACE("returning %d\n", ret
);
799 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
800 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
804 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
806 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
807 * crashes on most of these, I'll be safer.
812 case CERT_ACCESS_STATE_PROP_ID
:
813 case CERT_CERT_PROP_ID
:
814 case CERT_CRL_PROP_ID
:
815 case CERT_CTL_PROP_ID
:
816 SetLastError(E_INVALIDARG
);
819 ret
= CertContext_SetProperty(cert_from_ptr(pCertContext
), dwPropId
, dwFlags
,
821 TRACE("returning %d\n", ret
);
825 /* Acquires the private key using the key provider info, retrieving info from
826 * the certificate if info is NULL. The acquired provider is returned in
827 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
829 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
830 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
833 BOOL allocated
= FALSE
, ret
= TRUE
;
837 ret
= CertGetCertificateContextProperty(pCert
,
838 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
841 info
= HeapAlloc(GetProcessHeap(), 0, size
);
844 ret
= CertGetCertificateContextProperty(pCert
,
845 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
850 SetLastError(ERROR_OUTOFMEMORY
);
855 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
859 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
860 info
->pwszProvName
, info
->dwProvType
, 0);
865 for (i
= 0; i
< info
->cProvParam
; i
++)
867 CryptSetProvParam(*phCryptProv
,
868 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
869 info
->rgProvParam
[i
].dwFlags
);
871 *pdwKeySpec
= info
->dwKeySpec
;
874 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
877 HeapFree(GetProcessHeap(), 0, info
);
881 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
882 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
883 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
885 BOOL ret
= FALSE
, cache
= FALSE
;
886 PCRYPT_KEY_PROV_INFO info
= NULL
;
887 CERT_KEY_CONTEXT keyContext
;
890 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
891 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
893 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
897 ret
= CertGetCertificateContextProperty(pCert
,
898 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
901 info
= HeapAlloc(GetProcessHeap(), 0, size
);
902 ret
= CertGetCertificateContextProperty(pCert
,
903 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
905 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
908 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
913 size
= sizeof(keyContext
);
914 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
918 *phCryptProv
= keyContext
.hCryptProv
;
920 *pdwKeySpec
= keyContext
.dwKeySpec
;
921 if (pfCallerFreeProv
)
922 *pfCallerFreeProv
= !cache
;
927 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
928 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
931 *phCryptProv
= keyContext
.hCryptProv
;
933 *pdwKeySpec
= keyContext
.dwKeySpec
;
936 keyContext
.cbSize
= sizeof(keyContext
);
937 if (CertSetCertificateContextProperty(pCert
,
938 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
940 if (pfCallerFreeProv
)
941 *pfCallerFreeProv
= FALSE
;
946 if (pfCallerFreeProv
)
947 *pfCallerFreeProv
= TRUE
;
951 HeapFree(GetProcessHeap(), 0, info
);
955 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
956 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
959 BOOL matches
= FALSE
;
961 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
962 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
966 /* Need to sign something to verify the sig. What to sign? Why not
967 * the certificate itself?
969 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
970 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
971 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
973 BYTE
*certEncoded
= CryptMemAlloc(size
);
977 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
978 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
979 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
980 NULL
, certEncoded
, &size
))
982 if (size
== pCert
->cbCertEncoded
&&
983 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
986 CryptMemFree(certEncoded
);
989 CryptReleaseContext(csp
, 0);
994 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
995 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
997 CRYPT_KEY_PROV_INFO copy
;
998 WCHAR containerW
[MAX_PATH
];
1001 MultiByteToWideChar(CP_ACP
, 0, container
, -1, containerW
, ARRAY_SIZE(containerW
));
1002 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
1003 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
1006 copy
= *keyProvInfo
;
1007 copy
.pwszContainerName
= containerW
;
1008 matches
= key_prov_info_matches_cert(pCert
, ©
);
1011 keyProvInfo
->pwszContainerName
=
1012 CryptMemAlloc((strlenW(containerW
) + 1) * sizeof(WCHAR
));
1013 if (keyProvInfo
->pwszContainerName
)
1015 strcpyW(keyProvInfo
->pwszContainerName
, containerW
);
1016 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
1024 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
1025 * private key matches pCert's public key. Upon success, updates keyProvInfo
1026 * with the matching container's info (free keyProvInfo.pwszContainerName upon
1028 * Returns TRUE if found, FALSE if not.
1030 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
1031 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
1033 HCRYPTPROV defProvider
;
1034 BOOL ret
, found
= FALSE
;
1035 char containerA
[MAX_PATH
];
1037 assert(keyProvInfo
->pwszContainerName
== NULL
);
1038 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
1039 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
1040 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
1042 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
1044 while (ret
&& !found
)
1046 DWORD size
= sizeof(containerA
);
1048 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
1049 (BYTE
*)containerA
, &size
, enumFlags
);
1051 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
1052 if (enumFlags
& CRYPT_FIRST
)
1054 enumFlags
&= ~CRYPT_FIRST
;
1055 enumFlags
|= CRYPT_NEXT
;
1058 CryptReleaseContext(defProvider
, 0);
1063 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
1065 BOOL found
= FALSE
, ret
= TRUE
;
1066 DWORD index
= 0, cbProvName
= 0;
1067 CRYPT_KEY_PROV_INFO keyProvInfo
;
1069 TRACE("(%p, %08x)\n", pCert
, dwFlags
);
1071 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
1072 while (ret
&& !found
)
1076 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
1080 if (size
<= cbProvName
)
1081 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1082 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1085 CryptMemFree(keyProvInfo
.pwszProvName
);
1086 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
1087 if (keyProvInfo
.pwszProvName
)
1090 ret
= CryptEnumProvidersW(index
, NULL
, 0,
1091 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
1094 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
1095 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
1096 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
1097 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1098 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1100 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
1101 found
= find_key_prov_info_in_provider(pCert
,
1106 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
1107 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
1108 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
1110 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
1111 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
1112 found
= find_key_prov_info_in_provider(pCert
,
1125 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1127 CryptMemFree(keyProvInfo
.pwszProvName
);
1128 CryptMemFree(keyProvInfo
.pwszContainerName
);
1132 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
1134 BOOL matches
= FALSE
;
1137 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
1140 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
1144 if (CertGetCertificateContextProperty(pCert
,
1145 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
1146 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
1147 CryptMemFree(keyProvInfo
);
1153 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
1154 DWORD dwFlags
, void *pvReserved
)
1158 TRACE("(%p, %08x, %p)\n", pCert
, dwFlags
, pvReserved
);
1160 matches
= cert_prov_info_matches_cert(pCert
);
1162 matches
= find_matching_provider(pCert
, dwFlags
);
1166 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1167 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1171 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1173 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1174 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1175 &pCertId2
->SerialNumber
);
1176 TRACE("returning %d\n", ret
);
1180 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1181 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1185 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1187 if (pCertName1
->cbData
== pCertName2
->cbData
)
1189 if (pCertName1
->cbData
)
1190 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1191 pCertName1
->cbData
);
1197 TRACE("returning %d\n", ret
);
1201 /* Returns the number of significant bytes in pInt, where a byte is
1202 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1203 * for negative numbers. pInt is assumed to be little-endian.
1205 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1207 DWORD ret
= pInt
->cbData
;
1211 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1213 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1221 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1222 PCRYPT_INTEGER_BLOB pInt2
)
1227 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1229 cb1
= CRYPT_significantBytes(pInt1
);
1230 cb2
= CRYPT_significantBytes(pInt2
);
1234 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1240 TRACE("returning %d\n", ret
);
1244 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1245 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1249 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1251 /* RSA public key data should start with ASN_SEQUENCE,
1252 * otherwise it's not a RSA_CSP_PUBLICKEYBLOB.
1254 if (!pPublicKey1
->PublicKey
.cbData
|| pPublicKey1
->PublicKey
.pbData
[0] != ASN_SEQUENCE
)
1255 dwCertEncodingType
= 0;
1257 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1259 case 0: /* Seems to mean "raw binary bits" */
1260 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1261 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1263 if (pPublicKey2
->PublicKey
.cbData
)
1264 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1265 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1273 WARN("Unknown encoding type %08x\n", dwCertEncodingType
);
1275 case X509_ASN_ENCODING
:
1277 BLOBHEADER
*pblob1
, *pblob2
;
1280 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1281 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1282 CRYPT_DECODE_ALLOC_FLAG
, &pblob1
, &length
))
1284 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1285 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1286 CRYPT_DECODE_ALLOC_FLAG
, &pblob2
, &length
))
1288 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1289 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1290 *pk2
= (LPVOID
)(pblob2
+ 1);
1291 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1292 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1305 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1306 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1310 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
1312 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1314 SetLastError(ERROR_FILE_NOT_FOUND
);
1317 if (pPublicKey
->Algorithm
.pszObjId
&&
1318 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1320 FIXME("unimplemented for DH public keys\n");
1321 SetLastError(CRYPT_E_ASN1_BADTAG
);
1325 PCCRYPT_OID_INFO info
;
1330 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, pPublicKey
->Algorithm
.pszObjId
, 0);
1335 TRACE("public key algid %#x (%s)\n", info
->u
.Algid
, debugstr_a(pPublicKey
->Algorithm
.pszObjId
));
1337 ret
= CryptImportPublicKeyInfo(I_CryptGetDefaultCryptProv(info
->u
.Algid
), dwCertEncodingType
, pPublicKey
, &key
);
1341 ret
= CryptGetKeyParam(key
, KP_KEYLEN
, (BYTE
*)&len
, &size
, 0);
1342 CryptDestroyKey(key
);
1345 /* fallback to RSA */
1348 ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1349 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1350 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1355 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1357 len
= rsaPubKey
->bitlen
;
1364 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1365 DWORD dwFlags
, const void *pvPara
);
1367 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1368 DWORD dwFlags
, const void *pvPara
)
1372 DWORD size
= sizeof(hash
);
1374 ret
= CertGetCertificateContextProperty(pCertContext
,
1375 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1378 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1380 if (size
== pHash
->cbData
)
1381 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1388 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1389 DWORD dwFlags
, const void *pvPara
)
1393 DWORD size
= sizeof(hash
);
1395 ret
= CertGetCertificateContextProperty(pCertContext
,
1396 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1399 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1401 if (size
== pHash
->cbData
)
1402 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1409 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1410 DWORD dwFlags
, const void *pvPara
)
1412 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1415 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1416 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1418 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1419 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1424 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1425 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1427 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1430 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1431 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1435 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1436 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1438 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1441 /* Matching serial number and subject match.. */
1442 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1443 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1445 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1446 &pCertInfo
->SerialNumber
);
1449 /* failing that, if the serial number and issuer match, we match */
1450 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1451 &pCertInfo
->SerialNumber
);
1453 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1454 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1456 TRACE("returning %d\n", ret
);
1460 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1461 DWORD dwFlags
, const void *pvPara
)
1463 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1466 switch (id
->dwIdChoice
)
1468 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1469 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1470 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1472 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1473 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1475 case CERT_ID_SHA1_HASH
:
1476 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1479 case CERT_ID_KEY_IDENTIFIER
:
1483 ret
= CertGetCertificateContextProperty(pCertContext
,
1484 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1485 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1487 LPBYTE buf
= CryptMemAlloc(size
);
1491 CertGetCertificateContextProperty(pCertContext
,
1492 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1493 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1510 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1511 DWORD dwFlags
, const void *pvPara
)
1513 PCCERT_CONTEXT toCompare
= pvPara
;
1514 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1515 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1518 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1519 DWORD dwFlags
, const void *pvPara
)
1521 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1525 ret
= CertGetCertificateContextProperty(pCertContext
,
1526 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1527 if (ret
&& size
== hash
->cbData
)
1529 LPBYTE buf
= CryptMemAlloc(size
);
1533 CertGetCertificateContextProperty(pCertContext
,
1534 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1535 ret
= !memcmp(buf
, hash
->pbData
, size
);
1546 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1547 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1550 BOOL matches
= FALSE
;
1555 ret
= CertEnumCertificatesInStore(store
, ret
);
1557 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1558 } while (ret
!= NULL
&& !matches
);
1562 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1563 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1565 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1566 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1568 return CertEnumCertificatesInStore(store
, prev
);
1571 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1572 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1575 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1576 PCERT_EXTENSION ext
;
1579 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1580 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1582 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1584 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1585 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1586 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1592 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1594 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1595 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1596 sizeof(CERT_NAME_BLOB
));
1597 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1598 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1600 else if (info
->KeyId
.cbData
)
1602 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1603 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1608 found
= cert_compare_certs_in_store(store
, prev
,
1609 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1613 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1614 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1616 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1618 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1619 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1620 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1626 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1627 info
->AuthorityCertSerialNumber
.cbData
)
1629 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1632 for (i
= 0; !directoryName
&&
1633 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1634 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1635 == CERT_ALT_NAME_DIRECTORY_NAME
)
1637 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1640 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1641 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1642 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1643 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1644 &info
->AuthorityCertSerialNumber
,
1645 sizeof(CRYPT_INTEGER_BLOB
));
1649 FIXME("no supported name type in authority key id2\n");
1653 else if (info
->KeyId
.cbData
)
1655 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1656 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1661 found
= cert_compare_certs_in_store(store
, prev
,
1662 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1667 found
= cert_compare_certs_in_store(store
, prev
,
1668 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1669 dwFlags
, &subject
->pCertInfo
->Issuer
);
1673 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1674 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1676 PCERT_NAME_BLOB name
;
1680 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1681 name
= &pCertContext
->pCertInfo
->Subject
;
1683 name
= &pCertContext
->pCertInfo
->Issuer
;
1684 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1685 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1688 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1694 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1695 CERT_SIMPLE_NAME_STR
, str
, len
);
1696 for (ptr
= str
; *ptr
; ptr
++)
1697 *ptr
= tolowerW(*ptr
);
1698 if (strstrW(str
, pvPara
))
1706 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1707 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1709 PCCERT_CONTEXT found
= NULL
;
1711 TRACE("%s\n", debugstr_a(pvPara
));
1715 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1716 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1722 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1723 for (ptr
= str
; *ptr
; ptr
++)
1724 *ptr
= tolowerW(*ptr
);
1725 found
= cert_compare_certs_in_store(store
, prev
,
1726 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1731 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1735 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1736 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1738 PCCERT_CONTEXT found
= NULL
;
1740 TRACE("%s\n", debugstr_w(pvPara
));
1744 DWORD len
= strlenW(pvPara
);
1745 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1752 for (src
= pvPara
, dst
= str
; *src
; src
++, dst
++)
1753 *dst
= tolowerW(*src
);
1755 found
= cert_compare_certs_in_store(store
, prev
,
1756 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1761 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1765 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1766 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1767 PCCERT_CONTEXT pPrevCertContext
)
1770 CertFindFunc find
= NULL
;
1771 CertCompareFunc compare
= NULL
;
1773 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1774 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1776 switch (dwType
>> CERT_COMPARE_SHIFT
)
1778 case CERT_COMPARE_ANY
:
1779 find
= find_cert_any
;
1781 case CERT_COMPARE_MD5_HASH
:
1782 compare
= compare_cert_by_md5_hash
;
1784 case CERT_COMPARE_SHA1_HASH
:
1785 compare
= compare_cert_by_sha1_hash
;
1787 case CERT_COMPARE_NAME
:
1788 compare
= compare_cert_by_name
;
1790 case CERT_COMPARE_PUBLIC_KEY
:
1791 compare
= compare_cert_by_public_key
;
1793 case CERT_COMPARE_NAME_STR_A
:
1794 find
= find_cert_by_name_str_a
;
1796 case CERT_COMPARE_NAME_STR_W
:
1797 find
= find_cert_by_name_str_w
;
1799 case CERT_COMPARE_SUBJECT_CERT
:
1800 compare
= compare_cert_by_subject_cert
;
1802 case CERT_COMPARE_CERT_ID
:
1803 compare
= compare_cert_by_cert_id
;
1805 case CERT_COMPARE_ISSUER_OF
:
1806 find
= find_cert_by_issuer
;
1808 case CERT_COMPARE_EXISTING
:
1809 compare
= compare_existing_cert
;
1811 case CERT_COMPARE_SIGNATURE_HASH
:
1812 compare
= compare_cert_by_signature_hash
;
1815 FIXME("find type %08x unimplemented\n", dwType
);
1819 ret
= find(hCertStore
, dwType
, dwFlags
, pvPara
, pPrevCertContext
);
1821 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1822 compare
, dwType
, dwFlags
, pvPara
);
1826 SetLastError(CRYPT_E_NOT_FOUND
);
1827 TRACE("returning %p\n", ret
);
1831 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1832 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1834 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1838 SetLastError(E_INVALIDARG
);
1841 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1842 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1845 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1846 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1848 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1849 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1851 if (*pdwFlags
& ~supportedFlags
)
1853 SetLastError(E_INVALIDARG
);
1856 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1859 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1862 /* FIXME: what if the CRL has expired? */
1865 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1866 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1867 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1870 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1872 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1874 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1875 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1877 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1879 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1880 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1881 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1882 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1887 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1888 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1893 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1894 pPrevIssuerContext
, *pdwFlags
);
1896 if (!pSubjectContext
)
1898 SetLastError(E_INVALIDARG
);
1902 ret
= CertFindCertificateInStore(hCertStore
,
1903 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1904 pSubjectContext
, pPrevIssuerContext
);
1907 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1910 CertFreeCertificateContext(ret
);
1913 if (CRYPT_IsCertificateSelfSigned(pSubjectContext
))
1915 CertFreeCertificateContext(ret
);
1917 SetLastError(CRYPT_E_SELF_SIGNED
);
1920 TRACE("returning %p\n", ret
);
1924 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1929 } OLD_CERT_REVOCATION_STATUS
;
1931 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1932 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
1934 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1935 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1936 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1940 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1941 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1943 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1944 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1946 SetLastError(E_INVALIDARG
);
1951 static HCRYPTOIDFUNCSET set
= NULL
;
1955 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
1956 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
1962 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
1967 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
1971 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
1975 for (ptr
= dllList
; ret
&& *ptr
;
1976 ptr
+= lstrlenW(ptr
) + 1)
1978 CertVerifyRevocationFunc func
;
1979 HCRYPTOIDFUNCADDR hFunc
;
1981 ret
= CryptGetDefaultOIDFunctionAddress(set
,
1982 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
1985 ret
= func(dwEncodingType
, dwRevType
, cContext
,
1986 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1987 CryptFreeOIDFunctionAddress(hFunc
, 0);
1991 CryptMemFree(dllList
);
1995 SetLastError(ERROR_OUTOFMEMORY
);
2006 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
2007 CRYPT_ATTRIBUTE rgAttr
[])
2009 PCRYPT_ATTRIBUTE ret
= NULL
;
2012 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
2018 SetLastError(ERROR_INVALID_PARAMETER
);
2022 for (i
= 0; !ret
&& i
< cAttr
; i
++)
2023 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
2028 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
2029 CERT_EXTENSION rgExtensions
[])
2031 PCERT_EXTENSION ret
= NULL
;
2034 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
2040 SetLastError(ERROR_INVALID_PARAMETER
);
2044 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
2045 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
2046 rgExtensions
[i
].pszObjId
))
2047 ret
= &rgExtensions
[i
];
2051 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
2053 PCERT_RDN_ATTR ret
= NULL
;
2056 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
2060 SetLastError(ERROR_INVALID_PARAMETER
);
2064 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
2065 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
2066 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
2067 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
2068 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
2072 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
2073 const CERT_RDN_ATTR
*attr
)
2078 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
2080 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
2082 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
2084 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
2087 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
2090 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2091 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
2093 if (attr
->Value
.cbData
!=
2094 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2096 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2097 match
= !strncmpiW(nameStr
, attrStr
,
2098 attr
->Value
.cbData
/ sizeof(WCHAR
));
2100 match
= !strncmpW(nameStr
, attrStr
,
2101 attr
->Value
.cbData
/ sizeof(WCHAR
));
2102 TRACE("%s : %s => %d\n",
2103 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2104 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
2110 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
2111 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
2113 if (attr
->Value
.cbData
!=
2114 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
2116 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
2117 match
= !strncasecmp(nameStr
, attrStr
,
2118 attr
->Value
.cbData
);
2120 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
2121 TRACE("%s : %s => %d\n",
2122 debugstr_an(nameStr
, attr
->Value
.cbData
),
2123 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
2131 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
2132 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
2134 CERT_NAME_INFO
*name
;
2139 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
2142 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
2144 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
2145 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
2149 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
2150 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
2152 SetLastError(CRYPT_E_NO_MATCH
);
2158 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
2159 PCERT_INFO pCertInfo
)
2166 GetSystemTimeAsFileTime(&fileTime
);
2167 pTimeToVerify
= &fileTime
;
2169 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
2171 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
2178 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
2179 PCERT_INFO pIssuerInfo
)
2181 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
2183 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
2184 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
2187 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2188 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
2189 DWORD
*pcbComputedHash
)
2192 HCRYPTHASH hHash
= 0;
2194 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2195 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2198 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2203 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2206 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2208 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2209 pcbComputedHash
, 0);
2210 CryptDestroyHash(hHash
);
2216 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2217 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2218 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2221 HCRYPTHASH hHash
= 0;
2223 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2224 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2227 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2230 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2232 SetLastError(ERROR_FILE_NOT_FOUND
);
2240 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2241 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2242 (LPBYTE
)&buf
, &size
);
2245 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2248 ret
= CryptHashData(hHash
, buf
, size
, 0);
2250 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2251 pcbComputedHash
, 0);
2252 CryptDestroyHash(hHash
);
2260 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2261 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2262 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2265 CERT_SIGNED_CONTENT_INFO
*info
;
2268 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv
, dwCertEncodingType
,
2269 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2271 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2272 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2275 PCCRYPT_OID_INFO oidInfo
;
2279 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2280 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2281 info
->SignatureAlgorithm
.pszObjId
, 0);
2284 SetLastError(NTE_BAD_ALGID
);
2289 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2292 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2293 info
->ToBeSigned
.cbData
, 0);
2295 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2296 pcbComputedHash
, 0);
2297 CryptDestroyHash(hHash
);
2305 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2306 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2307 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2308 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2311 PCCRYPT_OID_INFO info
;
2314 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
2315 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2316 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2318 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2319 pSignatureAlgorithm
->pszObjId
, 0);
2322 SetLastError(NTE_BAD_ALGID
);
2325 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2328 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2329 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2332 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2333 cbEncodedToBeSigned
, 0);
2335 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2337 CryptDestroyHash(hHash
);
2344 SetLastError(ERROR_INVALID_PARAMETER
);
2349 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2352 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2353 cbEncodedToBeSigned
, 0);
2355 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2357 CryptDestroyHash(hHash
);
2364 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2365 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2366 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2367 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2370 DWORD encodedSize
, hashSize
;
2372 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2373 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2374 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2376 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2377 NULL
, &encodedSize
);
2380 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2384 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2385 pvStructInfo
, encoded
, &encodedSize
);
2388 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2389 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2390 pvHashAuxInfo
, NULL
, &hashSize
);
2393 PBYTE hash
= CryptMemAlloc(hashSize
);
2397 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2398 dwCertEncodingType
, encoded
, encodedSize
,
2399 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2402 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2404 info
.ToBeSigned
.cbData
= encodedSize
;
2405 info
.ToBeSigned
.pbData
= encoded
;
2406 info
.SignatureAlgorithm
= *pSignatureAlgorithm
;
2407 info
.Signature
.cbData
= hashSize
;
2408 info
.Signature
.pbData
= hash
;
2409 info
.Signature
.cUnusedBits
= 0;
2410 ret
= CryptEncodeObject(dwCertEncodingType
,
2411 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2419 CryptMemFree(encoded
);
2427 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2428 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2429 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2431 CRYPT_DATA_BLOB blob
= { cbEncoded
, (BYTE
*)pbEncoded
};
2433 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2434 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, &blob
,
2435 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2438 static BOOL
CRYPT_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2439 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2443 ALG_ID pubKeyID
, hashID
;
2445 hashID
= info
->u
.Algid
;
2446 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2447 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2450 /* Load the default provider if necessary */
2452 hCryptProv
= I_CryptGetDefaultCryptProv(0);
2453 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2454 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2459 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2462 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2463 signedCert
->ToBeSigned
.cbData
, 0);
2465 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2466 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2467 CryptDestroyHash(hash
);
2469 CryptDestroyKey(key
);
2474 static BOOL
CNG_CalcHash(const WCHAR
*algorithm
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2475 BYTE
**hash_value
, DWORD
*hash_len
)
2477 BCRYPT_HASH_HANDLE hash
= NULL
;
2478 BCRYPT_ALG_HANDLE alg
= NULL
;
2482 if ((status
= BCryptOpenAlgorithmProvider(&alg
, algorithm
, NULL
, 0)))
2485 if ((status
= BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0)))
2488 if ((status
= BCryptHashData(hash
, signedCert
->ToBeSigned
.pbData
, signedCert
->ToBeSigned
.cbData
, 0)))
2491 if ((status
= BCryptGetProperty(hash
, BCRYPT_HASH_LENGTH
, (BYTE
*)hash_len
, sizeof(*hash_len
), &size
, 0)))
2494 if (!(*hash_value
= CryptMemAlloc(*hash_len
)))
2496 status
= STATUS_NO_MEMORY
;
2500 if ((status
= BCryptFinishHash(hash
, *hash_value
, *hash_len
, 0)))
2502 CryptMemFree(*hash_value
);
2507 if (hash
) BCryptDestroyHash(hash
);
2508 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2509 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2513 static BOOL
CNG_ImportECCPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2515 DWORD blob_magic
, ecckey_len
, size
;
2516 BCRYPT_ALG_HANDLE alg
= NULL
;
2517 BCRYPT_ECCKEY_BLOB
*ecckey
;
2518 const WCHAR
*sign_algo
;
2522 if (!pubKeyInfo
->PublicKey
.cbData
)
2524 SetLastError(NTE_BAD_ALGID
);
2528 if (pubKeyInfo
->PublicKey
.pbData
[0] != 0x4)
2530 FIXME("Compressed ECC curves (%02x) not yet supported\n", pubKeyInfo
->PublicKey
.pbData
[0]);
2531 SetLastError(NTE_BAD_ALGID
);
2535 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_OBJECT_IDENTIFIER
, pubKeyInfo
->Algorithm
.Parameters
.pbData
,
2536 pubKeyInfo
->Algorithm
.Parameters
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_curve
, &size
))
2539 if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P256
))
2541 sign_algo
= BCRYPT_ECDSA_P256_ALGORITHM
;
2542 blob_magic
= BCRYPT_ECDSA_PUBLIC_P256_MAGIC
;
2544 else if (!strcmp(*ecc_curve
, szOID_ECC_CURVE_P384
))
2546 sign_algo
= BCRYPT_ECDSA_P384_ALGORITHM
;
2547 blob_magic
= BCRYPT_ECDSA_PUBLIC_P384_MAGIC
;
2551 FIXME("Unsupported ecc curve type: %s\n", *ecc_curve
);
2555 LocalFree(ecc_curve
);
2559 SetLastError(NTE_BAD_ALGID
);
2563 if ((status
= BCryptOpenAlgorithmProvider(&alg
, sign_algo
, NULL
, 0)))
2566 ecckey_len
= sizeof(BCRYPT_ECCKEY_BLOB
) + pubKeyInfo
->PublicKey
.cbData
- 1;
2567 if (!(ecckey
= CryptMemAlloc(ecckey_len
)))
2569 status
= STATUS_NO_MEMORY
;
2573 ecckey
->dwMagic
= blob_magic
;
2574 ecckey
->cbKey
= (pubKeyInfo
->PublicKey
.cbData
- 1) / 2;
2575 memcpy(ecckey
+ 1, pubKeyInfo
->PublicKey
.pbData
+ 1, pubKeyInfo
->PublicKey
.cbData
- 1);
2577 status
= BCryptImportKeyPair(alg
, NULL
, BCRYPT_ECCPUBLIC_BLOB
, key
, (BYTE
*)ecckey
, ecckey_len
, 0);
2578 CryptMemFree(ecckey
);
2581 if (alg
) BCryptCloseAlgorithmProvider(alg
, 0);
2582 if (status
) SetLastError(RtlNtStatusToDosError(status
));
2586 static BOOL
CNG_ImportPubKey(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, BCRYPT_KEY_HANDLE
*key
)
2588 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_ECC_PUBLIC_KEY
))
2589 return CNG_ImportECCPubKey(pubKeyInfo
, key
);
2591 FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo
->Algorithm
.pszObjId
));
2592 SetLastError(NTE_BAD_ALGID
);
2596 static BOOL
CNG_PrepareSignatureECC(BYTE
*encoded_sig
, DWORD encoded_size
, BYTE
**sig_value
, DWORD
*sig_len
)
2598 CERT_ECC_SIGNATURE
*ecc_sig
;
2602 if (!CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ECC_SIGNATURE
, encoded_sig
, encoded_size
,
2603 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &ecc_sig
, &size
))
2606 if (!ecc_sig
->r
.cbData
|| !ecc_sig
->s
.cbData
)
2609 SetLastError(ERROR_INVALID_DATA
);
2613 *sig_len
= ecc_sig
->r
.cbData
+ ecc_sig
->s
.cbData
;
2614 if (!(*sig_value
= CryptMemAlloc(*sig_len
)))
2617 SetLastError(ERROR_OUTOFMEMORY
);
2621 for (i
= 0; i
< ecc_sig
->r
.cbData
; i
++)
2622 (*sig_value
)[i
] = ecc_sig
->r
.pbData
[ecc_sig
->r
.cbData
- i
- 1];
2623 for (i
= 0; i
< ecc_sig
->s
.cbData
; i
++)
2624 (*sig_value
)[ecc_sig
->r
.cbData
+ i
] = ecc_sig
->s
.pbData
[ecc_sig
->s
.cbData
- i
- 1];
2630 static BOOL
CNG_PrepareSignature(CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
,
2631 BYTE
**sig_value
, DWORD
*sig_len
)
2637 if (!signedCert
->Signature
.cbData
)
2639 SetLastError(ERROR_INVALID_DATA
);
2643 if (!(encoded_sig
= CryptMemAlloc(signedCert
->Signature
.cbData
)))
2645 SetLastError(ERROR_OUTOFMEMORY
);
2649 for (i
= 0; i
< signedCert
->Signature
.cbData
; i
++)
2650 encoded_sig
[i
] = signedCert
->Signature
.pbData
[signedCert
->Signature
.cbData
- i
- 1];
2652 if (!strcmp(pubKeyInfo
->Algorithm
.pszObjId
, szOID_ECC_PUBLIC_KEY
))
2653 ret
= CNG_PrepareSignatureECC(encoded_sig
, signedCert
->Signature
.cbData
, sig_value
, sig_len
);
2656 FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo
->Algorithm
.pszObjId
));
2657 SetLastError(NTE_BAD_ALGID
);
2660 CryptMemFree(encoded_sig
);
2664 static BOOL
CNG_VerifySignature(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2665 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
, const CRYPT_OID_INFO
*info
)
2667 BCRYPT_KEY_HANDLE key
= NULL
;
2668 BYTE
*hash_value
= NULL
, *sig_value
;
2669 DWORD hash_len
, sig_len
;
2673 ret
= CNG_ImportPubKey(pubKeyInfo
, &key
);
2676 ret
= CNG_CalcHash(info
->pwszCNGAlgid
, signedCert
, &hash_value
, &hash_len
);
2679 ret
= CNG_PrepareSignature(pubKeyInfo
, signedCert
, &sig_value
, &sig_len
);
2682 status
= BCryptVerifySignature(key
, NULL
, hash_value
, hash_len
, sig_value
, sig_len
, 0);
2685 FIXME("Failed to verify signature: %08x\n", status
);
2686 SetLastError(RtlNtStatusToDosError(status
));
2689 CryptMemFree(sig_value
);
2691 CryptMemFree(hash_value
);
2693 BCryptDestroyKey(key
);
2699 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, DWORD dwCertEncodingType
,
2700 CERT_PUBLIC_KEY_INFO
*pubKeyInfo
, const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2702 CCRYPT_OID_INFO
*info
;
2704 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2705 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2707 SetLastError(NTE_BAD_ALGID
);
2711 if (info
->u
.Algid
== CALG_OID_INFO_CNG_ONLY
)
2712 return CNG_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2714 return CRYPT_VerifySignature(hCryptProv
, dwCertEncodingType
, pubKeyInfo
, signedCert
, info
);
2717 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2718 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2719 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2722 CRYPT_DATA_BLOB subjectBlob
;
2724 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
2725 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2726 dwFlags
, pvReserved
);
2728 switch (dwSubjectType
)
2730 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2732 PCRYPT_DATA_BLOB blob
= pvSubject
;
2734 subjectBlob
.pbData
= blob
->pbData
;
2735 subjectBlob
.cbData
= blob
->cbData
;
2738 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2740 PCERT_CONTEXT context
= pvSubject
;
2742 subjectBlob
.pbData
= context
->pbCertEncoded
;
2743 subjectBlob
.cbData
= context
->cbCertEncoded
;
2746 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2748 PCRL_CONTEXT context
= pvSubject
;
2750 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2751 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2755 SetLastError(E_INVALIDARG
);
2761 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2764 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2765 subjectBlob
.pbData
, subjectBlob
.cbData
,
2766 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2767 &signedCert
, &size
);
2770 switch (dwIssuerType
)
2772 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2773 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2774 dwCertEncodingType
, pvIssuer
,
2777 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2778 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2780 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2783 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2784 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2787 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2790 SetLastError(E_INVALIDARG
);
2795 FIXME("unimplemented for NULL signer\n");
2796 SetLastError(E_INVALIDARG
);
2801 SetLastError(E_INVALIDARG
);
2804 LocalFree(signedCert
);
2810 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2811 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
2813 PCERT_EXTENSION ext
;
2816 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
2819 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
2820 pCertInfo
->rgExtension
);
2823 CRYPT_BIT_BLOB usage
;
2824 DWORD size
= sizeof(usage
);
2826 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
2827 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2831 if (cbKeyUsage
< usage
.cbData
)
2835 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
2836 if (cbKeyUsage
> usage
.cbData
)
2837 memset(pbKeyUsage
+ usage
.cbData
, 0,
2838 cbKeyUsage
- usage
.cbData
);
2847 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
2848 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
2850 PCERT_ENHKEY_USAGE usage
= NULL
;
2854 if (!pCertContext
|| !pcbUsage
)
2856 SetLastError(ERROR_INVALID_PARAMETER
);
2860 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
2862 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
2866 if (CertGetCertificateContextProperty(pCertContext
,
2867 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
2869 LPBYTE buf
= CryptMemAlloc(propSize
);
2873 if (CertGetCertificateContextProperty(pCertContext
,
2874 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
2876 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2877 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
2878 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2884 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
2886 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
2887 pCertContext
->pCertInfo
->cExtension
,
2888 pCertContext
->pCertInfo
->rgExtension
);
2892 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2893 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2894 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2899 /* If a particular location is specified, this should fail. Otherwise
2900 * it should succeed with an empty usage. (This is true on Win2k and
2901 * later, which we emulate.)
2905 SetLastError(CRYPT_E_NOT_FOUND
);
2909 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
2915 *pcbUsage
= bytesNeeded
;
2916 else if (*pcbUsage
< bytesNeeded
)
2918 SetLastError(ERROR_MORE_DATA
);
2919 *pcbUsage
= bytesNeeded
;
2924 *pcbUsage
= bytesNeeded
;
2928 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
2929 sizeof(CERT_ENHKEY_USAGE
) +
2930 usage
->cUsageIdentifier
* sizeof(LPSTR
));
2932 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
2933 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
2934 sizeof(CERT_ENHKEY_USAGE
));
2935 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2937 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2938 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2939 nextOID
+= strlen(nextOID
) + 1;
2943 pUsage
->cUsageIdentifier
= 0;
2948 TRACE("returning %d\n", ret
);
2952 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
2953 PCERT_ENHKEY_USAGE pUsage
)
2957 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
2961 CRYPT_DATA_BLOB blob
= { 0, NULL
};
2963 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
2964 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
2967 ret
= CertSetCertificateContextProperty(pCertContext
,
2968 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
2969 LocalFree(blob
.pbData
);
2973 ret
= CertSetCertificateContextProperty(pCertContext
,
2974 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
2978 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2979 LPCSTR pszUsageIdentifier
)
2984 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2986 if (CertGetEnhancedKeyUsage(pCertContext
,
2987 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
2989 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
2993 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2994 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
2998 BOOL exists
= FALSE
;
3000 /* Make sure usage doesn't already exist */
3001 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
3003 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
3004 pszUsageIdentifier
))
3009 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
3010 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3016 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
3017 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
3018 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
3019 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
3020 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3022 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3023 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
3024 nextOID
+= strlen(nextOID
) + 1;
3026 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
3027 strcpy(nextOID
, pszUsageIdentifier
);
3028 newUsage
->cUsageIdentifier
= i
+ 1;
3029 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
3030 CryptMemFree(newUsage
);
3036 CryptMemFree(usage
);
3043 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
3044 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
3048 usage
->rgpszUsageIdentifier
=
3049 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
3050 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
3051 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
3052 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
3053 usage
->cUsageIdentifier
= 1;
3054 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
3055 CryptMemFree(usage
);
3063 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
3064 LPCSTR pszUsageIdentifier
)
3068 CERT_ENHKEY_USAGE usage
;
3070 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
3072 size
= sizeof(usage
);
3073 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3074 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
3075 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3077 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3081 ret
= CertGetEnhancedKeyUsage(pCertContext
,
3082 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
3085 if (pUsage
->cUsageIdentifier
)
3090 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
3092 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
3093 pszUsageIdentifier
))
3095 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
3096 pUsage
->rgpszUsageIdentifier
[i
] =
3097 pUsage
->rgpszUsageIdentifier
[i
+ 1];
3099 pUsage
->cUsageIdentifier
--;
3100 /* Remove the usage if it's empty */
3101 if (pUsage
->cUsageIdentifier
)
3102 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
3104 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
3107 CryptMemFree(pUsage
);
3114 /* it fit in an empty usage, therefore there's nothing to remove */
3126 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
3128 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
3130 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3132 if (indexIndex
+ 1 > field
->cIndexes
)
3134 if (field
->cIndexes
)
3135 field
->indexes
= CryptMemRealloc(field
->indexes
,
3136 (indexIndex
+ 1) * sizeof(DWORD
));
3138 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
3141 field
->indexes
[indexIndex
] = 0;
3142 field
->cIndexes
= indexIndex
+ 1;
3146 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
3149 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
3152 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
3154 assert(field
->cIndexes
);
3155 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
3159 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
3160 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
3163 DWORD i
, cbOIDs
= 0;
3164 BOOL allUsagesValid
= TRUE
;
3165 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
3167 TRACE("(%d, %p, %d, %p, %d)\n", cCerts
, rghCerts
, *cNumOIDs
,
3170 for (i
= 0; i
< cCerts
; i
++)
3172 CERT_ENHKEY_USAGE usage
;
3173 DWORD size
= sizeof(usage
);
3175 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
3176 /* Success is deliberately ignored: it implies all usages are valid */
3177 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
3179 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
3181 allUsagesValid
= FALSE
;
3184 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
3187 if (!validUsages
.cUsageIdentifier
)
3191 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
3192 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
3193 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3194 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
3196 validUsages
.rgpszUsageIdentifier
=
3197 CryptMemAlloc(cbOIDs
);
3198 if (validUsages
.rgpszUsageIdentifier
)
3200 LPSTR nextOID
= (LPSTR
)
3201 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
3202 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3204 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3206 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
3207 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
3208 pUsage
->rgpszUsageIdentifier
[j
]);
3209 nextOID
+= lstrlenA(nextOID
) + 1;
3215 struct BitField validIndexes
= { 0, NULL
};
3216 DWORD j
, k
, numRemoved
= 0;
3218 /* Merge: build a bitmap of all the indexes of
3219 * validUsages.rgpszUsageIdentifier that are in pUsage.
3221 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
3223 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
3225 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
3226 validUsages
.rgpszUsageIdentifier
[k
]))
3228 CRYPT_SetBitInField(&validIndexes
, k
);
3233 /* Merge by removing from validUsages those that are
3234 * not in the bitmap.
3236 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
3238 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
3240 if (j
< validUsages
.cUsageIdentifier
- 1)
3242 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
3243 &validUsages
.rgpszUsageIdentifier
[j
+
3245 (validUsages
.cUsageIdentifier
- numRemoved
3246 - j
- 1) * sizeof(LPSTR
));
3248 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
3250 validUsages
.cUsageIdentifier
--;
3254 validUsages
.cUsageIdentifier
--;
3257 CryptMemFree(validIndexes
.indexes
);
3260 CryptMemFree(pUsage
);
3272 *cNumOIDs
= validUsages
.cUsageIdentifier
;
3275 else if (*pcbOIDs
< cbOIDs
)
3278 SetLastError(ERROR_MORE_DATA
);
3283 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
3284 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
3287 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
3289 rghOIDs
[i
] = nextOID
;
3290 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
3291 nextOID
+= lstrlenA(nextOID
) + 1;
3295 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
3296 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
3297 TRACE("returning %d\n", ret
);
3301 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
3302 * pInfo is NULL, from the attributes of hProv.
3304 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
3305 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
3307 CRYPT_KEY_PROV_INFO info
= { 0 };
3315 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
3318 LPSTR szContainer
= CryptMemAlloc(size
);
3322 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
3323 (BYTE
*)szContainer
, &size
, 0);
3326 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3330 info
.pwszContainerName
= CryptMemAlloc(len
*
3332 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
3333 info
.pwszContainerName
, len
);
3336 CryptMemFree(szContainer
);
3339 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
3342 LPSTR szProvider
= CryptMemAlloc(size
);
3346 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
3350 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3354 info
.pwszProvName
= CryptMemAlloc(len
*
3356 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
3357 info
.pwszProvName
, len
);
3360 CryptMemFree(szProvider
);
3363 /* in case no CRYPT_KEY_PROV_INFO given,
3364 * we always use AT_SIGNATURE key spec
3366 info
.dwKeySpec
= AT_SIGNATURE
;
3367 size
= sizeof(info
.dwProvType
);
3368 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
3371 info
.dwProvType
= PROV_RSA_FULL
;
3375 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
3380 CryptMemFree(info
.pwszContainerName
);
3381 CryptMemFree(info
.pwszProvName
);
3385 /* Creates a signed certificate context from the unsigned, encoded certificate
3386 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
3388 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
3389 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
3391 PCCERT_CONTEXT context
= NULL
;
3395 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3396 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
3399 LPBYTE sig
= CryptMemAlloc(sigSize
);
3401 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3402 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
3405 CERT_SIGNED_CONTENT_INFO signedInfo
;
3406 BYTE
*encodedSignedCert
= NULL
;
3407 DWORD encodedSignedCertSize
= 0;
3409 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
3410 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
3411 signedInfo
.SignatureAlgorithm
= *sigAlgo
;
3412 signedInfo
.Signature
.cbData
= sigSize
;
3413 signedInfo
.Signature
.pbData
= sig
;
3414 signedInfo
.Signature
.cUnusedBits
= 0;
3415 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
3416 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
3417 &encodedSignedCert
, &encodedSignedCertSize
);
3420 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3421 encodedSignedCert
, encodedSignedCertSize
);
3422 LocalFree(encodedSignedCert
);
3430 /* Copies data from the parameters into info, where:
3431 * pSerialNumber: The serial number. Must not be NULL.
3432 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3434 * pSignatureAlgorithm: Optional.
3435 * pStartTime: The starting time of the certificate. If NULL, the current
3436 * system time is used.
3437 * pEndTime: The ending time of the certificate. If NULL, one year past the
3438 * starting time is used.
3439 * pubKey: The public key of the certificate. Must not be NULL.
3440 * pExtensions: Extensions to be included with the certificate. Optional.
3442 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3443 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3444 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3445 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3446 const CERT_EXTENSIONS
*pExtensions
)
3448 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3451 assert(pSerialNumber
);
3452 assert(pSubjectIssuerBlob
);
3455 if (pExtensions
&& pExtensions
->cExtension
)
3456 info
->dwVersion
= CERT_V3
;
3458 info
->dwVersion
= CERT_V1
;
3459 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3460 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3461 if (pSignatureAlgorithm
)
3462 info
->SignatureAlgorithm
= *pSignatureAlgorithm
;
3465 info
->SignatureAlgorithm
.pszObjId
= oid
;
3466 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3467 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3469 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3470 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3472 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3474 GetSystemTimeAsFileTime(&info
->NotBefore
);
3476 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3481 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3484 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3487 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3488 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3489 info
->SubjectPublicKeyInfo
= *pubKey
;
3492 info
->cExtension
= pExtensions
->cExtension
;
3493 info
->rgExtension
= pExtensions
->rgExtension
;
3497 info
->cExtension
= 0;
3498 info
->rgExtension
= NULL
;
3502 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3503 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3504 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3506 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3508 HCRYPTPROV hProv
= 0;
3509 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
3513 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3515 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3517 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3518 rpcrt
, "RpcStringFreeA");
3520 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3523 RPC_STATUS status
= uuidCreate(&uuid
);
3525 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3527 unsigned char *uuidStr
;
3529 status
= uuidToString(&uuid
, &uuidStr
);
3530 if (status
== RPC_S_OK
)
3532 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3533 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3539 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3541 CryptDestroyKey(key
);
3543 rpcStringFree(&uuidStr
);
3552 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3553 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3554 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3555 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3556 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3558 PCCERT_CONTEXT context
= NULL
;
3559 BOOL ret
, releaseContext
= FALSE
;
3560 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3561 DWORD pubKeySize
= 0, dwKeySpec
;
3563 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
3564 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3565 pExtensions
, pExtensions
);
3567 if(!pSubjectIssuerBlob
)
3569 SetLastError(ERROR_INVALID_PARAMETER
);
3573 dwKeySpec
= pKeyProvInfo
? pKeyProvInfo
->dwKeySpec
: AT_SIGNATURE
;
3578 hProv
= CRYPT_CreateKeyProv();
3579 releaseContext
= TRUE
;
3581 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3583 SetLastError(NTE_BAD_FLAGS
);
3589 /* acquire the context using the given information*/
3590 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3591 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3592 pKeyProvInfo
->dwFlags
);
3595 if(GetLastError() != NTE_BAD_KEYSET
)
3597 /* create the key set */
3598 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3599 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3600 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3604 /* check if the key is here */
3605 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3608 if (NTE_NO_KEY
== GetLastError())
3609 { /* generate the key */
3610 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3614 CryptReleaseContext(hProv
,0);
3615 SetLastError(NTE_BAD_KEYSET
);
3619 CryptDestroyKey(hKey
);
3620 releaseContext
= TRUE
;
3624 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3628 pubKey
= CryptMemAlloc(pubKeySize
);
3631 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3632 pubKey
, &pubKeySize
);
3635 CERT_INFO info
= { 0 };
3636 CRYPT_DER_BLOB blob
= { 0, NULL
};
3638 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3640 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3641 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3642 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3643 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3644 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3648 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3649 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3650 &info
.SignatureAlgorithm
);
3652 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3653 blob
.pbData
, blob
.cbData
);
3654 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3655 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3656 LocalFree(blob
.pbData
);
3659 CryptMemFree(pubKey
);
3663 CryptReleaseContext(hProv
, 0);
3667 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3668 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3669 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3670 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3672 FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType
,
3673 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3674 pVerifyUsageStatus
);
3675 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3679 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3680 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3681 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3683 TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType
, dwEncodingType
,
3684 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3688 FIXME("dwFlags 0x%08x not handled\n", dwFlags
);
3693 FIXME("pCreatePara not handled\n");
3697 switch (dwContextType
)
3699 case CERT_STORE_CERTIFICATE_CONTEXT
:
3700 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3701 case CERT_STORE_CRL_CONTEXT
:
3702 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3703 case CERT_STORE_CTL_CONTEXT
:
3704 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3706 WARN("unknown context type: 0x%x\n", dwContextType
);
3711 BOOL WINAPI
CryptSetKeyIdentifierProperty(const CRYPT_HASH_BLOB
*pKeyIdentifier
, DWORD dwPropId
,
3712 DWORD dwFlags
, LPCWSTR pwszComputerName
, void *pvReserved
, const void *pvData
)
3714 FIXME("(%p, 0x%x, 0x%x, %s, %p, %p): stub\n", pKeyIdentifier
, dwPropId
, dwFlags
,
3715 debugstr_w(pwszComputerName
), pvReserved
, pvData
);