在运行Windows7的某些系统上,我们在
当我们调用
CryptAcquireCertificatePrivateKey()我们得到一个错误
crypt_e_no_key_属性(0x8009200B)。
并非所有的盒子都会发生这种情况。我们自然而然地认为它是一个不在网络上的域机器的东西,在那里它不能刷新它的域内容,但是我们让它在一些独立的机器上复制。
我的钥匙代码
if((StoreHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER, QWARQ_CERT_STORE_NAME)) != NULL)
{
/* Look for certificate with matching user guid. */
if((CertContext = CertFindCertificateInStore(StoreHandle,PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_STR,DataBuffer, NULL)) != NULL)
{
if(CryptAcquireCertificatePrivateKey(CertContext, 0,NULL, &CryptProvHandle, &KeySpec, &FreeHandle))
{
}
else
{
DWORD dwError=GetLastError(); //CRYPT_E_NO_KEY_PROPERTY
}
}
}
下面是生成密钥/密钥容器的代码
if(CryptAcquireContext(&hCryptProv,KEY_CONTAINER_NAME, NULL,PROV_RSA_FULL, 0) == FALSE) //CRYPT_NEWKEYSET
{
DWORD result = GetLastError();
if (NTE_BAD_KEY_STATE == result)
{
DebugLogging::DbgPrintF(TEXT("[CertInitialization] NTE_BAD_KEY_STATE - user has changed his password \n"), result);
return false;
}
else if (NTE_BAD_KEYSET != result)
{
DebugLogging::DbgPrintF(TEXT("[CertInitialization] could not acquire CSP[0x%x]\n"), result);
return false;
}
if(CryptAcquireContext(&hCryptProv, KEY_CONTAINER_NAME, NULL,PROV_RSA_FULL, CRYPT_NEWKEYSET) == FALSE) //CRYPT_NEWKEYSET
{
DWORD result = GetLastError();
DebugLogging::DbgPrintF(TEXT("[CertInitialization] could not acquire CSP from new keyset[0x%x]\n"), result);
return false;
}
}
if(CryptGenKey(hCryptProv, AT_KEYEXCHANGE, RSA2048BIT_KEY |CRYPT_EXPORTABLE, &hKey)== FALSE)
{
DebugLogging::DbgPrintF(TEXT("CertGeneration() could not generate key[%d]\n"),GetLastError());
return false;
}