在一些运行 Windows 7 的系统上,我们在名称密钥容器中生成了一个密钥,如果我们更改用户的密码,当我们调用 CryptAcquireCertificatePrivateKey() 时,我们会收到错误 CRYPT_E_NO_KEY_PROPERTY (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;
}