我想使用Crypto API在Windows7上获取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());
    }

最佳答案

我敢打赌,您获得的不是-2146885682,而是-2146885628,又名0x80092004,又名CRYPT_E_NOT_FOUND。很简单,商店中没有CTL。

10-04 13:34