如何解决将私钥写入令牌
我使用 PKCS#11 来模拟令牌,我在 Windows 证书存储中有一个证书,上面写着“您有一个与此证书对应的私钥”。我尝试找到可以读取证书私钥的 WinAPI 函数(该证书位于证书存储区或 pfx 格式的硬盘驱动器中)。我怎么能做到这一点? 我的功能
static void ImportPrivateKey(const char* fileName,const char* password)
{
HCERTSTORE hSystemStore;
//Open the certificate store to be searched
if(hSystemStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,NULL,CERT_SYSTEM_STORE_CURRENT_USER,L"MY"))
printf("Opened the MY system store. \n");
else
printf( "Could not open the MY system store.\n");
PCCERT_CONTEXT pDesiredCert = NULL;
LPWSTR lpszCertSubject = L"7172gka"; // !!!! Name of certificate to search
// Search certificate
if(pDesiredCert=CertFindCertificateInStore(
hSystemStore,MY_ENCODING_TYPE,CERT_FIND_SUBJECT_STR,lpszCertSubject,pDesiredCert))
{
printf("The desired certificate was found. \n");
}
else
{
printf("Could not find the desired certificate.\n");
}
DWORD dwSize = 0;
CRYPT_KEY_PROV_INFO* pKeyInfo = NULL;
if(!(CertGetCertificateContextProperty(
pDesiredCert,CERT_KEY_PROV_INFO_PROP_ID,&dwSize)))
{
printf("Error getting key property.\n");
}
if(pKeyInfo)
free(pKeyInfo);
pKeyInfo = (CRYPT_KEY_PROV_INFO*)malloc(dwSize);
if(!pKeyInfo)
{
printf("Error allocating memory for pKeyInfo.\n");
}
if(!(CertGetCertificateContextProperty(
pDesiredCert,pKeyInfo,&dwSize)))
{
printf("The second call to the function failed\n");
}
printf("%s\n",pKeyInfo->pwszProvName);
if(pKeyInfo->dwKeySpec == AT_SIGNATURE)
printf("Ok!\n");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。