我想使用 Crypto API 在 Windows7 上获取 CTL(证书信任列表),我使用 CertFindCTLInStore 函数,但它总是返回 false;GetLastError() 函数返回 -2146885682,我不知道我哪里做错了。代码如下:
hCertStore = CertOpenSystemStore(NULL, pszStoreName);
if(NULL != hCertStore)
{
char buffer[8*1024];
memset(buffer,0,sizeof(buffer));
PCCTL_CONTEXT pCTL = CertCreateCTLContext(
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
(const BYTE*)buffer,
sizeof(buffer));
CTL_FIND_USAGE_PARA usagePara;
//first
pCTL = CertFindCTLInStore(
hCertStore,
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
0,
CTL_FIND_ANY,
NULL,
NULL);
if (NULL != pCTL)
{
printf("Find a CTL\n");
}
else
{
printf("Error Code :%d\n",GetLastError());
}
//Second
CRYPT_HASH_BLOB cryBlob;
pCTL = CertFindCTLInStore(
hCertStore,
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
0,
CTL_FIND_SHA1_HASH,
&cryBlob,
NULL);
if (NULL != pCTL)
{
printf("Find a CTL\n");
}
else
{
printf("Error Code :%d\n",GetLastError());
}
//third
pCTL = CertFindCTLInStore(
hCertStore,
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
0,
CTL_FIND_MD5_HASH,
&cryBlob,
NULL);
if (NULL != pCTL)
{
printf("Find a CTL\n");
}
else
{
printf("Error Code :%d\n",GetLastError());
}
//fourth
CTL_FIND_USAGE_PARA ctlPara;
pCTL = CertFindCTLInStore(
hCertStore,
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
0,
CTL_FIND_USAGE,
&ctlPara,
NULL);
if (NULL != pCTL)
{
printf("Find a CTL\n");
}
else
{
printf("Error Code :%d\n",GetLastError());
}
//fifth
CTL_FIND_USAGE_PARA ctlPara2;
pCTL = CertFindCTLInStore(
hCertStore,
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
0,
CTL_FIND_SAME_USAGE_FLAG,
&ctlPara2,
NULL);
if (NULL != pCTL)
{
printf("Find a CTL\n");
}
else
{
printf("Error Code :%d\n",GetLastError());
}
//six
CTL_CONTEXT ctlText;
pCTL = CertFindCTLInStore(
hCertStore,
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
0,
CTL_FIND_EXISTING,
&ctlText,
NULL);
if (NULL != pCTL)
{
printf("Find a CTL\n");
}
else
{
printf("Error Code :%d\n",GetLastError());
}
//seven
CTL_FIND_SUBJECT_PARA ctlSubPara;
pCTL = CertFindCTLInStore(
hCertStore,
X509_ASN_ENCODING|PKCS_7_ASN_ENCODING,
0,
CTL_FIND_SUBJECT,
&ctlSubPara,
NULL);
if (NULL != pCTL)
{
printf("Find a CTL\n");
}
else
{
printf("Error Code :%d\n",GetLastError());
}