2

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.

tandaica0612
  • 369
  • 10
  • 23

1 Answers1

2

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;
}
ya23
  • 14,226
  • 9
  • 46
  • 43
Raj
  • 1,113
  • 1
  • 17
  • 34