2 * Copyright 2004-2006 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "crypt32_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
35 /* Internal version of CertGetCertificateContextProperty that gets properties
36 * directly from the context (or the context it's linked to, depending on its
37 * type.) Doesn't handle special-case properties, since they are handled by
38 * CertGetCertificateContextProperty, and are particular to the store in which
39 * the property exists (which is separate from the context.)
41 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
42 void *pvData
, DWORD
*pcbData
);
44 /* Internal version of CertSetCertificateContextProperty that sets properties
45 * directly on the context (or the context it's linked to, depending on its
46 * type.) Doesn't handle special cases, since they're handled by
47 * CertSetCertificateContextProperty anyway.
49 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
50 DWORD dwFlags
, const void *pvData
);
52 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
53 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
54 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
56 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
57 pbCertEncoded
, cbCertEncoded
);
60 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
, dwCertEncodingType
,
61 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
65 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
66 dwAddDisposition
, ppCertContext
);
67 CertFreeCertificateContext(cert
);
74 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreA(LPCSTR pszCertStoreName
,
75 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
80 TRACE("(%s, %p, %d)\n", debugstr_a(pszCertStoreName
), pbCertEncoded
,
83 store
= CertOpenSystemStoreA(0, pszCertStoreName
);
86 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
87 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
88 CertCloseStore(store
, 0);
93 BOOL WINAPI
CertAddEncodedCertificateToSystemStoreW(LPCWSTR pszCertStoreName
,
94 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
99 TRACE("(%s, %p, %d)\n", debugstr_w(pszCertStoreName
), pbCertEncoded
,
102 store
= CertOpenSystemStoreW(0, pszCertStoreName
);
105 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
106 pbCertEncoded
, cbCertEncoded
, CERT_STORE_ADD_USE_EXISTING
, NULL
);
107 CertCloseStore(store
, 0);
112 BOOL WINAPI
CertAddCertificateLinkToStore(HCERTSTORE hCertStore
,
113 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
114 PCCERT_CONTEXT
*ppCertContext
)
117 PWINECRYPT_CERTSTORE store
= (PWINECRYPT_CERTSTORE
)hCertStore
;
120 FIXME("(%p, %p, %08x, %p): semi-stub\n", hCertStore
, pCertContext
,
121 dwAddDisposition
, ppCertContext
);
122 if (store
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
124 if (store
->type
== StoreTypeCollection
)
126 SetLastError(E_INVALIDARG
);
129 return CertAddCertificateContextToStore(hCertStore
, pCertContext
,
130 dwAddDisposition
, ppCertContext
);
133 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
134 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
136 PCERT_CONTEXT cert
= NULL
;
138 PCERT_INFO certInfo
= NULL
;
141 TRACE("(%08x, %p, %d)\n", dwCertEncodingType
, pbCertEncoded
,
144 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
145 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
151 cert
= Context_CreateDataContext(sizeof(CERT_CONTEXT
));
154 data
= CryptMemAlloc(cbCertEncoded
);
161 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
162 cert
->dwCertEncodingType
= dwCertEncodingType
;
163 cert
->pbCertEncoded
= data
;
164 cert
->cbCertEncoded
= cbCertEncoded
;
165 cert
->pCertInfo
= certInfo
;
166 cert
->hCertStore
= 0;
173 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(
174 PCCERT_CONTEXT pCertContext
)
176 TRACE("(%p)\n", pCertContext
);
181 Context_AddRef((void *)pCertContext
, sizeof(CERT_CONTEXT
));
185 static void CertDataContext_Free(void *context
)
187 PCERT_CONTEXT certContext
= context
;
189 CryptMemFree(certContext
->pbCertEncoded
);
190 LocalFree(certContext
->pCertInfo
);
193 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
197 TRACE("(%p)\n", pCertContext
);
200 ret
= Context_Release((void *)pCertContext
, sizeof(CERT_CONTEXT
),
201 CertDataContext_Free
);
205 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
208 PCONTEXT_PROPERTY_LIST properties
= Context_GetProperties(
209 pCertContext
, sizeof(CERT_CONTEXT
));
212 TRACE("(%p, %d)\n", pCertContext
, dwPropId
);
215 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
221 static BOOL
CertContext_GetHashProp(void *context
, DWORD dwPropId
,
222 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
225 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
229 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
231 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
236 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
243 else if (*pcbData
< cb
)
245 SetLastError(ERROR_MORE_DATA
);
251 memcpy(pvData
, pb
, cb
);
257 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
258 void *pvData
, DWORD
*pcbData
)
260 PCCERT_CONTEXT pCertContext
= context
;
261 PCONTEXT_PROPERTY_LIST properties
=
262 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
264 CRYPT_DATA_BLOB blob
;
266 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
269 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
273 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
276 /* Implicit properties */
279 case CERT_SHA1_HASH_PROP_ID
:
280 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
281 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
284 case CERT_MD5_HASH_PROP_ID
:
285 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
286 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
289 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
290 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
291 pCertContext
->pCertInfo
->Subject
.pbData
,
292 pCertContext
->pCertInfo
->Subject
.cbData
,
295 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
296 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
297 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
298 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
301 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
302 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
303 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
304 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
307 case CERT_SIGNATURE_HASH_PROP_ID
:
308 ret
= CryptHashToBeSigned(0, pCertContext
->dwCertEncodingType
,
309 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
313 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
315 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
318 case CERT_KEY_IDENTIFIER_PROP_ID
:
320 PCERT_EXTENSION ext
= CertFindExtension(
321 szOID_SUBJECT_KEY_IDENTIFIER
, pCertContext
->pCertInfo
->cExtension
,
322 pCertContext
->pCertInfo
->rgExtension
);
326 CRYPT_DATA_BLOB value
;
327 DWORD size
= sizeof(value
);
329 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
330 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
331 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
335 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
337 CertContext_SetProperty(context
, dwPropId
, 0, &value
);
341 SetLastError(ERROR_INVALID_DATA
);
345 SetLastError(CRYPT_E_NOT_FOUND
);
348 TRACE("returning %d\n", ret
);
352 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
354 DWORD i
, containerLen
, provNameLen
;
355 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
357 info
->pwszContainerName
= (LPWSTR
)data
;
358 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
359 data
+= containerLen
;
361 info
->pwszProvName
= (LPWSTR
)data
;
362 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
365 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
366 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
368 for (i
= 0; i
< info
->cProvParam
; i
++)
370 info
->rgProvParam
[i
].pbData
= data
;
371 data
+= info
->rgProvParam
[i
].cbData
;
375 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
376 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
380 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
385 case CERT_CERT_PROP_ID
:
386 case CERT_CRL_PROP_ID
:
387 case CERT_CTL_PROP_ID
:
388 SetLastError(E_INVALIDARG
);
391 case CERT_ACCESS_STATE_PROP_ID
:
392 if (pCertContext
->hCertStore
)
393 ret
= CertGetStoreProperty(pCertContext
->hCertStore
, dwPropId
,
399 ret
= CertContext_CopyParam(pvData
, pcbData
, &state
, sizeof(state
));
402 case CERT_KEY_PROV_HANDLE_PROP_ID
:
404 CERT_KEY_CONTEXT keyContext
;
405 DWORD size
= sizeof(keyContext
);
407 ret
= CertContext_GetProperty((void *)pCertContext
,
408 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
410 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
411 sizeof(keyContext
.hCryptProv
));
414 case CERT_KEY_PROV_INFO_PROP_ID
:
415 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
418 CRYPT_FixKeyProvInfoPointers(pvData
);
421 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
425 TRACE("returning %d\n", ret
);
429 /* Copies key provider info from from into to, where to is assumed to be a
430 * contiguous buffer of memory large enough for from and all its associated
431 * data, but whose pointers are uninitialized.
432 * Upon return, to contains a contiguous copy of from, packed in the following
434 * - CRYPT_KEY_PROV_INFO
435 * - pwszContainerName
437 * - rgProvParam[0]...
439 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
440 const CRYPT_KEY_PROV_INFO
*from
)
443 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
445 if (from
->pwszContainerName
)
447 to
->pwszContainerName
= (LPWSTR
)nextData
;
448 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
449 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
452 to
->pwszContainerName
= NULL
;
453 if (from
->pwszProvName
)
455 to
->pwszProvName
= (LPWSTR
)nextData
;
456 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
457 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
460 to
->pwszProvName
= NULL
;
461 to
->dwProvType
= from
->dwProvType
;
462 to
->dwFlags
= from
->dwFlags
;
463 to
->cProvParam
= from
->cProvParam
;
464 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
465 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
466 to
->dwKeySpec
= from
->dwKeySpec
;
467 for (i
= 0; i
< to
->cProvParam
; i
++)
469 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
470 sizeof(CRYPT_KEY_PROV_PARAM
));
471 to
->rgProvParam
[i
].pbData
= nextData
;
472 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
473 from
->rgProvParam
[i
].cbData
);
474 nextData
+= from
->rgProvParam
[i
].cbData
;
478 static BOOL
CertContext_SetKeyProvInfoProperty(PCONTEXT_PROPERTY_LIST properties
,
479 const CRYPT_KEY_PROV_INFO
*info
)
483 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
485 if (info
->pwszContainerName
)
486 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
489 if (info
->pwszProvName
)
490 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
493 size
+= containerSize
+ provNameSize
;
494 for (i
= 0; i
< info
->cProvParam
; i
++)
495 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
496 buf
= CryptMemAlloc(size
);
499 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
500 ret
= ContextPropertyList_SetProperty(properties
,
501 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
509 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
510 DWORD dwFlags
, const void *pvData
)
512 PCONTEXT_PROPERTY_LIST properties
=
513 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
516 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
524 case CERT_AUTO_ENROLL_PROP_ID
:
525 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
526 case CERT_DESCRIPTION_PROP_ID
:
527 case CERT_FRIENDLY_NAME_PROP_ID
:
528 case CERT_HASH_PROP_ID
:
529 case CERT_KEY_IDENTIFIER_PROP_ID
:
530 case CERT_MD5_HASH_PROP_ID
:
531 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
532 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
533 case CERT_PVK_FILE_PROP_ID
:
534 case CERT_SIGNATURE_HASH_PROP_ID
:
535 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
536 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
537 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
538 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
539 case CERT_ENROLLMENT_PROP_ID
:
540 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
541 case CERT_RENEWAL_PROP_ID
:
545 const CRYPT_DATA_BLOB
*blob
= pvData
;
547 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
548 blob
->pbData
, blob
->cbData
);
552 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
557 case CERT_DATE_STAMP_PROP_ID
:
559 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
560 pvData
, sizeof(FILETIME
));
563 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
567 case CERT_KEY_CONTEXT_PROP_ID
:
571 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
573 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
575 SetLastError(E_INVALIDARG
);
579 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
580 (const BYTE
*)keyContext
, keyContext
->cbSize
);
584 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
589 case CERT_KEY_PROV_INFO_PROP_ID
:
591 ret
= CertContext_SetKeyProvInfoProperty(properties
, pvData
);
594 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
598 case CERT_KEY_PROV_HANDLE_PROP_ID
:
600 CERT_KEY_CONTEXT keyContext
;
601 DWORD size
= sizeof(keyContext
);
603 ret
= CertContext_GetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
607 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
608 CryptReleaseContext(keyContext
.hCryptProv
, 0);
610 keyContext
.cbSize
= sizeof(keyContext
);
612 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
615 keyContext
.hCryptProv
= 0;
616 keyContext
.dwKeySpec
= AT_SIGNATURE
;
618 ret
= CertContext_SetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
623 FIXME("%d: stub\n", dwPropId
);
627 TRACE("returning %d\n", ret
);
631 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
632 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
636 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
638 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
639 * crashes on most of these, I'll be safer.
644 case CERT_ACCESS_STATE_PROP_ID
:
645 case CERT_CERT_PROP_ID
:
646 case CERT_CRL_PROP_ID
:
647 case CERT_CTL_PROP_ID
:
648 SetLastError(E_INVALIDARG
);
651 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
653 TRACE("returning %d\n", ret
);
657 /* Acquires the private key using the key provider info, retrieving info from
658 * the certificate if info is NULL. The acquired provider is returned in
659 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
661 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
662 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
665 BOOL allocated
= FALSE
, ret
= TRUE
;
669 ret
= CertGetCertificateContextProperty(pCert
,
670 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
673 info
= HeapAlloc(GetProcessHeap(), 0, size
);
676 ret
= CertGetCertificateContextProperty(pCert
,
677 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
682 SetLastError(ERROR_OUTOFMEMORY
);
687 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
691 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
692 info
->pwszProvName
, info
->dwProvType
, 0);
697 for (i
= 0; i
< info
->cProvParam
; i
++)
699 CryptSetProvParam(*phCryptProv
,
700 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
701 info
->rgProvParam
[i
].dwFlags
);
703 *pdwKeySpec
= info
->dwKeySpec
;
706 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
709 HeapFree(GetProcessHeap(), 0, info
);
713 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
714 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
715 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
717 BOOL ret
= FALSE
, cache
= FALSE
;
718 PCRYPT_KEY_PROV_INFO info
= NULL
;
719 CERT_KEY_CONTEXT keyContext
;
722 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
723 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
725 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
729 ret
= CertGetCertificateContextProperty(pCert
,
730 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
733 info
= HeapAlloc(GetProcessHeap(), 0, size
);
734 ret
= CertGetCertificateContextProperty(pCert
,
735 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
737 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
740 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
745 size
= sizeof(keyContext
);
746 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
750 *phCryptProv
= keyContext
.hCryptProv
;
752 *pdwKeySpec
= keyContext
.dwKeySpec
;
753 if (pfCallerFreeProv
)
754 *pfCallerFreeProv
= !cache
;
759 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
760 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
763 *phCryptProv
= keyContext
.hCryptProv
;
765 *pdwKeySpec
= keyContext
.dwKeySpec
;
768 keyContext
.cbSize
= sizeof(keyContext
);
769 if (CertSetCertificateContextProperty(pCert
,
770 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
772 if (pfCallerFreeProv
)
773 *pfCallerFreeProv
= FALSE
;
778 if (pfCallerFreeProv
)
779 *pfCallerFreeProv
= TRUE
;
783 HeapFree(GetProcessHeap(), 0, info
);
787 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
788 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
791 BOOL matches
= FALSE
;
793 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
794 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
798 /* Need to sign something to verify the sig. What to sign? Why not
799 * the certificate itself?
801 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
802 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
803 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
805 BYTE
*certEncoded
= CryptMemAlloc(size
);
809 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
810 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
811 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
812 NULL
, certEncoded
, &size
))
814 if (size
== pCert
->cbCertEncoded
&&
815 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
818 CryptMemFree(certEncoded
);
821 CryptReleaseContext(csp
, 0);
826 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
827 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
829 CRYPT_KEY_PROV_INFO copy
;
830 WCHAR containerW
[MAX_PATH
];
831 BOOL matches
= FALSE
;
833 MultiByteToWideChar(CP_ACP
, 0, container
, -1,
834 containerW
, sizeof(containerW
) / sizeof(containerW
[0]));
835 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
836 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
839 memcpy(©
, keyProvInfo
, sizeof(copy
));
840 copy
.pwszContainerName
= containerW
;
841 matches
= key_prov_info_matches_cert(pCert
, ©
);
844 keyProvInfo
->pwszContainerName
=
845 CryptMemAlloc((strlenW(containerW
) + 1) * sizeof(WCHAR
));
846 if (keyProvInfo
->pwszContainerName
)
848 strcpyW(keyProvInfo
->pwszContainerName
, containerW
);
849 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
857 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
858 * private key matches pCert's public key. Upon success, updates keyProvInfo
859 * with the matching container's info (free keyProvInfo.pwszContainerName upon
861 * Returns TRUE if found, FALSE if not.
863 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
864 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
866 HCRYPTPROV defProvider
;
867 BOOL ret
, found
= FALSE
;
868 char containerA
[MAX_PATH
];
870 assert(keyProvInfo
->pwszContainerName
== NULL
);
871 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
872 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
873 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
875 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
877 while (ret
&& !found
)
879 DWORD size
= sizeof(containerA
);
881 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
882 (BYTE
*)containerA
, &size
, enumFlags
);
884 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
885 if (enumFlags
& CRYPT_FIRST
)
887 enumFlags
&= ~CRYPT_FIRST
;
888 enumFlags
|= CRYPT_NEXT
;
891 CryptReleaseContext(defProvider
, 0);
896 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
898 BOOL found
= FALSE
, ret
= TRUE
;
899 DWORD index
= 0, cbProvName
= 0;
900 CRYPT_KEY_PROV_INFO keyProvInfo
;
902 TRACE("(%p, %08x)\n", pCert
, dwFlags
);
904 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
905 while (ret
&& !found
)
909 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
913 if (size
<= cbProvName
)
914 ret
= CryptEnumProvidersW(index
, NULL
, 0,
915 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
918 CryptMemFree(keyProvInfo
.pwszProvName
);
919 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
920 if (keyProvInfo
.pwszProvName
)
923 ret
= CryptEnumProvidersW(index
, NULL
, 0,
924 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
927 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
928 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
929 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
930 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
931 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
933 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
934 found
= find_key_prov_info_in_provider(pCert
,
939 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
940 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
941 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
943 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
944 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
945 found
= find_key_prov_info_in_provider(pCert
,
958 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
960 CryptMemFree(keyProvInfo
.pwszProvName
);
961 CryptMemFree(keyProvInfo
.pwszContainerName
);
965 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
967 BOOL matches
= FALSE
;
970 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
973 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
977 if (CertGetCertificateContextProperty(pCert
,
978 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
979 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
980 CryptMemFree(keyProvInfo
);
986 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
987 DWORD dwFlags
, void *pvReserved
)
989 BOOL matches
= FALSE
;
991 TRACE("(%p, %08x, %p)\n", pCert
, dwFlags
, pvReserved
);
993 matches
= cert_prov_info_matches_cert(pCert
);
995 matches
= find_matching_provider(pCert
, dwFlags
);
999 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1000 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1004 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1006 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1007 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1008 &pCertId2
->SerialNumber
);
1009 TRACE("returning %d\n", ret
);
1013 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1014 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1018 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1020 if (pCertName1
->cbData
== pCertName2
->cbData
)
1022 if (pCertName1
->cbData
)
1023 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1024 pCertName1
->cbData
);
1030 TRACE("returning %d\n", ret
);
1034 /* Returns the number of significant bytes in pInt, where a byte is
1035 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1036 * for negative numbers. pInt is assumed to be little-endian.
1038 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1040 DWORD ret
= pInt
->cbData
;
1044 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1046 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1054 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1055 PCRYPT_INTEGER_BLOB pInt2
)
1060 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1062 cb1
= CRYPT_significantBytes(pInt1
);
1063 cb2
= CRYPT_significantBytes(pInt2
);
1067 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1073 TRACE("returning %d\n", ret
);
1077 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1078 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1082 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1084 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1086 case 0: /* Seems to mean "raw binary bits" */
1087 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1088 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1090 if (pPublicKey2
->PublicKey
.cbData
)
1091 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1092 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1100 WARN("Unknown encoding type %08x\n", dwCertEncodingType
);
1102 case X509_ASN_ENCODING
:
1104 BLOBHEADER
*pblob1
, *pblob2
;
1107 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1108 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1111 pblob1
= CryptMemAlloc(length
);
1112 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1113 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1114 0, pblob1
, &length
))
1116 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1117 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1120 pblob2
= CryptMemAlloc(length
);
1121 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1122 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1123 0, pblob2
, &length
))
1125 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1126 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1127 *pk2
= (LPVOID
)(pblob2
+ 1);
1128 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1129 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1131 CryptMemFree(pblob2
);
1134 CryptMemFree(pblob1
);
1143 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1144 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1148 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
1150 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1152 SetLastError(ERROR_FILE_NOT_FOUND
);
1155 if (pPublicKey
->Algorithm
.pszObjId
&&
1156 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1158 FIXME("unimplemented for DH public keys\n");
1159 SetLastError(CRYPT_E_ASN1_BADTAG
);
1165 BOOL ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1166 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1167 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1172 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1174 len
= rsaPubKey
->bitlen
;
1181 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1182 DWORD dwFlags
, const void *pvPara
);
1184 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1185 DWORD dwFlags
, const void *pvPara
)
1189 DWORD size
= sizeof(hash
);
1191 ret
= CertGetCertificateContextProperty(pCertContext
,
1192 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1195 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1197 if (size
== pHash
->cbData
)
1198 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1205 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1206 DWORD dwFlags
, const void *pvPara
)
1210 DWORD size
= sizeof(hash
);
1212 ret
= CertGetCertificateContextProperty(pCertContext
,
1213 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1216 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1218 if (size
== pHash
->cbData
)
1219 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1226 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1227 DWORD dwFlags
, const void *pvPara
)
1229 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1232 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1233 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1235 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1236 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1241 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1242 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1244 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1247 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1248 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1252 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1253 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1255 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1258 /* Matching serial number and subject match.. */
1259 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1260 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1262 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1263 &pCertInfo
->SerialNumber
);
1266 /* failing that, if the serial number and issuer match, we match */
1267 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1268 &pCertInfo
->SerialNumber
);
1270 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1271 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1273 TRACE("returning %d\n", ret
);
1277 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1278 DWORD dwFlags
, const void *pvPara
)
1280 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1283 switch (id
->dwIdChoice
)
1285 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1286 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1287 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1289 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1290 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1292 case CERT_ID_SHA1_HASH
:
1293 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1296 case CERT_ID_KEY_IDENTIFIER
:
1300 ret
= CertGetCertificateContextProperty(pCertContext
,
1301 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1302 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1304 LPBYTE buf
= CryptMemAlloc(size
);
1308 CertGetCertificateContextProperty(pCertContext
,
1309 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1310 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1325 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1326 DWORD dwFlags
, const void *pvPara
)
1328 PCCERT_CONTEXT toCompare
= pvPara
;
1329 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1330 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1333 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1334 DWORD dwFlags
, const void *pvPara
)
1336 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1340 ret
= CertGetCertificateContextProperty(pCertContext
,
1341 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1342 if (ret
&& size
== hash
->cbData
)
1344 LPBYTE buf
= CryptMemAlloc(size
);
1348 CertGetCertificateContextProperty(pCertContext
,
1349 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1350 ret
= !memcmp(buf
, hash
->pbData
, size
);
1359 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1360 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1363 BOOL matches
= FALSE
;
1368 ret
= CertEnumCertificatesInStore(store
, ret
);
1370 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1371 } while (ret
!= NULL
&& !matches
);
1375 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1376 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1378 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1379 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1381 return CertEnumCertificatesInStore(store
, prev
);
1384 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1385 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1388 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1389 PCERT_EXTENSION ext
;
1392 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1393 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1395 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1397 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1398 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1399 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1405 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1407 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1408 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1409 sizeof(CERT_NAME_BLOB
));
1410 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1411 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1413 else if (info
->KeyId
.cbData
)
1415 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1416 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1421 found
= cert_compare_certs_in_store(store
, prev
,
1422 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1426 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1427 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1429 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1431 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1432 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1433 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1439 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1440 info
->AuthorityCertSerialNumber
.cbData
)
1442 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1445 for (i
= 0; !directoryName
&&
1446 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1447 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1448 == CERT_ALT_NAME_DIRECTORY_NAME
)
1450 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1453 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1454 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1455 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1456 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1457 &info
->AuthorityCertSerialNumber
,
1458 sizeof(CRYPT_INTEGER_BLOB
));
1462 FIXME("no supported name type in authority key id2\n");
1466 else if (info
->KeyId
.cbData
)
1468 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1469 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1474 found
= cert_compare_certs_in_store(store
, prev
,
1475 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1480 found
= cert_compare_certs_in_store(store
, prev
,
1481 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1482 dwFlags
, &subject
->pCertInfo
->Issuer
);
1486 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1487 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1489 PCERT_NAME_BLOB name
;
1493 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1494 name
= &pCertContext
->pCertInfo
->Subject
;
1496 name
= &pCertContext
->pCertInfo
->Issuer
;
1497 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1498 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1501 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1507 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1508 CERT_SIMPLE_NAME_STR
, str
, len
);
1509 for (ptr
= str
; *ptr
; ptr
++)
1510 *ptr
= tolowerW(*ptr
);
1511 if (strstrW(str
, pvPara
))
1519 static PCCERT_CONTEXT
find_cert_by_name_str(HCERTSTORE store
, DWORD dwType
,
1520 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1522 PCCERT_CONTEXT found
= NULL
;
1524 TRACE("%s\n", debugstr_w(pvPara
));
1528 DWORD len
= strlenW(pvPara
);
1529 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1536 for (src
= pvPara
, dst
= str
; *src
; src
++, dst
++)
1537 *dst
= tolowerW(*src
);
1539 found
= cert_compare_certs_in_store(store
, prev
,
1540 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1545 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1549 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1550 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1551 PCCERT_CONTEXT pPrevCertContext
)
1554 CertFindFunc find
= NULL
;
1555 CertCompareFunc compare
= NULL
;
1557 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1558 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1560 switch (dwType
>> CERT_COMPARE_SHIFT
)
1562 case CERT_COMPARE_ANY
:
1563 find
= find_cert_any
;
1565 case CERT_COMPARE_MD5_HASH
:
1566 compare
= compare_cert_by_md5_hash
;
1568 case CERT_COMPARE_SHA1_HASH
:
1569 compare
= compare_cert_by_sha1_hash
;
1571 case CERT_COMPARE_NAME
:
1572 compare
= compare_cert_by_name
;
1574 case CERT_COMPARE_PUBLIC_KEY
:
1575 compare
= compare_cert_by_public_key
;
1577 case CERT_COMPARE_NAME_STR_W
:
1578 find
= find_cert_by_name_str
;
1580 case CERT_COMPARE_SUBJECT_CERT
:
1581 compare
= compare_cert_by_subject_cert
;
1583 case CERT_COMPARE_CERT_ID
:
1584 compare
= compare_cert_by_cert_id
;
1586 case CERT_COMPARE_ISSUER_OF
:
1587 find
= find_cert_by_issuer
;
1589 case CERT_COMPARE_EXISTING
:
1590 compare
= compare_existing_cert
;
1592 case CERT_COMPARE_SIGNATURE_HASH
:
1593 compare
= compare_cert_by_signature_hash
;
1596 FIXME("find type %08x unimplemented\n", dwType
);
1600 ret
= find(hCertStore
, dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1602 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1603 compare
, dwType
, dwFlags
, pvPara
);
1607 SetLastError(CRYPT_E_NOT_FOUND
);
1608 TRACE("returning %p\n", ret
);
1612 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1613 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1615 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1619 SetLastError(E_INVALIDARG
);
1622 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1623 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1626 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1627 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1629 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1630 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1632 if (*pdwFlags
& ~supportedFlags
)
1634 SetLastError(E_INVALIDARG
);
1637 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1640 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1643 /* FIXME: what if the CRL has expired? */
1646 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1647 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1648 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1651 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1653 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1655 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1656 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1658 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1660 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1661 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1662 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1663 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1668 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1669 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1674 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1675 pPrevIssuerContext
, *pdwFlags
);
1677 if (!pSubjectContext
)
1679 SetLastError(E_INVALIDARG
);
1683 ret
= CertFindCertificateInStore(hCertStore
,
1684 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1685 pSubjectContext
, pPrevIssuerContext
);
1688 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1691 CertFreeCertificateContext(ret
);
1695 TRACE("returning %p\n", ret
);
1699 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1704 } OLD_CERT_REVOCATION_STATUS
, *POLD_CERT_REVOCATION_STATUS
;
1706 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1707 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
1709 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1710 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1711 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1715 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1716 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1718 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1719 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1721 SetLastError(E_INVALIDARG
);
1726 static HCRYPTOIDFUNCSET set
= NULL
;
1730 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
1731 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
1737 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
1742 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
1746 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
1750 for (ptr
= dllList
; ret
&& *ptr
;
1751 ptr
+= lstrlenW(ptr
) + 1)
1753 CertVerifyRevocationFunc func
;
1754 HCRYPTOIDFUNCADDR hFunc
;
1756 ret
= CryptGetDefaultOIDFunctionAddress(set
,
1757 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
1760 ret
= func(dwEncodingType
, dwRevType
, cContext
,
1761 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1762 CryptFreeOIDFunctionAddress(hFunc
, 0);
1766 CryptMemFree(dllList
);
1770 SetLastError(ERROR_OUTOFMEMORY
);
1781 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
1782 CRYPT_ATTRIBUTE rgAttr
[])
1784 PCRYPT_ATTRIBUTE ret
= NULL
;
1787 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
1793 SetLastError(ERROR_INVALID_PARAMETER
);
1797 for (i
= 0; !ret
&& i
< cAttr
; i
++)
1798 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
1803 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
1804 CERT_EXTENSION rgExtensions
[])
1806 PCERT_EXTENSION ret
= NULL
;
1809 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
1815 SetLastError(ERROR_INVALID_PARAMETER
);
1819 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
1820 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
1821 rgExtensions
[i
].pszObjId
))
1822 ret
= &rgExtensions
[i
];
1826 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
1828 PCERT_RDN_ATTR ret
= NULL
;
1831 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
1835 SetLastError(ERROR_INVALID_PARAMETER
);
1839 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
1840 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
1841 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
1842 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
1843 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
1847 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
1848 const CERT_RDN_ATTR
*attr
)
1853 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
1855 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
1857 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
1859 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
1862 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
1865 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1866 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
1868 if (attr
->Value
.cbData
!=
1869 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1871 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1872 match
= !strncmpiW(nameStr
, attrStr
,
1873 attr
->Value
.cbData
/ sizeof(WCHAR
));
1875 match
= !strncmpW(nameStr
, attrStr
,
1876 attr
->Value
.cbData
/ sizeof(WCHAR
));
1877 TRACE("%s : %s => %d\n",
1878 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1879 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1885 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1886 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
1888 if (attr
->Value
.cbData
!=
1889 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1891 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1892 match
= !strncasecmp(nameStr
, attrStr
,
1893 attr
->Value
.cbData
);
1895 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
1896 TRACE("%s : %s => %d\n",
1897 debugstr_an(nameStr
, attr
->Value
.cbData
),
1898 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
1906 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
1907 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
1909 CERT_NAME_INFO
*name
;
1914 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
1917 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
1919 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
1920 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
1924 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
1925 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
1927 SetLastError(CRYPT_E_NO_MATCH
);
1933 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
1934 PCERT_INFO pCertInfo
)
1941 GetSystemTimeAsFileTime(&fileTime
);
1942 pTimeToVerify
= &fileTime
;
1944 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
1946 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
1953 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
1954 PCERT_INFO pIssuerInfo
)
1956 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
1958 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
1959 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
1962 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
1963 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
1964 DWORD
*pcbComputedHash
)
1967 HCRYPTHASH hHash
= 0;
1969 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
1970 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
1973 hCryptProv
= CRYPT_GetDefaultProvider();
1978 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
1981 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
1983 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
1984 pcbComputedHash
, 0);
1985 CryptDestroyHash(hHash
);
1991 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
1992 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
1993 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
1996 HCRYPTHASH hHash
= 0;
1998 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
1999 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2002 hCryptProv
= CRYPT_GetDefaultProvider();
2005 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2007 SetLastError(ERROR_FILE_NOT_FOUND
);
2015 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2016 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2017 (LPBYTE
)&buf
, &size
);
2020 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2023 ret
= CryptHashData(hHash
, buf
, size
, 0);
2025 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2026 pcbComputedHash
, 0);
2027 CryptDestroyHash(hHash
);
2035 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2036 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2037 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2040 CERT_SIGNED_CONTENT_INFO
*info
;
2043 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv
, dwCertEncodingType
,
2044 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2046 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2047 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2050 PCCRYPT_OID_INFO oidInfo
;
2054 hCryptProv
= CRYPT_GetDefaultProvider();
2055 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2056 info
->SignatureAlgorithm
.pszObjId
, 0);
2059 SetLastError(NTE_BAD_ALGID
);
2064 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2067 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2068 info
->ToBeSigned
.cbData
, 0);
2070 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2071 pcbComputedHash
, 0);
2072 CryptDestroyHash(hHash
);
2080 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2081 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2082 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2083 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2086 PCCRYPT_OID_INFO info
;
2089 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
2090 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2091 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2093 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2094 pSignatureAlgorithm
->pszObjId
, 0);
2097 SetLastError(NTE_BAD_ALGID
);
2100 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2103 hCryptProv
= CRYPT_GetDefaultProvider();
2104 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2107 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2108 cbEncodedToBeSigned
, 0);
2110 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2112 CryptDestroyHash(hHash
);
2119 SetLastError(ERROR_INVALID_PARAMETER
);
2124 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2127 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2128 cbEncodedToBeSigned
, 0);
2130 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2132 CryptDestroyHash(hHash
);
2139 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2140 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2141 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2142 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2145 DWORD encodedSize
, hashSize
;
2147 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2148 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2149 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2151 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2152 NULL
, &encodedSize
);
2155 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2159 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2160 pvStructInfo
, encoded
, &encodedSize
);
2163 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2164 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2165 pvHashAuxInfo
, NULL
, &hashSize
);
2168 PBYTE hash
= CryptMemAlloc(hashSize
);
2172 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2173 dwCertEncodingType
, encoded
, encodedSize
,
2174 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2177 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2179 info
.ToBeSigned
.cbData
= encodedSize
;
2180 info
.ToBeSigned
.pbData
= encoded
;
2181 memcpy(&info
.SignatureAlgorithm
,
2182 pSignatureAlgorithm
,
2183 sizeof(info
.SignatureAlgorithm
));
2184 info
.Signature
.cbData
= hashSize
;
2185 info
.Signature
.pbData
= hash
;
2186 info
.Signature
.cUnusedBits
= 0;
2187 ret
= CryptEncodeObject(dwCertEncodingType
,
2188 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2194 CryptMemFree(encoded
);
2200 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2201 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2202 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2204 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2205 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, (void *)pbEncoded
,
2206 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2209 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
,
2210 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pubKeyInfo
,
2211 const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2215 PCCRYPT_OID_INFO info
;
2216 ALG_ID pubKeyID
, hashID
;
2218 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2219 signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2220 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2222 SetLastError(NTE_BAD_ALGID
);
2225 hashID
= info
->u
.Algid
;
2226 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2227 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2230 /* Load the default provider if necessary */
2232 hCryptProv
= CRYPT_GetDefaultProvider();
2233 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2234 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2239 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2242 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2243 signedCert
->ToBeSigned
.cbData
, 0);
2245 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2246 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2247 CryptDestroyHash(hash
);
2249 CryptDestroyKey(key
);
2254 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2255 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2256 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2259 CRYPT_DATA_BLOB subjectBlob
;
2261 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
2262 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2263 dwFlags
, pvReserved
);
2265 switch (dwSubjectType
)
2267 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2269 PCRYPT_DATA_BLOB blob
= pvSubject
;
2271 subjectBlob
.pbData
= blob
->pbData
;
2272 subjectBlob
.cbData
= blob
->cbData
;
2275 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2277 PCERT_CONTEXT context
= pvSubject
;
2279 subjectBlob
.pbData
= context
->pbCertEncoded
;
2280 subjectBlob
.cbData
= context
->cbCertEncoded
;
2283 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2285 PCRL_CONTEXT context
= pvSubject
;
2287 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2288 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2292 SetLastError(E_INVALIDARG
);
2298 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2301 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2302 subjectBlob
.pbData
, subjectBlob
.cbData
,
2303 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2304 &signedCert
, &size
);
2307 switch (dwIssuerType
)
2309 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2310 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2311 dwCertEncodingType
, pvIssuer
,
2314 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2315 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2317 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2320 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2321 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2324 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2327 SetLastError(E_INVALIDARG
);
2332 FIXME("unimplemented for NULL signer\n");
2333 SetLastError(E_INVALIDARG
);
2338 SetLastError(E_INVALIDARG
);
2341 LocalFree(signedCert
);
2347 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2348 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
2350 PCERT_EXTENSION ext
;
2353 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
2356 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
2357 pCertInfo
->rgExtension
);
2360 CRYPT_BIT_BLOB usage
;
2361 DWORD size
= sizeof(usage
);
2363 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
2364 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2368 if (cbKeyUsage
< usage
.cbData
)
2372 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
2373 if (cbKeyUsage
> usage
.cbData
)
2374 memset(pbKeyUsage
+ usage
.cbData
, 0,
2375 cbKeyUsage
- usage
.cbData
);
2384 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
2385 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
2387 PCERT_ENHKEY_USAGE usage
= NULL
;
2391 if (!pCertContext
|| !pcbUsage
)
2393 SetLastError(ERROR_INVALID_PARAMETER
);
2397 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
2399 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
2403 if (CertGetCertificateContextProperty(pCertContext
,
2404 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
2406 LPBYTE buf
= CryptMemAlloc(propSize
);
2410 if (CertGetCertificateContextProperty(pCertContext
,
2411 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
2413 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2414 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
2415 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2421 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
2423 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
2424 pCertContext
->pCertInfo
->cExtension
,
2425 pCertContext
->pCertInfo
->rgExtension
);
2429 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2430 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2431 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2436 /* If a particular location is specified, this should fail. Otherwise
2437 * it should succeed with an empty usage. (This is true on Win2k and
2438 * later, which we emulate.)
2442 SetLastError(CRYPT_E_NOT_FOUND
);
2446 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
2452 *pcbUsage
= bytesNeeded
;
2453 else if (*pcbUsage
< bytesNeeded
)
2455 SetLastError(ERROR_MORE_DATA
);
2456 *pcbUsage
= bytesNeeded
;
2461 *pcbUsage
= bytesNeeded
;
2465 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
2466 sizeof(CERT_ENHKEY_USAGE
) +
2467 usage
->cUsageIdentifier
* sizeof(LPSTR
));
2469 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
2470 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
2471 sizeof(CERT_ENHKEY_USAGE
));
2472 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2474 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2475 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2476 nextOID
+= strlen(nextOID
) + 1;
2480 pUsage
->cUsageIdentifier
= 0;
2485 TRACE("returning %d\n", ret
);
2489 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
2490 PCERT_ENHKEY_USAGE pUsage
)
2494 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
2498 CRYPT_DATA_BLOB blob
= { 0, NULL
};
2500 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
2501 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
2504 ret
= CertSetCertificateContextProperty(pCertContext
,
2505 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
2506 LocalFree(blob
.pbData
);
2510 ret
= CertSetCertificateContextProperty(pCertContext
,
2511 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
2515 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2516 LPCSTR pszUsageIdentifier
)
2521 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2523 if (CertGetEnhancedKeyUsage(pCertContext
,
2524 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
2526 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
2530 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2531 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
2535 BOOL exists
= FALSE
;
2537 /* Make sure usage doesn't already exist */
2538 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
2540 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
2541 pszUsageIdentifier
))
2546 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
2547 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2553 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
2554 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
2555 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
2556 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
2557 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2559 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2560 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2561 nextOID
+= strlen(nextOID
) + 1;
2563 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2564 strcpy(nextOID
, pszUsageIdentifier
);
2565 newUsage
->cUsageIdentifier
= i
+ 1;
2566 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
2567 CryptMemFree(newUsage
);
2573 CryptMemFree(usage
);
2580 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
2581 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2585 usage
->rgpszUsageIdentifier
=
2586 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
2587 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
2588 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
2589 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
2590 usage
->cUsageIdentifier
= 1;
2591 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
2592 CryptMemFree(usage
);
2600 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2601 LPCSTR pszUsageIdentifier
)
2605 CERT_ENHKEY_USAGE usage
;
2607 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2609 size
= sizeof(usage
);
2610 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2611 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
2612 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2614 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2618 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2619 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
2622 if (pUsage
->cUsageIdentifier
)
2627 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
2629 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
2630 pszUsageIdentifier
))
2632 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
2633 pUsage
->rgpszUsageIdentifier
[i
] =
2634 pUsage
->rgpszUsageIdentifier
[i
+ 1];
2636 pUsage
->cUsageIdentifier
--;
2637 /* Remove the usage if it's empty */
2638 if (pUsage
->cUsageIdentifier
)
2639 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
2641 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
2644 CryptMemFree(pUsage
);
2651 /* it fit in an empty usage, therefore there's nothing to remove */
2663 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
2665 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
2667 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2669 if (indexIndex
+ 1 > field
->cIndexes
)
2671 if (field
->cIndexes
)
2672 field
->indexes
= CryptMemRealloc(field
->indexes
,
2673 (indexIndex
+ 1) * sizeof(DWORD
));
2675 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
2678 field
->indexes
[indexIndex
] = 0;
2679 field
->cIndexes
= indexIndex
+ 1;
2683 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
2686 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
2689 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2691 assert(field
->cIndexes
);
2692 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
2696 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
2697 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
2700 DWORD i
, cbOIDs
= 0;
2701 BOOL allUsagesValid
= TRUE
;
2702 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
2704 TRACE("(%d, %p, %d, %p, %d)\n", cCerts
, rghCerts
, *cNumOIDs
,
2707 for (i
= 0; i
< cCerts
; i
++)
2709 CERT_ENHKEY_USAGE usage
;
2710 DWORD size
= sizeof(usage
);
2712 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
2713 /* Success is deliberately ignored: it implies all usages are valid */
2714 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2716 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2718 allUsagesValid
= FALSE
;
2721 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
2724 if (!validUsages
.cUsageIdentifier
)
2728 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
2729 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
2730 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2731 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
2733 validUsages
.rgpszUsageIdentifier
=
2734 CryptMemAlloc(cbOIDs
);
2735 if (validUsages
.rgpszUsageIdentifier
)
2737 LPSTR nextOID
= (LPSTR
)
2738 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
2739 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2741 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2743 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
2744 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
2745 pUsage
->rgpszUsageIdentifier
[j
]);
2746 nextOID
+= lstrlenA(nextOID
) + 1;
2752 struct BitField validIndexes
= { 0, NULL
};
2753 DWORD j
, k
, numRemoved
= 0;
2755 /* Merge: build a bitmap of all the indexes of
2756 * validUsages.rgpszUsageIdentifier that are in pUsage.
2758 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
2760 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
2762 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
2763 validUsages
.rgpszUsageIdentifier
[k
]))
2765 CRYPT_SetBitInField(&validIndexes
, k
);
2770 /* Merge by removing from validUsages those that are
2771 * not in the bitmap.
2773 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2775 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
2777 if (j
< validUsages
.cUsageIdentifier
- 1)
2779 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
2780 &validUsages
.rgpszUsageIdentifier
[j
+
2782 (validUsages
.cUsageIdentifier
- numRemoved
2783 - j
- 1) * sizeof(LPSTR
));
2785 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
2787 validUsages
.cUsageIdentifier
--;
2791 validUsages
.cUsageIdentifier
--;
2794 CryptMemFree(validIndexes
.indexes
);
2797 CryptMemFree(pUsage
);
2809 *cNumOIDs
= validUsages
.cUsageIdentifier
;
2812 else if (*pcbOIDs
< cbOIDs
)
2815 SetLastError(ERROR_MORE_DATA
);
2820 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
2821 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2824 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
2826 rghOIDs
[i
] = nextOID
;
2827 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
2828 nextOID
+= lstrlenA(nextOID
) + 1;
2832 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
2833 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
2834 TRACE("returning %d\n", ret
);
2838 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
2839 * pInfo is NULL, from the attributes of hProv.
2841 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
2842 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
2844 CRYPT_KEY_PROV_INFO info
= { 0 };
2852 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
2855 LPSTR szContainer
= CryptMemAlloc(size
);
2859 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
2860 (BYTE
*)szContainer
, &size
, 0);
2863 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2867 info
.pwszContainerName
= CryptMemAlloc(len
*
2869 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2870 info
.pwszContainerName
, len
);
2873 CryptMemFree(szContainer
);
2876 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
2879 LPSTR szProvider
= CryptMemAlloc(size
);
2883 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
2887 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2891 info
.pwszProvName
= CryptMemAlloc(len
*
2893 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2894 info
.pwszProvName
, len
);
2897 CryptMemFree(szProvider
);
2900 size
= sizeof(info
.dwKeySpec
);
2901 /* in case no CRYPT_KEY_PROV_INFO given,
2902 * we always use AT_SIGNATURE key spec
2904 info
.dwKeySpec
= AT_SIGNATURE
;
2905 size
= sizeof(info
.dwProvType
);
2906 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
2909 info
.dwProvType
= PROV_RSA_FULL
;
2913 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
2918 CryptMemFree(info
.pwszContainerName
);
2919 CryptMemFree(info
.pwszProvName
);
2923 /* Creates a signed certificate context from the unsigned, encoded certificate
2924 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
2926 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
2927 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
2929 PCCERT_CONTEXT context
= NULL
;
2933 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2934 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
2937 LPBYTE sig
= CryptMemAlloc(sigSize
);
2939 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2940 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
2943 CERT_SIGNED_CONTENT_INFO signedInfo
;
2944 BYTE
*encodedSignedCert
= NULL
;
2945 DWORD encodedSignedCertSize
= 0;
2947 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
2948 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
2949 memcpy(&signedInfo
.SignatureAlgorithm
, sigAlgo
,
2950 sizeof(signedInfo
.SignatureAlgorithm
));
2951 signedInfo
.Signature
.cbData
= sigSize
;
2952 signedInfo
.Signature
.pbData
= sig
;
2953 signedInfo
.Signature
.cUnusedBits
= 0;
2954 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
2955 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2956 &encodedSignedCert
, &encodedSignedCertSize
);
2959 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2960 encodedSignedCert
, encodedSignedCertSize
);
2961 LocalFree(encodedSignedCert
);
2969 /* Copies data from the parameters into info, where:
2970 * pSerialNumber: The serial number. Must not be NULL.
2971 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
2973 * pSignatureAlgorithm: Optional.
2974 * pStartTime: The starting time of the certificate. If NULL, the current
2975 * system time is used.
2976 * pEndTime: The ending time of the certificate. If NULL, one year past the
2977 * starting time is used.
2978 * pubKey: The public key of the certificate. Must not be NULL.
2979 * pExtensions: Extensions to be included with the certificate. Optional.
2981 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
2982 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
2983 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
2984 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
2985 const CERT_EXTENSIONS
*pExtensions
)
2987 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
2990 assert(pSerialNumber
);
2991 assert(pSubjectIssuerBlob
);
2994 if (pExtensions
&& pExtensions
->cExtension
)
2995 info
->dwVersion
= CERT_V3
;
2997 info
->dwVersion
= CERT_V1
;
2998 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
2999 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3000 if (pSignatureAlgorithm
)
3001 memcpy(&info
->SignatureAlgorithm
, pSignatureAlgorithm
,
3002 sizeof(info
->SignatureAlgorithm
));
3005 info
->SignatureAlgorithm
.pszObjId
= oid
;
3006 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3007 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3009 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3010 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3012 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3014 GetSystemTimeAsFileTime(&info
->NotBefore
);
3016 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3021 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3024 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3027 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3028 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3029 memcpy(&info
->SubjectPublicKeyInfo
, pubKey
,
3030 sizeof(info
->SubjectPublicKeyInfo
));
3033 info
->cExtension
= pExtensions
->cExtension
;
3034 info
->rgExtension
= pExtensions
->rgExtension
;
3038 info
->cExtension
= 0;
3039 info
->rgExtension
= NULL
;
3043 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3044 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3045 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3047 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3049 HCRYPTPROV hProv
= 0;
3050 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
3054 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3056 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3058 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3059 rpcrt
, "RpcStringFreeA");
3061 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3064 RPC_STATUS status
= uuidCreate(&uuid
);
3066 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3068 unsigned char *uuidStr
;
3070 status
= uuidToString(&uuid
, &uuidStr
);
3071 if (status
== RPC_S_OK
)
3073 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3074 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3080 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3082 CryptDestroyKey(key
);
3084 rpcStringFree(&uuidStr
);
3093 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3094 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3095 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3096 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3097 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3099 PCCERT_CONTEXT context
= NULL
;
3100 BOOL ret
, releaseContext
= FALSE
;
3101 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3102 DWORD pubKeySize
= 0,dwKeySpec
= AT_SIGNATURE
;
3104 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
3105 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3106 pExtensions
, pExtensions
);
3108 if(!pSubjectIssuerBlob
)
3110 SetLastError(ERROR_INVALID_PARAMETER
);
3118 hProv
= CRYPT_CreateKeyProv();
3119 releaseContext
= TRUE
;
3121 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3123 SetLastError(NTE_BAD_FLAGS
);
3129 /* acquire the context using the given information*/
3130 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3131 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3132 pKeyProvInfo
->dwFlags
);
3135 if(GetLastError() != NTE_BAD_KEYSET
)
3137 /* create the key set */
3138 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3139 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3140 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3144 dwKeySpec
= pKeyProvInfo
->dwKeySpec
;
3145 /* check if the key is here */
3146 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3149 if (NTE_NO_KEY
== GetLastError())
3150 { /* generate the key */
3151 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3155 CryptReleaseContext(hProv
,0);
3156 SetLastError(NTE_BAD_KEYSET
);
3160 CryptDestroyKey(hKey
);
3161 releaseContext
= TRUE
;
3164 else if (pKeyProvInfo
)
3166 SetLastError(ERROR_INVALID_PARAMETER
);
3170 CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3172 pubKey
= CryptMemAlloc(pubKeySize
);
3175 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3176 pubKey
, &pubKeySize
);
3179 CERT_INFO info
= { 0 };
3180 CRYPT_DER_BLOB blob
= { 0, NULL
};
3182 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3184 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3185 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3186 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3187 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3188 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3192 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3193 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3194 &info
.SignatureAlgorithm
);
3196 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3197 blob
.pbData
, blob
.cbData
);
3198 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3199 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3200 LocalFree(blob
.pbData
);
3203 CryptMemFree(pubKey
);
3206 CryptReleaseContext(hProv
, 0);
3210 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3211 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3212 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3213 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3215 FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType
,
3216 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3217 pVerifyUsageStatus
);
3218 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3222 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3223 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3224 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3226 TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType
, dwEncodingType
,
3227 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3231 FIXME("dwFlags 0x%08x not handled\n", dwFlags
);
3236 FIXME("pCreatePara not handled\n");
3240 switch (dwContextType
)
3242 case CERT_STORE_CERTIFICATE_CONTEXT
:
3243 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3244 case CERT_STORE_CRL_CONTEXT
:
3245 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3246 case CERT_STORE_CTL_CONTEXT
:
3247 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3249 WARN("unknown context type: 0x%x\n", dwContextType
);