С помощью устаревшего API шифрования закрытый ключ импортируется успешно. проверено с помощью certutil -scinfo. Проблема с этим подходом заключается в том, что пользователю отображаются два диалоговых окна: одно для выбора виртуальной смарт-карты, а другое для PIN-кода.
Я создал виртуальную смарт-карту TPM с помощью следующей команды tpmvscmgr.
"tpmvscmgr.exe create /name TestVSC /pin default /adminkey random /generate"
Это код с устаревшим криптографическим API.
CryptAcquireContext() показывает диалоговые окна пользователя.< /p>
Код: Выделить всё
BOOL Legacy_LoadPrivateKey(LPBYTE pbBlob, DWORD cbSize, PCCERT_CONTEXT pCertContext,
CHAR szContainerName[1024])
{
DWORD dwLen = 0;
HCRYPTPROV hProv;
BOOL bRet = CryptAcquireContext(&hProv, NULL, MS_SCARD_PROV, PROV_RSA_FULL,
CRYPT_NEWKEYSET);
if (bRet)
{
const std::string pin = "12345678";
// get the container name
dwLen = 1024;
CryptGetProvParam(hProv, PP_CONTAINER, (BYTE*)szContainerName, &dwLen, 0);
HCRYPTKEY hKey;
bRet = CryptImportKey(hProv, pbBlob, cbSize, NULL, 0, &hKey);
if (bRet)
{
bRet = CryptSetKeyParam(hKey, KP_CERTIFICATE, pCertContext->pbCertEncoded, 0);
if (!bRet)
{
DWORD dwError = GetLastError();
//_tprintf(_T("Failed to import the certificate into the smart card. Error 0x%.8X\n"), dwError);
}
CryptDestroyKey(hKey);
}
else
{
DWORD dwError = GetLastError();
//_tprintf(_T("Failed to import the private key into the smart card. Error 0x%.8X\n"), dwError);
}
CryptReleaseContext(hProv, 0);
if (!bRet)
{
// delete the container because of the error
CryptAcquireContextA(&hProv, szContainerName, MS_SCARD_PROV_A, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
}
}
else
{
DWORD dwError = GetLastError();
//_tprintf(_T("Failed to create a new container on the smart card. Error 0x%.8X\n"), dwError);
}
return bRet;
}
NCryptImportKey() возвращает ошибку NTE_BAD_KEY_STATE
Код: Выделить всё
bool Ncrypt_ImportPrivateKey(const wchar_t* keyName, BYTE* privateKeyBlob, DWORD privateKeyBlobSize) {
NCRYPT_KEY_HANDLE hKey = 0;
SECURITY_STATUS status;
// Open a handle to the Microsoft Smart Card Key Storage Provider
NCRYPT_PROV_HANDLE hProvider = 0;
SECURITY_STATUS status = NCryptOpenStorageProvider(&hProvider,
MS_SMART_CARD_KEY_STORAGE_PROVIDER, 0);
if (status != ERROR_SUCCESS) {
std::cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/78709810/import-private-key-to-microsoft-tpm-virtual-smart-card-without-popups-for-card-s[/url]