如何使用CryptoAPI在Windows7上获取证书信任列表

时间:2013-08-08 01:48:43

标签: c++ windows certificate cryptoapi

我想在Windows7上使用Crypto API获取CTL(证书信任列表),我使用CertFindCTLInStore函数,但它总是返回false; GetLastError()函数返回-2146885682,我不知道我在哪里做错了。 代码如下:

hCertStore = CertOpenSystemStore(         空值,         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());
    }

1 个答案:

答案 0 :(得分:0)

我打赌你没有得到-2146885682但是-2146885628,又名0x80092004又名CRYPT_E_NOT_FOUND。很简单,商店里没有CTL。