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 "crypt32_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
34 /* Internal version of CertGetCertificateContextProperty that gets properties
35 * directly from the context (or the context it's linked to, depending on its
36 * type.) Doesn't handle special-case properties, since they are handled by
37 * CertGetCertificateContextProperty, and are particular to the store in which
38 * the property exists (which is separate from the context.)
40 static BOOL WINAPI
CertContext_GetProperty(void *context
, DWORD dwPropId
,
41 void *pvData
, DWORD
*pcbData
);
43 /* Internal version of CertSetCertificateContextProperty that sets properties
44 * directly on the context (or the context it's linked to, depending on its
45 * type.) Doesn't handle special cases, since they're handled by
46 * CertSetCertificateContextProperty anyway.
48 static BOOL WINAPI
CertContext_SetProperty(void *context
, DWORD dwPropId
,
49 DWORD dwFlags
, const void *pvData
);
51 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
52 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
53 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
55 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
56 pbCertEncoded
, cbCertEncoded
);
59 TRACE("(%p, %08x, %p, %d, %08x, %p)\n", hCertStore
, dwCertEncodingType
,
60 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
64 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
65 dwAddDisposition
, ppCertContext
);
66 CertFreeCertificateContext(cert
);
73 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
74 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
76 PCERT_CONTEXT cert
= NULL
;
78 PCERT_INFO certInfo
= NULL
;
81 TRACE("(%08x, %p, %d)\n", dwCertEncodingType
, pbCertEncoded
,
84 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
85 pbCertEncoded
, cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
86 (BYTE
*)&certInfo
, &size
);
91 cert
= (PCERT_CONTEXT
)Context_CreateDataContext(sizeof(CERT_CONTEXT
));
94 data
= CryptMemAlloc(cbCertEncoded
);
101 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
102 cert
->dwCertEncodingType
= dwCertEncodingType
;
103 cert
->pbCertEncoded
= data
;
104 cert
->cbCertEncoded
= cbCertEncoded
;
105 cert
->pCertInfo
= certInfo
;
106 cert
->hCertStore
= 0;
110 return (PCCERT_CONTEXT
)cert
;
113 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(
114 PCCERT_CONTEXT pCertContext
)
116 TRACE("(%p)\n", pCertContext
);
117 Context_AddRef((void *)pCertContext
, sizeof(CERT_CONTEXT
));
121 static void CertDataContext_Free(void *context
)
123 PCERT_CONTEXT certContext
= (PCERT_CONTEXT
)context
;
125 CryptMemFree(certContext
->pbCertEncoded
);
126 LocalFree(certContext
->pCertInfo
);
129 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
131 TRACE("(%p)\n", pCertContext
);
134 Context_Release((void *)pCertContext
, sizeof(CERT_CONTEXT
),
135 CertDataContext_Free
);
139 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
142 PCONTEXT_PROPERTY_LIST properties
= Context_GetProperties(
143 (void *)pCertContext
, sizeof(CERT_CONTEXT
));
146 TRACE("(%p, %d)\n", pCertContext
, dwPropId
);
149 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
155 static BOOL
CertContext_GetHashProp(void *context
, DWORD dwPropId
,
156 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
159 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
163 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
165 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
170 static BOOL
CertContext_CopyParam(void *pvData
, DWORD
*pcbData
, const void *pb
,
177 else if (*pcbData
< cb
)
179 SetLastError(ERROR_MORE_DATA
);
185 memcpy(pvData
, pb
, cb
);
191 static BOOL WINAPI
CertContext_GetProperty(void *context
, DWORD dwPropId
,
192 void *pvData
, DWORD
*pcbData
)
194 PCCERT_CONTEXT pCertContext
= (PCCERT_CONTEXT
)context
;
195 PCONTEXT_PROPERTY_LIST properties
=
196 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
198 CRYPT_DATA_BLOB blob
;
200 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
203 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
207 ret
= CertContext_CopyParam(pvData
, pcbData
, blob
.pbData
, blob
.cbData
);
210 /* Implicit properties */
213 case CERT_SHA1_HASH_PROP_ID
:
214 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
215 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
218 case CERT_MD5_HASH_PROP_ID
:
219 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
220 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
223 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
224 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
225 pCertContext
->pCertInfo
->Subject
.pbData
,
226 pCertContext
->pCertInfo
->Subject
.cbData
,
229 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
230 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
231 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
232 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
235 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
236 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
237 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
238 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
241 case CERT_SIGNATURE_HASH_PROP_ID
:
242 FIXME("CERT_SIGNATURE_HASH_PROP_ID unimplemented\n");
243 SetLastError(CRYPT_E_NOT_FOUND
);
245 case CERT_KEY_IDENTIFIER_PROP_ID
:
247 PCERT_EXTENSION ext
= CertFindExtension(
248 szOID_SUBJECT_KEY_IDENTIFIER
, pCertContext
->pCertInfo
->cExtension
,
249 pCertContext
->pCertInfo
->rgExtension
);
253 CRYPT_DATA_BLOB
*value
;
256 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
257 szOID_SUBJECT_KEY_IDENTIFIER
, ext
->Value
.pbData
,
258 ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &value
,
262 ret
= CertContext_CopyParam(pvData
, pcbData
, value
->pbData
,
264 CertContext_SetProperty(context
, dwPropId
, 0, value
);
269 SetLastError(ERROR_INVALID_DATA
);
273 SetLastError(CRYPT_E_NOT_FOUND
);
276 TRACE("returning %d\n", ret
);
280 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
282 DWORD i
, containerLen
, provNameLen
;
283 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
285 info
->pwszContainerName
= (LPWSTR
)data
;
286 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
287 data
+= containerLen
;
289 info
->pwszProvName
= (LPWSTR
)data
;
290 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
293 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
294 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
296 for (i
= 0; i
< info
->cProvParam
; i
++)
298 info
->rgProvParam
[i
].pbData
= data
;
299 data
+= info
->rgProvParam
[i
].cbData
;
303 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
304 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
308 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
313 case CERT_CERT_PROP_ID
:
314 case CERT_CRL_PROP_ID
:
315 case CERT_CTL_PROP_ID
:
316 SetLastError(E_INVALIDARG
);
319 case CERT_ACCESS_STATE_PROP_ID
:
320 if (pCertContext
->hCertStore
)
321 ret
= CertGetStoreProperty(pCertContext
->hCertStore
, dwPropId
,
327 ret
= CertContext_CopyParam(pvData
, pcbData
, &state
, sizeof(state
));
330 case CERT_KEY_PROV_HANDLE_PROP_ID
:
332 CERT_KEY_CONTEXT keyContext
;
333 DWORD size
= sizeof(keyContext
);
335 ret
= CertContext_GetProperty((void *)pCertContext
,
336 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
338 ret
= CertContext_CopyParam(pvData
, pcbData
, &keyContext
.hCryptProv
,
339 sizeof(keyContext
.hCryptProv
));
342 case CERT_KEY_PROV_INFO_PROP_ID
:
343 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
346 CRYPT_FixKeyProvInfoPointers((PCRYPT_KEY_PROV_INFO
)pvData
);
349 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
353 TRACE("returning %d\n", ret
);
357 /* Copies key provider info from from into to, where to is assumed to be a
358 * contiguous buffer of memory large enough for from and all its associated
359 * data, but whose pointers are uninitialized.
360 * Upon return, to contains a contiguous copy of from, packed in the following
362 * - CRYPT_KEY_PROV_INFO
363 * - pwszContainerName
365 * - rgProvParam[0]...
367 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
368 const CRYPT_KEY_PROV_INFO
*from
)
371 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
373 if (from
->pwszContainerName
)
375 to
->pwszContainerName
= (LPWSTR
)nextData
;
376 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
377 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
380 to
->pwszContainerName
= NULL
;
381 if (from
->pwszProvName
)
383 to
->pwszProvName
= (LPWSTR
)nextData
;
384 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
385 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
388 to
->pwszProvName
= NULL
;
389 to
->dwProvType
= from
->dwProvType
;
390 to
->dwFlags
= from
->dwFlags
;
391 to
->cProvParam
= from
->cProvParam
;
392 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
393 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
394 to
->dwKeySpec
= from
->dwKeySpec
;
395 for (i
= 0; i
< to
->cProvParam
; i
++)
397 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
398 sizeof(CRYPT_KEY_PROV_PARAM
));
399 to
->rgProvParam
[i
].pbData
= nextData
;
400 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
401 from
->rgProvParam
[i
].cbData
);
402 nextData
+= from
->rgProvParam
[i
].cbData
;
406 static BOOL
CertContext_SetKeyProvInfoProperty(PCONTEXT_PROPERTY_LIST properties
,
407 const CRYPT_KEY_PROV_INFO
*info
)
411 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
413 if (info
->pwszContainerName
)
414 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
417 if (info
->pwszProvName
)
418 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
421 size
+= containerSize
+ provNameSize
;
422 for (i
= 0; i
< info
->cProvParam
; i
++)
423 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
424 buf
= CryptMemAlloc(size
);
427 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
428 ret
= ContextPropertyList_SetProperty(properties
,
429 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
437 static BOOL WINAPI
CertContext_SetProperty(void *context
, DWORD dwPropId
,
438 DWORD dwFlags
, const void *pvData
)
440 PCONTEXT_PROPERTY_LIST properties
=
441 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
444 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
452 case CERT_AUTO_ENROLL_PROP_ID
:
453 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
454 case CERT_DESCRIPTION_PROP_ID
:
455 case CERT_FRIENDLY_NAME_PROP_ID
:
456 case CERT_HASH_PROP_ID
:
457 case CERT_KEY_IDENTIFIER_PROP_ID
:
458 case CERT_MD5_HASH_PROP_ID
:
459 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
460 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
461 case CERT_PVK_FILE_PROP_ID
:
462 case CERT_SIGNATURE_HASH_PROP_ID
:
463 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
464 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
465 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
466 case CERT_ENROLLMENT_PROP_ID
:
467 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
468 case CERT_RENEWAL_PROP_ID
:
472 const CRYPT_DATA_BLOB
*blob
= (const CRYPT_DATA_BLOB
*)pvData
;
474 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
475 blob
->pbData
, blob
->cbData
);
479 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
484 case CERT_DATE_STAMP_PROP_ID
:
486 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
487 (const BYTE
*)pvData
, sizeof(FILETIME
));
490 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
494 case CERT_KEY_CONTEXT_PROP_ID
:
498 const CERT_KEY_CONTEXT
*keyContext
= (const CERT_KEY_CONTEXT
*)pvData
;
500 if (keyContext
->cbSize
!= sizeof(CERT_KEY_CONTEXT
))
502 SetLastError(E_INVALIDARG
);
506 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
507 (const BYTE
*)keyContext
, keyContext
->cbSize
);
511 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
516 case CERT_KEY_PROV_INFO_PROP_ID
:
518 ret
= CertContext_SetKeyProvInfoProperty(properties
,
519 (const CRYPT_KEY_PROV_INFO
*)pvData
);
522 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
526 case CERT_KEY_PROV_HANDLE_PROP_ID
:
528 CERT_KEY_CONTEXT keyContext
;
529 DWORD size
= sizeof(keyContext
);
531 ret
= CertContext_GetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
535 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
536 CryptReleaseContext(keyContext
.hCryptProv
, 0);
538 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
540 keyContext
.hCryptProv
= 0;
541 ret
= CertContext_SetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
547 FIXME("%d: stub\n", dwPropId
);
551 TRACE("returning %d\n", ret
);
555 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
556 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
560 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
562 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
563 * crashes on most of these, I'll be safer.
568 case CERT_ACCESS_STATE_PROP_ID
:
569 case CERT_CERT_PROP_ID
:
570 case CERT_CRL_PROP_ID
:
571 case CERT_CTL_PROP_ID
:
572 SetLastError(E_INVALIDARG
);
575 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
577 TRACE("returning %d\n", ret
);
581 /* Acquires the private key using the key provider info, retrieving info from
582 * the certificate if info is NULL. The acquired provider is returned in
583 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
585 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
586 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
589 BOOL allocated
= FALSE
, ret
= TRUE
;
593 ret
= CertGetCertificateContextProperty(pCert
,
594 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
597 info
= (PCRYPT_KEY_PROV_INFO
)HeapAlloc(GetProcessHeap(), 0, size
);
600 ret
= CertGetCertificateContextProperty(pCert
,
601 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
606 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
610 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
611 info
->pwszProvName
, info
->dwProvType
, 0);
616 for (i
= 0; i
< info
->cProvParam
; i
++)
618 CryptSetProvParam(*phCryptProv
,
619 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
620 info
->rgProvParam
[i
].dwFlags
);
622 *pdwKeySpec
= info
->dwKeySpec
;
625 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
628 HeapFree(GetProcessHeap(), 0, info
);
632 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
633 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
*phCryptProv
,
634 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
636 BOOL ret
= FALSE
, cache
= FALSE
;
637 PCRYPT_KEY_PROV_INFO info
= NULL
;
638 CERT_KEY_CONTEXT keyContext
;
641 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
642 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
644 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
648 ret
= CertGetCertificateContextProperty(pCert
,
649 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
652 info
= (PCRYPT_KEY_PROV_INFO
)HeapAlloc(
653 GetProcessHeap(), 0, size
);
654 ret
= CertGetCertificateContextProperty(pCert
,
655 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
657 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
660 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
665 size
= sizeof(keyContext
);
666 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
670 *phCryptProv
= keyContext
.hCryptProv
;
672 *pdwKeySpec
= keyContext
.dwKeySpec
;
673 if (pfCallerFreeProv
)
674 *pfCallerFreeProv
= !cache
;
679 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
680 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
683 *phCryptProv
= keyContext
.hCryptProv
;
685 *pdwKeySpec
= keyContext
.dwKeySpec
;
688 keyContext
.cbSize
= sizeof(keyContext
);
689 if (CertSetCertificateContextProperty(pCert
,
690 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
692 if (pfCallerFreeProv
)
693 *pfCallerFreeProv
= FALSE
;
698 if (pfCallerFreeProv
)
699 *pfCallerFreeProv
= TRUE
;
703 HeapFree(GetProcessHeap(), 0, info
);
707 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
708 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
710 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
712 return CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
713 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
714 &pCertId2
->SerialNumber
);
717 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
718 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
722 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
724 if (pCertName1
->cbData
== pCertName2
->cbData
)
726 if (pCertName1
->cbData
)
727 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
737 /* Returns the number of significant bytes in pInt, where a byte is
738 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
739 * for negative numbers. pInt is assumed to be little-endian.
741 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
743 DWORD ret
= pInt
->cbData
;
747 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
749 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
757 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
758 PCRYPT_INTEGER_BLOB pInt2
)
763 TRACE("(%p, %p)\n", pInt1
, pInt2
);
765 cb1
= CRYPT_significantBytes(pInt1
);
766 cb2
= CRYPT_significantBytes(pInt2
);
770 ret
= !memcmp(pInt1
->pbData
, pInt1
->pbData
, cb1
);
779 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
780 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
784 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
786 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
787 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
789 if (pPublicKey2
->PublicKey
.cbData
)
790 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
791 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
800 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
801 PCERT_PUBLIC_KEY_INFO pPublicKey
)
805 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
807 if (dwCertEncodingType
!= X509_ASN_ENCODING
)
809 SetLastError(ERROR_FILE_NOT_FOUND
);
812 if (pPublicKey
->Algorithm
.pszObjId
&&
813 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
815 FIXME("unimplemented for DH public keys\n");
816 SetLastError(CRYPT_E_ASN1_BADTAG
);
822 BOOL ret
= CryptDecodeObjectEx(dwCertEncodingType
,
823 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
824 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
829 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)((LPBYTE
)buf
+
832 len
= rsaPubKey
->bitlen
;
839 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
840 DWORD dwFlags
, const void *pvPara
);
842 static BOOL
compare_cert_any(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
843 DWORD dwFlags
, const void *pvPara
)
848 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
849 DWORD dwFlags
, const void *pvPara
)
853 DWORD size
= sizeof(hash
);
855 ret
= CertGetCertificateContextProperty(pCertContext
,
856 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
859 const CRYPT_HASH_BLOB
*pHash
= (const CRYPT_HASH_BLOB
*)pvPara
;
861 if (size
== pHash
->cbData
)
862 ret
= !memcmp(pHash
->pbData
, hash
, size
);
869 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
870 DWORD dwFlags
, const void *pvPara
)
874 DWORD size
= sizeof(hash
);
876 ret
= CertGetCertificateContextProperty(pCertContext
,
877 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
880 const CRYPT_HASH_BLOB
*pHash
= (const CRYPT_HASH_BLOB
*)pvPara
;
882 if (size
== pHash
->cbData
)
883 ret
= !memcmp(pHash
->pbData
, hash
, size
);
890 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
891 DWORD dwFlags
, const void *pvPara
)
893 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
896 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
897 toCompare
= &pCertContext
->pCertInfo
->Subject
;
899 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
900 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
905 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
906 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
908 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
910 return CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
911 &pCertInfo
->Issuer
, &pCertContext
->pCertInfo
->Subject
);
914 static BOOL
compare_cert_by_cert_id(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
915 DWORD dwFlags
, const void *pvPara
)
917 CERT_ID
*id
= (CERT_ID
*)pvPara
;
920 switch (id
->dwIdChoice
)
922 case CERT_ID_ISSUER_SERIAL_NUMBER
:
923 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
924 &pCertContext
->pCertInfo
->Issuer
, &id
->u
.IssuerSerialNumber
.Issuer
);
926 ret
= CertCompareIntegerBlob(&pCertContext
->pCertInfo
->SerialNumber
,
927 &id
->u
.IssuerSerialNumber
.SerialNumber
);
929 case CERT_ID_SHA1_HASH
:
930 ret
= compare_cert_by_sha1_hash(pCertContext
, dwType
, dwFlags
,
933 case CERT_ID_KEY_IDENTIFIER
:
937 ret
= CertGetCertificateContextProperty(pCertContext
,
938 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
939 if (ret
&& size
== id
->u
.KeyId
.cbData
)
941 LPBYTE buf
= CryptMemAlloc(size
);
945 CertGetCertificateContextProperty(pCertContext
,
946 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
947 ret
= !memcmp(buf
, id
->u
.KeyId
.pbData
, size
);
961 static BOOL
compare_cert_by_issuer(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
962 DWORD dwFlags
, const void *pvPara
)
964 return compare_cert_by_subject_cert(pCertContext
, dwType
, dwFlags
,
965 ((PCCERT_CONTEXT
)pvPara
)->pCertInfo
);
968 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
969 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
970 PCCERT_CONTEXT pPrevCertContext
)
973 CertCompareFunc compare
;
975 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore
, dwCertEncodingType
,
976 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
978 switch (dwType
>> CERT_COMPARE_SHIFT
)
980 case CERT_COMPARE_ANY
:
981 compare
= compare_cert_any
;
983 case CERT_COMPARE_MD5_HASH
:
984 compare
= compare_cert_by_md5_hash
;
986 case CERT_COMPARE_SHA1_HASH
:
987 compare
= compare_cert_by_sha1_hash
;
989 case CERT_COMPARE_NAME
:
990 compare
= compare_cert_by_name
;
992 case CERT_COMPARE_SUBJECT_CERT
:
993 compare
= compare_cert_by_subject_cert
;
995 case CERT_COMPARE_CERT_ID
:
996 compare
= compare_cert_by_cert_id
;
998 case CERT_COMPARE_ISSUER_OF
:
999 compare
= compare_cert_by_issuer
;
1002 FIXME("find type %08x unimplemented\n", dwType
);
1008 BOOL matches
= FALSE
;
1010 ret
= pPrevCertContext
;
1012 ret
= CertEnumCertificatesInStore(hCertStore
, ret
);
1014 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
1015 } while (ret
!= NULL
&& !matches
);
1017 SetLastError(CRYPT_E_NOT_FOUND
);
1021 SetLastError(CRYPT_E_NOT_FOUND
);
1027 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
1028 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
1030 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
1034 SetLastError(E_INVALIDARG
);
1037 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
1038 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
1041 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
1042 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
1044 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
1045 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
1047 if (*pdwFlags
& ~supportedFlags
)
1049 SetLastError(E_INVALIDARG
);
1052 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
1055 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
1058 /* FIXME: what if the CRL has expired? */
1061 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
1062 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1063 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1066 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1068 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1070 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1071 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1073 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1075 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1076 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1077 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1078 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1083 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1084 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1089 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1090 pPrevIssuerContext
, *pdwFlags
);
1092 if (!pSubjectContext
)
1094 SetLastError(E_INVALIDARG
);
1098 ret
= CertFindCertificateInStore(hCertStore
,
1099 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1100 pSubjectContext
, pPrevIssuerContext
);
1103 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1106 CertFreeCertificateContext(ret
);
1114 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
1115 CRYPT_ATTRIBUTE rgAttr
[])
1117 PCRYPT_ATTRIBUTE ret
= NULL
;
1120 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
1126 SetLastError(ERROR_INVALID_PARAMETER
);
1130 for (i
= 0; !ret
&& i
< cAttr
; i
++)
1131 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
1136 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
1137 CERT_EXTENSION rgExtensions
[])
1139 PCERT_EXTENSION ret
= NULL
;
1142 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
1148 SetLastError(ERROR_INVALID_PARAMETER
);
1152 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
1153 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
1154 rgExtensions
[i
].pszObjId
))
1155 ret
= &rgExtensions
[i
];
1159 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
1161 PCERT_RDN_ATTR ret
= NULL
;
1164 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
1168 SetLastError(ERROR_INVALID_PARAMETER
);
1172 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
1173 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
1174 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
1175 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
1176 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
1180 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
1181 PCERT_INFO pCertInfo
)
1190 GetSystemTime(&sysTime
);
1191 SystemTimeToFileTime(&sysTime
, &fileTime
);
1192 pTimeToVerify
= &fileTime
;
1194 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
1196 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
1203 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
1204 PCERT_INFO pIssuerInfo
)
1206 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
1208 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
1209 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
1212 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
1213 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
1214 DWORD
*pcbComputedHash
)
1217 HCRYPTHASH hHash
= 0;
1219 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
1220 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
1223 hCryptProv
= CRYPT_GetDefaultProvider();
1228 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
1231 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
1233 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
1234 pcbComputedHash
, 0);
1235 CryptDestroyHash(hHash
);
1241 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
, ALG_ID Algid
,
1242 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
1243 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
1246 HCRYPTHASH hHash
= 0;
1248 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
1249 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
1252 hCryptProv
= CRYPT_GetDefaultProvider();
1260 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_PUBLIC_KEY_INFO
,
1261 pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &buf
, &size
);
1264 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
1267 ret
= CryptHashData(hHash
, buf
, size
, 0);
1269 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
1270 pcbComputedHash
, 0);
1271 CryptDestroyHash(hHash
);
1279 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
1280 DWORD dwKeySpec
, DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
1281 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
1282 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
1285 PCCRYPT_OID_INFO info
;
1288 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
1289 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
1290 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
1292 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
1293 pSignatureAlgorithm
->pszObjId
, 0);
1296 SetLastError(NTE_BAD_ALGID
);
1299 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
1302 hCryptProv
= CRYPT_GetDefaultProvider();
1303 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
1306 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
1307 cbEncodedToBeSigned
, 0);
1309 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
1311 CryptDestroyHash(hHash
);
1318 SetLastError(ERROR_INVALID_PARAMETER
);
1323 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
1326 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
1327 cbEncodedToBeSigned
, 0);
1329 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
1331 CryptDestroyHash(hHash
);
1338 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProv
,
1339 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
1340 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
1341 const void *pvHashAuxInfo
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
)
1344 DWORD encodedSize
, hashSize
;
1346 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
1347 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
1348 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
1350 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
1351 NULL
, &encodedSize
);
1354 PBYTE encoded
= CryptMemAlloc(encodedSize
);
1358 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
1359 pvStructInfo
, encoded
, &encodedSize
);
1362 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
1363 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
1364 pvHashAuxInfo
, NULL
, &hashSize
);
1367 PBYTE hash
= CryptMemAlloc(hashSize
);
1371 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
1372 dwCertEncodingType
, encoded
, encodedSize
,
1373 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
1376 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
1378 info
.ToBeSigned
.cbData
= encodedSize
;
1379 info
.ToBeSigned
.pbData
= encoded
;
1380 memcpy(&info
.SignatureAlgorithm
,
1381 pSignatureAlgorithm
,
1382 sizeof(info
.SignatureAlgorithm
));
1383 info
.Signature
.cbData
= hashSize
;
1384 info
.Signature
.pbData
= hash
;
1385 info
.Signature
.cUnusedBits
= 0;
1386 ret
= CryptEncodeObject(dwCertEncodingType
,
1387 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
1393 CryptMemFree(encoded
);
1399 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV_LEGACY hCryptProv
,
1400 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
1401 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1403 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
1404 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, (void *)pbEncoded
,
1405 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
1408 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV_LEGACY hCryptProv
,
1409 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pubKeyInfo
,
1410 const CERT_SIGNED_CONTENT_INFO
*signedCert
)
1414 PCCRYPT_OID_INFO info
;
1415 ALG_ID pubKeyID
, hashID
;
1417 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
1418 signedCert
->SignatureAlgorithm
.pszObjId
, 0);
1419 if (!info
|| info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
)
1421 SetLastError(NTE_BAD_ALGID
);
1424 hashID
= info
->u
.Algid
;
1425 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
1426 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
1429 /* Load the default provider if necessary */
1431 hCryptProv
= CRYPT_GetDefaultProvider();
1432 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
1433 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
1438 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
1441 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
1442 signedCert
->ToBeSigned
.cbData
, 0);
1444 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
1445 signedCert
->Signature
.cbData
, key
, NULL
, 0);
1446 CryptDestroyHash(hash
);
1448 CryptDestroyKey(key
);
1453 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV_LEGACY hCryptProv
,
1454 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
1455 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
1458 CRYPT_DATA_BLOB subjectBlob
;
1460 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
1461 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
1462 dwFlags
, pvReserved
);
1464 switch (dwSubjectType
)
1466 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
1468 PCRYPT_DATA_BLOB blob
= (PCRYPT_DATA_BLOB
)pvSubject
;
1470 subjectBlob
.pbData
= blob
->pbData
;
1471 subjectBlob
.cbData
= blob
->cbData
;
1474 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
1476 PCERT_CONTEXT context
= (PCERT_CONTEXT
)pvSubject
;
1478 subjectBlob
.pbData
= context
->pbCertEncoded
;
1479 subjectBlob
.cbData
= context
->cbCertEncoded
;
1482 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
1484 PCRL_CONTEXT context
= (PCRL_CONTEXT
)pvSubject
;
1486 subjectBlob
.pbData
= context
->pbCrlEncoded
;
1487 subjectBlob
.cbData
= context
->cbCrlEncoded
;
1491 SetLastError(E_INVALIDARG
);
1497 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
1500 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
1501 subjectBlob
.pbData
, subjectBlob
.cbData
,
1502 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1503 (BYTE
*)&signedCert
, &size
);
1506 switch (dwIssuerType
)
1508 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
1509 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
1510 dwCertEncodingType
, (PCERT_PUBLIC_KEY_INFO
)pvIssuer
,
1513 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
1514 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
1516 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
1519 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
1520 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
1523 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
1526 SetLastError(E_INVALIDARG
);
1531 FIXME("unimplemented for NULL signer\n");
1532 SetLastError(E_INVALIDARG
);
1537 SetLastError(E_INVALIDARG
);
1540 LocalFree(signedCert
);
1546 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
1547 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
1549 PCERT_ENHKEY_USAGE usage
= NULL
;
1553 if (!pCertContext
|| !pcbUsage
)
1555 SetLastError(ERROR_INVALID_PARAMETER
);
1559 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
1561 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
1565 if (CertGetCertificateContextProperty(pCertContext
,
1566 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
1568 LPBYTE buf
= CryptMemAlloc(propSize
);
1572 if (CertGetCertificateContextProperty(pCertContext
,
1573 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
1575 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
1576 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
1577 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
1583 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
1585 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
1586 pCertContext
->pCertInfo
->cExtension
,
1587 pCertContext
->pCertInfo
->rgExtension
);
1591 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
1592 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1593 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
1598 /* If a particular location is specified, this should fail. Otherwise
1599 * it should succeed with an empty usage. (This is true on Win2k and
1600 * later, which we emulate.)
1604 SetLastError(CRYPT_E_NOT_FOUND
);
1608 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
1614 *pcbUsage
= bytesNeeded
;
1615 else if (*pcbUsage
< bytesNeeded
)
1617 SetLastError(ERROR_MORE_DATA
);
1618 *pcbUsage
= bytesNeeded
;
1623 *pcbUsage
= bytesNeeded
;
1627 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
1628 sizeof(CERT_ENHKEY_USAGE
) +
1629 usage
->cUsageIdentifier
* sizeof(LPSTR
));
1631 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
1632 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
1633 sizeof(CERT_ENHKEY_USAGE
));
1634 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
1636 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
1637 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
1638 nextOID
+= strlen(nextOID
) + 1;
1642 pUsage
->cUsageIdentifier
= 0;
1647 TRACE("returning %d\n", ret
);
1651 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
1652 PCERT_ENHKEY_USAGE pUsage
)
1656 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
1660 CRYPT_DATA_BLOB blob
= { 0, NULL
};
1662 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
1663 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
1666 ret
= CertSetCertificateContextProperty(pCertContext
,
1667 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
1668 LocalFree(blob
.pbData
);
1672 ret
= CertSetCertificateContextProperty(pCertContext
,
1673 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
1677 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
1678 LPCSTR pszUsageIdentifier
)
1683 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
1685 if (CertGetEnhancedKeyUsage(pCertContext
,
1686 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
1688 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
1692 ret
= CertGetEnhancedKeyUsage(pCertContext
,
1693 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
1696 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
1697 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
1704 newUsage
->rgpszUsageIdentifier
=
1705 (LPSTR
*)((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
1706 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
+
1707 (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
1708 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
1710 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
1711 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
1712 nextOID
+= strlen(nextOID
) + 1;
1714 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
1715 strcpy(nextOID
, pszUsageIdentifier
);
1716 newUsage
->cUsageIdentifier
= i
+ 1;
1717 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
1718 CryptMemFree(newUsage
);
1721 CryptMemFree(usage
);
1728 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
1729 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
1733 usage
->rgpszUsageIdentifier
=
1734 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
1735 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
1736 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
1737 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
1738 usage
->cUsageIdentifier
= 1;
1739 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
1740 CryptMemFree(usage
);
1748 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
1749 LPCSTR pszUsageIdentifier
)
1753 CERT_ENHKEY_USAGE usage
;
1755 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
1757 size
= sizeof(usage
);
1758 ret
= CertGetEnhancedKeyUsage(pCertContext
,
1759 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
1760 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
1762 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
1766 ret
= CertGetEnhancedKeyUsage(pCertContext
,
1767 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
1770 if (pUsage
->cUsageIdentifier
)
1775 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
1777 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
1778 pszUsageIdentifier
))
1780 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
1781 pUsage
->rgpszUsageIdentifier
[i
] =
1782 pUsage
->rgpszUsageIdentifier
[i
+ 1];
1784 pUsage
->cUsageIdentifier
--;
1785 /* Remove the usage if it's empty */
1786 if (pUsage
->cUsageIdentifier
)
1787 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
1789 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
1792 CryptMemFree(pUsage
);
1799 /* it fit in an empty usage, therefore there's nothing to remove */
1805 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
1806 int *cNumOIDSs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
1809 DWORD i
, cbOIDs
= 0;
1810 BOOL allUsagesValid
= TRUE
;
1811 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
1813 TRACE("(%d, %p, %p, %p, %d)\n", cCerts
, *rghCerts
, cNumOIDSs
,
1816 for (i
= 0; ret
&& i
< cCerts
; i
++)
1818 CERT_ENHKEY_USAGE usage
;
1819 DWORD size
= sizeof(usage
);
1821 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
1822 /* Success is deliberately ignored: it implies all usages are valid */
1823 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
1825 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
1827 allUsagesValid
= FALSE
;
1830 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
1833 if (!validUsages
.cUsageIdentifier
)
1837 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
1838 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
1839 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
1840 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
1842 validUsages
.rgpszUsageIdentifier
=
1843 CryptMemAlloc(cbOIDs
);
1844 if (validUsages
.rgpszUsageIdentifier
)
1846 LPSTR nextOID
= (LPSTR
)
1847 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
1848 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
1850 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
1852 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
1853 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
1854 pUsage
->rgpszUsageIdentifier
[j
]);
1855 nextOID
+= lstrlenA(nextOID
) + 1;
1863 DWORD j
, k
, validIndexes
= 0, numRemoved
= 0;
1865 /* Merge: build a bitmap of all the indexes of
1866 * validUsages.rgpszUsageIdentifier that are in pUsage.
1868 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
1870 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
1872 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
1873 validUsages
.rgpszUsageIdentifier
[k
]))
1875 validIndexes
|= (1 << k
);
1880 /* Merge by removing from validUsages those that are
1881 * not in the bitmap.
1883 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
1885 if (!(validIndexes
& (1 << j
)))
1887 if (j
< validUsages
.cUsageIdentifier
- 1)
1889 memcpy(&validUsages
.rgpszUsageIdentifier
[j
],
1890 &validUsages
.rgpszUsageIdentifier
[j
+
1892 (validUsages
.cUsageIdentifier
- numRemoved
1893 - j
- 1) * sizeof(LPSTR
));
1895 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
1900 validUsages
.cUsageIdentifier
--;
1905 CryptMemFree(pUsage
);
1920 if (!rghOIDs
|| *pcbOIDs
< cbOIDs
)
1923 SetLastError(ERROR_MORE_DATA
);
1928 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
1929 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
1932 *cNumOIDSs
= validUsages
.cUsageIdentifier
;
1933 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
1935 rghOIDs
[i
] = nextOID
;
1936 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
1937 nextOID
+= lstrlenA(nextOID
) + 1;
1942 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
1946 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
1947 * pInfo is NULL, from the attributes of hProv.
1949 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
1950 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
1952 CRYPT_KEY_PROV_INFO info
= { 0 };
1960 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
1963 LPSTR szContainer
= CryptMemAlloc(size
);
1967 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
1968 (BYTE
*)szContainer
, &size
, 0);
1971 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
1975 info
.pwszContainerName
= CryptMemAlloc(len
*
1977 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
1978 info
.pwszContainerName
, len
);
1981 CryptMemFree(szContainer
);
1984 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
1987 LPSTR szProvider
= CryptMemAlloc(size
);
1991 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
1995 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
1999 info
.pwszProvName
= CryptMemAlloc(len
*
2001 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
2002 info
.pwszProvName
, len
);
2005 CryptMemFree(szProvider
);
2008 size
= sizeof(info
.dwKeySpec
);
2009 /* in case no CRYPT_KEY_PROV_INFO given,
2010 * we always use AT_SIGNATURE key spec
2012 info
.dwKeySpec
= AT_SIGNATURE
;
2013 size
= sizeof(info
.dwProvType
);
2014 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
2017 info
.dwProvType
= PROV_RSA_FULL
;
2021 ret
= CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
2026 CryptMemFree(info
.pwszContainerName
);
2027 CryptMemFree(info
.pwszProvName
);
2031 /* Creates a signed certificate context from the unsigned, encoded certificate
2032 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
2034 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
2035 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
2037 PCCERT_CONTEXT context
= NULL
;
2041 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2042 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
2045 LPBYTE sig
= CryptMemAlloc(sigSize
);
2047 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2048 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
2051 CERT_SIGNED_CONTENT_INFO signedInfo
;
2052 BYTE
*encodedSignedCert
= NULL
;
2053 DWORD encodedSignedCertSize
= 0;
2055 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
2056 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
2057 memcpy(&signedInfo
.SignatureAlgorithm
, sigAlgo
,
2058 sizeof(signedInfo
.SignatureAlgorithm
));
2059 signedInfo
.Signature
.cbData
= sigSize
;
2060 signedInfo
.Signature
.pbData
= sig
;
2061 signedInfo
.Signature
.cUnusedBits
= 0;
2062 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
2063 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2064 (BYTE
*)&encodedSignedCert
, &encodedSignedCertSize
);
2067 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2068 encodedSignedCert
, encodedSignedCertSize
);
2069 LocalFree(encodedSignedCert
);
2077 /* Copies data from the parameters into info, where:
2078 * pSerialNumber: The serial number. Must not be NULL.
2079 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
2081 * pSignatureAlgorithm: Optional.
2082 * pStartTime: The starting time of the certificate. If NULL, the current
2083 * system time is used.
2084 * pEndTime: The ending time of the certificate. If NULL, one year past the
2085 * starting time is used.
2086 * pubKey: The public key of the certificate. Must not be NULL.
2087 * pExtensions: Extensions to be included with the certificate. Optional.
2089 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
2090 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
2091 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
2092 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
2093 const CERT_EXTENSIONS
*pExtensions
)
2095 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
2098 assert(pSerialNumber
);
2099 assert(pSubjectIssuerBlob
);
2102 info
->dwVersion
= CERT_V3
;
2103 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
2104 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
2105 if (pSignatureAlgorithm
)
2106 memcpy(&info
->SignatureAlgorithm
, pSignatureAlgorithm
,
2107 sizeof(info
->SignatureAlgorithm
));
2110 info
->SignatureAlgorithm
.pszObjId
= oid
;
2111 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
2112 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
2114 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
2115 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
2117 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
2119 GetSystemTimeAsFileTime(&info
->NotBefore
);
2121 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
2126 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
2129 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
2132 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
2133 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
2134 memcpy(&info
->SubjectPublicKeyInfo
, pubKey
,
2135 sizeof(info
->SubjectPublicKeyInfo
));
2138 info
->cExtension
= pExtensions
->cExtension
;
2139 info
->rgExtension
= pExtensions
->rgExtension
;
2143 info
->cExtension
= 0;
2144 info
->rgExtension
= NULL
;
2148 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
2149 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
2150 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
2152 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
2154 HCRYPTPROV hProv
= 0;
2155 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
2159 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
2161 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
2163 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
2164 rpcrt
, "RpcStringFreeA");
2166 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
2169 RPC_STATUS status
= uuidCreate(&uuid
);
2171 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
2173 unsigned char *uuidStr
;
2175 status
= uuidToString(&uuid
, &uuidStr
);
2176 if (status
== RPC_S_OK
)
2178 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
2179 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
2185 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
2187 CryptDestroyKey(key
);
2189 rpcStringFree(&uuidStr
);
2198 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hProv
,
2199 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
2200 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
2201 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
2202 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
2204 PCCERT_CONTEXT context
= NULL
;
2205 BOOL ret
, releaseContext
= FALSE
;
2206 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
2207 DWORD pubKeySize
= 0,dwKeySpec
= AT_SIGNATURE
;
2209 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
2210 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
2211 pExtensions
, pExtensions
);
2213 if(!pSubjectIssuerBlob
)
2215 SetLastError(ERROR_INVALID_PARAMETER
);
2223 hProv
= CRYPT_CreateKeyProv();
2224 releaseContext
= TRUE
;
2226 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
2228 SetLastError(NTE_BAD_FLAGS
);
2234 /* acquire the context using the given information*/
2235 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
2236 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
2237 pKeyProvInfo
->dwFlags
);
2240 if(GetLastError() != NTE_BAD_KEYSET
)
2242 /* create the key set */
2243 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
2244 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
2245 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
2249 dwKeySpec
= pKeyProvInfo
->dwKeySpec
;
2250 /* check if the key is here */
2251 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
2254 if (NTE_NO_KEY
== GetLastError())
2255 { /* generate the key */
2256 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
2260 CryptReleaseContext(hProv
,0);
2261 SetLastError(NTE_BAD_KEYSET
);
2265 CryptDestroyKey(hKey
);
2266 releaseContext
= TRUE
;
2269 else if (pKeyProvInfo
)
2271 SetLastError(ERROR_INVALID_PARAMETER
);
2275 CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
2277 pubKey
= CryptMemAlloc(pubKeySize
);
2280 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2281 pubKey
, &pubKeySize
);
2284 CERT_INFO info
= { 0 };
2285 CRYPT_DER_BLOB blob
= { 0, NULL
};
2287 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
2289 CryptGenRandom(hProv
, sizeof(serial
), serial
);
2290 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
2291 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
2292 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
2293 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, (BYTE
*)&blob
.pbData
,
2297 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
2298 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
2299 &info
.SignatureAlgorithm
);
2301 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2302 blob
.pbData
, blob
.cbData
);
2303 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
2304 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
2305 LocalFree(blob
.pbData
);
2308 CryptMemFree(pubKey
);
2311 CryptReleaseContext(hProv
, 0);
2315 BOOL WINAPI
CertGetCertificateChain(HCERTCHAINENGINE hChainEngine
,
2316 PCCERT_CONTEXT pCertContext
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
2317 PCERT_CHAIN_PARA pChainPara
, DWORD dwFlags
, LPVOID pvReserved
,
2318 PCCERT_CHAIN_CONTEXT
* ppChainContext
)
2320 FIXME(" %p %p %p %p %p 0x%08X %p %p - stub\n",hChainEngine
, pCertContext
,
2321 pTime
, hAdditionalStore
, pChainPara
, dwFlags
, pvReserved
, ppChainContext
);
2323 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);