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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/debug.h"
28 #include "crypt32_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32 PCRYPT_ATTRIBUTE WINAPI CertFindAttribute(LPCSTR pszObjId, DWORD cAttr,
33 CRYPT_ATTRIBUTE rgAttr[])
35 PCRYPT_ATTRIBUTE ret = NULL;
38 TRACE("%s %ld %p\n", debugstr_a(pszObjId), cAttr, rgAttr);
44 SetLastError(ERROR_INVALID_PARAMETER);
48 for (i = 0; !ret && i < cAttr; i++)
49 if (rgAttr[i].pszObjId && !strcmp(pszObjId, rgAttr[i].pszObjId))
54 PCERT_EXTENSION WINAPI CertFindExtension(LPCSTR pszObjId, DWORD cExtensions,
55 CERT_EXTENSION rgExtensions[])
57 PCERT_EXTENSION ret = NULL;
60 TRACE("%s %ld %p\n", debugstr_a(pszObjId), cExtensions, rgExtensions);
66 SetLastError(ERROR_INVALID_PARAMETER);
70 for (i = 0; !ret && i < cExtensions; i++)
71 if (rgExtensions[i].pszObjId && !strcmp(pszObjId,
72 rgExtensions[i].pszObjId))
73 ret = &rgExtensions[i];
77 PCERT_RDN_ATTR WINAPI CertFindRDNAttr(LPCSTR pszObjId, PCERT_NAME_INFO pName)
79 PCERT_RDN_ATTR ret = NULL;
82 TRACE("%s %p\n", debugstr_a(pszObjId), pName);
86 SetLastError(ERROR_INVALID_PARAMETER);
90 for (i = 0; !ret && i < pName->cRDN; i++)
91 for (j = 0; !ret && j < pName->rgRDN[i].cRDNAttr; j++)
92 if (pName->rgRDN[i].rgRDNAttr[j].pszObjId && !strcmp(pszObjId,
93 pName->rgRDN[i].rgRDNAttr[j].pszObjId))
94 ret = &pName->rgRDN[i].rgRDNAttr[j];
98 LONG WINAPI CertVerifyTimeValidity(LPFILETIME pTimeToVerify,
108 GetSystemTime(&sysTime);
109 SystemTimeToFileTime(&sysTime, &fileTime);
110 pTimeToVerify = &fileTime;
112 if ((ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotBefore)) >= 0)
114 ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotAfter);
121 BOOL WINAPI CryptHashCertificate(HCRYPTPROV hCryptProv, ALG_ID Algid,
122 DWORD dwFlags, const BYTE *pbEncoded, DWORD cbEncoded, BYTE *pbComputedHash,
123 DWORD *pcbComputedHash)
126 HCRYPTHASH hHash = 0;
128 TRACE("(%ld, %d, %08lx, %p, %ld, %p, %p)\n", hCryptProv, Algid, dwFlags,
129 pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash);
132 hCryptProv = CRYPT_GetDefaultProvider();
137 ret = CryptCreateHash(hCryptProv, Algid, 0, 0, &hHash);
140 ret = CryptHashData(hHash, pbEncoded, cbEncoded, 0);
142 ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
144 CryptDestroyHash(hHash);
150 BOOL WINAPI CryptSignCertificate(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
151 DWORD dwCertEncodingType, const BYTE *pbEncodedToBeSigned,
152 DWORD cbEncodedToBeSigned, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
153 const void *pvHashAuxInfo, BYTE *pbSignature, DWORD *pcbSignature)
159 TRACE("(%08lx, %ld, %ld, %p, %ld, %p, %p, %p, %p)\n", hCryptProv,
160 dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned,
161 pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature);
163 algID = CertOIDToAlgId(pSignatureAlgorithm->pszObjId);
166 SetLastError(NTE_BAD_ALGID);
171 SetLastError(ERROR_INVALID_PARAMETER);
175 ret = CryptCreateHash(hCryptProv, algID, 0, 0, &hHash);
178 ret = CryptHashData(hHash, pbEncodedToBeSigned, cbEncodedToBeSigned, 0);
180 ret = CryptSignHashW(hHash, dwKeySpec, NULL, 0, pbSignature,
182 CryptDestroyHash(hHash);
187 BOOL WINAPI CryptVerifyCertificateSignature(HCRYPTPROV hCryptProv,
188 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
189 PCERT_PUBLIC_KEY_INFO pPublicKey)
191 return CryptVerifyCertificateSignatureEx(hCryptProv, dwCertEncodingType,
192 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, (void *)pbEncoded,
193 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY, pPublicKey, 0, NULL);
196 static BOOL CRYPT_VerifyCertSignatureFromPublicKeyInfo(HCRYPTPROV hCryptProv,
197 DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pubKeyInfo,
198 PCERT_SIGNED_CONTENT_INFO signedCert)
201 ALG_ID algID = CertOIDToAlgId(pubKeyInfo->Algorithm.pszObjId);
204 /* Load the default provider if necessary */
206 hCryptProv = CRYPT_GetDefaultProvider();
207 ret = CryptImportPublicKeyInfoEx(hCryptProv, dwCertEncodingType,
208 pubKeyInfo, algID, 0, NULL, &key);
213 /* Some key algorithms aren't hash algorithms, so map them */
214 if (algID == CALG_RSA_SIGN || algID == CALG_RSA_KEYX)
216 ret = CryptCreateHash(hCryptProv, algID, 0, 0, &hash);
219 ret = CryptHashData(hash, signedCert->ToBeSigned.pbData,
220 signedCert->ToBeSigned.cbData, 0);
222 ret = CryptVerifySignatureW(hash, signedCert->Signature.pbData,
223 signedCert->Signature.cbData, key, NULL, 0);
224 CryptDestroyHash(hash);
226 CryptDestroyKey(key);
231 BOOL WINAPI CryptVerifyCertificateSignatureEx(HCRYPTPROV hCryptProv,
232 DWORD dwCertEncodingType, DWORD dwSubjectType, void *pvSubject,
233 DWORD dwIssuerType, void *pvIssuer, DWORD dwFlags, void *pvReserved)
236 CRYPT_DATA_BLOB subjectBlob;
238 TRACE("(%08lx, %ld, %ld, %p, %ld, %p, %08lx, %p)\n", hCryptProv,
239 dwCertEncodingType, dwSubjectType, pvSubject, dwIssuerType, pvIssuer,
240 dwFlags, pvReserved);
242 switch (dwSubjectType)
244 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB:
246 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvSubject;
248 subjectBlob.pbData = blob->pbData;
249 subjectBlob.cbData = blob->cbData;
252 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT:
254 PCERT_CONTEXT context = (PCERT_CONTEXT)pvSubject;
256 subjectBlob.pbData = context->pbCertEncoded;
257 subjectBlob.cbData = context->cbCertEncoded;
260 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL:
262 PCRL_CONTEXT context = (PCRL_CONTEXT)pvSubject;
264 subjectBlob.pbData = context->pbCrlEncoded;
265 subjectBlob.cbData = context->cbCrlEncoded;
269 SetLastError(E_INVALIDARG);
275 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
278 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT,
279 subjectBlob.pbData, subjectBlob.cbData,
280 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
281 (BYTE *)&signedCert, &size);
284 switch (dwIssuerType)
286 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY:
287 ret = CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv,
288 dwCertEncodingType, (PCERT_PUBLIC_KEY_INFO)pvIssuer,
291 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT:
292 ret = CRYPT_VerifyCertSignatureFromPublicKeyInfo(hCryptProv,
294 &((PCCERT_CONTEXT)pvIssuer)->pCertInfo->SubjectPublicKeyInfo,
297 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN:
298 FIXME("CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN: stub\n");
301 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL:
304 SetLastError(E_INVALIDARG);
309 FIXME("unimplemented for NULL signer\n");
310 SetLastError(E_INVALIDARG);
315 SetLastError(E_INVALIDARG);
318 LocalFree(signedCert);
324 BOOL WINAPI CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext, DWORD dwFlags,
325 PCERT_ENHKEY_USAGE pUsage, DWORD *pcbUsage)
327 PCERT_ENHKEY_USAGE usage = NULL;
331 if (!pCertContext || !pcbUsage)
333 SetLastError(ERROR_INVALID_PARAMETER);
337 TRACE("(%p, %08lx, %p, %ld)\n", pCertContext, dwFlags, pUsage, *pcbUsage);
339 if (!(dwFlags & CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG))
343 if (CertGetCertificateContextProperty(pCertContext,
344 CERT_ENHKEY_USAGE_PROP_ID, NULL, &propSize))
346 LPBYTE buf = CryptMemAlloc(propSize);
350 if (CertGetCertificateContextProperty(pCertContext,
351 CERT_ENHKEY_USAGE_PROP_ID, buf, &propSize))
353 ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
354 X509_ENHANCED_KEY_USAGE, buf, propSize,
355 CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
361 if (!usage && !(dwFlags & CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG))
363 PCERT_EXTENSION ext = CertFindExtension(szOID_ENHANCED_KEY_USAGE,
364 pCertContext->pCertInfo->cExtension,
365 pCertContext->pCertInfo->rgExtension);
369 ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
370 X509_ENHANCED_KEY_USAGE, ext->Value.pbData, ext->Value.cbData,
371 CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
376 /* If a particular location is specified, this should fail. Otherwise
377 * it should succeed with an empty usage. (This is true on Win2k and
378 * later, which we emulate.)
382 SetLastError(CRYPT_E_NOT_FOUND);
386 bytesNeeded = sizeof(CERT_ENHKEY_USAGE);
392 *pcbUsage = bytesNeeded;
393 else if (*pcbUsage < bytesNeeded)
395 SetLastError(ERROR_MORE_DATA);
396 *pcbUsage = bytesNeeded;
401 *pcbUsage = bytesNeeded;
405 LPSTR nextOID = (LPSTR)((LPBYTE)pUsage +
406 sizeof(CERT_ENHKEY_USAGE) +
407 usage->cUsageIdentifier * sizeof(LPSTR));
409 pUsage->cUsageIdentifier = usage->cUsageIdentifier;
410 pUsage->rgpszUsageIdentifier = (LPSTR *)((LPBYTE)pUsage +
411 sizeof(CERT_ENHKEY_USAGE));
412 for (i = 0; i < usage->cUsageIdentifier; i++)
414 pUsage->rgpszUsageIdentifier[i] = nextOID;
415 strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
416 nextOID += strlen(nextOID) + 1;
420 pUsage->cUsageIdentifier = 0;
425 TRACE("returning %d\n", ret);
429 BOOL WINAPI CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext,
430 PCERT_ENHKEY_USAGE pUsage)
434 TRACE("(%p, %p)\n", pCertContext, pUsage);
438 CRYPT_DATA_BLOB blob = { 0, NULL };
440 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_ENHANCED_KEY_USAGE,
441 pUsage, CRYPT_ENCODE_ALLOC_FLAG, NULL, &blob.pbData, &blob.cbData);
444 ret = CertSetCertificateContextProperty(pCertContext,
445 CERT_ENHKEY_USAGE_PROP_ID, 0, &blob);
446 LocalFree(blob.pbData);
450 ret = CertSetCertificateContextProperty(pCertContext,
451 CERT_ENHKEY_USAGE_PROP_ID, 0, NULL);
455 BOOL WINAPI CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
456 LPCSTR pszUsageIdentifier)
461 TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
463 if (CertGetEnhancedKeyUsage(pCertContext,
464 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, NULL, &size))
466 PCERT_ENHKEY_USAGE usage = CryptMemAlloc(size);
470 ret = CertGetEnhancedKeyUsage(pCertContext,
471 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size);
474 PCERT_ENHKEY_USAGE newUsage = CryptMemAlloc(size +
475 sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
482 newUsage->rgpszUsageIdentifier =
483 (LPSTR *)((LPBYTE)newUsage + sizeof(CERT_ENHKEY_USAGE));
484 nextOID = (LPSTR)((LPBYTE)newUsage->rgpszUsageIdentifier +
485 (usage->cUsageIdentifier + 1) * sizeof(LPSTR));
486 for (i = 0; i < usage->cUsageIdentifier; i++)
488 newUsage->rgpszUsageIdentifier[i] = nextOID;
489 strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
490 nextOID += strlen(nextOID) + 1;
492 newUsage->rgpszUsageIdentifier[i] = nextOID;
493 strcpy(nextOID, pszUsageIdentifier);
494 newUsage->cUsageIdentifier = i + 1;
495 ret = CertSetEnhancedKeyUsage(pCertContext, newUsage);
496 CryptMemFree(newUsage);
506 PCERT_ENHKEY_USAGE usage = CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE) +
507 sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
511 usage->rgpszUsageIdentifier =
512 (LPSTR *)((LPBYTE)usage + sizeof(CERT_ENHKEY_USAGE));
513 usage->rgpszUsageIdentifier[0] = (LPSTR)((LPBYTE)usage +
514 sizeof(CERT_ENHKEY_USAGE) + sizeof(LPSTR));
515 strcpy(usage->rgpszUsageIdentifier[0], pszUsageIdentifier);
516 usage->cUsageIdentifier = 1;
517 ret = CertSetEnhancedKeyUsage(pCertContext, usage);
526 BOOL WINAPI CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
527 LPCSTR pszUsageIdentifier)
531 CERT_ENHKEY_USAGE usage;
533 TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
535 size = sizeof(usage);
536 ret = CertGetEnhancedKeyUsage(pCertContext,
537 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, &usage, &size);
538 if (!ret && GetLastError() == ERROR_MORE_DATA)
540 PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
544 ret = CertGetEnhancedKeyUsage(pCertContext,
545 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, pUsage, &size);
548 if (pUsage->cUsageIdentifier)
553 for (i = 0; i < pUsage->cUsageIdentifier; i++)
555 if (!strcmp(pUsage->rgpszUsageIdentifier[i],
558 if (found && i < pUsage->cUsageIdentifier - 1)
559 pUsage->rgpszUsageIdentifier[i] =
560 pUsage->rgpszUsageIdentifier[i + 1];
562 pUsage->cUsageIdentifier--;
563 /* Remove the usage if it's empty */
564 if (pUsage->cUsageIdentifier)
565 ret = CertSetEnhancedKeyUsage(pCertContext, pUsage);
567 ret = CertSetEnhancedKeyUsage(pCertContext, NULL);
570 CryptMemFree(pUsage);
577 /* it fit in an empty usage, therefore there's nothing to remove */
583 BOOL WINAPI CertGetValidUsages(DWORD cCerts, PCCERT_CONTEXT *rghCerts,
584 int *cNumOIDSs, LPSTR *rghOIDs, DWORD *pcbOIDs)
588 BOOL allUsagesValid = TRUE;
589 CERT_ENHKEY_USAGE validUsages = { 0, NULL };
591 TRACE("(%ld, %p, %p, %p, %ld)\n", cCerts, *rghCerts, cNumOIDSs,
594 for (i = 0; ret && i < cCerts; i++)
596 CERT_ENHKEY_USAGE usage;
597 DWORD size = sizeof(usage);
599 ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, &usage, &size);
600 /* Success is deliberately ignored: it implies all usages are valid */
601 if (!ret && GetLastError() == ERROR_MORE_DATA)
603 PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
605 allUsagesValid = FALSE;
608 ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, pUsage, &size);
611 if (!validUsages.cUsageIdentifier)
615 cbOIDs = pUsage->cUsageIdentifier * sizeof(LPSTR);
616 validUsages.cUsageIdentifier = pUsage->cUsageIdentifier;
617 for (j = 0; j < validUsages.cUsageIdentifier; j++)
618 cbOIDs += lstrlenA(pUsage->rgpszUsageIdentifier[j])
620 validUsages.rgpszUsageIdentifier =
621 CryptMemAlloc(cbOIDs);
622 if (validUsages.rgpszUsageIdentifier)
624 LPSTR nextOID = (LPSTR)
625 ((LPBYTE)validUsages.rgpszUsageIdentifier +
626 validUsages.cUsageIdentifier * sizeof(LPSTR));
628 for (j = 0; j < validUsages.cUsageIdentifier; j++)
630 validUsages.rgpszUsageIdentifier[j] = nextOID;
631 lstrcpyA(validUsages.rgpszUsageIdentifier[j],
632 pUsage->rgpszUsageIdentifier[j]);
633 nextOID += lstrlenA(nextOID) + 1;
641 DWORD j, k, validIndexes = 0, numRemoved = 0;
643 /* Merge: build a bitmap of all the indexes of
644 * validUsages.rgpszUsageIdentifier that are in pUsage.
646 for (j = 0; j < pUsage->cUsageIdentifier; j++)
648 for (k = 0; k < validUsages.cUsageIdentifier; k++)
650 if (!strcmp(pUsage->rgpszUsageIdentifier[j],
651 validUsages.rgpszUsageIdentifier[k]))
653 validIndexes |= (1 << k);
658 /* Merge by removing from validUsages those that are
661 for (j = 0; j < validUsages.cUsageIdentifier; j++)
663 if (!(validIndexes & (1 << j)))
665 if (j < validUsages.cUsageIdentifier - 1)
667 memcpy(&validUsages.rgpszUsageIdentifier[j],
668 &validUsages.rgpszUsageIdentifier[j +
670 (validUsages.cUsageIdentifier - numRemoved
671 - j - 1) * sizeof(LPSTR));
673 validUsages.rgpszUsageIdentifier[j]) + 1 +
678 validUsages.cUsageIdentifier--;
683 CryptMemFree(pUsage);
698 if (!rghOIDs || *pcbOIDs < cbOIDs)
701 SetLastError(ERROR_MORE_DATA);
706 LPSTR nextOID = (LPSTR)((LPBYTE)rghOIDs +
707 validUsages.cUsageIdentifier * sizeof(LPSTR));
710 *cNumOIDSs = validUsages.cUsageIdentifier;
711 for (i = 0; i < validUsages.cUsageIdentifier; i++)
713 rghOIDs[i] = nextOID;
714 lstrcpyA(nextOID, validUsages.rgpszUsageIdentifier[i]);
715 nextOID += lstrlenA(nextOID) + 1;
720 CryptMemFree(validUsages.rgpszUsageIdentifier);
724 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
725 * pInfo is NULL, from the attributes of hProv.
727 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context,
728 PCRYPT_KEY_PROV_INFO pInfo, HCRYPTPROV hProv)
730 CRYPT_KEY_PROV_INFO info = { 0 };
738 ret = CryptGetProvParam(hProv, PP_CONTAINER, NULL, &size, 0);
741 LPSTR szContainer = CryptMemAlloc(size);
745 ret = CryptGetProvParam(hProv, PP_CONTAINER,
746 (BYTE *)szContainer, &size, 0);
749 len = MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
753 info.pwszContainerName = CryptMemAlloc(len *
755 len = MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
756 info.pwszContainerName, len);
759 CryptMemFree(szContainer);
762 ret = CryptGetProvParam(hProv, PP_NAME, NULL, &size, 0);
765 LPSTR szProvider = CryptMemAlloc(size);
769 ret = CryptGetProvParam(hProv, PP_NAME, (BYTE *)szProvider,
773 len = MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
777 info.pwszProvName = CryptMemAlloc(len *
779 len = MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
780 info.pwszProvName, len);
783 CryptMemFree(szProvider);
786 size = sizeof(info.dwKeySpec);
787 ret = CryptGetProvParam(hProv, PP_KEYSPEC, (LPBYTE)&info.dwKeySpec,
790 info.dwKeySpec = AT_SIGNATURE;
791 size = sizeof(info.dwProvType);
792 ret = CryptGetProvParam(hProv, PP_PROVTYPE, (LPBYTE)&info.dwProvType,
795 info.dwProvType = PROV_RSA_FULL;
799 ret = CertSetCertificateContextProperty(context, CERT_KEY_PROV_INFO_PROP_ID,
804 CryptMemFree(info.pwszContainerName);
805 CryptMemFree(info.pwszProvName);
809 /* Creates a signed certificate context from the unsigned, encoded certificate
810 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
812 static PCCERT_CONTEXT CRYPT_CreateSignedCert(PCRYPT_DER_BLOB blob,
813 HCRYPTPROV hProv, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo)
815 PCCERT_CONTEXT context = NULL;
819 ret = CryptSignCertificate(hProv, AT_SIGNATURE, X509_ASN_ENCODING,
820 blob->pbData, blob->cbData, sigAlgo, NULL, NULL, &sigSize);
823 LPBYTE sig = CryptMemAlloc(sigSize);
825 ret = CryptSignCertificate(hProv, AT_SIGNATURE, X509_ASN_ENCODING,
826 blob->pbData, blob->cbData, sigAlgo, NULL, sig, &sigSize);
829 CERT_SIGNED_CONTENT_INFO signedInfo;
830 BYTE *encodedSignedCert = NULL;
831 DWORD encodedSignedCertSize = 0;
833 signedInfo.ToBeSigned.cbData = blob->cbData;
834 signedInfo.ToBeSigned.pbData = blob->pbData;
835 memcpy(&signedInfo.SignatureAlgorithm, sigAlgo,
836 sizeof(signedInfo.SignatureAlgorithm));
837 signedInfo.Signature.cbData = sigSize;
838 signedInfo.Signature.pbData = sig;
839 signedInfo.Signature.cUnusedBits = 0;
840 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT,
841 &signedInfo, CRYPT_ENCODE_ALLOC_FLAG, NULL,
842 (BYTE *)&encodedSignedCert, &encodedSignedCertSize);
845 context = CertCreateCertificateContext(X509_ASN_ENCODING,
846 encodedSignedCert, encodedSignedCertSize);
847 LocalFree(encodedSignedCert);
855 /* Copies data from the parameters into info, where:
856 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
858 * pSignatureAlgorithm: Optional.
859 * pStartTime: The starting time of the certificate. If NULL, the current
860 * system time is used.
861 * pEndTime: The ending time of the certificate. If NULL, one year past the
862 * starting time is used.
863 * pubKey: The public key of the certificate. Must not be NULL.
864 * pExtensions: Extensions to be included with the certificate. Optional.
866 static void CRYPT_MakeCertInfo(PCERT_INFO info,
867 PCERT_NAME_BLOB pSubjectIssuerBlob,
868 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm, PSYSTEMTIME pStartTime,
869 PSYSTEMTIME pEndTime, PCERT_PUBLIC_KEY_INFO pubKey,
870 PCERT_EXTENSIONS pExtensions)
872 /* FIXME: what serial number to use? */
873 static const BYTE serialNum[] = { 1 };
876 assert(pSubjectIssuerBlob);
879 info->dwVersion = CERT_V3;
880 info->SerialNumber.cbData = sizeof(serialNum);
881 info->SerialNumber.pbData = (LPBYTE)serialNum;
882 if (pSignatureAlgorithm)
883 memcpy(&info->SignatureAlgorithm, pSignatureAlgorithm,
884 sizeof(info->SignatureAlgorithm));
887 info->SignatureAlgorithm.pszObjId = szOID_RSA_SHA1RSA;
888 info->SignatureAlgorithm.Parameters.cbData = 0;
889 info->SignatureAlgorithm.Parameters.pbData = NULL;
891 info->Issuer.cbData = pSubjectIssuerBlob->cbData;
892 info->Issuer.pbData = pSubjectIssuerBlob->pbData;
894 SystemTimeToFileTime(pStartTime, &info->NotBefore);
896 GetSystemTimeAsFileTime(&info->NotBefore);
898 SystemTimeToFileTime(pStartTime, &info->NotAfter);
903 if (FileTimeToSystemTime(&info->NotBefore, &endTime))
906 SystemTimeToFileTime(&endTime, &info->NotAfter);
909 info->Subject.cbData = pSubjectIssuerBlob->cbData;
910 info->Subject.pbData = pSubjectIssuerBlob->pbData;
911 memcpy(&info->SubjectPublicKeyInfo, pubKey,
912 sizeof(info->SubjectPublicKeyInfo));
915 info->cExtension = pExtensions->cExtension;
916 info->rgExtension = pExtensions->rgExtension;
920 info->cExtension = 0;
921 info->rgExtension = NULL;
925 typedef RPC_STATUS (RPC_ENTRY *UuidCreateFunc)(UUID *);
926 typedef RPC_STATUS (RPC_ENTRY *UuidToStringFunc)(UUID *, unsigned char **);
927 typedef RPC_STATUS (RPC_ENTRY *RpcStringFreeFunc)(unsigned char **);
929 static HCRYPTPROV CRYPT_CreateKeyProv(void)
931 HCRYPTPROV hProv = 0;
932 HMODULE rpcrt = LoadLibraryA("rpcrt4");
936 UuidCreateFunc uuidCreate = (UuidCreateFunc)GetProcAddress(rpcrt,
938 UuidToStringFunc uuidToString = (UuidToStringFunc)GetProcAddress(rpcrt,
940 RpcStringFreeFunc rpcStringFree = (RpcStringFreeFunc)GetProcAddress(
941 rpcrt, "RpcStringFree");
943 if (uuidCreate && uuidToString && rpcStringFree)
946 RPC_STATUS status = uuidCreate(&uuid);
948 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
950 unsigned char *uuidStr;
952 status = uuidToString(&uuid, &uuidStr);
953 if (status == RPC_S_OK)
955 BOOL ret = CryptAcquireContextA(&hProv, (LPCSTR)uuidStr,
956 MS_DEF_PROV_A, PROV_RSA_FULL, CRYPT_NEWKEYSET);
962 ret = CryptGenKey(hProv, AT_SIGNATURE, 0, &key);
964 CryptDestroyKey(key);
966 rpcStringFree(&uuidStr);
975 PCCERT_CONTEXT WINAPI CertCreateSelfSignCertificate(HCRYPTPROV hProv,
976 PCERT_NAME_BLOB pSubjectIssuerBlob, DWORD dwFlags,
977 PCRYPT_KEY_PROV_INFO pKeyProvInfo,
978 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm, PSYSTEMTIME pStartTime,
979 PSYSTEMTIME pEndTime, PCERT_EXTENSIONS pExtensions)
981 PCCERT_CONTEXT context = NULL;
982 BOOL ret, releaseContext = FALSE;
983 PCERT_PUBLIC_KEY_INFO pubKey = NULL;
984 DWORD pubKeySize = 0;
986 TRACE("(0x%08lx, %p, %08lx, %p, %p, %p, %p, %p)\n", hProv,
987 pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime,
988 pExtensions, pExtensions);
992 hProv = CRYPT_CreateKeyProv();
993 releaseContext = TRUE;
996 CryptExportPublicKeyInfo(hProv, AT_SIGNATURE, X509_ASN_ENCODING, NULL,
998 pubKey = CryptMemAlloc(pubKeySize);
1001 ret = CryptExportPublicKeyInfo(hProv, AT_SIGNATURE, X509_ASN_ENCODING,
1002 pubKey, &pubKeySize);
1005 CERT_INFO info = { 0 };
1006 CRYPT_DER_BLOB blob = { 0, NULL };
1009 CRYPT_MakeCertInfo(&info, pSubjectIssuerBlob, pSignatureAlgorithm,
1010 pStartTime, pEndTime, pubKey, pExtensions);
1011 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
1012 &info, CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&blob.pbData,
1016 if (!(dwFlags & CERT_CREATE_SELFSIGN_NO_SIGN))
1017 context = CRYPT_CreateSignedCert(&blob, hProv,
1018 &info.SignatureAlgorithm);
1020 context = CertCreateCertificateContext(X509_ASN_ENCODING,
1021 blob.pbData, blob.cbData);
1022 if (context && !(dwFlags & CERT_CREATE_SELFSIGN_NO_KEY_INFO))
1023 CertContext_SetKeyProvInfo(context, pKeyProvInfo, hProv);
1024 LocalFree(blob.pbData);
1027 CryptMemFree(pubKey);
1030 CryptReleaseContext(hProv, 0);