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 WINECRYPT_CERTSTORE
*store
= (WINECRYPT_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 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
146 SetLastError(E_INVALIDARG
);
150 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
151 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
157 cert
= Context_CreateDataContext(sizeof(CERT_CONTEXT
));
160 data
= CryptMemAlloc(cbCertEncoded
);
163 CertFreeCertificateContext(cert
);
167 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
168 cert
->dwCertEncodingType
= dwCertEncodingType
;
169 cert
->pbCertEncoded
= data
;
170 cert
->cbCertEncoded
= cbCertEncoded
;
171 cert
->pCertInfo
= certInfo
;
172 cert
->hCertStore
= 0;
179 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(
180 PCCERT_CONTEXT pCertContext
)
182 TRACE("(%p)\n", pCertContext
);
187 Context_AddRef((void *)pCertContext
);
191 static void CertDataContext_Free(void *context
)
193 PCERT_CONTEXT certContext
= context
;
195 CryptMemFree(certContext
->pbCertEncoded
);
196 LocalFree(certContext
->pCertInfo
);
199 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
203 TRACE("(%p)\n", pCertContext
);
206 ret
= Context_Release((void *)pCertContext
, sizeof(CERT_CONTEXT
),
207 CertDataContext_Free
);
211 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
214 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(pCertContext
);
217 TRACE("(%p, %d)\n", pCertContext
, dwPropId
);
220 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
226 static BOOL
CertContext_GetHashProp(void *context
, DWORD dwPropId
,
227 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
230 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
234 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
236 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
241 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
248 else if (*pcbData
< cb
)
250 SetLastError(ERROR_MORE_DATA
);
256 memcpy(pvData
, pb
, cb
);
262 static BOOL
CertContext_GetProperty(void *context
, DWORD dwPropId
,
263 void *pvData
, DWORD
*pcbData
)
265 PCCERT_CONTEXT pCertContext
= context
;
266 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(context
);
268 CRYPT_DATA_BLOB blob
;
270 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
273 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
277 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
280 /* Implicit properties */
283 case CERT_SHA1_HASH_PROP_ID
:
284 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
285 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
288 case CERT_MD5_HASH_PROP_ID
:
289 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
290 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
293 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
294 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
295 pCertContext
->pCertInfo
->Subject
.pbData
,
296 pCertContext
->pCertInfo
->Subject
.cbData
,
299 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
300 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
301 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
302 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
305 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
306 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
307 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
308 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
311 case CERT_SIGNATURE_HASH_PROP_ID
:
312 ret
= CryptHashToBeSigned(0, pCertContext
->dwCertEncodingType
,
313 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
317 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
319 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
322 case CERT_KEY_IDENTIFIER_PROP_ID
:
324 PCERT_EXTENSION ext
= CertFindExtension(
325 szOID_SUBJECT_KEY_IDENTIFIER
, pCertContext
->pCertInfo
->cExtension
,
326 pCertContext
->pCertInfo
->rgExtension
);
330 CRYPT_DATA_BLOB value
;
331 DWORD size
= sizeof(value
);
333 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
334 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
335 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &value
,
339 ret
= CertContext_CopyParam(pvData
, pcbData
, value
.pbData
,
341 CertContext_SetProperty(context
, dwPropId
, 0, &value
);
345 SetLastError(ERROR_INVALID_DATA
);
349 SetLastError(CRYPT_E_NOT_FOUND
);
352 TRACE("returning %d\n", ret
);
356 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
358 DWORD i
, containerLen
, provNameLen
;
359 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
361 info
->pwszContainerName
= (LPWSTR
)data
;
362 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
363 data
+= containerLen
;
365 info
->pwszProvName
= (LPWSTR
)data
;
366 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
369 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
370 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
372 for (i
= 0; i
< info
->cProvParam
; i
++)
374 info
->rgProvParam
[i
].pbData
= data
;
375 data
+= info
->rgProvParam
[i
].cbData
;
379 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
380 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
384 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
389 case CERT_CERT_PROP_ID
:
390 case CERT_CRL_PROP_ID
:
391 case CERT_CTL_PROP_ID
:
392 SetLastError(E_INVALIDARG
);
395 case CERT_ACCESS_STATE_PROP_ID
:
396 if (pCertContext
->hCertStore
)
397 ret
= CertGetStoreProperty(pCertContext
->hCertStore
, dwPropId
,
403 ret
= CertContext_CopyParam(pvData
, pcbData
, &state
, sizeof(state
));
406 case CERT_KEY_PROV_HANDLE_PROP_ID
:
408 CERT_KEY_CONTEXT keyContext
;
409 DWORD size
= sizeof(keyContext
);
411 ret
= CertContext_GetProperty((void *)pCertContext
,
412 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
414 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
415 sizeof(keyContext
.hCryptProv
));
418 case CERT_KEY_PROV_INFO_PROP_ID
:
419 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
422 CRYPT_FixKeyProvInfoPointers(pvData
);
425 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
429 TRACE("returning %d\n", ret
);
433 /* Copies key provider info from from into to, where to is assumed to be a
434 * contiguous buffer of memory large enough for from and all its associated
435 * data, but whose pointers are uninitialized.
436 * Upon return, to contains a contiguous copy of from, packed in the following
438 * - CRYPT_KEY_PROV_INFO
439 * - pwszContainerName
441 * - rgProvParam[0]...
443 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
444 const CRYPT_KEY_PROV_INFO
*from
)
447 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
449 if (from
->pwszContainerName
)
451 to
->pwszContainerName
= (LPWSTR
)nextData
;
452 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
453 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
456 to
->pwszContainerName
= NULL
;
457 if (from
->pwszProvName
)
459 to
->pwszProvName
= (LPWSTR
)nextData
;
460 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
461 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
464 to
->pwszProvName
= NULL
;
465 to
->dwProvType
= from
->dwProvType
;
466 to
->dwFlags
= from
->dwFlags
;
467 to
->cProvParam
= from
->cProvParam
;
468 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
469 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
470 to
->dwKeySpec
= from
->dwKeySpec
;
471 for (i
= 0; i
< to
->cProvParam
; i
++)
473 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
474 sizeof(CRYPT_KEY_PROV_PARAM
));
475 to
->rgProvParam
[i
].pbData
= nextData
;
476 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
477 from
->rgProvParam
[i
].cbData
);
478 nextData
+= from
->rgProvParam
[i
].cbData
;
482 static BOOL
CertContext_SetKeyProvInfoProperty(CONTEXT_PROPERTY_LIST
*properties
,
483 const CRYPT_KEY_PROV_INFO
*info
)
487 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
489 if (info
->pwszContainerName
)
490 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
493 if (info
->pwszProvName
)
494 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
497 size
+= containerSize
+ provNameSize
;
498 for (i
= 0; i
< info
->cProvParam
; i
++)
499 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
500 buf
= CryptMemAlloc(size
);
503 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
504 ret
= ContextPropertyList_SetProperty(properties
,
505 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
513 static BOOL
CertContext_SetProperty(void *context
, DWORD dwPropId
,
514 DWORD dwFlags
, const void *pvData
)
516 CONTEXT_PROPERTY_LIST
*properties
= Context_GetProperties(context
);
519 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
527 case CERT_AUTO_ENROLL_PROP_ID
:
528 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
529 case CERT_DESCRIPTION_PROP_ID
:
530 case CERT_FRIENDLY_NAME_PROP_ID
:
531 case CERT_HASH_PROP_ID
:
532 case CERT_KEY_IDENTIFIER_PROP_ID
:
533 case CERT_MD5_HASH_PROP_ID
:
534 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
535 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
536 case CERT_PVK_FILE_PROP_ID
:
537 case CERT_SIGNATURE_HASH_PROP_ID
:
538 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
539 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
540 case CERT_EXTENDED_ERROR_INFO_PROP_ID
:
541 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
542 case CERT_ENROLLMENT_PROP_ID
:
543 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
544 case CERT_RENEWAL_PROP_ID
:
548 const CRYPT_DATA_BLOB
*blob
= pvData
;
550 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
551 blob
->pbData
, blob
->cbData
);
555 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
560 case CERT_DATE_STAMP_PROP_ID
:
562 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
563 pvData
, sizeof(FILETIME
));
566 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
570 case CERT_KEY_CONTEXT_PROP_ID
:
574 const CERT_KEY_CONTEXT
*keyContext
= pvData
;
576 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
578 SetLastError(E_INVALIDARG
);
582 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
583 (const BYTE
*)keyContext
, keyContext
->cbSize
);
587 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
592 case CERT_KEY_PROV_INFO_PROP_ID
:
594 ret
= CertContext_SetKeyProvInfoProperty(properties
, pvData
);
597 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
601 case CERT_KEY_PROV_HANDLE_PROP_ID
:
603 CERT_KEY_CONTEXT keyContext
;
604 DWORD size
= sizeof(keyContext
);
606 ret
= CertContext_GetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
610 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
611 CryptReleaseContext(keyContext
.hCryptProv
, 0);
613 keyContext
.cbSize
= sizeof(keyContext
);
615 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
618 keyContext
.hCryptProv
= 0;
619 keyContext
.dwKeySpec
= AT_SIGNATURE
;
621 ret
= CertContext_SetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
626 FIXME("%d: stub\n", dwPropId
);
630 TRACE("returning %d\n", ret
);
634 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
635 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
639 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
641 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
642 * crashes on most of these, I'll be safer.
647 case CERT_ACCESS_STATE_PROP_ID
:
648 case CERT_CERT_PROP_ID
:
649 case CERT_CRL_PROP_ID
:
650 case CERT_CTL_PROP_ID
:
651 SetLastError(E_INVALIDARG
);
654 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
656 TRACE("returning %d\n", ret
);
660 /* Acquires the private key using the key provider info, retrieving info from
661 * the certificate if info is NULL. The acquired provider is returned in
662 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
664 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
665 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
668 BOOL allocated
= FALSE
, ret
= TRUE
;
672 ret
= CertGetCertificateContextProperty(pCert
,
673 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
676 info
= HeapAlloc(GetProcessHeap(), 0, size
);
679 ret
= CertGetCertificateContextProperty(pCert
,
680 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
685 SetLastError(ERROR_OUTOFMEMORY
);
690 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
694 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
695 info
->pwszProvName
, info
->dwProvType
, 0);
700 for (i
= 0; i
< info
->cProvParam
; i
++)
702 CryptSetProvParam(*phCryptProv
,
703 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
704 info
->rgProvParam
[i
].dwFlags
);
706 *pdwKeySpec
= info
->dwKeySpec
;
709 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
712 HeapFree(GetProcessHeap(), 0, info
);
716 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
717 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
718 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
720 BOOL ret
= FALSE
, cache
= FALSE
;
721 PCRYPT_KEY_PROV_INFO info
= NULL
;
722 CERT_KEY_CONTEXT keyContext
;
725 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
726 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
728 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
732 ret
= CertGetCertificateContextProperty(pCert
,
733 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
736 info
= HeapAlloc(GetProcessHeap(), 0, size
);
737 ret
= CertGetCertificateContextProperty(pCert
,
738 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
740 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
743 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
748 size
= sizeof(keyContext
);
749 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
753 *phCryptProv
= keyContext
.hCryptProv
;
755 *pdwKeySpec
= keyContext
.dwKeySpec
;
756 if (pfCallerFreeProv
)
757 *pfCallerFreeProv
= !cache
;
762 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
763 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
766 *phCryptProv
= keyContext
.hCryptProv
;
768 *pdwKeySpec
= keyContext
.dwKeySpec
;
771 keyContext
.cbSize
= sizeof(keyContext
);
772 if (CertSetCertificateContextProperty(pCert
,
773 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
775 if (pfCallerFreeProv
)
776 *pfCallerFreeProv
= FALSE
;
781 if (pfCallerFreeProv
)
782 *pfCallerFreeProv
= TRUE
;
786 HeapFree(GetProcessHeap(), 0, info
);
790 static BOOL
key_prov_info_matches_cert(PCCERT_CONTEXT pCert
,
791 const CRYPT_KEY_PROV_INFO
*keyProvInfo
)
794 BOOL matches
= FALSE
;
796 if (CryptAcquireContextW(&csp
, keyProvInfo
->pwszContainerName
,
797 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
, keyProvInfo
->dwFlags
))
801 /* Need to sign something to verify the sig. What to sign? Why not
802 * the certificate itself?
804 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
805 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
, pCert
->pCertInfo
,
806 &pCert
->pCertInfo
->SignatureAlgorithm
, NULL
, NULL
, &size
))
808 BYTE
*certEncoded
= CryptMemAlloc(size
);
812 if (CryptSignAndEncodeCertificate(csp
, AT_SIGNATURE
,
813 pCert
->dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
814 pCert
->pCertInfo
, &pCert
->pCertInfo
->SignatureAlgorithm
,
815 NULL
, certEncoded
, &size
))
817 if (size
== pCert
->cbCertEncoded
&&
818 !memcmp(certEncoded
, pCert
->pbCertEncoded
, size
))
821 CryptMemFree(certEncoded
);
824 CryptReleaseContext(csp
, 0);
829 static BOOL
container_matches_cert(PCCERT_CONTEXT pCert
, LPCSTR container
,
830 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
832 CRYPT_KEY_PROV_INFO copy
;
833 WCHAR containerW
[MAX_PATH
];
834 BOOL matches
= FALSE
;
836 MultiByteToWideChar(CP_ACP
, 0, container
, -1,
837 containerW
, sizeof(containerW
) / sizeof(containerW
[0]));
838 /* We make a copy of the CRYPT_KEY_PROV_INFO because the caller expects
839 * keyProvInfo->pwszContainerName to be NULL or a heap-allocated container
843 copy
.pwszContainerName
= containerW
;
844 matches
= key_prov_info_matches_cert(pCert
, ©
);
847 keyProvInfo
->pwszContainerName
=
848 CryptMemAlloc((strlenW(containerW
) + 1) * sizeof(WCHAR
));
849 if (keyProvInfo
->pwszContainerName
)
851 strcpyW(keyProvInfo
->pwszContainerName
, containerW
);
852 keyProvInfo
->dwKeySpec
= AT_SIGNATURE
;
860 /* Searches the provider named keyProvInfo.pwszProvName for a container whose
861 * private key matches pCert's public key. Upon success, updates keyProvInfo
862 * with the matching container's info (free keyProvInfo.pwszContainerName upon
864 * Returns TRUE if found, FALSE if not.
866 static BOOL
find_key_prov_info_in_provider(PCCERT_CONTEXT pCert
,
867 CRYPT_KEY_PROV_INFO
*keyProvInfo
)
869 HCRYPTPROV defProvider
;
870 BOOL ret
, found
= FALSE
;
871 char containerA
[MAX_PATH
];
873 assert(keyProvInfo
->pwszContainerName
== NULL
);
874 if ((ret
= CryptAcquireContextW(&defProvider
, NULL
,
875 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
876 keyProvInfo
->dwFlags
| CRYPT_VERIFYCONTEXT
)))
878 DWORD enumFlags
= keyProvInfo
->dwFlags
| CRYPT_FIRST
;
880 while (ret
&& !found
)
882 DWORD size
= sizeof(containerA
);
884 ret
= CryptGetProvParam(defProvider
, PP_ENUMCONTAINERS
,
885 (BYTE
*)containerA
, &size
, enumFlags
);
887 found
= container_matches_cert(pCert
, containerA
, keyProvInfo
);
888 if (enumFlags
& CRYPT_FIRST
)
890 enumFlags
&= ~CRYPT_FIRST
;
891 enumFlags
|= CRYPT_NEXT
;
894 CryptReleaseContext(defProvider
, 0);
899 static BOOL
find_matching_provider(PCCERT_CONTEXT pCert
, DWORD dwFlags
)
901 BOOL found
= FALSE
, ret
= TRUE
;
902 DWORD index
= 0, cbProvName
= 0;
903 CRYPT_KEY_PROV_INFO keyProvInfo
;
905 TRACE("(%p, %08x)\n", pCert
, dwFlags
);
907 memset(&keyProvInfo
, 0, sizeof(keyProvInfo
));
908 while (ret
&& !found
)
912 ret
= CryptEnumProvidersW(index
, NULL
, 0, &keyProvInfo
.dwProvType
,
916 if (size
<= cbProvName
)
917 ret
= CryptEnumProvidersW(index
, NULL
, 0,
918 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
921 CryptMemFree(keyProvInfo
.pwszProvName
);
922 keyProvInfo
.pwszProvName
= CryptMemAlloc(size
);
923 if (keyProvInfo
.pwszProvName
)
926 ret
= CryptEnumProvidersW(index
, NULL
, 0,
927 &keyProvInfo
.dwProvType
, keyProvInfo
.pwszProvName
, &size
);
930 if (dwFlags
& CRYPT_FIND_SILENT_KEYSET_FLAG
)
931 keyProvInfo
.dwFlags
|= CRYPT_SILENT
;
932 if (dwFlags
& CRYPT_FIND_USER_KEYSET_FLAG
||
933 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
934 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
936 keyProvInfo
.dwFlags
|= CRYPT_USER_KEYSET
;
937 found
= find_key_prov_info_in_provider(pCert
,
942 if (dwFlags
& CRYPT_FIND_MACHINE_KEYSET_FLAG
||
943 !(dwFlags
& (CRYPT_FIND_USER_KEYSET_FLAG
|
944 CRYPT_FIND_MACHINE_KEYSET_FLAG
)))
946 keyProvInfo
.dwFlags
&= ~CRYPT_USER_KEYSET
;
947 keyProvInfo
.dwFlags
|= CRYPT_MACHINE_KEYSET
;
948 found
= find_key_prov_info_in_provider(pCert
,
961 CertSetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
963 CryptMemFree(keyProvInfo
.pwszProvName
);
964 CryptMemFree(keyProvInfo
.pwszContainerName
);
968 static BOOL
cert_prov_info_matches_cert(PCCERT_CONTEXT pCert
)
970 BOOL matches
= FALSE
;
973 if (CertGetCertificateContextProperty(pCert
, CERT_KEY_PROV_INFO_PROP_ID
,
976 CRYPT_KEY_PROV_INFO
*keyProvInfo
= CryptMemAlloc(size
);
980 if (CertGetCertificateContextProperty(pCert
,
981 CERT_KEY_PROV_INFO_PROP_ID
, keyProvInfo
, &size
))
982 matches
= key_prov_info_matches_cert(pCert
, keyProvInfo
);
983 CryptMemFree(keyProvInfo
);
989 BOOL WINAPI
CryptFindCertificateKeyProvInfo(PCCERT_CONTEXT pCert
,
990 DWORD dwFlags
, void *pvReserved
)
992 BOOL matches
= FALSE
;
994 TRACE("(%p, %08x, %p)\n", pCert
, dwFlags
, pvReserved
);
996 matches
= cert_prov_info_matches_cert(pCert
);
998 matches
= find_matching_provider(pCert
, dwFlags
);
1002 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
1003 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
1007 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
1009 ret
= CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
1010 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
1011 &pCertId2
->SerialNumber
);
1012 TRACE("returning %d\n", ret
);
1016 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
1017 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
1021 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
1023 if (pCertName1
->cbData
== pCertName2
->cbData
)
1025 if (pCertName1
->cbData
)
1026 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
1027 pCertName1
->cbData
);
1033 TRACE("returning %d\n", ret
);
1037 /* Returns the number of significant bytes in pInt, where a byte is
1038 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
1039 * for negative numbers. pInt is assumed to be little-endian.
1041 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
1043 DWORD ret
= pInt
->cbData
;
1047 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
1049 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
1057 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
1058 PCRYPT_INTEGER_BLOB pInt2
)
1063 TRACE("(%p, %p)\n", pInt1
, pInt2
);
1065 cb1
= CRYPT_significantBytes(pInt1
);
1066 cb2
= CRYPT_significantBytes(pInt2
);
1070 ret
= !memcmp(pInt1
->pbData
, pInt2
->pbData
, cb1
);
1076 TRACE("returning %d\n", ret
);
1080 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
1081 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
1085 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
1087 switch (GET_CERT_ENCODING_TYPE(dwCertEncodingType
))
1089 case 0: /* Seems to mean "raw binary bits" */
1090 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
1091 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
1093 if (pPublicKey2
->PublicKey
.cbData
)
1094 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
1095 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
1103 WARN("Unknown encoding type %08x\n", dwCertEncodingType
);
1105 case X509_ASN_ENCODING
:
1107 BLOBHEADER
*pblob1
, *pblob2
;
1110 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1111 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1114 pblob1
= CryptMemAlloc(length
);
1115 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1116 pPublicKey1
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
,
1117 0, pblob1
, &length
))
1119 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1120 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1123 pblob2
= CryptMemAlloc(length
);
1124 if (CryptDecodeObject(dwCertEncodingType
, RSA_CSP_PUBLICKEYBLOB
,
1125 pPublicKey2
->PublicKey
.pbData
, pPublicKey2
->PublicKey
.cbData
,
1126 0, pblob2
, &length
))
1128 /* The RSAPUBKEY structure directly follows the BLOBHEADER */
1129 RSAPUBKEY
*pk1
= (LPVOID
)(pblob1
+ 1),
1130 *pk2
= (LPVOID
)(pblob2
+ 1);
1131 ret
= (pk1
->bitlen
== pk2
->bitlen
) && (pk1
->pubexp
== pk2
->pubexp
)
1132 && !memcmp(pk1
+ 1, pk2
+ 1, pk1
->bitlen
/8);
1134 CryptMemFree(pblob2
);
1137 CryptMemFree(pblob1
);
1146 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
1147 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1151 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
1153 if (GET_CERT_ENCODING_TYPE(dwCertEncodingType
) != X509_ASN_ENCODING
)
1155 SetLastError(ERROR_FILE_NOT_FOUND
);
1158 if (pPublicKey
->Algorithm
.pszObjId
&&
1159 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
1161 FIXME("unimplemented for DH public keys\n");
1162 SetLastError(CRYPT_E_ASN1_BADTAG
);
1168 BOOL ret
= CryptDecodeObjectEx(dwCertEncodingType
,
1169 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
1170 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
1175 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)(buf
+ sizeof(BLOBHEADER
));
1177 len
= rsaPubKey
->bitlen
;
1184 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1185 DWORD dwFlags
, const void *pvPara
);
1187 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1188 DWORD dwFlags
, const void *pvPara
)
1192 DWORD size
= sizeof(hash
);
1194 ret
= CertGetCertificateContextProperty(pCertContext
,
1195 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
1198 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1200 if (size
== pHash
->cbData
)
1201 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1208 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1209 DWORD dwFlags
, const void *pvPara
)
1213 DWORD size
= sizeof(hash
);
1215 ret
= CertGetCertificateContextProperty(pCertContext
,
1216 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
1219 const CRYPT_HASH_BLOB
*pHash
= pvPara
;
1221 if (size
== pHash
->cbData
)
1222 ret
= !memcmp(pHash
->pbData
, hash
, size
);
1229 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1230 DWORD dwFlags
, const void *pvPara
)
1232 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
1235 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1236 toCompare
= &pCertContext
->pCertInfo
->Subject
;
1238 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
1239 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1244 static BOOL
compare_cert_by_public_key(PCCERT_CONTEXT pCertContext
,
1245 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1247 CERT_PUBLIC_KEY_INFO
*publicKey
= (CERT_PUBLIC_KEY_INFO
*)pvPara
;
1250 ret
= CertComparePublicKeyInfo(pCertContext
->dwCertEncodingType
,
1251 &pCertContext
->pCertInfo
->SubjectPublicKeyInfo
, publicKey
);
1255 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
1256 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1258 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
1261 /* Matching serial number and subject match.. */
1262 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1263 &pCertContext
->pCertInfo
->Subject
, &pCertInfo
->Issuer
);
1265 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1266 &pCertInfo
->SerialNumber
);
1269 /* failing that, if the serial number and issuer match, we match */
1270 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1271 &pCertInfo
->SerialNumber
);
1273 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1274 &pCertContext
->pCertInfo
->Issuer
, &pCertInfo
->Issuer
);
1276 TRACE("returning %d\n", ret
);
1280 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1281 DWORD dwFlags
, const void *pvPara
)
1283 CERT_ID
*id
= (CERT_ID
*)pvPara
;
1286 switch (id
->dwIdChoice
)
1288 case CERT_ID_ISSUER_SERIAL_NUMBER
:
1289 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
1290 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
1292 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
1293 &id
->u
.IssuerSerialNumber
.SerialNumber
);
1295 case CERT_ID_SHA1_HASH
:
1296 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
1299 case CERT_ID_KEY_IDENTIFIER
:
1303 ret
= CertGetCertificateContextProperty(pCertContext
,
1304 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
1305 if (ret
&& size
== id
->u
.KeyId
.cbData
)
1307 LPBYTE buf
= CryptMemAlloc(size
);
1311 CertGetCertificateContextProperty(pCertContext
,
1312 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
1313 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
1328 static BOOL
compare_existing_cert(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1329 DWORD dwFlags
, const void *pvPara
)
1331 PCCERT_CONTEXT toCompare
= pvPara
;
1332 return CertCompareCertificate(pCertContext
->dwCertEncodingType
,
1333 pCertContext
->pCertInfo
, toCompare
->pCertInfo
);
1336 static BOOL
compare_cert_by_signature_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1337 DWORD dwFlags
, const void *pvPara
)
1339 const CRYPT_HASH_BLOB
*hash
= pvPara
;
1343 ret
= CertGetCertificateContextProperty(pCertContext
,
1344 CERT_SIGNATURE_HASH_PROP_ID
, NULL
, &size
);
1345 if (ret
&& size
== hash
->cbData
)
1347 LPBYTE buf
= CryptMemAlloc(size
);
1351 CertGetCertificateContextProperty(pCertContext
,
1352 CERT_SIGNATURE_HASH_PROP_ID
, buf
, &size
);
1353 ret
= !memcmp(buf
, hash
->pbData
, size
);
1362 static inline PCCERT_CONTEXT
cert_compare_certs_in_store(HCERTSTORE store
,
1363 PCCERT_CONTEXT prev
, CertCompareFunc compare
, DWORD dwType
, DWORD dwFlags
,
1366 BOOL matches
= FALSE
;
1371 ret
= CertEnumCertificatesInStore(store
, ret
);
1373 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1374 } while (ret
!= NULL
&& !matches
);
1378 typedef PCCERT_CONTEXT (*CertFindFunc
)(HCERTSTORE store
, DWORD dwType
,
1379 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
);
1381 static PCCERT_CONTEXT
find_cert_any(HCERTSTORE store
, DWORD dwType
,
1382 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1384 return CertEnumCertificatesInStore(store
, prev
);
1387 static PCCERT_CONTEXT
find_cert_by_issuer(HCERTSTORE store
, DWORD dwType
,
1388 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1391 PCCERT_CONTEXT found
= NULL
, subject
= pvPara
;
1392 PCERT_EXTENSION ext
;
1395 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
1396 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1398 CERT_AUTHORITY_KEY_ID_INFO
*info
;
1400 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1401 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1402 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1408 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
1410 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1411 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
1412 sizeof(CERT_NAME_BLOB
));
1413 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1414 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
1416 else if (info
->KeyId
.cbData
)
1418 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1419 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1424 found
= cert_compare_certs_in_store(store
, prev
,
1425 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1429 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
1430 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
1432 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
1434 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
1435 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1436 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1442 if (info
->AuthorityCertIssuer
.cAltEntry
&&
1443 info
->AuthorityCertSerialNumber
.cbData
)
1445 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
1448 for (i
= 0; !directoryName
&&
1449 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
1450 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
1451 == CERT_ALT_NAME_DIRECTORY_NAME
)
1453 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
1456 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
1457 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
1458 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
1459 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
1460 &info
->AuthorityCertSerialNumber
,
1461 sizeof(CRYPT_INTEGER_BLOB
));
1465 FIXME("no supported name type in authority key id2\n");
1469 else if (info
->KeyId
.cbData
)
1471 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
1472 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
1477 found
= cert_compare_certs_in_store(store
, prev
,
1478 compare_cert_by_cert_id
, dwType
, dwFlags
, &id
);
1483 found
= cert_compare_certs_in_store(store
, prev
,
1484 compare_cert_by_name
, CERT_COMPARE_NAME
| CERT_COMPARE_SUBJECT_CERT
,
1485 dwFlags
, &subject
->pCertInfo
->Issuer
);
1489 static BOOL
compare_cert_by_name_str(PCCERT_CONTEXT pCertContext
,
1490 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
1492 PCERT_NAME_BLOB name
;
1496 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
1497 name
= &pCertContext
->pCertInfo
->Subject
;
1499 name
= &pCertContext
->pCertInfo
->Issuer
;
1500 len
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1501 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1504 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1510 CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1511 CERT_SIMPLE_NAME_STR
, str
, len
);
1512 for (ptr
= str
; *ptr
; ptr
++)
1513 *ptr
= tolowerW(*ptr
);
1514 if (strstrW(str
, pvPara
))
1522 static PCCERT_CONTEXT
find_cert_by_name_str_a(HCERTSTORE store
, DWORD dwType
,
1523 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1525 PCCERT_CONTEXT found
= NULL
;
1527 TRACE("%s\n", debugstr_a(pvPara
));
1531 int len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
1532 LPWSTR str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1538 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, str
, len
);
1539 for (ptr
= str
; *ptr
; ptr
++)
1540 *ptr
= tolowerW(*ptr
);
1541 found
= cert_compare_certs_in_store(store
, prev
,
1542 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1547 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1551 static PCCERT_CONTEXT
find_cert_by_name_str_w(HCERTSTORE store
, DWORD dwType
,
1552 DWORD dwFlags
, const void *pvPara
, PCCERT_CONTEXT prev
)
1554 PCCERT_CONTEXT found
= NULL
;
1556 TRACE("%s\n", debugstr_w(pvPara
));
1560 DWORD len
= strlenW(pvPara
);
1561 LPWSTR str
= CryptMemAlloc((len
+ 1) * sizeof(WCHAR
));
1568 for (src
= pvPara
, dst
= str
; *src
; src
++, dst
++)
1569 *dst
= tolowerW(*src
);
1571 found
= cert_compare_certs_in_store(store
, prev
,
1572 compare_cert_by_name_str
, dwType
, dwFlags
, str
);
1577 found
= find_cert_any(store
, dwType
, dwFlags
, NULL
, prev
);
1581 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
1582 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
1583 PCCERT_CONTEXT pPrevCertContext
)
1586 CertFindFunc find
= NULL
;
1587 CertCompareFunc compare
= NULL
;
1589 TRACE("(%p, %08x, %08x, %08x, %p, %p)\n", hCertStore
, dwCertEncodingType
,
1590 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1592 switch (dwType
>> CERT_COMPARE_SHIFT
)
1594 case CERT_COMPARE_ANY
:
1595 find
= find_cert_any
;
1597 case CERT_COMPARE_MD5_HASH
:
1598 compare
= compare_cert_by_md5_hash
;
1600 case CERT_COMPARE_SHA1_HASH
:
1601 compare
= compare_cert_by_sha1_hash
;
1603 case CERT_COMPARE_NAME
:
1604 compare
= compare_cert_by_name
;
1606 case CERT_COMPARE_PUBLIC_KEY
:
1607 compare
= compare_cert_by_public_key
;
1609 case CERT_COMPARE_NAME_STR_A
:
1610 find
= find_cert_by_name_str_a
;
1612 case CERT_COMPARE_NAME_STR_W
:
1613 find
= find_cert_by_name_str_w
;
1615 case CERT_COMPARE_SUBJECT_CERT
:
1616 compare
= compare_cert_by_subject_cert
;
1618 case CERT_COMPARE_CERT_ID
:
1619 compare
= compare_cert_by_cert_id
;
1621 case CERT_COMPARE_ISSUER_OF
:
1622 find
= find_cert_by_issuer
;
1624 case CERT_COMPARE_EXISTING
:
1625 compare
= compare_existing_cert
;
1627 case CERT_COMPARE_SIGNATURE_HASH
:
1628 compare
= compare_cert_by_signature_hash
;
1631 FIXME("find type %08x unimplemented\n", dwType
);
1635 ret
= find(hCertStore
, dwFlags
, dwType
, pvPara
, pPrevCertContext
);
1637 ret
= cert_compare_certs_in_store(hCertStore
, pPrevCertContext
,
1638 compare
, dwType
, dwFlags
, pvPara
);
1642 SetLastError(CRYPT_E_NOT_FOUND
);
1643 TRACE("returning %p\n", ret
);
1647 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1648 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1650 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1654 SetLastError(E_INVALIDARG
);
1657 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1658 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1661 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1662 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1664 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1665 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1667 if (*pdwFlags
& ~supportedFlags
)
1669 SetLastError(E_INVALIDARG
);
1672 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1675 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1678 /* FIXME: what if the CRL has expired? */
1681 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1682 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1683 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1686 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1688 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1690 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1691 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1693 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1695 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1696 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1697 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1698 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1703 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1704 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1709 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1710 pPrevIssuerContext
, *pdwFlags
);
1712 if (!pSubjectContext
)
1714 SetLastError(E_INVALIDARG
);
1718 ret
= CertFindCertificateInStore(hCertStore
,
1719 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1720 pSubjectContext
, pPrevIssuerContext
);
1723 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1726 CertFreeCertificateContext(ret
);
1730 TRACE("returning %p\n", ret
);
1734 typedef struct _OLD_CERT_REVOCATION_STATUS
{
1739 } OLD_CERT_REVOCATION_STATUS
;
1741 typedef BOOL (WINAPI
*CertVerifyRevocationFunc
)(DWORD
, DWORD
, DWORD
,
1742 void **, DWORD
, PCERT_REVOCATION_PARA
, PCERT_REVOCATION_STATUS
);
1744 BOOL WINAPI
CertVerifyRevocation(DWORD dwEncodingType
, DWORD dwRevType
,
1745 DWORD cContext
, PVOID rgpvContext
[], DWORD dwFlags
,
1746 PCERT_REVOCATION_PARA pRevPara
, PCERT_REVOCATION_STATUS pRevStatus
)
1750 TRACE("(%08x, %d, %d, %p, %08x, %p, %p)\n", dwEncodingType
, dwRevType
,
1751 cContext
, rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1753 if (pRevStatus
->cbSize
!= sizeof(OLD_CERT_REVOCATION_STATUS
) &&
1754 pRevStatus
->cbSize
!= sizeof(CERT_REVOCATION_STATUS
))
1756 SetLastError(E_INVALIDARG
);
1761 static HCRYPTOIDFUNCSET set
= NULL
;
1765 set
= CryptInitOIDFunctionSet(CRYPT_OID_VERIFY_REVOCATION_FUNC
, 0);
1766 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
, NULL
, &size
);
1772 SetLastError(CRYPT_E_NO_REVOCATION_DLL
);
1777 LPWSTR dllList
= CryptMemAlloc(size
* sizeof(WCHAR
)), ptr
;
1781 ret
= CryptGetDefaultOIDDllList(set
, dwEncodingType
,
1785 for (ptr
= dllList
; ret
&& *ptr
;
1786 ptr
+= lstrlenW(ptr
) + 1)
1788 CertVerifyRevocationFunc func
;
1789 HCRYPTOIDFUNCADDR hFunc
;
1791 ret
= CryptGetDefaultOIDFunctionAddress(set
,
1792 dwEncodingType
, ptr
, 0, (void **)&func
, &hFunc
);
1795 ret
= func(dwEncodingType
, dwRevType
, cContext
,
1796 rgpvContext
, dwFlags
, pRevPara
, pRevStatus
);
1797 CryptFreeOIDFunctionAddress(hFunc
, 0);
1801 CryptMemFree(dllList
);
1805 SetLastError(ERROR_OUTOFMEMORY
);
1816 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
1817 CRYPT_ATTRIBUTE rgAttr
[])
1819 PCRYPT_ATTRIBUTE ret
= NULL
;
1822 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
1828 SetLastError(ERROR_INVALID_PARAMETER
);
1832 for (i
= 0; !ret
&& i
< cAttr
; i
++)
1833 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
1838 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
1839 CERT_EXTENSION rgExtensions
[])
1841 PCERT_EXTENSION ret
= NULL
;
1844 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
1850 SetLastError(ERROR_INVALID_PARAMETER
);
1854 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
1855 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
1856 rgExtensions
[i
].pszObjId
))
1857 ret
= &rgExtensions
[i
];
1861 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
1863 PCERT_RDN_ATTR ret
= NULL
;
1866 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
1870 SetLastError(ERROR_INVALID_PARAMETER
);
1874 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
1875 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
1876 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
1877 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
1878 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
1882 static BOOL
find_matching_rdn_attr(DWORD dwFlags
, const CERT_NAME_INFO
*name
,
1883 const CERT_RDN_ATTR
*attr
)
1888 for (i
= 0; !match
&& i
< name
->cRDN
; i
++)
1890 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
1892 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
1894 name
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
==
1897 if (dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
)
1900 (LPCWSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1901 LPCWSTR attrStr
= (LPCWSTR
)attr
->Value
.pbData
;
1903 if (attr
->Value
.cbData
!=
1904 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1906 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1907 match
= !strncmpiW(nameStr
, attrStr
,
1908 attr
->Value
.cbData
/ sizeof(WCHAR
));
1910 match
= !strncmpW(nameStr
, attrStr
,
1911 attr
->Value
.cbData
/ sizeof(WCHAR
));
1912 TRACE("%s : %s => %d\n",
1913 debugstr_wn(nameStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1914 debugstr_wn(attrStr
, attr
->Value
.cbData
/ sizeof(WCHAR
)),
1920 (LPCSTR
)name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
;
1921 LPCSTR attrStr
= (LPCSTR
)attr
->Value
.pbData
;
1923 if (attr
->Value
.cbData
!=
1924 name
->rgRDN
[i
].rgRDNAttr
[j
].Value
.cbData
)
1926 else if (dwFlags
& CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
)
1927 match
= !strncasecmp(nameStr
, attrStr
,
1928 attr
->Value
.cbData
);
1930 match
= !strncmp(nameStr
, attrStr
, attr
->Value
.cbData
);
1931 TRACE("%s : %s => %d\n",
1932 debugstr_an(nameStr
, attr
->Value
.cbData
),
1933 debugstr_an(attrStr
, attr
->Value
.cbData
), match
);
1941 BOOL WINAPI
CertIsRDNAttrsInCertificateName(DWORD dwCertEncodingType
,
1942 DWORD dwFlags
, PCERT_NAME_BLOB pCertName
, PCERT_RDN pRDN
)
1944 CERT_NAME_INFO
*name
;
1949 TRACE("(%08x, %08x, %p, %p)\n", dwCertEncodingType
, dwFlags
, pCertName
,
1952 type
= dwFlags
& CERT_UNICODE_IS_RDN_ATTRS_FLAG
? X509_UNICODE_NAME
:
1954 if ((ret
= CryptDecodeObjectEx(dwCertEncodingType
, type
, pCertName
->pbData
,
1955 pCertName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &name
, &size
)))
1959 for (i
= 0; ret
&& i
< pRDN
->cRDNAttr
; i
++)
1960 ret
= find_matching_rdn_attr(dwFlags
, name
, &pRDN
->rgRDNAttr
[i
]);
1962 SetLastError(CRYPT_E_NO_MATCH
);
1968 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
1969 PCERT_INFO pCertInfo
)
1976 GetSystemTimeAsFileTime(&fileTime
);
1977 pTimeToVerify
= &fileTime
;
1979 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
1981 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
1988 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
1989 PCERT_INFO pIssuerInfo
)
1991 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
1993 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
1994 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
1997 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
1998 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
1999 DWORD
*pcbComputedHash
)
2002 HCRYPTHASH hHash
= 0;
2004 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2005 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
2008 hCryptProv
= CRYPT_GetDefaultProvider();
2013 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2016 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
2018 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2019 pcbComputedHash
, 0);
2020 CryptDestroyHash(hHash
);
2026 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
2027 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
2028 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2031 HCRYPTHASH hHash
= 0;
2033 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
2034 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
2037 hCryptProv
= CRYPT_GetDefaultProvider();
2040 if ((dwCertEncodingType
& CERT_ENCODING_TYPE_MASK
) != X509_ASN_ENCODING
)
2042 SetLastError(ERROR_FILE_NOT_FOUND
);
2050 ret
= CRYPT_AsnEncodePubKeyInfoNoNull(dwCertEncodingType
,
2051 X509_PUBLIC_KEY_INFO
, pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2052 (LPBYTE
)&buf
, &size
);
2055 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
2058 ret
= CryptHashData(hHash
, buf
, size
, 0);
2060 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2061 pcbComputedHash
, 0);
2062 CryptDestroyHash(hHash
);
2070 BOOL WINAPI
CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv
,
2071 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2072 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
2075 CERT_SIGNED_CONTENT_INFO
*info
;
2078 TRACE("(%08lx, %08x, %p, %d, %p, %d)\n", hCryptProv
, dwCertEncodingType
,
2079 pbEncoded
, cbEncoded
, pbComputedHash
, *pcbComputedHash
);
2081 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2082 pbEncoded
, cbEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &size
);
2085 PCCRYPT_OID_INFO oidInfo
;
2089 hCryptProv
= CRYPT_GetDefaultProvider();
2090 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2091 info
->SignatureAlgorithm
.pszObjId
, 0);
2094 SetLastError(NTE_BAD_ALGID
);
2099 ret
= CryptCreateHash(hCryptProv
, oidInfo
->u
.Algid
, 0, 0, &hHash
);
2102 ret
= CryptHashData(hHash
, info
->ToBeSigned
.pbData
,
2103 info
->ToBeSigned
.cbData
, 0);
2105 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
2106 pcbComputedHash
, 0);
2107 CryptDestroyHash(hHash
);
2115 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2116 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
2117 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2118 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
2121 PCCRYPT_OID_INFO info
;
2124 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
2125 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
2126 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
2128 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2129 pSignatureAlgorithm
->pszObjId
, 0);
2132 SetLastError(NTE_BAD_ALGID
);
2135 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
2138 hCryptProv
= CRYPT_GetDefaultProvider();
2139 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2142 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2143 cbEncodedToBeSigned
, 0);
2145 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
2147 CryptDestroyHash(hHash
);
2154 SetLastError(ERROR_INVALID_PARAMETER
);
2159 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
2162 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
2163 cbEncodedToBeSigned
, 0);
2165 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
2167 CryptDestroyHash(hHash
);
2174 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
2175 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
2176 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
2177 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
2180 DWORD encodedSize
, hashSize
;
2182 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
2183 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
2184 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
2186 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
2187 NULL
, &encodedSize
);
2190 PBYTE encoded
= CryptMemAlloc(encodedSize
);
2194 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
2195 pvStructInfo
, encoded
, &encodedSize
);
2198 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2199 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
2200 pvHashAuxInfo
, NULL
, &hashSize
);
2203 PBYTE hash
= CryptMemAlloc(hashSize
);
2207 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
2208 dwCertEncodingType
, encoded
, encodedSize
,
2209 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
2212 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
2214 info
.ToBeSigned
.cbData
= encodedSize
;
2215 info
.ToBeSigned
.pbData
= encoded
;
2216 info
.SignatureAlgorithm
= *pSignatureAlgorithm
;
2217 info
.Signature
.cbData
= hashSize
;
2218 info
.Signature
.pbData
= hash
;
2219 info
.Signature
.cUnusedBits
= 0;
2220 ret
= CryptEncodeObject(dwCertEncodingType
,
2221 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
2227 CryptMemFree(encoded
);
2233 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
2234 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
2235 PCERT_PUBLIC_KEY_INFO pPublicKey
)
2237 CRYPT_DATA_BLOB blob
= { cbEncoded
, (BYTE
*)pbEncoded
};
2239 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
2240 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, &blob
,
2241 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
2244 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
,
2245 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pubKeyInfo
,
2246 const CERT_SIGNED_CONTENT_INFO
*signedCert
)
2250 PCCRYPT_OID_INFO info
;
2251 ALG_ID pubKeyID
, hashID
;
2253 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2254 signedCert
->SignatureAlgorithm
.pszObjId
, 0);
2255 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
2257 SetLastError(NTE_BAD_ALGID
);
2260 hashID
= info
->u
.Algid
;
2261 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
2262 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
2265 /* Load the default provider if necessary */
2267 hCryptProv
= CRYPT_GetDefaultProvider();
2268 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
2269 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
2274 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
2277 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
2278 signedCert
->ToBeSigned
.cbData
, 0);
2280 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
2281 signedCert
->Signature
.cbData
, key
, NULL
, 0);
2282 CryptDestroyHash(hash
);
2284 CryptDestroyKey(key
);
2289 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
2290 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
2291 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
2294 CRYPT_DATA_BLOB subjectBlob
;
2296 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
2297 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
2298 dwFlags
, pvReserved
);
2300 switch (dwSubjectType
)
2302 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
2304 PCRYPT_DATA_BLOB blob
= pvSubject
;
2306 subjectBlob
.pbData
= blob
->pbData
;
2307 subjectBlob
.cbData
= blob
->cbData
;
2310 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
2312 PCERT_CONTEXT context
= pvSubject
;
2314 subjectBlob
.pbData
= context
->pbCertEncoded
;
2315 subjectBlob
.cbData
= context
->cbCertEncoded
;
2318 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
2320 PCRL_CONTEXT context
= pvSubject
;
2322 subjectBlob
.pbData
= context
->pbCrlEncoded
;
2323 subjectBlob
.cbData
= context
->cbCrlEncoded
;
2327 SetLastError(E_INVALIDARG
);
2333 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
2336 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
2337 subjectBlob
.pbData
, subjectBlob
.cbData
,
2338 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2339 &signedCert
, &size
);
2342 switch (dwIssuerType
)
2344 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
2345 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2346 dwCertEncodingType
, pvIssuer
,
2349 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
2350 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
2352 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
2355 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
2356 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
2359 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
2362 SetLastError(E_INVALIDARG
);
2367 FIXME("unimplemented for NULL signer\n");
2368 SetLastError(E_INVALIDARG
);
2373 SetLastError(E_INVALIDARG
);
2376 LocalFree(signedCert
);
2382 BOOL WINAPI
CertGetIntendedKeyUsage(DWORD dwCertEncodingType
,
2383 PCERT_INFO pCertInfo
, BYTE
*pbKeyUsage
, DWORD cbKeyUsage
)
2385 PCERT_EXTENSION ext
;
2388 TRACE("(%08x, %p, %p, %d)\n", dwCertEncodingType
, pCertInfo
, pbKeyUsage
,
2391 ext
= CertFindExtension(szOID_KEY_USAGE
, pCertInfo
->cExtension
,
2392 pCertInfo
->rgExtension
);
2395 CRYPT_BIT_BLOB usage
;
2396 DWORD size
= sizeof(usage
);
2398 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_BITS
,
2399 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2403 if (cbKeyUsage
< usage
.cbData
)
2407 memcpy(pbKeyUsage
, usage
.pbData
, usage
.cbData
);
2408 if (cbKeyUsage
> usage
.cbData
)
2409 memset(pbKeyUsage
+ usage
.cbData
, 0,
2410 cbKeyUsage
- usage
.cbData
);
2419 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
2420 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
2422 PCERT_ENHKEY_USAGE usage
= NULL
;
2426 if (!pCertContext
|| !pcbUsage
)
2428 SetLastError(ERROR_INVALID_PARAMETER
);
2432 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
2434 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
2438 if (CertGetCertificateContextProperty(pCertContext
,
2439 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
2441 LPBYTE buf
= CryptMemAlloc(propSize
);
2445 if (CertGetCertificateContextProperty(pCertContext
,
2446 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
2448 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2449 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
2450 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2456 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
2458 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
2459 pCertContext
->pCertInfo
->cExtension
,
2460 pCertContext
->pCertInfo
->rgExtension
);
2464 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
2465 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2466 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
2471 /* If a particular location is specified, this should fail. Otherwise
2472 * it should succeed with an empty usage. (This is true on Win2k and
2473 * later, which we emulate.)
2477 SetLastError(CRYPT_E_NOT_FOUND
);
2481 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
2487 *pcbUsage
= bytesNeeded
;
2488 else if (*pcbUsage
< bytesNeeded
)
2490 SetLastError(ERROR_MORE_DATA
);
2491 *pcbUsage
= bytesNeeded
;
2496 *pcbUsage
= bytesNeeded
;
2500 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
2501 sizeof(CERT_ENHKEY_USAGE
) +
2502 usage
->cUsageIdentifier
* sizeof(LPSTR
));
2504 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
2505 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
2506 sizeof(CERT_ENHKEY_USAGE
));
2507 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2509 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2510 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2511 nextOID
+= strlen(nextOID
) + 1;
2515 pUsage
->cUsageIdentifier
= 0;
2520 TRACE("returning %d\n", ret
);
2524 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
2525 PCERT_ENHKEY_USAGE pUsage
)
2529 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
2533 CRYPT_DATA_BLOB blob
= { 0, NULL
};
2535 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
2536 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
2539 ret
= CertSetCertificateContextProperty(pCertContext
,
2540 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
2541 LocalFree(blob
.pbData
);
2545 ret
= CertSetCertificateContextProperty(pCertContext
,
2546 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
2550 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2551 LPCSTR pszUsageIdentifier
)
2556 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2558 if (CertGetEnhancedKeyUsage(pCertContext
,
2559 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
2561 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
2565 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2566 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
2570 BOOL exists
= FALSE
;
2572 /* Make sure usage doesn't already exist */
2573 for (i
= 0; !exists
&& i
< usage
->cUsageIdentifier
; i
++)
2575 if (!strcmp(usage
->rgpszUsageIdentifier
[i
],
2576 pszUsageIdentifier
))
2581 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
2582 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2588 newUsage
->rgpszUsageIdentifier
= (LPSTR
*)
2589 ((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
2590 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
2591 + (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
2592 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2594 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2595 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
2596 nextOID
+= strlen(nextOID
) + 1;
2598 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
2599 strcpy(nextOID
, pszUsageIdentifier
);
2600 newUsage
->cUsageIdentifier
= i
+ 1;
2601 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
2602 CryptMemFree(newUsage
);
2608 CryptMemFree(usage
);
2615 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
2616 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
2620 usage
->rgpszUsageIdentifier
=
2621 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
2622 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
2623 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
2624 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
2625 usage
->cUsageIdentifier
= 1;
2626 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
2627 CryptMemFree(usage
);
2635 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
2636 LPCSTR pszUsageIdentifier
)
2640 CERT_ENHKEY_USAGE usage
;
2642 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
2644 size
= sizeof(usage
);
2645 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2646 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
2647 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2649 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2653 ret
= CertGetEnhancedKeyUsage(pCertContext
,
2654 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
2657 if (pUsage
->cUsageIdentifier
)
2662 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
2664 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
2665 pszUsageIdentifier
))
2667 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
2668 pUsage
->rgpszUsageIdentifier
[i
] =
2669 pUsage
->rgpszUsageIdentifier
[i
+ 1];
2671 pUsage
->cUsageIdentifier
--;
2672 /* Remove the usage if it's empty */
2673 if (pUsage
->cUsageIdentifier
)
2674 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
2676 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
2679 CryptMemFree(pUsage
);
2686 /* it fit in an empty usage, therefore there's nothing to remove */
2698 #define BITS_PER_DWORD (sizeof(DWORD) * 8)
2700 static void CRYPT_SetBitInField(struct BitField
*field
, DWORD bit
)
2702 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2704 if (indexIndex
+ 1 > field
->cIndexes
)
2706 if (field
->cIndexes
)
2707 field
->indexes
= CryptMemRealloc(field
->indexes
,
2708 (indexIndex
+ 1) * sizeof(DWORD
));
2710 field
->indexes
= CryptMemAlloc(sizeof(DWORD
));
2713 field
->indexes
[indexIndex
] = 0;
2714 field
->cIndexes
= indexIndex
+ 1;
2718 field
->indexes
[indexIndex
] |= 1 << (bit
% BITS_PER_DWORD
);
2721 static BOOL
CRYPT_IsBitInFieldSet(const struct BitField
*field
, DWORD bit
)
2724 DWORD indexIndex
= bit
/ BITS_PER_DWORD
;
2726 assert(field
->cIndexes
);
2727 set
= field
->indexes
[indexIndex
] & (1 << (bit
% BITS_PER_DWORD
));
2731 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
2732 int *cNumOIDs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
2735 DWORD i
, cbOIDs
= 0;
2736 BOOL allUsagesValid
= TRUE
;
2737 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
2739 TRACE("(%d, %p, %d, %p, %d)\n", cCerts
, rghCerts
, *cNumOIDs
,
2742 for (i
= 0; i
< cCerts
; i
++)
2744 CERT_ENHKEY_USAGE usage
;
2745 DWORD size
= sizeof(usage
);
2747 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
2748 /* Success is deliberately ignored: it implies all usages are valid */
2749 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
2751 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
2753 allUsagesValid
= FALSE
;
2756 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
2759 if (!validUsages
.cUsageIdentifier
)
2763 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
2764 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
2765 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2766 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
2768 validUsages
.rgpszUsageIdentifier
=
2769 CryptMemAlloc(cbOIDs
);
2770 if (validUsages
.rgpszUsageIdentifier
)
2772 LPSTR nextOID
= (LPSTR
)
2773 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
2774 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2776 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2778 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
2779 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
2780 pUsage
->rgpszUsageIdentifier
[j
]);
2781 nextOID
+= lstrlenA(nextOID
) + 1;
2787 struct BitField validIndexes
= { 0, NULL
};
2788 DWORD j
, k
, numRemoved
= 0;
2790 /* Merge: build a bitmap of all the indexes of
2791 * validUsages.rgpszUsageIdentifier that are in pUsage.
2793 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
2795 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
2797 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
2798 validUsages
.rgpszUsageIdentifier
[k
]))
2800 CRYPT_SetBitInField(&validIndexes
, k
);
2805 /* Merge by removing from validUsages those that are
2806 * not in the bitmap.
2808 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
2810 if (!CRYPT_IsBitInFieldSet(&validIndexes
, j
))
2812 if (j
< validUsages
.cUsageIdentifier
- 1)
2814 memmove(&validUsages
.rgpszUsageIdentifier
[j
],
2815 &validUsages
.rgpszUsageIdentifier
[j
+
2817 (validUsages
.cUsageIdentifier
- numRemoved
2818 - j
- 1) * sizeof(LPSTR
));
2820 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
2822 validUsages
.cUsageIdentifier
--;
2826 validUsages
.cUsageIdentifier
--;
2829 CryptMemFree(validIndexes
.indexes
);
2832 CryptMemFree(pUsage
);
2844 *cNumOIDs
= validUsages
.cUsageIdentifier
;
2847 else if (*pcbOIDs
< cbOIDs
)
2850 SetLastError(ERROR_MORE_DATA
);
2855 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
2856 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
2859 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
2861 rghOIDs
[i
] = nextOID
;
2862 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
2863 nextOID
+= lstrlenA(nextOID
) + 1;
2867 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
2868 TRACE("cNumOIDs: %d\n", *cNumOIDs
);
2869 TRACE("returning %d\n", ret
);
2873 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
2874 * pInfo is NULL, from the attributes of hProv.
2876 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
2877 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
2879 CRYPT_KEY_PROV_INFO info
= { 0 };
2887 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
2890 LPSTR szContainer
= CryptMemAlloc(size
);
2894 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
2895 (BYTE
*)szContainer
, &size
, 0);
2898 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2902 info
.pwszContainerName
= CryptMemAlloc(len
*
2904 MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
2905 info
.pwszContainerName
, len
);
2908 CryptMemFree(szContainer
);
2911 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
2914 LPSTR szProvider
= CryptMemAlloc(size
);
2918 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
2922 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2926 info
.pwszProvName
= CryptMemAlloc(len
*
2928 MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2929 info
.pwszProvName
, len
);
2932 CryptMemFree(szProvider
);
2935 size
= sizeof(info
.dwKeySpec
);
2936 /* in case no CRYPT_KEY_PROV_INFO given,
2937 * we always use AT_SIGNATURE key spec
2939 info
.dwKeySpec
= AT_SIGNATURE
;
2940 size
= sizeof(info
.dwProvType
);
2941 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
2944 info
.dwProvType
= PROV_RSA_FULL
;
2948 CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
2953 CryptMemFree(info
.pwszContainerName
);
2954 CryptMemFree(info
.pwszProvName
);
2958 /* Creates a signed certificate context from the unsigned, encoded certificate
2959 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
2961 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
2962 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
2964 PCCERT_CONTEXT context
= NULL
;
2968 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2969 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
2972 LPBYTE sig
= CryptMemAlloc(sigSize
);
2974 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2975 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
2978 CERT_SIGNED_CONTENT_INFO signedInfo
;
2979 BYTE
*encodedSignedCert
= NULL
;
2980 DWORD encodedSignedCertSize
= 0;
2982 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
2983 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
2984 signedInfo
.SignatureAlgorithm
= *sigAlgo
;
2985 signedInfo
.Signature
.cbData
= sigSize
;
2986 signedInfo
.Signature
.pbData
= sig
;
2987 signedInfo
.Signature
.cUnusedBits
= 0;
2988 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
2989 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2990 &encodedSignedCert
, &encodedSignedCertSize
);
2993 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2994 encodedSignedCert
, encodedSignedCertSize
);
2995 LocalFree(encodedSignedCert
);
3003 /* Copies data from the parameters into info, where:
3004 * pSerialNumber: The serial number. Must not be NULL.
3005 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
3007 * pSignatureAlgorithm: Optional.
3008 * pStartTime: The starting time of the certificate. If NULL, the current
3009 * system time is used.
3010 * pEndTime: The ending time of the certificate. If NULL, one year past the
3011 * starting time is used.
3012 * pubKey: The public key of the certificate. Must not be NULL.
3013 * pExtensions: Extensions to be included with the certificate. Optional.
3015 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
3016 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
3017 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
3018 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
3019 const CERT_EXTENSIONS
*pExtensions
)
3021 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
3024 assert(pSerialNumber
);
3025 assert(pSubjectIssuerBlob
);
3028 if (pExtensions
&& pExtensions
->cExtension
)
3029 info
->dwVersion
= CERT_V3
;
3031 info
->dwVersion
= CERT_V1
;
3032 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
3033 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
3034 if (pSignatureAlgorithm
)
3035 info
->SignatureAlgorithm
= *pSignatureAlgorithm
;
3038 info
->SignatureAlgorithm
.pszObjId
= oid
;
3039 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
3040 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
3042 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
3043 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
3045 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
3047 GetSystemTimeAsFileTime(&info
->NotBefore
);
3049 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
3054 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
3057 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
3060 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
3061 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
3062 info
->SubjectPublicKeyInfo
= *pubKey
;
3065 info
->cExtension
= pExtensions
->cExtension
;
3066 info
->rgExtension
= pExtensions
->rgExtension
;
3070 info
->cExtension
= 0;
3071 info
->rgExtension
= NULL
;
3075 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
3076 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
3077 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
3079 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
3081 HCRYPTPROV hProv
= 0;
3082 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
3086 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
3088 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
3090 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
3091 rpcrt
, "RpcStringFreeA");
3093 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
3096 RPC_STATUS status
= uuidCreate(&uuid
);
3098 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
3100 unsigned char *uuidStr
;
3102 status
= uuidToString(&uuid
, &uuidStr
);
3103 if (status
== RPC_S_OK
)
3105 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
3106 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
3112 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
3114 CryptDestroyKey(key
);
3116 rpcStringFree(&uuidStr
);
3125 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
3126 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
3127 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
3128 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
3129 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
3131 PCCERT_CONTEXT context
= NULL
;
3132 BOOL ret
, releaseContext
= FALSE
;
3133 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
3134 DWORD pubKeySize
= 0, dwKeySpec
;
3136 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
3137 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
3138 pExtensions
, pExtensions
);
3140 if(!pSubjectIssuerBlob
)
3142 SetLastError(ERROR_INVALID_PARAMETER
);
3146 dwKeySpec
= pKeyProvInfo
? pKeyProvInfo
->dwKeySpec
: AT_SIGNATURE
;
3151 hProv
= CRYPT_CreateKeyProv();
3152 releaseContext
= TRUE
;
3154 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
3156 SetLastError(NTE_BAD_FLAGS
);
3162 /* acquire the context using the given information*/
3163 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3164 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3165 pKeyProvInfo
->dwFlags
);
3168 if(GetLastError() != NTE_BAD_KEYSET
)
3170 /* create the key set */
3171 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
3172 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
3173 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
3177 /* check if the key is here */
3178 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
3181 if (NTE_NO_KEY
== GetLastError())
3182 { /* generate the key */
3183 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
3187 CryptReleaseContext(hProv
,0);
3188 SetLastError(NTE_BAD_KEYSET
);
3192 CryptDestroyKey(hKey
);
3193 releaseContext
= TRUE
;
3197 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
3201 pubKey
= CryptMemAlloc(pubKeySize
);
3204 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
3205 pubKey
, &pubKeySize
);
3208 CERT_INFO info
= { 0 };
3209 CRYPT_DER_BLOB blob
= { 0, NULL
};
3211 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
3213 CryptGenRandom(hProv
, sizeof(serial
), serial
);
3214 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
3215 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
3216 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
3217 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
,
3221 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
3222 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
3223 &info
.SignatureAlgorithm
);
3225 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
3226 blob
.pbData
, blob
.cbData
);
3227 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
3228 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
3229 LocalFree(blob
.pbData
);
3232 CryptMemFree(pubKey
);
3236 CryptReleaseContext(hProv
, 0);
3240 BOOL WINAPI
CertVerifyCTLUsage(DWORD dwEncodingType
, DWORD dwSubjectType
,
3241 void *pvSubject
, PCTL_USAGE pSubjectUsage
, DWORD dwFlags
,
3242 PCTL_VERIFY_USAGE_PARA pVerifyUsagePara
,
3243 PCTL_VERIFY_USAGE_STATUS pVerifyUsageStatus
)
3245 FIXME("(0x%x, %d, %p, %p, 0x%x, %p, %p): stub\n", dwEncodingType
,
3246 dwSubjectType
, pvSubject
, pSubjectUsage
, dwFlags
, pVerifyUsagePara
,
3247 pVerifyUsageStatus
);
3248 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
3252 const void * WINAPI
CertCreateContext(DWORD dwContextType
, DWORD dwEncodingType
,
3253 const BYTE
*pbEncoded
, DWORD cbEncoded
,
3254 DWORD dwFlags
, PCERT_CREATE_CONTEXT_PARA pCreatePara
)
3256 TRACE("(0x%x, 0x%x, %p, %d, 0x%08x, %p)\n", dwContextType
, dwEncodingType
,
3257 pbEncoded
, cbEncoded
, dwFlags
, pCreatePara
);
3261 FIXME("dwFlags 0x%08x not handled\n", dwFlags
);
3266 FIXME("pCreatePara not handled\n");
3270 switch (dwContextType
)
3272 case CERT_STORE_CERTIFICATE_CONTEXT
:
3273 return CertCreateCertificateContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3274 case CERT_STORE_CRL_CONTEXT
:
3275 return CertCreateCRLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3276 case CERT_STORE_CTL_CONTEXT
:
3277 return CertCreateCTLContext(dwEncodingType
, pbEncoded
, cbEncoded
);
3279 WARN("unknown context type: 0x%x\n", dwContextType
);