n***@email.com
2005-09-10 20:41:36 UTC
Hi,
i have some problems with certificates, my code is heavily based on
certificate creation example from MS SDK and i'm not sure about way how
private key asociated with my certificate is inserted into system... my
code goes about this way:
#define MY_PROVNAME MS_ENHANCED_PROV
#define MY_PROVTYPE PROV_RSA_FULL
#define MY_ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
// create crypto context
if(!CryptAcquireContext(&hCryptProv, szContainer, MY_PROVNAME,
MY_PROVTYPE, CRYPT_NEWKEYSET | dwCertContainer))
break;
// generate public/private key pair
if(!CryptGenKey(hCryptProv, dwKeyType, CRYPT_EXPORTABLE,
&hPubPrivKeyPair))
break;
... here goes code about filling CERT_INFO structure and adding some
important extensions to certificate...
// finally, crypt, sign & encode certificate
if(!CryptSignAndEncodeCertificate(hIssuerCryptProv, dwIssuerKeyType,
MY_ENCODING, X509_CERT_TO_BE_SIGNED, (LPVOID)&stCertInfo,
&(stIssuerCert->pCertInfo->SignatureAlgorithm), NULL, NULL, &dwSize))
break;
if(!(pEncodedCert = (LPBYTE)HeapAlloc(hHeap, 0, dwSize)))
break;
if(!CryptSignAndEncodeCertificate(hIssuerCryptProv, dwIssuerKeyType,
MY_ENCODING, X509_CERT_TO_BE_SIGNED,
(LPVOID)&stCertInfo, &(stIssuerCert->pCertInfo->SignatureAlgorithm),
NULL, pEncodedCert, &dwSize))
break;
// open specified certificate store
if(!(hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, MY_ENCODING,
NULL, dwCertLocation, A2W(szCertStore))))
break;
// insert encoded certificate into store
if(!CertAddEncodedCertificateToStore(hCertStore, MY_ENCODING,
pEncodedCert, dwSize, CERT_STORE_ADD_REPLACE_EXISTING, &pCertContext))
break;
// get private key blob
LPBYTE pPrivateKey = NULL;
if(!CryptExportKey(hPubPrivKeyPair, NULL, PRIVATEKEYBLOB, 0, NULL,
&dwSize))
break;
if(!(pPrivateKey = (LPBYTE)HeapAlloc(hHeap, 0, dwSize)))
break;
if(!CryptExportKey(hPubPrivKeyPair, NULL, PRIVATEKEYBLOB, 0,
pPrivateKey, &dwSize))
break;
// import key
HCRYPTKEY outKey = NULL;
if(!CryptImportKey(hCryptProv, pPrivateKey, dwSize, 0,
CRYPT_EXPORTABLE, &outKey))
break;
// initialize CRYPT_KEY_PROV_INFO structure
ZeroMemory(&stCryptKeyProvInfo, sizeof(stCryptKeyProvInfo));
stCryptKeyProvInfo.pwszContainerName = A2W(szContainer);
stCryptKeyProvInfo.pwszProvName = A2W(MY_PROVNAME);
stCryptKeyProvInfo.dwProvType = MY_PROVTYPE;
stCryptKeyProvInfo.dwKeySpec = dwKeyType;
// set certificate's key provider info
if(!CertSetCertificateContextProperty(pCertContext,
CERT_KEY_PROV_INFO_PROP_ID, 0, (LPVOID)&stCryptKeyProvInfo))
break;
certificate imported to store in this 2-way manner (first certificate,
then key) and opened in mmc/certificates have private key asociated
with it (as i'm told by certificate) but this certificate didn't worked
in IIS when i tried to use HTTPS (and SSL Diagnostics told me that
CryptAcquireCertificatePrivateKey has failed).
Any advice about what's wrong with my code or how works association of
private key to certificate would be greatly appreciated, any included
code is welcomed also.
NonSuch
i have some problems with certificates, my code is heavily based on
certificate creation example from MS SDK and i'm not sure about way how
private key asociated with my certificate is inserted into system... my
code goes about this way:
#define MY_PROVNAME MS_ENHANCED_PROV
#define MY_PROVTYPE PROV_RSA_FULL
#define MY_ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
// create crypto context
if(!CryptAcquireContext(&hCryptProv, szContainer, MY_PROVNAME,
MY_PROVTYPE, CRYPT_NEWKEYSET | dwCertContainer))
break;
// generate public/private key pair
if(!CryptGenKey(hCryptProv, dwKeyType, CRYPT_EXPORTABLE,
&hPubPrivKeyPair))
break;
... here goes code about filling CERT_INFO structure and adding some
important extensions to certificate...
// finally, crypt, sign & encode certificate
if(!CryptSignAndEncodeCertificate(hIssuerCryptProv, dwIssuerKeyType,
MY_ENCODING, X509_CERT_TO_BE_SIGNED, (LPVOID)&stCertInfo,
&(stIssuerCert->pCertInfo->SignatureAlgorithm), NULL, NULL, &dwSize))
break;
if(!(pEncodedCert = (LPBYTE)HeapAlloc(hHeap, 0, dwSize)))
break;
if(!CryptSignAndEncodeCertificate(hIssuerCryptProv, dwIssuerKeyType,
MY_ENCODING, X509_CERT_TO_BE_SIGNED,
(LPVOID)&stCertInfo, &(stIssuerCert->pCertInfo->SignatureAlgorithm),
NULL, pEncodedCert, &dwSize))
break;
// open specified certificate store
if(!(hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, MY_ENCODING,
NULL, dwCertLocation, A2W(szCertStore))))
break;
// insert encoded certificate into store
if(!CertAddEncodedCertificateToStore(hCertStore, MY_ENCODING,
pEncodedCert, dwSize, CERT_STORE_ADD_REPLACE_EXISTING, &pCertContext))
break;
// get private key blob
LPBYTE pPrivateKey = NULL;
if(!CryptExportKey(hPubPrivKeyPair, NULL, PRIVATEKEYBLOB, 0, NULL,
&dwSize))
break;
if(!(pPrivateKey = (LPBYTE)HeapAlloc(hHeap, 0, dwSize)))
break;
if(!CryptExportKey(hPubPrivKeyPair, NULL, PRIVATEKEYBLOB, 0,
pPrivateKey, &dwSize))
break;
// import key
HCRYPTKEY outKey = NULL;
if(!CryptImportKey(hCryptProv, pPrivateKey, dwSize, 0,
CRYPT_EXPORTABLE, &outKey))
break;
// initialize CRYPT_KEY_PROV_INFO structure
ZeroMemory(&stCryptKeyProvInfo, sizeof(stCryptKeyProvInfo));
stCryptKeyProvInfo.pwszContainerName = A2W(szContainer);
stCryptKeyProvInfo.pwszProvName = A2W(MY_PROVNAME);
stCryptKeyProvInfo.dwProvType = MY_PROVTYPE;
stCryptKeyProvInfo.dwKeySpec = dwKeyType;
// set certificate's key provider info
if(!CertSetCertificateContextProperty(pCertContext,
CERT_KEY_PROV_INFO_PROP_ID, 0, (LPVOID)&stCryptKeyProvInfo))
break;
certificate imported to store in this 2-way manner (first certificate,
then key) and opened in mmc/certificates have private key asociated
with it (as i'm told by certificate) but this certificate didn't worked
in IIS when i tried to use HTTPS (and SSL Diagnostics told me that
CryptAcquireCertificatePrivateKey has failed).
Any advice about what's wrong with my code or how works association of
private key to certificate would be greatly appreciated, any included
code is welcomed also.
NonSuch