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 WINAPI
CertContext_GetProperty(void *context
, DWORD dwPropId
,
171 void *pvData
, DWORD
*pcbData
)
173 PCCERT_CONTEXT pCertContext
= (PCCERT_CONTEXT
)context
;
174 PCONTEXT_PROPERTY_LIST properties
=
175 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
177 CRYPT_DATA_BLOB blob
;
179 TRACE("(%p, %d, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
182 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
, &blob
);
188 *pcbData
= blob
.cbData
;
189 else if (*pcbData
< blob
.cbData
)
191 SetLastError(ERROR_MORE_DATA
);
192 *pcbData
= blob
.cbData
;
197 memcpy(pvData
, blob
.pbData
, blob
.cbData
);
198 *pcbData
= blob
.cbData
;
203 /* Implicit properties */
206 case CERT_SHA1_HASH_PROP_ID
:
207 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
208 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
211 case CERT_MD5_HASH_PROP_ID
:
212 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
213 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
216 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
217 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
218 pCertContext
->pCertInfo
->Subject
.pbData
,
219 pCertContext
->pCertInfo
->Subject
.cbData
,
222 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
223 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
224 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
225 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
228 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
229 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
230 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
231 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
234 case CERT_SIGNATURE_HASH_PROP_ID
:
235 FIXME("CERT_SIGNATURE_HASH_PROP_ID unimplemented\n");
236 SetLastError(CRYPT_E_NOT_FOUND
);
239 SetLastError(CRYPT_E_NOT_FOUND
);
242 TRACE("returning %d\n", ret
);
246 void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
248 DWORD i
, containerLen
, provNameLen
;
249 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
251 info
->pwszContainerName
= (LPWSTR
)data
;
252 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
253 data
+= containerLen
;
255 info
->pwszProvName
= (LPWSTR
)data
;
256 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
259 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
260 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
262 for (i
= 0; i
< info
->cProvParam
; i
++)
264 info
->rgProvParam
[i
].pbData
= data
;
265 data
+= info
->rgProvParam
[i
].cbData
;
269 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
270 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
274 TRACE("(%p, %d, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
279 case CERT_CERT_PROP_ID
:
280 case CERT_CRL_PROP_ID
:
281 case CERT_CTL_PROP_ID
:
282 SetLastError(E_INVALIDARG
);
285 case CERT_ACCESS_STATE_PROP_ID
:
288 *pcbData
= sizeof(DWORD
);
291 else if (*pcbData
< sizeof(DWORD
))
293 SetLastError(ERROR_MORE_DATA
);
294 *pcbData
= sizeof(DWORD
);
299 if (pCertContext
->hCertStore
)
300 ret
= CertGetStoreProperty(pCertContext
->hCertStore
, dwPropId
,
303 *(DWORD
*)pvData
= 0;
307 case CERT_KEY_IDENTIFIER_PROP_ID
:
308 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
,
311 SetLastError(ERROR_INVALID_DATA
);
313 case CERT_KEY_PROV_HANDLE_PROP_ID
:
315 CERT_KEY_CONTEXT keyContext
;
316 DWORD size
= sizeof(keyContext
);
318 ret
= CertContext_GetProperty((void *)pCertContext
,
319 CERT_KEY_CONTEXT_PROP_ID
, &keyContext
, &size
);
323 *pcbData
= sizeof(HCRYPTPROV
);
324 else if (*pcbData
< sizeof(HCRYPTPROV
))
326 SetLastError(ERROR_MORE_DATA
);
327 *pcbData
= sizeof(HCRYPTPROV
);
331 *(HCRYPTPROV
*)pvData
= keyContext
.hCryptProv
;
335 case CERT_KEY_PROV_INFO_PROP_ID
:
336 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
339 CRYPT_FixKeyProvInfoPointers((PCRYPT_KEY_PROV_INFO
)pvData
);
342 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
346 TRACE("returning %d\n", ret
);
350 /* Copies key provider info from from into to, where to is assumed to be a
351 * contiguous buffer of memory large enough for from and all its associated
352 * data, but whose pointers are uninitialized.
353 * Upon return, to contains a contiguous copy of from, packed in the following
355 * - CRYPT_KEY_PROV_INFO
356 * - pwszContainerName
358 * - rgProvParam[0]...
360 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
361 const CRYPT_KEY_PROV_INFO
*from
)
364 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
366 if (from
->pwszContainerName
)
368 to
->pwszContainerName
= (LPWSTR
)nextData
;
369 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
370 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
373 to
->pwszContainerName
= NULL
;
374 if (from
->pwszProvName
)
376 to
->pwszProvName
= (LPWSTR
)nextData
;
377 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
378 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
381 to
->pwszProvName
= NULL
;
382 to
->dwProvType
= from
->dwProvType
;
383 to
->dwFlags
= from
->dwFlags
;
384 to
->cProvParam
= from
->cProvParam
;
385 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
386 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
387 to
->dwKeySpec
= from
->dwKeySpec
;
388 for (i
= 0; i
< to
->cProvParam
; i
++)
390 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
391 sizeof(CRYPT_KEY_PROV_PARAM
));
392 to
->rgProvParam
[i
].pbData
= nextData
;
393 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
394 from
->rgProvParam
[i
].cbData
);
395 nextData
+= from
->rgProvParam
[i
].cbData
;
399 static BOOL
CertContext_SetKeyProvInfoProperty(PCONTEXT_PROPERTY_LIST properties
,
400 const CRYPT_KEY_PROV_INFO
*info
)
404 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
406 if (info
->pwszContainerName
)
407 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
410 if (info
->pwszProvName
)
411 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
414 size
+= containerSize
+ provNameSize
;
415 for (i
= 0; i
< info
->cProvParam
; i
++)
416 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
417 buf
= CryptMemAlloc(size
);
420 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
421 ret
= ContextPropertyList_SetProperty(properties
,
422 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
430 static BOOL WINAPI
CertContext_SetProperty(void *context
, DWORD dwPropId
,
431 DWORD dwFlags
, const void *pvData
)
433 PCONTEXT_PROPERTY_LIST properties
=
434 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
437 TRACE("(%p, %d, %08x, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
445 case CERT_AUTO_ENROLL_PROP_ID
:
446 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
447 case CERT_DESCRIPTION_PROP_ID
:
448 case CERT_FRIENDLY_NAME_PROP_ID
:
449 case CERT_HASH_PROP_ID
:
450 case CERT_KEY_IDENTIFIER_PROP_ID
:
451 case CERT_MD5_HASH_PROP_ID
:
452 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
453 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
454 case CERT_PVK_FILE_PROP_ID
:
455 case CERT_SIGNATURE_HASH_PROP_ID
:
456 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
457 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
458 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
459 case CERT_ENROLLMENT_PROP_ID
:
460 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
461 case CERT_RENEWAL_PROP_ID
:
465 const CRYPT_DATA_BLOB
*blob
= (const CRYPT_DATA_BLOB
*)pvData
;
467 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
468 blob
->pbData
, blob
->cbData
);
472 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
477 case CERT_DATE_STAMP_PROP_ID
:
479 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
480 (const BYTE
*)pvData
, sizeof(FILETIME
));
483 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
487 case CERT_KEY_CONTEXT_PROP_ID
:
491 const CERT_KEY_CONTEXT
*keyContext
= (const CERT_KEY_CONTEXT
*)pvData
;
493 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
494 (const BYTE
*)keyContext
, keyContext
->cbSize
);
498 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
503 case CERT_KEY_PROV_INFO_PROP_ID
:
505 ret
= CertContext_SetKeyProvInfoProperty(properties
,
506 (const CRYPT_KEY_PROV_INFO
*)pvData
);
509 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
513 case CERT_KEY_PROV_HANDLE_PROP_ID
:
515 CERT_KEY_CONTEXT keyContext
;
516 DWORD size
= sizeof(keyContext
);
518 ret
= CertContext_GetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
522 if (!(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
523 CryptReleaseContext(keyContext
.hCryptProv
, 0);
525 keyContext
.hCryptProv
= *(const HCRYPTPROV
*)pvData
;
527 keyContext
.hCryptProv
= 0;
528 ret
= CertContext_SetProperty(context
, CERT_KEY_CONTEXT_PROP_ID
,
534 FIXME("%d: stub\n", dwPropId
);
538 TRACE("returning %d\n", ret
);
542 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
543 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
547 TRACE("(%p, %d, %08x, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
549 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
550 * crashes on most of these, I'll be safer.
555 case CERT_ACCESS_STATE_PROP_ID
:
556 case CERT_CERT_PROP_ID
:
557 case CERT_CRL_PROP_ID
:
558 case CERT_CTL_PROP_ID
:
559 SetLastError(E_INVALIDARG
);
562 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
564 TRACE("returning %d\n", ret
);
568 /* Acquires the private key using the key provider info, retrieving info from
569 * the certificate if info is NULL. The acquired provider is returned in
570 * *phCryptProv, and the key spec for the provider is returned in *pdwKeySpec.
572 static BOOL
CRYPT_AcquirePrivateKeyFromProvInfo(PCCERT_CONTEXT pCert
,
573 PCRYPT_KEY_PROV_INFO info
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
)
576 BOOL allocated
= FALSE
, ret
= TRUE
;
580 ret
= CertGetCertificateContextProperty(pCert
,
581 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
584 info
= (PCRYPT_KEY_PROV_INFO
)HeapAlloc(GetProcessHeap(), 0, size
);
587 ret
= CertGetCertificateContextProperty(pCert
,
588 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
593 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
597 ret
= CryptAcquireContextW(phCryptProv
, info
->pwszContainerName
,
598 info
->pwszProvName
, info
->dwProvType
, 0);
603 for (i
= 0; i
< info
->cProvParam
; i
++)
605 CryptSetProvParam(*phCryptProv
,
606 info
->rgProvParam
[i
].dwParam
, info
->rgProvParam
[i
].pbData
,
607 info
->rgProvParam
[i
].dwFlags
);
609 *pdwKeySpec
= info
->dwKeySpec
;
612 SetLastError(CRYPT_E_NO_KEY_PROPERTY
);
615 HeapFree(GetProcessHeap(), 0, info
);
619 BOOL WINAPI
CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert
,
620 DWORD dwFlags
, void *pvReserved
, HCRYPTPROV
*phCryptProv
, DWORD
*pdwKeySpec
,
621 BOOL
*pfCallerFreeProv
)
623 BOOL ret
= FALSE
, cache
= FALSE
;
624 PCRYPT_KEY_PROV_INFO info
= NULL
;
625 CERT_KEY_CONTEXT keyContext
;
628 TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert
, dwFlags
, pvReserved
,
629 phCryptProv
, pdwKeySpec
, pfCallerFreeProv
);
631 if (dwFlags
& CRYPT_ACQUIRE_USE_PROV_INFO_FLAG
)
635 ret
= CertGetCertificateContextProperty(pCert
,
636 CERT_KEY_PROV_INFO_PROP_ID
, 0, &size
);
639 info
= (PCRYPT_KEY_PROV_INFO
)HeapAlloc(
640 GetProcessHeap(), 0, size
);
641 ret
= CertGetCertificateContextProperty(pCert
,
642 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
);
644 cache
= info
->dwFlags
& CERT_SET_KEY_CONTEXT_PROP_ID
;
647 else if (dwFlags
& CRYPT_ACQUIRE_CACHE_FLAG
)
652 size
= sizeof(keyContext
);
653 ret
= CertGetCertificateContextProperty(pCert
, CERT_KEY_CONTEXT_PROP_ID
,
657 *phCryptProv
= keyContext
.hCryptProv
;
659 *pdwKeySpec
= keyContext
.dwKeySpec
;
660 if (pfCallerFreeProv
)
661 *pfCallerFreeProv
= !cache
;
666 ret
= CRYPT_AcquirePrivateKeyFromProvInfo(pCert
, info
,
667 &keyContext
.hCryptProv
, &keyContext
.dwKeySpec
);
670 *phCryptProv
= keyContext
.hCryptProv
;
672 *pdwKeySpec
= keyContext
.dwKeySpec
;
675 keyContext
.cbSize
= sizeof(keyContext
);
676 if (CertSetCertificateContextProperty(pCert
,
677 CERT_KEY_CONTEXT_PROP_ID
, 0, &keyContext
))
679 if (pfCallerFreeProv
)
680 *pfCallerFreeProv
= FALSE
;
685 if (pfCallerFreeProv
)
686 *pfCallerFreeProv
= TRUE
;
690 HeapFree(GetProcessHeap(), 0, info
);
694 BOOL WINAPI
CertCompareCertificate(DWORD dwCertEncodingType
,
695 PCERT_INFO pCertId1
, PCERT_INFO pCertId2
)
697 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertId1
, pCertId2
);
699 return CertCompareCertificateName(dwCertEncodingType
, &pCertId1
->Issuer
,
700 &pCertId2
->Issuer
) && CertCompareIntegerBlob(&pCertId1
->SerialNumber
,
701 &pCertId2
->SerialNumber
);
704 BOOL WINAPI
CertCompareCertificateName(DWORD dwCertEncodingType
,
705 PCERT_NAME_BLOB pCertName1
, PCERT_NAME_BLOB pCertName2
)
709 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pCertName1
, pCertName2
);
711 if (pCertName1
->cbData
== pCertName2
->cbData
)
713 if (pCertName1
->cbData
)
714 ret
= !memcmp(pCertName1
->pbData
, pCertName2
->pbData
,
724 /* Returns the number of significant bytes in pInt, where a byte is
725 * insignificant if it's a leading 0 for positive numbers or a leading 0xff
726 * for negative numbers. pInt is assumed to be little-endian.
728 static DWORD
CRYPT_significantBytes(const CRYPT_INTEGER_BLOB
*pInt
)
730 DWORD ret
= pInt
->cbData
;
734 if (pInt
->pbData
[ret
- 2] <= 0x7f && pInt
->pbData
[ret
- 1] == 0)
736 else if (pInt
->pbData
[ret
- 2] >= 0x80 && pInt
->pbData
[ret
- 1] == 0xff)
744 BOOL WINAPI
CertCompareIntegerBlob(PCRYPT_INTEGER_BLOB pInt1
,
745 PCRYPT_INTEGER_BLOB pInt2
)
750 TRACE("(%p, %p)\n", pInt1
, pInt2
);
752 cb1
= CRYPT_significantBytes(pInt1
);
753 cb2
= CRYPT_significantBytes(pInt2
);
757 ret
= !memcmp(pInt1
->pbData
, pInt1
->pbData
, cb1
);
766 BOOL WINAPI
CertComparePublicKeyInfo(DWORD dwCertEncodingType
,
767 PCERT_PUBLIC_KEY_INFO pPublicKey1
, PCERT_PUBLIC_KEY_INFO pPublicKey2
)
771 TRACE("(%08x, %p, %p)\n", dwCertEncodingType
, pPublicKey1
, pPublicKey2
);
773 if (pPublicKey1
->PublicKey
.cbData
== pPublicKey2
->PublicKey
.cbData
&&
774 pPublicKey1
->PublicKey
.cUnusedBits
== pPublicKey2
->PublicKey
.cUnusedBits
)
776 if (pPublicKey2
->PublicKey
.cbData
)
777 ret
= !memcmp(pPublicKey1
->PublicKey
.pbData
,
778 pPublicKey2
->PublicKey
.pbData
, pPublicKey1
->PublicKey
.cbData
);
787 DWORD WINAPI
CertGetPublicKeyLength(DWORD dwCertEncodingType
,
788 PCERT_PUBLIC_KEY_INFO pPublicKey
)
792 TRACE("(%08x, %p)\n", dwCertEncodingType
, pPublicKey
);
794 if (dwCertEncodingType
!= X509_ASN_ENCODING
)
796 SetLastError(ERROR_FILE_NOT_FOUND
);
799 if (pPublicKey
->Algorithm
.pszObjId
&&
800 !strcmp(pPublicKey
->Algorithm
.pszObjId
, szOID_RSA_DH
))
802 FIXME("unimplemented for DH public keys\n");
803 SetLastError(CRYPT_E_ASN1_BADTAG
);
809 BOOL ret
= CryptDecodeObjectEx(dwCertEncodingType
,
810 RSA_CSP_PUBLICKEYBLOB
, pPublicKey
->PublicKey
.pbData
,
811 pPublicKey
->PublicKey
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &buf
,
816 RSAPUBKEY
*rsaPubKey
= (RSAPUBKEY
*)((LPBYTE
)buf
+
819 len
= rsaPubKey
->bitlen
;
826 typedef BOOL (*CertCompareFunc
)(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
827 DWORD dwFlags
, const void *pvPara
);
829 static BOOL
compare_cert_any(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
830 DWORD dwFlags
, const void *pvPara
)
835 static BOOL
compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
836 DWORD dwFlags
, const void *pvPara
)
840 DWORD size
= sizeof(hash
);
842 ret
= CertGetCertificateContextProperty(pCertContext
,
843 CERT_MD5_HASH_PROP_ID
, hash
, &size
);
846 const CRYPT_HASH_BLOB
*pHash
= (const CRYPT_HASH_BLOB
*)pvPara
;
848 if (size
== pHash
->cbData
)
849 ret
= !memcmp(pHash
->pbData
, hash
, size
);
856 static BOOL
compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
857 DWORD dwFlags
, const void *pvPara
)
861 DWORD size
= sizeof(hash
);
863 ret
= CertGetCertificateContextProperty(pCertContext
,
864 CERT_SHA1_HASH_PROP_ID
, hash
, &size
);
867 const CRYPT_HASH_BLOB
*pHash
= (const CRYPT_HASH_BLOB
*)pvPara
;
869 if (size
== pHash
->cbData
)
870 ret
= !memcmp(pHash
->pbData
, hash
, size
);
877 static BOOL
compare_cert_by_name(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
878 DWORD dwFlags
, const void *pvPara
)
880 CERT_NAME_BLOB
*blob
= (CERT_NAME_BLOB
*)pvPara
, *toCompare
;
883 if (dwType
& CERT_INFO_SUBJECT_FLAG
)
884 toCompare
= &pCertContext
->pCertInfo
->Subject
;
886 toCompare
= &pCertContext
->pCertInfo
->Issuer
;
887 ret
= CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
892 static BOOL
compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext
,
893 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
895 CERT_INFO
*pCertInfo
= (CERT_INFO
*)pvPara
;
897 return CertCompareCertificateName(pCertContext
->dwCertEncodingType
,
898 &pCertInfo
->Issuer
, &pCertContext
->pCertInfo
->Subject
);
901 static BOOL
compare_cert_by_issuer(PCCERT_CONTEXT pCertContext
,
902 DWORD dwType
, DWORD dwFlags
, const void *pvPara
)
904 return compare_cert_by_subject_cert(pCertContext
, dwType
, dwFlags
,
905 ((PCCERT_CONTEXT
)pvPara
)->pCertInfo
);
908 PCCERT_CONTEXT WINAPI
CertFindCertificateInStore(HCERTSTORE hCertStore
,
909 DWORD dwCertEncodingType
, DWORD dwFlags
, DWORD dwType
, const void *pvPara
,
910 PCCERT_CONTEXT pPrevCertContext
)
913 CertCompareFunc compare
;
915 TRACE("(%p, %d, %d, %d, %p, %p)\n", hCertStore
, dwCertEncodingType
,
916 dwFlags
, dwType
, pvPara
, pPrevCertContext
);
918 switch (dwType
>> CERT_COMPARE_SHIFT
)
920 case CERT_COMPARE_ANY
:
921 compare
= compare_cert_any
;
923 case CERT_COMPARE_MD5_HASH
:
924 compare
= compare_cert_by_md5_hash
;
926 case CERT_COMPARE_SHA1_HASH
:
927 compare
= compare_cert_by_sha1_hash
;
929 case CERT_COMPARE_NAME
:
930 compare
= compare_cert_by_name
;
932 case CERT_COMPARE_SUBJECT_CERT
:
933 compare
= compare_cert_by_subject_cert
;
935 case CERT_COMPARE_ISSUER_OF
:
936 compare
= compare_cert_by_issuer
;
939 FIXME("find type %08x unimplemented\n", dwType
);
945 BOOL matches
= FALSE
;
947 ret
= pPrevCertContext
;
949 ret
= CertEnumCertificatesInStore(hCertStore
, ret
);
951 matches
= compare(ret
, dwType
, dwFlags
, pvPara
);
952 } while (ret
!= NULL
&& !matches
);
954 SetLastError(CRYPT_E_NOT_FOUND
);
958 SetLastError(CRYPT_E_NOT_FOUND
);
964 PCCERT_CONTEXT WINAPI
CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore
,
965 DWORD dwCertEncodingType
, PCERT_INFO pCertId
)
967 TRACE("(%p, %08x, %p)\n", hCertStore
, dwCertEncodingType
, pCertId
);
971 SetLastError(E_INVALIDARG
);
974 return CertFindCertificateInStore(hCertStore
, dwCertEncodingType
, 0,
975 CERT_FIND_SUBJECT_CERT
, pCertId
, NULL
);
978 BOOL WINAPI
CertVerifySubjectCertificateContext(PCCERT_CONTEXT pSubject
,
979 PCCERT_CONTEXT pIssuer
, DWORD
*pdwFlags
)
981 static const DWORD supportedFlags
= CERT_STORE_REVOCATION_FLAG
|
982 CERT_STORE_SIGNATURE_FLAG
| CERT_STORE_TIME_VALIDITY_FLAG
;
984 if (*pdwFlags
& ~supportedFlags
)
986 SetLastError(E_INVALIDARG
);
989 if (*pdwFlags
& CERT_STORE_REVOCATION_FLAG
)
992 PCCRL_CONTEXT crl
= CertGetCRLFromStore(pSubject
->hCertStore
, pSubject
,
995 /* FIXME: what if the CRL has expired? */
998 if (CertVerifyCRLRevocation(pSubject
->dwCertEncodingType
,
999 pSubject
->pCertInfo
, 1, (PCRL_INFO
*)&crl
->pCrlInfo
))
1000 *pdwFlags
&= CERT_STORE_REVOCATION_FLAG
;
1003 *pdwFlags
|= CERT_STORE_NO_CRL_FLAG
;
1005 if (*pdwFlags
& CERT_STORE_TIME_VALIDITY_FLAG
)
1007 if (0 == CertVerifyTimeValidity(NULL
, pSubject
->pCertInfo
))
1008 *pdwFlags
&= ~CERT_STORE_TIME_VALIDITY_FLAG
;
1010 if (*pdwFlags
& CERT_STORE_SIGNATURE_FLAG
)
1012 if (CryptVerifyCertificateSignatureEx(0, pSubject
->dwCertEncodingType
,
1013 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)pSubject
,
1014 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)pIssuer
, 0, NULL
))
1015 *pdwFlags
&= ~CERT_STORE_SIGNATURE_FLAG
;
1020 PCCERT_CONTEXT WINAPI
CertGetIssuerCertificateFromStore(HCERTSTORE hCertStore
,
1021 PCCERT_CONTEXT pSubjectContext
, PCCERT_CONTEXT pPrevIssuerContext
,
1026 TRACE("(%p, %p, %p, %08x)\n", hCertStore
, pSubjectContext
,
1027 pPrevIssuerContext
, *pdwFlags
);
1029 if (!pSubjectContext
)
1031 SetLastError(E_INVALIDARG
);
1035 ret
= CertFindCertificateInStore(hCertStore
,
1036 pSubjectContext
->dwCertEncodingType
, 0, CERT_FIND_ISSUER_OF
,
1037 pSubjectContext
, pPrevIssuerContext
);
1040 if (!CertVerifySubjectCertificateContext(pSubjectContext
, ret
,
1043 CertFreeCertificateContext(ret
);
1051 PCRYPT_ATTRIBUTE WINAPI
CertFindAttribute(LPCSTR pszObjId
, DWORD cAttr
,
1052 CRYPT_ATTRIBUTE rgAttr
[])
1054 PCRYPT_ATTRIBUTE ret
= NULL
;
1057 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cAttr
, rgAttr
);
1063 SetLastError(ERROR_INVALID_PARAMETER
);
1067 for (i
= 0; !ret
&& i
< cAttr
; i
++)
1068 if (rgAttr
[i
].pszObjId
&& !strcmp(pszObjId
, rgAttr
[i
].pszObjId
))
1073 PCERT_EXTENSION WINAPI
CertFindExtension(LPCSTR pszObjId
, DWORD cExtensions
,
1074 CERT_EXTENSION rgExtensions
[])
1076 PCERT_EXTENSION ret
= NULL
;
1079 TRACE("%s %d %p\n", debugstr_a(pszObjId
), cExtensions
, rgExtensions
);
1085 SetLastError(ERROR_INVALID_PARAMETER
);
1089 for (i
= 0; !ret
&& i
< cExtensions
; i
++)
1090 if (rgExtensions
[i
].pszObjId
&& !strcmp(pszObjId
,
1091 rgExtensions
[i
].pszObjId
))
1092 ret
= &rgExtensions
[i
];
1096 PCERT_RDN_ATTR WINAPI
CertFindRDNAttr(LPCSTR pszObjId
, PCERT_NAME_INFO pName
)
1098 PCERT_RDN_ATTR ret
= NULL
;
1101 TRACE("%s %p\n", debugstr_a(pszObjId
), pName
);
1105 SetLastError(ERROR_INVALID_PARAMETER
);
1109 for (i
= 0; !ret
&& i
< pName
->cRDN
; i
++)
1110 for (j
= 0; !ret
&& j
< pName
->rgRDN
[i
].cRDNAttr
; j
++)
1111 if (pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
&& !strcmp(pszObjId
,
1112 pName
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
1113 ret
= &pName
->rgRDN
[i
].rgRDNAttr
[j
];
1117 LONG WINAPI
CertVerifyTimeValidity(LPFILETIME pTimeToVerify
,
1118 PCERT_INFO pCertInfo
)
1127 GetSystemTime(&sysTime
);
1128 SystemTimeToFileTime(&sysTime
, &fileTime
);
1129 pTimeToVerify
= &fileTime
;
1131 if ((ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotBefore
)) >= 0)
1133 ret
= CompareFileTime(pTimeToVerify
, &pCertInfo
->NotAfter
);
1140 BOOL WINAPI
CertVerifyValidityNesting(PCERT_INFO pSubjectInfo
,
1141 PCERT_INFO pIssuerInfo
)
1143 TRACE("(%p, %p)\n", pSubjectInfo
, pIssuerInfo
);
1145 return CertVerifyTimeValidity(&pSubjectInfo
->NotBefore
, pIssuerInfo
) == 0
1146 && CertVerifyTimeValidity(&pSubjectInfo
->NotAfter
, pIssuerInfo
) == 0;
1149 BOOL WINAPI
CryptHashCertificate(HCRYPTPROV hCryptProv
, ALG_ID Algid
,
1150 DWORD dwFlags
, const BYTE
*pbEncoded
, DWORD cbEncoded
, BYTE
*pbComputedHash
,
1151 DWORD
*pcbComputedHash
)
1154 HCRYPTHASH hHash
= 0;
1156 TRACE("(%08lx, %d, %08x, %p, %d, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
1157 pbEncoded
, cbEncoded
, pbComputedHash
, pcbComputedHash
);
1160 hCryptProv
= CRYPT_GetDefaultProvider();
1165 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
1168 ret
= CryptHashData(hHash
, pbEncoded
, cbEncoded
, 0);
1170 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
1171 pcbComputedHash
, 0);
1172 CryptDestroyHash(hHash
);
1178 BOOL WINAPI
CryptHashPublicKeyInfo(HCRYPTPROV hCryptProv
, ALG_ID Algid
,
1179 DWORD dwFlags
, DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pInfo
,
1180 BYTE
*pbComputedHash
, DWORD
*pcbComputedHash
)
1183 HCRYPTHASH hHash
= 0;
1185 TRACE("(%08lx, %d, %08x, %d, %p, %p, %p)\n", hCryptProv
, Algid
, dwFlags
,
1186 dwCertEncodingType
, pInfo
, pbComputedHash
, pcbComputedHash
);
1189 hCryptProv
= CRYPT_GetDefaultProvider();
1197 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_PUBLIC_KEY_INFO
,
1198 pInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &buf
, &size
);
1201 ret
= CryptCreateHash(hCryptProv
, Algid
, 0, 0, &hHash
);
1204 ret
= CryptHashData(hHash
, buf
, size
, 0);
1206 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbComputedHash
,
1207 pcbComputedHash
, 0);
1208 CryptDestroyHash(hHash
);
1216 BOOL WINAPI
CryptSignCertificate(HCRYPTPROV hCryptProv
, DWORD dwKeySpec
,
1217 DWORD dwCertEncodingType
, const BYTE
*pbEncodedToBeSigned
,
1218 DWORD cbEncodedToBeSigned
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
1219 const void *pvHashAuxInfo
, BYTE
*pbSignature
, DWORD
*pcbSignature
)
1222 PCCRYPT_OID_INFO info
;
1225 TRACE("(%08lx, %d, %d, %p, %d, %p, %p, %p, %p)\n", hCryptProv
,
1226 dwKeySpec
, dwCertEncodingType
, pbEncodedToBeSigned
, cbEncodedToBeSigned
,
1227 pSignatureAlgorithm
, pvHashAuxInfo
, pbSignature
, pcbSignature
);
1229 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
1230 pSignatureAlgorithm
->pszObjId
, 0);
1233 SetLastError(NTE_BAD_ALGID
);
1236 if (info
->dwGroupId
== CRYPT_HASH_ALG_OID_GROUP_ID
)
1239 hCryptProv
= CRYPT_GetDefaultProvider();
1240 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
1243 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
1244 cbEncodedToBeSigned
, 0);
1246 ret
= CryptGetHashParam(hHash
, HP_HASHVAL
, pbSignature
,
1248 CryptDestroyHash(hHash
);
1255 SetLastError(ERROR_INVALID_PARAMETER
);
1260 ret
= CryptCreateHash(hCryptProv
, info
->u
.Algid
, 0, 0, &hHash
);
1263 ret
= CryptHashData(hHash
, pbEncodedToBeSigned
,
1264 cbEncodedToBeSigned
, 0);
1266 ret
= CryptSignHashW(hHash
, dwKeySpec
, NULL
, 0, pbSignature
,
1268 CryptDestroyHash(hHash
);
1275 BOOL WINAPI
CryptSignAndEncodeCertificate(HCRYPTPROV hCryptProv
,
1276 DWORD dwKeySpec
, DWORD dwCertEncodingType
, LPCSTR lpszStructType
,
1277 const void *pvStructInfo
, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
,
1278 const void *pvHashAuxInfo
, PBYTE pbEncoded
, DWORD
*pcbEncoded
)
1281 DWORD encodedSize
, hashSize
;
1283 TRACE("(%08lx, %d, %d, %s, %p, %p, %p, %p, %p)\n", hCryptProv
, dwKeySpec
,
1284 dwCertEncodingType
, debugstr_a(lpszStructType
), pvStructInfo
,
1285 pSignatureAlgorithm
, pvHashAuxInfo
, pbEncoded
, pcbEncoded
);
1287 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
, pvStructInfo
,
1288 NULL
, &encodedSize
);
1291 PBYTE encoded
= CryptMemAlloc(encodedSize
);
1295 ret
= CryptEncodeObject(dwCertEncodingType
, lpszStructType
,
1296 pvStructInfo
, encoded
, &encodedSize
);
1299 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
1300 dwCertEncodingType
, encoded
, encodedSize
, pSignatureAlgorithm
,
1301 pvHashAuxInfo
, NULL
, &hashSize
);
1304 PBYTE hash
= CryptMemAlloc(hashSize
);
1308 ret
= CryptSignCertificate(hCryptProv
, dwKeySpec
,
1309 dwCertEncodingType
, encoded
, encodedSize
,
1310 pSignatureAlgorithm
, pvHashAuxInfo
, hash
, &hashSize
);
1313 CERT_SIGNED_CONTENT_INFO info
= { { 0 } };
1315 info
.ToBeSigned
.cbData
= encodedSize
;
1316 info
.ToBeSigned
.pbData
= encoded
;
1317 memcpy(&info
.SignatureAlgorithm
,
1318 pSignatureAlgorithm
,
1319 sizeof(info
.SignatureAlgorithm
));
1320 info
.Signature
.cbData
= hashSize
;
1321 info
.Signature
.pbData
= hash
;
1322 info
.Signature
.cUnusedBits
= 0;
1323 ret
= CryptEncodeObject(dwCertEncodingType
,
1324 X509_CERT
, &info
, pbEncoded
, pcbEncoded
);
1330 CryptMemFree(encoded
);
1336 BOOL WINAPI
CryptVerifyCertificateSignature(HCRYPTPROV hCryptProv
,
1337 DWORD dwCertEncodingType
, const BYTE
*pbEncoded
, DWORD cbEncoded
,
1338 PCERT_PUBLIC_KEY_INFO pPublicKey
)
1340 return CryptVerifyCertificateSignatureEx(hCryptProv
, dwCertEncodingType
,
1341 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
, (void *)pbEncoded
,
1342 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
, pPublicKey
, 0, NULL
);
1345 static BOOL
CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV hCryptProv
,
1346 DWORD dwCertEncodingType
, PCERT_PUBLIC_KEY_INFO pubKeyInfo
,
1347 const CERT_SIGNED_CONTENT_INFO
*signedCert
)
1351 PCCRYPT_OID_INFO info
;
1352 ALG_ID pubKeyID
, hashID
;
1354 info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
1355 pubKeyInfo
->Algorithm
.pszObjId
, 0);
1356 if (!info
|| (info
->dwGroupId
!= CRYPT_PUBKEY_ALG_OID_GROUP_ID
&&
1357 info
->dwGroupId
!= CRYPT_SIGN_ALG_OID_GROUP_ID
))
1359 SetLastError(NTE_BAD_ALGID
);
1362 if (info
->dwGroupId
== CRYPT_PUBKEY_ALG_OID_GROUP_ID
)
1364 switch (info
->u
.Algid
)
1367 pubKeyID
= CALG_RSA_SIGN
;
1371 pubKeyID
= CALG_RSA_SIGN
;
1375 FIXME("unimplemented for %s\n", pubKeyInfo
->Algorithm
.pszObjId
);
1381 hashID
= info
->u
.Algid
;
1382 if (info
->ExtraInfo
.cbData
>= sizeof(ALG_ID
))
1383 pubKeyID
= *(ALG_ID
*)info
->ExtraInfo
.pbData
;
1387 /* Load the default provider if necessary */
1389 hCryptProv
= CRYPT_GetDefaultProvider();
1390 ret
= CryptImportPublicKeyInfoEx(hCryptProv
, dwCertEncodingType
,
1391 pubKeyInfo
, pubKeyID
, 0, NULL
, &key
);
1396 ret
= CryptCreateHash(hCryptProv
, hashID
, 0, 0, &hash
);
1399 ret
= CryptHashData(hash
, signedCert
->ToBeSigned
.pbData
,
1400 signedCert
->ToBeSigned
.cbData
, 0);
1402 ret
= CryptVerifySignatureW(hash
, signedCert
->Signature
.pbData
,
1403 signedCert
->Signature
.cbData
, key
, NULL
, 0);
1404 CryptDestroyHash(hash
);
1406 CryptDestroyKey(key
);
1411 BOOL WINAPI
CryptVerifyCertificateSignatureEx(HCRYPTPROV hCryptProv
,
1412 DWORD dwCertEncodingType
, DWORD dwSubjectType
, void *pvSubject
,
1413 DWORD dwIssuerType
, void *pvIssuer
, DWORD dwFlags
, void *pvReserved
)
1416 CRYPT_DATA_BLOB subjectBlob
;
1418 TRACE("(%08lx, %d, %d, %p, %d, %p, %08x, %p)\n", hCryptProv
,
1419 dwCertEncodingType
, dwSubjectType
, pvSubject
, dwIssuerType
, pvIssuer
,
1420 dwFlags
, pvReserved
);
1422 switch (dwSubjectType
)
1424 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB
:
1426 PCRYPT_DATA_BLOB blob
= (PCRYPT_DATA_BLOB
)pvSubject
;
1428 subjectBlob
.pbData
= blob
->pbData
;
1429 subjectBlob
.cbData
= blob
->cbData
;
1432 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
:
1434 PCERT_CONTEXT context
= (PCERT_CONTEXT
)pvSubject
;
1436 subjectBlob
.pbData
= context
->pbCertEncoded
;
1437 subjectBlob
.cbData
= context
->cbCertEncoded
;
1440 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL
:
1442 PCRL_CONTEXT context
= (PCRL_CONTEXT
)pvSubject
;
1444 subjectBlob
.pbData
= context
->pbCrlEncoded
;
1445 subjectBlob
.cbData
= context
->cbCrlEncoded
;
1449 SetLastError(E_INVALIDARG
);
1455 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
1458 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
,
1459 subjectBlob
.pbData
, subjectBlob
.cbData
,
1460 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1461 (BYTE
*)&signedCert
, &size
);
1464 switch (dwIssuerType
)
1466 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY
:
1467 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
1468 dwCertEncodingType
, (PCERT_PUBLIC_KEY_INFO
)pvIssuer
,
1471 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
:
1472 ret
= CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv
,
1474 &((PCCERT_CONTEXT
)pvIssuer
)->pCertInfo
->SubjectPublicKeyInfo
,
1477 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN
:
1478 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
1481 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL
:
1484 SetLastError(E_INVALIDARG
);
1489 FIXME("unimplemented for NULL signer\n");
1490 SetLastError(E_INVALIDARG
);
1495 SetLastError(E_INVALIDARG
);
1498 LocalFree(signedCert
);
1504 BOOL WINAPI
CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
, DWORD dwFlags
,
1505 PCERT_ENHKEY_USAGE pUsage
, DWORD
*pcbUsage
)
1507 PCERT_ENHKEY_USAGE usage
= NULL
;
1511 if (!pCertContext
|| !pcbUsage
)
1513 SetLastError(ERROR_INVALID_PARAMETER
);
1517 TRACE("(%p, %08x, %p, %d)\n", pCertContext
, dwFlags
, pUsage
, *pcbUsage
);
1519 if (!(dwFlags
& CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
))
1523 if (CertGetCertificateContextProperty(pCertContext
,
1524 CERT_ENHKEY_USAGE_PROP_ID
, NULL
, &propSize
))
1526 LPBYTE buf
= CryptMemAlloc(propSize
);
1530 if (CertGetCertificateContextProperty(pCertContext
,
1531 CERT_ENHKEY_USAGE_PROP_ID
, buf
, &propSize
))
1533 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
1534 X509_ENHANCED_KEY_USAGE
, buf
, propSize
,
1535 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
1541 if (!usage
&& !(dwFlags
& CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
))
1543 PCERT_EXTENSION ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
1544 pCertContext
->pCertInfo
->cExtension
,
1545 pCertContext
->pCertInfo
->rgExtension
);
1549 ret
= CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
,
1550 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1551 CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &usage
, &bytesNeeded
);
1556 /* If a particular location is specified, this should fail. Otherwise
1557 * it should succeed with an empty usage. (This is true on Win2k and
1558 * later, which we emulate.)
1562 SetLastError(CRYPT_E_NOT_FOUND
);
1566 bytesNeeded
= sizeof(CERT_ENHKEY_USAGE
);
1572 *pcbUsage
= bytesNeeded
;
1573 else if (*pcbUsage
< bytesNeeded
)
1575 SetLastError(ERROR_MORE_DATA
);
1576 *pcbUsage
= bytesNeeded
;
1581 *pcbUsage
= bytesNeeded
;
1585 LPSTR nextOID
= (LPSTR
)((LPBYTE
)pUsage
+
1586 sizeof(CERT_ENHKEY_USAGE
) +
1587 usage
->cUsageIdentifier
* sizeof(LPSTR
));
1589 pUsage
->cUsageIdentifier
= usage
->cUsageIdentifier
;
1590 pUsage
->rgpszUsageIdentifier
= (LPSTR
*)((LPBYTE
)pUsage
+
1591 sizeof(CERT_ENHKEY_USAGE
));
1592 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
1594 pUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
1595 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
1596 nextOID
+= strlen(nextOID
) + 1;
1600 pUsage
->cUsageIdentifier
= 0;
1605 TRACE("returning %d\n", ret
);
1609 BOOL WINAPI
CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext
,
1610 PCERT_ENHKEY_USAGE pUsage
)
1614 TRACE("(%p, %p)\n", pCertContext
, pUsage
);
1618 CRYPT_DATA_BLOB blob
= { 0, NULL
};
1620 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
1621 pUsage
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &blob
.pbData
, &blob
.cbData
);
1624 ret
= CertSetCertificateContextProperty(pCertContext
,
1625 CERT_ENHKEY_USAGE_PROP_ID
, 0, &blob
);
1626 LocalFree(blob
.pbData
);
1630 ret
= CertSetCertificateContextProperty(pCertContext
,
1631 CERT_ENHKEY_USAGE_PROP_ID
, 0, NULL
);
1635 BOOL WINAPI
CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
1636 LPCSTR pszUsageIdentifier
)
1641 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
1643 if (CertGetEnhancedKeyUsage(pCertContext
,
1644 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, NULL
, &size
))
1646 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(size
);
1650 ret
= CertGetEnhancedKeyUsage(pCertContext
,
1651 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
);
1654 PCERT_ENHKEY_USAGE newUsage
= CryptMemAlloc(size
+
1655 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
1662 newUsage
->rgpszUsageIdentifier
=
1663 (LPSTR
*)((LPBYTE
)newUsage
+ sizeof(CERT_ENHKEY_USAGE
));
1664 nextOID
= (LPSTR
)((LPBYTE
)newUsage
->rgpszUsageIdentifier
+
1665 (usage
->cUsageIdentifier
+ 1) * sizeof(LPSTR
));
1666 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
1668 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
1669 strcpy(nextOID
, usage
->rgpszUsageIdentifier
[i
]);
1670 nextOID
+= strlen(nextOID
) + 1;
1672 newUsage
->rgpszUsageIdentifier
[i
] = nextOID
;
1673 strcpy(nextOID
, pszUsageIdentifier
);
1674 newUsage
->cUsageIdentifier
= i
+ 1;
1675 ret
= CertSetEnhancedKeyUsage(pCertContext
, newUsage
);
1676 CryptMemFree(newUsage
);
1679 CryptMemFree(usage
);
1686 PCERT_ENHKEY_USAGE usage
= CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE
) +
1687 sizeof(LPSTR
) + strlen(pszUsageIdentifier
) + 1);
1691 usage
->rgpszUsageIdentifier
=
1692 (LPSTR
*)((LPBYTE
)usage
+ sizeof(CERT_ENHKEY_USAGE
));
1693 usage
->rgpszUsageIdentifier
[0] = (LPSTR
)((LPBYTE
)usage
+
1694 sizeof(CERT_ENHKEY_USAGE
) + sizeof(LPSTR
));
1695 strcpy(usage
->rgpszUsageIdentifier
[0], pszUsageIdentifier
);
1696 usage
->cUsageIdentifier
= 1;
1697 ret
= CertSetEnhancedKeyUsage(pCertContext
, usage
);
1698 CryptMemFree(usage
);
1706 BOOL WINAPI
CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext
,
1707 LPCSTR pszUsageIdentifier
)
1711 CERT_ENHKEY_USAGE usage
;
1713 TRACE("(%p, %s)\n", pCertContext
, debugstr_a(pszUsageIdentifier
));
1715 size
= sizeof(usage
);
1716 ret
= CertGetEnhancedKeyUsage(pCertContext
,
1717 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, &usage
, &size
);
1718 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
1720 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
1724 ret
= CertGetEnhancedKeyUsage(pCertContext
,
1725 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, pUsage
, &size
);
1728 if (pUsage
->cUsageIdentifier
)
1733 for (i
= 0; i
< pUsage
->cUsageIdentifier
; i
++)
1735 if (!strcmp(pUsage
->rgpszUsageIdentifier
[i
],
1736 pszUsageIdentifier
))
1738 if (found
&& i
< pUsage
->cUsageIdentifier
- 1)
1739 pUsage
->rgpszUsageIdentifier
[i
] =
1740 pUsage
->rgpszUsageIdentifier
[i
+ 1];
1742 pUsage
->cUsageIdentifier
--;
1743 /* Remove the usage if it's empty */
1744 if (pUsage
->cUsageIdentifier
)
1745 ret
= CertSetEnhancedKeyUsage(pCertContext
, pUsage
);
1747 ret
= CertSetEnhancedKeyUsage(pCertContext
, NULL
);
1750 CryptMemFree(pUsage
);
1757 /* it fit in an empty usage, therefore there's nothing to remove */
1763 BOOL WINAPI
CertGetValidUsages(DWORD cCerts
, PCCERT_CONTEXT
*rghCerts
,
1764 int *cNumOIDSs
, LPSTR
*rghOIDs
, DWORD
*pcbOIDs
)
1767 DWORD i
, cbOIDs
= 0;
1768 BOOL allUsagesValid
= TRUE
;
1769 CERT_ENHKEY_USAGE validUsages
= { 0, NULL
};
1771 TRACE("(%d, %p, %p, %p, %d)\n", cCerts
, *rghCerts
, cNumOIDSs
,
1774 for (i
= 0; ret
&& i
< cCerts
; i
++)
1776 CERT_ENHKEY_USAGE usage
;
1777 DWORD size
= sizeof(usage
);
1779 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, &usage
, &size
);
1780 /* Success is deliberately ignored: it implies all usages are valid */
1781 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
1783 PCERT_ENHKEY_USAGE pUsage
= CryptMemAlloc(size
);
1785 allUsagesValid
= FALSE
;
1788 ret
= CertGetEnhancedKeyUsage(rghCerts
[i
], 0, pUsage
, &size
);
1791 if (!validUsages
.cUsageIdentifier
)
1795 cbOIDs
= pUsage
->cUsageIdentifier
* sizeof(LPSTR
);
1796 validUsages
.cUsageIdentifier
= pUsage
->cUsageIdentifier
;
1797 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
1798 cbOIDs
+= lstrlenA(pUsage
->rgpszUsageIdentifier
[j
])
1800 validUsages
.rgpszUsageIdentifier
=
1801 CryptMemAlloc(cbOIDs
);
1802 if (validUsages
.rgpszUsageIdentifier
)
1804 LPSTR nextOID
= (LPSTR
)
1805 ((LPBYTE
)validUsages
.rgpszUsageIdentifier
+
1806 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
1808 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
1810 validUsages
.rgpszUsageIdentifier
[j
] = nextOID
;
1811 lstrcpyA(validUsages
.rgpszUsageIdentifier
[j
],
1812 pUsage
->rgpszUsageIdentifier
[j
]);
1813 nextOID
+= lstrlenA(nextOID
) + 1;
1821 DWORD j
, k
, validIndexes
= 0, numRemoved
= 0;
1823 /* Merge: build a bitmap of all the indexes of
1824 * validUsages.rgpszUsageIdentifier that are in pUsage.
1826 for (j
= 0; j
< pUsage
->cUsageIdentifier
; j
++)
1828 for (k
= 0; k
< validUsages
.cUsageIdentifier
; k
++)
1830 if (!strcmp(pUsage
->rgpszUsageIdentifier
[j
],
1831 validUsages
.rgpszUsageIdentifier
[k
]))
1833 validIndexes
|= (1 << k
);
1838 /* Merge by removing from validUsages those that are
1839 * not in the bitmap.
1841 for (j
= 0; j
< validUsages
.cUsageIdentifier
; j
++)
1843 if (!(validIndexes
& (1 << j
)))
1845 if (j
< validUsages
.cUsageIdentifier
- 1)
1847 memcpy(&validUsages
.rgpszUsageIdentifier
[j
],
1848 &validUsages
.rgpszUsageIdentifier
[j
+
1850 (validUsages
.cUsageIdentifier
- numRemoved
1851 - j
- 1) * sizeof(LPSTR
));
1853 validUsages
.rgpszUsageIdentifier
[j
]) + 1 +
1858 validUsages
.cUsageIdentifier
--;
1863 CryptMemFree(pUsage
);
1878 if (!rghOIDs
|| *pcbOIDs
< cbOIDs
)
1881 SetLastError(ERROR_MORE_DATA
);
1886 LPSTR nextOID
= (LPSTR
)((LPBYTE
)rghOIDs
+
1887 validUsages
.cUsageIdentifier
* sizeof(LPSTR
));
1890 *cNumOIDSs
= validUsages
.cUsageIdentifier
;
1891 for (i
= 0; i
< validUsages
.cUsageIdentifier
; i
++)
1893 rghOIDs
[i
] = nextOID
;
1894 lstrcpyA(nextOID
, validUsages
.rgpszUsageIdentifier
[i
]);
1895 nextOID
+= lstrlenA(nextOID
) + 1;
1900 CryptMemFree(validUsages
.rgpszUsageIdentifier
);
1904 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
1905 * pInfo is NULL, from the attributes of hProv.
1907 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context
,
1908 const CRYPT_KEY_PROV_INFO
*pInfo
, HCRYPTPROV hProv
)
1910 CRYPT_KEY_PROV_INFO info
= { 0 };
1918 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
, NULL
, &size
, 0);
1921 LPSTR szContainer
= CryptMemAlloc(size
);
1925 ret
= CryptGetProvParam(hProv
, PP_CONTAINER
,
1926 (BYTE
*)szContainer
, &size
, 0);
1929 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
1933 info
.pwszContainerName
= CryptMemAlloc(len
*
1935 len
= MultiByteToWideChar(CP_ACP
, 0, szContainer
, -1,
1936 info
.pwszContainerName
, len
);
1939 CryptMemFree(szContainer
);
1942 ret
= CryptGetProvParam(hProv
, PP_NAME
, NULL
, &size
, 0);
1945 LPSTR szProvider
= CryptMemAlloc(size
);
1949 ret
= CryptGetProvParam(hProv
, PP_NAME
, (BYTE
*)szProvider
,
1953 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
1957 info
.pwszProvName
= CryptMemAlloc(len
*
1959 len
= MultiByteToWideChar(CP_ACP
, 0, szProvider
, -1,
1960 info
.pwszProvName
, len
);
1963 CryptMemFree(szProvider
);
1966 size
= sizeof(info
.dwKeySpec
);
1967 /* in case no CRYPT_KEY_PROV_INFO given,
1968 * we always use AT_SIGNATURE key spec
1970 info
.dwKeySpec
= AT_SIGNATURE
;
1971 size
= sizeof(info
.dwProvType
);
1972 ret
= CryptGetProvParam(hProv
, PP_PROVTYPE
, (LPBYTE
)&info
.dwProvType
,
1975 info
.dwProvType
= PROV_RSA_FULL
;
1979 ret
= CertSetCertificateContextProperty(context
, CERT_KEY_PROV_INFO_PROP_ID
,
1984 CryptMemFree(info
.pwszContainerName
);
1985 CryptMemFree(info
.pwszProvName
);
1989 /* Creates a signed certificate context from the unsigned, encoded certificate
1990 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
1992 static PCCERT_CONTEXT
CRYPT_CreateSignedCert(const CRYPT_DER_BLOB
*blob
,
1993 HCRYPTPROV hProv
, DWORD dwKeySpec
, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo
)
1995 PCCERT_CONTEXT context
= NULL
;
1999 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2000 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, NULL
, &sigSize
);
2003 LPBYTE sig
= CryptMemAlloc(sigSize
);
2005 ret
= CryptSignCertificate(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2006 blob
->pbData
, blob
->cbData
, sigAlgo
, NULL
, sig
, &sigSize
);
2009 CERT_SIGNED_CONTENT_INFO signedInfo
;
2010 BYTE
*encodedSignedCert
= NULL
;
2011 DWORD encodedSignedCertSize
= 0;
2013 signedInfo
.ToBeSigned
.cbData
= blob
->cbData
;
2014 signedInfo
.ToBeSigned
.pbData
= blob
->pbData
;
2015 memcpy(&signedInfo
.SignatureAlgorithm
, sigAlgo
,
2016 sizeof(signedInfo
.SignatureAlgorithm
));
2017 signedInfo
.Signature
.cbData
= sigSize
;
2018 signedInfo
.Signature
.pbData
= sig
;
2019 signedInfo
.Signature
.cUnusedBits
= 0;
2020 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT
,
2021 &signedInfo
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
,
2022 (BYTE
*)&encodedSignedCert
, &encodedSignedCertSize
);
2025 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2026 encodedSignedCert
, encodedSignedCertSize
);
2027 LocalFree(encodedSignedCert
);
2035 /* Copies data from the parameters into info, where:
2036 * pSerialNumber: The serial number. Must not be NULL.
2037 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
2039 * pSignatureAlgorithm: Optional.
2040 * pStartTime: The starting time of the certificate. If NULL, the current
2041 * system time is used.
2042 * pEndTime: The ending time of the certificate. If NULL, one year past the
2043 * starting time is used.
2044 * pubKey: The public key of the certificate. Must not be NULL.
2045 * pExtensions: Extensions to be included with the certificate. Optional.
2047 static void CRYPT_MakeCertInfo(PCERT_INFO info
, const CRYPT_DATA_BLOB
*pSerialNumber
,
2048 const CERT_NAME_BLOB
*pSubjectIssuerBlob
,
2049 const CRYPT_ALGORITHM_IDENTIFIER
*pSignatureAlgorithm
, const SYSTEMTIME
*pStartTime
,
2050 const SYSTEMTIME
*pEndTime
, const CERT_PUBLIC_KEY_INFO
*pubKey
,
2051 const CERT_EXTENSIONS
*pExtensions
)
2053 static CHAR oid
[] = szOID_RSA_SHA1RSA
;
2056 assert(pSerialNumber
);
2057 assert(pSubjectIssuerBlob
);
2060 info
->dwVersion
= CERT_V3
;
2061 info
->SerialNumber
.cbData
= pSerialNumber
->cbData
;
2062 info
->SerialNumber
.pbData
= pSerialNumber
->pbData
;
2063 if (pSignatureAlgorithm
)
2064 memcpy(&info
->SignatureAlgorithm
, pSignatureAlgorithm
,
2065 sizeof(info
->SignatureAlgorithm
));
2068 info
->SignatureAlgorithm
.pszObjId
= oid
;
2069 info
->SignatureAlgorithm
.Parameters
.cbData
= 0;
2070 info
->SignatureAlgorithm
.Parameters
.pbData
= NULL
;
2072 info
->Issuer
.cbData
= pSubjectIssuerBlob
->cbData
;
2073 info
->Issuer
.pbData
= pSubjectIssuerBlob
->pbData
;
2075 SystemTimeToFileTime(pStartTime
, &info
->NotBefore
);
2077 GetSystemTimeAsFileTime(&info
->NotBefore
);
2079 SystemTimeToFileTime(pEndTime
, &info
->NotAfter
);
2084 if (FileTimeToSystemTime(&info
->NotBefore
, &endTime
))
2087 SystemTimeToFileTime(&endTime
, &info
->NotAfter
);
2090 info
->Subject
.cbData
= pSubjectIssuerBlob
->cbData
;
2091 info
->Subject
.pbData
= pSubjectIssuerBlob
->pbData
;
2092 memcpy(&info
->SubjectPublicKeyInfo
, pubKey
,
2093 sizeof(info
->SubjectPublicKeyInfo
));
2096 info
->cExtension
= pExtensions
->cExtension
;
2097 info
->rgExtension
= pExtensions
->rgExtension
;
2101 info
->cExtension
= 0;
2102 info
->rgExtension
= NULL
;
2106 typedef RPC_STATUS (RPC_ENTRY
*UuidCreateFunc
)(UUID
*);
2107 typedef RPC_STATUS (RPC_ENTRY
*UuidToStringFunc
)(UUID
*, unsigned char **);
2108 typedef RPC_STATUS (RPC_ENTRY
*RpcStringFreeFunc
)(unsigned char **);
2110 static HCRYPTPROV
CRYPT_CreateKeyProv(void)
2112 HCRYPTPROV hProv
= 0;
2113 HMODULE rpcrt
= LoadLibraryA("rpcrt4");
2117 UuidCreateFunc uuidCreate
= (UuidCreateFunc
)GetProcAddress(rpcrt
,
2119 UuidToStringFunc uuidToString
= (UuidToStringFunc
)GetProcAddress(rpcrt
,
2121 RpcStringFreeFunc rpcStringFree
= (RpcStringFreeFunc
)GetProcAddress(
2122 rpcrt
, "RpcStringFreeA");
2124 if (uuidCreate
&& uuidToString
&& rpcStringFree
)
2127 RPC_STATUS status
= uuidCreate(&uuid
);
2129 if (status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
)
2131 unsigned char *uuidStr
;
2133 status
= uuidToString(&uuid
, &uuidStr
);
2134 if (status
== RPC_S_OK
)
2136 BOOL ret
= CryptAcquireContextA(&hProv
, (LPCSTR
)uuidStr
,
2137 MS_DEF_PROV_A
, PROV_RSA_FULL
, CRYPT_NEWKEYSET
);
2143 ret
= CryptGenKey(hProv
, AT_SIGNATURE
, 0, &key
);
2145 CryptDestroyKey(key
);
2147 rpcStringFree(&uuidStr
);
2156 PCCERT_CONTEXT WINAPI
CertCreateSelfSignCertificate(HCRYPTPROV hProv
,
2157 PCERT_NAME_BLOB pSubjectIssuerBlob
, DWORD dwFlags
,
2158 PCRYPT_KEY_PROV_INFO pKeyProvInfo
,
2159 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm
, PSYSTEMTIME pStartTime
,
2160 PSYSTEMTIME pEndTime
, PCERT_EXTENSIONS pExtensions
)
2162 PCCERT_CONTEXT context
= NULL
;
2163 BOOL ret
, releaseContext
= FALSE
;
2164 PCERT_PUBLIC_KEY_INFO pubKey
= NULL
;
2165 DWORD pubKeySize
= 0,dwKeySpec
= AT_SIGNATURE
;
2167 TRACE("(%08lx, %p, %08x, %p, %p, %p, %p, %p)\n", hProv
,
2168 pSubjectIssuerBlob
, dwFlags
, pKeyProvInfo
, pSignatureAlgorithm
, pStartTime
,
2169 pExtensions
, pExtensions
);
2171 if(!pSubjectIssuerBlob
)
2173 SetLastError(ERROR_INVALID_PARAMETER
);
2181 hProv
= CRYPT_CreateKeyProv();
2182 releaseContext
= TRUE
;
2184 else if (pKeyProvInfo
->dwFlags
& CERT_SET_KEY_PROV_HANDLE_PROP_ID
)
2186 SetLastError(NTE_BAD_FLAGS
);
2192 /* acquire the context using the given information*/
2193 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
2194 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
2195 pKeyProvInfo
->dwFlags
);
2198 if(GetLastError() != NTE_BAD_KEYSET
)
2200 /* create the key set */
2201 ret
= CryptAcquireContextW(&hProv
,pKeyProvInfo
->pwszContainerName
,
2202 pKeyProvInfo
->pwszProvName
,pKeyProvInfo
->dwProvType
,
2203 pKeyProvInfo
->dwFlags
|CRYPT_NEWKEYSET
);
2207 dwKeySpec
= pKeyProvInfo
->dwKeySpec
;
2208 /* check if the key is here */
2209 ret
= CryptGetUserKey(hProv
,dwKeySpec
,&hKey
);
2212 if (NTE_NO_KEY
== GetLastError())
2213 { /* generate the key */
2214 ret
= CryptGenKey(hProv
,dwKeySpec
,0,&hKey
);
2218 CryptReleaseContext(hProv
,0);
2219 SetLastError(NTE_BAD_KEYSET
);
2223 CryptDestroyKey(hKey
);
2224 releaseContext
= TRUE
;
2227 else if (pKeyProvInfo
)
2229 SetLastError(ERROR_INVALID_PARAMETER
);
2233 CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
, NULL
,
2235 pubKey
= CryptMemAlloc(pubKeySize
);
2238 ret
= CryptExportPublicKeyInfo(hProv
, dwKeySpec
, X509_ASN_ENCODING
,
2239 pubKey
, &pubKeySize
);
2242 CERT_INFO info
= { 0 };
2243 CRYPT_DER_BLOB blob
= { 0, NULL
};
2245 CRYPT_DATA_BLOB serialBlob
= { sizeof(serial
), serial
};
2247 CryptGenRandom(hProv
, sizeof(serial
), serial
);
2248 CRYPT_MakeCertInfo(&info
, &serialBlob
, pSubjectIssuerBlob
,
2249 pSignatureAlgorithm
, pStartTime
, pEndTime
, pubKey
, pExtensions
);
2250 ret
= CryptEncodeObjectEx(X509_ASN_ENCODING
, X509_CERT_TO_BE_SIGNED
,
2251 &info
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, (BYTE
*)&blob
.pbData
,
2255 if (!(dwFlags
& CERT_CREATE_SELFSIGN_NO_SIGN
))
2256 context
= CRYPT_CreateSignedCert(&blob
, hProv
,dwKeySpec
,
2257 &info
.SignatureAlgorithm
);
2259 context
= CertCreateCertificateContext(X509_ASN_ENCODING
,
2260 blob
.pbData
, blob
.cbData
);
2261 if (context
&& !(dwFlags
& CERT_CREATE_SELFSIGN_NO_KEY_INFO
))
2262 CertContext_SetKeyProvInfo(context
, pKeyProvInfo
, hProv
);
2263 LocalFree(blob
.pbData
);
2266 CryptMemFree(pubKey
);
2269 CryptReleaseContext(hProv
, 0);
2273 BOOL WINAPI
CertGetCertificateChain(HCERTCHAINENGINE hChainEngine
,
2274 PCCERT_CONTEXT pCertContext
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
2275 PCERT_CHAIN_PARA pChainPara
, DWORD dwFlags
, LPVOID pvReserved
,
2276 PCCERT_CHAIN_CONTEXT
* ppChainContext
)
2278 FIXME(" %p %p %p %p %p 0x%08X %p %p - stub\n",hChainEngine
, pCertContext
,
2279 pTime
, hAdditionalStore
, pChainPara
, dwFlags
, pvReserved
, ppChainContext
);
2281 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);