1

我想使用 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());
    }
4

1 回答 1

0

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

于 2013-08-08T04:11:01.823 回答