tandaica0612
tandaica0612

Reputation: 371

How to obtain Provider name of a CSP from PCCERT_CONTEXT?

I have been try to get a provider name from PCCERT_CONTEXT because in my current project i must have load all certificate from smart card into my program. And in the future I have to deal with those certificate with some task like renewal certificate, delete certificate. But I have problem, I must to map CSP name and provider name with CryptAcquireContext to executive. And I currently confused how to archive this, can anyone have some guide to help me resolve this problem. I have try CertGetCertificateContextProperty with dwPropId is CERT_KEY_PROV_INFO_PROP_ID but i can not get CRYPT_KEY_PROV_INFO.

Upvotes: 2

Views: 2340

Answers (1)

Raj
Raj

Reputation: 1163

If I have understood you correctly, the following snippet shows how to extract the key provider information from a certificate.

void trace(char* message, DWORD errorCode)
{
    cout << message << errorCode;
}

std::wstring Test_CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext)
{
    DWORD dwSize = 0;    
    BOOL bIsSuccess = CertGetCertificateContextProperty(pCertContext, 
                                                        CERT_KEY_PROV_INFO_PROP_ID,
                                                        NULL,
                                                        &dwSize);
    if (!bIsSuccess)
    {
        trace("CertGetCertificateContextProperty failed with error: ", GetLastError());
        return L"";
    }

    PCRYPT_KEY_PROV_INFO pKeyProvInfo = (PCRYPT_KEY_PROV_INFO)LocalAlloc(LMEM_ZEROINIT, dwSize);
    if (pKeyProvInfo == NULL)
    {
        trace("LocalAlloc failed with error:", GetLastError());
        return L"";
    }

    bIsSuccess = CertGetCertificateContextProperty(pCertContext, 
                                                   CERT_KEY_PROV_INFO_PROP_ID,
                                                   pKeyProvInfo,
                                                   &dwSize);

    std::wstring provName;
    if (bIsSuccess)
    {
        provName = pKeyProvInfo->pwszProvName;
    }

    LocalFree(pKeyProvInfo);

    return provName;
}

Upvotes: 2

Related Questions