2

I'm trying to use CertEnumCertificatesInStore() in the CryptoAPI to iterate through all the root certificates and encode them into PEM files for use with OpenSSL. I've found a few examples of this so it seems to be possible, however, the PCCERT_CONTEXT I get back for each certificate has an invalid pointer for pbCertEncoded and cbCertEncoded (buffer size) is always 0, but I do not feel this should be the case as examples use the encoded buffer to convert the certificate into other formats. Has anyone else run into this issue of getting an empty buffer or can see a step i'm missing?

I've verified I am actually getting the certificates with the CryptUIDlgViewContext() function. I feel like I'm missing something very basic. Basic code below:

HCERTSTORE hStore = CertOpenSystemStore(NULL, L"ROOT");

for ( PCCERT_CONTEXT pCertContext = CertEnumCertificatesInStore(hStore, NULL); pCertContext != NULL; pCertContext = CertEnumCertificatesInStore(hStore, pCertContext) )
    {

        // This shows the certificates fine
        CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT,  pCertContext, NULL, NULL, 0, NULL)

        // but
        // pCertContext->pbCertEncoded is a Bad Ptr and
        // pCertContext->cbCertEncoded is always 0

        // If i try
        TCHAR *OutString = NULL;
        DWORD Size = 0;
        DWORD lastError;
        BOOL success = CryptBinaryToString(pCertContext->pbCertEncoded, pCertContext->cbCertEncoded, CRYPT_STRING_BASE64,OutString, &Size); 

        if( !success )
        {
            // I get a invalid parameter error here.
            lastError = GetLastError();
        }        
    }
Sid Brown
  • 21
  • 4
  • I was thinking it's just a basic setup / use of the CryptoApi but you're right. Always add code! – Sid Brown Sep 24 '12 at 14:11
  • Also, this is basically what i'm trying to do in the long run. http://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store – Sid Brown Sep 24 '12 at 14:51

1 Answers1

0

The encoded buffer does not get filled out when compiled in 64-bit. Compiling in 32 bit seems to solve this problem.

Sid Brown
  • 21
  • 4