2004-03-01 22:19:37 +01:00
|
|
|
/*
|
2006-02-02 13:19:39 +01:00
|
|
|
* Copyright 2004-2006 Juan Lang
|
2004-03-01 22:19:37 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-08-29 14:18:00 +02:00
|
|
|
*
|
2004-03-01 22:19:37 +01:00
|
|
|
*/
|
2006-02-07 16:31:21 +01:00
|
|
|
|
2005-09-07 15:25:56 +02:00
|
|
|
#include <assert.h>
|
2004-03-01 22:19:37 +01:00
|
|
|
#include <stdarg.h>
|
2006-10-18 21:24:50 +02:00
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
2018-06-26 09:59:37 +02:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2004-03-01 22:19:37 +01:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2018-06-26 09:59:37 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#define CRYPT_OID_INFO_HAS_EXTRA_FIELDS
|
2004-03-01 22:19:37 +01:00
|
|
|
#include "wincrypt.h"
|
2018-10-26 12:17:26 +02:00
|
|
|
#include "snmp.h"
|
2018-06-26 09:59:37 +02:00
|
|
|
#include "bcrypt.h"
|
2006-04-06 02:54:15 +02:00
|
|
|
#include "winnls.h"
|
|
|
|
#include "rpc.h"
|
2004-03-01 22:19:37 +01:00
|
|
|
#include "wine/debug.h"
|
2005-08-25 11:51:59 +02:00
|
|
|
#include "crypt32_private.h"
|
2004-03-01 22:19:37 +01:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
|
|
|
|
|
2006-05-25 00:15:56 +02:00
|
|
|
/* Internal version of CertGetCertificateContextProperty that gets properties
|
|
|
|
* directly from the context (or the context it's linked to, depending on its
|
|
|
|
* type.) Doesn't handle special-case properties, since they are handled by
|
|
|
|
* CertGetCertificateContextProperty, and are particular to the store in which
|
|
|
|
* the property exists (which is separate from the context.)
|
|
|
|
*/
|
2013-10-17 11:07:24 +02:00
|
|
|
static BOOL CertContext_GetProperty(cert_t *cert, DWORD dwPropId,
|
2006-05-25 00:15:56 +02:00
|
|
|
void *pvData, DWORD *pcbData);
|
|
|
|
|
|
|
|
/* Internal version of CertSetCertificateContextProperty that sets properties
|
|
|
|
* directly on the context (or the context it's linked to, depending on its
|
|
|
|
* type.) Doesn't handle special cases, since they're handled by
|
|
|
|
* CertSetCertificateContextProperty anyway.
|
|
|
|
*/
|
2013-10-17 11:07:24 +02:00
|
|
|
static BOOL CertContext_SetProperty(cert_t *cert, DWORD dwPropId,
|
2006-05-25 00:15:56 +02:00
|
|
|
DWORD dwFlags, const void *pvData);
|
|
|
|
|
|
|
|
BOOL WINAPI CertAddEncodedCertificateToStore(HCERTSTORE hCertStore,
|
|
|
|
DWORD dwCertEncodingType, const BYTE *pbCertEncoded, DWORD cbCertEncoded,
|
|
|
|
DWORD dwAddDisposition, PCCERT_CONTEXT *ppCertContext)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT cert = CertCreateCertificateContext(dwCertEncodingType,
|
|
|
|
pbCertEncoded, cbCertEncoded);
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore, dwCertEncodingType,
|
2006-05-25 00:15:56 +02:00
|
|
|
pbCertEncoded, cbCertEncoded, dwAddDisposition, ppCertContext);
|
|
|
|
|
|
|
|
if (cert)
|
|
|
|
{
|
|
|
|
ret = CertAddCertificateContextToStore(hCertStore, cert,
|
|
|
|
dwAddDisposition, ppCertContext);
|
|
|
|
CertFreeCertificateContext(cert);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-03 22:54:15 +01:00
|
|
|
BOOL WINAPI CertAddEncodedCertificateToSystemStoreA(LPCSTR pszCertStoreName,
|
|
|
|
const BYTE *pbCertEncoded, DWORD cbCertEncoded)
|
|
|
|
{
|
|
|
|
HCERTSTORE store;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%s, %p, %d)\n", debugstr_a(pszCertStoreName), pbCertEncoded,
|
|
|
|
cbCertEncoded);
|
|
|
|
|
|
|
|
store = CertOpenSystemStoreA(0, pszCertStoreName);
|
|
|
|
if (store)
|
|
|
|
{
|
|
|
|
ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
|
|
|
|
pbCertEncoded, cbCertEncoded, CERT_STORE_ADD_USE_EXISTING, NULL);
|
|
|
|
CertCloseStore(store, 0);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertAddEncodedCertificateToSystemStoreW(LPCWSTR pszCertStoreName,
|
|
|
|
const BYTE *pbCertEncoded, DWORD cbCertEncoded)
|
|
|
|
{
|
|
|
|
HCERTSTORE store;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%s, %p, %d)\n", debugstr_w(pszCertStoreName), pbCertEncoded,
|
|
|
|
cbCertEncoded);
|
|
|
|
|
|
|
|
store = CertOpenSystemStoreW(0, pszCertStoreName);
|
|
|
|
if (store)
|
|
|
|
{
|
|
|
|
ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
|
|
|
|
pbCertEncoded, cbCertEncoded, CERT_STORE_ADD_USE_EXISTING, NULL);
|
|
|
|
CertCloseStore(store, 0);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
static const context_vtbl_t cert_vtbl;
|
|
|
|
|
2013-10-14 14:47:30 +02:00
|
|
|
static void Cert_free(context_t *context)
|
|
|
|
{
|
|
|
|
cert_t *cert = (cert_t*)context;
|
|
|
|
|
|
|
|
CryptMemFree(cert->ctx.pbCertEncoded);
|
|
|
|
LocalFree(cert->ctx.pCertInfo);
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
static context_t *Cert_clone(context_t *context, WINECRYPT_CERTSTORE *store, BOOL use_link)
|
2013-10-15 16:53:54 +02:00
|
|
|
{
|
|
|
|
cert_t *cert;
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
if(use_link) {
|
2013-10-18 10:50:43 +02:00
|
|
|
cert = (cert_t*)Context_CreateLinkContext(sizeof(CERT_CONTEXT), context, store);
|
2013-10-17 11:06:44 +02:00
|
|
|
if(!cert)
|
|
|
|
return NULL;
|
|
|
|
}else {
|
|
|
|
const cert_t *cloned = (const cert_t*)context;
|
|
|
|
DWORD size = 0;
|
|
|
|
BOOL res;
|
|
|
|
|
2013-10-21 15:58:56 +02:00
|
|
|
cert = (cert_t*)Context_CreateDataContext(sizeof(CERT_CONTEXT), &cert_vtbl, store);
|
|
|
|
if(!cert)
|
2013-10-17 11:06:44 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Context_CopyProperties(&cert->ctx, &cloned->ctx);
|
|
|
|
|
|
|
|
cert->ctx.dwCertEncodingType = cloned->ctx.dwCertEncodingType;
|
|
|
|
cert->ctx.pbCertEncoded = CryptMemAlloc(cloned->ctx.cbCertEncoded);
|
|
|
|
memcpy(cert->ctx.pbCertEncoded, cloned->ctx.pbCertEncoded, cloned->ctx.cbCertEncoded);
|
|
|
|
cert->ctx.cbCertEncoded = cloned->ctx.cbCertEncoded;
|
|
|
|
|
|
|
|
/* FIXME: We don't need to decode the object here, we could just clone cert info. */
|
|
|
|
res = CryptDecodeObjectEx(cert->ctx.dwCertEncodingType, X509_CERT_TO_BE_SIGNED,
|
|
|
|
cert->ctx.pbCertEncoded, cert->ctx.cbCertEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
|
|
|
|
&cert->ctx.pCertInfo, &size);
|
|
|
|
if(!res) {
|
|
|
|
CertFreeCertificateContext(&cert->ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 16:53:54 +02:00
|
|
|
|
|
|
|
cert->ctx.hCertStore = store;
|
|
|
|
return &cert->base;
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:47:30 +02:00
|
|
|
static const context_vtbl_t cert_vtbl = {
|
2013-10-15 16:53:54 +02:00
|
|
|
Cert_free,
|
|
|
|
Cert_clone
|
2013-10-14 14:47:30 +02:00
|
|
|
};
|
|
|
|
|
2013-11-01 10:29:15 +01:00
|
|
|
static BOOL add_cert_to_store(WINECRYPT_CERTSTORE *store, const CERT_CONTEXT *cert,
|
2013-10-17 11:06:44 +02:00
|
|
|
DWORD add_disposition, BOOL use_link, PCCERT_CONTEXT *ret_context)
|
2013-10-15 16:52:57 +02:00
|
|
|
{
|
2013-10-17 11:06:44 +02:00
|
|
|
const CERT_CONTEXT *existing = NULL;
|
|
|
|
BOOL ret = TRUE, inherit_props = FALSE;
|
2013-10-17 11:08:15 +02:00
|
|
|
context_t *new_context = NULL;
|
2013-10-15 16:52:57 +02:00
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
switch (add_disposition)
|
2013-10-15 16:52:57 +02:00
|
|
|
{
|
|
|
|
case CERT_STORE_ADD_ALWAYS:
|
|
|
|
break;
|
|
|
|
case CERT_STORE_ADD_NEW:
|
|
|
|
case CERT_STORE_ADD_REPLACE_EXISTING:
|
|
|
|
case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
|
|
|
|
case CERT_STORE_ADD_USE_EXISTING:
|
|
|
|
case CERT_STORE_ADD_NEWER:
|
|
|
|
case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES:
|
|
|
|
{
|
|
|
|
BYTE hashToAdd[20];
|
|
|
|
DWORD size = sizeof(hashToAdd);
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
ret = CertGetCertificateContextProperty(cert, CERT_HASH_PROP_ID,
|
2013-10-15 16:52:57 +02:00
|
|
|
hashToAdd, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
existing = CertFindCertificateInStore(store, cert->dwCertEncodingType, 0,
|
|
|
|
CERT_FIND_SHA1_HASH, &blob, NULL);
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2013-10-17 11:06:44 +02:00
|
|
|
FIXME("Unimplemented add disposition %d\n", add_disposition);
|
2013-10-15 16:52:57 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2013-10-17 11:06:44 +02:00
|
|
|
return FALSE;
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
switch (add_disposition)
|
2013-10-15 16:52:57 +02:00
|
|
|
{
|
|
|
|
case CERT_STORE_ADD_ALWAYS:
|
|
|
|
break;
|
|
|
|
case CERT_STORE_ADD_NEW:
|
|
|
|
if (existing)
|
|
|
|
{
|
|
|
|
TRACE("found matching certificate, not adding\n");
|
|
|
|
SetLastError(CRYPT_E_EXISTS);
|
2013-10-17 11:06:44 +02:00
|
|
|
return FALSE;
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CERT_STORE_ADD_REPLACE_EXISTING:
|
|
|
|
break;
|
|
|
|
case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
|
2013-10-17 11:06:44 +02:00
|
|
|
if (use_link)
|
|
|
|
FIXME("CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES: semi-stub for links\n");
|
2013-10-15 16:52:57 +02:00
|
|
|
if (existing)
|
2013-10-17 11:06:44 +02:00
|
|
|
inherit_props = TRUE;
|
2013-10-15 16:52:57 +02:00
|
|
|
break;
|
|
|
|
case CERT_STORE_ADD_USE_EXISTING:
|
2013-10-17 11:06:44 +02:00
|
|
|
if(use_link)
|
|
|
|
FIXME("CERT_STORE_ADD_USE_EXISTING: semi-stub for links\n");
|
2013-10-15 16:52:57 +02:00
|
|
|
if (existing)
|
|
|
|
{
|
2013-10-17 11:06:44 +02:00
|
|
|
Context_CopyProperties(existing, cert);
|
|
|
|
if (ret_context)
|
|
|
|
*ret_context = CertDuplicateCertificateContext(existing);
|
|
|
|
return TRUE;
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CERT_STORE_ADD_NEWER:
|
2013-10-17 11:06:44 +02:00
|
|
|
if (existing && CompareFileTime(&existing->pCertInfo->NotBefore, &cert->pCertInfo->NotBefore) >= 0)
|
2013-10-15 16:52:57 +02:00
|
|
|
{
|
2013-10-17 11:06:44 +02:00
|
|
|
TRACE("existing certificate is newer, not adding\n");
|
|
|
|
SetLastError(CRYPT_E_EXISTS);
|
|
|
|
return FALSE;
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES:
|
|
|
|
if (existing)
|
|
|
|
{
|
2013-10-17 11:06:44 +02:00
|
|
|
if (CompareFileTime(&existing->pCertInfo->NotBefore, &cert->pCertInfo->NotBefore) >= 0)
|
2013-10-15 16:52:57 +02:00
|
|
|
{
|
|
|
|
TRACE("existing certificate is newer, not adding\n");
|
|
|
|
SetLastError(CRYPT_E_EXISTS);
|
2013-10-17 11:06:44 +02:00
|
|
|
return FALSE;
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
2013-10-17 11:06:44 +02:00
|
|
|
inherit_props = TRUE;
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
/* FIXME: We have tests that this works, but what should we really do in this case? */
|
|
|
|
if(!store) {
|
|
|
|
if(ret_context)
|
|
|
|
*ret_context = CertDuplicateCertificateContext(cert);
|
|
|
|
return TRUE;
|
2013-10-15 16:52:57 +02:00
|
|
|
}
|
2013-10-17 11:06:44 +02:00
|
|
|
|
2013-10-17 11:08:15 +02:00
|
|
|
ret = store->vtbl->certs.addContext(store, context_from_ptr(cert), existing ? context_from_ptr(existing) : NULL,
|
|
|
|
(ret_context || inherit_props) ? &new_context : NULL, use_link);
|
2013-10-17 11:06:44 +02:00
|
|
|
if(!ret)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if(inherit_props)
|
2013-10-17 11:08:15 +02:00
|
|
|
Context_CopyProperties(context_ptr(new_context), existing);
|
2013-10-17 11:06:44 +02:00
|
|
|
|
2013-10-18 10:50:43 +02:00
|
|
|
if(ret_context)
|
2013-10-17 11:08:15 +02:00
|
|
|
*ret_context = context_ptr(new_context);
|
2013-10-18 10:50:43 +02:00
|
|
|
else if(new_context)
|
2013-10-17 11:08:15 +02:00
|
|
|
Context_Release(new_context);
|
2013-10-15 16:52:57 +02:00
|
|
|
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:06:44 +02:00
|
|
|
BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore, PCCERT_CONTEXT pCertContext,
|
|
|
|
DWORD dwAddDisposition, PCCERT_CONTEXT *ppStoreContext)
|
|
|
|
{
|
|
|
|
WINECRYPT_CERTSTORE *store = hCertStore;
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext, dwAddDisposition, ppStoreContext);
|
|
|
|
|
|
|
|
return add_cert_to_store(store, pCertContext, dwAddDisposition, FALSE, ppStoreContext);
|
|
|
|
}
|
|
|
|
|
2009-11-03 01:16:46 +01:00
|
|
|
BOOL WINAPI CertAddCertificateLinkToStore(HCERTSTORE hCertStore,
|
|
|
|
PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
|
|
|
|
PCCERT_CONTEXT *ppCertContext)
|
|
|
|
{
|
2010-05-15 00:48:18 +02:00
|
|
|
static int calls;
|
2013-09-04 12:38:51 +02:00
|
|
|
WINECRYPT_CERTSTORE *store = (WINECRYPT_CERTSTORE*)hCertStore;
|
2010-05-15 00:44:17 +02:00
|
|
|
|
2010-05-15 00:48:18 +02:00
|
|
|
if (!(calls++))
|
|
|
|
FIXME("(%p, %p, %08x, %p): semi-stub\n", hCertStore, pCertContext,
|
|
|
|
dwAddDisposition, ppCertContext);
|
2010-05-15 00:44:17 +02:00
|
|
|
if (store->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
|
|
|
|
return FALSE;
|
|
|
|
if (store->type == StoreTypeCollection)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-10-17 11:06:44 +02:00
|
|
|
return add_cert_to_store(hCertStore, pCertContext, dwAddDisposition, TRUE, ppCertContext);
|
2009-11-03 01:16:46 +01:00
|
|
|
}
|
|
|
|
|
2006-05-25 00:15:56 +02:00
|
|
|
PCCERT_CONTEXT WINAPI CertCreateCertificateContext(DWORD dwCertEncodingType,
|
|
|
|
const BYTE *pbCertEncoded, DWORD cbCertEncoded)
|
|
|
|
{
|
2013-10-21 15:58:56 +02:00
|
|
|
cert_t *cert = NULL;
|
|
|
|
BYTE *data = NULL;
|
2006-05-25 00:15:56 +02:00
|
|
|
BOOL ret;
|
|
|
|
PCERT_INFO certInfo = NULL;
|
|
|
|
DWORD size = 0;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08x, %p, %d)\n", dwCertEncodingType, pbCertEncoded,
|
2006-05-25 00:15:56 +02:00
|
|
|
cbCertEncoded);
|
|
|
|
|
2011-09-22 14:20:50 +02:00
|
|
|
if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-06-19 23:10:29 +02:00
|
|
|
ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT_TO_BE_SIGNED,
|
|
|
|
pbCertEncoded, cbCertEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL,
|
2009-01-26 11:01:47 +01:00
|
|
|
&certInfo, &size);
|
2013-10-21 15:58:56 +02:00
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
2006-05-25 00:15:56 +02:00
|
|
|
|
2013-10-21 15:58:56 +02:00
|
|
|
cert = (cert_t*)Context_CreateDataContext(sizeof(CERT_CONTEXT), &cert_vtbl, &empty_store);
|
|
|
|
if (!cert)
|
|
|
|
return NULL;
|
|
|
|
data = CryptMemAlloc(cbCertEncoded);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
Context_Release(&cert->base);
|
|
|
|
return NULL;
|
2006-05-25 00:15:56 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 15:58:56 +02:00
|
|
|
memcpy(data, pbCertEncoded, cbCertEncoded);
|
|
|
|
cert->ctx.dwCertEncodingType = dwCertEncodingType;
|
|
|
|
cert->ctx.pbCertEncoded = data;
|
|
|
|
cert->ctx.cbCertEncoded = cbCertEncoded;
|
|
|
|
cert->ctx.pCertInfo = certInfo;
|
|
|
|
cert->ctx.hCertStore = &empty_store;
|
|
|
|
|
|
|
|
return &cert->ctx;
|
2006-05-25 00:15:56 +02:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:47:07 +02:00
|
|
|
PCCERT_CONTEXT WINAPI CertDuplicateCertificateContext(PCCERT_CONTEXT pCertContext)
|
2006-05-25 00:15:56 +02:00
|
|
|
{
|
|
|
|
TRACE("(%p)\n", pCertContext);
|
2009-01-29 13:32:59 +01:00
|
|
|
|
|
|
|
if (!pCertContext)
|
|
|
|
return NULL;
|
|
|
|
|
2013-10-14 14:47:07 +02:00
|
|
|
Context_AddRef(&cert_from_ptr(pCertContext)->base);
|
2006-05-25 00:15:56 +02:00
|
|
|
return pCertContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertFreeCertificateContext(PCCERT_CONTEXT pCertContext)
|
|
|
|
{
|
|
|
|
TRACE("(%p)\n", pCertContext);
|
|
|
|
|
|
|
|
if (pCertContext)
|
2013-10-18 10:50:03 +02:00
|
|
|
Context_Release(&cert_from_ptr(pCertContext)->base);
|
|
|
|
return TRUE;
|
2006-05-25 00:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext,
|
|
|
|
DWORD dwPropId)
|
|
|
|
{
|
2013-10-17 11:07:24 +02:00
|
|
|
cert_t *cert = cert_from_ptr(pCertContext);
|
2006-05-25 00:15:56 +02:00
|
|
|
DWORD ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %d)\n", pCertContext, dwPropId);
|
2006-05-25 00:15:56 +02:00
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
if (cert->base.properties)
|
|
|
|
ret = ContextPropertyList_EnumPropIDs(cert->base.properties, dwPropId);
|
2006-05-25 00:15:56 +02:00
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
static BOOL CertContext_GetHashProp(cert_t *cert, DWORD dwPropId,
|
2006-05-25 00:15:56 +02:00
|
|
|
ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
|
|
|
|
DWORD *pcbData)
|
|
|
|
{
|
|
|
|
BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
|
|
|
|
pcbData);
|
2007-05-14 00:25:04 +02:00
|
|
|
if (ret && pvData)
|
2006-05-25 00:15:56 +02:00
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB blob = { *pcbData, pvData };
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_SetProperty(cert, dwPropId, 0, &blob);
|
2006-05-25 00:15:56 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-02 21:21:10 +02:00
|
|
|
static BOOL CertContext_CopyParam(void *pvData, DWORD *pcbData, const void *pb,
|
|
|
|
DWORD cb)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
if (!pvData)
|
|
|
|
*pcbData = cb;
|
|
|
|
else if (*pcbData < cb)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
*pcbData = cb;
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(pvData, pb, cb);
|
|
|
|
*pcbData = cb;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:20:11 +02:00
|
|
|
void CRYPT_ConvertKeyContext(const struct store_CERT_KEY_CONTEXT *src, CERT_KEY_CONTEXT *dst)
|
|
|
|
{
|
|
|
|
dst->cbSize = sizeof(*dst);
|
|
|
|
dst->hCryptProv = src->hCryptProv;
|
|
|
|
dst->dwKeySpec = src->dwKeySpec;
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:40:45 +01:00
|
|
|
/*
|
|
|
|
* Fix offsets in a continuous block of memory of CRYPT_KEY_PROV_INFO with
|
|
|
|
* its associated data.
|
|
|
|
*/
|
|
|
|
static void fix_KeyProvInfoProperty(CRYPT_KEY_PROV_INFO *info)
|
|
|
|
{
|
|
|
|
BYTE *data;
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
data = (BYTE *)(info + 1) + sizeof(CRYPT_KEY_PROV_PARAM) * info->cProvParam;
|
|
|
|
|
|
|
|
if (info->pwszContainerName)
|
|
|
|
{
|
|
|
|
info->pwszContainerName = (LPWSTR)data;
|
|
|
|
data += (lstrlenW(info->pwszContainerName) + 1) * sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->pwszProvName)
|
|
|
|
{
|
|
|
|
info->pwszProvName = (LPWSTR)data;
|
|
|
|
data += (lstrlenW(info->pwszProvName) + 1) * sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
info->rgProvParam = info->cProvParam ? (CRYPT_KEY_PROV_PARAM *)(info + 1) : NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < info->cProvParam; i++)
|
|
|
|
{
|
|
|
|
info->rgProvParam[i].pbData = info->rgProvParam[i].cbData ? data : NULL;
|
|
|
|
data += info->rgProvParam[i].cbData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy to a continuous block of memory of CRYPT_KEY_PROV_INFO with
|
|
|
|
* its associated data.
|
|
|
|
*/
|
|
|
|
static void copy_KeyProvInfoProperty(const CRYPT_KEY_PROV_INFO *from, CRYPT_KEY_PROV_INFO *to)
|
|
|
|
{
|
|
|
|
BYTE *data;
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
data = (BYTE *)(to + 1) + sizeof(CRYPT_KEY_PROV_PARAM) * from->cProvParam;
|
|
|
|
|
|
|
|
if (from->pwszContainerName)
|
|
|
|
{
|
|
|
|
to->pwszContainerName = (LPWSTR)data;
|
|
|
|
lstrcpyW((LPWSTR)data, from->pwszContainerName);
|
|
|
|
data += (lstrlenW(from->pwszContainerName) + 1) * sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
to->pwszContainerName = NULL;
|
|
|
|
|
|
|
|
if (from->pwszProvName)
|
|
|
|
{
|
|
|
|
to->pwszProvName = (LPWSTR)data;
|
|
|
|
lstrcpyW((LPWSTR)data, from->pwszProvName);
|
|
|
|
data += (lstrlenW(from->pwszProvName) + 1) * sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
to->pwszProvName = NULL;
|
|
|
|
|
|
|
|
to->dwProvType = from->dwProvType;
|
|
|
|
to->dwFlags = from->dwFlags;
|
|
|
|
to->cProvParam = from->cProvParam;
|
|
|
|
to->rgProvParam = from->cProvParam ? (CRYPT_KEY_PROV_PARAM *)(to + 1) : NULL;
|
|
|
|
to->dwKeySpec = from->dwKeySpec;
|
|
|
|
|
|
|
|
for (i = 0; i < from->cProvParam; i++)
|
|
|
|
{
|
|
|
|
to->rgProvParam[i].dwParam = from->rgProvParam[i].dwParam;
|
|
|
|
to->rgProvParam[i].dwFlags = from->rgProvParam[i].dwFlags;
|
|
|
|
to->rgProvParam[i].cbData = from->rgProvParam[i].cbData;
|
|
|
|
to->rgProvParam[i].pbData = from->rgProvParam[i].cbData ? data : NULL;
|
|
|
|
memcpy(data, from->rgProvParam[i].pbData, from->rgProvParam[i].cbData);
|
|
|
|
data += from->rgProvParam[i].cbData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
static BOOL CertContext_GetProperty(cert_t *cert, DWORD dwPropId,
|
2006-05-25 00:15:56 +02:00
|
|
|
void *pvData, DWORD *pcbData)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
CRYPT_DATA_BLOB blob;
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
TRACE("(%p, %d, %p, %p)\n", cert, dwPropId, pvData, pcbData);
|
2006-05-25 00:15:56 +02:00
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
if (cert->base.properties)
|
|
|
|
ret = ContextPropertyList_FindProperty(cert->base.properties, dwPropId, &blob);
|
2006-05-25 00:15:56 +02:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
if (ret)
|
2020-09-10 15:20:11 +02:00
|
|
|
{
|
|
|
|
CERT_KEY_CONTEXT ctx;
|
|
|
|
if (dwPropId == CERT_KEY_CONTEXT_PROP_ID)
|
|
|
|
{
|
|
|
|
CRYPT_ConvertKeyContext((const struct store_CERT_KEY_CONTEXT *)blob.pbData, &ctx);
|
|
|
|
blob.pbData = (BYTE *)&ctx;
|
|
|
|
blob.cbData = ctx.cbSize;
|
|
|
|
}
|
2007-08-02 21:21:10 +02:00
|
|
|
ret = CertContext_CopyParam(pvData, pcbData, blob.pbData, blob.cbData);
|
2020-09-10 15:20:11 +02:00
|
|
|
}
|
2006-05-25 00:15:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Implicit properties */
|
|
|
|
switch (dwPropId)
|
|
|
|
{
|
|
|
|
case CERT_SHA1_HASH_PROP_ID:
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetHashProp(cert, dwPropId, CALG_SHA1,
|
|
|
|
cert->ctx.pbCertEncoded, cert->ctx.cbCertEncoded, pvData,
|
2006-05-25 00:15:56 +02:00
|
|
|
pcbData);
|
|
|
|
break;
|
|
|
|
case CERT_MD5_HASH_PROP_ID:
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetHashProp(cert, dwPropId, CALG_MD5,
|
|
|
|
cert->ctx.pbCertEncoded, cert->ctx.cbCertEncoded, pvData,
|
2006-05-25 00:15:56 +02:00
|
|
|
pcbData);
|
|
|
|
break;
|
|
|
|
case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetHashProp(cert, dwPropId, CALG_MD5,
|
|
|
|
cert->ctx.pCertInfo->Subject.pbData,
|
|
|
|
cert->ctx.pCertInfo->Subject.cbData,
|
2006-05-25 00:15:56 +02:00
|
|
|
pvData, pcbData);
|
|
|
|
break;
|
|
|
|
case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetHashProp(cert, dwPropId, CALG_MD5,
|
|
|
|
cert->ctx.pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
|
|
|
|
cert->ctx.pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData,
|
2006-05-25 00:15:56 +02:00
|
|
|
pvData, pcbData);
|
|
|
|
break;
|
|
|
|
case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID:
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetHashProp(cert, dwPropId, CALG_MD5,
|
|
|
|
cert->ctx.pCertInfo->SerialNumber.pbData,
|
|
|
|
cert->ctx.pCertInfo->SerialNumber.cbData,
|
2006-05-25 00:15:56 +02:00
|
|
|
pvData, pcbData);
|
|
|
|
break;
|
|
|
|
case CERT_SIGNATURE_HASH_PROP_ID:
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CryptHashToBeSigned(0, cert->ctx.dwCertEncodingType,
|
|
|
|
cert->ctx.pbCertEncoded, cert->ctx.cbCertEncoded, pvData,
|
2008-08-08 19:36:45 +02:00
|
|
|
pcbData);
|
|
|
|
if (ret && pvData)
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB blob = { *pcbData, pvData };
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_SetProperty(cert, dwPropId, 0, &blob);
|
2008-08-08 19:36:45 +02:00
|
|
|
}
|
2006-05-25 00:15:56 +02:00
|
|
|
break;
|
2007-08-02 21:29:49 +02:00
|
|
|
case CERT_KEY_IDENTIFIER_PROP_ID:
|
|
|
|
{
|
|
|
|
PCERT_EXTENSION ext = CertFindExtension(
|
2013-10-17 11:07:24 +02:00
|
|
|
szOID_SUBJECT_KEY_IDENTIFIER, cert->ctx.pCertInfo->cExtension,
|
|
|
|
cert->ctx.pCertInfo->rgExtension);
|
2007-08-02 21:29:49 +02:00
|
|
|
|
|
|
|
if (ext)
|
|
|
|
{
|
2007-08-13 23:31:02 +02:00
|
|
|
CRYPT_DATA_BLOB value;
|
2007-11-01 15:57:09 +01:00
|
|
|
DWORD size = sizeof(value);
|
2007-08-02 21:29:49 +02:00
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
|
|
|
|
szOID_SUBJECT_KEY_IDENTIFIER, ext->Value.pbData,
|
2007-08-13 23:31:02 +02:00
|
|
|
ext->Value.cbData, CRYPT_DECODE_NOCOPY_FLAG, NULL, &value,
|
2007-08-02 21:29:49 +02:00
|
|
|
&size);
|
|
|
|
if (ret)
|
|
|
|
{
|
2007-08-13 23:31:02 +02:00
|
|
|
ret = CertContext_CopyParam(pvData, pcbData, value.pbData,
|
|
|
|
value.cbData);
|
2013-10-17 11:07:24 +02:00
|
|
|
CertContext_SetProperty(cert, dwPropId, 0, &value);
|
2007-08-02 21:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(ERROR_INVALID_DATA);
|
|
|
|
break;
|
|
|
|
}
|
2006-05-25 00:15:56 +02:00
|
|
|
default:
|
|
|
|
SetLastError(CRYPT_E_NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
|
|
|
|
DWORD dwPropId, void *pvData, DWORD *pcbData)
|
|
|
|
{
|
2013-10-17 11:07:24 +02:00
|
|
|
cert_t *cert = cert_from_ptr(pCertContext);
|
2006-05-25 00:15:56 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %d, %p, %p)\n", pCertContext, dwPropId, pvData, pcbData);
|
2006-05-25 00:15:56 +02:00
|
|
|
|
|
|
|
switch (dwPropId)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case CERT_CERT_PROP_ID:
|
|
|
|
case CERT_CRL_PROP_ID:
|
|
|
|
case CERT_CTL_PROP_ID:
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
case CERT_ACCESS_STATE_PROP_ID:
|
2013-10-21 15:59:11 +02:00
|
|
|
ret = CertGetStoreProperty(cert->ctx.hCertStore, dwPropId, pvData, pcbData);
|
2006-05-25 00:15:56 +02:00
|
|
|
break;
|
2006-07-10 18:10:36 +02:00
|
|
|
case CERT_KEY_PROV_HANDLE_PROP_ID:
|
|
|
|
{
|
|
|
|
CERT_KEY_CONTEXT keyContext;
|
|
|
|
DWORD size = sizeof(keyContext);
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetProperty(cert,
|
2006-07-10 18:10:36 +02:00
|
|
|
CERT_KEY_CONTEXT_PROP_ID, &keyContext, &size);
|
|
|
|
if (ret)
|
2007-08-02 21:21:10 +02:00
|
|
|
ret = CertContext_CopyParam(pvData, pcbData, &keyContext.hCryptProv,
|
|
|
|
sizeof(keyContext.hCryptProv));
|
2006-07-10 18:10:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2006-05-25 00:15:56 +02:00
|
|
|
case CERT_KEY_PROV_INFO_PROP_ID:
|
2020-12-03 17:40:45 +01:00
|
|
|
ret = CertContext_GetProperty(cert, dwPropId, pvData, pcbData);
|
2006-05-25 00:15:56 +02:00
|
|
|
if (ret && pvData)
|
2020-12-03 17:40:45 +01:00
|
|
|
fix_KeyProvInfoProperty(pvData);
|
2006-05-25 00:15:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetProperty(cert, dwPropId, pvData,
|
2006-05-25 00:15:56 +02:00
|
|
|
pcbData);
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:40:45 +01:00
|
|
|
/*
|
|
|
|
* Create a continuous block of memory for CRYPT_KEY_PROV_INFO with
|
|
|
|
* its associated data, and add it to the certificate properties.
|
2006-05-25 00:15:56 +02:00
|
|
|
*/
|
2020-12-03 17:40:45 +01:00
|
|
|
static BOOL CertContext_SetKeyProvInfoProperty(CONTEXT_PROPERTY_LIST *properties, const CRYPT_KEY_PROV_INFO *info)
|
2006-05-25 00:15:56 +02:00
|
|
|
{
|
2020-12-03 17:40:45 +01:00
|
|
|
CRYPT_KEY_PROV_INFO *prop;
|
|
|
|
DWORD size = sizeof(CRYPT_KEY_PROV_INFO), i;
|
2006-05-25 00:15:56 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-05 03:13:49 +02:00
|
|
|
if (info->pwszContainerName)
|
2020-06-09 04:32:37 +02:00
|
|
|
size += (lstrlenW(info->pwszContainerName) + 1) * sizeof(WCHAR);
|
2006-10-05 03:13:49 +02:00
|
|
|
if (info->pwszProvName)
|
2020-06-09 04:32:37 +02:00
|
|
|
size += (lstrlenW(info->pwszProvName) + 1) * sizeof(WCHAR);
|
|
|
|
|
2006-05-25 00:15:56 +02:00
|
|
|
for (i = 0; i < info->cProvParam; i++)
|
2020-12-03 17:40:45 +01:00
|
|
|
size += sizeof(CRYPT_KEY_PROV_PARAM) + info->rgProvParam[i].cbData;
|
2020-06-09 04:32:37 +02:00
|
|
|
|
2020-12-03 17:40:45 +01:00
|
|
|
prop = HeapAlloc(GetProcessHeap(), 0, size);
|
|
|
|
if (!prop)
|
2006-05-25 00:15:56 +02:00
|
|
|
{
|
2020-12-03 17:40:45 +01:00
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
2006-05-25 00:15:56 +02:00
|
|
|
}
|
2020-12-03 17:40:45 +01:00
|
|
|
|
|
|
|
copy_KeyProvInfoProperty(info, prop);
|
|
|
|
|
|
|
|
ret = ContextPropertyList_SetProperty(properties, CERT_KEY_PROV_INFO_PROP_ID, (const BYTE *)prop, size);
|
|
|
|
HeapFree(GetProcessHeap(), 0, prop);
|
|
|
|
|
2006-05-25 00:15:56 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:20:11 +02:00
|
|
|
static BOOL CertContext_SetKeyContextProperty(CONTEXT_PROPERTY_LIST *properties,
|
|
|
|
const CERT_KEY_CONTEXT *keyContext)
|
|
|
|
{
|
|
|
|
struct store_CERT_KEY_CONTEXT ctx;
|
|
|
|
|
|
|
|
ctx.cbSize = sizeof(ctx);
|
|
|
|
ctx.hCryptProv = keyContext->hCryptProv;
|
|
|
|
ctx.dwKeySpec = keyContext->dwKeySpec;
|
|
|
|
|
|
|
|
return ContextPropertyList_SetProperty(properties, CERT_KEY_CONTEXT_PROP_ID,
|
|
|
|
(const BYTE *)&ctx, ctx.cbSize);
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
static BOOL CertContext_SetProperty(cert_t *cert, DWORD dwPropId,
|
2006-05-25 00:15:56 +02:00
|
|
|
DWORD dwFlags, const void *pvData)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
TRACE("(%p, %d, %08x, %p)\n", cert, dwPropId, dwFlags, pvData);
|
2006-05-25 00:15:56 +02:00
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
if (!cert->base.properties)
|
2006-05-25 00:15:56 +02:00
|
|
|
ret = FALSE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (dwPropId)
|
|
|
|
{
|
|
|
|
case CERT_AUTO_ENROLL_PROP_ID:
|
|
|
|
case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
|
|
|
|
case CERT_DESCRIPTION_PROP_ID:
|
|
|
|
case CERT_FRIENDLY_NAME_PROP_ID:
|
|
|
|
case CERT_HASH_PROP_ID:
|
|
|
|
case CERT_KEY_IDENTIFIER_PROP_ID:
|
|
|
|
case CERT_MD5_HASH_PROP_ID:
|
|
|
|
case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
|
|
|
|
case CERT_PUBKEY_ALG_PARA_PROP_ID:
|
|
|
|
case CERT_PVK_FILE_PROP_ID:
|
|
|
|
case CERT_SIGNATURE_HASH_PROP_ID:
|
|
|
|
case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
|
|
|
|
case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
|
2008-09-18 01:15:56 +02:00
|
|
|
case CERT_EXTENDED_ERROR_INFO_PROP_ID:
|
2006-05-25 00:15:56 +02:00
|
|
|
case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
|
|
|
|
case CERT_ENROLLMENT_PROP_ID:
|
|
|
|
case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
|
2015-08-26 07:36:29 +02:00
|
|
|
case CERT_OCSP_RESPONSE_PROP_ID:
|
2006-05-25 00:15:56 +02:00
|
|
|
case CERT_RENEWAL_PROP_ID:
|
|
|
|
{
|
2006-07-10 18:10:36 +02:00
|
|
|
if (pvData)
|
|
|
|
{
|
2008-12-09 23:46:55 +01:00
|
|
|
const CRYPT_DATA_BLOB *blob = pvData;
|
2006-07-10 18:10:36 +02:00
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = ContextPropertyList_SetProperty(cert->base.properties, dwPropId,
|
2006-07-10 18:10:36 +02:00
|
|
|
blob->pbData, blob->cbData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-17 11:07:24 +02:00
|
|
|
ContextPropertyList_RemoveProperty(cert->base.properties, dwPropId);
|
2006-07-10 18:10:36 +02:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
2006-05-25 00:15:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CERT_DATE_STAMP_PROP_ID:
|
2006-07-10 18:10:36 +02:00
|
|
|
if (pvData)
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = ContextPropertyList_SetProperty(cert->base.properties, dwPropId,
|
2008-12-09 23:46:55 +01:00
|
|
|
pvData, sizeof(FILETIME));
|
2006-07-10 18:10:36 +02:00
|
|
|
else
|
|
|
|
{
|
2013-10-17 11:07:24 +02:00
|
|
|
ContextPropertyList_RemoveProperty(cert->base.properties, dwPropId);
|
2006-07-10 18:10:36 +02:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CERT_KEY_CONTEXT_PROP_ID:
|
|
|
|
if (pvData)
|
|
|
|
{
|
2008-12-09 23:46:55 +01:00
|
|
|
const CERT_KEY_CONTEXT *keyContext = pvData;
|
2006-07-10 18:10:36 +02:00
|
|
|
|
2007-08-02 21:21:46 +02:00
|
|
|
if (keyContext->cbSize != sizeof(CERT_KEY_CONTEXT))
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
2020-09-10 15:20:11 +02:00
|
|
|
ret = CertContext_SetKeyContextProperty(cert->base.properties, pvData);
|
2006-07-10 18:10:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-17 11:07:24 +02:00
|
|
|
ContextPropertyList_RemoveProperty(cert->base.properties, dwPropId);
|
2006-07-10 18:10:36 +02:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
2006-05-25 00:15:56 +02:00
|
|
|
break;
|
|
|
|
case CERT_KEY_PROV_INFO_PROP_ID:
|
2006-07-10 18:10:36 +02:00
|
|
|
if (pvData)
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_SetKeyProvInfoProperty(cert->base.properties, pvData);
|
2006-07-10 18:10:36 +02:00
|
|
|
else
|
|
|
|
{
|
2013-10-17 11:07:24 +02:00
|
|
|
ContextPropertyList_RemoveProperty(cert->base.properties, dwPropId);
|
2006-07-10 18:10:36 +02:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CERT_KEY_PROV_HANDLE_PROP_ID:
|
|
|
|
{
|
|
|
|
CERT_KEY_CONTEXT keyContext;
|
|
|
|
DWORD size = sizeof(keyContext);
|
|
|
|
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_GetProperty(cert, CERT_KEY_CONTEXT_PROP_ID,
|
2006-07-10 18:10:36 +02:00
|
|
|
&keyContext, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
|
|
|
|
CryptReleaseContext(keyContext.hCryptProv, 0);
|
|
|
|
}
|
2008-12-10 00:00:44 +01:00
|
|
|
keyContext.cbSize = sizeof(keyContext);
|
|
|
|
if (pvData)
|
|
|
|
keyContext.hCryptProv = *(const HCRYPTPROV *)pvData;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keyContext.hCryptProv = 0;
|
|
|
|
keyContext.dwKeySpec = AT_SIGNATURE;
|
|
|
|
}
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_SetProperty(cert, CERT_KEY_CONTEXT_PROP_ID,
|
2008-12-10 00:00:44 +01:00
|
|
|
0, &keyContext);
|
2006-05-25 00:15:56 +02:00
|
|
|
break;
|
2006-07-10 18:10:36 +02:00
|
|
|
}
|
2006-05-25 00:15:56 +02:00
|
|
|
default:
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("%d: stub\n", dwPropId);
|
2006-05-25 00:15:56 +02:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
|
|
|
|
DWORD dwPropId, DWORD dwFlags, const void *pvData)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %d, %08x, %p)\n", pCertContext, dwPropId, dwFlags, pvData);
|
2006-05-25 00:15:56 +02:00
|
|
|
|
|
|
|
/* Handle special cases for "read-only"/invalid prop IDs. Windows just
|
|
|
|
* crashes on most of these, I'll be safer.
|
|
|
|
*/
|
|
|
|
switch (dwPropId)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case CERT_ACCESS_STATE_PROP_ID:
|
|
|
|
case CERT_CERT_PROP_ID:
|
|
|
|
case CERT_CRL_PROP_ID:
|
|
|
|
case CERT_CTL_PROP_ID:
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-10-17 11:07:24 +02:00
|
|
|
ret = CertContext_SetProperty(cert_from_ptr(pCertContext), dwPropId, dwFlags,
|
2006-05-25 00:15:56 +02:00
|
|
|
pvData);
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-11 15:01:48 +02:00
|
|
|
/* Acquires the private key using the key provider info, retrieving info from
|
|
|
|
* the certificate if info is NULL. The acquired provider is returned in
|
|
|
|
* *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
|
|
|
|
*/
|
2019-01-31 06:24:21 +01:00
|
|
|
static BOOL CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert, DWORD dwFlags,
|
2006-07-11 15:01:48 +02:00
|
|
|
PCRYPT_KEY_PROV_INFO info, HCRYPTPROV *phCryptProv, DWORD *pdwKeySpec)
|
|
|
|
{
|
|
|
|
DWORD size = 0;
|
|
|
|
BOOL allocated = FALSE, ret = TRUE;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
{
|
|
|
|
ret = CertGetCertificateContextProperty(pCert,
|
|
|
|
CERT_KEY_PROV_INFO_PROP_ID, 0, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
2008-11-03 22:35:50 +01:00
|
|
|
info = HeapAlloc(GetProcessHeap(), 0, size);
|
2006-07-11 15:01:48 +02:00
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
ret = CertGetCertificateContextProperty(pCert,
|
|
|
|
CERT_KEY_PROV_INFO_PROP_ID, info, &size);
|
|
|
|
allocated = TRUE;
|
2008-09-09 19:22:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
ret = FALSE;
|
2006-07-11 15:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_NO_KEY_PROPERTY);
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptAcquireContextW(phCryptProv, info->pwszContainerName,
|
2019-01-31 06:24:21 +01:00
|
|
|
info->pwszProvName, info->dwProvType, (dwFlags & CRYPT_ACQUIRE_SILENT_FLAG) ? CRYPT_SILENT : 0);
|
2006-07-11 15:01:48 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < info->cProvParam; i++)
|
|
|
|
{
|
|
|
|
CryptSetProvParam(*phCryptProv,
|
|
|
|
info->rgProvParam[i].dwParam, info->rgProvParam[i].pbData,
|
|
|
|
info->rgProvParam[i].dwFlags);
|
|
|
|
}
|
|
|
|
*pdwKeySpec = info->dwKeySpec;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(CRYPT_E_NO_KEY_PROPERTY);
|
|
|
|
}
|
|
|
|
if (allocated)
|
|
|
|
HeapFree(GetProcessHeap(), 0, info);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert,
|
2007-08-03 00:44:26 +02:00
|
|
|
DWORD dwFlags, void *pvReserved, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE *phCryptProv,
|
|
|
|
DWORD *pdwKeySpec, BOOL *pfCallerFreeProv)
|
2006-07-11 15:01:48 +02:00
|
|
|
{
|
|
|
|
BOOL ret = FALSE, cache = FALSE;
|
|
|
|
PCRYPT_KEY_PROV_INFO info = NULL;
|
|
|
|
CERT_KEY_CONTEXT keyContext;
|
|
|
|
DWORD size;
|
2019-02-06 08:25:49 +01:00
|
|
|
PCCERT_CONTEXT cert_in_store = NULL;
|
2006-07-11 15:01:48 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert, dwFlags, pvReserved,
|
2006-07-11 15:01:48 +02:00
|
|
|
phCryptProv, pdwKeySpec, pfCallerFreeProv);
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_ACQUIRE_USE_PROV_INFO_FLAG)
|
|
|
|
{
|
|
|
|
DWORD size = 0;
|
|
|
|
|
|
|
|
ret = CertGetCertificateContextProperty(pCert,
|
|
|
|
CERT_KEY_PROV_INFO_PROP_ID, 0, &size);
|
2019-02-06 08:25:49 +01:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
HCERTSTORE hstore;
|
|
|
|
|
|
|
|
hstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
|
2020-11-09 11:41:52 +01:00
|
|
|
CERT_SYSTEM_STORE_CURRENT_USER, L"My");
|
2019-02-06 08:25:49 +01:00
|
|
|
if (hstore)
|
|
|
|
{
|
|
|
|
cert_in_store = CertFindCertificateInStore(hstore, pCert->dwCertEncodingType, 0,
|
|
|
|
CERT_FIND_EXISTING, pCert, NULL);
|
|
|
|
if (cert_in_store)
|
|
|
|
{
|
|
|
|
ret = CertGetCertificateContextProperty(cert_in_store, CERT_KEY_PROV_INFO_PROP_ID, 0, &size);
|
|
|
|
if (ret)
|
|
|
|
pCert = cert_in_store;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CertFreeCertificateContext(cert_in_store);
|
|
|
|
cert_in_store = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CertCloseStore(hstore, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-11 15:01:48 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2008-11-03 22:35:50 +01:00
|
|
|
info = HeapAlloc(GetProcessHeap(), 0, size);
|
2006-07-11 15:01:48 +02:00
|
|
|
ret = CertGetCertificateContextProperty(pCert,
|
|
|
|
CERT_KEY_PROV_INFO_PROP_ID, info, &size);
|
|
|
|
if (ret)
|
|
|
|
cache = info->dwFlags & CERT_SET_KEY_CONTEXT_PROP_ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dwFlags & CRYPT_ACQUIRE_CACHE_FLAG)
|
|
|
|
cache = TRUE;
|
|
|
|
*phCryptProv = 0;
|
|
|
|
if (cache)
|
|
|
|
{
|
|
|
|
size = sizeof(keyContext);
|
|
|
|
ret = CertGetCertificateContextProperty(pCert, CERT_KEY_CONTEXT_PROP_ID,
|
|
|
|
&keyContext, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
*phCryptProv = keyContext.hCryptProv;
|
|
|
|
if (pdwKeySpec)
|
|
|
|
*pdwKeySpec = keyContext.dwKeySpec;
|
|
|
|
if (pfCallerFreeProv)
|
2019-01-31 06:24:25 +01:00
|
|
|
*pfCallerFreeProv = FALSE;
|
2006-07-11 15:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*phCryptProv)
|
|
|
|
{
|
2019-01-31 06:24:21 +01:00
|
|
|
ret = CRYPT_AcquirePrivateKeyFromProvInfo(pCert, dwFlags, info,
|
2006-07-11 15:01:48 +02:00
|
|
|
&keyContext.hCryptProv, &keyContext.dwKeySpec);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
*phCryptProv = keyContext.hCryptProv;
|
|
|
|
if (pdwKeySpec)
|
|
|
|
*pdwKeySpec = keyContext.dwKeySpec;
|
|
|
|
if (cache)
|
|
|
|
{
|
|
|
|
keyContext.cbSize = sizeof(keyContext);
|
|
|
|
if (CertSetCertificateContextProperty(pCert,
|
|
|
|
CERT_KEY_CONTEXT_PROP_ID, 0, &keyContext))
|
|
|
|
{
|
|
|
|
if (pfCallerFreeProv)
|
|
|
|
*pfCallerFreeProv = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pfCallerFreeProv)
|
|
|
|
*pfCallerFreeProv = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HeapFree(GetProcessHeap(), 0, info);
|
2019-02-06 08:25:49 +01:00
|
|
|
if (cert_in_store)
|
|
|
|
CertFreeCertificateContext(cert_in_store);
|
2006-07-11 15:01:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-27 18:01:06 +01:00
|
|
|
static BOOL key_prov_info_matches_cert(PCCERT_CONTEXT pCert,
|
|
|
|
const CRYPT_KEY_PROV_INFO *keyProvInfo)
|
|
|
|
{
|
|
|
|
HCRYPTPROV csp;
|
|
|
|
BOOL matches = FALSE;
|
|
|
|
|
|
|
|
if (CryptAcquireContextW(&csp, keyProvInfo->pwszContainerName,
|
|
|
|
keyProvInfo->pwszProvName, keyProvInfo->dwProvType, keyProvInfo->dwFlags))
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
/* Need to sign something to verify the sig. What to sign? Why not
|
|
|
|
* the certificate itself?
|
|
|
|
*/
|
|
|
|
if (CryptSignAndEncodeCertificate(csp, AT_SIGNATURE,
|
|
|
|
pCert->dwCertEncodingType, X509_CERT_TO_BE_SIGNED, pCert->pCertInfo,
|
|
|
|
&pCert->pCertInfo->SignatureAlgorithm, NULL, NULL, &size))
|
|
|
|
{
|
|
|
|
BYTE *certEncoded = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (certEncoded)
|
|
|
|
{
|
|
|
|
if (CryptSignAndEncodeCertificate(csp, AT_SIGNATURE,
|
|
|
|
pCert->dwCertEncodingType, X509_CERT_TO_BE_SIGNED,
|
|
|
|
pCert->pCertInfo, &pCert->pCertInfo->SignatureAlgorithm,
|
|
|
|
NULL, certEncoded, &size))
|
|
|
|
{
|
|
|
|
if (size == pCert->cbCertEncoded &&
|
|
|
|
!memcmp(certEncoded, pCert->pbCertEncoded, size))
|
|
|
|
matches = TRUE;
|
|
|
|
}
|
|
|
|
CryptMemFree(certEncoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptReleaseContext(csp, 0);
|
|
|
|
}
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL container_matches_cert(PCCERT_CONTEXT pCert, LPCSTR container,
|
|
|
|
CRYPT_KEY_PROV_INFO *keyProvInfo)
|
|
|
|
{
|
|
|
|
CRYPT_KEY_PROV_INFO copy;
|
|
|
|
WCHAR containerW[MAX_PATH];
|
2014-11-01 02:44:29 +01:00
|
|
|
BOOL matches;
|
2009-01-27 18:01:06 +01:00
|
|
|
|
2018-06-20 09:37:55 +02:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, container, -1, containerW, ARRAY_SIZE(containerW));
|
2009-01-27 18:01:06 +01:00
|
|
|
/* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
|
|
|
|
* keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
|
|
|
|
* name.
|
|
|
|
*/
|
2013-02-05 13:22:27 +01:00
|
|
|
copy = *keyProvInfo;
|
2009-01-27 18:01:06 +01:00
|
|
|
copy.pwszContainerName = containerW;
|
|
|
|
matches = key_prov_info_matches_cert(pCert, ©);
|
|
|
|
if (matches)
|
|
|
|
{
|
|
|
|
keyProvInfo->pwszContainerName =
|
2020-11-09 11:41:21 +01:00
|
|
|
CryptMemAlloc((lstrlenW(containerW) + 1) * sizeof(WCHAR));
|
2009-01-27 18:01:06 +01:00
|
|
|
if (keyProvInfo->pwszContainerName)
|
|
|
|
{
|
2020-11-09 11:41:21 +01:00
|
|
|
lstrcpyW(keyProvInfo->pwszContainerName, containerW);
|
2009-01-27 18:01:06 +01:00
|
|
|
keyProvInfo->dwKeySpec = AT_SIGNATURE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
matches = FALSE;
|
|
|
|
}
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Searches the provider named keyProvInfo.pwszProvName for a container whose
|
|
|
|
* private key matches pCert's public key. Upon success, updates keyProvInfo
|
|
|
|
* with the matching container's info (free keyProvInfo.pwszContainerName upon
|
|
|
|
* success.)
|
|
|
|
* Returns TRUE if found, FALSE if not.
|
|
|
|
*/
|
|
|
|
static BOOL find_key_prov_info_in_provider(PCCERT_CONTEXT pCert,
|
|
|
|
CRYPT_KEY_PROV_INFO *keyProvInfo)
|
|
|
|
{
|
|
|
|
HCRYPTPROV defProvider;
|
|
|
|
BOOL ret, found = FALSE;
|
|
|
|
char containerA[MAX_PATH];
|
|
|
|
|
|
|
|
assert(keyProvInfo->pwszContainerName == NULL);
|
|
|
|
if ((ret = CryptAcquireContextW(&defProvider, NULL,
|
|
|
|
keyProvInfo->pwszProvName, keyProvInfo->dwProvType,
|
|
|
|
keyProvInfo->dwFlags | CRYPT_VERIFYCONTEXT)))
|
|
|
|
{
|
|
|
|
DWORD enumFlags = keyProvInfo->dwFlags | CRYPT_FIRST;
|
|
|
|
|
|
|
|
while (ret && !found)
|
|
|
|
{
|
|
|
|
DWORD size = sizeof(containerA);
|
|
|
|
|
|
|
|
ret = CryptGetProvParam(defProvider, PP_ENUMCONTAINERS,
|
|
|
|
(BYTE *)containerA, &size, enumFlags);
|
|
|
|
if (ret)
|
|
|
|
found = container_matches_cert(pCert, containerA, keyProvInfo);
|
|
|
|
if (enumFlags & CRYPT_FIRST)
|
|
|
|
{
|
|
|
|
enumFlags &= ~CRYPT_FIRST;
|
|
|
|
enumFlags |= CRYPT_NEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptReleaseContext(defProvider, 0);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL find_matching_provider(PCCERT_CONTEXT pCert, DWORD dwFlags)
|
|
|
|
{
|
|
|
|
BOOL found = FALSE, ret = TRUE;
|
|
|
|
DWORD index = 0, cbProvName = 0;
|
|
|
|
CRYPT_KEY_PROV_INFO keyProvInfo;
|
|
|
|
|
|
|
|
TRACE("(%p, %08x)\n", pCert, dwFlags);
|
|
|
|
|
|
|
|
memset(&keyProvInfo, 0, sizeof(keyProvInfo));
|
|
|
|
while (ret && !found)
|
|
|
|
{
|
|
|
|
DWORD size = 0;
|
|
|
|
|
|
|
|
ret = CryptEnumProvidersW(index, NULL, 0, &keyProvInfo.dwProvType,
|
|
|
|
NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (size <= cbProvName)
|
|
|
|
ret = CryptEnumProvidersW(index, NULL, 0,
|
|
|
|
&keyProvInfo.dwProvType, keyProvInfo.pwszProvName, &size);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CryptMemFree(keyProvInfo.pwszProvName);
|
|
|
|
keyProvInfo.pwszProvName = CryptMemAlloc(size);
|
|
|
|
if (keyProvInfo.pwszProvName)
|
|
|
|
{
|
|
|
|
cbProvName = size;
|
|
|
|
ret = CryptEnumProvidersW(index, NULL, 0,
|
|
|
|
&keyProvInfo.dwProvType, keyProvInfo.pwszProvName, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_FIND_SILENT_KEYSET_FLAG)
|
|
|
|
keyProvInfo.dwFlags |= CRYPT_SILENT;
|
|
|
|
if (dwFlags & CRYPT_FIND_USER_KEYSET_FLAG ||
|
|
|
|
!(dwFlags & (CRYPT_FIND_USER_KEYSET_FLAG |
|
|
|
|
CRYPT_FIND_MACHINE_KEYSET_FLAG)))
|
|
|
|
{
|
|
|
|
keyProvInfo.dwFlags |= CRYPT_USER_KEYSET;
|
|
|
|
found = find_key_prov_info_in_provider(pCert,
|
|
|
|
&keyProvInfo);
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (dwFlags & CRYPT_FIND_MACHINE_KEYSET_FLAG ||
|
|
|
|
!(dwFlags & (CRYPT_FIND_USER_KEYSET_FLAG |
|
|
|
|
CRYPT_FIND_MACHINE_KEYSET_FLAG)))
|
|
|
|
{
|
|
|
|
keyProvInfo.dwFlags &= ~CRYPT_USER_KEYSET;
|
|
|
|
keyProvInfo.dwFlags |= CRYPT_MACHINE_KEYSET;
|
|
|
|
found = find_key_prov_info_in_provider(pCert,
|
|
|
|
&keyProvInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
|
|
|
CertSetCertificateContextProperty(pCert, CERT_KEY_PROV_INFO_PROP_ID,
|
|
|
|
0, &keyProvInfo);
|
|
|
|
CryptMemFree(keyProvInfo.pwszProvName);
|
|
|
|
CryptMemFree(keyProvInfo.pwszContainerName);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL cert_prov_info_matches_cert(PCCERT_CONTEXT pCert)
|
|
|
|
{
|
|
|
|
BOOL matches = FALSE;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if (CertGetCertificateContextProperty(pCert, CERT_KEY_PROV_INFO_PROP_ID,
|
|
|
|
NULL, &size))
|
|
|
|
{
|
|
|
|
CRYPT_KEY_PROV_INFO *keyProvInfo = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (keyProvInfo)
|
|
|
|
{
|
|
|
|
if (CertGetCertificateContextProperty(pCert,
|
|
|
|
CERT_KEY_PROV_INFO_PROP_ID, keyProvInfo, &size))
|
|
|
|
matches = key_prov_info_matches_cert(pCert, keyProvInfo);
|
|
|
|
CryptMemFree(keyProvInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert,
|
|
|
|
DWORD dwFlags, void *pvReserved)
|
|
|
|
{
|
2014-11-01 02:44:29 +01:00
|
|
|
BOOL matches;
|
2009-01-27 18:01:06 +01:00
|
|
|
|
|
|
|
TRACE("(%p, %08x, %p)\n", pCert, dwFlags, pvReserved);
|
|
|
|
|
|
|
|
matches = cert_prov_info_matches_cert(pCert);
|
|
|
|
if (!matches)
|
|
|
|
matches = find_matching_provider(pCert, dwFlags);
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
2006-05-16 19:39:29 +02:00
|
|
|
BOOL WINAPI CertCompareCertificate(DWORD dwCertEncodingType,
|
|
|
|
PCERT_INFO pCertId1, PCERT_INFO pCertId2)
|
|
|
|
{
|
2007-08-09 22:19:33 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08x, %p, %p)\n", dwCertEncodingType, pCertId1, pCertId2);
|
2006-05-16 19:39:29 +02:00
|
|
|
|
2007-08-09 22:19:33 +02:00
|
|
|
ret = CertCompareCertificateName(dwCertEncodingType, &pCertId1->Issuer,
|
2006-05-16 19:39:29 +02:00
|
|
|
&pCertId2->Issuer) && CertCompareIntegerBlob(&pCertId1->SerialNumber,
|
|
|
|
&pCertId2->SerialNumber);
|
2007-08-09 22:19:33 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
2006-05-16 19:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertCompareCertificateName(DWORD dwCertEncodingType,
|
|
|
|
PCERT_NAME_BLOB pCertName1, PCERT_NAME_BLOB pCertName2)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08x, %p, %p)\n", dwCertEncodingType, pCertName1, pCertName2);
|
2006-05-16 19:39:29 +02:00
|
|
|
|
|
|
|
if (pCertName1->cbData == pCertName2->cbData)
|
|
|
|
{
|
|
|
|
if (pCertName1->cbData)
|
|
|
|
ret = !memcmp(pCertName1->pbData, pCertName2->pbData,
|
|
|
|
pCertName1->cbData);
|
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
2007-08-09 22:19:33 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
2006-05-16 19:39:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the number of significant bytes in pInt, where a byte is
|
|
|
|
* insignificant if it's a leading 0 for positive numbers or a leading 0xff
|
|
|
|
* for negative numbers. pInt is assumed to be little-endian.
|
|
|
|
*/
|
2007-04-09 21:28:22 +02:00
|
|
|
static DWORD CRYPT_significantBytes(const CRYPT_INTEGER_BLOB *pInt)
|
2006-05-16 19:39:29 +02:00
|
|
|
{
|
|
|
|
DWORD ret = pInt->cbData;
|
|
|
|
|
|
|
|
while (ret > 1)
|
|
|
|
{
|
|
|
|
if (pInt->pbData[ret - 2] <= 0x7f && pInt->pbData[ret - 1] == 0)
|
|
|
|
ret--;
|
|
|
|
else if (pInt->pbData[ret - 2] >= 0x80 && pInt->pbData[ret - 1] == 0xff)
|
|
|
|
ret--;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1,
|
|
|
|
PCRYPT_INTEGER_BLOB pInt2)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD cb1, cb2;
|
|
|
|
|
|
|
|
TRACE("(%p, %p)\n", pInt1, pInt2);
|
|
|
|
|
|
|
|
cb1 = CRYPT_significantBytes(pInt1);
|
|
|
|
cb2 = CRYPT_significantBytes(pInt2);
|
|
|
|
if (cb1 == cb2)
|
2006-05-25 17:51:37 +02:00
|
|
|
{
|
|
|
|
if (cb1)
|
2007-08-07 18:19:41 +02:00
|
|
|
ret = !memcmp(pInt1->pbData, pInt2->pbData, cb1);
|
2006-05-25 17:51:37 +02:00
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2006-05-16 19:39:29 +02:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
2007-08-09 22:19:33 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
2006-05-16 19:39:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertComparePublicKeyInfo(DWORD dwCertEncodingType,
|
|
|
|
PCERT_PUBLIC_KEY_INFO pPublicKey1, PCERT_PUBLIC_KEY_INFO pPublicKey2)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08x, %p, %p)\n", dwCertEncodingType, pPublicKey1, pPublicKey2);
|
2006-05-16 19:39:29 +02:00
|
|
|
|
2018-10-26 12:17:26 +02:00
|
|
|
/* RSA public key data should start with ASN_SEQUENCE,
|
|
|
|
* otherwise it's not a RSA_CSP_PUBLICKEYBLOB.
|
|
|
|
*/
|
|
|
|
if (!pPublicKey1->PublicKey.cbData || pPublicKey1->PublicKey.pbData[0] != ASN_SEQUENCE)
|
|
|
|
dwCertEncodingType = 0;
|
|
|
|
|
2008-06-29 12:33:48 +02:00
|
|
|
switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType))
|
2006-05-16 19:39:29 +02:00
|
|
|
{
|
2008-06-29 12:33:48 +02:00
|
|
|
case 0: /* Seems to mean "raw binary bits" */
|
|
|
|
if (pPublicKey1->PublicKey.cbData == pPublicKey2->PublicKey.cbData &&
|
|
|
|
pPublicKey1->PublicKey.cUnusedBits == pPublicKey2->PublicKey.cUnusedBits)
|
|
|
|
{
|
|
|
|
if (pPublicKey2->PublicKey.cbData)
|
|
|
|
ret = !memcmp(pPublicKey1->PublicKey.pbData,
|
|
|
|
pPublicKey2->PublicKey.pbData, pPublicKey1->PublicKey.cbData);
|
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
2006-05-16 19:39:29 +02:00
|
|
|
else
|
2008-06-29 12:33:48 +02:00
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN("Unknown encoding type %08x\n", dwCertEncodingType);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case X509_ASN_ENCODING:
|
|
|
|
{
|
|
|
|
BLOBHEADER *pblob1, *pblob2;
|
|
|
|
DWORD length;
|
2006-05-16 19:39:29 +02:00
|
|
|
ret = FALSE;
|
2008-06-29 12:33:48 +02:00
|
|
|
if (CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
|
|
|
|
pPublicKey1->PublicKey.pbData, pPublicKey1->PublicKey.cbData,
|
2018-10-25 13:50:35 +02:00
|
|
|
CRYPT_DECODE_ALLOC_FLAG, &pblob1, &length))
|
2008-06-29 12:33:48 +02:00
|
|
|
{
|
|
|
|
if (CryptDecodeObject(dwCertEncodingType, RSA_CSP_PUBLICKEYBLOB,
|
2018-10-25 13:50:35 +02:00
|
|
|
pPublicKey2->PublicKey.pbData, pPublicKey2->PublicKey.cbData,
|
|
|
|
CRYPT_DECODE_ALLOC_FLAG, &pblob2, &length))
|
2008-06-29 12:33:48 +02:00
|
|
|
{
|
2018-10-25 13:50:35 +02:00
|
|
|
/* The RSAPUBKEY structure directly follows the BLOBHEADER */
|
|
|
|
RSAPUBKEY *pk1 = (LPVOID)(pblob1 + 1),
|
|
|
|
*pk2 = (LPVOID)(pblob2 + 1);
|
|
|
|
ret = (pk1->bitlen == pk2->bitlen) && (pk1->pubexp == pk2->pubexp)
|
|
|
|
&& !memcmp(pk1 + 1, pk2 + 1, pk1->bitlen/8);
|
|
|
|
|
|
|
|
LocalFree(pblob2);
|
2008-06-29 12:33:48 +02:00
|
|
|
}
|
2018-10-25 13:50:35 +02:00
|
|
|
LocalFree(pblob1);
|
2008-06-29 12:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-05-16 19:39:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-09-28 08:07:26 +02:00
|
|
|
DWORD WINAPI CertGetPublicKeyLength(DWORD dwCertEncodingType,
|
|
|
|
PCERT_PUBLIC_KEY_INFO pPublicKey)
|
|
|
|
{
|
|
|
|
DWORD len = 0;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08x, %p)\n", dwCertEncodingType, pPublicKey);
|
2006-09-28 08:07:26 +02:00
|
|
|
|
2008-05-25 19:23:44 +02:00
|
|
|
if (GET_CERT_ENCODING_TYPE(dwCertEncodingType) != X509_ASN_ENCODING)
|
2006-09-28 08:07:26 +02:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_FILE_NOT_FOUND);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (pPublicKey->Algorithm.pszObjId &&
|
|
|
|
!strcmp(pPublicKey->Algorithm.pszObjId, szOID_RSA_DH))
|
|
|
|
{
|
|
|
|
FIXME("unimplemented for DH public keys\n");
|
|
|
|
SetLastError(CRYPT_E_ASN1_BADTAG);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-02 15:07:03 +01:00
|
|
|
PCCRYPT_OID_INFO info;
|
2006-09-28 08:07:26 +02:00
|
|
|
DWORD size;
|
|
|
|
PBYTE buf;
|
2018-11-02 15:07:03 +01:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, pPublicKey->Algorithm.pszObjId, 0);
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
HCRYPTKEY key;
|
|
|
|
|
|
|
|
TRACE("public key algid %#x (%s)\n", info->u.Algid, debugstr_a(pPublicKey->Algorithm.pszObjId));
|
|
|
|
|
2018-11-19 09:06:18 +01:00
|
|
|
ret = CryptImportPublicKeyInfo(I_CryptGetDefaultCryptProv(info->u.Algid), dwCertEncodingType, pPublicKey, &key);
|
2018-11-02 15:07:03 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
size = sizeof(len);
|
|
|
|
ret = CryptGetKeyParam(key, KP_KEYLEN, (BYTE *)&len, &size, 0);
|
|
|
|
CryptDestroyKey(key);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
/* fallback to RSA */
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(dwCertEncodingType,
|
2006-09-28 08:07:26 +02:00
|
|
|
RSA_CSP_PUBLICKEYBLOB, pPublicKey->PublicKey.pbData,
|
|
|
|
pPublicKey->PublicKey.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &buf,
|
|
|
|
&size);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2007-12-30 16:29:38 +01:00
|
|
|
RSAPUBKEY *rsaPubKey = (RSAPUBKEY *)(buf + sizeof(BLOBHEADER));
|
2006-09-28 08:07:26 +02:00
|
|
|
|
|
|
|
len = rsaPubKey->bitlen;
|
|
|
|
LocalFree(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-05-18 16:34:03 +02:00
|
|
|
typedef BOOL (*CertCompareFunc)(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara);
|
|
|
|
|
|
|
|
static BOOL compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
BYTE hash[16];
|
|
|
|
DWORD size = sizeof(hash);
|
|
|
|
|
|
|
|
ret = CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_MD5_HASH_PROP_ID, hash, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_HASH_BLOB *pHash = pvPara;
|
2006-05-18 16:34:03 +02:00
|
|
|
|
|
|
|
if (size == pHash->cbData)
|
|
|
|
ret = !memcmp(pHash->pbData, hash, size);
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
BYTE hash[20];
|
|
|
|
DWORD size = sizeof(hash);
|
|
|
|
|
|
|
|
ret = CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_SHA1_HASH_PROP_ID, hash, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
const CRYPT_HASH_BLOB *pHash = pvPara;
|
2006-05-18 16:34:03 +02:00
|
|
|
|
|
|
|
if (size == pHash->cbData)
|
|
|
|
ret = !memcmp(pHash->pbData, hash, size);
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL compare_cert_by_name(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
2006-05-25 17:51:37 +02:00
|
|
|
CERT_NAME_BLOB *blob = (CERT_NAME_BLOB *)pvPara, *toCompare;
|
2006-05-18 16:34:03 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (dwType & CERT_INFO_SUBJECT_FLAG)
|
|
|
|
toCompare = &pCertContext->pCertInfo->Subject;
|
|
|
|
else
|
|
|
|
toCompare = &pCertContext->pCertInfo->Issuer;
|
2006-05-25 17:51:37 +02:00
|
|
|
ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
|
|
|
|
toCompare, blob);
|
2006-05-18 16:34:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-27 18:00:40 +01:00
|
|
|
static BOOL compare_cert_by_public_key(PCCERT_CONTEXT pCertContext,
|
|
|
|
DWORD dwType, DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
|
|
|
CERT_PUBLIC_KEY_INFO *publicKey = (CERT_PUBLIC_KEY_INFO *)pvPara;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ret = CertComparePublicKeyInfo(pCertContext->dwCertEncodingType,
|
|
|
|
&pCertContext->pCertInfo->SubjectPublicKeyInfo, publicKey);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-05-18 16:34:03 +02:00
|
|
|
static BOOL compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext,
|
|
|
|
DWORD dwType, DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
2006-05-25 17:51:37 +02:00
|
|
|
CERT_INFO *pCertInfo = (CERT_INFO *)pvPara;
|
2007-08-07 22:15:00 +02:00
|
|
|
BOOL ret;
|
2006-05-18 16:34:03 +02:00
|
|
|
|
2007-08-09 22:18:51 +02:00
|
|
|
/* Matching serial number and subject match.. */
|
2007-08-07 22:15:00 +02:00
|
|
|
ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
|
2009-10-15 20:52:25 +02:00
|
|
|
&pCertContext->pCertInfo->Subject, &pCertInfo->Issuer);
|
2007-08-09 22:18:51 +02:00
|
|
|
if (ret)
|
2007-08-07 22:15:00 +02:00
|
|
|
ret = CertCompareIntegerBlob(&pCertContext->pCertInfo->SerialNumber,
|
|
|
|
&pCertInfo->SerialNumber);
|
2007-08-09 22:18:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* failing that, if the serial number and issuer match, we match */
|
|
|
|
ret = CertCompareIntegerBlob(&pCertContext->pCertInfo->SerialNumber,
|
|
|
|
&pCertInfo->SerialNumber);
|
|
|
|
if (ret)
|
|
|
|
ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
|
2009-10-15 20:52:25 +02:00
|
|
|
&pCertContext->pCertInfo->Issuer, &pCertInfo->Issuer);
|
2007-08-09 22:18:51 +02:00
|
|
|
}
|
2007-08-07 22:15:00 +02:00
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
2006-05-18 16:34:03 +02:00
|
|
|
}
|
|
|
|
|
2007-08-02 21:30:19 +02:00
|
|
|
static BOOL compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
|
|
|
CERT_ID *id = (CERT_ID *)pvPara;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
switch (id->dwIdChoice)
|
|
|
|
{
|
|
|
|
case CERT_ID_ISSUER_SERIAL_NUMBER:
|
|
|
|
ret = CertCompareCertificateName(pCertContext->dwCertEncodingType,
|
|
|
|
&pCertContext->pCertInfo->Issuer, &id->u.IssuerSerialNumber.Issuer);
|
|
|
|
if (ret)
|
|
|
|
ret = CertCompareIntegerBlob(&pCertContext->pCertInfo->SerialNumber,
|
|
|
|
&id->u.IssuerSerialNumber.SerialNumber);
|
|
|
|
break;
|
|
|
|
case CERT_ID_SHA1_HASH:
|
|
|
|
ret = compare_cert_by_sha1_hash(pCertContext, dwType, dwFlags,
|
|
|
|
&id->u.HashId);
|
|
|
|
break;
|
|
|
|
case CERT_ID_KEY_IDENTIFIER:
|
|
|
|
{
|
|
|
|
DWORD size = 0;
|
|
|
|
|
|
|
|
ret = CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_KEY_IDENTIFIER_PROP_ID, NULL, &size);
|
|
|
|
if (ret && size == id->u.KeyId.cbData)
|
|
|
|
{
|
|
|
|
LPBYTE buf = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_KEY_IDENTIFIER_PROP_ID, buf, &size);
|
|
|
|
ret = !memcmp(buf, id->u.KeyId.pbData, size);
|
|
|
|
CryptMemFree(buf);
|
|
|
|
}
|
2013-12-08 06:09:17 +01:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
2007-08-02 21:30:19 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
2007-08-13 23:30:29 +02:00
|
|
|
break;
|
2007-08-02 21:30:19 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-15 20:14:23 +02:00
|
|
|
static BOOL compare_existing_cert(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT toCompare = pvPara;
|
|
|
|
return CertCompareCertificate(pCertContext->dwCertEncodingType,
|
|
|
|
pCertContext->pCertInfo, toCompare->pCertInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
2007-08-02 21:30:19 +02:00
|
|
|
DWORD dwFlags, const void *pvPara)
|
2006-05-18 16:34:03 +02:00
|
|
|
{
|
2009-10-15 20:14:23 +02:00
|
|
|
const CRYPT_HASH_BLOB *hash = pvPara;
|
|
|
|
DWORD size = 0;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ret = CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_SIGNATURE_HASH_PROP_ID, NULL, &size);
|
|
|
|
if (ret && size == hash->cbData)
|
|
|
|
{
|
|
|
|
LPBYTE buf = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_SIGNATURE_HASH_PROP_ID, buf, &size);
|
|
|
|
ret = !memcmp(buf, hash->pbData, size);
|
|
|
|
CryptMemFree(buf);
|
|
|
|
}
|
2013-12-08 06:09:17 +01:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
2009-10-15 20:14:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline PCCERT_CONTEXT cert_compare_certs_in_store(HCERTSTORE store,
|
|
|
|
PCCERT_CONTEXT prev, CertCompareFunc compare, DWORD dwType, DWORD dwFlags,
|
|
|
|
const void *pvPara)
|
|
|
|
{
|
|
|
|
BOOL matches = FALSE;
|
|
|
|
PCCERT_CONTEXT ret;
|
|
|
|
|
|
|
|
ret = prev;
|
|
|
|
do {
|
|
|
|
ret = CertEnumCertificatesInStore(store, ret);
|
|
|
|
if (ret)
|
|
|
|
matches = compare(ret, dwType, dwFlags, pvPara);
|
|
|
|
} while (ret != NULL && !matches);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef PCCERT_CONTEXT (*CertFindFunc)(HCERTSTORE store, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev);
|
|
|
|
|
|
|
|
static PCCERT_CONTEXT find_cert_any(HCERTSTORE store, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
|
|
|
|
{
|
|
|
|
return CertEnumCertificatesInStore(store, prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PCCERT_CONTEXT find_cert_by_issuer(HCERTSTORE store, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
PCCERT_CONTEXT found = NULL, subject = pvPara;
|
2007-08-07 22:14:02 +02:00
|
|
|
PCERT_EXTENSION ext;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER,
|
|
|
|
subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
|
|
|
|
{
|
|
|
|
CERT_AUTHORITY_KEY_ID_INFO *info;
|
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
|
|
|
|
X509_AUTHORITY_KEY_ID, ext->Value.pbData, ext->Value.cbData,
|
|
|
|
CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
|
|
|
|
&info, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CERT_ID id;
|
|
|
|
|
|
|
|
if (info->CertIssuer.cbData && info->CertSerialNumber.cbData)
|
|
|
|
{
|
|
|
|
id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
|
|
|
|
memcpy(&id.u.IssuerSerialNumber.Issuer, &info->CertIssuer,
|
|
|
|
sizeof(CERT_NAME_BLOB));
|
|
|
|
memcpy(&id.u.IssuerSerialNumber.SerialNumber,
|
|
|
|
&info->CertSerialNumber, sizeof(CRYPT_INTEGER_BLOB));
|
|
|
|
}
|
|
|
|
else if (info->KeyId.cbData)
|
|
|
|
{
|
|
|
|
id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
|
|
|
|
memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
2009-10-15 20:14:23 +02:00
|
|
|
if (ret)
|
|
|
|
found = cert_compare_certs_in_store(store, prev,
|
|
|
|
compare_cert_by_cert_id, dwType, dwFlags, &id);
|
2007-08-07 22:14:02 +02:00
|
|
|
LocalFree(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2,
|
|
|
|
subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
|
|
|
|
{
|
|
|
|
CERT_AUTHORITY_KEY_ID2_INFO *info;
|
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
|
|
|
|
X509_AUTHORITY_KEY_ID2, ext->Value.pbData, ext->Value.cbData,
|
|
|
|
CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
|
|
|
|
&info, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CERT_ID id;
|
|
|
|
|
|
|
|
if (info->AuthorityCertIssuer.cAltEntry &&
|
|
|
|
info->AuthorityCertSerialNumber.cbData)
|
|
|
|
{
|
|
|
|
PCERT_ALT_NAME_ENTRY directoryName = NULL;
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; !directoryName &&
|
|
|
|
i < info->AuthorityCertIssuer.cAltEntry; i++)
|
|
|
|
if (info->AuthorityCertIssuer.rgAltEntry[i].dwAltNameChoice
|
|
|
|
== CERT_ALT_NAME_DIRECTORY_NAME)
|
|
|
|
directoryName =
|
|
|
|
&info->AuthorityCertIssuer.rgAltEntry[i];
|
|
|
|
if (directoryName)
|
|
|
|
{
|
|
|
|
id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
|
|
|
|
memcpy(&id.u.IssuerSerialNumber.Issuer,
|
|
|
|
&directoryName->u.DirectoryName, sizeof(CERT_NAME_BLOB));
|
|
|
|
memcpy(&id.u.IssuerSerialNumber.SerialNumber,
|
|
|
|
&info->AuthorityCertSerialNumber,
|
|
|
|
sizeof(CRYPT_INTEGER_BLOB));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME("no supported name type in authority key id2\n");
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (info->KeyId.cbData)
|
|
|
|
{
|
|
|
|
id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
|
|
|
|
memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
2009-10-15 20:14:23 +02:00
|
|
|
if (ret)
|
|
|
|
found = cert_compare_certs_in_store(store, prev,
|
|
|
|
compare_cert_by_cert_id, dwType, dwFlags, &id);
|
2007-08-07 22:14:02 +02:00
|
|
|
LocalFree(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-10-15 20:14:23 +02:00
|
|
|
found = cert_compare_certs_in_store(store, prev,
|
|
|
|
compare_cert_by_name, CERT_COMPARE_NAME | CERT_COMPARE_SUBJECT_CERT,
|
|
|
|
dwFlags, &subject->pCertInfo->Issuer);
|
|
|
|
return found;
|
2009-10-15 19:54:09 +02:00
|
|
|
}
|
|
|
|
|
2009-10-17 01:32:46 +02:00
|
|
|
static BOOL compare_cert_by_name_str(PCCERT_CONTEXT pCertContext,
|
|
|
|
DWORD dwType, DWORD dwFlags, const void *pvPara)
|
|
|
|
{
|
|
|
|
PCERT_NAME_BLOB name;
|
|
|
|
DWORD len;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (dwType & CERT_INFO_SUBJECT_FLAG)
|
|
|
|
name = &pCertContext->pCertInfo->Subject;
|
|
|
|
else
|
|
|
|
name = &pCertContext->pCertInfo->Issuer;
|
|
|
|
len = CertNameToStrW(pCertContext->dwCertEncodingType, name,
|
|
|
|
CERT_SIMPLE_NAME_STR, NULL, 0);
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
LPWSTR str = CryptMemAlloc(len * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
CertNameToStrW(pCertContext->dwCertEncodingType, name,
|
|
|
|
CERT_SIMPLE_NAME_STR, str, len);
|
2020-11-09 11:41:21 +01:00
|
|
|
wcslwr(str);
|
|
|
|
if (wcsstr(str, pvPara))
|
2009-10-17 01:32:46 +02:00
|
|
|
ret = TRUE;
|
|
|
|
CryptMemFree(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-09 03:22:22 +02:00
|
|
|
static PCCERT_CONTEXT find_cert_by_name_str_a(HCERTSTORE store, DWORD dwType,
|
|
|
|
DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT found = NULL;
|
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_a(pvPara));
|
|
|
|
|
|
|
|
if (pvPara)
|
|
|
|
{
|
|
|
|
int len = MultiByteToWideChar(CP_ACP, 0, pvPara, -1, NULL, 0);
|
|
|
|
LPWSTR str = CryptMemAlloc(len * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, pvPara, -1, str, len);
|
2020-11-09 11:41:21 +01:00
|
|
|
wcslwr(str);
|
2011-06-09 03:22:22 +02:00
|
|
|
found = cert_compare_certs_in_store(store, prev,
|
|
|
|
compare_cert_by_name_str, dwType, dwFlags, str);
|
|
|
|
CryptMemFree(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
found = find_cert_any(store, dwType, dwFlags, NULL, prev);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PCCERT_CONTEXT find_cert_by_name_str_w(HCERTSTORE store, DWORD dwType,
|
2009-10-17 01:32:46 +02:00
|
|
|
DWORD dwFlags, const void *pvPara, PCCERT_CONTEXT prev)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT found = NULL;
|
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(pvPara));
|
|
|
|
|
|
|
|
if (pvPara)
|
|
|
|
{
|
2020-11-09 11:41:21 +01:00
|
|
|
DWORD len = lstrlenW(pvPara);
|
2009-10-17 01:32:46 +02:00
|
|
|
LPWSTR str = CryptMemAlloc((len + 1) * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
{
|
2020-11-09 11:41:21 +01:00
|
|
|
wcscpy( str, pvPara );
|
|
|
|
wcslwr( str );
|
2009-10-17 01:32:46 +02:00
|
|
|
found = cert_compare_certs_in_store(store, prev,
|
|
|
|
compare_cert_by_name_str, dwType, dwFlags, str);
|
|
|
|
CryptMemFree(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
found = find_cert_any(store, dwType, dwFlags, NULL, prev);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2006-05-18 16:34:03 +02:00
|
|
|
PCCERT_CONTEXT WINAPI CertFindCertificateInStore(HCERTSTORE hCertStore,
|
|
|
|
DWORD dwCertEncodingType, DWORD dwFlags, DWORD dwType, const void *pvPara,
|
|
|
|
PCCERT_CONTEXT pPrevCertContext)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT ret;
|
2009-10-15 19:54:09 +02:00
|
|
|
CertFindFunc find = NULL;
|
|
|
|
CertCompareFunc compare = NULL;
|
2019-01-31 09:35:32 +01:00
|
|
|
CERT_ID cert_id;
|
2006-05-18 16:34:03 +02:00
|
|
|
|
2007-09-27 20:08:36 +02:00
|
|
|
TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore, dwCertEncodingType,
|
2006-05-18 16:34:03 +02:00
|
|
|
dwFlags, dwType, pvPara, pPrevCertContext);
|
|
|
|
|
|
|
|
switch (dwType >> CERT_COMPARE_SHIFT)
|
|
|
|
{
|
|
|
|
case CERT_COMPARE_ANY:
|
2009-10-15 19:54:09 +02:00
|
|
|
find = find_cert_any;
|
2006-05-18 16:34:03 +02:00
|
|
|
break;
|
|
|
|
case CERT_COMPARE_MD5_HASH:
|
|
|
|
compare = compare_cert_by_md5_hash;
|
|
|
|
break;
|
|
|
|
case CERT_COMPARE_SHA1_HASH:
|
|
|
|
compare = compare_cert_by_sha1_hash;
|
|
|
|
break;
|
|
|
|
case CERT_COMPARE_NAME:
|
|
|
|
compare = compare_cert_by_name;
|
|
|
|
break;
|
2009-01-27 18:00:40 +01:00
|
|
|
case CERT_COMPARE_PUBLIC_KEY:
|
|
|
|
compare = compare_cert_by_public_key;
|
|
|
|
break;
|
2011-06-09 03:22:22 +02:00
|
|
|
case CERT_COMPARE_NAME_STR_A:
|
|
|
|
find = find_cert_by_name_str_a;
|
|
|
|
break;
|
2009-10-17 01:32:46 +02:00
|
|
|
case CERT_COMPARE_NAME_STR_W:
|
2011-06-09 03:22:22 +02:00
|
|
|
find = find_cert_by_name_str_w;
|
2009-10-17 01:32:46 +02:00
|
|
|
break;
|
2006-05-18 16:34:03 +02:00
|
|
|
case CERT_COMPARE_SUBJECT_CERT:
|
|
|
|
compare = compare_cert_by_subject_cert;
|
|
|
|
break;
|
2019-01-31 09:35:32 +01:00
|
|
|
case CERT_COMPARE_KEY_IDENTIFIER:
|
|
|
|
cert_id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
|
|
|
|
cert_id.u.KeyId = *(const CRYPT_HASH_BLOB *)pvPara;
|
|
|
|
pvPara = &cert_id;
|
|
|
|
/* fall through */
|
2007-08-02 21:30:19 +02:00
|
|
|
case CERT_COMPARE_CERT_ID:
|
|
|
|
compare = compare_cert_by_cert_id;
|
|
|
|
break;
|
2006-05-18 16:34:03 +02:00
|
|
|
case CERT_COMPARE_ISSUER_OF:
|
2009-10-15 20:14:23 +02:00
|
|
|
find = find_cert_by_issuer;
|
2006-05-18 16:34:03 +02:00
|
|
|
break;
|
2007-11-26 20:49:36 +01:00
|
|
|
case CERT_COMPARE_EXISTING:
|
|
|
|
compare = compare_existing_cert;
|
|
|
|
break;
|
2008-08-08 19:44:46 +02:00
|
|
|
case CERT_COMPARE_SIGNATURE_HASH:
|
|
|
|
compare = compare_cert_by_signature_hash;
|
|
|
|
break;
|
2006-05-18 16:34:03 +02:00
|
|
|
default:
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("find type %08x unimplemented\n", dwType);
|
2006-05-18 16:34:03 +02:00
|
|
|
}
|
|
|
|
|
2009-10-15 19:54:09 +02:00
|
|
|
if (find)
|
2018-12-03 16:14:08 +01:00
|
|
|
ret = find(hCertStore, dwType, dwFlags, pvPara, pPrevCertContext);
|
2009-10-15 19:54:09 +02:00
|
|
|
else if (compare)
|
2009-10-15 19:46:44 +02:00
|
|
|
ret = cert_compare_certs_in_store(hCertStore, pPrevCertContext,
|
|
|
|
compare, dwType, dwFlags, pvPara);
|
2006-05-18 16:34:03 +02:00
|
|
|
else
|
|
|
|
ret = NULL;
|
2009-10-15 19:54:09 +02:00
|
|
|
if (!ret)
|
|
|
|
SetLastError(CRYPT_E_NOT_FOUND);
|
2007-08-09 22:19:33 +02:00
|
|
|
TRACE("returning %p\n", ret);
|
2006-05-18 16:34:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
PCCERT_CONTEXT WINAPI CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore,
|
|
|
|
DWORD dwCertEncodingType, PCERT_INFO pCertId)
|
|
|
|
{
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %08x, %p)\n", hCertStore, dwCertEncodingType, pCertId);
|
2006-05-18 16:34:03 +02:00
|
|
|
|
|
|
|
if (!pCertId)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return CertFindCertificateInStore(hCertStore, dwCertEncodingType, 0,
|
|
|
|
CERT_FIND_SUBJECT_CERT, pCertId, NULL);
|
|
|
|
}
|
|
|
|
|
2006-05-26 18:48:13 +02:00
|
|
|
BOOL WINAPI CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject,
|
|
|
|
PCCERT_CONTEXT pIssuer, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
static const DWORD supportedFlags = CERT_STORE_REVOCATION_FLAG |
|
|
|
|
CERT_STORE_SIGNATURE_FLAG | CERT_STORE_TIME_VALIDITY_FLAG;
|
|
|
|
|
|
|
|
if (*pdwFlags & ~supportedFlags)
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (*pdwFlags & CERT_STORE_REVOCATION_FLAG)
|
|
|
|
{
|
2006-07-07 22:19:53 +02:00
|
|
|
DWORD flags = 0;
|
|
|
|
PCCRL_CONTEXT crl = CertGetCRLFromStore(pSubject->hCertStore, pSubject,
|
|
|
|
NULL, &flags);
|
2006-05-26 18:48:13 +02:00
|
|
|
|
2006-07-07 22:19:53 +02:00
|
|
|
/* FIXME: what if the CRL has expired? */
|
2006-05-26 18:48:13 +02:00
|
|
|
if (crl)
|
|
|
|
{
|
2006-07-07 22:19:53 +02:00
|
|
|
if (CertVerifyCRLRevocation(pSubject->dwCertEncodingType,
|
|
|
|
pSubject->pCertInfo, 1, (PCRL_INFO *)&crl->pCrlInfo))
|
|
|
|
*pdwFlags &= CERT_STORE_REVOCATION_FLAG;
|
2006-05-26 18:48:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*pdwFlags |= CERT_STORE_NO_CRL_FLAG;
|
|
|
|
}
|
|
|
|
if (*pdwFlags & CERT_STORE_TIME_VALIDITY_FLAG)
|
|
|
|
{
|
|
|
|
if (0 == CertVerifyTimeValidity(NULL, pSubject->pCertInfo))
|
|
|
|
*pdwFlags &= ~CERT_STORE_TIME_VALIDITY_FLAG;
|
|
|
|
}
|
|
|
|
if (*pdwFlags & CERT_STORE_SIGNATURE_FLAG)
|
|
|
|
{
|
|
|
|
if (CryptVerifyCertificateSignatureEx(0, pSubject->dwCertEncodingType,
|
|
|
|
CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT, (void *)pSubject,
|
|
|
|
CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT, (void *)pIssuer, 0, NULL))
|
|
|
|
*pdwFlags &= ~CERT_STORE_SIGNATURE_FLAG;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-05-18 16:34:03 +02:00
|
|
|
PCCERT_CONTEXT WINAPI CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore,
|
|
|
|
PCCERT_CONTEXT pSubjectContext, PCCERT_CONTEXT pPrevIssuerContext,
|
|
|
|
DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %p, %p, %08x)\n", hCertStore, pSubjectContext,
|
2006-05-18 16:34:03 +02:00
|
|
|
pPrevIssuerContext, *pdwFlags);
|
|
|
|
|
2006-05-26 18:48:13 +02:00
|
|
|
if (!pSubjectContext)
|
2006-05-18 16:34:03 +02:00
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-05-26 18:48:13 +02:00
|
|
|
|
2006-05-18 16:34:03 +02:00
|
|
|
ret = CertFindCertificateInStore(hCertStore,
|
|
|
|
pSubjectContext->dwCertEncodingType, 0, CERT_FIND_ISSUER_OF,
|
|
|
|
pSubjectContext, pPrevIssuerContext);
|
|
|
|
if (ret)
|
|
|
|
{
|
2006-05-26 18:48:13 +02:00
|
|
|
if (!CertVerifySubjectCertificateContext(pSubjectContext, ret,
|
|
|
|
pdwFlags))
|
2006-05-18 16:34:03 +02:00
|
|
|
{
|
2006-05-26 18:48:13 +02:00
|
|
|
CertFreeCertificateContext(ret);
|
|
|
|
ret = NULL;
|
2006-05-18 16:34:03 +02:00
|
|
|
}
|
2014-07-23 01:43:06 +02:00
|
|
|
if (CRYPT_IsCertificateSelfSigned(pSubjectContext))
|
|
|
|
{
|
|
|
|
CertFreeCertificateContext(ret);
|
|
|
|
ret = NULL;
|
|
|
|
SetLastError(CRYPT_E_SELF_SIGNED);
|
|
|
|
}
|
2006-05-18 16:34:03 +02:00
|
|
|
}
|
2007-08-09 22:19:33 +02:00
|
|
|
TRACE("returning %p\n", ret);
|
2006-05-18 16:34:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-23 21:59:43 +02:00
|
|
|
typedef struct _OLD_CERT_REVOCATION_STATUS {
|
|
|
|
DWORD cbSize;
|
|
|
|
DWORD dwIndex;
|
|
|
|
DWORD dwError;
|
|
|
|
DWORD dwReason;
|
2013-09-04 12:40:32 +02:00
|
|
|
} OLD_CERT_REVOCATION_STATUS;
|
2007-10-23 21:59:43 +02:00
|
|
|
|
|
|
|
typedef BOOL (WINAPI *CertVerifyRevocationFunc)(DWORD, DWORD, DWORD,
|
|
|
|
void **, DWORD, PCERT_REVOCATION_PARA, PCERT_REVOCATION_STATUS);
|
|
|
|
|
2007-10-23 21:57:32 +02:00
|
|
|
BOOL WINAPI CertVerifyRevocation(DWORD dwEncodingType, DWORD dwRevType,
|
2007-10-28 02:49:27 +02:00
|
|
|
DWORD cContext, PVOID rgpvContext[], DWORD dwFlags,
|
2007-10-23 21:57:32 +02:00
|
|
|
PCERT_REVOCATION_PARA pRevPara, PCERT_REVOCATION_STATUS pRevStatus)
|
|
|
|
{
|
2007-10-23 21:59:43 +02:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType, dwRevType,
|
2007-10-23 21:57:32 +02:00
|
|
|
cContext, rgpvContext, dwFlags, pRevPara, pRevStatus);
|
2007-10-23 21:59:43 +02:00
|
|
|
|
|
|
|
if (pRevStatus->cbSize != sizeof(OLD_CERT_REVOCATION_STATUS) &&
|
|
|
|
pRevStatus->cbSize != sizeof(CERT_REVOCATION_STATUS))
|
|
|
|
{
|
|
|
|
SetLastError(E_INVALIDARG);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (cContext)
|
|
|
|
{
|
|
|
|
static HCRYPTOIDFUNCSET set = NULL;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if (!set)
|
|
|
|
set = CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC, 0);
|
|
|
|
ret = CryptGetDefaultOIDDllList(set, dwEncodingType, NULL, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (size == 1)
|
|
|
|
{
|
|
|
|
/* empty list */
|
|
|
|
SetLastError(CRYPT_E_NO_REVOCATION_DLL);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LPWSTR dllList = CryptMemAlloc(size * sizeof(WCHAR)), ptr;
|
|
|
|
|
|
|
|
if (dllList)
|
|
|
|
{
|
|
|
|
ret = CryptGetDefaultOIDDllList(set, dwEncodingType,
|
|
|
|
dllList, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
for (ptr = dllList; ret && *ptr;
|
|
|
|
ptr += lstrlenW(ptr) + 1)
|
|
|
|
{
|
|
|
|
CertVerifyRevocationFunc func;
|
|
|
|
HCRYPTOIDFUNCADDR hFunc;
|
|
|
|
|
|
|
|
ret = CryptGetDefaultOIDFunctionAddress(set,
|
|
|
|
dwEncodingType, ptr, 0, (void **)&func, &hFunc);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = func(dwEncodingType, dwRevType, cContext,
|
|
|
|
rgpvContext, dwFlags, pRevPara, pRevStatus);
|
|
|
|
CryptFreeOIDFunctionAddress(hFunc, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(dllList);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
return ret;
|
2007-10-23 21:57:32 +02:00
|
|
|
}
|
|
|
|
|
2005-07-07 22:27:55 +02:00
|
|
|
PCRYPT_ATTRIBUTE WINAPI CertFindAttribute(LPCSTR pszObjId, DWORD cAttr,
|
|
|
|
CRYPT_ATTRIBUTE rgAttr[])
|
|
|
|
{
|
|
|
|
PCRYPT_ATTRIBUTE ret = NULL;
|
|
|
|
DWORD i;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%s %d %p\n", debugstr_a(pszObjId), cAttr, rgAttr);
|
2005-07-07 22:27:55 +02:00
|
|
|
|
|
|
|
if (!cAttr)
|
|
|
|
return NULL;
|
|
|
|
if (!pszObjId)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; !ret && i < cAttr; i++)
|
|
|
|
if (rgAttr[i].pszObjId && !strcmp(pszObjId, rgAttr[i].pszObjId))
|
|
|
|
ret = &rgAttr[i];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
PCERT_EXTENSION WINAPI CertFindExtension(LPCSTR pszObjId, DWORD cExtensions,
|
|
|
|
CERT_EXTENSION rgExtensions[])
|
|
|
|
{
|
|
|
|
PCERT_EXTENSION ret = NULL;
|
|
|
|
DWORD i;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("%s %d %p\n", debugstr_a(pszObjId), cExtensions, rgExtensions);
|
2005-07-07 22:27:55 +02:00
|
|
|
|
|
|
|
if (!cExtensions)
|
|
|
|
return NULL;
|
|
|
|
if (!pszObjId)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; !ret && i < cExtensions; i++)
|
|
|
|
if (rgExtensions[i].pszObjId && !strcmp(pszObjId,
|
|
|
|
rgExtensions[i].pszObjId))
|
|
|
|
ret = &rgExtensions[i];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
PCERT_RDN_ATTR WINAPI CertFindRDNAttr(LPCSTR pszObjId, PCERT_NAME_INFO pName)
|
|
|
|
{
|
|
|
|
PCERT_RDN_ATTR ret = NULL;
|
|
|
|
DWORD i, j;
|
|
|
|
|
|
|
|
TRACE("%s %p\n", debugstr_a(pszObjId), pName);
|
|
|
|
|
|
|
|
if (!pszObjId)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; !ret && i < pName->cRDN; i++)
|
|
|
|
for (j = 0; !ret && j < pName->rgRDN[i].cRDNAttr; j++)
|
|
|
|
if (pName->rgRDN[i].rgRDNAttr[j].pszObjId && !strcmp(pszObjId,
|
|
|
|
pName->rgRDN[i].rgRDNAttr[j].pszObjId))
|
|
|
|
ret = &pName->rgRDN[i].rgRDNAttr[j];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-17 02:08:03 +01:00
|
|
|
static BOOL find_matching_rdn_attr(DWORD dwFlags, const CERT_NAME_INFO *name,
|
|
|
|
const CERT_RDN_ATTR *attr)
|
|
|
|
{
|
|
|
|
DWORD i, j;
|
|
|
|
BOOL match = FALSE;
|
|
|
|
|
|
|
|
for (i = 0; !match && i < name->cRDN; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < name->rgRDN[i].cRDNAttr; j++)
|
|
|
|
{
|
|
|
|
if (!strcmp(name->rgRDN[i].rgRDNAttr[j].pszObjId,
|
|
|
|
attr->pszObjId) &&
|
|
|
|
name->rgRDN[i].rgRDNAttr[j].dwValueType ==
|
|
|
|
attr->dwValueType)
|
|
|
|
{
|
|
|
|
if (dwFlags & CERT_UNICODE_IS_RDN_ATTRS_FLAG)
|
|
|
|
{
|
|
|
|
LPCWSTR nameStr =
|
|
|
|
(LPCWSTR)name->rgRDN[i].rgRDNAttr[j].Value.pbData;
|
|
|
|
LPCWSTR attrStr = (LPCWSTR)attr->Value.pbData;
|
|
|
|
|
2009-11-21 00:37:46 +01:00
|
|
|
if (attr->Value.cbData !=
|
|
|
|
name->rgRDN[i].rgRDNAttr[j].Value.cbData)
|
|
|
|
match = FALSE;
|
|
|
|
else if (dwFlags & CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG)
|
2020-11-09 11:41:21 +01:00
|
|
|
match = !wcsnicmp(nameStr, attrStr,
|
2009-11-17 02:08:03 +01:00
|
|
|
attr->Value.cbData / sizeof(WCHAR));
|
|
|
|
else
|
2020-11-09 11:41:21 +01:00
|
|
|
match = !wcsncmp(nameStr, attrStr,
|
2009-11-17 02:08:03 +01:00
|
|
|
attr->Value.cbData / sizeof(WCHAR));
|
|
|
|
TRACE("%s : %s => %d\n",
|
|
|
|
debugstr_wn(nameStr, attr->Value.cbData / sizeof(WCHAR)),
|
|
|
|
debugstr_wn(attrStr, attr->Value.cbData / sizeof(WCHAR)),
|
|
|
|
match);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LPCSTR nameStr =
|
|
|
|
(LPCSTR)name->rgRDN[i].rgRDNAttr[j].Value.pbData;
|
|
|
|
LPCSTR attrStr = (LPCSTR)attr->Value.pbData;
|
|
|
|
|
2009-11-21 00:37:46 +01:00
|
|
|
if (attr->Value.cbData !=
|
|
|
|
name->rgRDN[i].rgRDNAttr[j].Value.cbData)
|
|
|
|
match = FALSE;
|
|
|
|
else if (dwFlags & CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG)
|
2019-04-03 18:20:30 +02:00
|
|
|
match = !_strnicmp(nameStr, attrStr,
|
2009-11-17 02:08:03 +01:00
|
|
|
attr->Value.cbData);
|
|
|
|
else
|
|
|
|
match = !strncmp(nameStr, attrStr, attr->Value.cbData);
|
|
|
|
TRACE("%s : %s => %d\n",
|
|
|
|
debugstr_an(nameStr, attr->Value.cbData),
|
|
|
|
debugstr_an(attrStr, attr->Value.cbData), match);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2009-11-16 20:59:29 +01:00
|
|
|
BOOL WINAPI CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType,
|
|
|
|
DWORD dwFlags, PCERT_NAME_BLOB pCertName, PCERT_RDN pRDN)
|
|
|
|
{
|
2009-11-17 02:08:03 +01:00
|
|
|
CERT_NAME_INFO *name;
|
|
|
|
LPCSTR type;
|
|
|
|
DWORD size;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType, dwFlags, pCertName,
|
|
|
|
pRDN);
|
|
|
|
|
|
|
|
type = dwFlags & CERT_UNICODE_IS_RDN_ATTRS_FLAG ? X509_UNICODE_NAME :
|
|
|
|
X509_NAME;
|
|
|
|
if ((ret = CryptDecodeObjectEx(dwCertEncodingType, type, pCertName->pbData,
|
|
|
|
pCertName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &name, &size)))
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; ret && i < pRDN->cRDNAttr; i++)
|
|
|
|
ret = find_matching_rdn_attr(dwFlags, name, &pRDN->rgRDNAttr[i]);
|
|
|
|
if (!ret)
|
|
|
|
SetLastError(CRYPT_E_NO_MATCH);
|
|
|
|
LocalFree(name);
|
|
|
|
}
|
|
|
|
return ret;
|
2009-11-16 20:59:29 +01:00
|
|
|
}
|
|
|
|
|
2005-07-07 22:27:55 +02:00
|
|
|
LONG WINAPI CertVerifyTimeValidity(LPFILETIME pTimeToVerify,
|
|
|
|
PCERT_INFO pCertInfo)
|
|
|
|
{
|
|
|
|
FILETIME fileTime;
|
|
|
|
LONG ret;
|
|
|
|
|
|
|
|
if (!pTimeToVerify)
|
|
|
|
{
|
2007-10-18 20:14:12 +02:00
|
|
|
GetSystemTimeAsFileTime(&fileTime);
|
2005-07-07 22:27:55 +02:00
|
|
|
pTimeToVerify = &fileTime;
|
|
|
|
}
|
|
|
|
if ((ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotBefore)) >= 0)
|
|
|
|
{
|
|
|
|
ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotAfter);
|
|
|
|
if (ret < 0)
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2005-08-25 11:51:59 +02:00
|
|
|
|
2006-10-04 16:49:29 +02:00
|
|
|
BOOL WINAPI CertVerifyValidityNesting(PCERT_INFO pSubjectInfo,
|
|
|
|
PCERT_INFO pIssuerInfo)
|
|
|
|
{
|
|
|
|
TRACE("(%p, %p)\n", pSubjectInfo, pIssuerInfo);
|
|
|
|
|
|
|
|
return CertVerifyTimeValidity(&pSubjectInfo->NotBefore, pIssuerInfo) == 0
|
|
|
|
&& CertVerifyTimeValidity(&pSubjectInfo->NotAfter, pIssuerInfo) == 0;
|
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv, ALG_ID Algid,
|
2005-08-25 11:51:59 +02:00
|
|
|
DWORD dwFlags, const BYTE *pbEncoded, DWORD cbEncoded, BYTE *pbComputedHash,
|
|
|
|
DWORD *pcbComputedHash)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
HCRYPTHASH hHash = 0;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv, Algid, dwFlags,
|
2005-08-25 11:51:59 +02:00
|
|
|
pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash);
|
|
|
|
|
|
|
|
if (!hCryptProv)
|
2019-01-23 10:27:44 +01:00
|
|
|
hCryptProv = I_CryptGetDefaultCryptProv(Algid);
|
2005-08-25 11:51:59 +02:00
|
|
|
if (!Algid)
|
|
|
|
Algid = CALG_SHA1;
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptCreateHash(hCryptProv, Algid, 0, 0, &hHash);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptHashData(hHash, pbEncoded, cbEncoded, 0);
|
|
|
|
if (ret)
|
|
|
|
ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
|
|
|
|
pcbComputedHash, 0);
|
|
|
|
CryptDestroyHash(hHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2005-09-15 11:31:20 +02:00
|
|
|
|
2019-01-29 06:17:17 +01:00
|
|
|
BOOL WINAPI CryptHashCertificate2(LPCWSTR pwszCNGHashAlgid, DWORD dwFlags,
|
|
|
|
void *pvReserved, const BYTE *pbEncoded, DWORD cbEncoded, BYTE *pbComputedHash,
|
|
|
|
DWORD *pcbComputedHash)
|
|
|
|
{
|
|
|
|
BCRYPT_HASH_HANDLE hash = NULL;
|
|
|
|
BCRYPT_ALG_HANDLE alg = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
DWORD hash_len;
|
|
|
|
DWORD hash_len_size;
|
|
|
|
|
|
|
|
TRACE("(%s, %08x, %p, %p, %d, %p, %p)\n", debugstr_w(pwszCNGHashAlgid),
|
|
|
|
dwFlags, pvReserved, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash);
|
|
|
|
|
|
|
|
if ((status = BCryptOpenAlgorithmProvider(&alg, pwszCNGHashAlgid, NULL, 0)))
|
|
|
|
{
|
|
|
|
if (status == STATUS_NOT_IMPLEMENTED)
|
|
|
|
status = STATUS_NOT_FOUND;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = BCryptCreateHash(alg, &hash, NULL, 0, NULL, 0, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if ((status = BCryptGetProperty(hash, BCRYPT_HASH_LENGTH, (BYTE *)&hash_len, sizeof(hash_len), &hash_len_size, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (!pbComputedHash)
|
|
|
|
{
|
|
|
|
*pcbComputedHash = hash_len;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pcbComputedHash < hash_len)
|
|
|
|
{
|
|
|
|
status = ERROR_MORE_DATA;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pcbComputedHash = hash_len;
|
|
|
|
|
|
|
|
if ((status = BCryptHashData(hash, (BYTE *)pbEncoded, cbEncoded, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if ((status = BCryptFinishHash(hash, pbComputedHash, hash_len, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (hash) BCryptDestroyHash(hash);
|
|
|
|
if (alg) BCryptCloseAlgorithmProvider(alg, 0);
|
|
|
|
if (status) SetLastError(status);
|
|
|
|
return !status;
|
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv, ALG_ID Algid,
|
2006-07-29 02:26:24 +02:00
|
|
|
DWORD dwFlags, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo,
|
|
|
|
BYTE *pbComputedHash, DWORD *pcbComputedHash)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
HCRYPTHASH hHash = 0;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv, Algid, dwFlags,
|
2006-07-29 02:26:24 +02:00
|
|
|
dwCertEncodingType, pInfo, pbComputedHash, pcbComputedHash);
|
|
|
|
|
|
|
|
if (!hCryptProv)
|
2018-11-02 15:06:57 +01:00
|
|
|
hCryptProv = I_CryptGetDefaultCryptProv(0);
|
2006-07-29 02:26:24 +02:00
|
|
|
if (!Algid)
|
|
|
|
Algid = CALG_MD5;
|
2009-10-20 22:29:03 +02:00
|
|
|
if ((dwCertEncodingType & CERT_ENCODING_TYPE_MASK) != X509_ASN_ENCODING)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_FILE_NOT_FOUND);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-07-29 02:26:24 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
BYTE *buf;
|
|
|
|
DWORD size = 0;
|
|
|
|
|
2009-10-20 22:29:03 +02:00
|
|
|
ret = CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType,
|
|
|
|
X509_PUBLIC_KEY_INFO, pInfo, CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
|
|
|
(LPBYTE)&buf, &size);
|
2006-07-29 02:26:24 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptCreateHash(hCryptProv, Algid, 0, 0, &hHash);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptHashData(hHash, buf, size, 0);
|
|
|
|
if (ret)
|
|
|
|
ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
|
|
|
|
pcbComputedHash, 0);
|
|
|
|
CryptDestroyHash(hHash);
|
|
|
|
}
|
|
|
|
LocalFree(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-08 19:02:21 +02:00
|
|
|
BOOL WINAPI CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv,
|
|
|
|
DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
BYTE *pbComputedHash, DWORD *pcbComputedHash)
|
|
|
|
{
|
2008-08-08 19:25:43 +02:00
|
|
|
BOOL ret;
|
|
|
|
CERT_SIGNED_CONTENT_INFO *info;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv, dwCertEncodingType,
|
2008-08-08 19:02:21 +02:00
|
|
|
pbEncoded, cbEncoded, pbComputedHash, *pcbComputedHash);
|
2008-08-08 19:25:43 +02:00
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT,
|
2009-01-26 11:01:47 +01:00
|
|
|
pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &size);
|
2008-08-08 19:25:43 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
PCCRYPT_OID_INFO oidInfo;
|
|
|
|
HCRYPTHASH hHash;
|
|
|
|
|
|
|
|
if (!hCryptProv)
|
2018-11-02 15:06:57 +01:00
|
|
|
hCryptProv = I_CryptGetDefaultCryptProv(0);
|
2008-08-08 19:25:43 +02:00
|
|
|
oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
|
|
|
|
info->SignatureAlgorithm.pszObjId, 0);
|
|
|
|
if (!oidInfo)
|
|
|
|
{
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = CryptCreateHash(hCryptProv, oidInfo->u.Algid, 0, 0, &hHash);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptHashData(hHash, info->ToBeSigned.pbData,
|
|
|
|
info->ToBeSigned.cbData, 0);
|
|
|
|
if (ret)
|
|
|
|
ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
|
|
|
|
pcbComputedHash, 0);
|
|
|
|
CryptDestroyHash(hHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LocalFree(info);
|
|
|
|
}
|
|
|
|
return ret;
|
2008-08-08 19:02:21 +02:00
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
|
|
|
|
DWORD dwKeySpec, DWORD dwCertEncodingType, const BYTE *pbEncodedToBeSigned,
|
2005-10-31 11:04:06 +01:00
|
|
|
DWORD cbEncodedToBeSigned, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
|
|
|
|
const void *pvHashAuxInfo, BYTE *pbSignature, DWORD *pcbSignature)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
2006-10-03 06:46:08 +02:00
|
|
|
PCCRYPT_OID_INFO info;
|
2005-10-31 11:04:06 +01:00
|
|
|
HCRYPTHASH hHash;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv,
|
2005-10-31 11:04:06 +01:00
|
|
|
dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned,
|
|
|
|
pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature);
|
|
|
|
|
2006-10-03 06:46:08 +02:00
|
|
|
info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
|
|
|
|
pSignatureAlgorithm->pszObjId, 0);
|
|
|
|
if (!info)
|
2005-10-31 11:04:06 +01:00
|
|
|
{
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-10-03 06:46:08 +02:00
|
|
|
if (info->dwGroupId == CRYPT_HASH_ALG_OID_GROUP_ID)
|
2005-10-31 11:04:06 +01:00
|
|
|
{
|
2006-10-03 06:46:08 +02:00
|
|
|
if (!hCryptProv)
|
2018-11-02 15:06:57 +01:00
|
|
|
hCryptProv = I_CryptGetDefaultCryptProv(0);
|
2006-10-18 21:24:50 +02:00
|
|
|
ret = CryptCreateHash(hCryptProv, info->u.Algid, 0, 0, &hHash);
|
2006-10-03 06:46:08 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptHashData(hHash, pbEncodedToBeSigned,
|
|
|
|
cbEncodedToBeSigned, 0);
|
|
|
|
if (ret)
|
|
|
|
ret = CryptGetHashParam(hHash, HP_HASHVAL, pbSignature,
|
|
|
|
pcbSignature, 0);
|
|
|
|
CryptDestroyHash(hHash);
|
|
|
|
}
|
2005-10-31 11:04:06 +01:00
|
|
|
}
|
2006-10-03 06:46:08 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!hCryptProv)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-18 21:24:50 +02:00
|
|
|
ret = CryptCreateHash(hCryptProv, info->u.Algid, 0, 0, &hHash);
|
2006-10-03 06:46:08 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptHashData(hHash, pbEncodedToBeSigned,
|
|
|
|
cbEncodedToBeSigned, 0);
|
|
|
|
if (ret)
|
|
|
|
ret = CryptSignHashW(hHash, dwKeySpec, NULL, 0, pbSignature,
|
|
|
|
pcbSignature);
|
|
|
|
CryptDestroyHash(hHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2005-10-31 11:04:06 +01:00
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv,
|
2006-10-03 06:46:08 +02:00
|
|
|
DWORD dwKeySpec, DWORD dwCertEncodingType, LPCSTR lpszStructType,
|
|
|
|
const void *pvStructInfo, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
|
2007-08-04 03:13:39 +02:00
|
|
|
const void *pvHashAuxInfo, BYTE *pbEncoded, DWORD *pcbEncoded)
|
2006-10-03 06:46:08 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD encodedSize, hashSize;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv, dwKeySpec,
|
2006-10-03 06:46:08 +02:00
|
|
|
dwCertEncodingType, debugstr_a(lpszStructType), pvStructInfo,
|
|
|
|
pSignatureAlgorithm, pvHashAuxInfo, pbEncoded, pcbEncoded);
|
|
|
|
|
|
|
|
ret = CryptEncodeObject(dwCertEncodingType, lpszStructType, pvStructInfo,
|
|
|
|
NULL, &encodedSize);
|
2005-10-31 11:04:06 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2006-10-03 06:46:08 +02:00
|
|
|
PBYTE encoded = CryptMemAlloc(encodedSize);
|
|
|
|
|
|
|
|
if (encoded)
|
|
|
|
{
|
|
|
|
ret = CryptEncodeObject(dwCertEncodingType, lpszStructType,
|
|
|
|
pvStructInfo, encoded, &encodedSize);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptSignCertificate(hCryptProv, dwKeySpec,
|
|
|
|
dwCertEncodingType, encoded, encodedSize, pSignatureAlgorithm,
|
|
|
|
pvHashAuxInfo, NULL, &hashSize);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
PBYTE hash = CryptMemAlloc(hashSize);
|
|
|
|
|
|
|
|
if (hash)
|
|
|
|
{
|
|
|
|
ret = CryptSignCertificate(hCryptProv, dwKeySpec,
|
|
|
|
dwCertEncodingType, encoded, encodedSize,
|
|
|
|
pSignatureAlgorithm, pvHashAuxInfo, hash, &hashSize);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CERT_SIGNED_CONTENT_INFO info = { { 0 } };
|
|
|
|
|
|
|
|
info.ToBeSigned.cbData = encodedSize;
|
|
|
|
info.ToBeSigned.pbData = encoded;
|
2013-02-05 13:22:27 +01:00
|
|
|
info.SignatureAlgorithm = *pSignatureAlgorithm;
|
2006-10-03 06:46:08 +02:00
|
|
|
info.Signature.cbData = hashSize;
|
|
|
|
info.Signature.pbData = hash;
|
|
|
|
info.Signature.cUnusedBits = 0;
|
|
|
|
ret = CryptEncodeObject(dwCertEncodingType,
|
|
|
|
X509_CERT, &info, pbEncoded, pcbEncoded);
|
|
|
|
}
|
|
|
|
CryptMemFree(hash);
|
|
|
|
}
|
2013-12-08 06:09:17 +01:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
2006-10-03 06:46:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(encoded);
|
|
|
|
}
|
2013-12-08 06:09:17 +01:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
2005-10-31 11:04:06 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv,
|
2005-10-31 11:04:06 +01:00
|
|
|
DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
PCERT_PUBLIC_KEY_INFO pPublicKey)
|
|
|
|
{
|
2012-02-02 05:15:27 +01:00
|
|
|
CRYPT_DATA_BLOB blob = { cbEncoded, (BYTE *)pbEncoded };
|
|
|
|
|
2005-10-31 11:04:06 +01:00
|
|
|
return CryptVerifyCertificateSignatureEx(hCryptProv, dwCertEncodingType,
|
2012-02-02 05:15:27 +01:00
|
|
|
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, &blob,
|
2005-10-31 11:04:06 +01:00
|
|
|
CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY, pPublicKey, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:59:37 +02:00
|
|
|
static BOOL CRYPT_VerifySignature(HCRYPTPROV_LEGACY hCryptProv, DWORD dwCertEncodingType,
|
|
|
|
CERT_PUBLIC_KEY_INFO *pubKeyInfo, const CERT_SIGNED_CONTENT_INFO *signedCert, const CRYPT_OID_INFO *info)
|
2006-05-10 08:21:44 +02:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
HCRYPTKEY key;
|
2006-10-03 17:32:10 +02:00
|
|
|
ALG_ID pubKeyID, hashID;
|
2006-05-10 08:21:44 +02:00
|
|
|
|
2007-07-31 23:59:04 +02:00
|
|
|
hashID = info->u.Algid;
|
|
|
|
if (info->ExtraInfo.cbData >= sizeof(ALG_ID))
|
|
|
|
pubKeyID = *(ALG_ID *)info->ExtraInfo.pbData;
|
2006-10-03 17:32:10 +02:00
|
|
|
else
|
2007-07-31 23:59:04 +02:00
|
|
|
pubKeyID = hashID;
|
2006-05-10 08:21:44 +02:00
|
|
|
/* Load the default provider if necessary */
|
|
|
|
if (!hCryptProv)
|
2019-01-23 10:27:37 +01:00
|
|
|
hCryptProv = I_CryptGetDefaultCryptProv(hashID);
|
2006-05-10 08:21:44 +02:00
|
|
|
ret = CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType,
|
2006-10-03 17:32:10 +02:00
|
|
|
pubKeyInfo, pubKeyID, 0, NULL, &key);
|
2006-05-10 08:21:44 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
HCRYPTHASH hash;
|
|
|
|
|
2006-10-03 17:32:10 +02:00
|
|
|
ret = CryptCreateHash(hCryptProv, hashID, 0, 0, &hash);
|
2006-05-10 08:21:44 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CryptHashData(hash, signedCert->ToBeSigned.pbData,
|
|
|
|
signedCert->ToBeSigned.cbData, 0);
|
|
|
|
if (ret)
|
|
|
|
ret = CryptVerifySignatureW(hash, signedCert->Signature.pbData,
|
|
|
|
signedCert->Signature.cbData, key, NULL, 0);
|
|
|
|
CryptDestroyHash(hash);
|
|
|
|
}
|
|
|
|
CryptDestroyKey(key);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:59:37 +02:00
|
|
|
static BOOL CNG_CalcHash(const WCHAR *algorithm, const CERT_SIGNED_CONTENT_INFO *signedCert,
|
|
|
|
BYTE **hash_value, DWORD *hash_len)
|
|
|
|
{
|
|
|
|
BCRYPT_HASH_HANDLE hash = NULL;
|
|
|
|
BCRYPT_ALG_HANDLE alg = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if ((status = BCryptOpenAlgorithmProvider(&alg, algorithm, NULL, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if ((status = BCryptCreateHash(alg, &hash, NULL, 0, NULL, 0, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if ((status = BCryptHashData(hash, signedCert->ToBeSigned.pbData, signedCert->ToBeSigned.cbData, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if ((status = BCryptGetProperty(hash, BCRYPT_HASH_LENGTH, (BYTE *)hash_len, sizeof(*hash_len), &size, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (!(*hash_value = CryptMemAlloc(*hash_len)))
|
|
|
|
{
|
|
|
|
status = STATUS_NO_MEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = BCryptFinishHash(hash, *hash_value, *hash_len, 0)))
|
|
|
|
{
|
|
|
|
CryptMemFree(*hash_value);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (hash) BCryptDestroyHash(hash);
|
|
|
|
if (alg) BCryptCloseAlgorithmProvider(alg, 0);
|
|
|
|
if (status) SetLastError(RtlNtStatusToDosError(status));
|
|
|
|
return status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CNG_ImportECCPubKey(CERT_PUBLIC_KEY_INFO *pubKeyInfo, BCRYPT_KEY_HANDLE *key)
|
|
|
|
{
|
|
|
|
DWORD blob_magic, ecckey_len, size;
|
|
|
|
BCRYPT_ALG_HANDLE alg = NULL;
|
|
|
|
BCRYPT_ECCKEY_BLOB *ecckey;
|
|
|
|
const WCHAR *sign_algo;
|
|
|
|
char **ecc_curve;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if (!pubKeyInfo->PublicKey.cbData)
|
|
|
|
{
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pubKeyInfo->PublicKey.pbData[0] != 0x4)
|
|
|
|
{
|
|
|
|
FIXME("Compressed ECC curves (%02x) not yet supported\n", pubKeyInfo->PublicKey.pbData[0]);
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CryptDecodeObjectEx(X509_ASN_ENCODING, X509_OBJECT_IDENTIFIER, pubKeyInfo->Algorithm.Parameters.pbData,
|
|
|
|
pubKeyInfo->Algorithm.Parameters.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &ecc_curve, &size))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!strcmp(*ecc_curve, szOID_ECC_CURVE_P256))
|
|
|
|
{
|
|
|
|
sign_algo = BCRYPT_ECDSA_P256_ALGORITHM;
|
|
|
|
blob_magic = BCRYPT_ECDSA_PUBLIC_P256_MAGIC;
|
|
|
|
}
|
|
|
|
else if (!strcmp(*ecc_curve, szOID_ECC_CURVE_P384))
|
|
|
|
{
|
|
|
|
sign_algo = BCRYPT_ECDSA_P384_ALGORITHM;
|
|
|
|
blob_magic = BCRYPT_ECDSA_PUBLIC_P384_MAGIC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME("Unsupported ecc curve type: %s\n", *ecc_curve);
|
|
|
|
sign_algo = NULL;
|
|
|
|
blob_magic = 0;
|
|
|
|
}
|
|
|
|
LocalFree(ecc_curve);
|
|
|
|
|
|
|
|
if (!sign_algo)
|
|
|
|
{
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = BCryptOpenAlgorithmProvider(&alg, sign_algo, NULL, 0)))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ecckey_len = sizeof(BCRYPT_ECCKEY_BLOB) + pubKeyInfo->PublicKey.cbData - 1;
|
|
|
|
if (!(ecckey = CryptMemAlloc(ecckey_len)))
|
|
|
|
{
|
|
|
|
status = STATUS_NO_MEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ecckey->dwMagic = blob_magic;
|
|
|
|
ecckey->cbKey = (pubKeyInfo->PublicKey.cbData - 1) / 2;
|
|
|
|
memcpy(ecckey + 1, pubKeyInfo->PublicKey.pbData + 1, pubKeyInfo->PublicKey.cbData - 1);
|
|
|
|
|
|
|
|
status = BCryptImportKeyPair(alg, NULL, BCRYPT_ECCPUBLIC_BLOB, key, (BYTE*)ecckey, ecckey_len, 0);
|
2018-07-11 07:42:01 +02:00
|
|
|
CryptMemFree(ecckey);
|
2018-06-26 09:59:37 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (alg) BCryptCloseAlgorithmProvider(alg, 0);
|
|
|
|
if (status) SetLastError(RtlNtStatusToDosError(status));
|
|
|
|
return !status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CNG_ImportPubKey(CERT_PUBLIC_KEY_INFO *pubKeyInfo, BCRYPT_KEY_HANDLE *key)
|
|
|
|
{
|
|
|
|
if (!strcmp(pubKeyInfo->Algorithm.pszObjId, szOID_ECC_PUBLIC_KEY))
|
|
|
|
return CNG_ImportECCPubKey(pubKeyInfo, key);
|
|
|
|
|
|
|
|
FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo->Algorithm.pszObjId));
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CNG_PrepareSignatureECC(BYTE *encoded_sig, DWORD encoded_size, BYTE **sig_value, DWORD *sig_len)
|
|
|
|
{
|
|
|
|
CERT_ECC_SIGNATURE *ecc_sig;
|
|
|
|
DWORD size;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ECC_SIGNATURE, encoded_sig, encoded_size,
|
|
|
|
CRYPT_DECODE_ALLOC_FLAG, NULL, &ecc_sig, &size))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!ecc_sig->r.cbData || !ecc_sig->s.cbData)
|
|
|
|
{
|
|
|
|
LocalFree(ecc_sig);
|
|
|
|
SetLastError(ERROR_INVALID_DATA);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*sig_len = ecc_sig->r.cbData + ecc_sig->s.cbData;
|
|
|
|
if (!(*sig_value = CryptMemAlloc(*sig_len)))
|
|
|
|
{
|
|
|
|
LocalFree(ecc_sig);
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ecc_sig->r.cbData; i++)
|
|
|
|
(*sig_value)[i] = ecc_sig->r.pbData[ecc_sig->r.cbData - i - 1];
|
|
|
|
for (i = 0; i < ecc_sig->s.cbData; i++)
|
|
|
|
(*sig_value)[ecc_sig->r.cbData + i] = ecc_sig->s.pbData[ecc_sig->s.cbData - i - 1];
|
|
|
|
|
|
|
|
LocalFree(ecc_sig);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CNG_PrepareSignature(CERT_PUBLIC_KEY_INFO *pubKeyInfo, const CERT_SIGNED_CONTENT_INFO *signedCert,
|
|
|
|
BYTE **sig_value, DWORD *sig_len)
|
|
|
|
{
|
|
|
|
BYTE *encoded_sig;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!signedCert->Signature.cbData)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_DATA);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(encoded_sig = CryptMemAlloc(signedCert->Signature.cbData)))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < signedCert->Signature.cbData; i++)
|
|
|
|
encoded_sig[i] = signedCert->Signature.pbData[signedCert->Signature.cbData - i - 1];
|
|
|
|
|
|
|
|
if (!strcmp(pubKeyInfo->Algorithm.pszObjId, szOID_ECC_PUBLIC_KEY))
|
|
|
|
ret = CNG_PrepareSignatureECC(encoded_sig, signedCert->Signature.cbData, sig_value, sig_len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME("Unsupported public key type: %s\n", debugstr_a(pubKeyInfo->Algorithm.pszObjId));
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptMemFree(encoded_sig);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CNG_VerifySignature(HCRYPTPROV_LEGACY hCryptProv, DWORD dwCertEncodingType,
|
|
|
|
CERT_PUBLIC_KEY_INFO *pubKeyInfo, const CERT_SIGNED_CONTENT_INFO *signedCert, const CRYPT_OID_INFO *info)
|
|
|
|
{
|
|
|
|
BCRYPT_KEY_HANDLE key = NULL;
|
|
|
|
BYTE *hash_value = NULL, *sig_value;
|
|
|
|
DWORD hash_len, sig_len;
|
|
|
|
NTSTATUS status;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ret = CNG_ImportPubKey(pubKeyInfo, &key);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CNG_CalcHash(info->pwszCNGAlgid, signedCert, &hash_value, &hash_len);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CNG_PrepareSignature(pubKeyInfo, signedCert, &sig_value, &sig_len);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
status = BCryptVerifySignature(key, NULL, hash_value, hash_len, sig_value, sig_len, 0);
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
FIXME("Failed to verify signature: %08x\n", status);
|
|
|
|
SetLastError(RtlNtStatusToDosError(status));
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
CryptMemFree(sig_value);
|
|
|
|
}
|
|
|
|
CryptMemFree(hash_value);
|
|
|
|
}
|
|
|
|
BCryptDestroyKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv, DWORD dwCertEncodingType,
|
|
|
|
CERT_PUBLIC_KEY_INFO *pubKeyInfo, const CERT_SIGNED_CONTENT_INFO *signedCert)
|
|
|
|
{
|
|
|
|
CCRYPT_OID_INFO *info;
|
|
|
|
|
|
|
|
info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, signedCert->SignatureAlgorithm.pszObjId, 0);
|
|
|
|
if (!info || info->dwGroupId != CRYPT_SIGN_ALG_OID_GROUP_ID)
|
|
|
|
{
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->u.Algid == CALG_OID_INFO_CNG_ONLY)
|
|
|
|
return CNG_VerifySignature(hCryptProv, dwCertEncodingType, pubKeyInfo, signedCert, info);
|
|
|
|
else
|
|
|
|
return CRYPT_VerifySignature(hCryptProv, dwCertEncodingType, pubKeyInfo, signedCert, info);
|
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
BOOL WINAPI CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv,
|
2005-10-31 11:04:06 +01:00
|
|
|
DWORD dwCertEncodingType, DWORD dwSubjectType, void *pvSubject,
|
|
|
|
DWORD dwIssuerType, void *pvIssuer, DWORD dwFlags, void *pvReserved)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
CRYPT_DATA_BLOB subjectBlob;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv,
|
2005-10-31 11:04:06 +01:00
|
|
|
dwCertEncodingType, dwSubjectType, pvSubject, dwIssuerType, pvIssuer,
|
|
|
|
dwFlags, pvReserved);
|
|
|
|
|
|
|
|
switch (dwSubjectType)
|
|
|
|
{
|
|
|
|
case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB:
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
PCRYPT_DATA_BLOB blob = pvSubject;
|
2005-10-31 11:04:06 +01:00
|
|
|
|
|
|
|
subjectBlob.pbData = blob->pbData;
|
|
|
|
subjectBlob.cbData = blob->cbData;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT:
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
PCERT_CONTEXT context = pvSubject;
|
2005-10-31 11:04:06 +01:00
|
|
|
|
|
|
|
subjectBlob.pbData = context->pbCertEncoded;
|
|
|
|
subjectBlob.cbData = context->cbCertEncoded;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL:
|
|
|
|
{
|
2009-01-26 11:01:47 +01:00
|
|
|
PCRL_CONTEXT context = pvSubject;
|
2005-10-31 11:04:06 +01:00
|
|
|
|
|
|
|
subjectBlob.pbData = context->pbCrlEncoded;
|
|
|
|
subjectBlob.cbData = context->cbCrlEncoded;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2006-05-11 18:48:01 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2005-10-31 11:04:06 +01:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
|
|
|
|
DWORD size = 0;
|
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT,
|
|
|
|
subjectBlob.pbData, subjectBlob.cbData,
|
|
|
|
CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
|
2009-01-26 11:01:47 +01:00
|
|
|
&signedCert, &size);
|
2005-10-31 11:04:06 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
switch (dwIssuerType)
|
|
|
|
{
|
|
|
|
case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY:
|
2006-05-10 08:21:44 +02:00
|
|
|
ret = CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv,
|
2009-01-26 11:01:47 +01:00
|
|
|
dwCertEncodingType, pvIssuer,
|
2006-05-10 08:21:44 +02:00
|
|
|
signedCert);
|
2005-10-31 11:04:06 +01:00
|
|
|
break;
|
|
|
|
case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT:
|
2006-05-10 08:21:44 +02:00
|
|
|
ret = CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv,
|
|
|
|
dwCertEncodingType,
|
|
|
|
&((PCCERT_CONTEXT)pvIssuer)->pCertInfo->SubjectPublicKeyInfo,
|
|
|
|
signedCert);
|
|
|
|
break;
|
2005-10-31 11:04:06 +01:00
|
|
|
case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN:
|
2006-05-10 08:21:44 +02:00
|
|
|
FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
|
2005-10-31 11:04:06 +01:00
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL:
|
|
|
|
if (pvIssuer)
|
|
|
|
{
|
2006-05-11 18:48:01 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2005-10-31 11:04:06 +01:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME("unimplemented for NULL signer\n");
|
2006-05-11 18:48:01 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2005-10-31 11:04:06 +01:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2006-05-11 18:48:01 +02:00
|
|
|
SetLastError(E_INVALIDARG);
|
2005-10-31 11:04:06 +01:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
LocalFree(signedCert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-03 00:41:58 +01:00
|
|
|
BOOL WINAPI CertGetIntendedKeyUsage(DWORD dwCertEncodingType,
|
|
|
|
PCERT_INFO pCertInfo, BYTE *pbKeyUsage, DWORD cbKeyUsage)
|
|
|
|
{
|
2009-11-03 19:45:51 +01:00
|
|
|
PCERT_EXTENSION ext;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType, pCertInfo, pbKeyUsage,
|
2009-11-03 00:41:58 +01:00
|
|
|
cbKeyUsage);
|
2009-11-03 19:45:51 +01:00
|
|
|
|
|
|
|
ext = CertFindExtension(szOID_KEY_USAGE, pCertInfo->cExtension,
|
|
|
|
pCertInfo->rgExtension);
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
CRYPT_BIT_BLOB usage;
|
|
|
|
DWORD size = sizeof(usage);
|
|
|
|
|
|
|
|
ret = CryptDecodeObjectEx(dwCertEncodingType, X509_BITS,
|
|
|
|
ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_NOCOPY_FLAG, NULL,
|
|
|
|
&usage, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (cbKeyUsage < usage.cbData)
|
|
|
|
ret = FALSE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(pbKeyUsage, usage.pbData, usage.cbData);
|
|
|
|
if (cbKeyUsage > usage.cbData)
|
|
|
|
memset(pbKeyUsage + usage.cbData, 0,
|
|
|
|
cbKeyUsage - usage.cbData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(0);
|
|
|
|
return ret;
|
2009-11-03 00:41:58 +01:00
|
|
|
}
|
|
|
|
|
2006-02-18 15:49:54 +01:00
|
|
|
BOOL WINAPI CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext, DWORD dwFlags,
|
|
|
|
PCERT_ENHKEY_USAGE pUsage, DWORD *pcbUsage)
|
|
|
|
{
|
|
|
|
PCERT_ENHKEY_USAGE usage = NULL;
|
|
|
|
DWORD bytesNeeded;
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
if (!pCertContext || !pcbUsage)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %08x, %p, %d)\n", pCertContext, dwFlags, pUsage, *pcbUsage);
|
2006-04-13 11:44:52 +02:00
|
|
|
|
2006-02-18 15:49:54 +01:00
|
|
|
if (!(dwFlags & CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG))
|
|
|
|
{
|
|
|
|
DWORD propSize = 0;
|
|
|
|
|
|
|
|
if (CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_ENHKEY_USAGE_PROP_ID, NULL, &propSize))
|
|
|
|
{
|
|
|
|
LPBYTE buf = CryptMemAlloc(propSize);
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
if (CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_ENHKEY_USAGE_PROP_ID, buf, &propSize))
|
|
|
|
{
|
|
|
|
ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
|
|
|
|
X509_ENHANCED_KEY_USAGE, buf, propSize,
|
|
|
|
CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
|
|
|
|
}
|
|
|
|
CryptMemFree(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!usage && !(dwFlags & CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG))
|
|
|
|
{
|
|
|
|
PCERT_EXTENSION ext = CertFindExtension(szOID_ENHANCED_KEY_USAGE,
|
|
|
|
pCertContext->pCertInfo->cExtension,
|
|
|
|
pCertContext->pCertInfo->rgExtension);
|
|
|
|
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
|
|
|
|
X509_ENHANCED_KEY_USAGE, ext->Value.pbData, ext->Value.cbData,
|
|
|
|
CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!usage)
|
|
|
|
{
|
|
|
|
/* If a particular location is specified, this should fail. Otherwise
|
|
|
|
* it should succeed with an empty usage. (This is true on Win2k and
|
|
|
|
* later, which we emulate.)
|
|
|
|
*/
|
|
|
|
if (dwFlags)
|
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_NOT_FOUND);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bytesNeeded = sizeof(CERT_ENHKEY_USAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!pUsage)
|
|
|
|
*pcbUsage = bytesNeeded;
|
|
|
|
else if (*pcbUsage < bytesNeeded)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
*pcbUsage = bytesNeeded;
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pcbUsage = bytesNeeded;
|
|
|
|
if (usage)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
LPSTR nextOID = (LPSTR)((LPBYTE)pUsage +
|
|
|
|
sizeof(CERT_ENHKEY_USAGE) +
|
|
|
|
usage->cUsageIdentifier * sizeof(LPSTR));
|
|
|
|
|
|
|
|
pUsage->cUsageIdentifier = usage->cUsageIdentifier;
|
|
|
|
pUsage->rgpszUsageIdentifier = (LPSTR *)((LPBYTE)pUsage +
|
|
|
|
sizeof(CERT_ENHKEY_USAGE));
|
|
|
|
for (i = 0; i < usage->cUsageIdentifier; i++)
|
|
|
|
{
|
|
|
|
pUsage->rgpszUsageIdentifier[i] = nextOID;
|
|
|
|
strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
|
|
|
|
nextOID += strlen(nextOID) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pUsage->cUsageIdentifier = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (usage)
|
|
|
|
LocalFree(usage);
|
|
|
|
TRACE("returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext,
|
|
|
|
PCERT_ENHKEY_USAGE pUsage)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("(%p, %p)\n", pCertContext, pUsage);
|
|
|
|
|
|
|
|
if (pUsage)
|
|
|
|
{
|
|
|
|
CRYPT_DATA_BLOB blob = { 0, NULL };
|
|
|
|
|
|
|
|
ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_ENHANCED_KEY_USAGE,
|
|
|
|
pUsage, CRYPT_ENCODE_ALLOC_FLAG, NULL, &blob.pbData, &blob.cbData);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = CertSetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_ENHKEY_USAGE_PROP_ID, 0, &blob);
|
|
|
|
LocalFree(blob.pbData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = CertSetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_ENHKEY_USAGE_PROP_ID, 0, NULL);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
|
|
|
|
LPCSTR pszUsageIdentifier)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
|
|
|
|
|
|
|
|
if (CertGetEnhancedKeyUsage(pCertContext,
|
|
|
|
CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, NULL, &size))
|
|
|
|
{
|
|
|
|
PCERT_ENHKEY_USAGE usage = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (usage)
|
|
|
|
{
|
|
|
|
ret = CertGetEnhancedKeyUsage(pCertContext,
|
|
|
|
CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
2008-02-14 19:54:51 +01:00
|
|
|
DWORD i;
|
|
|
|
BOOL exists = FALSE;
|
2006-02-18 15:49:54 +01:00
|
|
|
|
2008-02-14 19:54:51 +01:00
|
|
|
/* Make sure usage doesn't already exist */
|
|
|
|
for (i = 0; !exists && i < usage->cUsageIdentifier; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(usage->rgpszUsageIdentifier[i],
|
|
|
|
pszUsageIdentifier))
|
|
|
|
exists = TRUE;
|
|
|
|
}
|
|
|
|
if (!exists)
|
2006-02-18 15:49:54 +01:00
|
|
|
{
|
2008-02-14 19:54:51 +01:00
|
|
|
PCERT_ENHKEY_USAGE newUsage = CryptMemAlloc(size +
|
|
|
|
sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
|
|
|
|
|
|
|
|
if (newUsage)
|
2006-02-18 15:49:54 +01:00
|
|
|
{
|
2008-02-14 19:54:51 +01:00
|
|
|
LPSTR nextOID;
|
|
|
|
|
|
|
|
newUsage->rgpszUsageIdentifier = (LPSTR *)
|
|
|
|
((LPBYTE)newUsage + sizeof(CERT_ENHKEY_USAGE));
|
|
|
|
nextOID = (LPSTR)((LPBYTE)newUsage->rgpszUsageIdentifier
|
|
|
|
+ (usage->cUsageIdentifier + 1) * sizeof(LPSTR));
|
|
|
|
for (i = 0; i < usage->cUsageIdentifier; i++)
|
|
|
|
{
|
|
|
|
newUsage->rgpszUsageIdentifier[i] = nextOID;
|
|
|
|
strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
|
|
|
|
nextOID += strlen(nextOID) + 1;
|
|
|
|
}
|
2006-02-18 15:49:54 +01:00
|
|
|
newUsage->rgpszUsageIdentifier[i] = nextOID;
|
2008-02-14 19:54:51 +01:00
|
|
|
strcpy(nextOID, pszUsageIdentifier);
|
|
|
|
newUsage->cUsageIdentifier = i + 1;
|
|
|
|
ret = CertSetEnhancedKeyUsage(pCertContext, newUsage);
|
|
|
|
CryptMemFree(newUsage);
|
2006-02-18 15:49:54 +01:00
|
|
|
}
|
2008-02-14 19:54:51 +01:00
|
|
|
else
|
|
|
|
ret = FALSE;
|
2006-02-18 15:49:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(usage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PCERT_ENHKEY_USAGE usage = CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE) +
|
|
|
|
sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
|
|
|
|
|
|
|
|
if (usage)
|
|
|
|
{
|
|
|
|
usage->rgpszUsageIdentifier =
|
|
|
|
(LPSTR *)((LPBYTE)usage + sizeof(CERT_ENHKEY_USAGE));
|
|
|
|
usage->rgpszUsageIdentifier[0] = (LPSTR)((LPBYTE)usage +
|
|
|
|
sizeof(CERT_ENHKEY_USAGE) + sizeof(LPSTR));
|
|
|
|
strcpy(usage->rgpszUsageIdentifier[0], pszUsageIdentifier);
|
|
|
|
usage->cUsageIdentifier = 1;
|
|
|
|
ret = CertSetEnhancedKeyUsage(pCertContext, usage);
|
|
|
|
CryptMemFree(usage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
|
|
|
|
LPCSTR pszUsageIdentifier)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD size;
|
|
|
|
CERT_ENHKEY_USAGE usage;
|
|
|
|
|
|
|
|
TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
|
|
|
|
|
|
|
|
size = sizeof(usage);
|
|
|
|
ret = CertGetEnhancedKeyUsage(pCertContext,
|
|
|
|
CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, &usage, &size);
|
|
|
|
if (!ret && GetLastError() == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (pUsage)
|
|
|
|
{
|
|
|
|
ret = CertGetEnhancedKeyUsage(pCertContext,
|
|
|
|
CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, pUsage, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (pUsage->cUsageIdentifier)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
BOOL found = FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < pUsage->cUsageIdentifier; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(pUsage->rgpszUsageIdentifier[i],
|
|
|
|
pszUsageIdentifier))
|
|
|
|
found = TRUE;
|
|
|
|
if (found && i < pUsage->cUsageIdentifier - 1)
|
|
|
|
pUsage->rgpszUsageIdentifier[i] =
|
|
|
|
pUsage->rgpszUsageIdentifier[i + 1];
|
|
|
|
}
|
|
|
|
pUsage->cUsageIdentifier--;
|
|
|
|
/* Remove the usage if it's empty */
|
|
|
|
if (pUsage->cUsageIdentifier)
|
|
|
|
ret = CertSetEnhancedKeyUsage(pCertContext, pUsage);
|
|
|
|
else
|
|
|
|
ret = CertSetEnhancedKeyUsage(pCertContext, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(pUsage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* it fit in an empty usage, therefore there's nothing to remove */
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-03-08 03:29:18 +01:00
|
|
|
|
2007-10-05 01:12:41 +02:00
|
|
|
struct BitField
|
|
|
|
{
|
|
|
|
DWORD cIndexes;
|
|
|
|
DWORD *indexes;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BITS_PER_DWORD (sizeof(DWORD) * 8)
|
|
|
|
|
|
|
|
static void CRYPT_SetBitInField(struct BitField *field, DWORD bit)
|
|
|
|
{
|
|
|
|
DWORD indexIndex = bit / BITS_PER_DWORD;
|
|
|
|
|
|
|
|
if (indexIndex + 1 > field->cIndexes)
|
|
|
|
{
|
|
|
|
if (field->cIndexes)
|
|
|
|
field->indexes = CryptMemRealloc(field->indexes,
|
|
|
|
(indexIndex + 1) * sizeof(DWORD));
|
|
|
|
else
|
|
|
|
field->indexes = CryptMemAlloc(sizeof(DWORD));
|
|
|
|
if (field->indexes)
|
2007-10-19 22:36:03 +02:00
|
|
|
{
|
|
|
|
field->indexes[indexIndex] = 0;
|
2007-10-05 01:12:41 +02:00
|
|
|
field->cIndexes = indexIndex + 1;
|
2007-10-19 22:36:03 +02:00
|
|
|
}
|
2007-10-05 01:12:41 +02:00
|
|
|
}
|
|
|
|
if (field->indexes)
|
|
|
|
field->indexes[indexIndex] |= 1 << (bit % BITS_PER_DWORD);
|
|
|
|
}
|
|
|
|
|
2009-06-11 23:10:39 +02:00
|
|
|
static BOOL CRYPT_IsBitInFieldSet(const struct BitField *field, DWORD bit)
|
2007-10-05 01:12:41 +02:00
|
|
|
{
|
2014-11-01 02:44:29 +01:00
|
|
|
BOOL set;
|
2007-10-05 01:12:41 +02:00
|
|
|
DWORD indexIndex = bit / BITS_PER_DWORD;
|
|
|
|
|
|
|
|
assert(field->cIndexes);
|
|
|
|
set = field->indexes[indexIndex] & (1 << (bit % BITS_PER_DWORD));
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2006-03-08 03:29:18 +01:00
|
|
|
BOOL WINAPI CertGetValidUsages(DWORD cCerts, PCCERT_CONTEXT *rghCerts,
|
2007-10-04 17:18:49 +02:00
|
|
|
int *cNumOIDs, LPSTR *rghOIDs, DWORD *pcbOIDs)
|
2006-03-08 03:29:18 +01:00
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
DWORD i, cbOIDs = 0;
|
|
|
|
BOOL allUsagesValid = TRUE;
|
|
|
|
CERT_ENHKEY_USAGE validUsages = { 0, NULL };
|
|
|
|
|
2007-10-04 17:56:31 +02:00
|
|
|
TRACE("(%d, %p, %d, %p, %d)\n", cCerts, rghCerts, *cNumOIDs,
|
2006-03-08 03:29:18 +01:00
|
|
|
rghOIDs, *pcbOIDs);
|
|
|
|
|
2007-10-04 23:56:04 +02:00
|
|
|
for (i = 0; i < cCerts; i++)
|
2006-03-08 03:29:18 +01:00
|
|
|
{
|
|
|
|
CERT_ENHKEY_USAGE usage;
|
|
|
|
DWORD size = sizeof(usage);
|
|
|
|
|
|
|
|
ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, &usage, &size);
|
|
|
|
/* Success is deliberately ignored: it implies all usages are valid */
|
|
|
|
if (!ret && GetLastError() == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
allUsagesValid = FALSE;
|
|
|
|
if (pUsage)
|
|
|
|
{
|
|
|
|
ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, pUsage, &size);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!validUsages.cUsageIdentifier)
|
|
|
|
{
|
|
|
|
DWORD j;
|
|
|
|
|
|
|
|
cbOIDs = pUsage->cUsageIdentifier * sizeof(LPSTR);
|
|
|
|
validUsages.cUsageIdentifier = pUsage->cUsageIdentifier;
|
|
|
|
for (j = 0; j < validUsages.cUsageIdentifier; j++)
|
|
|
|
cbOIDs += lstrlenA(pUsage->rgpszUsageIdentifier[j])
|
|
|
|
+ 1;
|
|
|
|
validUsages.rgpszUsageIdentifier =
|
|
|
|
CryptMemAlloc(cbOIDs);
|
|
|
|
if (validUsages.rgpszUsageIdentifier)
|
|
|
|
{
|
|
|
|
LPSTR nextOID = (LPSTR)
|
|
|
|
((LPBYTE)validUsages.rgpszUsageIdentifier +
|
|
|
|
validUsages.cUsageIdentifier * sizeof(LPSTR));
|
|
|
|
|
|
|
|
for (j = 0; j < validUsages.cUsageIdentifier; j++)
|
|
|
|
{
|
|
|
|
validUsages.rgpszUsageIdentifier[j] = nextOID;
|
|
|
|
lstrcpyA(validUsages.rgpszUsageIdentifier[j],
|
|
|
|
pUsage->rgpszUsageIdentifier[j]);
|
|
|
|
nextOID += lstrlenA(nextOID) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-05 01:12:41 +02:00
|
|
|
struct BitField validIndexes = { 0, NULL };
|
|
|
|
DWORD j, k, numRemoved = 0;
|
2006-03-08 03:29:18 +01:00
|
|
|
|
|
|
|
/* Merge: build a bitmap of all the indexes of
|
|
|
|
* validUsages.rgpszUsageIdentifier that are in pUsage.
|
|
|
|
*/
|
|
|
|
for (j = 0; j < pUsage->cUsageIdentifier; j++)
|
|
|
|
{
|
|
|
|
for (k = 0; k < validUsages.cUsageIdentifier; k++)
|
|
|
|
{
|
|
|
|
if (!strcmp(pUsage->rgpszUsageIdentifier[j],
|
|
|
|
validUsages.rgpszUsageIdentifier[k]))
|
|
|
|
{
|
2007-10-05 01:12:41 +02:00
|
|
|
CRYPT_SetBitInField(&validIndexes, k);
|
2006-03-08 03:29:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Merge by removing from validUsages those that are
|
|
|
|
* not in the bitmap.
|
|
|
|
*/
|
|
|
|
for (j = 0; j < validUsages.cUsageIdentifier; j++)
|
|
|
|
{
|
2007-10-05 01:12:41 +02:00
|
|
|
if (!CRYPT_IsBitInFieldSet(&validIndexes, j))
|
2006-03-08 03:29:18 +01:00
|
|
|
{
|
|
|
|
if (j < validUsages.cUsageIdentifier - 1)
|
|
|
|
{
|
2007-11-01 17:45:49 +01:00
|
|
|
memmove(&validUsages.rgpszUsageIdentifier[j],
|
2006-03-08 03:29:18 +01:00
|
|
|
&validUsages.rgpszUsageIdentifier[j +
|
|
|
|
numRemoved + 1],
|
|
|
|
(validUsages.cUsageIdentifier - numRemoved
|
|
|
|
- j - 1) * sizeof(LPSTR));
|
|
|
|
cbOIDs -= lstrlenA(
|
|
|
|
validUsages.rgpszUsageIdentifier[j]) + 1 +
|
|
|
|
sizeof(LPSTR);
|
2007-10-05 00:28:46 +02:00
|
|
|
validUsages.cUsageIdentifier--;
|
2006-03-08 03:29:18 +01:00
|
|
|
numRemoved++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
validUsages.cUsageIdentifier--;
|
|
|
|
}
|
|
|
|
}
|
2007-10-05 01:12:41 +02:00
|
|
|
CryptMemFree(validIndexes.indexes);
|
2006-03-08 03:29:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(pUsage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-04 23:56:04 +02:00
|
|
|
ret = TRUE;
|
|
|
|
if (allUsagesValid)
|
|
|
|
{
|
|
|
|
*cNumOIDs = -1;
|
|
|
|
*pcbOIDs = 0;
|
|
|
|
}
|
|
|
|
else
|
2006-03-08 03:29:18 +01:00
|
|
|
{
|
2007-10-04 23:59:05 +02:00
|
|
|
*cNumOIDs = validUsages.cUsageIdentifier;
|
2007-10-04 23:56:04 +02:00
|
|
|
if (!rghOIDs)
|
|
|
|
*pcbOIDs = cbOIDs;
|
|
|
|
else if (*pcbOIDs < cbOIDs)
|
2006-03-08 03:29:18 +01:00
|
|
|
{
|
2007-10-04 23:56:04 +02:00
|
|
|
*pcbOIDs = cbOIDs;
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
ret = FALSE;
|
2006-03-08 03:29:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-04 23:56:04 +02:00
|
|
|
LPSTR nextOID = (LPSTR)((LPBYTE)rghOIDs +
|
|
|
|
validUsages.cUsageIdentifier * sizeof(LPSTR));
|
2006-03-08 03:29:18 +01:00
|
|
|
|
2007-10-04 23:56:04 +02:00
|
|
|
*pcbOIDs = cbOIDs;
|
|
|
|
for (i = 0; i < validUsages.cUsageIdentifier; i++)
|
|
|
|
{
|
|
|
|
rghOIDs[i] = nextOID;
|
|
|
|
lstrcpyA(nextOID, validUsages.rgpszUsageIdentifier[i]);
|
|
|
|
nextOID += lstrlenA(nextOID) + 1;
|
2006-03-08 03:29:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(validUsages.rgpszUsageIdentifier);
|
2007-10-04 17:56:31 +02:00
|
|
|
TRACE("cNumOIDs: %d\n", *cNumOIDs);
|
|
|
|
TRACE("returning %d\n", ret);
|
2006-03-08 03:29:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2006-04-06 02:54:15 +02:00
|
|
|
|
|
|
|
/* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
|
|
|
|
* pInfo is NULL, from the attributes of hProv.
|
|
|
|
*/
|
|
|
|
static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context,
|
2007-04-09 21:28:22 +02:00
|
|
|
const CRYPT_KEY_PROV_INFO *pInfo, HCRYPTPROV hProv)
|
2006-04-06 02:54:15 +02:00
|
|
|
{
|
|
|
|
CRYPT_KEY_PROV_INFO info = { 0 };
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (!pInfo)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
ret = CryptGetProvParam(hProv, PP_CONTAINER, NULL, &size, 0);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
LPSTR szContainer = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (szContainer)
|
|
|
|
{
|
2006-04-11 06:05:55 +02:00
|
|
|
ret = CryptGetProvParam(hProv, PP_CONTAINER,
|
|
|
|
(BYTE *)szContainer, &size, 0);
|
2006-04-06 02:54:15 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
len = MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
|
|
|
|
NULL, 0);
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
info.pwszContainerName = CryptMemAlloc(len *
|
|
|
|
sizeof(WCHAR));
|
2009-12-15 17:23:10 +01:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
|
2006-04-06 02:54:15 +02:00
|
|
|
info.pwszContainerName, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(szContainer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = CryptGetProvParam(hProv, PP_NAME, NULL, &size, 0);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
LPSTR szProvider = CryptMemAlloc(size);
|
|
|
|
|
|
|
|
if (szProvider)
|
|
|
|
{
|
2006-04-11 06:05:55 +02:00
|
|
|
ret = CryptGetProvParam(hProv, PP_NAME, (BYTE *)szProvider,
|
|
|
|
&size, 0);
|
2006-04-06 02:54:15 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
len = MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
|
|
|
|
NULL, 0);
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
info.pwszProvName = CryptMemAlloc(len *
|
|
|
|
sizeof(WCHAR));
|
2009-12-15 17:23:10 +01:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
|
2006-04-06 02:54:15 +02:00
|
|
|
info.pwszProvName, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(szProvider);
|
|
|
|
}
|
|
|
|
}
|
2007-05-10 04:01:03 +02:00
|
|
|
/* in case no CRYPT_KEY_PROV_INFO given,
|
|
|
|
* we always use AT_SIGNATURE key spec
|
|
|
|
*/
|
|
|
|
info.dwKeySpec = AT_SIGNATURE;
|
2006-04-06 02:54:15 +02:00
|
|
|
size = sizeof(info.dwProvType);
|
|
|
|
ret = CryptGetProvParam(hProv, PP_PROVTYPE, (LPBYTE)&info.dwProvType,
|
|
|
|
&size, 0);
|
|
|
|
if (!ret)
|
|
|
|
info.dwProvType = PROV_RSA_FULL;
|
|
|
|
pInfo = &info;
|
|
|
|
}
|
|
|
|
|
2009-12-15 17:23:10 +01:00
|
|
|
CertSetCertificateContextProperty(context, CERT_KEY_PROV_INFO_PROP_ID,
|
2006-04-06 02:54:15 +02:00
|
|
|
0, pInfo);
|
|
|
|
|
|
|
|
if (pInfo == &info)
|
|
|
|
{
|
|
|
|
CryptMemFree(info.pwszContainerName);
|
|
|
|
CryptMemFree(info.pwszProvName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates a signed certificate context from the unsigned, encoded certificate
|
|
|
|
* in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
|
|
|
|
*/
|
2007-04-09 21:28:22 +02:00
|
|
|
static PCCERT_CONTEXT CRYPT_CreateSignedCert(const CRYPT_DER_BLOB *blob,
|
2007-05-10 04:01:03 +02:00
|
|
|
HCRYPTPROV hProv, DWORD dwKeySpec, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo)
|
2006-04-06 02:54:15 +02:00
|
|
|
{
|
|
|
|
PCCERT_CONTEXT context = NULL;
|
|
|
|
BOOL ret;
|
|
|
|
DWORD sigSize = 0;
|
|
|
|
|
2007-05-10 04:01:03 +02:00
|
|
|
ret = CryptSignCertificate(hProv, dwKeySpec, X509_ASN_ENCODING,
|
2006-04-06 02:54:15 +02:00
|
|
|
blob->pbData, blob->cbData, sigAlgo, NULL, NULL, &sigSize);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
LPBYTE sig = CryptMemAlloc(sigSize);
|
|
|
|
|
2007-05-10 04:01:03 +02:00
|
|
|
ret = CryptSignCertificate(hProv, dwKeySpec, X509_ASN_ENCODING,
|
2006-04-06 02:54:15 +02:00
|
|
|
blob->pbData, blob->cbData, sigAlgo, NULL, sig, &sigSize);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CERT_SIGNED_CONTENT_INFO signedInfo;
|
|
|
|
BYTE *encodedSignedCert = NULL;
|
|
|
|
DWORD encodedSignedCertSize = 0;
|
|
|
|
|
|
|
|
signedInfo.ToBeSigned.cbData = blob->cbData;
|
|
|
|
signedInfo.ToBeSigned.pbData = blob->pbData;
|
2013-02-05 13:22:27 +01:00
|
|
|
signedInfo.SignatureAlgorithm = *sigAlgo;
|
2006-04-06 02:54:15 +02:00
|
|
|
signedInfo.Signature.cbData = sigSize;
|
|
|
|
signedInfo.Signature.pbData = sig;
|
|
|
|
signedInfo.Signature.cUnusedBits = 0;
|
|
|
|
ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT,
|
|
|
|
&signedInfo, CRYPT_ENCODE_ALLOC_FLAG, NULL,
|
2009-01-26 11:01:47 +01:00
|
|
|
&encodedSignedCert, &encodedSignedCertSize);
|
2006-04-06 02:54:15 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
context = CertCreateCertificateContext(X509_ASN_ENCODING,
|
|
|
|
encodedSignedCert, encodedSignedCertSize);
|
|
|
|
LocalFree(encodedSignedCert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(sig);
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copies data from the parameters into info, where:
|
2006-07-14 08:06:49 +02:00
|
|
|
* pSerialNumber: The serial number. Must not be NULL.
|
2006-04-06 02:54:15 +02:00
|
|
|
* pSubjectIssuerBlob: Specifies both the subject and issuer for info.
|
|
|
|
* Must not be NULL
|
|
|
|
* pSignatureAlgorithm: Optional.
|
|
|
|
* pStartTime: The starting time of the certificate. If NULL, the current
|
|
|
|
* system time is used.
|
|
|
|
* pEndTime: The ending time of the certificate. If NULL, one year past the
|
|
|
|
* starting time is used.
|
|
|
|
* pubKey: The public key of the certificate. Must not be NULL.
|
|
|
|
* pExtensions: Extensions to be included with the certificate. Optional.
|
|
|
|
*/
|
2007-04-09 21:28:22 +02:00
|
|
|
static void CRYPT_MakeCertInfo(PCERT_INFO info, const CRYPT_DATA_BLOB *pSerialNumber,
|
|
|
|
const CERT_NAME_BLOB *pSubjectIssuerBlob,
|
|
|
|
const CRYPT_ALGORITHM_IDENTIFIER *pSignatureAlgorithm, const SYSTEMTIME *pStartTime,
|
|
|
|
const SYSTEMTIME *pEndTime, const CERT_PUBLIC_KEY_INFO *pubKey,
|
|
|
|
const CERT_EXTENSIONS *pExtensions)
|
2006-04-06 02:54:15 +02:00
|
|
|
{
|
2006-05-30 21:42:31 +02:00
|
|
|
static CHAR oid[] = szOID_RSA_SHA1RSA;
|
2006-04-06 02:54:15 +02:00
|
|
|
|
|
|
|
assert(info);
|
2006-07-14 08:06:49 +02:00
|
|
|
assert(pSerialNumber);
|
2006-04-06 02:54:15 +02:00
|
|
|
assert(pSubjectIssuerBlob);
|
|
|
|
assert(pubKey);
|
|
|
|
|
2009-10-28 18:48:02 +01:00
|
|
|
if (pExtensions && pExtensions->cExtension)
|
|
|
|
info->dwVersion = CERT_V3;
|
|
|
|
else
|
|
|
|
info->dwVersion = CERT_V1;
|
2006-07-14 08:06:49 +02:00
|
|
|
info->SerialNumber.cbData = pSerialNumber->cbData;
|
|
|
|
info->SerialNumber.pbData = pSerialNumber->pbData;
|
2006-04-06 02:54:15 +02:00
|
|
|
if (pSignatureAlgorithm)
|
2013-02-05 13:22:27 +01:00
|
|
|
info->SignatureAlgorithm = *pSignatureAlgorithm;
|
2006-04-06 02:54:15 +02:00
|
|
|
else
|
|
|
|
{
|
2006-05-30 21:42:31 +02:00
|
|
|
info->SignatureAlgorithm.pszObjId = oid;
|
2006-04-06 02:54:15 +02:00
|
|
|
info->SignatureAlgorithm.Parameters.cbData = 0;
|
|
|
|
info->SignatureAlgorithm.Parameters.pbData = NULL;
|
|
|
|
}
|
|
|
|
info->Issuer.cbData = pSubjectIssuerBlob->cbData;
|
|
|
|
info->Issuer.pbData = pSubjectIssuerBlob->pbData;
|
|
|
|
if (pStartTime)
|
|
|
|
SystemTimeToFileTime(pStartTime, &info->NotBefore);
|
|
|
|
else
|
|
|
|
GetSystemTimeAsFileTime(&info->NotBefore);
|
|
|
|
if (pEndTime)
|
2006-07-11 15:01:48 +02:00
|
|
|
SystemTimeToFileTime(pEndTime, &info->NotAfter);
|
2006-04-06 02:54:15 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SYSTEMTIME endTime;
|
|
|
|
|
|
|
|
if (FileTimeToSystemTime(&info->NotBefore, &endTime))
|
|
|
|
{
|
|
|
|
endTime.wYear++;
|
|
|
|
SystemTimeToFileTime(&endTime, &info->NotAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info->Subject.cbData = pSubjectIssuerBlob->cbData;
|
|
|
|
info->Subject.pbData = pSubjectIssuerBlob->pbData;
|
2013-02-05 13:22:27 +01:00
|
|
|
info->SubjectPublicKeyInfo = *pubKey;
|
2006-04-06 02:54:15 +02:00
|
|
|
if (pExtensions)
|
|
|
|
{
|
|
|
|
info->cExtension = pExtensions->cExtension;
|
|
|
|
info->rgExtension = pExtensions->rgExtension;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->cExtension = 0;
|
|
|
|
info->rgExtension = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef RPC_STATUS (RPC_ENTRY *UuidCreateFunc)(UUID *);
|
|
|
|
typedef RPC_STATUS (RPC_ENTRY *UuidToStringFunc)(UUID *, unsigned char **);
|
|
|
|
typedef RPC_STATUS (RPC_ENTRY *RpcStringFreeFunc)(unsigned char **);
|
|
|
|
|
|
|
|
static HCRYPTPROV CRYPT_CreateKeyProv(void)
|
|
|
|
{
|
|
|
|
HCRYPTPROV hProv = 0;
|
2020-11-09 11:41:52 +01:00
|
|
|
HMODULE rpcrt = LoadLibraryW(L"rpcrt4");
|
2006-04-06 02:54:15 +02:00
|
|
|
|
|
|
|
if (rpcrt)
|
|
|
|
{
|
|
|
|
UuidCreateFunc uuidCreate = (UuidCreateFunc)GetProcAddress(rpcrt,
|
|
|
|
"UuidCreate");
|
|
|
|
UuidToStringFunc uuidToString = (UuidToStringFunc)GetProcAddress(rpcrt,
|
2006-07-14 08:06:49 +02:00
|
|
|
"UuidToStringA");
|
2006-04-06 02:54:15 +02:00
|
|
|
RpcStringFreeFunc rpcStringFree = (RpcStringFreeFunc)GetProcAddress(
|
2006-07-14 08:06:49 +02:00
|
|
|
rpcrt, "RpcStringFreeA");
|
2006-04-06 02:54:15 +02:00
|
|
|
|
|
|
|
if (uuidCreate && uuidToString && rpcStringFree)
|
|
|
|
{
|
|
|
|
UUID uuid;
|
|
|
|
RPC_STATUS status = uuidCreate(&uuid);
|
|
|
|
|
|
|
|
if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
|
|
|
|
{
|
|
|
|
unsigned char *uuidStr;
|
|
|
|
|
|
|
|
status = uuidToString(&uuid, &uuidStr);
|
|
|
|
if (status == RPC_S_OK)
|
|
|
|
{
|
|
|
|
BOOL ret = CryptAcquireContextA(&hProv, (LPCSTR)uuidStr,
|
|
|
|
MS_DEF_PROV_A, PROV_RSA_FULL, CRYPT_NEWKEYSET);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
HCRYPTKEY key;
|
|
|
|
|
|
|
|
ret = CryptGenKey(hProv, AT_SIGNATURE, 0, &key);
|
|
|
|
if (ret)
|
|
|
|
CryptDestroyKey(key);
|
|
|
|
}
|
|
|
|
rpcStringFree(&uuidStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FreeLibrary(rpcrt);
|
|
|
|
}
|
|
|
|
return hProv;
|
|
|
|
}
|
|
|
|
|
2007-08-03 00:44:26 +02:00
|
|
|
PCCERT_CONTEXT WINAPI CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv,
|
2006-04-06 02:54:15 +02:00
|
|
|
PCERT_NAME_BLOB pSubjectIssuerBlob, DWORD dwFlags,
|
|
|
|
PCRYPT_KEY_PROV_INFO pKeyProvInfo,
|
|
|
|
PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm, PSYSTEMTIME pStartTime,
|
|
|
|
PSYSTEMTIME pEndTime, PCERT_EXTENSIONS pExtensions)
|
|
|
|
{
|
|
|
|
PCCERT_CONTEXT context = NULL;
|
|
|
|
BOOL ret, releaseContext = FALSE;
|
|
|
|
PCERT_PUBLIC_KEY_INFO pubKey = NULL;
|
2012-05-30 23:26:56 +02:00
|
|
|
DWORD pubKeySize = 0, dwKeySpec;
|
2006-04-06 02:54:15 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv,
|
2006-04-06 02:54:15 +02:00
|
|
|
pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime,
|
|
|
|
pExtensions, pExtensions);
|
|
|
|
|
2007-05-10 04:01:03 +02:00
|
|
|
if(!pSubjectIssuerBlob)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-30 23:26:56 +02:00
|
|
|
dwKeySpec = pKeyProvInfo ? pKeyProvInfo->dwKeySpec : AT_SIGNATURE;
|
2006-04-06 02:54:15 +02:00
|
|
|
if (!hProv)
|
|
|
|
{
|
2007-05-10 04:01:03 +02:00
|
|
|
if (!pKeyProvInfo)
|
|
|
|
{
|
|
|
|
hProv = CRYPT_CreateKeyProv();
|
|
|
|
releaseContext = TRUE;
|
|
|
|
}
|
|
|
|
else if (pKeyProvInfo->dwFlags & CERT_SET_KEY_PROV_HANDLE_PROP_ID)
|
|
|
|
{
|
|
|
|
SetLastError(NTE_BAD_FLAGS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HCRYPTKEY hKey = 0;
|
|
|
|
/* acquire the context using the given information*/
|
|
|
|
ret = CryptAcquireContextW(&hProv,pKeyProvInfo->pwszContainerName,
|
|
|
|
pKeyProvInfo->pwszProvName,pKeyProvInfo->dwProvType,
|
|
|
|
pKeyProvInfo->dwFlags);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
if(GetLastError() != NTE_BAD_KEYSET)
|
|
|
|
return NULL;
|
|
|
|
/* create the key set */
|
|
|
|
ret = CryptAcquireContextW(&hProv,pKeyProvInfo->pwszContainerName,
|
|
|
|
pKeyProvInfo->pwszProvName,pKeyProvInfo->dwProvType,
|
|
|
|
pKeyProvInfo->dwFlags|CRYPT_NEWKEYSET);
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* check if the key is here */
|
|
|
|
ret = CryptGetUserKey(hProv,dwKeySpec,&hKey);
|
|
|
|
if(!ret)
|
|
|
|
{
|
|
|
|
if (NTE_NO_KEY == GetLastError())
|
|
|
|
{ /* generate the key */
|
|
|
|
ret = CryptGenKey(hProv,dwKeySpec,0,&hKey);
|
|
|
|
}
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
CryptReleaseContext(hProv,0);
|
|
|
|
SetLastError(NTE_BAD_KEYSET);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptDestroyKey(hKey);
|
|
|
|
releaseContext = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2006-04-06 02:54:15 +02:00
|
|
|
|
2012-05-19 00:29:03 +02:00
|
|
|
ret = CryptExportPublicKeyInfo(hProv, dwKeySpec, X509_ASN_ENCODING, NULL,
|
2006-04-06 02:54:15 +02:00
|
|
|
&pubKeySize);
|
2012-05-19 00:29:03 +02:00
|
|
|
if (!ret)
|
|
|
|
goto end;
|
2006-04-06 02:54:15 +02:00
|
|
|
pubKey = CryptMemAlloc(pubKeySize);
|
|
|
|
if (pubKey)
|
|
|
|
{
|
2007-05-10 04:01:03 +02:00
|
|
|
ret = CryptExportPublicKeyInfo(hProv, dwKeySpec, X509_ASN_ENCODING,
|
2006-04-06 02:54:15 +02:00
|
|
|
pubKey, &pubKeySize);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
CERT_INFO info = { 0 };
|
|
|
|
CRYPT_DER_BLOB blob = { 0, NULL };
|
2006-07-14 08:06:49 +02:00
|
|
|
BYTE serial[16];
|
|
|
|
CRYPT_DATA_BLOB serialBlob = { sizeof(serial), serial };
|
2006-04-06 02:54:15 +02:00
|
|
|
|
2006-07-14 08:06:49 +02:00
|
|
|
CryptGenRandom(hProv, sizeof(serial), serial);
|
|
|
|
CRYPT_MakeCertInfo(&info, &serialBlob, pSubjectIssuerBlob,
|
|
|
|
pSignatureAlgorithm, pStartTime, pEndTime, pubKey, pExtensions);
|
2006-04-06 02:54:15 +02:00
|
|
|
ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
|
2009-01-26 11:01:47 +01:00
|
|
|
&info, CRYPT_ENCODE_ALLOC_FLAG, NULL, &blob.pbData,
|
2006-04-06 02:54:15 +02:00
|
|
|
&blob.cbData);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (!(dwFlags & CERT_CREATE_SELFSIGN_NO_SIGN))
|
2007-05-10 04:01:03 +02:00
|
|
|
context = CRYPT_CreateSignedCert(&blob, hProv,dwKeySpec,
|
2006-04-06 02:54:15 +02:00
|
|
|
&info.SignatureAlgorithm);
|
|
|
|
else
|
|
|
|
context = CertCreateCertificateContext(X509_ASN_ENCODING,
|
|
|
|
blob.pbData, blob.cbData);
|
|
|
|
if (context && !(dwFlags & CERT_CREATE_SELFSIGN_NO_KEY_INFO))
|
|
|
|
CertContext_SetKeyProvInfo(context, pKeyProvInfo, hProv);
|
|
|
|
LocalFree(blob.pbData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CryptMemFree(pubKey);
|
|
|
|
}
|
2012-05-19 00:29:03 +02:00
|
|
|
end:
|
2006-04-06 02:54:15 +02:00
|
|
|
if (releaseContext)
|
|
|
|
CryptReleaseContext(hProv, 0);
|
|
|
|
return context;
|
|
|
|
}
|
2008-05-07 18:03:25 +02:00
|
|
|
|
|
|
|
BOOL WINAPI CertVerifyCTLUsage(DWORD dwEncodingType, DWORD dwSubjectType,
|
|
|
|
void *pvSubject, PCTL_USAGE pSubjectUsage, DWORD dwFlags,
|
|
|
|
PCTL_VERIFY_USAGE_PARA pVerifyUsagePara,
|
|
|
|
PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus)
|
|
|
|
{
|
|
|
|
FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType,
|
|
|
|
dwSubjectType, pvSubject, pSubjectUsage, dwFlags, pVerifyUsagePara,
|
|
|
|
pVerifyUsageStatus);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-06-23 09:36:26 +02:00
|
|
|
|
|
|
|
const void * WINAPI CertCreateContext(DWORD dwContextType, DWORD dwEncodingType,
|
|
|
|
const BYTE *pbEncoded, DWORD cbEncoded,
|
|
|
|
DWORD dwFlags, PCERT_CREATE_CONTEXT_PARA pCreatePara)
|
|
|
|
{
|
|
|
|
TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType, dwEncodingType,
|
|
|
|
pbEncoded, cbEncoded, dwFlags, pCreatePara);
|
|
|
|
|
|
|
|
if (dwFlags)
|
|
|
|
{
|
|
|
|
FIXME("dwFlags 0x%08x not handled\n", dwFlags);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pCreatePara)
|
|
|
|
{
|
|
|
|
FIXME("pCreatePara not handled\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dwContextType)
|
|
|
|
{
|
|
|
|
case CERT_STORE_CERTIFICATE_CONTEXT:
|
|
|
|
return CertCreateCertificateContext(dwEncodingType, pbEncoded, cbEncoded);
|
|
|
|
case CERT_STORE_CRL_CONTEXT:
|
|
|
|
return CertCreateCRLContext(dwEncodingType, pbEncoded, cbEncoded);
|
|
|
|
case CERT_STORE_CTL_CONTEXT:
|
|
|
|
return CertCreateCTLContext(dwEncodingType, pbEncoded, cbEncoded);
|
|
|
|
default:
|
|
|
|
WARN("unknown context type: 0x%x\n", dwContextType);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 09:11:11 +01:00
|
|
|
|
|
|
|
BOOL WINAPI CryptSetKeyIdentifierProperty(const CRYPT_HASH_BLOB *pKeyIdentifier, DWORD dwPropId,
|
|
|
|
DWORD dwFlags, LPCWSTR pwszComputerName, void *pvReserved, const void *pvData)
|
|
|
|
{
|
|
|
|
FIXME("(%p, 0x%x, 0x%x, %s, %p, %p): stub\n", pKeyIdentifier, dwPropId, dwFlags,
|
|
|
|
debugstr_w(pwszComputerName), pvReserved, pvData);
|
|
|
|
return FALSE;
|
|
|
|
}
|