none
PFXExportCertStoreEx doesnt work for Root Store.Please help... RRS feed

  • Question

  • I am trying to export the certificates form the Root store of a device.The function call to PFXExportCertStoreEx is not working and seems to be raising unhanded System error.PFXExportCertStoreEx  doesnt work irrespective of whether EXPORT_PRIVATE_KEYS is included or not. both Sample code below.

    Function call :

    ExportCertificate(L"Root", "\Hard Disk\Test.pfx", "TEST");

    Function code below:


    HRESULT CertificateHandler::ExportCertificate(__in LPCTSTR storeName, __in LPCTSTR filePath, __in LPCTSTR passwd)

    {

    HRESULT hr = S_OK;

    CRYPT_DATA_BLOB pfxBlob;

    HCERTSTORE hCertStore = NULL;

    DWORD numberOfBytesWritten = 0;

    HANDLE hCertFile = NULL;

    hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0,

                            0, CERT_SYSTEM_STORE_CURRENT_USER, storeName);

    CBREx((hCertStore != NULL), GetLastError());

    //CBREx(TRUE == PFXExportCertStoreEx(hCertStore, &pfxBlob, passwd, NULL, EXPORT_PRIVATE_KEYS | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY ), GetLastError());



    CBREx(TRUE == PFXExportCertStoreEx(hCertStore, &pfxBlob, passwd, NULL, 0), GetLastError());

    if(pfxBlob.cbData > 0)

    {

    //create file


    hCertFile = ::CreateFile( filePath, GENERIC_WRITE,

    NULL , 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );

    if( INVALID_HANDLE_VALUE == hCertFile )

    {

    hr = GetLastError();

    RETAILMSG(1, (_T("ExportCertificate ERROR: Filed to create the file.error code : %u\r\n"), hr));

    goto Error;

    }

    //write to file


    CBREx((0 !=  ::WriteFile( hCertFile, &pfxBlob.pbData[0], pfxBlob.cbData, &numberOfBytesWritten, NULL)), GetLastError());

    }

    Error:

    if (hCertStore)

    {

    CertCloseStore(hCertStore, 0);

    }

    //close the file handle


    if(hCertFile)

    {

    ::CloseHandle(hCertFile);

    }

    return hr;

    }

    Tuesday, September 4, 2018 6:56 AM

Answers

  • Thanks.

    There is an issue in my call to PFXExportCertStoreEx.It has to be called twice. Once to fetch the expected blob size. Second time with blob memory allocated.

    This issue is fixed now with the above changes made.


    • Edited by RachanaSingh Wednesday, September 5, 2018 6:34 AM
    • Marked as answer by RachanaSingh Wednesday, September 5, 2018 6:34 AM
    Wednesday, September 5, 2018 6:34 AM