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 BOOL WINAPI CryptVerifyCertificateSignatureEx(HCRYPTPROV hCryptProv,
197 DWORD dwCertEncodingType, DWORD dwSubjectType, void *pvSubject,
198 DWORD dwIssuerType, void *pvIssuer, DWORD dwFlags, void *pvReserved)
201 CRYPT_DATA_BLOB subjectBlob;
203 TRACE("(%08lx, %ld, %ld, %p, %ld, %p, %08lx, %p)\n", hCryptProv,
204 dwCertEncodingType, dwSubjectType, pvSubject, dwIssuerType, pvIssuer,
205 dwFlags, pvReserved);
207 switch (dwSubjectType)
209 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB:
211 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvSubject;
213 subjectBlob.pbData = blob->pbData;
214 subjectBlob.cbData = blob->cbData;
217 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT:
219 PCERT_CONTEXT context = (PCERT_CONTEXT)pvSubject;
221 subjectBlob.pbData = context->pbCertEncoded;
222 subjectBlob.cbData = context->cbCertEncoded;
225 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL:
227 PCRL_CONTEXT context = (PCRL_CONTEXT)pvSubject;
229 subjectBlob.pbData = context->pbCrlEncoded;
230 subjectBlob.cbData = context->cbCrlEncoded;
234 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
240 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
243 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT,
244 subjectBlob.pbData, subjectBlob.cbData,
245 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
246 (BYTE *)&signedCert, &size);
249 switch (dwIssuerType)
251 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY:
253 PCERT_PUBLIC_KEY_INFO pubKeyInfo =
254 (PCERT_PUBLIC_KEY_INFO)pvIssuer;
255 ALG_ID algID = CertOIDToAlgId(pubKeyInfo->Algorithm.pszObjId);
261 ret = CryptImportPublicKeyInfoEx(hCryptProv,
262 dwCertEncodingType, pubKeyInfo, algID, 0, NULL, &key);
267 ret = CryptCreateHash(hCryptProv, algID, 0, 0, &hash);
270 ret = CryptHashData(hash,
271 signedCert->ToBeSigned.pbData,
272 signedCert->ToBeSigned.cbData, 0);
275 ret = CryptVerifySignatureW(hash,
276 signedCert->Signature.pbData,
277 signedCert->Signature.cbData, key, NULL, 0);
279 CryptDestroyHash(hash);
281 CryptDestroyKey(key);
286 SetLastError(NTE_BAD_ALGID);
291 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT:
292 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN:
293 FIXME("issuer type %ld: stub\n", dwIssuerType);
296 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL:
299 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
304 FIXME("unimplemented for NULL signer\n");
305 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
310 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
313 LocalFree(signedCert);
319 BOOL WINAPI CertGetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext, DWORD dwFlags,
320 PCERT_ENHKEY_USAGE pUsage, DWORD *pcbUsage)
322 PCERT_ENHKEY_USAGE usage = NULL;
326 TRACE("(%p, %08lx, %p, %ld)\n", pCertContext, dwFlags, pUsage, *pcbUsage);
328 if (!pCertContext || !pcbUsage)
330 SetLastError(ERROR_INVALID_PARAMETER);
334 if (!(dwFlags & CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG))
338 if (CertGetCertificateContextProperty(pCertContext,
339 CERT_ENHKEY_USAGE_PROP_ID, NULL, &propSize))
341 LPBYTE buf = CryptMemAlloc(propSize);
345 if (CertGetCertificateContextProperty(pCertContext,
346 CERT_ENHKEY_USAGE_PROP_ID, buf, &propSize))
348 ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
349 X509_ENHANCED_KEY_USAGE, buf, propSize,
350 CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
356 if (!usage && !(dwFlags & CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG))
358 PCERT_EXTENSION ext = CertFindExtension(szOID_ENHANCED_KEY_USAGE,
359 pCertContext->pCertInfo->cExtension,
360 pCertContext->pCertInfo->rgExtension);
364 ret = CryptDecodeObjectEx(pCertContext->dwCertEncodingType,
365 X509_ENHANCED_KEY_USAGE, ext->Value.pbData, ext->Value.cbData,
366 CRYPT_ENCODE_ALLOC_FLAG, NULL, &usage, &bytesNeeded);
371 /* If a particular location is specified, this should fail. Otherwise
372 * it should succeed with an empty usage. (This is true on Win2k and
373 * later, which we emulate.)
377 SetLastError(CRYPT_E_NOT_FOUND);
381 bytesNeeded = sizeof(CERT_ENHKEY_USAGE);
387 *pcbUsage = bytesNeeded;
388 else if (*pcbUsage < bytesNeeded)
390 SetLastError(ERROR_MORE_DATA);
391 *pcbUsage = bytesNeeded;
396 *pcbUsage = bytesNeeded;
400 LPSTR nextOID = (LPSTR)((LPBYTE)pUsage +
401 sizeof(CERT_ENHKEY_USAGE) +
402 usage->cUsageIdentifier * sizeof(LPSTR));
404 pUsage->cUsageIdentifier = usage->cUsageIdentifier;
405 pUsage->rgpszUsageIdentifier = (LPSTR *)((LPBYTE)pUsage +
406 sizeof(CERT_ENHKEY_USAGE));
407 for (i = 0; i < usage->cUsageIdentifier; i++)
409 pUsage->rgpszUsageIdentifier[i] = nextOID;
410 strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
411 nextOID += strlen(nextOID) + 1;
415 pUsage->cUsageIdentifier = 0;
420 TRACE("returning %d\n", ret);
424 BOOL WINAPI CertSetEnhancedKeyUsage(PCCERT_CONTEXT pCertContext,
425 PCERT_ENHKEY_USAGE pUsage)
429 TRACE("(%p, %p)\n", pCertContext, pUsage);
433 CRYPT_DATA_BLOB blob = { 0, NULL };
435 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_ENHANCED_KEY_USAGE,
436 pUsage, CRYPT_ENCODE_ALLOC_FLAG, NULL, &blob.pbData, &blob.cbData);
439 ret = CertSetCertificateContextProperty(pCertContext,
440 CERT_ENHKEY_USAGE_PROP_ID, 0, &blob);
441 LocalFree(blob.pbData);
445 ret = CertSetCertificateContextProperty(pCertContext,
446 CERT_ENHKEY_USAGE_PROP_ID, 0, NULL);
450 BOOL WINAPI CertAddEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
451 LPCSTR pszUsageIdentifier)
456 TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
458 if (CertGetEnhancedKeyUsage(pCertContext,
459 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, NULL, &size))
461 PCERT_ENHKEY_USAGE usage = CryptMemAlloc(size);
465 ret = CertGetEnhancedKeyUsage(pCertContext,
466 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size);
469 PCERT_ENHKEY_USAGE newUsage = CryptMemAlloc(size +
470 sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
477 newUsage->rgpszUsageIdentifier =
478 (LPSTR *)((LPBYTE)newUsage + sizeof(CERT_ENHKEY_USAGE));
479 nextOID = (LPSTR)((LPBYTE)newUsage->rgpszUsageIdentifier +
480 (usage->cUsageIdentifier + 1) * sizeof(LPSTR));
481 for (i = 0; i < usage->cUsageIdentifier; i++)
483 newUsage->rgpszUsageIdentifier[i] = nextOID;
484 strcpy(nextOID, usage->rgpszUsageIdentifier[i]);
485 nextOID += strlen(nextOID) + 1;
487 newUsage->rgpszUsageIdentifier[i] = nextOID;
488 strcpy(nextOID, pszUsageIdentifier);
489 newUsage->cUsageIdentifier = i + 1;
490 ret = CertSetEnhancedKeyUsage(pCertContext, newUsage);
491 CryptMemFree(newUsage);
501 PCERT_ENHKEY_USAGE usage = CryptMemAlloc(sizeof(CERT_ENHKEY_USAGE) +
502 sizeof(LPSTR) + strlen(pszUsageIdentifier) + 1);
506 usage->rgpszUsageIdentifier =
507 (LPSTR *)((LPBYTE)usage + sizeof(CERT_ENHKEY_USAGE));
508 usage->rgpszUsageIdentifier[0] = (LPSTR)((LPBYTE)usage +
509 sizeof(CERT_ENHKEY_USAGE) + sizeof(LPSTR));
510 strcpy(usage->rgpszUsageIdentifier[0], pszUsageIdentifier);
511 usage->cUsageIdentifier = 1;
512 ret = CertSetEnhancedKeyUsage(pCertContext, usage);
521 BOOL WINAPI CertRemoveEnhancedKeyUsageIdentifier(PCCERT_CONTEXT pCertContext,
522 LPCSTR pszUsageIdentifier)
526 CERT_ENHKEY_USAGE usage;
528 TRACE("(%p, %s)\n", pCertContext, debugstr_a(pszUsageIdentifier));
530 size = sizeof(usage);
531 ret = CertGetEnhancedKeyUsage(pCertContext,
532 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, &usage, &size);
533 if (!ret && GetLastError() == ERROR_MORE_DATA)
535 PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
539 ret = CertGetEnhancedKeyUsage(pCertContext,
540 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, pUsage, &size);
543 if (pUsage->cUsageIdentifier)
548 for (i = 0; i < pUsage->cUsageIdentifier; i++)
550 if (!strcmp(pUsage->rgpszUsageIdentifier[i],
553 if (found && i < pUsage->cUsageIdentifier - 1)
554 pUsage->rgpszUsageIdentifier[i] =
555 pUsage->rgpszUsageIdentifier[i + 1];
557 pUsage->cUsageIdentifier--;
558 /* Remove the usage if it's empty */
559 if (pUsage->cUsageIdentifier)
560 ret = CertSetEnhancedKeyUsage(pCertContext, pUsage);
562 ret = CertSetEnhancedKeyUsage(pCertContext, NULL);
565 CryptMemFree(pUsage);
572 /* it fit in an empty usage, therefore there's nothing to remove */
578 BOOL WINAPI CertGetValidUsages(DWORD cCerts, PCCERT_CONTEXT *rghCerts,
579 int *cNumOIDSs, LPSTR *rghOIDs, DWORD *pcbOIDs)
583 BOOL allUsagesValid = TRUE;
584 CERT_ENHKEY_USAGE validUsages = { 0, NULL };
586 TRACE("(%ld, %p, %p, %p, %ld)\n", cCerts, *rghCerts, cNumOIDSs,
589 for (i = 0; ret && i < cCerts; i++)
591 CERT_ENHKEY_USAGE usage;
592 DWORD size = sizeof(usage);
594 ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, &usage, &size);
595 /* Success is deliberately ignored: it implies all usages are valid */
596 if (!ret && GetLastError() == ERROR_MORE_DATA)
598 PCERT_ENHKEY_USAGE pUsage = CryptMemAlloc(size);
600 allUsagesValid = FALSE;
603 ret = CertGetEnhancedKeyUsage(rghCerts[i], 0, pUsage, &size);
606 if (!validUsages.cUsageIdentifier)
610 cbOIDs = pUsage->cUsageIdentifier * sizeof(LPSTR);
611 validUsages.cUsageIdentifier = pUsage->cUsageIdentifier;
612 for (j = 0; j < validUsages.cUsageIdentifier; j++)
613 cbOIDs += lstrlenA(pUsage->rgpszUsageIdentifier[j])
615 validUsages.rgpszUsageIdentifier =
616 CryptMemAlloc(cbOIDs);
617 if (validUsages.rgpszUsageIdentifier)
619 LPSTR nextOID = (LPSTR)
620 ((LPBYTE)validUsages.rgpszUsageIdentifier +
621 validUsages.cUsageIdentifier * sizeof(LPSTR));
623 for (j = 0; j < validUsages.cUsageIdentifier; j++)
625 validUsages.rgpszUsageIdentifier[j] = nextOID;
626 lstrcpyA(validUsages.rgpszUsageIdentifier[j],
627 pUsage->rgpszUsageIdentifier[j]);
628 nextOID += lstrlenA(nextOID) + 1;
636 DWORD j, k, validIndexes = 0, numRemoved = 0;
638 /* Merge: build a bitmap of all the indexes of
639 * validUsages.rgpszUsageIdentifier that are in pUsage.
641 for (j = 0; j < pUsage->cUsageIdentifier; j++)
643 for (k = 0; k < validUsages.cUsageIdentifier; k++)
645 if (!strcmp(pUsage->rgpszUsageIdentifier[j],
646 validUsages.rgpszUsageIdentifier[k]))
648 validIndexes |= (1 << k);
653 /* Merge by removing from validUsages those that are
656 for (j = 0; j < validUsages.cUsageIdentifier; j++)
658 if (!(validIndexes & (1 << j)))
660 if (j < validUsages.cUsageIdentifier - 1)
662 memcpy(&validUsages.rgpszUsageIdentifier[j],
663 &validUsages.rgpszUsageIdentifier[j +
665 (validUsages.cUsageIdentifier - numRemoved
666 - j - 1) * sizeof(LPSTR));
668 validUsages.rgpszUsageIdentifier[j]) + 1 +
673 validUsages.cUsageIdentifier--;
678 CryptMemFree(pUsage);
693 if (!rghOIDs || *pcbOIDs < cbOIDs)
696 SetLastError(ERROR_MORE_DATA);
701 LPSTR nextOID = (LPSTR)((LPBYTE)rghOIDs +
702 validUsages.cUsageIdentifier * sizeof(LPSTR));
705 *cNumOIDSs = validUsages.cUsageIdentifier;
706 for (i = 0; i < validUsages.cUsageIdentifier; i++)
708 rghOIDs[i] = nextOID;
709 lstrcpyA(nextOID, validUsages.rgpszUsageIdentifier[i]);
710 nextOID += lstrlenA(nextOID) + 1;
715 CryptMemFree(validUsages.rgpszUsageIdentifier);
719 /* Sets the CERT_KEY_PROV_INFO_PROP_ID property of context from pInfo, or, if
720 * pInfo is NULL, from the attributes of hProv.
722 static void CertContext_SetKeyProvInfo(PCCERT_CONTEXT context,
723 PCRYPT_KEY_PROV_INFO pInfo, HCRYPTPROV hProv)
725 CRYPT_KEY_PROV_INFO info = { 0 };
733 ret = CryptGetProvParam(hProv, PP_CONTAINER, NULL, &size, 0);
736 LPSTR szContainer = CryptMemAlloc(size);
740 ret = CryptGetProvParam(hProv, PP_CONTAINER, (BYTE *)szContainer,
744 len = MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
748 info.pwszContainerName = CryptMemAlloc(len *
750 len = MultiByteToWideChar(CP_ACP, 0, szContainer, -1,
751 info.pwszContainerName, len);
754 CryptMemFree(szContainer);
757 ret = CryptGetProvParam(hProv, PP_NAME, NULL, &size, 0);
760 LPSTR szProvider = CryptMemAlloc(size);
764 ret = CryptGetProvParam(hProv, PP_NAME, (BYTE *)szProvider, &size, 0);
767 len = MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
771 info.pwszProvName = CryptMemAlloc(len *
773 len = MultiByteToWideChar(CP_ACP, 0, szProvider, -1,
774 info.pwszProvName, len);
777 CryptMemFree(szProvider);
780 size = sizeof(info.dwKeySpec);
781 ret = CryptGetProvParam(hProv, PP_KEYSPEC, (LPBYTE)&info.dwKeySpec,
784 info.dwKeySpec = AT_SIGNATURE;
785 size = sizeof(info.dwProvType);
786 ret = CryptGetProvParam(hProv, PP_PROVTYPE, (LPBYTE)&info.dwProvType,
789 info.dwProvType = PROV_RSA_FULL;
793 ret = CertSetCertificateContextProperty(context, CERT_KEY_PROV_INFO_PROP_ID,
798 CryptMemFree(info.pwszContainerName);
799 CryptMemFree(info.pwszProvName);
803 /* Creates a signed certificate context from the unsigned, encoded certificate
804 * in blob, using the crypto provider hProv and the signature algorithm sigAlgo.
806 static PCCERT_CONTEXT CRYPT_CreateSignedCert(PCRYPT_DER_BLOB blob,
807 HCRYPTPROV hProv, PCRYPT_ALGORITHM_IDENTIFIER sigAlgo)
809 PCCERT_CONTEXT context = NULL;
813 ret = CryptSignCertificate(hProv, AT_SIGNATURE, X509_ASN_ENCODING,
814 blob->pbData, blob->cbData, sigAlgo, NULL, NULL, &sigSize);
817 LPBYTE sig = CryptMemAlloc(sigSize);
819 ret = CryptSignCertificate(hProv, AT_SIGNATURE, X509_ASN_ENCODING,
820 blob->pbData, blob->cbData, sigAlgo, NULL, sig, &sigSize);
823 CERT_SIGNED_CONTENT_INFO signedInfo;
824 BYTE *encodedSignedCert = NULL;
825 DWORD encodedSignedCertSize = 0;
827 signedInfo.ToBeSigned.cbData = blob->cbData;
828 signedInfo.ToBeSigned.pbData = blob->pbData;
829 memcpy(&signedInfo.SignatureAlgorithm, sigAlgo,
830 sizeof(signedInfo.SignatureAlgorithm));
831 signedInfo.Signature.cbData = sigSize;
832 signedInfo.Signature.pbData = sig;
833 signedInfo.Signature.cUnusedBits = 0;
834 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT,
835 &signedInfo, CRYPT_ENCODE_ALLOC_FLAG, NULL,
836 (BYTE *)&encodedSignedCert, &encodedSignedCertSize);
839 context = CertCreateCertificateContext(X509_ASN_ENCODING,
840 encodedSignedCert, encodedSignedCertSize);
841 LocalFree(encodedSignedCert);
849 /* Copies data from the parameters into info, where:
850 * pSubjectIssuerBlob: Specifies both the subject and issuer for info.
852 * pSignatureAlgorithm: Optional.
853 * pStartTime: The starting time of the certificate. If NULL, the current
854 * system time is used.
855 * pEndTime: The ending time of the certificate. If NULL, one year past the
856 * starting time is used.
857 * pubKey: The public key of the certificate. Must not be NULL.
858 * pExtensions: Extensions to be included with the certificate. Optional.
860 static void CRYPT_MakeCertInfo(PCERT_INFO info,
861 PCERT_NAME_BLOB pSubjectIssuerBlob,
862 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm, PSYSTEMTIME pStartTime,
863 PSYSTEMTIME pEndTime, PCERT_PUBLIC_KEY_INFO pubKey,
864 PCERT_EXTENSIONS pExtensions)
866 /* FIXME: what serial number to use? */
867 static const BYTE serialNum[] = { 1 };
870 assert(pSubjectIssuerBlob);
873 info->dwVersion = CERT_V3;
874 info->SerialNumber.cbData = sizeof(serialNum);
875 info->SerialNumber.pbData = (LPBYTE)serialNum;
876 if (pSignatureAlgorithm)
877 memcpy(&info->SignatureAlgorithm, pSignatureAlgorithm,
878 sizeof(info->SignatureAlgorithm));
881 info->SignatureAlgorithm.pszObjId = szOID_RSA_SHA1RSA;
882 info->SignatureAlgorithm.Parameters.cbData = 0;
883 info->SignatureAlgorithm.Parameters.pbData = NULL;
885 info->Issuer.cbData = pSubjectIssuerBlob->cbData;
886 info->Issuer.pbData = pSubjectIssuerBlob->pbData;
888 SystemTimeToFileTime(pStartTime, &info->NotBefore);
890 GetSystemTimeAsFileTime(&info->NotBefore);
892 SystemTimeToFileTime(pStartTime, &info->NotBefore);
897 if (FileTimeToSystemTime(&info->NotBefore, &endTime))
900 SystemTimeToFileTime(&endTime, &info->NotAfter);
903 info->Subject.cbData = pSubjectIssuerBlob->cbData;
904 info->Subject.pbData = pSubjectIssuerBlob->pbData;
905 memcpy(&info->SubjectPublicKeyInfo, pubKey,
906 sizeof(info->SubjectPublicKeyInfo));
909 info->cExtension = pExtensions->cExtension;
910 info->rgExtension = pExtensions->rgExtension;
914 info->cExtension = 0;
915 info->rgExtension = NULL;
919 typedef RPC_STATUS (RPC_ENTRY *UuidCreateFunc)(UUID *);
920 typedef RPC_STATUS (RPC_ENTRY *UuidToStringFunc)(UUID *, unsigned char **);
921 typedef RPC_STATUS (RPC_ENTRY *RpcStringFreeFunc)(unsigned char **);
923 static HCRYPTPROV CRYPT_CreateKeyProv(void)
925 HCRYPTPROV hProv = 0;
926 HMODULE rpcrt = LoadLibraryA("rpcrt4");
930 UuidCreateFunc uuidCreate = (UuidCreateFunc)GetProcAddress(rpcrt,
932 UuidToStringFunc uuidToString = (UuidToStringFunc)GetProcAddress(rpcrt,
934 RpcStringFreeFunc rpcStringFree = (RpcStringFreeFunc)GetProcAddress(
935 rpcrt, "RpcStringFree");
937 if (uuidCreate && uuidToString && rpcStringFree)
940 RPC_STATUS status = uuidCreate(&uuid);
942 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
944 unsigned char *uuidStr;
946 status = uuidToString(&uuid, &uuidStr);
947 if (status == RPC_S_OK)
949 BOOL ret = CryptAcquireContextA(&hProv, (LPCSTR)uuidStr,
950 MS_DEF_PROV_A, PROV_RSA_FULL, CRYPT_NEWKEYSET);
956 ret = CryptGenKey(hProv, AT_SIGNATURE, 0, &key);
958 CryptDestroyKey(key);
960 rpcStringFree(&uuidStr);
969 PCCERT_CONTEXT WINAPI CertCreateSelfSignCertificate(HCRYPTPROV hProv,
970 PCERT_NAME_BLOB pSubjectIssuerBlob, DWORD dwFlags,
971 PCRYPT_KEY_PROV_INFO pKeyProvInfo,
972 PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm, PSYSTEMTIME pStartTime,
973 PSYSTEMTIME pEndTime, PCERT_EXTENSIONS pExtensions)
975 PCCERT_CONTEXT context = NULL;
976 BOOL ret, releaseContext = FALSE;
977 PCERT_PUBLIC_KEY_INFO pubKey = NULL;
978 DWORD pubKeySize = 0;
980 TRACE("(0x%08lx, %p, %08lx, %p, %p, %p, %p, %p)\n", hProv,
981 pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime,
982 pExtensions, pExtensions);
986 hProv = CRYPT_CreateKeyProv();
987 releaseContext = TRUE;
990 CryptExportPublicKeyInfo(hProv, AT_SIGNATURE, X509_ASN_ENCODING, NULL,
992 pubKey = CryptMemAlloc(pubKeySize);
995 ret = CryptExportPublicKeyInfo(hProv, AT_SIGNATURE, X509_ASN_ENCODING,
996 pubKey, &pubKeySize);
999 CERT_INFO info = { 0 };
1000 CRYPT_DER_BLOB blob = { 0, NULL };
1003 CRYPT_MakeCertInfo(&info, pSubjectIssuerBlob, pSignatureAlgorithm,
1004 pStartTime, pEndTime, pubKey, pExtensions);
1005 ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
1006 &info, CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&blob.pbData,
1010 if (!(dwFlags & CERT_CREATE_SELFSIGN_NO_SIGN))
1011 context = CRYPT_CreateSignedCert(&blob, hProv,
1012 &info.SignatureAlgorithm);
1014 context = CertCreateCertificateContext(X509_ASN_ENCODING,
1015 blob.pbData, blob.cbData);
1016 if (context && !(dwFlags & CERT_CREATE_SELFSIGN_NO_KEY_INFO))
1017 CertContext_SetKeyProvInfo(context, pKeyProvInfo, hProv);
1018 LocalFree(blob.pbData);
1021 CryptMemFree(pubKey);
1024 CryptReleaseContext(hProv, 0);